1 /* $Id: main.c,v 1.60 2017/04/30 20:57:56 Julien.Ramseier Exp $ */
5 #include <unistd.h> /* for _exit() */
7 #include <stdlib.h> /* for _exit() */
13 # define USE_MKSTEMP 1
14 #elif defined(HAVE_FCNTL_H)
15 # define USE_MKSTEMP 1
16 # include <fcntl.h> /* for open(), O_EXCL, etc. */
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 default_file_prefix
[] = "y";
53 static char *file_prefix
= default_file_prefix
;
56 char *input_file_name
;
57 size_t input_file_name_len
= 0;
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
;
99 char **symbol_destructor
;
100 char **symbol_type_tag
;
101 int locations
= 0; /* default to no position processing */
102 int backtrack
= 0; /* default is no backtracking */
103 char *initial_action
= NULL
;
117 * Since fclose() is called via the signal handler, it might die. Don't loop
118 * if there is a problem closing a file.
120 #define DO_CLOSE(fp) \
127 static int got_intr
= 0;
132 DO_CLOSE(input_file
);
133 DO_CLOSE(output_file
);
135 DO_CLOSE(externs_file
);
139 DO_CLOSE(action_file
);
140 DO_CLOSE(defines_file
);
141 DO_CLOSE(graph_file
);
143 DO_CLOSE(union_file
);
144 DO_CLOSE(verbose_file
);
151 DO_FREE(code_file_name
);
154 DO_FREE(defines_file_name
);
157 DO_FREE(externs_file_name
);
160 DO_FREE(output_file_name
);
163 DO_FREE(verbose_file_name
);
166 DO_FREE(graph_file_name
);
180 onintr(int sig GCC_UNUSED
)
190 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
191 signal(SIGINT
, onintr
);
194 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
195 signal(SIGTERM
, onintr
);
198 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
199 signal(SIGHUP
, onintr
);
206 static const char *msg
[] =
210 ," -b file_prefix set filename prefix (default \"y.\")"
211 ," -B create a backtracking parser"
212 ," -d write definitions (" DEFINES_SUFFIX
")"
213 ," -i write interface (y.tab.i)"
214 ," -g write a graphical description"
215 ," -l suppress #line directives"
216 ," -L enable position processing, e.g., \"%locations\""
217 ," -o output_file (default \"" OUTPUT_SUFFIX
"\")"
218 ," -p symbol_prefix set symbol prefix (default \"yy\")"
219 ," -P create a reentrant parser, e.g., \"%pure-parser\""
220 ," -r produce separate code and table files (y.code.c)"
221 ," -s suppress #define's for quoted names in %token lines"
222 ," -t add debugging support"
223 ," -v write description (y.output)"
224 ," -V show version information and exit"
229 fprintf(stderr
, "Usage: %s [options] filename\n", myname
);
230 for (n
= 0; n
< sizeof(msg
) / sizeof(msg
[0]); ++n
)
231 fprintf(stderr
, "%s\n", msg
[n
]);
242 #if defined(YYBTYACC)
245 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
266 #if defined(YYBTYACC)
269 unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
294 printf("%s - %s\n", myname
, VERSION
);
298 /* noop for bison compatibility. byacc is already designed to be posix
299 * yacc compatible. */
308 getargs(int argc
, char *argv
[])
317 for (i
= 1; i
< argc
; ++i
)
332 goto no_more_options
;
338 file_prefix
= argv
[i
];
345 output_file_name
= s
;
347 output_file_name
= argv
[i
];
356 symbol_prefix
= argv
[i
];
384 input_file_name_len
= strlen(argv
[i
]);
385 input_file_name
= TMALLOC(char, input_file_name_len
+ 1);
386 NO_SPACE(input_file_name
);
387 strcpy(input_file_name
, argv
[i
]);
404 #define CREATE_FILE_NAME(dest, suffix) \
405 dest = alloc_file_name(len, suffix)
408 alloc_file_name(size_t len
, const char *suffix
)
410 char *result
= TMALLOC(char, len
+ strlen(suffix
) + 1);
413 strcpy(result
, file_prefix
);
414 strcpy(result
+ len
, suffix
);
419 find_suffix(char *name
, const char *suffix
)
421 size_t len
= strlen(name
);
422 size_t slen
= strlen(suffix
);
426 if (strcmp(name
, suffix
) == 0)
433 create_file_names(void)
436 const char *defines_suffix
;
437 const char *externs_suffix
;
441 defines_suffix
= DEFINES_SUFFIX
;
442 externs_suffix
= EXTERNS_SUFFIX
;
444 /* compute the file_prefix from the user provided output_file_name */
445 if (output_file_name
!= 0)
447 if (!(suffix
= find_suffix(output_file_name
, OUTPUT_SUFFIX
))
448 && (suffix
= find_suffix(output_file_name
, ".c")))
450 defines_suffix
= ".h";
451 externs_suffix
= ".i";
457 len
= (size_t) (suffix
- output_file_name
);
458 file_prefix
= TMALLOC(char, len
+ 1);
459 NO_SPACE(file_prefix
);
460 strncpy(file_prefix
, output_file_name
, len
)[len
] = 0;
463 len
= strlen(file_prefix
);
465 /* if "-o filename" was not given */
466 if (output_file_name
== 0)
469 CREATE_FILE_NAME(output_file_name
, OUTPUT_SUFFIX
);
474 CREATE_FILE_NAME(code_file_name
, CODE_SUFFIX
);
477 code_file_name
= output_file_name
;
481 CREATE_FILE_NAME(defines_file_name
, defines_suffix
);
486 CREATE_FILE_NAME(externs_file_name
, externs_suffix
);
491 CREATE_FILE_NAME(verbose_file_name
, VERBOSE_SUFFIX
);
496 CREATE_FILE_NAME(graph_file_name
, GRAPH_SUFFIX
);
509 while (my_tmpfiles
!= 0)
511 MY_TMPFILES
*next
= my_tmpfiles
->next
;
513 (void)chmod(my_tmpfiles
->name
, 0644);
514 (void)unlink(my_tmpfiles
->name
);
516 free(my_tmpfiles
->name
);
525 my_mkstemp(char *temp
)
533 * Split-up to use tempnam, rather than tmpnam; the latter (like
534 * mkstemp) is unusable on Windows.
536 if ((fname
= strrchr(temp
, '/')) != 0)
538 dname
= strdup(temp
);
539 dname
[++fname
- temp
] = '\0';
546 if ((name
= tempnam(dname
, fname
)) != 0)
548 fd
= open(name
, O_CREAT
| O_EXCL
| O_RDWR
);
561 #define mkstemp(s) my_mkstemp(s)
567 * tmpfile() should be adequate, except that it may require special privileges
568 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
571 open_tmpfile(const char *label
)
573 #define MY_FMT "%s/%.*sXXXXXX"
581 if ((tmpdir
= getenv("TMPDIR")) == 0 || access(tmpdir
, W_OK
) != 0)
588 if (access(tmpdir
, W_OK
) != 0)
592 /* The size of the format is guaranteed to be longer than the result from
593 * printing empty strings with it; this calculation accounts for the
594 * string-lengths as well.
596 name
= malloc(strlen(tmpdir
) + sizeof(MY_FMT
) + strlen(label
));
601 mode_t save_umask
= umask(0177);
603 if ((mark
= strrchr(label
, '_')) == 0)
604 mark
= label
+ strlen(label
);
606 sprintf(name
, MY_FMT
, tmpdir
, (int)(mark
- label
), label
);
610 result
= fdopen(fd
, "w+");
615 if (my_tmpfiles
== 0)
617 atexit(close_tmpfiles
);
620 item
= NEW(MY_TMPFILES
);
624 NO_SPACE(item
->name
);
626 item
->next
= my_tmpfiles
;
630 (void)umask(save_umask
);
649 input_file
= fopen(input_file_name
, "r");
651 open_error(input_file_name
);
654 action_file
= open_tmpfile("action_file");
655 text_file
= open_tmpfile("text_file");
659 verbose_file
= fopen(verbose_file_name
, "w");
660 if (verbose_file
== 0)
661 open_error(verbose_file_name
);
666 graph_file
= fopen(graph_file_name
, "w");
668 open_error(graph_file_name
);
669 fprintf(graph_file
, "digraph %s {\n", file_prefix
);
670 fprintf(graph_file
, "\tedge [fontsize=10];\n");
671 fprintf(graph_file
, "\tnode [shape=box,fontsize=10];\n");
672 fprintf(graph_file
, "\torientation=landscape;\n");
673 fprintf(graph_file
, "\trankdir=LR;\n");
674 fprintf(graph_file
, "\t/*\n");
675 fprintf(graph_file
, "\tmargin=0.2;\n");
676 fprintf(graph_file
, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
677 fprintf(graph_file
, "\tratio=auto;\n");
678 fprintf(graph_file
, "\t*/\n");
683 defines_file
= fopen(defines_file_name
, "w");
684 if (defines_file
== 0)
685 open_error(defines_file_name
);
686 union_file
= open_tmpfile("union_file");
691 externs_file
= fopen(externs_file_name
, "w");
692 if (externs_file
== 0)
693 open_error(externs_file_name
);
696 output_file
= fopen(output_file_name
, "w");
697 if (output_file
== 0)
698 open_error(output_file_name
);
702 code_file
= fopen(code_file_name
, "w");
704 open_error(code_file_name
);
707 code_file
= output_file
;
711 main(int argc
, char *argv
[])
715 exit_code
= EXIT_SUCCESS
;