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