kernel - Handle spinlock indefinite wait edge case
[dragonfly.git] / contrib / byacc / main.c
blob9a8099c5ec2ec99c09e8ee1874cef1ff3cfff60b
1 /* $Id: main.c,v 1.60 2017/04/30 20:57:56 Julien.Ramseier 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 default_file_prefix[] = "y";
53 static char *file_prefix = default_file_prefix;
55 char *code_file_name;
56 char *input_file_name;
57 size_t input_file_name_len = 0;
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;
95 int error_verbose;
97 #if defined(YYBTYACC)
98 Value_t *symbol_pval;
99 char **symbol_destructor;
100 char **symbol_type_tag;
101 int locations = 0; /* default to no position processing */
102 int backtrack = 0; /* default is no backtracking */
103 char *initial_action = NULL;
104 #endif
106 int exit_code;
108 Value_t *ritem;
109 Value_t *rlhs;
110 Value_t *rrhs;
111 Value_t *rprec;
112 Assoc_t *rassoc;
113 Value_t **derives;
114 char *nullable;
117 * Since fclose() is called via the signal handler, it might die. Don't loop
118 * if there is a problem closing a file.
120 #define DO_CLOSE(fp) \
121 if (fp != 0) { \
122 FILE *use = fp; \
123 fp = 0; \
124 fclose(use); \
127 static int got_intr = 0;
129 void
130 done(int k)
132 DO_CLOSE(input_file);
133 DO_CLOSE(output_file);
134 if (iflag)
135 DO_CLOSE(externs_file);
136 if (rflag)
137 DO_CLOSE(code_file);
139 DO_CLOSE(action_file);
140 DO_CLOSE(defines_file);
141 DO_CLOSE(graph_file);
142 DO_CLOSE(text_file);
143 DO_CLOSE(union_file);
144 DO_CLOSE(verbose_file);
146 if (got_intr)
147 _exit(EXIT_FAILURE);
149 #ifdef NO_LEAKS
150 if (rflag)
151 DO_FREE(code_file_name);
153 if (dflag)
154 DO_FREE(defines_file_name);
156 if (iflag)
157 DO_FREE(externs_file_name);
159 if (oflag)
160 DO_FREE(output_file_name);
162 if (vflag)
163 DO_FREE(verbose_file_name);
165 if (gflag)
166 DO_FREE(graph_file_name);
168 lr0_leaks();
169 lalr_leaks();
170 mkpar_leaks();
171 mstring_leaks();
172 output_leaks();
173 reader_leaks();
174 #endif
176 exit(k);
179 static void
180 onintr(int sig GCC_UNUSED)
182 got_intr = 1;
183 done(EXIT_FAILURE);
186 static void
187 set_signals(void)
189 #ifdef SIGINT
190 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
191 signal(SIGINT, onintr);
192 #endif
193 #ifdef SIGTERM
194 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
195 signal(SIGTERM, onintr);
196 #endif
197 #ifdef SIGHUP
198 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
199 signal(SIGHUP, onintr);
200 #endif
203 static void
204 usage(void)
206 static const char *msg[] =
209 ,"Options:"
210 ," -b file_prefix set filename prefix (default \"y.\")"
211 ," -B create a backtracking parser"
212 ," -d write definitions (" DEFINES_SUFFIX ")"
213 ," -i write interface (y.tab.i)"
214 ," -g write a graphical description"
215 ," -l suppress #line directives"
216 ," -L enable position processing, e.g., \"%locations\""
217 ," -o output_file (default \"" OUTPUT_SUFFIX "\")"
218 ," -p symbol_prefix set symbol prefix (default \"yy\")"
219 ," -P create a reentrant parser, e.g., \"%pure-parser\""
220 ," -r produce separate code and table files (y.code.c)"
221 ," -s suppress #define's for quoted names in %token lines"
222 ," -t add debugging support"
223 ," -v write description (y.output)"
224 ," -V show version information and exit"
226 unsigned n;
228 fflush(stdout);
229 fprintf(stderr, "Usage: %s [options] filename\n", myname);
230 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
231 fprintf(stderr, "%s\n", msg[n]);
233 exit(1);
236 static void
237 setflag(int ch)
239 switch (ch)
241 case 'B':
242 #if defined(YYBTYACC)
243 backtrack = 1;
244 #else
245 unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
246 #endif
247 break;
249 case 'd':
250 dflag = 1;
251 break;
253 case 'g':
254 gflag = 1;
255 break;
257 case 'i':
258 iflag = 1;
259 break;
261 case 'l':
262 lflag = 1;
263 break;
265 case 'L':
266 #if defined(YYBTYACC)
267 locations = 1;
268 #else
269 unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
270 #endif
271 break;
273 case 'P':
274 pure_parser = 1;
275 break;
277 case 'r':
278 rflag = 1;
279 break;
281 case 's':
282 sflag = 1;
283 break;
285 case 't':
286 tflag = 1;
287 break;
289 case 'v':
290 vflag = 1;
291 break;
293 case 'V':
294 printf("%s - %s\n", myname, VERSION);
295 exit(EXIT_SUCCESS);
297 case 'y':
298 /* noop for bison compatibility. byacc is already designed to be posix
299 * yacc compatible. */
300 break;
302 default:
303 usage();
307 static void
308 getargs(int argc, char *argv[])
310 int i;
311 char *s;
312 int ch;
314 if (argc > 0)
315 myname = argv[0];
317 for (i = 1; i < argc; ++i)
319 s = argv[i];
320 if (*s != '-')
321 break;
322 switch (ch = *++s)
324 case '\0':
325 input_file = stdin;
326 if (i + 1 < argc)
327 usage();
328 return;
330 case '-':
331 ++i;
332 goto no_more_options;
334 case 'b':
335 if (*++s)
336 file_prefix = s;
337 else if (++i < argc)
338 file_prefix = argv[i];
339 else
340 usage();
341 continue;
343 case 'o':
344 if (*++s)
345 output_file_name = s;
346 else if (++i < argc)
347 output_file_name = argv[i];
348 else
349 usage();
350 continue;
352 case 'p':
353 if (*++s)
354 symbol_prefix = s;
355 else if (++i < argc)
356 symbol_prefix = argv[i];
357 else
358 usage();
359 continue;
361 default:
362 setflag(ch);
363 break;
366 for (;;)
368 switch (ch = *++s)
370 case '\0':
371 goto end_of_option;
373 default:
374 setflag(ch);
375 break;
378 end_of_option:;
381 no_more_options:;
382 if (i + 1 != argc)
383 usage();
384 input_file_name_len = strlen(argv[i]);
385 input_file_name = TMALLOC(char, input_file_name_len + 1);
386 NO_SPACE(input_file_name);
387 strcpy(input_file_name, argv[i]);
390 void *
391 allocate(size_t n)
393 void *p;
395 p = NULL;
396 if (n)
398 p = CALLOC(1, n);
399 NO_SPACE(p);
401 return (p);
404 #define CREATE_FILE_NAME(dest, suffix) \
405 dest = alloc_file_name(len, suffix)
407 static char *
408 alloc_file_name(size_t len, const char *suffix)
410 char *result = TMALLOC(char, len + strlen(suffix) + 1);
411 if (result == 0)
412 no_space();
413 strcpy(result, file_prefix);
414 strcpy(result + len, suffix);
415 return result;
418 static char *
419 find_suffix(char *name, const char *suffix)
421 size_t len = strlen(name);
422 size_t slen = strlen(suffix);
423 if (len >= slen)
425 name += len - slen;
426 if (strcmp(name, suffix) == 0)
427 return name;
429 return NULL;
432 static void
433 create_file_names(void)
435 size_t len;
436 const char *defines_suffix;
437 const char *externs_suffix;
438 char *suffix;
440 suffix = NULL;
441 defines_suffix = DEFINES_SUFFIX;
442 externs_suffix = EXTERNS_SUFFIX;
444 /* compute the file_prefix from the user provided output_file_name */
445 if (output_file_name != 0)
447 if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
448 && (suffix = find_suffix(output_file_name, ".c")))
450 defines_suffix = ".h";
451 externs_suffix = ".i";
455 if (suffix != NULL)
457 len = (size_t) (suffix - output_file_name);
458 file_prefix = TMALLOC(char, len + 1);
459 NO_SPACE(file_prefix);
460 strncpy(file_prefix, output_file_name, len)[len] = 0;
462 else
463 len = strlen(file_prefix);
465 /* if "-o filename" was not given */
466 if (output_file_name == 0)
468 oflag = 1;
469 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
472 if (rflag)
474 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
476 else
477 code_file_name = output_file_name;
479 if (dflag)
481 CREATE_FILE_NAME(defines_file_name, defines_suffix);
484 if (iflag)
486 CREATE_FILE_NAME(externs_file_name, externs_suffix);
489 if (vflag)
491 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
494 if (gflag)
496 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
499 if (suffix != NULL)
501 FREE(file_prefix);
505 #if USE_MKSTEMP
506 static void
507 close_tmpfiles(void)
509 while (my_tmpfiles != 0)
511 MY_TMPFILES *next = my_tmpfiles->next;
513 (void)chmod(my_tmpfiles->name, 0644);
514 (void)unlink(my_tmpfiles->name);
516 free(my_tmpfiles->name);
517 free(my_tmpfiles);
519 my_tmpfiles = next;
523 #ifndef HAVE_MKSTEMP
524 static int
525 my_mkstemp(char *temp)
527 int fd;
528 char *dname;
529 char *fname;
530 char *name;
533 * Split-up to use tempnam, rather than tmpnam; the latter (like
534 * mkstemp) is unusable on Windows.
536 if ((fname = strrchr(temp, '/')) != 0)
538 dname = strdup(temp);
539 dname[++fname - temp] = '\0';
541 else
543 dname = 0;
544 fname = temp;
546 if ((name = tempnam(dname, fname)) != 0)
548 fd = open(name, O_CREAT | O_EXCL | O_RDWR);
549 strcpy(temp, name);
551 else
553 fd = -1;
556 if (dname != 0)
557 free(dname);
559 return fd;
561 #define mkstemp(s) my_mkstemp(s)
562 #endif
564 #endif
567 * tmpfile() should be adequate, except that it may require special privileges
568 * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
570 static FILE *
571 open_tmpfile(const char *label)
573 #define MY_FMT "%s/%.*sXXXXXX"
574 FILE *result;
575 #if USE_MKSTEMP
576 int fd;
577 const char *tmpdir;
578 char *name;
579 const char *mark;
581 if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
583 #ifdef P_tmpdir
584 tmpdir = P_tmpdir;
585 #else
586 tmpdir = "/tmp";
587 #endif
588 if (access(tmpdir, W_OK) != 0)
589 tmpdir = ".";
592 /* The size of the format is guaranteed to be longer than the result from
593 * printing empty strings with it; this calculation accounts for the
594 * string-lengths as well.
596 name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
598 result = 0;
599 if (name != 0)
601 mode_t save_umask = umask(0177);
603 if ((mark = strrchr(label, '_')) == 0)
604 mark = label + strlen(label);
606 sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
607 fd = mkstemp(name);
608 if (fd >= 0)
610 result = fdopen(fd, "w+");
611 if (result != 0)
613 MY_TMPFILES *item;
615 if (my_tmpfiles == 0)
617 atexit(close_tmpfiles);
620 item = NEW(MY_TMPFILES);
621 NO_SPACE(item);
623 item->name = name;
624 NO_SPACE(item->name);
626 item->next = my_tmpfiles;
627 my_tmpfiles = item;
630 (void)umask(save_umask);
632 #else
633 result = tmpfile();
634 #endif
636 if (result == 0)
637 open_error(label);
638 return result;
639 #undef MY_FMT
642 static void
643 open_files(void)
645 create_file_names();
647 if (input_file == 0)
649 input_file = fopen(input_file_name, "r");
650 if (input_file == 0)
651 open_error(input_file_name);
654 action_file = open_tmpfile("action_file");
655 text_file = open_tmpfile("text_file");
657 if (vflag)
659 verbose_file = fopen(verbose_file_name, "w");
660 if (verbose_file == 0)
661 open_error(verbose_file_name);
664 if (gflag)
666 graph_file = fopen(graph_file_name, "w");
667 if (graph_file == 0)
668 open_error(graph_file_name);
669 fprintf(graph_file, "digraph %s {\n", file_prefix);
670 fprintf(graph_file, "\tedge [fontsize=10];\n");
671 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
672 fprintf(graph_file, "\torientation=landscape;\n");
673 fprintf(graph_file, "\trankdir=LR;\n");
674 fprintf(graph_file, "\t/*\n");
675 fprintf(graph_file, "\tmargin=0.2;\n");
676 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
677 fprintf(graph_file, "\tratio=auto;\n");
678 fprintf(graph_file, "\t*/\n");
681 if (dflag)
683 defines_file = fopen(defines_file_name, "w");
684 if (defines_file == 0)
685 open_error(defines_file_name);
686 union_file = open_tmpfile("union_file");
689 if (iflag)
691 externs_file = fopen(externs_file_name, "w");
692 if (externs_file == 0)
693 open_error(externs_file_name);
696 output_file = fopen(output_file_name, "w");
697 if (output_file == 0)
698 open_error(output_file_name);
700 if (rflag)
702 code_file = fopen(code_file_name, "w");
703 if (code_file == 0)
704 open_error(code_file_name);
706 else
707 code_file = output_file;
711 main(int argc, char *argv[])
713 SRexpect = -1;
714 RRexpect = -1;
715 exit_code = EXIT_SUCCESS;
717 set_signals();
718 getargs(argc, argv);
719 open_files();
720 reader();
721 lr0();
722 lalr();
723 make_parser();
724 graph();
725 finalize_closure();
726 verbose();
727 output();
728 done(exit_code);
729 /*NOTREACHED */