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