First search request/response that works.
[egate.git] / www / wtcl.c
index ebdabb5..5255b66 100644 (file)
  * USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * $Log: wtcl.c,v $
- * Revision 1.1  1995/10/20 14:02:42  adam
+ * Revision 1.4  1995/10/27 17:30:16  adam
+ * First search request/response that works.
+ *
+ * Revision 1.3  1995/10/27  15:12:14  adam
+ * IrTcl incorporated in the gateway.
+ * Better separation of script types.
+ * Z39.50 gateway scripts entered.
+ *
+ * Revision 1.2  1995/10/23  16:55:43  adam
+ * A lot of changes - really.
+ *
+ * Revision 1.1  1995/10/20  14:02:42  adam
  * First version of WWW gateway with embedded Tcl.
  *
  */
 #include <assert.h>
 #include <ctype.h>
 
-#include <tcl.h>
-
-#include "wproto.h"
-#include "winterp.h"
+#include "wtcl.h"
 
-static void *do_create (void *args);
-static int do_exec (WCLIENT wcl, const char *fname, char *parms, void *mydata);
+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 = {
     "tcl",
@@ -76,11 +84,75 @@ struct tcl_info {
     char  *fbuf;
     int    fbuf_size;
     int    fbuf_ptr;
+    WCLIENT wcl;
 };
 
-static void *do_create (void *args)
+Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
+{
+    struct tcl_info *p;
+
+    if (strcmp (w_interp->ctrl->name, "tcl"))
+    {
+        gw_log (GW_LOG_FATAL, mod, "Internal failure");
+        assert (0);
+    }
+    p = (struct tcl_info*) w_interp->mydata;
+    return p->interp;
+}
+
+static int proc_html_invoke (ClientData clientData, Tcl_Interp *interp,
+                             int argc, char **argv)
+{
+    struct tcl_info *p = (struct tcl_info*) clientData;
+    int i;
+
+    for (i = 1; i<argc; i++)
+        wo_puts (p->wcl, argv[i]);
+    return TCL_OK;
+}
+
+static int proc_htmlr_invoke (ClientData clientData, Tcl_Interp *interp,
+                              int argc, char **argv)
+{
+    struct tcl_info *p = (struct tcl_info*) clientData;
+    int r;
+
+    r = proc_html_invoke (clientData, interp, argc, argv);
+    wo_putc (p->wcl, '\n');
+    return r;
+}
+
+static int proc_form_invoke (ClientData clientData, Tcl_Interp *interp,
+                             int argc, char **argv)
+{
+    struct tcl_info *p = (struct tcl_info*) clientData;
+    int i;
+    if (argc == 2)
+    {
+        Tcl_AppendResult (p->interp, wgetval (p->wcl, argv[1]), NULL);
+        return TCL_OK;
+    }    
+    for (i = 0; *p->wcl->wf_data[i].name; i++)
+    { 
+        Tcl_AppendResult (p->interp, "{ ", NULL);
+        Tcl_AppendElement (p->interp, p->wcl->wf_data[i].name);
+        Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
+        Tcl_AppendResult (p->interp, " }\n", NULL);
+    }
+    return TCL_OK;
+}
+
+int Tcl_AppInit (Tcl_Interp *interp)
+{
+    if (Tcl_Init (interp) == TCL_ERROR)
+        return TCL_ERROR;
+    return TCL_OK;
+}
+
+static void *do_create (WCLIENT wcl, void *args)
 {
     struct tcl_info *p;
+    char tmp_str[256];
 
     if (!(p = malloc (sizeof(*p))))
     {
@@ -92,23 +164,38 @@ static void *do_create (void *args)
         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
         exit (1);
     }
+    p->wcl = wcl;
     p->fbuf_size = 1024;
     if (!(p->fbuf = malloc (p->fbuf_size)))
     {
         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
         exit (1);
     }
+    Tcl_AppInit (p->interp);
+    Tcl_CreateCommand (p->interp, "html", proc_html_invoke, p, NULL);
+    Tcl_CreateCommand (p->interp, "htmlr", proc_htmlr_invoke, p, NULL);
+    Tcl_CreateCommand (p->interp, "form", proc_form_invoke, p, NULL);
+    sprintf (tmp_str, "%d", wcl->id);
+    Tcl_SetVar (p->interp, "sessionId", tmp_str, TCL_GLOBAL_ONLY);
     return p;
 }
 
-static int tcl_exec (WCLIENT wcl, const char *fname, char *parms,
+static void report_error (struct tcl_info *p, int errorLine,
+                          const char *pre, const char *msg)
+{
+    gw_log (GW_LOG_WARN, mod, "%s %d %s", pre, errorLine, msg);
+    wo_printf (p->wcl, "\n<br><hr>\n<strong>"
+               "%s %d</strong><br>\n", pre, errorLine);
+    wo_printf (p->wcl, "<xmp>\n%s</xmp>\n<hr>\n", msg);
+}
+
+static int tcl_exec (const char *fname, char *parms,
                      struct tcl_info *p, FILE *inf, int *lineno)
 {
     int c, escape = 0, level = 0;
     int r, fbuf_ptr = 0;
     int local_line = 0;
 
-    gw_log (GW_LOG_DEBUG, mod, "tcl_exec. line %d", *lineno);
     while (1)
     {
         if (fbuf_ptr == p->fbuf_size-1)
@@ -127,7 +214,8 @@ static int tcl_exec (WCLIENT wcl, const char *fname, char *parms,
         c = getc (inf);
         if (c == EOF)
         {
-            gw_log (GW_LOG_WARN, mod, "Unexpected EOF: unbalanced braces");
+            report_error (p, *lineno, "Error in Tcl script starting at line",
+                                      "Unexpected EOF (missing right brace)");
             return -1;
         }
         if (c == '\\')
@@ -152,19 +240,16 @@ static int tcl_exec (WCLIENT wcl, const char *fname, char *parms,
         p->fbuf[fbuf_ptr++] = c;
     }
     p->fbuf[fbuf_ptr] = '\0';
-    gw_log (GW_LOG_DEBUG, mod, "Tcl_Eval. %d lines", local_line);
     r = Tcl_Eval (p->interp, p->fbuf);
-    if (r != TCL_OK)
-    {
-        gw_log (GW_LOG_WARN, mod, "Error in Tcl script starting on line %d",
-                *lineno);
-    }
+    if (r == TCL_ERROR)
+        report_error (p, p->interp->errorLine + *lineno - 1, 
+                      "Error in Tcl script in line", 
+                      Tcl_GetVar (p->interp, "errorInfo", 0));
     (*lineno) += local_line;
     return 0;
 }
 
-static int do_exec (WCLIENT wcl, const char *fname, char *parms,
-                    void *mydata)
+static int do_exec (const char *fname, char *parms, void *mydata)
 {
     struct tcl_info *p = mydata;
     int c, escape = 0;
@@ -177,6 +262,7 @@ static int do_exec (WCLIENT wcl, const char *fname, char *parms,
         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
         return -1;
     }
+    Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
     while ((c = getc(inf)) != EOF)
     {
         if (c == '\\')
@@ -184,10 +270,10 @@ static int do_exec (WCLIENT wcl, const char *fname, char *parms,
         else if (c == '{')
         {
             if (escape)
-                wo_putc (wcl, c);
+                wo_putc (p->wcl, c);
             else
             {
-                if (tcl_exec (wcl, fname, parms, p, inf, &lineno))
+                if (tcl_exec (fname, parms, p, inf, &lineno))
                 {
                     fclose (inf);
                     return -2;
@@ -200,7 +286,7 @@ static int do_exec (WCLIENT wcl, const char *fname, char *parms,
             if (c == '\n')
                 lineno++;
             escape = 0;
-            wo_putc (wcl, c);
+            wo_putc (p->wcl, c);
         }
     }
     fclose (inf);