Annotation of src/usr.bin/m4/gnum4.c, Revision 1.47
1.47 ! deraadt 1: /* $OpenBSD: gnum4.c,v 1.46 2014/07/10 14:12:31 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:
1.41 marco 28: /*
1.1 espie 29: * functions needed to support gnu-m4 extensions, including a fake freezing
30: */
31:
1.5 espie 32: #include <sys/types.h>
1.8 espie 33: #include <sys/wait.h>
1.5 espie 34: #include <ctype.h>
1.36 espie 35: #include <err.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>
1.8 espie 42: #include <errno.h>
43: #include <unistd.h>
1.47 ! deraadt 44: #include <limits.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
1.27 espie 54: * First search in the current directory.
1.1 espie 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: }
1.41 marco 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");
1.41 marco 111: if (!envpath)
1.1 espie 112: return;
113: /* for portability: getenv result is read-only */
114: envpath = strdup(envpath);
115: if (!envpath)
116: errx(1, "out of memory");
1.41 marco 117: for (sweep = envpath;
1.1 espie 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: {
1.47 ! deraadt 127: char path[PATH_MAX];
1.1 espie 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.41 marco 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.32 espie 167: m4errx(1, "indir: undefined macro %s.", argv[2]);
1.5 espie 168: argv[1] = p->defn;
1.41 marco 169:
1.25 espie 170: eval(argv+1, argc-1, p->type, is_traced(n));
1.5 espie 171: }
172:
1.41 marco 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
1.32 espie 183: m4errx(1, "unknown builtin %s.", argv[2]);
1.41 marco 184: }
1.5 espie 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);
1.46 espie 197: static void exit_regerror(int, regex_t *, const char *);
198: static void do_subst(const char *, regex_t *, const char *, const char *,
199: regmatch_t *);
200: static void do_regexpindex(const char *, regex_t *, const char *, regmatch_t *);
201: static void do_regexp(const char *, regex_t *, const char *, const char *,
202: regmatch_t *);
1.16 millert 203: static void add_sub(int, const char *, regex_t *, regmatch_t *);
204: static void add_replace(const char *, regex_t *, const char *, regmatch_t *);
1.14 espie 205: #define addconstantstring(s) addchars((s), sizeof(s)-1)
1.5 espie 206:
1.41 marco 207: static void
1.18 espie 208: addchars(const char *c, size_t n)
1.5 espie 209: {
210: if (n == 0)
211: return;
1.15 espie 212: while (current + n > bufsize) {
1.5 espie 213: if (bufsize == 0)
214: bufsize = 1024;
215: else
216: bufsize *= 2;
1.26 espie 217: buffer = xrealloc(buffer, bufsize, NULL);
1.5 espie 218: }
219: memcpy(buffer+current, c, n);
220: current += n;
221: }
222:
1.41 marco 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;
1.26 espie 231: buffer = xrealloc(buffer, bufsize, NULL);
1.5 espie 232: }
233: buffer[current++] = c;
234: }
235:
236: static char *
237: getstring()
238: {
239: addchar('\0');
240: current = 0;
241: return buffer;
242: }
243:
244:
1.41 marco 245: static void
1.46 espie 246: exit_regerror(int er, regex_t *re, const char *source)
1.5 espie 247: {
1.41 marco 248: size_t errlen;
249: char *errbuf;
1.5 espie 250:
251: errlen = regerror(er, re, NULL, 0);
1.41 marco 252: errbuf = xalloc(errlen,
1.26 espie 253: "malloc in regerror: %lu", (unsigned long)errlen);
1.5 espie 254: regerror(er, re, errbuf, errlen);
1.46 espie 255: m4errx(1, "regular expression error in %s: %s.", source, errbuf);
1.5 espie 256: }
257:
258: static void
1.18 espie 259: add_sub(int n, const char *string, regex_t *re, regmatch_t *pm)
1.5 espie 260: {
261: if (n > re->re_nsub)
262: warnx("No subexpression %d", n);
263: /* Subexpressions that did not match are
264: * not an error. */
265: else if (pm[n].rm_so != -1 &&
266: pm[n].rm_eo != -1) {
267: addchars(string + pm[n].rm_so,
268: pm[n].rm_eo - pm[n].rm_so);
269: }
270: }
271:
272: /* Add replacement string to the output buffer, recognizing special
273: * constructs and replacing them with substrings of the original string.
274: */
1.41 marco 275: static void
1.18 espie 276: add_replace(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
1.5 espie 277: {
278: const char *p;
279:
280: for (p = replace; *p != '\0'; p++) {
281: if (*p == '&' && !mimic_gnu) {
282: add_sub(0, string, re, pm);
283: continue;
284: }
285: if (*p == '\\') {
286: if (p[1] == '\\') {
287: addchar(p[1]);
1.10 espie 288: p++;
1.5 espie 289: continue;
290: }
291: if (p[1] == '&') {
292: if (mimic_gnu)
293: add_sub(0, string, re, pm);
294: else
295: addchar(p[1]);
296: p++;
297: continue;
298: }
1.43 deraadt 299: if (isdigit((unsigned char)p[1])) {
1.5 espie 300: add_sub(*(++p) - '0', string, re, pm);
301: continue;
302: }
303: }
1.41 marco 304: addchar(*p);
1.5 espie 305: }
306: }
307:
1.41 marco 308: static void
1.46 espie 309: do_subst(const char *string, regex_t *re, const char *source,
310: const char *replace, regmatch_t *pm)
1.5 espie 311: {
312: int error;
1.11 espie 313: int flags = 0;
314: const char *last_match = NULL;
1.5 espie 315:
1.11 espie 316: while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
1.13 espie 317: if (pm[0].rm_eo != 0) {
318: if (string[pm[0].rm_eo-1] == '\n')
319: flags = 0;
320: else
321: flags = REG_NOTBOL;
322: }
1.5 espie 323:
1.41 marco 324: /* NULL length matches are special... We use the `vi-mode'
1.5 espie 325: * rule: don't allow a NULL-match at the last match
1.41 marco 326: * position.
1.5 espie 327: */
1.41 marco 328: if (pm[0].rm_so == pm[0].rm_eo &&
1.11 espie 329: string + pm[0].rm_so == last_match) {
1.5 espie 330: if (*string == '\0')
331: return;
332: addchar(*string);
1.13 espie 333: if (*string++ == '\n')
334: flags = 0;
335: else
336: flags = REG_NOTBOL;
1.5 espie 337: continue;
338: }
1.11 espie 339: last_match = string + pm[0].rm_so;
340: addchars(string, pm[0].rm_so);
1.5 espie 341: add_replace(string, re, replace, pm);
342: string += pm[0].rm_eo;
343: }
344: if (error != REG_NOMATCH)
1.46 espie 345: exit_regerror(error, re, source);
1.5 espie 346: pbstr(string);
347: }
348:
1.41 marco 349: static void
1.46 espie 350: do_regexp(const char *string, regex_t *re, const char *source,
351: 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)) {
1.41 marco 356: case 0:
1.5 espie 357: add_replace(string, re, replace, pm);
358: pbstr(getstring());
359: break;
360: case REG_NOMATCH:
361: break;
362: default:
1.46 espie 363: exit_regerror(error, re, source);
1.5 espie 364: }
365: }
366:
1.41 marco 367: static void
1.46 espie 368: do_regexpindex(const char *string, regex_t *re, const char *source,
369: regmatch_t *pm)
1.5 espie 370: {
371: int error;
372:
373: switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
374: case 0:
375: pbunsigned(pm[0].rm_so);
376: break;
377: case REG_NOMATCH:
378: pbnum(-1);
379: break;
380: default:
1.46 espie 381: exit_regerror(error, re, source);
1.5 espie 382: }
383: }
384:
385: /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
386: * says. So we twiddle with the regexp before passing it to regcomp.
387: */
388: static char *
1.18 espie 389: twiddle(const char *p)
1.5 espie 390: {
1.28 espie 391: /* + at start of regexp is a normal character for Gnu m4 */
392: if (*p == '^') {
393: addchar(*p);
394: p++;
395: }
396: if (*p == '+') {
397: addchar('\\');
398: }
1.5 espie 399: /* This could use strcspn for speed... */
400: while (*p != '\0') {
1.14 espie 401: if (*p == '\\') {
402: switch(p[1]) {
403: case '(':
404: case ')':
405: case '|':
406: addchar(p[1]);
407: break;
408: case 'w':
409: addconstantstring("[_a-zA-Z0-9]");
410: break;
411: case 'W':
412: addconstantstring("[^_a-zA-Z0-9]");
413: break;
414: case '<':
415: addconstantstring("[[:<:]]");
416: break;
417: case '>':
418: addconstantstring("[[:>:]]");
419: break;
420: default:
421: addchars(p, 2);
422: break;
423: }
1.5 espie 424: p+=2;
425: continue;
426: }
1.14 espie 427: if (*p == '(' || *p == ')' || *p == '|')
1.5 espie 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
1.18 espie 442: dopatsubst(const char *argv[], int argc)
1.5 espie 443: {
444: if (argc <= 3) {
445: warnx("Too few arguments to patsubst");
446: return;
447: }
1.20 espie 448: /* special case: empty regexp */
449: if (argv[3][0] == '\0') {
450: const char *s;
1.21 espie 451: size_t len;
1.41 marco 452: if (argc > 4 && argv[4])
1.21 espie 453: len = strlen(argv[4]);
454: else
455: len = 0;
1.20 espie 456: for (s = argv[2]; *s != '\0'; s++) {
457: addchars(argv[4], len);
458: addchar(*s);
459: }
460: } else {
461: int error;
462: regex_t re;
463: regmatch_t *pmatch;
1.30 espie 464: int mode = REG_EXTENDED;
1.46 espie 465: const char *source;
1.30 espie 466: size_t l = strlen(argv[3]);
467:
468: if (!mimic_gnu ||
1.41 marco 469: (argv[3][0] == '^') ||
1.30 espie 470: (l > 0 && argv[3][l-1] == '$'))
471: mode |= REG_NEWLINE;
1.20 espie 472:
1.46 espie 473: source = mimic_gnu ? twiddle(argv[3]) : argv[3];
474: error = regcomp(&re, source, mode);
1.20 espie 475: if (error != 0)
1.46 espie 476: exit_regerror(error, &re, source);
1.41 marco 477:
1.45 espie 478: pmatch = xreallocarray(NULL, re.re_nsub+1, sizeof(regmatch_t),
479: NULL);
1.46 espie 480: do_subst(argv[2], &re, source,
1.21 espie 481: argc > 4 && argv[4] != NULL ? argv[4] : "", pmatch);
1.20 espie 482: free(pmatch);
483: regfree(&re);
484: }
1.5 espie 485: pbstr(getstring());
486: }
487:
488: void
1.18 espie 489: doregexp(const char *argv[], int argc)
1.5 espie 490: {
491: int error;
492: regex_t re;
493: regmatch_t *pmatch;
1.46 espie 494: const char *source;
1.5 espie 495:
496: if (argc <= 3) {
1.7 espie 497: warnx("Too few arguments to regexp");
1.5 espie 498: return;
499: }
1.40 espie 500: /* special gnu case */
501: if (argv[3][0] == '\0' && mimic_gnu) {
502: if (argc == 4 || argv[4] == NULL)
503: return;
1.41 marco 504: else
1.40 espie 505: pbstr(argv[4]);
506: }
1.46 espie 507: source = mimic_gnu ? twiddle(argv[3]) : argv[3];
508: error = regcomp(&re, source, REG_EXTENDED|REG_NEWLINE);
1.5 espie 509: if (error != 0)
1.46 espie 510: exit_regerror(error, &re, source);
1.41 marco 511:
1.45 espie 512: pmatch = xreallocarray(NULL, re.re_nsub+1, sizeof(regmatch_t), NULL);
1.40 espie 513: if (argc == 4 || argv[4] == NULL)
1.46 espie 514: do_regexpindex(argv[2], &re, source, pmatch);
1.5 espie 515: else
1.46 espie 516: do_regexp(argv[2], &re, source, argv[4], pmatch);
1.5 espie 517: free(pmatch);
518: regfree(&re);
1.33 espie 519: }
520:
521: void
522: doformat(const char *argv[], int argc)
523: {
524: const char *format = argv[2];
525: int pos = 3;
1.39 espie 526: int left_padded;
527: long width;
528: size_t l;
529: const char *thisarg;
530: char temp[2];
531: long extra;
1.35 espie 532:
1.33 espie 533: while (*format != 0) {
534: if (*format != '%') {
535: addchar(*format++);
1.39 espie 536: continue;
537: }
538:
539: format++;
540: if (*format == '%') {
541: addchar(*format++);
542: continue;
543: }
544: if (*format == 0) {
545: addchar('%');
546: break;
547: }
548:
549: if (*format == '*') {
550: format++;
551: if (pos >= argc)
1.41 marco 552: m4errx(1,
1.39 espie 553: "Format with too many format specifiers.");
554: width = strtol(argv[pos++], NULL, 10);
555: } else {
556: width = strtol(format, (char **)&format, 10);
557: }
558: if (width < 0) {
559: left_padded = 1;
560: width = -width;
1.37 espie 561: } else {
1.39 espie 562: left_padded = 0;
563: }
564: if (*format == '.') {
1.33 espie 565: format++;
1.39 espie 566: if (*format == '*') {
567: format++;
568: if (pos >= argc)
1.41 marco 569: m4errx(1,
1.39 espie 570: "Format with too many format specifiers.");
571: extra = strtol(argv[pos++], NULL, 10);
1.33 espie 572: } else {
1.39 espie 573: extra = strtol(format, (char **)&format, 10);
1.33 espie 574: }
1.39 espie 575: } else {
576: extra = LONG_MAX;
577: }
578: if (pos >= argc)
579: m4errx(1, "Format with too many format specifiers.");
580: switch(*format) {
581: case 's':
582: thisarg = argv[pos++];
583: break;
584: case 'c':
585: temp[0] = strtoul(argv[pos++], NULL, 10);
586: temp[1] = 0;
587: thisarg = temp;
588: break;
589: default:
1.41 marco 590: m4errx(1, "Unsupported format specification: %s.",
1.39 espie 591: argv[2]);
592: }
593: format++;
594: l = strlen(thisarg);
595: if (l > extra)
596: l = extra;
597: if (!left_padded) {
598: while (l < width--)
599: addchar(' ');
600: }
601: addchars(thisarg, l);
602: if (left_padded) {
603: while (l < width--)
604: addchar(' ');
1.33 espie 605: }
606: }
607: pbstr(getstring());
1.8 espie 608: }
609:
610: void
1.18 espie 611: doesyscmd(const char *cmd)
1.8 espie 612: {
613: int p[2];
614: pid_t pid, cpid;
615: char *argv[4];
616: int cc;
617: int status;
618:
619: /* Follow gnu m4 documentation: first flush buffers. */
620: fflush(NULL);
621:
622: argv[0] = "sh";
623: argv[1] = "-c";
624: argv[2] = (char *)cmd;
625: argv[3] = NULL;
626:
627: /* Just set up standard output, share stderr and stdin with m4 */
628: if (pipe(p) == -1)
629: err(1, "bad pipe");
630: switch(cpid = fork()) {
631: case -1:
632: err(1, "bad fork");
633: /* NOTREACHED */
634: case 0:
635: (void) close(p[0]);
636: (void) dup2(p[1], 1);
637: (void) close(p[1]);
638: execv(_PATH_BSHELL, argv);
639: exit(1);
640: default:
641: /* Read result in two stages, since m4's buffer is
642: * pushback-only. */
643: (void) close(p[1]);
644: do {
645: char result[BUFSIZE];
646: cc = read(p[0], result, sizeof result);
647: if (cc > 0)
648: addchars(result, cc);
649: } while (cc > 0 || (cc == -1 && errno == EINTR));
650:
651: (void) close(p[0]);
652: while ((pid = wait(&status)) != cpid && pid >= 0)
653: continue;
654: pbstr(getstring());
655: }
1.31 espie 656: }
657:
658: void
659: getdivfile(const char *name)
660: {
661: FILE *f;
662: int c;
663:
664: f = fopen(name, "r");
665: if (!f)
666: return;
667:
668: while ((c = getc(f))!= EOF)
669: putc(c, active);
670: (void) fclose(f);
1.5 espie 671: }