Annotation of src/usr.bin/m4/misc.c, Revision 1.34
1.34 ! jmc 1: /* $OpenBSD: misc.c,v 1.33 2005/09/06 15:33:21 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.
1.29 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #include <sys/types.h>
37: #include <errno.h>
38: #include <unistd.h>
1.30 espie 39: #include <stdarg.h>
1.1 deraadt 40: #include <stdio.h>
41: #include <stdlib.h>
1.7 espie 42: #include <stddef.h>
1.1 deraadt 43: #include <string.h>
1.7 espie 44: #include <err.h>
1.1 deraadt 45: #include "mdef.h"
46: #include "stdd.h"
47: #include "extern.h"
48: #include "pathnames.h"
49:
1.9 espie 50:
51: char *ep; /* first free char in strspace */
52: static char *strspace; /* string space for evaluation */
1.24 espie 53: char *endest; /* end of string space */
1.9 espie 54: static size_t strsize = STRSPMAX;
55: static size_t bufsize = BUFSIZE;
56:
1.17 espie 57: char *buf; /* push-back buffer */
58: char *bufbase; /* the base for current ilevel */
59: char *bbase[MAXINP]; /* the base for each ilevel */
60: char *bp; /* first available character */
1.24 espie 61: char *endpbb; /* end of push-back buffer */
1.9 espie 62:
63:
1.1 deraadt 64: /*
65: * find the index of second str in the first str.
66: */
1.7 espie 67: ptrdiff_t
1.27 espie 68: indx(const char *s1, const char *s2)
1.1 deraadt 69: {
1.12 espie 70: char *t;
1.7 espie 71:
1.12 espie 72: t = strstr(s1, s2);
73: if (t == NULL)
74: return (-1);
1.7 espie 75: else
1.12 espie 76: return (t - s1);
1.1 deraadt 77: }
78: /*
1.33 espie 79: * pushback - push character back onto input
1.1 deraadt 80: */
81: void
1.33 espie 82: pushback(int c)
1.1 deraadt 83: {
1.17 espie 84: if (c == EOF)
85: return;
1.9 espie 86: if (bp >= endpbb)
87: enlarge_bufspace();
88: *bp++ = c;
1.1 deraadt 89: }
90:
91: /*
92: * pbstr - push string back onto input
1.33 espie 93: * pushback is replicated to improve
1.1 deraadt 94: * performance.
95: */
96: void
1.27 espie 97: pbstr(const char *s)
1.1 deraadt 98: {
1.9 espie 99: size_t n;
1.1 deraadt 100:
1.9 espie 101: n = strlen(s);
1.12 espie 102: while (endpbb - bp <= n)
1.9 espie 103: enlarge_bufspace();
104: while (n > 0)
105: *bp++ = s[--n];
1.1 deraadt 106: }
107:
108: /*
109: * pbnum - convert number to string, push back on input.
110: */
111: void
1.27 espie 112: pbnum(int n)
1.1 deraadt 113: {
1.31 espie 114: pbnumbase(n, 10, 0);
115: }
116:
117: void
118: pbnumbase(int n, int base, int d)
119: {
120: static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
1.12 espie 121: int num;
1.31 espie 122: int printed = 0;
123:
124: if (base > 36)
125: errx(1, "base %d > 36: not supported", base);
126:
127: if (base < 2)
128: errx(1, "bad base %d for conversion", base);
1.1 deraadt 129:
130: num = (n < 0) ? -n : n;
131: do {
1.33 espie 132: pushback(digits[num % base]);
1.31 espie 133: printed++;
1.1 deraadt 134: }
1.31 espie 135: while ((num /= base) > 0);
136:
137: if (n < 0)
138: printed++;
139: while (printed++ < d)
1.33 espie 140: pushback('0');
1.1 deraadt 141:
142: if (n < 0)
1.33 espie 143: pushback('-');
1.1 deraadt 144: }
145:
1.18 espie 146: /*
147: * pbunsigned - convert unsigned long to string, push back on input.
148: */
149: void
1.27 espie 150: pbunsigned(unsigned long n)
1.18 espie 151: {
152: do {
1.33 espie 153: pushback(n % 10 + '0');
1.18 espie 154: }
155: while ((n /= 10) > 0);
156: }
1.9 espie 157:
158: void
159: initspaces()
160: {
161: int i;
162:
1.30 espie 163: strspace = xalloc(strsize+1, NULL);
1.9 espie 164: ep = strspace;
165: endest = strspace+strsize;
1.30 espie 166: buf = (char *)xalloc(bufsize, NULL);
1.9 espie 167: bufbase = buf;
168: bp = buf;
169: endpbb = buf + bufsize;
170: for (i = 0; i < MAXINP; i++)
171: bbase[i] = buf;
172: }
173:
1.24 espie 174: void
1.17 espie 175: enlarge_strspace()
1.9 espie 176: {
177: char *newstrspace;
1.19 espie 178: int i;
1.9 espie 179:
180: strsize *= 2;
181: newstrspace = malloc(strsize + 1);
182: if (!newstrspace)
183: errx(1, "string space overflow");
184: memcpy(newstrspace, strspace, strsize/2);
1.19 espie 185: for (i = 0; i <= sp; i++)
186: if (sstack[i])
187: mstack[i].sstr = (mstack[i].sstr - strspace)
188: + newstrspace;
1.9 espie 189: ep = (ep-strspace) + newstrspace;
1.19 espie 190: free(strspace);
1.9 espie 191: strspace = newstrspace;
192: endest = strspace + strsize;
193: }
194:
1.24 espie 195: void
1.17 espie 196: enlarge_bufspace()
1.9 espie 197: {
1.17 espie 198: char *newbuf;
1.9 espie 199: int i;
200:
1.30 espie 201: bufsize += bufsize/2;
202: newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
1.9 espie 203: for (i = 0; i < MAXINP; i++)
204: bbase[i] = (bbase[i]-buf)+newbuf;
205: bp = (bp-buf)+newbuf;
206: bufbase = (bufbase-buf)+newbuf;
207: buf = newbuf;
1.10 espie 208: endpbb = buf+bufsize;
1.9 espie 209: }
210:
1.1 deraadt 211: /*
212: * chrsave - put single char on string space
213: */
214: void
1.27 espie 215: chrsave(int c)
1.1 deraadt 216: {
1.9 espie 217: if (ep >= endest)
218: enlarge_strspace();
219: *ep++ = c;
1.1 deraadt 220: }
221:
222: /*
223: * read in a diversion file, and dispose it.
224: */
225: void
1.27 espie 226: getdiv(int n)
1.1 deraadt 227: {
1.12 espie 228: int c;
1.1 deraadt 229:
230: if (active == outfile[n])
1.7 espie 231: errx(1, "undivert: diversion still active");
1.8 espie 232: rewind(outfile[n]);
233: while ((c = getc(outfile[n])) != EOF)
234: putc(c, active);
1.1 deraadt 235: (void) fclose(outfile[n]);
1.13 espie 236: outfile[n] = NULL;
1.1 deraadt 237: }
238:
239: void
1.27 espie 240: onintr(int signo)
1.1 deraadt 241: {
1.21 espie 242: #define intrmessage "m4: interrupted.\n"
1.26 deraadt 243: write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
1.21 espie 244: _exit(1);
1.1 deraadt 245: }
246:
247: /*
248: * killdiv - get rid of the diversion files
249: */
250: void
251: killdiv()
252: {
1.12 espie 253: int n;
1.1 deraadt 254:
1.20 espie 255: for (n = 0; n < maxout; n++)
1.1 deraadt 256: if (outfile[n] != NULL) {
257: (void) fclose(outfile[n]);
258: }
1.20 espie 259: }
260:
261: /*
262: * resizedivs: allocate more diversion files */
263: void
1.27 espie 264: resizedivs(int n)
1.20 espie 265: {
266: int i;
267:
1.30 espie 268: outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n,
269: "too many diverts %d", n);
1.20 espie 270: for (i = maxout; i < n; i++)
271: outfile[i] = NULL;
272: maxout = n;
1.1 deraadt 273: }
274:
1.18 espie 275: void *
1.30 espie 276: xalloc(size_t n, const char *fmt, ...)
1.1 deraadt 277: {
1.30 espie 278: void *p = malloc(n);
1.1 deraadt 279:
1.30 espie 280: if (p == NULL) {
281: if (fmt == NULL)
282: err(1, "malloc");
283: else {
284: va_list va;
285:
286: va_start(va, fmt);
287: verr(1, fmt, va);
288: va_end(va);
289: }
290: }
291: return p;
292: }
293:
294: void *
295: xrealloc(void *old, size_t n, const char *fmt, ...)
296: {
297: char *p = realloc(old, n);
298:
299: if (p == NULL) {
300: free(old);
301: if (fmt == NULL)
302: err(1, "realloc");
303: else {
304: va_list va;
305:
306: va_start(va, fmt);
307: verr(1, fmt, va);
308: va_end(va);
309: }
310: }
1.1 deraadt 311: return p;
312: }
313:
314: char *
1.27 espie 315: xstrdup(const char *s)
1.1 deraadt 316: {
1.12 espie 317: char *p = strdup(s);
1.1 deraadt 318: if (p == NULL)
1.7 espie 319: err(1, "strdup");
1.1 deraadt 320: return p;
321: }
322:
323: void
324: usage()
325: {
1.34 ! jmc 326: fprintf(stderr, "usage: m4 [-gs] [-Dname[=value]] [-d flags] [-I dirname] [-o filename] [-t macro] [-Uname]\n");
1.1 deraadt 327: exit(1);
328: }
329:
1.15 espie 330: int
1.27 espie 331: obtain_char(struct input_file *f)
1.15 espie 332: {
1.17 espie 333: if (f->c == EOF)
334: return EOF;
1.33 espie 335:
336: f->c = fgetc(f->file);
337: if (f->c == '\n')
1.15 espie 338: f->lineno++;
339:
340: return f->c;
341: }
342:
343: void
1.27 espie 344: set_input(struct input_file *f, FILE *real, const char *name)
1.15 espie 345: {
346: f->file = real;
347: f->lineno = 1;
348: f->c = 0;
349: f->name = xstrdup(name);
1.28 espie 350: emit_synchline();
351: }
352:
353: void
354: do_emit_synchline()
355: {
356: fprintf(active, "#line %lu \"%s\"\n",
357: infile[ilevel].lineno, infile[ilevel].name);
358: infile[ilevel].synch_lineno = infile[ilevel].lineno;
1.15 espie 359: }
360:
361: void
1.27 espie 362: release_input(struct input_file *f)
1.15 espie 363: {
364: if (f->file != stdin)
365: fclose(f->file);
1.17 espie 366: f->c = EOF;
1.16 espie 367: /*
368: * XXX can't free filename, as there might still be
369: * error information pointing to it.
370: */
1.18 espie 371: }
372:
373: void
1.27 espie 374: doprintlineno(struct input_file *f)
1.18 espie 375: {
376: pbunsigned(f->lineno);
377: }
378:
379: void
1.27 espie 380: doprintfilename(struct input_file *f)
1.18 espie 381: {
1.25 espie 382: pbstr(rquote);
1.18 espie 383: pbstr(f->name);
1.25 espie 384: pbstr(lquote);
1.22 espie 385: }
386:
387: /*
388: * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
389: * and later dump everything that was added since then to a file.
390: */
391: size_t
392: buffer_mark()
393: {
394: return bp - buf;
395: }
396:
397:
398: void
1.27 espie 399: dump_buffer(FILE *f, size_t m)
1.22 espie 400: {
401: char *s;
402:
1.23 espie 403: for (s = bp; s-buf > m;)
1.22 espie 404: fputc(*--s, f);
1.15 espie 405: }