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