From: deraadt Date: Mon, 15 Jan 1996 01:12:11 +0000 (+0000) Subject: from netbsd: X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=29a0bfdc5382b21da509d0e2b206d3201c737f88;p=openbsd from netbsd: Handle multichar comment and quote delimiters (up to 5 characters, per the manual page). Takes care of PR#485. --- diff --git a/usr.bin/m4/eval.c b/usr.bin/m4/eval.c index a59c3095c08..b164f03fd7d 100644 --- a/usr.bin/m4/eval.c +++ b/usr.bin/m4/eval.c @@ -1,4 +1,4 @@ -/* $NetBSD: eval.c,v 1.4 1995/09/28 05:37:28 tls Exp $ */ +/* $NetBSD: eval.c,v 1.5 1996/01/13 23:25:23 pk Exp $ */ /* * Copyright (c) 1989, 1993 @@ -40,7 +40,7 @@ #if 0 static char sccsid[] = "@(#)eval.c 8.2 (Berkeley) 4/27/95"; #else -static char rcsid[] = "$NetBSD: eval.c,v 1.4 1995/09/28 05:37:28 tls Exp $"; +static char rcsid[] = "$NetBSD: eval.c,v 1.5 1996/01/13 23:25:23 pk Exp $"; #endif #endif /* not lint */ @@ -236,15 +236,24 @@ register int td; * argv[2]) */ if (argc > 3) { + int k; for (n = argc - 1; n > 3; n--) { - putback(rquote); + k = strlen(rquote); + while (k--) + putback(rquote[k]); pbstr(argv[n]); - putback(lquote); + k = strlen(lquote); + while (k--) + putback(lquote[k]); putback(','); } - putback(rquote); + k = strlen(rquote); + while (k--) + putback(rquote[k]); pbstr(argv[3]); - putback(lquote); + k = strlen(lquote); + while (k--) + putback(lquote[k]); } break; @@ -473,9 +482,13 @@ char *name; register ndptr p; if ((p = lookup(name)) != nil && p->defn != null) { - putback(rquote); + int n = strlen(rquote); + while (n--) + putback(rquote[n]); pbstr(p->defn); - putback(lquote); + n = strlen(lquote); + while (n--) + putback(lquote[n]); } } @@ -605,17 +618,17 @@ register int argc; { if (argc > 2) { if (*argv[2]) - lquote = *argv[2]; + strncpy(lquote, argv[2], MAXCCHARS); if (argc > 3) { if (*argv[3]) - rquote = *argv[3]; + strncpy(rquote, argv[3], MAXCCHARS); } else - rquote = lquote; + strcpy(rquote, lquote); } else { - lquote = LQUOTE; - rquote = RQUOTE; + lquote[0] = LQUOTE, lquote[1] = '\0'; + rquote[0] = RQUOTE, rquote[1] = '\0'; } } @@ -629,17 +642,17 @@ register int argc; { if (argc > 2) { if (*argv[2]) - scommt = *argv[2]; + strncpy(scommt, argv[2], MAXCCHARS); if (argc > 3) { if (*argv[3]) - ecommt = *argv[3]; + strncpy(ecommt, argv[3], MAXCCHARS); } else - ecommt = ECOMMT; + ecommt[0] = ECOMMT, ecommt[1] = '\0'; } else { - scommt = SCOMMT; - ecommt = ECOMMT; + scommt[0] = SCOMMT, scommt[1] = '\0'; + ecommt[0] = ECOMMT, ecommt[1] = '\0'; } } diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h index 1b33741de55..437ceff887a 100644 --- a/usr.bin/m4/extern.h +++ b/usr.bin/m4/extern.h @@ -1,4 +1,4 @@ -/* $NetBSD: extern.h,v 1.2 1995/09/28 05:37:34 tls Exp $ */ +/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -85,14 +85,14 @@ extern char *bp; /* first available character */ extern char buf[]; /* push-back buffer */ extern char *bufbase; /* buffer base for this ilevel */ extern char *bbase[]; /* buffer base per ilevel */ -extern char ecommt; /* end character for comment */ +extern char ecommt[]; /* end character for comment */ extern char *endest; /* end of string space */ extern char *endpbb; /* end of push-back buffer */ extern char *ep; /* first free char in strspace */ -extern char lquote; /* left quote character (`) */ +extern char lquote[]; /* left quote character (`) */ extern char *m4temp; /* filename for diversions */ extern char *m4wraps; /* m4wrap string default. */ extern char *null; /* as it says.. just a null. */ extern char *progname; /* program name */ -extern char rquote; /* right quote character (') */ -extern char scommt; /* start character for comment */ +extern char rquote[]; /* right quote character (') */ +extern char scommt[]; /* start character for comment */ diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c index 9c8bd87d735..25fe8fce55b 100644 --- a/usr.bin/m4/main.c +++ b/usr.bin/m4/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.10 1995/09/29 00:27:51 cgd Exp $ */ +/* $NetBSD: main.c,v 1.11 1996/01/13 23:25:26 pk Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -46,7 +46,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$NetBSD: main.c,v 1.10 1995/09/29 00:27:51 cgd Exp $"; +static char rcsid[] = "$NetBSD: main.c,v 1.11 1996/01/13 23:25:26 pk Exp $"; #endif #endif /* not lint */ @@ -89,10 +89,10 @@ int oindex = 0; /* diversion index.. */ char *null = ""; /* as it says.. just a null.. */ char *m4wraps = ""; /* m4wrap string default.. */ char *progname; /* name of this program */ -char lquote = LQUOTE; /* left quote character (`) */ -char rquote = RQUOTE; /* right quote character (') */ -char scommt = SCOMMT; /* start character for comment */ -char ecommt = ECOMMT; /* end character for comment */ +char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */ +char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */ +char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */ +char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */ struct keyblk keywrds[] = { /* m4 keywords to be installed */ "include", INCLTYPE, @@ -242,6 +242,38 @@ main(argc,argv) ndptr inspect(); +/* + * Look ahead (at most MAXCCHARS characters) for `token'. + * (on input `t == token[0]') + * Used for comment and quoting delimiters. + * Returns 1 if `token' present; copied to output. + * 0 if `token' not found; all characters pushed back + */ +int +do_look_ahead(t, token) + int t; + char *token; +{ + int i; + + if (t != token[0]) + oops("internal error", ""); + + for (i = 1; *++token; i++) { + t = gpbc(); + if (t == EOF || t != *token) { + if (t != EOF) + putback(t); + while (--i) + putback(*--token); + return 0; + } + } + return 1; +} + +#define LOOK_AHEAD(t, token) ((t)==(token)[0] && do_look_ahead(t,token)) + /* * macro - the work horse.. */ @@ -254,7 +286,8 @@ macro() { register int nlpar; cycle { - if ((t = gpbc()) == '_' || isalpha(t)) { + t = gpbc(); + if (t == '_' || isalpha(t)) { putback(t); if ((p = inspect(s = token)) == nil) { if (sp < 0) @@ -300,12 +333,13 @@ macro() { * non-alpha single-char token seen.. * [the order of else if .. stmts is important.] */ - else if (t == lquote) { /* strip quotes */ + else if (LOOK_AHEAD(t,lquote)) { /* strip quotes */ nlpar = 1; do { - if ((l = gpbc()) == rquote) + l = gpbc(); + if (LOOK_AHEAD(l,rquote)) nlpar--; - else if (l == lquote) + else if (LOOK_AHEAD(l,lquote)) nlpar++; else if (l == EOF) oops("missing right quote", ""); @@ -319,12 +353,26 @@ macro() { while (nlpar != 0); } - else if (sp < 0) { /* not in a macro at all */ - if (t == scommt) { /* comment handling here */ + else if (sp < 0 && LOOK_AHEAD(t, scommt)) { + int i; + for (i = 0; i < MAXCCHARS && scommt[i]; i++) + putc(scommt[i], active); + + for(;;) { + t = gpbc(); + if (LOOK_AHEAD(t, ecommt)) { + for (i = 0; i < MAXCCHARS && ecommt[i]; + i++) + putc(ecommt[i], active); + break; + } + if (t == EOF) + break; putc(t, active); - while ((t = gpbc()) != ecommt) - putc(t, active); } + } + + else if (sp < 0) { /* not in a macro at all */ putc(t, active); /* output directly.. */ } diff --git a/usr.bin/m4/mdef.h b/usr.bin/m4/mdef.h index 110cc5b67dc..dacbf60d99d 100644 --- a/usr.bin/m4/mdef.h +++ b/usr.bin/m4/mdef.h @@ -1,4 +1,4 @@ -/* $NetBSD: mdef.h,v 1.6 1995/09/28 05:37:39 tls Exp $ */ +/* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */ /* * Copyright (c) 1989, 1993 @@ -104,6 +104,7 @@ #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 ALL 1 #define TOP 0