]> git.saurik.com Git - bison.git/blob - src/files.c
806b57290e0eb3ebbb617ef1df39cb41936ac589
[bison.git] / src / files.c
1 /* Open and close files for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
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/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <error.h>
25 #include <dirname.h>
26 #include <get-errno.h>
27 #include <quote.h>
28 #include <stdio-safer.h>
29 #include <xstrndup.h>
30
31 #include "complain.h"
32 #include "files.h"
33 #include "getargs.h"
34 #include "gram.h"
35
36 /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is
37 tempting, but don't do that: for the time being our handling of the
38 %directive vs --option leaves precedence to the options by deciding
39 that if a %directive sets a variable which is really set (i.e., not
40 NULL), then the %directive is ignored. As a result, %name-prefix,
41 for instance, will not be honored. */
42
43 char const *spec_outfile = NULL; /* for -o. */
44 char const *spec_file_prefix = NULL; /* for -b. */
45 char const *spec_name_prefix = NULL; /* for -p. */
46 char *spec_verbose_file = NULL; /* for --verbose. */
47 char *spec_graph_file = NULL; /* for -g. */
48 char *spec_defines_file = NULL; /* for --defines. */
49 char *parser_file_name;
50
51 /* All computed output file names. */
52 static char **file_names = NULL;
53 static int file_names_count = 0;
54
55 uniqstr grammar_file = NULL;
56 uniqstr current_file = NULL;
57
58 /* If --output=dir/foo.c was specified,
59 DIR_PREFIX is `dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are `dir/foo'.
60
61 If --output=dir/foo.tab.c was specified, DIR_PREFIX is `dir/',
62 ALL_BUT_EXT is `dir/foo.tab', and ALL_BUT_TAB_EXT is `dir/foo'.
63
64 If --output was not specified but --file-prefix=dir/foo was specified,
65 ALL_BUT_EXT = `foo.tab' and ALL_BUT_TAB_EXT = `foo'.
66
67 If neither --output nor --file was specified but the input grammar
68 is name dir/foo.y, ALL_BUT_EXT and ALL_BUT_TAB_EXT are `foo'.
69
70 If neither --output nor --file was specified, DIR_PREFIX is the
71 empty string (meaning the current directory); otherwise it is
72 `dir/'. */
73
74 char *all_but_ext;
75 static char *all_but_tab_ext;
76 char *dir_prefix;
77
78 /* C source file extension (the parser source). */
79 static char *src_extension = NULL;
80 /* Header file extension (if option ``-d'' is specified). */
81 static char *header_extension = NULL;
82 \f
83 /*-----------------------------------------------------------------.
84 | Return a newly allocated string composed of the concatenation of |
85 | STR1, and STR2. |
86 `-----------------------------------------------------------------*/
87
88 static char *
89 concat2 (char const *str1, char const *str2)
90 {
91 size_t len = strlen (str1) + strlen (str2);
92 char *res = xmalloc (len + 1);
93 char *cp;
94 cp = stpcpy (res, str1);
95 cp = stpcpy (cp, str2);
96 return res;
97 }
98
99 /*-----------------------------------------------------------------.
100 | Try to open file NAME with mode MODE, and print an error message |
101 | if fails. |
102 `-----------------------------------------------------------------*/
103
104 FILE *
105 xfopen (const char *name, const char *mode)
106 {
107 FILE *ptr;
108
109 ptr = fopen_safer (name, mode);
110 if (!ptr)
111 error (EXIT_FAILURE, get_errno (), _("cannot open file `%s'"), name);
112
113 return ptr;
114 }
115
116 /*-------------------------------------------------------------.
117 | Try to close file PTR, and print an error message if fails. |
118 `-------------------------------------------------------------*/
119
120 void
121 xfclose (FILE *ptr)
122 {
123 if (ptr == NULL)
124 return;
125
126 if (ferror (ptr))
127 error (EXIT_FAILURE, 0, _("I/O error"));
128
129 if (fclose (ptr) != 0)
130 error (EXIT_FAILURE, get_errno (), _("cannot close file"));
131 }
132 \f
133
134 /*------------------------------------------------------------------.
135 | Compute ALL_BUT_EXT, ALL_BUT_TAB_EXT and output files extensions. |
136 `------------------------------------------------------------------*/
137
138 /* In the string S, replace all characters FROM by TO. */
139 static void
140 tr (char *s, char from, char to)
141 {
142 for (; *s; s++)
143 if (*s == from)
144 *s = to;
145 }
146
147 /* Compute extensions from the grammar file extension. */
148 static void
149 compute_exts_from_gf (const char *ext)
150 {
151 if (strcmp (ext, ".y") == 0)
152 {
153 src_extension = xstrdup (language->src_extension);
154 header_extension = xstrdup (language->header_extension);
155 }
156 else
157 {
158 src_extension = xstrdup (ext);
159 header_extension = xstrdup (ext);
160 tr (src_extension, 'y', 'c');
161 tr (src_extension, 'Y', 'C');
162 tr (header_extension, 'y', 'h');
163 tr (header_extension, 'Y', 'H');
164 }
165 }
166
167 /* Compute extensions from the given c source file extension. */
168 static void
169 compute_exts_from_src (const char *ext)
170 {
171 /* We use this function when the user specifies `-o' or `--output',
172 so the extenions must be computed unconditionally from the file name
173 given by this option. */
174 src_extension = xstrdup (ext);
175 header_extension = xstrdup (ext);
176 tr (header_extension, 'c', 'h');
177 tr (header_extension, 'C', 'H');
178 }
179
180
181 /* Decompose FILE_NAME in four parts: *BASE, *TAB, and *EXT, the fourth
182 part, (the directory) is ranging from FILE_NAME to the char before
183 *BASE, so we don't need an additional parameter.
184
185 *EXT points to the last period in the basename, or NULL if none.
186
187 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
188 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
189 cannot be equal to *BASE.
190
191 None are allocated, they are simply pointers to parts of FILE_NAME.
192 Examples:
193
194 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
195 '.c'
196
197 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
198
199 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
200
201 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
202
203 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
204
205 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
206
207 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
208
209 static void
210 file_name_split (const char *file_name,
211 const char **base, const char **tab, const char **ext)
212 {
213 *base = last_component (file_name);
214
215 /* Look for the extension, i.e., look for the last dot. */
216 *ext = strrchr (*base, '.');
217 *tab = NULL;
218
219 /* If there is an extension, check if there is a `.tab' part right
220 before. */
221 if (*ext)
222 {
223 size_t baselen = *ext - *base;
224 size_t dottablen = 4;
225 if (dottablen < baselen
226 && (strncmp (*ext - dottablen, ".tab", dottablen) == 0
227 || strncmp (*ext - dottablen, "_tab", dottablen) == 0))
228 *tab = *ext - dottablen;
229 }
230 }
231
232
233 static void
234 compute_file_name_parts (void)
235 {
236 const char *base, *tab, *ext;
237
238 /* Compute ALL_BUT_EXT and ALL_BUT_TAB_EXT from SPEC_OUTFILE
239 or GRAMMAR_FILE.
240
241 The precise -o name will be used for FTABLE. For other output
242 files, remove the ".c" or ".tab.c" suffix. */
243 if (spec_outfile)
244 {
245 file_name_split (spec_outfile, &base, &tab, &ext);
246 dir_prefix = xstrndup (spec_outfile, base - spec_outfile);
247
248 /* ALL_BUT_EXT goes up the EXT, excluding it. */
249 all_but_ext =
250 xstrndup (spec_outfile,
251 (strlen (spec_outfile) - (ext ? strlen (ext) : 0)));
252
253 /* ALL_BUT_TAB_EXT goes up to TAB, excluding it. */
254 all_but_tab_ext =
255 xstrndup (spec_outfile,
256 (strlen (spec_outfile)
257 - (tab ? strlen (tab) : (ext ? strlen (ext) : 0))));
258
259 if (ext)
260 compute_exts_from_src (ext);
261 }
262 else
263 {
264 file_name_split (grammar_file, &base, &tab, &ext);
265
266 if (spec_file_prefix)
267 {
268 /* If --file-prefix=foo was specified, ALL_BUT_TAB_EXT = `foo'. */
269 dir_prefix = xstrndup (grammar_file, base - grammar_file);
270 all_but_tab_ext = xstrdup (spec_file_prefix);
271 }
272 else if (yacc_flag)
273 {
274 /* If --yacc, then the output is `y.tab.c'. */
275 dir_prefix = xstrdup ("");
276 all_but_tab_ext = xstrdup ("y");
277 }
278 else
279 {
280 /* Otherwise, ALL_BUT_TAB_EXT is computed from the input
281 grammar: `foo/bar.yy' => `bar'. */
282 dir_prefix = xstrdup ("");
283 all_but_tab_ext =
284 xstrndup (base, (strlen (base) - (ext ? strlen (ext) : 0)));
285 }
286
287 if (language->add_tab)
288 all_but_ext = concat2 (all_but_tab_ext, TAB_EXT);
289 else
290 all_but_ext = xstrdup (all_but_tab_ext);
291
292 /* Compute the extensions from the grammar file name. */
293 if (ext && !yacc_flag)
294 compute_exts_from_gf (ext);
295 }
296 }
297
298
299 /* Compute the output file names. Warn if we detect conflicting
300 outputs to the same file. */
301
302 void
303 compute_output_file_names (void)
304 {
305 compute_file_name_parts ();
306
307 /* If not yet done. */
308 if (!src_extension)
309 src_extension = xstrdup (".c");
310 if (!header_extension)
311 header_extension = xstrdup (".h");
312
313 parser_file_name =
314 (spec_outfile
315 ? xstrdup (spec_outfile)
316 : concat2 (all_but_ext, src_extension));
317
318 if (defines_flag)
319 {
320 if (! spec_defines_file)
321 spec_defines_file = concat2 (all_but_ext, header_extension);
322 }
323
324 if (graph_flag)
325 {
326 if (! spec_graph_file)
327 spec_graph_file = concat2 (all_but_tab_ext, ".dot");
328 output_file_name_check (spec_graph_file);
329 }
330
331 if (report_flag)
332 {
333 spec_verbose_file = concat2 (all_but_tab_ext, OUTPUT_EXT);
334 output_file_name_check (spec_verbose_file);
335 }
336
337 free (all_but_tab_ext);
338 free (src_extension);
339 free (header_extension);
340 }
341
342 void
343 output_file_name_check (char const *file_name)
344 {
345 {
346 int i;
347 for (i = 0; i < file_names_count; i++)
348 if (0 == strcmp (file_names[i], file_name))
349 warn (_("conflicting outputs to file %s"), quote (file_name));
350 }
351 file_names = xnrealloc (file_names, ++file_names_count, sizeof *file_names);
352 file_names[file_names_count-1] = xstrdup (file_name);
353 }
354
355 void
356 output_file_names_free (void)
357 {
358 free (all_but_ext);
359 free (spec_verbose_file);
360 free (spec_graph_file);
361 free (spec_defines_file);
362 free (parser_file_name);
363 free (dir_prefix);
364 {
365 int i;
366 for (i = 0; i < file_names_count; i++)
367 free (file_names[i]);
368 }
369 free (file_names);
370 }