main.c revision 1.19 1 /* $NetBSD: main.c,v 1.19 2021/02/20 22:57:56 christos Exp $ */
2
3 #include "defs.h"
4
5 #include <sys/cdefs.h>
6 __RCSID("$NetBSD: main.c,v 1.19 2021/02/20 22:57:56 christos Exp $");
7 /* Id: main.c,v 1.70 2020/09/10 17:32:55 tom Exp */
8
9 #include <signal.h>
10 #if !defined(_WIN32) || defined(__MINGW32__)
11 #include <unistd.h> /* for _exit() */
12 #else
13 #include <stdlib.h> /* for _exit() */
14 #endif
15
16
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
25
26 #if USE_MKSTEMP
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 typedef struct _my_tmpfiles
31 {
32 struct _my_tmpfiles *next;
33 char *name;
34 }
35 MY_TMPFILES;
36
37 static MY_TMPFILES *my_tmpfiles;
38 #endif /* USE_MKSTEMP */
39
40 char dflag;
41 char dflag2;
42 char gflag;
43 char iflag;
44 char lflag;
45 static char oflag;
46 char rflag;
47 char sflag;
48 char tflag;
49 char vflag;
50
51 const char *symbol_prefix;
52 const char *myname = "yacc";
53
54 int lineno;
55 int outline;
56
57 static char default_file_prefix[] = "y";
58 static int explicit_file_name;
59
60 static char *file_prefix = default_file_prefix;
61
62 char *code_file_name;
63 char *input_file_name;
64 size_t input_file_name_len = 0;
65 char *defines_file_name;
66 char *externs_file_name;
67
68 static char *graph_file_name;
69 static char *output_file_name;
70 static char *verbose_file_name;
71
72 FILE *action_file; /* a temp file, used to save actions associated */
73 /* with rules until the parser is written */
74 FILE *code_file; /* y.code.c (used when the -r option is specified) */
75 FILE *defines_file; /* y.tab.h */
76 FILE *externs_file; /* y.tab.i */
77 FILE *input_file; /* the input file */
78 FILE *output_file; /* y.tab.c */
79 FILE *text_file; /* a temp file, used to save text until all */
80 /* symbols have been defined */
81 FILE *union_file; /* a temp file, used to save the union */
82 /* definition until all symbol have been */
83 /* defined */
84 FILE *verbose_file; /* y.output */
85 FILE *graph_file; /* y.dot */
86
87 Value_t nitems;
88 Value_t nrules;
89 Value_t nsyms;
90 Value_t ntokens;
91 Value_t nvars;
92
93 Value_t start_symbol;
94 char **symbol_name;
95 char **symbol_pname;
96 Value_t *symbol_value;
97 Value_t *symbol_prec;
98 char *symbol_assoc;
99
100 int pure_parser;
101 int token_table;
102 int error_verbose;
103
104 #if defined(YYBTYACC)
105 Value_t *symbol_pval;
106 char **symbol_destructor;
107 char **symbol_type_tag;
108 int locations = 0; /* default to no position processing */
109 int backtrack = 0; /* default is no backtracking */
110 char *initial_action = NULL;
111 #endif
112
113 int exit_code;
114
115 Value_t *ritem;
116 Value_t *rlhs;
117 Value_t *rrhs;
118 Value_t *rprec;
119 Assoc_t *rassoc;
120 Value_t **derives;
121 char *nullable;
122
123 /*
124 * Since fclose() is called via the signal handler, it might die. Don't loop
125 * if there is a problem closing a file.
126 */
127 #define DO_CLOSE(fp) \
128 if (fp != 0) { \
129 FILE *use = fp; \
130 fp = 0; \
131 fclose(use); \
132 }
133
134 static int got_intr = 0;
135
136 void
137 done(int k)
138 {
139 DO_CLOSE(input_file);
140 DO_CLOSE(output_file);
141 if (iflag)
142 DO_CLOSE(externs_file);
143 if (rflag)
144 DO_CLOSE(code_file);
145
146 DO_CLOSE(action_file);
147 DO_CLOSE(defines_file);
148 DO_CLOSE(graph_file);
149 DO_CLOSE(text_file);
150 DO_CLOSE(union_file);
151 DO_CLOSE(verbose_file);
152
153 if (got_intr)
154 _exit(EXIT_FAILURE);
155
156 #ifdef NO_LEAKS
157 if (rflag)
158 DO_FREE(code_file_name);
159
160 if (dflag && !dflag2)
161 DO_FREE(defines_file_name);
162
163 if (iflag)
164 DO_FREE(externs_file_name);
165
166 if (oflag)
167 DO_FREE(output_file_name);
168
169 if (vflag)
170 DO_FREE(verbose_file_name);
171
172 if (gflag)
173 DO_FREE(graph_file_name);
174
175 lr0_leaks();
176 lalr_leaks();
177 mkpar_leaks();
178 mstring_leaks();
179 output_leaks();
180 reader_leaks();
181 #endif
182
183 exit(k);
184 }
185
186 static void
187 onintr(int sig GCC_UNUSED)
188 {
189 got_intr = 1;
190 done(EXIT_FAILURE);
191 }
192
193 static void
194 set_signals(void)
195 {
196 #ifdef SIGINT
197 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
198 signal(SIGINT, onintr);
199 #endif
200 #ifdef SIGTERM
201 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
202 signal(SIGTERM, onintr);
203 #endif
204 #ifdef SIGHUP
205 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
206 signal(SIGHUP, onintr);
207 #endif
208 }
209
210 static void
211 usage(void)
212 {
213 static const char *msg[] =
214 {
215 ""
216 ,"Options:"
217 ," -b file_prefix set filename prefix (default \"y.\")"
218 ," -B create a backtracking parser"
219 ," -d write definitions (" DEFINES_SUFFIX ")"
220 ," -H defines_file write definitions to defines_file"
221 ," -i write interface (y.tab.i)"
222 ," -g write a graphical description"
223 ," -l suppress #line directives"
224 ," -L enable position processing, e.g., \"%locations\""
225 ," -o output_file (default \"" OUTPUT_SUFFIX "\")"
226 ," -p symbol_prefix set symbol prefix (default \"yy\")"
227 ," -P create a reentrant parser, e.g., \"%pure-parser\""
228 ," -r produce separate code and table files (y.code.c)"
229 ," -s suppress #define's for quoted names in %token lines"
230 ," -t add debugging support"
231 ," -v write description (y.output)"
232 ," -V show version information and exit"
233 };
234 unsigned n;
235
236 fflush(stdout);
237 fprintf(stderr, "Usage: %s [options] filename\n", myname);
238 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
239 fprintf(stderr, "%s\n", msg[n]);
240
241 exit(EXIT_FAILURE);
242 }
243
244 static void
245 setflag(int ch)
246 {
247 switch (ch)
248 {
249 case 'B':
250 #if defined(YYBTYACC)
251 backtrack = 1;
252 #else
253 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
254 #endif
255 break;
256
257 case 'd':
258 dflag = 1;
259 dflag2 = 0;
260 break;
261
262 case 'g':
263 gflag = 1;
264 break;
265
266 case 'i':
267 iflag = 1;
268 break;
269
270 case 'l':
271 lflag = 1;
272 break;
273
274 case 'L':
275 #if defined(YYBTYACC)
276 locations = 1;
277 #else
278 unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
279 #endif
280 break;
281
282 case 'P':
283 pure_parser = 1;
284 break;
285
286 case 'r':
287 rflag = 1;
288 break;
289
290 case 's':
291 sflag = 1;
292 break;
293
294 case 't':
295 tflag = 1;
296 break;
297
298 case 'v':
299 vflag = 1;
300 break;
301
302 case 'V':
303 printf("%s - %s\n", myname, VERSION);
304 exit(EXIT_SUCCESS);
305
306 case 'y':
307 /* noop for bison compatibility. byacc is already designed to be posix
308 * yacc compatible. */
309 break;
310
311 default:
312 usage();
313 }
314 }
315
316 static void
317 getargs(int argc, char *argv[])
318 {
319 int i;
320 #ifdef HAVE_GETOPT
321 int ch;
322
323 if (argc > 0)
324 myname = argv[0];
325
326 while ((ch = getopt(argc, argv, "Bb:dgH:ilLo:Pp:rstVvy")) != -1)
327 {
328 switch (ch)
329 {
330 case 'b':
331 file_prefix = optarg;
332 break;
333 case 'H':
334 dflag = dflag2 = 1;
335 defines_file_name = optarg;
336 break;
337 case 'o':
338 output_file_name = optarg;
339 break;
340 case 'p':
341 symbol_prefix = optarg;
342 break;
343 default:
344 setflag(ch);
345 break;
346 }
347 }
348 if ((i = optind) < argc)
349 {
350 /* getopt handles "--" specially, while we handle "-" specially */
351 if (!strcmp(argv[i], "-"))
352 {
353 if ((i + 1) < argc)
354 usage();
355 input_file = stdin;
356 return;
357 }
358 }
359 #else
360 char *s;
361 int ch;
362
363 if (argc > 0)
364 myname = argv[0];
365
366 for (i = 1; i < argc; ++i)
367 {
368 s = argv[i];
369 if (*s != '-')
370 break;
371 switch (ch = *++s)
372 {
373 case '\0':
374 input_file = stdin;
375 if (i + 1 < argc)
376 usage();
377 return;
378
379 case '-':
380 ++i;
381 goto no_more_options;
382
383 case 'b':
384 if (*++s)
385 file_prefix = s;
386 else if (++i < argc)
387 file_prefix = argv[i];
388 else
389 usage();
390 continue;
391
392 case 'H':
393 dflag = dflag2 = 1;
394 if (*++s)
395 defines_file_name = s;
396 else if (++i < argc)
397 defines_file_name = argv[i];
398 else
399 usage();
400 continue;
401
402 case 'o':
403 if (*++s)
404 output_file_name = s;
405 else if (++i < argc)
406 output_file_name = argv[i];
407 else
408 usage();
409 explicit_file_name = 1;
410 continue;
411
412 case 'p':
413 if (*++s)
414 symbol_prefix = s;
415 else if (++i < argc)
416 symbol_prefix = argv[i];
417 else
418 usage();
419 continue;
420
421 default:
422 setflag(ch);
423 break;
424 }
425
426 for (;;)
427 {
428 switch (ch = *++s)
429 {
430 case '\0':
431 goto end_of_option;
432
433 default:
434 setflag(ch);
435 break;
436 }
437 }
438 end_of_option:;
439 }
440
441 no_more_options:
442
443 #endif /* HAVE_GETOPT */
444 if (i + 1 != argc)
445 usage();
446 input_file_name_len = strlen(argv[i]);
447 input_file_name = TMALLOC(char, input_file_name_len + 1);
448 NO_SPACE(input_file_name);
449 strcpy(input_file_name, argv[i]);
450 }
451
452 void *
453 allocate(size_t n)
454 {
455 void *p;
456
457 p = NULL;
458 if (n)
459 {
460 p = CALLOC(1, n);
461 NO_SPACE(p);
462 }
463 return (p);
464 }
465
466 #define CREATE_FILE_NAME(dest, suffix) \
467 dest = alloc_file_name(len, suffix)
468
469 static char *
470 alloc_file_name(size_t len, const char *suffix)
471 {
472 char *result = TMALLOC(char, len + strlen(suffix) + 1);
473 if (result == 0)
474 no_space();
475 strcpy(result, file_prefix);
476 strcpy(result + len, suffix);
477 return result;
478 }
479
480 static char *
481 find_suffix(char *name, const char *suffix)
482 {
483 size_t len = strlen(name);
484 size_t slen = strlen(suffix);
485 if (len >= slen)
486 {
487 name += len - slen;
488 if (strcmp(name, suffix) == 0)
489 return name;
490 }
491 return NULL;
492 }
493
494 static void
495 create_file_names(void)
496 {
497 size_t len;
498 const char *defines_suffix;
499 const char *externs_suffix;
500 char *suffix;
501
502 suffix = NULL;
503 defines_suffix = DEFINES_SUFFIX;
504 externs_suffix = EXTERNS_SUFFIX;
505
506 /* compute the file_prefix from the user provided output_file_name */
507 if (output_file_name != 0)
508 {
509 if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
510 && (suffix = find_suffix(output_file_name, ".c")))
511 {
512 defines_suffix = ".h";
513 externs_suffix = ".i";
514 }
515 }
516
517 if (suffix != NULL)
518 {
519 len = (size_t) (suffix - output_file_name);
520 file_prefix = TMALLOC(char, len + 1);
521 NO_SPACE(file_prefix);
522 strncpy(file_prefix, output_file_name, len)[len] = 0;
523 }
524 else
525 len = strlen(file_prefix);
526
527 /* if "-o filename" was not given */
528 if (output_file_name == 0)
529 {
530 oflag = 1;
531 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
532 }
533
534 if (rflag)
535 {
536 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
537 }
538 else
539 code_file_name = output_file_name;
540
541 if (dflag && !dflag2)
542 {
543 if (explicit_file_name)
544 {
545 char *xsuffix;
546 defines_file_name = strdup(output_file_name);
547 if (defines_file_name == 0)
548 no_space();
549 /* does the output_file_name have a known suffix */
550 xsuffix = strrchr(output_file_name, '.');
551 if (xsuffix != 0 &&
552 (!strcmp(xsuffix, ".c") || /* good, old-fashioned C */
553 !strcmp(xsuffix, ".C") || /* C++, or C on Windows */
554 !strcmp(xsuffix, ".cc") || /* C++ */
555 !strcmp(xsuffix, ".cxx") || /* C++ */
556 !strcmp(xsuffix, ".cpp"))) /* C++ (Windows) */
557 {
558 strncpy(defines_file_name, output_file_name,
559 xsuffix - output_file_name + 1);
560 defines_file_name[xsuffix - output_file_name + 1] = 'h';
561 defines_file_name[xsuffix - output_file_name + 2] = 0;
562 } else {
563 fprintf(stderr,"%s: suffix of output file name %s"
564 " not recognized, no -d file generated.\n",
565 myname, output_file_name);
566 dflag = 0;
567 free(defines_file_name);
568 defines_file_name = 0;
569 }
570 } else {
571 CREATE_FILE_NAME(defines_file_name, defines_suffix);
572 }
573 }
574
575 if (iflag)
576 {
577 CREATE_FILE_NAME(externs_file_name, externs_suffix);
578 }
579
580 if (vflag)
581 {
582 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
583 }
584
585 if (gflag)
586 {
587 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
588 }
589
590 if (suffix != NULL)
591 {
592 FREE(file_prefix);
593 }
594 }
595
596 #if USE_MKSTEMP
597 static void
598 close_tmpfiles(void)
599 {
600 while (my_tmpfiles != 0)
601 {
602 MY_TMPFILES *next = my_tmpfiles->next;
603
604 (void)chmod(my_tmpfiles->name, 0644);
605 (void)unlink(my_tmpfiles->name);
606
607 free(my_tmpfiles->name);
608 free(my_tmpfiles);
609
610 my_tmpfiles = next;
611 }
612 }
613
614 #ifndef HAVE_MKSTEMP
615 static int
616 my_mkstemp(char *temp)
617 {
618 int fd;
619 char *dname;
620 char *fname;
621 char *name;
622
623 /*
624 * Split-up to use tempnam, rather than tmpnam; the latter (like
625 * mkstemp) is unusable on Windows.
626 */
627 if ((fname = strrchr(temp, '/')) != 0)
628 {
629 dname = strdup(temp);
630 dname[++fname - temp] = '\0';
631 }
632 else
633 {
634 dname = 0;
635 fname = temp;
636 }
637 if ((name = tempnam(dname, fname)) != 0)
638 {
639 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
640 strcpy(temp, name);
641 }
642 else
643 {
644 fd = -1;
645 }
646
647 if (dname != 0)
648 free(dname);
649
650 return fd;
651 }
652 #define mkstemp(s) my_mkstemp(s)
653 #endif
654
655 #endif
656
657 /*
658 * tmpfile() should be adequate, except that it may require special privileges
659 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
660 */
661 static FILE *
662 open_tmpfile(const char *label)
663 {
664 #define MY_FMT "%s/%.*sXXXXXX"
665 FILE *result;
666 #if USE_MKSTEMP
667 const char *tmpdir;
668 char *name;
669
670 if (((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) ||
671 ((tmpdir = getenv("TEMP")) == 0 || access(tmpdir, W_OK) != 0))
672 {
673 #ifdef P_tmpdir
674 tmpdir = P_tmpdir;
675 #else
676 tmpdir = "/tmp";
677 #endif
678 if (access(tmpdir, W_OK) != 0)
679 tmpdir = ".";
680 }
681
682 /* The size of the format is guaranteed to be longer than the result from
683 * printing empty strings with it; this calculation accounts for the
684 * string-lengths as well.
685 */
686 name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
687
688 result = 0;
689 if (name != 0)
690 {
691 int fd;
692 const char *mark;
693
694 mode_t save_umask = umask(0177);
695
696 if ((mark = strrchr(label, '_')) == 0)
697 mark = label + strlen(label);
698
699 sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
700 fd = mkstemp(name);
701 if (fd >= 0
702 && (result = fdopen(fd, "w+")) != 0)
703 {
704 MY_TMPFILES *item;
705
706 if (my_tmpfiles == 0)
707 {
708 atexit(close_tmpfiles);
709 }
710
711 item = NEW(MY_TMPFILES);
712 NO_SPACE(item);
713
714 item->name = name;
715 NO_SPACE(item->name);
716
717 item->next = my_tmpfiles;
718 my_tmpfiles = item;
719 }
720 else
721 {
722 FREE(name);
723 }
724 (void)umask(save_umask);
725 }
726 #else
727 result = tmpfile();
728 #endif
729
730 if (result == 0)
731 open_error(label);
732 return result;
733 #undef MY_FMT
734 }
735
736 static void
737 open_files(void)
738 {
739 create_file_names();
740
741 if (input_file == 0)
742 {
743 input_file = fopen(input_file_name, "r");
744 if (input_file == 0)
745 open_error(input_file_name);
746 }
747
748 action_file = open_tmpfile("action_file");
749 text_file = open_tmpfile("text_file");
750
751 if (vflag)
752 {
753 verbose_file = fopen(verbose_file_name, "w");
754 if (verbose_file == 0)
755 open_error(verbose_file_name);
756 }
757
758 if (gflag)
759 {
760 graph_file = fopen(graph_file_name, "w");
761 if (graph_file == 0)
762 open_error(graph_file_name);
763 fprintf(graph_file, "digraph %s {\n", file_prefix);
764 fprintf(graph_file, "\tedge [fontsize=10];\n");
765 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
766 fprintf(graph_file, "\torientation=landscape;\n");
767 fprintf(graph_file, "\trankdir=LR;\n");
768 fprintf(graph_file, "\t/*\n");
769 fprintf(graph_file, "\tmargin=0.2;\n");
770 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
771 fprintf(graph_file, "\tratio=auto;\n");
772 fprintf(graph_file, "\t*/\n");
773 }
774
775 if (dflag || dflag2)
776 {
777 defines_file = fopen(defines_file_name, "w");
778 if (defines_file == 0)
779 open_error(defines_file_name);
780 union_file = open_tmpfile("union_file");
781 }
782
783 if (iflag)
784 {
785 externs_file = fopen(externs_file_name, "w");
786 if (externs_file == 0)
787 open_error(externs_file_name);
788 }
789
790 output_file = fopen(output_file_name, "w");
791 if (output_file == 0)
792 open_error(output_file_name);
793
794 if (rflag)
795 {
796 code_file = fopen(code_file_name, "w");
797 if (code_file == 0)
798 open_error(code_file_name);
799 }
800 else
801 code_file = output_file;
802 }
803
804 int
805 main(int argc, char *argv[])
806 {
807 SRexpect = -1;
808 RRexpect = -1;
809 exit_code = EXIT_SUCCESS;
810
811 set_signals();
812 getargs(argc, argv);
813 open_files();
814 reader();
815 lr0();
816 lalr();
817 make_parser();
818 graph();
819 finalize_closure();
820 verbose();
821 output();
822 done(exit_code);
823 /*NOTREACHED */
824 }
825