main.c revision 272953
1/* $Id: main.c,v 1.51 2014/05/28 02:01:55 Tom.Shields Exp $ */
2
3#include <signal.h>
4#ifndef _WIN32
5#include <unistd.h>		/* for _exit() */
6#else
7#include <stdlib.h>		/* for _exit() */
8#endif
9
10#include "defs.h"
11
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
20
21#if USE_MKSTEMP
22#include <sys/types.h>
23#include <sys/stat.h>
24
25typedef struct _my_tmpfiles
26{
27    struct _my_tmpfiles *next;
28    char *name;
29}
30MY_TMPFILES;
31
32static MY_TMPFILES *my_tmpfiles;
33#endif /* USE_MKSTEMP */
34
35char dflag;
36char gflag;
37char iflag;
38char lflag;
39static char oflag;
40char rflag;
41char sflag;
42char tflag;
43char vflag;
44
45const char *symbol_prefix;
46const char *myname = "yacc";
47
48int lineno;
49int outline;
50
51static char empty_string[] = "";
52static char default_file_prefix[] = "y";
53
54static char *file_prefix = default_file_prefix;
55
56char *code_file_name;
57char *input_file_name = empty_string;
58char *defines_file_name;
59char *externs_file_name;
60
61static char *graph_file_name;
62static char *output_file_name;
63static char *verbose_file_name;
64
65FILE *action_file;	/*  a temp file, used to save actions associated    */
66			/*  with rules until the parser is written          */
67FILE *code_file;	/*  y.code.c (used when the -r option is specified) */
68FILE *defines_file;	/*  y.tab.h                                         */
69FILE *externs_file;	/*  y.tab.i                                         */
70FILE *input_file;	/*  the input file                                  */
71FILE *output_file;	/*  y.tab.c                                         */
72FILE *text_file;	/*  a temp file, used to save text until all        */
73			/*  symbols have been defined                       */
74FILE *union_file;	/*  a temp file, used to save the union             */
75			/*  definition until all symbol have been           */
76			/*  defined                                         */
77FILE *verbose_file;	/*  y.output                                        */
78FILE *graph_file;	/*  y.dot                                           */
79
80Value_t nitems;
81Value_t nrules;
82Value_t nsyms;
83Value_t ntokens;
84Value_t nvars;
85
86Value_t start_symbol;
87char **symbol_name;
88char **symbol_pname;
89Value_t *symbol_value;
90Value_t *symbol_prec;
91char *symbol_assoc;
92
93int pure_parser;
94int token_table;
95
96#if defined(YYBTYACC)
97Value_t *symbol_pval;
98char **symbol_destructor;
99char **symbol_type_tag;
100int locations = 0;	/* default to no position processing */
101int backtrack = 0;	/* default is no backtracking */
102#endif
103
104int exit_code;
105
106Value_t *ritem;
107Value_t *rlhs;
108Value_t *rrhs;
109Value_t *rprec;
110Assoc_t *rassoc;
111Value_t **derives;
112char *nullable;
113
114/*
115 * Since fclose() is called via the signal handler, it might die.  Don't loop
116 * if there is a problem closing a file.
117 */
118#define DO_CLOSE(fp) \
119	if (fp != 0) { \
120	    FILE *use = fp; \
121	    fp = 0; \
122	    fclose(use); \
123	}
124
125static int got_intr = 0;
126
127void
128done(int k)
129{
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);
136
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);
143
144    if (got_intr)
145	_exit(EXIT_FAILURE);
146
147#ifdef NO_LEAKS
148    if (rflag)
149	DO_FREE(code_file_name);
150
151    if (dflag)
152	DO_FREE(defines_file_name);
153
154    if (iflag)
155	DO_FREE(externs_file_name);
156
157    if (oflag)
158	DO_FREE(output_file_name);
159
160    if (vflag)
161	DO_FREE(verbose_file_name);
162
163    if (gflag)
164	DO_FREE(graph_file_name);
165
166    lr0_leaks();
167    lalr_leaks();
168    mkpar_leaks();
169    mstring_leaks();
170    output_leaks();
171    reader_leaks();
172#endif
173
174    exit(k);
175}
176
177static void
178onintr(int sig GCC_UNUSED)
179{
180    got_intr = 1;
181    done(EXIT_FAILURE);
182}
183
184static void
185set_signals(void)
186{
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
199}
200
201static void
202usage(void)
203{
204    static const char *msg[] =
205    {
206	""
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"
223    };
224    unsigned n;
225
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]);
230
231    exit(1);
232}
233
234static void
235setflag(int ch)
236{
237    switch (ch)
238    {
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;
246
247    case 'd':
248	dflag = 1;
249	break;
250
251    case 'g':
252	gflag = 1;
253	break;
254
255    case 'i':
256	iflag = 1;
257	break;
258
259    case 'l':
260	lflag = 1;
261	break;
262
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;
270
271    case 'P':
272	pure_parser = 1;
273	break;
274
275    case 'r':
276	rflag = 1;
277	break;
278
279    case 's':
280	sflag = 1;
281	break;
282
283    case 't':
284	tflag = 1;
285	break;
286
287    case 'v':
288	vflag = 1;
289	break;
290
291    case 'V':
292	printf("%s - %s\n", myname, VERSION);
293	exit(EXIT_SUCCESS);
294
295    case 'y':
296	/* noop for bison compatibility. byacc is already designed to be posix
297	 * yacc compatible. */
298	break;
299
300    default:
301	usage();
302    }
303}
304
305static void
306getargs(int argc, char *argv[])
307{
308    int i;
309    char *s;
310    int ch;
311
312    if (argc > 0)
313	myname = argv[0];
314
315    for (i = 1; i < argc; ++i)
316    {
317	s = argv[i];
318	if (*s != '-')
319	    break;
320	switch (ch = *++s)
321	{
322	case '\0':
323	    input_file = stdin;
324	    if (i + 1 < argc)
325		usage();
326	    return;
327
328	case '-':
329	    ++i;
330	    goto no_more_options;
331
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;
340
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;
349
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;
358
359	default:
360	    setflag(ch);
361	    break;
362	}
363
364	for (;;)
365	{
366	    switch (ch = *++s)
367	    {
368	    case '\0':
369		goto end_of_option;
370
371	    default:
372		setflag(ch);
373		break;
374	    }
375	}
376      end_of_option:;
377    }
378
379  no_more_options:;
380    if (i + 1 != argc)
381	usage();
382    input_file_name = argv[i];
383}
384
385void *
386allocate(size_t n)
387{
388    void *p;
389
390    p = NULL;
391    if (n)
392    {
393	p = CALLOC(1, n);
394	NO_SPACE(p);
395    }
396    return (p);
397}
398
399#define CREATE_FILE_NAME(dest, suffix) \
400	dest = alloc_file_name(len, suffix)
401
402static char *
403alloc_file_name(size_t len, const char *suffix)
404{
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;
411}
412
413static void
414create_file_names(void)
415{
416    size_t len;
417    const char *defines_suffix;
418    const char *externs_suffix;
419    char *prefix;
420
421    prefix = NULL;
422    defines_suffix = DEFINES_SUFFIX;
423    externs_suffix = EXTERNS_SUFFIX;
424
425    /* compute the file_prefix from the user provided output_file_name */
426    if (output_file_name != 0)
427    {
428	if (!(prefix = strstr(output_file_name, OUTPUT_SUFFIX))
429	    && (prefix = strstr(output_file_name, ".c")))
430	{
431	    defines_suffix = ".h";
432	    externs_suffix = ".i";
433	}
434    }
435
436    if (prefix != NULL)
437    {
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;
442    }
443    else
444	len = strlen(file_prefix);
445
446    /* if "-o filename" was not given */
447    if (output_file_name == 0)
448    {
449	oflag = 1;
450	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
451    }
452
453    if (rflag)
454    {
455	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
456    }
457    else
458	code_file_name = output_file_name;
459
460    if (dflag)
461    {
462	CREATE_FILE_NAME(defines_file_name, defines_suffix);
463    }
464
465    if (iflag)
466    {
467	CREATE_FILE_NAME(externs_file_name, externs_suffix);
468    }
469
470    if (vflag)
471    {
472	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
473    }
474
475    if (gflag)
476    {
477	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
478    }
479
480    if (prefix != NULL)
481    {
482	FREE(file_prefix);
483    }
484}
485
486#if USE_MKSTEMP
487static void
488close_tmpfiles(void)
489{
490    while (my_tmpfiles != 0)
491    {
492	MY_TMPFILES *next = my_tmpfiles->next;
493
494	chmod(my_tmpfiles->name, 0644);
495	unlink(my_tmpfiles->name);
496
497	free(my_tmpfiles->name);
498	free(my_tmpfiles);
499
500	my_tmpfiles = next;
501    }
502}
503
504#ifndef HAVE_MKSTEMP
505static int
506my_mkstemp(char *temp)
507{
508    int fd;
509    char *dname;
510    char *fname;
511    char *name;
512
513    /*
514     * Split-up to use tempnam, rather than tmpnam; the latter (like
515     * mkstemp) is unusable on Windows.
516     */
517    if ((fname = strrchr(temp, '/')) != 0)
518    {
519	dname = strdup(temp);
520	dname[++fname - temp] = '\0';
521    }
522    else
523    {
524	dname = 0;
525	fname = temp;
526    }
527    if ((name = tempnam(dname, fname)) != 0)
528    {
529	fd = open(name, O_CREAT | O_EXCL | O_RDWR);
530	strcpy(temp, name);
531    }
532    else
533    {
534	fd = -1;
535    }
536
537    if (dname != 0)
538	free(dname);
539
540    return fd;
541}
542#define mkstemp(s) my_mkstemp(s)
543#endif
544
545#endif
546
547/*
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.
550 */
551static FILE *
552open_tmpfile(const char *label)
553{
554    FILE *result;
555#if USE_MKSTEMP
556    int fd;
557    const char *tmpdir;
558    char *name;
559    const char *mark;
560
561    if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0)
562    {
563#ifdef P_tmpdir
564	tmpdir = P_tmpdir;
565#else
566	tmpdir = "/tmp";
567#endif
568	if (access(tmpdir, W_OK) != 0)
569	    tmpdir = ".";
570    }
571
572    name = malloc(strlen(tmpdir) + 10 + strlen(label));
573
574    result = 0;
575    if (name != 0)
576    {
577	if ((mark = strrchr(label, '_')) == 0)
578	    mark = label + strlen(label);
579
580	sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label);
581	fd = mkstemp(name);
582	if (fd >= 0)
583	{
584	    result = fdopen(fd, "w+");
585	    if (result != 0)
586	    {
587		MY_TMPFILES *item;
588
589		if (my_tmpfiles == 0)
590		{
591		    atexit(close_tmpfiles);
592		}
593
594		item = NEW(MY_TMPFILES);
595		NO_SPACE(item);
596
597		item->name = name;
598		NO_SPACE(item->name);
599
600		item->next = my_tmpfiles;
601		my_tmpfiles = item;
602	    }
603	}
604    }
605#else
606    result = tmpfile();
607#endif
608
609    if (result == 0)
610	open_error(label);
611    return result;
612}
613
614static void
615open_files(void)
616{
617    create_file_names();
618
619    if (input_file == 0)
620    {
621	input_file = fopen(input_file_name, "r");
622	if (input_file == 0)
623	    open_error(input_file_name);
624    }
625
626    action_file = open_tmpfile("action_file");
627    text_file = open_tmpfile("text_file");
628
629    if (vflag)
630    {
631	verbose_file = fopen(verbose_file_name, "w");
632	if (verbose_file == 0)
633	    open_error(verbose_file_name);
634    }
635
636    if (gflag)
637    {
638	graph_file = fopen(graph_file_name, "w");
639	if (graph_file == 0)
640	    open_error(graph_file_name);
641	fprintf(graph_file, "digraph %s {\n", file_prefix);
642	fprintf(graph_file, "\tedge [fontsize=10];\n");
643	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
644	fprintf(graph_file, "\torientation=landscape;\n");
645	fprintf(graph_file, "\trankdir=LR;\n");
646	fprintf(graph_file, "\t/*\n");
647	fprintf(graph_file, "\tmargin=0.2;\n");
648	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
649	fprintf(graph_file, "\tratio=auto;\n");
650	fprintf(graph_file, "\t*/\n");
651    }
652
653    if (dflag)
654    {
655	defines_file = fopen(defines_file_name, "w");
656	if (defines_file == 0)
657	    open_error(defines_file_name);
658	union_file = open_tmpfile("union_file");
659    }
660
661    if (iflag)
662    {
663	externs_file = fopen(externs_file_name, "w");
664	if (externs_file == 0)
665	    open_error(externs_file_name);
666    }
667
668    output_file = fopen(output_file_name, "w");
669    if (output_file == 0)
670	open_error(output_file_name);
671
672    if (rflag)
673    {
674	code_file = fopen(code_file_name, "w");
675	if (code_file == 0)
676	    open_error(code_file_name);
677    }
678    else
679	code_file = output_file;
680}
681
682int
683main(int argc, char *argv[])
684{
685    SRexpect = -1;
686    RRexpect = -1;
687    exit_code = EXIT_SUCCESS;
688
689    set_signals();
690    getargs(argc, argv);
691    open_files();
692    reader();
693    lr0();
694    lalr();
695    make_parser();
696    graph();
697    finalize_closure();
698    verbose();
699    output();
700    done(exit_code);
701    /*NOTREACHED */
702}
703