1 /* $Id: main.c,v 1.54 2014/10/06 22:40:07 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 ," -i write interface (y.tab.i)"
212 ," -g write a graphical description"
213 ," -l suppress #line directives"
214 ," -L enable position processing, e.g., \"%locations\""
215 ," -o output_file (default \"" OUTPUT_SUFFIX "\")"
216 ," -p symbol_prefix set symbol prefix (default \"yy\")"
217 ," -P create a reentrant parser, e.g., \"%pure-parser\""
218 ," -r produce separate code and table files (y.code.c)"
219 ," -s suppress #define's for quoted names in %token lines"
220 ," -t add debugging support"
221 ," -v write description (y.output)"
222 ," -V show version information and exit"
227 fprintf(stderr, "Usage: %s [options] filename\n", myname);
228 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
229 fprintf(stderr, "%s\n", msg[n]);
240 #if defined(YYBTYACC)
243 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
264 #if defined(YYBTYACC)
267 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
292 printf("%s - %s\n", myname, VERSION);
296 /* noop for bison compatibility. byacc is already designed to be posix
297 * yacc compatible. */
306 getargs(int argc, char *argv[])
315 for (i = 1; i < argc; ++i)
330 goto no_more_options;
336 file_prefix = argv[i];
343 output_file_name = s;
345 output_file_name = argv[i];
354 symbol_prefix = argv[i];
382 input_file_name = argv[i];
399 #define CREATE_FILE_NAME(dest, suffix) \
400 dest = alloc_file_name(len, suffix)
403 alloc_file_name(size_t len, const char *suffix)
405 char *result = TMALLOC(char, len + strlen(suffix) + 1);
408 strcpy(result, file_prefix);
409 strcpy(result + len, suffix);
414 create_file_names(void)
417 const char *defines_suffix;
418 const char *externs_suffix;
422 defines_suffix = DEFINES_SUFFIX;
423 externs_suffix = EXTERNS_SUFFIX;
425 /* compute the file_prefix from the user provided output_file_name */
426 if (output_file_name != 0)
428 if (!(prefix = strstr(output_file_name, OUTPUT_SUFFIX))
429 && (prefix = strstr(output_file_name, ".c")))
431 defines_suffix = ".h";
432 externs_suffix = ".i";
438 len = (size_t) (prefix - output_file_name);
439 file_prefix = TMALLOC(char, len + 1);
440 NO_SPACE(file_prefix);
441 strncpy(file_prefix, output_file_name, len)[len] = 0;
444 len = strlen(file_prefix);
446 /* if "-o filename" was not given */
447 if (output_file_name == 0)
450 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
455 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
458 code_file_name = output_file_name;
462 CREATE_FILE_NAME(defines_file_name, defines_suffix);
467 CREATE_FILE_NAME(externs_file_name, externs_suffix);
472 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
477 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
490 while (my_tmpfiles != 0)
492 MY_TMPFILES *next = my_tmpfiles->next;
494 (void)chmod(my_tmpfiles->name, 0644);
495 (void)unlink(my_tmpfiles->name);
497 free(my_tmpfiles->name);
506 my_mkstemp(char *temp)
514 * Split-up to use tempnam, rather than tmpnam; the latter (like
515 * mkstemp) is unusable on Windows.
517 if ((fname = strrchr(temp, '/')) != 0)
519 dname = strdup(temp);
520 dname[++fname - temp] = '\0';
527 if ((name = tempnam(dname, fname)) != 0)
529 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
542 #define mkstemp(s) my_mkstemp(s)
548 * tmpfile() should be adequate, except that it may require special privileges
549 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
552 open_tmpfile(const char *label)
561 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
568 if (access(tmpdir, W_OK) != 0)
572 name = malloc(strlen(tmpdir) + 10 + strlen(label));
577 mode_t save_umask = umask(0177);
579 if ((mark = strrchr(label, '_')) == 0)
580 mark = label + strlen(label);
582 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
586 result = fdopen(fd, "w+");
591 if (my_tmpfiles == 0)
593 atexit(close_tmpfiles);
596 item = NEW(MY_TMPFILES);
600 NO_SPACE(item->name);
602 item->next = my_tmpfiles;
606 (void)umask(save_umask);
624 input_file = fopen(input_file_name, "r");
626 open_error(input_file_name);
629 action_file = open_tmpfile("action_file");
630 text_file = open_tmpfile("text_file");
634 verbose_file = fopen(verbose_file_name, "w");
635 if (verbose_file == 0)
636 open_error(verbose_file_name);
641 graph_file = fopen(graph_file_name, "w");
643 open_error(graph_file_name);
644 fprintf(graph_file, "digraph %s {\n", file_prefix);
645 fprintf(graph_file, "\tedge [fontsize=10];\n");
646 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
647 fprintf(graph_file, "\torientation=landscape;\n");
648 fprintf(graph_file, "\trankdir=LR;\n");
649 fprintf(graph_file, "\t/*\n");
650 fprintf(graph_file, "\tmargin=0.2;\n");
651 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
652 fprintf(graph_file, "\tratio=auto;\n");
653 fprintf(graph_file, "\t*/\n");
658 defines_file = fopen(defines_file_name, "w");
659 if (defines_file == 0)
660 open_error(defines_file_name);
661 union_file = open_tmpfile("union_file");
666 externs_file = fopen(externs_file_name, "w");
667 if (externs_file == 0)
668 open_error(externs_file_name);
671 output_file = fopen(output_file_name, "w");
672 if (output_file == 0)
673 open_error(output_file_name);
677 code_file = fopen(code_file_name, "w");
679 open_error(code_file_name);
682 code_file = output_file;
686 main(int argc, char *argv[])
690 exit_code = EXIT_SUCCESS;