1 /* $Id: main.c,v 1.56 2016/03/25 00:16:28 Jung-uk.Kim 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 find_suffix(char *name, const char *suffix)
416 size_t len = strlen(name);
417 size_t slen = strlen(suffix);
421 if (strcmp(name, suffix) == 0)
428 create_file_names(void)
431 const char *defines_suffix;
432 const char *externs_suffix;
436 defines_suffix = DEFINES_SUFFIX;
437 externs_suffix = EXTERNS_SUFFIX;
439 /* compute the file_prefix from the user provided output_file_name */
440 if (output_file_name != 0)
442 if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
443 && (suffix = find_suffix(output_file_name, ".c")))
445 defines_suffix = ".h";
446 externs_suffix = ".i";
452 len = (size_t) (suffix - output_file_name);
453 file_prefix = TMALLOC(char, len + 1);
454 NO_SPACE(file_prefix);
455 strncpy(file_prefix, output_file_name, len)[len] = 0;
458 len = strlen(file_prefix);
460 /* if "-o filename" was not given */
461 if (output_file_name == 0)
464 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
469 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
472 code_file_name = output_file_name;
476 CREATE_FILE_NAME(defines_file_name, defines_suffix);
481 CREATE_FILE_NAME(externs_file_name, externs_suffix);
486 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
491 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
504 while (my_tmpfiles != 0)
506 MY_TMPFILES *next = my_tmpfiles->next;
508 (void)chmod(my_tmpfiles->name, 0644);
509 (void)unlink(my_tmpfiles->name);
511 free(my_tmpfiles->name);
520 my_mkstemp(char *temp)
528 * Split-up to use tempnam, rather than tmpnam; the latter (like
529 * mkstemp) is unusable on Windows.
531 if ((fname = strrchr(temp, '/')) != 0)
533 dname = strdup(temp);
534 dname[++fname - temp] = '\0';
541 if ((name = tempnam(dname, fname)) != 0)
543 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
556 #define mkstemp(s) my_mkstemp(s)
562 * tmpfile() should be adequate, except that it may require special privileges
563 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
566 open_tmpfile(const char *label)
568 #define MY_FMT "%s/%.*sXXXXXX"
576 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
583 if (access(tmpdir, W_OK) != 0)
587 /* The size of the format is guaranteed to be longer than the result from
588 * printing empty strings with it; this calculation accounts for the
589 * string-lengths as well.
591 name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
596 mode_t save_umask = umask(0177);
598 if ((mark = strrchr(label, '_')) == 0)
599 mark = label + strlen(label);
601 sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
605 result = fdopen(fd, "w+");
610 if (my_tmpfiles == 0)
612 atexit(close_tmpfiles);
615 item = NEW(MY_TMPFILES);
619 NO_SPACE(item->name);
621 item->next = my_tmpfiles;
625 (void)umask(save_umask);
644 input_file = fopen(input_file_name, "r");
646 open_error(input_file_name);
649 action_file = open_tmpfile("action_file");
650 text_file = open_tmpfile("text_file");
654 verbose_file = fopen(verbose_file_name, "w");
655 if (verbose_file == 0)
656 open_error(verbose_file_name);
661 graph_file = fopen(graph_file_name, "w");
663 open_error(graph_file_name);
664 fprintf(graph_file, "digraph %s {\n", file_prefix);
665 fprintf(graph_file, "\tedge [fontsize=10];\n");
666 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
667 fprintf(graph_file, "\torientation=landscape;\n");
668 fprintf(graph_file, "\trankdir=LR;\n");
669 fprintf(graph_file, "\t/*\n");
670 fprintf(graph_file, "\tmargin=0.2;\n");
671 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
672 fprintf(graph_file, "\tratio=auto;\n");
673 fprintf(graph_file, "\t*/\n");
678 defines_file = fopen(defines_file_name, "w");
679 if (defines_file == 0)
680 open_error(defines_file_name);
681 union_file = open_tmpfile("union_file");
686 externs_file = fopen(externs_file_name, "w");
687 if (externs_file == 0)
688 open_error(externs_file_name);
691 output_file = fopen(output_file_name, "w");
692 if (output_file == 0)
693 open_error(output_file_name);
697 code_file = fopen(code_file_name, "w");
699 open_error(code_file_name);
702 code_file = output_file;
706 main(int argc, char *argv[])
710 exit_code = EXIT_SUCCESS;