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