source: trunk/yacc/main.c@ 3188

Last change on this file since 3188 was 2464, checked in by bird, 20 years ago

FreeBSD CVS 2005-07-07

File size: 10.4 KB
Line 
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Paul Corbett.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if 0
38#ifndef lint
39static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
40#endif
41#endif
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: src/usr.bin/yacc/main.c,v 1.22 2004/03/05 02:05:56 wes Exp $");
45
46#include <paths.h>
47#include <signal.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51#include "defs.h"
52
53char dflag;
54char lflag;
55char rflag;
56char tflag;
57char vflag;
58
59const char *symbol_prefix;
60const char *file_prefix = "y";
61char temp_form[] = "yacc.XXXXXXXXXXX";
62
63int lineno;
64int outline;
65
66char *action_file_name;
67char *code_file_name;
68char *defines_file_name;
69const char *input_file_name = "";
70char *output_file_name;
71char *text_file_name;
72char *union_file_name;
73char *verbose_file_name;
74
75FILE *action_file; /* a temp file, used to save actions associated */
76 /* with rules until the parser is written */
77FILE *code_file; /* y.code.c (used when the -r option is specified) */
78FILE *defines_file; /* y.tab.h */
79FILE *input_file; /* the input file */
80FILE *output_file; /* y.tab.c */
81FILE *text_file; /* a temp file, used to save text until all */
82 /* symbols have been defined */
83FILE *union_file; /* a temp file, used to save the union */
84 /* definition until all symbol have been */
85 /* defined */
86FILE *verbose_file; /* y.output */
87
88int nitems;
89int nrules;
90int nsyms;
91int ntokens;
92int nvars;
93
94int start_symbol;
95char **symbol_name;
96short *symbol_value;
97short *symbol_prec;
98char *symbol_assoc;
99
100short *ritem;
101short *rlhs;
102short *rrhs;
103short *rprec;
104char *rassoc;
105short **derives;
106char *nullable;
107
108static void create_file_names(void);
109static void getargs(int, char **);
110static void onintr(int);
111static void open_files(void);
112static void set_signals(void);
113static void usage(void);
114
115volatile sig_atomic_t sigdie;
116
117__dead2 void
118done(k)
119int k;
120{
121 if (action_file) { fclose(action_file); unlink(action_file_name); }
122 if (text_file) { fclose(text_file); unlink(text_file_name); }
123 if (union_file) { fclose(union_file); unlink(union_file_name); }
124 if (sigdie) { _exit(k); }
125 exit(k);
126}
127
128
129static void
130onintr(signo)
131 int signo __unused;
132{
133 sigdie = 1;
134 done(1);
135}
136
137
138static void
139set_signals()
140{
141#ifdef SIGINT
142 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
143 signal(SIGINT, onintr);
144#endif
145#ifdef SIGTERM
146 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
147 signal(SIGTERM, onintr);
148#endif
149#ifdef SIGHUP
150 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
151 signal(SIGHUP, onintr);
152#endif
153}
154
155
156static void
157usage()
158{
159 fprintf(stderr, "%s\n%s\n",
160 "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
161 " [-p symbol_prefix] filename");
162 exit(1);
163}
164
165
166static void
167getargs(argc, argv)
168int argc;
169char *argv[];
170{
171 int ch;
172
173 while ((ch = getopt(argc, argv, "b:dlo:p:rtv")) != -1)
174 {
175 switch (ch)
176 {
177 case 'b':
178 file_prefix = optarg;
179 break;
180
181 case 'd':
182 dflag = 1;
183 break;
184
185 case 'l':
186 lflag = 1;
187 break;
188
189 case 'o':
190 output_file_name = optarg;
191 break;
192
193 case 'p':
194 symbol_prefix = optarg;