1 /* $OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen Exp $ */
2 /* $NetBSD: main.c,v 1.42 2012/04/25 18:23:58 christos Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
38 * Facility: m4 macro processor
41 #if HAVE_NBTOOL_CONFIG_H
42 #include "nbtool_config.h"
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: main.c,v 1.42 2012/04/25 18:23:58 christos Exp $");
61 #include "pathnames.h"
63 ndptr hashtab
[HASHSIZE
]; /* hash table for macros etc. */
64 stae
*mstack
; /* stack of m4 machine */
65 char *sstack
; /* shadow stack, for string space extension */
66 static size_t STACKMAX
; /* current maximum size of stack */
67 int sp
; /* current m4 stack pointer */
68 int fp
; /* m4 call frame pointer */
69 struct input_file infile
[MAXINP
];/* input file stack (0=stdin) */
70 FILE **outfile
; /* diversion array(0=bitbucket)*/
72 FILE *active
; /* active output file pointer */
73 int ilevel
= 0; /* input file stack pointer */
74 int oindex
= 0; /* diversion index.. */
75 const char *null
= ""; /* as it says.. just a null.. */
76 char **m4wraps
= NULL
; /* m4wraps array. */
77 int maxwraps
= 0; /* size of m4wraps array */
78 int wrapindex
= 0; /* current offset in m4wraps */
79 char lquote
[MAXCCHARS
+1] = {LQUOTE
}; /* left quote character (`) */
80 char rquote
[MAXCCHARS
+1] = {RQUOTE
}; /* right quote character (') */
81 char scommt
[MAXCCHARS
+1] = {SCOMMT
}; /* start character for comment */
82 char ecommt
[MAXCCHARS
+1] = {ECOMMT
}; /* end character for comment */
83 int synch_lines
= 0; /* line synchronisation for C preprocessor */
84 int prefix_builtins
= 0; /* -P option to prefix builtin keywords */
87 const char *knam
; /* keyword name */
88 int ktyp
; /* keyword type */
91 struct keyblk keywrds
[] = { /* m4 keywords to be installed */
92 { "include", INCLTYPE
},
93 { "sinclude", SINCTYPE
},
94 { "define", DEFITYPE
},
96 { "divert", DIVRTYPE
| NOARGS
},
99 { "substr", SUBSTYPE
},
100 { "ifelse", IFELTYPE
},
101 { "ifdef", IFDFTYPE
},
103 { "incr", INCRTYPE
},
104 { "decr", DECRTYPE
},
105 { "dnl", DNLNTYPE
| NOARGS
},
106 { "changequote", CHNQTYPE
| NOARGS
},
107 { "changecom", CHNCTYPE
| NOARGS
},
108 { "index", INDXTYPE
},
110 { "paste", PASTTYPE
},
111 { "spaste", SPASTYPE
},
112 /* Newer extensions, needed to handle gnu-m4 scripts */
113 { "indir", INDIRTYPE
},
114 { "builtin", BUILTINTYPE
},
115 { "patsubst", PATSTYPE
},
116 { "regexp", REGEXPTYPE
},
117 { "esyscmd", ESYSCMDTYPE
},
118 { "__file__", FILENAMETYPE
| NOARGS
},
119 { "__line__", LINETYPE
| NOARGS
},
121 { "popdef", POPDTYPE
},
122 { "pushdef", PUSDTYPE
},
123 { "dumpdef", DUMPTYPE
| NOARGS
},
124 { "shift", SHIFTYPE
| NOARGS
},
125 { "translit", TRNLTYPE
},
126 { "undefine", UNDFTYPE
},
127 { "undivert", UNDVTYPE
| NOARGS
},
128 { "divnum", DIVNTYPE
| NOARGS
},
129 { "maketemp", 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
},
138 #if defined(unix) || defined(__unix__)
139 { "unix", SELFTYPE
| NOARGS
},
142 { "vms", SELFTYPE
| NOARGS
},
147 #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk))
153 static struct position
{
156 } quotes
[MAXRECORD
], paren
[MAXRECORD
];
158 static void record(struct position
*, int);
159 static void dump_stack(struct position
*, int);
161 static void macro(void);
162 static void initkwds(void);
163 static ndptr
inspect(int, char *);
164 static int do_look_ahead(int, const char *);
165 static void reallyoutputstr(const char *);
166 static void reallyputchar(int);
168 static void enlarge_stack(void);
173 fprintf(stderr
, "usage: %s [-gPs] [-Dname[=value]] [-d flags] "
174 "[-I dirname] [-o filename]\n"
175 "\t[-t macro] [-Uname] [file ...]\n", getprogname());
182 char intrmessage
[] = "m4: interrupted.\n";
183 write(STDERR_FILENO
, intrmessage
, sizeof(intrmessage
)-1);
188 main(int argc
, char *argv
[])
194 setprogname(argv
[0]);
196 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
197 signal(SIGINT
, onintr
);
201 STACKMAX
= INITSTACKMAX
;
203 mstack
= (stae
*)xalloc(sizeof(stae
) * STACKMAX
, NULL
);
204 sstack
= (char *)xalloc(STACKMAX
, NULL
);
210 while ((c
= getopt(argc
, argv
, "gst:d:D:U:o:I:P")) != -1)
213 case 'D': /* define something..*/
214 for (p
= optarg
; *p
; p
++)
222 addtoincludepath(optarg
);
227 case 'U': /* undefine... */
228 macro_popdef(optarg
);
234 set_trace_flags(optarg
);
240 mark_traced(optarg
, 1);
254 setup_builtin("format", FORMATTYPE
);
256 active
= stdout
; /* default active output */
259 sp
= -1; /* stack pointer initialized */
260 fp
= 0; /* frame pointer initialized */
261 set_input(infile
+0, stdin
, "stdin");
262 /* default input (naturally) */
265 for (; argc
--; ++argv
) {
267 if (p
[0] == '-' && p
[1] == EOS
)
268 set_input(infile
, stdin
, "stdin");
269 else if (fopen_trypath(infile
, p
) == NULL
)
274 release_input(infile
);
280 ilevel
= 0; /* in case m4wrap includes.. */
281 bufbase
= bp
= buf
; /* use the entire buffer */
283 while (wrapindex
!= 0) {
284 for (i
= 0; i
< wrapindex
; i
++)
290 for (i
= 0; i
< wrapindex
; i
++) {
297 if (active
!= stdout
)
298 active
= stdout
; /* reset output just in case */
299 for (n
= 1; n
< maxout
; n
++) /* default wrap-up: undivert */
300 if (outfile
[n
] != NULL
)
302 /* remove bitbucket if used */
303 if (outfile
[0] != NULL
) {
304 (void) fclose(outfile
[0]);
311 * Look ahead for `token'.
312 * (on input `t == token[0]')
313 * Used for comment and quoting delimiters.
314 * Returns 1 if `token' present; copied to output.
315 * 0 if `token' not found; all characters pushed back
318 do_look_ahead(int t
, const char *token
)
322 assert((unsigned char)t
== (unsigned char)token
[0]);
324 for (i
= 1; *++token
; i
++) {
326 if (t
== EOF
|| (unsigned char)t
!= (unsigned char)*token
) {
336 #define LOOK_AHEAD(t, token) (t != EOF && \
337 (unsigned char)(t)==(unsigned char)(token)[0] && \
338 do_look_ahead(t,token))
341 * macro - the work horse..
346 char token
[MAXTOK
+1];
354 if (LOOK_AHEAD(t
,lquote
)) { /* strip quotes */
356 record(quotes
, nlpar
++);
358 * Opening quote: scan forward until matching
359 * closing quote has been found.
364 if (LOOK_AHEAD(l
,rquote
)) {
367 } else if (LOOK_AHEAD(l
,lquote
)) {
368 record(quotes
, nlpar
++);
370 } else if (l
== EOF
) {
372 warnx("unclosed quote:");
374 warnx("%d unclosed quotes:", nlpar
);
375 dump_stack(quotes
, nlpar
);
387 } else if (sp
< 0 && LOOK_AHEAD(t
, scommt
)) {
388 reallyoutputstr(scommt
);
392 if (LOOK_AHEAD(t
, ecommt
)) {
393 reallyoutputstr(ecommt
);
400 } else if (t
== '_' || isalpha(t
)) {
401 p
= inspect(t
, token
);
403 pushback(l
= gpbc());
404 if (p
== NULL
|| (l
!= LPAREN
&&
405 (macro_getdef(p
)->type
& NEEDARGS
) != 0))
409 * real thing.. First build a call frame:
411 pushf(fp
); /* previous call frm */
412 pushf(macro_getdef(p
)->type
); /* type of the call */
414 pushf(0); /* parenthesis level */
415 fp
= sp
; /* new frame pointer */
417 * now push the string arguments:
419 pushs1(macro_getdef(p
)->defn
); /* defn string */
420 pushs1((char *)macro_name(p
)); /* macro name */
421 pushs(ep
); /* start next..*/
423 if (l
!= LPAREN
&& PARLEV
== 0) {
427 if ((size_t)sp
== STACKMAX
)
428 errx(1, "internal stack overflow");
429 eval((const char **) mstack
+fp
+1, 2,
430 CALTYP
, TRACESTATUS
);
432 ep
= PREVEP
; /* flush strspace */
433 sp
= PREVSP
; /* previous sp.. */
434 fp
= PREVFP
; /* rewind stack...*/
437 } else if (t
== EOF
) {
438 if (sp
> -1 && ilevel
<= 0) {
439 warnx( "unexpected end of input, unclosed parenthesis:");
440 dump_stack(paren
, PARLEV
);
444 break; /* all done thanks.. */
445 release_input(infile
+ilevel
--);
447 bufbase
= bbase
[ilevel
];
449 } else if (sp
< 0) { /* not in a macro at all */
450 reallyputchar(t
); /* output directly.. */
458 while (isspace(l
= gpbc())) /* skip blank, tab, nl.. */
462 record(paren
, PARLEV
++);
468 else { /* end of argument list */
471 if ((size_t)sp
== STACKMAX
)
472 errx(1, "internal stack overflow");
474 eval((const char **) mstack
+fp
+1, sp
-fp
,
475 CALTYP
, TRACESTATUS
);
477 ep
= PREVEP
; /* flush strspace */
478 sp
= PREVSP
; /* previous sp.. */
479 fp
= PREVFP
; /* rewind stack...*/
485 chrsave(EOS
); /* new argument */
486 while (isspace(l
= gpbc()))
495 if (LOOK_AHEAD(t
, scommt
)) {
497 for (q
= scommt
; *q
; q
++)
501 if (LOOK_AHEAD(t
, ecommt
)) {
502 for (q
= ecommt
; *q
; q
++)
511 CHRSAVE(t
); /* stack the char */
518 * output string directly, without pushing it for reparses.
521 outputstr(const char *s
)
531 reallyoutputstr(const char *s
)
537 infile
[ilevel
].synch_lineno
++;
538 if (infile
[ilevel
].synch_lineno
!=
539 infile
[ilevel
].lineno
)
551 if (synch_lines
&& c
== '\n') {
552 infile
[ilevel
].synch_lineno
++;
553 if (infile
[ilevel
].synch_lineno
!= infile
[ilevel
].lineno
)
559 * build an input token..
560 * consider only those starting with _ or A-Za-z.
563 inspect(int c
, char *tp
)
566 char *etp
= tp
+MAXTOK
;
571 while ((isalnum(c
= gpbc()) || c
== '_') && tp
< etp
)
576 /* token is too long, it won't match anything, but it can still
580 while (isalnum(c
= gpbc()) || c
== '_') {
590 p
= ohash_find(¯os
, ohash_qlookupi(¯os
, name
, (void *)&tp
));
593 if (macro_getdef(p
) == NULL
)
599 * initkwds - initialise m4 keywords as fast as possible.
600 * This very similar to install, but without certain overheads,
601 * such as calling lookup. Malloc is not used for storing the
602 * keyword strings, since we simply use the static pointers
603 * within keywrds block.
611 for (i
= 0; i
< MAXKEYS
; i
++) {
612 type
= keywrds
[i
].ktyp
& TYPEMASK
;
613 if ((keywrds
[i
].ktyp
& NOARGS
) == 0)
615 setup_builtin(keywrds
[i
].knam
, type
);
620 record(struct position
*t
, int lev
)
622 if (lev
< MAXRECORD
) {
623 t
[lev
].name
= CURRENT_NAME
;
624 t
[lev
].line
= CURRENT_LINE
;
629 dump_stack(struct position
*t
, int lev
)
633 for (i
= 0; i
< lev
; i
++) {
634 if (i
== MAXRECORD
) {
635 fprintf(stderr
, " ...\n");
638 fprintf(stderr
, " %s at line %lu\n",
639 t
[i
].name
, t
[i
].line
);
647 STACKMAX
+= STACKMAX
/2;
648 mstack
= xrealloc(mstack
, sizeof(stae
) * STACKMAX
,
649 "Evaluation stack overflow (%lu)",
650 (unsigned long)STACKMAX
);
651 sstack
= xrealloc(sstack
, STACKMAX
,
652 "Evaluation stack overflow (%lu)",
653 (unsigned long)STACKMAX
);