1 /* $Id: main.c,v 1.50 2014/04/22 23:34:47 tom Exp $ */
5 #include <unistd.h> /* for _exit() */
7 #include <stdlib.h> /* for _exit() */
13 # define USE_MKSTEMP 1
14 #elif defined(HAVE_FCNTL_H)
15 # define USE_MKSTEMP 1
16 # include <fcntl.h> /* for open(), O_EXCL, etc. */
18 # define USE_MKSTEMP 0
22 #include <sys/types.h>
25 typedef struct _my_tmpfiles
27 struct _my_tmpfiles *next;
32 static MY_TMPFILES *my_tmpfiles;
33 #endif /* USE_MKSTEMP */
45 const char *symbol_prefix;
46 const char *myname = "yacc";
51 static char empty_string[] = "";
52 static char default_file_prefix[] = "y";
54 static char *file_prefix = default_file_prefix;
57 char *input_file_name = empty_string;
58 char *defines_file_name;
59 char *externs_file_name;
61 static char *graph_file_name;
62 static char *output_file_name;
63 static char *verbose_file_name;
65 FILE *action_file; /* a temp file, used to save actions associated */
66 /* with rules until the parser is written */
67 FILE *code_file; /* y.code.c (used when the -r option is specified) */
68 FILE *defines_file; /* y.tab.h */
69 FILE *externs_file; /* y.tab.i */
70 FILE *input_file; /* the input file */
71 FILE *output_file; /* y.tab.c */
72 FILE *text_file; /* a temp file, used to save text until all */
73 /* symbols have been defined */
74 FILE *union_file; /* a temp file, used to save the union */
75 /* definition until all symbol have been */
77 FILE *verbose_file; /* y.output */
78 FILE *graph_file; /* y.dot */
89 Value_t *symbol_value;
98 char **symbol_destructor;
99 char **symbol_type_tag;
100 int locations = 0; /* default to no position processing */
101 int backtrack = 0; /* default is no backtracking */
115 * Since fclose() is called via the signal handler, it might die. Don't loop
116 * if there is a problem closing a file.
118 #define DO_CLOSE(fp) \
125 static int got_intr = 0;
130 DO_CLOSE(input_file);
131 DO_CLOSE(output_file);
133 DO_CLOSE(externs_file);
137 DO_CLOSE(action_file);
138 DO_CLOSE(defines_file);
139 DO_CLOSE(graph_file);
141 DO_CLOSE(union_file);
142 DO_CLOSE(verbose_file);
149 DO_FREE(code_file_name);
152 DO_FREE(defines_file_name);
155 DO_FREE(externs_file_name);
158 DO_FREE(output_file_name);
161 DO_FREE(verbose_file_name);
164 DO_FREE(graph_file_name);
178 onintr(int sig GCC_UNUSED)
188 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
189 signal(SIGINT, onintr);
192 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
193 signal(SIGTERM, onintr);
196 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
197 signal(SIGHUP, onintr);
204 static const char *msg[] =
208 ," -b file_prefix set filename prefix (default \"y.\")"
209 ," -B create a backtracking parser"
210 ," -d write definitions (" DEFINES_SUFFIX ")"
211 ," -D enable value stack memory reclamation"
212 ," -i write interface (y.tab.i)"
213 ," -g write a graphical description"
214 ," -l suppress #line directives"
215 ," -L enable position processing, e.g., \"%locations\""
216 ," -o output_file (default \"" OUTPUT_SUFFIX "\")"
217 ," -p symbol_prefix set symbol prefix (default \"yy\")"
218 ," -P create a reentrant parser, e.g., \"%pure-parser\""
219 ," -r produce separate code and table files (y.code.c)"
220 ," -s suppress #define's for quoted names in %token lines"
221 ," -t add debugging support"
222 ," -v write description (y.output)"
223 ," -V show version information and exit"
228 fprintf(stderr, "Usage: %s [options] filename\n", myname);
229 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
230 fprintf(stderr, "%s\n", msg[n]);
241 #if defined(YYBTYACC)
244 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
265 #if defined(YYBTYACC)
268 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
293 printf("%s - %s\n", myname, VERSION);
297 /* noop for bison compatibility. byacc is already designed to be posix
298 * yacc compatible. */
307 getargs(int argc, char *argv[])
316 for (i = 1; i < argc; ++i)
331 goto no_more_options;
337 file_prefix = argv[i];
344 output_file_name = s;
346 output_file_name = argv[i];
355 symbol_prefix = argv[i];
383 input_file_name = argv[i];
400 #define CREATE_FILE_NAME(dest, suffix) \
401 dest = alloc_file_name(len, suffix)
404 alloc_file_name(size_t len, const char *suffix)
406 char *result = TMALLOC(char, len + strlen(suffix) + 1);
409 strcpy(result, file_prefix);
410 strcpy(result + len, suffix);
415 create_file_names(void)
418 const char *defines_suffix;
419 const char *externs_suffix;
423 defines_suffix = DEFINES_SUFFIX;
424 externs_suffix = EXTERNS_SUFFIX;
426 /* compute the file_prefix from the user provided output_file_name */
427 if (output_file_name != 0)
429 if (!(prefix = strstr(output_file_name, OUTPUT_SUFFIX))
430 && (prefix = strstr(output_file_name, ".c")))
432 defines_suffix = ".h";
433 externs_suffix = ".i";
439 len = (size_t) (prefix - output_file_name);
440 file_prefix = TMALLOC(char, len + 1);
441 NO_SPACE(file_prefix);
442 strncpy(file_prefix, output_file_name, len)[len] = 0;
445 len = strlen(file_prefix);
447 /* if "-o filename" was not given */
448 if (output_file_name == 0)
451 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
456 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
459 code_file_name = output_file_name;
463 CREATE_FILE_NAME(defines_file_name, defines_suffix);
468 CREATE_FILE_NAME(externs_file_name, externs_suffix);
473 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
478 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
491 while (my_tmpfiles != 0)
493 MY_TMPFILES *next = my_tmpfiles->next;
495 chmod(my_tmpfiles->name, 0644);
496 unlink(my_tmpfiles->name);
498 free(my_tmpfiles->name);
507 my_mkstemp(char *temp)
515 * Split-up to use tempnam, rather than tmpnam; the latter (like
516 * mkstemp) is unusable on Windows.
518 if ((fname = strrchr(temp, '/')) != 0)
520 dname = strdup(temp);
521 dname[++fname - temp] = '\0';
528 if ((name = tempnam(dname, fname)) != 0)
530 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
543 #define mkstemp(s) my_mkstemp(s)
549 * tmpfile() should be adequate, except that it may require special privileges
550 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
553 open_tmpfile(const char *label)
562 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
569 if (access(tmpdir, W_OK) != 0)
573 name = malloc(strlen(tmpdir) + 10 + strlen(label));
578 if ((mark = strrchr(label, '_')) == 0)
579 mark = label + strlen(label);
581 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
585 result = fdopen(fd, "w+");
590 if (my_tmpfiles == 0)
592 atexit(close_tmpfiles);
595 item = NEW(MY_TMPFILES);
599 NO_SPACE(item->name);
601 item->next = my_tmpfiles;
622 input_file = fopen(input_file_name, "r");
624 open_error(input_file_name);
627 action_file = open_tmpfile("action_file");
628 text_file = open_tmpfile("text_file");
632 verbose_file = fopen(verbose_file_name, "w");
633 if (verbose_file == 0)
634 open_error(verbose_file_name);
639 graph_file = fopen(graph_file_name, "w");
641 open_error(graph_file_name);
642 fprintf(graph_file, "digraph %s {\n", file_prefix);
643 fprintf(graph_file, "\tedge [fontsize=10];\n");
644 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
645 fprintf(graph_file, "\torientation=landscape;\n");
646 fprintf(graph_file, "\trankdir=LR;\n");
647 fprintf(graph_file, "\t/*\n");
648 fprintf(graph_file, "\tmargin=0.2;\n");
649 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
650 fprintf(graph_file, "\tratio=auto;\n");
651 fprintf(graph_file, "\t*/\n");
656 defines_file = fopen(defines_file_name, "w");
657 if (defines_file == 0)
658 open_error(defines_file_name);
659 union_file = open_tmpfile("union_file");
664 externs_file = fopen(externs_file_name, "w");
665 if (externs_file == 0)
666 open_error(externs_file_name);
669 output_file = fopen(output_file_name, "w");
670 if (output_file == 0)
671 open_error(output_file_name);
675 code_file = fopen(code_file_name, "w");
677 open_error(code_file_name);
680 code_file = output_file;
684 main(int argc, char *argv[])
688 exit_code = EXIT_SUCCESS;