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