1 /* $Id: main.c,v 1.23 2009/10/27 09:06:44 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;
72 * Since fclose() is called via the signal handler, it might die. Don't loop
73 * if there is a problem closing a file.
75 #define DO_CLOSE(fp) \
82 static int got_intr = 0;
88 DO_CLOSE(output_file);
90 DO_CLOSE(action_file);
91 DO_CLOSE(defines_file);
95 DO_CLOSE(verbose_file);
102 DO_FREE(code_file_name);
105 DO_FREE(defines_file_name);
108 DO_FREE(output_file_name);
111 DO_FREE(verbose_file_name);
114 DO_FREE(graph_file_name);
127 onintr(int sig GCC_UNUSED)
137 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
138 signal(SIGINT, onintr);
141 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
142 signal(SIGTERM, onintr);
145 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
146 signal(SIGHUP, onintr);
153 static const char *msg[] =
157 ," -b file_prefix set filename prefix (default \"y.\")"
158 ," -d write definitions (y.tab.h)"
159 ," -g write a graphical description"
160 ," -l suppress #line directives"
161 ," -o output_file (default \"y.tab.c\")"
162 ," -p symbol_prefix set symbol prefix (default \"yy\")"
163 ," -r produce separate code and table files (y.code.c)"
164 ," -t add debugging support"
165 ," -v write description (y.output)"
166 ," -V show version information and exit"
171 fprintf(stderr, "Usage: %s [options] filename\n", myname);
172 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
173 fprintf(stderr, "%s\n", msg[n]);
208 printf("%s - %s\n", myname, VERSION);
217 getargs(int argc, char *argv[])
226 for (i = 1; i < argc; ++i)
241 goto no_more_options;
247 file_prefix = argv[i];
254 output_file_name = s;
256 output_file_name = argv[i];
265 symbol_prefix = argv[i];
293 input_file_name = argv[i];
311 #define CREATE_FILE_NAME(dest, suffix) \
312 dest = MALLOC(len + strlen(suffix) + 1); \
315 strcpy(dest, file_prefix); \
316 strcpy(dest + len, suffix)
319 create_file_names(void)
322 const char *defines_suffix;
326 defines_suffix = DEFINES_SUFFIX;
328 /* compute the file_prefix from the user provided output_file_name */
329 if (output_file_name != 0)
331 if (!(prefix = strstr(output_file_name, ".tab.c"))
332 && (prefix = strstr(output_file_name, ".c")))
333 defines_suffix = ".h";
338 len = (size_t) (prefix - output_file_name);
339 file_prefix = (char *)MALLOC(len + 1);
340 if (file_prefix == 0)
342 strncpy(file_prefix, output_file_name, len)[len] = 0;
345 len = strlen(file_prefix);
347 /* if "-o filename" was not given */
348 if (output_file_name == 0)
351 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
356 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
359 code_file_name = output_file_name;
363 CREATE_FILE_NAME(defines_file_name, defines_suffix);
368 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
373 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
389 input_file = fopen(input_file_name, "r");
391 open_error(input_file_name);
394 action_file = tmpfile();
395 if (action_file == 0)
396 open_error("action_file");
398 text_file = tmpfile();
400 open_error("text_file");
404 verbose_file = fopen(verbose_file_name, "w");
405 if (verbose_file == 0)
406 open_error(verbose_file_name);
411 graph_file = fopen(graph_file_name, "w");
413 open_error(graph_file_name);
414 fprintf(graph_file, "digraph %s {\n", file_prefix);
415 fprintf(graph_file, "\tedge [fontsize=10];\n");
416 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
417 fprintf(graph_file, "\torientation=landscape;\n");
418 fprintf(graph_file, "\trankdir=LR;\n");
419 fprintf(graph_file, "\t/*\n");
420 fprintf(graph_file, "\tmargin=0.2;\n");
421 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
422 fprintf(graph_file, "\tratio=auto;\n");
423 fprintf(graph_file, "\t*/\n");
428 defines_file = fopen(defines_file_name, "w");
429 if (defines_file == 0)
430 open_error(defines_file_name);
431 union_file = tmpfile();
433 open_error("union_file");
436 output_file = fopen(output_file_name, "w");
437 if (output_file == 0)
438 open_error(output_file_name);
442 code_file = fopen(code_file_name, "w");
444 open_error(code_file_name);
447 code_file = output_file;
451 main(int argc, char *argv[])
455 exit_code = EXIT_SUCCESS;