sbin/hammer: Fix whitespace alignment changed by e0d7dd09
[dragonfly.git] / contrib / byacc / main.c
blobb43cd134a151759996d96e730984057217b240dc
1 /* $Id: main.c,v 1.54 2014/10/06 22:40:07 tom Exp $ */
3 #include <signal.h>
4 #ifndef _WIN32
5 #include <unistd.h> /* for _exit() */
6 #else
7 #include <stdlib.h> /* for _exit() */
8 #endif
10 #include "defs.h"
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
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 sflag;
42 char tflag;
43 char vflag;
45 const char *symbol_prefix;
46 const char *myname = "yacc";
48 int lineno;
49 int outline;
51 static char empty_string[] = "";
52 static char default_file_prefix[] = "y";
54 static char *file_prefix = default_file_prefix;
56 char *code_file_name;
57 char *input_file_name = empty_string;
58 char *defines_file_name;
59 char *externs_file_name;
61 static char *graph_file_name;
62 static char *output_file_name;
63 static char *verbose_file_name;
65 FILE *action_file; /* a temp file, used to save actions associated */
66 /* with rules until the parser is written */
67 FILE *code_file; /* y.code.c (used when the -r option is specified) */
68 FILE *defines_file; /* y.tab.h */
69 FILE *externs_file; /* y.tab.i */
70 FILE *input_file; /* the input file */
71 FILE *output_file; /* y.tab.c */
72 FILE *text_file; /* a temp file, used to save text until all */
73 /* symbols have been defined */
74 FILE *union_file; /* a temp file, used to save the union */
75 /* definition until all symbol have been */
76 /* defined */
77 FILE *verbose_file; /* y.output */
78 FILE *graph_file; /* y.dot */
80 Value_t nitems;
81 Value_t nrules;
82 Value_t nsyms;
83 Value_t ntokens;
84 Value_t nvars;
86 Value_t start_symbol;
87 char **symbol_name;
88 char **symbol_pname;
89 Value_t *symbol_value;
90 Value_t *symbol_prec;
91 char *symbol_assoc;
93 int pure_parser;
94 int token_table;
96 #if defined(YYBTYACC)
97 Value_t *symbol_pval;
98 char **symbol_destructor;
99 char **symbol_type_tag;
100 int locations = 0; /* default to no position processing */
101 int backtrack = 0; /* default is no backtracking */
102 #endif
104 int exit_code;
106 Value_t *ritem;
107 Value_t *rlhs;
108 Value_t *rrhs;
109 Value_t *rprec;
110 Assoc_t *rassoc;
111 Value_t **derives;
112 char *nullable;
115 * Since fclose() is called via the signal handler, it might die. Don't loop
116 * if there is a problem closing a file.
118 #define DO_CLOSE(fp) \
119 if (fp != 0) { \
120 FILE *use = fp; \
121 fp = 0; \
122 fclose(use); \
125 static int got_intr = 0;
127 void
128 done(int k)
130 DO_CLOSE(input_file);
131 DO_CLOSE(output_file);
132 if (iflag)
133 DO_CLOSE(externs_file);
134 if (rflag)
135 DO_CLOSE(code_file);
137 DO_CLOSE(action_file);
138 DO_CLOSE(defines_file);
139 DO_CLOSE(graph_file);
140 DO_CLOSE(text_file);
141 DO_CLOSE(union_file);
142 DO_CLOSE(verbose_file);
144 if (got_intr)
145 _exit(EXIT_FAILURE);
147 #ifdef NO_LEAKS
148 if (rflag)
149 DO_FREE(code_file_name);
151 if (dflag)
152 DO_FREE(defines_file_name);
154 if (iflag)
155 DO_FREE(externs_file_name);
157 if (oflag)
158 DO_FREE(output_file_name);
160 if (vflag)
161 DO_FREE(verbose_file_name);
163 if (gflag)
164 DO_FREE(graph_file_name);
166 lr0_leaks();
167 lalr_leaks();
168 mkpar_leaks();
169 mstring_leaks();
170 output_leaks();
171 reader_leaks();
172 #endif
174 exit(k);
177 static void
178 onintr(int sig GCC_UNUSED)
180 got_intr = 1;
181 done(EXIT_FAILURE);
184 static void
185 set_signals(void)
187 #ifdef SIGINT
188 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
189 signal(SIGINT, onintr);
190 #endif
191 #ifdef SIGTERM
192 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
193 signal(SIGTERM, onintr);
194 #endif
195 #ifdef SIGHUP
196 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
197 signal(SIGHUP, onintr);
198 #endif
201 static void
202 usage(void)
204 static const char *msg[] =
207 ,"Options:"
208 ," -b file_prefix set filename prefix (default \"y.\")"
209 ," -B create a backtracking parser"
210 ," -d write definitions (" DEFINES_SUFFIX ")"
211 ," -i write interface (y.tab.i)"
212 ," -g write a graphical description"
213 ," -l suppress #line directives"
214 ," -L enable position processing, e.g., \"%locations\""
215 ," -o output_file (default \"" OUTPUT_SUFFIX "\")"
216 ," -p symbol_prefix set symbol prefix (default \"yy\")"
217 ," -P create a reentrant parser, e.g., \"%pure-parser\""
218 ," -r produce separate code and table files (y.code.c)"
219 ," -s suppress #define's for quoted names in %token lines"
220 ," -t add debugging support"
221 ," -v write description (y.output)"
222 ," -V show version information and exit"
224 unsigned n;
226 fflush(stdout);
227 fprintf(stderr, "Usage: %s [options] filename\n", myname);
228 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
229 fprintf(stderr, "%s\n", msg[n]);
231 exit(1);
234 static void
235 setflag(int ch)
237 switch (ch)
239 case 'B':
240 #if defined(YYBTYACC)
241 backtrack = 1;
242 #else
243 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
244 #endif
245 break;
247 case 'd':
248 dflag = 1;
249 break;
251 case 'g':
252 gflag = 1;
253 break;
255 case 'i':
256 iflag = 1;
257 break;
259 case 'l':
260 lflag = 1;
261 break;
263 case 'L':
264 #if defined(YYBTYACC)
265 locations = 1;
266 #else
267 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
268 #endif
269 break;
271 case 'P':
272 pure_parser = 1;
273 break;
275 case 'r':
276 rflag = 1;
277 break;
279 case 's':
280 sflag = 1;
281 break;
283 case 't':
284 tflag = 1;
285 break;
287 case 'v':
288 vflag = 1;
289 break;
291 case 'V':
292 printf("%s - %s\n", myname, VERSION);
293 exit(EXIT_SUCCESS);
295 case 'y':
296 /* noop for bison compatibility. byacc is already designed to be posix
297 * yacc compatible. */
298 break;
300 default:
301 usage();
305 static void
306 getargs(int argc, char *argv[])
308 int i;
309 char *s;
310 int ch;
312 if (argc > 0)
313 myname = argv[0];
315 for (i = 1; i < argc; ++i)
317 s = argv[i];
318 if (*s != '-')
319 break;
320 switch (ch = *++s)
322 case '\0':
323 input_file = stdin;
324 if (i + 1 < argc)
325 usage();
326 return;
328 case '-':
329 ++i;
330 goto no_more_options;
332 case 'b':
333 if (*++s)
334 file_prefix = s;
335 else if (++i < argc)
336 file_prefix = argv[i];
337 else
338 usage();
339 continue;
341 case 'o':
342 if (*++s)
343 output_file_name = s;
344 else if (++i < argc)
345 output_file_name = argv[i];
346 else
347 usage();
348 continue;
350 case 'p':
351 if (*++s)
352 symbol_prefix = s;
353 else if (++i < argc)
354 symbol_prefix = argv[i];
355 else
356 usage();
357 continue;
359 default:
360 setflag(ch);
361 break;
364 for (;;)
366 switch (ch = *++s)
368 case '\0':
369 goto end_of_option;
371 default:
372 setflag(ch);
373 break;
376 end_of_option:;
379 no_more_options:;
380 if (i + 1 != argc)
381 usage();
382 input_file_name = argv[i];
385 void *
386 allocate(size_t n)
388 void *p;
390 p = NULL;
391 if (n)
393 p = CALLOC(1, n);
394 NO_SPACE(p);
396 return (p);
399 #define CREATE_FILE_NAME(dest, suffix) \
400 dest = alloc_file_name(len, suffix)
402 static char *
403 alloc_file_name(size_t len, const char *suffix)
405 char *result = TMALLOC(char, len + strlen(suffix) + 1);
406 if (result == 0)
407 no_space();
408 strcpy(result, file_prefix);
409 strcpy(result + len, suffix);
410 return result;
413 static void
414 create_file_names(void)
416 size_t len;
417 const char *defines_suffix;
418 const char *externs_suffix;
419 char *prefix;
421 prefix = NULL;
422 defines_suffix = DEFINES_SUFFIX;
423 externs_suffix = EXTERNS_SUFFIX;
425 /* compute the file_prefix from the user provided output_file_name */
426 if (output_file_name != 0)
428 if (!(prefix = strstr(output_file_name, OUTPUT_SUFFIX))
429 && (prefix = strstr(output_file_name, ".c")))
431 defines_suffix = ".h";
432 externs_suffix = ".i";
436 if (prefix != NULL)
438 len = (size_t) (prefix - output_file_name);
439 file_prefix = TMALLOC(char, len + 1);
440 NO_SPACE(file_prefix);
441 strncpy(file_prefix, output_file_name, len)[len] = 0;
443 else
444 len = strlen(file_prefix);
446 /* if "-o filename" was not given */
447 if (output_file_name == 0)
449 oflag = 1;
450 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
453 if (rflag)
455 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
457 else
458 code_file_name = output_file_name;
460 if (dflag)
462 CREATE_FILE_NAME(defines_file_name, defines_suffix);
465 if (iflag)
467 CREATE_FILE_NAME(externs_file_name, externs_suffix);
470 if (vflag)
472 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
475 if (gflag)
477 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
480 if (prefix != NULL)
482 FREE(file_prefix);
486 #if USE_MKSTEMP
487 static void
488 close_tmpfiles(void)
490 while (my_tmpfiles != 0)
492 MY_TMPFILES *next = my_tmpfiles->next;
494 (void)chmod(my_tmpfiles->name, 0644);
495 (void)unlink(my_tmpfiles->name);
497 free(my_tmpfiles->name);
498 free(my_tmpfiles);
500 my_tmpfiles = next;
504 #ifndef HAVE_MKSTEMP
505 static int
506 my_mkstemp(char *temp)
508 int fd;
509 char *dname;
510 char *fname;
511 char *name;
514 * Split-up to use tempnam, rather than tmpnam; the latter (like
515 * mkstemp) is unusable on Windows.
517 if ((fname = strrchr(temp, '/')) != 0)
519 dname = strdup(temp);
520 dname[++fname - temp] = '\0';
522 else
524 dname = 0;
525 fname = temp;
527 if ((name = tempnam(dname, fname)) != 0)
529 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
530 strcpy(temp, name);
532 else
534 fd = -1;
537 if (dname != 0)
538 free(dname);
540 return fd;
542 #define mkstemp(s) my_mkstemp(s)
543 #endif
545 #endif
548 * tmpfile() should be adequate, except that it may require special privileges
549 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
551 static FILE *
552 open_tmpfile(const char *label)
554 FILE *result;
555 #if USE_MKSTEMP
556 int fd;
557 const char *tmpdir;
558 char *name;
559 const char *mark;
561 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
563 #ifdef P_tmpdir
564 tmpdir = P_tmpdir;
565 #else
566 tmpdir = "/tmp";
567 #endif
568 if (access(tmpdir, W_OK) != 0)
569 tmpdir = ".";
572 name = malloc(strlen(tmpdir) + 10 + strlen(label));
574 result = 0;
575 if (name != 0)
577 mode_t save_umask = umask(0177);
579 if ((mark = strrchr(label, '_')) == 0)
580 mark = label + strlen(label);
582 sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
583 fd = mkstemp(name);
584 if (fd >= 0)
586 result = fdopen(fd, "w+");
587 if (result != 0)
589 MY_TMPFILES *item;
591 if (my_tmpfiles == 0)
593 atexit(close_tmpfiles);
596 item = NEW(MY_TMPFILES);
597 NO_SPACE(item);
599 item->name = name;
600 NO_SPACE(item->name);
602 item->next = my_tmpfiles;
603 my_tmpfiles = item;
606 (void)umask(save_umask);
608 #else
609 result = tmpfile();
610 #endif
612 if (result == 0)
613 open_error(label);
614 return result;
617 static void
618 open_files(void)
620 create_file_names();
622 if (input_file == 0)
624 input_file = fopen(input_file_name, "r");
625 if (input_file == 0)
626 open_error(input_file_name);
629 action_file = open_tmpfile("action_file");
630 text_file = open_tmpfile("text_file");
632 if (vflag)
634 verbose_file = fopen(verbose_file_name, "w");
635 if (verbose_file == 0)
636 open_error(verbose_file_name);
639 if (gflag)
641 graph_file = fopen(graph_file_name, "w");
642 if (graph_file == 0)
643 open_error(graph_file_name);
644 fprintf(graph_file, "digraph %s {\n", file_prefix);
645 fprintf(graph_file, "\tedge [fontsize=10];\n");
646 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
647 fprintf(graph_file, "\torientation=landscape;\n");
648 fprintf(graph_file, "\trankdir=LR;\n");
649 fprintf(graph_file, "\t/*\n");
650 fprintf(graph_file, "\tmargin=0.2;\n");
651 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
652 fprintf(graph_file, "\tratio=auto;\n");
653 fprintf(graph_file, "\t*/\n");
656 if (dflag)
658 defines_file = fopen(defines_file_name, "w");
659 if (defines_file == 0)
660 open_error(defines_file_name);
661 union_file = open_tmpfile("union_file");
664 if (iflag)
666 externs_file = fopen(externs_file_name, "w");
667 if (externs_file == 0)
668 open_error(externs_file_name);
671 output_file = fopen(output_file_name, "w");
672 if (output_file == 0)
673 open_error(output_file_name);
675 if (rflag)
677 code_file = fopen(code_file_name, "w");
678 if (code_file == 0)
679 open_error(code_file_name);
681 else
682 code_file = output_file;
686 main(int argc, char *argv[])
688 SRexpect = -1;
689 RRexpect = -1;
690 exit_code = EXIT_SUCCESS;
692 set_signals();
693 getargs(argc, argv);
694 open_files();
695 reader();
696 lr0();
697 lalr();
698 make_parser();
699 graph();
700 finalize_closure();
701 verbose();
702 output();
703 done(exit_code);
704 /*NOTREACHED */