1 /* Open and close files for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004
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., 59 Temple Place - Suite 330, Boston, MA
27 #include <get-errno.h>
36 /* From basename.c. Almost a lie, as it returns a char *. */
37 const char *base_name (char const *name
);
41 struct obstack pre_prologue_obstack
;
42 struct obstack post_prologue_obstack
;
44 /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is
45 tempting, but don't do that: for the time being our handling of the
46 %directive vs --option leaves precedence to the options by deciding
47 that if a %directive sets a variable which is really set (i.e., not
48 NULL), then the %directive is ignored. As a result, %name-prefix,
49 for instance, will not be honored. */
51 char *spec_outfile
= NULL
; /* for -o. */
52 char *spec_file_prefix
= NULL
; /* for -b. */
53 const char *spec_name_prefix
= NULL
; /* for -p. */
54 char *spec_verbose_file
= NULL
; /* for --verbose. */
55 char *spec_graph_file
= NULL
; /* for -g. */
56 char *spec_defines_file
= NULL
; /* for --defines. */
57 char *parser_file_name
= NULL
;
59 uniqstr grammar_file
= NULL
;
60 uniqstr current_file
= NULL
;
62 static char *full_base_name
= NULL
;
64 /* Prefix used to generate output file names. */
65 char *short_base_name
= NULL
;
67 /* C source file extension (the parser source). */
68 const char *src_extension
= NULL
;
69 /* Header file extension (if option ``-d'' is specified). */
70 const char *header_extension
= NULL
;
72 /*-----------------------------------------------------------------.
73 | Return a newly allocated string composed of the concatenation of |
75 `-----------------------------------------------------------------*/
78 concat2 (char const *str1
, char const *str2
)
80 size_t len
= strlen (str1
) + strlen (str2
);
81 char *res
= xmalloc (len
+ 1);
83 cp
= stpcpy (res
, str1
);
84 cp
= stpcpy (cp
, str2
);
88 /*-----------------------------------------------------------------.
89 | Try to open file NAME with mode MODE, and print an error message |
91 `-----------------------------------------------------------------*/
94 xfopen (const char *name
, const char *mode
)
98 ptr
= fopen (name
, mode
);
100 error (EXIT_FAILURE
, get_errno (), _("cannot open file `%s'"), name
);
105 /*-------------------------------------------------------------.
106 | Try to close file PTR, and print an error message if fails. |
107 `-------------------------------------------------------------*/
116 error (EXIT_FAILURE
, 0, _("I/O error"));
118 if (fclose (ptr
) != 0)
119 error (EXIT_FAILURE
, get_errno (), _("cannot close file"));
123 /*---------------------------------------------------------------------.
124 | Compute FULL_BASE_NAME, SHORT_BASE_NAME and output files extensions. |
125 `---------------------------------------------------------------------*/
127 /* Replace all characters FROM by TO in the string IN.
128 and returns a new allocated string. */
130 tr (const char *in
, char from
, char to
)
133 char *out
= xmalloc (strlen (in
) + 1);
135 for (temp
= out
; *in
; in
++, out
++)
144 /* Compute extensions from the grammar file extension. */
146 compute_exts_from_gf (const char *ext
)
148 src_extension
= tr (ext
, 'y', 'c');
149 src_extension
= tr (src_extension
, 'Y', 'C');
150 header_extension
= tr (ext
, 'y', 'h');
151 header_extension
= tr (header_extension
, 'Y', 'H');
154 /* Compute extensions from the given c source file extension. */
156 compute_exts_from_src (const char *ext
)
158 /* We use this function when the user specifies `-o' or `--output',
159 so the extenions must be computed unconditionally from the file name
160 given by this option. */
161 src_extension
= xstrdup (ext
);
162 header_extension
= tr (ext
, 'c', 'h');
163 header_extension
= tr (header_extension
, 'C', 'H');
167 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
168 part, (the directory) is ranging from FILENAME to the char before
169 *BASE, so we don't need an additional parameter.
171 *EXT points to the last period in the basename, or NULL if none.
173 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
174 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
175 cannot be equal to *BASE.
177 None are allocated, they are simply pointers to parts of FILENAME.
180 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
183 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
185 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
187 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
189 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
191 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
193 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
196 filename_split (const char *filename
,
197 const char **base
, const char **tab
, const char **ext
)
199 *base
= base_name (filename
);
201 /* Look for the extension, i.e., look for the last dot. */
202 *ext
= strrchr (*base
, '.');
205 /* If there is an extension, check if there is a `.tab' part right
209 size_t baselen
= *ext
- *base
;
210 size_t dottablen
= 4;
211 if (dottablen
< baselen
212 && (strncmp (*ext
- dottablen
, ".tab", dottablen
) == 0
213 || strncmp (*ext
- dottablen
, "_tab", dottablen
) == 0))
214 *tab
= *ext
- dottablen
;
219 /* FIXME: Should use xstrndup. */
222 compute_base_names (void)
224 const char *base
, *tab
, *ext
;
226 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
227 FULL_BASE_NAME and SHORT_BASE_NAME are `foo'.
229 If --output=foo.tab.c was specified, FULL_BASE_NAME is `foo.tab'
230 and SHORT_BASE_NAME is `foo'.
232 The precise -o name will be used for FTABLE. For other output
233 files, remove the ".c" or ".tab.c" suffix. */
236 filename_split (spec_outfile
, &base
, &tab
, &ext
);
238 /* The full base name goes up the EXT, excluding it. */
240 xstrndup (spec_outfile
,
241 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
243 /* The short base name goes up to TAB, excluding it. */
245 xstrndup (spec_outfile
,
246 (strlen (spec_outfile
)
247 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
250 compute_exts_from_src (ext
);
253 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
254 and SHORT_BASE_NAME = `foo'.
256 Construct names from it. */
259 if (spec_file_prefix
)
261 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
263 short_base_name
= xstrdup (spec_file_prefix
);
267 /* If --yacc, then the output is `y.tab.c'. */
268 short_base_name
= xstrdup ("y");
272 /* Otherwise, the short base name is computed from the input
273 grammar: `foo/bar.yy' => `bar'. */
274 filename_split (grammar_file
, &base
, &tab
, &ext
);
277 (strlen (base
) - (ext
? strlen (ext
) : 0)));
280 full_base_name
= xmalloc (strlen (short_base_name
)
281 + strlen (TAB_EXT
) + 1);
282 stpcpy (stpcpy (full_base_name
, short_base_name
), TAB_EXT
);
284 /* Compute the extensions from the grammar file name. */
285 filename_split (grammar_file
, &base
, &tab
, &ext
);
286 if (ext
&& !yacc_flag
)
287 compute_exts_from_gf (ext
);
292 /* Compute the output file names. Warn if we detect conflicting
293 outputs to the same file. */
296 compute_output_file_names (void)
303 compute_base_names ();
305 /* If not yet done. */
307 src_extension
= ".c";
308 if (!header_extension
)
309 header_extension
= ".h";
311 name
[names
++] = parser_file_name
=
312 spec_outfile
? spec_outfile
: concat2 (full_base_name
, src_extension
);
316 if (! spec_defines_file
)
317 spec_defines_file
= concat2 (full_base_name
, header_extension
);
318 name
[names
++] = spec_defines_file
;
323 if (! spec_graph_file
)
324 spec_graph_file
= concat2 (short_base_name
, ".vcg");
325 name
[names
++] = spec_graph_file
;
330 spec_verbose_file
= concat2 (short_base_name
, OUTPUT_EXT
);
331 name
[names
++] = spec_verbose_file
;
334 for (j
= 0; j
< names
; j
++)
335 for (i
= 0; i
< j
; i
++)
336 if (strcmp (name
[i
], name
[j
]) == 0)
337 warn (_("conflicting outputs to file %s"), quote (name
[i
]));