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