IrTcl incorporated in the gateway.
[egate.git] / www / wirtcl.c
diff --git a/www/wirtcl.c b/www/wirtcl.c
new file mode 100644 (file)
index 0000000..c6548ce
--- /dev/null
@@ -0,0 +1,224 @@
+/*
+ * Copyright (c) 1995, the EUROPAGATE consortium (see below).
+ *
+ * The EUROPAGATE consortium members are:
+ *
+ *    University College Dublin
+ *    Danmarks Teknologiske Videnscenter
+ *    An Chomhairle Leabharlanna
+ *    Consejo Superior de Investigaciones Cientificas
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The names of EUROPAGATE or the project partners may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * 3. Users of this software (implementors and gateway operators) agree to
+ * inform the EUROPAGATE consortium of their use of the software. This
+ * information will be used to evaluate the EUROPAGATE project and the
+ * software, and to plan further developments. The consortium may use
+ * the information in later publications.
+ * 
+ * 4. Users of this software agree to make their best efforts, when
+ * documenting their use of the software, to acknowledge the EUROPAGATE
+ * consortium, and the role played by the software in their work.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
+ * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+ * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
+ * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * $Log: wirtcl.c,v $
+ * Revision 1.1  1995/10/27 15:12:08  adam
+ * IrTcl incorporated in the gateway.
+ * Better separation of script types.
+ * Z39.50 gateway scripts entered.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#ifdef AIX
+#include <sys/select.h>
+#endif
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
+
+#include "wtcl.h"
+#include "wirtcl.h"
+
+static void *do_create (WCLIENT wcl, void *args);
+static int do_exec (const char *fname, char *parms, void *mydata);
+
+static struct w_interp_type w_interp_t = {
+    "irtcl",
+    do_create,
+    do_exec
+};
+
+W_Interp_Type w_interp_irtcl = &w_interp_t;
+
+
+static char *mod = "wirtcl";
+
+struct tcl_info {
+    W_Interp w_interp;
+    Tcl_Interp *interp;
+    WCLIENT wcl;
+};
+
+static void *do_create (WCLIENT wcl, void *args)
+{
+    struct tcl_info *p;
+
+    if (!(p = malloc (sizeof(*p))))
+    {
+        gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
+        exit (1);
+    }
+    if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
+    {
+        gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
+        exit (1);
+    }
+    p->wcl = wcl;
+    p->interp = w_interp_tcl_get (p->w_interp);
+    if (Irtcl_Init(p->interp) == TCL_ERROR)
+    {
+        gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
+        exit (1);
+    }
+    /* initialize irtcl */
+    return p;
+}
+
+/* select(2) callbacks */
+struct callback {
+    void (*r_handle)(ClientData);
+    void (*w_handle)(ClientData);
+    void (*x_handle)(ClientData);
+    void *obj;
+};
+#define MAX_CALLBACK 200
+
+static struct callback callback_table[MAX_CALLBACK];
+static int max_fd = 3;            /* don't worry: it will grow... */
+
+static int do_exec (const char *fname, char *parms, void *mydata)
+{
+    struct tcl_info *p = mydata;
+    int i, r, min_fd = 0;
+    const char *cp;
+    static fd_set fdset_tcl_r;
+    static fd_set fdset_tcl_w;
+    static fd_set fdset_tcl_x;
+
+    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;
+    }
+    if ((r = w_interp_exec (p->w_interp, fname, parms)))
+        return r;
+    while (1)
+    {
+        FD_ZERO (&fdset_tcl_r);
+        FD_ZERO (&fdset_tcl_w);
+        FD_ZERO (&fdset_tcl_x);
+        for (r=0, i=min_fd; i<=max_fd; i++)
+        {
+            if (callback_table[i].w_handle)
+            {
+                FD_SET (i, &fdset_tcl_w);
+                r++;
+            }
+            if (callback_table[i].r_handle)
+            {
+                FD_SET (i, &fdset_tcl_r);
+                r++;
+            }
+            if (callback_table[i].x_handle)
+            {
+                FD_SET (i, &fdset_tcl_x);
+                r++;
+            }
+        }
+        if (!r)
+            return 0;
+        if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
+                          &fdset_tcl_x, 0)) < 0)
+        {
+            perror("select");
+            exit(1);
+        }
+        if (!r)
+            continue;
+        for (i=min_fd; 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);
+            }
+        }
+       if ((cp=Tcl_GetVar (p->interp, "sessionWait", 0)) && !strcmp (cp, "0"))
+            return 0;
+    }
+    return 0;
+}
+
+void ir_select_add (int fd, void *obj)
+{
+    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;
+    if (fd > max_fd)
+        max_fd = fd;
+}
+
+void ir_select_add_write (int fd, void *obj)
+{
+    callback_table[fd].w_handle = ir_select_write;
+    if (fd > max_fd)
+        max_fd = fd;
+}
+
+void ir_select_remove_write (int fd, void *obj)
+{
+    callback_table[fd].w_handle = NULL;
+}
+
+void ir_select_remove (int fd, void *obj)
+{
+    callback_table[fd].r_handle = NULL;
+    callback_table[fd].w_handle = NULL;
+    callback_table[fd].x_handle = NULL;
+}