Annotation of src/usr.bin/m4/gnum4.c, Revision 1.10
1.10 ! espie 1: /* $OpenBSD: gnum4.c,v 1.9 2001/07/28 05:36:18 pvalchev 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>
1.8 espie 34: #include <sys/wait.h>
1.5 espie 35: #include <ctype.h>
1.8 espie 36: #include <paths.h>
1.5 espie 37: #include <regex.h>
1.1 espie 38: #include <stddef.h>
39: #include <stdlib.h>
40: #include <stdio.h>
41: #include <string.h>
42: #include <err.h>
1.8 espie 43: #include <errno.h>
44: #include <unistd.h>
1.1 espie 45: #include "mdef.h"
46: #include "stdd.h"
47: #include "extern.h"
48:
1.5 espie 49:
50: int mimic_gnu = 0;
51:
1.1 espie 52: /*
53: * Support for include path search
54: * First search in the the current directory.
55: * If not found, and the path is not absolute, include path kicks in.
56: * First, -I options, in the order found on the command line.
57: * Then M4PATH env variable
58: */
59:
60: struct path_entry {
61: char *name;
62: struct path_entry *next;
63: } *first, *last;
64:
1.3 espie 65: static struct path_entry *new_path_entry __P((const char *));
66: static void ensure_m4path __P((void));
1.4 espie 67: static struct input_file *dopath __P((struct input_file *, const char *));
1.3 espie 68:
1.1 espie 69: static struct path_entry *
70: new_path_entry(dirname)
1.3 espie 71: const char *dirname;
1.1 espie 72: {
73: struct path_entry *n;
74:
75: n = malloc(sizeof(struct path_entry));
76: if (!n)
77: errx(1, "out of memory");
78: n->name = strdup(dirname);
79: if (!n->name)
80: errx(1, "out of memory");
81: n->next = 0;
82: return n;
83: }
84:
85: void
86: addtoincludepath(dirname)
87: const char *dirname;
88: {
89: struct path_entry *n;
90:
91: n = new_path_entry(dirname);
92:
93: if (last) {
94: last->next = n;
95: last = n;
96: }
97: else
98: last = first = n;
99: }
100:
101: static void
102: ensure_m4path()
103: {
104: static int envpathdone = 0;
105: char *envpath;
106: char *sweep;
107: char *path;
108:
109: if (envpathdone)
110: return;
111: envpathdone = TRUE;
112: envpath = getenv("M4PATH");
113: if (!envpath)
114: return;
115: /* for portability: getenv result is read-only */
116: envpath = strdup(envpath);
117: if (!envpath)
118: errx(1, "out of memory");
119: for (sweep = envpath;
120: (path = strsep(&sweep, ":")) != NULL;)
121: addtoincludepath(path);
122: free(envpath);
123: }
124:
125: static
1.4 espie 126: struct input_file *
127: dopath(i, filename)
128: struct input_file *i;
1.3 espie 129: const char *filename;
1.1 espie 130: {
131: char path[MAXPATHLEN];
132: struct path_entry *pe;
1.4 espie 133: FILE *f;
1.1 espie 134:
135: for (pe = first; pe; pe = pe->next) {
136: snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
1.4 espie 137: if ((f = fopen(path, "r")) != 0) {
138: set_input(i, f, path);
139: return i;
140: }
1.1 espie 141: }
142: return NULL;
143: }
144:
1.4 espie 145: struct input_file *
146: fopen_trypath(i, filename)
147: struct input_file *i;
1.1 espie 148: const char *filename;
149: {
150: FILE *f;
151:
152: f = fopen(filename, "r");
1.4 espie 153: if (f != NULL) {
154: set_input(i, f, filename);
155: return i;
156: }
1.1 espie 157: if (filename[0] == '/')
158: return NULL;
159:
160: ensure_m4path();
161:
1.4 espie 162: return dopath(i, filename);
1.1 espie 163: }
164:
1.5 espie 165: void
166: doindir(argv, argc)
167: const char *argv[];
168: int argc;
169: {
170: ndptr p;
171:
172: p = lookup(argv[2]);
173: if (p == NULL)
174: errx(1, "undefined macro %s", argv[2]);
175: argv[1] = p->defn;
176: if (p->type == MACRTYPE)
177: expand(argv+1, argc-1);
178: else
179: eval(argv+1, argc-1, p->type);
180: }
181:
182: void
183: dobuiltin(argv, argc)
184: const char *argv[];
185: int argc;
186: {
187: int n;
188: argv[1] = NULL;
189: n = builtin_type(argv[2]);
190: if (n != -1)
1.6 espie 191: eval(argv+1, argc-1, n);
1.5 espie 192: else
193: errx(1, "unknown builtin %s", argv[2]);
194: }
195:
196:
197: /* We need some temporary buffer space, as pb pushes BACK and substitution
198: * proceeds forward... */
199: static char *buffer;
200: static size_t bufsize = 0;
201: static size_t current = 0;
202:
203: static void addchars __P((const char *, size_t));
204: static void addchar __P((char));
205: static char *twiddle __P((const char *));
206: static char *getstring __P((void));
207: static void exit_regerror __P((int, regex_t *));
208: static void do_subst __P((const char *, regex_t *, const char *, regmatch_t *));
209: static void do_regexpindex __P((const char *, regex_t *, regmatch_t *));
210: static void do_regexp __P((const char *, regex_t *, const char *, regmatch_t *));
211: static void add_sub __P((int, const char *, regex_t *, regmatch_t *));
212: static void add_replace __P((const char *, regex_t *, const char *, regmatch_t *));
213:
214: static void
215: addchars(c, n)
216: const char *c;
217: size_t n;
218: {
219: if (n == 0)
220: return;
221: if (current + n > bufsize) {
222: if (bufsize == 0)
223: bufsize = 1024;
224: else
225: bufsize *= 2;
226: buffer = realloc(buffer, bufsize);
227: if (buffer == NULL)
228: errx(1, "out of memory");
229: }
230: memcpy(buffer+current, c, n);
231: current += n;
232: }
233:
234: static void
235: addchar(c)
236: char c;
237: {
238: if (current +1 > bufsize) {
239: if (bufsize == 0)
240: bufsize = 1024;
241: else
242: bufsize *= 2;
243: buffer = realloc(buffer, bufsize);
244: if (buffer == NULL)
245: errx(1, "out of memory");
246: }
247: buffer[current++] = c;
248: }
249:
250: static char *
251: getstring()
252: {
253: addchar('\0');
254: current = 0;
255: return buffer;
256: }
257:
258:
259: static void
260: exit_regerror(er, re)
261: int er;
262: regex_t *re;
263: {
264: size_t errlen;
265: char *errbuf;
266:
267: errlen = regerror(er, re, NULL, 0);
268: errbuf = xalloc(errlen);
269: regerror(er, re, errbuf, errlen);
270: errx(1, "regular expression error: %s", errbuf);
271: }
272:
273: static void
274: add_sub(n, string, re, pm)
275: int n;
276: const char *string;
277: regex_t *re;
278: regmatch_t *pm;
279: {
280: if (n > re->re_nsub)
281: warnx("No subexpression %d", n);
282: /* Subexpressions that did not match are
283: * not an error. */
284: else if (pm[n].rm_so != -1 &&
285: pm[n].rm_eo != -1) {
286: addchars(string + pm[n].rm_so,
287: pm[n].rm_eo - pm[n].rm_so);
288: }
289: }
290:
291: /* Add replacement string to the output buffer, recognizing special
292: * constructs and replacing them with substrings of the original string.
293: */
294: static void
295: add_replace(string, re, replace, pm)
296: const char *string;
297: regex_t *re;
298: const char *replace;
299: regmatch_t *pm;
300: {
301: const char *p;
302:
303: for (p = replace; *p != '\0'; p++) {
304: if (*p == '&' && !mimic_gnu) {
305: add_sub(0, string, re, pm);
306: continue;
307: }
308: if (*p == '\\') {
309: if (p[1] == '\\') {
310: addchar(p[1]);
1.10 ! espie 311: p++;
1.5 espie 312: continue;
313: }
314: if (p[1] == '&') {
315: if (mimic_gnu)
316: add_sub(0, string, re, pm);
317: else
318: addchar(p[1]);
319: p++;
320: continue;
321: }
322: if (isdigit(p[1])) {
323: add_sub(*(++p) - '0', string, re, pm);
324: continue;
325: }
326: }
327: addchar(*p);
328: }
329: }
330:
331: static void
332: do_subst(string, re, replace, pm)
333: const char *string;
334: regex_t *re;
335: const char *replace;
336: regmatch_t *pm;
337: {
338: int error;
339: regoff_t last_match = -1;
340:
341: while ((error = regexec(re, string, re->re_nsub+1, pm, 0)) == 0) {
342:
343: /* NULL length matches are special... We use the `vi-mode'
344: * rule: don't allow a NULL-match at the last match
345: * position.
346: */
347: if (pm[0].rm_so == pm[0].rm_eo && pm[0].rm_so == last_match) {
348: if (*string == '\0')
349: return;
350: addchar(*string);
351: string++;
352: continue;
353: }
354: last_match = pm[0].rm_so;
355: addchars(string, last_match);
356: add_replace(string, re, replace, pm);
357: string += pm[0].rm_eo;
358: }
359: if (error != REG_NOMATCH)
360: exit_regerror(error, re);
361: pbstr(string);
362: }
363:
364: static void
365: do_regexp(string, re, replace, pm)
366: const char *string;
367: regex_t *re;
368: const char *replace;
369: regmatch_t *pm;
370: {
371: int error;
372:
373: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
374: case 0:
375: add_replace(string, re, replace, pm);
376: pbstr(getstring());
377: break;
378: case REG_NOMATCH:
379: break;
380: default:
381: exit_regerror(error, re);
382: }
383: }
384:
385: static void
386: do_regexpindex(string, re, pm)
387: const char *string;
388: regex_t *re;
389: regmatch_t *pm;
390: {
391: int error;
392:
393: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
394: case 0:
395: pbunsigned(pm[0].rm_so);
396: break;
397: case REG_NOMATCH:
398: pbnum(-1);
399: break;
400: default:
401: exit_regerror(error, re);
402: }
403: }
404:
405: /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
406: * says. So we twiddle with the regexp before passing it to regcomp.
407: */
408: static char *
409: twiddle(p)
410: const char *p;
411: {
412: /* This could use strcspn for speed... */
413: while (*p != '\0') {
414: if (*p == '\\' && (p[1] == '(' || p[1] == ')')) {
415: addchar(p[1]);
416: p+=2;
417: continue;
418: }
419: if (*p == '(' || *p == ')')
420: addchar('\\');
421:
422: addchar(*p);
423: p++;
424: }
425: return getstring();
426: }
427:
428: /* patsubst(string, regexp, opt replacement) */
429: /* argv[2]: string
430: * argv[3]: regexp
431: * argv[4]: opt rep
432: */
433: void
434: dopatsubst(argv, argc)
435: const char *argv[];
436: int argc;
437: {
438: int error;
439: regex_t re;
440: regmatch_t *pmatch;
441:
442: if (argc <= 3) {
443: warnx("Too few arguments to patsubst");
444: return;
445: }
446: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
447: REG_EXTENDED);
448: if (error != 0)
449: exit_regerror(error, &re);
450:
451: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7 espie 452: do_subst(argv[2], &re,
453: argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
1.5 espie 454: pbstr(getstring());
455: free(pmatch);
456: regfree(&re);
457: }
458:
459: void
460: doregexp(argv, argc)
461: const char *argv[];
462: int argc;
463: {
464: int error;
465: regex_t re;
466: regmatch_t *pmatch;
467:
468: if (argc <= 3) {
1.7 espie 469: warnx("Too few arguments to regexp");
1.5 espie 470: return;
471: }
472: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
473: REG_EXTENDED);
474: if (error != 0)
475: exit_regerror(error, &re);
476:
477: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7 espie 478: if (argv[4] == NULL || argc == 4)
1.5 espie 479: do_regexpindex(argv[2], &re, pmatch);
480: else
481: do_regexp(argv[2], &re, argv[4], pmatch);
482: free(pmatch);
483: regfree(&re);
1.8 espie 484: }
485:
486: void
487: doesyscmd(cmd)
488: const char *cmd;
489: {
490: int p[2];
491: pid_t pid, cpid;
492: char *argv[4];
493: int cc;
494: int status;
495:
496: /* Follow gnu m4 documentation: first flush buffers. */
497: fflush(NULL);
498:
499: argv[0] = "sh";
500: argv[1] = "-c";
501: argv[2] = (char *)cmd;
502: argv[3] = NULL;
503:
504: /* Just set up standard output, share stderr and stdin with m4 */
505: if (pipe(p) == -1)
506: err(1, "bad pipe");
507: switch(cpid = fork()) {
508: case -1:
509: err(1, "bad fork");
510: /* NOTREACHED */
511: case 0:
512: (void) close(p[0]);
513: (void) dup2(p[1], 1);
514: (void) close(p[1]);
515: execv(_PATH_BSHELL, argv);
516: exit(1);
517: default:
518: /* Read result in two stages, since m4's buffer is
519: * pushback-only. */
520: (void) close(p[1]);
521: do {
522: char result[BUFSIZE];
523: cc = read(p[0], result, sizeof result);
524: if (cc > 0)
525: addchars(result, cc);
526: } while (cc > 0 || (cc == -1 && errno == EINTR));
527:
528: (void) close(p[0]);
529: while ((pid = wait(&status)) != cpid && pid >= 0)
530: continue;
531: pbstr(getstring());
532: }
1.5 espie 533: }