1 /* $NetBSD: main.c,v 1.7 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: main.c,v 1.36 2011/09/06 22:44:45 tom Exp */
7 __RCSID("$NetBSD: main.c,v 1.7 2011/09/10 21:29:04 christos Exp $");
10 #include <unistd.h> /* for _exit() */
13 #if defined(HAVE_ATEXIT)
15 # define USE_MKSTEMP 1
16 # elif defined(HAVE_FCNTL_H)
17 # define USE_MKSTEMP 1
18 # include <fcntl.h> /* for open(), O_EXCL, etc. */
20 # define USE_MKSTEMP 0
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 */
49 const char *symbol_prefix
;
50 const char *myname
= "yacc";
55 static char empty_string
[] = "";
56 static char default_file_prefix
[] = "y";
57 static int explicit_file_name
;
59 static char *file_prefix
= default_file_prefix
;
62 char *input_file_name
= empty_string
;
63 char *defines_file_name
;
64 char *externs_file_name
;
66 static char *graph_file_name
;
67 static char *output_file_name
;
68 static char *verbose_file_name
;
70 FILE *action_file
; /* a temp file, used to save actions associated */
71 /* with rules until the parser is written */
72 FILE *code_file
; /* y.code.c (used when the -r option is specified) */
73 FILE *defines_file
; /* y.tab.h */
74 FILE *externs_file
; /* y.tab.i */
75 FILE *input_file
; /* the input file */
76 FILE *output_file
; /* y.tab.c */
77 FILE *text_file
; /* a temp file, used to save text until all */
78 /* symbols have been defined */
79 FILE *union_file
; /* a temp file, used to save the union */
80 /* definition until all symbol have been */
82 FILE *verbose_file
; /* y.output */
83 FILE *graph_file
; /* y.dot */
94 Value_t
*symbol_value
;
110 * Since fclose() is called via the signal handler, it might die. Don't loop
111 * if there is a problem closing a file.
113 #define DO_CLOSE(fp) \
120 static int got_intr
= 0;
125 DO_CLOSE(input_file
);
126 DO_CLOSE(output_file
);
128 DO_CLOSE(action_file
);
129 DO_CLOSE(defines_file
);
130 DO_CLOSE(graph_file
);
132 DO_CLOSE(union_file
);
133 DO_CLOSE(verbose_file
);
140 DO_FREE(code_file_name
);
143 DO_FREE(defines_file_name
);
146 DO_FREE(externs_file_name
);
149 DO_FREE(output_file_name
);
152 DO_FREE(verbose_file_name
);
155 DO_FREE(graph_file_name
);
171 onintr(int sig GCC_UNUSED
)
181 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
182 signal(SIGINT
, onintr
);
185 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
186 signal(SIGTERM
, onintr
);
189 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
190 signal(SIGHUP
, onintr
);
197 static const char *msg
[] =
201 ," -b file_prefix set filename prefix (default \"y.\")"
202 ," -d write definitions (y.tab.h)"
203 ," -i write interface (y.tab.i)"
204 ," -g write a graphical description"
205 ," -l suppress #line directives"
206 ," -o output_file (default \"y.tab.c\")"
207 ," -p symbol_prefix set symbol prefix (default \"yy\")"
208 ," -P create a reentrant parser, e.g., \"%pure-parser\""
209 ," -r produce separate code and table files (y.code.c)"
210 ," -t add debugging support"
211 ," -v write description (y.output)"
212 ," -V show version information and exit"
217 fprintf(stderr
, "Usage: %s [options] filename\n", myname
);
218 for (n
= 0; n
< sizeof(msg
) / sizeof(msg
[0]); ++n
)
219 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
];
318 explicit_file_name
= 1;
325 symbol_prefix
= argv
[i
];
353 input_file_name
= argv
[i
];
370 #define CREATE_FILE_NAME(dest, suffix) \
371 dest = MALLOC(len + strlen(suffix) + 1); \
373 strcpy(dest, file_prefix); \
374 strcpy(dest + len, suffix)
377 create_file_names(void)
380 const char *defines_suffix
;
381 const char *externs_suffix
;
385 defines_suffix
= DEFINES_SUFFIX
;
386 externs_suffix
= EXTERNS_SUFFIX
;
388 /* compute the file_prefix from the user provided output_file_name */
389 if (output_file_name
!= 0)
391 if (!(prefix
= strstr(output_file_name
, ".tab.c"))
392 && (prefix
= strstr(output_file_name
, ".c")))
394 defines_suffix
= ".h";
395 externs_suffix
= ".i";
401 len
= (size_t) (prefix
- output_file_name
);
402 file_prefix
= MALLOC(len
+ 1);
403 NO_SPACE(file_prefix
);
404 strncpy(file_prefix
, output_file_name
, len
)[len
] = 0;
407 len
= strlen(file_prefix
);
409 /* if "-o filename" was not given */
410 if (output_file_name
== 0)
413 CREATE_FILE_NAME(output_file_name
, OUTPUT_SUFFIX
);
418 CREATE_FILE_NAME(code_file_name
, CODE_SUFFIX
);
421 code_file_name
= output_file_name
;
425 if (explicit_file_name
)
428 defines_file_name
= strdup(output_file_name
);
429 if (defines_file_name
== 0)
431 /* does the output_file_name have a known suffix */
432 suffix
= strrchr(output_file_name
, '.');
434 (!strcmp(suffix
, ".c") || /* good, old-fashioned C */
435 !strcmp(suffix
, ".C") || /* C++, or C on Windows */
436 !strcmp(suffix
, ".cc") || /* C++ */
437 !strcmp(suffix
, ".cxx") || /* C++ */
438 !strcmp(suffix
, ".cpp"))) /* C++ (Windows) */
440 strncpy(defines_file_name
, output_file_name
,
441 suffix
- output_file_name
+ 1);
442 defines_file_name
[suffix
- output_file_name
+ 1] = 'h';
443 defines_file_name
[suffix
- output_file_name
+ 2] = 0;
445 fprintf(stderr
,"%s: suffix of output file name %s"
446 " not recognized, no -d file generated.\n",
447 myname
, output_file_name
);
449 free(defines_file_name
);
450 defines_file_name
= 0;
453 CREATE_FILE_NAME(defines_file_name
, defines_suffix
);
459 CREATE_FILE_NAME(externs_file_name
, externs_suffix
);
464 CREATE_FILE_NAME(verbose_file_name
, VERBOSE_SUFFIX
);
469 CREATE_FILE_NAME(graph_file_name
, GRAPH_SUFFIX
);
482 while (my_tmpfiles
!= 0)
484 MY_TMPFILES
*next
= my_tmpfiles
->next
;
486 chmod(my_tmpfiles
->name
, 0644);
487 unlink(my_tmpfiles
->name
);
489 free(my_tmpfiles
->name
);
498 my_mkstemp(char *temp
)
506 * Split-up to use tempnam, rather than tmpnam; the latter (like
507 * mkstemp) is unusable on Windows.
509 if ((fname
= strrchr(temp
, '/')) != 0)
511 dname
= strdup(temp
);
512 dname
[++fname
- temp
] = '\0';
519 if ((name
= tempnam(dname
, fname
)) != 0)
521 fd
= open(name
, O_CREAT
| O_EXCL
| O_RDWR
);
534 #define mkstemp(s) my_mkstemp(s)
540 * tmpfile() should be adequate, except that it may require special privileges
541 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
544 open_tmpfile(const char *label
)
553 if ((tmpdir
= getenv("TMPDIR")) == 0 || access(tmpdir
, W_OK
) != 0)
560 if (access(tmpdir
, W_OK
) != 0)
564 name
= malloc(strlen(tmpdir
) + 10 + strlen(label
));
569 if ((mark
= strrchr(label
, '_')) == 0)
570 mark
= label
+ strlen(label
);
572 sprintf(name
, "%s/%.*sXXXXXX", tmpdir
, (int)(mark
- label
), label
);
576 result
= fdopen(fd
, "w+");
581 if (my_tmpfiles
== 0)
583 atexit(close_tmpfiles
);
586 item
= NEW(MY_TMPFILES
);
590 NO_SPACE(item
->name
);
592 item
->next
= my_tmpfiles
;
613 input_file
= fopen(input_file_name
, "r");
615 open_error(input_file_name
);
618 action_file
= open_tmpfile("action_file");
619 text_file
= open_tmpfile("text_file");
623 verbose_file
= fopen(verbose_file_name
, "w");
624 if (verbose_file
== 0)
625 open_error(verbose_file_name
);
630 graph_file
= fopen(graph_file_name
, "w");
632 open_error(graph_file_name
);
633 fprintf(graph_file
, "digraph %s {\n", file_prefix
);
634 fprintf(graph_file
, "\tedge [fontsize=10];\n");
635 fprintf(graph_file
, "\tnode [shape=box,fontsize=10];\n");
636 fprintf(graph_file
, "\torientation=landscape;\n");
637 fprintf(graph_file
, "\trankdir=LR;\n");
638 fprintf(graph_file
, "\t/*\n");
639 fprintf(graph_file
, "\tmargin=0.2;\n");
640 fprintf(graph_file
, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
641 fprintf(graph_file
, "\tratio=auto;\n");
642 fprintf(graph_file
, "\t*/\n");
647 defines_file
= fopen(defines_file_name
, "w");
648 if (defines_file
== 0)
649 open_error(defines_file_name
);
650 union_file
= open_tmpfile("union_file");
655 externs_file
= fopen(externs_file_name
, "w");
656 if (externs_file
== 0)
657 open_error(externs_file_name
);
660 output_file
= fopen(output_file_name
, "w");
661 if (output_file
== 0)
662 open_error(output_file_name
);
666 code_file
= fopen(code_file_name
, "w");
668 open_error(code_file_name
);
671 code_file
= output_file
;
675 main(int argc
, char *argv
[])
679 exit_code
= EXIT_SUCCESS
;