1 /* Open and close files for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
27 #include <get-errno.h>
36 #include "stdio-safer.h"
38 struct obstack pre_prologue_obstack
;
39 struct obstack post_prologue_obstack
;
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. */
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 const *spec_verbose_file
= NULL
; /* for --verbose. */
52 char const *spec_graph_file
= NULL
; /* for -g. */
53 char const *spec_defines_file
= NULL
; /* for --defines. */
54 char const *parser_file_name
;
56 uniqstr grammar_file
= NULL
;
57 uniqstr current_file
= NULL
;
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'.
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'.
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'.
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'.
71 If neither --output nor --file was specified, DIR_PREFIX is the
72 empty string (meaning the current directory); otherwise it is
75 static char const *all_but_ext
;
76 static char const *all_but_tab_ext
;
77 char const *dir_prefix
;
79 /* C source file extension (the parser source). */
80 static char const *src_extension
= NULL
;
81 /* Header file extension (if option ``-d'' is specified). */
82 static char const *header_extension
= NULL
;
84 /*-----------------------------------------------------------------.
85 | Return a newly allocated string composed of the concatenation of |
87 `-----------------------------------------------------------------*/
90 concat2 (char const *str1
, char const *str2
)
92 size_t len
= strlen (str1
) + strlen (str2
);
93 char *res
= xmalloc (len
+ 1);
95 cp
= stpcpy (res
, str1
);
96 cp
= stpcpy (cp
, str2
);
100 /*-----------------------------------------------------------------.
101 | Try to open file NAME with mode MODE, and print an error message |
103 `-----------------------------------------------------------------*/
106 xfopen (const char *name
, const char *mode
)
110 ptr
= fopen_safer (name
, mode
);
112 error (EXIT_FAILURE
, get_errno (), _("cannot open file `%s'"), name
);
117 /*-------------------------------------------------------------.
118 | Try to close file PTR, and print an error message if fails. |
119 `-------------------------------------------------------------*/
128 error (EXIT_FAILURE
, 0, _("I/O error"));
130 if (fclose (ptr
) != 0)
131 error (EXIT_FAILURE
, get_errno (), _("cannot close file"));
135 /*------------------------------------------------------------------.
136 | Compute ALL_BUT_EXT, ALL_BUT_TAB_EXT and output files extensions. |
137 `------------------------------------------------------------------*/
139 /* Replace all characters FROM by TO in the string IN.
140 and returns a new allocated string. */
142 tr (const char *in
, char from
, char to
)
145 char *out
= xmalloc (strlen (in
) + 1);
147 for (temp
= out
; *in
; in
++, out
++)
156 /* Compute extensions from the grammar file extension. */
158 compute_exts_from_gf (const char *ext
)
160 src_extension
= tr (ext
, 'y', 'c');
161 src_extension
= tr (src_extension
, 'Y', 'C');
162 header_extension
= tr (ext
, 'y', 'h');
163 header_extension
= tr (header_extension
, 'Y', 'H');
166 /* Compute extensions from the given c source file extension. */
168 compute_exts_from_src (const char *ext
)
170 /* We use this function when the user specifies `-o' or `--output',
171 so the extenions must be computed unconditionally from the file name
172 given by this option. */
173 src_extension
= xstrdup (ext
);
174 header_extension
= tr (ext
, 'c', 'h');
175 header_extension
= tr (header_extension
, 'C', 'H');
179 /* Decompose FILE_NAME in four parts: *BASE, *TAB, and *EXT, the fourth
180 part, (the directory) is ranging from FILE_NAME to the char before
181 *BASE, so we don't need an additional parameter.
183 *EXT points to the last period in the basename, or NULL if none.
185 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
186 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
187 cannot be equal to *BASE.
189 None are allocated, they are simply pointers to parts of FILE_NAME.
192 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
195 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
197 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
199 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
201 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
203 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
205 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
208 file_name_split (const char *file_name
,
209 const char **base
, const char **tab
, const char **ext
)
211 *base
= base_name (file_name
);
213 /* Look for the extension, i.e., look for the last dot. */
214 *ext
= strrchr (*base
, '.');
217 /* If there is an extension, check if there is a `.tab' part right
221 size_t baselen
= *ext
- *base
;
222 size_t dottablen
= 4;
223 if (dottablen
< baselen
224 && (strncmp (*ext
- dottablen
, ".tab", dottablen
) == 0
225 || strncmp (*ext
- dottablen
, "_tab", dottablen
) == 0))
226 *tab
= *ext
- dottablen
;
232 compute_file_name_parts (void)
234 const char *base
, *tab
, *ext
;
236 /* Compute ALL_BUT_EXT and ALL_BUT_TAB_EXT from SPEC_OUTFILE
239 The precise -o name will be used for FTABLE. For other output
240 files, remove the ".c" or ".tab.c" suffix. */
243 file_name_split (spec_outfile
, &base
, &tab
, &ext
);
244 dir_prefix
= xstrndup (spec_outfile
, base
- spec_outfile
);
246 /* ALL_BUT_EXT goes up the EXT, excluding it. */
248 xstrndup (spec_outfile
,
249 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
251 /* ALL_BUT_TAB_EXT goes up to TAB, excluding it. */
253 xstrndup (spec_outfile
,
254 (strlen (spec_outfile
)
255 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
258 compute_exts_from_src (ext
);
262 file_name_split (grammar_file
, &base
, &tab
, &ext
);
264 if (spec_file_prefix
)
266 /* If --file-prefix=foo was specified, ALL_BUT_TAB_EXT =
268 dir_prefix
= xstrndup (grammar_file
, base
- grammar_file
);
269 all_but_tab_ext
= xstrdup (spec_file_prefix
);
273 /* If --yacc, then the output is `y.tab.c'. */
275 all_but_tab_ext
= "y";
279 /* Otherwise, ALL_BUT_TAB_EXT is computed from the input
280 grammar: `foo/bar.yy' => `bar'. */
284 (strlen (base
) - (ext
? strlen (ext
) : 0)));
287 all_but_ext
= concat2 (all_but_tab_ext
, TAB_EXT
);
289 /* Compute the extensions from the grammar file name. */
290 if (ext
&& !yacc_flag
)
291 compute_exts_from_gf (ext
);
296 /* Compute the output file names. Warn if we detect conflicting
297 outputs to the same file. */
300 compute_output_file_names (void)
307 compute_file_name_parts ();
309 /* If not yet done. */
311 src_extension
= ".c";
312 if (!header_extension
)
313 header_extension
= ".h";
315 name
[names
++] = parser_file_name
=
316 spec_outfile
? spec_outfile
: concat2 (all_but_ext
, src_extension
);
320 if (! spec_defines_file
)
321 spec_defines_file
= concat2 (all_but_ext
, header_extension
);
322 name
[names
++] = spec_defines_file
;
327 if (! spec_graph_file
)
328 spec_graph_file
= concat2 (all_but_tab_ext
, ".vcg");
329 name
[names
++] = spec_graph_file
;
334 spec_verbose_file
= concat2 (all_but_tab_ext
, OUTPUT_EXT
);
335 name
[names
++] = spec_verbose_file
;
338 for (j
= 0; j
< names
; j
++)
339 for (i
= 0; i
< j
; i
++)
340 if (strcmp (name
[i
], name
[j
]) == 0)
341 warn (_("conflicting outputs to file %s"), quote (name
[i
]));