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
38 static char const copyright[] =
39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40 All rights reserved.\n";
45 static char const sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
47 static const char rcsid[] =
48 "$FreeBSD: src/usr.bin/yacc/main.c,v 1.12 2000/01/10 20:26:24 kris Exp $";
64 char *file_prefix = "y";
65 char *temp_form = "yacc.XXXXXXXXXXX";
70 char *action_file_name;
72 char *defines_file_name;
73 char *input_file_name = "";
74 char *output_file_name;
76 char *union_file_name;
77 char *verbose_file_name;
79 FILE *action_file; /* a temp file, used to save actions associated */
80 /* with rules until the parser is written */
81 FILE *code_file; /* y.code.c (used when the -r option is specified) */
82 FILE *defines_file; /* y.tab.h */
83 FILE *input_file; /* the input file */
84 FILE *output_file; /* y.tab.c */
85 FILE *text_file; /* a temp file, used to save text until all */
86 /* symbols have been defined */
87 FILE *union_file; /* a temp file, used to save the union */
88 /* definition until all symbol have been */
90 FILE *verbose_file; /* y.output */
112 static void create_file_names __P((void));
113 static void getargs __P((int, char **));
114 static void onintr __P((int));
115 static void open_files __P((void));
116 static void set_signals __P((void));
117 static void usage __P((void));
124 if (action_file) { fclose(action_file); unlink(action_file_name); }
125 if (text_file) { fclose(text_file); unlink(text_file_name); }
126 if (union_file) { fclose(union_file); unlink(union_file_name); }
143 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
144 signal(SIGINT, onintr);
147 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
148 signal(SIGTERM, onintr);
151 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
152 signal(SIGHUP, onintr);
160 fprintf(stderr, "%s\n%s\n",
161 "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
162 " [-p symbol_prefix] filename");
175 for (i = 1; i < argc; ++i)
178 if (*s != '-') break;
183 if (i + 1 < argc) usage();
188 goto no_more_options;
194 file_prefix = argv[i];
209 output_file_name = s;
211 output_file_name = argv[i];
220 symbol_prefix = argv[i];
276 if (i + 1 != argc) usage();
277 input_file_name = argv[i];
303 tmpdir = getenv("TMPDIR");
304 if (tmpdir == 0) tmpdir = "/tmp";
306 len = strlen(tmpdir);
308 if (len && tmpdir[len-1] != '/')
311 action_file_name = MALLOC(i);
312 if (action_file_name == 0) no_space();
313 text_file_name = MALLOC(i);
314 if (text_file_name == 0) no_space();
315 union_file_name = MALLOC(i);
316 if (union_file_name == 0) no_space();
318 strcpy(action_file_name, tmpdir);
319 strcpy(text_file_name, tmpdir);
320 strcpy(union_file_name, tmpdir);
322 if (len && tmpdir[len - 1] != '/')
324 action_file_name[len] = '/';
325 text_file_name[len] = '/';
326 union_file_name[len] = '/';
330 strcpy(action_file_name + len, temp_form);
331 strcpy(text_file_name + len, temp_form);
332 strcpy(union_file_name + len, temp_form);
334 action_file_name[len + 5] = 'a';
335 text_file_name[len + 5] = 't';
336 union_file_name[len + 5] = 'u';
338 if (output_file_name != 0)
340 file_prefix = output_file_name;
341 len = strlen(file_prefix);
345 len = strlen(file_prefix);
346 output_file_name = MALLOC(len + 7);
347 if (output_file_name == 0)
349 strcpy(output_file_name, file_prefix);
350 strcpy(output_file_name + len, OUTPUT_SUFFIX);
355 code_file_name = MALLOC(len + 8);
356 if (code_file_name == 0)
358 strcpy(code_file_name, file_prefix);
359 if (file_prefix == output_file_name)
362 * XXX ".tab.c" here is OUTPUT_SUFFIX, but since its length is
363 * in various magic numbers, don't bother using the macro.
365 if (len >= 6 && strcmp(code_file_name + len - 6, ".tab.c") == 0)
366 strcpy(code_file_name + len - 6, CODE_SUFFIX);
367 else if (len >= 2 && strcmp(code_file_name + len - 2, ".c") == 0)
368 strcpy(code_file_name + len - 2, CODE_SUFFIX);
370 strcpy(code_file_name + len, CODE_SUFFIX);
373 strcpy(code_file_name + len, CODE_SUFFIX);
376 code_file_name = output_file_name;
380 defines_file_name = MALLOC(len + 7);
381 if (defines_file_name == 0)
383 strcpy(defines_file_name, file_prefix);
384 if (file_prefix == output_file_name)
386 #define BISON_DEFINES_SUFFIX ".h"
387 if (len >= 2 && strcmp(defines_file_name + len - 2, ".c") == 0)
388 strcpy(defines_file_name + len - 2, BISON_DEFINES_SUFFIX);
390 strcpy(defines_file_name + len, BISON_DEFINES_SUFFIX);
393 strcpy(defines_file_name + len, DEFINES_SUFFIX);
398 verbose_file_name = MALLOC(len + 8);
399 if (verbose_file_name == 0)
401 strcpy(verbose_file_name, file_prefix);
402 if (file_prefix == output_file_name)
404 if (len >= 6 && strcmp(verbose_file_name + len - 6, ".tab.c") == 0)
405 strcpy(verbose_file_name + len - 6, VERBOSE_SUFFIX);
406 else if (len >= 2 && strcmp(verbose_file_name + len - 2, ".c") == 0)
407 strcpy(verbose_file_name + len - 2, VERBOSE_SUFFIX);
409 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
412 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
426 input_file = fopen(input_file_name, "r");
428 open_error(input_file_name);
431 fd = mkstemp(action_file_name);
432 if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) {
435 open_error(action_file_name);
437 fd = mkstemp(text_file_name);
438 if (fd < 0 || (text_file = fdopen(fd, "w")) == NULL) {
441 open_error(text_file_name);
443 fd = mkstemp(union_file_name);
444 if (fd < 0 || (union_file = fdopen(fd, "w")) == NULL) {
447 open_error(union_file_name);
450 text_file = fopen(text_file_name, "w");
452 open_error(text_file_name);
456 verbose_file = fopen(verbose_file_name, "w");
457 if (verbose_file == 0)
458 open_error(verbose_file_name);
463 defines_file = fopen(defines_file_name, "w");
464 if (defines_file == 0)
465 open_error(defines_file_name);
466 union_file = fopen(union_file_name, "w");
468 open_error(union_file_name);
471 output_file = fopen(output_file_name, "w");
472 if (output_file == 0)
473 open_error(output_file_name);
477 code_file = fopen(code_file_name, "w");
479 open_error(code_file_name);
482 code_file = output_file;