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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
55 const char *symbol_prefix;
56 const char *file_prefix = "y";
57 char temp_form[] = "yacc.XXXXXXXXXXX";
62 char *action_file_name;
64 char *defines_file_name;
65 const char *input_file_name = "";
66 char *output_file_name;
68 char *union_file_name;
69 char *verbose_file_name;
71 FILE *action_file; /* a temp file, used to save actions associated */
72 /* with rules until the parser is written */
73 FILE *code_file; /* y.code.c (used when the -r option is specified) */
74 FILE *defines_file; /* y.tab.h */
75 FILE *input_file; /* the input file */
76 FILE *output_file; /* y.tab.c */
77 FILE *text_file; /* a temp file, used to save text until all */
78 /* symbols have been defined */
79 FILE *union_file; /* a temp file, used to save the union */
80 /* definition until all symbol have been */
82 FILE *verbose_file; /* y.output */
104 static void create_file_names(void);
105 static void getargs(int, char **);
106 static void onintr(int);
107 static void open_files(void);
108 static void set_signals(void);
109 static void usage(void);
111 volatile sig_atomic_t sigdie;
116 if (action_file) { fclose(action_file); unlink(action_file_name); }
117 if (text_file) { fclose(text_file); unlink(text_file_name); }
118 if (union_file) { fclose(union_file); unlink(union_file_name); }
119 if (sigdie) { _exit(k); }
125 onintr(int signo __unused)
136 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
137 signal(SIGINT, onintr);
140 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
141 signal(SIGTERM, onintr);
144 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
145 signal(SIGHUP, onintr);
153 fprintf(stderr, "%s\n%s\n",
154 "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
155 " [-p symbol_prefix] filename");
161 getargs(int argc, char *argv[])
165 while ((ch = getopt(argc, argv, "b:dlo:p:rtvy")) != -1)
170 file_prefix = optarg;
182 output_file_name = optarg;
186 symbol_prefix = optarg;
202 /* for bison compatibility -- byacc is already POSIX compatible */
210 if (optind + 1 != argc)
212 if (strcmp(argv[optind], "-") == 0)
215 input_file_name = argv[optind];
235 create_file_names(void)
240 if (!(tmpdir = getenv("TMPDIR")))
243 len = strlen(tmpdir);
244 i = len + strlen(temp_form) + 1;
245 if (len && tmpdir[len-1] != '/')
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();
255 strcpy(action_file_name, tmpdir);
256 strcpy(text_file_name, tmpdir);
257 strcpy(union_file_name, tmpdir);
259 if (len && tmpdir[len - 1] != '/')
261 action_file_name[len] = '/';
262 text_file_name[len] = '/';
263 union_file_name[len] = '/';
267 strcpy(action_file_name + len, temp_form);
268 strcpy(text_file_name + len, temp_form);
269 strcpy(union_file_name + len, temp_form);
271 action_file_name[len + 5] = 'a';
272 text_file_name[len + 5] = 't';
273 union_file_name[len + 5] = 'u';
275 if (output_file_name != 0)
277 file_prefix = output_file_name;
278 len = strlen(file_prefix);
282 len = strlen(file_prefix);
283 output_file_name = malloc(len + 7);
284 if (output_file_name == 0)
286 strcpy(output_file_name, file_prefix);
287 strcpy(output_file_name + len, OUTPUT_SUFFIX);
292 code_file_name = malloc(len + 8);
293 if (code_file_name == 0)
295 strcpy(code_file_name, file_prefix);
296 if (file_prefix == output_file_name)
299 * XXX ".tab.c" here is OUTPUT_SUFFIX, but since its length is
300 * in various magic numbers, don't bother using the macro.
302 if (len >= 6 && strcmp(code_file_name + len - 6, ".tab.c") == 0)
303 strcpy(code_file_name + len - 6, CODE_SUFFIX);
304 else if (len >= 2 && strcmp(code_file_name + len - 2, ".c") == 0)
305 strcpy(code_file_name + len - 2, CODE_SUFFIX);
307 strcpy(code_file_name + len, CODE_SUFFIX);
310 strcpy(code_file_name + len, CODE_SUFFIX);
313 code_file_name = output_file_name;
317 defines_file_name = malloc(len + 7);
318 if (defines_file_name == 0)
320 strcpy(defines_file_name, file_prefix);
321 if (file_prefix == output_file_name)
323 #define BISON_DEFINES_SUFFIX ".h"
324 if (len >= 2 && strcmp(defines_file_name + len - 2, ".c") == 0)
325 strcpy(defines_file_name + len - 2, BISON_DEFINES_SUFFIX);
327 strcpy(defines_file_name + len, BISON_DEFINES_SUFFIX);
330 strcpy(defines_file_name + len, DEFINES_SUFFIX);
335 verbose_file_name = malloc(len + 8);
336 if (verbose_file_name == 0)
338 strcpy(verbose_file_name, file_prefix);
339 if (file_prefix == output_file_name)
341 if (len >= 6 && strcmp(verbose_file_name + len - 6, ".tab.c") == 0)
342 strcpy(verbose_file_name + len - 6, VERBOSE_SUFFIX);
343 else if (len >= 2 && strcmp(verbose_file_name + len - 2, ".c") == 0)
344 strcpy(verbose_file_name + len - 2, VERBOSE_SUFFIX);
346 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
349 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
363 input_file = fopen(input_file_name, "r");
365 open_error(input_file_name);
368 fd = mkstemp(action_file_name);
369 if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) {
372 open_error(action_file_name);
374 fd = mkstemp(text_file_name);
375 if (fd < 0 || (text_file = fdopen(fd, "w")) == NULL) {
378 open_error(text_file_name);
380 fd = mkstemp(union_file_name);
381 if (fd < 0 || (union_file = fdopen(fd, "w")) == NULL) {
384 open_error(union_file_name);
387 text_file = fopen(text_file_name, "w");
389 open_error(text_file_name);
393 verbose_file = fopen(verbose_file_name, "w");
394 if (verbose_file == 0)
395 open_error(verbose_file_name);
400 defines_file = fopen(defines_file_name, "w");
401 if (defines_file == 0)
402 open_error(defines_file_name);
403 union_file = fopen(union_file_name, "w");
405 open_error(union_file_name);
408 output_file = fopen(output_file_name, "w");
409 if (output_file == 0)
410 open_error(output_file_name);
414 code_file = fopen(code_file_name, "w");
416 open_error(code_file_name);
419 code_file = output_file;
424 main(int argc, char *argv[])