summaryrefslogtreecommitdiffstats
path: root/usr.bin/m4
diff options
context:
space:
mode:
authorjmallett <jmallett@FreeBSD.org>2001-11-16 23:50:40 +0000
committerjmallett <jmallett@FreeBSD.org>2001-11-16 23:50:40 +0000
commitaaec7e94a098accbb09e8865d9725512f30c57fe (patch)
tree3c158fac601df8122f8e9bb1fba1706cc32f33b0 /usr.bin/m4
parent9cfdcac9f3f74feff4d4ac8b345980acca383ad8 (diff)
downloadFreeBSD-src-aaec7e94a098accbb09e8865d9725512f30c57fe.zip
FreeBSD-src-aaec7e94a098accbb09e8865d9725512f30c57fe.tar.gz
Import OpenBSD m4 as of today.
Diffstat (limited to 'usr.bin/m4')
-rw-r--r--usr.bin/m4/misc.c333
1 files changed, 224 insertions, 109 deletions
diff --git a/usr.bin/m4/misc.c b/usr.bin/m4/misc.c
index 2ed115b..e4bfa7e 100644
--- a/usr.bin/m4/misc.c
+++ b/usr.bin/m4/misc.c
@@ -1,3 +1,6 @@
+/* $OpenBSD: misc.c,v 1.26 2001/11/16 23:50:40 deraadt Exp $ */
+/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
+
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -35,7 +38,11 @@
*/
#ifndef lint
+#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
+#else
+static char rcsid[] = "$OpenBSD: misc.c,v 1.26 2001/11/16 23:50:40 deraadt Exp $";
+#endif
#endif /* not lint */
#include <sys/types.h>
@@ -43,42 +50,56 @@ static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stddef.h>
#include <string.h>
+#include <err.h>
#include "mdef.h"
#include "stdd.h"
#include "extern.h"
#include "pathnames.h"
+
+char *ep; /* first free char in strspace */
+static char *strspace; /* string space for evaluation */
+char *endest; /* end of string space */
+static size_t strsize = STRSPMAX;
+static size_t bufsize = BUFSIZE;
+
+char *buf; /* push-back buffer */
+char *bufbase; /* the base for current ilevel */
+char *bbase[MAXINP]; /* the base for each ilevel */
+char *bp; /* first available character */
+char *endpbb; /* end of push-back buffer */
+
+
/*
* find the index of second str in the first str.
*/
-int
+ptrdiff_t
indx(s1, s2)
-char *s1;
-char *s2;
+ const char *s1;
+ const char *s2;
{
- register char *t;
- register char *p;
- register char *m;
-
- for (p = s1; *p; p++) {
- for (t = p, m = s2; *m && *m == *t; m++, t++);
- if (!*m)
- return (p - s1);
- }
- return (-1);
+ char *t;
+
+ t = strstr(s1, s2);
+ if (t == NULL)
+ return (-1);
+ else
+ return (t - s1);
}
/*
* putback - push character back onto input
*/
void
putback(c)
-char c;
+ int c;
{
- if (bp < endpbb)
- *bp++ = c;
- else
- oops("too many characters pushed back");
+ if (c == EOF)
+ return;
+ if (bp >= endpbb)
+ enlarge_bufspace();
+ *bp++ = c;
}
/*
@@ -88,22 +109,15 @@ char c;
*/
void
pbstr(s)
-register char *s;
+ const char *s;
{
- register char *es;
- register char *zp;
-
- es = s;
- zp = bp;
-
- while (*es)
- es++;
- es--;
- while (es >= s)
- if (zp < endpbb)
- *zp++ = *es--;
- if ((bp = zp) == endpbb)
- oops("too many characters pushed back");
+ size_t n;
+
+ n = strlen(s);
+ while (endpbb - bp <= n)
+ enlarge_bufspace();
+ while (n > 0)
+ *bp++ = s[--n];
}
/*
@@ -111,9 +125,9 @@ register char *s;
*/
void
pbnum(n)
-int n;
+ int n;
{
- register int num;
+ int num;
num = (n < 0) ? -n : n;
do {
@@ -126,16 +140,83 @@ int n;
}
/*
+ * pbunsigned - convert unsigned long to string, push back on input.
+ */
+void
+pbunsigned(n)
+ unsigned long n;
+{
+ do {
+ putback(n % 10 + '0');
+ }
+ while ((n /= 10) > 0);
+}
+
+void
+initspaces()
+{
+ int i;
+
+ strspace = xalloc(strsize+1);
+ ep = strspace;
+ endest = strspace+strsize;
+ buf = (char *)xalloc(bufsize);
+ bufbase = buf;
+ bp = buf;
+ endpbb = buf + bufsize;
+ for (i = 0; i < MAXINP; i++)
+ bbase[i] = buf;
+}
+
+void
+enlarge_strspace()
+{
+ char *newstrspace;
+ int i;
+
+ strsize *= 2;
+ newstrspace = malloc(strsize + 1);
+ if (!newstrspace)
+ errx(1, "string space overflow");
+ memcpy(newstrspace, strspace, strsize/2);
+ for (i = 0; i <= sp; i++)
+ if (sstack[i])
+ mstack[i].sstr = (mstack[i].sstr - strspace)
+ + newstrspace;
+ ep = (ep-strspace) + newstrspace;
+ free(strspace);
+ strspace = newstrspace;
+ endest = strspace + strsize;
+}
+
+void
+enlarge_bufspace()
+{
+ char *newbuf;
+ int i;
+
+ bufsize *= 2;
+ newbuf = realloc(buf, bufsize);
+ 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+bufsize;
+}
+
+/*
* chrsave - put single char on string space
*/
void
chrsave(c)
-char c;
+ int c;
{
- if (ep < endest)
- *ep++ = c;
- else
- oops("string space overflow");
+ if (ep >= endest)
+ enlarge_strspace();
+ *ep++ = c;
}
/*
@@ -143,36 +224,26 @@ char c;
*/
void
getdiv(n)
-int n;
+ int n;
{
- register int c;
- register FILE *dfil;
+ int c;
if (active == outfile[n])
- oops("%s: diversion still active.", "undivert");
+ errx(1, "undivert: diversion still active");
+ rewind(outfile[n]);
+ while ((c = getc(outfile[n])) != EOF)
+ putc(c, active);
(void) fclose(outfile[n]);
outfile[n] = NULL;
- m4temp[UNIQUE] = n + '0';
- if ((dfil = fopen(m4temp, "r")) == NULL)
- oops("%s: cannot undivert.", m4temp);
- else
- while ((c = getc(dfil)) != EOF)
- putc(c, active);
- (void) fclose(dfil);
-
-#ifdef vms
- if (remove(m4temp))
-#else
- if (unlink(m4temp) == -1)
-#endif
- oops("%s: cannot unlink.", m4temp);
}
void
onintr(signo)
int signo;
{
- oops("interrupted.");
+#define intrmessage "m4: interrupted.\n"
+ write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
+ _exit(1);
}
/*
@@ -181,86 +252,130 @@ onintr(signo)
void
killdiv()
{
- register int n;
+ int n;
- for (n = 0; n < MAXOUT; n++)
+ for (n = 0; n < maxout; n++)
if (outfile[n] != NULL) {
(void) fclose(outfile[n]);
- m4temp[UNIQUE] = n + '0';
-#ifdef vms
- (void) remove(m4temp);
-#else
- (void) unlink(m4temp);
-#endif
}
}
-char *
+/*
+ * resizedivs: allocate more diversion files */
+void
+resizedivs(n)
+ int n;
+{
+ int i;
+
+ outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
+ if (outfile == NULL)
+ errx(1, "too many diverts %d", n);
+ for (i = maxout; i < n; i++)
+ outfile[i] = NULL;
+ maxout = n;
+}
+
+void *
xalloc(n)
-unsigned long n;
+ size_t n;
{
- register char *p = malloc(n);
+ char *p = malloc(n);
if (p == NULL)
- oops("malloc: %s", strerror(errno));
+ err(1, "malloc");
return p;
}
char *
xstrdup(s)
-const char *s;
+ const char *s;
{
- register char *p = strdup(s);
+ char *p = strdup(s);
if (p == NULL)
- oops("strdup: %s", strerror(errno));
+ err(1, "strdup");
return p;
}
-char *
-basename(s)
-register char *s;
+void
+usage()
+{
+ fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname] [-I dirname...]\n");
+ exit(1);
+}
+
+int
+obtain_char(f)
+ struct input_file *f;
+{
+ if (f->c == EOF)
+ return EOF;
+ else if (f->c == '\n')
+ f->lineno++;
+
+ f->c = fgetc(f->file);
+ return f->c;
+}
+
+void
+set_input(f, real, name)
+ struct input_file *f;
+ FILE *real;
+ const char *name;
{
- register char *p;
- extern char *strrchr();
+ f->file = real;
+ f->lineno = 1;
+ f->c = 0;
+ f->name = xstrdup(name);
+}
- if ((p = strrchr(s, '/')) == NULL)
- return s;
+void
+release_input(f)
+ struct input_file *f;
+{
+ if (f->file != stdin)
+ fclose(f->file);
+ f->c = EOF;
+ /*
+ * XXX can't free filename, as there might still be
+ * error information pointing to it.
+ */
+}
- return ++p;
+void
+doprintlineno(f)
+ struct input_file *f;
+{
+ pbunsigned(f->lineno);
}
void
-usage()
+doprintfilename(f)
+ struct input_file *f;
{
- fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
- exit(1);
+ pbstr(rquote);
+ pbstr(f->name);
+ pbstr(lquote);
+}
+
+/*
+ * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
+ * and later dump everything that was added since then to a file.
+ */
+size_t
+buffer_mark()
+{
+ return bp - buf;
}
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
void
-#if __STDC__
-oops(const char *fmt, ...)
-#else
-oops(fmt, va_alist)
- char *fmt;
- va_dcl
-#endif
+dump_buffer(f, m)
+ FILE *f;
+ size_t m;
{
- va_list ap;
-#if __STDC__
- va_start(ap, fmt);
-#else
- va_start(ap);
-#endif
- (void)fprintf(stderr, "%s: ", progname);
- (void)vfprintf(stderr, fmt, ap);
- va_end(ap);
- (void)fprintf(stderr, "\n");
- exit(1);
- /* NOTREACHED */
+ char *s;
+
+ for (s = bp; s-buf > m;)
+ fputc(*--s, f);
}
OpenPOWER on IntegriCloud