loader: remove shouting from ORB's variable name
[hvf.git] / build / byacc / main.c
blob4014595524ac268fe141ad74fb2afbd4a34bf374
1 /* $Id: main.c,v 1.36 2011/09/06 22:44:45 tom Exp $ */
3 #include <signal.h>
4 #include <unistd.h> /* for _exit() */
6 #include "defs.h"
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
21 #if USE_MKSTEMP
22 #include <sys/types.h>
23 #include <sys/stat.h>
25 typedef struct _my_tmpfiles
27 struct _my_tmpfiles *next;
28 char *name;
30 MY_TMPFILES;
32 static MY_TMPFILES *my_tmpfiles;
33 #endif /* USE_MKSTEMP */
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;
44 const char *symbol_prefix;
45 const char *myname = "yacc";
47 int lineno;
48 int outline;
50 static char empty_string[] = "";
51 static char default_file_prefix[] = "y";
53 static char *file_prefix = default_file_prefix;
55 char *code_file_name;
56 char *input_file_name = empty_string;
57 char *defines_file_name;
58 char *externs_file_name;
60 static char *graph_file_name;
61 static char *output_file_name;
62 static char *verbose_file_name;
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 */
79 int nitems;
80 int nrules;
81 int nsyms;
82 int ntokens;
83 int nvars;
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;
92 int pure_parser;
93 int exit_code;
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;
104 * Since fclose() is called via the signal handler, it might die. Don't loop
105 * if there is a problem closing a file.
107 #define DO_CLOSE(fp) \
108 if (fp != 0) { \
109 FILE *use = fp; \
110 fp = 0; \
111 fclose(use); \
114 static int got_intr = 0;
116 void
117 done(int k)
119 DO_CLOSE(input_file);
120 DO_CLOSE(output_file);
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);
129 if (got_intr)
130 _exit(EXIT_FAILURE);
132 #ifdef NO_LEAKS
133 if (rflag)
134 DO_FREE(code_file_name);
136 if (dflag)
137 DO_FREE(defines_file_name);
139 if (iflag)
140 DO_FREE(externs_file_name);
142 if (oflag)
143 DO_FREE(output_file_name);
145 if (vflag)
146 DO_FREE(verbose_file_name);
148 if (gflag)
149 DO_FREE(graph_file_name);
151 lr0_leaks();
152 lalr_leaks();
153 mkpar_leaks();
154 output_leaks();
155 reader_leaks();
156 #endif
158 if (rflag)
159 DO_CLOSE(code_file);
161 exit(k);
164 static void
165 onintr(int sig GCC_UNUSED)
167 got_intr = 1;
168 done(EXIT_FAILURE);
171 static void
172 set_signals(void)
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
188 static void
189 usage(void)
191 static const char *msg[] =
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"
208 unsigned n;
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]);
215 exit(1);
218 static void
219 setflag(int ch)
221 switch (ch)
223 case 'd':
224 dflag = 1;
225 break;
227 case 'g':
228 gflag = 1;
229 break;
231 case 'i':
232 iflag = 1;
233 break;
235 case 'l':
236 lflag = 1;
237 break;
239 case 'P':
240 pure_parser = 1;
241 break;
243 case 'r':
244 rflag = 1;
245 break;
247 case 't':
248 tflag = 1;
249 break;
251 case 'v':
252 vflag = 1;
253 break;
255 case 'V':
256 printf("%s - %s\n", myname, VERSION);
257 exit(EXIT_SUCCESS);
259 case 'y':
260 /* noop for bison compatibility. byacc is already designed to be posix
261 * yacc compatible. */
262 break;
264 default:
265 usage();
269 static void
270 getargs(int argc, char *argv[])
272 int i;
273 char *s;
274 int ch;
276 if (argc > 0)
277 myname = argv[0];
279 for (i = 1; i < argc; ++i)
281 s = argv[i];
282 if (*s != '-')
283 break;
284 switch (ch = *++s)
286 case '\0':
287 input_file = stdin;
288 if (i + 1 < argc)
289 usage();
290 return;
292 case '-':
293 ++i;
294 goto no_more_options;
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;
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;
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;
323 default:
324 setflag(ch);
325 break;
328 for (;;)
330 switch (ch = *++s)
332 case '\0':
333 goto end_of_option;
335 default:
336 setflag(ch);
337 break;
340 end_of_option:;
343 no_more_options:;
344 if (i + 1 != argc)
345 usage();
346 input_file_name = argv[i];
349 void *
350 allocate(size_t n)
352 void *p;
354 p = NULL;
355 if (n)
357 p = CALLOC(1, n);
358 NO_SPACE(p);
360 return (p);
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)
369 static void
370 create_file_names(void)
372 size_t len;
373 const char *defines_suffix;
374 const char *externs_suffix;
375 char *prefix;
377 prefix = NULL;
378 defines_suffix = DEFINES_SUFFIX;
379 externs_suffix = EXTERNS_SUFFIX;
381 /* compute the file_prefix from the user provided output_file_name */
382 if (output_file_name != 0)
384 if (!(prefix = strstr(output_file_name, ".tab.c"))
385 && (prefix = strstr(output_file_name, ".c")))
387 defines_suffix = ".h";
388 externs_suffix = ".i";
392 if (prefix != NULL)
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;
399 else
400 len = strlen(file_prefix);
402 /* if "-o filename" was not given */
403 if (output_file_name == 0)
405 oflag = 1;
406 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
409 if (rflag)
411 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
413 else
414 code_file_name = output_file_name;
416 if (dflag)
418 CREATE_FILE_NAME(defines_file_name, defines_suffix);
421 if (iflag)
423 CREATE_FILE_NAME(externs_file_name, externs_suffix);
426 if (vflag)
428 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
431 if (gflag)
433 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
436 if (prefix != NULL)
438 FREE(file_prefix);
442 #if USE_MKSTEMP
443 static void
444 close_tmpfiles(void)
446 while (my_tmpfiles != 0)
448 MY_TMPFILES *next = my_tmpfiles->next;
450 chmod(my_tmpfiles->name, 0644);
451 unlink(my_tmpfiles->name);
453 free(my_tmpfiles->name);
454 free(my_tmpfiles);
456 my_tmpfiles = next;
460 #ifndef HAVE_MKSTEMP
461 static int
462 my_mkstemp(char *temp)
464 int fd;
465 char *dname;
466 char *fname;
467 char *name;
470 * Split-up to use tempnam, rather than tmpnam; the latter (like
471 * mkstemp) is unusable on Windows.
473 if ((fname = strrchr(temp, '/')) != 0)
475 dname = strdup(temp);
476 dname[++fname - temp] = '\0';
478 else
480 dname = 0;
481 fname = temp;
483 if ((name = tempnam(dname, fname)) != 0)
485 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
486 strcpy(temp, name);
488 else
490 fd = -1;
493 if (dname != 0)
494 free(dname);
496 return fd;
498 #define mkstemp(s) my_mkstemp(s)
499 #endif
501 #endif
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.
507 static FILE *
508 open_tmpfile(const char *label)
510 FILE *result;
511 #if USE_MKSTEMP
512 int fd;
513 const char *tmpdir;
514 char *name;
515 const char *mark;
517 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
519 #ifdef P_tmpdir
520 tmpdir = P_tmpdir;
521 #else
522 tmpdir = "/tmp";
523 #endif
524 if (access(tmpdir, W_OK) != 0)
525 tmpdir = ".";
528 name = malloc(strlen(tmpdir) + 10 + strlen(label));
530 result = 0;
531 if (name != 0)
533 if ((mark = strrchr(label, '_')) == 0)
534 mark = label + strlen(label);
536 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
537 fd = mkstemp(name);
538 if (fd >= 0)
540 result = fdopen(fd, "w+");
541 if (result != 0)
543 MY_TMPFILES *item;
545 if (my_tmpfiles == 0)
547 atexit(close_tmpfiles);
550 item = NEW(MY_TMPFILES);
551 NO_SPACE(item);
553 item->name = name;
554 NO_SPACE(item->name);
556 item->next = my_tmpfiles;
557 my_tmpfiles = item;
561 #else
562 result = tmpfile();
563 #endif
565 if (result == 0)
566 open_error(label);
567 return result;
570 static void
571 open_files(void)
573 create_file_names();
575 if (input_file == 0)
577 input_file = fopen(input_file_name, "r");
578 if (input_file == 0)
579 open_error(input_file_name);
582 action_file = open_tmpfile("action_file");
583 text_file = open_tmpfile("text_file");
585 if (vflag)
587 verbose_file = fopen(verbose_file_name, "w");
588 if (verbose_file == 0)
589 open_error(verbose_file_name);
592 if (gflag)
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");
609 if (dflag)
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");
617 if (iflag)
619 externs_file = fopen(externs_file_name, "w");
620 if (externs_file == 0)
621 open_error(externs_file_name);
624 output_file = fopen(output_file_name, "w");
625 if (output_file == 0)
626 open_error(output_file_name);
628 if (rflag)
630 code_file = fopen(code_file_name, "w");
631 if (code_file == 0)
632 open_error(code_file_name);
634 else
635 code_file = output_file;
639 main(int argc, char *argv[])
641 SRexpect = -1;
642 RRexpect = -1;
643 exit_code = EXIT_SUCCESS;
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 */