1 /* $Id: main.c,v 1.38 2012/01/14 01:01:15 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 */
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;
105 * Since fclose() is called via the signal handler, it might die. Don't loop
106 * if there is a problem closing a file.
108 #define DO_CLOSE(fp) \
115 static int got_intr = 0;
120 DO_CLOSE(input_file);
121 DO_CLOSE(output_file);
123 DO_CLOSE(action_file);
124 DO_CLOSE(defines_file);
125 DO_CLOSE(graph_file);
127 DO_CLOSE(union_file);
128 DO_CLOSE(verbose_file);
135 DO_FREE(code_file_name);
138 DO_FREE(defines_file_name);
141 DO_FREE(externs_file_name);
144 DO_FREE(output_file_name);
147 DO_FREE(verbose_file_name);
150 DO_FREE(graph_file_name);
166 onintr(int sig GCC_UNUSED)
176 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
177 signal(SIGINT, onintr);
180 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
181 signal(SIGTERM, onintr);
184 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
185 signal(SIGHUP, onintr);
192 static const char *msg[] =
196 ," -b file_prefix set filename prefix (default \"y.\")"
197 ," -d write definitions (y.tab.h)"
198 ," -i write interface (y.tab.i)"
199 ," -g write a graphical description"
200 ," -l suppress #line directives"
201 ," -o output_file (default \"y.tab.c\")"
202 ," -p symbol_prefix set symbol prefix (default \"yy\")"
203 ," -P create a reentrant parser, e.g., \"%pure-parser\""
204 ," -r produce separate code and table files (y.code.c)"
205 ," -s suppress #define's for quoted names in %token lines"
206 ," -t add debugging support"
207 ," -v write description (y.output)"
208 ," -V show version information and exit"
213 fprintf(stderr, "Usage: %s [options] filename\n", myname);
214 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
215 fprintf(stderr, "%s\n", msg[n]);
262 printf("%s - %s\n", myname, VERSION);
266 /* noop for bison compatibility. byacc is already designed to be posix
267 * yacc compatible. */
276 getargs(int argc, char *argv[])
285 for (i = 1; i < argc; ++i)
300 goto no_more_options;
306 file_prefix = argv[i];
313 output_file_name = s;
315 output_file_name = argv[i];
324 symbol_prefix = argv[i];
352 input_file_name = argv[i];
369 #define CREATE_FILE_NAME(dest, suffix) \
370 dest = MALLOC(len + strlen(suffix) + 1); \
372 strcpy(dest, file_prefix); \
373 strcpy(dest + len, suffix)
376 create_file_names(void)
379 const char *defines_suffix;
380 const char *externs_suffix;
384 defines_suffix = DEFINES_SUFFIX;
385 externs_suffix = EXTERNS_SUFFIX;
387 /* compute the file_prefix from the user provided output_file_name */
388 if (output_file_name != 0)
390 if (!(prefix = strstr(output_file_name, ".tab.c"))
391 && (prefix = strstr(output_file_name, ".c")))
393 defines_suffix = ".h";
394 externs_suffix = ".i";
400 len = (size_t) (prefix - output_file_name);
401 file_prefix = (char *)MALLOC(len + 1);
402 NO_SPACE(file_prefix);
403 strncpy(file_prefix, output_file_name, len)[len] = 0;
406 len = strlen(file_prefix);
408 /* if "-o filename" was not given */
409 if (output_file_name == 0)
412 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
417 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
420 code_file_name = output_file_name;
424 CREATE_FILE_NAME(defines_file_name, defines_suffix);
429 CREATE_FILE_NAME(externs_file_name, externs_suffix);
434 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
439 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
452 while (my_tmpfiles != 0)
454 MY_TMPFILES *next = my_tmpfiles->next;
456 chmod(my_tmpfiles->name, 0644);
457 unlink(my_tmpfiles->name);
459 free(my_tmpfiles->name);
468 my_mkstemp(char *temp)
476 * Split-up to use tempnam, rather than tmpnam; the latter (like
477 * mkstemp) is unusable on Windows.
479 if ((fname = strrchr(temp, '/')) != 0)
481 dname = strdup(temp);
482 dname[++fname - temp] = '\0';
489 if ((name = tempnam(dname, fname)) != 0)
491 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
504 #define mkstemp(s) my_mkstemp(s)
510 * tmpfile() should be adequate, except that it may require special privileges
511 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
514 open_tmpfile(const char *label)
523 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
530 if (access(tmpdir, W_OK) != 0)
534 name = malloc(strlen(tmpdir) + 10 + strlen(label));
539 if ((mark = strrchr(label, '_')) == 0)
540 mark = label + strlen(label);
542 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
546 result = fdopen(fd, "w+");
551 if (my_tmpfiles == 0)
553 atexit(close_tmpfiles);
556 item = NEW(MY_TMPFILES);
560 NO_SPACE(item->name);
562 item->next = my_tmpfiles;
583 input_file = fopen(input_file_name, "r");
585 open_error(input_file_name);
588 action_file = open_tmpfile("action_file");
589 text_file = open_tmpfile("text_file");
593 verbose_file = fopen(verbose_file_name, "w");
594 if (verbose_file == 0)
595 open_error(verbose_file_name);
600 graph_file = fopen(graph_file_name, "w");
602 open_error(graph_file_name);
603 fprintf(graph_file, "digraph %s {\n", file_prefix);
604 fprintf(graph_file, "\tedge [fontsize=10];\n");
605 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
606 fprintf(graph_file, "\torientation=landscape;\n");
607 fprintf(graph_file, "\trankdir=LR;\n");
608 fprintf(graph_file, "\t/*\n");
609 fprintf(graph_file, "\tmargin=0.2;\n");
610 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
611 fprintf(graph_file, "\tratio=auto;\n");
612 fprintf(graph_file, "\t*/\n");
617 defines_file = fopen(defines_file_name, "w");
618 if (defines_file == 0)
619 open_error(defines_file_name);
620 union_file = open_tmpfile("union_file");
625 externs_file = fopen(externs_file_name, "w");
626 if (externs_file == 0)
627 open_error(externs_file_name);
630 output_file = fopen(output_file_name, "w");
631 if (output_file == 0)
632 open_error(output_file_name);
636 code_file = fopen(code_file_name, "w");
638 open_error(code_file_name);
641 code_file = output_file;
645 main(int argc, char *argv[])
649 exit_code = EXIT_SUCCESS;