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 3895105358SPhilippe Charnier #if 0 399b50d902SRodney W. Grimes static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93"; 4095105358SPhilippe Charnier #endif 4195105358SPhilippe Charnier static const char rcsid[] = 42c3aac50fSPeter Wemm "$FreeBSD$"; 439b50d902SRodney W. Grimes #endif /* not lint */ 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <sys/types.h> 4695105358SPhilippe Charnier #include <err.h> 479b50d902SRodney W. Grimes #include <stdio.h> 489b50d902SRodney W. Grimes #include <stdlib.h> 499b50d902SRodney W. Grimes #include <string.h> 5095105358SPhilippe Charnier #include <unistd.h> 519b50d902SRodney W. Grimes #include "mdef.h" 529b50d902SRodney W. Grimes #include "stdd.h" 539b50d902SRodney W. Grimes #include "extern.h" 549b50d902SRodney W. Grimes #include "pathnames.h" 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes /* 579b50d902SRodney W. Grimes * find the index of second str in the first str. 589b50d902SRodney W. Grimes */ 599b50d902SRodney W. Grimes int 609b50d902SRodney W. Grimes indx(s1, s2) 619b50d902SRodney W. Grimes char *s1; 629b50d902SRodney W. Grimes char *s2; 639b50d902SRodney W. Grimes { 649b50d902SRodney W. Grimes register char *t; 659b50d902SRodney W. Grimes register char *p; 669b50d902SRodney W. Grimes register char *m; 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes for (p = s1; *p; p++) { 699b50d902SRodney W. Grimes for (t = p, m = s2; *m && *m == *t; m++, t++); 709b50d902SRodney W. Grimes if (!*m) 719b50d902SRodney W. Grimes return (p - s1); 729b50d902SRodney W. Grimes } 739b50d902SRodney W. Grimes return (-1); 749b50d902SRodney W. Grimes } 759b50d902SRodney W. Grimes /* 769b50d902SRodney W. Grimes * putback - push character back onto input 779b50d902SRodney W. Grimes */ 789b50d902SRodney W. Grimes void 799b50d902SRodney W. Grimes putback(c) 807c5eeb39SAndrey A. Chernov int c; 819b50d902SRodney W. Grimes { 827c5eeb39SAndrey A. Chernov if (c == EOF) 837c5eeb39SAndrey A. Chernov c = 0; 84b63b4cdbSAndrey A. Chernov else if (c == 0) 85b63b4cdbSAndrey A. Chernov return; 869b50d902SRodney W. Grimes if (bp < endpbb) 879b50d902SRodney W. Grimes *bp++ = c; 889b50d902SRodney W. Grimes else 8995105358SPhilippe Charnier errx(1, "too many characters pushed back"); 909b50d902SRodney W. Grimes } 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes /* 939b50d902SRodney W. Grimes * pbstr - push string back onto input 949b50d902SRodney W. Grimes * putback is replicated to improve 959b50d902SRodney W. Grimes * performance. 969b50d902SRodney W. Grimes */ 979b50d902SRodney W. Grimes void 989b50d902SRodney W. Grimes pbstr(s) 997c5eeb39SAndrey A. Chernov register unsigned char *s; 1009b50d902SRodney W. Grimes { 1017c5eeb39SAndrey A. Chernov register unsigned char *es; 1027c5eeb39SAndrey A. Chernov register unsigned char *zp; 1039b50d902SRodney W. Grimes 1049b50d902SRodney W. Grimes es = s; 1059b50d902SRodney W. Grimes zp = bp; 1069b50d902SRodney W. Grimes 1079b50d902SRodney W. Grimes while (*es) 1089b50d902SRodney W. Grimes es++; 1099b50d902SRodney W. Grimes es--; 1109b50d902SRodney W. Grimes while (es >= s) 1119b50d902SRodney W. Grimes if (zp < endpbb) 1129b50d902SRodney W. Grimes *zp++ = *es--; 1139b50d902SRodney W. Grimes if ((bp = zp) == endpbb) 11495105358SPhilippe Charnier errx(1, "too many characters pushed back"); 1159b50d902SRodney W. Grimes } 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes /* 1189b50d902SRodney W. Grimes * pbnum - convert number to string, push back on input. 1199b50d902SRodney W. Grimes */ 1209b50d902SRodney W. Grimes void 1219b50d902SRodney W. Grimes pbnum(n) 1229b50d902SRodney W. Grimes int n; 1239b50d902SRodney W. Grimes { 1249b50d902SRodney W. Grimes register int num; 1259b50d902SRodney W. Grimes 1269b50d902SRodney W. Grimes num = (n < 0) ? -n : n; 1279b50d902SRodney W. Grimes do { 1289b50d902SRodney W. Grimes putback(num % 10 + '0'); 1299b50d902SRodney W. Grimes } 1309b50d902SRodney W. Grimes while ((num /= 10) > 0); 1319b50d902SRodney W. Grimes 1329b50d902SRodney W. Grimes if (n < 0) 1339b50d902SRodney W. Grimes putback('-'); 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes /* 1379b50d902SRodney W. Grimes * chrsave - put single char on string space 1389b50d902SRodney W. Grimes */ 1399b50d902SRodney W. Grimes void 1409b50d902SRodney W. Grimes chrsave(c) 1419b50d902SRodney W. Grimes char c; 1429b50d902SRodney W. Grimes { 1439b50d902SRodney W. Grimes if (ep < endest) 1449b50d902SRodney W. Grimes *ep++ = c; 1459b50d902SRodney W. Grimes else 14695105358SPhilippe Charnier errx(1, "string space overflow"); 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes 1499b50d902SRodney W. Grimes /* 1509b50d902SRodney W. Grimes * read in a diversion file, and dispose it. 1519b50d902SRodney W. Grimes */ 1529b50d902SRodney W. Grimes void 1539b50d902SRodney W. Grimes getdiv(n) 1549b50d902SRodney W. Grimes int n; 1559b50d902SRodney W. Grimes { 1569b50d902SRodney W. Grimes register int c; 1579b50d902SRodney W. Grimes register FILE *dfil; 1589b50d902SRodney W. Grimes 1599b50d902SRodney W. Grimes if (active == outfile[n]) 16095105358SPhilippe Charnier errx(1, "undivert: diversion still active"); 1619b50d902SRodney W. Grimes (void) fclose(outfile[n]); 1629b50d902SRodney W. Grimes outfile[n] = NULL; 1639b50d902SRodney W. Grimes m4temp[UNIQUE] = n + '0'; 1649b50d902SRodney W. Grimes if ((dfil = fopen(m4temp, "r")) == NULL) 16595105358SPhilippe Charnier errx(1, "%s: cannot undivert", m4temp); 1669b50d902SRodney W. Grimes else 1679b50d902SRodney W. Grimes while ((c = getc(dfil)) != EOF) 1689b50d902SRodney W. Grimes putc(c, active); 1699b50d902SRodney W. Grimes (void) fclose(dfil); 1709b50d902SRodney W. Grimes 1719b50d902SRodney W. Grimes #ifdef vms 1729b50d902SRodney W. Grimes if (remove(m4temp)) 1739b50d902SRodney W. Grimes #else 1749b50d902SRodney W. Grimes if (unlink(m4temp) == -1) 1759b50d902SRodney W. Grimes #endif 17695105358SPhilippe Charnier errx(1, "%s: cannot unlink", m4temp); 1779b50d902SRodney W. Grimes } 1789b50d902SRodney W. Grimes 1799b50d902SRodney W. Grimes void 1809b50d902SRodney W. Grimes onintr(signo) 1819b50d902SRodney W. Grimes int signo; 1829b50d902SRodney W. Grimes { 18395105358SPhilippe Charnier errx(1, "interrupted"); 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes 1869b50d902SRodney W. Grimes /* 1879b50d902SRodney W. Grimes * killdiv - get rid of the diversion files 1889b50d902SRodney W. Grimes */ 1899b50d902SRodney W. Grimes void 1909b50d902SRodney W. Grimes killdiv() 1919b50d902SRodney W. Grimes { 1929b50d902SRodney W. Grimes register int n; 1939b50d902SRodney W. Grimes 1949b50d902SRodney W. Grimes for (n = 0; n < MAXOUT; n++) 1959b50d902SRodney W. Grimes if (outfile[n] != NULL) { 1969b50d902SRodney W. Grimes (void) fclose(outfile[n]); 1979b50d902SRodney W. Grimes m4temp[UNIQUE] = n + '0'; 1989b50d902SRodney W. Grimes #ifdef vms 1999b50d902SRodney W. Grimes (void) remove(m4temp); 2009b50d902SRodney W. Grimes #else 2019b50d902SRodney W. Grimes (void) unlink(m4temp); 2029b50d902SRodney W. Grimes #endif 2039b50d902SRodney W. Grimes } 2049b50d902SRodney W. Grimes } 2059b50d902SRodney W. Grimes 2069b50d902SRodney W. Grimes char * 2079b50d902SRodney W. Grimes xalloc(n) 2089b50d902SRodney W. Grimes unsigned long n; 2099b50d902SRodney W. Grimes { 2109b50d902SRodney W. Grimes register char *p = malloc(n); 2119b50d902SRodney W. Grimes 2129b50d902SRodney W. Grimes if (p == NULL) 21395105358SPhilippe Charnier err(1, "malloc"); 2149b50d902SRodney W. Grimes return p; 2159b50d902SRodney W. Grimes } 2169b50d902SRodney W. Grimes 2179b50d902SRodney W. Grimes char * 2189b50d902SRodney W. Grimes xstrdup(s) 2199b50d902SRodney W. Grimes const char *s; 2209b50d902SRodney W. Grimes { 2219b50d902SRodney W. Grimes register char *p = strdup(s); 2229b50d902SRodney W. Grimes if (p == NULL) 22395105358SPhilippe Charnier err(1, "strdup"); 2249b50d902SRodney W. Grimes return p; 2259b50d902SRodney W. Grimes } 2269b50d902SRodney W. Grimes 2279b50d902SRodney W. Grimes char * 2289b50d902SRodney W. Grimes basename(s) 2299b50d902SRodney W. Grimes register char *s; 2309b50d902SRodney W. Grimes { 2319b50d902SRodney W. Grimes register char *p; 2329b50d902SRodney W. Grimes 2339b50d902SRodney W. Grimes if ((p = strrchr(s, '/')) == NULL) 2349b50d902SRodney W. Grimes return s; 2359b50d902SRodney W. Grimes 2369b50d902SRodney W. Grimes return ++p; 2379b50d902SRodney W. Grimes } 2389b50d902SRodney W. Grimes 2399b50d902SRodney W. Grimes void 240164c01f0SGregory Neil Shapiro cleanup(n) 241164c01f0SGregory Neil Shapiro int n; 242164c01f0SGregory Neil Shapiro { 243164c01f0SGregory Neil Shapiro if (outfile[0] != NULL) { 244164c01f0SGregory Neil Shapiro (void) fclose(outfile[0]); 245164c01f0SGregory Neil Shapiro outfile[0] = NULL; 246164c01f0SGregory Neil Shapiro m4temp[UNIQUE] = '0'; 247164c01f0SGregory Neil Shapiro (void) remove(m4temp); 248164c01f0SGregory Neil Shapiro } 249164c01f0SGregory Neil Shapiro (void) remove(m4dir); 250164c01f0SGregory Neil Shapiro } 251164c01f0SGregory Neil Shapiro 252164c01f0SGregory Neil Shapiro void 2539b50d902SRodney W. Grimes usage() 2549b50d902SRodney W. Grimes { 2559b50d902SRodney W. Grimes fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n"); 2569b50d902SRodney W. Grimes exit(1); 2579b50d902SRodney W. Grimes } 258