Updated version to 0.2.1.
[tclrobot.git] / hswitch.c
index 3a05f5e..15ddbc6 100644 (file)
--- a/hswitch.c
+++ b/hswitch.c
@@ -1,5 +1,5 @@
 /*
- * $Id: hswitch.c,v 1.2 1998/10/15 12:31:01 adam Exp $
+ * $Id: hswitch.c,v 1.5 2001/11/08 10:23:02 adam Exp $
  */
 #include <assert.h>
 #include <string.h>
@@ -8,16 +8,16 @@
 
 #include "tclrobot.h"
 
-#define TAG_MAX_LEN 32
+#define TAG_MAX_LEN 64
 
 #define SPACECHR " \t\r\n\f"
 
-#define DEBUG(x) 
+#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;
 }
@@ -81,6 +81,7 @@ struct tagParm {
 
 struct tagInfo {
     int level;
+    int nest;
     char *pattern;
     char *code;
 
@@ -163,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);
 
@@ -170,7 +172,6 @@ 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];
@@ -183,7 +184,13 @@ static int tagEnd (Tcl_Interp *interp, struct tagInfo *tag,
                 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;
@@ -195,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;
@@ -207,24 +214,42 @@ 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] == '<' && cp[1] == '/')/* end tag */
         {