Rationalize the reporting of "macro argument too long";
authorRobert R. Henry <rrh@ucbvax.Berkeley.EDU>
Fri, 29 Oct 1982 02:31:31 +0000 (18:31 -0800)
committerRobert R. Henry <rrh@ucbvax.Berkeley.EDU>
Fri, 29 Oct 1982 02:31:31 +0000 (18:31 -0800)
paramaterize the maximum number of characaters per macro argument, and the
maximum number of macro arguments.

SCCS-vsn: old/roff/common_source/n3.c 4.2

usr/src/old/roff/common_source/n3.c

index 8a3574a..84f773b 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)n3.c       4.1 %G%";
+static char sccsid[] = "@(#)n3.c       4.2 %G%";
 #endif lint
 
 #include "tdef.h"
 #endif lint
 
 #include "tdef.h"
@@ -412,13 +412,20 @@ popi(){
        ch0 = p->pch0;
        return(p->pch);
 }
        ch0 = p->pch0;
        return(p->pch);
 }
+
+/*
+ *     test that the end of the allocation is above a certain location
+ *     in memory
+ */
+#define SPACETEST(base, size) while ((enda - (size)) <= (char *)(base)){setbrk(DELTA);}
+
 pushi(newip)
 filep newip;
 {
        register struct s *p;
 pushi(newip)
 filep newip;
 {
        register struct s *p;
-       extern char *setbrk();
+       extern char     *setbrk();
 
 
-       if((enda - sizeof(struct s)) < (char *)nxf)setbrk(DELTA);
+       SPACETEST(nxf, sizeof(struct s));
        p = nxf;
        p->pframe = frame;
        p->pip = ip;
        p = nxf;
        p->pframe = frame;
        p->pip = ip;
@@ -432,104 +439,165 @@ filep newip;
        cp = ap = 0;
        nchar = rchar = pendt = ch0 = ch = 0;
        frame = nxf;
        cp = ap = 0;
        nchar = rchar = pendt = ch0 = ch = 0;
        frame = nxf;
-       if(nxf->nargs == 0) nxf += 1;
-               else nxf = (struct s *)argtop;
+       if (nxf->nargs == 0) 
+               nxf += 1;
+       else 
+               nxf = (struct s *)argtop;
        return(ip = newip);
 }
        return(ip = newip);
 }
-char *setbrk(x)
-int x;
+
+
+char   *setbrk(x)
+int    x;
 {
 {
-       register char *i;
-       char *sbrk();
+       register char   *i;
+       char    *sbrk();
 
 
-       if((i = sbrk(x)) == MAXPTR){
+/* ought to be rounded up by sizeof(int) */
+       if (x % 2 == 1) 
+               x++;
+       if ( (i = sbrk(x)) >= (char *)MAXPTR) {
                prstrfl("Core limit reached.\n");
                edone(0100);
                prstrfl("Core limit reached.\n");
                edone(0100);
-       }else{
+       } else {
                enda = i + x;
        }
        return(i);
 }
                enda = i + x;
        }
        return(i);
 }
-getsn(){
+
+
+getsn()
+{
        register i;
 
        register i;
 
-       if((i=getach()) == 0)return(0);
-       if(i == '(')return(getrq());
-               else return(i);
+       if ((i = getach()) == 0)
+               return(0);
+       if (i == '(')
+               return(getrq());
+       else 
+               return(i);
 }
 }
-setstr(){
+
+
+setstr()
+{
        register i;
 
        lgf++;
        register i;
 
        lgf++;
-       if(((i=getsn()) == 0) ||
-          ((i=findmn(i)) == -1) ||
-          !(contab[i].rq & MMASK)){
+       if (    ((i = getsn()) == 0)
+            || ((i = findmn(i)) == -1)
+            ||  !(contab[i].rq & MMASK)) {
                lgf--;
                return(0);
                lgf--;
                return(0);
-       }else{
-               if((enda-2) < (char *)nxf)setbrk(DELTA);
+       } else {
+               SPACETEST(nxf, sizeof(struct s));
                nxf->nargs = 0;
                strflg++;
                lgf--;
                return(pushi(((filep)contab[i].x.mx)<<BLKBITS));
        }
 }
                nxf->nargs = 0;
                strflg++;
                lgf--;
                return(pushi(((filep)contab[i].x.mx)<<BLKBITS));
        }
 }
+
+typedef        int     tchar;
+#define        cbits(x)        ((x) & CMASK)
+
 collect()
 {
 collect()
 {
-       register i;
-       register int *strp;
-       int *lim;
-       int **argpp, **argppend;
-       int quote;
+       register j;
+       tchar i;
+       register tchar *strp;
+       tchar * lim;
+       tchar * *argpp, **argppend;
+       int     quote;
        struct s *savnxf;
 
        copyf++;
        nxf->nargs = 0;
        savnxf = nxf;
        struct s *savnxf;
 
        copyf++;
        nxf->nargs = 0;
        savnxf = nxf;
-       if(skip())goto rtn;
-       lim = (int *)(nxf = savnxf + sizeof(struct s)/sizeof(savnxf));
+       if (skip())
+               goto rtn;
+
+       {
+               char *memp;
+               memp = (char *)savnxf;
+               /*
+                *      1 s structure for the macro descriptor
+                *      APERMAC tchar *'s for pointers into the strings
+                *      space for the tchar's themselves
+                */
+               memp += sizeof(struct s);
+               /*
+                *      CPERMAC (the total # of characters for ALL arguments)
+                *      to a macros, has been carefully chosen
+                *      so that the distance between stack frames is < DELTA 
+                */
+#define        CPERMAC 200
+#define        APERMAC 9
+               memp += APERMAC * sizeof(tchar *);
+               memp += CPERMAC * sizeof(tchar);
+               nxf = (struct s*)memp;
+       }
+       lim = (tchar *)nxf;
+       argpp = (tchar **)(savnxf + 1);
+       argppend = &argpp[APERMAC];
+       SPACETEST(argppend, sizeof(tchar *));
+       strp = (tchar *)argppend;
+       /*
+        *      Zero out all the string pointers before filling them in.
+        */
+       for (j = 0; j < APERMAC; j++){
+               argpp[j] = (tchar *)0;
+       }
+#if 0
+       fprintf(stderr, "savnxf=0x%x,nxf=0x%x,argpp=0x%x,strp=argppend=0x%x,lim=0x%x,enda=0x%x\n",
+               savnxf, nxf, argpp, strp, lim, enda);
+#endif 0
        strflg = 0;
        strflg = 0;
-       if((argppend =
-               (argpp = (int **)savnxf+(sizeof(struct s)/sizeof(int **))) + 9)
-               > (int **)enda)setbrk(DELTA);
-       strp = (int *)argppend;
-       for(i=8; i>=0; i--)argpp[i] = 0;
-       while((argpp != argppend) && (!skip())){
+       while ((argpp != argppend) && (!skip())) {
                *argpp++ = strp;
                quote = 0;
                *argpp++ = strp;
                quote = 0;
-               if(((i = getch()) & CMASK) == '"')quote++;
-                       else ch = i;
-               while(1){
+               if (cbits(i = getch()) == '"')
+                       quote++;
+               else 
+                       ch = i;
+               while (1) {
                        i = getch();
                        i = getch();
-                       if( nlflg ||
-                         ((!quote) && ((i & CMASK) == ' ')))break;
-                       if(quote && ((i & CMASK) == '"') &&
-                         (((i=getch()) & CMASK) != '"')){
+                       if ( nlflg ||  (!quote && cbits(i) == ' '))
+                               break;
+                       if (   quote
+                           && (cbits(i) == '"')
+                           && (cbits(i = getch()) != '"')) {
                                ch = i;
                                break;
                        }
                        *strp++ = i;
                                ch = i;
                                break;
                        }
                        *strp++ = i;
-                       if(strflg && (strp >= lim)){
+                       if (strflg && (strp >= lim)) {
+#if 0
+                               fprintf(stderr, "strp=0x%x, lim = 0x%x\n",
+                                       strp, lim);
+#endif 0
                                prstrfl("Macro argument too long.\n");
                                copyf--;
                                edone(004);
                        }
                                prstrfl("Macro argument too long.\n");
                                copyf--;
                                edone(004);
                        }
-                       if((enda-4) <= (char *)strp)setbrk(DELTA);
+                       SPACETEST(strp, 3 * sizeof(tchar));
                }
                *strp++ = 0;
        }
        nxf = savnxf;
                }
                *strp++ = 0;
        }
        nxf = savnxf;
-       nxf->nargs = argpp -(int **)(nxf + 1);
+       nxf->nargs = argpp - (tchar **)(savnxf + 1);
        argtop = strp;
 rtn:
        copyf--;
 }
        argtop = strp;
 rtn:
        copyf--;
 }
+
+
 seta()
 {
        register i;
 
        if(((i = (getch() & CMASK) - '0') > 0) &&
 seta()
 {
        register i;
 
        if(((i = (getch() & CMASK) - '0') > 0) &&
-               (i <= 9) && (i <= frame->nargs))ap = *((int **)frame + i-1 + (sizeof(struct s)/sizeof(int **)));
+               (i <= APERMAC) && (i <= frame->nargs))ap = *((int **)frame + i-1 + (sizeof(struct s)/sizeof(int **)));
 }
 caseda(){
        app++;
 }
 caseda(){
        app++;