+/* Gateway kernel - Z39.50 Persistence
+ * Europagate, 1995
+ *
+ * $Log: persist.c,v $
+ * Revision 1.1 1995/04/19 07:31:10 adam
+ * First work on Z39.50 persistence.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "kernel.h"
+
+static int set_change;
+
+static int obtain_set (ZASS zass, struct gw_user_set *set)
+{
+ const struct zass_searchent *p;
+
+ p = zass_search (zass, set->rpn, set->name, set->database);
+ if (!p)
+ return 2;
+ if (p->errcode != -1)
+ return 3;
+ if (p->num != set->hits)
+ set_change = 1;
+ set->present_flag = 1;
+ set->hits = p->num;
+ return 0;
+}
+
+static int obtain_sets (ZASS zass, struct ccl_rpn_node *rpn,
+ struct gw_user_set *sets)
+{
+ struct gw_user_set *set;
+ int r;
+
+ switch (rpn->kind)
+ {
+ case CCL_RPN_AND:
+ case CCL_RPN_OR:
+ case CCL_RPN_NOT:
+ case CCL_RPN_PROX:
+ if ((r=obtain_sets (zass, rpn->u.p[0], sets)))
+ return r;
+ return obtain_sets (zass, rpn->u.p[1], sets);
+ case CCL_RPN_TERM:
+ return 0;
+ case CCL_RPN_SET:
+ set = user_set_search (rpn->u.setname);
+ if (!set)
+ return 1;
+ if (set->present_flag)
+ return 0;
+ }
+ return obtain_set (zass, set);
+}
+
+const struct zass_searchent *zass_p_search (ZASS zass,
+ struct ccl_rpn_node *rpn,
+ const char *result_set,
+ const char *database,
+ struct gw_user_set *sets)
+{
+ int r;
+
+ set_change = 0;
+ r = obtain_sets (zass, rpn, sets);
+ if (r)
+ return NULL;
+ return zass_search (zass, rpn, (char*) result_set, (char*) database);
+}
+
+const struct zass_presentent *zass_p_present (ZASS zass,
+ const char *result_set, int offset, int number)
+{
+ struct gw_user_set *set;
+
+ set = user_set_search (result_set);
+ if (!set)
+ return NULL;
+ if (!set->present_flag)
+ {
+ int r = obtain_set (zass, set);
+ if (r)
+ return NULL;
+ }
+ return zass_present (zass, (char*) result_set, offset, number);
+}
+
+struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
+{
+ struct ccl_rpn_node *rpn;
+ struct ccl_rpn_attr **attrp;
+ char *cp;
+ int type, value, no_read;
+
+ if (!fgets (buf, 1024, inf))
+ return NULL;
+ if ((cp = strchr (buf, '\n')))
+ *cp = '\0';
+ switch (*buf)
+ {
+ case 'A':
+ rpn = malloc (sizeof (*rpn));
+ if (!rpn)
+ return NULL;
+ rpn->kind = CCL_RPN_AND;
+ rpn->u.p[0] = load_rpn (buf, inf);
+ rpn->u.p[1] = load_rpn (buf, inf);
+ break;
+ case 'O':
+ rpn = malloc (sizeof (*rpn));
+ if (!rpn)
+ return NULL;
+ rpn->kind = CCL_RPN_OR;
+ rpn->u.p[0] = load_rpn (buf, inf);
+ rpn->u.p[1] = load_rpn (buf, inf);
+ break;
+ case 'N':
+ rpn = malloc (sizeof (*rpn));
+ if (!rpn)
+ return NULL;
+ rpn->kind = CCL_RPN_NOT;
+ rpn->u.p[0] = load_rpn (buf, inf);
+ rpn->u.p[1] = load_rpn (buf, inf);
+ break;
+ case 'P':
+ rpn = malloc (sizeof (*rpn));
+ if (!rpn)
+ return NULL;
+ rpn->kind = CCL_RPN_PROX;
+ rpn->u.p[0] = load_rpn (buf, inf);
+ rpn->u.p[1] = load_rpn (buf, inf);
+ break;
+ case 'T':
+ rpn = malloc (sizeof (*rpn));
+ if (!rpn)
+ return NULL;
+ rpn->kind = CCL_RPN_TERM;
+
+ rpn->u.t.term = gw_strdup (buf+2);
+ attrp = &rpn->u.t.attr_list;
+ if (!fgets (buf, 1024, inf))
+ return NULL;
+ cp = buf;
+ while (sscanf (cp, "%d %d%n", &type, &value, &no_read) > 1)
+ {
+ *attrp = malloc (sizeof(**attrp));
+ (*attrp)->type = type;
+ (*attrp)->value = value;
+ attrp = &(*attrp)->next;
+ cp += no_read;
+ }
+ *attrp = NULL;
+ break;
+ case 'S':
+ rpn = malloc (sizeof (*rpn));
+ if (!rpn)
+ return NULL;
+ rpn->kind = CCL_RPN_SET;
+ rpn->u.setname = gw_strdup (buf+2);
+ }
+ return rpn;
+}
+
+int load_p_state (int userid)
+{
+ FILE *inf;
+ char fname[128];
+ char fline[1025];
+ char database[1024];
+ char resultname[32];
+ int hits;
+ struct gw_user_set *set;
+
+ sprintf (fname, "persist.%d", userid);
+
+ inf = fopen (fname, "r");
+ if (!inf)
+ {
+ gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG,
+ "Couldn't open %s", fname);
+ return -1;
+ }
+ gw_log (GW_LOG_FATAL, KERNEL_LOG,
+ "Reading persistence file %s", fname);
+ if (!fgets (fline, 1024, inf))
+ return -1;
+ if (sscanf (fline, "%s", info.target) != 1)
+ *info.target = '\0';
+ gw_log (GW_LOG_FATAL, KERNEL_LOG,
+ "Reading persistence file %s (2)", fname);
+ read_kernel_res ();
+ gw_log (GW_LOG_FATAL, KERNEL_LOG, "reopen_target start");
+ reopen_target ();
+ gw_log (GW_LOG_FATAL, KERNEL_LOG, "reopen_target end");
+ while (fgets (fline, 1024, inf))
+ {
+ gw_log (GW_LOG_FATAL, KERNEL_LOG,
+ "Reading persistence file %s (3)", fname);
+ if (sscanf (fline, "%s %d %s", resultname, &hits, database) != 3)
+ return -1;
+ gw_log (GW_LOG_FATAL, KERNEL_LOG,
+ "Adding %s, %d hits, database %s",
+ resultname, hits, database);
+ set = user_set_add (resultname, hits, database, NULL, 0);
+ set->rpn = load_rpn (fline, inf);
+ ccl_pr_tree (set->rpn, stderr);
+ fgets (fline, 1024, inf);
+ }
+ fclose (inf);
+ return 0;
+}
+
+static void save_rpn (struct ccl_rpn_node *rpn, FILE *of)
+{
+ struct ccl_rpn_attr *attr;
+
+ switch (rpn->kind)
+ {
+ case CCL_RPN_AND:
+ fprintf (of, "A\n");
+ save_rpn (rpn->u.p[0], of);
+ save_rpn (rpn->u.p[1], of);
+ break;
+ case CCL_RPN_OR:
+ fprintf (of, "O\n");
+ save_rpn (rpn->u.p[0], of);
+ save_rpn (rpn->u.p[1], of);
+ break;
+ case CCL_RPN_NOT:
+ fprintf (of, "N\n");
+ save_rpn (rpn->u.p[0], of);
+ save_rpn (rpn->u.p[1], of);
+ break;
+ case CCL_RPN_PROX:
+ fprintf (of, "P\n");
+ save_rpn (rpn->u.p[0], of);
+ save_rpn (rpn->u.p[1], of);
+ break;
+ case CCL_RPN_TERM:
+ fprintf (of, "T %s\n", rpn->u.t.term);
+ for (attr = rpn->u.t.attr_list; attr; attr = attr->next)
+ fprintf (of, "%d %d ", attr->type, attr->value);
+ fprintf (of, "\n");
+ break;
+ case CCL_RPN_SET:
+ fprintf (of, "S %s\n", rpn->u.setname);
+ }
+}
+
+int save_p_state (int userid)
+{
+ FILE *of;
+ char fname[128];
+ struct gw_user_set *sp;
+
+ sprintf (fname, "persist.%d", userid);
+
+ of = fopen (fname, "w");
+ if (!of)
+ {
+ gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG,
+ "Couldn't open %s", fname);
+ return -1;
+ }
+ gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Writing persistence file %s", fname);
+ fprintf (of, "%s\n", info.target);
+ for (sp = info.sets; sp; sp = sp->prev)
+ {
+ fprintf (of, "%s %d %s\n", sp->name, sp->hits, sp->database);
+ save_rpn (sp->rpn, of);
+ fprintf (of, "X\n");
+ }
+ fclose (of);
+ return 0;
+}
+