Fixed some bugs with escape sequences.
[egate.git] / www / wtcl.c
index a3fa1f6..f67fb32 100644 (file)
  * USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * $Log: wtcl.c,v $
+ * Revision 1.23  1998/03/26 11:29:02  franck
+ * Fixed some bugs with escape sequences.
+ *
+ * Revision 1.22  1997/12/15 15:05:03  adam
+ * Enhanced the code that deals with FORM variables.
+ *
+ * Revision 1.21  1997/02/10 10:15:40  adam
+ * Bug fix: egw_source <file> <var> didn't work properly.
+ *
+ * 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
@@ -195,7 +212,6 @@ static int proc_form_invoke (struct tcl_info *p, wform_data *wfdata,
 {
     const char *arg = NULL;
     int failFlag = 0;
-    int i;
 
     if (argc == 3)
     {
@@ -220,27 +236,27 @@ static int proc_form_invoke (struct tcl_info *p, wform_data *wfdata,
         arg = argv[1];
     if (arg)
     {
-        for (i = 0; *wfdata[i].name; i++)
-            if (!strcmp (arg, wfdata[i].name))
+        for (; wfdata; wfdata = wfdata->next)
+            if (!strcmp (arg, wfdata->name))
             {
                 failFlag = 0;
-                if (*wfdata[i].value)
-                    Tcl_AppendElement (p->interp, wfdata[i].value);
+                if (wfdata->value)
+                    Tcl_AppendElement (p->interp, wfdata->value);
             }
         if (failFlag)
         {
             Tcl_AppendResult (p->interp, arg, " doesn't exist", NULL);
             return TCL_ERROR;
         }
-        return TCL_OK;
     }    
-    for (i = 0; *wfdata[i].name; i++)
-    {
-        Tcl_AppendResult (p->interp, "{ ", NULL);
-        Tcl_AppendElement (p->interp, wfdata[i].name);
-        Tcl_AppendElement (p->interp, wfdata[i].value);
-        Tcl_AppendResult (p->interp, " }\n", NULL);
-    }
+    else
+        for ( ; wfdata; wfdata = wfdata->next)
+        {
+            Tcl_AppendResult (p->interp, "{ ", NULL);
+            Tcl_AppendElement (p->interp, wfdata->name);
+            Tcl_AppendElement (p->interp, wfdata->value ? wfdata->value : "");
+            Tcl_AppendResult (p->interp, " }\n", NULL);
+        }
     return TCL_OK;
 }
 
@@ -311,7 +327,7 @@ static int proc_enc (ClientData clientData, Tcl_Interp *interp,
         while (*cp)
         { 
             if (*cp < ' ' || *cp >= 127 || *cp == '&' || *cp == '?'
-                || *cp == '%' || *cp == '+')
+                || *cp == '%' || *cp == '+' || *cp == '"' || *cp == '=')
             {
                 sprintf (buf1+1, "%02X", *cp & 0xff);
                 Tcl_AppendResult (interp, buf1, NULL);
@@ -380,22 +396,32 @@ static int proc_source (ClientData clientData, Tcl_Interp *interp,
                         int argc, char **argv)
 {
     struct tcl_info *p = (struct tcl_info*) clientData;
-    int r;
+    int r, offset = 0;
 
-    if (argc != 2)
+    if (argc < 2 || argc > 3)
     {
         Tcl_AppendResult (p->interp,
-                          "wrong # args: should be egw_source file", NULL);
+                          "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);
-    if (r == -2)
+    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 source ",
+        Tcl_AppendResult (p->interp, "egw_source: couldn't open ",
                          argv[1], NULL);
         return TCL_ERROR;
     }
-    else if (r == -1)
+    else if (r == -2)
     {
         Tcl_AppendResult (p->interp, "egw_source: Tcl error in script ",
                          argv[1], NULL);
@@ -404,6 +430,14 @@ static int proc_source (ClientData clientData, Tcl_Interp *interp,
     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)
 {
@@ -445,6 +479,7 @@ static void *do_create (WCLIENT wcl, void *args)
     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;
@@ -524,6 +559,45 @@ static int tcl_exec (const char *fname, struct tcl_info *p, FILE *inf,
     return r;
 }
 
+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)
 {
     int c, escape = 0;
@@ -536,14 +610,31 @@ static int exec_file (const char *fname, struct tcl_info *p)
         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
         return -1;
     }
-    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)
+            {
                 wo_putc (p->wcl, c);
+                escape = 0;
+                c = getc (inf);
+            }
+            else
+                c = var_ref (p, inf);
+            break;
+        case '{':
+            if (escape)
+            {
+                wo_putc (p->wcl, c);
+                escape = 0;
+            }
             else
             {
                 int r = tcl_exec (fname, p, inf, &lineno);
@@ -558,14 +649,14 @@ static int exec_file (const char *fname, struct tcl_info *p)
                     return -2;
                 }
             }
-            escape = 0;
-        }
-        else
-        {
+            c = getc (inf);
+            break;
+        default:
             if (c == '\n')
                 lineno++;
             escape = 0;
             wo_putc (p->wcl, c);
+            c = getc (inf);
         }
     }
     fclose (inf);