main.c revision 1.1.1.2 1 1.1 christos /* $NetBSD: main.c,v 1.1.1.2 2010/12/23 23:36:26 christos Exp $ */
2 1.1 christos
3 1.1.1.2 christos /* Id: main.c,v 1.30 2010/11/24 15:13:39 tom Exp */
4 1.1 christos
5 1.1 christos #include <signal.h>
6 1.1 christos #include <unistd.h> /* for _exit() */
7 1.1 christos
8 1.1 christos #include "defs.h"
9 1.1 christos
10 1.1 christos char dflag;
11 1.1 christos char gflag;
12 1.1 christos char lflag;
13 1.1.1.2 christos static char oflag;
14 1.1 christos char rflag;
15 1.1 christos char tflag;
16 1.1 christos char vflag;
17 1.1 christos
18 1.1 christos const char *symbol_prefix;
19 1.1 christos const char *myname = "yacc";
20 1.1 christos
21 1.1 christos int lineno;
22 1.1 christos int outline;
23 1.1 christos
24 1.1 christos static char empty_string[] = "";
25 1.1 christos static char default_file_prefix[] = "y";
26 1.1 christos
27 1.1 christos static char *file_prefix = default_file_prefix;
28 1.1 christos
29 1.1 christos char *code_file_name;
30 1.1 christos char *input_file_name = empty_string;
31 1.1.1.2 christos static char *defines_file_name;
32 1.1.1.2 christos static char *graph_file_name;
33 1.1.1.2 christos static char *output_file_name;
34 1.1.1.2 christos static char *verbose_file_name;
35 1.1 christos
36 1.1 christos FILE *action_file; /* a temp file, used to save actions associated */
37 1.1 christos /* with rules until the parser is written */
38 1.1 christos FILE *code_file; /* y.code.c (used when the -r option is specified) */
39 1.1 christos FILE *defines_file; /* y.tab.h */
40 1.1 christos FILE *input_file; /* the input file */
41 1.1 christos FILE *output_file; /* y.tab.c */
42 1.1 christos FILE *text_file; /* a temp file, used to save text until all */
43 1.1 christos /* symbols have been defined */
44 1.1 christos FILE *union_file; /* a temp file, used to save the union */
45 1.1 christos /* definition until all symbol have been */
46 1.1 christos /* defined */
47 1.1 christos FILE *verbose_file; /* y.output */
48 1.1 christos FILE *graph_file; /* y.dot */
49 1.1 christos
50 1.1 christos int nitems;
51 1.1 christos int nrules;
52 1.1 christos int nsyms;
53 1.1 christos int ntokens;
54 1.1 christos int nvars;
55 1.1 christos
56 1.1 christos Value_t start_symbol;
57 1.1 christos char **symbol_name;
58 1.1 christos char **symbol_pname;
59 1.1 christos Value_t *symbol_value;
60 1.1 christos short *symbol_prec;
61 1.1 christos char *symbol_assoc;
62 1.1 christos
63 1.1.1.2 christos int pure_parser;
64 1.1 christos int exit_code;
65 1.1 christos
66 1.1 christos Value_t *ritem;
67 1.1 christos Value_t *rlhs;
68 1.1 christos Value_t *rrhs;
69 1.1 christos Value_t *rprec;
70 1.1 christos Assoc_t *rassoc;
71 1.1 christos Value_t **derives;
72 1.1 christos char *nullable;
73 1.1 christos
74 1.1 christos /*
75 1.1 christos * Since fclose() is called via the signal handler, it might die. Don't loop
76 1.1 christos * if there is a problem closing a file.
77 1.1 christos */
78 1.1 christos #define DO_CLOSE(fp) \
79 1.1 christos if (fp != 0) { \
80 1.1 christos FILE *use = fp; \
81 1.1 christos fp = 0; \
82 1.1 christos fclose(use); \
83 1.1 christos }
84 1.1 christos
85 1.1 christos static int got_intr = 0;
86 1.1 christos
87 1.1 christos void
88 1.1 christos done(int k)
89 1.1 christos {
90 1.1 christos DO_CLOSE(input_file);
91 1.1 christos DO_CLOSE(output_file);
92 1.1 christos
93 1.1 christos DO_CLOSE(action_file);
94 1.1 christos DO_CLOSE(defines_file);
95 1.1 christos DO_CLOSE(graph_file);
96 1.1 christos DO_CLOSE(text_file);
97 1.1 christos DO_CLOSE(union_file);
98 1.1 christos DO_CLOSE(verbose_file);
99 1.1 christos
100 1.1 christos if (got_intr)
101 1.1 christos _exit(EXIT_FAILURE);
102 1.1 christos
103 1.1 christos #ifdef NO_LEAKS
104 1.1 christos if (rflag)
105 1.1 christos DO_FREE(code_file_name);
106 1.1 christos
107 1.1 christos if (dflag)
108 1.1 christos DO_FREE(defines_file_name);
109 1.1 christos
110 1.1 christos if (oflag)
111 1.1 christos DO_FREE(output_file_name);
112 1.1 christos
113 1.1 christos if (vflag)
114 1.1 christos DO_FREE(verbose_file_name);
115 1.1 christos
116 1.1 christos if (gflag)
117 1.1 christos DO_FREE(graph_file_name);
118 1.1 christos
119 1.1 christos lr0_leaks();
120 1.1 christos lalr_leaks();
121 1.1 christos mkpar_leaks();
122 1.1 christos output_leaks();
123 1.1 christos reader_leaks();
124 1.1 christos #endif
125 1.1 christos
126 1.1.1.2 christos if (rflag)
127 1.1.1.2 christos DO_CLOSE(code_file);
128 1.1.1.2 christos
129 1.1 christos exit(k);
130 1.1 christos }
131 1.1 christos
132 1.1 christos static void
133 1.1 christos onintr(int sig GCC_UNUSED)
134 1.1 christos {
135 1.1 christos got_intr = 1;
136 1.1 christos done(EXIT_FAILURE);
137 1.1 christos }
138 1.1 christos
139 1.1 christos static void
140 1.1 christos set_signals(void)
141 1.1 christos {
142 1.1 christos #ifdef SIGINT
143 1.1 christos if (signal(SIGINT, SIG_IGN) != SIG_IGN)
144 1.1 christos signal(SIGINT, onintr);
145 1.1 christos #endif
146 1.1 christos #ifdef SIGTERM
147 1.1 christos if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
148 1.1 christos signal(SIGTERM, onintr);
149 1.1 christos #endif
150 1.1 christos #ifdef SIGHUP
151 1.1 christos if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
152 1.1 christos signal(SIGHUP, onintr);
153 1.1 christos #endif
154 1.1 christos }
155 1.1 christos
156 1.1 christos static void
157 1.1 christos usage(void)
158 1.1 christos {
159 1.1 christos static const char *msg[] =
160 1.1 christos {
161 1.1 christos ""
162 1.1 christos ,"Options:"
163 1.1 christos ," -b file_prefix set filename prefix (default \"y.\")"
164 1.1 christos ," -d write definitions (y.tab.h)"
165 1.1 christos ," -g write a graphical description"
166 1.1 christos ," -l suppress #line directives"
167 1.1 christos ," -o output_file (default \"y.tab.c\")"
168 1.1 christos ," -p symbol_prefix set symbol prefix (default \"yy\")"
169 1.1.1.2 christos ," -P create a reentrant parser, e.g., \"%pure-parser\""
170 1.1 christos ," -r produce separate code and table files (y.code.c)"
171 1.1 christos ," -t add debugging support"
172 1.1 christos ," -v write description (y.output)"
173 1.1 christos ," -V show version information and exit"
174 1.1 christos };
175 1.1 christos unsigned n;
176 1.1 christos
177 1.1 christos fflush(stdout);
178 1.1 christos fprintf(stderr, "Usage: %s [options] filename\n", myname);
179 1.1 christos for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
180 1.1 christos fprintf(stderr, "%s\n", msg[n]);
181 1.1 christos
182 1.1 christos exit(1);
183 1.1 christos }
184 1.1 christos
185 1.1 christos static void
186 1.1 christos setflag(int ch)
187 1.1 christos {
188 1.1 christos switch (ch)
189 1.1 christos {
190 1.1 christos case 'd':
191 1.1 christos dflag = 1;
192 1.1 christos break;
193 1.1 christos
194 1.1 christos case 'g':
195 1.1 christos gflag = 1;
196 1.1 christos break;
197 1.1 christos
198 1.1 christos case 'l':
199 1.1 christos lflag = 1;
200 1.1 christos break;
201 1.1 christos
202 1.1.1.2 christos case 'P':
203 1.1.1.2 christos pure_parser = 1;
204 1.1.1.2 christos break;
205 1.1.1.2 christos
206 1.1 christos case 'r':
207 1.1 christos rflag = 1;
208 1.1 christos break;
209 1.1 christos
210 1.1 christos case 't':
211 1.1 christos tflag = 1;
212 1.1 christos break;
213 1.1 christos
214 1.1 christos case 'v':
215 1.1 christos vflag = 1;
216 1.1 christos break;
217 1.1 christos
218 1.1 christos case 'V':
219 1.1 christos printf("%s - %s\n", myname, VERSION);
220 1.1 christos exit(EXIT_SUCCESS);
221 1.1 christos
222 1.1.1.2 christos case 'y':
223 1.1.1.2 christos /* noop for bison compatibility. byacc is already designed to be posix
224 1.1.1.2 christos * yacc compatible. */
225 1.1.1.2 christos break;
226 1.1.1.2 christos
227 1.1 christos default:
228 1.1 christos usage();
229 1.1 christos }
230 1.1 christos }
231 1.1 christos
232 1.1 christos static void
233 1.1 christos getargs(int argc, char *argv[])
234 1.1 christos {
235 1.1 christos int i;
236 1.1 christos char *s;
237 1.1 christos int ch;
238 1.1 christos
239 1.1 christos if (argc > 0)
240 1.1 christos myname = argv[0];
241 1.1 christos
242 1.1 christos for (i = 1; i < argc; ++i)
243 1.1 christos {
244 1.1 christos s = argv[i];
245 1.1 christos if (*s != '-')
246 1.1 christos break;
247 1.1 christos switch (ch = *++s)
248 1.1 christos {
249 1.1 christos case '\0':
250 1.1 christos input_file = stdin;
251 1.1 christos if (i + 1 < argc)
252 1.1 christos usage();
253 1.1 christos return;
254 1.1 christos
255 1.1 christos case '-':
256 1.1 christos ++i;
257 1.1 christos goto no_more_options;
258 1.1 christos
259 1.1 christos case 'b':
260 1.1 christos if (*++s)
261 1.1 christos file_prefix = s;
262 1.1 christos else if (++i < argc)
263 1.1 christos file_prefix = argv[i];
264 1.1 christos else
265 1.1 christos usage();
266 1.1 christos continue;
267 1.1 christos
268 1.1 christos case 'o':
269 1.1 christos if (*++s)
270 1.1 christos output_file_name = s;
271 1.1 christos else if (++i < argc)
272 1.1 christos output_file_name = argv[i];
273 1.1 christos else
274 1.1 christos usage();
275 1.1 christos continue;
276 1.1 christos
277 1.1 christos case 'p':
278 1.1 christos if (*++s)
279 1.1 christos symbol_prefix = s;
280 1.1 christos else if (++i < argc)
281 1.1 christos symbol_prefix = argv[i];
282 1.1 christos else
283 1.1 christos usage();
284 1.1 christos continue;
285 1.1 christos
286 1.1 christos default:
287 1.1 christos setflag(ch);
288 1.1 christos break;
289 1.1 christos }
290 1.1 christos
291 1.1 christos for (;;)
292 1.1 christos {
293 1.1 christos switch (ch = *++s)
294 1.1 christos {
295 1.1 christos case '\0':
296 1.1 christos goto end_of_option;
297 1.1 christos
298 1.1 christos default:
299 1.1 christos setflag(ch);
300 1.1 christos break;
301 1.1 christos }
302 1.1 christos }
303 1.1 christos end_of_option:;
304 1.1 christos }
305 1.1 christos
306 1.1 christos no_more_options:;
307 1.1 christos if (i + 1 != argc)
308 1.1 christos usage();
309 1.1 christos input_file_name = argv[i];
310 1.1 christos }
311 1.1 christos
312 1.1 christos char *
313 1.1.1.2 christos allocate(size_t n)
314 1.1 christos {
315 1.1 christos char *p;
316 1.1 christos
317 1.1 christos p = NULL;
318 1.1 christos if (n)
319 1.1 christos {
320 1.1 christos p = CALLOC(1, n);
321 1.1.1.2 christos NO_SPACE(p);
322 1.1 christos }
323 1.1 christos return (p);
324 1.1 christos }
325 1.1 christos
326 1.1 christos #define CREATE_FILE_NAME(dest, suffix) \
327 1.1 christos dest = MALLOC(len + strlen(suffix) + 1); \
328 1.1.1.2 christos NO_SPACE(dest); \
329 1.1 christos strcpy(dest, file_prefix); \
330 1.1 christos strcpy(dest + len, suffix)
331 1.1 christos
332 1.1 christos static void
333 1.1 christos create_file_names(void)
334 1.1 christos {
335 1.1 christos size_t len;
336 1.1 christos const char *defines_suffix;
337 1.1 christos char *prefix;
338 1.1 christos
339 1.1 christos prefix = NULL;
340 1.1 christos defines_suffix = DEFINES_SUFFIX;
341 1.1 christos
342 1.1 christos /* compute the file_prefix from the user provided output_file_name */
343 1.1 christos if (output_file_name != 0)
344 1.1 christos {
345 1.1 christos if (!(prefix = strstr(output_file_name, ".tab.c"))
346 1.1 christos && (prefix = strstr(output_file_name, ".c")))
347 1.1 christos defines_suffix = ".h";
348 1.1 christos }
349 1.1 christos
350 1.1 christos if (prefix != NULL)
351 1.1 christos {
352 1.1 christos len = (size_t) (prefix - output_file_name);
353 1.1 christos file_prefix = (char *)MALLOC(len + 1);
354 1.1.1.2 christos NO_SPACE(file_prefix);
355 1.1 christos strncpy(file_prefix, output_file_name, len)[len] = 0;
356 1.1 christos }
357 1.1 christos else
358 1.1 christos len = strlen(file_prefix);
359 1.1 christos
360 1.1 christos /* if "-o filename" was not given */
361 1.1 christos if (output_file_name == 0)
362 1.1 christos {
363 1.1 christos oflag = 1;
364 1.1 christos CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
365 1.1 christos }
366 1.1 christos
367 1.1 christos if (rflag)
368 1.1 christos {
369 1.1 christos CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
370 1.1 christos }
371 1.1 christos else
372 1.1 christos code_file_name = output_file_name;
373 1.1 christos
374 1.1 christos if (dflag)
375 1.1 christos {
376 1.1 christos CREATE_FILE_NAME(defines_file_name, defines_suffix);
377 1.1 christos }
378 1.1 christos
379 1.1 christos if (vflag)
380 1.1 christos {
381 1.1 christos CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
382 1.1 christos }
383 1.1 christos
384 1.1 christos if (gflag)
385 1.1 christos {
386 1.1 christos CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
387 1.1 christos }
388 1.1 christos
389 1.1 christos if (prefix != NULL)
390 1.1 christos {
391 1.1 christos FREE(file_prefix);
392 1.1 christos }
393 1.1 christos }
394 1.1 christos
395 1.1 christos static void
396 1.1 christos open_files(void)
397 1.1 christos {
398 1.1 christos create_file_names();
399 1.1 christos
400 1.1 christos if (input_file == 0)
401 1.1 christos {
402 1.1 christos input_file = fopen(input_file_name, "r");
403 1.1 christos if (input_file == 0)
404 1.1 christos open_error(input_file_name);
405 1.1 christos }
406 1.1 christos
407 1.1 christos action_file = tmpfile();
408 1.1 christos if (action_file == 0)
409 1.1 christos open_error("action_file");
410 1.1 christos
411 1.1 christos text_file = tmpfile();
412 1.1 christos if (text_file == 0)
413 1.1 christos open_error("text_file");
414 1.1 christos
415 1.1 christos if (vflag)
416 1.1 christos {
417 1.1 christos verbose_file = fopen(verbose_file_name, "w");
418 1.1 christos if (verbose_file == 0)
419 1.1 christos open_error(verbose_file_name);
420 1.1 christos }
421 1.1 christos
422 1.1 christos if (gflag)
423 1.1 christos {
424 1.1 christos graph_file = fopen(graph_file_name, "w");
425 1.1 christos if (graph_file == 0)
426 1.1 christos open_error(graph_file_name);
427 1.1 christos fprintf(graph_file, "digraph %s {\n", file_prefix);
428 1.1 christos fprintf(graph_file, "\tedge [fontsize=10];\n");
429 1.1 christos fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
430 1.1 christos fprintf(graph_file, "\torientation=landscape;\n");
431 1.1 christos fprintf(graph_file, "\trankdir=LR;\n");
432 1.1 christos fprintf(graph_file, "\t/*\n");
433 1.1 christos fprintf(graph_file, "\tmargin=0.2;\n");
434 1.1 christos fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
435 1.1 christos fprintf(graph_file, "\tratio=auto;\n");
436 1.1 christos fprintf(graph_file, "\t*/\n");
437 1.1 christos }
438 1.1 christos
439 1.1 christos if (dflag)
440 1.1 christos {
441 1.1 christos defines_file = fopen(defines_file_name, "w");
442 1.1 christos if (defines_file == 0)
443 1.1 christos open_error(defines_file_name);
444 1.1 christos union_file = tmpfile();
445 1.1 christos if (union_file == 0)
446 1.1 christos open_error("union_file");
447 1.1 christos }
448 1.1 christos
449 1.1 christos output_file = fopen(output_file_name, "w");
450 1.1 christos if (output_file == 0)
451 1.1 christos open_error(output_file_name);
452 1.1 christos
453 1.1 christos if (rflag)
454 1.1 christos {
455 1.1 christos code_file = fopen(code_file_name, "w");
456 1.1 christos if (code_file == 0)
457 1.1 christos open_error(code_file_name);
458 1.1 christos }
459 1.1 christos else
460 1.1 christos code_file = output_file;
461 1.1 christos }
462 1.1 christos
463 1.1 christos int
464 1.1 christos main(int argc, char *argv[])
465 1.1 christos {
466 1.1 christos SRexpect = -1;
467 1.1 christos RRexpect = -1;
468 1.1 christos exit_code = EXIT_SUCCESS;
469 1.1 christos
470 1.1 christos set_signals();
471 1.1 christos getargs(argc, argv);
472 1.1 christos open_files();
473 1.1 christos reader();
474 1.1 christos lr0();
475 1.1 christos lalr();
476 1.1 christos make_parser();
477 1.1 christos graph();
478 1.1 christos finalize_closure();
479 1.1 christos verbose();
480 1.1 christos output();
481 1.1 christos done(exit_code);
482 1.1 christos /*NOTREACHED */
483 1.1 christos }
484