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