Minor changes.
[egate.git] / kernel / urp.c
index d047cac..740d43e 100644 (file)
@@ -2,7 +2,22 @@
  * Europagate, 1995
  *
  * $Log: urp.c,v $
- * Revision 1.3  1995/02/16 18:35:09  adam
+ * Revision 1.8  1995/02/21 12:12:00  adam
+ * Diagnostic record with error info. observed.
+ *
+ * Revision 1.7  1995/02/20  21:16:20  adam
+ * FML support. Bug fixes. Profile for drewdb.
+ *
+ * Revision 1.6  1995/02/17  14:41:14  quinn
+ * Added simple display of records.
+ *
+ * Revision 1.5  1995/02/17  14:22:13  adam
+ * First steps of CCL show command. Not finished yet.
+ *
+ * Revision 1.4  1995/02/17  09:08:36  adam
+ * Reply with subject. CCL base command implemented.
+ *
+ * Revision 1.3  1995/02/16  18:35:09  adam
  * First use of Zdist library. Search requests are supported.
  * Present requests are not supported yet.
  *
@@ -22,6 +37,7 @@
 #include <assert.h>
 #include <ctype.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "kernel.h"
 
@@ -44,10 +60,15 @@ static int reopen_target (void)
     v = gw_res_get (info.kernel_res, "gw.description", NULL);
     if (v)
         fprintf (reply_fd, "%s\n", v);
-    fprintf (reply_fd, "%s %s:%d\n %s\n",
-             gw_res_get (info.kernel_res, "gw.msg.databases",
-                         "Available databases on"),
-             info.hostname, info.port, info.databases);
+    fprintf (reply_fd, "%s %s:%d\n",
+             gw_res_get (info.kernel_res, "gw.msg.connect",
+                         "Connection established to"),
+             info.hostname, info.port);
+    if (*info.databases)
+        fprintf (reply_fd, "%s:\n%s\n",
+                 gw_res_get (info.kernel_res, "gw.msg.databases",
+                             "Available databases"),
+                 info.databases);
     return 0;
 }
 
@@ -141,15 +162,19 @@ static char *error_no_search (struct error_no_struct *tab, int no)
     return NULL;
 }
 
-static int email_header (FILE *inf, char *from_str)
+static int email_header (FILE *inf, char *from_str, char *subject_str)
 {
     *from_str = '\0';
+    *subject_str = '\0';
     while (fgets (line_buf, LINE_MAX, inf))
     {
         if (line_buf[0] == '\n')
             return 0;
         if (strncmp (line_buf, "From ", 5) == 0)
             sscanf (line_buf+4, "%s", from_str);
+        if (strncmp (line_buf, "Subject: ", 9) == 0 &&
+            sscanf (line_buf+9, "%s", subject_str+1) == 1)
+            strcpy (subject_str, line_buf+9);
     }
     return 1;
 }
@@ -186,30 +211,295 @@ static int exec_find (struct ccl_token *list)
 
     if (!info.zass)
         return -2;
+    if (!*info.databases)
+    {
+        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);
     p = zass_search (info.zass, rpn, "Default", info.databases);
     if (!p)
         return -1;
-    fprintf (reply_fd, "%d %s\n", p->num,
-             gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
     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);
+    else
+        fprintf (reply_fd, "%d %s\n", p->num,
+                 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
     return 0;
 }
 
 static int exec_target (struct ccl_token *list)
 {
+    int len;
     if (list->kind == CCL_TOK_EOL)
         return -1;
-    memcpy (info.target, list->name, list->len);
-    info.target [list->len] = '\0';
+    len = list->len;
+    memcpy (info.target, list->name, len);
+    info.target [len] = '\0';
 
     read_kernel_res ();
     return reopen_target ();
 }
 
+static int exec_base (struct ccl_token *list)
+{
+    struct ccl_token *li = list;
+    int len = 0;
+
+    if (list->kind == CCL_TOK_EOL)
+        return -1;
+    free (info.databases);
+    while (li->kind != CCL_TOK_EOL)
+    {
+        len += li->len + 1;
+        li = li->next;
+        if (li->kind == CCL_TOK_COMMA)
+            li = li->next;
+    }
+    info.databases = malloc (len);
+    assert (info.databases);
+    len = 0;
+    li = list;
+    while (li->kind != CCL_TOK_EOL)
+    {
+        memcpy (info.databases+len, li->name, li->len);
+        len += li->len;
+        info.databases[len++] = ',';
+        li = li->next;
+        if (li->kind == CCL_TOK_COMMA)
+            li = li->next;
+    }
+    info.databases[len-1] = '\0';
+    return 0;
+}
+
+struct command_word show_tab [] = 
+{
+{   "f", "format"},
+{   "p", "position"},
+{   NULL, NULL }
+};
+
+static int exec_show (struct ccl_token *list)
+{
+    const struct zass_presentent *zp;
+    char num_str[20];
+    struct ccl_token *set_token = NULL;
+    struct ccl_token *format_token = NULL;
+    struct ccl_token *li = list;
+
+    if (list->kind == CCL_TOK_EOL)
+        return -1;
+    if (!info.zass)
+        return -2;
+    while (li->kind != CCL_TOK_EOL)
+    {
+        int modifier_no = 0;
+        if (li->next->kind == CCL_TOK_EQ)
+        {
+            if (li->kind == CCL_TOK_SET)    /* set = <name> ? */
+            {
+                li = li->next->next;
+                set_token = li;
+            }
+            else 
+            {
+                modifier_no = command_search (show_tab, li, "ccl.token.");
+                if (!modifier_no)
+                {
+                    fprintf (reply_fd, "Unknown modifier in show\n");
+                    return -1;
+                }
+                li = li->next->next;
+                if (modifier_no == 1)       /* f = <name> ? */
+                    format_token = li;
+                else if (modifier_no == 2)  /* p = <name> ? */
+                {
+                    if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
+                        && li->next->kind == CCL_TOK_MINUS
+                        && li->next->next != CCL_TOK_EOL)
+                        li = li->next->next;
+                }
+            }
+            li = li->next;
+        }
+        else
+            li = li->next;
+    }
+    if (set_token)
+        gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
+                set_token->name);
+    if (format_token)
+        gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
+                format_token->name);
+
+    li = list;
+    while (li->kind != CCL_TOK_EOL)
+    {
+        int modifier_no = 0;
+        int offset = 0;
+        int number = 0;
+        int len;
+        if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
+        {
+            modifier_no = command_search (show_tab, li, "ccl.token.");
+            li = li->next->next;
+            if (modifier_no == 2)  /* p = <name> ? */
+            {
+                if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
+                    && li->next->kind == CCL_TOK_MINUS
+                    && li->next->next != CCL_TOK_EOL)
+                {
+                    len = li->len;
+                    memcpy (num_str, li->name, len);
+                    num_str [len] = '\0';
+                    offset = atoi (num_str);
+                    li = li->next->next;
+
+                    len = li->len;
+                    memcpy (num_str, li->name, len);
+                    num_str [len] = '\0';
+                    number = atoi (num_str) - offset + 1;
+                }
+                else
+                {
+                    len = li->len;
+                    memcpy (num_str, li->name, len);
+                    num_str [len] = '\0';
+                    offset = atoi (num_str);
+                    number = 1;
+                }
+            }
+            li = li->next;
+        }
+        else
+        {
+            len = li->len;
+            memcpy (num_str, li->name, len);
+            num_str[len] = '\0';
+            number = atoi (num_str);
+            offset = 1;
+            li = li->next;
+        }
+        if (offset > 0 && number > 0)
+        {
+            if (set_token)
+            {
+                len = set_token->len;
+                memcpy (num_str, set_token->name, len);
+                num_str[len] = '\0';
+            }
+            else
+                strcpy (num_str, "Default");
+            gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records from"
+                    " offset %d in set %s", number, offset, num_str);
+            zp = zass_present(info.zass, num_str, offset, number);
+            if (zp)
+            {
+                int i;
+                zass_record *pp;
+                
+                fprintf (reply_fd, gw_res_get (info.kernel_res,
+                                               "gw.msg.records",
+                                               "Got %d records"),
+                         zp->num);
+                fprintf (reply_fd, "\n");
+                for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
+                {
+                    Iso2709Rec rec;
+#if USE_FML
+                    const char *arg_ar[5];
+#endif
+                    fprintf (reply_fd, "--- %d/%d ---\n",
+                             i+offset, offset+zp->num);
+                    if (pp->which == ZASS_REC_DIAG)
+                    {
+                        fprintf (reply_fd, "Record error %d: %s\n",
+                                 pp->errcode, pp->errstring);
+                        continue;
+                    }
+                    else if (pp->which != ZASS_REC_USMARC)
+                    {
+                        fprintf (reply_fd, "Unknown record kind %d\n",
+                                 pp->which);
+                        continue;
+                    }
+                    rec = iso2709_cvt (pp->record);
+#if USE_FML
+                    if (format_token)
+                    {
+                        len = format_token->len;
+                        memcpy (num_str, format_token->name, len);
+                        num_str[len] = '\0';
+                    }
+                    if (format_token && 
+                        (!strcmp (num_str, "0") || !strcmp (num_str, "1")))
+                    {
+                        arg_ar[0] = "\\f";
+                        arg_ar[1] = num_str;
+                        arg_ar[2] = " \\list";
+                        arg_ar[3] = marc_to_str (info.fml, rec);
+                        arg_ar[4] = NULL;
+                        fml_exec_call_argv (info.fml, arg_ar);
+                    }
+                    else
+                        iso2709_display (rec, reply_fd);
+#else
+                    iso2709_display (rec, reply_fd);
+#endif
+                    iso2709_rm (rec);
+                }
+            }
+        }
+    }
+#if 0
+    len = list->len;
+    memcpy (num_str, list->name, len);
+    num_str[len] = '\0';
+
+    num = atoi (num_str);
+    if (!num)
+        return -3;
+    gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
+    zp = zass_present(info.zass, "Default", 1, num);
+    if (zp)
+    {
+        int i;
+        zass_record *pp;
+
+       fprintf (reply_fd, gw_res_get (info.kernel_res,
+                                       "gw.msg.records", "Got %d records"),
+                 zp->num);
+        fprintf (reply_fd, "\n");
+       for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
+       {
+#if USE_FML
+            const char *arg_ar[3];
+#endif
+           Iso2709Rec rec = iso2709_cvt (pp->record);
+
+            fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
+#if USE_FML
+            arg_ar[0] = "\\f0 \\list";
+            arg_ar[1] = marc_to_str (info.fml, rec);
+            arg_ar[2] = NULL;
+            fml_exec_call_argv (info.fml, arg_ar);
+#else
+           iso2709_display (rec, reply_fd);
+#endif
+           iso2709_rm (rec);
+       }
+    }
+#endif
+    return 0;
+}
+
 static int exec_command (const char *str)
 {
     struct ccl_token *cmd = ccl_tokenize (str);
@@ -225,10 +515,12 @@ static int exec_command (const char *str)
         {
         case 1:
             return exec_find (cmd->next);
-            break;
+        case 2:
+            return exec_show (cmd->next);
+        case 3:
+            return exec_base (cmd->next);
         case 9:
             return exec_target (cmd->next);
-            break;
         default:
             fprintf (reply_fd, "%s\n",
                      gw_res_get (info.kernel_res, "gw.err.unimplemented",
@@ -237,6 +529,7 @@ static int exec_command (const char *str)
     }
     else
     {
+        fprintf (reply_fd, "\n> %s", str);
         fprintf (reply_fd, "  ^ %s\n", 
                  gw_res_get (info.kernel_res, "gw.err.unknown.command",
                              "unknown command"));
@@ -247,10 +540,11 @@ static int exec_command (const char *str)
 int urp (FILE *inf)
 {
     char from_str[128];
+    char subject_str[128];
     int command_no = 0;
     char *reply_fname = NULL;
 
-    if (email_header (inf, from_str))
+    if (email_header (inf, from_str, subject_str))
     {
         gw_log (GW_LOG_WARN, "urp", "No message body");
         return -1;
@@ -269,6 +563,14 @@ int urp (FILE *inf)
                     reply_fname);
             return -1;
         }
+        fprintf (reply_fd, "Subject: ");
+        if (*subject_str)
+            fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
+        else
+            fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
+                                                   "gw.msg.subject",
+                                                   "Your Query"));
+        fprintf (reply_fd, "\n");
     }
     else
         gw_log (GW_LOG_WARN, "urp", "No From in email header");
@@ -281,7 +583,7 @@ int urp (FILE *inf)
         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");       
+        ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
         if (isalpha (line_buf[0]))
             exec_command (line_buf);
         command_no++;