2003-04-28 Piers Haken <piersh@friskit.com>
[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 [-tvc] [-b file_prefix] filename\n", myname);
135     exit(1);
136 }
137
138
139 getargs(argc, argv)
140 int argc;
141 char *argv[];
142 {
143     register int i;
144     register char *s;
145
146     if (argc > 0) myname = argv[0];
147     for (i = 1; i < argc; ++i)
148     {
149         s = argv[i];
150         if (*s != '-') break;
151         switch (*++s)
152         {
153         case '\0':
154             input_file = stdin;
155             if (i + 1 < argc) usage();
156             return;
157
158         case '-':
159             ++i;
160             goto no_more_options;
161
162         case 'b':
163             if (*++s)
164                  file_prefix = s;
165             else if (++i < argc)
166                 file_prefix = argv[i];
167             else
168                 usage();
169             continue;
170
171         case 't':
172             tflag = 1;
173             break;
174
175         case 'c':
176             csharp = 1;
177             line_format = "#line %d \"%s\"\n";
178             default_line_format = "#line default\n";
179             break;
180             
181         case 'v':
182             vflag = 1;
183             break;
184
185         default:
186             usage();
187         }
188
189         for (;;)
190         {
191             switch (*++s)
192             {
193             case '\0':
194                 goto end_of_option;
195
196             case 't':
197                 tflag = 1;
198                 break;
199
200             case 'v':
201                 vflag = 1;
202                 break;
203
204             case 'c':
205                 csharp = 1;
206                 break;
207
208             default:
209                 usage();
210             }
211         }
212 end_of_option:;
213     }
214
215 no_more_options:;
216     if (i + 1 != argc) usage();
217     input_file_name = argv[i];
218 }
219
220
221 char *
222 allocate(n)
223 unsigned n;
224 {
225     register char *p;
226
227     p = NULL;
228     if (n)
229     {
230         p = CALLOC(1, n);
231         if (!p) no_space();
232     }
233     return (p);
234 }
235
236
237 create_file_names()
238 {
239     int i, len;
240     char *tmpdir;
241
242     tmpdir = getenv("TMPDIR");
243     if (tmpdir == 0) tmpdir = "/tmp";
244
245     len = strlen(tmpdir);
246     i = len + 13;
247     if (len && tmpdir[len-1] != '/')
248         ++i;
249
250     action_file_name = MALLOC(i);
251     if (action_file_name == 0) no_space();
252     prolog_file_name = MALLOC(i);
253     if (prolog_file_name == 0) no_space();
254     local_file_name = MALLOC(i);
255     if (local_file_name == 0) no_space();
256
257     strcpy(action_file_name, tmpdir);
258     strcpy(prolog_file_name, tmpdir);
259     strcpy(local_file_name, tmpdir);
260
261     if (len && tmpdir[len - 1] != '/')
262     {
263         action_file_name[len] = '/';
264         prolog_file_name[len] = '/';
265         local_file_name[len] = '/';
266         ++len;
267     }
268
269     strcpy(action_file_name + len, temp_form);
270     strcpy(prolog_file_name + len, temp_form);
271     strcpy(local_file_name + len, temp_form);
272
273     action_file_name[len + 5] = 'a';
274     prolog_file_name[len + 5] = 'p';
275     local_file_name[len + 5] = 'l';
276
277     mkstemp(action_file_name);
278     mkstemp(prolog_file_name);
279     mkstemp(local_file_name);
280
281     len = strlen(file_prefix);
282
283     if (vflag)
284     {
285         verbose_file_name = MALLOC(len + 8);
286         if (verbose_file_name == 0)
287             no_space();
288         strcpy(verbose_file_name, file_prefix);
289         strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
290     }
291 }
292
293
294 open_files()
295 {
296     create_file_names();
297
298     if (input_file == 0)
299     {
300         input_file = fopen(input_file_name, "r");
301         if (input_file == 0)
302             open_error(input_file_name);
303     }
304
305     action_file = fopen(action_file_name, "w");
306     if (action_file == 0)
307         open_error(action_file_name);
308
309     prolog_file = fopen(prolog_file_name, "w");
310     if (prolog_file == 0)
311         open_error(prolog_file_name);
312
313     local_file = fopen(local_file_name, "w");
314     if (local_file == 0)
315         open_error(local_file_name);
316
317     if (vflag)
318     {
319         verbose_file = fopen(verbose_file_name, "w");
320         if (verbose_file == 0)
321             open_error(verbose_file_name);
322     }
323 }
324
325
326 int
327 main(argc, argv)
328 int argc;
329 char *argv[];
330 {
331     set_signals();
332     getargs(argc, argv);
333     open_files();
334     reader();
335     lr0();
336     lalr();
337     make_parser();
338     verbose();
339     output();
340     done(0);
341     /*NOTREACHED*/
342 }