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