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