Updated version to 0.2.1.
[tclrobot.git] / hswitch.c
index e631c1c..15ddbc6 100644 (file)
--- a/hswitch.c
+++ b/hswitch.c
@@ -1,5 +1,5 @@
 /*
- * $Id: hswitch.c,v 1.1 1996/08/06 14:04:22 adam Exp $
+ * $Id: hswitch.c,v 1.5 2001/11/08 10:23:02 adam Exp $
  */
 #include <assert.h>
 #include <string.h>
@@ -8,14 +8,16 @@
 
 #include "tclrobot.h"
 
-#define TAG_MAX_LEN 32
+#define TAG_MAX_LEN 64
 
 #define SPACECHR " \t\r\n\f"
 
+#define DEBUG(x)
+
 static int skipSpace (const char *cp)
 {
     int i = 0;
-    while (strchr (SPACECHR, cp[i]))
+    while (cp[i] && strchr (SPACECHR, cp[i]))
         i++;
     return i;
 }
@@ -23,10 +25,15 @@ static int skipSpace (const char *cp)
 static int skipTag (const char *cp, char *dst)
 {
     int i;
+    int j = 0;
 
-    for (i=0; i<TAG_MAX_LEN-1 && cp[i] && !strchr (SPACECHR "/>=", cp[i]); i++)
-        dst[i] = tolower(cp[i]);
-    dst[i] = '\0';
+    for (i=0; cp[i] && !strchr (SPACECHR "/>=", cp[i]); i++)
+       if (j < TAG_MAX_LEN-1)
+       {
+           dst[j] = tolower(cp[j]);
+           j++;
+       }
+    dst[j] = '\0';
     return i;
 }
 
@@ -74,6 +81,7 @@ struct tagParm {
 
 struct tagInfo {
     int level;
+    int nest;
     char *pattern;
     char *code;
 
@@ -102,6 +110,7 @@ static int tagStart (struct tagInfo *tag, const char *tagString,
     if (tag && !tag->level)
     {
         strcpy (tag->name, tagString);
+       DEBUG(printf ("------ consuming this %s\n", tag->name));
         tag->tagParms = NULL;
         nParms = &tag->tagParms;
     }
@@ -109,15 +118,20 @@ static int tagStart (struct tagInfo *tag, const char *tagString,
     i = skipSpace (cp);
     while (cp[i] && cp[i] != '>')
     {
-        int nor =  skipParm (cp+i, parm_name, &parm_value);
+        int nor = skipParm (cp+i, parm_name, &parm_value);
         i += nor;
+       if (nor && tag)
+       {
+           DEBUG(printf ("parm_name=%s parm_value=%s\n", parm_name, parm_value));
+       }
         if (nor && tag && !tag->level)
         {
             *nParms = malloc (sizeof(**nParms));
             assert (*nParms);
-            (*nParms)->next = NULL;
             strcpy ((*nParms)->name, parm_name);
             (*nParms)->value = parm_value;
+            (*nParms)->next = NULL;
+           nParms = &(*nParms)->next;
         }
         else
         {
@@ -150,6 +164,7 @@ static int tagEnd (Tcl_Interp *interp, struct tagInfo *tag,
         -- (tag->level);
         if (!tag->level)
         {
+           int tcl_err;
             struct tagParm *tp = tag->tagParms;
             char *value = malloc (body_end - tag->body_start + 1);
 
@@ -157,19 +172,25 @@ static int tagEnd (Tcl_Interp *interp, struct tagInfo *tag,
             memcpy (value, tag->body_start, body_end - tag->body_start);
             value[body_end - tag->body_start] = '\0';
             Tcl_SetVar (interp, "body", value, 0);
-            free (value);
             while (tp)
             {
                 char vname[TAG_MAX_LEN+30];
                 struct tagParm *tp0 = tp;
                 
                 sprintf (vname, "parm(%s)", tp->name);
+               DEBUG(printf ("vname=%s\n", vname));
 
                 Tcl_SetVar (interp, vname, tp->value ? tp->value : "",0);
                 tp = tp->next;
                 free (tp0);
             }
-            Tcl_Eval (interp, tag->code);
+            tcl_err = Tcl_Eval (interp, tag->code);
+            free (value);
+           if (tcl_err == TCL_ERROR)
+           {
+               printf ("Error: code=%d %s\n", tcl_err, interp->result);
+               exit (1);
+           }
         }
     }
     return i;
@@ -181,7 +202,7 @@ int htmlSwitch (ClientData clientData, Tcl_Interp *interp,
     struct tagInfo *tags;
     int noTags;
     const char *cp;
-    int i, argi = 1;
+    int i = 0, argi = 1;
 
     cp = argv[argi++];
     noTags = (argc - argi)/2;
@@ -193,25 +214,44 @@ int htmlSwitch (ClientData clientData, Tcl_Interp *interp,
     }
     tags = malloc (sizeof(*tags) * noTags);
     assert (tags);
-    for (i = 0; i<noTags; i++)
+    while (argi < argc-1)
     {
         tags[i].level = 0;
+        tags[i].nest = 1;
+        if (!strcmp(argv[argi], "-nonest"))
+        {
+            argi++;
+            tags[i].nest = 0;
+        }
+        else if (!strcmp(argv[argi], "-nest"))
+        {
+            argi++;
+            tags[i].nest = 1;
+        }
         tags[i].pattern = argv[argi++];
         tags[i].code = argv[argi++];
+        i++;
     }
+    noTags = i;
     while (*cp)
     {
         if (cp[0] == '<' && cp[1] != '/')     /* start tag */
         {
             char tagStr[TAG_MAX_LEN];
             int tagI;
+            const char *body_start = cp;
 
             cp++;
             cp += skipTag (cp, tagStr);
             tagI = tagLookup (tags, noTags, tagStr);
+           DEBUG(printf ("tagStr = %s tagI = %d\n", tagStr, tagI));
             cp += tagStart (tagI >= 0 ? tags+tagI : NULL, tagStr, cp);
+            if (tagI >= 0 && tags[tagI].nest == 0)
+            {
+                cp += tagEnd (interp, tags+tagI, tagStr, body_start, cp);
+            }
         }
-        else if (cp[0] == '<')                /* end tag */
+        else if (cp[0] == '<' && cp[1] == '/')/* end tag */
         {
             char tagStr[TAG_MAX_LEN];
             const char *body_end = cp;