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