1 /* $OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen Exp $ */
2 /* $NetBSD: main.c,v 1.38 2009/10/26 21:11:28 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.38 2009/10/26 21:11:28 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);
170 int main(int, char *[]);
173 main(int argc
, char *argv
[])
179 setprogname(argv
[0]);
181 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
182 signal(SIGINT
, onintr
);
186 STACKMAX
= INITSTACKMAX
;
188 mstack
= (stae
*)xalloc(sizeof(stae
) * STACKMAX
, NULL
);
189 sstack
= (char *)xalloc(STACKMAX
, NULL
);
195 while ((c
= getopt(argc
, argv
, "gst:d:D:U:o:I:P")) != -1)
198 case 'D': /* define something..*/
199 for (p
= optarg
; *p
; p
++)
207 addtoincludepath(optarg
);
212 case 'U': /* undefine... */
213 macro_popdef(optarg
);
219 set_trace_flags(optarg
);
225 mark_traced(optarg
, 1);
239 setup_builtin("format", FORMATTYPE
);
241 active
= stdout
; /* default active output */
244 sp
= -1; /* stack pointer initialized */
245 fp
= 0; /* frame pointer initialized */
246 set_input(infile
+0, stdin
, "stdin");
247 /* default input (naturally) */
250 for (; argc
--; ++argv
) {
252 if (p
[0] == '-' && p
[1] == EOS
)
253 set_input(infile
, stdin
, "stdin");
254 else if (fopen_trypath(infile
, p
) == NULL
)
259 release_input(infile
);
265 ilevel
= 0; /* in case m4wrap includes.. */
266 bufbase
= bp
= buf
; /* use the entire buffer */
268 while (wrapindex
!= 0) {
269 for (i
= 0; i
< wrapindex
; i
++)
275 for (i
= 0; i
< wrapindex
; i
++) {
282 if (active
!= stdout
)
283 active
= stdout
; /* reset output just in case */
284 for (n
= 1; n
< maxout
; n
++) /* default wrap-up: undivert */
285 if (outfile
[n
] != NULL
)
287 /* remove bitbucket if used */
288 if (outfile
[0] != NULL
) {
289 (void) fclose(outfile
[0]);
296 * Look ahead for `token'.
297 * (on input `t == token[0]')
298 * Used for comment and quoting delimiters.
299 * Returns 1 if `token' present; copied to output.
300 * 0 if `token' not found; all characters pushed back
303 do_look_ahead(int t
, const char *token
)
307 assert((unsigned char)t
== (unsigned char)token
[0]);
309 for (i
= 1; *++token
; i
++) {
311 if (t
== EOF
|| (unsigned char)t
!= (unsigned char)*token
) {
321 #define LOOK_AHEAD(t, token) (t != EOF && \
322 (unsigned char)(t)==(unsigned char)(token)[0] && \
323 do_look_ahead(t,token))
326 * macro - the work horse..
331 char token
[MAXTOK
+1];
339 if (LOOK_AHEAD(t
,lquote
)) { /* strip quotes */
341 record(quotes
, nlpar
++);
343 * Opening quote: scan forward until matching
344 * closing quote has been found.
349 if (LOOK_AHEAD(l
,rquote
)) {
352 } else if (LOOK_AHEAD(l
,lquote
)) {
353 record(quotes
, nlpar
++);
355 } else if (l
== EOF
) {
357 warnx("unclosed quote:");
359 warnx("%d unclosed quotes:", nlpar
);
360 dump_stack(quotes
, nlpar
);
372 } else if (sp
< 0 && LOOK_AHEAD(t
, scommt
)) {
373 reallyoutputstr(scommt
);
377 if (LOOK_AHEAD(t
, ecommt
)) {
378 reallyoutputstr(ecommt
);
385 } else if (t
== '_' || isalpha(t
)) {
386 p
= inspect(t
, token
);
388 pushback(l
= gpbc());
389 if (p
== NULL
|| (l
!= LPAREN
&&
390 (macro_getdef(p
)->type
& NEEDARGS
) != 0))
394 * real thing.. First build a call frame:
396 pushf(fp
); /* previous call frm */
397 pushf(macro_getdef(p
)->type
); /* type of the call */
399 pushf(0); /* parenthesis level */
400 fp
= sp
; /* new frame pointer */
402 * now push the string arguments:
404 pushs1(macro_getdef(p
)->defn
); /* defn string */
405 pushs1((char *)macro_name(p
)); /* macro name */
406 pushs(ep
); /* start next..*/
408 if (l
!= LPAREN
&& PARLEV
== 0) {
412 if ((size_t)sp
== STACKMAX
)
413 errx(1, "internal stack overflow");
414 eval((const char **) mstack
+fp
+1, 2,
415 CALTYP
, TRACESTATUS
);
417 ep
= PREVEP
; /* flush strspace */
418 sp
= PREVSP
; /* previous sp.. */
419 fp
= PREVFP
; /* rewind stack...*/
422 } else if (t
== EOF
) {
423 if (sp
> -1 && ilevel
<= 0) {
424 warnx( "unexpected end of input, unclosed parenthesis:");
425 dump_stack(paren
, PARLEV
);
429 break; /* all done thanks.. */
430 release_input(infile
+ilevel
--);
432 bufbase
= bbase
[ilevel
];
434 } else if (sp
< 0) { /* not in a macro at all */
435 reallyputchar(t
); /* output directly.. */
443 while (isspace(l
= gpbc())) /* skip blank, tab, nl.. */
447 record(paren
, PARLEV
++);
453 else { /* end of argument list */
456 if ((size_t)sp
== STACKMAX
)
457 errx(1, "internal stack overflow");
459 eval((const char **) mstack
+fp
+1, sp
-fp
,
460 CALTYP
, TRACESTATUS
);
462 ep
= PREVEP
; /* flush strspace */
463 sp
= PREVSP
; /* previous sp.. */
464 fp
= PREVFP
; /* rewind stack...*/
470 chrsave(EOS
); /* new argument */
471 while (isspace(l
= gpbc()))
480 if (LOOK_AHEAD(t
, scommt
)) {
482 for (q
= scommt
; *q
; p
++)
486 if (LOOK_AHEAD(t
, ecommt
)) {
487 for (q
= ecommt
; *q
; q
++)
496 CHRSAVE(t
); /* stack the char */
503 * output string directly, without pushing it for reparses.
506 outputstr(const char *s
)
516 reallyoutputstr(const char *s
)
522 infile
[ilevel
].synch_lineno
++;
523 if (infile
[ilevel
].synch_lineno
!=
524 infile
[ilevel
].lineno
)
536 if (synch_lines
&& c
== '\n') {
537 infile
[ilevel
].synch_lineno
++;
538 if (infile
[ilevel
].synch_lineno
!= infile
[ilevel
].lineno
)
544 * build an input token..
545 * consider only those starting with _ or A-Za-z.
548 inspect(int c
, char *tp
)
551 char *etp
= tp
+MAXTOK
;
556 while ((isalnum(c
= gpbc()) || c
== '_') && tp
< etp
)
561 /* token is too long, it won't match anything, but it can still
565 while (isalnum(c
= gpbc()) || c
== '_') {
575 p
= ohash_find(¯os
, ohash_qlookupi(¯os
, name
, (const char **)&tp
));
578 if (macro_getdef(p
) == NULL
)
584 * initkwds - initialise m4 keywords as fast as possible.
585 * This very similar to install, but without certain overheads,
586 * such as calling lookup. Malloc is not used for storing the
587 * keyword strings, since we simply use the static pointers
588 * within keywrds block.
596 for (i
= 0; i
< MAXKEYS
; i
++) {
597 type
= keywrds
[i
].ktyp
& TYPEMASK
;
598 if ((keywrds
[i
].ktyp
& NOARGS
) == 0)
600 setup_builtin(keywrds
[i
].knam
, type
);
605 record(struct position
*t
, int lev
)
607 if (lev
< MAXRECORD
) {
608 t
[lev
].name
= CURRENT_NAME
;
609 t
[lev
].line
= CURRENT_LINE
;
614 dump_stack(struct position
*t
, int lev
)
618 for (i
= 0; i
< lev
; i
++) {
619 if (i
== MAXRECORD
) {
620 fprintf(stderr
, " ...\n");
623 fprintf(stderr
, " %s at line %lu\n",
624 t
[i
].name
, t
[i
].line
);
632 STACKMAX
+= STACKMAX
/2;
633 mstack
= xrealloc(mstack
, sizeof(stae
) * STACKMAX
,
634 "Evaluation stack overflow (%lu)",
635 (unsigned long)STACKMAX
);
636 sstack
= xrealloc(sstack
, STACKMAX
,
637 "Evaluation stack overflow (%lu)",
638 (unsigned long)STACKMAX
);