Annotation of src/usr.bin/m4/gnum4.c, Revision 1.13
1.13 ! espie 1: /* $OpenBSD: gnum4.c,v 1.12 2001/09/18 13:52:58 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) {
1.13 ! espie 340: if (pm[0].rm_eo != 0) {
! 341: if (string[pm[0].rm_eo-1] == '\n')
! 342: flags = 0;
! 343: else
! 344: flags = REG_NOTBOL;
! 345: }
1.5 espie 346:
347: /* NULL length matches are special... We use the `vi-mode'
348: * rule: don't allow a NULL-match at the last match
349: * position.
350: */
1.11 espie 351: if (pm[0].rm_so == pm[0].rm_eo &&
352: string + pm[0].rm_so == last_match) {
1.5 espie 353: if (*string == '\0')
354: return;
355: addchar(*string);
1.13 ! espie 356: if (*string++ == '\n')
! 357: flags = 0;
! 358: else
! 359: flags = REG_NOTBOL;
1.5 espie 360: continue;
361: }
1.11 espie 362: last_match = string + pm[0].rm_so;
363: addchars(string, pm[0].rm_so);
1.5 espie 364: add_replace(string, re, replace, pm);
365: string += pm[0].rm_eo;
366: }
367: if (error != REG_NOMATCH)
368: exit_regerror(error, re);
369: pbstr(string);
370: }
371:
372: static void
373: do_regexp(string, re, replace, pm)
374: const char *string;
375: regex_t *re;
376: const char *replace;
377: regmatch_t *pm;
378: {
379: int error;
380:
381: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
382: case 0:
383: add_replace(string, re, replace, pm);
384: pbstr(getstring());
385: break;
386: case REG_NOMATCH:
387: break;
388: default:
389: exit_regerror(error, re);
390: }
391: }
392:
393: static void
394: do_regexpindex(string, re, pm)
395: const char *string;
396: regex_t *re;
397: regmatch_t *pm;
398: {
399: int error;
400:
401: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
402: case 0:
403: pbunsigned(pm[0].rm_so);
404: break;
405: case REG_NOMATCH:
406: pbnum(-1);
407: break;
408: default:
409: exit_regerror(error, re);
410: }
411: }
412:
413: /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
414: * says. So we twiddle with the regexp before passing it to regcomp.
415: */
416: static char *
417: twiddle(p)
418: const char *p;
419: {
420: /* This could use strcspn for speed... */
421: while (*p != '\0') {
422: if (*p == '\\' && (p[1] == '(' || p[1] == ')')) {
423: addchar(p[1]);
424: p+=2;
425: continue;
426: }
427: if (*p == '(' || *p == ')')
428: addchar('\\');
429:
430: addchar(*p);
431: p++;
432: }
433: return getstring();
434: }
435:
436: /* patsubst(string, regexp, opt replacement) */
437: /* argv[2]: string
438: * argv[3]: regexp
439: * argv[4]: opt rep
440: */
441: void
442: dopatsubst(argv, argc)
443: const char *argv[];
444: int argc;
445: {
446: int error;
447: regex_t re;
448: regmatch_t *pmatch;
449:
450: if (argc <= 3) {
451: warnx("Too few arguments to patsubst");
452: return;
453: }
454: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
1.11 espie 455: REG_NEWLINE | REG_EXTENDED);
1.5 espie 456: if (error != 0)
457: exit_regerror(error, &re);
458:
459: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7 espie 460: do_subst(argv[2], &re,
461: argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
1.5 espie 462: pbstr(getstring());
463: free(pmatch);
464: regfree(&re);
465: }
466:
467: void
468: doregexp(argv, argc)
469: const char *argv[];
470: int argc;
471: {
472: int error;
473: regex_t re;
474: regmatch_t *pmatch;
475:
476: if (argc <= 3) {
1.7 espie 477: warnx("Too few arguments to regexp");
1.5 espie 478: return;
479: }
480: error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
481: REG_EXTENDED);
482: if (error != 0)
483: exit_regerror(error, &re);
484:
485: pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7 espie 486: if (argv[4] == NULL || argc == 4)
1.5 espie 487: do_regexpindex(argv[2], &re, pmatch);
488: else
489: do_regexp(argv[2], &re, argv[4], pmatch);
490: free(pmatch);
491: regfree(&re);
1.8 espie 492: }
493:
494: void
495: doesyscmd(cmd)
496: const char *cmd;
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: }