Home | History | Annotate | Line # | Download | only in dist
main.c revision 1.5
      1 /*	$NetBSD: main.c,v 1.5 2010/12/25 19:24:28 joerg Exp $	*/
      2 /* Id: main.c,v 1.30 2010/11/24 15:13:39 tom Exp */
      3 
      4 #if HAVE_NBTOOL_CONFIG_H
      5 #include "nbtool_config.h"
      6 #endif
      7 
      8 #include <sys/cdefs.h>
      9 __RCSID("$NetBSD: main.c,v 1.5 2010/12/25 19:24:28 joerg Exp $");
     10 
     11 #include "defs.h"
     12 
     13 #include <signal.h>
     14 #include <unistd.h>		/* for _exit() */
     15 
     16 
     17 char dflag;
     18 char gflag;
     19 char lflag;
     20 static char oflag;
     21 char rflag;
     22 char tflag;
     23 char vflag;
     24 
     25 const char *symbol_prefix;
     26 const char *myname = "yacc";
     27 
     28 int lineno;
     29 int outline;
     30 
     31 static char empty_string[] = "";
     32 static char default_file_prefix[] = "y";
     33 static int explicit_file_name;
     34 
     35 static char *file_prefix = default_file_prefix;
     36 
     37 char *code_file_name;
     38 char *input_file_name = empty_string;
     39 static char *defines_file_name;
     40 static char *graph_file_name;
     41 static char *output_file_name;
     42 static char *verbose_file_name;
     43 
     44 FILE *action_file;	/*  a temp file, used to save actions associated    */
     45 			/*  with rules until the parser is written          */
     46 FILE *code_file;	/*  y.code.c (used when the -r option is specified) */
     47 FILE *defines_file;	/*  y.tab.h                                         */
     48 FILE *input_file;	/*  the input file                                  */
     49 FILE *output_file;	/*  y.tab.c                                         */
     50 FILE *text_file;	/*  a temp file, used to save text until all        */
     51 			/*  symbols have been defined                       */
     52 FILE *union_file;	/*  a temp file, used to save the union             */
     53 			/*  definition until all symbol have been           */
     54 			/*  defined                                         */
     55 FILE *verbose_file;	/*  y.output                                        */
     56 FILE *graph_file;	/*  y.dot                                           */
     57 
     58 int nitems;
     59 int nrules;
     60 int nsyms;
     61 int ntokens;
     62 int nvars;
     63 
     64 Value_t start_symbol;
     65 char **symbol_name;
     66 char **symbol_pname;
     67 Value_t *symbol_value;
     68 short *symbol_prec;
     69 char *symbol_assoc;
     70 
     71 int pure_parser;
     72 int exit_code;
     73 
     74 Value_t *ritem;
     75 Value_t *rlhs;
     76 Value_t *rrhs;
     77 Value_t *rprec;
     78 Assoc_t *rassoc;
     79 Value_t **derives;
     80 char *nullable;
     81 
     82 /*
     83  * Since fclose() is called via the signal handler, it might die.  Don't loop
     84  * if there is a problem closing a file.
     85  */
     86 #define DO_CLOSE(fp) \
     87 	if (fp != 0) { \
     88 	    FILE *use = fp; \
     89 	    fp = 0; \
     90 	    fclose(use); \
     91 	}
     92 
     93 static int got_intr = 0;
     94 
     95 void
     96 done(int k)
     97 {
     98     DO_CLOSE(input_file);
     99     DO_CLOSE(output_file);
    100 
    101     DO_CLOSE(action_file);
    102     DO_CLOSE(defines_file);
    103     DO_CLOSE(graph_file);
    104     DO_CLOSE(text_file);
    105     DO_CLOSE(union_file);
    106     DO_CLOSE(verbose_file);
    107 
    108     if (got_intr)
    109 	_exit(EXIT_FAILURE);
    110 
    111 #ifdef NO_LEAKS
    112     if (rflag)
    113 	DO_FREE(code_file_name);
    114 
    115     if (dflag)
    116 	DO_FREE(defines_file_name);
    117 
    118     if (oflag)
    119 	DO_FREE(output_file_name);
    120 
    121     if (vflag)
    122 	DO_FREE(verbose_file_name);
    123 
    124     if (gflag)
    125 	DO_FREE(graph_file_name);
    126 
    127     lr0_leaks();
    128     lalr_leaks();
    129     mkpar_leaks();
    130     output_leaks();
    131     reader_leaks();
    132 #endif
    133 
    134     if (rflag)
    135 	DO_CLOSE(code_file);
    136 
    137     exit(k);
    138 }
    139 
    140 static void
    141 onintr(int sig GCC_UNUSED)
    142 {
    143     got_intr = 1;
    144     done(EXIT_FAILURE);
    145 }
    146 
    147 static void
    148 set_signals(void)
    149 {
    150 #ifdef SIGINT
    151     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    152 	signal(SIGINT, onintr);
    153 #endif
    154 #ifdef SIGTERM
    155     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    156 	signal(SIGTERM, onintr);
    157 #endif
    158 #ifdef SIGHUP
    159     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    160 	signal(SIGHUP, onintr);
    161 #endif
    162 }
    163 
    164 static void
    165 usage(void)
    166 {
    167     static const char *msg[] =
    168     {
    169 	""
    170 	,"Options:"
    171 	,"  -b file_prefix        set filename prefix (default \"y.\")"
    172 	,"  -d                    write definitions (y.tab.h)"
    173 	,"  -g                    write a graphical description"
    174 	,"  -l                    suppress #line directives"
    175 	,"  -o output_file        (default \"y.tab.c\")"
    176 	,"  -p symbol_prefix      set symbol prefix (default \"yy\")"
    177 	,"  -P                    create a reentrant parser, e.g., \"%pure-parser\""
    178 	,"  -r                    produce separate code and table files (y.code.c)"
    179 	,"  -t                    add debugging support"
    180 	,"  -v                    write description (y.output)"
    181 	,"  -V                    show version information and exit"
    182     };
    183     unsigned n;
    184 
    185     fflush(stdout);
    186     fprintf(stderr, "Usage: %s [options] filename\n", myname);
    187     for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
    188 	fprintf(stderr, "%s\n", msg[n]);
    189 
    190     exit(1);
    191 }
    192 
    193 static void
    194 setflag(int ch)
    195 {
    196     switch (ch)
    197     {
    198     case 'd':
    199 	dflag = 1;
    200 	break;
    201 
    202     case 'g':
    203 	gflag = 1;
    204 	break;
    205 
    206     case 'l':
    207 	lflag = 1;
    208 	break;
    209 
    210     case 'P':
    211 	pure_parser = 1;
    212 	break;
    213 
    214     case 'r':
    215 	rflag = 1;
    216 	break;
    217 
    218     case 't':
    219 	tflag = 1;
    220 	break;
    221 
    222     case 'v':
    223 	vflag = 1;
    224 	break;
    225 
    226     case 'V':
    227 	printf("%s - %s\n", myname, VERSION);
    228 	exit(EXIT_SUCCESS);
    229 
    230     case 'y':
    231 	/* noop for bison compatibility. byacc is already designed to be posix
    232 	 * yacc compatible. */
    233 	break;
    234 
    235     default:
    236 	usage();
    237     }
    238 }
    239 
    240 static void
    241 getargs(int argc, char *argv[])
    242 {
    243     int i;
    244     char *s;
    245     int ch;
    246 
    247     if (argc > 0)
    248 	myname = argv[0];
    249 
    250     for (i = 1; i < argc; ++i)
    251     {
    252 	s = argv[i];
    253 	if (*s != '-')
    254 	    break;
    255 	switch (ch = *++s)
    256 	{
    257 	case '\0':
    258 	    input_file = stdin;
    259 	    if (i + 1 < argc)
    260 		usage();
    261 	    return;
    262 
    263 	case '-':
    264 	    ++i;
    265 	    goto no_more_options;
    266 
    267 	case 'b':
    268 	    if (*++s)
    269 		file_prefix = s;
    270 	    else if (++i < argc)
    271 		file_prefix = argv[i];
    272 	    else
    273 		usage();
    274 	    continue;
    275 
    276 	case 'o':
    277 	    if (*++s)
    278 		output_file_name = s;
    279 	    else if (++i < argc)
    280 		output_file_name = argv[i];
    281 	    else
    282 		usage();
    283 	    explicit_file_name = 1;
    284 	    continue;
    285 
    286 	case 'p':
    287 	    if (*++s)
    288 		symbol_prefix = s;
    289 	    else if (++i < argc)
    290 		symbol_prefix = argv[i];
    291 	    else
    292 		usage();
    293 	    continue;
    294 
    295 	default:
    296 	    setflag(ch);
    297 	    break;
    298 	}
    299 
    300 	for (;;)
    301 	{
    302 	    switch (ch = *++s)
    303 	    {
    304 	    case '\0':
    305 		goto end_of_option;
    306 
    307 	    default:
    308 		setflag(ch);
    309 		break;
    310 	    }
    311 	}
    312       end_of_option:;
    313     }
    314 
    315   no_more_options:;
    316     if (i + 1 != argc)
    317 	usage();
    318     input_file_name = argv[i];
    319 }
    320 
    321 char *
    322 allocate(size_t n)
    323 {
    324     char *p;
    325 
    326     p = NULL;
    327     if (n)
    328     {
    329 	p = CALLOC(1, n);
    330 	NO_SPACE(p);
    331     }
    332     return (p);
    333 }
    334 
    335 #define CREATE_FILE_NAME(dest, suffix) \
    336 	dest = MALLOC(len + strlen(suffix) + 1); \
    337 	NO_SPACE(dest); \
    338 	strcpy(dest, file_prefix); \
    339 	strcpy(dest + len, suffix)
    340 
    341 static void
    342 create_file_names(void)
    343 {
    344     size_t len;
    345     const char *defines_suffix;
    346     char *prefix;
    347 
    348     prefix = NULL;
    349     defines_suffix = DEFINES_SUFFIX;
    350 
    351     /* compute the file_prefix from the user provided output_file_name */
    352     if (output_file_name != 0)
    353     {
    354 	if (!(prefix = strstr(output_file_name, ".tab.c"))
    355 	    && (prefix = strstr(output_file_name, ".c")))
    356 	    defines_suffix = ".h";
    357     }
    358 
    359     if (prefix != NULL)
    360     {
    361 	len = (size_t) (prefix - output_file_name);
    362 	file_prefix = MALLOC(len + 1);
    363 	NO_SPACE(file_prefix);
    364 	strncpy(file_prefix, output_file_name, len)[len] = 0;
    365     }
    366     else
    367 	len = strlen(file_prefix);
    368 
    369     /* if "-o filename" was not given */
    370     if (output_file_name == 0)
    371     {
    372 	oflag = 1;
    373 	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
    374     }
    375 
    376     if (rflag)
    377     {
    378 	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
    379     }
    380     else
    381 	code_file_name = output_file_name;
    382 
    383     if (dflag)
    384     {
    385 	if (explicit_file_name)
    386 	{
    387 	    char *suffix;
    388 	    defines_file_name = strdup(output_file_name);
    389 	    if (defines_file_name == 0)
    390 		no_space();
    391 	    /* does the output_file_name have a known suffix */
    392             suffix = strrchr(output_file_name, '.');
    393             if (suffix != 0 &&
    394 		(!strcmp(suffix, ".c") ||   /* good, old-fashioned C */
    395                  !strcmp(suffix, ".C") ||   /* C++, or C on Windows */
    396                  !strcmp(suffix, ".cc") ||  /* C++ */
    397                  !strcmp(suffix, ".cxx") || /* C++ */
    398                  !strcmp(suffix, ".cpp")))  /* C++ (Windows) */
    399             {
    400                 strncpy(defines_file_name, output_file_name,
    401                         suffix - output_file_name + 1);
    402                 defines_file_name[suffix - output_file_name + 1] = 'h';
    403                 defines_file_name[suffix - output_file_name + 2] = 0;
    404             } else {
    405                 fprintf(stderr,"%s: suffix of output file name %s"
    406                                " not recognized, no -d file generated.\n",
    407                         myname, output_file_name);
    408                 dflag = 0;
    409                 free(defines_file_name);
    410                 defines_file_name = 0;
    411             }
    412 	} else {
    413 	    CREATE_FILE_NAME(defines_file_name, defines_suffix);
    414 	}
    415     }
    416 
    417     if (vflag)
    418     {
    419 	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
    420     }
    421 
    422     if (gflag)
    423     {
    424 	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
    425     }
    426 
    427     if (prefix != NULL)
    428     {
    429 	FREE(file_prefix);
    430     }
    431 }
    432 
    433 static void
    434 open_files(void)
    435 {
    436     create_file_names();
    437 
    438     if (input_file == 0)
    439     {
    440 	input_file = fopen(input_file_name, "r");
    441 	if (input_file == 0)
    442 	    open_error(input_file_name);
    443     }
    444 
    445     action_file = tmpfile();
    446     if (action_file == 0)
    447 	open_error("action_file");
    448 
    449     text_file = tmpfile();
    450     if (text_file == 0)
    451 	open_error("text_file");
    452 
    453     if (vflag)
    454     {
    455 	verbose_file = fopen(verbose_file_name, "w");
    456 	if (verbose_file == 0)
    457 	    open_error(verbose_file_name);
    458     }
    459 
    460     if (gflag)
    461     {
    462 	graph_file = fopen(graph_file_name, "w");
    463 	if (graph_file == 0)
    464 	    open_error(graph_file_name);
    465 	fprintf(graph_file, "digraph %s {\n", file_prefix);
    466 	fprintf(graph_file, "\tedge [fontsize=10];\n");
    467 	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
    468 	fprintf(graph_file, "\torientation=landscape;\n");
    469 	fprintf(graph_file, "\trankdir=LR;\n");
    470 	fprintf(graph_file, "\t/*\n");
    471 	fprintf(graph_file, "\tmargin=0.2;\n");
    472 	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
    473 	fprintf(graph_file, "\tratio=auto;\n");
    474 	fprintf(graph_file, "\t*/\n");
    475     }
    476 
    477     if (dflag)
    478     {
    479 	defines_file = fopen(defines_file_name, "w");
    480 	if (defines_file == 0)
    481 	    open_error(defines_file_name);
    482 	union_file = tmpfile();
    483 	if (union_file == 0)
    484 	    open_error("union_file");
    485     }
    486 
    487     output_file = fopen(output_file_name, "w");
    488     if (output_file == 0)
    489 	open_error(output_file_name);
    490 
    491     if (rflag)
    492     {
    493 	code_file = fopen(code_file_name, "w");
    494 	if (code_file == 0)
    495 	    open_error(code_file_name);
    496     }
    497     else
    498 	code_file = output_file;
    499 }
    500 
    501 int
    502 main(int argc, char *argv[])
    503 {
    504     SRexpect = -1;
    505     RRexpect = -1;
    506     exit_code = EXIT_SUCCESS;
    507 
    508     set_signals();
    509     getargs(argc, argv);
    510     open_files();
    511     reader();
    512     lr0();
    513     lalr();
    514     make_parser();
    515     graph();
    516     finalize_closure();
    517     verbose();
    518     output();
    519     done(exit_code);
    520     /*NOTREACHED */
    521 }
    522