1/* $OpenBSD: main.c,v 1.34 2020/05/24 17:31:54 espie Exp $	 */
2/* $NetBSD: main.c,v 1.5 1996/03/19 03:21:38 jtc Exp $	 */
3
4/*
5 * Copyright (c) 1989 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Robert Paul Corbett.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/types.h>
37#include <fcntl.h>
38#include <paths.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include "defs.h"
42
43char dflag;
44char lflag;
45char rflag;
46char tflag;
47char vflag;
48
49char *symbol_prefix;
50char *file_prefix = "y";
51
52int lineno;
53int outline;
54
55int explicit_file_name;
56
57char *code_file_name;
58char *defines_file_name;
59char *input_file_name = "";
60char *output_file_name;
61char *verbose_file_name;
62
63FILE *action_file;	/* a temp file, used to save actions associated    */
64			/* with rules until the parser is written	   */
65FILE *code_file;	/* y.code.c (used when the -r option is specified) */
66FILE *defines_file;	/* y.tab.h					   */
67FILE *input_file;	/* the input file				   */
68FILE *output_file;	/* y.tab.c					   */
69FILE *text_file;	/* a temp file, used to save text until all	   */
70			/* symbols have been defined			   */
71FILE *union_file;	/* a temp file, used to save the union		   */
72			/* definition until all symbol have been	   */
73			/* defined					   */
74FILE *verbose_file;	/* y.output					   */
75
76int nitems;
77int nrules;
78int nsyms;
79int ntokens;
80int nvars;
81
82int start_symbol;
83char **symbol_name;
84short *symbol_value;
85short *symbol_prec;
86char *symbol_assoc;
87
88short *ritem;
89short *rlhs;
90short *rrhs;
91short *rprec;
92char *rassoc;
93short **derives;
94char *nullable;
95
96void usage(void);
97void getargs(int, char *[]);
98void create_file_names(void);
99void open_files(void);
100
101void
102usage(void)
103{
104	fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-o output_file] [-p symbol_prefix] file\n", __progname);
105	exit(1);
106}
107
108
109void
110getargs(int argc, char *argv[])
111{
112	int ch;
113
114	while ((ch = getopt(argc, argv, "b:dlo:p:rtv")) != -1) {
115		switch (ch) {
116		case 'b':
117			file_prefix = optarg;
118			break;
119
120		case 'd':
121			dflag = 1;
122			break;
123
124		case 'l':
125			lflag = 1;
126			break;
127
128		case 'o':
129			output_file_name = optarg;
130			explicit_file_name = 1;
131			break;
132
133		case 'p':
134			symbol_prefix = optarg;
135			break;
136
137		case 'r':
138			rflag = 1;
139			break;
140
141		case 't':
142			tflag = 1;
143			break;
144
145		case 'v':
146			vflag = 1;
147			break;
148
149		default:
150			usage();
151		}
152	}
153	argc -= optind;
154	argv += optind;
155
156	if (argc != 1)
157		usage();
158	if (strcmp(*argv, "-") == 0)
159		input_file = stdin;
160	else
161		input_file_name = *argv;
162}
163
164
165void *
166allocate(size_t n)
167{
168	void *v;
169
170	v = NULL;
171	if (n) {
172		v = calloc(1, n);
173		if (!v)
174			no_space();
175	}
176	return (v);
177}
178
179void
180create_file_names(void)
181{
182	if (output_file_name == NULL) {
183		if (asprintf(&output_file_name, "%s%s", file_prefix, OUTPUT_SUFFIX)
184		    == -1)
185			no_space();
186	}
187	if (rflag) {
188		if (asprintf(&code_file_name, "%s%s", file_prefix, CODE_SUFFIX) == -1)
189			no_space();
190	} else
191		code_file_name = output_file_name;
192
193	if (dflag) {
194		if (explicit_file_name) {
195			char *suffix;
196
197			defines_file_name = strdup(output_file_name);
198			if (defines_file_name == 0)
199				no_space();
200
201			/* does the output_file_name have a known suffix */
202			if ((suffix = strrchr(output_file_name, '.')) != 0 &&
203			    (!strcmp(suffix, ".c") ||	/* good, old-fashioned C */
204			     !strcmp(suffix, ".C") ||	/* C++, or C on Windows */
205			     !strcmp(suffix, ".cc") ||	/* C++ */
206			     !strcmp(suffix, ".cxx") ||	/* C++ */
207			     !strcmp(suffix, ".cpp"))) {/* C++ (Windows) */
208				strncpy(defines_file_name, output_file_name,
209					suffix - output_file_name + 1);
210				defines_file_name[suffix - output_file_name + 1] = 'h';
211				defines_file_name[suffix - output_file_name + 2] = '\0';
212			} else {
213				fprintf(stderr, "%s: suffix of output file name %s"
214				 " not recognized, no -d file generated.\n",
215					__progname, output_file_name);
216				dflag = 0;
217				free(defines_file_name);
218				defines_file_name = 0;
219			}
220		} else {
221			if (asprintf(&defines_file_name, "%s%s", file_prefix,
222				     DEFINES_SUFFIX) == -1)
223				no_space();
224		}
225	}
226	if (vflag) {
227		if (asprintf(&verbose_file_name, "%s%s", file_prefix,
228			     VERBOSE_SUFFIX) == -1)
229			no_space();
230	}
231}
232
233
234FILE *
235create_temp(void)
236{
237	FILE *f;
238
239	f = tmpfile();
240	if (f == NULL)
241		tempfile_error();
242	return f;
243}
244
245void
246open_files(void)
247{
248	create_file_names();
249
250	if (input_file == NULL) {
251		input_file = fopen(input_file_name, "r");
252		if (input_file == NULL)
253			open_error(input_file_name);
254	}
255	action_file = create_temp();
256
257	text_file = create_temp();
258
259	if (vflag) {
260		verbose_file = fopen(verbose_file_name, "w");
261		if (verbose_file == NULL)
262			open_error(verbose_file_name);
263	}
264	if (dflag) {
265		defines_file = fopen(defines_file_name, "w");
266		if (defines_file == NULL)
267			open_write_error(defines_file_name);
268		union_file = create_temp();
269	}
270	output_file = fopen(output_file_name, "w");
271	if (output_file == NULL)
272		open_error(output_file_name);
273
274	if (rflag) {
275		code_file = fopen(code_file_name, "w");
276		if (code_file == NULL)
277			open_error(code_file_name);
278	} else
279		code_file = output_file;
280}
281
282
283int
284main(int argc, char *argv[])
285{
286	if (pledge("stdio rpath wpath cpath", NULL) == -1)
287		fatal("pledge: invalid arguments");
288
289	getargs(argc, argv);
290	open_files();
291	reader();
292	lr0();
293	lalr();
294	make_parser();
295	verbose();
296	output();
297	return (0);
298}
299