Annotation of src/usr.bin/m4/misc.c, Revision 1.2
1.1 deraadt 1: /* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
2:
3: /*
4: * Copyright (c) 1989, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Ozan Yigit at York University.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
42: #else
43: static char rcsid[] = "$NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $";
44: #endif
45: #endif /* not lint */
46:
47: #include <sys/types.h>
1.2 ! deraadt 48: #include <sys/file.h>
1.1 deraadt 49: #include <errno.h>
50: #include <unistd.h>
51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
54: #include "mdef.h"
55: #include "stdd.h"
56: #include "extern.h"
57: #include "pathnames.h"
58:
59: /*
60: * find the index of second str in the first str.
61: */
62: int
63: indx(s1, s2)
64: char *s1;
65: char *s2;
66: {
67: register char *t;
68: register char *p;
69: register char *m;
70:
71: for (p = s1; *p; p++) {
72: for (t = p, m = s2; *m && *m == *t; m++, t++);
73: if (!*m)
74: return (p - s1);
75: }
76: return (-1);
77: }
78: /*
79: * putback - push character back onto input
80: */
81: void
82: putback(c)
83: char c;
84: {
85: if (bp < endpbb)
86: *bp++ = c;
87: else
88: oops("too many characters pushed back");
89: }
90:
91: /*
92: * pbstr - push string back onto input
93: * putback is replicated to improve
94: * performance.
95: */
96: void
97: pbstr(s)
98: register char *s;
99: {
100: register char *es;
101: register char *zp;
102:
103: es = s;
104: zp = bp;
105:
106: while (*es)
107: es++;
108: es--;
109: while (es >= s)
110: if (zp < endpbb)
111: *zp++ = *es--;
112: if ((bp = zp) == endpbb)
113: oops("too many characters pushed back");
114: }
115:
116: /*
117: * pbnum - convert number to string, push back on input.
118: */
119: void
120: pbnum(n)
121: int n;
122: {
123: register int num;
124:
125: num = (n < 0) ? -n : n;
126: do {
127: putback(num % 10 + '0');
128: }
129: while ((num /= 10) > 0);
130:
131: if (n < 0)
132: putback('-');
133: }
134:
135: /*
136: * chrsave - put single char on string space
137: */
138: void
139: chrsave(c)
140: char c;
141: {
142: if (ep < endest)
143: *ep++ = c;
144: else
145: oops("string space overflow");
146: }
147:
148: /*
149: * read in a diversion file, and dispose it.
150: */
151: void
152: getdiv(n)
153: int n;
154: {
155: register int c;
156: register FILE *dfil;
1.2 ! deraadt 157: int fd;
1.1 deraadt 158:
159: if (active == outfile[n])
160: oops("%s: diversion still active.", "undivert");
161: (void) fclose(outfile[n]);
162: outfile[n] = NULL;
163: m4temp[UNIQUE] = n + '0';
1.2 ! deraadt 164:
! 165: if ((fd = open(m4temp, O_RDWR|O_EXCL|O_CREAT, 0666)) == -1 ||
! 166: (dfil = fdopen(fd, "r")) == NULL) {
! 167: if (fd != -1)
! 168: close(fd);
1.1 deraadt 169: oops("%s: cannot undivert.", m4temp);
1.2 ! deraadt 170: } else
1.1 deraadt 171: while ((c = getc(dfil)) != EOF)
172: putc(c, active);
173: (void) fclose(dfil);
174:
175: #ifdef vms
176: if (remove(m4temp))
177: #else
178: if (unlink(m4temp) == -1)
179: #endif
180: oops("%s: cannot unlink.", m4temp);
181: }
182:
183: void
184: onintr(signo)
185: int signo;
186: {
187: oops("interrupted.");
188: }
189:
190: /*
191: * killdiv - get rid of the diversion files
192: */
193: void
194: killdiv()
195: {
196: register int n;
197:
198: for (n = 0; n < MAXOUT; n++)
199: if (outfile[n] != NULL) {
200: (void) fclose(outfile[n]);
201: m4temp[UNIQUE] = n + '0';
202: #ifdef vms
203: (void) remove(m4temp);
204: #else
205: (void) unlink(m4temp);
206: #endif
207: }
208: }
209:
210: char *
211: xalloc(n)
212: unsigned long n;
213: {
214: register char *p = malloc(n);
215:
216: if (p == NULL)
217: oops("malloc: %s", strerror(errno));
218: return p;
219: }
220:
221: char *
222: xstrdup(s)
223: const char *s;
224: {
225: register char *p = strdup(s);
226: if (p == NULL)
227: oops("strdup: %s", strerror(errno));
228: return p;
229: }
230:
231: char *
232: basename(s)
233: register char *s;
234: {
235: register char *p;
236: extern char *strrchr();
237:
238: if ((p = strrchr(s, '/')) == NULL)
239: return s;
240:
241: return ++p;
242: }
243:
244: void
245: usage()
246: {
247: fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
248: exit(1);
249: }
250:
251: #if __STDC__
252: #include <stdarg.h>
253: #else
254: #include <varargs.h>
255: #endif
256:
257: void
258: #if __STDC__
259: oops(const char *fmt, ...)
260: #else
261: oops(fmt, va_alist)
262: char *fmt;
263: va_dcl
264: #endif
265: {
266: va_list ap;
267: #if __STDC__
268: va_start(ap, fmt);
269: #else
270: va_start(ap);
271: #endif
272: (void)fprintf(stderr, "%s: ", progname);
273: (void)vfprintf(stderr, fmt, ap);
274: va_end(ap);
275: (void)fprintf(stderr, "\n");
276: exit(1);
277: /* NOTREACHED */
278: }