1162271Srwatson/* $OpenBSD: main.c,v 1.81 2012/04/12 17:00:11 espie Exp $ */ 2162271Srwatson/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */ 3172106Srwatson 4162271Srwatson/*- 5162271Srwatson * Copyright (c) 1989, 1993 6162271Srwatson * The Regents of the University of California. All rights reserved. 7162271Srwatson * 8162271Srwatson * This code is derived from software contributed to Berkeley by 9162271Srwatson * Ozan Yigit at York University. 10162271Srwatson * 11162271Srwatson * Redistribution and use in source and binary forms, with or without 12162271Srwatson * modification, are permitted provided that the following conditions 13162271Srwatson * are met: 14162271Srwatson * 1. Redistributions of source code must retain the above copyright 15162271Srwatson * notice, this list of conditions and the following disclaimer. 16162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright 17162271Srwatson * notice, this list of conditions and the following disclaimer in the 18162271Srwatson * documentation and/or other materials provided with the distribution. 19162271Srwatson * 3. Neither the name of the University nor the names of its contributors 20162271Srwatson * may be used to endorse or promote products derived from this software 21162271Srwatson * without specific prior written permission. 22162271Srwatson * 23162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24162271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25162271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26162271Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27162271Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28162271Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29162271Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30162271Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31162271Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32162271Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33162271Srwatson * SUCH DAMAGE. 34172106Srwatson */ 35172106Srwatson 36162271Srwatson/* 37162271Srwatson * main.c 38162271Srwatson * Facility: m4 macro processor 39162271Srwatson * by: oz 40162271Srwatson */ 41162271Srwatson#include <sys/cdefs.h> 42162271Srwatson__FBSDID("$FreeBSD: releng/10.3/usr.bin/m4/main.c 250226 2013-05-03 23:29:38Z jkim $"); 43162271Srwatson 44162271Srwatson#include <assert.h> 45162271Srwatson#include <signal.h> 46172106Srwatson#include <err.h> 47172106Srwatson#include <errno.h> 48172106Srwatson#include <unistd.h> 49172106Srwatson#include <stdio.h> 50172106Srwatson#include <ctype.h> 51172106Srwatson#include <string.h> 52172106Srwatson#include <stddef.h> 53172106Srwatson#include <stdint.h> 54172106Srwatson#include <stdlib.h> 55162271Srwatson#include <ohash.h> 56162271Srwatson#include "mdef.h" 57172106Srwatson#include "stdd.h" 58172106Srwatson#include "extern.h" 59172106Srwatson#include "pathnames.h" 60172106Srwatson 61162271Srwatsonstae *mstack; /* stack of m4 machine */ 62162271Srwatsonchar *sstack; /* shadow stack, for string space extension */ 63172106Srwatsonstatic size_t STACKMAX; /* current maximum size of stack */ 64172106Srwatsonint sp; /* current m4 stack pointer */ 65172106Srwatsonint fp; /* m4 call frame pointer */ 66172106Srwatsonstruct input_file infile[MAXINP];/* input file stack (0=stdin) */ 67172106SrwatsonFILE **outfile; /* diversion array(0=bitbucket)*/ 68172106Srwatsonint maxout; 69172106SrwatsonFILE *active; /* active output file pointer */ 70172106Srwatsonint ilevel = 0; /* input file stack pointer */ 71172106Srwatsonint oindex = 0; /* diversion index.. */ 72172106Srwatsonconst char *null = ""; /* as it says.. just a null.. */ 73172106Srwatsonchar **m4wraps = NULL; /* m4wraps array. */ 74172106Srwatsonint maxwraps = 0; /* size of m4wraps array */ 75172106Srwatsonint wrapindex = 0; /* current offset in m4wraps */ 76172106Srwatsonchar lquote[MAXCCHARS+1] = {LQUOTE}; /* left quote character (`) */ 77172106Srwatsonchar rquote[MAXCCHARS+1] = {RQUOTE}; /* right quote character (') */ 78172106Srwatsonchar scommt[MAXCCHARS+1] = {SCOMMT}; /* start character for comment */ 79172106Srwatsonchar ecommt[MAXCCHARS+1] = {ECOMMT}; /* end character for comment */ 80172106Srwatsonint synch_lines = 0; /* line synchronisation for C preprocessor */ 81172106Srwatsonint prefix_builtins = 0; /* -P option to prefix builtin keywords */ 82172106Srwatson 83172106Srwatsonstruct keyblk { 84172106Srwatson const char *knam; /* keyword name */ 85172106Srwatson int ktyp; /* keyword type */ 86172106Srwatson}; 87172106Srwatson 88172106Srwatsonstatic struct keyblk keywrds[] = { /* m4 keywords to be installed */ 89172106Srwatson { "include", INCLTYPE }, 90172106Srwatson { "sinclude", SINCTYPE }, 91172106Srwatson { "define", DEFITYPE }, 92172106Srwatson { "defn", DEFNTYPE }, 93172106Srwatson { "divert", DIVRTYPE | NOARGS }, 94172106Srwatson { "expr", EXPRTYPE }, 95172106Srwatson { "eval", EXPRTYPE }, 96162271Srwatson { "substr", SUBSTYPE }, 97172106Srwatson { "ifelse", IFELTYPE }, 98162271Srwatson { "ifdef", IFDFTYPE }, 99172106Srwatson { "len", LENGTYPE }, 100162271Srwatson { "incr", INCRTYPE }, 101172106Srwatson { "decr", DECRTYPE }, 102172106Srwatson { "dnl", DNLNTYPE | NOARGS }, 103172106Srwatson { "changequote", CHNQTYPE | NOARGS }, 104172106Srwatson { "changecom", CHNCTYPE | NOARGS }, 105172106Srwatson { "index", INDXTYPE }, 106172106Srwatson#ifdef EXTENDED 107172106Srwatson { "paste", PASTTYPE }, 108172106Srwatson { "spaste", SPASTYPE }, 109172106Srwatson /* Newer extensions, needed to handle gnu-m4 scripts */ 110172106Srwatson { "indir", INDIRTYPE}, 111172106Srwatson { "builtin", BUILTINTYPE}, 112172106Srwatson { "patsubst", PATSTYPE}, 113172106Srwatson { "regexp", REGEXPTYPE}, 114172106Srwatson { "esyscmd", ESYSCMDTYPE}, 115162271Srwatson { "__file__", FILENAMETYPE | NOARGS}, 116172106Srwatson { "__line__", LINETYPE | NOARGS}, 117172106Srwatson#endif 118172106Srwatson { "popdef", POPDTYPE }, 119172106Srwatson { "pushdef", PUSDTYPE }, 120162271Srwatson { "dumpdef", DUMPTYPE | NOARGS }, 121172106Srwatson { "shift", SHIFTYPE | NOARGS }, 122172106Srwatson { "translit", TRNLTYPE }, 123172106Srwatson { "undefine", UNDFTYPE }, 124172106Srwatson { "undivert", UNDVTYPE | NOARGS }, 125172106Srwatson { "divnum", DIVNTYPE | NOARGS }, 126172106Srwatson { "maketemp", MKTMTYPE }, 127172106Srwatson { "mkstemp", MKTMTYPE }, 128172106Srwatson { "errprint", ERRPTYPE | NOARGS }, 129172106Srwatson { "m4wrap", M4WRTYPE | NOARGS }, 130172106Srwatson { "m4exit", EXITTYPE | NOARGS }, 131172106Srwatson { "syscmd", SYSCTYPE }, 132172106Srwatson { "sysval", SYSVTYPE | NOARGS }, 133172106Srwatson { "traceon", TRACEONTYPE | NOARGS }, 134172106Srwatson { "traceoff", TRACEOFFTYPE | NOARGS }, 135162271Srwatson 136172106Srwatson#if defined(unix) || defined(__unix__) 137172106Srwatson { "unix", SELFTYPE | NOARGS }, 138172106Srwatson#else 139172106Srwatson#ifdef vms 140162271Srwatson { "vms", SELFTYPE | NOARGS }, 141172106Srwatson#endif 142172106Srwatson#endif 143172106Srwatson}; 144172106Srwatson 145172106Srwatson#define MAXKEYS (sizeof(keywrds)/sizeof(struct keyblk)) 146172106Srwatson 147172106Srwatson#define MAXRECORD 50 148172106Srwatsonstatic struct position { 149172106Srwatson char *name; 150172106Srwatson unsigned long line; 151172106Srwatson} quotes[MAXRECORD], paren[MAXRECORD]; 152172106Srwatson 153172106Srwatsonstatic void record(struct position *, int); 154172106Srwatsonstatic void dump_stack(struct position *, int); 155162271Srwatson 156172106Srwatsonstatic void macro(void); 157172106Srwatsonstatic void initkwds(void); 158172106Srwatsonstatic ndptr inspect(int, char *); 159172106Srwatsonstatic int do_look_ahead(int, const char *); 160162271Srwatsonstatic void reallyoutputstr(const char *); 161172106Srwatsonstatic void reallyputchar(int); 162172106Srwatson 163172106Srwatsonstatic void enlarge_stack(void); 164172106Srwatson 165172106Srwatsonint main(int, char *[]); 166172106Srwatson 167172106Srwatsonint exit_code = 0; 168172106Srwatson 169172106Srwatsonint 170172106Srwatsonmain(int argc, char *argv[]) 171172106Srwatson{ 172172106Srwatson int c; 173172106Srwatson int n; 174172106Srwatson char *p; 175162271Srwatson 176172106Srwatson if (signal(SIGINT, SIG_IGN) != SIG_IGN) 177172106Srwatson signal(SIGINT, onintr); 178172106Srwatson 179162271Srwatson init_macros(); 180172106Srwatson initspaces(); 181172106Srwatson STACKMAX = INITSTACKMAX; 182172106Srwatson 183162271Srwatson mstack = (stae *)xalloc(sizeof(stae) * STACKMAX, NULL); 184172106Srwatson sstack = (char *)xalloc(STACKMAX, NULL); 185162271Srwatson 186172106Srwatson maxout = 0; 187172106Srwatson outfile = NULL; 188172106Srwatson resizedivs(MAXOUT); 189162271Srwatson 190172106Srwatson while ((c = getopt(argc, argv, "gst:d:D:U:o:I:P")) != -1) 191172106Srwatson switch(c) { 192172106Srwatson 193162271Srwatson case 'D': /* define something..*/ 194162271Srwatson for (p = optarg; *p; p++) 195 if (*p == '=') 196 break; 197 if (*p) 198 *p++ = EOS; 199 dodefine(optarg, p); 200 break; 201 case 'I': 202 addtoincludepath(optarg); 203 break; 204 case 'P': 205 prefix_builtins = 1; 206 break; 207 case 'U': /* undefine... */ 208 macro_popdef(optarg); 209 break; 210 case 'g': 211 mimic_gnu = 1; 212 break; 213 case 'd': 214 set_trace_flags(optarg); 215 break; 216 case 's': 217 synch_lines = 1; 218 break; 219 case 't': 220 mark_traced(optarg, 1); 221 break; 222 case 'o': 223 trace_file(optarg); 224 break; 225 case '?': 226 usage(); 227 } 228 229 argc -= optind; 230 argv += optind; 231 232 initkwds(); 233 if (mimic_gnu) 234 setup_builtin("format", FORMATTYPE); 235 236 active = stdout; /* default active output */ 237 bbase[0] = bufbase; 238 if (!argc) { 239 sp = -1; /* stack pointer initialized */ 240 fp = 0; /* frame pointer initialized */ 241 set_input(infile+0, stdin, "stdin"); 242 /* default input (naturally) */ 243 macro(); 244 } else 245 for (; argc--; ++argv) { 246 p = *argv; 247 if (p[0] == '-' && p[1] == EOS) 248 set_input(infile, stdin, "stdin"); 249 else if (fopen_trypath(infile, p) == NULL) 250 err(1, "%s", p); 251 sp = -1; 252 fp = 0; 253 macro(); 254 release_input(infile); 255 } 256 257 if (wrapindex) { 258 int i; 259 260 ilevel = 0; /* in case m4wrap includes.. */ 261 bufbase = bp = buf; /* use the entire buffer */ 262 if (mimic_gnu) { 263 while (wrapindex != 0) { 264 for (i = 0; i < wrapindex; i++) 265 pbstr(m4wraps[i]); 266 wrapindex =0; 267 macro(); 268 } 269 } else { 270 for (i = 0; i < wrapindex; i++) { 271 pbstr(m4wraps[i]); 272 macro(); 273 } 274 } 275 } 276 277 if (active != stdout) 278 active = stdout; /* reset output just in case */ 279 for (n = 1; n < maxout; n++) /* default wrap-up: undivert */ 280 if (outfile[n] != NULL) 281 getdiv(n); 282 /* remove bitbucket if used */ 283 if (outfile[0] != NULL) { 284 (void) fclose(outfile[0]); 285 } 286 287 return exit_code; 288} 289 290/* 291 * Look ahead for `token'. 292 * (on input `t == token[0]') 293 * Used for comment and quoting delimiters. 294 * Returns 1 if `token' present; copied to output. 295 * 0 if `token' not found; all characters pushed back 296 */ 297static int 298do_look_ahead(int t, const char *token) 299{ 300 int i; 301 302 assert((unsigned char)t == (unsigned char)token[0]); 303 304 for (i = 1; *++token; i++) { 305 t = gpbc(); 306 if (t == EOF || (unsigned char)t != (unsigned char)*token) { 307 pushback(t); 308 while (--i) 309 pushback(*--token); 310 return 0; 311 } 312 } 313 return 1; 314} 315 316#define LOOK_AHEAD(t, token) (t != EOF && \ 317 (unsigned char)(t)==(unsigned char)(token)[0] && \ 318 do_look_ahead(t,token)) 319 320/* 321 * macro - the work horse.. 322 */ 323static void 324macro(void) 325{ 326 char token[MAXTOK+1]; 327 int t, l; 328 ndptr p; 329 int nlpar; 330 331 cycle { 332 t = gpbc(); 333 334 if (LOOK_AHEAD(t,lquote)) { /* strip quotes */ 335 nlpar = 0; 336 record(quotes, nlpar++); 337 /* 338 * Opening quote: scan forward until matching 339 * closing quote has been found. 340 */ 341 do { 342 343 l = gpbc(); 344 if (LOOK_AHEAD(l,rquote)) { 345 if (--nlpar > 0) 346 outputstr(rquote); 347 } else if (LOOK_AHEAD(l,lquote)) { 348 record(quotes, nlpar++); 349 outputstr(lquote); 350 } else if (l == EOF) { 351 if (nlpar == 1) 352 warnx("unclosed quote:"); 353 else 354 warnx("%d unclosed quotes:", nlpar); 355 dump_stack(quotes, nlpar); 356 exit(1); 357 } else { 358 if (nlpar > 0) { 359 if (sp < 0) 360 reallyputchar(l); 361 else 362 CHRSAVE(l); 363 } 364 } 365 } 366 while (nlpar != 0); 367 } else if (sp < 0 && LOOK_AHEAD(t, scommt)) { 368 reallyoutputstr(scommt); 369 370 for(;;) { 371 t = gpbc(); 372 if (LOOK_AHEAD(t, ecommt)) { 373 reallyoutputstr(ecommt); 374 break; 375 } 376 if (t == EOF) 377 break; 378 reallyputchar(t); 379 } 380 } else if (t == '_' || isalpha(t)) { 381 p = inspect(t, token); 382 if (p != NULL) 383 pushback(l = gpbc()); 384 if (p == NULL || (l != LPAREN && 385 (macro_getdef(p)->type & NEEDARGS) != 0)) 386 outputstr(token); 387 else { 388 /* 389 * real thing.. First build a call frame: 390 */ 391 pushf(fp); /* previous call frm */ 392 pushf(macro_getdef(p)->type); /* type of the call */ 393 pushf(is_traced(p)); 394 pushf(0); /* parenthesis level */ 395 fp = sp; /* new frame pointer */ 396 /* 397 * now push the string arguments: 398 */ 399 pushs1(macro_getdef(p)->defn); /* defn string */ 400 pushs1((char *)macro_name(p)); /* macro name */ 401 pushs(ep); /* start next..*/ 402 403 if (l != LPAREN && PARLEV == 0) { 404 /* no bracks */ 405 chrsave(EOS); 406 407 if (sp == (int)STACKMAX) 408 errx(1, "internal stack overflow"); 409 eval((const char **) mstack+fp+1, 2, 410 CALTYP, TRACESTATUS); 411 412 ep = PREVEP; /* flush strspace */ 413 sp = PREVSP; /* previous sp.. */ 414 fp = PREVFP; /* rewind stack...*/ 415 } 416 } 417 } else if (t == EOF) { 418 if (sp > -1 && ilevel <= 0) { 419 warnx( "unexpected end of input, unclosed parenthesis:"); 420 dump_stack(paren, PARLEV); 421 exit(1); 422 } 423 if (ilevel <= 0) 424 break; /* all done thanks.. */ 425 release_input(infile+ilevel--); 426 emit_synchline(); 427 bufbase = bbase[ilevel]; 428 continue; 429 } else if (sp < 0) { /* not in a macro at all */ 430 reallyputchar(t); /* output directly.. */ 431 } 432 433 else switch(t) { 434 435 case LPAREN: 436 if (PARLEV > 0) 437 chrsave(t); 438 while (isspace(l = gpbc())) /* skip blank, tab, nl.. */ 439 if (PARLEV > 0) 440 chrsave(l); 441 pushback(l); 442 record(paren, PARLEV++); 443 break; 444 445 case RPAREN: 446 if (--PARLEV > 0) 447 chrsave(t); 448 else { /* end of argument list */ 449 chrsave(EOS); 450 451 if (sp == (int)STACKMAX) 452 errx(1, "internal stack overflow"); 453 454 eval((const char **) mstack+fp+1, sp-fp, 455 CALTYP, TRACESTATUS); 456 457 ep = PREVEP; /* flush strspace */ 458 sp = PREVSP; /* previous sp.. */ 459 fp = PREVFP; /* rewind stack...*/ 460 } 461 break; 462 463 case COMMA: 464 if (PARLEV == 1) { 465 chrsave(EOS); /* new argument */ 466 while (isspace(l = gpbc())) 467 ; 468 pushback(l); 469 pushs(ep); 470 } else 471 chrsave(t); 472 break; 473 474 default: 475 if (LOOK_AHEAD(t, scommt)) { 476 char *cp; 477 for (cp = scommt; *cp; cp++) 478 chrsave(*cp); 479 for(;;) { 480 t = gpbc(); 481 if (LOOK_AHEAD(t, ecommt)) { 482 for (cp = ecommt; *cp; cp++) 483 chrsave(*cp); 484 break; 485 } 486 if (t == EOF) 487 break; 488 CHRSAVE(t); 489 } 490 } else 491 CHRSAVE(t); /* stack the char */ 492 break; 493 } 494 } 495} 496 497/* 498 * output string directly, without pushing it for reparses. 499 */ 500void 501outputstr(const char *s) 502{ 503 if (sp < 0) 504 reallyoutputstr(s); 505 else 506 while (*s) 507 CHRSAVE(*s++); 508} 509 510void 511reallyoutputstr(const char *s) 512{ 513 if (synch_lines) { 514 while (*s) { 515 fputc(*s, active); 516 if (*s++ == '\n') { 517 infile[ilevel].synch_lineno++; 518 if (infile[ilevel].synch_lineno != 519 infile[ilevel].lineno) 520 do_emit_synchline(); 521 } 522 } 523 } else 524 fputs(s, active); 525} 526 527void 528reallyputchar(int c) 529{ 530 putc(c, active); 531 if (synch_lines && c == '\n') { 532 infile[ilevel].synch_lineno++; 533 if (infile[ilevel].synch_lineno != infile[ilevel].lineno) 534 do_emit_synchline(); 535 } 536} 537 538/* 539 * build an input token.. 540 * consider only those starting with _ or A-Za-z. 541 */ 542static ndptr 543inspect(int c, char *tp) 544{ 545 char *name = tp; 546 char *etp = tp+MAXTOK; 547 ndptr p; 548 549 *tp++ = c; 550 551 while ((isalnum(c = gpbc()) || c == '_') && tp < etp) 552 *tp++ = c; 553 if (c != EOF) 554 PUSHBACK(c); 555 *tp = EOS; 556 /* token is too long, it won't match anything, but it can still 557 * be output. */ 558 if (tp == ep) { 559 outputstr(name); 560 while (isalnum(c = gpbc()) || c == '_') { 561 if (sp < 0) 562 reallyputchar(c); 563 else 564 CHRSAVE(c); 565 } 566 *name = EOS; 567 return NULL; 568 } 569 570 p = ohash_find(¯os, ohash_qlookupi(¯os, name, (const char **)&tp)); 571 if (p == NULL) 572 return NULL; 573 if (macro_getdef(p) == NULL) 574 return NULL; 575 return p; 576} 577 578/* 579 * initkwds - initialise m4 keywords as fast as possible. 580 * This very similar to install, but without certain overheads, 581 * such as calling lookup. Malloc is not used for storing the 582 * keyword strings, since we simply use the static pointers 583 * within keywrds block. 584 */ 585static void 586initkwds(void) 587{ 588 unsigned int type; 589 int i; 590 591 for (i = 0; i < (int)MAXKEYS; i++) { 592 type = keywrds[i].ktyp & TYPEMASK; 593 if ((keywrds[i].ktyp & NOARGS) == 0) 594 type |= NEEDARGS; 595 setup_builtin(keywrds[i].knam, type); 596 } 597} 598 599static void 600record(struct position *t, int lev) 601{ 602 if (lev < MAXRECORD) { 603 t[lev].name = CURRENT_NAME; 604 t[lev].line = CURRENT_LINE; 605 } 606} 607 608static void 609dump_stack(struct position *t, int lev) 610{ 611 int i; 612 613 for (i = 0; i < lev; i++) { 614 if (i == MAXRECORD) { 615 fprintf(stderr, " ...\n"); 616 break; 617 } 618 fprintf(stderr, " %s at line %lu\n", 619 t[i].name, t[i].line); 620 } 621} 622 623 624static void 625enlarge_stack(void) 626{ 627 STACKMAX += STACKMAX/2; 628 mstack = xrealloc(mstack, sizeof(stae) * STACKMAX, 629 "Evaluation stack overflow (%lu)", 630 (unsigned long)STACKMAX); 631 sstack = xrealloc(sstack, STACKMAX, 632 "Evaluation stack overflow (%lu)", 633 (unsigned long)STACKMAX); 634} 635