1 /* $Id: main.c,v 1.40 2012/09/29 13:11:00 Adrian.Bunk Exp $ */
4 #include <unistd.h> /* for _exit() */
10 #elif defined(HAVE_FCNTL_H)
11 # define USE_MKSTEMP 1
12 # include <fcntl.h> /* for open(), O_EXCL, etc. */
14 # define USE_MKSTEMP 0
18 #include <sys/types.h>
21 typedef struct _my_tmpfiles
23 struct _my_tmpfiles *next;
28 static MY_TMPFILES *my_tmpfiles;
29 #endif /* USE_MKSTEMP */
41 const char *symbol_prefix;
42 const char *myname = "yacc";
47 static char empty_string[] = "";
48 static char default_file_prefix[] = "y";
50 static char *file_prefix = default_file_prefix;
53 char *input_file_name = empty_string;
54 char *defines_file_name;
55 char *externs_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 *externs_file; /* y.tab.i */
66 FILE *input_file; /* the input file */
67 FILE *output_file; /* y.tab.c */
68 FILE *text_file; /* a temp file, used to save text until all */
69 /* symbols have been defined */
70 FILE *union_file; /* a temp file, used to save the union */
71 /* definition until all symbol have been */
73 FILE *verbose_file; /* y.output */
74 FILE *graph_file; /* y.dot */
85 Value_t *symbol_value;
101 * Since fclose() is called via the signal handler, it might die. Don't loop
102 * if there is a problem closing a file.
104 #define DO_CLOSE(fp) \
111 static int got_intr = 0;
116 DO_CLOSE(input_file);
117 DO_CLOSE(output_file);
119 DO_CLOSE(action_file);
120 DO_CLOSE(defines_file);
121 DO_CLOSE(graph_file);
123 DO_CLOSE(union_file);
124 DO_CLOSE(verbose_file);
131 DO_FREE(code_file_name);
134 DO_FREE(defines_file_name);
137 DO_FREE(externs_file_name);
140 DO_FREE(output_file_name);
143 DO_FREE(verbose_file_name);
146 DO_FREE(graph_file_name);
162 onintr(int sig GCC_UNUSED)
172 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
173 signal(SIGINT, onintr);
176 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
177 signal(SIGTERM, onintr);
180 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
181 signal(SIGHUP, onintr);
188 static const char *msg[] =
192 ," -b file_prefix set filename prefix (default \"y.\")"
193 ," -d write definitions (y.tab.h)"
194 ," -i write interface (y.tab.i)"
195 ," -g write a graphical description"
196 ," -l suppress #line directives"
197 ," -o output_file (default \"y.tab.c\")"
198 ," -p symbol_prefix set symbol prefix (default \"yy\")"
199 ," -P create a reentrant parser, e.g., \"%pure-parser\""
200 ," -r produce separate code and table files (y.code.c)"
201 ," -s suppress #define's for quoted names in %token lines"
202 ," -t add debugging support"
203 ," -v write description (y.output)"
204 ," -V show version information and exit"
209 fprintf(stderr, "Usage: %s [options] filename\n", myname);
210 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
211 fprintf(stderr, "%s\n", msg[n]);
258 printf("%s - %s\n", myname, VERSION);
262 /* noop for bison compatibility. byacc is already designed to be posix
263 * yacc compatible. */
272 getargs(int argc, char *argv[])
281 for (i = 1; i < argc; ++i)
296 goto no_more_options;
302 file_prefix = argv[i];
309 output_file_name = s;
311 output_file_name = argv[i];
320 symbol_prefix = argv[i];
348 input_file_name = argv[i];
365 #define CREATE_FILE_NAME(dest, suffix) \
366 dest = TMALLOC(char, len + strlen(suffix) + 1); \
368 strcpy(dest, file_prefix); \
369 strcpy(dest + len, suffix)
372 create_file_names(void)
375 const char *defines_suffix;
376 const char *externs_suffix;
380 defines_suffix = DEFINES_SUFFIX;
381 externs_suffix = EXTERNS_SUFFIX;
383 /* compute the file_prefix from the user provided output_file_name */
384 if (output_file_name != 0)
386 if (!(prefix = strstr(output_file_name, ".tab.c"))
387 && (prefix = strstr(output_file_name, ".c")))
389 defines_suffix = ".h";
390 externs_suffix = ".i";
396 len = (size_t) (prefix - output_file_name);
397 file_prefix = TMALLOC(char, len + 1);
398 NO_SPACE(file_prefix);
399 strncpy(file_prefix, output_file_name, len)[len] = 0;
402 len = strlen(file_prefix);
404 /* if "-o filename" was not given */
405 if (output_file_name == 0)
408 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
413 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
416 code_file_name = output_file_name;
420 CREATE_FILE_NAME(defines_file_name, defines_suffix);
425 CREATE_FILE_NAME(externs_file_name, externs_suffix);
430 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
435 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
448 while (my_tmpfiles != 0)
450 MY_TMPFILES *next = my_tmpfiles->next;
452 chmod(my_tmpfiles->name, 0644);
453 unlink(my_tmpfiles->name);
455 free(my_tmpfiles->name);
464 my_mkstemp(char *temp)
472 * Split-up to use tempnam, rather than tmpnam; the latter (like
473 * mkstemp) is unusable on Windows.
475 if ((fname = strrchr(temp, '/')) != 0)
477 dname = strdup(temp);
478 dname[++fname - temp] = '\0';
485 if ((name = tempnam(dname, fname)) != 0)
487 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
500 #define mkstemp(s) my_mkstemp(s)
506 * tmpfile() should be adequate, except that it may require special privileges
507 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
510 open_tmpfile(const char *label)
519 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
526 if (access(tmpdir, W_OK) != 0)
530 name = malloc(strlen(tmpdir) + 10 + strlen(label));
535 if ((mark = strrchr(label, '_')) == 0)
536 mark = label + strlen(label);
538 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
542 result = fdopen(fd, "w+");
547 if (my_tmpfiles == 0)
549 atexit(close_tmpfiles);
552 item = NEW(MY_TMPFILES);
556 NO_SPACE(item->name);
558 item->next = my_tmpfiles;
579 input_file = fopen(input_file_name, "r");
581 open_error(input_file_name);
584 action_file = open_tmpfile("action_file");
585 text_file = open_tmpfile("text_file");
589 verbose_file = fopen(verbose_file_name, "w");
590 if (verbose_file == 0)
591 open_error(verbose_file_name);
596 graph_file = fopen(graph_file_name, "w");
598 open_error(graph_file_name);
599 fprintf(graph_file, "digraph %s {\n", file_prefix);
600 fprintf(graph_file, "\tedge [fontsize=10];\n");
601 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
602 fprintf(graph_file, "\torientation=landscape;\n");
603 fprintf(graph_file, "\trankdir=LR;\n");
604 fprintf(graph_file, "\t/*\n");
605 fprintf(graph_file, "\tmargin=0.2;\n");
606 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
607 fprintf(graph_file, "\tratio=auto;\n");
608 fprintf(graph_file, "\t*/\n");
613 defines_file = fopen(defines_file_name, "w");
614 if (defines_file == 0)
615 open_error(defines_file_name);
616 union_file = open_tmpfile("union_file");
621 externs_file = fopen(externs_file_name, "w");
622 if (externs_file == 0)
623 open_error(externs_file_name);
626 output_file = fopen(output_file_name, "w");
627 if (output_file == 0)
628 open_error(output_file_name);
632 code_file = fopen(code_file_name, "w");
634 open_error(code_file_name);
637 code_file = output_file;
641 main(int argc, char *argv[])
645 exit_code = EXIT_SUCCESS;