xref: /openbsd-src/usr.bin/m4/main.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen 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 
42 #include <assert.h>
43 #include <signal.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <ctype.h>
49 #include <string.h>
50 #include <stddef.h>
51 #include <stdint.h>
52 #include <stdlib.h>
53 #include <ohash.h>
54 #include "mdef.h"
55 #include "stdd.h"
56 #include "extern.h"
57 #include "pathnames.h"
58 
59 ndptr hashtab[HASHSIZE];	/* hash table for macros etc.  */
60 stae *mstack;		 	/* stack of m4 machine         */
61 char *sstack;		 	/* shadow stack, for string space extension */
62 static size_t STACKMAX;		/* current maximum size of stack */
63 int sp; 			/* current m4  stack pointer   */
64 int fp; 			/* m4 call frame pointer       */
65 struct input_file infile[MAXINP];/* input file stack (0=stdin)  */
66 FILE **outfile;			/* diversion array(0=bitbucket)*/
67 int maxout;
68 FILE *active;			/* active output file pointer  */
69 int ilevel = 0; 		/* input file stack pointer    */
70 int oindex = 0; 		/* diversion index..	       */
71 char *null = "";                /* as it says.. just a null..  */
72 char **m4wraps = NULL;		/* m4wraps array.     	       */
73 int maxwraps = 0;		/* size of m4wraps array       */
74 int wrapindex = 0;		/* current offset in m4wraps   */
75 char lquote[MAXCCHARS+1] = {LQUOTE};	/* left quote character  (`)   */
76 char rquote[MAXCCHARS+1] = {RQUOTE};	/* right quote character (')   */
77 char scommt[MAXCCHARS+1] = {SCOMMT};	/* start character for comment */
78 char ecommt[MAXCCHARS+1] = {ECOMMT};	/* end character for comment   */
79 int  synch_lines = 0;		/* line synchronisation for C preprocessor */
80 int  prefix_builtins = 0;	/* -P option to prefix builtin keywords */
81 
82 struct keyblk {
83         char    *knam;          /* keyword name */
84         int     ktyp;           /* keyword type */
85 };
86 
87 struct keyblk keywrds[] = {	/* m4 keywords to be installed */
88 	{ "include",      INCLTYPE },
89 	{ "sinclude",     SINCTYPE },
90 	{ "define",       DEFITYPE },
91 	{ "defn",         DEFNTYPE },
92 	{ "divert",       DIVRTYPE | NOARGS },
93 	{ "expr",         EXPRTYPE },
94 	{ "eval",         EXPRTYPE },
95 	{ "substr",       SUBSTYPE },
96 	{ "ifelse",       IFELTYPE },
97 	{ "ifdef",        IFDFTYPE },
98 	{ "len",          LENGTYPE },
99 	{ "incr",         INCRTYPE },
100 	{ "decr",         DECRTYPE },
101 	{ "dnl",          DNLNTYPE | NOARGS },
102 	{ "changequote",  CHNQTYPE | NOARGS },
103 	{ "changecom",    CHNCTYPE | NOARGS },
104 	{ "index",        INDXTYPE },
105 #ifdef EXTENDED
106 	{ "paste",        PASTTYPE },
107 	{ "spaste",       SPASTYPE },
108     	/* Newer extensions, needed to handle gnu-m4 scripts */
109 	{ "indir",        INDIRTYPE},
110 	{ "builtin",      BUILTINTYPE},
111 	{ "patsubst",	  PATSTYPE},
112 	{ "regexp",	  REGEXPTYPE},
113 	{ "esyscmd",	  ESYSCMDTYPE},
114 	{ "__file__",	  FILENAMETYPE | NOARGS},
115 	{ "__line__",	  LINETYPE | NOARGS},
116 #endif
117 	{ "popdef",       POPDTYPE },
118 	{ "pushdef",      PUSDTYPE },
119 	{ "dumpdef",      DUMPTYPE | NOARGS },
120 	{ "shift",        SHIFTYPE | NOARGS },
121 	{ "translit",     TRNLTYPE },
122 	{ "undefine",     UNDFTYPE },
123 	{ "undivert",     UNDVTYPE | NOARGS },
124 	{ "divnum",       DIVNTYPE | NOARGS },
125 	{ "maketemp",     MKTMTYPE },
126 	{ "errprint",     ERRPTYPE | NOARGS },
127 	{ "m4wrap",       M4WRTYPE | NOARGS },
128 	{ "m4exit",       EXITTYPE | NOARGS },
129 	{ "syscmd",       SYSCTYPE },
130 	{ "sysval",       SYSVTYPE | NOARGS },
131 	{ "traceon",	  TRACEONTYPE | NOARGS },
132 	{ "traceoff",	  TRACEOFFTYPE | NOARGS },
133 
134 #if defined(unix) || defined(__unix__)
135 	{ "unix",         SELFTYPE | NOARGS },
136 #else
137 #ifdef vms
138 	{ "vms",          SELFTYPE | NOARGS },
139 #endif
140 #endif
141 };
142 
143 #define MAXKEYS	(sizeof(keywrds)/sizeof(struct keyblk))
144 
145 extern int optind;
146 extern char *optarg;
147 
148 #define MAXRECORD 50
149 static struct position {
150 	char *name;
151 	unsigned long line;
152 } quotes[MAXRECORD], paren[MAXRECORD];
153 
154 static void record(struct position *, int);
155 static void dump_stack(struct position *, int);
156 
157 static void macro(void);
158 static void initkwds(void);
159 static ndptr inspect(int, char *);
160 static int do_look_ahead(int, const char *);
161 static void reallyoutputstr(const char *);
162 static void reallyputchar(int);
163 
164 static void enlarge_stack(void);
165 
166 int main(int, char *[]);
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 = (stae *)xalloc(sizeof(stae) * STACKMAX, NULL);
183 	sstack = (char *)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 0;
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 				pushs1(macro_getdef(p)->defn);	/* 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 == 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 (sp > -1 && ilevel <= 0) {
418 				warnx( "unexpected end of input, unclosed parenthesis:");
419 				dump_stack(paren, PARLEV);
420 				exit(1);
421 			}
422 			if (ilevel <= 0)
423 				break;			/* all done thanks.. */
424 			release_input(infile+ilevel--);
425 			emit_synchline();
426 			bufbase = bbase[ilevel];
427 			continue;
428 		} else if (sp < 0) {		/* not in a macro at all */
429 			reallyputchar(t);	/* output directly..	 */
430 		}
431 
432 		else switch(t) {
433 
434 		case LPAREN:
435 			if (PARLEV > 0)
436 				chrsave(t);
437 			while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
438 				if (PARLEV > 0)
439 					chrsave(l);
440 			pushback(l);
441 			record(paren, PARLEV++);
442 			break;
443 
444 		case RPAREN:
445 			if (--PARLEV > 0)
446 				chrsave(t);
447 			else {			/* end of argument list */
448 				chrsave(EOS);
449 
450 				if (sp == STACKMAX)
451 					errx(1, "internal stack overflow");
452 
453 				eval((const char **) mstack+fp+1, sp-fp,
454 				    CALTYP, TRACESTATUS);
455 
456 				ep = PREVEP;	/* flush strspace */
457 				sp = PREVSP;	/* previous sp..  */
458 				fp = PREVFP;	/* rewind stack...*/
459 			}
460 			break;
461 
462 		case COMMA:
463 			if (PARLEV == 1) {
464 				chrsave(EOS);		/* new argument   */
465 				while (isspace(l = gpbc()))
466 					;
467 				pushback(l);
468 				pushs(ep);
469 			} else
470 				chrsave(t);
471 			break;
472 
473 		default:
474 			if (LOOK_AHEAD(t, scommt)) {
475 				char *p;
476 				for (p = scommt; *p; p++)
477 					chrsave(*p);
478 				for(;;) {
479 					t = gpbc();
480 					if (LOOK_AHEAD(t, ecommt)) {
481 						for (p = ecommt; *p; p++)
482 							chrsave(*p);
483 						break;
484 					}
485 					if (t == EOF)
486 					    break;
487 					CHRSAVE(t);
488 				}
489 			} else
490 				CHRSAVE(t);		/* stack the char */
491 			break;
492 		}
493 	}
494 }
495 
496 /*
497  * output string directly, without pushing it for reparses.
498  */
499 void
500 outputstr(const char *s)
501 {
502 	if (sp < 0)
503 		reallyoutputstr(s);
504 	else
505 		while (*s)
506 			CHRSAVE(*s++);
507 }
508 
509 void
510 reallyoutputstr(const char *s)
511 {
512 	if (synch_lines) {
513 		while (*s) {
514 			fputc(*s, active);
515 			if (*s++ == '\n') {
516 				infile[ilevel].synch_lineno++;
517 				if (infile[ilevel].synch_lineno !=
518 				    infile[ilevel].lineno)
519 					do_emit_synchline();
520 			}
521 		}
522 	} else
523 		fputs(s, active);
524 }
525 
526 void
527 reallyputchar(int c)
528 {
529 	putc(c, active);
530 	if (synch_lines && c == '\n') {
531 		infile[ilevel].synch_lineno++;
532 		if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
533 			do_emit_synchline();
534 	}
535 }
536 
537 /*
538  * build an input token..
539  * consider only those starting with _ or A-Za-z.
540  */
541 static ndptr
542 inspect(int c, char *tp)
543 {
544 	char *name = tp;
545 	char *etp = tp+MAXTOK;
546 	ndptr p;
547 
548 	*tp++ = c;
549 
550 	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
551 		*tp++ = c;
552 	if (c != EOF)
553 		PUSHBACK(c);
554 	*tp = EOS;
555 	/* token is too long, it won't match anything, but it can still
556 	 * be output. */
557 	if (tp == ep) {
558 		outputstr(name);
559 		while (isalnum(c = gpbc()) || c == '_') {
560 			if (sp < 0)
561 				reallyputchar(c);
562 			else
563 				CHRSAVE(c);
564 		}
565 		*name = EOS;
566 		return NULL;
567 	}
568 
569 	p = ohash_find(&macros, ohash_qlookupi(&macros, name, (const char **)&tp));
570 	if (p == NULL)
571 		return NULL;
572 	if (macro_getdef(p) == NULL)
573 		return NULL;
574 	return p;
575 }
576 
577 /*
578  * initkwds - initialise m4 keywords as fast as possible.
579  * This very similar to install, but without certain overheads,
580  * such as calling lookup. Malloc is not used for storing the
581  * keyword strings, since we simply use the static pointers
582  * within keywrds block.
583  */
584 static void
585 initkwds(void)
586 {
587 	unsigned int type;
588 	int i;
589 
590 	for (i = 0; i < MAXKEYS; i++) {
591 		type = keywrds[i].ktyp & TYPEMASK;
592 		if ((keywrds[i].ktyp & NOARGS) == 0)
593 			type |= NEEDARGS;
594 		setup_builtin(keywrds[i].knam, type);
595 	}
596 }
597 
598 static void
599 record(struct position *t, int lev)
600 {
601 	if (lev < MAXRECORD) {
602 		t[lev].name = CURRENT_NAME;
603 		t[lev].line = CURRENT_LINE;
604 	}
605 }
606 
607 static void
608 dump_stack(struct position *t, int lev)
609 {
610 	int i;
611 
612 	for (i = 0; i < lev; i++) {
613 		if (i == MAXRECORD) {
614 			fprintf(stderr, "   ...\n");
615 			break;
616 		}
617 		fprintf(stderr, "   %s at line %lu\n",
618 			t[i].name, t[i].line);
619 	}
620 }
621 
622 
623 static void
624 enlarge_stack(void)
625 {
626 	STACKMAX += STACKMAX/2;
627 	mstack = xrealloc(mstack, sizeof(stae) * STACKMAX,
628 	    "Evaluation stack overflow (%lu)",
629 	    (unsigned long)STACKMAX);
630 	sstack = xrealloc(sstack, STACKMAX,
631 	    "Evaluation stack overflow (%lu)",
632 	    (unsigned long)STACKMAX);
633 }
634