1 /* $Id: main.c,v 1.35 2010/12/27 01:21:59 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 */
43 const char *symbol_prefix;
44 const char *myname = "yacc";
49 static char empty_string[] = "";
50 static char default_file_prefix[] = "y";
52 static char *file_prefix = default_file_prefix;
55 char *input_file_name = empty_string;
56 static char *defines_file_name;
57 static char *graph_file_name;
58 static char *output_file_name;
59 static char *verbose_file_name;
61 FILE *action_file; /* a temp file, used to save actions associated */
62 /* with rules until the parser is written */
63 FILE *code_file; /* y.code.c (used when the -r option is specified) */
64 FILE *defines_file; /* y.tab.h */
65 FILE *input_file; /* the input file */
66 FILE *output_file; /* y.tab.c */
67 FILE *text_file; /* a temp file, used to save text until all */
68 /* symbols have been defined */
69 FILE *union_file; /* a temp file, used to save the union */
70 /* definition until all symbol have been */
72 FILE *verbose_file; /* y.output */
73 FILE *graph_file; /* y.dot */
84 Value_t *symbol_value;
100 * Since fclose() is called via the signal handler, it might die. Don't loop
101 * if there is a problem closing a file.
103 #define DO_CLOSE(fp) \
110 static int got_intr = 0;
115 DO_CLOSE(input_file);
116 DO_CLOSE(output_file);
118 DO_CLOSE(action_file);
119 DO_CLOSE(defines_file);
120 DO_CLOSE(graph_file);
122 DO_CLOSE(union_file);
123 DO_CLOSE(verbose_file);
130 DO_FREE(code_file_name);
133 DO_FREE(defines_file_name);
136 DO_FREE(output_file_name);
139 DO_FREE(verbose_file_name);
142 DO_FREE(graph_file_name);
158 onintr(int sig GCC_UNUSED)
168 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
169 signal(SIGINT, onintr);
172 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
173 signal(SIGTERM, onintr);
176 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
177 signal(SIGHUP, onintr);
184 static const char *msg[] =
188 ," -b file_prefix set filename prefix (default \"y.\")"
189 ," -d write definitions (y.tab.h)"
190 ," -g write a graphical description"
191 ," -l suppress #line directives"
192 ," -o output_file (default \"y.tab.c\")"
193 ," -p symbol_prefix set symbol prefix (default \"yy\")"
194 ," -P create a reentrant parser, e.g., \"%pure-parser\""
195 ," -r produce separate code and table files (y.code.c)"
196 ," -t add debugging support"
197 ," -v write description (y.output)"
198 ," -V show version information and exit"
203 fprintf(stderr, "Usage: %s [options] filename\n", myname);
204 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
205 fprintf(stderr, "%s\n", msg[n]);
244 printf("%s - %s\n", myname, VERSION);
248 /* noop for bison compatibility. byacc is already designed to be posix
249 * yacc compatible. */
258 getargs(int argc, char *argv[])
267 for (i = 1; i < argc; ++i)
282 goto no_more_options;
288 file_prefix = argv[i];
295 output_file_name = s;
297 output_file_name = argv[i];
306 symbol_prefix = argv[i];
334 input_file_name = argv[i];
351 #define CREATE_FILE_NAME(dest, suffix) \
352 dest = MALLOC(len + strlen(suffix) + 1); \
354 strcpy(dest, file_prefix); \
355 strcpy(dest + len, suffix)
358 create_file_names(void)
361 const char *defines_suffix;
365 defines_suffix = DEFINES_SUFFIX;
367 /* compute the file_prefix from the user provided output_file_name */
368 if (output_file_name != 0)
370 if (!(prefix = strstr(output_file_name, ".tab.c"))
371 && (prefix = strstr(output_file_name, ".c")))
372 defines_suffix = ".h";
377 len = (size_t) (prefix - output_file_name);
378 file_prefix = (char *)MALLOC(len + 1);
379 NO_SPACE(file_prefix);
380 strncpy(file_prefix, output_file_name, len)[len] = 0;
383 len = strlen(file_prefix);
385 /* if "-o filename" was not given */
386 if (output_file_name == 0)
389 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
394 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
397 code_file_name = output_file_name;
401 CREATE_FILE_NAME(defines_file_name, defines_suffix);
406 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
411 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
424 while (my_tmpfiles != 0)
426 MY_TMPFILES *next = my_tmpfiles->next;
428 chmod(my_tmpfiles->name, 0644);
429 unlink(my_tmpfiles->name);
431 free(my_tmpfiles->name);
440 my_mkstemp(char *temp)
448 * Split-up to use tempnam, rather than tmpnam; the latter (like
449 * mkstemp) is unusable on Windows.
451 if ((fname = strrchr(temp, '/')) != 0)
453 dname = strdup(temp);
454 dname[++fname - temp] = '\0';
461 if ((name = tempnam(dname, fname)) != 0)
463 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
476 #define mkstemp(s) my_mkstemp(s)
482 * tmpfile() should be adequate, except that it may require special privileges
483 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
486 open_tmpfile(const char *label)
495 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
502 if (access(tmpdir, W_OK) != 0)
506 name = malloc(strlen(tmpdir) + 10 + strlen(label));
511 if ((mark = strrchr(label, '_')) == 0)
512 mark = label + strlen(label);
514 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
518 result = fdopen(fd, "w+");
523 if (my_tmpfiles == 0)
525 atexit(close_tmpfiles);
528 item = NEW(MY_TMPFILES);
532 NO_SPACE(item->name);
534 item->next = my_tmpfiles;
555 input_file = fopen(input_file_name, "r");
557 open_error(input_file_name);
560 action_file = open_tmpfile("action_file");
561 text_file = open_tmpfile("text_file");
565 verbose_file = fopen(verbose_file_name, "w");
566 if (verbose_file == 0)
567 open_error(verbose_file_name);
572 graph_file = fopen(graph_file_name, "w");
574 open_error(graph_file_name);
575 fprintf(graph_file, "digraph %s {\n", file_prefix);
576 fprintf(graph_file, "\tedge [fontsize=10];\n");
577 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
578 fprintf(graph_file, "\torientation=landscape;\n");
579 fprintf(graph_file, "\trankdir=LR;\n");
580 fprintf(graph_file, "\t/*\n");
581 fprintf(graph_file, "\tmargin=0.2;\n");
582 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
583 fprintf(graph_file, "\tratio=auto;\n");
584 fprintf(graph_file, "\t*/\n");
589 defines_file = fopen(defines_file_name, "w");
590 if (defines_file == 0)
591 open_error(defines_file_name);
592 union_file = open_tmpfile("union_file");
595 output_file = fopen(output_file_name, "w");
596 if (output_file == 0)
597 open_error(output_file_name);
601 code_file = fopen(code_file_name, "w");
603 open_error(code_file_name);
606 code_file = output_file;
610 main(int argc, char *argv[])
614 exit_code = EXIT_SUCCESS;