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