summaryrefslogtreecommitdiff
path: root/usr.bin/m4
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>1999-09-06 13:29:33 +0000
committerMarc Espie <espie@cvs.openbsd.org>1999-09-06 13:29:33 +0000
commit15b823abfcdf66f4094be75d4323e178f83e2f7b (patch)
treece85f1455ac25f179169b8f8b7014f1ff7134c09 /usr.bin/m4
parentcd410cfd0a085cc78c2b7b30e65640c2cf463dc4 (diff)
Remove STRSPACE limitations.
A bit wasteful, but not too intrusive. Also remove pushback buffer limitations, as this would be mostly useless otherwise. Incidentally, pushback buffer overflow detection in pbstr was wrong.
Diffstat (limited to 'usr.bin/m4')
-rw-r--r--usr.bin/m4/extern.h8
-rw-r--r--usr.bin/m4/main.c13
-rw-r--r--usr.bin/m4/mdef.h22
-rw-r--r--usr.bin/m4/misc.c130
4 files changed, 125 insertions, 48 deletions
diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h
index 0ffc9de3339..94908ff34f7 100644
--- a/usr.bin/m4/extern.h
+++ b/usr.bin/m4/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.7 1999/09/06 13:20:40 espie Exp $ */
+/* $OpenBSD: extern.h,v 1.8 1999/09/06 13:29:32 espie Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*-
@@ -70,6 +70,8 @@ void pbstr __P((char *));
void putback __P((int));
void remhash __P((char *, int));
void usage __P((void));
+void initspaces __P((void));
+char *compute_prevep __P((void));
extern ndptr hashtab[]; /* hash table for macros etc. */
extern stae mstack[]; /* stack of m4 machine */
@@ -81,12 +83,10 @@ extern int ilevel; /* input file stack pointer */
extern int oindex; /* diversion index. */
extern int sp; /* current m4 stack pointer */
extern pbent *bp; /* first available character */
-extern pbent buf[]; /* push-back buffer */
+extern pbent *buf; /* push-back buffer */
extern pbent *bufbase; /* buffer base for this ilevel */
extern pbent *bbase[]; /* buffer base per ilevel */
extern char ecommt[]; /* end character for comment */
-extern char *endest; /* end of string space */
-extern pbent *endpbb; /* end of push-back buffer */
extern char *ep; /* first free char in strspace */
extern char lquote[]; /* left quote character (`) */
extern char *m4wraps; /* m4wrap string default. */
diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c
index 1e7be8c578d..a2a6b39d3df 100644
--- a/usr.bin/m4/main.c
+++ b/usr.bin/m4/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.13 1999/09/06 13:24:59 espie Exp $ */
+/* $OpenBSD: main.c,v 1.14 1999/09/06 13:29:32 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
@@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: main.c,v 1.13 1999/09/06 13:24:59 espie Exp $";
+static char rcsid[] = "$OpenBSD: main.c,v 1.14 1999/09/06 13:29:32 espie Exp $";
#endif
#endif /* not lint */
@@ -72,15 +72,7 @@ static char rcsid[] = "$OpenBSD: main.c,v 1.13 1999/09/06 13:24:59 espie Exp $";
#include "pathnames.h"
ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
-pbent buf[BUFSIZE]; /* push-back buffer */
-pbent *bufbase = buf; /* the base for current ilevel */
-pbent *bbase[MAXINP]; /* the base for each ilevel */
-pbent *bp = buf; /* first available character */
-pbent *endpbb = buf+BUFSIZE; /* end of push-back buffer */
stae mstack[STACKMAX+1]; /* stack of m4 machine */
-char strspace[STRSPMAX+1]; /* string space for evaluation */
-char *ep = strspace; /* first free char in strspace */
-char *endest= strspace+STRSPMAX;/* end of string space */
int sp; /* current m4 stack pointer */
int fp; /* m4 call frame pointer */
FILE *infile[MAXINP]; /* input file stack (0=stdin) */
@@ -164,6 +156,7 @@ main(argc,argv)
signal(SIGINT, onintr);
initkwds();
+ initspaces();
while ((c = getopt(argc, argv, "tD:U:o:")) != -1)
switch(c) {
diff --git a/usr.bin/m4/mdef.h b/usr.bin/m4/mdef.h
index f4498c057ba..ed5baaa61c2 100644
--- a/usr.bin/m4/mdef.h
+++ b/usr.bin/m4/mdef.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: mdef.h,v 1.5 1999/09/06 13:10:48 espie Exp $ */
+/* $OpenBSD: mdef.h,v 1.6 1999/09/06 13:29:32 espie Exp $ */
/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */
/*
@@ -97,15 +97,15 @@
*/
#define EOS '\0'
-#define MAXINP 10 /* maximum include files */
-#define MAXOUT 10 /* maximum # of diversions */
-#define MAXSTR 512 /* maximum size of string */
-#define BUFSIZE 4096 /* size of pushback buffer */
-#define STACKMAX 1024 /* size of call stack */
-#define STRSPMAX 4096 /* size of string space */
-#define MAXTOK MAXSTR /* maximum chars in a tokn */
-#define HASHSIZE 199 /* maximum size of hashtab */
-#define MAXCCHARS 5 /* max size of comment/quote delim */
+#define MAXINP 10 /* maximum include files */
+#define MAXOUT 10 /* maximum # of diversions */
+#define MAXSTR 512 /* maximum size of string */
+#define BUFSIZE 4096 /* starting size of pushback buffer */
+#define STACKMAX 1024 /* size of call stack */
+#define STRSPMAX 4096 /* starting size of string space */
+#define MAXTOK MAXSTR /* maximum chars in a tokn */
+#define HASHSIZE 199 /* maximum size of hashtab */
+#define MAXCCHARS 5 /* max size of comment/quote delim */
#define ALL 1
#define TOP 0
@@ -177,6 +177,6 @@ typedef short pbent; /* pushback entry; needs to hold chars + EOF */
*/
#define PARLEV (mstack[fp].sfra)
#define CALTYP (mstack[fp-1].sfra)
-#define PREVEP (mstack[fp+3].sstr)
+#define PREVEP compute_prevep()
#define PREVSP (fp-3)
#define PREVFP (mstack[fp-2].sfra)
diff --git a/usr.bin/m4/misc.c b/usr.bin/m4/misc.c
index 84d923b5d3e..07e15a89f07 100644
--- a/usr.bin/m4/misc.c
+++ b/usr.bin/m4/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.8 1999/09/06 13:20:40 espie Exp $ */
+/* $OpenBSD: misc.c,v 1.9 1999/09/06 13:29:32 espie Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
/*
@@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: misc.c,v 1.8 1999/09/06 13:20:40 espie Exp $";
+static char rcsid[] = "$OpenBSD: misc.c,v 1.9 1999/09/06 13:29:32 espie Exp $";
#endif
#endif /* not lint */
@@ -58,6 +58,23 @@ static char rcsid[] = "$OpenBSD: misc.c,v 1.8 1999/09/06 13:20:40 espie Exp $";
#include "extern.h"
#include "pathnames.h"
+
+char *ep; /* first free char in strspace */
+static char *strspace; /* string space for evaluation */
+static char *endest; /* end of string space */
+static size_t strsize = STRSPMAX;
+static size_t bufsize = BUFSIZE;
+static int low_sp = 0;
+
+pbent *buf; /* push-back buffer */
+pbent *bufbase; /* the base for current ilevel */
+pbent *bbase[MAXINP]; /* the base for each ilevel */
+pbent *bp; /* first available character */
+static pbent *endpbb; /* end of push-back buffer */
+
+
+static void enlarge_bufspace();
+static void enlarge_strspace();
/*
* find the index of second str in the first str.
*/
@@ -81,10 +98,9 @@ void
putback(c)
pbent c;
{
- if (bp < endpbb)
- *bp++ = c;
- else
- errx(1, "too many characters pushed back");
+ if (bp >= endpbb)
+ enlarge_bufspace();
+ *bp++ = c;
}
/*
@@ -96,20 +112,13 @@ void
pbstr(s)
register char *s;
{
- register char *es;
- pbent *zp;
-
- es = s;
- zp = bp;
-
- while (*es)
- es++;
- es--;
- while (es >= s)
- if (zp < endpbb)
- *zp++ = *es--;
- if ((bp = zp) == endpbb)
- errx(1, "too many characters pushed back");
+ size_t n;
+
+ n = strlen(s);
+ while (endpbb - bp < n)
+ enlarge_bufspace();
+ while (n > 0)
+ *bp++ = s[--n];
}
/*
@@ -131,6 +140,66 @@ int n;
putback('-');
}
+
+void
+initspaces()
+{
+ int i;
+
+ strspace = xalloc(strsize+1);
+ ep = strspace;
+ endest = strspace+strsize;
+ buf = (pbent *)xalloc(bufsize * sizeof(pbent));
+ bufbase = buf;
+ bp = buf;
+ endpbb = buf + bufsize;
+ for (i = 0; i < MAXINP; i++)
+ bbase[i] = buf;
+}
+
+/* XXX when chrsave is called, the current argument is
+ * always topmost on the stack. We make use of this to
+ * duplicate it transparently, and to reclaim the correct
+ * space when the stack is unwound.
+ */
+static
+void enlarge_strspace()
+{
+ char *newstrspace;
+
+ low_sp = sp;
+ strsize *= 2;
+ newstrspace = malloc(strsize + 1);
+ if (!newstrspace)
+ errx(1, "string space overflow");
+ memcpy(newstrspace, strspace, strsize/2);
+ /* reclaim memory in the easy, common case. */
+ if (ep == strspace)
+ free(strspace);
+ mstack[sp].sstr = (mstack[sp].sstr-strspace) + newstrspace;
+ ep = (ep-strspace) + newstrspace;
+ strspace = newstrspace;
+ endest = strspace + strsize;
+}
+
+static
+void enlarge_bufspace()
+{
+ pbent *newbuf;
+ int i;
+
+ bufsize *= 2;
+ newbuf = realloc(buf, bufsize*sizeof(pbent));
+ if (!newbuf)
+ errx(1, "too many characters pushed back");
+ for (i = 0; i < MAXINP; i++)
+ bbase[i] = (bbase[i]-buf)+newbuf;
+ bp = (bp-buf)+newbuf;
+ bufbase = (bufbase-buf)+newbuf;
+ buf = newbuf;
+ endpbb = buf+strsize;
+}
+
/*
* chrsave - put single char on string space
*/
@@ -138,10 +207,25 @@ void
chrsave(c)
char c;
{
- if (ep < endest)
- *ep++ = c;
+ if (ep >= endest)
+ enlarge_strspace();
+ *ep++ = c;
+}
+
+/*
+ * so we reclaim what string space we can
+ */
+char *
+compute_prevep()
+{
+ if (fp+3 <= low_sp)
+ {
+ return strspace;
+ }
else
- errx(1, "string space overflow");
+ {
+ return mstack[fp+3].sstr;
+ }
}
/*