1 /* $Id: main.c,v 1.36 2011/09/06 22:44:45 tom Exp $ */
4 #include <unistd.h> /* for _exit() */
8 #if defined(HAVE_ATEXIT)
10 # define USE_MKSTEMP 1
11 # elif defined(HAVE_FCNTL_H)
12 # define USE_MKSTEMP 1
13 # include <fcntl.h> /* for open(), O_EXCL, etc. */
15 # define USE_MKSTEMP 0
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 */
44 const char *symbol_prefix;
45 const char *myname = "yacc";
50 static char empty_string[] = "";
51 static char default_file_prefix[] = "y";
53 static char *file_prefix = default_file_prefix;
56 char *input_file_name = empty_string;
57 char *defines_file_name;
58 char *externs_file_name;
60 static char *graph_file_name;
61 static char *output_file_name;
62 static char *verbose_file_name;
64 FILE *action_file; /* a temp file, used to save actions associated */
65 /* with rules until the parser is written */
66 FILE *code_file; /* y.code.c (used when the -r option is specified) */
67 FILE *defines_file; /* y.tab.h */
68 FILE *externs_file; /* y.tab.i */
69 FILE *input_file; /* the input file */
70 FILE *output_file; /* y.tab.c */
71 FILE *text_file; /* a temp file, used to save text until all */
72 /* symbols have been defined */
73 FILE *union_file; /* a temp file, used to save the union */
74 /* definition until all symbol have been */
76 FILE *verbose_file; /* y.output */
77 FILE *graph_file; /* y.dot */
88 Value_t *symbol_value;
104 * Since fclose() is called via the signal handler, it might die. Don't loop
105 * if there is a problem closing a file.
107 #define DO_CLOSE(fp) \
114 static int got_intr = 0;
119 DO_CLOSE(input_file);
120 DO_CLOSE(output_file);
122 DO_CLOSE(action_file);
123 DO_CLOSE(defines_file);
124 DO_CLOSE(graph_file);
126 DO_CLOSE(union_file);
127 DO_CLOSE(verbose_file);
134 DO_FREE(code_file_name);
137 DO_FREE(defines_file_name);
140 DO_FREE(externs_file_name);
143 DO_FREE(output_file_name);
146 DO_FREE(verbose_file_name);
149 DO_FREE(graph_file_name);
165 onintr(int sig GCC_UNUSED)
175 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
176 signal(SIGINT, onintr);
179 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
180 signal(SIGTERM, onintr);
183 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
184 signal(SIGHUP, onintr);
191 static const char *msg[] =
195 ," -b file_prefix set filename prefix (default \"y.\")"
196 ," -d write definitions (y.tab.h)"
197 ," -i write interface (y.tab.i)"
198 ," -g write a graphical description"
199 ," -l suppress #line directives"
200 ," -o output_file (default \"y.tab.c\")"
201 ," -p symbol_prefix set symbol prefix (default \"yy\")"
202 ," -P create a reentrant parser, e.g., \"%pure-parser\""
203 ," -r produce separate code and table files (y.code.c)"
204 ," -t add debugging support"
205 ," -v write description (y.output)"
206 ," -V show version information and exit"
211 fprintf(stderr, "Usage: %s [options] filename\n", myname);
212 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
213 fprintf(stderr, "%s\n", msg[n]);
256 printf("%s - %s\n", myname, VERSION);
260 /* noop for bison compatibility. byacc is already designed to be posix
261 * yacc compatible. */
270 getargs(int argc, char *argv[])
279 for (i = 1; i < argc; ++i)
294 goto no_more_options;
300 file_prefix = argv[i];
307 output_file_name = s;
309 output_file_name = argv[i];
318 symbol_prefix = argv[i];
346 input_file_name = argv[i];
363 #define CREATE_FILE_NAME(dest, suffix) \
364 dest = MALLOC(len + strlen(suffix) + 1); \
366 strcpy(dest, file_prefix); \
367 strcpy(dest + len, suffix)
370 create_file_names(void)
373 const char *defines_suffix;
374 const char *externs_suffix;
378 defines_suffix = DEFINES_SUFFIX;
379 externs_suffix = EXTERNS_SUFFIX;
381 /* compute the file_prefix from the user provided output_file_name */
382 if (output_file_name != 0)
384 if (!(prefix = strstr(output_file_name, ".tab.c"))
385 && (prefix = strstr(output_file_name, ".c")))
387 defines_suffix = ".h";
388 externs_suffix = ".i";
394 len = (size_t) (prefix - output_file_name);
395 file_prefix = (char *)MALLOC(len + 1);
396 NO_SPACE(file_prefix);
397 strncpy(file_prefix, output_file_name, len)[len] = 0;
400 len = strlen(file_prefix);
402 /* if "-o filename" was not given */
403 if (output_file_name == 0)
406 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
411 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
414 code_file_name = output_file_name;
418 CREATE_FILE_NAME(defines_file_name, defines_suffix);
423 CREATE_FILE_NAME(externs_file_name, externs_suffix);
428 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
433 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
446 while (my_tmpfiles != 0)
448 MY_TMPFILES *next = my_tmpfiles->next;
450 chmod(my_tmpfiles->name, 0644);
451 unlink(my_tmpfiles->name);
453 free(my_tmpfiles->name);
462 my_mkstemp(char *temp)
470 * Split-up to use tempnam, rather than tmpnam; the latter (like
471 * mkstemp) is unusable on Windows.
473 if ((fname = strrchr(temp, '/')) != 0)
475 dname = strdup(temp);
476 dname[++fname - temp] = '\0';
483 if ((name = tempnam(dname, fname)) != 0)
485 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
498 #define mkstemp(s) my_mkstemp(s)
504 * tmpfile() should be adequate, except that it may require special privileges
505 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
508 open_tmpfile(const char *label)
517 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
524 if (access(tmpdir, W_OK) != 0)
528 name = malloc(strlen(tmpdir) + 10 + strlen(label));
533 if ((mark = strrchr(label, '_')) == 0)
534 mark = label + strlen(label);
536 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
540 result = fdopen(fd, "w+");
545 if (my_tmpfiles == 0)
547 atexit(close_tmpfiles);
550 item = NEW(MY_TMPFILES);
554 NO_SPACE(item->name);
556 item->next = my_tmpfiles;
577 input_file = fopen(input_file_name, "r");
579 open_error(input_file_name);
582 action_file = open_tmpfile("action_file");
583 text_file = open_tmpfile("text_file");
587 verbose_file = fopen(verbose_file_name, "w");
588 if (verbose_file == 0)
589 open_error(verbose_file_name);
594 graph_file = fopen(graph_file_name, "w");
596 open_error(graph_file_name);
597 fprintf(graph_file, "digraph %s {\n", file_prefix);
598 fprintf(graph_file, "\tedge [fontsize=10];\n");
599 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
600 fprintf(graph_file, "\torientation=landscape;\n");
601 fprintf(graph_file, "\trankdir=LR;\n");
602 fprintf(graph_file, "\t/*\n");
603 fprintf(graph_file, "\tmargin=0.2;\n");
604 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
605 fprintf(graph_file, "\tratio=auto;\n");
606 fprintf(graph_file, "\t*/\n");
611 defines_file = fopen(defines_file_name, "w");
612 if (defines_file == 0)
613 open_error(defines_file_name);
614 union_file = open_tmpfile("union_file");
619 externs_file = fopen(externs_file_name, "w");
620 if (externs_file == 0)
621 open_error(externs_file_name);
624 output_file = fopen(output_file_name, "w");
625 if (output_file == 0)
626 open_error(output_file_name);
630 code_file = fopen(code_file_name, "w");
632 open_error(code_file_name);
635 code_file = output_file;
639 main(int argc, char *argv[])
643 exit_code = EXIT_SUCCESS;