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