xref: /freebsd/usr.bin/m4/main.c (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /*	$OpenBSD: main.c,v 1.86 2015/11/03 16:21:47 deraadt 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 __FBSDID("$FreeBSD$");
45 
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"
62 
63 stae *mstack;			/* stack of m4 machine         */
64 char *sstack;			/* shadow stack, for string space extension */
65 static size_t STACKMAX;		/* current maximum size of stack */
66 int sp;				/* current m4  stack pointer   */
67 int fp;				/* m4 call frame pointer       */
68 struct input_file infile[MAXINP];/* input file stack (0=stdin)  */
69 FILE **outfile;			/* diversion array(0=bitbucket)*/
70 int maxout;
71 FILE *active;			/* active output file pointer  */
72 int ilevel = 0;			/* input file stack pointer    */
73 int oindex = 0;			/* diversion index..	       */
74 const char *null = "";                /* as it says.. just a null..  */
75 char **m4wraps = NULL;		/* m4wraps array.	       */
76 int maxwraps = 0;		/* size of m4wraps array       */
77 int wrapindex = 0;		/* current offset in m4wraps   */
78 char lquote[MAXCCHARS+1] = {LQUOTE};	/* left quote character  (`)   */
79 char rquote[MAXCCHARS+1] = {RQUOTE};	/* right quote character (')   */
80 char scommt[MAXCCHARS+1] = {SCOMMT};	/* start character for comment */
81 char ecommt[MAXCCHARS+1] = {ECOMMT};	/* end character for comment   */
82 int  synch_lines = 0;		/* line synchronisation for C preprocessor */
83 int  prefix_builtins = 0;	/* -P option to prefix builtin keywords */
84 
85 struct keyblk {
86         const char    *knam;          /* keyword name */
87         int     ktyp;           /* keyword type */
88 };
89 
90 static struct keyblk keywrds[] = {	/* m4 keywords to be installed */
91 	{ "include",      INCLTYPE },
92 	{ "sinclude",     SINCTYPE },
93 	{ "define",       DEFITYPE },
94 	{ "defn",         DEFNTYPE },
95 	{ "divert",       DIVRTYPE | NOARGS },
96 	{ "expr",         EXPRTYPE },
97 	{ "eval",         EXPRTYPE },
98 	{ "substr",       SUBSTYPE },
99 	{ "ifelse",       IFELTYPE },
100 	{ "ifdef",        IFDFTYPE },
101 	{ "len",          LENGTYPE },
102 	{ "incr",         INCRTYPE },
103 	{ "decr",         DECRTYPE },
104 	{ "dnl",          DNLNTYPE | NOARGS },
105 	{ "changequote",  CHNQTYPE | NOARGS },
106 	{ "changecom",    CHNCTYPE | NOARGS },
107 	{ "index",        INDXTYPE },
108 #ifdef EXTENDED
109 	{ "paste",        PASTTYPE },
110 	{ "spaste",       SPASTYPE },
111 	/* Newer extensions, needed to handle gnu-m4 scripts */
112 	{ "indir",        INDIRTYPE},
113 	{ "builtin",      BUILTINTYPE},
114 	{ "patsubst",	  PATSTYPE},
115 	{ "regexp",	  REGEXPTYPE},
116 	{ "esyscmd",	  ESYSCMDTYPE},
117 	{ "__file__",	  FILENAMETYPE | NOARGS},
118 	{ "__line__",	  LINETYPE | NOARGS},
119 #endif
120 	{ "popdef",       POPDTYPE },
121 	{ "pushdef",      PUSDTYPE },
122 	{ "dumpdef",      DUMPTYPE | NOARGS },
123 	{ "shift",        SHIFTYPE | NOARGS },
124 	{ "translit",     TRNLTYPE },
125 	{ "undefine",     UNDFTYPE },
126 	{ "undivert",     UNDVTYPE | NOARGS },
127 	{ "divnum",       DIVNTYPE | NOARGS },
128 	{ "maketemp",     MKTMTYPE },
129 	{ "mkstemp",      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 },
137 
138 	{ "unix",         SELFTYPE | NOARGS },
139 };
140 
141 #define MAXKEYS	(sizeof(keywrds)/sizeof(struct keyblk))
142 
143 extern int optind;
144 extern char *optarg;
145 
146 #define MAXRECORD 50
147 static struct position {
148 	char *name;
149 	unsigned long line;
150 } quotes[MAXRECORD], paren[MAXRECORD];
151 
152 static void record(struct position *, int);
153 static void dump_stack(struct position *, int);
154 
155 static void macro(void);
156 static void initkwds(void);
157 static ndptr inspect(int, char *);
158 static int do_look_ahead(int, const char *);
159 static void reallyoutputstr(const char *);
160 static void reallyputchar(int);
161 
162 static void enlarge_stack(void);
163 
164 int main(int, char *[]);
165 
166 int exit_code = 0;
167 
168 int
169 main(int argc, char *argv[])
170 {
171 	int c;
172 	int n;
173 	char *p;
174 
175 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
176 		signal(SIGINT, onintr);
177 
178 	init_macros();
179 	initspaces();
180 	STACKMAX = INITSTACKMAX;
181 
182 	mstack = xreallocarray(NULL, STACKMAX, sizeof(stae), NULL);
183 	sstack = xalloc(STACKMAX, NULL);
184 
185 	maxout = 0;
186 	outfile = NULL;
187 	resizedivs(MAXOUT);
188 
189 	while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1)
190 		switch(c) {
191 
192 		case 'D':               /* define something..*/
193 			for (p = optarg; *p; p++)
194 				if (*p == '=')
195 					break;
196 			if (*p)
197 				*p++ = EOS;
198 			dodefine(optarg, p);
199 			break;
200 		case 'I':
201 			addtoincludepath(optarg);
202 			break;
203 		case 'P':
204 			prefix_builtins = 1;
205 			break;
206 		case 'U':               /* undefine...       */
207 			macro_popdef(optarg);
208 			break;
209 		case 'g':
210 			mimic_gnu = 1;
211 			break;
212 		case 'd':
213 			set_trace_flags(optarg);
214 			break;
215 		case 's':
216 			synch_lines = 1;
217 			break;
218 		case 't':
219 			mark_traced(optarg, 1);
220 			break;
221 		case 'o':
222 			trace_file(optarg);
223                         break;
224 		case '?':
225 			usage();
226 		}
227 
228         argc -= optind;
229         argv += optind;
230 
231 	initkwds();
232 	if (mimic_gnu)
233 		setup_builtin("format", FORMATTYPE);
234 
235 	active = stdout;		/* default active output     */
236 	bbase[0] = bufbase;
237         if (!argc) {
238 		sp = -1;		/* stack pointer initialized */
239 		fp = 0;			/* frame pointer initialized */
240 		set_input(infile+0, stdin, "stdin");
241 					/* default input (naturally) */
242 		macro();
243 	} else
244 		for (; argc--; ++argv) {
245 			p = *argv;
246 			if (p[0] == '-' && p[1] == EOS)
247 				set_input(infile, stdin, "stdin");
248 			else if (fopen_trypath(infile, p) == NULL)
249 				err(1, "%s", p);
250 			sp = -1;
251 			fp = 0;
252 			macro();
253 			release_input(infile);
254 		}
255 
256 	if (wrapindex) {
257 		int i;
258 
259 		ilevel = 0;		/* in case m4wrap includes.. */
260 		bufbase = bp = buf;	/* use the entire buffer   */
261 		if (mimic_gnu) {
262 			while (wrapindex != 0) {
263 				for (i = 0; i < wrapindex; i++)
264 					pbstr(m4wraps[i]);
265 				wrapindex =0;
266 				macro();
267 			}
268 		} else {
269 			for (i = 0; i < wrapindex; i++) {
270 				pbstr(m4wraps[i]);
271 				macro();
272 			}
273 		}
274 	}
275 
276 	if (active != stdout)
277 		active = stdout;	/* reset output just in case */
278 	for (n = 1; n < maxout; n++)	/* default wrap-up: undivert */
279 		if (outfile[n] != NULL)
280 			getdiv(n);
281 					/* remove bitbucket if used  */
282 	if (outfile[0] != NULL) {
283 		(void) fclose(outfile[0]);
284 	}
285 
286 	return exit_code;
287 }
288 
289 /*
290  * Look ahead for `token'.
291  * (on input `t == token[0]')
292  * Used for comment and quoting delimiters.
293  * Returns 1 if `token' present; copied to output.
294  *         0 if `token' not found; all characters pushed back
295  */
296 static int
297 do_look_ahead(int t, const char *token)
298 {
299 	int i;
300 
301 	assert((unsigned char)t == (unsigned char)token[0]);
302 
303 	for (i = 1; *++token; i++) {
304 		t = gpbc();
305 		if (t == EOF || (unsigned char)t != (unsigned char)*token) {
306 			pushback(t);
307 			while (--i)
308 				pushback(*--token);
309 			return 0;
310 		}
311 	}
312 	return 1;
313 }
314 
315 #define LOOK_AHEAD(t, token) (t != EOF &&		\
316     (unsigned char)(t)==(unsigned char)(token)[0] &&	\
317     do_look_ahead(t,token))
318 
319 /*
320  * macro - the work horse..
321  */
322 static void
323 macro(void)
324 {
325 	char token[MAXTOK+1];
326 	int t, l;
327 	ndptr p;
328 	int  nlpar;
329 
330 	cycle {
331 		t = gpbc();
332 
333 		if (LOOK_AHEAD(t,lquote)) {	/* strip quotes */
334 			nlpar = 0;
335 			record(quotes, nlpar++);
336 			/*
337 			 * Opening quote: scan forward until matching
338 			 * closing quote has been found.
339 			 */
340 			do {
341 
342 				l = gpbc();
343 				if (LOOK_AHEAD(l,rquote)) {
344 					if (--nlpar > 0)
345 						outputstr(rquote);
346 				} else if (LOOK_AHEAD(l,lquote)) {
347 					record(quotes, nlpar++);
348 					outputstr(lquote);
349 				} else if (l == EOF) {
350 					if (nlpar == 1)
351 						warnx("unclosed quote:");
352 					else
353 						warnx("%d unclosed quotes:", nlpar);
354 					dump_stack(quotes, nlpar);
355 					exit(1);
356 				} else {
357 					if (nlpar > 0) {
358 						if (sp < 0)
359 							reallyputchar(l);
360 						else
361 							CHRSAVE(l);
362 					}
363 				}
364 			}
365 			while (nlpar != 0);
366 		} else if (sp < 0 && LOOK_AHEAD(t, scommt)) {
367 			reallyoutputstr(scommt);
368 
369 			for(;;) {
370 				t = gpbc();
371 				if (LOOK_AHEAD(t, ecommt)) {
372 					reallyoutputstr(ecommt);
373 					break;
374 				}
375 				if (t == EOF)
376 					break;
377 				reallyputchar(t);
378 			}
379 		} else if (t == '_' || isalpha(t)) {
380 			p = inspect(t, token);
381 			if (p != NULL)
382 				pushback(l = gpbc());
383 			if (p == NULL || (l != LPAREN &&
384 			    (macro_getdef(p)->type & NEEDARGS) != 0))
385 				outputstr(token);
386 			else {
387 		/*
388 		 * real thing.. First build a call frame:
389 		 */
390 				pushf(fp);	/* previous call frm */
391 				pushf(macro_getdef(p)->type); /* type of the call  */
392 				pushf(is_traced(p));
393 				pushf(0);	/* parenthesis level */
394 				fp = sp;	/* new frame pointer */
395 		/*
396 		 * now push the string arguments:
397 		 */
398 				pushdef(p);			/* defn string */
399 				pushs1((char *)macro_name(p));	/* macro name  */
400 				pushs(ep);			/* start next..*/
401 
402 				if (l != LPAREN && PARLEV == 0) {
403 				    /* no bracks  */
404 					chrsave(EOS);
405 
406 					if (sp == (int)STACKMAX)
407 						errx(1, "internal stack overflow");
408 					eval((const char **) mstack+fp+1, 2,
409 					    CALTYP, TRACESTATUS);
410 
411 					ep = PREVEP;	/* flush strspace */
412 					sp = PREVSP;	/* previous sp..  */
413 					fp = PREVFP;	/* rewind stack...*/
414 				}
415 			}
416 		} else if (t == EOF) {
417 			if (!mimic_gnu /* you can puke right there */
418 			    && sp > -1 && ilevel <= 0) {
419 				warnx( "unexpected end of input, unclosed parenthesis:");
420 				dump_stack(paren, PARLEV);
421 				exit(1);
422 			}
423 			if (ilevel <= 0)
424 				break;			/* all done thanks.. */
425 			release_input(infile+ilevel--);
426 			emit_synchline();
427 			bufbase = bbase[ilevel];
428 			continue;
429 		} else if (sp < 0) {		/* not in a macro at all */
430 			reallyputchar(t);	/* output directly..	 */
431 		}
432 
433 		else switch(t) {
434 
435 		case LPAREN:
436 			if (PARLEV > 0)
437 				chrsave(t);
438 			while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
439 				if (PARLEV > 0)
440 					chrsave(l);
441 			pushback(l);
442 			record(paren, PARLEV++);
443 			break;
444 
445 		case RPAREN:
446 			if (--PARLEV > 0)
447 				chrsave(t);
448 			else {			/* end of argument list */
449 				chrsave(EOS);
450 
451 				if (sp == (int)STACKMAX)
452 					errx(1, "internal stack overflow");
453 
454 				eval((const char **) mstack+fp+1, sp-fp,
455 				    CALTYP, TRACESTATUS);
456 
457 				ep = PREVEP;	/* flush strspace */
458 				sp = PREVSP;	/* previous sp..  */
459 				fp = PREVFP;	/* rewind stack...*/
460 			}
461 			break;
462 
463 		case COMMA:
464 			if (PARLEV == 1) {
465 				chrsave(EOS);		/* new argument   */
466 				while (isspace(l = gpbc()))
467 					;
468 				pushback(l);
469 				pushs(ep);
470 			} else
471 				chrsave(t);
472 			break;
473 
474 		default:
475 			if (LOOK_AHEAD(t, scommt)) {
476 				char *p;
477 				for (p = scommt; *p; p++)
478 					chrsave(*p);
479 				for(;;) {
480 					t = gpbc();
481 					if (LOOK_AHEAD(t, ecommt)) {
482 						for (p = ecommt; *p; p++)
483 							chrsave(*p);
484 						break;
485 					}
486 					if (t == EOF)
487 					    break;
488 					CHRSAVE(t);
489 				}
490 			} else
491 				CHRSAVE(t);		/* stack the char */
492 			break;
493 		}
494 	}
495 }
496 
497 /*
498  * output string directly, without pushing it for reparses.
499  */
500 void
501 outputstr(const char *s)
502 {
503 	if (sp < 0)
504 		reallyoutputstr(s);
505 	else
506 		while (*s)
507 			CHRSAVE(*s++);
508 }
509 
510 void
511 reallyoutputstr(const char *s)
512 {
513 	if (synch_lines) {
514 		while (*s) {
515 			fputc(*s, active);
516 			if (*s++ == '\n') {
517 				infile[ilevel].synch_lineno++;
518 				if (infile[ilevel].synch_lineno !=
519 				    infile[ilevel].lineno)
520 					do_emit_synchline();
521 			}
522 		}
523 	} else
524 		fputs(s, active);
525 }
526 
527 void
528 reallyputchar(int c)
529 {
530 	putc(c, active);
531 	if (synch_lines && c == '\n') {
532 		infile[ilevel].synch_lineno++;
533 		if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
534 			do_emit_synchline();
535 	}
536 }
537 
538 /*
539  * build an input token..
540  * consider only those starting with _ or A-Za-z.
541  */
542 static ndptr
543 inspect(int c, char *tp)
544 {
545 	char *name = tp;
546 	char *etp = tp+MAXTOK;
547 	ndptr p;
548 
549 	*tp++ = c;
550 
551 	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
552 		*tp++ = c;
553 	if (c != EOF)
554 		PUSHBACK(c);
555 	*tp = EOS;
556 	/* token is too long, it won't match anything, but it can still
557 	 * be output. */
558 	if (tp == ep) {
559 		outputstr(name);
560 		while (isalnum(c = gpbc()) || c == '_') {
561 			if (sp < 0)
562 				reallyputchar(c);
563 			else
564 				CHRSAVE(c);
565 		}
566 		*name = EOS;
567 		return NULL;
568 	}
569 
570 	p = ohash_find(&macros, ohash_qlookupi(&macros, name, (const char **)&tp));
571 	if (p == NULL)
572 		return NULL;
573 	if (macro_getdef(p) == NULL)
574 		return NULL;
575 	return p;
576 }
577 
578 /*
579  * initkwds - initialise m4 keywords as fast as possible.
580  * This very similar to install, but without certain overheads,
581  * such as calling lookup. Malloc is not used for storing the
582  * keyword strings, since we simply use the static pointers
583  * within keywrds block.
584  */
585 static void
586 initkwds(void)
587 {
588 	unsigned int type;
589 	int i;
590 
591 	for (i = 0; i < (int)MAXKEYS; i++) {
592 		type = keywrds[i].ktyp & TYPEMASK;
593 		if ((keywrds[i].ktyp & NOARGS) == 0)
594 			type |= NEEDARGS;
595 		setup_builtin(keywrds[i].knam, type);
596 	}
597 }
598 
599 static void
600 record(struct position *t, int lev)
601 {
602 	if (lev < MAXRECORD) {
603 		t[lev].name = CURRENT_NAME;
604 		t[lev].line = CURRENT_LINE;
605 	}
606 }
607 
608 static void
609 dump_stack(struct position *t, int lev)
610 {
611 	int i;
612 
613 	for (i = 0; i < lev; i++) {
614 		if (i == MAXRECORD) {
615 			fprintf(stderr, "   ...\n");
616 			break;
617 		}
618 		fprintf(stderr, "   %s at line %lu\n",
619 			t[i].name, t[i].line);
620 	}
621 }
622 
623 
624 static void
625 enlarge_stack(void)
626 {
627 	STACKMAX += STACKMAX/2;
628 	mstack = xreallocarray(mstack, STACKMAX, sizeof(stae),
629 	    "Evaluation stack overflow (%lu)",
630 	    (unsigned long)STACKMAX);
631 	sstack = xrealloc(sstack, STACKMAX,
632 	    "Evaluation stack overflow (%lu)",
633 	    (unsigned long)STACKMAX);
634 }
635