Added another structure for improved recursion detection
authorceriel <none@none>
Thu, 5 Feb 1987 17:00:14 +0000 (17:00 +0000)
committerceriel <none@none>
Thu, 5 Feb 1987 17:00:14 +0000 (17:00 +0000)
util/cpp/macro.h
util/cpp/replace.c

index b04953a..ffa70b3 100644 (file)
@@ -36,6 +36,24 @@ extern char *std_alloc();
 #endif
 #define        free_macro(p) st_free(p, &h_macro, sizeof(struct macro))
 
+struct mlist {
+       struct mlist *next;
+       struct macro *m_mac;
+       char *m_repl;
+};
+
+/* allocation definitions of struct mlist */
+extern char *st_alloc();
+extern struct mlist *h_mlist;
+#ifdef DEBUG
+extern int cnt_mlist;
+extern char *std_alloc();
+#define        new_mlist() ((struct mlist *) std_alloc((char **)&h_mlist, sizeof(struct mlist), 20, &cnt_mlist))
+#else
+#define        new_mlist() ((struct mlist *) st_alloc((char **)&h_mlist, sizeof(struct mlist), 20))
+#endif
+#define        free_mlist(p) st_free(p, &h_mlist, sizeof(struct mlist))
+
 
 /* `token' numbers of keywords of command-line processor
 */
index 846cadf..7bc959c 100644 (file)
@@ -17,7 +17,7 @@
 char *strcpy(), *strcat();
 char *long2str();
 
-PRIVATE struct macro   *ReplaceList;   /* list of currently active macros */
+PRIVATE struct mlist *ReplaceList;     /* list of currently active macros */
 
 EXPORT int
 replace(idef)
@@ -36,6 +36,7 @@ replace(idef)
        register char c;
        char **actpars, **getactuals();
        char *reptext, *macro2buffer();
+       register struct mlist *repl;
        int size;
 
        if (mac->mc_flag & NOREPLACE) {
@@ -70,27 +71,36 @@ replace(idef)
                if (mac->mc_flag & FUNC) {
                        struct idf *param = findidf(*actpars);
 
+                       repl = new_mlist();
                        if (param && param->id_macro) 
                                reptext = "1";
                        else
                                reptext = "0";
                        InsertText(reptext, 1);
-                       mac->next = ReplaceList;
-                       ReplaceList = mac;
+
+                       repl->next = ReplaceList;
+                       ReplaceList = repl;
+                       repl->m_mac = mac;
                        return 1;
                }
        }
+
+       repl = new_mlist();
+       repl->m_mac = mac;
        if (mac->mc_flag & FUNC) /* this macro leads to special action  */
                macro_func(idef);
        if (mac->mc_nps <= 0) {
-               mac->mc_flag |= NOREPLACE;
                reptext = mac->mc_text;
                size = mac->mc_length;
+               mac->mc_flag |= NOREPLACE;      /* a file called __FILE__ ??? */
+       }
+       else {
+               reptext = macro2buffer(idef, actpars, &size); /* create input buffer */
+               repl->m_repl = reptext;
        }
-       else reptext = macro2buffer(idef, actpars, &size); /* create input buffer */
        InsertText(reptext, size);
-       mac->next = ReplaceList;
-       ReplaceList = mac;
+       repl->next = ReplaceList;
+       ReplaceList = repl;
        return 1;
 }
 
@@ -182,14 +192,18 @@ DoUnstack()
 EXPORT
 EnableMacros()
 {
-       register struct macro *p = ReplaceList;
+       register struct mlist *p = ReplaceList;
 
        assert(Unstacked > 0);
        while (Unstacked > 0) {
+               struct mlist *nxt = p->next;
+
                assert(p != 0);
-               p->mc_flag &= ~NOREPLACE;
-               p->mc_count = 0;
-               p = p->next;
+               p->m_mac->mc_flag &= ~NOREPLACE;
+               if (p->m_mac->mc_count) p->m_mac->mc_count--;
+               if (p->m_repl) free(p->m_repl);
+               free_mlist(p);
+               p = nxt;
                Unstacked--;
        }
        ReplaceList = p;