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