gnum4.c revision 1.1 1 1.1 tv /* $NetBSD: gnum4.c,v 1.1 2001/11/14 06:16:08 tv Exp $ */
2 1.1 tv /* $OpenBSD: gnum4.c,v 1.15 2001/10/13 20:18:48 espie Exp $ */
3 1.1 tv
4 1.1 tv /*
5 1.1 tv * Copyright (c) 1999 Marc Espie
6 1.1 tv *
7 1.1 tv * Redistribution and use in source and binary forms, with or without
8 1.1 tv * modification, are permitted provided that the following conditions
9 1.1 tv * are met:
10 1.1 tv * 1. Redistributions of source code must retain the above copyright
11 1.1 tv * notice, this list of conditions and the following disclaimer.
12 1.1 tv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tv * notice, this list of conditions and the following disclaimer in the
14 1.1 tv * documentation and/or other materials provided with the distribution.
15 1.1 tv *
16 1.1 tv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1 tv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 tv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 tv * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1 tv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 tv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 tv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 tv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 tv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 tv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 tv * SUCH DAMAGE.
27 1.1 tv */
28 1.1 tv
29 1.1 tv /*
30 1.1 tv * functions needed to support gnu-m4 extensions, including a fake freezing
31 1.1 tv */
32 1.1 tv
33 1.1 tv #include <sys/param.h>
34 1.1 tv #include <sys/types.h>
35 1.1 tv #include <sys/wait.h>
36 1.1 tv #include <ctype.h>
37 1.1 tv #include <err.h>
38 1.1 tv #include <errno.h>
39 1.1 tv #include <paths.h>
40 1.1 tv #include <regex.h>
41 1.1 tv #include <stddef.h>
42 1.1 tv #include <stdlib.h>
43 1.1 tv #include <stdio.h>
44 1.1 tv #include <string.h>
45 1.1 tv #include "mdef.h"
46 1.1 tv #include "stdd.h"
47 1.1 tv #include "extern.h"
48 1.1 tv
49 1.1 tv
50 1.1 tv int mimic_gnu = 0;
51 1.1 tv
52 1.1 tv /*
53 1.1 tv * Support for include path search
54 1.1 tv * First search in the the current directory.
55 1.1 tv * If not found, and the path is not absolute, include path kicks in.
56 1.1 tv * First, -I options, in the order found on the command line.
57 1.1 tv * Then M4PATH env variable
58 1.1 tv */
59 1.1 tv
60 1.1 tv struct path_entry {
61 1.1 tv char *name;
62 1.1 tv struct path_entry *next;
63 1.1 tv } *first, *last;
64 1.1 tv
65 1.1 tv static struct path_entry *new_path_entry __P((const char *));
66 1.1 tv static void ensure_m4path __P((void));
67 1.1 tv static struct input_file *dopath __P((struct input_file *, const char *));
68 1.1 tv
69 1.1 tv static struct path_entry *
70 1.1 tv new_path_entry(dirname)
71 1.1 tv const char *dirname;
72 1.1 tv {
73 1.1 tv struct path_entry *n;
74 1.1 tv
75 1.1 tv n = malloc(sizeof(struct path_entry));
76 1.1 tv if (!n)
77 1.1 tv errx(1, "out of memory");
78 1.1 tv n->name = strdup(dirname);
79 1.1 tv if (!n->name)
80 1.1 tv errx(1, "out of memory");
81 1.1 tv n->next = 0;
82 1.1 tv return n;
83 1.1 tv }
84 1.1 tv
85 1.1 tv void
86 1.1 tv addtoincludepath(dirname)
87 1.1 tv const char *dirname;
88 1.1 tv {
89 1.1 tv struct path_entry *n;
90 1.1 tv
91 1.1 tv n = new_path_entry(dirname);
92 1.1 tv
93 1.1 tv if (last) {
94 1.1 tv last->next = n;
95 1.1 tv last = n;
96 1.1 tv }
97 1.1 tv else
98 1.1 tv last = first = n;
99 1.1 tv }
100 1.1 tv
101 1.1 tv static void
102 1.1 tv ensure_m4path()
103 1.1 tv {
104 1.1 tv static int envpathdone = 0;
105 1.1 tv char *envpath;
106 1.1 tv char *sweep;
107 1.1 tv char *path;
108 1.1 tv
109 1.1 tv if (envpathdone)
110 1.1 tv return;
111 1.1 tv envpathdone = TRUE;
112 1.1 tv envpath = getenv("M4PATH");
113 1.1 tv if (!envpath)
114 1.1 tv return;
115 1.1 tv /* for portability: getenv result is read-only */
116 1.1 tv envpath = strdup(envpath);
117 1.1 tv if (!envpath)
118 1.1 tv errx(1, "out of memory");
119 1.1 tv for (sweep = envpath;
120 1.1 tv (path = strsep(&sweep, ":")) != NULL;)
121 1.1 tv addtoincludepath(path);
122 1.1 tv free(envpath);
123 1.1 tv }
124 1.1 tv
125 1.1 tv static
126 1.1 tv struct input_file *
127 1.1 tv dopath(i, filename)
128 1.1 tv struct input_file *i;
129 1.1 tv const char *filename;
130 1.1 tv {
131 1.1 tv char path[MAXPATHLEN];
132 1.1 tv struct path_entry *pe;
133 1.1 tv FILE *f;
134 1.1 tv
135 1.1 tv for (pe = first; pe; pe = pe->next) {
136 1.1 tv snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
137 1.1 tv if ((f = fopen(path, "r")) != 0) {
138 1.1 tv set_input(i, f, path);
139 1.1 tv return i;
140 1.1 tv }
141 1.1 tv }
142 1.1 tv return NULL;
143 1.1 tv }
144 1.1 tv
145 1.1 tv struct input_file *
146 1.1 tv fopen_trypath(i, filename)
147 1.1 tv struct input_file *i;
148 1.1 tv const char *filename;
149 1.1 tv {
150 1.1 tv FILE *f;
151 1.1 tv
152 1.1 tv f = fopen(filename, "r");
153 1.1 tv if (f != NULL) {
154 1.1 tv set_input(i, f, filename);
155 1.1 tv return i;
156 1.1 tv }
157 1.1 tv if (filename[0] == '/')
158 1.1 tv return NULL;
159 1.1 tv
160 1.1 tv ensure_m4path();
161 1.1 tv
162 1.1 tv return dopath(i, filename);
163 1.1 tv }
164 1.1 tv
165 1.1 tv void
166 1.1 tv doindir(argv, argc)
167 1.1 tv const char *argv[];
168 1.1 tv int argc;
169 1.1 tv {
170 1.1 tv ndptr p;
171 1.1 tv
172 1.1 tv p = lookup(argv[2]);
173 1.1 tv if (p == NULL)
174 1.1 tv errx(1, "undefined macro %s", argv[2]);
175 1.1 tv argv[1] = p->defn;
176 1.1 tv eval(argv+1, argc-1, p->type);
177 1.1 tv }
178 1.1 tv
179 1.1 tv void
180 1.1 tv dobuiltin(argv, argc)
181 1.1 tv const char *argv[];
182 1.1 tv int argc;
183 1.1 tv {
184 1.1 tv int n;
185 1.1 tv argv[1] = NULL;
186 1.1 tv n = builtin_type(argv[2]);
187 1.1 tv if (n != -1)
188 1.1 tv eval(argv+1, argc-1, n);
189 1.1 tv else
190 1.1 tv errx(1, "unknown builtin %s", argv[2]);
191 1.1 tv }
192 1.1 tv
193 1.1 tv
194 1.1 tv /* We need some temporary buffer space, as pb pushes BACK and substitution
195 1.1 tv * proceeds forward... */
196 1.1 tv static char *buffer;
197 1.1 tv static size_t bufsize = 0;
198 1.1 tv static size_t current = 0;
199 1.1 tv
200 1.1 tv static void addchars __P((const char *, size_t));
201 1.1 tv static void addchar __P((char));
202 1.1 tv static char *twiddle __P((const char *));
203 1.1 tv static char *getstring __P((void));
204 1.1 tv static void exit_regerror __P((int, regex_t *));
205 1.1 tv static void do_subst __P((const char *, regex_t *, const char *, regmatch_t *));
206 1.1 tv static void do_regexpindex __P((const char *, regex_t *, regmatch_t *));
207 1.1 tv static void do_regexp __P((const char *, regex_t *, const char *, regmatch_t *));
208 1.1 tv static void add_sub __P((int, const char *, regex_t *, regmatch_t *));
209 1.1 tv static void add_replace __P((const char *, regex_t *, const char *, regmatch_t *));
210 1.1 tv #define addconstantstring(s) addchars((s), sizeof(s)-1)
211 1.1 tv
212 1.1 tv static void
213 1.1 tv addchars(c, n)
214 1.1 tv const char *c;
215 1.1 tv size_t n;
216 1.1 tv {
217 1.1 tv if (n == 0)
218 1.1 tv return;
219 1.1 tv while (current + n > bufsize) {
220 1.1 tv if (bufsize == 0)
221 1.1 tv bufsize = 1024;
222 1.1 tv else
223 1.1 tv bufsize *= 2;
224 1.1 tv buffer = realloc(buffer, bufsize);
225 1.1 tv if (buffer == NULL)
226 1.1 tv errx(1, "out of memory");
227 1.1 tv }
228 1.1 tv memcpy(buffer+current, c, n);
229 1.1 tv current += n;
230 1.1 tv }
231 1.1 tv
232 1.1 tv static void
233 1.1 tv addchar(c)
234 1.1 tv char c;
235 1.1 tv {
236 1.1 tv if (current +1 > bufsize) {
237 1.1 tv if (bufsize == 0)
238 1.1 tv bufsize = 1024;
239 1.1 tv else
240 1.1 tv bufsize *= 2;
241 1.1 tv buffer = realloc(buffer, bufsize);
242 1.1 tv if (buffer == NULL)
243 1.1 tv errx(1, "out of memory");
244 1.1 tv }
245 1.1 tv buffer[current++] = c;
246 1.1 tv }
247 1.1 tv
248 1.1 tv static char *
249 1.1 tv getstring()
250 1.1 tv {
251 1.1 tv addchar('\0');
252 1.1 tv current = 0;
253 1.1 tv return buffer;
254 1.1 tv }
255 1.1 tv
256 1.1 tv
257 1.1 tv static void
258 1.1 tv exit_regerror(er, re)
259 1.1 tv int er;
260 1.1 tv regex_t *re;
261 1.1 tv {
262 1.1 tv size_t errlen;
263 1.1 tv char *errbuf;
264 1.1 tv
265 1.1 tv errlen = regerror(er, re, NULL, 0);
266 1.1 tv errbuf = xalloc(errlen);
267 1.1 tv regerror(er, re, errbuf, errlen);
268 1.1 tv errx(1, "regular expression error: %s", errbuf);
269 1.1 tv }
270 1.1 tv
271 1.1 tv static void
272 1.1 tv add_sub(n, string, re, pm)
273 1.1 tv int n;
274 1.1 tv const char *string;
275 1.1 tv regex_t *re;
276 1.1 tv regmatch_t *pm;
277 1.1 tv {
278 1.1 tv if (n > re->re_nsub)
279 1.1 tv warnx("No subexpression %d", n);
280 1.1 tv /* Subexpressions that did not match are
281 1.1 tv * not an error. */
282 1.1 tv else if (pm[n].rm_so != -1 &&
283 1.1 tv pm[n].rm_eo != -1) {
284 1.1 tv addchars(string + pm[n].rm_so,
285 1.1 tv pm[n].rm_eo - pm[n].rm_so);
286 1.1 tv }
287 1.1 tv }
288 1.1 tv
289 1.1 tv /* Add replacement string to the output buffer, recognizing special
290 1.1 tv * constructs and replacing them with substrings of the original string.
291 1.1 tv */
292 1.1 tv static void
293 1.1 tv add_replace(string, re, replace, pm)
294 1.1 tv const char *string;
295 1.1 tv regex_t *re;
296 1.1 tv const char *replace;
297 1.1 tv regmatch_t *pm;
298 1.1 tv {
299 1.1 tv const char *p;
300 1.1 tv
301 1.1 tv for (p = replace; *p != '\0'; p++) {
302 1.1 tv if (*p == '&' && !mimic_gnu) {
303 1.1 tv add_sub(0, string, re, pm);
304 1.1 tv continue;
305 1.1 tv }
306 1.1 tv if (*p == '\\') {
307 1.1 tv if (p[1] == '\\') {
308 1.1 tv addchar(p[1]);
309 1.1 tv p++;
310 1.1 tv continue;
311 1.1 tv }
312 1.1 tv if (p[1] == '&') {
313 1.1 tv if (mimic_gnu)
314 1.1 tv add_sub(0, string, re, pm);
315 1.1 tv else
316 1.1 tv addchar(p[1]);
317 1.1 tv p++;
318 1.1 tv continue;
319 1.1 tv }
320 1.1 tv if (isdigit(p[1])) {
321 1.1 tv add_sub(*(++p) - '0', string, re, pm);
322 1.1 tv continue;
323 1.1 tv }
324 1.1 tv }
325 1.1 tv addchar(*p);
326 1.1 tv }
327 1.1 tv }
328 1.1 tv
329 1.1 tv static void
330 1.1 tv do_subst(string, re, replace, pm)
331 1.1 tv const char *string;
332 1.1 tv regex_t *re;
333 1.1 tv const char *replace;
334 1.1 tv regmatch_t *pm;
335 1.1 tv {
336 1.1 tv int error;
337 1.1 tv int flags = 0;
338 1.1 tv const char *last_match = NULL;
339 1.1 tv
340 1.1 tv while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
341 1.1 tv if (pm[0].rm_eo != 0) {
342 1.1 tv if (string[pm[0].rm_eo-1] == '\n')
343 1.1 tv flags = 0;
344 1.1 tv else
345 1.1 tv flags = REG_NOTBOL;
346 1.1 tv }
347 1.1 tv
348 1.1 tv /* NULL length matches are special... We use the `vi-mode'
349 1.1 tv * rule: don't allow a NULL-match at the last match
350 1.1 tv * position.
351 1.1 tv */
352 1.1 tv if (pm[0].rm_so == pm[0].rm_eo &&
353 1.1 tv string + pm[0].rm_so == last_match) {
354 1.1 tv if (*string == '\0')
355 1.1 tv return;
356 1.1 tv addchar(*string);
357 1.1 tv if (*string++ == '\n')
358 1.1 tv flags = 0;
359 1.1 tv else
360 1.1 tv flags = REG_NOTBOL;
361 1.1 tv continue;
362 1.1 tv }
363 1.1 tv last_match = string + pm[0].rm_so;
364 1.1 tv addchars(string, pm[0].rm_so);
365 1.1 tv add_replace(string, re, replace, pm);
366 1.1 tv string += pm[0].rm_eo;
367 1.1 tv }
368 1.1 tv if (error != REG_NOMATCH)
369 1.1 tv exit_regerror(error, re);
370 1.1 tv pbstr(string);
371 1.1 tv }
372 1.1 tv
373 1.1 tv static void
374 1.1 tv do_regexp(string, re, replace, pm)
375 1.1 tv const char *string;
376 1.1 tv regex_t *re;
377 1.1 tv const char *replace;
378 1.1 tv regmatch_t *pm;
379 1.1 tv {
380 1.1 tv int error;
381 1.1 tv
382 1.1 tv switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
383 1.1 tv case 0:
384 1.1 tv add_replace(string, re, replace, pm);
385 1.1 tv pbstr(getstring());
386 1.1 tv break;
387 1.1 tv case REG_NOMATCH:
388 1.1 tv break;
389 1.1 tv default:
390 1.1 tv exit_regerror(error, re);
391 1.1 tv }
392 1.1 tv }
393 1.1 tv
394 1.1 tv static void
395 1.1 tv do_regexpindex(string, re, pm)
396 1.1 tv const char *string;
397 1.1 tv regex_t *re;
398 1.1 tv regmatch_t *pm;
399 1.1 tv {
400 1.1 tv int error;
401 1.1 tv
402 1.1 tv switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
403 1.1 tv case 0:
404 1.1 tv pbunsigned(pm[0].rm_so);
405 1.1 tv break;
406 1.1 tv case REG_NOMATCH:
407 1.1 tv pbnum(-1);
408 1.1 tv break;
409 1.1 tv default:
410 1.1 tv exit_regerror(error, re);
411 1.1 tv }
412 1.1 tv }
413 1.1 tv
414 1.1 tv /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
415 1.1 tv * says. So we twiddle with the regexp before passing it to regcomp.
416 1.1 tv */
417 1.1 tv static char *
418 1.1 tv twiddle(p)
419 1.1 tv const char *p;
420 1.1 tv {
421 1.1 tv /* This could use strcspn for speed... */
422 1.1 tv while (*p != '\0') {
423 1.1 tv if (*p == '\\') {
424 1.1 tv switch(p[1]) {
425 1.1 tv case '(':
426 1.1 tv case ')':
427 1.1 tv case '|':
428 1.1 tv addchar(p[1]);
429 1.1 tv break;
430 1.1 tv case 'w':
431 1.1 tv addconstantstring("[_a-zA-Z0-9]");
432 1.1 tv break;
433 1.1 tv case 'W':
434 1.1 tv addconstantstring("[^_a-zA-Z0-9]");
435 1.1 tv break;
436 1.1 tv case '<':
437 1.1 tv addconstantstring("[[:<:]]");
438 1.1 tv break;
439 1.1 tv case '>':
440 1.1 tv addconstantstring("[[:>:]]");
441 1.1 tv break;
442 1.1 tv default:
443 1.1 tv addchars(p, 2);
444 1.1 tv break;
445 1.1 tv }
446 1.1 tv p+=2;
447 1.1 tv continue;
448 1.1 tv }
449 1.1 tv if (*p == '(' || *p == ')' || *p == '|')
450 1.1 tv addchar('\\');
451 1.1 tv
452 1.1 tv addchar(*p);
453 1.1 tv p++;
454 1.1 tv }
455 1.1 tv return getstring();
456 1.1 tv }
457 1.1 tv
458 1.1 tv /* patsubst(string, regexp, opt replacement) */
459 1.1 tv /* argv[2]: string
460 1.1 tv * argv[3]: regexp
461 1.1 tv * argv[4]: opt rep
462 1.1 tv */
463 1.1 tv void
464 1.1 tv dopatsubst(argv, argc)
465 1.1 tv const char *argv[];
466 1.1 tv int argc;
467 1.1 tv {
468 1.1 tv int error;
469 1.1 tv regex_t re;
470 1.1 tv regmatch_t *pmatch;
471 1.1 tv
472 1.1 tv if (argc <= 3) {
473 1.1 tv warnx("Too few arguments to patsubst");
474 1.1 tv return;
475 1.1 tv }
476 1.1 tv error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
477 1.1 tv REG_NEWLINE | REG_EXTENDED);
478 1.1 tv if (error != 0)
479 1.1 tv exit_regerror(error, &re);
480 1.1 tv
481 1.1 tv pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
482 1.1 tv do_subst(argv[2], &re,
483 1.1 tv argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
484 1.1 tv pbstr(getstring());
485 1.1 tv free(pmatch);
486 1.1 tv regfree(&re);
487 1.1 tv }
488 1.1 tv
489 1.1 tv void
490 1.1 tv doregexp(argv, argc)
491 1.1 tv const char *argv[];
492 1.1 tv int argc;
493 1.1 tv {
494 1.1 tv int error;
495 1.1 tv regex_t re;
496 1.1 tv regmatch_t *pmatch;
497 1.1 tv
498 1.1 tv if (argc <= 3) {
499 1.1 tv warnx("Too few arguments to regexp");
500 1.1 tv return;
501 1.1 tv }
502 1.1 tv error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
503 1.1 tv REG_EXTENDED);
504 1.1 tv if (error != 0)
505 1.1 tv exit_regerror(error, &re);
506 1.1 tv
507 1.1 tv pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
508 1.1 tv if (argv[4] == NULL || argc == 4)
509 1.1 tv do_regexpindex(argv[2], &re, pmatch);
510 1.1 tv else
511 1.1 tv do_regexp(argv[2], &re, argv[4], pmatch);
512 1.1 tv free(pmatch);
513 1.1 tv regfree(&re);
514 1.1 tv }
515 1.1 tv
516 1.1 tv void
517 1.1 tv doesyscmd(cmd)
518 1.1 tv const char *cmd;
519 1.1 tv {
520 1.1 tv int p[2];
521 1.1 tv pid_t pid, cpid;
522 1.1 tv char *argv[4];
523 1.1 tv int cc;
524 1.1 tv int status;
525 1.1 tv
526 1.1 tv /* Follow gnu m4 documentation: first flush buffers. */
527 1.1 tv fflush(NULL);
528 1.1 tv
529 1.1 tv argv[0] = "sh";
530 1.1 tv argv[1] = "-c";
531 1.1 tv argv[2] = (char *)cmd;
532 1.1 tv argv[3] = NULL;
533 1.1 tv
534 1.1 tv /* Just set up standard output, share stderr and stdin with m4 */
535 1.1 tv if (pipe(p) == -1)
536 1.1 tv err(1, "bad pipe");
537 1.1 tv switch(cpid = fork()) {
538 1.1 tv case -1:
539 1.1 tv err(1, "bad fork");
540 1.1 tv /* NOTREACHED */
541 1.1 tv case 0:
542 1.1 tv (void) close(p[0]);
543 1.1 tv (void) dup2(p[1], 1);
544 1.1 tv (void) close(p[1]);
545 1.1 tv execv(_PATH_BSHELL, argv);
546 1.1 tv exit(1);
547 1.1 tv default:
548 1.1 tv /* Read result in two stages, since m4's buffer is
549 1.1 tv * pushback-only. */
550 1.1 tv (void) close(p[1]);
551 1.1 tv do {
552 1.1 tv char result[BUFSIZE];
553 1.1 tv cc = read(p[0], result, sizeof result);
554 1.1 tv if (cc > 0)
555 1.1 tv addchars(result, cc);
556 1.1 tv } while (cc > 0 || (cc == -1 && errno == EINTR));
557 1.1 tv
558 1.1 tv (void) close(p[0]);
559 1.1 tv while ((pid = wait(&status)) != cpid && pid >= 0)
560 1.1 tv continue;
561 1.1 tv pbstr(getstring());
562 1.1 tv }
563 1.1 tv }
564