xref: /openbsd/usr.bin/m4/main.c (revision cecf84d4)
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 
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 = xreallocarray(NULL, STACKMAX, sizeof(stae), NULL);
185 	sstack = 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 				pushdef(p);			/* 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 (!mimic_gnu /* you can puke right there */
420 			    && sp > -1 && ilevel <= 0) {
421 				warnx( "unexpected end of input, unclosed parenthesis:");
422 				dump_stack(paren, PARLEV);
423 				exit(1);
424 			}
425 			if (ilevel <= 0)
426 				break;			/* all done thanks.. */
427 			release_input(infile+ilevel--);
428 			emit_synchline();
429 			bufbase = bbase[ilevel];
430 			continue;
431 		} else if (sp < 0) {		/* not in a macro at all */
432 			reallyputchar(t);	/* output directly..	 */
433 		}
434 
435 		else switch(t) {
436 
437 		case LPAREN:
438 			if (PARLEV > 0)
439 				chrsave(t);
440 			while (isspace(l = gpbc())) /* skip blank, tab, nl.. */
441 				if (PARLEV > 0)
442 					chrsave(l);
443 			pushback(l);
444 			record(paren, PARLEV++);
445 			break;
446 
447 		case RPAREN:
448 			if (--PARLEV > 0)
449 				chrsave(t);
450 			else {			/* end of argument list */
451 				chrsave(EOS);
452 
453 				if (sp == STACKMAX)
454 					errx(1, "internal stack overflow");
455 
456 				eval((const char **) mstack+fp+1, sp-fp,
457 				    CALTYP, TRACESTATUS);
458 
459 				ep = PREVEP;	/* flush strspace */
460 				sp = PREVSP;	/* previous sp..  */
461 				fp = PREVFP;	/* rewind stack...*/
462 			}
463 			break;
464 
465 		case COMMA:
466 			if (PARLEV == 1) {
467 				chrsave(EOS);		/* new argument   */
468 				while (isspace(l = gpbc()))
469 					;
470 				pushback(l);
471 				pushs(ep);
472 			} else
473 				chrsave(t);
474 			break;
475 
476 		default:
477 			if (LOOK_AHEAD(t, scommt)) {
478 				char *p;
479 				for (p = scommt; *p; p++)
480 					chrsave(*p);
481 				for(;;) {
482 					t = gpbc();
483 					if (LOOK_AHEAD(t, ecommt)) {
484 						for (p = ecommt; *p; p++)
485 							chrsave(*p);
486 						break;
487 					}
488 					if (t == EOF)
489 					    break;
490 					CHRSAVE(t);
491 				}
492 			} else
493 				CHRSAVE(t);		/* stack the char */
494 			break;
495 		}
496 	}
497 }
498 
499 /*
500  * output string directly, without pushing it for reparses.
501  */
502 void
503 outputstr(const char *s)
504 {
505 	if (sp < 0)
506 		reallyoutputstr(s);
507 	else
508 		while (*s)
509 			CHRSAVE(*s++);
510 }
511 
512 void
513 reallyoutputstr(const char *s)
514 {
515 	if (synch_lines) {
516 		while (*s) {
517 			fputc(*s, active);
518 			if (*s++ == '\n') {
519 				infile[ilevel].synch_lineno++;
520 				if (infile[ilevel].synch_lineno !=
521 				    infile[ilevel].lineno)
522 					do_emit_synchline();
523 			}
524 		}
525 	} else
526 		fputs(s, active);
527 }
528 
529 void
530 reallyputchar(int c)
531 {
532 	putc(c, active);
533 	if (synch_lines && c == '\n') {
534 		infile[ilevel].synch_lineno++;
535 		if (infile[ilevel].synch_lineno != infile[ilevel].lineno)
536 			do_emit_synchline();
537 	}
538 }
539 
540 /*
541  * build an input token..
542  * consider only those starting with _ or A-Za-z.
543  */
544 static ndptr
545 inspect(int c, char *tp)
546 {
547 	char *name = tp;
548 	char *etp = tp+MAXTOK;
549 	ndptr p;
550 
551 	*tp++ = c;
552 
553 	while ((isalnum(c = gpbc()) || c == '_') && tp < etp)
554 		*tp++ = c;
555 	if (c != EOF)
556 		PUSHBACK(c);
557 	*tp = EOS;
558 	/* token is too long, it won't match anything, but it can still
559 	 * be output. */
560 	if (tp == ep) {
561 		outputstr(name);
562 		while (isalnum(c = gpbc()) || c == '_') {
563 			if (sp < 0)
564 				reallyputchar(c);
565 			else
566 				CHRSAVE(c);
567 		}
568 		*name = EOS;
569 		return NULL;
570 	}
571 
572 	p = ohash_find(&macros, ohash_qlookupi(&macros, name, (const char **)&tp));
573 	if (p == NULL)
574 		return NULL;
575 	if (macro_getdef(p) == NULL)
576 		return NULL;
577 	return p;
578 }
579 
580 /*
581  * initkwds - initialise m4 keywords as fast as possible.
582  * This very similar to install, but without certain overheads,
583  * such as calling lookup. Malloc is not used for storing the
584  * keyword strings, since we simply use the static pointers
585  * within keywrds block.
586  */
587 static void
588 initkwds(void)
589 {
590 	unsigned int type;
591 	int i;
592 
593 	for (i = 0; i < MAXKEYS; i++) {
594 		type = keywrds[i].ktyp & TYPEMASK;
595 		if ((keywrds[i].ktyp & NOARGS) == 0)
596 			type |= NEEDARGS;
597 		setup_builtin(keywrds[i].knam, type);
598 	}
599 }
600 
601 static void
602 record(struct position *t, int lev)
603 {
604 	if (lev < MAXRECORD) {
605 		t[lev].name = CURRENT_NAME;
606 		t[lev].line = CURRENT_LINE;
607 	}
608 }
609 
610 static void
611 dump_stack(struct position *t, int lev)
612 {
613 	int i;
614 
615 	for (i = 0; i < lev; i++) {
616 		if (i == MAXRECORD) {
617 			fprintf(stderr, "   ...\n");
618 			break;
619 		}
620 		fprintf(stderr, "   %s at line %lu\n",
621 			t[i].name, t[i].line);
622 	}
623 }
624 
625 
626 static void
627 enlarge_stack(void)
628 {
629 	STACKMAX += STACKMAX/2;
630 	mstack = xreallocarray(mstack, STACKMAX, sizeof(stae),
631 	    "Evaluation stack overflow (%lu)",
632 	    (unsigned long)STACKMAX);
633 	sstack = xrealloc(sstack, STACKMAX,
634 	    "Evaluation stack overflow (%lu)",
635 	    (unsigned long)STACKMAX);
636 }
637