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