[corlib] Fix a few NSLogWriter issues. (#13799)
[mono-project.git] / mcs / jay / main.c
blob2dc3580bd182781a10adc7d132f3764c1a8edce6
1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Paul Corbett.
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.
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.
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 */
43 #ifndef lint
44 static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
45 #endif /* not lint */
47 #include <signal.h>
48 #include "defs.h"
50 // FIXME autoconf or use remove instead of unlink
51 #ifdef _MSC_VER
52 #include <io.h>
53 #else
54 #include <unistd.h>
55 #endif
57 char tflag;
58 char vflag;
59 int csharp = 0;
61 const char *file_prefix = (char*)"y";
62 char *myname = (char*)"yacc";
63 const char *temp_form = "yacc.XXXXXXX";
65 int lineno;
66 int outline;
68 char *action_file_name;
69 char *input_file_name = (char*)"";
70 char *prolog_file_name;
71 char *local_file_name;
72 char *verbose_file_name;
73 char *output_file_name = 0;
75 FILE *action_file; /* a temp file, used to save actions associated */
76 /* with rules until the parser is written */
77 FILE *input_file; /* the input file */
78 FILE *prolog_file; /* temp files, used to save text until all */
79 FILE *local_file; /* symbols have been defined */
80 FILE *verbose_file; /* y.output */
81 FILE *output_file; /* defaults to stdout */
83 int nitems;
84 int nrules;
85 int nsyms;
86 int ntokens;
87 int nvars;
88 int nmethods;
90 int start_symbol;
91 char **symbol_name;
92 short *symbol_value;
93 short *symbol_prec;
94 char *symbol_assoc;
95 char **methods;
97 short *ritem;
98 short *rlhs;
99 short *rrhs;
100 short *rprec;
101 char *rassoc;
102 short **derives;
103 char *nullable;
105 #if defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
106 extern char* mktemp();
107 #define mkstemp mktemp
108 #endif
110 extern char *getenv();
112 void
113 done (int k)
115 if (action_file) { fclose(action_file); unlink(action_file_name); }
116 if (prolog_file) { fclose(prolog_file); unlink(prolog_file_name); }
117 if (local_file) { fclose(local_file); unlink(local_file_name); }
118 if (output_file && (output_file != stdout)) { fclose(output_file); if (k != 0) unlink(output_file_name); }
119 exit(k);
122 static void
123 onintr (int signo)
125 (void)signo; // unused
126 done(1);
129 static void
130 set_signals (void)
132 #ifdef SIGINT
133 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
134 signal(SIGINT, onintr);
135 #endif
136 #ifdef SIGTERM
137 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
138 signal(SIGTERM, onintr);
139 #endif
140 #ifdef SIGHUP
141 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
142 signal(SIGHUP, onintr);
143 #endif
146 static void
147 usage (void)
149 fprintf(stderr, "usage: %s [-tvcp] [-b file_prefix] [-o output_filename] input_filename\n", myname);
150 exit(1);
153 static void
154 print_skel_dir(void)
156 printf ("%s\n", SKEL_DIRECTORY);
157 exit (0);
160 static void
161 getargs (int argc, char *argv[])
163 register int i;
164 register char *s;
166 if (argc > 0) myname = argv[0];
167 for (i = 1; i < argc; ++i)
169 s = argv[i];
170 if (*s != '-') break;
171 switch (*++s)
173 case '\0':
174 input_file = stdin;
175 if (i + 1 < argc) usage();
176 return;
178 case '-':
179 ++i;
180 goto no_more_options;
182 case 'b':
183 if (*++s)
184 file_prefix = s;
185 else if (++i < argc)
186 file_prefix = argv[i];
187 else
188 usage();
189 continue;
191 case 'o':
192 if (*++s)
193 output_file_name = s;
194 else if (++i < argc)
195 output_file_name = argv[i];
196 else
197 usage();
198 continue;
200 case 't':
201 tflag = 1;
202 break;
204 case 'p':
205 print_skel_dir ();
206 break;
208 case 'c':
209 csharp = 1;
210 line_format = "#line %d \"%s\"\n";
211 default_line_format = "#line default\n";
212 break;
214 case 'v':
215 vflag = 1;
216 break;
218 default:
219 usage();
222 for (;;)
224 switch (*++s)
226 case '\0':
227 goto end_of_option;
229 case 't':
230 tflag = 1;
231 break;
233 case 'v':
234 vflag = 1;
235 break;
237 case 'p':
238 print_skel_dir ();
239 break;
241 case 'c':
242 csharp = 1;
243 line_format = "#line %d \"%s\"\n";
244 default_line_format = "#line default\n";
246 break;
248 default:
249 usage();
252 end_of_option:;
255 no_more_options:;
256 if (i + 1 != argc) usage();
257 input_file_name = argv[i];
260 char *
261 allocate (unsigned n)
263 register char *p;
265 p = NULL;
266 if (n)
268 p = (char*)CALLOC(1, n);
269 if (!p) no_space();
271 return (p);
274 #ifdef __GNUC__
275 #define GNUC_UNUSED __attribute__((__unused__))
276 #else
277 #define GNUC_UNUSED
278 #endif
280 static void
281 create_file_names (void)
283 int i, len;
284 const char *tmpdir;
285 int mkstemp_res GNUC_UNUSED;
287 #if defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
288 tmpdir = ".";
289 #else
290 tmpdir = getenv("TMPDIR");
291 if (tmpdir == 0) tmpdir = getenv ("TMP");
292 if (tmpdir == 0) tmpdir = getenv ("TEMP");
293 if (tmpdir == 0) tmpdir = "/tmp";
294 #endif
296 len = strlen(tmpdir);
297 i = len + 13;
298 if (len && tmpdir[len-1] != '/')
299 ++i;
301 action_file_name = (char*)MALLOC(i);
302 if (action_file_name == 0) no_space();
303 prolog_file_name = (char*)MALLOC(i);
304 if (prolog_file_name == 0) no_space();
305 local_file_name = (char*)MALLOC(i);
306 if (local_file_name == 0) no_space();
308 strcpy(action_file_name, tmpdir);
309 strcpy(prolog_file_name, tmpdir);
310 strcpy(local_file_name, tmpdir);
312 if (len && tmpdir[len - 1] != '/')
314 action_file_name[len] = '/';
315 prolog_file_name[len] = '/';
316 local_file_name[len] = '/';
317 ++len;
320 strcpy(action_file_name + len, temp_form);
321 strcpy(prolog_file_name + len, temp_form);
322 strcpy(local_file_name + len, temp_form);
324 action_file_name[len + 5] = 'a';
325 prolog_file_name[len + 5] = 'p';
326 local_file_name[len + 5] = 'l';
328 mkstemp_res = mkstemp(action_file_name);
329 mkstemp_res = mkstemp(prolog_file_name);
330 mkstemp_res = mkstemp(local_file_name);
332 len = strlen(file_prefix);
334 if (vflag)
336 verbose_file_name = (char*)MALLOC(len + 8);
337 if (verbose_file_name == 0)
338 no_space();
339 strcpy(verbose_file_name, file_prefix);
340 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
344 static void
345 open_files (void)
347 create_file_names();
349 if (input_file == 0)
351 input_file = fopen(input_file_name, "r");
352 if (input_file == 0)
353 open_error(input_file_name);
356 action_file = fopen(action_file_name, "w");
357 if (action_file == 0)
358 open_error(action_file_name);
360 prolog_file = fopen(prolog_file_name, "w");
361 if (prolog_file == 0)
362 open_error(prolog_file_name);
364 local_file = fopen(local_file_name, "w");
365 if (local_file == 0)
366 open_error(local_file_name);
368 if (vflag)
370 verbose_file = fopen(verbose_file_name, "w");
371 if (verbose_file == 0)
372 open_error(verbose_file_name);
375 if (output_file == 0)
377 if (output_file_name != 0) {
378 output_file = fopen(output_file_name, "w");
379 if (output_file == 0)
380 open_error(output_file_name);
381 } else {
382 output_file = stdout;
389 main (int argc, char *argv[])
391 set_signals();
392 getargs(argc, argv);
393 open_files();
394 reader();
395 lr0();
396 lalr();
397 make_parser();
398 verbose();
399 output();
400 done(0);
401 /*NOTREACHED*/