| 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 | *
|
|---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 9 | * modification, are permitted provided that the following conditions
|
|---|
| 10 | * are met:
|
|---|
| 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.
|
|---|
| 23 | *
|
|---|
| 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
|
|---|
| 34 | * SUCH DAMAGE.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #if 0
|
|---|
| 38 | #ifndef lint
|
|---|
| 39 | static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
|
|---|
| 40 | #endif
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | #include <sys/cdefs.h>
|
|---|
| 44 | __FBSDID("$FreeBSD: src/usr.bin/yacc/main.c,v 1.22 2004/03/05 02:05:56 wes Exp $");
|
|---|
| 45 |
|
|---|
| 46 | #include <paths.h>
|
|---|
| 47 | #include <signal.h>
|
|---|
| 48 | #include <stdlib.h>
|
|---|
| 49 | #include <string.h>
|
|---|
| 50 | #include <unistd.h>
|
|---|
| 51 | #include "defs.h"
|
|---|
| 52 |
|
|---|
| 53 | char dflag;
|
|---|
| 54 | char lflag;
|
|---|
| 55 | char rflag;
|
|---|
| 56 | char tflag;
|
|---|
| 57 | char vflag;
|
|---|
| 58 |
|
|---|
| 59 | const char *symbol_prefix;
|
|---|
| 60 | const char *file_prefix = "y";
|
|---|
| 61 | char temp_form[] = "yacc.XXXXXXXXXXX";
|
|---|
| 62 |
|
|---|
| 63 | int lineno;
|
|---|
| 64 | int outline;
|
|---|
| 65 |
|
|---|
| 66 | char *action_file_name;
|
|---|
| 67 | char *code_file_name;
|
|---|
| 68 | char *defines_file_name;
|
|---|
| 69 | const char *input_file_name = "";
|
|---|
| 70 | char *output_file_name;
|
|---|
| 71 | char *text_file_name;
|
|---|
| 72 | char *union_file_name;
|
|---|
| 73 | char *verbose_file_name;
|
|---|
| 74 |
|
|---|
| 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 */
|
|---|
| 85 | /* defined */
|
|---|
| 86 | FILE *verbose_file; /* y.output */
|
|---|
| 87 |
|
|---|
| 88 | int nitems;
|
|---|
| 89 | int nrules;
|
|---|
| 90 | int nsyms;
|
|---|
| 91 | int ntokens;
|
|---|
| 92 | int nvars;
|
|---|
| 93 |
|
|---|
| 94 | int start_symbol;
|
|---|
| 95 | char **symbol_name;
|
|---|
| 96 | short *symbol_value;
|
|---|
| 97 | short *symbol_prec;
|
|---|
| 98 | char *symbol_assoc;
|
|---|
| 99 |
|
|---|
| 100 | short *ritem;
|
|---|
| 101 | short *rlhs;
|
|---|
| 102 | short *rrhs;
|
|---|
| 103 | short *rprec;
|
|---|
| 104 | char *rassoc;
|
|---|
| 105 | short **derives;
|
|---|
| 106 | char *nullable;
|
|---|
| 107 |
|
|---|
| 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);
|
|---|
| 114 |
|
|---|
| 115 | volatile sig_atomic_t sigdie;
|
|---|
| 116 |
|
|---|
| 117 | __dead2 void
|
|---|
| 118 | done(k)
|
|---|
| 119 | int k;
|
|---|
| 120 | {
|
|---|
| 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); }
|
|---|
| 125 | exit(k);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | static void
|
|---|
| 130 | onintr(signo)
|
|---|
| 131 | int signo __unused;
|
|---|
| 132 | {
|
|---|
| 133 | sigdie = 1;
|
|---|
| 134 | done(1);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | static void
|
|---|
| 139 | set_signals()
|
|---|
| 140 | {
|
|---|
| 141 | #ifdef SIGINT
|
|---|
| 142 | if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|---|
| 143 | signal(SIGINT, onintr);
|
|---|
| 144 | #endif
|
|---|
| 145 | #ifdef SIGTERM
|
|---|
| 146 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
|
|---|
| 147 | signal(SIGTERM, onintr);
|
|---|
| 148 | #endif
|
|---|
| 149 | #ifdef SIGHUP
|
|---|
| 150 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
|
|---|
| 151 | signal(SIGHUP, onintr);
|
|---|
| 152 | #endif
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | static void
|
|---|
| 157 | usage()
|
|---|
| 158 | {
|
|---|
| 159 | fprintf(stderr, "%s\n%s\n",
|
|---|
| 160 | "usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
|
|---|
| 161 | " [-p symbol_prefix] filename");
|
|---|
| 162 | exit(1);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | static void
|
|---|
| 167 | getargs(argc, argv)
|
|---|
| 168 | int argc;
|
|---|
| 169 | char *argv[];
|
|---|
| 170 | {
|
|---|
| 171 | int ch;
|
|---|
| 172 |
|
|---|
| 173 | while ((ch = getopt(argc, argv, "b:dlo:p:rtv")) != -1)
|
|---|
| 174 | {
|
|---|
| 175 | switch (ch)
|
|---|
| 176 | {
|
|---|
| 177 | case 'b':
|
|---|
| 178 | file_prefix = optarg;
|
|---|
| 179 | break;
|
|---|
| 180 |
|
|---|
| 181 | case 'd':
|
|---|
| 182 | dflag = 1;
|
|---|
| 183 | break;
|
|---|
| 184 |
|
|---|
| 185 | case 'l':
|
|---|
| 186 | lflag = 1;
|
|---|
| 187 | break;
|
|---|
| 188 |
|
|---|
| 189 | case 'o':
|
|---|
| 190 | output_file_name = optarg;
|
|---|
| 191 | break;
|
|---|
| 192 |
|
|---|
| 193 | case 'p':
|
|---|
| 194 | symbol_prefix = optarg;
|
|---|
|
|---|