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