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