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