1 /* $NetBSD: main.c,v 1.8 2013/04/06 14:52:24 christos Exp $ */
6 __RCSID("$NetBSD: main.c,v 1.8 2013/04/06 14:52:24 christos Exp $");
7 /* Id: main.c,v 1.40 2012/09/29 13:11:00 Adrian.Bunk Exp */
10 #include <unistd.h> /* for _exit() */
14 # define USE_MKSTEMP 1
15 #elif defined(HAVE_FCNTL_H)
16 # define USE_MKSTEMP 1
17 # include <fcntl.h> /* for open(), O_EXCL, etc. */
19 # define USE_MKSTEMP 0
23 #include <sys/types.h>
26 typedef struct _my_tmpfiles
28 struct _my_tmpfiles
*next
;
33 static MY_TMPFILES
*my_tmpfiles
;
34 #endif /* USE_MKSTEMP */
46 const char *symbol_prefix
;
47 const char *myname
= "yacc";
52 static char empty_string
[] = "";
53 static char default_file_prefix
[] = "y";
54 static int explicit_file_name
;
56 static char *file_prefix
= default_file_prefix
;
59 char *input_file_name
= empty_string
;
60 char *defines_file_name
;
61 char *externs_file_name
;
63 static char *graph_file_name
;
64 static char *output_file_name
;
65 static char *verbose_file_name
;
67 FILE *action_file
; /* a temp file, used to save actions associated */
68 /* with rules until the parser is written */
69 FILE *code_file
; /* y.code.c (used when the -r option is specified) */
70 FILE *defines_file
; /* y.tab.h */
71 FILE *externs_file
; /* y.tab.i */
72 FILE *input_file
; /* the input file */
73 FILE *output_file
; /* y.tab.c */
74 FILE *text_file
; /* a temp file, used to save text until all */
75 /* symbols have been defined */
76 FILE *union_file
; /* a temp file, used to save the union */
77 /* definition until all symbol have been */
79 FILE *verbose_file
; /* y.output */
80 FILE *graph_file
; /* y.dot */
91 Value_t
*symbol_value
;
107 * Since fclose() is called via the signal handler, it might die. Don't loop
108 * if there is a problem closing a file.
110 #define DO_CLOSE(fp) \
117 static int got_intr
= 0;
122 DO_CLOSE(input_file
);
123 DO_CLOSE(output_file
);
125 DO_CLOSE(action_file
);
126 DO_CLOSE(defines_file
);
127 DO_CLOSE(graph_file
);
129 DO_CLOSE(union_file
);
130 DO_CLOSE(verbose_file
);
137 DO_FREE(code_file_name
);
140 DO_FREE(defines_file_name
);
143 DO_FREE(externs_file_name
);
146 DO_FREE(output_file_name
);
149 DO_FREE(verbose_file_name
);
152 DO_FREE(graph_file_name
);
168 onintr(int sig GCC_UNUSED
)
178 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
179 signal(SIGINT
, onintr
);
182 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
183 signal(SIGTERM
, onintr
);
186 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
187 signal(SIGHUP
, onintr
);
194 static const char *msg
[] =
198 ," -b file_prefix set filename prefix (default \"y.\")"
199 ," -d write definitions (y.tab.h)"
200 ," -i write interface (y.tab.i)"
201 ," -g write a graphical description"
202 ," -l suppress #line directives"
203 ," -o output_file (default \"y.tab.c\")"
204 ," -p symbol_prefix set symbol prefix (default \"yy\")"
205 ," -P create a reentrant parser, e.g., \"%pure-parser\""
206 ," -r produce separate code and table files (y.code.c)"
207 ," -s suppress #define's for quoted names in %token lines"
208 ," -t add debugging support"
209 ," -v write description (y.output)"
210 ," -V show version information and exit"
215 fprintf(stderr
, "Usage: %s [options] filename\n", myname
);
216 for (n
= 0; n
< sizeof(msg
) / sizeof(msg
[0]); ++n
)
217 fprintf(stderr
, "%s\n", msg
[n
]);
264 printf("%s - %s\n", myname
, VERSION
);
268 /* noop for bison compatibility. byacc is already designed to be posix
269 * yacc compatible. */
278 getargs(int argc
, char *argv
[])
287 for (i
= 1; i
< argc
; ++i
)
302 goto no_more_options
;
308 file_prefix
= argv
[i
];
315 output_file_name
= s
;
317 output_file_name
= argv
[i
];
320 explicit_file_name
= 1;
327 symbol_prefix
= argv
[i
];
355 input_file_name
= argv
[i
];
372 #define CREATE_FILE_NAME(dest, suffix) \
373 dest = TMALLOC(char, len + strlen(suffix) + 1); \
375 strcpy(dest, file_prefix); \
376 strcpy(dest + len, suffix)
379 create_file_names(void)
382 const char *defines_suffix
;
383 const char *externs_suffix
;
387 defines_suffix
= DEFINES_SUFFIX
;
388 externs_suffix
= EXTERNS_SUFFIX
;
390 /* compute the file_prefix from the user provided output_file_name */
391 if (output_file_name
!= 0)
393 if (!(prefix
= strstr(output_file_name
, ".tab.c"))
394 && (prefix
= strstr(output_file_name
, ".c")))
396 defines_suffix
= ".h";
397 externs_suffix
= ".i";
403 len
= (size_t) (prefix
- output_file_name
);
404 file_prefix
= TMALLOC(char, len
+ 1);
405 NO_SPACE(file_prefix
);
406 strncpy(file_prefix
, output_file_name
, len
)[len
] = 0;
409 len
= strlen(file_prefix
);
411 /* if "-o filename" was not given */
412 if (output_file_name
== 0)
415 CREATE_FILE_NAME(output_file_name
, OUTPUT_SUFFIX
);
420 CREATE_FILE_NAME(code_file_name
, CODE_SUFFIX
);
423 code_file_name
= output_file_name
;
427 if (explicit_file_name
)
430 defines_file_name
= strdup(output_file_name
);
431 if (defines_file_name
== 0)
433 /* does the output_file_name have a known suffix */
434 suffix
= strrchr(output_file_name
, '.');
436 (!strcmp(suffix
, ".c") || /* good, old-fashioned C */
437 !strcmp(suffix
, ".C") || /* C++, or C on Windows */
438 !strcmp(suffix
, ".cc") || /* C++ */
439 !strcmp(suffix
, ".cxx") || /* C++ */
440 !strcmp(suffix
, ".cpp"))) /* C++ (Windows) */
442 strncpy(defines_file_name
, output_file_name
,
443 suffix
- output_file_name
+ 1);
444 defines_file_name
[suffix
- output_file_name
+ 1] = 'h';
445 defines_file_name
[suffix
- output_file_name
+ 2] = 0;
447 fprintf(stderr
,"%s: suffix of output file name %s"
448 " not recognized, no -d file generated.\n",
449 myname
, output_file_name
);
451 free(defines_file_name
);
452 defines_file_name
= 0;
455 CREATE_FILE_NAME(defines_file_name
, defines_suffix
);
461 CREATE_FILE_NAME(externs_file_name
, externs_suffix
);
466 CREATE_FILE_NAME(verbose_file_name
, VERBOSE_SUFFIX
);
471 CREATE_FILE_NAME(graph_file_name
, GRAPH_SUFFIX
);
484 while (my_tmpfiles
!= 0)
486 MY_TMPFILES
*next
= my_tmpfiles
->next
;
488 chmod(my_tmpfiles
->name
, 0644);
489 unlink(my_tmpfiles
->name
);
491 free(my_tmpfiles
->name
);
500 my_mkstemp(char *temp
)
508 * Split-up to use tempnam, rather than tmpnam; the latter (like
509 * mkstemp) is unusable on Windows.
511 if ((fname
= strrchr(temp
, '/')) != 0)
513 dname
= strdup(temp
);
514 dname
[++fname
- temp
] = '\0';
521 if ((name
= tempnam(dname
, fname
)) != 0)
523 fd
= open(name
, O_CREAT
| O_EXCL
| O_RDWR
);
536 #define mkstemp(s) my_mkstemp(s)
542 * tmpfile() should be adequate, except that it may require special privileges
543 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
546 open_tmpfile(const char *label
)
555 if ((tmpdir
= getenv("TMPDIR")) == 0 || access(tmpdir
, W_OK
) != 0)
562 if (access(tmpdir
, W_OK
) != 0)
566 name
= malloc(strlen(tmpdir
) + 10 + strlen(label
));
571 if ((mark
= strrchr(label
, '_')) == 0)
572 mark
= label
+ strlen(label
);
574 sprintf(name
, "%s/%.*sXXXXXX", tmpdir
, (int)(mark
- label
), label
);
578 result
= fdopen(fd
, "w+");
583 if (my_tmpfiles
== 0)
585 atexit(close_tmpfiles
);
588 item
= NEW(MY_TMPFILES
);
592 NO_SPACE(item
->name
);
594 item
->next
= my_tmpfiles
;
615 input_file
= fopen(input_file_name
, "r");
617 open_error(input_file_name
);
620 action_file
= open_tmpfile("action_file");
621 text_file
= open_tmpfile("text_file");
625 verbose_file
= fopen(verbose_file_name
, "w");
626 if (verbose_file
== 0)
627 open_error(verbose_file_name
);
632 graph_file
= fopen(graph_file_name
, "w");
634 open_error(graph_file_name
);
635 fprintf(graph_file
, "digraph %s {\n", file_prefix
);
636 fprintf(graph_file
, "\tedge [fontsize=10];\n");
637 fprintf(graph_file
, "\tnode [shape=box,fontsize=10];\n");
638 fprintf(graph_file
, "\torientation=landscape;\n");
639 fprintf(graph_file
, "\trankdir=LR;\n");
640 fprintf(graph_file
, "\t/*\n");
641 fprintf(graph_file
, "\tmargin=0.2;\n");
642 fprintf(graph_file
, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
643 fprintf(graph_file
, "\tratio=auto;\n");
644 fprintf(graph_file
, "\t*/\n");
649 defines_file
= fopen(defines_file_name
, "w");
650 if (defines_file
== 0)
651 open_error(defines_file_name
);
652 union_file
= open_tmpfile("union_file");
657 externs_file
= fopen(externs_file_name
, "w");
658 if (externs_file
== 0)
659 open_error(externs_file_name
);
662 output_file
= fopen(output_file_name
, "w");
663 if (output_file
== 0)
664 open_error(output_file_name
);
668 code_file
= fopen(code_file_name
, "w");
670 open_error(code_file_name
);
673 code_file
= output_file
;
677 main(int argc
, char *argv
[])
681 exit_code
= EXIT_SUCCESS
;