1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the Q Public License version 1.0. */
11 /***********************************************************************/
13 /* Based on public-domain code from Berkeley Yacc */
15 /* $Id: main.c 7382 2006-04-16 23:28:22Z doligez $ */
35 char *file_prefix = 0;
36 char *myname = "yacc";
38 char temp_form[] = "yacc.X";
40 char temp_form[] = "yacc.XXXXXXX";
44 char *virtual_input_file_name = NULL;
47 char *action_file_name;
48 char *entry_file_name;
50 char *interface_file_name;
51 char *defines_file_name;
52 char *input_file_name = "";
53 char *output_file_name;
55 char *union_file_name;
56 char *verbose_file_name;
58 FILE *action_file; /* a temp file, used to save actions associated */
59 /* with rules until the parser is written */
61 FILE *code_file; /* y.code.c (used when the -r option is specified) */
62 FILE *defines_file; /* y.tab.h */
63 FILE *input_file; /* the input file */
64 FILE *output_file; /* y.tab.c */
65 FILE *text_file; /* a temp file, used to save text until all */
66 /* symbols have been defined */
67 FILE *union_file; /* a temp file, used to save the union */
68 /* definition until all symbol have been */
70 FILE *verbose_file; /* y.output */
86 char *symbol_true_token;
96 extern char *mktemp(char *);
97 extern char *getenv(const char *);
102 if (action_file) { fclose(action_file); unlink(action_file_name); }
103 if (entry_file) { fclose(entry_file); unlink(entry_file_name); }
104 if (text_file) { fclose(text_file); unlink(text_file_name); }
105 if (union_file) { fclose(union_file); unlink(union_file_name); }
106 if (output_file && k > 0) {
107 fclose(output_file); unlink(output_file_name);
109 if (interface_file && k > 0) {
110 fclose(interface_file); unlink(interface_file_name);
116 void onintr(int dummy)
122 void set_signals(void)
125 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
126 signal(SIGINT, onintr);
129 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
130 signal(SIGTERM, onintr);
133 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
134 signal(SIGHUP, onintr);
141 fprintf(stderr, "usage: %s [-v] [-q] [-b file_prefix] filename\n",
146 void getargs(int argc, char **argv)
151 if (argc > 0) myname = argv[0];
152 for (i = 1; i < argc; ++i)
155 if (*s != '-') break;
160 file_prefix = "stdin";
161 if (i + 1 < argc) usage();
166 goto no_more_options;
169 if (!strcmp (argv[i], "-version")){
170 printf ("The Objective Caml parser generator, version "
186 file_prefix = argv[i];
218 if (i + 1 != argc) usage();
219 input_file_name = argv[i];
220 if (file_prefix == 0) {
222 len = strlen(argv[i]);
223 file_prefix = malloc(len + 1);
224 if (file_prefix == 0) no_space();
225 strcpy(file_prefix, argv[i]);
228 if (file_prefix[len] == '.') {
229 file_prefix[len] = 0;
238 allocate(unsigned int n)
252 void create_file_names(void)
259 i = sizeof(temp_form);
261 tmpdir = getenv("TMPDIR");
262 if (tmpdir == 0) tmpdir = "/tmp";
263 len = strlen(tmpdir);
264 i = len + sizeof(temp_form);
265 if (len && tmpdir[len-1] != '/')
269 action_file_name = MALLOC(i);
270 if (action_file_name == 0) no_space();
271 entry_file_name = MALLOC(i);
272 if (entry_file_name == 0) no_space();
273 text_file_name = MALLOC(i);
274 if (text_file_name == 0) no_space();
275 union_file_name = MALLOC(i);
276 if (union_file_name == 0) no_space();
279 strcpy(action_file_name, tmpdir);
280 strcpy(entry_file_name, tmpdir);
281 strcpy(text_file_name, tmpdir);
282 strcpy(union_file_name, tmpdir);
284 if (len && tmpdir[len - 1] != '/')
286 action_file_name[len] = '/';
287 entry_file_name[len] = '/';
288 text_file_name[len] = '/';
289 union_file_name[len] = '/';
294 strcpy(action_file_name + len, temp_form);
295 strcpy(entry_file_name + len, temp_form);
296 strcpy(text_file_name + len, temp_form);
297 strcpy(union_file_name + len, temp_form);
299 action_file_name[len + 5] = 'a';
300 entry_file_name[len + 5] = 'e';
301 text_file_name[len + 5] = 't';
302 union_file_name[len + 5] = 'u';
305 mktemp(action_file_name);
306 mktemp(entry_file_name);
307 mktemp(text_file_name);
308 mktemp(union_file_name);
311 len = strlen(file_prefix);
313 output_file_name = MALLOC(len + 7);
314 if (output_file_name == 0)
316 strcpy(output_file_name, file_prefix);
317 strcpy(output_file_name + len, OUTPUT_SUFFIX);
319 code_file_name = output_file_name;
323 verbose_file_name = MALLOC(len + 8);
324 if (verbose_file_name == 0)
326 strcpy(verbose_file_name, file_prefix);
327 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
330 interface_file_name = MALLOC(len + 8);
331 if (interface_file_name == 0)
333 strcpy(interface_file_name, file_prefix);
334 strcpy(interface_file_name + len, INTERFACE_SUFFIX);
339 void open_files(void)
345 input_file = fopen(input_file_name, "r");
347 open_error(input_file_name);
350 action_file = fopen(action_file_name, "w");
351 if (action_file == 0)
352 open_error(action_file_name);
354 entry_file = fopen(entry_file_name, "w");
356 open_error(entry_file_name);
358 text_file = fopen(text_file_name, "w");
360 open_error(text_file_name);
364 verbose_file = fopen(verbose_file_name, "w");
365 if (verbose_file == 0)
366 open_error(verbose_file_name);
371 defines_file = fopen(defines_file_name, "w");
372 if (defines_file == 0)
373 open_error(defines_file_name);
374 union_file = fopen(union_file_name, "w");
376 open_error(union_file_name);
379 output_file = fopen(output_file_name, "w");
380 if (output_file == 0)
381 open_error(output_file_name);
385 code_file = fopen(code_file_name, "w");
387 open_error(code_file_name);
390 code_file = output_file;
393 interface_file = fopen(interface_file_name, "w");
394 if (interface_file == 0)
395 open_error(interface_file_name);
398 int main(int argc, char **argv)