Annotation of src/usr.bin/m4/gnum4.c, Revision 1.11
1.11 ! espie 1: /* $OpenBSD: gnum4.c,v 1.10 2001/09/16 21:08:55 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;
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;
1.11 ! espie 339: int flags = 0;
! 340: const char *last_match = NULL;
1.5 espie 341:
1.11 ! espie 342: while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
! 343: flags = REG_NOTBOL;
1.5 espie 344:
345: /* NULL length matches are special... We use the `vi-mode'
346: * rule: don't allow a NULL-match at the last match
347: * position.
348: */
1.11 ! espie 349: if (pm[0].rm_so == pm[0].rm_eo &&
! 350: string + pm[0].rm_so == last_match) {
1.5 espie 351: if (*string == '\0')
352: return;
353: addchar(*string);
354: string++;
355: continue;
356: }
1.11 ! espie 357: last_match = string + pm[0].rm_so;
! 358: addchars(string, pm[0].rm_so);
1.5 espie 359: add_replace(string, re, replace, pm);
360: string += pm[0].rm_eo;
361: }
362: if (error != REG_NOMATCH)
363: exit_regerror(error, re);
364: pbstr(string);
365: }
366:
367: static void
368: do_regexp(string, re, replace, pm)
369: const char *string;
370: regex_t *re;
371: const char *replace;
372: regmatch_t *pm;
373: {
374: int error;
375:
376: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
377: case 0:
378: add_replace(string, re, replace, pm);
379: pbstr(getstring());
380: break;
381: case REG_NOMATCH:
382: break;
383: default:
384: exit_regerror(error, re);
385: }
386: }
387:
388: static void
389: do_regexpindex(string, re, pm)
390: const char *string;
391: regex_t *re;
392: regmatch_t *pm;
393: {
394: int error;
395:
396: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
397: case 0:
398: pbunsigned(pm[0].rm_so);
399: break;
400: case REG_NOMATCH:
401: pbnum(-1);
402: break;
403: default:
404: exit_regerror(error, re);
405: }
406: }
407:
408: /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
409: * says. So we twiddle with the regexp before passing it to regcomp.
410: */
411: static char *
412: twiddle(p)
413: const char *p;
414: {
415: /* This could use strcspn for speed... */
416: while (*p != '\0') {
417: if (*p == '\\' && (p[1] == '(' || p[1] == ')')) {
418: addchar(p[1]);
419: p+=2;
420: continue;
421: }
422: if (*p == '(' || *p == ')')
423: addchar('\\');
424:
425: addchar(*p);
426: p++;
427: }
428: return getstring();
429: }
430:
431: /* patsubst(string, regexp, opt replacement) */
432: /* argv[2]: string
433: * argv[3]: regexp
434: * argv[4]: opt rep
435: */
436: void
437: dopatsubst(argv, argc)
438: const char *argv[];
439: int argc;
440: {
441: int error;
442: regex_t re;
443: regmatch_t *pmatch;
444:
445: if (argc <= 3) {
446: warnx("Too few arguments to patsubst");
447: return;
448: }
449: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
1.11 ! espie 450: REG_NEWLINE | REG_EXTENDED);
1.5 espie 451: if (error != 0)
452: exit_regerror(error, &re);
453:
454: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7 espie 455: do_subst(argv[2], &re,
456: argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
1.5 espie 457: pbstr(getstring());
458: free(pmatch);
459: regfree(&re);
460: }
461:
462: void
463: doregexp(argv, argc)
464: const char *argv[];
465: int argc;
466: {
467: int error;
468: regex_t re;
469: regmatch_t *pmatch;
470:
471: if (argc <= 3) {
1.7 espie 472: warnx("Too few arguments to regexp");
1.5 espie 473: return;
474: }
475: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
476: REG_EXTENDED);
477: if (error != 0)
478: exit_regerror(error, &re);
479:
480: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7 espie 481: if (argv[4] == NULL || argc == 4)
1.5 espie 482: do_regexpindex(argv[2], &re, pmatch);
483: else
484: do_regexp(argv[2], &re, argv[4], pmatch);
485: free(pmatch);
486: regfree(&re);
1.8 espie 487: }
488:
489: void
490: doesyscmd(cmd)
491: const char *cmd;
492: {
493: int p[2];
494: pid_t pid, cpid;
495: char *argv[4];
496: int cc;
497: int status;
498:
499: /* Follow gnu m4 documentation: first flush buffers. */
500: fflush(NULL);
501:
502: argv[0] = "sh";
503: argv[1] = "-c";
504: argv[2] = (char *)cmd;
505: argv[3] = NULL;
506:
507: /* Just set up standard output, share stderr and stdin with m4 */
508: if (pipe(p) == -1)
509: err(1, "bad pipe");
510: switch(cpid = fork()) {
511: case -1:
512: err(1, "bad fork");
513: /* NOTREACHED */
514: case 0:
515: (void) close(p[0]);
516: (void) dup2(p[1], 1);
517: (void) close(p[1]);
518: execv(_PATH_BSHELL, argv);
519: exit(1);
520: default:
521: /* Read result in two stages, since m4's buffer is
522: * pushback-only. */
523: (void) close(p[1]);
524: do {
525: char result[BUFSIZE];
526: cc = read(p[0], result, sizeof result);
527: if (cc > 0)
528: addchars(result, cc);
529: } while (cc > 0 || (cc == -1 && errno == EINTR));
530:
531: (void) close(p[0]);
532: while ((pid = wait(&status)) != cpid && pid >= 0)
533: continue;
534: pbstr(getstring());
535: }
1.5 espie 536: }