1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <get-errno.h>
33 /* From basename.c. Almost a lie, as it returns a char *. */
34 const char *base_name (char const *name
);
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 *spec_outfile
= NULL
; /* for -o. */
49 char *spec_file_prefix
= NULL
; /* for -b. */
50 const char *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
= NULL
;
56 uniqstr grammar_file
= NULL
;
57 uniqstr current_file
= NULL
;
59 static char *full_base_name
= NULL
;
61 /* Prefix used to generate output file names. */
62 char *short_base_name
= NULL
;
64 /* C source file extension (the parser source). */
65 const char *src_extension
= NULL
;
66 /* Header file extension (if option ``-d'' is specified). */
67 const char *header_extension
= NULL
;
69 /*-----------------------------------------------------------------.
70 | Return a newly allocated string composed of the concatenation of |
72 `-----------------------------------------------------------------*/
75 concat2 (char const *str1
, char const *str2
)
77 size_t len
= strlen (str1
) + strlen (str2
);
78 char *res
= XMALLOC (char, len
+ 1);
80 cp
= stpcpy (res
, str1
);
81 cp
= stpcpy (cp
, str2
);
85 /*-----------------------------------------------------------------.
86 | Try to open file NAME with mode MODE, and print an error message |
88 `-----------------------------------------------------------------*/
91 xfopen (const char *name
, const char *mode
)
95 ptr
= fopen (name
, mode
);
97 error (EXIT_FAILURE
, get_errno (), _("cannot open file `%s'"), name
);
102 /*-------------------------------------------------------------.
103 | Try to close file PTR, and print an error message if fails. |
104 `-------------------------------------------------------------*/
113 error (EXIT_FAILURE
, 0, _("I/O error"));
115 if (fclose (ptr
) != 0)
116 error (EXIT_FAILURE
, get_errno (), _("cannot close file"));
120 /*----------------------------------------------------------------.
121 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
122 `----------------------------------------------------------------*/
124 /* Replace all characters FROM by TO in the string IN.
125 and returns a new allocated string. */
127 tr (const char *in
, char from
, char to
)
132 out
= XMALLOC (char, strlen (in
) + 1);
134 for (temp
= out
; *in
; in
++, out
++)
143 /* Computes extensions from the grammar file extension. */
145 compute_exts_from_gf (const char *ext
)
147 src_extension
= tr (ext
, 'y', 'c');
148 src_extension
= tr (src_extension
, 'Y', 'C');
149 header_extension
= tr (ext
, 'y', 'h');
150 header_extension
= tr (header_extension
, 'Y', 'H');
153 /* Computes extensions from the given c source file extension. */
155 compute_exts_from_src (const char *ext
)
157 /* We use this function when the user specifies `-o' or `--output',
158 so the extenions must be computed unconditionally from the file name
159 given by this option. */
160 src_extension
= xstrdup (ext
);
161 header_extension
= tr (ext
, 'c', 'h');
162 header_extension
= tr (header_extension
, 'C', 'H');
166 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
167 part, (the directory) is ranging from FILENAME to the char before
168 *BASE, so we don't need an additional parameter.
170 *EXT points to the last period in the basename, or NULL if none.
172 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
173 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
174 cannot be equal to *BASE.
176 None are allocated, they are simply pointers to parts of FILENAME.
179 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
182 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
184 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
186 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
188 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
190 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
192 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
195 filename_split (const char *filename
,
196 const char **base
, const char **tab
, const char **ext
)
198 *base
= base_name (filename
);
200 /* Look for the extension, i.e., look for the last dot. */
201 *ext
= strrchr (*base
, '.');
204 /* If there is an exentension, check if there is a `.tab' part right
207 && (*ext
- *base
) > (int) strlen (".tab")
208 && (!strncmp (*ext
- strlen (".tab"), ".tab", strlen (".tab"))
209 || !strncmp (*ext
- strlen ("_tab"), "_tab", strlen ("_tab"))))
210 *tab
= *ext
- strlen (".tab");
214 /* FIXME: Should use xstrndup. */
217 compute_base_names (void)
219 const char *base
, *tab
, *ext
;
221 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
222 BASE_NAME and SHORT_BASE_NAME are `foo'.
224 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
225 SHORT_BASE_NAME is `foo'.
227 The precise -o name will be used for FTABLE. For other output
228 files, remove the ".c" or ".tab.c" suffix. */
231 filename_split (spec_outfile
, &base
, &tab
, &ext
);
233 /* The full base name goes up the EXT, excluding it. */
235 xstrndup (spec_outfile
,
236 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
238 /* The short base name goes up to TAB, excluding it. */
240 xstrndup (spec_outfile
,
241 (strlen (spec_outfile
)
242 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
245 compute_exts_from_src (ext
);
248 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
249 and SHORT_BASE_NAME = `foo'.
251 Construct names from it. */
254 if (spec_file_prefix
)
256 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
258 short_base_name
= xstrdup (spec_file_prefix
);
262 /* If --yacc, then the output is `y.tab.c'. */
263 short_base_name
= xstrdup ("y");
267 /* Otherwise, the short base name is computed from the input
268 grammar: `foo/bar.yy' => `bar'. */
269 filename_split (grammar_file
, &base
, &tab
, &ext
);
272 (strlen (base
) - (ext
? strlen (ext
) : 0)));
275 full_base_name
= XMALLOC (char,
276 strlen (short_base_name
)
277 + strlen (TAB_EXT
) + 1);
278 stpcpy (stpcpy (full_base_name
, short_base_name
), TAB_EXT
);
280 /* Computes the extensions from the grammar file name. */
281 filename_split (grammar_file
, &base
, &tab
, &ext
);
282 if (ext
&& !yacc_flag
)
283 compute_exts_from_gf (ext
);
287 /*-------------------------------------------------------.
288 | Close the open files, compute the output files names. |
289 `-------------------------------------------------------*/
292 compute_output_file_names (void)
294 compute_base_names ();
296 /* If not yet done. */
298 src_extension
= ".c";
299 if (!header_extension
)
300 header_extension
= ".h";
303 spec_outfile
? spec_outfile
: concat2 (full_base_name
, src_extension
);
305 /* It the defines filename if not given, we create it. */
306 if (!spec_defines_file
)
307 spec_defines_file
= concat2 (full_base_name
, header_extension
);
309 /* It the graph filename if not given, we create it. */
310 if (!spec_graph_file
)
311 spec_graph_file
= concat2 (short_base_name
, ".vcg");
313 spec_verbose_file
= concat2 (short_base_name
, OUTPUT_EXT
);