]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2012 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include <get-errno.h>
29 #include <stdio-safer.h>
37 /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is
38 tempting, but don't do that: for the time being our handling of the
39 %directive vs --option leaves precedence to the options by deciding
40 that if a %directive sets a variable which is really set (i.e., not
41 NULL), then the %directive is ignored. As a result, %name-prefix,
42 for instance, will not be honored. */
44 char const *spec_outfile
= NULL
; /* for -o. */
45 char const *spec_file_prefix
= NULL
; /* for -b. */
46 char const *spec_name_prefix
= NULL
; /* for -p. */
47 char *spec_verbose_file
= NULL
; /* for --verbose. */
48 char *spec_graph_file
= NULL
; /* for -g. */
49 char *spec_xml_file
= NULL
; /* for -x. */
50 char *spec_defines_file
= NULL
; /* for --defines. */
51 char *parser_file_name
;
53 /* All computed output file names. */
54 static char **file_names
= NULL
;
55 static int file_names_count
= 0;
57 uniqstr grammar_file
= NULL
;
58 uniqstr current_file
= NULL
;
60 /* If --output=dir/foo.c was specified,
61 DIR_PREFIX is `dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are `dir/foo'.
63 If --output=dir/foo.tab.c was specified, DIR_PREFIX is `dir/',
64 ALL_BUT_EXT is `dir/foo.tab', and ALL_BUT_TAB_EXT is `dir/foo'.
66 If --output was not specified but --file-prefix=dir/foo was specified,
67 ALL_BUT_EXT = `foo.tab' and ALL_BUT_TAB_EXT = `foo'.
69 If neither --output nor --file was specified but the input grammar
70 is name dir/foo.y, ALL_BUT_EXT and ALL_BUT_TAB_EXT are `foo'.
72 If neither --output nor --file was specified, DIR_PREFIX is the
73 empty string (meaning the current directory); otherwise it is
77 static char *all_but_tab_ext
;
80 /* C source file extension (the parser source). */
81 static char *src_extension
= NULL
;
82 /* Header file extension (if option ``-d'' is specified). */
83 static char *header_extension
= NULL
;
85 /*-----------------------------------------------------------------.
86 | Return a newly allocated string composed of the concatenation of |
88 `-----------------------------------------------------------------*/
91 concat2 (char const *str1
, char const *str2
)
93 size_t len
= strlen (str1
) + strlen (str2
);
94 char *res
= xmalloc (len
+ 1);
96 cp
= stpcpy (res
, str1
);
97 cp
= stpcpy (cp
, str2
);
101 /*-----------------------------------------------------------------.
102 | Try to open file NAME with mode MODE, and print an error message |
104 `-----------------------------------------------------------------*/
107 xfopen (const char *name
, const char *mode
)
111 ptr
= fopen_safer (name
, mode
);
113 error (EXIT_FAILURE
, get_errno (),
114 _("%s: cannot open"), quotearg_colon (name
));
119 /*-------------------------------------------------------------.
120 | Try to close file PTR, and print an error message if fails. |
121 `-------------------------------------------------------------*/
130 error (EXIT_FAILURE
, 0, _("input/output error"));
132 if (fclose (ptr
) != 0)
133 error (EXIT_FAILURE
, get_errno (), _("cannot close file"));
138 xfdopen (int fd
, char const *mode
)
140 FILE *res
= fdopen (fd
, mode
);
142 error (EXIT_FAILURE
, get_errno (), "fdopen");
146 /*------------------------------------------------------------------.
147 | Compute ALL_BUT_EXT, ALL_BUT_TAB_EXT and output files extensions. |
148 `------------------------------------------------------------------*/
150 /* Compute extensions from the grammar file extension. */
152 compute_exts_from_gf (const char *ext
)
154 if (STREQ (ext
, ".y"))
156 src_extension
= xstrdup (language
->src_extension
);
157 header_extension
= xstrdup (language
->header_extension
);
161 src_extension
= xstrdup (ext
);
162 header_extension
= xstrdup (ext
);
163 tr (src_extension
, 'y', 'c');
164 tr (src_extension
, 'Y', 'C');
165 tr (header_extension
, 'y', 'h');
166 tr (header_extension
, 'Y', 'H');
170 /* Compute extensions from the given c source file extension. */
172 compute_exts_from_src (const char *ext
)
174 /* We use this function when the user specifies `-o' or `--output',
175 so the extenions must be computed unconditionally from the file name
176 given by this option. */
177 src_extension
= xstrdup (ext
);
178 header_extension
= xstrdup (ext
);
179 tr (header_extension
, 'c', 'h');
180 tr (header_extension
, 'C', 'H');
184 /* Decompose FILE_NAME in four parts: *BASE, *TAB, and *EXT, the fourth
185 part, (the directory) is ranging from FILE_NAME to the char before
186 *BASE, so we don't need an additional parameter.
188 *EXT points to the last period in the basename, or NULL if none.
190 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
191 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
192 cannot be equal to *BASE.
194 None are allocated, they are simply pointers to parts of FILE_NAME.
197 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
200 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
202 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
204 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
206 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
208 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
210 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
213 file_name_split (const char *file_name
,
214 const char **base
, const char **tab
, const char **ext
)
216 *base
= last_component (file_name
);
218 /* Look for the extension, i.e., look for the last dot. */
219 *ext
= strrchr (*base
, '.');
222 /* If there is an extension, check if there is a `.tab' part right
226 size_t baselen
= *ext
- *base
;
227 size_t dottablen
= sizeof (TAB_EXT
) - 1;
228 if (dottablen
< baselen
229 && STRPREFIX_LIT (TAB_EXT
, *ext
- dottablen
))
230 *tab
= *ext
- dottablen
;
236 compute_file_name_parts (void)
238 const char *base
, *tab
, *ext
;
240 /* Compute ALL_BUT_EXT and ALL_BUT_TAB_EXT from SPEC_OUTFILE
243 The precise -o name will be used for FTABLE. For other output
244 files, remove the ".c" or ".tab.c" suffix. */
247 file_name_split (spec_outfile
, &base
, &tab
, &ext
);
248 dir_prefix
= xstrndup (spec_outfile
, base
- spec_outfile
);
250 /* ALL_BUT_EXT goes up the EXT, excluding it. */
252 xstrndup (spec_outfile
,
253 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
255 /* ALL_BUT_TAB_EXT goes up to TAB, excluding it. */
257 xstrndup (spec_outfile
,
258 (strlen (spec_outfile
)
259 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
262 compute_exts_from_src (ext
);
266 file_name_split (grammar_file
, &base
, &tab
, &ext
);
268 if (spec_file_prefix
)
270 /* If --file-prefix=foo was specified, ALL_BUT_TAB_EXT = `foo'. */
272 xstrndup (spec_file_prefix
,
273 last_component (spec_file_prefix
) - spec_file_prefix
);
274 all_but_tab_ext
= xstrdup (spec_file_prefix
);
278 /* If --yacc, then the output is `y.tab.c'. */
279 dir_prefix
= xstrdup ("");
280 all_but_tab_ext
= xstrdup ("y");
284 /* Otherwise, ALL_BUT_TAB_EXT is computed from the input
285 grammar: `foo/bar.yy' => `bar'. */
286 dir_prefix
= xstrdup ("");
288 xstrndup (base
, (strlen (base
) - (ext
? strlen (ext
) : 0)));
291 if (language
->add_tab
)
292 all_but_ext
= concat2 (all_but_tab_ext
, TAB_EXT
);
294 all_but_ext
= xstrdup (all_but_tab_ext
);
296 /* Compute the extensions from the grammar file name. */
297 if (ext
&& !yacc_flag
)
298 compute_exts_from_gf (ext
);
303 /* Compute the output file names. Warn if we detect conflicting
304 outputs to the same file. */
307 compute_output_file_names (void)
309 compute_file_name_parts ();
311 /* If not yet done. */
313 src_extension
= xstrdup (".c");
314 if (!header_extension
)
315 header_extension
= xstrdup (".h");
319 ? xstrdup (spec_outfile
)
320 : concat2 (all_but_ext
, src_extension
));
324 if (! spec_defines_file
)
325 spec_defines_file
= concat2 (all_but_ext
, header_extension
);
330 if (! spec_graph_file
)
331 spec_graph_file
= concat2 (all_but_tab_ext
, ".dot");
332 output_file_name_check (&spec_graph_file
);
338 spec_xml_file
= concat2 (all_but_tab_ext
, ".xml");
339 output_file_name_check (&spec_xml_file
);
344 if (!spec_verbose_file
)
345 spec_verbose_file
= concat2 (all_but_tab_ext
, OUTPUT_EXT
);
346 output_file_name_check (&spec_verbose_file
);
349 free (all_but_tab_ext
);
350 free (src_extension
);
351 free (header_extension
);
355 output_file_name_check (char **file_name
)
357 bool conflict
= false;
358 if (STREQ (*file_name
, grammar_file
))
360 complain (_("refusing to overwrite the input file %s"),
367 for (i
= 0; i
< file_names_count
; i
++)
368 if (STREQ (file_names
[i
], *file_name
))
370 warn (_("conflicting outputs to file %s"),
378 *file_name
= strdup ("/dev/null");
382 file_names
= xnrealloc (file_names
, ++file_names_count
,
384 file_names
[file_names_count
-1] = xstrdup (*file_name
);
389 output_file_names_free (void)
392 free (spec_verbose_file
);
393 free (spec_graph_file
);
394 free (spec_xml_file
);
395 free (spec_defines_file
);
396 free (parser_file_name
);
400 for (i
= 0; i
< file_names_count
; i
++)
401 free (file_names
[i
]);