Annotation of src/usr.bin/m4/gnum4.c, Revision 1.6.2.1
1.6.2.1 ! jason 1: /* $OpenBSD: gnum4.c,v 1.7 2000/06/28 10:01:27 espie Exp $ */
1.1 espie 2:
3: /*
4: * Copyright (c) 1999 Marc Espie
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: * SUCH DAMAGE.
26: */
27:
28: /*
29: * functions needed to support gnu-m4 extensions, including a fake freezing
30: */
31:
32: #include <sys/param.h>
1.5 espie 33: #include <sys/types.h>
34: #include <ctype.h>
35: #include <regex.h>
1.1 espie 36: #include <stddef.h>
37: #include <stdlib.h>
38: #include <stdio.h>
39: #include <string.h>
40: #include <err.h>
41: #include "mdef.h"
42: #include "stdd.h"
43: #include "extern.h"
44:
1.5 espie 45:
46: int mimic_gnu = 0;
47:
1.1 espie 48: /*
49: * Support for include path search
50: * First search in the the current directory.
51: * If not found, and the path is not absolute, include path kicks in.
52: * First, -I options, in the order found on the command line.
53: * Then M4PATH env variable
54: */
55:
56: struct path_entry {
57: char *name;
58: struct path_entry *next;
59: } *first, *last;
60:
1.3 espie 61: static struct path_entry *new_path_entry __P((const char *));
62: static void ensure_m4path __P((void));
1.4 espie 63: static struct input_file *dopath __P((struct input_file *, const char *));
1.3 espie 64:
1.1 espie 65: static struct path_entry *
66: new_path_entry(dirname)
1.3 espie 67: const char *dirname;
1.1 espie 68: {
69: struct path_entry *n;
70:
71: n = malloc(sizeof(struct path_entry));
72: if (!n)
73: errx(1, "out of memory");
74: n->name = strdup(dirname);
75: if (!n->name)
76: errx(1, "out of memory");
77: n->next = 0;
78: return n;
79: }
80:
81: void
82: addtoincludepath(dirname)
83: const char *dirname;
84: {
85: struct path_entry *n;
86:
87: n = new_path_entry(dirname);
88:
89: if (last) {
90: last->next = n;
91: last = n;
92: }
93: else
94: last = first = n;
95: }
96:
97: static void
98: ensure_m4path()
99: {
100: static int envpathdone = 0;
101: char *envpath;
102: char *sweep;
103: char *path;
104:
105: if (envpathdone)
106: return;
107: envpathdone = TRUE;
108: envpath = getenv("M4PATH");
109: if (!envpath)
110: return;
111: /* for portability: getenv result is read-only */
112: envpath = strdup(envpath);
113: if (!envpath)
114: errx(1, "out of memory");
115: for (sweep = envpath;
116: (path = strsep(&sweep, ":")) != NULL;)
117: addtoincludepath(path);
118: free(envpath);
119: }
120:
121: static
1.4 espie 122: struct input_file *
123: dopath(i, filename)
124: struct input_file *i;
1.3 espie 125: const char *filename;
1.1 espie 126: {
127: char path[MAXPATHLEN];
128: struct path_entry *pe;
1.4 espie 129: FILE *f;
1.1 espie 130:
131: for (pe = first; pe; pe = pe->next) {
132: snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
1.4 espie 133: if ((f = fopen(path, "r")) != 0) {
134: set_input(i, f, path);
135: return i;
136: }
1.1 espie 137: }
138: return NULL;
139: }
140:
1.4 espie 141: struct input_file *
142: fopen_trypath(i, filename)
143: struct input_file *i;
1.1 espie 144: const char *filename;
145: {
146: FILE *f;
147:
148: f = fopen(filename, "r");
1.4 espie 149: if (f != NULL) {
150: set_input(i, f, filename);
151: return i;
152: }
1.1 espie 153: if (filename[0] == '/')
154: return NULL;
155:
156: ensure_m4path();
157:
1.4 espie 158: return dopath(i, filename);
1.1 espie 159: }
160:
1.5 espie 161: void
162: doindir(argv, argc)
163: const char *argv[];
164: int argc;
165: {
166: ndptr p;
167:
168: p = lookup(argv[2]);
169: if (p == NULL)
170: errx(1, "undefined macro %s", argv[2]);
171: argv[1] = p->defn;
172: if (p->type == MACRTYPE)
173: expand(argv+1, argc-1);
174: else
175: eval(argv+1, argc-1, p->type);
176: }
177:
178: void
179: dobuiltin(argv, argc)
180: const char *argv[];
181: int argc;
182: {
183: int n;
184: argv[1] = NULL;
185: n = builtin_type(argv[2]);
186: if (n != -1)
1.6 espie 187: eval(argv+1, argc-1, n);
1.5 espie 188: else
189: errx(1, "unknown builtin %s", argv[2]);
190: }
191:
192:
193: /* We need some temporary buffer space, as pb pushes BACK and substitution
194: * proceeds forward... */
195: static char *buffer;
196: static size_t bufsize = 0;
197: static size_t current = 0;
198:
199: static void addchars __P((const char *, size_t));
200: static void addchar __P((char));
201: static char *twiddle __P((const char *));
202: static char *getstring __P((void));
203: static void exit_regerror __P((int, regex_t *));
204: static void do_subst __P((const char *, regex_t *, const char *, regmatch_t *));
205: static void do_regexpindex __P((const char *, regex_t *, regmatch_t *));
206: static void do_regexp __P((const char *, regex_t *, const char *, regmatch_t *));
207: static void add_sub __P((int, const char *, regex_t *, regmatch_t *));
208: static void add_replace __P((const char *, regex_t *, const char *, regmatch_t *));
209:
210: static void
211: addchars(c, n)
212: const char *c;
213: size_t n;
214: {
215: if (n == 0)
216: return;
217: if (current + n > bufsize) {
218: if (bufsize == 0)
219: bufsize = 1024;
220: else
221: bufsize *= 2;
222: buffer = realloc(buffer, bufsize);
223: if (buffer == NULL)
224: errx(1, "out of memory");
225: }
226: memcpy(buffer+current, c, n);
227: current += n;
228: }
229:
230: static void
231: addchar(c)
232: char c;
233: {
234: if (current +1 > bufsize) {
235: if (bufsize == 0)
236: bufsize = 1024;
237: else
238: bufsize *= 2;
239: buffer = realloc(buffer, bufsize);
240: if (buffer == NULL)
241: errx(1, "out of memory");
242: }
243: buffer[current++] = c;
244: }
245:
246: static char *
247: getstring()
248: {
249: addchar('\0');
250: current = 0;
251: return buffer;
252: }
253:
254:
255: static void
256: exit_regerror(er, re)
257: int er;
258: regex_t *re;
259: {
260: size_t errlen;
261: char *errbuf;
262:
263: errlen = regerror(er, re, NULL, 0);
264: errbuf = xalloc(errlen);
265: regerror(er, re, errbuf, errlen);
266: errx(1, "regular expression error: %s", errbuf);
267: }
268:
269: static void
270: add_sub(n, string, re, pm)
271: int n;
272: const char *string;
273: regex_t *re;
274: regmatch_t *pm;
275: {
276: if (n > re->re_nsub)
277: warnx("No subexpression %d", n);
278: /* Subexpressions that did not match are
279: * not an error. */
280: else if (pm[n].rm_so != -1 &&
281: pm[n].rm_eo != -1) {
282: addchars(string + pm[n].rm_so,
283: pm[n].rm_eo - pm[n].rm_so);
284: }
285: }
286:
287: /* Add replacement string to the output buffer, recognizing special
288: * constructs and replacing them with substrings of the original string.
289: */
290: static void
291: add_replace(string, re, replace, pm)
292: const char *string;
293: regex_t *re;
294: const char *replace;
295: regmatch_t *pm;
296: {
297: const char *p;
298:
299: for (p = replace; *p != '\0'; p++) {
300: if (*p == '&' && !mimic_gnu) {
301: add_sub(0, string, re, pm);
302: continue;
303: }
304: if (*p == '\\') {
305: if (p[1] == '\\') {
306: addchar(p[1]);
307: continue;
308: }
309: if (p[1] == '&') {
310: if (mimic_gnu)
311: add_sub(0, string, re, pm);
312: else
313: addchar(p[1]);
314: p++;
315: continue;
316: }
317: if (isdigit(p[1])) {
318: add_sub(*(++p) - '0', string, re, pm);
319: continue;
320: }
321: }
322: addchar(*p);
323: }
324: }
325:
326: static void
327: do_subst(string, re, replace, pm)
328: const char *string;
329: regex_t *re;
330: const char *replace;
331: regmatch_t *pm;
332: {
333: int error;
334: regoff_t last_match = -1;
335:
336: while ((error = regexec(re, string, re->re_nsub+1, pm, 0)) == 0) {
337:
338: /* NULL length matches are special... We use the `vi-mode'
339: * rule: don't allow a NULL-match at the last match
340: * position.
341: */
342: if (pm[0].rm_so == pm[0].rm_eo && pm[0].rm_so == last_match) {
343: if (*string == '\0')
344: return;
345: addchar(*string);
346: string++;
347: continue;
348: }
349: last_match = pm[0].rm_so;
350: addchars(string, last_match);
351: add_replace(string, re, replace, pm);
352: string += pm[0].rm_eo;
353: }
354: if (error != REG_NOMATCH)
355: exit_regerror(error, re);
356: pbstr(string);
357: }
358:
359: static void
360: do_regexp(string, re, replace, pm)
361: const char *string;
362: regex_t *re;
363: const char *replace;
364: regmatch_t *pm;
365: {
366: int error;
367: const char *p;
368:
369: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
370: case 0:
371: add_replace(string, re, replace, pm);
372: pbstr(getstring());
373: break;
374: case REG_NOMATCH:
375: break;
376: default:
377: exit_regerror(error, re);
378: }
379: }
380:
381: static void
382: do_regexpindex(string, re, pm)
383: const char *string;
384: regex_t *re;
385: regmatch_t *pm;
386: {
387: int error;
388:
389: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
390: case 0:
391: pbunsigned(pm[0].rm_so);
392: break;
393: case REG_NOMATCH:
394: pbnum(-1);
395: break;
396: default:
397: exit_regerror(error, re);
398: }
399: }
400:
401: /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
402: * says. So we twiddle with the regexp before passing it to regcomp.
403: */
404: static char *
405: twiddle(p)
406: const char *p;
407: {
408: /* This could use strcspn for speed... */
409: while (*p != '\0') {
410: if (*p == '\\' && (p[1] == '(' || p[1] == ')')) {
411: addchar(p[1]);
412: p+=2;
413: continue;
414: }
415: if (*p == '(' || *p == ')')
416: addchar('\\');
417:
418: addchar(*p);
419: p++;
420: }
421: return getstring();
422: }
423:
424: /* patsubst(string, regexp, opt replacement) */
425: /* argv[2]: string
426: * argv[3]: regexp
427: * argv[4]: opt rep
428: */
429: void
430: dopatsubst(argv, argc)
431: const char *argv[];
432: int argc;
433: {
434: int error;
435: regex_t re;
436: regmatch_t *pmatch;
437:
438: if (argc <= 3) {
439: warnx("Too few arguments to patsubst");
440: return;
441: }
442: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
443: REG_EXTENDED);
444: if (error != 0)
445: exit_regerror(error, &re);
446:
447: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.6.2.1 ! jason 448: do_subst(argv[2], &re,
! 449: argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
1.5 espie 450: pbstr(getstring());
451: free(pmatch);
452: regfree(&re);
453: }
454:
455: void
456: doregexp(argv, argc)
457: const char *argv[];
458: int argc;
459: {
460: int error;
461: regex_t re;
462: regmatch_t *pmatch;
463:
464: if (argc <= 3) {
1.6.2.1 ! jason 465: warnx("Too few arguments to regexp");
1.5 espie 466: return;
467: }
468: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
469: REG_EXTENDED);
470: if (error != 0)
471: exit_regerror(error, &re);
472:
473: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.6.2.1 ! jason 474: if (argv[4] == NULL || argc == 4)
1.5 espie 475: do_regexpindex(argv[2], &re, pmatch);
476: else
477: do_regexp(argv[2], &re, argv[4], pmatch);
478: free(pmatch);
479: regfree(&re);
480: }