No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / yacc / main.c
blob2fe7491c650310cb54a4352b90bbd7c12c84a006
1 /* $NetBSD: main.c,v 1.20 2008/07/21 14:19:28 lukem Exp $ */
3 /*
4 * Copyright (c) 1989 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Paul Corbett.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
39 #include <sys/cdefs.h>
40 #if defined(__COPYRIGHT) && !defined(lint)
41 __COPYRIGHT("@(#) Copyright (c) 1989\
42 The Regents of the University of California. All rights reserved.");
43 #endif /* not lint */
45 #if defined(__RCSID) && !defined(lint)
46 #if 0
47 static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
48 #else
49 __RCSID("$NetBSD: main.c,v 1.20 2008/07/21 14:19:28 lukem Exp $");
50 #endif
51 #endif /* not lint */
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include "defs.h"
58 char dflag;
59 char lflag;
60 char rflag;
61 char tflag;
62 char vflag;
64 const char *symbol_prefix;
65 const char *myname = "yacc";
68 int lineno;
69 int outline;
71 char *action_file_name;
72 char *code_file_name;
73 char *defines_file_name;
74 const char *input_file_name = "";
75 char *output_file_name;
76 char *text_file_name;
77 char *union_file_name;
78 char *verbose_file_name;
80 FILE *action_file; /* a temp file, used to save actions associated */
81 /* with rules until the parser is written */
82 FILE *code_file; /* y.code.c (used when the -r option is specified) */
83 FILE *defines_file; /* y.tab.h */
84 FILE *input_file; /* the input file */
85 FILE *output_file; /* y.tab.c */
86 FILE *text_file; /* a temp file, used to save text until all */
87 /* symbols have been defined */
88 FILE *union_file; /* a temp file, used to save the union */
89 /* definition until all symbol have been */
90 /* defined */
91 FILE *verbose_file; /* y.output */
93 int nitems;
94 int nrules;
95 int nsyms;
96 int ntokens;
97 int nvars;
99 int start_symbol;
100 char **symbol_name;
101 short *symbol_value;
102 short *symbol_prec;
103 char *symbol_assoc;
105 short *ritem;
106 short *rlhs;
107 short *rrhs;
108 short *rprec;
109 char *rassoc;
110 short **derives;
111 char *nullable;
113 static const char *file_prefix = "y";
114 static const char *temp_form = "yacc.XXXXXXX";
115 static int explicit_file_name;
118 static __dead void onintr(int);
119 static void set_signals(void);
120 static __dead void usage(void);
121 static void getargs(int, char *[]);
122 static void create_file_names(void);
123 static void open_files(void);
125 /* coverity[+kill] */
126 __dead void
127 done(int k)
129 if (action_file) { fclose(action_file); unlink(action_file_name); }
130 if (text_file) { fclose(text_file); unlink(text_file_name); }
131 if (union_file) { fclose(union_file); unlink(union_file_name); }
132 exit(k);
136 static void
137 onintr(int signo)
139 done(1);
143 static void
144 set_signals()
146 #ifdef SIGINT
147 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
148 signal(SIGINT, onintr);
149 #endif
150 #ifdef SIGTERM
151 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
152 signal(SIGTERM, onintr);
153 #endif
154 #ifdef SIGHUP
155 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
156 signal(SIGHUP, onintr);
157 #endif
161 static void
162 usage(void)
164 fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-o outputfile] "
165 "[-p symbol_prefix] filename\n", myname);
166 exit(1);
170 static void
171 getargs(int argc, char *argv[])
173 int i;
174 char *s;
176 if (argc > 0) myname = argv[0];
177 for (i = 1; i < argc; ++i)
179 s = argv[i];
180 if (*s != '-') break;
181 switch (*++s)
183 case '\0':
184 input_file = stdin;
185 if (i + 1 < argc) usage();
186 return;
188 case '-':
189 ++i;
190 goto no_more_options;
192 case 'b':
193 if (*++s)
194 file_prefix = s;
195 else if (++i < argc)
196 file_prefix = argv[i];
197 else
198 usage();
199 continue;
201 case 'd':
202 dflag = 1;
203 break;
205 case 'l':
206 lflag = 1;
207 break;
209 case 'o':
210 if (*++s)
211 output_file_name = s;
212 else if (++i < argc)
213 output_file_name = argv[i];
214 else
215 usage();
216 explicit_file_name = 1;
217 continue;
219 case 'p':
220 if (*++s)
221 symbol_prefix = s;
222 else if (++i < argc)
223 symbol_prefix = argv[i];
224 else
225 usage();
226 continue;
228 case 'r':
229 rflag = 1;
230 break;
232 case 't':
233 tflag = 1;
234 break;
236 case 'v':
237 vflag = 1;
238 break;
240 default:
241 usage();
244 for (;;)
246 switch (*++s)
248 case '\0':
249 goto end_of_option;
251 case 'd':
252 dflag = 1;
253 break;
255 case 'l':
256 lflag = 1;
257 break;
259 case 'r':
260 rflag = 1;
261 break;
263 case 't':
264 tflag = 1;
265 break;
267 case 'v':
268 vflag = 1;
269 break;
271 default:
272 usage();
275 end_of_option:;
278 no_more_options:;
279 if (i + 1 != argc) usage();
280 input_file_name = argv[i];
284 char *
285 allocate(unsigned n)
287 char *p;
289 p = NULL;
290 if (n)
292 p = CALLOC(1, n);
293 if (!p) no_space();
295 return (p);
299 static void
300 create_file_names(void)
302 int i, len;
303 const char *tmpdir;
305 tmpdir = getenv("TMPDIR");
306 if (tmpdir == 0) tmpdir = "/tmp";
308 len = strlen(tmpdir);
309 i = len + 13;
310 if (len && tmpdir[len-1] != '/')
311 ++i;
313 action_file_name = MALLOC(i);
314 if (action_file_name == 0) no_space();
315 text_file_name = MALLOC(i);
316 if (text_file_name == 0) no_space();
317 union_file_name = MALLOC(i);
318 if (union_file_name == 0) no_space();
320 strlcpy(action_file_name, tmpdir, i);
321 strlcpy(text_file_name, tmpdir, i);
322 strlcpy(union_file_name, tmpdir, i);
324 if (len && tmpdir[len - 1] != '/')
326 action_file_name[len] = '/';
327 text_file_name[len] = '/';
328 union_file_name[len] = '/';
329 ++len;
332 strlcpy(action_file_name + len, temp_form, i - len);
333 strlcpy(text_file_name + len, temp_form, i - len);
334 strlcpy(union_file_name + len, temp_form, i - len);
336 action_file_name[len + 5] = 'a';
337 text_file_name[len + 5] = 't';
338 union_file_name[len + 5] = 'u';
340 len = strlen(file_prefix);
342 if (!output_file_name)
344 asprintf(&output_file_name, "%s%s", file_prefix, OUTPUT_SUFFIX);
345 if (output_file_name == 0)
346 no_space();
349 if (rflag)
351 asprintf(&code_file_name, "%s%s", file_prefix, CODE_SUFFIX);
352 if (code_file_name == 0)
353 no_space();
355 else
356 code_file_name = output_file_name;
358 if (dflag)
360 if (explicit_file_name)
362 char *suffix;
363 defines_file_name = strdup(output_file_name);
364 if (defines_file_name == 0)
365 no_space();
366 /* does the output_file_name have a known suffix */
367 suffix = strrchr(output_file_name, '.');
368 if (suffix != 0 &&
369 (!strcmp(suffix, ".c") || /* good, old-fashioned C */
370 !strcmp(suffix, ".C") || /* C++, or C on Windows */
371 !strcmp(suffix, ".cc") || /* C++ */
372 !strcmp(suffix, ".cxx") || /* C++ */
373 !strcmp(suffix, ".cpp"))) /* C++ (Windows) */
375 strncpy(defines_file_name, output_file_name,
376 suffix - output_file_name + 1);
377 defines_file_name[suffix - output_file_name + 1] = 'h';
378 defines_file_name[suffix - output_file_name + 2] = 0;
379 } else {
380 fprintf(stderr,"%s: suffix of output file name %s"
381 " not recognized, no -d file generated.\n",
382 myname, output_file_name);
383 dflag = 0;
384 free(defines_file_name);
385 defines_file_name = 0;
388 else
390 asprintf(&defines_file_name, "%s%s", file_prefix, DEFINES_SUFFIX);
391 if (defines_file_name == 0)
392 no_space();
396 if (vflag)
398 asprintf(&verbose_file_name, "%s%s", file_prefix, VERBOSE_SUFFIX);
399 if (verbose_file_name == 0)
400 no_space();
405 static void
406 open_files(void)
408 int fd;
410 create_file_names();
412 if (input_file == 0)
414 input_file = fopen(input_file_name, "r");
415 if (input_file == 0)
416 open_error(input_file_name);
419 if (((fd = mkstemp(action_file_name)) == -1) ||
420 (action_file = fdopen(fd, "w")) == NULL)
421 open_error(action_file_name);
423 if (((fd = mkstemp(text_file_name)) == -1) ||
424 (text_file = fdopen(fd, "w")) == NULL)
425 open_error(text_file_name);
427 if (vflag)
429 verbose_file = fopen(verbose_file_name, "w");
430 if (verbose_file == 0)
431 open_error(verbose_file_name);
434 if (dflag)
436 defines_file = fopen(defines_file_name, "w");
437 if (defines_file == 0)
438 open_error(defines_file_name);
439 if (((fd = mkstemp(union_file_name)) == -1) ||
440 (union_file = fdopen(fd, "w")) == NULL)
441 open_error(union_file_name);
444 output_file = fopen(output_file_name, "w");
445 if (output_file == 0)
446 open_error(output_file_name);
448 if (rflag)
450 code_file = fopen(code_file_name, "w");
451 if (code_file == 0)
452 open_error(code_file_name);
454 else
455 code_file = output_file;
460 main(int argc, char *argv[])
462 set_signals();
463 getargs(argc, argv);
464 open_files();
465 reader();
466 lr0();
467 lalr();
468 make_parser();
469 verbose();
470 output();
471 done(0);
472 /*NOTREACHED*/
473 return 0;