1 /* $Id: main.c,v 1.24 2010/02/17 00:43:08 tom Exp $ */
4 #include <unistd.h> /* for _exit() */
16 const char *symbol_prefix;
17 const char *myname = "yacc";
22 static char empty_string[] = "";
23 static char default_file_prefix[] = "y";
25 static char *file_prefix = default_file_prefix;
28 char *defines_file_name;
29 char *input_file_name = empty_string;
30 char *output_file_name = 0;
31 char *verbose_file_name;
32 char *graph_file_name;
34 FILE *action_file; /* a temp file, used to save actions associated */
35 /* with rules until the parser is written */
36 FILE *code_file; /* y.code.c (used when the -r option is specified) */
37 FILE *defines_file; /* y.tab.h */
38 FILE *input_file; /* the input file */
39 FILE *output_file; /* y.tab.c */
40 FILE *text_file; /* a temp file, used to save text until all */
41 /* symbols have been defined */
42 FILE *union_file; /* a temp file, used to save the union */
43 /* definition until all symbol have been */
45 FILE *verbose_file; /* y.output */
46 FILE *graph_file; /* y.dot */
57 Value_t *symbol_value;
73 * Since fclose() is called via the signal handler, it might die. Don't loop
74 * if there is a problem closing a file.
76 #define DO_CLOSE(fp) \
83 static int got_intr = 0;
89 DO_CLOSE(output_file);
91 DO_CLOSE(action_file);
92 DO_CLOSE(defines_file);
96 DO_CLOSE(verbose_file);
103 DO_FREE(code_file_name);
106 DO_FREE(defines_file_name);
109 DO_FREE(output_file_name);
112 DO_FREE(verbose_file_name);
115 DO_FREE(graph_file_name);
128 onintr(int sig GCC_UNUSED)
138 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
139 signal(SIGINT, onintr);
142 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
143 signal(SIGTERM, onintr);
146 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
147 signal(SIGHUP, onintr);
154 static const char *msg[] =
158 ," -b file_prefix set filename prefix (default \"y.\")"
159 ," -d write definitions (y.tab.h)"
160 ," -g write a graphical description"
161 ," -l suppress #line directives"
162 ," -o output_file (default \"y.tab.c\")"
163 ," -p symbol_prefix set symbol prefix (default \"yy\")"
164 ," -r produce separate code and table files (y.code.c)"
165 ," -t add debugging support"
166 ," -v write description (y.output)"
167 ," -V show version information and exit"
172 fprintf(stderr, "Usage: %s [options] filename\n", myname);
173 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
174 fprintf(stderr, "%s\n", msg[n]);
213 printf("%s - %s\n", myname, VERSION);
222 getargs(int argc, char *argv[])
231 for (i = 1; i < argc; ++i)
246 goto no_more_options;
252 file_prefix = argv[i];
259 output_file_name = s;
261 output_file_name = argv[i];
270 symbol_prefix = argv[i];
298 input_file_name = argv[i];
316 #define CREATE_FILE_NAME(dest, suffix) \
317 dest = MALLOC(len + strlen(suffix) + 1); \
320 strcpy(dest, file_prefix); \
321 strcpy(dest + len, suffix)
324 create_file_names(void)
327 const char *defines_suffix;
331 defines_suffix = DEFINES_SUFFIX;
333 /* compute the file_prefix from the user provided output_file_name */
334 if (output_file_name != 0)
336 if (!(prefix = strstr(output_file_name, ".tab.c"))
337 && (prefix = strstr(output_file_name, ".c")))
338 defines_suffix = ".h";
343 len = (size_t) (prefix - output_file_name);
344 file_prefix = (char *)MALLOC(len + 1);
345 if (file_prefix == 0)
347 strncpy(file_prefix, output_file_name, len)[len] = 0;
350 len = strlen(file_prefix);
352 /* if "-o filename" was not given */
353 if (output_file_name == 0)
356 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
361 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
364 code_file_name = output_file_name;
368 CREATE_FILE_NAME(defines_file_name, defines_suffix);
373 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
378 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
394 input_file = fopen(input_file_name, "r");
396 open_error(input_file_name);
399 action_file = tmpfile();
400 if (action_file == 0)
401 open_error("action_file");
403 text_file = tmpfile();
405 open_error("text_file");
409 verbose_file = fopen(verbose_file_name, "w");
410 if (verbose_file == 0)
411 open_error(verbose_file_name);
416 graph_file = fopen(graph_file_name, "w");
418 open_error(graph_file_name);
419 fprintf(graph_file, "digraph %s {\n", file_prefix);
420 fprintf(graph_file, "\tedge [fontsize=10];\n");
421 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
422 fprintf(graph_file, "\torientation=landscape;\n");
423 fprintf(graph_file, "\trankdir=LR;\n");
424 fprintf(graph_file, "\t/*\n");
425 fprintf(graph_file, "\tmargin=0.2;\n");
426 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
427 fprintf(graph_file, "\tratio=auto;\n");
428 fprintf(graph_file, "\t*/\n");
433 defines_file = fopen(defines_file_name, "w");
434 if (defines_file == 0)
435 open_error(defines_file_name);
436 union_file = tmpfile();
438 open_error("union_file");
441 output_file = fopen(output_file_name, "w");
442 if (output_file == 0)
443 open_error(output_file_name);
447 code_file = fopen(code_file_name, "w");
449 open_error(code_file_name);
452 code_file = output_file;
456 main(int argc, char *argv[])
460 exit_code = EXIT_SUCCESS;