aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/m4
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2015-05-18 22:40:12 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2015-05-18 22:40:12 +0000
commit31129d4f23b03650ced18c38d66790873ce3963b (patch)
tree8e96dcb3eb8b70d5a12c81791048eeee34b1ee9f /usr.bin/m4
parent2b490bc747e67a2b9cecb12321ed4719600d31e9 (diff)
downloadsrc-31129d4f23b03650ced18c38d66790873ce3963b.tar.gz
src-31129d4f23b03650ced18c38d66790873ce3963b.zip
Notes
Diffstat (limited to 'usr.bin/m4')
-rw-r--r--usr.bin/m4/extern.h1
-rw-r--r--usr.bin/m4/gnum4.c20
-rw-r--r--usr.bin/m4/look.c68
-rw-r--r--usr.bin/m4/m4.16
-rw-r--r--usr.bin/m4/main.c7
-rw-r--r--usr.bin/m4/mdef.h21
-rw-r--r--usr.bin/m4/misc.c9
-rw-r--r--usr.bin/m4/parser.y8
8 files changed, 108 insertions, 32 deletions
diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h
index fd2d3ae57099..d10f9aea4f35 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 cac01ece6818..dcccf3ba135f 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 70497e01e593..383fbc6f7fd2 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 0e1d83a85319..b7dc13153e5c 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 ced881b2ca91..84ba03ac2782 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 0db78ef7b575..49b3f500fbed 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 5223a353151f..eeca68e643c1 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 $ */
/*
@@ -64,6 +64,7 @@ unsigned char *bbase[MAXINP]; /* the base for each ilevel */
unsigned char *bp; /* first available character */
unsigned char *endpbb; /* end of push-back buffer */
+
/*
* find the index of second str in the first str.
*/
@@ -186,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;
@@ -264,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);
@@ -276,7 +277,7 @@ m4errx(int exitstatus, const char *fmt, ...)
va_end(ap);
}
fprintf(stderr, "\n");
- exit(exitstatus);
+ exit(eval);
}
/*
diff --git a/usr.bin/m4/parser.y b/usr.bin/m4/parser.y
index 8e8ad8c5e264..3e4c80512a41 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 *);
%}