1 /* $Id: main.c,v 1.41 2014/01/01 14:23:27 Christos.Zoulas Exp $ */
4 #include <unistd.h> /* for _exit() */
10 #elif defined(HAVE_FCNTL_H)
11 # define USE_MKSTEMP 1
12 # include <fcntl.h> /* for open(), O_EXCL, etc. */
14 # define USE_MKSTEMP 0
18 #include <sys/types.h>
21 typedef struct _my_tmpfiles
23 struct _my_tmpfiles *next;
28 static MY_TMPFILES *my_tmpfiles;
29 #endif /* USE_MKSTEMP */
41 const char *symbol_prefix;
42 const char *myname = "yacc";
47 static char empty_string[] = "";
48 static char default_file_prefix[] = "y";
50 static char *file_prefix = default_file_prefix;
53 char *input_file_name = empty_string;
54 char *defines_file_name;
55 char *externs_file_name;
57 static char *graph_file_name;
58 static char *output_file_name;
59 static char *verbose_file_name;
61 FILE *action_file; /* a temp file, used to save actions associated */
62 /* with rules until the parser is written */
63 FILE *code_file; /* y.code.c (used when the -r option is specified) */
64 FILE *defines_file; /* y.tab.h */
65 FILE *externs_file; /* y.tab.i */
66 FILE *input_file; /* the input file */
67 FILE *output_file; /* y.tab.c */
68 FILE *text_file; /* a temp file, used to save text until all */
69 /* symbols have been defined */
70 FILE *union_file; /* a temp file, used to save the union */
71 /* definition until all symbol have been */
73 FILE *verbose_file; /* y.output */
74 FILE *graph_file; /* y.dot */
85 Value_t *symbol_value;
102 * Since fclose() is called via the signal handler, it might die. Don't loop
103 * if there is a problem closing a file.
105 #define DO_CLOSE(fp) \
112 static int got_intr = 0;
117 DO_CLOSE(input_file);
118 DO_CLOSE(output_file);
120 DO_CLOSE(action_file);
121 DO_CLOSE(defines_file);
122 DO_CLOSE(graph_file);
124 DO_CLOSE(union_file);
125 DO_CLOSE(verbose_file);
132 DO_FREE(code_file_name);
135 DO_FREE(defines_file_name);
138 DO_FREE(externs_file_name);
141 DO_FREE(output_file_name);
144 DO_FREE(verbose_file_name);
147 DO_FREE(graph_file_name);
163 onintr(int sig GCC_UNUSED)
173 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
174 signal(SIGINT, onintr);
177 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
178 signal(SIGTERM, onintr);
181 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
182 signal(SIGHUP, onintr);
189 static const char *msg[] =
193 ," -b file_prefix set filename prefix (default \"y.\")"
194 ," -d write definitions (y.tab.h)"
195 ," -i write interface (y.tab.i)"
196 ," -g write a graphical description"
197 ," -l suppress #line directives"
198 ," -o output_file (default \"y.tab.c\")"
199 ," -p symbol_prefix set symbol prefix (default \"yy\")"
200 ," -P create a reentrant parser, e.g., \"%pure-parser\""
201 ," -r produce separate code and table files (y.code.c)"
202 ," -s suppress #define's for quoted names in %token lines"
203 ," -t add debugging support"
204 ," -v write description (y.output)"
205 ," -V show version information and exit"
210 fprintf(stderr, "Usage: %s [options] filename\n", myname);
211 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
212 fprintf(stderr, "%s\n", msg[n]);
259 printf("%s - %s\n", myname, VERSION);
263 /* noop for bison compatibility. byacc is already designed to be posix
264 * yacc compatible. */
273 getargs(int argc, char *argv[])
282 for (i = 1; i < argc; ++i)
297 goto no_more_options;
303 file_prefix = argv[i];
310 output_file_name = s;
312 output_file_name = argv[i];
321 symbol_prefix = argv[i];
349 input_file_name = argv[i];
366 #define CREATE_FILE_NAME(dest, suffix) \
367 dest = TMALLOC(char, len + strlen(suffix) + 1); \
369 strcpy(dest, file_prefix); \
370 strcpy(dest + len, suffix)
373 create_file_names(void)
376 const char *defines_suffix;
377 const char *externs_suffix;
381 defines_suffix = DEFINES_SUFFIX;
382 externs_suffix = EXTERNS_SUFFIX;
384 /* compute the file_prefix from the user provided output_file_name */
385 if (output_file_name != 0)
387 if (!(prefix = strstr(output_file_name, ".tab.c"))
388 && (prefix = strstr(output_file_name, ".c")))
390 defines_suffix = ".h";
391 externs_suffix = ".i";
397 len = (size_t) (prefix - output_file_name);
398 file_prefix = TMALLOC(char, len + 1);
399 NO_SPACE(file_prefix);
400 strncpy(file_prefix, output_file_name, len)[len] = 0;
403 len = strlen(file_prefix);
405 /* if "-o filename" was not given */
406 if (output_file_name == 0)
409 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
414 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
417 code_file_name = output_file_name;
421 CREATE_FILE_NAME(defines_file_name, defines_suffix);
426 CREATE_FILE_NAME(externs_file_name, externs_suffix);
431 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
436 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
449 while (my_tmpfiles != 0)
451 MY_TMPFILES *next = my_tmpfiles->next;
453 chmod(my_tmpfiles->name, 0644);
454 unlink(my_tmpfiles->name);
456 free(my_tmpfiles->name);
465 my_mkstemp(char *temp)
473 * Split-up to use tempnam, rather than tmpnam; the latter (like
474 * mkstemp) is unusable on Windows.
476 if ((fname = strrchr(temp, '/')) != 0)
478 dname = strdup(temp);
479 dname[++fname - temp] = '\0';
486 if ((name = tempnam(dname, fname)) != 0)
488 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
501 #define mkstemp(s) my_mkstemp(s)
507 * tmpfile() should be adequate, except that it may require special privileges
508 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
511 open_tmpfile(const char *label)
520 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
527 if (access(tmpdir, W_OK) != 0)
531 name = malloc(strlen(tmpdir) + 10 + strlen(label));
536 if ((mark = strrchr(label, '_')) == 0)
537 mark = label + strlen(label);
539 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
543 result = fdopen(fd, "w+");
548 if (my_tmpfiles == 0)
550 atexit(close_tmpfiles);
553 item = NEW(MY_TMPFILES);
557 NO_SPACE(item->name);
559 item->next = my_tmpfiles;
580 input_file = fopen(input_file_name, "r");
582 open_error(input_file_name);
585 action_file = open_tmpfile("action_file");
586 text_file = open_tmpfile("text_file");
590 verbose_file = fopen(verbose_file_name, "w");
591 if (verbose_file == 0)
592 open_error(verbose_file_name);
597 graph_file = fopen(graph_file_name, "w");
599 open_error(graph_file_name);
600 fprintf(graph_file, "digraph %s {\n", file_prefix);
601 fprintf(graph_file, "\tedge [fontsize=10];\n");
602 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
603 fprintf(graph_file, "\torientation=landscape;\n");
604 fprintf(graph_file, "\trankdir=LR;\n");
605 fprintf(graph_file, "\t/*\n");
606 fprintf(graph_file, "\tmargin=0.2;\n");
607 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
608 fprintf(graph_file, "\tratio=auto;\n");
609 fprintf(graph_file, "\t*/\n");
614 defines_file = fopen(defines_file_name, "w");
615 if (defines_file == 0)
616 open_error(defines_file_name);
617 union_file = open_tmpfile("union_file");
622 externs_file = fopen(externs_file_name, "w");
623 if (externs_file == 0)
624 open_error(externs_file_name);
627 output_file = fopen(output_file_name, "w");
628 if (output_file == 0)
629 open_error(output_file_name);
633 code_file = fopen(code_file_name, "w");
635 open_error(code_file_name);
638 code_file = output_file;
642 main(int argc, char *argv[])
646 exit_code = EXIT_SUCCESS;