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