Minor changes.
[egate.git] / kernel / urp.c
index 29b0e62..740d43e 100644 (file)
@@ -2,7 +2,13 @@
  * Europagate, 1995
  *
  * $Log: urp.c,v $
- * Revision 1.6  1995/02/17 14:41:14  quinn
+ * 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
@@ -34,7 +40,6 @@
 #include <unistd.h>
 
 #include "kernel.h"
-#include <iso2709.h>
 
 #define LINE_MAX 256
 
@@ -55,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;
 }
 
@@ -201,16 +211,26 @@ 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;
 }
 
@@ -259,20 +279,189 @@ static int exec_base (struct ccl_token *list)
     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;
-    zass_record *pp;
     char num_str[20];
-    int num;
+    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;
-    
-    memcpy (num_str, list->name, list->len);
-    num_str[list->len] = '\0';
+    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)
@@ -281,14 +470,33 @@ static int exec_show (struct ccl_token *list)
     zp = zass_present(info.zass, "Default", 1, num);
     if (zp)
     {
-       fprintf(reply_fd, "Got %d records\n", zp->num);
-       for (pp = zp->records; pp; pp = pp->next)
+        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++)
        {
-           Iso2709Rec p = iso2709_cvt(pp->record);
-           iso2709_display(p, reply_fd);
-           iso2709_rm(p);
+#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;
 }
 
@@ -361,7 +569,7 @@ int urp (FILE *inf)
         else
             fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
                                                    "gw.msg.subject",
-                                                   "Your Z39.50 Query"));
+                                                   "Your Query"));
         fprintf (reply_fd, "\n");
     }
     else
@@ -375,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++;