Release 6
[unix-history] / usr / src / usr.bin / yacc / main.c
CommitLineData
39ceacc0
KB
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 *
6ecf3d85 8 * %sccs.include.redist.c%
39ceacc0
KB
9 */
10
11#ifndef lint
12char copyright[] =
13"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
14 All rights reserved.\n";
15#endif /* not lint */
16
17#ifndef lint
304df2f3 18static char sccsid[] = "@(#)main.c 5.4 (Berkeley) %G%";
39ceacc0
KB
19#endif /* not lint */
20
21#include <signal.h>
22#include "defs.h"
23
24char dflag;
25char lflag;
ed9bc349 26char rflag;
39ceacc0
KB
27char tflag;
28char vflag;
29
ed9bc349 30char *file_prefix = "y";
39ceacc0
KB
31char *myname = "yacc";
32char *temp_form = "yacc.XXXXXXX";
33
34int lineno;
35int outline;
36
37char *action_file_name;
ed9bc349 38char *code_file_name;
39ceacc0
KB
39char *defines_file_name;
40char *input_file_name = "";
41char *output_file_name;
42char *text_file_name;
43char *union_file_name;
44char *verbose_file_name;
45
46FILE *action_file; /* a temp file, used to save actions associated */
47 /* with rules until the parser is written */
ed9bc349 48FILE *code_file; /* y.code.c (used when the -r option is specified) */
39ceacc0
KB
49FILE *defines_file; /* y.tab.h */
50FILE *input_file; /* the input file */
51FILE *output_file; /* y.tab.c */
52FILE *text_file; /* a temp file, used to save text until all */
53 /* symbols have been defined */
54FILE *union_file; /* a temp file, used to save the union */
55 /* definition until all symbol have been */
56 /* defined */
57FILE *verbose_file; /* y.output */
58
59int nitems;
60int nrules;
61int nsyms;
62int ntokens;
63int nvars;
64
65int start_symbol;
66char **symbol_name;
67short *symbol_value;
68short *symbol_prec;
69char *symbol_assoc;
70
71short *ritem;
72short *rlhs;
73short *rrhs;
74short *rprec;
75char *rassoc;
76short **derives;
77char *nullable;
78
79extern char *mktemp();
80extern char *getenv();
81
82
83done(k)
84int k;
85{
86 if (action_file) { fclose(action_file); unlink(action_file_name); }
87 if (text_file) { fclose(text_file); unlink(text_file_name); }
88 if (union_file) { fclose(union_file); unlink(union_file_name); }
89 exit(k);
90}
91
92
304df2f3 93void
39ceacc0
KB
94onintr()
95{
96 done(1);
97}
98
99
100set_signals()
101{
102#ifdef SIGINT
103 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
104 signal(SIGINT, onintr);
105#endif
106#ifdef SIGTERM
107 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
108 signal(SIGTERM, onintr);
109#endif
110#ifdef SIGHUP
111 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
112 signal(SIGHUP, onintr);
113#endif
114}
115
116
117usage()
118{
ed9bc349 119 fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
39ceacc0
KB
120 exit(1);
121}
122
123
124getargs(argc, argv)
125int argc;
126char *argv[];
127{
128 register int i;
129 register char *s;
130
131 if (argc > 0) myname = argv[0];
132 for (i = 1; i < argc; ++i)
133 {
134 s = argv[i];
135 if (*s != '-') break;
136 switch (*++s)
137 {
138 case '\0':
139 input_file = stdin;
140 if (i + 1 < argc) usage();
141 return;
142
ed9bc349 143 case '-':
39ceacc0
KB
144 ++i;
145 goto no_more_options;
146
147 case 'b':
ed9bc349
BC
148 if (*++s)
149 file_prefix = s;
150 else if (++i < argc)
151 file_prefix = argv[i];
152 else
153 usage();
39ceacc0
KB
154 continue;
155
156 case 'd':
157 dflag = 1;
158 break;
159
160 case 'l':
161 lflag = 1;
162 break;
163
ed9bc349
BC
164 case 'r':
165 rflag = 1;
166 break;
167
39ceacc0
KB
168 case 't':
169 tflag = 1;
170 break;
171
172 case 'v':
173 vflag = 1;
174 break;
175
176 default:
177 usage();
178 }
179
180 for (;;)
181 {
182 switch (*++s)
183 {
184 case '\0':
185 goto end_of_option;
186
187 case 'd':
188 dflag = 1;
189 break;
190
191 case 'l':
192 lflag = 1;
193 break;
194
ed9bc349
BC
195 case 'r':
196 rflag = 1;
197 break;
198
39ceacc0
KB
199 case 't':
200 tflag = 1;
201 break;
202
203 case 'v':
204 vflag = 1;
205 break;
206
207 default:
208 usage();
209 }
210 }
211end_of_option:;
212 }
213
214no_more_options:;
215 if (i + 1 != argc) usage();
216 input_file_name = argv[i];
217}
218
219
220char *
221allocate(n)
222unsigned n;
223{
224 register char *p;
225
ed9bc349
BC
226 p = NULL;
227 if (n)
228 {
229 p = CALLOC(1, n);
230 if (!p) no_space();
231 }
39ceacc0
KB
232 return (p);
233}
234
235
236create_file_names()
237{
238 int i, len;
239 char *tmpdir;
240
241 tmpdir = getenv("TMPDIR");
242 if (tmpdir == 0) tmpdir = "/tmp";
243
244 len = strlen(tmpdir);
245 i = len + 13;
246 if (len && tmpdir[len-1] != '/')
247 ++i;
248
249 action_file_name = MALLOC(i);
250 if (action_file_name == 0) no_space();
251 text_file_name = MALLOC(i);
252 if (text_file_name == 0) no_space();
253 union_file_name = MALLOC(i);
254 if (union_file_name == 0) no_space();
255
256 strcpy(action_file_name, tmpdir);
257 strcpy(text_file_name, tmpdir);
258 strcpy(union_file_name, tmpdir);
259
260 if (len && tmpdir[len - 1] != '/')
261 {
262 action_file_name[len] = '/';
263 text_file_name[len] = '/';
264 union_file_name[len] = '/';
265 ++len;
266 }
267
268 strcpy(action_file_name + len, temp_form);
269 strcpy(text_file_name + len, temp_form);
270 strcpy(union_file_name + len, temp_form);
271
272 action_file_name[len + 5] = 'a';
273 text_file_name[len + 5] = 't';
274 union_file_name[len + 5] = 'u';
275
276 mktemp(action_file_name);
277 mktemp(text_file_name);
278 mktemp(union_file_name);
279
ed9bc349
BC
280 len = strlen(file_prefix);
281
282 output_file_name = MALLOC(len + 7);
283 if (output_file_name == 0)
284 no_space();
285 strcpy(output_file_name, file_prefix);
286 strcpy(output_file_name + len, OUTPUT_SUFFIX);
287
288 if (rflag)
289 {
290 code_file_name = MALLOC(len + 8);
291 if (code_file_name == 0)
292 no_space();
293 strcpy(code_file_name, file_prefix);
294 strcpy(code_file_name + len, CODE_SUFFIX);
295 }
296 else
297 code_file_name = output_file_name;
298
39ceacc0
KB
299 if (dflag)
300 {
39ceacc0 301 defines_file_name = MALLOC(len + 7);
ed9bc349
BC
302 if (defines_file_name == 0)
303 no_space();
304 strcpy(defines_file_name, file_prefix);
39ceacc0
KB
305 strcpy(defines_file_name + len, DEFINES_SUFFIX);
306 }
307
39ceacc0
KB
308 if (vflag)
309 {
310 verbose_file_name = MALLOC(len + 8);
ed9bc349
BC
311 if (verbose_file_name == 0)
312 no_space();
313 strcpy(verbose_file_name, file_prefix);
39ceacc0
KB
314 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
315 }
316}
317
318
319open_files()
320{
321 create_file_names();
322
323 if (input_file == 0)
324 {
325 input_file = fopen(input_file_name, "r");
ed9bc349
BC
326 if (input_file == 0)
327 open_error(input_file_name);
39ceacc0
KB
328 }
329
330 action_file = fopen(action_file_name, "w");
ed9bc349
BC
331 if (action_file == 0)
332 open_error(action_file_name);
39ceacc0
KB
333
334 text_file = fopen(text_file_name, "w");
ed9bc349
BC
335 if (text_file == 0)
336 open_error(text_file_name);
39ceacc0
KB
337
338 if (vflag)
339 {
340 verbose_file = fopen(verbose_file_name, "w");
ed9bc349
BC
341 if (verbose_file == 0)
342 open_error(verbose_file_name);
39ceacc0
KB
343 }
344
345 if (dflag)
346 {
347 defines_file = fopen(defines_file_name, "w");
ed9bc349
BC
348 if (defines_file == 0)
349 open_error(defines_file_name);
39ceacc0 350 union_file = fopen(union_file_name, "w");
ed9bc349
BC
351 if (union_file == 0)
352 open_error(union_file_name);
39ceacc0
KB
353 }
354
355 output_file = fopen(output_file_name, "w");
ed9bc349
BC
356 if (output_file == 0)
357 open_error(output_file_name);
358
359 if (rflag)
360 {
361 code_file = fopen(code_file_name, "w");
362 if (code_file == 0)
363 open_error(code_file_name);
364 }
365 else
366 code_file = output_file;
39ceacc0
KB
367}
368
369
370int
371main(argc, argv)
372int argc;
373char *argv[];
374{
375 set_signals();
376 getargs(argc, argv);
377 open_files();
378 reader();
379 lr0();
380 lalr();
381 make_parser();
382 verbose();
383 output();
384 done(0);
385 /*NOTREACHED*/
386}