2 * Copyright (c) 1989 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
39 static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
59 const char *symbol_prefix;
60 const char *file_prefix = "y";
61 char temp_form[] = "yacc.XXXXXXXXXXX";
66 char *action_file_name;
68 char *defines_file_name;
69 const char *input_file_name = "";
70 char *output_file_name;
72 char *union_file_name;
73 char *verbose_file_name;
75 FILE *action_file; /* a temp file, used to save actions associated */
76 /* with rules until the parser is written */
77 FILE *code_file; /* y.code.c (used when the -r option is specified) */
78 FILE *defines_file; /* y.tab.h */
79 FILE *input_file; /* the input file */
80 FILE *output_file; /* y.tab.c */
81 FILE *text_file; /* a temp file, used to save text until all */
82 /* symbols have been defined */
83 FILE *union_file; /* a temp file, used to save the union */
84 /* definition until all symbol have been */
86 FILE *verbose_file; /* y.output */
108 static void create_file_names(void);
109 static void getargs(int, char **);
110 static void onintr(int);
111 static void open_files(void);
112 static void set_signals(void);
113 static void usage(void);
115 volatile sig_atomic_t sigdie;
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); }
142 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
143 signal(SIGINT, onintr);
146 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
147 signal(SIGTERM, onintr);
150 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
151 signal(SIGHUP, onintr);
159 fprintf(stderr, "%s\n%s\n",
160 "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
161 " [-p symbol_prefix] filename");
173 while ((ch = getopt(argc, argv, "b:dlo:p:rtv")) != -1)
178 file_prefix = optarg;
190 output_file_name = optarg;
194 symbol_prefix = optarg;
214 if (optind + 1 != argc)
216 if (strcmp(argv[optind], "-") == 0)
219 input_file_name = argv[optind];
245 if (!(tmpdir = getenv("TMPDIR")))
248 len = strlen(tmpdir);
249 i = len + strlen(temp_form) + 1;
250 if (len && tmpdir[len-1] != '/')
253 action_file_name = MALLOC(i);
254 if (action_file_name == 0) no_space();
255 text_file_name = MALLOC(i);
256 if (text_file_name == 0) no_space();
257 union_file_name = MALLOC(i);
258 if (union_file_name == 0) no_space();
260 strcpy(action_file_name, tmpdir);
261 strcpy(text_file_name, tmpdir);
262 strcpy(union_file_name, tmpdir);
264 if (len && tmpdir[len - 1] != '/')
266 action_file_name[len] = '/';
267 text_file_name[len] = '/';
268 union_file_name[len] = '/';
272 strcpy(action_file_name + len, temp_form);
273 strcpy(text_file_name + len, temp_form);
274 strcpy(union_file_name + len, temp_form);
276 action_file_name[len + 5] = 'a';
277 text_file_name[len + 5] = 't';
278 union_file_name[len + 5] = 'u';
280 if (output_file_name != 0)
282 file_prefix = output_file_name;
283 len = strlen(file_prefix);
287 len = strlen(file_prefix);
288 output_file_name = MALLOC(len + 7);
289 if (output_file_name == 0)
291 strcpy(output_file_name, file_prefix);
292 strcpy(output_file_name + len, OUTPUT_SUFFIX);
297 code_file_name = MALLOC(len + 8);
298 if (code_file_name == 0)
300 strcpy(code_file_name, file_prefix);
301 if (file_prefix == output_file_name)
304 * XXX ".tab.c" here is OUTPUT_SUFFIX, but since its length is
305 * in various magic numbers, don't bother using the macro.
307 if (len >= 6 && strcmp(code_file_name + len - 6, ".tab.c") == 0)
308 strcpy(code_file_name + len - 6, CODE_SUFFIX);
309 else if (len >= 2 && strcmp(code_file_name + len - 2, ".c") == 0)
310 strcpy(code_file_name + len - 2, CODE_SUFFIX);
312 strcpy(code_file_name + len, CODE_SUFFIX);
315 strcpy(code_file_name + len, CODE_SUFFIX);
318 code_file_name = output_file_name;
322 defines_file_name = MALLOC(len + 7);
323 if (defines_file_name == 0)
325 strcpy(defines_file_name, file_prefix);
326 if (file_prefix == output_file_name)
328 #define BISON_DEFINES_SUFFIX ".h"
329 if (len >= 2 && strcmp(defines_file_name + len - 2, ".c") == 0)
330 strcpy(defines_file_name + len - 2, BISON_DEFINES_SUFFIX);
332 strcpy(defines_file_name + len, BISON_DEFINES_SUFFIX);
335 strcpy(defines_file_name + len, DEFINES_SUFFIX);
340 verbose_file_name = MALLOC(len + 8);
341 if (verbose_file_name == 0)
343 strcpy(verbose_file_name, file_prefix);
344 if (file_prefix == output_file_name)
346 if (len >= 6 && strcmp(verbose_file_name + len - 6, ".tab.c") == 0)
347 strcpy(verbose_file_name + len - 6, VERBOSE_SUFFIX);
348 else if (len >= 2 && strcmp(verbose_file_name + len - 2, ".c") == 0)
349 strcpy(verbose_file_name + len - 2, VERBOSE_SUFFIX);
351 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
354 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
368 input_file = fopen(input_file_name, "r");
370 open_error(input_file_name);
373 fd = mkstemp(action_file_name);
374 if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) {
377 open_error(action_file_name);
379 fd = mkstemp(text_file_name);
380 if (fd < 0 || (text_file = fdopen(fd, "w")) == NULL) {
383 open_error(text_file_name);
385 fd = mkstemp(union_file_name);
386 if (fd < 0 || (union_file = fdopen(fd, "w")) == NULL) {
389 open_error(union_file_name);
392 text_file = fopen(text_file_name, "w");
394 open_error(text_file_name);
398 verbose_file = fopen(verbose_file_name, "w");
399 if (verbose_file == 0)
400 open_error(verbose_file_name);
405 defines_file = fopen(defines_file_name, "w");
406 if (defines_file == 0)
407 open_error(defines_file_name);
408 union_file = fopen(union_file_name, "w");
410 open_error(union_file_name);
413 output_file = fopen(output_file_name, "w");
414 if (output_file == 0)
415 open_error(output_file_name);
419 code_file = fopen(code_file_name, "w");
421 open_error(code_file_name);
424 code_file = output_file;
429 main(int argc, char *argv[])