Lots of changes. They aren't visible though.
[egate.git] / www / wirtcl.c
index e0e48e8..6425be9 100644 (file)
  * USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * $Log: wirtcl.c,v $
- * Revision 1.5  1995/10/31 16:56:24  adam
+ * Revision 1.16  1996/02/29 15:40:23  adam
+ * New function w_interp_irtcl_get that returns Tcl interpreter of
+ * IrTcl interpreter.
+ *
+ * Revision 1.15  1996/02/21  14:58:01  adam
+ * Modified to use ir_tcl_select_set.
+ *
+ * Revision 1.14  1996/02/12  10:10:31  adam
+ * Resource/config system used by the gateway.
+ *
+ * Revision 1.13  1996/01/24  08:26:54  adam
+ * All tcl commands prefixed with egw_ (except the html command).
+ *
+ * Revision 1.12  1996/01/12  10:05:18  adam
+ * If script name ends with ';' HTTP/GET/Expires will be defined.
+ * The cgi interface only reads final handshake if response from
+ * server (shell) was zero-terminated [If it isn't it probably died].
+ *
+ * Revision 1.11  1996/01/09  16:16:49  adam
+ * Port to OSF/1. Gif references moved from /gif/ to /egwgif/.
+ *
+ * Revision 1.10  1996/01/09  10:46:50  adam
+ * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile.
+ *
+ * Revision 1.9  1995/11/07  14:56:59  adam
+ * Work on search in multiple targets.
+ * New wtcl command: wlog.
+ * Optional timeout parameter to zwait.
+ *
+ * Revision 1.8  1995/11/06  17:44:22  adam
+ * State reestablised when shell restarts. History of previous
+ * result sets.
+ *
+ * Revision 1.7  1995/11/02  16:35:37  adam
+ * Bug fixes and select on FIFOs in wcgi - doesn't really work!
+ *
+ * Revision 1.6  1995/11/01  16:15:47  adam
+ * Better presentation of records. Query/set number persistent.
+ *
+ * Revision 1.5  1995/10/31  16:56:24  adam
  * Record presentation.
  *
  * Revision 1.4  1995/10/31  10:03:53  adam
 
 static void *do_create (WCLIENT wcl, void *args);
 static int do_exec (const char *fname, char *parms, void *mydata);
+static int do_load (char *parms, void *mydata);
+static int do_save (char *parms, void *mydata);
 
 static struct w_interp_type w_interp_t = {
     "irtcl",
     do_create,
-    do_exec
+    do_exec,
+    do_load,
+    do_save
 };
 
 W_Interp_Type w_interp_irtcl = &w_interp_t;
@@ -98,8 +141,7 @@ struct tcl_info {
     WCLIENT wcl;
 };
 
-
-static int events (struct tcl_info *p, char *waitVar);
+static int events (struct tcl_info *p, char *waitVar, int tout);
 
 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
                               int argc, char **argv)
@@ -108,18 +150,14 @@ static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
     
     if (argc < 2)
         return TCL_OK;
-    events (p, argv[1]);
-    return TCL_OK;
+    return events (p, argv[1], (argc == 3) ? atoi(argv[2]) : 0);
 }
 
-
-
-/* select(2) callbacks */
+/* select callbacks */
 struct callback {
-    void (*r_handle)(ClientData);
-    void (*w_handle)(ClientData);
-    void (*x_handle)(ClientData);
-    void *obj;
+    void (*handle)(ClientData, int, int, int);
+    int r, w, e;
+    ClientData obj;
 };
 #define MAX_CALLBACK 200
 
@@ -148,15 +186,11 @@ static void *do_create (WCLIENT wcl, void *args)
         gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
         exit (1);
     }
-    log_init(LOG_ALL, "irtcl", "/usr/local/etc/httpd/logs/irtcl_log");
+    log_init(LOG_ALL, "irtcl", "irtcl_log");
     /* initialize irtcl */
-    Tcl_CreateCommand (p->interp, "zwait", proc_zwait_invoke, p, NULL);
+    Tcl_CreateCommand (p->interp, "egw_wait", proc_zwait_invoke, p, NULL);
     for (i=0; i<MAX_CALLBACK; i++)
-    {
-        callback_table[i].r_handle = NULL;
-        callback_table[i].w_handle = NULL;
-        callback_table[i].x_handle = NULL;
-    }
+        callback_table[i].handle = NULL;
     return p;
 }
 
@@ -171,7 +205,7 @@ static int do_exec (const char *fname, char *parms, void *mydata)
 }
 
 
-static int events (struct tcl_info *p, char *waitVar)
+static int events (struct tcl_info *p, char *waitVar, int tout)
 {
     int r, i;
     char *cp;
@@ -201,6 +235,16 @@ static int events (struct tcl_info *p, char *waitVar)
     gw_log (GW_LOG_DEBUG, mod, "Waiting %s=%s", waitVar, waitVarVal);
     while (1)
     {
+        struct timeval to, *top;
+       if (tout > 0)
+       {
+           to.tv_usec = 0;
+           to.tv_sec = tout;
+           top = &to;
+       }
+       else
+           top = 0;
+
         if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
             strcmp (cp, waitVarVal))
         {
@@ -215,17 +259,17 @@ static int events (struct tcl_info *p, char *waitVar)
         
         for (r=0, i=0; i<=max_fd; i++)
         {
-            if (callback_table[i].w_handle)
+            if (callback_table[i].handle && callback_table[i].w)
             {
                 FD_SET (i, &fdset_tcl_w);
                 r++;
             }
-            if (callback_table[i].r_handle)
+            if (callback_table[i].handle && callback_table[i].r)
             {
                 FD_SET (i, &fdset_tcl_r);
                 r++;
             }
-            if (callback_table[i].x_handle)
+            if (callback_table[i].handle && callback_table[i].e)
             {
                 FD_SET (i, &fdset_tcl_x);
                 r++;
@@ -233,10 +277,12 @@ static int events (struct tcl_info *p, char *waitVar)
         }
         if (!r)
             break;
+#if 1
         gw_log (GW_LOG_DEBUG, mod, "fifo select %d", fifo_in);
         FD_SET (fifo_in, &fdset_tcl_r);
+#endif
         if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
-                          &fdset_tcl_x, 0)) < 0)
+                          &fdset_tcl_x, top)) < 0)
         {
             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, mod, "select");
             exit(1);
@@ -244,61 +290,74 @@ static int events (struct tcl_info *p, char *waitVar)
         if (!r)
         {
             gw_log (GW_LOG_DEBUG, mod, "timeout");
-            break;
+            free (waitVarVal);
+            return TCL_ERROR;
         }
         if (FD_ISSET (fifo_in, &fdset_tcl_r))
         {
             gw_log (GW_LOG_DEBUG, mod, "FIFO closed");
-            break;
+            free (waitVarVal);
+            return TCL_ERROR;
         }
         for (i=0; i<=max_fd; i++)
         {
-            if (FD_ISSET (i, &fdset_tcl_r))
-            {
-                assert (callback_table[i].r_handle);
-                (*callback_table[i].r_handle) (callback_table[i].obj);
-            }
-            if (FD_ISSET (i, &fdset_tcl_w))
-            {
-                assert (callback_table[i].w_handle);
-                (*callback_table[i].w_handle) (callback_table[i].obj);
-            }
-            if (FD_ISSET (i, &fdset_tcl_x))
-            {
-                assert (callback_table[i].x_handle);
-                (*callback_table[i].x_handle) (callback_table[i].obj);
-            }
+            int r_flag = 0;
+            int w_flag = 0;
+            int e_flag = 0;
+
+            if (!callback_table[i].handle)
+                continue;
+            if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
+                r_flag = 1;
+            if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
+                w_flag = 1;
+            if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
+                e_flag = 1;
+            if (r_flag || w_flag || e_flag)
+                (*callback_table[i].handle)(callback_table[i].obj,
+                 r_flag, w_flag, e_flag);
         }
     }
     free (waitVarVal);
     return TCL_OK;
 }
 
-void ir_select_add (int fd, void *obj)
+void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
+                        int fd, ClientData clientData, int r, int w, int e)
 {
-    callback_table[fd].obj = obj;
-    callback_table[fd].r_handle = ir_select_read;
-    callback_table[fd].w_handle = NULL;
-    callback_table[fd].x_handle = NULL;
+    callback_table[fd].handle = f;
+    callback_table[fd].obj = clientData;
+    callback_table[fd].r = r;
+    callback_table[fd].w = w;
+    callback_table[fd].e = e;
     if (fd > max_fd)
         max_fd = fd;
 }
 
-void ir_select_add_write (int fd, void *obj)
+static int do_load (char *parms, void *mydata)
 {
-    callback_table[fd].w_handle = ir_select_write;
-    if (fd > max_fd)
-        max_fd = fd;
+    struct tcl_info *p = mydata;
+
+    return w_interp_load_state (p->w_interp, parms);
 }
 
-void ir_select_remove_write (int fd, void *obj)
+static int do_save (char *parms, void *mydata)
 {
-    callback_table[fd].w_handle = NULL;
+    struct tcl_info *p = mydata;
+
+    return w_interp_save_state (p->w_interp, parms);
 }
 
-void ir_select_remove (int fd, void *obj)
+Tcl_Interp *w_interp_irtcl_get (W_Interp w_interp)
 {
-    callback_table[fd].r_handle = NULL;
-    callback_table[fd].w_handle = NULL;
-    callback_table[fd].x_handle = NULL;
+    struct tcl_info *p;
+
+    if (strcmp (w_interp->ctrl->name, "irtcl"))
+    {
+        gw_log (GW_LOG_FATAL, mod, "Internal failure");
+        assert (0);
+    }
+    p = (struct tcl_info*) w_interp->mydata;
+    return p->interp;
 }
+