1 /* $NetBSD: main.c,v 1.20 2008/07/21 14:19:28 lukem Exp $ */
4 * Copyright (c) 1989 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 #if defined(__COPYRIGHT) && !defined(lint)
41 __COPYRIGHT("@(#) Copyright (c) 1989\
42 The Regents of the University of California. All rights reserved.");
45 #if defined(__RCSID) && !defined(lint)
47 static char sccsid
[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
49 __RCSID("$NetBSD: main.c,v 1.20 2008/07/21 14:19:28 lukem Exp $");
64 const char *symbol_prefix
;
65 const char *myname
= "yacc";
71 char *action_file_name
;
73 char *defines_file_name
;
74 const char *input_file_name
= "";
75 char *output_file_name
;
77 char *union_file_name
;
78 char *verbose_file_name
;
80 FILE *action_file
; /* a temp file, used to save actions associated */
81 /* with rules until the parser is written */
82 FILE *code_file
; /* y.code.c (used when the -r option is specified) */
83 FILE *defines_file
; /* y.tab.h */
84 FILE *input_file
; /* the input file */
85 FILE *output_file
; /* y.tab.c */
86 FILE *text_file
; /* a temp file, used to save text until all */
87 /* symbols have been defined */
88 FILE *union_file
; /* a temp file, used to save the union */
89 /* definition until all symbol have been */
91 FILE *verbose_file
; /* y.output */
113 static const char *file_prefix
= "y";
114 static const char *temp_form
= "yacc.XXXXXXX";
115 static int explicit_file_name
;
118 static __dead
void onintr(int);
119 static void set_signals(void);
120 static __dead
void usage(void);
121 static void getargs(int, char *[]);
122 static void create_file_names(void);
123 static void open_files(void);
125 /* coverity[+kill] */
129 if (action_file
) { fclose(action_file
); unlink(action_file_name
); }
130 if (text_file
) { fclose(text_file
); unlink(text_file_name
); }
131 if (union_file
) { fclose(union_file
); unlink(union_file_name
); }
147 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
148 signal(SIGINT
, onintr
);
151 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
152 signal(SIGTERM
, onintr
);
155 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
156 signal(SIGHUP
, onintr
);
164 fprintf(stderr
, "usage: %s [-dlrtv] [-b file_prefix] [-o outputfile] "
165 "[-p symbol_prefix] filename\n", myname
);
171 getargs(int argc
, char *argv
[])
176 if (argc
> 0) myname
= argv
[0];
177 for (i
= 1; i
< argc
; ++i
)
180 if (*s
!= '-') break;
185 if (i
+ 1 < argc
) usage();
190 goto no_more_options
;
196 file_prefix
= argv
[i
];
211 output_file_name
= s
;
213 output_file_name
= argv
[i
];
216 explicit_file_name
= 1;
223 symbol_prefix
= argv
[i
];
279 if (i
+ 1 != argc
) usage();
280 input_file_name
= argv
[i
];
300 create_file_names(void)
305 tmpdir
= getenv("TMPDIR");
306 if (tmpdir
== 0) tmpdir
= "/tmp";
308 len
= strlen(tmpdir
);
310 if (len
&& tmpdir
[len
-1] != '/')
313 action_file_name
= MALLOC(i
);
314 if (action_file_name
== 0) no_space();
315 text_file_name
= MALLOC(i
);
316 if (text_file_name
== 0) no_space();
317 union_file_name
= MALLOC(i
);
318 if (union_file_name
== 0) no_space();
320 strlcpy(action_file_name
, tmpdir
, i
);
321 strlcpy(text_file_name
, tmpdir
, i
);
322 strlcpy(union_file_name
, tmpdir
, i
);
324 if (len
&& tmpdir
[len
- 1] != '/')
326 action_file_name
[len
] = '/';
327 text_file_name
[len
] = '/';
328 union_file_name
[len
] = '/';
332 strlcpy(action_file_name
+ len
, temp_form
, i
- len
);
333 strlcpy(text_file_name
+ len
, temp_form
, i
- len
);
334 strlcpy(union_file_name
+ len
, temp_form
, i
- len
);
336 action_file_name
[len
+ 5] = 'a';
337 text_file_name
[len
+ 5] = 't';
338 union_file_name
[len
+ 5] = 'u';
340 len
= strlen(file_prefix
);
342 if (!output_file_name
)
344 asprintf(&output_file_name
, "%s%s", file_prefix
, OUTPUT_SUFFIX
);
345 if (output_file_name
== 0)
351 asprintf(&code_file_name
, "%s%s", file_prefix
, CODE_SUFFIX
);
352 if (code_file_name
== 0)
356 code_file_name
= output_file_name
;
360 if (explicit_file_name
)
363 defines_file_name
= strdup(output_file_name
);
364 if (defines_file_name
== 0)
366 /* does the output_file_name have a known suffix */
367 suffix
= strrchr(output_file_name
, '.');
369 (!strcmp(suffix
, ".c") || /* good, old-fashioned C */
370 !strcmp(suffix
, ".C") || /* C++, or C on Windows */
371 !strcmp(suffix
, ".cc") || /* C++ */
372 !strcmp(suffix
, ".cxx") || /* C++ */
373 !strcmp(suffix
, ".cpp"))) /* C++ (Windows) */
375 strncpy(defines_file_name
, output_file_name
,
376 suffix
- output_file_name
+ 1);
377 defines_file_name
[suffix
- output_file_name
+ 1] = 'h';
378 defines_file_name
[suffix
- output_file_name
+ 2] = 0;
380 fprintf(stderr
,"%s: suffix of output file name %s"
381 " not recognized, no -d file generated.\n",
382 myname
, output_file_name
);
384 free(defines_file_name
);
385 defines_file_name
= 0;
390 asprintf(&defines_file_name
, "%s%s", file_prefix
, DEFINES_SUFFIX
);
391 if (defines_file_name
== 0)
398 asprintf(&verbose_file_name
, "%s%s", file_prefix
, VERBOSE_SUFFIX
);
399 if (verbose_file_name
== 0)
414 input_file
= fopen(input_file_name
, "r");
416 open_error(input_file_name
);
419 if (((fd
= mkstemp(action_file_name
)) == -1) ||
420 (action_file
= fdopen(fd
, "w")) == NULL
)
421 open_error(action_file_name
);
423 if (((fd
= mkstemp(text_file_name
)) == -1) ||
424 (text_file
= fdopen(fd
, "w")) == NULL
)
425 open_error(text_file_name
);
429 verbose_file
= fopen(verbose_file_name
, "w");
430 if (verbose_file
== 0)
431 open_error(verbose_file_name
);
436 defines_file
= fopen(defines_file_name
, "w");
437 if (defines_file
== 0)
438 open_error(defines_file_name
);
439 if (((fd
= mkstemp(union_file_name
)) == -1) ||
440 (union_file
= fdopen(fd
, "w")) == NULL
)
441 open_error(union_file_name
);
444 output_file
= fopen(output_file_name
, "w");
445 if (output_file
== 0)
446 open_error(output_file_name
);
450 code_file
= fopen(code_file_name
, "w");
452 open_error(code_file_name
);
455 code_file
= output_file
;
460 main(int argc
, char *argv
[])