138825Sbostic /*
2*62080Sbostic * Copyright (c) 1989, 1993
3*62080Sbostic * The Regents of the University of California. All rights reserved.
438825Sbostic *
538825Sbostic * This code is derived from software contributed to Berkeley by
651769Sbostic * Ozan Yigit at York University.
738825Sbostic *
842689Sbostic * %sccs.include.redist.c%
938825Sbostic */
1038825Sbostic
1138825Sbostic #ifndef lint
12*62080Sbostic static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 06/06/93";
1338825Sbostic #endif /* not lint */
1438825Sbostic
1550960Sbostic #include <sys/types.h>
1650960Sbostic #include <errno.h>
1746697Sbostic #include <unistd.h>
1846697Sbostic #include <stdio.h>
1946697Sbostic #include <stdlib.h>
2046697Sbostic #include <string.h>
2138825Sbostic #include "mdef.h"
2250960Sbostic #include "stdd.h"
2350960Sbostic #include "extern.h"
2446697Sbostic #include "pathnames.h"
2550960Sbostic
2638825Sbostic /*
2750960Sbostic * find the index of second str in the first str.
2838825Sbostic */
2950960Sbostic int
indx(s1,s2)3038825Sbostic indx(s1, s2)
3138825Sbostic char *s1;
3238825Sbostic char *s2;
3338825Sbostic {
3450960Sbostic register char *t;
3550960Sbostic register char *p;
3650960Sbostic register char *m;
3750960Sbostic
3850960Sbostic for (p = s1; *p; p++) {
3950960Sbostic for (t = p, m = s2; *m && *m == *t; m++, t++);
4050960Sbostic if (!*m)
4150960Sbostic return (p - s1);
4250960Sbostic }
4350960Sbostic return (-1);
4438825Sbostic }
4538825Sbostic /*
4638825Sbostic * putback - push character back onto input
4738825Sbostic */
4850960Sbostic void
putback(c)4950960Sbostic putback(c)
5038825Sbostic char c;
5138825Sbostic {
5250960Sbostic if (bp < endpbb)
5350960Sbostic *bp++ = c;
5450960Sbostic else
5550960Sbostic oops("too many characters pushed back");
5638825Sbostic }
5750960Sbostic
5838825Sbostic /*
5938825Sbostic * pbstr - push string back onto input
6038825Sbostic * putback is replicated to improve
6138825Sbostic * performance.
6238825Sbostic */
6350960Sbostic void
pbstr(s)6438825Sbostic pbstr(s)
6538825Sbostic register char *s;
6638825Sbostic {
6750960Sbostic register char *es;
6838825Sbostic register char *zp;
6938825Sbostic
7038825Sbostic es = s;
7138825Sbostic zp = bp;
7238825Sbostic
7350960Sbostic while (*es)
7450960Sbostic es++;
7550960Sbostic es--;
7650960Sbostic while (es >= s)
7750960Sbostic if (zp < endpbb)
7850960Sbostic *zp++ = *es--;
7950960Sbostic if ((bp = zp) == endpbb)
8050960Sbostic oops("too many characters pushed back");
8138825Sbostic }
8250960Sbostic
8338825Sbostic /*
8438825Sbostic * pbnum - convert number to string, push back on input.
8538825Sbostic */
8650960Sbostic void
pbnum(n)8750960Sbostic pbnum(n)
8838825Sbostic int n;
8938825Sbostic {
9050960Sbostic register int num;
9138825Sbostic
9250960Sbostic num = (n < 0) ? -n : n;
9350960Sbostic do {
9450960Sbostic putback(num % 10 + '0');
9550960Sbostic }
9650960Sbostic while ((num /= 10) > 0);
9750960Sbostic
9850960Sbostic if (n < 0)
9950960Sbostic putback('-');
10038825Sbostic }
10150960Sbostic
10238825Sbostic /*
10338825Sbostic * chrsave - put single char on string space
10438825Sbostic */
10550960Sbostic void
chrsave(c)10650960Sbostic chrsave(c)
10738825Sbostic char c;
10838825Sbostic {
10950960Sbostic if (ep < endest)
11050960Sbostic *ep++ = c;
11150960Sbostic else
11250960Sbostic oops("string space overflow");
11338825Sbostic }
11450960Sbostic
11538825Sbostic /*
11650960Sbostic * read in a diversion file, and dispose it.
11738825Sbostic */
11850960Sbostic void
getdiv(n)11950960Sbostic getdiv(n)
12050960Sbostic int n;
12150960Sbostic {
12250960Sbostic register int c;
12350960Sbostic register FILE *dfil;
12438825Sbostic
12550960Sbostic if (active == outfile[n])
12650960Sbostic oops("%s: diversion still active.", "undivert");
12750960Sbostic (void) fclose(outfile[n]);
12850960Sbostic outfile[n] = NULL;
12950960Sbostic m4temp[UNIQUE] = n + '0';
13050960Sbostic if ((dfil = fopen(m4temp, "r")) == NULL)
13150960Sbostic oops("%s: cannot undivert.", m4temp);
13250960Sbostic else
13350960Sbostic while ((c = getc(dfil)) != EOF)
13450960Sbostic putc(c, active);
13550960Sbostic (void) fclose(dfil);
13650960Sbostic
13750960Sbostic #ifdef vms
13850960Sbostic if (remove(m4temp))
13950960Sbostic #else
14038825Sbostic if (unlink(m4temp) == -1)
14150960Sbostic #endif
14250960Sbostic oops("%s: cannot unlink.", m4temp);
14338825Sbostic }
14450960Sbostic
14550960Sbostic void
onintr(signo)14650960Sbostic onintr(signo)
14750960Sbostic int signo;
14838825Sbostic {
14950960Sbostic oops("interrupted.");
15038825Sbostic }
15150960Sbostic
15238825Sbostic /*
15350960Sbostic * killdiv - get rid of the diversion files
15438825Sbostic */
15546697Sbostic void
killdiv()15650960Sbostic killdiv()
15750960Sbostic {
15850960Sbostic register int n;
15950960Sbostic
16050960Sbostic for (n = 0; n < MAXOUT; n++)
16150960Sbostic if (outfile[n] != NULL) {
16250960Sbostic (void) fclose(outfile[n]);
16350960Sbostic m4temp[UNIQUE] = n + '0';
16450960Sbostic #ifdef vms
16550960Sbostic (void) remove(m4temp);
16650960Sbostic #else
16750960Sbostic (void) unlink(m4temp);
16850960Sbostic #endif
16950960Sbostic }
17038825Sbostic }
17150960Sbostic
17250960Sbostic char *
xalloc(n)17350960Sbostic xalloc(n)
17450960Sbostic unsigned long n;
17550960Sbostic {
17650960Sbostic register char *p = malloc(n);
17750960Sbostic
17850960Sbostic if (p == NULL)
17950960Sbostic oops("malloc: %s", strerror(errno));
18050960Sbostic return p;
18138825Sbostic }
18250960Sbostic
18350960Sbostic char *
xstrdup(s)18450960Sbostic xstrdup(s)
18550960Sbostic const char *s;
18650960Sbostic {
18750960Sbostic register char *p = strdup(s);
18850960Sbostic if (p == NULL)
18950960Sbostic oops("strdup: %s", strerror(errno));
19050960Sbostic return p;
19138825Sbostic }
19250960Sbostic
19350960Sbostic char *
basename(s)19450960Sbostic basename(s)
19550960Sbostic register char *s;
19650960Sbostic {
19750960Sbostic register char *p;
19850960Sbostic extern char *strrchr();
19950960Sbostic
20050960Sbostic if ((p = strrchr(s, '/')) == NULL)
20150960Sbostic return s;
20250960Sbostic
20350960Sbostic return ++p;
20450960Sbostic }
20550960Sbostic
20650960Sbostic void
usage()20750960Sbostic usage()
20850960Sbostic {
20950960Sbostic fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
21050960Sbostic exit(1);
21150960Sbostic }
21250960Sbostic
21350960Sbostic #if __STDC__
21450960Sbostic #include <stdarg.h>
21550960Sbostic #else
21650960Sbostic #include <varargs.h>
21750960Sbostic #endif
21850960Sbostic
21950960Sbostic void
22050960Sbostic #if __STDC__
oops(const char * fmt,...)22150960Sbostic oops(const char *fmt, ...)
22250960Sbostic #else
22350960Sbostic oops(fmt, va_alist)
22450960Sbostic char *fmt;
22550960Sbostic va_dcl
22650960Sbostic #endif
22750960Sbostic {
22850960Sbostic va_list ap;
22950960Sbostic #if __STDC__
23050960Sbostic va_start(ap, fmt);
23150960Sbostic #else
23250960Sbostic va_start(ap);
23350960Sbostic #endif
23450960Sbostic (void)fprintf(stderr, "%s: ", progname);
23550960Sbostic (void)vfprintf(stderr, fmt, ap);
23650960Sbostic va_end(ap);
23750960Sbostic (void)fprintf(stderr, "\n");
23850960Sbostic exit(1);
23950960Sbostic /* NOTREACHED */
24050960Sbostic }
241