1 /* $OpenBSD: main.c,v 1.87 2017/06/15 13:48:42 bcallah Exp $ */
2 /* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
3
4 /*-
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Copyright (c) 1989, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Ozan Yigit at York University.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 /*
39 * main.c
40 * Facility: m4 macro processor
41 * by: oz
42 */
43 #include <sys/cdefs.h>
44 #include <assert.h>
45 #include <signal.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <getopt.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <ctype.h>
52 #include <string.h>
53 #include <stddef.h>
54 #include <stdint.h>
55 #include <stdlib.h>
56 #include <ohash.h>
57 #include "mdef.h"
58 #include "stdd.h"
59 #include "extern.h"
60 #include "pathnames.h"
61
62 static const char *shortopts = "+D:d::EGgI:o:Pst:U:";
63 static const struct option longopts[] = {
64 { "define", required_argument, NULL, 'D' },
65 { "debug", optional_argument, NULL, 'd' },
66 { "fatal-warnings", no_argument, NULL, 'E' },
67 { "traditional", no_argument, NULL, 'G' },
68 { "gnu", no_argument, NULL, 'g' },
69 { "include", required_argument, NULL, 'I' },
70 { "error-output", required_argument, NULL, 'o' },
71 { "prefix-builtins", no_argument, NULL, 'P' },
72 { "synclines", no_argument, NULL, 's' },
73 { "trace", required_argument, NULL, 't' },
74 { "undefine", required_argument, NULL, 'U' },
75 { NULL, 0, NULL, 0 },
76 };
77
78 stae *mstack; /* stack of m4 machine */
79 char *sstack; /* shadow stack, for string space extension */
80 static size_t STACKMAX; /* current maximum size of stack */
81 int sp; /* current m4 stack pointer */
82 int fp; /* m4 call frame pointer */
83 struct input_file infile[MAXINP];/* input file stack (0=stdin) */
84 FILE **outfile; /* diversion array(0=bitbucket)*/
85 int maxout;
86 FILE *active; /* active output file pointer */
87 int ilevel = 0; /* input file stack pointer */
88 int oindex = 0; /* diversion index.. */
89 const char *null = ""; /* as it says.. just a null.. */
90 char **m4wraps = NULL; /* m4wraps array. */
91 int maxwraps = 0; /* size of m4wraps array */
92 int wrapindex = 0; /* current offset in m4wraps */
93 char lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */
94 char rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */
95 char scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */
96 char ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */
97 int synch_lines = 0; /* line synchronisation for C preprocessor */
98 int prefix_builtins = 0; /* -P option to prefix builtin keywords */
99 int error_warns = 0; /* -E option to make warnings exit_code = 1 */
100 int fatal_warns = 0; /* -E -E option to make warnings fatal */
101
102 struct keyblk {
103 const char *knam; /* keyword name */
104 int ktyp; /* keyword type */
105 };
106
107 static struct keyblk keywrds[] = { /* m4 keywords to be installed */
108 { "include", INCLUDETYPE },
109 { "sinclude", SINCLUDETYPE },
110 { "define", DEFINETYPE },
111 { "defn", DEFNTYPE },
112 { "divert", DIVERTTYPE | NOARGS },
113 { "eval", EVALTYPE },
114 { "expr", EVALTYPE },
115 { "substr", SUBSTRTYPE },
116 { "ifelse", IFELSETYPE },
117 { "ifdef", IFDEFTYPE },
118 { "len", LENTYPE },
119 { "incr", INCRTYPE },
120 { "decr", DECRTYPE },
121 { "dnl", DNLTYPE | NOARGS },
122 { "changequote", CHANGEQUOTETYPE | NOARGS },
123 { "changecom", CHANGECOMTYPE | NOARGS },
124 { "index", INDEXTYPE },
125 #ifdef EXTENDED
126 { "paste", PASTETYPE },
127 { "spaste", SPASTETYPE },
128 /* Newer extensions, needed to handle gnu-m4 scripts */
129 { "indir", INDIRTYPE},
130 { "builtin", BUILTINTYPE},
131 { "patsubst", PATSUBSTTYPE},
132 { "regexp", REGEXPTYPE},
133 { "esyscmd", ESYSCMDTYPE},
134 { "__file__", FILENAMETYPE | NOARGS},
135 { "__line__", LINETYPE | NOARGS},
136 #endif
137 { "popdef", POPDEFTYPE },
138 { "pushdef", PUSHDEFTYPE },
139 { "dumpdef", DUMPDEFTYPE | NOARGS },
140 { "shift", SHIFTTYPE | NOARGS },
141 { "translit", TRANSLITTYPE },
142 { "undefine", UNDEFINETYPE },
143 { "undivert", UNDIVERTTYPE | NOARGS },
144 { "divnum", DIVNUMTYPE | NOARGS },
145 { "maketemp", MKSTEMPTYPE },
146 { "mkstemp", MKSTEMPTYPE },
147 { "errprint", ERRPRINTTYPE | NOARGS },
148 { "m4wrap", M4WRAPTYPE | NOARGS },
149 { "m4exit", M4EXITTYPE | NOARGS },
150 { "syscmd", SYSCMDTYPE },
151 { "sysval", SYSVALTYPE | NOARGS },
152 { "traceon", TRACEONTYPE | NOARGS },
153 { "traceoff", TRACEOFFTYPE | NOARGS },
154
155 /* Macro that expands to itself, signature of the current OS */
156 { "unix", SELFTYPE | NOARGS },
157 };
158
159 #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
160
161 extern int optind;
162 extern char *optarg;
163
164 #define MAXRECORD 50
165 static struct position {
166 char *name;
167 unsigned long line;
168 } quotes[MAXRECORD], paren[MAXRECORD];
169
170 static void record(struct position *, int);
171 static void dump_stack(struct position *, int);
172
173 static void macro(void);
174 static void initkwds(void);
175 static ndptr inspect(int, char *);
176 static int do_look_ahead(int, const char *);
177 static void reallyoutputstr(const char *);
178 static void reallyputchar(int);
179
180 static void enlarge_stack(void);
181
182 int main(int, char *[]);
183
184 int exit_code = 0;
185
186 int
main(int argc,char * argv[])187 main(int argc, char *argv[])
188 {
189 int c;
190 int n;
191 char *p;
192
193 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
194 signal(SIGINT, onintr);
195
196 init_macros();
197 initspaces();
198 STACKMAX = INITSTACKMAX;
199
200 mstack = xreallocarray(NULL, STACKMAX, sizeof(stae), NULL);
201 sstack = xalloc(STACKMAX, NULL);
202
203 maxout = 0;
204 outfile = NULL;
205 resizedivs(MAXOUT);
206
207 while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
208 switch(c) {
209
210 case 'D': /* define something..*/
211 for (p = optarg; *p; p++)
212 if (*p == '=')
213 break;
214 if (*p)
215 *p++ = EOS;
216 dodefine(optarg, p);
217 break;
218 case 'E': /* like GNU m4 1.4.9+ */
219 if (error_warns == 0)
220 error_warns = 1;
221 else
222 fatal_warns = 1;
223 break;
224 case 'I':
225 addtoincludepath(optarg);
226 break;
227 case 'P':
228 prefix_builtins = 1;
229 break;
230 case 'U': /* undefine... */
231 macro_popdef(optarg);
232 break;
233 case 'G':
234 mimic_gnu = 0;
235 break;
236 case 'g':
237 mimic_gnu = 1;
238 break;
239 case 'd':
240 set_trace_flags(optarg ? optarg : "aeq");
241 break;
242 case 's':
243 synch_lines = 1;
244 break;
245 case 't':
246 mark_traced(optarg, 1);
247 break;
248 case 'o':
249 trace_file(optarg);
250 break;
251 case '?':
252 usage();
253 }
254
255 argc -= optind;
256 argv += optind;
257
258 initkwds();
259 if (mimic_gnu)
260 setup_builtin("format", FORMATTYPE);
261
262 active = stdout; /* default active output */
263 bbase[0] = bufbase;
264 if (!argc) {
265 sp = -1; /* stack pointer initialized */
266 fp = 0; /* frame pointer initialized */
267 set_input(infile+0, stdin, "stdin");
268 /* default input (naturally) */
269 macro();
270 } else
271 for (; argc--; ++argv) {
272 p = *argv;
273 if (p[0] == '-' && p[1] == EOS)
274 set_input(infile, stdin, "stdin");
275 else if (fopen_trypath(infile, p) == NULL)
276 err(1, "%s", p);
277 sp = -1;
278 fp = 0;
279 macro();
280 release_input(infile);
281 }
282
283 if (wrapindex) {
284 int i;
285
286 ilevel = 0; /* in case m4wrap includes.. */
287 bufbase = bp = buf; /* use the entire buffer */
288 if (mimic_gnu) {
289 while (wrapindex != 0) {
290 for (i = 0; i < wrapindex; i++)
291 pbstr(m4wraps[i]);
292 wrapindex =0;
293 macro();
294 }
295 } else {
296 for (i = 0; i < wrapindex; i++) {
297 pbstr(m4wraps[i]);
298 macro();
299 }
300 }
301 }
302
303 if (active != stdout)
304 active = stdout; /* reset output just in case */
305 for (n = 1; n < maxout; n++) /* default wrap-up: undivert */
306 if (outfile[n] != NULL)
307 getdiv(n);
308 /* remove bitbucket if used */
309 if (outfile[0] != NULL) {
310 (void) fclose(outfile[0]);
311 }
312
313 return exit_code;
314 }
315
316 /*
317 * Look ahead for `token'.
318 * (on input `t == token[0]')
319 * Used for comment and quoting delimiters.
320 * Returns 1 if `token' present; copied to output.
321 * 0 if `token' not found; all characters pushed back
322 */
323 static int
do_look_ahead(int t,const char * token)324 do_look_ahead(int t, const char *token)
325 {
326 int i;
327
328 assert((unsigned char)t == (unsigned char)token[0]);
329
330 for (i = 1; *++token; i++) {
331 t = gpbc();
332 if (t == EOF || (unsigned char)t != (unsigned char)*token) {
333 pushback(t);
334 while (--i)
335 pushback(*--token);
336 return 0;
337 }
338 }
339 return 1;
340 }
341
342 #define LOOK_AHEAD(t, token) (t != EOF && \
343 (unsigned char)(t)==(unsigned char)(token)[0] && \
344 do_look_ahead(t,token))
345
346 /*
347 * macro - the work horse..
348 */
349 static void
macro(void)350 macro(void)
351 {
352 char token[MAXTOK+1];
353 int t, l;
354 ndptr p;
355 int nlpar;
356
357 cycle {
358 t = gpbc();
359
360 if (LOOK_AHEAD(t,lquote)) { /* strip quotes */
361 nlpar = 0;
362 record(quotes, nlpar++);
363 /*
364 * Opening quote: scan forward until matching
365 * closing quote has been found.
366 */
367 do {
368
369 l = gpbc();
370 if (LOOK_AHEAD(l,rquote)) {
371 if (--nlpar > 0)
372 outputstr(rquote);
373 } else if (LOOK_AHEAD(l,lquote)) {
374 record(quotes, nlpar++);
375 outputstr(lquote);
376 } else if (l == EOF) {
377 if (nlpar == 1)
378 warnx("unclosed quote:");
379 else
380 warnx("%d unclosed quotes:", nlpar);
381 dump_stack(quotes, nlpar);
382 exit(1);
383 } else {
384 if (nlpar > 0) {
385 if (sp < 0)
386 reallyputchar(l);
387 else
388 CHRSAVE(l);
389 }
390 }
391 } while (nlpar != 0);
392 } else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
393 reallyoutputstr(scommt);
394
395 for(;;) {
396 t = gpbc();
397 if (LOOK_AHEAD(t, ecommt)) {
398 reallyoutputstr(ecommt);
399 break;
400 }
401 if (t == EOF)
402 break;
403 reallyputchar(t);
404 }
405 } else if (t == '_' || isalpha(t)) {
406 p = inspect(t, token);
407 if (p != NULL)
408 pushback(l = gpbc());
409 if (p == NULL || (l != LPAREN &&
410 (macro_getdef(p)->type & NEEDARGS) != 0))
411 outputstr(token);
412 else {
413 /*
414 * real thing.. First build a call frame:
415 */
416 pushf(fp); /* previous call frm */
417 pushf(macro_getdef(p)->type); /* type of the call */
418 pushf(is_traced(p));
419 pushf(0); /* parenthesis level */
420 fp = sp; /* new frame pointer */
421 /*
422 * now push the string arguments:
423 */
424 pushdef(p); /* defn string */
425 pushs1((char *)macro_name(p)); /* macro name */
426 pushs(ep); /* start next..*/
427
428 if (l != LPAREN && PARLEV == 0) {
429 /* no bracks */
430 chrsave(EOS);
431
432 if (sp == (int)STACKMAX)
433 errx(1, "internal stack overflow");
434 eval((const char **) mstack+fp+1, 2,
435 CALTYP, TRACESTATUS);
436
437 ep = PREVEP; /* flush strspace */
438 sp = PREVSP; /* previous sp.. */
439 fp = PREVFP; /* rewind stack...*/
440 }
441 }
442 } else if (t == EOF) {
443 if (!mimic_gnu /* you can puke right there */
444 && sp > -1 && ilevel <= 0) {
445 warnx( "unexpected end of input, unclosed parenthesis:");
446 dump_stack(paren, PARLEV);
447 exit(1);
448 }
449 if (ilevel <= 0)
450 break; /* all done thanks.. */
451 release_input(infile+ilevel--);
452 emit_synchline();
453 bufbase = bbase[ilevel];
454 continue;
455 } else if (sp < 0) { /* not in a macro at all */
456 reallyputchar(t); /* output directly.. */
457 }
458
459 else switch(t) {
460
461 case LPAREN:
462 if (PARLEV > 0)
463 chrsave(t);
464 while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
465 if (PARLEV > 0)
466 chrsave(l);
467 pushback(l);
468 record(paren, PARLEV++);
469 break;
470
471 case RPAREN:
472 if (--PARLEV > 0)
473 chrsave(t);
474 else { /* end of argument list */
475 chrsave(EOS);
476
477 if (sp == (int)STACKMAX)
478 errx(1, "internal stack overflow");
479
480 eval((const char **) mstack+fp+1, sp-fp,
481 CALTYP, TRACESTATUS);
482
483 ep = PREVEP; /* flush strspace */
484 sp = PREVSP; /* previous sp.. */
485 fp = PREVFP; /* rewind stack...*/
486 }
487 break;
488
489 case COMMA:
490 if (PARLEV == 1) {
491 chrsave(EOS); /* new argument */
492 while (isspace(l = gpbc()))
493 ;
494 pushback(l);
495 pushs(ep);
496 } else
497 chrsave(t);
498 break;
499
500 default:
501 if (LOOK_AHEAD(t, scommt)) {
502 char *p;
503 for (p = scommt; *p; p++)
504 chrsave(*p);
505 for(;;) {
506 t = gpbc();
507 if (LOOK_AHEAD(t, ecommt)) {
508 for (p = ecommt; *p; p++)
509 chrsave(*p);
510 break;
511 }
512 if (t == EOF)
513 break;
514 CHRSAVE(t);
515 }
516 } else
517 CHRSAVE(t); /* stack the char */
518 break;
519 }
520 }
521 }
522
523 /*
524 * output string directly, without pushing it for reparses.
525 */
526 void
outputstr(const char * s)527 outputstr(const char *s)
528 {
529 if (sp < 0)
530 reallyoutputstr(s);
531 else
532 while (*s)
533 CHRSAVE(*s++);
534 }
535
536 void
reallyoutputstr(const char * s)537 reallyoutputstr(const char *s)
538 {
539 if (synch_lines) {
540 while (*s) {
541 fputc(*s, active);
542 if (*s++ == '\n') {
543 infile[ilevel].synch_lineno++;
544 if (infile[ilevel].synch_lineno !=
545 infile[ilevel].lineno)
546 do_emit_synchline();
547 }
548 }
549 } else
550 fputs(s, active);
551 }
552
553 void
reallyputchar(int c)554 reallyputchar(int c)
555 {
556 putc(c, active);
557 if (synch_lines && c == '\n') {
558 infile[ilevel].synch_lineno++;
559 if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
560 do_emit_synchline();
561 }
562 }
563
564 /*
565 * build an input token..
566 * consider only those starting with _ or A-Za-z.
567 */
568 static ndptr
inspect(int c,char * tp)569 inspect(int c, char *tp)
570 {
571 char *name = tp;
572 char *etp = tp+MAXTOK;
573 ndptr p;
574
575 *tp++ = c;
576
577 while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
578 *tp++ = c;
579 if (c != EOF)
580 PUSHBACK(c);
581 *tp = EOS;
582 /* token is too long, it won't match anything, but it can still
583 * be output. */
584 if (tp == ep) {
585 outputstr(name);
586 while (isalnum(c = gpbc()) || c == '_') {
587 if (sp < 0)
588 reallyputchar(c);
589 else
590 CHRSAVE(c);
591 }
592 *name = EOS;
593 return NULL;
594 }
595
596 p = ohash_find(¯os, ohash_qlookupi(¯os, name, (const char **)&tp));
597 if (p == NULL)
598 return NULL;
599 if (macro_getdef(p) == NULL)
600 return NULL;
601 return p;
602 }
603
604 /*
605 * initkwds - initialise m4 keywords as fast as possible.
606 * This very similar to install, but without certain overheads,
607 * such as calling lookup. Malloc is not used for storing the
608 * keyword strings, since we simply use the static pointers
609 * within keywrds block.
610 */
611 static void
initkwds(void)612 initkwds(void)
613 {
614 unsigned int type;
615 int i;
616
617 for (i = 0; i < (int)MAXKEYS; i++) {
618 type = keywrds[i].ktyp & TYPEMASK;
619 if ((keywrds[i].ktyp & NOARGS) == 0)
620 type |= NEEDARGS;
621 setup_builtin(keywrds[i].knam, type);
622 }
623 }
624
625 static void
record(struct position * t,int lev)626 record(struct position *t, int lev)
627 {
628 if (lev < MAXRECORD) {
629 t[lev].name = CURRENT_NAME;
630 t[lev].line = CURRENT_LINE;
631 }
632 }
633
634 static void
dump_stack(struct position * t,int lev)635 dump_stack(struct position *t, int lev)
636 {
637 int i;
638
639 for (i = 0; i < lev; i++) {
640 if (i == MAXRECORD) {
641 fprintf(stderr, " ...\n");
642 break;
643 }
644 fprintf(stderr, " %s at line %lu\n",
645 t[i].name, t[i].line);
646 }
647 }
648
649
650 static void
enlarge_stack(void)651 enlarge_stack(void)
652 {
653 STACKMAX += STACKMAX/2;
654 mstack = xreallocarray(mstack, STACKMAX, sizeof(stae),
655 "Evaluation stack overflow (%lu)",
656 (unsigned long)STACKMAX);
657 sstack = xrealloc(sstack, STACKMAX,
658 "Evaluation stack overflow (%lu)",
659 (unsigned long)STACKMAX);
660 }
661