CCL commands stop/continue implemented. New functions gw_res_{int,bool}
[egate.git] / kernel / urp.c
index a8956da..173f5b5 100644 (file)
@@ -2,7 +2,22 @@
  * Europagate, 1995
  *
  * $Log: urp.c,v $
- * Revision 1.28  1995/04/19 13:19:09  adam
+ * Revision 1.32  1995/05/03 07:37:46  adam
+ * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
+ * are used when possible.
+ *
+ * Revision 1.31  1995/05/01  12:43:38  adam
+ * First work on resource monitor program.
+ *
+ * Revision 1.30  1995/04/20  16:10:47  adam
+ * Modified to work with non-blocking zass-api. Not using non-blocking
+ * facility yet.
+ *
+ * Revision 1.29  1995/04/19  16:01:58  adam
+ * Some hacks to get the FIFO communication work!! Isn't reliable.
+ * Resource gw.account added - default account info.
+ *
+ * Revision 1.28  1995/04/19  13:19:09  adam
  * New command: account - for authentication.
  *
  * Revision 1.27  1995/04/19  10:46:19  adam
  *
  */
 
+/*
+   Todo:
+     stop/continue commands
+     info/status (other name?)
+     per-user definitions
+     better persistence diagnostics
+     show - position continuation
+     resource gw.path - use chdir call instead
+
+   Optional:
+     automatic information about target-aliases: name, query-support, etc.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <unistd.h>
 #include <fcntl.h>
 
+#include <lgets.h>
 #include <ttyemit.h>
 #include <strqueue.h>
 #include "kernel.h"
@@ -115,37 +144,14 @@ static void put_esc_str (const char *s)
         tty_emit (*s++);
 }
 
-int lgets (char *buf, int max, int fd)
-{
-    int r, no = 0;
-
-    --max;
-    while (no <= max)
-    {
-        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;
-}
-
 int reopen_target (void)
 {
     const char *v;
     if (info.zass)
         gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
-    info.zass = zass_open (info.hostname, info.port, *info.account ?
-                           info.account : NULL);
+    info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
+                           *info.account ? info.account : NULL);
     if (!info.zass)
     {
         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
@@ -189,17 +195,17 @@ static struct command_word {
     char *resource_suffix;
 } command_tab [] = 
 {
-{   "find", "find"},
-{   "show", "show"},
-{   "base", "base" },
-{   "help", "help" },
-{   "info", "info" },
+{   "find",     "find"},
+{   "show",     "show"},
+{   "base",     "base" },
+{   "help",     "help" },
+{   "info",     "info" },
 {   "continue", "continue" },
-{   "status", "status" },
-{   "cancel", "cancel" },
-{   "target", "target" },
-{   "stop",   "stop" },
-{   "account", "account" },
+{   "status",   "status" },
+{   "stop",     "stop" },
+{   "target",   "target" },
+{   "cancel",   "cancel" },
+{   "account",  "account" },
 {   NULL, NULL }
 };
 
@@ -303,7 +309,6 @@ static int exec_help (struct ccl_token *list)
     static char *sep = "-------------------------------\\n";
     help_general ();
 
-#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"));
@@ -320,7 +325,6 @@ static int exec_help (struct ccl_token *list)
     put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
                              "show <spec>    - retrieves and displays "
                              "records\n"));
-#endif
     return 0;
 }
 
@@ -350,6 +354,9 @@ static int exec_find (struct ccl_token *list, const char *search_str)
         strcpy (setname, "Default");
     else
         sprintf (setname, "%d", info.setno);
+    info.next_position = 1;
+    if (info.setno >= 0)
+        info.setno++;
     rpn = ccl_find (info.bibset, list, &error, &pos);
     if (!rpn)
     {
@@ -398,8 +405,6 @@ static int exec_find (struct ccl_token *list, const char *search_str)
              gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
     us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
     fprintf (reply_fd, "Result-set %s created\n", setname);
-    if (info.setno >= 0)
-        info.setno++;
     return 0;
 }
 
@@ -440,7 +445,7 @@ static void exec_status_r (struct gw_user_set *sp)
 
 static int exec_status (struct ccl_token *list)
 {
-    fprintf (reply_fd, "  Name     Hits    Database    Find\n");
+    fprintf (reply_fd, "  Name    Hits     Database   Find\n");
     exec_status_r (info.sets);
     return 0;
 }
@@ -493,8 +498,7 @@ static void present (const char *set, int offset, int number,
     int max_number;
     char format_str[16];
     
-    max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show", 
-                                   "200"));
+    max_number = gw_res_int (info.kernel_res, "gw.max.show", 200);
     if (number > max_number)
         number = max_number;
     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
@@ -729,12 +733,20 @@ static int exec_show (struct ccl_token *list)
         us = user_set_search (NULL);
         if (us && us->hits != -1)    /* proper result-set? */
         {
-            default_show = atoi (gw_res_get (info.kernel_res,
-                                             "gw.default.show", "20"));
-            if (us->hits > default_show)
-                present (us->name, 1, default_show, format_token);
-            else if (us->hits > 0)
-                present (us->name, 1, us->hits, format_token);
+            default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
+            if (us->hits >= info.next_position + default_show)
+           {
+                present (us->name, info.next_position, default_show, 
+                        format_token);
+               info.next_position += default_show;
+           }
+            else if (us->hits >= info.next_position)
+           {
+                present (us->name, info.next_position, 
+                        us->hits - info.next_position + 1, 
+                        format_token);
+               info.next_position = us->hits + 1;
+            }
         }
         else                         /* display error message */
         {
@@ -747,11 +759,19 @@ static int exec_show (struct ccl_token *list)
     return 0;
 }
 
-static int exec_command (const char *str)
+/*
+ * exec_command: parse and execute ccl command in str.
+ * str:      ccl command string
+ * stop_flag pointer to integer. On completion the integer
+ *           is 1 (stop) or 2 (continue); 0 (other command)
+ * return: 0 success; non-zero otherwise
+ */
+static int exec_command (const char *str, int *stop_flag)
 {
     struct ccl_token *cmd = ccl_tokenize (str);
     int no;
 
+    *stop_flag = 0;
     if (cmd->kind != CCL_TOK_EOL &&
         (no = command_search (command_tab, cmd, "ccl.command.")))
     {
@@ -782,8 +802,17 @@ static int exec_command (const char *str)
             return exec_base (cmd->next);
         case 4:
             return exec_help (cmd->next);
+       case 6: /* continue */
+           *stop_flag = 2;
+           return 0;
         case 7:
             return exec_status (cmd->next);
+       case 8: /* stop */
+           info.zass = NULL;
+           *info.target = 0;
+           *stop_flag = 1;
+           read_kernel_res();
+           return 0;
         case 9:
             return exec_target (cmd->next);
         case 11:
@@ -822,7 +851,7 @@ int urp_start (int continuation, struct str_queue *queue)
     {
         info.reply_fname = tempnam (gw_res_get (info.kernel_res,
                                            "gw.reply.tmp.dir", NULL),
-                               gw_res_get (info.kernel_res,
+                                    gw_res_get (info.kernel_res,
                                            "gw.reply.tmp.prefix", "gwr"));
                                                  
         reply_fd = fopen (info.reply_fname, "w");
@@ -859,6 +888,7 @@ int urp_start (int continuation, struct str_queue *queue)
 int urp_command (struct str_queue *queue)
 {
     char *cp;
+    int stop_flag;
 
     while (str_queue_deq (queue, line_buf, LINE_MAX))
     {
@@ -875,7 +905,16 @@ int urp_command (struct str_queue *queue)
             *cp = '\0';
         gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
         if (isalpha (line_buf[0]))
-            exec_command (line_buf);
+       {
+            exec_command (line_buf, &stop_flag);
+           if (stop_flag)
+           {
+               info.command_no++;     /* prevent help! */
+                while (str_queue_deq (queue, line_buf, LINE_MAX))
+                    ;
+               return stop_flag;
+            }
+        }
         info.command_no++;
     }
     return 0;