Remove building with NOCRYPTO option
[minix.git] / external / bsd / byacc / dist / main.c
blob6b0e64ead7e0d4f21035f6d6cd8e53fc37b87cba
1 /* $NetBSD: main.c,v 1.11 2015/01/04 01:34:20 christos Exp $ */
3 #include "defs.h"
5 #include <sys/cdefs.h>
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 */
9 #include <signal.h>
10 #ifndef _WIN32
11 #include <unistd.h> /* for _exit() */
12 #else
13 #include <stdlib.h> /* for _exit() */
14 #endif
17 #ifdef HAVE_MKSTEMP
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. */
22 #else
23 # define USE_MKSTEMP 0
24 #endif
26 #if USE_MKSTEMP
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 typedef struct _my_tmpfiles
32 struct _my_tmpfiles *next;
33 char *name;
35 MY_TMPFILES;
37 static MY_TMPFILES *my_tmpfiles;
38 #endif /* USE_MKSTEMP */
40 char dflag;
41 char gflag;
42 char iflag;
43 char lflag;
44 static char oflag;
45 char rflag;
46 char sflag;
47 char tflag;
48 char vflag;
50 const char *symbol_prefix;
51 const char *myname = "yacc";
53 int lineno;
54 int outline;
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;
62 char *code_file_name;
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 */
82 /* defined */
83 FILE *verbose_file; /* y.output */
84 FILE *graph_file; /* y.dot */
86 Value_t nitems;
87 Value_t nrules;
88 Value_t nsyms;
89 Value_t ntokens;
90 Value_t nvars;
92 Value_t start_symbol;
93 char **symbol_name;
94 char **symbol_pname;
95 Value_t *symbol_value;
96 Value_t *symbol_prec;
97 char *symbol_assoc;
99 int pure_parser;
100 int token_table;
101 int error_verbose;
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;
110 #endif
112 int exit_code;
114 Value_t *ritem;
115 Value_t *rlhs;
116 Value_t *rrhs;
117 Value_t *rprec;
118 Assoc_t *rassoc;
119 Value_t **derives;
120 char *nullable;
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) \
127 if (fp != 0) { \
128 FILE *use = fp; \
129 fp = 0; \
130 fclose(use); \
133 static int got_intr = 0;
135 void
136 done(int k)
138 DO_CLOSE(input_file);
139 DO_CLOSE(output_file);
140 if (iflag)
141 DO_CLOSE(externs_file);
142 if (rflag)
143 DO_CLOSE(code_file);
145 DO_CLOSE(action_file);
146 DO_CLOSE(defines_file);
147 DO_CLOSE(graph_file);
148 DO_CLOSE(text_file);
149 DO_CLOSE(union_file);
150 DO_CLOSE(verbose_file);
152 if (got_intr)
153 _exit(EXIT_FAILURE);
155 #ifdef NO_LEAKS
156 if (rflag)
157 DO_FREE(code_file_name);
159 if (dflag)
160 DO_FREE(defines_file_name);
162 if (iflag)
163 DO_FREE(externs_file_name);
165 if (oflag)
166 DO_FREE(output_file_name);
168 if (vflag)
169 DO_FREE(verbose_file_name);
171 if (gflag)
172 DO_FREE(graph_file_name);
174 lr0_leaks();
175 lalr_leaks();
176 mkpar_leaks();
177 mstring_leaks();
178 output_leaks();
179 reader_leaks();
180 #endif
182 exit(k);
185 static void
186 onintr(int sig GCC_UNUSED)
188 got_intr = 1;
189 done(EXIT_FAILURE);
192 static void
193 set_signals(void)
195 #ifdef SIGINT
196 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
197 signal(SIGINT, onintr);
198 #endif
199 #ifdef SIGTERM
200 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
201 signal(SIGTERM, onintr);
202 #endif
203 #ifdef SIGHUP
204 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
205 signal(SIGHUP, onintr);
206 #endif
209 static void
210 usage(void)
212 static const char *msg[] =
215 ,"Options:"
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"
232 unsigned n;
234 fflush(stdout);
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]);
239 exit(1);
242 static void
243 setflag(int ch)
245 switch (ch)
247 case 'B':
248 #if defined(YYBTYACC)
249 backtrack = 1;
250 #else
251 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
252 #endif
253 break;
255 case 'd':
256 dflag = 1;
257 break;
259 case 'g':
260 gflag = 1;
261 break;
263 case 'i':
264 iflag = 1;
265 break;
267 case 'l':
268 lflag = 1;
269 break;
271 case 'L':
272 #if defined(YYBTYACC)
273 locations = 1;
274 #else
275 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
276 #endif
277 break;
279 case 'P':
280 pure_parser = 1;
281 break;
283 case 'r':
284 rflag = 1;
285 break;
287 case 's':
288 sflag = 1;
289 break;
291 case 't':
292 tflag = 1;
293 break;
295 case 'v':
296 vflag = 1;
297 break;
299 case 'V':
300 printf("%s - %s\n", myname, VERSION);
301 exit(EXIT_SUCCESS);
303 case 'y':
304 /* noop for bison compatibility. byacc is already designed to be posix
305 * yacc compatible. */
306 break;
308 default:
309 usage();
313 static void
314 getargs(int argc, char *argv[])
316 int i;
317 char *s;
318 int ch;
320 if (argc > 0)
321 myname = argv[0];
323 for (i = 1; i < argc; ++i)
325 s = argv[i];
326 if (*s != '-')
327 break;
328 switch (ch = *++s)
330 case '\0':
331 input_file = stdin;
332 if (i + 1 < argc)
333 usage();
334 return;
336 case '-':
337 ++i;
338 goto no_more_options;
340 case 'b':
341 if (*++s)
342 file_prefix = s;
343 else if (++i < argc)
344 file_prefix = argv[i];
345 else
346 usage();
347 continue;
349 case 'o':
350 if (*++s)
351 output_file_name = s;
352 else if (++i < argc)
353 output_file_name = argv[i];
354 else
355 usage();
356 explicit_file_name = 1;
357 continue;
359 case 'p':
360 if (*++s)
361 symbol_prefix = s;
362 else if (++i < argc)
363 symbol_prefix = argv[i];
364 else
365 usage();
366 continue;
368 default:
369 setflag(ch);
370 break;
373 for (;;)
375 switch (ch = *++s)
377 case '\0':
378 goto end_of_option;
380 default:
381 setflag(ch);
382 break;
385 end_of_option:;
388 no_more_options:;
389 if (i + 1 != argc)
390 usage();
391 input_file_name = argv[i];
394 void *
395 allocate(size_t n)
397 void *p;
399 p = NULL;
400 if (n)
402 p = CALLOC(1, n);
403 NO_SPACE(p);
405 return (p);
408 #define CREATE_FILE_NAME(dest, suffix) \
409 dest = alloc_file_name(len, suffix)
411 static char *
412 alloc_file_name(size_t len, const char *suffix)
414 char *result = TMALLOC(char, len + strlen(suffix) + 1);
415 if (result == 0)
416 no_space();
417 strcpy(result, file_prefix);
418 strcpy(result + len, suffix);
419 return result;
422 static void
423 create_file_names(void)
425 size_t len;
426 const char *defines_suffix;
427 const char *externs_suffix;
428 char *prefix;
430 prefix = NULL;
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";
445 if (prefix != NULL)
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;
452 else
453 len = strlen(file_prefix);
455 /* if "-o filename" was not given */
456 if (output_file_name == 0)
458 oflag = 1;
459 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
462 if (rflag)
464 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
466 else
467 code_file_name = output_file_name;
469 if (dflag)
471 if (explicit_file_name)
473 char *suffix;
474 defines_file_name = strdup(output_file_name);
475 if (defines_file_name == 0)
476 no_space();
477 /* does the output_file_name have a known suffix */
478 suffix = strrchr(output_file_name, '.');
479 if (suffix != 0 &&
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;
490 } else {
491 fprintf(stderr,"%s: suffix of output file name %s"
492 " not recognized, no -d file generated.\n",
493 myname, output_file_name);
494 dflag = 0;
495 free(defines_file_name);
496 defines_file_name = 0;
498 } else {
499 CREATE_FILE_NAME(defines_file_name, defines_suffix);
503 if (iflag)
505 CREATE_FILE_NAME(externs_file_name, externs_suffix);
508 if (vflag)
510 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
513 if (gflag)
515 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
518 if (prefix != NULL)
520 FREE(file_prefix);
524 #if USE_MKSTEMP
525 static void
526 close_tmpfiles(void)
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);
536 free(my_tmpfiles);
538 my_tmpfiles = next;
542 #ifndef HAVE_MKSTEMP
543 static int
544 my_mkstemp(char *temp)
546 int fd;
547 char *dname;
548 char *fname;
549 char *name;
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';
560 else
562 dname = 0;
563 fname = temp;
565 if ((name = tempnam(dname, fname)) != 0)
567 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
568 strcpy(temp, name);
570 else
572 fd = -1;
575 if (dname != 0)
576 free(dname);
578 return fd;
580 #define mkstemp(s) my_mkstemp(s)
581 #endif
583 #endif
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.
589 static FILE *
590 open_tmpfile(const char *label)
592 FILE *result;
593 #if USE_MKSTEMP
594 int fd;
595 const char *tmpdir;
596 char *name;
597 const char *mark;
599 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
601 #ifdef P_tmpdir
602 tmpdir = P_tmpdir;
603 #else
604 tmpdir = "/tmp";
605 #endif
606 if (access(tmpdir, W_OK) != 0)
607 tmpdir = ".";
610 name = malloc(strlen(tmpdir) + 10 + strlen(label));
612 result = 0;
613 if (name != 0)
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);
621 fd = mkstemp(name);
622 if (fd >= 0)
624 result = fdopen(fd, "w+");
625 if (result != 0)
627 MY_TMPFILES *item;
629 if (my_tmpfiles == 0)
631 atexit(close_tmpfiles);
634 item = NEW(MY_TMPFILES);
635 NO_SPACE(item);
637 item->name = name;
638 NO_SPACE(item->name);
640 item->next = my_tmpfiles;
641 my_tmpfiles = item;
644 (void)umask(save_umask);
646 #else
647 result = tmpfile();
648 #endif
650 if (result == 0)
651 open_error(label);
652 return result;
655 static void
656 open_files(void)
658 create_file_names();
660 if (input_file == 0)
662 input_file = fopen(input_file_name, "r");
663 if (input_file == 0)
664 open_error(input_file_name);
667 action_file = open_tmpfile("action_file");
668 text_file = open_tmpfile("text_file");
670 if (vflag)
672 verbose_file = fopen(verbose_file_name, "w");
673 if (verbose_file == 0)
674 open_error(verbose_file_name);
677 if (gflag)
679 graph_file = fopen(graph_file_name, "w");
680 if (graph_file == 0)
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");
694 if (dflag)
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");
702 if (iflag)
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);
713 if (rflag)
715 code_file = fopen(code_file_name, "w");
716 if (code_file == 0)
717 open_error(code_file_name);
719 else
720 code_file = output_file;
724 main(int argc, char *argv[])
726 SRexpect = -1;
727 RRexpect = -1;
728 exit_code = EXIT_SUCCESS;
730 set_signals();
731 getargs(argc, argv);
732 open_files();
733 reader();
734 lr0();
735 lalr();
736 make_parser();
737 graph();
738 finalize_closure();
739 verbose();
740 output();
741 done(exit_code);
742 /*NOTREACHED */