1 /* $OpenBSD: main.c,v 1.76 2008/08/16 12:21:46 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 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 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 { "errprint", ERRPTYPE | NOARGS }, 126 { "m4wrap", M4WRTYPE | NOARGS }, 127 { "m4exit", EXITTYPE | NOARGS }, 128 { "syscmd", SYSCTYPE }, 129 { "sysval", SYSVTYPE | NOARGS }, 130 { "traceon", TRACEONTYPE | NOARGS }, 131 { "traceoff", TRACEOFFTYPE | NOARGS }, 132 133 #if defined(unix) || defined(__unix__) 134 { "unix", SELFTYPE | NOARGS }, 135 #else 136 #ifdef vms 137 { "vms", SELFTYPE | NOARGS }, 138 #endif 139 #endif 140 }; 141 142 #define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk)) 143 144 extern int optind; 145 extern char *optarg; 146 147 #define MAXRECORD 50 148 static struct position { 149 char *name; 150 unsigned long line; 151 } quotes[MAXRECORD], paren[MAXRECORD]; 152 153 static void record(struct position *, int); 154 static void dump_stack(struct position *, int); 155 156 static void macro(void); 157 static void initkwds(void); 158 static ndptr inspect(int, char *); 159 static int do_look_ahead(int, const char *); 160 static void reallyoutputstr(const char *); 161 static void reallyputchar(int); 162 163 static void enlarge_stack(void); 164 165 int main(int, char *[]); 166 167 int 168 main(int argc, char *argv[]) 169 { 170 int c; 171 int n; 172 char *p; 173 174 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 175 signal(SIGINT, onintr); 176 177 init_macros(); 178 initkwds(); 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:")) != -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 'U': /* undefine... */ 204 macro_popdef(optarg); 205 break; 206 case 'g': 207 mimic_gnu = 1; 208 setup_builtin("format", FORMATTYPE); 209 break; 210 case 'd': 211 set_trace_flags(optarg); 212 break; 213 case 's': 214 synch_lines = 1; 215 break; 216 case 't': 217 mark_traced(optarg, 1); 218 break; 219 case 'o': 220 trace_file(optarg); 221 break; 222 case '?': 223 usage(); 224 } 225 226 argc -= optind; 227 argv += optind; 228 229 active = stdout; /* default active output */ 230 bbase[0] = bufbase; 231 if (!argc) { 232 sp = -1; /* stack pointer initialized */ 233 fp = 0; /* frame pointer initialized */ 234 set_input(infile+0, stdin, "stdin"); 235 /* default input (naturally) */ 236 macro(); 237 } else 238 for (; argc--; ++argv) { 239 p = *argv; 240 if (p[0] == '-' && p[1] == EOS) 241 set_input(infile, stdin, "stdin"); 242 else if (fopen_trypath(infile, p) == NULL) 243 err(1, "%s", p); 244 sp = -1; 245 fp = 0; 246 macro(); 247 release_input(infile); 248 } 249 250 if (wrapindex) { 251 int i; 252 253 ilevel = 0; /* in case m4wrap includes.. */ 254 bufbase = bp = buf; /* use the entire buffer */ 255 if (mimic_gnu) { 256 while (wrapindex != 0) { 257 for (i = 0; i < wrapindex; i++) 258 pbstr(m4wraps[i]); 259 wrapindex =0; 260 macro(); 261 } 262 } else { 263 for (i = 0; i < wrapindex; i++) { 264 pbstr(m4wraps[i]); 265 macro(); 266 } 267 } 268 } 269 270 if (active != stdout) 271 active = stdout; /* reset output just in case */ 272 for (n = 1; n < maxout; n++) /* default wrap-up: undivert */ 273 if (outfile[n] != NULL) 274 getdiv(n); 275 /* remove bitbucket if used */ 276 if (outfile[0] != NULL) { 277 (void) fclose(outfile[0]); 278 } 279 280 return 0; 281 } 282 283 /* 284 * Look ahead for `token'. 285 * (on input `t == token[0]') 286 * Used for comment and quoting delimiters. 287 * Returns 1 if `token' present; copied to output. 288 * 0 if `token' not found; all characters pushed back 289 */ 290 static int 291 do_look_ahead(int t, const char *token) 292 { 293 int i; 294 295 assert((unsigned char)t == (unsigned char)token[0]); 296 297 for (i = 1; *++token; i++) { 298 t = gpbc(); 299 if (t == EOF || (unsigned char)t != (unsigned char)*token) { 300 pushback(t); 301 while (--i) 302 pushback(*--token); 303 return 0; 304 } 305 } 306 return 1; 307 } 308 309 #define LOOK_AHEAD(t, token) (t != EOF && \ 310 (unsigned char)(t)==(unsigned char)(token)[0] && \ 311 do_look_ahead(t,token)) 312 313 /* 314 * macro - the work horse.. 315 */ 316 static void 317 macro(void) 318 { 319 char token[MAXTOK+1]; 320 int t, l; 321 ndptr p; 322 int nlpar; 323 324 cycle { 325 t = gpbc(); 326 327 if (LOOK_AHEAD(t,lquote)) { /* strip quotes */ 328 nlpar = 0; 329 record(quotes, nlpar++); 330 /* 331 * Opening quote: scan forward until matching 332 * closing quote has been found. 333 */ 334 do { 335 336 l = gpbc(); 337 if (LOOK_AHEAD(l,rquote)) { 338 if (--nlpar > 0) 339 outputstr(rquote); 340 } else if (LOOK_AHEAD(l,lquote)) { 341 record(quotes, nlpar++); 342 outputstr(lquote); 343 } else if (l == EOF) { 344 if (nlpar == 1) 345 warnx("unclosed quote:"); 346 else 347 warnx("%d unclosed quotes:", nlpar); 348 dump_stack(quotes, nlpar); 349 exit(1); 350 } else { 351 if (nlpar > 0) { 352 if (sp < 0) 353 reallyputchar(l); 354 else 355 CHRSAVE(l); 356 } 357 } 358 } 359 while (nlpar != 0); 360 } else if (sp < 0 && LOOK_AHEAD(t, scommt)) { 361 reallyoutputstr(scommt); 362 363 for(;;) { 364 t = gpbc(); 365 if (LOOK_AHEAD(t, ecommt)) { 366 reallyoutputstr(ecommt); 367 break; 368 } 369 if (t == EOF) 370 break; 371 reallyputchar(t); 372 } 373 } else if (t == '_' || isalpha(t)) { 374 p = inspect(t, token); 375 if (p != NULL) 376 pushback(l = gpbc()); 377 if (p == NULL || (l != LPAREN && 378 (macro_getdef(p)->type & NEEDARGS) != 0)) 379 outputstr(token); 380 else { 381 /* 382 * real thing.. First build a call frame: 383 */ 384 pushf(fp); /* previous call frm */ 385 pushf(macro_getdef(p)->type); /* type of the call */ 386 pushf(is_traced(p)); 387 pushf(0); /* parenthesis level */ 388 fp = sp; /* new frame pointer */ 389 /* 390 * now push the string arguments: 391 */ 392 pushs1(macro_getdef(p)->defn); /* defn string */ 393 pushs1((char *)macro_name(p)); /* macro name */ 394 pushs(ep); /* start next..*/ 395 396 if (l != LPAREN && PARLEV == 0) { 397 /* no bracks */ 398 chrsave(EOS); 399 400 if (sp == STACKMAX) 401 errx(1, "internal stack overflow"); 402 eval((const char **) mstack+fp+1, 2, 403 CALTYP, TRACESTATUS); 404 405 ep = PREVEP; /* flush strspace */ 406 sp = PREVSP; /* previous sp.. */ 407 fp = PREVFP; /* rewind stack...*/ 408 } 409 } 410 } else if (t == EOF) { 411 if (sp > -1 && ilevel <= 0) { 412 warnx( "unexpected end of input, unclosed parenthesis:"); 413 dump_stack(paren, PARLEV); 414 exit(1); 415 } 416 if (ilevel <= 0) 417 break; /* all done thanks.. */ 418 release_input(infile+ilevel--); 419 emit_synchline(); 420 bufbase = bbase[ilevel]; 421 continue; 422 } else if (sp < 0) { /* not in a macro at all */ 423 reallyputchar(t); /* output directly.. */ 424 } 425 426 else switch(t) { 427 428 case LPAREN: 429 if (PARLEV > 0) 430 chrsave(t); 431 while (isspace(l = gpbc())) /* skip blank, tab, nl.. */ 432 if (PARLEV > 0) 433 chrsave(l); 434 pushback(l); 435 record(paren, PARLEV++); 436 break; 437 438 case RPAREN: 439 if (--PARLEV > 0) 440 chrsave(t); 441 else { /* end of argument list */ 442 chrsave(EOS); 443 444 if (sp == STACKMAX) 445 errx(1, "internal stack overflow"); 446 447 eval((const char **) mstack+fp+1, sp-fp, 448 CALTYP, TRACESTATUS); 449 450 ep = PREVEP; /* flush strspace */ 451 sp = PREVSP; /* previous sp.. */ 452 fp = PREVFP; /* rewind stack...*/ 453 } 454 break; 455 456 case COMMA: 457 if (PARLEV == 1) { 458 chrsave(EOS); /* new argument */ 459 while (isspace(l = gpbc())) 460 ; 461 pushback(l); 462 pushs(ep); 463 } else 464 chrsave(t); 465 break; 466 467 default: 468 if (LOOK_AHEAD(t, scommt)) { 469 char *p; 470 for (p = scommt; *p; p++) 471 chrsave(*p); 472 for(;;) { 473 t = gpbc(); 474 if (LOOK_AHEAD(t, ecommt)) { 475 for (p = ecommt; *p; p++) 476 chrsave(*p); 477 break; 478 } 479 if (t == EOF) 480 break; 481 CHRSAVE(t); 482 } 483 } else 484 CHRSAVE(t); /* stack the char */ 485 break; 486 } 487 } 488 } 489 490 /* 491 * output string directly, without pushing it for reparses. 492 */ 493 void 494 outputstr(const char *s) 495 { 496 if (sp < 0) 497 reallyoutputstr(s); 498 else 499 while (*s) 500 CHRSAVE(*s++); 501 } 502 503 void 504 reallyoutputstr(const char *s) 505 { 506 if (synch_lines) { 507 while (*s) { 508 fputc(*s, active); 509 if (*s++ == '\n') { 510 infile[ilevel].synch_lineno++; 511 if (infile[ilevel].synch_lineno != 512 infile[ilevel].lineno) 513 do_emit_synchline(); 514 } 515 } 516 } else 517 fputs(s, active); 518 } 519 520 void 521 reallyputchar(int c) 522 { 523 putc(c, active); 524 if (synch_lines && c == '\n') { 525 infile[ilevel].synch_lineno++; 526 if (infile[ilevel].synch_lineno != infile[ilevel].lineno) 527 do_emit_synchline(); 528 } 529 } 530 531 /* 532 * build an input token.. 533 * consider only those starting with _ or A-Za-z. 534 */ 535 static ndptr 536 inspect(int c, char *tp) 537 { 538 char *name = tp; 539 char *etp = tp+MAXTOK; 540 ndptr p; 541 542 *tp++ = c; 543 544 while ((isalnum(c = gpbc()) || c == '_') && tp < etp) 545 *tp++ = c; 546 if (c != EOF) 547 PUSHBACK(c); 548 *tp = EOS; 549 /* token is too long, it won't match anything, but it can still 550 * be output. */ 551 if (tp == ep) { 552 outputstr(name); 553 while (isalnum(c = gpbc()) || c == '_') { 554 if (sp < 0) 555 reallyputchar(c); 556 else 557 CHRSAVE(c); 558 } 559 *name = EOS; 560 return NULL; 561 } 562 563 p = ohash_find(¯os, ohash_qlookupi(¯os, name, (const char **)&tp)); 564 if (p == NULL) 565 return NULL; 566 if (macro_getdef(p) == NULL) 567 return NULL; 568 return p; 569 } 570 571 /* 572 * initkwds - initialise m4 keywords as fast as possible. 573 * This very similar to install, but without certain overheads, 574 * such as calling lookup. Malloc is not used for storing the 575 * keyword strings, since we simply use the static pointers 576 * within keywrds block. 577 */ 578 static void 579 initkwds(void) 580 { 581 unsigned int type; 582 int i; 583 584 for (i = 0; i < MAXKEYS; i++) { 585 type = keywrds[i].ktyp & TYPEMASK; 586 if ((keywrds[i].ktyp & NOARGS) == 0) 587 type |= NEEDARGS; 588 setup_builtin(keywrds[i].knam, type); 589 } 590 } 591 592 static void 593 record(struct position *t, int lev) 594 { 595 if (lev < MAXRECORD) { 596 t[lev].name = CURRENT_NAME; 597 t[lev].line = CURRENT_LINE; 598 } 599 } 600 601 static void 602 dump_stack(struct position *t, int lev) 603 { 604 int i; 605 606 for (i = 0; i < lev; i++) { 607 if (i == MAXRECORD) { 608 fprintf(stderr, " ...\n"); 609 break; 610 } 611 fprintf(stderr, " %s at line %lu\n", 612 t[i].name, t[i].line); 613 } 614 } 615 616 617 static void 618 enlarge_stack(void) 619 { 620 STACKMAX += STACKMAX/2; 621 mstack = xrealloc(mstack, sizeof(stae) * STACKMAX, 622 "Evaluation stack overflow (%lu)", 623 (unsigned long)STACKMAX); 624 sstack = xrealloc(sstack, STACKMAX, 625 "Evaluation stack overflow (%lu)", 626 (unsigned long)STACKMAX); 627 } 628