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 */
7 __RCSID("$NetBSD: main.c,v 1.2 2009/10/29 00:56:20 christos Exp $");
10 #include <unistd.h> /* for _exit() */
21 const char *symbol_prefix
;
22 const char *myname
= "yacc";
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
;
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 */
52 FILE *verbose_file
; /* y.output */
53 FILE *graph_file
; /* y.dot */
64 Value_t
*symbol_value
;
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) \
89 static int got_intr
= 0;
95 DO_CLOSE(output_file
);
97 DO_CLOSE(action_file
);
98 DO_CLOSE(defines_file
);
101 DO_CLOSE(union_file
);
102 DO_CLOSE(verbose_file
);
109 DO_FREE(code_file_name
);
112 DO_FREE(defines_file_name
);
115 DO_FREE(output_file_name
);
118 DO_FREE(verbose_file_name
);
121 DO_FREE(graph_file_name
);
134 onintr(int sig GCC_UNUSED
)
144 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
145 signal(SIGINT
, onintr
);
148 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
149 signal(SIGTERM
, onintr
);
152 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
153 signal(SIGHUP
, onintr
);
160 static const char *msg
[] =
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"
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
]);
215 printf("%s - %s\n", myname
, VERSION
);
224 getargs(int argc
, char *argv
[])
233 for (i
= 1; i
< argc
; ++i
)
248 goto no_more_options
;
254 file_prefix
= argv
[i
];
261 output_file_name
= s
;
263 output_file_name
= argv
[i
];
266 explicit_file_name
= 1;
273 symbol_prefix
= argv
[i
];
301 input_file_name
= argv
[i
];
319 #define CREATE_FILE_NAME(dest, suffix) \
320 dest = MALLOC(len + strlen(suffix) + 1); \
323 strcpy(dest, file_prefix); \
324 strcpy(dest + len, suffix)
327 create_file_names(void)
330 const char *defines_suffix
;
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";
346 len
= (size_t) (prefix
- output_file_name
);
347 file_prefix
= allocated_file_prefix
= MALLOC(len
+ 1);
348 if (file_prefix
== 0)
350 strlcpy(allocated_file_prefix
, output_file_name
, len
+ 1);
353 len
= strlen(file_prefix
);
355 /* if "-o filename" was not given */
356 if (output_file_name
== 0)
359 CREATE_FILE_NAME(output_file_name
, OUTPUT_SUFFIX
);
364 CREATE_FILE_NAME(code_file_name
, CODE_SUFFIX
);
367 code_file_name
= output_file_name
;
371 if (explicit_file_name
)
374 defines_file_name
= strdup(output_file_name
);
375 if (defines_file_name
== 0)
377 /* does the output_file_name have a known suffix */
378 suffix
= strrchr(output_file_name
, '.');
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;
391 fprintf(stderr
,"%s: suffix of output file name %s"
392 " not recognized, no -d file generated.\n",
393 myname
, output_file_name
);
395 free(defines_file_name
);
396 defines_file_name
= 0;
399 CREATE_FILE_NAME(defines_file_name
, defines_suffix
);
405 CREATE_FILE_NAME(verbose_file_name
, VERBOSE_SUFFIX
);
410 CREATE_FILE_NAME(graph_file_name
, GRAPH_SUFFIX
);
413 if (allocated_file_prefix
!= NULL
)
415 FREE(allocated_file_prefix
);
426 input_file
= fopen(input_file_name
, "r");
428 open_error(input_file_name
);
431 action_file
= tmpfile();
432 if (action_file
== 0)
433 open_error("action_file");
435 text_file
= tmpfile();
437 open_error("text_file");
441 verbose_file
= fopen(verbose_file_name
, "w");
442 if (verbose_file
== 0)
443 open_error(verbose_file_name
);
448 graph_file
= fopen(graph_file_name
, "w");
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");
465 defines_file
= fopen(defines_file_name
, "w");
466 if (defines_file
== 0)
467 open_error(defines_file_name
);
468 union_file
= tmpfile();
470 open_error("union_file");
473 output_file
= fopen(output_file_name
, "w");
474 if (output_file
== 0)
475 open_error(output_file_name
);
479 code_file
= fopen(code_file_name
, "w");
481 open_error(code_file_name
);
484 code_file
= output_file
;
488 main(int argc
, char *argv
[])
492 exit_code
= EXIT_SUCCESS
;