Annotation of src/usr.bin/m4/gnum4.c, Revision 1.12
1.12 ! espie 1: /* $OpenBSD: gnum4.c,v 1.11 2001/09/18 13:42:37 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>
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;
1.12 ! espie 176: eval(argv+1, argc-1, p->type);
1.5 espie 177: }
178:
179: void
180: dobuiltin(argv, argc)
181: const char *argv[];
182: int argc;
183: {
184: int n;
185: argv[1] = NULL;
186: n = builtin_type(argv[2]);
187: if (n != -1)
1.6 espie 188: eval(argv+1, argc-1, n);
1.5 espie 189: else
190: errx(1, "unknown builtin %s", argv[2]);
191: }
192:
193:
194: /* We need some temporary buffer space, as pb pushes BACK and substitution
195: * proceeds forward... */
196: static char *buffer;
197: static size_t bufsize = 0;
198: static size_t current = 0;
199:
200: static void addchars __P((const char *, size_t));
201: static void addchar __P((char));
202: static char *twiddle __P((const char *));
203: static char *getstring __P((void));
204: static void exit_regerror __P((int, regex_t *));
205: static void do_subst __P((const char *, regex_t *, const char *, regmatch_t *));
206: static void do_regexpindex __P((const char *, regex_t *, regmatch_t *));
207: static void do_regexp __P((const char *, regex_t *, const char *, regmatch_t *));
208: static void add_sub __P((int, const char *, regex_t *, regmatch_t *));
209: static void add_replace __P((const char *, regex_t *, const char *, regmatch_t *));
210:
211: static void
212: addchars(c, n)
213: const char *c;
214: size_t n;
215: {
216: if (n == 0)
217: return;
218: if (current + n > bufsize) {
219: if (bufsize == 0)
220: bufsize = 1024;
221: else
222: bufsize *= 2;
223: buffer = realloc(buffer, bufsize);
224: if (buffer == NULL)
225: errx(1, "out of memory");
226: }
227: memcpy(buffer+current, c, n);
228: current += n;
229: }
230:
231: static void
232: addchar(c)
233: char c;
234: {
235: if (current +1 > bufsize) {
236: if (bufsize == 0)
237: bufsize = 1024;
238: else
239: bufsize *= 2;
240: buffer = realloc(buffer, bufsize);
241: if (buffer == NULL)
242: errx(1, "out of memory");
243: }
244: buffer[current++] = c;
245: }
246:
247: static char *
248: getstring()
249: {
250: addchar('\0');
251: current = 0;
252: return buffer;
253: }
254:
255:
256: static void
257: exit_regerror(er, re)
258: int er;
259: regex_t *re;
260: {
261: size_t errlen;
262: char *errbuf;
263:
264: errlen = regerror(er, re, NULL, 0);
265: errbuf = xalloc(errlen);
266: regerror(er, re, errbuf, errlen);
267: errx(1, "regular expression error: %s", errbuf);
268: }
269:
270: static void
271: add_sub(n, string, re, pm)
272: int n;
273: const char *string;
274: regex_t *re;
275: regmatch_t *pm;
276: {
277: if (n > re->re_nsub)
278: warnx("No subexpression %d", n);
279: /* Subexpressions that did not match are
280: * not an error. */
281: else if (pm[n].rm_so != -1 &&
282: pm[n].rm_eo != -1) {
283: addchars(string + pm[n].rm_so,
284: pm[n].rm_eo - pm[n].rm_so);
285: }
286: }
287:
288: /* Add replacement string to the output buffer, recognizing special
289: * constructs and replacing them with substrings of the original string.
290: */
291: static void
292: add_replace(string, re, replace, pm)
293: const char *string;
294: regex_t *re;
295: const char *replace;
296: regmatch_t *pm;
297: {
298: const char *p;
299:
300: for (p = replace; *p != '\0'; p++) {
301: if (*p == '&' && !mimic_gnu) {
302: add_sub(0, string, re, pm);
303: continue;
304: }
305: if (*p == '\\') {
306: if (p[1] == '\\') {
307: addchar(p[1]);
1.10 espie 308: p++;
1.5 espie 309: continue;
310: }
311: if (p[1] == '&') {
312: if (mimic_gnu)
313: add_sub(0, string, re, pm);
314: else
315: addchar(p[1]);
316: p++;
317: continue;
318: }
319: if (isdigit(p[1])) {
320: add_sub(*(++p) - '0', string, re, pm);
321: continue;
322: }
323: }
324: addchar(*p);
325: }
326: }
327:
328: static void
329: do_subst(string, re, replace, pm)
330: const char *string;
331: regex_t *re;
332: const char *replace;
333: regmatch_t *pm;
334: {
335: int error;
1.11 espie 336: int flags = 0;
337: const char *last_match = NULL;
1.5 espie 338:
1.11 espie 339: while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
340: flags = REG_NOTBOL;
1.5 espie 341:
342: /* NULL length matches are special... We use the `vi-mode'
343: * rule: don't allow a NULL-match at the last match
344: * position.
345: */
1.11 espie 346: if (pm[0].rm_so == pm[0].rm_eo &&
347: string + pm[0].rm_so == last_match) {
1.5 espie 348: if (*string == '\0')
349: return;
350: addchar(*string);
351: string++;
352: continue;
353: }
1.11 espie 354: last_match = string + pm[0].rm_so;
355: addchars(string, pm[0].rm_so);
1.5 espie 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],
1.11 espie 447: REG_NEWLINE | REG_EXTENDED);
1.5 espie 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: }