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