Annotation of src/usr.bin/m4/misc.c, Revision 1.17
1.17 ! espie 1: /* $OpenBSD: misc.c,v 1.16 2000/01/13 17:35:10 espie 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.17 ! espie 44: static char rcsid[] = "$OpenBSD: misc.c,v 1.16 2000/01/13 17:35:10 espie Exp $";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include <sys/types.h>
49: #include <errno.h>
50: #include <unistd.h>
51: #include <stdio.h>
52: #include <stdlib.h>
1.7 espie 53: #include <stddef.h>
1.1 deraadt 54: #include <string.h>
1.7 espie 55: #include <err.h>
1.1 deraadt 56: #include "mdef.h"
57: #include "stdd.h"
58: #include "extern.h"
59: #include "pathnames.h"
60:
1.9 espie 61:
62: char *ep; /* first free char in strspace */
63: static char *strspace; /* string space for evaluation */
64: static char *endest; /* end of string space */
65: static size_t strsize = STRSPMAX;
66: static size_t bufsize = BUFSIZE;
67: static int low_sp = 0;
68:
1.17 ! espie 69: char *buf; /* push-back buffer */
! 70: char *bufbase; /* the base for current ilevel */
! 71: char *bbase[MAXINP]; /* the base for each ilevel */
! 72: char *bp; /* first available character */
! 73: static char *endpbb; /* end of push-back buffer */
1.9 espie 74:
75:
1.14 espie 76: static void enlarge_bufspace __P((void));
77: static void enlarge_strspace __P((void));
1.1 deraadt 78: /*
79: * find the index of second str in the first str.
80: */
1.7 espie 81: ptrdiff_t
1.1 deraadt 82: indx(s1, s2)
1.12 espie 83: const char *s1;
84: const char *s2;
1.1 deraadt 85: {
1.12 espie 86: char *t;
1.7 espie 87:
1.12 espie 88: t = strstr(s1, s2);
89: if (t == NULL)
90: return (-1);
1.7 espie 91: else
1.12 espie 92: return (t - s1);
1.1 deraadt 93: }
94: /*
95: * putback - push character back onto input
96: */
97: void
98: putback(c)
1.17 ! espie 99: int c;
1.1 deraadt 100: {
1.17 ! espie 101: if (c == EOF)
! 102: return;
1.9 espie 103: if (bp >= endpbb)
104: enlarge_bufspace();
105: *bp++ = c;
1.1 deraadt 106: }
107:
108: /*
109: * pbstr - push string back onto input
110: * putback is replicated to improve
111: * performance.
112: */
113: void
114: pbstr(s)
1.14 espie 115: const char *s;
1.1 deraadt 116: {
1.9 espie 117: size_t n;
1.1 deraadt 118:
1.9 espie 119: n = strlen(s);
1.12 espie 120: while (endpbb - bp <= n)
1.9 espie 121: enlarge_bufspace();
122: while (n > 0)
123: *bp++ = s[--n];
1.1 deraadt 124: }
125:
126: /*
127: * pbnum - convert number to string, push back on input.
128: */
129: void
130: pbnum(n)
1.12 espie 131: int n;
1.1 deraadt 132: {
1.12 espie 133: int num;
1.1 deraadt 134:
135: num = (n < 0) ? -n : n;
136: do {
137: putback(num % 10 + '0');
138: }
139: while ((num /= 10) > 0);
140:
141: if (n < 0)
142: putback('-');
143: }
144:
1.9 espie 145:
146: void
147: initspaces()
148: {
149: int i;
150:
151: strspace = xalloc(strsize+1);
152: ep = strspace;
153: endest = strspace+strsize;
1.17 ! espie 154: buf = (char *)xalloc(bufsize);
1.9 espie 155: bufbase = buf;
156: bp = buf;
157: endpbb = buf + bufsize;
158: for (i = 0; i < MAXINP; i++)
159: bbase[i] = buf;
160: }
161:
162: /* XXX when chrsave is called, the current argument is
163: * always topmost on the stack. We make use of this to
164: * duplicate it transparently, and to reclaim the correct
165: * space when the stack is unwound.
166: */
1.17 ! espie 167: static void
! 168: enlarge_strspace()
1.9 espie 169: {
170: char *newstrspace;
171:
172: low_sp = sp;
173: strsize *= 2;
174: newstrspace = malloc(strsize + 1);
175: if (!newstrspace)
176: errx(1, "string space overflow");
177: memcpy(newstrspace, strspace, strsize/2);
178: /* reclaim memory in the easy, common case. */
179: if (ep == strspace)
180: free(strspace);
181: mstack[sp].sstr = (mstack[sp].sstr-strspace) + newstrspace;
182: ep = (ep-strspace) + newstrspace;
183: strspace = newstrspace;
184: endest = strspace + strsize;
185: }
186:
1.17 ! espie 187: static void
! 188: enlarge_bufspace()
1.9 espie 189: {
1.17 ! espie 190: char *newbuf;
1.9 espie 191: int i;
192:
193: bufsize *= 2;
1.17 ! espie 194: newbuf = realloc(buf, bufsize);
1.9 espie 195: if (!newbuf)
196: errx(1, "too many characters pushed back");
197: for (i = 0; i < MAXINP; i++)
198: bbase[i] = (bbase[i]-buf)+newbuf;
199: bp = (bp-buf)+newbuf;
200: bufbase = (bufbase-buf)+newbuf;
201: buf = newbuf;
1.10 espie 202: endpbb = buf+bufsize;
1.9 espie 203: }
204:
1.1 deraadt 205: /*
206: * chrsave - put single char on string space
207: */
208: void
209: chrsave(c)
1.14 espie 210: int c;
1.1 deraadt 211: {
1.9 espie 212: if (ep >= endest)
213: enlarge_strspace();
214: *ep++ = c;
215: }
216:
217: /*
218: * so we reclaim what string space we can
219: */
220: char *
221: compute_prevep()
222: {
223: if (fp+3 <= low_sp)
224: {
225: return strspace;
226: }
1.1 deraadt 227: else
1.9 espie 228: {
229: return mstack[fp+3].sstr;
230: }
1.1 deraadt 231: }
232:
233: /*
234: * read in a diversion file, and dispose it.
235: */
236: void
237: getdiv(n)
1.12 espie 238: int n;
1.1 deraadt 239: {
1.12 espie 240: int c;
1.1 deraadt 241:
242: if (active == outfile[n])
1.7 espie 243: errx(1, "undivert: diversion still active");
1.8 espie 244: rewind(outfile[n]);
245: while ((c = getc(outfile[n])) != EOF)
246: putc(c, active);
1.1 deraadt 247: (void) fclose(outfile[n]);
1.13 espie 248: outfile[n] = NULL;
1.1 deraadt 249: }
250:
251: void
252: onintr(signo)
253: int signo;
254: {
1.7 espie 255: errx(1, "interrupted.");
1.1 deraadt 256: }
257:
258: /*
259: * killdiv - get rid of the diversion files
260: */
261: void
262: killdiv()
263: {
1.12 espie 264: int n;
1.1 deraadt 265:
266: for (n = 0; n < MAXOUT; n++)
267: if (outfile[n] != NULL) {
268: (void) fclose(outfile[n]);
269: }
270: }
271:
272: char *
273: xalloc(n)
1.14 espie 274: size_t n;
1.1 deraadt 275: {
1.12 espie 276: char *p = malloc(n);
1.1 deraadt 277:
278: if (p == NULL)
1.7 espie 279: err(1, "malloc");
1.1 deraadt 280: return p;
281: }
282:
283: char *
284: xstrdup(s)
1.12 espie 285: const char *s;
1.1 deraadt 286: {
1.12 espie 287: char *p = strdup(s);
1.1 deraadt 288: if (p == NULL)
1.7 espie 289: err(1, "strdup");
1.1 deraadt 290: return p;
291: }
292:
293: void
294: usage()
295: {
1.11 espie 296: fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname] [-I dirname...]\n");
1.1 deraadt 297: exit(1);
298: }
299:
1.15 espie 300: int
301: obtain_char(f)
302: struct input_file *f;
303: {
1.17 ! espie 304: if (f->c == EOF)
! 305: return EOF;
! 306: else if (f->c == '\n')
1.15 espie 307: f->lineno++;
308:
309: f->c = fgetc(f->file);
310: return f->c;
311: }
312:
313: void
314: set_input(f, real, name)
315: struct input_file *f;
316: FILE *real;
317: const char *name;
318: {
319: f->file = real;
320: f->lineno = 1;
321: f->c = 0;
322: f->name = xstrdup(name);
323: }
324:
325: void
326: release_input(f)
327: struct input_file *f;
328: {
329: if (f->file != stdin)
330: fclose(f->file);
1.17 ! espie 331: f->c = EOF;
1.16 espie 332: /*
333: * XXX can't free filename, as there might still be
334: * error information pointing to it.
335: */
1.15 espie 336: }