Home | History | Annotate | Line # | Download | only in dist
main.c revision 1.20.8.1
      1 /*	$NetBSD: main.c,v 1.20.8.1 2025/08/02 05:20:54 perseant Exp $	*/
      2 
      3 #include "defs.h"
      4 
      5 #include <sys/cdefs.h>
      6 __RCSID("$NetBSD: main.c,v 1.20.8.1 2025/08/02 05:20:54 perseant Exp $");
      7 /* Id: main.c,v 1.74 2023/05/11 07:51:36 tom Exp  */
      8 
      9 #include <signal.h>
     10 #if !defined(_WIN32) || defined(__MINGW32__)
     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     DO_FREE(input_file_name);
    158 
    159     if (rflag)
    160 	DO_FREE(code_file_name);
    161 
    162     if (dflag && !dflag2)
    163 	DO_FREE(defines_file_name);
    164 
    165     if (iflag)
    166 	DO_FREE(externs_file_name);
    167 
    168     if (oflag)
    169 	DO_FREE(output_file_name);
    170 
    171     if (vflag)
    172 	DO_FREE(verbose_file_name);
    173 
    174     if (gflag)
    175 	DO_FREE(graph_file_name);
    176 
    177     lr0_leaks();
    178     lalr_leaks();
    179     mkpar_leaks();
    180     mstring_leaks();
    181     output_leaks();
    182     reader_leaks();
    183 #endif
    184 
    185     exit(k);
    186 }
    187 
    188 static void
    189 onintr(int sig GCC_UNUSED)
    190 {
    191     got_intr = 1;
    192     done(EXIT_FAILURE);
    193 }
    194 
    195 static void
    196 set_signals(void)
    197 {
    198 #ifdef SIGINT
    199     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    200 	signal(SIGINT, onintr);
    201 #endif
    202 #ifdef SIGTERM
    203     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    204 	signal(SIGTERM, onintr);
    205 #endif
    206 #ifdef SIGHUP
    207     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    208 	signal(SIGHUP, onintr);
    209 #endif
    210 }
    211 
    212 #define SIZEOF(v) (sizeof(v) / sizeof((v)[0]))
    213 
    214 /*
    215  * Long options are provided only as a compatibility aid for scripters.
    216  */
    217 /* *INDENT-OFF* */
    218 static const struct {
    219     const char long_opt[16];
    220     const char yacc_arg;
    221     const char yacc_opt;
    222 } long_opts[] = {
    223     { "defines",     1, 'H' },
    224     { "file-prefix", 1, 'b' },
    225     { "graph",       0, 'g' },
    226     { "help",        0, 'h' },
    227     { "name-prefix", 1, 'p' },
    228     { "no-lines",    0, 'l' },
    229     { "output",      1, 'o' },
    230     { "version",     0, 'V' }
    231 };
    232 /* *INDENT-ON* */
    233 
    234 /*
    235  * Usage-message is designed for 80 columns, with some unknowns.  Account for
    236  * those in the maximum width so that the usage message uses no relocatable
    237  * pointers.
    238  */
    239 #define USAGE_COLS (80 + sizeof(DEFINES_SUFFIX) + sizeof(OUTPUT_SUFFIX))
    240 
    241 static void
    242 usage(void)
    243 {
    244     /* *INDENT-OFF* */
    245     static const char msg[][USAGE_COLS] =
    246     {
    247 	{ "  -b file_prefix        set filename prefix (default \"y.\")" },
    248 	{ "  -B                    create a backtracking parser" },
    249 	{ "  -d                    write definitions (" DEFINES_SUFFIX ")" },
    250 	{ "  -h                    print this help-message" },
    251 	{ "  -H defines_file       write definitions to defines_file" },
    252 	{ "  -i                    write interface (y.tab.i)" },
    253 	{ "  -g                    write a graphical description" },
    254 	{ "  -l                    suppress #line directives" },
    255 	{ "  -L                    enable position processing, e.g., \"%locations\"" },
    256 	{ "  -o output_file        (default \"" OUTPUT_SUFFIX "\")" },
    257 	{ "  -p symbol_prefix      set symbol prefix (default \"yy\")" },
    258 	{ "  -P                    create a reentrant parser, e.g., \"%pure-parser\"" },
    259 	{ "  -r                    produce separate code and table files (y.code.c)" },
    260 	{ "  -s                    suppress #define's for quoted names in %token lines" },
    261 	{ "  -t                    add debugging support" },
    262 	{ "  -v                    write description (y.output)" },
    263 	{ "  -V                    show version information and exit" },
    264     };
    265     /* *INDENT-ON* */
    266     unsigned n;
    267 
    268     fflush(stdout);
    269     fprintf(stderr, "Usage: %s [options] filename\n", myname);
    270 
    271     fprintf(stderr, "\nOptions:\n");
    272     for (n = 0; n < SIZEOF(msg); ++n)
    273     {
    274 	fprintf(stderr, "%s\n", msg[n]);
    275     }
    276 
    277     fprintf(stderr, "\nLong options:\n");
    278     for (n = 0; n < SIZEOF(long_opts); ++n)
    279     {
    280 	fprintf(stderr, "  --%-20s-%c\n",
    281 		long_opts[n].long_opt,
    282 		long_opts[n].yacc_opt);
    283     }
    284 
    285     exit(EXIT_FAILURE);
    286 }
    287 
    288 static void
    289 invalid_option(const char *option)
    290 {
    291     fprintf(stderr, "invalid option: %s\n", option);
    292     usage();
    293 }
    294 
    295 static void
    296 setflag(int ch)
    297 {
    298     switch (ch)
    299     {
    300     case 'B':
    301 #if defined(YYBTYACC)
    302 	backtrack = 1;
    303 #else
    304 	unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
    305 #endif
    306 	break;
    307 
    308     case 'd':
    309 	dflag = 1;
    310 	dflag2 = 0;
    311 	break;
    312 
    313     case 'g':
    314 	gflag = 1;
    315 	break;
    316 
    317     case 'i':
    318 	iflag = 1;
    319 	break;
    320 
    321     case 'l':
    322 	lflag = 1;
    323 	break;
    324 
    325     case 'L':
    326 #if defined(YYBTYACC)
    327 	locations = 1;
    328 #else
    329 	unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
    330 #endif
    331 	break;
    332 
    333     case 'P':
    334 	pure_parser = 1;
    335 	break;
    336 
    337     case 'r':
    338 	rflag = 1;
    339 	break;
    340 
    341     case 's':
    342 	sflag = 1;
    343 	break;
    344 
    345     case 't':
    346 	tflag = 1;
    347 	break;
    348 
    349     case 'v':
    350 	vflag = 1;
    351 	break;
    352 
    353     case 'V':
    354 	printf("%s - %s\n", myname, VERSION);
    355 	exit(EXIT_SUCCESS);
    356 
    357     case 'y':
    358 	/* noop for bison compatibility. byacc is already designed to be posix
    359 	 * yacc compatible. */
    360 	break;
    361 
    362     default:
    363 	usage();
    364     }
    365 }
    366 
    367 static void
    368 getargs(int argc, char *argv[])
    369 {
    370     int i;
    371 #ifdef HAVE_GETOPT
    372     int ch;
    373 #endif
    374 
    375     /*
    376      * Map bison's long-options into yacc short options.
    377      */
    378     for (i = 1; i < argc; ++i)
    379     {
    380 	char *a = argv[i];
    381 
    382 	if (!strncmp(a, "--", 2))
    383 	{
    384 	    char *eqls;
    385 	    size_t lc;
    386 	    size_t len;
    387 
    388 	    if ((len = strlen(a)) == 2)
    389 		break;
    390 
    391 	    if ((eqls = strchr(a, '=')) != NULL)
    392 	    {
    393 		len = (size_t)(eqls - a);
    394 		if (len == 0 || eqls[1] == '\0')
    395 		    invalid_option(a);
    396 	    }
    397 
    398 	    for (lc = 0; lc < SIZEOF(long_opts); ++lc)
    399 	    {
    400 		if (!strncmp(long_opts[lc].long_opt, a + 2, len - 2))
    401 		{
    402 		    if (eqls != NULL && !long_opts[lc].yacc_arg)
    403 			invalid_option(a);
    404 		    *a++ = '-';
    405 		    *a++ = long_opts[lc].yacc_opt;
    406 		    *a = '\0';
    407 		    if (eqls)
    408 		    {
    409 			while ((*a++ = *++eqls) != '\0') /* empty */ ;
    410 		    }
    411 		    break;
    412 		}
    413 	    }
    414 	    if (!strncmp(a, "--", 2))
    415 		invalid_option(a);
    416 	}
    417     }
    418 
    419 #ifdef HAVE_GETOPT
    420     if (argc > 0)
    421 	myname = argv[0];
    422 
    423     while ((ch = getopt(argc, argv, "Bb:dghH:ilLo:Pp:rstVvy")) != -1)
    424     {
    425 	switch (ch)
    426 	{
    427 	case 'b':
    428 	    file_prefix = optarg;
    429 	    break;
    430 	case 'h':
    431 	    usage();
    432 	    break;
    433 	case 'H':
    434 	    dflag = dflag2 = 1;
    435 	    defines_file_name = optarg;
    436 	    break;
    437 	case 'o':
    438 	    output_file_name = optarg;
    439 	    explicit_file_name = 1;
    440 	    break;
    441 	case 'p':
    442 	    symbol_prefix = optarg;
    443 	    break;
    444 	default:
    445 	    setflag(ch);
    446 	    break;
    447 	}
    448     }
    449     if ((i = optind) < argc)
    450     {
    451 	/* getopt handles "--" specially, while we handle "-" specially */
    452 	if (!strcmp(argv[i], "-"))
    453 	{
    454 	    if ((i + 1) < argc)
    455 		usage();
    456 	    input_file = stdin;
    457 	    return;
    458 	}
    459     }
    460 #else
    461     char *s;
    462     int ch;
    463 
    464     if (argc > 0)
    465 	myname = argv[0];
    466 
    467     for (i = 1; i < argc; ++i)
    468     {
    469 	s = argv[i];
    470 	if (*s != '-')
    471 	    break;
    472 	switch (ch = *++s)
    473 	{
    474 	case '\0':
    475 	    input_file = stdin;
    476 	    if (i + 1 < argc)
    477 		usage();
    478 	    return;
    479 
    480 	case '-':
    481 	    ++i;
    482 	    goto no_more_options;
    483 
    484 	case 'b':
    485 	    if (*++s)
    486 		file_prefix = s;
    487 	    else if (++i < argc)
    488 		file_prefix = argv[i];
    489 	    else
    490 		usage();
    491 	    continue;
    492 
    493 	case 'H':
    494 	    dflag = dflag2 = 1;
    495 	    if (*++s)
    496 		defines_file_name = s;
    497 	    else if (++i < argc)
    498 		defines_file_name = argv[i];
    499 	    else
    500 		usage();
    501 	    continue;
    502 
    503 	case 'o':
    504 	    if (*++s)
    505 		output_file_name = s;
    506 	    else if (++i < argc)
    507 		output_file_name = argv[i];
    508 	    else
    509 		usage();
    510 	    explicit_file_name = 1;
    511 	    continue;
    512 
    513 	case 'p':
    514 	    if (*++s)
    515 		symbol_prefix = s;
    516 	    else if (++i < argc)
    517 		symbol_prefix = argv[i];
    518 	    else
    519 		usage();
    520 	    continue;
    521 
    522 	default:
    523 	    setflag(ch);
    524 	    break;
    525 	}
    526 
    527 	for (;;)
    528 	{
    529 	    switch (ch = *++s)
    530 	    {
    531 	    case '\0':
    532 		goto end_of_option;
    533 
    534 	    default:
    535 		setflag(ch);
    536 		break;
    537 	    }
    538 	}
    539       end_of_option:;
    540     }
    541 
    542   no_more_options:
    543 
    544 #endif /* HAVE_GETOPT */
    545     if (i + 1 != argc)
    546 	usage();
    547     input_file_name_len = strlen(argv[i]);
    548     input_file_name = TMALLOC(char, input_file_name_len + 1);
    549     NO_SPACE(input_file_name);
    550     strcpy(input_file_name, argv[i]);
    551 }
    552 
    553 void *
    554 allocate(size_t n)
    555 {
    556     void *p;
    557 
    558     p = NULL;
    559     if (n)
    560     {
    561 	p = CALLOC(1, n);
    562 	NO_SPACE(p);
    563     }
    564     return (p);
    565 }
    566 
    567 #define CREATE_FILE_NAME(dest, suffix) \
    568 	dest = alloc_file_name(len, suffix)
    569 
    570 static char *
    571 alloc_file_name(size_t len, const char *suffix)
    572 {
    573     char *result = TMALLOC(char, len + strlen(suffix) + 1);
    574     if (result == NULL)
    575 	on_error();
    576     strcpy(result, file_prefix);
    577     strcpy(result + len, suffix);
    578     return result;
    579 }
    580 
    581 static char *
    582 find_suffix(char *name, const char *suffix)
    583 {
    584     size_t len = strlen(name);
    585     size_t slen = strlen(suffix);
    586     if (len >= slen)
    587     {
    588 	name += len - slen;
    589 	if (strcmp(name, suffix) == 0)
    590 	    return name;
    591     }
    592     return NULL;
    593 }
    594 
    595 static void
    596 create_file_names(void)
    597 {
    598     size_t len;
    599     const char *defines_suffix;
    600     const char *externs_suffix;
    601     char *suffix;
    602 
    603     suffix = NULL;
    604     defines_suffix = DEFINES_SUFFIX;
    605     externs_suffix = EXTERNS_SUFFIX;
    606 
    607     /* compute the file_prefix from the user provided output_file_name */
    608     if (output_file_name != 0)
    609     {
    610 	if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
    611 	    && (suffix = find_suffix(output_file_name, ".c")))
    612 	{
    613 	    defines_suffix = ".h";
    614 	    externs_suffix = ".i";
    615 	}
    616     }
    617 
    618     if (suffix != NULL)
    619     {
    620 	len = (size_t)(suffix - output_file_name);
    621 	file_prefix = TMALLOC(char, len + 1);
    622 	NO_SPACE(file_prefix);
    623 	strncpy(file_prefix, output_file_name, len)[len] = 0;
    624     }
    625     else
    626 	len = strlen(file_prefix);
    627 
    628     /* if "-o filename" was not given */
    629     if (output_file_name == 0)
    630     {
    631 	oflag = 1;
    632 	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
    633     }
    634 
    635     if (rflag)
    636     {
    637 	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
    638     }
    639     else
    640 	code_file_name = output_file_name;
    641 
    642     if (dflag && !dflag2)
    643     {
    644 	if (explicit_file_name)
    645 	{
    646 	    char *xsuffix;
    647 	    defines_file_name = strdup(output_file_name);
    648 	    if (defines_file_name == 0)
    649 		on_error();
    650 	    /* does the output_file_name have a known suffix */
    651             xsuffix = strrchr(output_file_name, '.');
    652             if (xsuffix != 0 &&
    653 		(!strcmp(xsuffix, ".c") ||   /* good, old-fashioned C */
    654                  !strcmp(xsuffix, ".C") ||   /* C++, or C on Windows */
    655                  !strcmp(xsuffix, ".cc") ||  /* C++ */
    656                  !strcmp(xsuffix, ".cxx") || /* C++ */
    657                  !strcmp(xsuffix, ".cpp")))  /* C++ (Windows) */
    658             {
    659                 strncpy(defines_file_name, output_file_name,
    660                         xsuffix - output_file_name + 1);
    661                 defines_file_name[xsuffix - output_file_name + 1] = 'h';
    662                 defines_file_name[xsuffix - output_file_name + 2] = 0;
    663             } else {
    664                 fprintf(stderr,"%s: suffix of output file name %s"
    665                                " not recognized, no -d file generated.\n",
    666                         myname, output_file_name);
    667                 dflag = 0;
    668                 free(defines_file_name);
    669                 defines_file_name = 0;
    670             }
    671 	} else {
    672 	    CREATE_FILE_NAME(defines_file_name, defines_suffix);
    673 	}
    674     }
    675 
    676     if (iflag)
    677     {
    678 	CREATE_FILE_NAME(externs_file_name, externs_suffix);
    679     }
    680 
    681     if (vflag)
    682     {
    683 	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
    684     }
    685 
    686     if (gflag)
    687     {
    688 	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
    689     }
    690 
    691     if (suffix != NULL)
    692     {
    693 	FREE(file_prefix);
    694     }
    695 }
    696 
    697 #if USE_MKSTEMP
    698 static void
    699 close_tmpfiles(void)
    700 {
    701     while (my_tmpfiles != 0)
    702     {
    703 	MY_TMPFILES *next = my_tmpfiles->next;
    704 
    705 	(void)chmod(my_tmpfiles->name, 0644);
    706 	(void)unlink(my_tmpfiles->name);
    707 
    708 	free(my_tmpfiles->name);
    709 	free(my_tmpfiles);
    710 
    711 	my_tmpfiles = next;
    712     }
    713 }
    714 
    715 #ifndef HAVE_MKSTEMP
    716 static int
    717 my_mkstemp(char *temp)
    718 {
    719     int fd;
    720     char *dname;
    721     char *fname;
    722     char *name;
    723 
    724     /*
    725      * Split-up to use tempnam, rather than tmpnam; the latter (like
    726      * mkstemp) is unusable on Windows.
    727      */
    728     if ((fname = strrchr(temp, '/')) != 0)
    729     {
    730 	dname = strdup(temp);
    731 	dname[++fname - temp] = '\0';
    732     }
    733     else
    734     {
    735 	dname = 0;
    736 	fname = temp;
    737     }
    738     if ((name = tempnam(dname, fname)) != 0)
    739     {
    740 	fd = open(name, O_CREAT | O_EXCL | O_RDWR);
    741 	strcpy(temp, name);
    742     }
    743     else
    744     {
    745 	fd = -1;
    746     }
    747 
    748     if (dname != 0)
    749 	free(dname);
    750 
    751     return fd;
    752 }
    753 #define mkstemp(s) my_mkstemp(s)
    754 #endif
    755 
    756 #endif
    757 
    758 /*
    759  * tmpfile() should be adequate, except that it may require special privileges
    760  * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
    761  */
    762 static FILE *
    763 open_tmpfile(const char *label)
    764 {
    765 #define MY_FMT "%s/%.*sXXXXXX"
    766     FILE *result;
    767 #if USE_MKSTEMP
    768     const char *tmpdir;
    769     char *name;
    770 
    771     if (((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) ||
    772 	((tmpdir = getenv("TEMP")) == 0 || access(tmpdir, W_OK) != 0))
    773     {
    774 #ifdef P_tmpdir
    775 	tmpdir = P_tmpdir;
    776 #else
    777 	tmpdir = "/tmp";
    778 #endif
    779 	if (access(tmpdir, W_OK) != 0)
    780 	    tmpdir = ".";
    781     }
    782 
    783     /* The size of the format is guaranteed to be longer than the result from
    784      * printing empty strings with it; this calculation accounts for the
    785      * string-lengths as well.
    786      */
    787     name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
    788 
    789     result = 0;
    790     if (name != 0)
    791     {
    792 	int fd;
    793 	const char *mark;
    794 
    795 	mode_t save_umask = umask(0177);
    796 
    797 	if ((mark = strrchr(label, '_')) == 0)
    798 	    mark = label + strlen(label);
    799 
    800 	sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
    801 	fd = mkstemp(name);
    802 	if (fd >= 0
    803 	    && (result = fdopen(fd, "w+")) != 0)
    804 	{
    805 	    MY_TMPFILES *item;
    806 
    807 	    if (my_tmpfiles == 0)
    808 	    {
    809 		atexit(close_tmpfiles);
    810 	    }
    811 
    812 	    item = NEW(MY_TMPFILES);
    813 	    NO_SPACE(item);
    814 
    815 	    item->name = name;
    816 	    NO_SPACE(item->name);
    817 
    818 	    item->next = my_tmpfiles;
    819 	    my_tmpfiles = item;
    820 	}
    821 	else
    822 	{
    823 	    FREE(name);
    824 	}
    825 	(void)umask(save_umask);
    826     }
    827 #else
    828     result = tmpfile();
    829 #endif
    830 
    831     if (result == 0)
    832 	open_error(label);
    833     return result;
    834 #undef MY_FMT
    835 }
    836 
    837 static void
    838 open_files(void)
    839 {
    840     create_file_names();
    841 
    842     if (input_file == 0)
    843     {
    844 	input_file = fopen(input_file_name, "r");
    845 	if (input_file == 0)
    846 	    open_error(input_file_name);
    847     }
    848 
    849     action_file = open_tmpfile("action_file");
    850     text_file = open_tmpfile("text_file");
    851 
    852     if (vflag)
    853     {
    854 	verbose_file = fopen(verbose_file_name, "w");
    855 	if (verbose_file == 0)
    856 	    open_error(verbose_file_name);
    857     }
    858 
    859     if (gflag)
    860     {
    861 	graph_file = fopen(graph_file_name, "w");
    862 	if (graph_file == 0)
    863 	    open_error(graph_file_name);
    864 	fprintf(graph_file, "digraph %s {\n", file_prefix);
    865 	fprintf(graph_file, "\tedge [fontsize=10];\n");
    866 	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
    867 	fprintf(graph_file, "\torientation=landscape;\n");
    868 	fprintf(graph_file, "\trankdir=LR;\n");
    869 	fprintf(graph_file, "\t/*\n");
    870 	fprintf(graph_file, "\tmargin=0.2;\n");
    871 	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
    872 	fprintf(graph_file, "\tratio=auto;\n");
    873 	fprintf(graph_file, "\t*/\n");
    874     }
    875 
    876     if (dflag || dflag2)
    877     {
    878 	defines_file = fopen(defines_file_name, "w");
    879 	if (defines_file == 0)
    880 	    open_error(defines_file_name);
    881 	union_file = open_tmpfile("union_file");
    882     }
    883 
    884     if (iflag)
    885     {
    886 	externs_file = fopen(externs_file_name, "w");
    887 	if (externs_file == 0)
    888 	    open_error(externs_file_name);
    889     }
    890 
    891     output_file = fopen(output_file_name, "w");
    892     if (output_file == 0)
    893 	open_error(output_file_name);
    894 
    895     if (rflag)
    896     {
    897 	code_file = fopen(code_file_name, "w");
    898 	if (code_file == 0)
    899 	    open_error(code_file_name);
    900     }
    901     else
    902 	code_file = output_file;
    903 }
    904 
    905 int
    906 main(int argc, char *argv[])
    907 {
    908     SRexpect = -1;
    909     RRexpect = -1;
    910     exit_code = EXIT_SUCCESS;
    911 
    912     set_signals();
    913     getargs(argc, argv);
    914     open_files();
    915     reader();
    916     lr0();
    917     lalr();
    918     make_parser();
    919     graph();
    920     finalize_closure();
    921     verbose();
    922     output();
    923     done(exit_code);
    924     /*NOTREACHED */
    925 }
    926