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
36 * @(#) Copyright (c) 1989 The Regents of the University of California. All rights reserved.
37 * @(#)main.c 5.5 (Berkeley) 5/24/93
38 * $FreeBSD: src/usr.bin/yacc/main.c,v 1.12 2000/01/10 20:26:24 kris Exp $
53 const char *symbol_prefix;
54 const char *file_prefix = "y";
55 const char *temp_form = "yacc.XXXXXXXXXXX";
60 char *action_file_name;
62 char *defines_file_name;
63 const char *input_file_name = "";
64 char *output_file_name;
66 char *union_file_name;
67 char *verbose_file_name;
69 FILE *action_file; /* a temp file, used to save actions associated */
70 /* with rules until the parser is written */
71 FILE *code_file; /* y.code.c (used when the -r option is specified) */
72 FILE *defines_file; /* y.tab.h */
73 FILE *input_file; /* the input file */
74 FILE *output_file; /* y.tab.c */
75 FILE *text_file; /* a temp file, used to save text until all */
76 /* symbols have been defined */
77 FILE *union_file; /* a temp file, used to save the union */
78 /* definition until all symbol have been */
80 FILE *verbose_file; /* y.output */
102 static void cleanup(void);
103 static void create_file_names(void);
104 static void getargs(int, char **);
105 static void onintr(int);
106 static void open_files(void);
107 static void set_signals(void);
108 static void usage(void);
114 if (action_file) { fclose(action_file); unlink(action_file_name); }
115 if (text_file) { fclose(text_file); unlink(text_file_name); }
116 if (union_file) { fclose(union_file); unlink(union_file_name); }
121 onintr(int signo __unused)
124 unlink(action_file_name);
126 unlink(text_file_name);
128 unlink(union_file_name);
137 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
138 signal(SIGINT, onintr);
141 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
142 signal(SIGTERM, onintr);
145 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
146 signal(SIGHUP, onintr);
154 fprintf(stderr, "%s\n%s\n",
155 "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
156 " [-p symbol_prefix] filename");
162 getargs(int argc, char **argv)
167 for (i = 1; i < argc; ++i)
170 if (*s != '-') break;
175 if (i + 1 < argc) usage();
180 goto no_more_options;
186 file_prefix = argv[i];
201 output_file_name = s;
203 output_file_name = argv[i];
212 symbol_prefix = argv[i];
268 if (i + 1 != argc) usage();
269 input_file_name = argv[i];
289 create_file_names(void)
294 tmpdir = getenv("TMPDIR");
295 if (tmpdir == NULL) tmpdir = "/tmp";
297 len = strlen(tmpdir);
299 if (len && tmpdir[len-1] != '/')
302 action_file_name = MALLOC(i);
303 if (action_file_name == NULL) no_space();
304 text_file_name = MALLOC(i);
305 if (text_file_name == NULL) no_space();
306 union_file_name = MALLOC(i);
307 if (union_file_name == NULL) no_space();
309 strcpy(action_file_name, tmpdir);
310 strcpy(text_file_name, tmpdir);
311 strcpy(union_file_name, tmpdir);
313 if (len && tmpdir[len - 1] != '/')
315 action_file_name[len] = '/';
316 text_file_name[len] = '/';
317 union_file_name[len] = '/';
321 strcpy(action_file_name + len, temp_form);
322 strcpy(text_file_name + len, temp_form);
323 strcpy(union_file_name + len, temp_form);
325 action_file_name[len + 5] = 'a';
326 text_file_name[len + 5] = 't';
327 union_file_name[len + 5] = 'u';
329 if (output_file_name != NULL)
331 file_prefix = output_file_name;
332 len = strlen(file_prefix);
336 len = strlen(file_prefix);
337 output_file_name = MALLOC(len + 7);
338 if (output_file_name == NULL)
340 strcpy(output_file_name, file_prefix);
341 strcpy(output_file_name + len, OUTPUT_SUFFIX);
346 code_file_name = MALLOC(len + 8);
347 if (code_file_name == NULL)
349 strcpy(code_file_name, file_prefix);
350 if (file_prefix == output_file_name)
353 * XXX ".tab.c" here is OUTPUT_SUFFIX, but since its length is
354 * in various magic numbers, don't bother using the macro.
356 if (len >= 6 && strcmp(code_file_name + len - 6, ".tab.c") == 0)
357 strcpy(code_file_name + len - 6, CODE_SUFFIX);
358 else if (len >= 2 && strcmp(code_file_name + len - 2, ".c") == 0)
359 strcpy(code_file_name + len - 2, CODE_SUFFIX);
361 strcpy(code_file_name + len, CODE_SUFFIX);
364 strcpy(code_file_name + len, CODE_SUFFIX);
367 code_file_name = output_file_name;
371 defines_file_name = MALLOC(len + 7);
372 if (defines_file_name == NULL)
374 strcpy(defines_file_name, file_prefix);
375 if (file_prefix == output_file_name)
377 #define BISON_DEFINES_SUFFIX ".h"
378 if (len >= 2 && strcmp(defines_file_name + len - 2, ".c") == 0)
379 strcpy(defines_file_name + len - 2, BISON_DEFINES_SUFFIX);
381 strcpy(defines_file_name + len, BISON_DEFINES_SUFFIX);
384 strcpy(defines_file_name + len, DEFINES_SUFFIX);
389 verbose_file_name = MALLOC(len + 8);
390 if (verbose_file_name == NULL)
392 strcpy(verbose_file_name, file_prefix);
393 if (file_prefix == output_file_name)
395 if (len >= 6 && strcmp(verbose_file_name + len - 6, ".tab.c") == 0)
396 strcpy(verbose_file_name + len - 6, VERBOSE_SUFFIX);
397 else if (len >= 2 && strcmp(verbose_file_name + len - 2, ".c") == 0)
398 strcpy(verbose_file_name + len - 2, VERBOSE_SUFFIX);
400 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
403 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
415 if (input_file == NULL)
417 input_file = fopen(input_file_name, "r");
418 if (input_file == NULL)
419 open_error(input_file_name);
422 fd = mkstemp(action_file_name);
423 if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) {
426 open_error(action_file_name);
428 fd = mkstemp(text_file_name);
429 if (fd < 0 || (text_file = fdopen(fd, "w")) == NULL) {
432 open_error(text_file_name);
434 fd = mkstemp(union_file_name);
435 if (fd < 0 || (union_file = fdopen(fd, "w")) == NULL) {
438 open_error(union_file_name);
441 text_file = fopen(text_file_name, "w");
442 if (text_file == NULL)
443 open_error(text_file_name);
447 verbose_file = fopen(verbose_file_name, "w");
448 if (verbose_file == NULL)
449 open_error(verbose_file_name);
454 defines_file = fopen(defines_file_name, "w");
455 if (defines_file == NULL)
456 open_error(defines_file_name);
457 union_file = fopen(union_file_name, "w");
458 if (union_file == NULL)
459 open_error(union_file_name);
462 output_file = fopen(output_file_name, "w");
463 if (output_file == NULL)
464 open_error(output_file_name);
468 code_file = fopen(code_file_name, "w");
469 if (code_file == NULL)
470 open_error(code_file_name);
473 code_file = output_file;
478 main(int argc, char **argv)