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