xref: /freebsd/usr.bin/m4/misc.c (revision 7c5eeb390e76dea3ba24be99ceef17e1137e251c)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Ozan Yigit at York University.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
399b50d902SRodney W. Grimes #endif /* not lint */
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <sys/types.h>
429b50d902SRodney W. Grimes #include <errno.h>
439b50d902SRodney W. Grimes #include <unistd.h>
449b50d902SRodney W. Grimes #include <stdio.h>
459b50d902SRodney W. Grimes #include <stdlib.h>
469b50d902SRodney W. Grimes #include <string.h>
479b50d902SRodney W. Grimes #include "mdef.h"
489b50d902SRodney W. Grimes #include "stdd.h"
499b50d902SRodney W. Grimes #include "extern.h"
509b50d902SRodney W. Grimes #include "pathnames.h"
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes /*
539b50d902SRodney W. Grimes  * find the index of second str in the first str.
549b50d902SRodney W. Grimes  */
559b50d902SRodney W. Grimes int
569b50d902SRodney W. Grimes indx(s1, s2)
579b50d902SRodney W. Grimes char *s1;
589b50d902SRodney W. Grimes char *s2;
599b50d902SRodney W. Grimes {
609b50d902SRodney W. Grimes 	register char *t;
619b50d902SRodney W. Grimes 	register char *p;
629b50d902SRodney W. Grimes 	register char *m;
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes 	for (p = s1; *p; p++) {
659b50d902SRodney W. Grimes 		for (t = p, m = s2; *m && *m == *t; m++, t++);
669b50d902SRodney W. Grimes 		if (!*m)
679b50d902SRodney W. Grimes 			return (p - s1);
689b50d902SRodney W. Grimes 	}
699b50d902SRodney W. Grimes 	return (-1);
709b50d902SRodney W. Grimes }
719b50d902SRodney W. Grimes /*
729b50d902SRodney W. Grimes  *  putback - push character back onto input
739b50d902SRodney W. Grimes  */
749b50d902SRodney W. Grimes void
759b50d902SRodney W. Grimes putback(c)
767c5eeb39SAndrey A. Chernov int c;
779b50d902SRodney W. Grimes {
787c5eeb39SAndrey A. Chernov 	if (c == EOF)
797c5eeb39SAndrey A. Chernov 		c = 0;
809b50d902SRodney W. Grimes 	if (bp < endpbb)
819b50d902SRodney W. Grimes 		*bp++ = c;
829b50d902SRodney W. Grimes 	else
839b50d902SRodney W. Grimes 		oops("too many characters pushed back");
849b50d902SRodney W. Grimes }
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes /*
879b50d902SRodney W. Grimes  *  pbstr - push string back onto input
889b50d902SRodney W. Grimes  *          putback is replicated to improve
899b50d902SRodney W. Grimes  *          performance.
909b50d902SRodney W. Grimes  */
919b50d902SRodney W. Grimes void
929b50d902SRodney W. Grimes pbstr(s)
937c5eeb39SAndrey A. Chernov register unsigned char *s;
949b50d902SRodney W. Grimes {
957c5eeb39SAndrey A. Chernov 	register unsigned char *es;
967c5eeb39SAndrey A. Chernov 	register unsigned char *zp;
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes 	es = s;
999b50d902SRodney W. Grimes 	zp = bp;
1009b50d902SRodney W. Grimes 
1019b50d902SRodney W. Grimes 	while (*es)
1029b50d902SRodney W. Grimes 		es++;
1039b50d902SRodney W. Grimes 	es--;
1049b50d902SRodney W. Grimes 	while (es >= s)
1059b50d902SRodney W. Grimes 		if (zp < endpbb)
1069b50d902SRodney W. Grimes 			*zp++ = *es--;
1079b50d902SRodney W. Grimes 	if ((bp = zp) == endpbb)
1089b50d902SRodney W. Grimes 		oops("too many characters pushed back");
1099b50d902SRodney W. Grimes }
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes /*
1129b50d902SRodney W. Grimes  *  pbnum - convert number to string, push back on input.
1139b50d902SRodney W. Grimes  */
1149b50d902SRodney W. Grimes void
1159b50d902SRodney W. Grimes pbnum(n)
1169b50d902SRodney W. Grimes int n;
1179b50d902SRodney W. Grimes {
1189b50d902SRodney W. Grimes 	register int num;
1199b50d902SRodney W. Grimes 
1209b50d902SRodney W. Grimes 	num = (n < 0) ? -n : n;
1219b50d902SRodney W. Grimes 	do {
1229b50d902SRodney W. Grimes 		putback(num % 10 + '0');
1239b50d902SRodney W. Grimes 	}
1249b50d902SRodney W. Grimes 	while ((num /= 10) > 0);
1259b50d902SRodney W. Grimes 
1269b50d902SRodney W. Grimes 	if (n < 0)
1279b50d902SRodney W. Grimes 		putback('-');
1289b50d902SRodney W. Grimes }
1299b50d902SRodney W. Grimes 
1309b50d902SRodney W. Grimes /*
1319b50d902SRodney W. Grimes  *  chrsave - put single char on string space
1329b50d902SRodney W. Grimes  */
1339b50d902SRodney W. Grimes void
1349b50d902SRodney W. Grimes chrsave(c)
1359b50d902SRodney W. Grimes char c;
1369b50d902SRodney W. Grimes {
1379b50d902SRodney W. Grimes 	if (ep < endest)
1389b50d902SRodney W. Grimes 		*ep++ = c;
1399b50d902SRodney W. Grimes 	else
1409b50d902SRodney W. Grimes 		oops("string space overflow");
1419b50d902SRodney W. Grimes }
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes /*
1449b50d902SRodney W. Grimes  * read in a diversion file, and dispose it.
1459b50d902SRodney W. Grimes  */
1469b50d902SRodney W. Grimes void
1479b50d902SRodney W. Grimes getdiv(n)
1489b50d902SRodney W. Grimes int n;
1499b50d902SRodney W. Grimes {
1509b50d902SRodney W. Grimes 	register int c;
1519b50d902SRodney W. Grimes 	register FILE *dfil;
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes 	if (active == outfile[n])
1549b50d902SRodney W. Grimes 		oops("%s: diversion still active.", "undivert");
1559b50d902SRodney W. Grimes 	(void) fclose(outfile[n]);
1569b50d902SRodney W. Grimes 	outfile[n] = NULL;
1579b50d902SRodney W. Grimes 	m4temp[UNIQUE] = n + '0';
1589b50d902SRodney W. Grimes 	if ((dfil = fopen(m4temp, "r")) == NULL)
1599b50d902SRodney W. Grimes 		oops("%s: cannot undivert.", m4temp);
1609b50d902SRodney W. Grimes 	else
1619b50d902SRodney W. Grimes 		while ((c = getc(dfil)) != EOF)
1629b50d902SRodney W. Grimes 			putc(c, active);
1639b50d902SRodney W. Grimes 	(void) fclose(dfil);
1649b50d902SRodney W. Grimes 
1659b50d902SRodney W. Grimes #ifdef vms
1669b50d902SRodney W. Grimes 	if (remove(m4temp))
1679b50d902SRodney W. Grimes #else
1689b50d902SRodney W. Grimes 	if (unlink(m4temp) == -1)
1699b50d902SRodney W. Grimes #endif
1709b50d902SRodney W. Grimes 		oops("%s: cannot unlink.", m4temp);
1719b50d902SRodney W. Grimes }
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes void
1749b50d902SRodney W. Grimes onintr(signo)
1759b50d902SRodney W. Grimes 	int signo;
1769b50d902SRodney W. Grimes {
1779b50d902SRodney W. Grimes 	oops("interrupted.");
1789b50d902SRodney W. Grimes }
1799b50d902SRodney W. Grimes 
1809b50d902SRodney W. Grimes /*
1819b50d902SRodney W. Grimes  * killdiv - get rid of the diversion files
1829b50d902SRodney W. Grimes  */
1839b50d902SRodney W. Grimes void
1849b50d902SRodney W. Grimes killdiv()
1859b50d902SRodney W. Grimes {
1869b50d902SRodney W. Grimes 	register int n;
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes 	for (n = 0; n < MAXOUT; n++)
1899b50d902SRodney W. Grimes 		if (outfile[n] != NULL) {
1909b50d902SRodney W. Grimes 			(void) fclose(outfile[n]);
1919b50d902SRodney W. Grimes 			m4temp[UNIQUE] = n + '0';
1929b50d902SRodney W. Grimes #ifdef vms
1939b50d902SRodney W. Grimes 			(void) remove(m4temp);
1949b50d902SRodney W. Grimes #else
1959b50d902SRodney W. Grimes 			(void) unlink(m4temp);
1969b50d902SRodney W. Grimes #endif
1979b50d902SRodney W. Grimes 		}
1989b50d902SRodney W. Grimes }
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes char *
2019b50d902SRodney W. Grimes xalloc(n)
2029b50d902SRodney W. Grimes unsigned long n;
2039b50d902SRodney W. Grimes {
2049b50d902SRodney W. Grimes 	register char *p = malloc(n);
2059b50d902SRodney W. Grimes 
2069b50d902SRodney W. Grimes 	if (p == NULL)
2079b50d902SRodney W. Grimes 		oops("malloc: %s", strerror(errno));
2089b50d902SRodney W. Grimes 	return p;
2099b50d902SRodney W. Grimes }
2109b50d902SRodney W. Grimes 
2119b50d902SRodney W. Grimes char *
2129b50d902SRodney W. Grimes xstrdup(s)
2139b50d902SRodney W. Grimes const char *s;
2149b50d902SRodney W. Grimes {
2159b50d902SRodney W. Grimes 	register char *p = strdup(s);
2169b50d902SRodney W. Grimes 	if (p == NULL)
2179b50d902SRodney W. Grimes 		oops("strdup: %s", strerror(errno));
2189b50d902SRodney W. Grimes 	return p;
2199b50d902SRodney W. Grimes }
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes char *
2229b50d902SRodney W. Grimes basename(s)
2239b50d902SRodney W. Grimes register char *s;
2249b50d902SRodney W. Grimes {
2259b50d902SRodney W. Grimes 	register char *p;
2269b50d902SRodney W. Grimes 	extern char *strrchr();
2279b50d902SRodney W. Grimes 
2289b50d902SRodney W. Grimes 	if ((p = strrchr(s, '/')) == NULL)
2299b50d902SRodney W. Grimes 		return s;
2309b50d902SRodney W. Grimes 
2319b50d902SRodney W. Grimes 	return ++p;
2329b50d902SRodney W. Grimes }
2339b50d902SRodney W. Grimes 
2349b50d902SRodney W. Grimes void
2359b50d902SRodney W. Grimes usage()
2369b50d902SRodney W. Grimes {
2379b50d902SRodney W. Grimes 	fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
2389b50d902SRodney W. Grimes 	exit(1);
2399b50d902SRodney W. Grimes }
2409b50d902SRodney W. Grimes 
2419b50d902SRodney W. Grimes #if __STDC__
2429b50d902SRodney W. Grimes #include <stdarg.h>
2439b50d902SRodney W. Grimes #else
2449b50d902SRodney W. Grimes #include <varargs.h>
2459b50d902SRodney W. Grimes #endif
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes void
2489b50d902SRodney W. Grimes #if __STDC__
2499b50d902SRodney W. Grimes oops(const char *fmt, ...)
2509b50d902SRodney W. Grimes #else
2519b50d902SRodney W. Grimes oops(fmt, va_alist)
2529b50d902SRodney W. Grimes 	char *fmt;
2539b50d902SRodney W. Grimes 	va_dcl
2549b50d902SRodney W. Grimes #endif
2559b50d902SRodney W. Grimes {
2569b50d902SRodney W. Grimes 	va_list ap;
2579b50d902SRodney W. Grimes #if __STDC__
2589b50d902SRodney W. Grimes 	va_start(ap, fmt);
2599b50d902SRodney W. Grimes #else
2609b50d902SRodney W. Grimes 	va_start(ap);
2619b50d902SRodney W. Grimes #endif
2629b50d902SRodney W. Grimes 	(void)fprintf(stderr, "%s: ", progname);
2639b50d902SRodney W. Grimes 	(void)vfprintf(stderr, fmt, ap);
2649b50d902SRodney W. Grimes 	va_end(ap);
2659b50d902SRodney W. Grimes 	(void)fprintf(stderr, "\n");
2669b50d902SRodney W. Grimes 	exit(1);
2679b50d902SRodney W. Grimes 	/* NOTREACHED */
2689b50d902SRodney W. Grimes }
269