Enhanced the egw_source command. An optional parameter specifies the
[egate.git] / www / wtcl.c
index 5728d56..8772adc 100644 (file)
  * USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * $Log: wtcl.c,v $
+ * Revision 1.20  1997/01/31 11:16:01  adam
+ * Enhanced the egw_source command. An optional parameter specifies the
+ * name of a variable in which the HTML output is stored.
+ *
+ * Revision 1.19  1997/01/27 11:27:17  adam
+ * Implemented a new command, egw_clear, to clear http output cache.
+ * Changed prototype for function wo_clear.
+ *
+ * Revision 1.18  1997/01/24 13:13:13  adam
+ * Implemnted egw_source and added a "raw" option to the URL.
+ * Fixed a bug in the buffering system of wproto; the macro wo_putc could
+ * override memory if it was the first HTML generating function called.
+ *
+ * Revision 1.17  1996/05/31 08:02:56  adam
+ * Bug fix: egw_enc encoded '/' - it shouldn't.
+ *
  * Revision 1.16  1996/05/23 15:53:12  adam
  * Bug fix: egw_enc failed on 8-bit chars.
  * New command: egw_parms.
@@ -135,6 +151,8 @@ struct tcl_info {
     WCLIENT wcl;
 };
 
+static int exec_file (const char *fname, struct tcl_info *p);
+
 Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
 {
     struct tcl_info *p;
@@ -299,13 +317,17 @@ static int proc_enc (ClientData clientData, Tcl_Interp *interp,
     {
         const char *cp = argv[i];
         while (*cp)
-        {
-            if (*cp <= ' ' || *cp >= 127 || *cp == '/' || *cp == ' ' ||
-                *cp == '&' || *cp == ':' || *cp == '%')
+        { 
+            if (*cp < ' ' || *cp >= 127 || *cp == '&' || *cp == '?'
+                || *cp == '%' || *cp == '+')
             {
                 sprintf (buf1+1, "%02X", *cp & 0xff);
                 Tcl_AppendResult (interp, buf1, NULL);
             }
+            else if (*cp == ' ')
+            {
+                Tcl_AppendResult (interp, "+", NULL);
+            }
             else
             {
                 buf2[0] = *cp;
@@ -362,6 +384,53 @@ static int proc_prog (ClientData clientData, Tcl_Interp *interp,
     return TCL_OK;
 }
 
+static int proc_source (ClientData clientData, Tcl_Interp *interp,
+                        int argc, char **argv)
+{
+    struct tcl_info *p = (struct tcl_info*) clientData;
+    int r, offset = 0;
+
+    if (argc < 2 || argc > 3)
+    {
+        Tcl_AppendResult (p->interp,
+                          "wrong # args: should be egw_source file ?var?",
+                          NULL);
+        return TCL_ERROR;
+    }
+    if (argc == 3)
+        offset = wproto_save_push (p->wcl);
+    r = exec_file (argv[1], p);
+    Tcl_ResetResult (p->interp);
+    if (argc == 3)
+    {
+        char *res = wproto_save_pop (p->wcl, offset);
+        if (res)
+            Tcl_SetVar (p->interp, argv[2], res, 0);
+    }
+    if (r == -1)
+    {
+        Tcl_AppendResult (p->interp, "egw_source: couldn't open ",
+                         argv[1], NULL);
+        return TCL_ERROR;
+    }
+    else if (r == -2)
+    {
+        Tcl_AppendResult (p->interp, "egw_source: Tcl error in script ",
+                         argv[1], NULL);
+        return TCL_ERROR;
+    }
+    return TCL_OK;
+}
+
+static int proc_clear (ClientData clientData, Tcl_Interp *interp,
+                        int argc, char **argv)
+{
+    struct tcl_info *p = (struct tcl_info*) clientData;
+
+    wo_clear (p->wcl);
+    return TCL_OK;
+}
+
 int Tcl_AppInit (Tcl_Interp *interp)
 {
     if (Tcl_Init (interp) == TCL_ERROR)
@@ -401,6 +470,8 @@ static void *do_create (WCLIENT wcl, void *args)
     Tcl_CreateCommand (p->interp, "egw_enc", proc_enc, p, NULL);
     Tcl_CreateCommand (p->interp, "egw_dec", proc_dec, p, NULL);
     Tcl_CreateCommand (p->interp, "egw_prog", proc_prog, p, NULL);
+    Tcl_CreateCommand (p->interp, "egw_source", proc_source, p, NULL);
+    Tcl_CreateCommand (p->interp, "egw_clear", proc_clear, p, NULL);
     sprintf (tmp_str, "%d", wcl->id);
     Tcl_SetVar (p->interp, "sessionId", tmp_str, TCL_GLOBAL_ONLY);
     return p;
@@ -417,8 +488,8 @@ static void report_error (struct tcl_info *p, int 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)
+static int tcl_exec (const char *fname, struct tcl_info *p, FILE *inf,
+                     int *lineno)
 {
     int c, escape = 0, level = 0;
     int r, fbuf_ptr = 0;
@@ -480,9 +551,47 @@ static int tcl_exec (const char *fname, char *parms,
     return r;
 }
 
-static int do_exec (const char *fname, char *parms, void *mydata)
+static int var_ref (struct tcl_info *p, FILE *inf)
+{
+    int c, i = 0;
+    char name[32], *vp;
+
+    c = getc (inf);
+    if (c == '{')
+    {
+        while ((c = getc(inf)) != EOF)
+        {
+            if (c == '\n')
+                break;
+            if (c == '}')
+            {
+                c = getc (inf);
+                break;
+            }
+            if (i < 31)
+                name[i++] = c;
+        }
+    } 
+    else
+    {
+        while (c != EOF)
+        {
+            if (c != '(' && c != ')' && c != '-' && c != '_' && !isalnum(c))
+                break;
+            if (i < 31)
+                name[i++] = c;
+            c = getc (inf);
+        }
+    }
+    name[i] = '\0';
+    vp = Tcl_GetVar (p->interp, name, 0);
+    if (vp)
+        wo_puts (p->wcl, vp);
+    return c;
+}
+
+static int exec_file (const char *fname, struct tcl_info *p)
 {
-    struct tcl_info *p = mydata;
     int c, escape = 0;
     int lineno = 1;
     FILE *inf = fopen (fname, "r");
@@ -493,18 +602,30 @@ static int do_exec (const char *fname, char *parms, void *mydata)
         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)
+    c = getc (inf);
+    while (c != EOF)
     {
-        if (c == '\\')
-            escape = 1;
-        else if (c == '{')
+        switch (c)
         {
+        case '\\':
+            escape = 1;
+            c = getc (inf);
+            break;
+        case '$':
+            if (escape)
+            {
+                c = getc (inf);
+                wo_putc (p->wcl, c);
+            }
+            else
+                c = var_ref (p, inf);
+            break;
+        case '{':
             if (escape)
                 wo_putc (p->wcl, c);
             else
             {
-                int r = tcl_exec (fname, parms, p, inf, &lineno);
+                int r = tcl_exec (fname, p, inf, &lineno);
                 if (r == TCL_RETURN)
                 {
                     fclose (inf);
@@ -517,19 +638,27 @@ static int do_exec (const char *fname, char *parms, void *mydata)
                 }
             }
             escape = 0;
-        }
-        else
-        {
+            c = getc (inf);
+            break;
+        default:
             if (c == '\n')
                 lineno++;
             escape = 0;
             wo_putc (p->wcl, c);
+            c = getc (inf);
         }
     }
     fclose (inf);
     return 0;
 }
 
+static int do_exec (const char *fname, char *parms, void *mydata)
+{
+    struct tcl_info *p = mydata;
+
+    Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
+    return exec_file (fname, p);
+}
 
 static int do_load (char *parms, void *mydata)
 {