Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / byacc / dist / main.c
blobc06263249184ed926cf1900699a0ecaafa3fbf0e
1 /* $NetBSD: main.c,v 1.2 2009/10/29 00:56:20 christos Exp $ */
2 /* Id: main.c,v 1.23 2009/10/27 09:06:44 tom Exp */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: main.c,v 1.2 2009/10/29 00:56:20 christos Exp $");
9 #include <signal.h>
10 #include <unistd.h> /* for _exit() */
13 char dflag;
14 char gflag;
15 char lflag;
16 char oflag;
17 char rflag;
18 char tflag;
19 char vflag;
21 const char *symbol_prefix;
22 const char *myname = "yacc";
24 int lineno;
25 int outline;
27 static char empty_string[] = "";
28 static const char default_file_prefix[] = "y";
29 static int explicit_file_name;
31 static const char *file_prefix = default_file_prefix;
33 char *code_file_name;
34 char *defines_file_name;
35 const char *input_file_name = empty_string;
36 char *output_file_name = 0;
37 char *verbose_file_name;
38 char *graph_file_name;
39 char *allocated_file_prefix;
41 FILE *action_file; /* a temp file, used to save actions associated */
42 /* with rules until the parser is written */
43 FILE *code_file; /* y.code.c (used when the -r option is specified) */
44 FILE *defines_file; /* y.tab.h */
45 FILE *input_file; /* the input file */
46 FILE *output_file; /* y.tab.c */
47 FILE *text_file; /* a temp file, used to save text until all */
48 /* symbols have been defined */
49 FILE *union_file; /* a temp file, used to save the union */
50 /* definition until all symbol have been */
51 /* defined */
52 FILE *verbose_file; /* y.output */
53 FILE *graph_file; /* y.dot */
55 int nitems;
56 int nrules;
57 int nsyms;
58 int ntokens;
59 int nvars;
61 Value_t start_symbol;
62 char **symbol_name;
63 char **symbol_pname;
64 Value_t *symbol_value;
65 short *symbol_prec;
66 char *symbol_assoc;
68 int exit_code;
70 Value_t *ritem;
71 Value_t *rlhs;
72 Value_t *rrhs;
73 Value_t *rprec;
74 Assoc_t *rassoc;
75 Value_t **derives;
76 char *nullable;
79 * Since fclose() is called via the signal handler, it might die. Don't loop
80 * if there is a problem closing a file.
82 #define DO_CLOSE(fp) \
83 if (fp != 0) { \
84 FILE *use = fp; \
85 fp = 0; \
86 fclose(use); \
89 static int got_intr = 0;
91 void
92 done(int k)
94 DO_CLOSE(input_file);
95 DO_CLOSE(output_file);
97 DO_CLOSE(action_file);
98 DO_CLOSE(defines_file);
99 DO_CLOSE(graph_file);
100 DO_CLOSE(text_file);
101 DO_CLOSE(union_file);
102 DO_CLOSE(verbose_file);
104 if (got_intr)
105 _exit(EXIT_FAILURE);
107 #ifdef NO_LEAKS
108 if (rflag)
109 DO_FREE(code_file_name);
111 if (dflag)
112 DO_FREE(defines_file_name);
114 if (oflag)
115 DO_FREE(output_file_name);
117 if (vflag)
118 DO_FREE(verbose_file_name);
120 if (gflag)
121 DO_FREE(graph_file_name);
123 lr0_leaks();
124 lalr_leaks();
125 mkpar_leaks();
126 output_leaks();
127 reader_leaks();
128 #endif
130 exit(k);
133 static void
134 onintr(int sig GCC_UNUSED)
136 got_intr = 1;
137 done(EXIT_FAILURE);
140 static void
141 set_signals(void)
143 #ifdef SIGINT
144 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
145 signal(SIGINT, onintr);
146 #endif
147 #ifdef SIGTERM
148 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
149 signal(SIGTERM, onintr);
150 #endif
151 #ifdef SIGHUP
152 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
153 signal(SIGHUP, onintr);
154 #endif
157 static void
158 usage(void)
160 static const char *msg[] =
163 ,"Options:"
164 ," -b file_prefix set filename prefix (default \"y.\")"
165 ," -d write definitions (y.tab.h)"
166 ," -g write a graphical description"
167 ," -l suppress #line directives"
168 ," -o output_file (default \"y.tab.c\")"
169 ," -p symbol_prefix set symbol prefix (default \"yy\")"
170 ," -r produce separate code and table files (y.code.c)"
171 ," -t add debugging support"
172 ," -v write description (y.output)"
173 ," -V show version information and exit"
175 unsigned n;
177 fflush(stdout);
178 fprintf(stderr, "Usage: %s [options] filename\n", myname);
179 for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
180 fprintf(stderr, "%s\n", msg[n]);
182 exit(1);
185 static void
186 setflag(int ch)
188 switch (ch)
190 case 'd':
191 dflag = 1;
192 break;
194 case 'g':
195 gflag = 1;
196 break;
198 case 'l':
199 lflag = 1;
200 break;
202 case 'r':
203 rflag = 1;
204 break;
206 case 't':
207 tflag = 1;
208 break;
210 case 'v':
211 vflag = 1;
212 break;
214 case 'V':
215 printf("%s - %s\n", myname, VERSION);
216 exit(EXIT_SUCCESS);
218 default:
219 usage();
223 static void
224 getargs(int argc, char *argv[])
226 int i;
227 char *s;
228 int ch;
230 if (argc > 0)
231 myname = argv[0];
233 for (i = 1; i < argc; ++i)
235 s = argv[i];
236 if (*s != '-')
237 break;
238 switch (ch = *++s)
240 case '\0':
241 input_file = stdin;
242 if (i + 1 < argc)
243 usage();
244 return;
246 case '-':
247 ++i;
248 goto no_more_options;
250 case 'b':
251 if (*++s)
252 file_prefix = s;
253 else if (++i < argc)
254 file_prefix = argv[i];
255 else
256 usage();
257 continue;
259 case 'o':
260 if (*++s)
261 output_file_name = s;
262 else if (++i < argc)
263 output_file_name = argv[i];
264 else
265 usage();
266 explicit_file_name = 1;
267 continue;
269 case 'p':
270 if (*++s)
271 symbol_prefix = s;
272 else if (++i < argc)
273 symbol_prefix = argv[i];
274 else
275 usage();
276 continue;
278 default:
279 setflag(ch);
280 break;
283 for (;;)
285 switch (ch = *++s)
287 case '\0':
288 goto end_of_option;
290 default:
291 setflag(ch);
292 break;
295 end_of_option:;
298 no_more_options:;
299 if (i + 1 != argc)
300 usage();
301 input_file_name = argv[i];
304 char *
305 allocate(unsigned n)
307 char *p;
309 p = NULL;
310 if (n)
312 p = CALLOC(1, n);
313 if (!p)
314 no_space();
316 return (p);
319 #define CREATE_FILE_NAME(dest, suffix) \
320 dest = MALLOC(len + strlen(suffix) + 1); \
321 if (dest == 0) \
322 no_space(); \
323 strcpy(dest, file_prefix); \
324 strcpy(dest + len, suffix)
326 static void
327 create_file_names(void)
329 size_t len;
330 const char *defines_suffix;
331 char *prefix;
333 prefix = NULL;
334 defines_suffix = DEFINES_SUFFIX;
336 /* compute the file_prefix from the user provided output_file_name */
337 if (output_file_name != 0)
339 if (!(prefix = strstr(output_file_name, ".tab.c"))
340 && (prefix = strstr(output_file_name, ".c")))
341 defines_suffix = ".h";
344 if (prefix != NULL)
346 len = (size_t) (prefix - output_file_name);
347 file_prefix = allocated_file_prefix = MALLOC(len + 1);
348 if (file_prefix == 0)
349 no_space();
350 strlcpy(allocated_file_prefix, output_file_name, len + 1);
352 else
353 len = strlen(file_prefix);
355 /* if "-o filename" was not given */
356 if (output_file_name == 0)
358 oflag = 1;
359 CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
362 if (rflag)
364 CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
366 else
367 code_file_name = output_file_name;
369 if (dflag)
371 if (explicit_file_name)
373 char *suffix;
374 defines_file_name = strdup(output_file_name);
375 if (defines_file_name == 0)
376 no_space();
377 /* does the output_file_name have a known suffix */
378 suffix = strrchr(output_file_name, '.');
379 if (suffix != 0 &&
380 (!strcmp(suffix, ".c") || /* good, old-fashioned C */
381 !strcmp(suffix, ".C") || /* C++, or C on Windows */
382 !strcmp(suffix, ".cc") || /* C++ */
383 !strcmp(suffix, ".cxx") || /* C++ */
384 !strcmp(suffix, ".cpp"))) /* C++ (Windows) */
386 strncpy(defines_file_name, output_file_name,
387 suffix - output_file_name + 1);
388 defines_file_name[suffix - output_file_name + 1] = 'h';
389 defines_file_name[suffix - output_file_name + 2] = 0;
390 } else {
391 fprintf(stderr,"%s: suffix of output file name %s"
392 " not recognized, no -d file generated.\n",
393 myname, output_file_name);
394 dflag = 0;
395 free(defines_file_name);
396 defines_file_name = 0;
398 } else {
399 CREATE_FILE_NAME(defines_file_name, defines_suffix);
403 if (vflag)
405 CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
408 if (gflag)
410 CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
413 if (allocated_file_prefix != NULL)
415 FREE(allocated_file_prefix);
419 static void
420 open_files(void)
422 create_file_names();
424 if (input_file == 0)
426 input_file = fopen(input_file_name, "r");
427 if (input_file == 0)
428 open_error(input_file_name);
431 action_file = tmpfile();
432 if (action_file == 0)
433 open_error("action_file");
435 text_file = tmpfile();
436 if (text_file == 0)
437 open_error("text_file");
439 if (vflag)
441 verbose_file = fopen(verbose_file_name, "w");
442 if (verbose_file == 0)
443 open_error(verbose_file_name);
446 if (gflag)
448 graph_file = fopen(graph_file_name, "w");
449 if (graph_file == 0)
450 open_error(graph_file_name);
451 fprintf(graph_file, "digraph %s {\n", file_prefix);
452 fprintf(graph_file, "\tedge [fontsize=10];\n");
453 fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
454 fprintf(graph_file, "\torientation=landscape;\n");
455 fprintf(graph_file, "\trankdir=LR;\n");
456 fprintf(graph_file, "\t/*\n");
457 fprintf(graph_file, "\tmargin=0.2;\n");
458 fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
459 fprintf(graph_file, "\tratio=auto;\n");
460 fprintf(graph_file, "\t*/\n");
463 if (dflag)
465 defines_file = fopen(defines_file_name, "w");
466 if (defines_file == 0)
467 open_error(defines_file_name);
468 union_file = tmpfile();
469 if (union_file == 0)
470 open_error("union_file");
473 output_file = fopen(output_file_name, "w");
474 if (output_file == 0)
475 open_error(output_file_name);
477 if (rflag)
479 code_file = fopen(code_file_name, "w");
480 if (code_file == 0)
481 open_error(code_file_name);
483 else
484 code_file = output_file;
488 main(int argc, char *argv[])
490 SRexpect = -1;
491 RRexpect = -1;
492 exit_code = EXIT_SUCCESS;
494 set_signals();
495 getargs(argc, argv);
496 open_files();
497 reader();
498 lr0();
499 lalr();
500 make_parser();
501 graph();
502 finalize_closure();
503 verbose();
504 output();
505 done(exit_code);
506 /*NOTREACHED */