1 /* $NetBSD: main.c,v 1.11 2015/01/04 01:34:20 christos Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.11 2015/01/04 01:34:20 christos Exp $");
7 /* Id: main.c,v 1.54 2014/10/06 22:40:07 tom Exp */
11 #include <unistd.h> /* for _exit() */
13 #include <stdlib.h> /* for _exit() */
18 # define USE_MKSTEMP 1
19 #elif defined(HAVE_FCNTL_H)
20 # define USE_MKSTEMP 1
21 # include <fcntl.h> /* for open(), O_EXCL, etc. */
23 # define USE_MKSTEMP 0
27 #include <sys/types.h>
30 typedef struct _my_tmpfiles
32 struct _my_tmpfiles
*next
;
37 static MY_TMPFILES
*my_tmpfiles
;
38 #endif /* USE_MKSTEMP */
50 const char *symbol_prefix
;
51 const char *myname
= "yacc";
56 static char empty_string
[] = "";
57 static char default_file_prefix
[] = "y";
58 static int explicit_file_name
;
60 static char *file_prefix
= default_file_prefix
;
63 char *input_file_name
= empty_string
;
64 char *defines_file_name
;
65 char *externs_file_name
;
67 static char *graph_file_name
;
68 static char *output_file_name
;
69 static char *verbose_file_name
;
71 FILE *action_file
; /* a temp file, used to save actions associated */
72 /* with rules until the parser is written */
73 FILE *code_file
; /* y.code.c (used when the -r option is specified) */
74 FILE *defines_file
; /* y.tab.h */
75 FILE *externs_file
; /* y.tab.i */
76 FILE *input_file
; /* the input file */
77 FILE *output_file
; /* y.tab.c */
78 FILE *text_file
; /* a temp file, used to save text until all */
79 /* symbols have been defined */
80 FILE *union_file
; /* a temp file, used to save the union */
81 /* definition until all symbol have been */
83 FILE *verbose_file
; /* y.output */
84 FILE *graph_file
; /* y.dot */
95 Value_t
*symbol_value
;
103 #if defined(YYBTYACC)
104 Value_t
*symbol_pval
;
105 char **symbol_destructor
;
106 char **symbol_type_tag
;
107 int locations
= 0; /* default to no position processing */
108 int backtrack
= 0; /* default is no backtracking */
109 char *initial_action
= NULL
;
123 * Since fclose() is called via the signal handler, it might die. Don't loop
124 * if there is a problem closing a file.
126 #define DO_CLOSE(fp) \
133 static int got_intr
= 0;
138 DO_CLOSE(input_file
);
139 DO_CLOSE(output_file
);
141 DO_CLOSE(externs_file
);
145 DO_CLOSE(action_file
);
146 DO_CLOSE(defines_file
);
147 DO_CLOSE(graph_file
);
149 DO_CLOSE(union_file
);
150 DO_CLOSE(verbose_file
);
157 DO_FREE(code_file_name
);
160 DO_FREE(defines_file_name
);
163 DO_FREE(externs_file_name
);
166 DO_FREE(output_file_name
);
169 DO_FREE(verbose_file_name
);
172 DO_FREE(graph_file_name
);
186 onintr(int sig GCC_UNUSED
)
196 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
197 signal(SIGINT
, onintr
);
200 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
201 signal(SIGTERM
, onintr
);
204 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
205 signal(SIGHUP
, onintr
);
212 static const char *msg
[] =
216 ," -b file_prefix set filename prefix (default \"y.\")"
217 ," -B create a backtracking parser"
218 ," -d write definitions (" DEFINES_SUFFIX
")"
219 ," -i write interface (y.tab.i)"
220 ," -g write a graphical description"
221 ," -l suppress #line directives"
222 ," -L enable position processing, e.g., \"%locations\""
223 ," -o output_file (default \"" OUTPUT_SUFFIX
"\")"
224 ," -p symbol_prefix set symbol prefix (default \"yy\")"
225 ," -P create a reentrant parser, e.g., \"%pure-parser\""
226 ," -r produce separate code and table files (y.code.c)"
227 ," -s suppress #define's for quoted names in %token lines"
228 ," -t add debugging support"
229 ," -v write description (y.output)"
230 ," -V show version information and exit"
235 fprintf(stderr
, "Usage: %s [options] filename\n", myname
);
236 for (n
= 0; n
< sizeof(msg
) / sizeof(msg
[0]); ++n
)
237 fprintf(stderr
, "%s\n", msg
[n
]);
248 #if defined(YYBTYACC)
251 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
272 #if defined(YYBTYACC)
275 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
300 printf("%s - %s\n", myname
, VERSION
);
304 /* noop for bison compatibility. byacc is already designed to be posix
305 * yacc compatible. */
314 getargs(int argc
, char *argv
[])
323 for (i
= 1; i
< argc
; ++i
)
338 goto no_more_options
;
344 file_prefix
= argv
[i
];
351 output_file_name
= s
;
353 output_file_name
= argv
[i
];
356 explicit_file_name
= 1;
363 symbol_prefix
= argv
[i
];
391 input_file_name
= argv
[i
];
408 #define CREATE_FILE_NAME(dest, suffix) \
409 dest = alloc_file_name(len, suffix)
412 alloc_file_name(size_t len
, const char *suffix
)
414 char *result
= TMALLOC(char, len
+ strlen(suffix
) + 1);
417 strcpy(result
, file_prefix
);
418 strcpy(result
+ len
, suffix
);
423 create_file_names(void)
426 const char *defines_suffix
;
427 const char *externs_suffix
;
431 defines_suffix
= DEFINES_SUFFIX
;
432 externs_suffix
= EXTERNS_SUFFIX
;
434 /* compute the file_prefix from the user provided output_file_name */
435 if (output_file_name
!= 0)
437 if (!(prefix
= strstr(output_file_name
, OUTPUT_SUFFIX
))
438 && (prefix
= strstr(output_file_name
, ".c")))
440 defines_suffix
= ".h";
441 externs_suffix
= ".i";
447 len
= (size_t) (prefix
- output_file_name
);
448 file_prefix
= TMALLOC(char, len
+ 1);
449 NO_SPACE(file_prefix
);
450 strncpy(file_prefix
, output_file_name
, len
)[len
] = 0;
453 len
= strlen(file_prefix
);
455 /* if "-o filename" was not given */
456 if (output_file_name
== 0)
459 CREATE_FILE_NAME(output_file_name
, OUTPUT_SUFFIX
);
464 CREATE_FILE_NAME(code_file_name
, CODE_SUFFIX
);
467 code_file_name
= output_file_name
;
471 if (explicit_file_name
)
474 defines_file_name
= strdup(output_file_name
);
475 if (defines_file_name
== 0)
477 /* does the output_file_name have a known suffix */
478 suffix
= strrchr(output_file_name
, '.');
480 (!strcmp(suffix
, ".c") || /* good, old-fashioned C */
481 !strcmp(suffix
, ".C") || /* C++, or C on Windows */
482 !strcmp(suffix
, ".cc") || /* C++ */
483 !strcmp(suffix
, ".cxx") || /* C++ */
484 !strcmp(suffix
, ".cpp"))) /* C++ (Windows) */
486 strncpy(defines_file_name
, output_file_name
,
487 suffix
- output_file_name
+ 1);
488 defines_file_name
[suffix
- output_file_name
+ 1] = 'h';
489 defines_file_name
[suffix
- output_file_name
+ 2] = 0;
491 fprintf(stderr
,"%s: suffix of output file name %s"
492 " not recognized, no -d file generated.\n",
493 myname
, output_file_name
);
495 free(defines_file_name
);
496 defines_file_name
= 0;
499 CREATE_FILE_NAME(defines_file_name
, defines_suffix
);
505 CREATE_FILE_NAME(externs_file_name
, externs_suffix
);
510 CREATE_FILE_NAME(verbose_file_name
, VERBOSE_SUFFIX
);
515 CREATE_FILE_NAME(graph_file_name
, GRAPH_SUFFIX
);
528 while (my_tmpfiles
!= 0)
530 MY_TMPFILES
*next
= my_tmpfiles
->next
;
532 (void)chmod(my_tmpfiles
->name
, 0644);
533 (void)unlink(my_tmpfiles
->name
);
535 free(my_tmpfiles
->name
);
544 my_mkstemp(char *temp
)
552 * Split-up to use tempnam, rather than tmpnam; the latter (like
553 * mkstemp) is unusable on Windows.
555 if ((fname
= strrchr(temp
, '/')) != 0)
557 dname
= strdup(temp
);
558 dname
[++fname
- temp
] = '\0';
565 if ((name
= tempnam(dname
, fname
)) != 0)
567 fd
= open(name
, O_CREAT
| O_EXCL
| O_RDWR
);
580 #define mkstemp(s) my_mkstemp(s)
586 * tmpfile() should be adequate, except that it may require special privileges
587 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
590 open_tmpfile(const char *label
)
599 if ((tmpdir
= getenv("TMPDIR")) == 0 || access(tmpdir
, W_OK
) != 0)
606 if (access(tmpdir
, W_OK
) != 0)
610 name
= malloc(strlen(tmpdir
) + 10 + strlen(label
));
615 mode_t save_umask
= umask(0177);
617 if ((mark
= strrchr(label
, '_')) == 0)
618 mark
= label
+ strlen(label
);
620 sprintf(name
, "%s/%.*sXXXXXX", tmpdir
, (int)(mark
- label
), label
);
624 result
= fdopen(fd
, "w+");
629 if (my_tmpfiles
== 0)
631 atexit(close_tmpfiles
);
634 item
= NEW(MY_TMPFILES
);
638 NO_SPACE(item
->name
);
640 item
->next
= my_tmpfiles
;
644 (void)umask(save_umask
);
662 input_file
= fopen(input_file_name
, "r");
664 open_error(input_file_name
);
667 action_file
= open_tmpfile("action_file");
668 text_file
= open_tmpfile("text_file");
672 verbose_file
= fopen(verbose_file_name
, "w");
673 if (verbose_file
== 0)
674 open_error(verbose_file_name
);
679 graph_file
= fopen(graph_file_name
, "w");
681 open_error(graph_file_name
);
682 fprintf(graph_file
, "digraph %s {\n", file_prefix
);
683 fprintf(graph_file
, "\tedge [fontsize=10];\n");
684 fprintf(graph_file
, "\tnode [shape=box,fontsize=10];\n");
685 fprintf(graph_file
, "\torientation=landscape;\n");
686 fprintf(graph_file
, "\trankdir=LR;\n");
687 fprintf(graph_file
, "\t/*\n");
688 fprintf(graph_file
, "\tmargin=0.2;\n");
689 fprintf(graph_file
, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
690 fprintf(graph_file
, "\tratio=auto;\n");
691 fprintf(graph_file
, "\t*/\n");
696 defines_file
= fopen(defines_file_name
, "w");
697 if (defines_file
== 0)
698 open_error(defines_file_name
);
699 union_file
= open_tmpfile("union_file");
704 externs_file
= fopen(externs_file_name
, "w");
705 if (externs_file
== 0)
706 open_error(externs_file_name
);
709 output_file
= fopen(output_file_name
, "w");
710 if (output_file
== 0)
711 open_error(output_file_name
);
715 code_file
= fopen(code_file_name
, "w");
717 open_error(code_file_name
);
720 code_file
= output_file
;
724 main(int argc
, char *argv
[])
728 exit_code
= EXIT_SUCCESS
;