1 /* $Id: main.c,v 1.57 2016/12/02 18:44:44 tom Exp $ */ 2 3 #include <signal.h> 4 #ifndef _WIN32 5 #include <unistd.h> /* for _exit() */ 6 #else 7 #include <stdlib.h> /* for _exit() */ 8 #endif 9 10 #include "defs.h" 11 12 #ifdef HAVE_MKSTEMP 13 # define USE_MKSTEMP 1 14 #elif defined(HAVE_FCNTL_H) 15 # define USE_MKSTEMP 1 16 # include <fcntl.h> /* for open(), O_EXCL, etc. */ 17 #else 18 # define USE_MKSTEMP 0 19 #endif 20 21 #if USE_MKSTEMP 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 25 typedef struct _my_tmpfiles 26 { 27 struct _my_tmpfiles *next; 28 char *name; 29 } 30 MY_TMPFILES; 31 32 static MY_TMPFILES *my_tmpfiles; 33 #endif /* USE_MKSTEMP */ 34 35 char dflag; 36 char gflag; 37 char iflag; 38 char lflag; 39 static char oflag; 40 char rflag; 41 char sflag; 42 char tflag; 43 char vflag; 44 45 const char *symbol_prefix; 46 const char *myname = "yacc"; 47 48 int lineno; 49 int outline; 50 51 static char empty_string[] = ""; 52 static char default_file_prefix[] = "y"; 53 54 static char *file_prefix = default_file_prefix; 55 56 char *code_file_name; 57 char *input_file_name = empty_string; 58 char *defines_file_name; 59 char *externs_file_name; 60 61 static char *graph_file_name; 62 static char *output_file_name; 63 static char *verbose_file_name; 64 65 FILE *action_file; /* a temp file, used to save actions associated */ 66 /* with rules until the parser is written */ 67 FILE *code_file; /* y.code.c (used when the -r option is specified) */ 68 FILE *defines_file; /* y.tab.h */ 69 FILE *externs_file; /* y.tab.i */ 70 FILE *input_file; /* the input file */ 71 FILE *output_file; /* y.tab.c */ 72 FILE *text_file; /* a temp file, used to save text until all */ 73 /* symbols have been defined */ 74 FILE *union_file; /* a temp file, used to save the union */ 75 /* definition until all symbol have been */ 76 /* defined */ 77 FILE *verbose_file; /* y.output */ 78 FILE *graph_file; /* y.dot */ 79 80 Value_t nitems; 81 Value_t nrules; 82 Value_t nsyms; 83 Value_t ntokens; 84 Value_t nvars; 85 86 Value_t start_symbol; 87 char **symbol_name; 88 char **symbol_pname; 89 Value_t *symbol_value; 90 Value_t *symbol_prec; 91 char *symbol_assoc; 92 93 int pure_parser; 94 int token_table; 95 int error_verbose; 96 97 #if defined(YYBTYACC) 98 Value_t *symbol_pval; 99 char **symbol_destructor; 100 char **symbol_type_tag; 101 int locations = 0; /* default to no position processing */ 102 int backtrack = 0; /* default is no backtracking */ 103 char *initial_action = NULL; 104 #endif 105 106 int exit_code; 107 108 Value_t *ritem; 109 Value_t *rlhs; 110 Value_t *rrhs; 111 Value_t *rprec; 112 Assoc_t *rassoc; 113 Value_t **derives; 114 char *nullable; 115 116 /* 117 * Since fclose() is called via the signal handler, it might die. Don't loop 118 * if there is a problem closing a file. 119 */ 120 #define DO_CLOSE(fp) \ 121 if (fp != 0) { \ 122 FILE *use = fp; \ 123 fp = 0; \ 124 fclose(use); \ 125 } 126 127 static int got_intr = 0; 128 129 void 130 done(int k) 131 { 132 DO_CLOSE(input_file); 133 DO_CLOSE(output_file); 134 if (iflag) 135 DO_CLOSE(externs_file); 136 if (rflag) 137 DO_CLOSE(code_file); 138 139 DO_CLOSE(action_file); 140 DO_CLOSE(defines_file); 141 DO_CLOSE(graph_file); 142 DO_CLOSE(text_file); 143 DO_CLOSE(union_file); 144 DO_CLOSE(verbose_file); 145 146 if (got_intr) 147 _exit(EXIT_FAILURE); 148 149 #ifdef NO_LEAKS 150 if (rflag) 151 DO_FREE(code_file_name); 152 153 if (dflag) 154 DO_FREE(defines_file_name); 155 156 if (iflag) 157 DO_FREE(externs_file_name); 158 159 if (oflag) 160 DO_FREE(output_file_name); 161 162 if (vflag) 163 DO_FREE(verbose_file_name); 164 165 if (gflag) 166 DO_FREE(graph_file_name); 167 168 lr0_leaks(); 169 lalr_leaks(); 170 mkpar_leaks(); 171 mstring_leaks(); 172 output_leaks(); 173 reader_leaks(); 174 #endif 175 176 exit(k); 177 } 178 179 static void 180 onintr(int sig GCC_UNUSED) 181 { 182 got_intr = 1; 183 done(EXIT_FAILURE); 184 } 185 186 static void 187 set_signals(void) 188 { 189 #ifdef SIGINT 190 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 191 signal(SIGINT, onintr); 192 #endif 193 #ifdef SIGTERM 194 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 195 signal(SIGTERM, onintr); 196 #endif 197 #ifdef SIGHUP 198 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 199 signal(SIGHUP, onintr); 200 #endif 201 } 202 203 static void 204 usage(void) 205 { 206 static const char *msg[] = 207 { 208 "" 209 ,"Options:" 210 ," -b file_prefix set filename prefix (default \"y.\")" 211 ," -B create a backtracking parser" 212 ," -d write definitions (" DEFINES_SUFFIX ")" 213 ," -i write interface (y.tab.i)" 214 ," -g write a graphical description" 215 ," -l suppress #line directives" 216 ," -L enable position processing, e.g., \"%locations\"" 217 ," -o output_file (default \"" OUTPUT_SUFFIX "\")" 218 ," -p symbol_prefix set symbol prefix (default \"yy\")" 219 ," -P create a reentrant parser, e.g., \"%pure-parser\"" 220 ," -r produce separate code and table files (y.code.c)" 221 ," -s suppress #define's for quoted names in %token lines" 222 ," -t add debugging support" 223 ," -v write description (y.output)" 224 ," -V show version information and exit" 225 }; 226 unsigned n; 227 228 fflush(stdout); 229 fprintf(stderr, "Usage: %s [options] filename\n", myname); 230 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n) 231 fprintf(stderr, "%s\n", msg[n]); 232 233 exit(1); 234 } 235 236 static void 237 setflag(int ch) 238 { 239 switch (ch) 240 { 241 case 'B': 242 #if defined(YYBTYACC) 243 backtrack = 1; 244 #else 245 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc"); 246 #endif 247 break; 248 249 case 'd': 250 dflag = 1; 251 break; 252 253 case 'g': 254 gflag = 1; 255 break; 256 257 case 'i': 258 iflag = 1; 259 break; 260 261 case 'l': 262 lflag = 1; 263 break; 264 265 case 'L': 266 #if defined(YYBTYACC) 267 locations = 1; 268 #else 269 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc"); 270 #endif 271 break; 272 273 case 'P': 274 pure_parser = 1; 275 break; 276 277 case 'r': 278 rflag = 1; 279 break; 280 281 case 's': 282 sflag = 1; 283 break; 284 285 case 't': 286 tflag = 1; 287 break; 288 289 case 'v': 290 vflag = 1; 291 break; 292 293 case 'V': 294 printf("%s - %s\n", myname, VERSION); 295 exit(EXIT_SUCCESS); 296 297 case 'y': 298 /* noop for bison compatibility. byacc is already designed to be posix 299 * yacc compatible. */ 300 break; 301 302 default: 303 usage(); 304 } 305 } 306 307 static void 308 getargs(int argc, char *argv[]) 309 { 310 int i; 311 char *s; 312 int ch; 313 314 if (argc > 0) 315 myname = argv[0]; 316 317 for (i = 1; i < argc; ++i) 318 { 319 s = argv[i]; 320 if (*s != '-') 321 break; 322 switch (ch = *++s) 323 { 324 case '\0': 325 input_file = stdin; 326 if (i + 1 < argc) 327 usage(); 328 return; 329 330 case '-': 331 ++i; 332 goto no_more_options; 333 334 case 'b': 335 if (*++s) 336 file_prefix = s; 337 else if (++i < argc) 338 file_prefix = argv[i]; 339 else 340 usage(); 341 continue; 342 343 case 'o': 344 if (*++s) 345 output_file_name = s; 346 else if (++i < argc) 347 output_file_name = argv[i]; 348 else 349 usage(); 350 continue; 351 352 case 'p': 353 if (*++s) 354 symbol_prefix = s; 355 else if (++i < argc) 356 symbol_prefix = argv[i]; 357 else 358 usage(); 359 continue; 360 361 default: 362 setflag(ch); 363 break; 364 } 365 366 for (;;) 367 { 368 switch (ch = *++s) 369 { 370 case '\0': 371 goto end_of_option; 372 373 default: 374 setflag(ch); 375 break; 376 } 377 } 378 end_of_option:; 379 } 380 381 no_more_options:; 382 if (i + 1 != argc) 383 usage(); 384 input_file_name = argv[i]; 385 } 386 387 void * 388 allocate(size_t n) 389 { 390 void *p; 391 392 p = NULL; 393 if (n) 394 { 395 p = CALLOC(1, n); 396 NO_SPACE(p); 397 } 398 return (p); 399 } 400 401 #define CREATE_FILE_NAME(dest, suffix) \ 402 dest = alloc_file_name(len, suffix) 403 404 static char * 405 alloc_file_name(size_t len, const char *suffix) 406 { 407 char *result = TMALLOC(char, len + strlen(suffix) + 1); 408 if (result == 0) 409 no_space(); 410 strcpy(result, file_prefix); 411 strcpy(result + len, suffix); 412 return result; 413 } 414 415 static char * 416 find_suffix(char *name, const char *suffix) 417 { 418 size_t len = strlen(name); 419 size_t slen = strlen(suffix); 420 if (len >= slen) 421 { 422 name += len - slen; 423 if (strcmp(name, suffix) == 0) 424 return name; 425 } 426 return NULL; 427 } 428 429 static void 430 create_file_names(void) 431 { 432 size_t len; 433 const char *defines_suffix; 434 const char *externs_suffix; 435 char *suffix; 436 437 suffix = NULL; 438 defines_suffix = DEFINES_SUFFIX; 439 externs_suffix = EXTERNS_SUFFIX; 440 441 /* compute the file_prefix from the user provided output_file_name */ 442 if (output_file_name != 0) 443 { 444 if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX)) 445 && (suffix = find_suffix(output_file_name, ".c"))) 446 { 447 defines_suffix = ".h"; 448 externs_suffix = ".i"; 449 } 450 } 451 452 if (suffix != NULL) 453 { 454 len = (size_t) (suffix - output_file_name); 455 file_prefix = TMALLOC(char, len + 1); 456 NO_SPACE(file_prefix); 457 strncpy(file_prefix, output_file_name, len)[len] = 0; 458 } 459 else 460 len = strlen(file_prefix); 461 462 /* if "-o filename" was not given */ 463 if (output_file_name == 0) 464 { 465 oflag = 1; 466 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX); 467 } 468 469 if (rflag) 470 { 471 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX); 472 } 473 else 474 code_file_name = output_file_name; 475 476 if (dflag) 477 { 478 CREATE_FILE_NAME(defines_file_name, defines_suffix); 479 } 480 481 if (iflag) 482 { 483 CREATE_FILE_NAME(externs_file_name, externs_suffix); 484 } 485 486 if (vflag) 487 { 488 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX); 489 } 490 491 if (gflag) 492 { 493 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX); 494 } 495 496 if (suffix != NULL) 497 { 498 FREE(file_prefix); 499 } 500 } 501 502 #if USE_MKSTEMP 503 static void 504 close_tmpfiles(void) 505 { 506 while (my_tmpfiles != 0) 507 { 508 MY_TMPFILES *next = my_tmpfiles->next; 509 510 (void)chmod(my_tmpfiles->name, 0644); 511 (void)unlink(my_tmpfiles->name); 512 513 free(my_tmpfiles->name); 514 free(my_tmpfiles); 515 516 my_tmpfiles = next; 517 } 518 } 519 520 #ifndef HAVE_MKSTEMP 521 static int 522 my_mkstemp(char *temp) 523 { 524 int fd; 525 char *dname; 526 char *fname; 527 char *name; 528 529 /* 530 * Split-up to use tempnam, rather than tmpnam; the latter (like 531 * mkstemp) is unusable on Windows. 532 */ 533 if ((fname = strrchr(temp, '/')) != 0) 534 { 535 dname = strdup(temp); 536 dname[++fname - temp] = '\0'; 537 } 538 else 539 { 540 dname = 0; 541 fname = temp; 542 } 543 if ((name = tempnam(dname, fname)) != 0) 544 { 545 fd = open(name, O_CREAT | O_EXCL | O_RDWR); 546 strcpy(temp, name); 547 } 548 else 549 { 550 fd = -1; 551 } 552 553 if (dname != 0) 554 free(dname); 555 556 return fd; 557 } 558 #define mkstemp(s) my_mkstemp(s) 559 #endif 560 561 #endif 562 563 /* 564 * tmpfile() should be adequate, except that it may require special privileges 565 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory. 566 */ 567 static FILE * 568 open_tmpfile(const char *label) 569 { 570 #define MY_FMT "%s/%.*sXXXXXX" 571 FILE *result; 572 #if USE_MKSTEMP 573 int fd; 574 const char *tmpdir; 575 char *name; 576 const char *mark; 577 578 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) 579 { 580 #ifdef P_tmpdir 581 tmpdir = P_tmpdir; 582 #else 583 tmpdir = "/tmp"; 584 #endif 585 if (access(tmpdir, W_OK) != 0) 586 tmpdir = "."; 587 } 588 589 /* The size of the format is guaranteed to be longer than the result from 590 * printing empty strings with it; this calculation accounts for the 591 * string-lengths as well. 592 */ 593 name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label)); 594 595 result = 0; 596 if (name != 0) 597 { 598 mode_t save_umask = umask(0177); 599 600 if ((mark = strrchr(label, '_')) == 0) 601 mark = label + strlen(label); 602 603 sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label); 604 fd = mkstemp(name); 605 if (fd >= 0) 606 { 607 result = fdopen(fd, "w+"); 608 if (result != 0) 609 { 610 MY_TMPFILES *item; 611 612 if (my_tmpfiles == 0) 613 { 614 atexit(close_tmpfiles); 615 } 616 617 item = NEW(MY_TMPFILES); 618 NO_SPACE(item); 619 620 item->name = name; 621 NO_SPACE(item->name); 622 623 item->next = my_tmpfiles; 624 my_tmpfiles = item; 625 } 626 } 627 (void)umask(save_umask); 628 } 629 #else 630 result = tmpfile(); 631 #endif 632 633 if (result == 0) 634 open_error(label); 635 return result; 636 #undef MY_FMT 637 } 638 639 static void 640 open_files(void) 641 { 642 create_file_names(); 643 644 if (input_file == 0) 645 { 646 input_file = fopen(input_file_name, "r"); 647 if (input_file == 0) 648 open_error(input_file_name); 649 } 650 651 action_file = open_tmpfile("action_file"); 652 text_file = open_tmpfile("text_file"); 653 654 if (vflag) 655 { 656 verbose_file = fopen(verbose_file_name, "w"); 657 if (verbose_file == 0) 658 open_error(verbose_file_name); 659 } 660 661 if (gflag) 662 { 663 graph_file = fopen(graph_file_name, "w"); 664 if (graph_file == 0) 665 open_error(graph_file_name); 666 fprintf(graph_file, "digraph %s {\n", file_prefix); 667 fprintf(graph_file, "\tedge [fontsize=10];\n"); 668 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n"); 669 fprintf(graph_file, "\torientation=landscape;\n"); 670 fprintf(graph_file, "\trankdir=LR;\n"); 671 fprintf(graph_file, "\t/*\n"); 672 fprintf(graph_file, "\tmargin=0.2;\n"); 673 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n"); 674 fprintf(graph_file, "\tratio=auto;\n"); 675 fprintf(graph_file, "\t*/\n"); 676 } 677 678 if (dflag) 679 { 680 defines_file = fopen(defines_file_name, "w"); 681 if (defines_file == 0) 682 open_error(defines_file_name); 683 union_file = open_tmpfile("union_file"); 684 } 685 686 if (iflag) 687 { 688 externs_file = fopen(externs_file_name, "w"); 689 if (externs_file == 0) 690 open_error(externs_file_name); 691 } 692 693 output_file = fopen(output_file_name, "w"); 694 if (output_file == 0) 695 open_error(output_file_name); 696 697 if (rflag) 698 { 699 code_file = fopen(code_file_name, "w"); 700 if (code_file == 0) 701 open_error(code_file_name); 702 } 703 else 704 code_file = output_file; 705 } 706 707 int 708 main(int argc, char *argv[]) 709 { 710 SRexpect = -1; 711 RRexpect = -1; 712 exit_code = EXIT_SUCCESS; 713 714 set_signals(); 715 getargs(argc, argv); 716 open_files(); 717 reader(); 718 lr0(); 719 lalr(); 720 make_parser(); 721 graph(); 722 finalize_closure(); 723 verbose(); 724 output(); 725 done(exit_code); 726 /*NOTREACHED */ 727 } 728