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