diff options
author | sjg <sjg@FreeBSD.org> | 2015-05-27 01:19:58 +0000 |
---|---|---|
committer | sjg <sjg@FreeBSD.org> | 2015-05-27 01:19:58 +0000 |
commit | 65145fa4c81da358fcbc3b650156dab705dfa34e (patch) | |
tree | 55c065b6730aaac2afb6c29933ee6ec5fa4c4249 /usr.bin/m4 | |
parent | 60ff4eb0dff94a04d75d0d52a3957aaaf5f8c693 (diff) | |
parent | e6b664c390af88d4a87208bc042ce503da664c3b (diff) | |
download | FreeBSD-src-65145fa4c81da358fcbc3b650156dab705dfa34e.zip FreeBSD-src-65145fa4c81da358fcbc3b650156dab705dfa34e.tar.gz |
Merge sync of head
Diffstat (limited to 'usr.bin/m4')
-rw-r--r-- | usr.bin/m4/Makefile | 3 | ||||
-rw-r--r-- | usr.bin/m4/Makefile.depend | 1 | ||||
-rw-r--r-- | usr.bin/m4/extern.h | 1 | ||||
-rw-r--r-- | usr.bin/m4/gnum4.c | 20 | ||||
-rw-r--r-- | usr.bin/m4/look.c | 68 | ||||
-rw-r--r-- | usr.bin/m4/m4.1 | 6 | ||||
-rw-r--r-- | usr.bin/m4/main.c | 7 | ||||
-rw-r--r-- | usr.bin/m4/mdef.h | 21 | ||||
-rw-r--r-- | usr.bin/m4/misc.c | 25 | ||||
-rw-r--r-- | usr.bin/m4/parser.y | 8 |
10 files changed, 109 insertions, 51 deletions
diff --git a/usr.bin/m4/Makefile b/usr.bin/m4/Makefile index 9153e71..d955075 100644 --- a/usr.bin/m4/Makefile +++ b/usr.bin/m4/Makefile @@ -8,8 +8,7 @@ PROG= m4 CFLAGS+=-DEXTENDED -I${.CURDIR} -I${.CURDIR}/../../lib/libohash -DPADD= ${LIBY} ${LIBL} ${LIBM} ${LIBOHASH} -LDADD= -ly -ll -lm ${LDOHASH} +LIBADD= y l m ohash NO_WMISSING_VARIABLE_DECLARATIONS= diff --git a/usr.bin/m4/Makefile.depend b/usr.bin/m4/Makefile.depend index 5ef57d9..7c87b49 100644 --- a/usr.bin/m4/Makefile.depend +++ b/usr.bin/m4/Makefile.depend @@ -14,6 +14,7 @@ DIRDEPS = \ lib/liby \ lib/msun \ usr.bin/lex/lib \ + usr.bin/yacc.host \ .include <dirdeps.mk> diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h index fd2d3ae..d10f9ae 100644 --- a/usr.bin/m4/extern.h +++ b/usr.bin/m4/extern.h @@ -43,7 +43,6 @@ extern unsigned long expansion_id; /* expr.c */ extern int expr(const char *); -extern int32_t end_result; /* gnum4.c */ extern void addtoincludepath(const char *); diff --git a/usr.bin/m4/gnum4.c b/usr.bin/m4/gnum4.c index cac01ec..dcccf3b 100644 --- a/usr.bin/m4/gnum4.c +++ b/usr.bin/m4/gnum4.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gnum4.c,v 1.46 2014/07/10 14:12:31 espie Exp $ */ +/* $OpenBSD: gnum4.c,v 1.50 2015/04/29 00:13:26 millert Exp $ */ /* * Copyright (c) 1999 Marc Espie @@ -31,7 +31,6 @@ __FBSDID("$FreeBSD$"); * functions needed to support gnu-m4 extensions, including a fake freezing */ -#include <sys/param.h> #include <sys/types.h> #include <sys/wait.h> #include <ctype.h> @@ -40,10 +39,12 @@ __FBSDID("$FreeBSD$"); #include <regex.h> #include <stddef.h> #include <stdlib.h> +#include <stdint.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> +#include <limits.h> #include "mdef.h" #include "stdd.h" #include "extern.h" @@ -76,9 +77,7 @@ new_path_entry(const char *dirname) n = malloc(sizeof(struct path_entry)); if (!n) errx(1, "out of memory"); - n->name = strdup(dirname); - if (!n->name) - errx(1, "out of memory"); + n->name = xstrdup(dirname); n->next = 0; return n; } @@ -113,9 +112,7 @@ ensure_m4path(void) if (!envpath) return; /* for portability: getenv result is read-only */ - envpath = strdup(envpath); - if (!envpath) - errx(1, "out of memory"); + envpath = xstrdup(envpath); for (sweep = envpath; (path = strsep(&sweep, ":")) != NULL;) addtoincludepath(path); @@ -126,7 +123,7 @@ static struct input_file * dopath(struct input_file *i, const char *filename) { - char path[MAXPATHLEN]; + char path[PATH_MAX]; struct path_entry *pe; FILE *f; @@ -214,8 +211,11 @@ addchars(const char *c, size_t n) while (current + n > bufsize) { if (bufsize == 0) bufsize = 1024; - else + else if (bufsize <= SIZE_MAX/2) { bufsize *= 2; + } else { + errx(1, "size overflow"); + } buffer = xrealloc(buffer, bufsize, NULL); } memcpy(buffer+current, c, n); diff --git a/usr.bin/m4/look.c b/usr.bin/m4/look.c index 70497e0..383fbc6 100644 --- a/usr.bin/m4/look.c +++ b/usr.bin/m4/look.c @@ -1,4 +1,4 @@ -/* $OpenBSD: look.c,v 1.23 2014/05/12 19:11:19 espie Exp $ */ +/* $OpenBSD: look.c,v 1.24 2014/12/21 09:33:12 espie Exp $ */ /* * Copyright (c) 1989, 1993 @@ -56,6 +56,9 @@ static void hash_free(void *, void *); static void *element_alloc(size_t, void *); static void setup_definition(struct macro_definition *, const char *, const char *); +static void free_definition(char *); +static void keep(char *); +static int string_in_use(const char *); static struct ohash_info macro_info = { offsetof(struct ndblock, name), @@ -155,7 +158,7 @@ macro_define(const char *name, const char *defn) ndptr n = create_entry(name); if (n->d != NULL) { if (n->d->defn != null) - free(n->d->defn); + free_definition(n->d->defn); } else { n->d = xalloc(sizeof(struct macro_definition), NULL); n->d->next = NULL; @@ -273,3 +276,64 @@ macro_getbuiltin(const char *name) else return p; } + +/* XXX things are slightly more complicated than they seem. + * a macro may actually be "live" (in the middle of an expansion + * on the stack. + * So we actually may need to place it in an array for later... + */ + +static int kept_capacity = 0; +static int kept_size = 0; +static char **kept = NULL; + +static void +keep(char *ptr) +{ + if (kept_capacity <= kept_size) { + if (kept_capacity) + kept_capacity *= 2; + else + kept_capacity = 50; + kept = xreallocarray(kept, kept_capacity, + sizeof(char *), "Out of memory while saving %d strings\n", + kept_capacity); + } + kept[kept_size++] = ptr; +} + +static int +string_in_use(const char *ptr) +{ + int i; + for (i = 0; i <= sp; i++) { + if (sstack[i] == STORAGE_MACRO && mstack[i].sstr == ptr) + return 1; + } + return 0; +} + + +static void +free_definition(char *ptr) +{ + int i; + + /* first try to free old strings */ + for (i = 0; i < kept_size; i++) { + if (!string_in_use(kept[i])) { + kept_size--; + free(kept[i]); + if (i != kept_size) + kept[i] = kept[kept_size]; + i--; + } + } + + /* then deal with us */ + if (string_in_use(ptr)) + keep(ptr); + else + free(ptr); +} + diff --git a/usr.bin/m4/m4.1 b/usr.bin/m4/m4.1 index 0e1d83a..b7dc131 100644 --- a/usr.bin/m4/m4.1 +++ b/usr.bin/m4/m4.1 @@ -33,7 +33,7 @@ .\" .\" $FreeBSD$ .\" -.Dd January 12, 2014 +.Dd $Mdocdate: April 14 2014 $ .Dt M4 1 .Os .Sh NAME @@ -98,7 +98,9 @@ recognized as special when not followed by an open parenthesis. .Pp The options are as follows: .Bl -tag -width Ds -.It Fl D Ns Ar name Ns Op Pf = Ns Ar value +.It Fl D Ns Ar name Ns Oo +.Pf = Ns Ar value +.Oc Define the symbol .Ar name to have some value (or diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c index ced881b..84ba03a 100644 --- a/usr.bin/m4/main.c +++ b/usr.bin/m4/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.83 2014/05/12 19:11:19 espie Exp $ */ +/* $OpenBSD: main.c,v 1.84 2014/12/21 09:33:12 espie Exp $ */ /* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */ /*- @@ -144,6 +144,9 @@ static struct keyblk keywrds[] = { /* m4 keywords to be installed */ #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk)) +extern int optind; +extern char *optarg; + #define MAXRECORD 50 static struct position { char *name; @@ -396,7 +399,7 @@ macro(void) /* * now push the string arguments: */ - pushs1(macro_getdef(p)->defn); /* defn string */ + pushdef(p); /* defn string */ pushs1((char *)macro_name(p)); /* macro name */ pushs(ep); /* start next..*/ diff --git a/usr.bin/m4/mdef.h b/usr.bin/m4/mdef.h index 0db78ef..49b3f50 100644 --- a/usr.bin/m4/mdef.h +++ b/usr.bin/m4/mdef.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mdef.h,v 1.31 2011/09/27 07:24:02 espie Exp $ */ +/* $OpenBSD: mdef.h,v 1.32 2014/12/21 09:33:12 espie Exp $ */ /* $NetBSD: mdef.h,v 1.7 1996/01/13 23:25:27 pk Exp $ */ /* @@ -164,6 +164,10 @@ struct input_file { int c; }; +#define STORAGE_STRSPACE 0 +#define STORAGE_MACRO 1 +#define STORAGE_OTHER 2 + #define CURRENT_NAME (infile[ilevel].name) #define CURRENT_LINE (infile[ilevel].lineno) /* @@ -179,7 +183,7 @@ struct input_file { if (++sp == (int)STACKMAX) \ enlarge_stack();\ mstack[sp].sfra = (x); \ - sstack[sp] = 0; \ + sstack[sp] = STORAGE_OTHER; \ } while (0) #define pushs(x) \ @@ -187,7 +191,7 @@ struct input_file { if (++sp == (int)STACKMAX) \ enlarge_stack();\ mstack[sp].sstr = (x); \ - sstack[sp] = 1; \ + sstack[sp] = STORAGE_STRSPACE; \ } while (0) #define pushs1(x) \ @@ -195,8 +199,17 @@ struct input_file { if (++sp == (int)STACKMAX) \ enlarge_stack();\ mstack[sp].sstr = (x); \ - sstack[sp] = 0; \ + sstack[sp] = STORAGE_OTHER; \ + } while (0) + +#define pushdef(p) \ + do { \ + if (++sp == (int)STACKMAX) \ + enlarge_stack();\ + mstack[sp].sstr = macro_getdef(p)->defn;\ + sstack[sp] = STORAGE_MACRO; \ } while (0) + /* * . . diff --git a/usr.bin/m4/misc.c b/usr.bin/m4/misc.c index 1959448..eeca68e 100644 --- a/usr.bin/m4/misc.c +++ b/usr.bin/m4/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.44 2014/05/12 19:11:19 espie Exp $ */ +/* $OpenBSD: misc.c,v 1.45 2014/12/21 09:33:12 espie Exp $ */ /* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */ /* @@ -187,7 +187,7 @@ enlarge_strspace(void) errx(1, "string space overflow"); memcpy(newstrspace, strspace, strsize/2); for (i = 0; i <= sp; i++) - if (sstack[i]) + if (sstack[i] == STORAGE_STRSPACE) mstack[i].sstr = (mstack[i].sstr - strspace) + newstrspace; ep = (ep-strspace) + newstrspace; @@ -265,7 +265,7 @@ killdiv(void) extern char *__progname; void -m4errx(int exitstatus, const char *fmt, ...) +m4errx(int eval, const char *fmt, ...) { fprintf(stderr, "%s: ", __progname); fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE); @@ -277,7 +277,7 @@ m4errx(int exitstatus, const char *fmt, ...) va_end(ap); } fprintf(stderr, "\n"); - exit(exitstatus); + exit(eval); } /* @@ -352,23 +352,6 @@ xrealloc(void *old, size_t n, const char *fmt, ...) return p; } -/* - * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX - * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW - */ -#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) - -static void * -reallocarray(void *optr, size_t nmemb, size_t size) -{ - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && - nmemb > 0 && SIZE_MAX / nmemb < size) { - errno = ENOMEM; - return NULL; - } - return realloc(optr, size * nmemb); -} - void * xreallocarray(void *old, size_t s1, size_t s2, const char *fmt, ...) { diff --git a/usr.bin/m4/parser.y b/usr.bin/m4/parser.y index 8e8ad8c..3e4c8051 100644 --- a/usr.bin/m4/parser.y +++ b/usr.bin/m4/parser.y @@ -19,15 +19,9 @@ */ #include <math.h> -#include <stddef.h> -#include <stdio.h> #include <stdint.h> - -#include "mdef.h" -#include "extern.h" - #define YYSTYPE int32_t - +extern int32_t end_result; extern int yylex(void); extern int yyerror(const char *); %} |