1 /* Gateway kernel - Z39.50 Persistence
5 * Revision 1.4 1995/04/20 16:10:46 adam
6 * Modified to work with non-blocking zass-api. Not using non-blocking
9 * Revision 1.3 1995/04/19 13:19:09 adam
10 * New command: account - for authentication.
12 * Revision 1.2 1995/04/19 10:46:19 adam
13 * Persistency works much better now. New command: status - history-like
15 * Revision 1.1 1995/04/19 07:31:10 adam
16 * First work on Z39.50 persistence.
30 static int fgetsx (char *buf, int size, FILE *inf)
34 if (!fgets (buf, size, inf))
36 if ((cp = strchr (buf, '\n')))
41 static int set_change;
43 static int obtain_set (ZASS zass, struct gw_user_set *set)
45 const struct zass_searchent *p;
47 p = zass_search (zass, set->rpn, set->name, set->database, NULL);
52 if (p->num != set->hits)
54 set->present_flag = 1;
56 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s researched", set->name);
60 static int obtain_sets (ZASS zass, struct ccl_rpn_node *rpn,
61 struct gw_user_set *sets)
63 struct gw_user_set *set;
72 if ((r=obtain_sets (zass, rpn->u.p[0], sets)))
74 return obtain_sets (zass, rpn->u.p[1], sets);
78 set = user_set_search (rpn->u.setname);
81 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s not there at all",
85 if (set->present_flag)
87 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s already there",
92 return obtain_set (zass, set);
95 const struct zass_searchent *zass_p_search (ZASS zass,
96 struct ccl_rpn_node *rpn,
97 const char *result_set,
99 struct gw_user_set *sets)
104 r = obtain_sets (zass, rpn, sets);
107 return zass_search (zass, rpn, (char*) result_set, (char*) database, NULL);
110 const struct zass_presentent *zass_p_present (ZASS zass,
111 const char *result_set, int offset, int number)
113 struct gw_user_set *set;
115 set = user_set_search (result_set);
118 if (!set->present_flag)
120 const struct zass_searchent *p;
122 p = zass_p_search (zass, set->rpn, result_set, set->database,
127 return zass_present (zass, (char*) result_set, offset, number, NULL);
130 struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
132 struct ccl_rpn_node *rpn;
133 struct ccl_rpn_attr **attrp;
134 int type, value, no_read;
137 if (!fgetsx (buf, 1024, inf))
139 rpn = malloc (sizeof (*rpn));
145 rpn->kind = CCL_RPN_AND;
146 rpn->u.p[0] = load_rpn (buf, inf);
147 rpn->u.p[1] = load_rpn (buf, inf);
150 rpn->kind = CCL_RPN_OR;
151 rpn->u.p[0] = load_rpn (buf, inf);
152 rpn->u.p[1] = load_rpn (buf, inf);
155 rpn->kind = CCL_RPN_NOT;
156 rpn->u.p[0] = load_rpn (buf, inf);
157 rpn->u.p[1] = load_rpn (buf, inf);
160 rpn->kind = CCL_RPN_PROX;
161 rpn->u.p[0] = load_rpn (buf, inf);
162 rpn->u.p[1] = load_rpn (buf, inf);
165 rpn->kind = CCL_RPN_TERM;
167 rpn->u.t.term = gw_strdup (buf+2);
168 attrp = &rpn->u.t.attr_list;
169 if (!fgetsx (buf, 1024, inf))
172 while (sscanf (cp, "%d %d%n", &type, &value, &no_read) > 1)
174 *attrp = malloc (sizeof(**attrp));
175 (*attrp)->type = type;
176 (*attrp)->value = value;
177 attrp = &(*attrp)->next;
183 rpn->kind = CCL_RPN_SET;
184 rpn->u.setname = gw_strdup (buf+2);
193 int load_p_state (int userid)
201 struct gw_user_set *set;
203 sprintf (fname, "persist.%d", userid);
205 inf = fopen (fname, "r");
208 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG,
209 "Couldn't open %s", fname);
212 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
213 "Reading persistence file %s", fname);
215 if (!fgetsx (fline, 1024, inf))
217 if (sscanf (fline, "%s", info.target) != 1)
221 if (!fgetsx (fline, 1024, inf))
223 if (sscanf (fline, "%s", info.account) != 1)
224 *info.account = '\0';
226 if (!fgetsx (fline, 1024, inf))
228 free (info.database);
229 info.database = gw_strdup (fline);
231 if (!fgetsx (fline, 1024, inf))
233 if (sscanf (fline, "%d", &info.setno) != 1)
235 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
236 "Reading persistence file %s (2)", fname);
238 while (fgetsx (fline, 1024, inf))
240 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
241 "Reading persistence file %s (3)", fname);
242 if (sscanf (fline, "%s %d %s", resultname, &hits, database) != 3)
244 if (!fgetsx (fline, 1024, inf)) /* search string */
246 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
247 "Adding %s, %d hits, database %s",
248 resultname, hits, database);
249 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Search string %s", fline);
250 set = user_set_add (resultname, hits, database, NULL, 0, fline);
251 set->rpn = load_rpn (fline, inf);
252 ccl_pr_tree (set->rpn, stderr);
253 fgetsx (fline, 1024, inf);
256 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Finished reading %s", fname);
260 static void save_rpn (struct ccl_rpn_node *rpn, FILE *of)
262 struct ccl_rpn_attr *attr;
268 save_rpn (rpn->u.p[0], of);
269 save_rpn (rpn->u.p[1], of);
273 save_rpn (rpn->u.p[0], of);
274 save_rpn (rpn->u.p[1], of);
278 save_rpn (rpn->u.p[0], of);
279 save_rpn (rpn->u.p[1], of);
283 save_rpn (rpn->u.p[0], of);
284 save_rpn (rpn->u.p[1], of);
287 fprintf (of, "T %s\n", rpn->u.t.term);
288 for (attr = rpn->u.t.attr_list; attr; attr = attr->next)
289 fprintf (of, "%d %d ", attr->type, attr->value);
293 fprintf (of, "S %s\n", rpn->u.setname);
297 static void save_sets (FILE *of, struct gw_user_set *sp)
301 save_sets (of, sp->prev);
302 fprintf (of, "%s %d %s\n%s\n", sp->name, sp->hits, sp->database,
304 save_rpn (sp->rpn, of);
308 int save_p_state (int userid)
313 sprintf (fname, "persist.%d", userid);
315 of = fopen (fname, "w");
318 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG,
319 "Couldn't open %s", fname);
322 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Writing persistence file %s", fname);
323 fprintf (of, "%s\n%s\n%s\n%d\n", info.target, info.account,
324 info.database, info.setno);
325 save_sets (of, info.sets);