1 /* $Id: main.c,v 1.70 2020/09/10 17:32:55 tom Exp $ */
4 #if !defined(_WIN32) || defined(__MINGW32__)
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 ," -H 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 while ((ch = getopt(argc, argv, "Bb:dgH:ilLo:Pp:rstVvy")) != -1)
325 file_prefix = optarg;
329 defines_file_name = optarg;
332 output_file_name = optarg;
335 symbol_prefix = optarg;
342 if ((i = optind) < argc)
344 /* getopt handles "--" specially, while we handle "-" specially */
345 if (!strcmp(argv[i], "-"))
360 for (i = 1; i < argc; ++i)
375 goto no_more_options;
381 file_prefix = argv[i];
389 defines_file_name = s;
391 defines_file_name = argv[i];
398 output_file_name = s;
400 output_file_name = argv[i];
409 symbol_prefix = argv[i];
436 #endif /* HAVE_GETOPT */
439 input_file_name_len = strlen(argv[i]);
440 input_file_name = TMALLOC(char, input_file_name_len + 1);
441 NO_SPACE(input_file_name);
442 strcpy(input_file_name, argv[i]);
459 #define CREATE_FILE_NAME(dest, suffix) \
460 dest = alloc_file_name(len, suffix)
463 alloc_file_name(size_t len, const char *suffix)
465 char *result = TMALLOC(char, len + strlen(suffix) + 1);
468 strcpy(result, file_prefix);
469 strcpy(result + len, suffix);
474 find_suffix(char *name, const char *suffix)
476 size_t len = strlen(name);
477 size_t slen = strlen(suffix);
481 if (strcmp(name, suffix) == 0)
488 create_file_names(void)
491 const char *defines_suffix;
492 const char *externs_suffix;
496 defines_suffix = DEFINES_SUFFIX;
497 externs_suffix = EXTERNS_SUFFIX;
499 /* compute the file_prefix from the user provided output_file_name */
500 if (output_file_name != 0)
502 if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
503 && (suffix = find_suffix(output_file_name, ".c")))
505 defines_suffix = ".h";
506 externs_suffix = ".i";
512 len = (size_t) (suffix - output_file_name);
513 file_prefix = TMALLOC(char, len + 1);
514 NO_SPACE(file_prefix);
515 strncpy(file_prefix, output_file_name, len)[len] = 0;
518 len = strlen(file_prefix);
520 /* if "-o filename" was not given */
521 if (output_file_name == 0)
524 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
529 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
532 code_file_name = output_file_name;
534 if (dflag && !dflag2)
536 CREATE_FILE_NAME(defines_file_name, defines_suffix);
541 CREATE_FILE_NAME(externs_file_name, externs_suffix);
546 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
551 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
564 while (my_tmpfiles != 0)
566 MY_TMPFILES *next = my_tmpfiles->next;
568 (void)chmod(my_tmpfiles->name, 0644);
569 (void)unlink(my_tmpfiles->name);
571 free(my_tmpfiles->name);
580 my_mkstemp(char *temp)
588 * Split-up to use tempnam, rather than tmpnam; the latter (like
589 * mkstemp) is unusable on Windows.
591 if ((fname = strrchr(temp, '/')) != 0)
593 dname = strdup(temp);
594 dname[++fname - temp] = '\0';
601 if ((name = tempnam(dname, fname)) != 0)
603 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
616 #define mkstemp(s) my_mkstemp(s)
622 * tmpfile() should be adequate, except that it may require special privileges
623 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
626 open_tmpfile(const char *label)
628 #define MY_FMT "%s/%.*sXXXXXX"
634 if (((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) ||
635 ((tmpdir = getenv("TEMP")) == 0 || access(tmpdir, W_OK) != 0))
642 if (access(tmpdir, W_OK) != 0)
646 /* The size of the format is guaranteed to be longer than the result from
647 * printing empty strings with it; this calculation accounts for the
648 * string-lengths as well.
650 name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
658 mode_t save_umask = umask(0177);
660 if ((mark = strrchr(label, '_')) == 0)
661 mark = label + strlen(label);
663 sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
666 && (result = fdopen(fd, "w+")) != 0)
670 if (my_tmpfiles == 0)
672 atexit(close_tmpfiles);
675 item = NEW(MY_TMPFILES);
679 NO_SPACE(item->name);
681 item->next = my_tmpfiles;
688 (void)umask(save_umask);
707 input_file = fopen(input_file_name, "r");
709 open_error(input_file_name);
712 action_file = open_tmpfile("action_file");
713 text_file = open_tmpfile("text_file");
717 verbose_file = fopen(verbose_file_name, "w");
718 if (verbose_file == 0)
719 open_error(verbose_file_name);
724 graph_file = fopen(graph_file_name, "w");
726 open_error(graph_file_name);
727 fprintf(graph_file, "digraph %s {\n", file_prefix);
728 fprintf(graph_file, "\tedge [fontsize=10];\n");
729 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
730 fprintf(graph_file, "\torientation=landscape;\n");
731 fprintf(graph_file, "\trankdir=LR;\n");
732 fprintf(graph_file, "\t/*\n");
733 fprintf(graph_file, "\tmargin=0.2;\n");
734 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
735 fprintf(graph_file, "\tratio=auto;\n");
736 fprintf(graph_file, "\t*/\n");
741 defines_file = fopen(defines_file_name, "w");
742 if (defines_file == 0)
743 open_error(defines_file_name);
744 union_file = open_tmpfile("union_file");
749 externs_file = fopen(externs_file_name, "w");
750 if (externs_file == 0)
751 open_error(externs_file_name);
754 output_file = fopen(output_file_name, "w");
755 if (output_file == 0)
756 open_error(output_file_name);
760 code_file = fopen(code_file_name, "w");
762 open_error(code_file_name);
765 code_file = output_file;
769 main(int argc, char *argv[])
773 exit_code = EXIT_SUCCESS;