1 /* $Id: main.c,v 1.64 2019/06/16 17:12:39 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 */
46 const char *symbol_prefix;
47 const char *myname = "yacc";
52 static char default_file_prefix[] = "y";
54 static char *file_prefix = default_file_prefix;
57 char *input_file_name;
58 size_t input_file_name_len = 0;
59 char *defines_file_name;
60 char *externs_file_name;
62 static char *graph_file_name;
63 static char *output_file_name;
64 static char *verbose_file_name;
66 FILE *action_file; /* a temp file, used to save actions associated */
67 /* with rules until the parser is written */
68 FILE *code_file; /* y.code.c (used when the -r option is specified) */
69 FILE *defines_file; /* y.tab.h */
70 FILE *externs_file; /* y.tab.i */
71 FILE *input_file; /* the input file */
72 FILE *output_file; /* y.tab.c */
73 FILE *text_file; /* a temp file, used to save text until all */
74 /* symbols have been defined */
75 FILE *union_file; /* a temp file, used to save the union */
76 /* definition until all symbol have been */
78 FILE *verbose_file; /* y.output */
79 FILE *graph_file; /* y.dot */
90 Value_t *symbol_value;
100 char **symbol_destructor;
101 char **symbol_type_tag;
102 int locations = 0; /* default to no position processing */
103 int backtrack = 0; /* default is no backtracking */
104 char *initial_action = NULL;
118 * Since fclose() is called via the signal handler, it might die. Don't loop
119 * if there is a problem closing a file.
121 #define DO_CLOSE(fp) \
128 static int got_intr = 0;
133 DO_CLOSE(input_file);
134 DO_CLOSE(output_file);
136 DO_CLOSE(externs_file);
140 DO_CLOSE(action_file);
141 DO_CLOSE(defines_file);
142 DO_CLOSE(graph_file);
144 DO_CLOSE(union_file);
145 DO_CLOSE(verbose_file);
152 DO_FREE(code_file_name);
154 if (dflag && !dflag2)
155 DO_FREE(defines_file_name);
158 DO_FREE(externs_file_name);
161 DO_FREE(output_file_name);
164 DO_FREE(verbose_file_name);
167 DO_FREE(graph_file_name);
181 onintr(int sig GCC_UNUSED)
191 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
192 signal(SIGINT, onintr);
195 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
196 signal(SIGTERM, onintr);
199 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
200 signal(SIGHUP, onintr);
207 static const char *msg[] =
211 ," -b file_prefix set filename prefix (default \"y.\")"
212 ," -B create a backtracking parser"
213 ," -d write definitions (" DEFINES_SUFFIX ")"
214 ," -D defines_file write definitions to defines_file"
215 ," -i write interface (y.tab.i)"
216 ," -g write a graphical description"
217 ," -l suppress #line directives"
218 ," -L enable position processing, e.g., \"%locations\""
219 ," -o output_file (default \"" OUTPUT_SUFFIX "\")"
220 ," -p symbol_prefix set symbol prefix (default \"yy\")"
221 ," -P create a reentrant parser, e.g., \"%pure-parser\""
222 ," -r produce separate code and table files (y.code.c)"
223 ," -s suppress #define's for quoted names in %token lines"
224 ," -t add debugging support"
225 ," -v write description (y.output)"
226 ," -V show version information and exit"
231 fprintf(stderr, "Usage: %s [options] filename\n", myname);
232 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
233 fprintf(stderr, "%s\n", msg[n]);
244 #if defined(YYBTYACC)
247 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
269 #if defined(YYBTYACC)
272 unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
297 printf("%s - %s\n", myname, VERSION);
301 /* noop for bison compatibility. byacc is already designed to be posix
302 * yacc compatible. */
311 getargs(int argc, char *argv[])
320 for (i = 1; i < argc; ++i)
335 goto no_more_options;
341 file_prefix = argv[i];
349 defines_file_name = s;
351 defines_file_name = argv[i];
358 output_file_name = s;
360 output_file_name = argv[i];
369 symbol_prefix = argv[i];
397 input_file_name_len = strlen(argv[i]);
398 input_file_name = TMALLOC(char, input_file_name_len + 1);
399 NO_SPACE(input_file_name);
400 strcpy(input_file_name, argv[i]);
417 #define CREATE_FILE_NAME(dest, suffix) \
418 dest = alloc_file_name(len, suffix)
421 alloc_file_name(size_t len, const char *suffix)
423 char *result = TMALLOC(char, len + strlen(suffix) + 1);
426 strcpy(result, file_prefix);
427 strcpy(result + len, suffix);
432 find_suffix(char *name, const char *suffix)
434 size_t len = strlen(name);
435 size_t slen = strlen(suffix);
439 if (strcmp(name, suffix) == 0)
446 create_file_names(void)
449 const char *defines_suffix;
450 const char *externs_suffix;
454 defines_suffix = DEFINES_SUFFIX;
455 externs_suffix = EXTERNS_SUFFIX;
457 /* compute the file_prefix from the user provided output_file_name */
458 if (output_file_name != 0)
460 if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
461 && (suffix = find_suffix(output_file_name, ".c")))
463 defines_suffix = ".h";
464 externs_suffix = ".i";
470 len = (size_t) (suffix - output_file_name);
471 file_prefix = TMALLOC(char, len + 1);
472 NO_SPACE(file_prefix);
473 strncpy(file_prefix, output_file_name, len)[len] = 0;
476 len = strlen(file_prefix);
478 /* if "-o filename" was not given */
479 if (output_file_name == 0)
482 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
487 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
490 code_file_name = output_file_name;
492 if (dflag && !dflag2)
494 CREATE_FILE_NAME(defines_file_name, defines_suffix);
499 CREATE_FILE_NAME(externs_file_name, externs_suffix);
504 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
509 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
522 while (my_tmpfiles != 0)
524 MY_TMPFILES *next = my_tmpfiles->next;
526 (void)chmod(my_tmpfiles->name, 0644);
527 (void)unlink(my_tmpfiles->name);
529 free(my_tmpfiles->name);
538 my_mkstemp(char *temp)
546 * Split-up to use tempnam, rather than tmpnam; the latter (like
547 * mkstemp) is unusable on Windows.
549 if ((fname = strrchr(temp, '/')) != 0)
551 dname = strdup(temp);
552 dname[++fname - temp] = '\0';
559 if ((name = tempnam(dname, fname)) != 0)
561 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
574 #define mkstemp(s) my_mkstemp(s)
580 * tmpfile() should be adequate, except that it may require special privileges
581 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
584 open_tmpfile(const char *label)
586 #define MY_FMT "%s/%.*sXXXXXX"
594 if (((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) ||
595 ((tmpdir = getenv("TEMP")) == 0 || access(tmpdir, W_OK) != 0))
602 if (access(tmpdir, W_OK) != 0)
606 /* The size of the format is guaranteed to be longer than the result from
607 * printing empty strings with it; this calculation accounts for the
608 * string-lengths as well.
610 name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
615 mode_t save_umask = umask(0177);
617 if ((mark = strrchr(label, '_')) == 0)
618 mark = label + strlen(label);
620 sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
624 result = fdopen(fd, "w+");
629 if (my_tmpfiles == 0)
631 atexit(close_tmpfiles);
634 item = NEW(MY_TMPFILES);
638 NO_SPACE(item->name);
640 item->next = my_tmpfiles;
644 (void)umask(save_umask);
663 input_file = fopen(input_file_name, "r");
665 open_error(input_file_name);
668 action_file = open_tmpfile("action_file");
669 text_file = open_tmpfile("text_file");
673 verbose_file = fopen(verbose_file_name, "w");
674 if (verbose_file == 0)
675 open_error(verbose_file_name);
680 graph_file = fopen(graph_file_name, "w");
682 open_error(graph_file_name);
683 fprintf(graph_file, "digraph %s {\n", file_prefix);
684 fprintf(graph_file, "\tedge [fontsize=10];\n");
685 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
686 fprintf(graph_file, "\torientation=landscape;\n");
687 fprintf(graph_file, "\trankdir=LR;\n");
688 fprintf(graph_file, "\t/*\n");
689 fprintf(graph_file, "\tmargin=0.2;\n");
690 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
691 fprintf(graph_file, "\tratio=auto;\n");
692 fprintf(graph_file, "\t*/\n");
697 defines_file = fopen(defines_file_name, "w");
698 if (defines_file == 0)
699 open_error(defines_file_name);
700 union_file = open_tmpfile("union_file");
705 externs_file = fopen(externs_file_name, "w");
706 if (externs_file == 0)
707 open_error(externs_file_name);
710 output_file = fopen(output_file_name, "w");
711 if (output_file == 0)
712 open_error(output_file_name);
716 code_file = fopen(code_file_name, "w");
718 open_error(code_file_name);
721 code_file = output_file;
725 main(int argc, char *argv[])
729 exit_code = EXIT_SUCCESS;