tools/llvm: Do not build with symbols
[minix3.git] / usr.bin / m4 / main.c
blobba76b858427edd7ba23b8b93fa25ee546d836f73
1 /* $OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen Exp $ */
2 /* $NetBSD: main.c,v 1.42 2012/04/25 18:23:58 christos Exp $ */
4 /*-
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
37 * main.c
38 * Facility: m4 macro processor
39 * by: oz
41 #if HAVE_NBTOOL_CONFIG_H
42 #include "nbtool_config.h"
43 #endif
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: main.c,v 1.42 2012/04/25 18:23:58 christos Exp $");
46 #include <assert.h>
47 #include <signal.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <string.h>
54 #include <stddef.h>
55 #include <stdint.h>
56 #include <stdlib.h>
57 #include <ohash.h>
58 #include "mdef.h"
59 #include "stdd.h"
60 #include "extern.h"
61 #include "pathnames.h"
63 ndptr hashtab[HASHSIZE]; /* hash table for macros etc. */
64 stae *mstack; /* stack of m4 machine */
65 char *sstack; /* shadow stack, for string space extension */
66 static size_t STACKMAX; /* current maximum size of stack */
67 int sp; /* current m4 stack pointer */
68 int fp; /* m4 call frame pointer */
69 struct input_file infile[MAXINP];/* input file stack (0=stdin) */
70 FILE **outfile; /* diversion array(0=bitbucket)*/
71 int maxout;
72 FILE *active; /* active output file pointer */
73 int ilevel = 0; /* input file stack pointer */
74 int oindex = 0; /* diversion index.. */
75 const char *null = ""; /* as it says.. just a null.. */
76 char **m4wraps = NULL; /* m4wraps array. */
77 int maxwraps = 0; /* size of m4wraps array */
78 int wrapindex = 0; /* current offset in m4wraps */
79 char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
80 char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
81 char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
82 char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
83 int synch_lines = 0; /* line synchronisation for C preprocessor */
84 int prefix_builtins = 0; /* -P option to prefix builtin keywords */
86 struct keyblk {
87 const char *knam; /* keyword name */
88 int ktyp; /* keyword type */
91 struct keyblk keywrds[] = { /* m4 keywords to be installed */
92 { "include", INCLTYPE },
93 { "sinclude", SINCTYPE },
94 { "define", DEFITYPE },
95 { "defn", DEFNTYPE },
96 { "divert", DIVRTYPE | NOARGS },
97 { "expr", EXPRTYPE },
98 { "eval", EXPRTYPE },
99 { "substr", SUBSTYPE },
100 { "ifelse", IFELTYPE },
101 { "ifdef", IFDFTYPE },
102 { "len", LENGTYPE },
103 { "incr", INCRTYPE },
104 { "decr", DECRTYPE },
105 { "dnl", DNLNTYPE | NOARGS },
106 { "changequote", CHNQTYPE | NOARGS },
107 { "changecom", CHNCTYPE | NOARGS },
108 { "index", INDXTYPE },
109 #ifdef EXTENDED
110 { "paste", PASTTYPE },
111 { "spaste", SPASTYPE },
112 /* Newer extensions, needed to handle gnu-m4 scripts */
113 { "indir", INDIRTYPE},
114 { "builtin", BUILTINTYPE},
115 { "patsubst", PATSTYPE},
116 { "regexp", REGEXPTYPE},
117 { "esyscmd", ESYSCMDTYPE},
118 { "__file__", FILENAMETYPE | NOARGS},
119 { "__line__", LINETYPE | NOARGS},
120 #endif
121 { "popdef", POPDTYPE },
122 { "pushdef", PUSDTYPE },
123 { "dumpdef", DUMPTYPE | NOARGS },
124 { "shift", SHIFTYPE | NOARGS },
125 { "translit", TRNLTYPE },
126 { "undefine", UNDFTYPE },
127 { "undivert", UNDVTYPE | NOARGS },
128 { "divnum", DIVNTYPE | NOARGS },
129 { "maketemp", MKTMTYPE },
130 { "errprint", ERRPTYPE | NOARGS },
131 { "m4wrap", M4WRTYPE | NOARGS },
132 { "m4exit", EXITTYPE | NOARGS },
133 { "syscmd", SYSCTYPE },
134 { "sysval", SYSVTYPE | NOARGS },
135 { "traceon", TRACEONTYPE | NOARGS },
136 { "traceoff", TRACEOFFTYPE | NOARGS },
138 #if defined(unix) || defined(__unix__)
139 { "unix", SELFTYPE | NOARGS },
140 #else
141 #ifdef vms
142 { "vms", SELFTYPE | NOARGS },
143 #endif
144 #endif
147 #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
149 extern int optind;
150 extern char *optarg;
152 #define MAXRECORD 50
153 static struct position {
154 char *name;
155 unsigned long line;
156 } quotes[MAXRECORD], paren[MAXRECORD];
158 static void record(struct position *, int);
159 static void dump_stack(struct position *, int);
161 static void macro(void);
162 static void initkwds(void);
163 static ndptr inspect(int, char *);
164 static int do_look_ahead(int, const char *);
165 static void reallyoutputstr(const char *);
166 static void reallyputchar(int);
168 static void enlarge_stack(void);
170 __dead static void
171 usage(void)
173 fprintf(stderr, "usage: %s [-gPs] [-Dname[=value]] [-d flags] "
174 "[-I dirname] [-o filename]\n"
175 "\t[-t macro] [-Uname] [file ...]\n", getprogname());
176 exit(1);
179 __dead static void
180 onintr(int signo)
182 char intrmessage[] = "m4: interrupted.\n";
183 write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
184 _exit(1);
188 main(int argc, char *argv[])
190 int c;
191 int n;
192 char *p;
194 setprogname(argv[0]);
196 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
197 signal(SIGINT, onintr);
199 init_macros();
200 initspaces();
201 STACKMAX = INITSTACKMAX;
203 mstack = (stae *)xalloc(sizeof(stae) * STACKMAX, NULL);
204 sstack = (char *)xalloc(STACKMAX, NULL);
206 maxout = 0;
207 outfile = NULL;
208 resizedivs(MAXOUT);
210 while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1)
211 switch(c) {
213 case 'D': /* define something..*/
214 for (p = optarg; *p; p++)
215 if (*p == '=')
216 break;
217 if (*p)
218 *p++ = EOS;
219 dodefine(optarg, p);
220 break;
221 case 'I':
222 addtoincludepath(optarg);
223 break;
224 case 'P':
225 prefix_builtins = 1;
226 break;
227 case 'U': /* undefine... */
228 macro_popdef(optarg);
229 break;
230 case 'g':
231 mimic_gnu = 1;
232 break;
233 case 'd':
234 set_trace_flags(optarg);
235 break;
236 case 's':
237 synch_lines = 1;
238 break;
239 case 't':
240 mark_traced(optarg, 1);
241 break;
242 case 'o':
243 trace_file(optarg);
244 break;
245 case '?':
246 usage();
249 argc -= optind;
250 argv += optind;
252 initkwds();
253 if (mimic_gnu)
254 setup_builtin("format", FORMATTYPE);
256 active = stdout; /* default active output */
257 bbase[0] = bufbase;
258 if (!argc) {
259 sp = -1; /* stack pointer initialized */
260 fp = 0; /* frame pointer initialized */
261 set_input(infile+0, stdin, "stdin");
262 /* default input (naturally) */
263 macro();
264 } else
265 for (; argc--; ++argv) {
266 p = *argv;
267 if (p[0] == '-' && p[1] == EOS)
268 set_input(infile, stdin, "stdin");
269 else if (fopen_trypath(infile, p) == NULL)
270 err(1, "%s", p);
271 sp = -1;
272 fp = 0;
273 macro();
274 release_input(infile);
277 if (wrapindex) {
278 int i;
280 ilevel = 0; /* in case m4wrap includes.. */
281 bufbase = bp = buf; /* use the entire buffer */
282 if (mimic_gnu) {
283 while (wrapindex != 0) {
284 for (i = 0; i < wrapindex; i++)
285 pbstr(m4wraps[i]);
286 wrapindex =0;
287 macro();
289 } else {
290 for (i = 0; i < wrapindex; i++) {
291 pbstr(m4wraps[i]);
292 macro();
297 if (active != stdout)
298 active = stdout; /* reset output just in case */
299 for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
300 if (outfile[n] != NULL)
301 getdiv(n);
302 /* remove bitbucket if used */
303 if (outfile[0] != NULL) {
304 (void) fclose(outfile[0]);
307 return 0;
311 * Look ahead for `token'.
312 * (on input `t == token[0]')
313 * Used for comment and quoting delimiters.
314 * Returns 1 if `token' present; copied to output.
315 * 0 if `token' not found; all characters pushed back
317 static int
318 do_look_ahead(int t, const char *token)
320 int i;
322 assert((unsigned char)t == (unsigned char)token[0]);
324 for (i = 1; *++token; i++) {
325 t = gpbc();
326 if (t == EOF || (unsigned char)t != (unsigned char)*token) {
327 pushback(t);
328 while (--i)
329 pushback(*--token);
330 return 0;
333 return 1;
336 #define LOOK_AHEAD(t, token) (t != EOF && \
337 (unsigned char)(t)==(unsigned char)(token)[0] && \
338 do_look_ahead(t,token))
341 * macro - the work horse..
343 static void
344 macro(void)
346 char token[MAXTOK+1];
347 int t, l;
348 ndptr p;
349 int nlpar;
351 cycle {
352 t = gpbc();
354 if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
355 nlpar = 0;
356 record(quotes, nlpar++);
358 * Opening quote: scan forward until matching
359 * closing quote has been found.
361 do {
363 l = gpbc();
364 if (LOOK_AHEAD(l,rquote)) {
365 if (--nlpar > 0)
366 outputstr(rquote);
367 } else if (LOOK_AHEAD(l,lquote)) {
368 record(quotes, nlpar++);
369 outputstr(lquote);
370 } else if (l == EOF) {
371 if (nlpar == 1)
372 warnx("unclosed quote:");
373 else
374 warnx("%d unclosed quotes:", nlpar);
375 dump_stack(quotes, nlpar);
376 exit(1);
377 } else {
378 if (nlpar > 0) {
379 if (sp < 0)
380 reallyputchar(l);
381 else
382 CHRSAVE(l);
386 while (nlpar != 0);
387 } else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
388 reallyoutputstr(scommt);
390 for(;;) {
391 t = gpbc();
392 if (LOOK_AHEAD(t, ecommt)) {
393 reallyoutputstr(ecommt);
394 break;
396 if (t == EOF)
397 break;
398 reallyputchar(t);
400 } else if (t == '_' || isalpha(t)) {
401 p = inspect(t, token);
402 if (p != NULL)
403 pushback(l = gpbc());
404 if (p == NULL || (l != LPAREN &&
405 (macro_getdef(p)->type & NEEDARGS) != 0))
406 outputstr(token);
407 else {
409 * real thing.. First build a call frame:
411 pushf(fp); /* previous call frm */
412 pushf(macro_getdef(p)->type); /* type of the call */
413 pushf(is_traced(p));
414 pushf(0); /* parenthesis level */
415 fp = sp; /* new frame pointer */
417 * now push the string arguments:
419 pushs1(macro_getdef(p)->defn); /* defn string */
420 pushs1((char *)macro_name(p)); /* macro name */
421 pushs(ep); /* start next..*/
423 if (l != LPAREN && PARLEV == 0) {
424 /* no bracks */
425 chrsave(EOS);
427 if ((size_t)sp == STACKMAX)
428 errx(1, "internal stack overflow");
429 eval((const char **) mstack+fp+1, 2,
430 CALTYP, TRACESTATUS);
432 ep = PREVEP; /* flush strspace */
433 sp = PREVSP; /* previous sp.. */
434 fp = PREVFP; /* rewind stack...*/
437 } else if (t == EOF) {
438 if (sp > -1 && ilevel <= 0) {
439 warnx( "unexpected end of input, unclosed parenthesis:");
440 dump_stack(paren, PARLEV);
441 exit(1);
443 if (ilevel <= 0)
444 break; /* all done thanks.. */
445 release_input(infile+ilevel--);
446 emit_synchline();
447 bufbase = bbase[ilevel];
448 continue;
449 } else if (sp < 0) { /* not in a macro at all */
450 reallyputchar(t); /* output directly.. */
453 else switch(t) {
455 case LPAREN:
456 if (PARLEV > 0)
457 chrsave(t);
458 while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
459 if (PARLEV > 0)
460 chrsave(l);
461 pushback(l);
462 record(paren, PARLEV++);
463 break;
465 case RPAREN:
466 if (--PARLEV > 0)
467 chrsave(t);
468 else { /* end of argument list */
469 chrsave(EOS);
471 if ((size_t)sp == STACKMAX)
472 errx(1, "internal stack overflow");
474 eval((const char **) mstack+fp+1, sp-fp,
475 CALTYP, TRACESTATUS);
477 ep = PREVEP; /* flush strspace */
478 sp = PREVSP; /* previous sp.. */
479 fp = PREVFP; /* rewind stack...*/
481 break;
483 case COMMA:
484 if (PARLEV == 1) {
485 chrsave(EOS); /* new argument */
486 while (isspace(l = gpbc()))
488 pushback(l);
489 pushs(ep);
490 } else
491 chrsave(t);
492 break;
494 default:
495 if (LOOK_AHEAD(t, scommt)) {
496 char *q;
497 for (q = scommt; *q; q++)
498 chrsave(*q);
499 for(;;) {
500 t = gpbc();
501 if (LOOK_AHEAD(t, ecommt)) {
502 for (q = ecommt; *q; q++)
503 chrsave(*q);
504 break;
506 if (t == EOF)
507 break;
508 CHRSAVE(t);
510 } else
511 CHRSAVE(t); /* stack the char */
512 break;
518 * output string directly, without pushing it for reparses.
520 void
521 outputstr(const char *s)
523 if (sp < 0)
524 reallyoutputstr(s);
525 else
526 while (*s)
527 CHRSAVE(*s++);
530 void
531 reallyoutputstr(const char *s)
533 if (synch_lines) {
534 while (*s) {
535 fputc(*s, active);
536 if (*s++ == '\n') {
537 infile[ilevel].synch_lineno++;
538 if (infile[ilevel].synch_lineno !=
539 infile[ilevel].lineno)
540 do_emit_synchline();
543 } else
544 fputs(s, active);
547 void
548 reallyputchar(int c)
550 putc(c, active);
551 if (synch_lines && c == '\n') {
552 infile[ilevel].synch_lineno++;
553 if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
554 do_emit_synchline();
559 * build an input token..
560 * consider only those starting with _ or A-Za-z.
562 static ndptr
563 inspect(int c, char *tp)
565 char *name = tp;
566 char *etp = tp+MAXTOK;
567 ndptr p;
569 *tp++ = c;
571 while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
572 *tp++ = c;
573 if (c != EOF)
574 PUSHBACK(c);
575 *tp = EOS;
576 /* token is too long, it won't match anything, but it can still
577 * be output. */
578 if (tp == ep) {
579 outputstr(name);
580 while (isalnum(c = gpbc()) || c == '_') {
581 if (sp < 0)
582 reallyputchar(c);
583 else
584 CHRSAVE(c);
586 *name = EOS;
587 return NULL;
590 p = ohash_find(&macros, ohash_qlookupi(&macros, name, (void *)&tp));
591 if (p == NULL)
592 return NULL;
593 if (macro_getdef(p) == NULL)
594 return NULL;
595 return p;
599 * initkwds - initialise m4 keywords as fast as possible.
600 * This very similar to install, but without certain overheads,
601 * such as calling lookup. Malloc is not used for storing the
602 * keyword strings, since we simply use the static pointers
603 * within keywrds block.
605 static void
606 initkwds(void)
608 unsigned int type;
609 size_t i;
611 for (i = 0; i < MAXKEYS; i++) {
612 type = keywrds[i].ktyp & TYPEMASK;
613 if ((keywrds[i].ktyp & NOARGS) == 0)
614 type |= NEEDARGS;
615 setup_builtin(keywrds[i].knam, type);
619 static void
620 record(struct position *t, int lev)
622 if (lev < MAXRECORD) {
623 t[lev].name = CURRENT_NAME;
624 t[lev].line = CURRENT_LINE;
628 static void
629 dump_stack(struct position *t, int lev)
631 int i;
633 for (i = 0; i < lev; i++) {
634 if (i == MAXRECORD) {
635 fprintf(stderr, " ...\n");
636 break;
638 fprintf(stderr, " %s at line %lu\n",
639 t[i].name, t[i].line);
644 static void
645 enlarge_stack(void)
647 STACKMAX += STACKMAX/2;
648 mstack = xrealloc(mstack, sizeof(stae) * STACKMAX,
649 "Evaluation stack overflow (%lu)",
650 (unsigned long)STACKMAX);
651 sstack = xrealloc(sstack, STACKMAX,
652 "Evaluation stack overflow (%lu)",
653 (unsigned long)STACKMAX);