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