1 /* Gateway kernel - Z39.50 Persistence
5 * Revision 1.5 1995/05/02 15:26:00 adam
6 * Monitor observes death of child (email kernel). The number
7 * of simultanous processes is controlled now. Email requests are
8 * queued if necessary. This scheme should only be forced if no kernels
11 * Revision 1.4 1995/04/20 16:10:46 adam
12 * Modified to work with non-blocking zass-api. Not using non-blocking
15 * Revision 1.3 1995/04/19 13:19:09 adam
16 * New command: account - for authentication.
18 * Revision 1.2 1995/04/19 10:46:19 adam
19 * Persistency works much better now. New command: status - history-like
21 * Revision 1.1 1995/04/19 07:31:10 adam
22 * First work on Z39.50 persistence.
36 static int fgetsx (char *buf, int size, FILE *inf)
40 if (!fgets (buf, size, inf))
42 if ((cp = strchr (buf, '\n')))
47 static int set_change;
49 static int obtain_set (ZASS zass, struct gw_user_set *set)
51 const struct zass_searchent *p;
53 p = zass_search (zass, set->rpn, set->name, set->database, NULL);
58 if (p->num != set->hits)
60 set->present_flag = 1;
62 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s researched", set->name);
66 static int obtain_sets (ZASS zass, struct ccl_rpn_node *rpn,
67 struct gw_user_set *sets)
69 struct gw_user_set *set;
78 if ((r=obtain_sets (zass, rpn->u.p[0], sets)))
80 return obtain_sets (zass, rpn->u.p[1], sets);
84 set = user_set_search (rpn->u.setname);
87 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s not there at all",
91 if (set->present_flag)
93 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s already there",
98 return obtain_set (zass, set);
101 const struct zass_searchent *zass_p_search (ZASS zass,
102 struct ccl_rpn_node *rpn,
103 const char *result_set,
104 const char *database,
105 struct gw_user_set *sets)
110 r = obtain_sets (zass, rpn, sets);
113 return zass_search (zass, rpn, (char*) result_set, (char*) database, NULL);
116 const struct zass_presentent *zass_p_present (ZASS zass,
117 const char *result_set, int offset, int number)
119 struct gw_user_set *set;
121 set = user_set_search (result_set);
124 if (!set->present_flag)
126 const struct zass_searchent *p;
128 p = zass_p_search (zass, set->rpn, result_set, set->database,
133 return zass_present (zass, (char*) result_set, offset, number, NULL);
136 struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
138 struct ccl_rpn_node *rpn;
139 struct ccl_rpn_attr **attrp;
140 int type, value, no_read;
143 if (!fgetsx (buf, 1024, inf))
145 rpn = malloc (sizeof (*rpn));
151 rpn->kind = CCL_RPN_AND;
152 rpn->u.p[0] = load_rpn (buf, inf);
153 rpn->u.p[1] = load_rpn (buf, inf);
156 rpn->kind = CCL_RPN_OR;
157 rpn->u.p[0] = load_rpn (buf, inf);
158 rpn->u.p[1] = load_rpn (buf, inf);
161 rpn->kind = CCL_RPN_NOT;
162 rpn->u.p[0] = load_rpn (buf, inf);
163 rpn->u.p[1] = load_rpn (buf, inf);
166 rpn->kind = CCL_RPN_PROX;
167 rpn->u.p[0] = load_rpn (buf, inf);
168 rpn->u.p[1] = load_rpn (buf, inf);
171 rpn->kind = CCL_RPN_TERM;
173 rpn->u.t.term = gw_strdup (buf+2);
174 attrp = &rpn->u.t.attr_list;
175 if (!fgetsx (buf, 1024, inf))
178 while (sscanf (cp, "%d %d%n", &type, &value, &no_read) > 1)
180 *attrp = malloc (sizeof(**attrp));
181 (*attrp)->type = type;
182 (*attrp)->value = value;
183 attrp = &(*attrp)->next;
189 rpn->kind = CCL_RPN_SET;
190 rpn->u.setname = gw_strdup (buf+2);
199 int load_p_state (int userid)
207 struct gw_user_set *set;
209 sprintf (fname, "persist.%d", userid);
211 inf = fopen (fname, "r");
214 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG,
215 "Couldn't open %s", fname);
218 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
219 "Reading persistence file %s", fname);
221 if (!fgetsx (fline, 1024, inf))
223 if (sscanf (fline, "%s", info.target) != 1)
227 if (!fgetsx (fline, 1024, inf))
229 if (sscanf (fline, "%s", info.account) != 1)
230 *info.account = '\0';
232 if (!fgetsx (fline, 1024, inf))
234 free (info.database);
235 info.database = gw_strdup (fline);
237 if (!fgetsx (fline, 1024, inf))
239 if (sscanf (fline, "%d", &info.setno) != 1)
241 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
242 "Reading persistence file %s (2)", fname);
246 while (fgetsx (fline, 1024, inf))
248 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
249 "Reading persistence file %s (3)", fname);
250 if (sscanf (fline, "%s %d %s", resultname, &hits, database) != 3)
252 if (!fgetsx (fline, 1024, inf)) /* search string */
254 gw_log (GW_LOG_DEBUG, KERNEL_LOG,
255 "Adding %s, %d hits, database %s",
256 resultname, hits, database);
257 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Search string %s", fline);
258 set = user_set_add (resultname, hits, database, NULL, 0, fline);
259 set->rpn = load_rpn (fline, inf);
260 ccl_pr_tree (set->rpn, stderr);
261 fgetsx (fline, 1024, inf);
264 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Finished reading %s", fname);
268 static void save_rpn (struct ccl_rpn_node *rpn, FILE *of)
270 struct ccl_rpn_attr *attr;
276 save_rpn (rpn->u.p[0], of);
277 save_rpn (rpn->u.p[1], of);
281 save_rpn (rpn->u.p[0], of);
282 save_rpn (rpn->u.p[1], of);
286 save_rpn (rpn->u.p[0], of);
287 save_rpn (rpn->u.p[1], of);
291 save_rpn (rpn->u.p[0], of);
292 save_rpn (rpn->u.p[1], of);
295 fprintf (of, "T %s\n", rpn->u.t.term);
296 for (attr = rpn->u.t.attr_list; attr; attr = attr->next)
297 fprintf (of, "%d %d ", attr->type, attr->value);
301 fprintf (of, "S %s\n", rpn->u.setname);
305 static void save_sets (FILE *of, struct gw_user_set *sp)
309 save_sets (of, sp->prev);
310 fprintf (of, "%s %d %s\n%s\n", sp->name, sp->hits, sp->database,
312 save_rpn (sp->rpn, of);
316 int save_p_state (int userid)
321 sprintf (fname, "persist.%d", userid);
323 of = fopen (fname, "w");
326 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG,
327 "Couldn't open %s", fname);
330 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Writing persistence file %s", fname);
331 fprintf (of, "%s\n%s\n%s\n%d\n", info.target, info.account,
332 info.database, info.setno);
333 save_sets (of, info.sets);