xref: /original-bsd/usr.bin/yacc/main.c (revision e59fb703)
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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 char copyright[] =
13 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
14  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)main.c	5.4 (Berkeley) 02/26/91";
19 #endif /* not lint */
20 
21 #include <signal.h>
22 #include "defs.h"
23 
24 char dflag;
25 char lflag;
26 char rflag;
27 char tflag;
28 char vflag;
29 
30 char *file_prefix = "y";
31 char *myname = "yacc";
32 char *temp_form = "yacc.XXXXXXX";
33 
34 int lineno;
35 int outline;
36 
37 char *action_file_name;
38 char *code_file_name;
39 char *defines_file_name;
40 char *input_file_name = "";
41 char *output_file_name;
42 char *text_file_name;
43 char *union_file_name;
44 char *verbose_file_name;
45 
46 FILE *action_file;	/*  a temp file, used to save actions associated    */
47 			/*  with rules until the parser is written	    */
48 FILE *code_file;	/*  y.code.c (used when the -r option is specified) */
49 FILE *defines_file;	/*  y.tab.h					    */
50 FILE *input_file;	/*  the input file				    */
51 FILE *output_file;	/*  y.tab.c					    */
52 FILE *text_file;	/*  a temp file, used to save text until all	    */
53 			/*  symbols have been defined			    */
54 FILE *union_file;	/*  a temp file, used to save the union		    */
55 			/*  definition until all symbol have been	    */
56 			/*  defined					    */
57 FILE *verbose_file;	/*  y.output					    */
58 
59 int nitems;
60 int nrules;
61 int nsyms;
62 int ntokens;
63 int nvars;
64 
65 int   start_symbol;
66 char  **symbol_name;
67 short *symbol_value;
68 short *symbol_prec;
69 char  *symbol_assoc;
70 
71 short *ritem;
72 short *rlhs;
73 short *rrhs;
74 short *rprec;
75 char  *rassoc;
76 short **derives;
77 char *nullable;
78 
79 extern char *mktemp();
80 extern char *getenv();
81 
82 
83 done(k)
84 int k;
85 {
86     if (action_file) { fclose(action_file); unlink(action_file_name); }
87     if (text_file) { fclose(text_file); unlink(text_file_name); }
88     if (union_file) { fclose(union_file); unlink(union_file_name); }
89     exit(k);
90 }
91 
92 
93 void
94 onintr()
95 {
96     done(1);
97 }
98 
99 
100 set_signals()
101 {
102 #ifdef SIGINT
103     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
104 	signal(SIGINT, onintr);
105 #endif
106 #ifdef SIGTERM
107     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
108 	signal(SIGTERM, onintr);
109 #endif
110 #ifdef SIGHUP
111     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
112 	signal(SIGHUP, onintr);
113 #endif
114 }
115 
116 
117 usage()
118 {
119     fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
120     exit(1);
121 }
122 
123 
124 getargs(argc, argv)
125 int argc;
126 char *argv[];
127 {
128     register int i;
129     register char *s;
130 
131     if (argc > 0) myname = argv[0];
132     for (i = 1; i < argc; ++i)
133     {
134 	s = argv[i];
135 	if (*s != '-') break;
136 	switch (*++s)
137 	{
138 	case '\0':
139 	    input_file = stdin;
140 	    if (i + 1 < argc) usage();
141 	    return;
142 
143 	case '-':
144 	    ++i;
145 	    goto no_more_options;
146 
147 	case 'b':
148 	    if (*++s)
149 		 file_prefix = s;
150 	    else if (++i < argc)
151 		file_prefix = argv[i];
152 	    else
153 		usage();
154 	    continue;
155 
156 	case 'd':
157 	    dflag = 1;
158 	    break;
159 
160 	case 'l':
161 	    lflag = 1;
162 	    break;
163 
164 	case 'r':
165 	    rflag = 1;
166 	    break;
167 
168 	case 't':
169 	    tflag = 1;
170 	    break;
171 
172 	case 'v':
173 	    vflag = 1;
174 	    break;
175 
176 	default:
177 	    usage();
178 	}
179 
180 	for (;;)
181 	{
182 	    switch (*++s)
183 	    {
184 	    case '\0':
185 		goto end_of_option;
186 
187 	    case 'd':
188 		dflag = 1;
189 		break;
190 
191 	    case 'l':
192 		lflag = 1;
193 		break;
194 
195 	    case 'r':
196 		rflag = 1;
197 		break;
198 
199 	    case 't':
200 		tflag = 1;
201 		break;
202 
203 	    case 'v':
204 		vflag = 1;
205 		break;
206 
207 	    default:
208 		usage();
209 	    }
210 	}
211 end_of_option:;
212     }
213 
214 no_more_options:;
215     if (i + 1 != argc) usage();
216     input_file_name = argv[i];
217 }
218 
219 
220 char *
221 allocate(n)
222 unsigned n;
223 {
224     register char *p;
225 
226     p = NULL;
227     if (n)
228     {
229 	p = CALLOC(1, n);
230 	if (!p) no_space();
231     }
232     return (p);
233 }
234 
235 
236 create_file_names()
237 {
238     int i, len;
239     char *tmpdir;
240 
241     tmpdir = getenv("TMPDIR");
242     if (tmpdir == 0) tmpdir = "/tmp";
243 
244     len = strlen(tmpdir);
245     i = len + 13;
246     if (len && tmpdir[len-1] != '/')
247 	++i;
248 
249     action_file_name = MALLOC(i);
250     if (action_file_name == 0) no_space();
251     text_file_name = MALLOC(i);
252     if (text_file_name == 0) no_space();
253     union_file_name = MALLOC(i);
254     if (union_file_name == 0) no_space();
255 
256     strcpy(action_file_name, tmpdir);
257     strcpy(text_file_name, tmpdir);
258     strcpy(union_file_name, tmpdir);
259 
260     if (len && tmpdir[len - 1] != '/')
261     {
262 	action_file_name[len] = '/';
263 	text_file_name[len] = '/';
264 	union_file_name[len] = '/';
265 	++len;
266     }
267 
268     strcpy(action_file_name + len, temp_form);
269     strcpy(text_file_name + len, temp_form);
270     strcpy(union_file_name + len, temp_form);
271 
272     action_file_name[len + 5] = 'a';
273     text_file_name[len + 5] = 't';
274     union_file_name[len + 5] = 'u';
275 
276     mktemp(action_file_name);
277     mktemp(text_file_name);
278     mktemp(union_file_name);
279 
280     len = strlen(file_prefix);
281 
282     output_file_name = MALLOC(len + 7);
283     if (output_file_name == 0)
284 	no_space();
285     strcpy(output_file_name, file_prefix);
286     strcpy(output_file_name + len, OUTPUT_SUFFIX);
287 
288     if (rflag)
289     {
290 	code_file_name = MALLOC(len + 8);
291 	if (code_file_name == 0)
292 	    no_space();
293 	strcpy(code_file_name, file_prefix);
294 	strcpy(code_file_name + len, CODE_SUFFIX);
295     }
296     else
297 	code_file_name = output_file_name;
298 
299     if (dflag)
300     {
301 	defines_file_name = MALLOC(len + 7);
302 	if (defines_file_name == 0)
303 	    no_space();
304 	strcpy(defines_file_name, file_prefix);
305 	strcpy(defines_file_name + len, DEFINES_SUFFIX);
306     }
307 
308     if (vflag)
309     {
310 	verbose_file_name = MALLOC(len + 8);
311 	if (verbose_file_name == 0)
312 	    no_space();
313 	strcpy(verbose_file_name, file_prefix);
314 	strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
315     }
316 }
317 
318 
319 open_files()
320 {
321     create_file_names();
322 
323     if (input_file == 0)
324     {
325 	input_file = fopen(input_file_name, "r");
326 	if (input_file == 0)
327 	    open_error(input_file_name);
328     }
329 
330     action_file = fopen(action_file_name, "w");
331     if (action_file == 0)
332 	open_error(action_file_name);
333 
334     text_file = fopen(text_file_name, "w");
335     if (text_file == 0)
336 	open_error(text_file_name);
337 
338     if (vflag)
339     {
340 	verbose_file = fopen(verbose_file_name, "w");
341 	if (verbose_file == 0)
342 	    open_error(verbose_file_name);
343     }
344 
345     if (dflag)
346     {
347 	defines_file = fopen(defines_file_name, "w");
348 	if (defines_file == 0)
349 	    open_error(defines_file_name);
350 	union_file = fopen(union_file_name, "w");
351 	if (union_file ==  0)
352 	    open_error(union_file_name);
353     }
354 
355     output_file = fopen(output_file_name, "w");
356     if (output_file == 0)
357 	open_error(output_file_name);
358 
359     if (rflag)
360     {
361 	code_file = fopen(code_file_name, "w");
362 	if (code_file == 0)
363 	    open_error(code_file_name);
364     }
365     else
366 	code_file = output_file;
367 }
368 
369 
370 int
371 main(argc, argv)
372 int argc;
373 char *argv[];
374 {
375     set_signals();
376     getargs(argc, argv);
377     open_files();
378     reader();
379     lr0();
380     lalr();
381     make_parser();
382     verbose();
383     output();
384     done(0);
385     /*NOTREACHED*/
386 }
387