First work on Z39.50 persistence.
[egate.git] / kernel / urp.c
index 5515208..032392c 100644 (file)
@@ -1,8 +1,43 @@
-/* Gateway kernel
+/* Gateway kernel - User Request Processor
  * Europagate, 1995
  *
  * $Log: urp.c,v $
- * Revision 1.15  1995/02/23 08:32:17  adam
+ * Revision 1.26  1995/04/19 07:31:12  adam
+ * First work on Z39.50 persistence.
+ *
+ * Revision 1.25  1995/04/17  09:34:33  adam
+ * Timeout (idletime) adjustable. Minor changes in kernel.
+ *
+ * Revision 1.24  1995/03/28  11:42:35  adam
+ * First use of string-queue utility.
+ *
+ * Revision 1.23  1995/03/28  08:01:28  adam
+ * FIFO existence is used to test for a running kernel.
+ *
+ * Revision 1.22  1995/03/27  12:51:05  adam
+ * New log level in use: GW_LOG_ERRNO.
+ *
+ * Revision 1.21  1995/03/27  08:24:04  adam
+ * First use of gip interface and gw-db.
+ * First work on eti program.
+ *
+ * Revision 1.20  1995/03/03  17:19:17  adam
+ * Smarter presentation. Bug fix in email header interpretation.
+ *
+ * Revision 1.19  1995/03/02  09:32:11  adam
+ * New presentation formats. f0=full, f1=brief, f2=mid
+ *
+ * Revision 1.18  1995/03/01  14:32:26  adam
+ * Better diagnostics. Default is, that only one database selected when
+ * several are known.
+ *
+ * Revision 1.17  1995/02/28  13:16:26  adam
+ * Configurable From: added.
+ *
+ * Revision 1.16  1995/02/23  10:08:20  adam
+ * Added logging of all user commands.
+ *
+ * Revision 1.15  1995/02/23  08:32:17  adam
  * Changed header.
  *
  * Revision 1.13  1995/02/22  16:54:42  adam
 #include <ctype.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 
+#include <ttyemit.h>
+#include <strqueue.h>
 #include "kernel.h"
 
-#define LINE_MAX 256
+static char line_buf[LINE_MAX+1];
 
 static void put_esc_str (const char *s)
 {
-    int escape_flag = 0;
     while (*s)
+        tty_emit (*s++);
+}
+
+int lgets (char *buf, int max, int fd)
+{
+    int r, no = 0;
+
+    --max;
+    while (no <= max)
     {
-        if (*s == '\\' && s[1])
-        {
-            switch (*++s)
-            {
-            case 'n':
-                fputc ('\n', reply_fd);
-                break;
-            case 't':
-                fputc ('\t', reply_fd);
-                break;
-            default:
-                fputc (*s, reply_fd);
-                break;
-            }
-            escape_flag = 1;
-        }
-        else
-        {
-            if (*s != ' ' || !escape_flag)
-                fputc (*s, reply_fd);
-            escape_flag = 0;
-        }
-        s++;
+        if ((r=read (fd, buf+no, 1)) != 1)
+       {
+           if (r == -1)
+               gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
+           buf[no] = '\0';
+           return 0;
+       }
+       if (buf[no] == 1)
+            return 0;    
+       if (buf[no++] == '\n')
+           break;
     }
+    buf[no] = '\0';    
+    return 1;
 }
 
-static int reopen_target (void)
+int reopen_target (void)
 {
     const char *v;
     if (info.zass)
-        gw_log (GW_LOG_WARN, "urp", "Zass free...");
+        gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
     info.zass = zass_open (info.hostname, info.port);
     if (!info.zass)
     {
@@ -112,7 +148,10 @@ static int reopen_target (void)
     }
     v = gw_res_get (info.kernel_res, "gw.description", NULL);
     if (v)
-        fprintf (reply_fd, "%s\n", v);
+    {
+        put_esc_str (v);
+        fprintf (reply_fd, "\n");
+    }
     fprintf (reply_fd, "%s %s:%d\n",
              gw_res_get (info.kernel_res, "gw.msg.connect",
                          "Connection established to"),
@@ -122,10 +161,14 @@ static int reopen_target (void)
                  gw_res_get (info.kernel_res, "gw.msg.databases",
                              "Available databases"),
                  info.databases);
+    if (*info.database)
+        fprintf (reply_fd, "%s:\n%s\n",
+                 gw_res_get (info.kernel_res, "gw.msg.database",
+                             "Selected databases"),
+                 info.database);
     return 0;
 }
 
-static char line_buf[LINE_MAX+1];
 
 static struct command_word {
     char *default_value;
@@ -141,6 +184,7 @@ static struct command_word {
 {   "status", "status" },
 {   "cancel", "cancel" },
 {   "target", "target" },
+{   "stop",   "stop" },
 {   NULL, NULL }
 };
 
@@ -215,17 +259,18 @@ static char *error_no_search (struct error_no_struct *tab, int no)
     return NULL;
 }
 
-static int email_header (FILE *inf, char *from_str, char *subject_str)
+static int email_header (struct str_queue *sq,
+                         char *from_str, char *subject_str)
 {
     *from_str = '\0';
-    *subject_str = '\0';
-    while (fgets (line_buf, LINE_MAX, inf))
+    *subject_str = '\0';    
+    while (str_queue_deq (sq, line_buf, LINE_MAX))
     {
         if (line_buf[0] == '\n')
             return 0;
-        if (strncmp (line_buf, "From ", 5) == 0)
+        if (memcmp (line_buf, "From ", 5) == 0)
             sscanf (line_buf+4, "%s", from_str);
-        if (strncmp (line_buf, "Subject: ", 9) == 0 &&
+        if (memcmp (line_buf, "Subject: ", 9) == 0 &&
             sscanf (line_buf+9, "%s", subject_str+1) == 1)
             strcpy (subject_str, line_buf+9);
     }
@@ -240,37 +285,56 @@ static void help_general (void)
 
 static int exec_help (struct ccl_token *list)
 {
+    static char *sep = "-------------------------------\\n";
     help_general ();
 
-#if 0
+#if 1
+    put_esc_str (sep);
     put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
                              "target <name> - selects a given target\n"));
 
+    put_esc_str (sep);
     put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
                              "base <base>..  - selects databases\n"));
 
+    put_esc_str (sep);
     put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
                              "find <query>   - performs a search request\n"));
 
+    put_esc_str (sep);
     put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
                              "show <spec>    - retrieves and displays "
                              "records\n"));
-    put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
-                             "help           - displays help\n"));
 #endif
     return 0;
 }
 
+static void display_diag_error (int code, const char *addinfo)
+{
+    static char str[20];
+
+    sprintf (str, "gw.bib1.diag.%d", code);
+    fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
+             gw_res_get (info.kernel_res, "gw.msg.z39errcode", 
+                         "Z39.50 Error"),
+             code,
+             gw_res_get (info.kernel_res, str, ""), addinfo);
+}
+
 static int exec_find (struct ccl_token *list)
 {
     const struct zass_searchent *p;
     struct gw_user_set *us;
+    char setname[32];
 
     struct ccl_rpn_node *rpn;
     int error;
     const char *pos;
 
-    us = user_set_add ("Default", -1);
+    if (info.setno == -1)
+        strcpy (setname, "Default");
+    else
+        sprintf (setname, "%d", info.setno);
     rpn = ccl_find (info.bibset, list, &error, &pos);
     if (!rpn)
     {
@@ -293,30 +357,34 @@ static int exec_find (struct ccl_token *list)
     ccl_pr_tree (rpn, reply_fd);
     fprintf (reply_fd, "\n");
 
-    if (!*info.databases)
+    if (!*info.database )
     {
         fprintf (reply_fd, "%s\n",
                  gw_res_get (info.kernel_res, "gw.err.no.database",
                              "You must select database"));
         return -3;
     }
-    gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
-            info.databases);
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
+            info.database );
     assert (info.zass);
-    p = zass_search (info.zass, rpn, "Default", info.databases);
+    fprintf (reply_fd, "Searching in database %s\n", info.database);
+    p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
     if (!p)
+    {
+        fprintf (reply_fd, "Search fail\n");
         return -1;
+    }
     if (p->errcode != -1)
     {
-        fprintf (reply_fd, "%s %d: %s\n",
-                 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
-                             "Z39.50 error code"),
-                 p->errcode, p->errstring);
+        display_diag_error (p->errcode, p->errstring);
         return -2;
     }
     fprintf (reply_fd, "%d %s\n", p->num,
              gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
-    us->hits = p->num;
+    us = user_set_add (setname, p->num, info.database, rpn, 1);
+    fprintf (reply_fd, "Result-set %s created\n", setname);
+    if (info.setno >= 0)
+        info.setno++;
     return 0;
 }
 
@@ -341,7 +409,7 @@ static int exec_base (struct ccl_token *list)
     assert (info.zass);
     if (list->kind == CCL_TOK_EOL)
         return -1;
-    free (info.databases);
+    free (info.database);
     while (li->kind != CCL_TOK_EOL)
     {
         len += li->len + 1;
@@ -349,20 +417,20 @@ static int exec_base (struct ccl_token *list)
         if (li->kind == CCL_TOK_COMMA)
             li = li->next;
     }
-    info.databases = malloc (len);
-    assert (info.databases);
+    info.database  = malloc (len);
+    assert (info.database );
     len = 0;
     li = list;
     while (li->kind != CCL_TOK_EOL)
     {
-        memcpy (info.databases+len, li->name, li->len);
+        memcpy (info.database+len, li->name, li->len);
         len += li->len;
-        info.databases[len++] = ',';
+        info.database[len++] = ',';
         li = li->next;
         if (li->kind == CCL_TOK_COMMA)
             li = li->next;
     }
-    info.databases[len-1] = '\0';
+    info.database[len-1] = '\0';
     return 0;
 }
 
@@ -379,21 +447,35 @@ static void present (const char *set, int offset, int number,
     const struct zass_presentent *zp;
     int len;
     int max_number;
-    char format_str[40];
+    char format_str[16];
     
     max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show", 
                                    "200"));
     if (number > max_number)
         number = max_number;
-    gw_log (GW_LOG_DEBUG, "urp", "present in set %s", set);
-    gw_log (GW_LOG_DEBUG, "urp", "present of %d records from offset %d",
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
             number, offset);
-    zp = zass_present(info.zass, (char *) set, offset, number);
+    zp = zass_p_present(info.zass, (char *) set, offset, number);
     if (zp)
     {
         int i;
         zass_record *pp;
-        
+        char path[128];
+        int  record_log_fd = -1;
+        const char *record_log_name;
+
+        record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
+                                      NULL);
+        if (record_log_name)
+        {
+            sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
+                                                "gw.path", "."),
+                     record_log_name );
+            record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
+            if (record_log_fd == -1)
+                gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
+        }
         fprintf (reply_fd, gw_res_get (info.kernel_res,
                                        "gw.msg.records",
                                        "Got %d records"),
@@ -411,8 +493,7 @@ static void present (const char *set, int offset, int number,
             {
                 if (pp->which == ZASS_REC_DIAG)
                 {
-                    fprintf (reply_fd, "Record error %d: %s\n",
-                             pp->errcode, pp->errstring);
+                    display_diag_error (pp->errcode, pp->errstring);
                     continue;
                 }
                 else if (pp->which != ZASS_REC_USMARC)
@@ -422,6 +503,8 @@ static void present (const char *set, int offset, int number,
                     continue;
                 }
             }
+            if (record_log_fd != -1)
+                write (record_log_fd, pp->record, strlen(pp->record));
             rec = iso2709_cvt (pp->record);
             if (rec)
             {
@@ -429,11 +512,14 @@ static void present (const char *set, int offset, int number,
                 if (format_token)
                 {
                     len = format_token->len;
+                   if (len >= sizeof(format_str))
+                       len = sizeof(format_str)-1;
                     memcpy (format_str, format_token->name, len);
                     format_str[len] = '\0';
                 }
                 if (info.fml && format_token && 
-                    (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
+                    (!strcmp (format_str, "0") || !strcmp (format_str, "1")
+                    || !strcmp(format_str, "2")))
                 {
                     arg_ar[0] = "\\f";
                     arg_ar[1] = format_str;
@@ -452,6 +538,8 @@ static void present (const char *set, int offset, int number,
             else
                 fprintf (reply_fd, "Not a MARC record\n");
         }
+        if (record_log_fd != -1)
+            close (record_log_fd);
     }
 }
 
@@ -504,10 +592,10 @@ static int exec_show (struct ccl_token *list)
             li = li->next;
     }
     if (set_token)
-        gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
+        gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
                 set_token->name);
     if (format_token)
-        gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
+        gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
                 format_token->name);
 
     li = list;
@@ -625,7 +713,7 @@ static int exec_command (const char *str)
     {
         if (!info.zass && no != 9 && no != 4)
             reopen_target ();
-        fprintf (reply_fd, "\n> %s", str);
+        fprintf (reply_fd, "\n> %s\n", str);
         if (!info.zass && (no == 1 || no == 2 || no == 3))
         {
             fprintf (reply_fd, "%s\n",
@@ -654,7 +742,7 @@ static int exec_command (const char *str)
     }
     else
     {
-        fprintf (reply_fd, "\n> %s", str);
+        fprintf (reply_fd, "\n> %s\n", str);
         fprintf (reply_fd, "  ^ %s\n", 
                  gw_res_get (info.kernel_res, "gw.err.unknown.command",
                              "unknown command. "
@@ -663,32 +751,36 @@ static int exec_command (const char *str)
     return 0;
 }
 
-int urp (FILE *inf)
+int urp_start (int continuation, struct str_queue *queue)
 {
-    char from_str[128];
     char subject_str[128];
-    int command_no = 0;
-    char *reply_fname = NULL;
 
-    if (email_header (inf, from_str, subject_str))
+    info.command_no = 0;
+    info.reply_fname = NULL;
+
+    if (email_header (queue, info.from_str, subject_str))
     {
-        gw_log (GW_LOG_WARN, "urp", "No message body");
+        gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
         return -1;
     }
-    if (*from_str)
+    tty_init (stdout, 40, 70);
+    if (*info.from_str)
     {
-        reply_fname = tempnam (gw_res_get (info.kernel_res,
+        info.reply_fname = tempnam (gw_res_get (info.kernel_res,
                                            "gw.reply.tmp.dir", NULL),
                                gw_res_get (info.kernel_res,
                                            "gw.reply.tmp.prefix", "gwr"));
                                                  
-        reply_fd = fopen (reply_fname, "w");
+        reply_fd = fopen (info.reply_fname, "w");
         if (!reply_fd)
         {
-            gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
-                    reply_fname);
+            gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
+                    info.reply_fname);
             return -1;
         }
+        tty_init (reply_fd, 0, 0);
+        fprintf (reply_fd, "From: %s\n",
+                 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
         fprintf (reply_fd, "Subject: ");
         if (*subject_str)
             fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
@@ -697,51 +789,77 @@ int urp (FILE *inf)
                                                    "gw.msg.subject",
                                                    "Your Query"));
         fprintf (reply_fd, "\n");
-        gw_log (GW_LOG_DEBUG, "urp", "Mail from %s", from_str);
+        gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
     }
     else
-        gw_log (GW_LOG_WARN, "urp", "No From in email header");
+        gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
     fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
                                            "Email->Z39.50 gateway"));
-    while (fgets (line_buf, LINE_MAX, inf))
+    if (continuation)
+        fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
+                                              "gw.msg.cont",
+                                              "Continued..."));
+    ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
+    ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
+    ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
+    ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
+    return 0;
+}
+
+int urp_command (struct str_queue *queue)
+{
+    char *cp;
+
+    while (str_queue_deq (queue, line_buf, LINE_MAX))
     {
         if (line_buf[0] == '\n')
-            break;
-        ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
-        ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
-        ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
-        ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
+            if (info.command_no)
+           {
+               while (str_queue_deq (queue, 0, 0))
+                   ;
+                break;
+           }
+            else 
+                continue;
+        if ((cp = strchr (line_buf, '\n')))
+            *cp = '\0';
+        gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
         if (isalpha (line_buf[0]))
             exec_command (line_buf);
-        command_no++;
+        info.command_no++;
     }
-    if (!command_no)
+    return 0;
+}
+
+void urp_end (void)
+{
+    if (!info.command_no)
     {
-        fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
-                                               "gw.err.nullbody",
-                                               "No body"));
+        fprintf (reply_fd, "%s\n", 
+                gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
         help_general ();
     }
-    if (*from_str)
+    if (*info.from_str)
     {
         const char *mta;
         char cmd[256];
         int mta_code;
 
-        assert (reply_fname);
+        assert (info.reply_fname);
         fclose (reply_fd);
         reply_fd = stdout;
 
         mta = gw_res_get (info.kernel_res, "gw.reply.mta",
-                          "/usr/lib/sendmail");
-        sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
+                         "/usr/lib/sendmail");
+        sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
         
         mta_code = system (cmd);
         if (mta_code)
-            gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
-                    cmd, mta_code);
-        unlink (reply_fname);        
-        gw_log (GW_LOG_DEBUG, "urp", "Normal session end of %s", from_str);
+            gw_log (GW_LOG_FATAL, KERNEL_LOG,
+                    "Reply '%s' got exit code %d", cmd, mta_code);
+       else
+            unlink (info.reply_fname);        
+        gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);
     }
-    return 0;
 }
+