Home | History | Annotate | Line # | Download | only in dist
main.c revision 1.1.1.6
      1 /*	$NetBSD: main.c,v 1.1.1.6 2015/01/03 22:58:23 christos Exp $	*/
      2 
      3 /* Id: main.c,v 1.54 2014/10/06 22:40:07 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 empty_string[] = "";
     54 static char default_file_prefix[] = "y";
     55 
     56 static char *file_prefix = default_file_prefix;
     57 
     58 char *code_file_name;
     59 char *input_file_name = empty_string;
     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 
     98 #if defined(YYBTYACC)
     99 Value_t *symbol_pval;
    100 char **symbol_destructor;
    101 char **symbol_type_tag;
    102 int locations = 0;	/* default to no position processing */
    103 int backtrack = 0;	/* default is no backtracking */
    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 void
    416 create_file_names(void)
    417 {
    418     size_t len;
    419     const char *defines_suffix;
    420     const char *externs_suffix;
    421     char *prefix;
    422 
    423     prefix = NULL;
    424     defines_suffix = DEFINES_SUFFIX;
    425     externs_suffix = EXTERNS_SUFFIX;
    426 
    427     /* compute the file_prefix from the user provided output_file_name */
    428     if (output_file_name != 0)
    429     {
    430 	if (!(prefix = strstr(output_file_name, OUTPUT_SUFFIX))
    431 	    && (prefix = strstr(output_file_name, ".c")))
    432 	{
    433 	    defines_suffix = ".h";
    434 	    externs_suffix = ".i";
    435 	}
    436     }
    437 
    438     if (prefix != NULL)
    439     {
    440 	len = (size_t) (prefix - output_file_name);
    441 	file_prefix = TMALLOC(char, len + 1);
    442 	NO_SPACE(file_prefix);
    443 	strncpy(file_prefix, output_file_name, len)[len] = 0;
    444     }
    445     else
    446 	len = strlen(file_prefix);
    447 
    448     /* if "-o filename" was not given */
    449     if (output_file_name == 0)
    450     {
    451 	oflag = 1;
    452 	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
    453     }
    454 
    455     if (rflag)
    456     {
    457 	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
    458     }
    459     else
    460 	code_file_name = output_file_name;
    461 
    462     if (dflag)
    463     {
    464 	CREATE_FILE_NAME(defines_file_name, defines_suffix);
    465     }
    466 
    467     if (iflag)
    468     {
    469 	CREATE_FILE_NAME(externs_file_name, externs_suffix);
    470     }
    471 
    472     if (vflag)
    473     {
    474 	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
    475     }
    476 
    477     if (gflag)
    478     {
    479 	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
    480     }
    481 
    482     if (prefix != NULL)
    483     {
    484 	FREE(file_prefix);
    485     }
    486 }
    487 
    488 #if USE_MKSTEMP
    489 static void
    490 close_tmpfiles(void)
    491 {
    492     while (my_tmpfiles != 0)
    493     {
    494 	MY_TMPFILES *next = my_tmpfiles->next;
    495 
    496 	(void)chmod(my_tmpfiles->name, 0644);
    497 	(void)unlink(my_tmpfiles->name);
    498 
    499 	free(my_tmpfiles->name);
    500 	free(my_tmpfiles);
    501 
    502 	my_tmpfiles = next;
    503     }
    504 }
    505 
    506 #ifndef HAVE_MKSTEMP
    507 static int
    508 my_mkstemp(char *temp)
    509 {
    510     int fd;
    511     char *dname;
    512     char *fname;
    513     char *name;
    514 
    515     /*
    516      * Split-up to use tempnam, rather than tmpnam; the latter (like
    517      * mkstemp) is unusable on Windows.
    518      */
    519     if ((fname = strrchr(temp, '/')) != 0)
    520     {
    521 	dname = strdup(temp);
    522 	dname[++fname - temp] = '\0';
    523     }
    524     else
    525     {
    526 	dname = 0;
    527 	fname = temp;
    528     }
    529     if ((name = tempnam(dname, fname)) != 0)
    530     {
    531 	fd = open(name, O_CREAT | O_EXCL | O_RDWR);
    532 	strcpy(temp, name);
    533     }
    534     else
    535     {
    536 	fd = -1;
    537     }
    538 
    539     if (dname != 0)
    540 	free(dname);
    541 
    542     return fd;
    543 }
    544 #define mkstemp(s) my_mkstemp(s)
    545 #endif
    546 
    547 #endif
    548 
    549 /*
    550  * tmpfile() should be adequate, except that it may require special privileges
    551  * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
    552  */
    553 static FILE *
    554 open_tmpfile(const char *label)
    555 {
    556     FILE *result;
    557 #if USE_MKSTEMP
    558     int fd;
    559     const char *tmpdir;
    560     char *name;
    561     const char *mark;
    562 
    563     if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
    564     {
    565 #ifdef P_tmpdir
    566 	tmpdir = P_tmpdir;
    567 #else
    568 	tmpdir = "/tmp";
    569 #endif
    570 	if (access(tmpdir, W_OK) != 0)
    571 	    tmpdir = ".";
    572     }
    573 
    574     name = malloc(strlen(tmpdir) + 10 + strlen(label));
    575 
    576     result = 0;
    577     if (name != 0)
    578     {
    579 	mode_t save_umask = umask(0177);
    580 
    581 	if ((mark = strrchr(label, '_')) == 0)
    582 	    mark = label + strlen(label);
    583 
    584 	sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
    585 	fd = mkstemp(name);
    586 	if (fd >= 0)
    587 	{
    588 	    result = fdopen(fd, "w+");
    589 	    if (result != 0)
    590 	    {
    591 		MY_TMPFILES *item;
    592 
    593 		if (my_tmpfiles == 0)
    594 		{
    595 		    atexit(close_tmpfiles);
    596 		}
    597 
    598 		item = NEW(MY_TMPFILES);
    599 		NO_SPACE(item);
    600 
    601 		item->name = name;
    602 		NO_SPACE(item->name);
    603 
    604 		item->next = my_tmpfiles;
    605 		my_tmpfiles = item;
    606 	    }
    607 	}
    608 	(void)umask(save_umask);
    609     }
    610 #else
    611     result = tmpfile();
    612 #endif
    613 
    614     if (result == 0)
    615 	open_error(label);
    616     return result;
    617 }
    618 
    619 static void
    620 open_files(void)
    621 {
    622     create_file_names();
    623 
    624     if (input_file == 0)
    625     {
    626 	input_file = fopen(input_file_name, "r");
    627 	if (input_file == 0)
    628 	    open_error(input_file_name);
    629     }
    630 
    631     action_file = open_tmpfile("action_file");
    632     text_file = open_tmpfile("text_file");
    633 
    634     if (vflag)
    635     {
    636 	verbose_file = fopen(verbose_file_name, "w");
    637 	if (verbose_file == 0)
    638 	    open_error(verbose_file_name);
    639     }
    640 
    641     if (gflag)
    642     {
    643 	graph_file = fopen(graph_file_name, "w");
    644 	if (graph_file == 0)
    645 	    open_error(graph_file_name);
    646 	fprintf(graph_file, "digraph %s {\n", file_prefix);
    647 	fprintf(graph_file, "\tedge [fontsize=10];\n");
    648 	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
    649 	fprintf(graph_file, "\torientation=landscape;\n");
    650 	fprintf(graph_file, "\trankdir=LR;\n");
    651 	fprintf(graph_file, "\t/*\n");
    652 	fprintf(graph_file, "\tmargin=0.2;\n");
    653 	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
    654 	fprintf(graph_file, "\tratio=auto;\n");
    655 	fprintf(graph_file, "\t*/\n");
    656     }
    657 
    658     if (dflag)
    659     {
    660 	defines_file = fopen(defines_file_name, "w");
    661 	if (defines_file == 0)
    662 	    open_error(defines_file_name);
    663 	union_file = open_tmpfile("union_file");
    664     }
    665 
    666     if (iflag)
    667     {
    668 	externs_file = fopen(externs_file_name, "w");
    669 	if (externs_file == 0)
    670 	    open_error(externs_file_name);
    671     }
    672 
    673     output_file = fopen(output_file_name, "w");
    674     if (output_file == 0)
    675 	open_error(output_file_name);
    676 
    677     if (rflag)
    678     {
    679 	code_file = fopen(code_file_name, "w");
    680 	if (code_file == 0)
    681 	    open_error(code_file_name);
    682     }
    683     else
    684 	code_file = output_file;
    685 }
    686 
    687 int
    688 main(int argc, char *argv[])
    689 {
    690     SRexpect = -1;
    691     RRexpect = -1;
    692     exit_code = EXIT_SUCCESS;
    693 
    694     set_signals();
    695     getargs(argc, argv);
    696     open_files();
    697     reader();
    698     lr0();
    699     lalr();
    700     make_parser();
    701     graph();
    702     finalize_closure();
    703     verbose();
    704     output();
    705     done(exit_code);
    706     /*NOTREACHED */
    707 }
    708