]> git.saurik.com Git - bison.git/blob - src/files.c
(yylineno, yyoutname): Remove static vars, replacing
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
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)
10 any later version.
11
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.
16
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
20 02111-1307, USA. */
21
22
23 #include "system.h"
24 #include "getargs.h"
25 #include "files.h"
26 #include "gram.h"
27 #include "error.h"
28 #include "complain.h"
29
30 /* From basename.c. Almost a lie, as it returns a char *. */
31 const char *base_name (char const *name);
32
33 FILE *finput = NULL;
34
35 struct obstack pre_prologue_obstack;
36 struct obstack post_prologue_obstack;
37
38 /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is
39 tempting, but don't do that: for the time being our handling of the
40 %directive vs --option leaves precedence to the options by deciding
41 that if a %directive sets a variable which is really set (i.e., not
42 NULL), then the %directive is ignored. As a result, %name-prefix,
43 for instance, will not be honored. */
44
45 char *spec_outfile = NULL; /* for -o. */
46 char *spec_file_prefix = NULL; /* for -b. */
47 const char *spec_name_prefix = NULL; /* for -p. */
48 char *spec_verbose_file = NULL; /* for --verbose. */
49 char *spec_graph_file = NULL; /* for -g. */
50 char *spec_defines_file = NULL; /* for --defines. */
51 char *parser_file_name = NULL;
52
53 char *infile = NULL;
54
55 static char *full_base_name = NULL;
56
57 /* Prefix used to generate output file names. */
58 char *short_base_name = NULL;
59
60 /* C source file extension (the parser source). */
61 const char *src_extension = NULL;
62 /* Header file extension (if option ``-d'' is specified). */
63 const char *header_extension = NULL;
64 \f
65
66 /*--------------------------.
67 | Is SUFFIX ending STRING? |
68 `--------------------------*/
69
70 int
71 strsuffix (const char *string, const char *suffix)
72 {
73 size_t string_len = strlen (string);
74 size_t suffix_len = strlen (suffix);
75 if (suffix_len <= string_len)
76 return !strcmp (string + string_len - suffix_len, suffix);
77 else
78 return 0;
79 }
80
81
82 /*-----------------------------------------------------------------.
83 | Return a newly allocated string composed of the concatenation of |
84 | STRING1, and STRING2. |
85 `-----------------------------------------------------------------*/
86
87 char*
88 stringappend (const char *string1, const char *string2)
89 {
90 size_t len = strlen (string1) + strlen (string2);
91 char *res = XMALLOC (char, len + 1);
92 char *cp;
93 cp = stpcpy (res, string1);
94 cp = stpcpy (cp, string2);
95 return res;
96 }
97
98 /*-----------------------------------------------------------------.
99 | Try to open file NAME with mode MODE, and print an error message |
100 | if fails. |
101 `-----------------------------------------------------------------*/
102
103 FILE *
104 xfopen (const char *name, const char *mode)
105 {
106 FILE *ptr;
107
108 ptr = fopen (name, mode);
109 if (!ptr)
110 error (EXIT_FAILURE, errno, _("cannot open file `%s'"), name);
111
112 return ptr;
113 }
114
115 /*-------------------------------------------------------------.
116 | Try to close file PTR, and print an error message if fails. |
117 `-------------------------------------------------------------*/
118
119 void
120 xfclose (FILE *ptr)
121 {
122 if (ptr == NULL)
123 return;
124
125 if (ferror (ptr))
126 error (EXIT_FAILURE, 0, _("I/O error"));
127
128 if (fclose (ptr) != 0)
129 error (EXIT_FAILURE, errno, _("cannot close file"));
130 }
131 \f
132
133 /*----------------------------------------------------------------.
134 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
135 `----------------------------------------------------------------*/
136
137 /* Replace all characters FROM by TO in the string IN.
138 and returns a new allocated string. */
139 static char *
140 tr (const char *in, char from, char to)
141 {
142 char *temp;
143 char *out;
144
145 out = XMALLOC (char, strlen (in) + 1);
146
147 for (temp = out; *in; in++, out++)
148 if (*in == from)
149 *out = to;
150 else
151 *out = *in;
152 *out = 0;
153 return (temp);
154 }
155
156 /* Computes extensions from the grammar file extension. */
157 static void
158 compute_exts_from_gf (const char *ext)
159 {
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');
164 }
165
166 /* Computes extensions from the given c source file extension. */
167 static void
168 compute_exts_from_src (const char *ext)
169 {
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');
176 }
177
178
179 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
180 part, (the directory) is ranging from FILENAME to the char before
181 *BASE, so we don't need an additional parameter.
182
183 *EXT points to the last period in the basename, or NULL if none.
184
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.
188
189 None are allocated, they are simply pointers to parts of FILENAME.
190 Examples:
191
192 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
193 '.c'
194
195 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
196
197 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
198
199 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
200
201 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
202
203 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
204
205 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
206
207 static void
208 filename_split (const char *filename,
209 const char **base, const char **tab, const char **ext)
210 {
211 *base = base_name (filename);
212
213 /* Look for the extension, i.e., look for the last dot. */
214 *ext = strrchr (*base, '.');
215 *tab = NULL;
216
217 /* If there is an exentension, check if there is a `.tab' part right
218 before. */
219 if (*ext
220 && (*ext - *base) > (int) strlen (".tab")
221 && (!strncmp (*ext - strlen (".tab"), ".tab", strlen (".tab"))
222 || !strncmp (*ext - strlen ("_tab"), "_tab", strlen ("_tab"))))
223 *tab = *ext - strlen (".tab");
224 }
225
226
227 /* FIXME: Should use xstrndup. */
228
229 static void
230 compute_base_names (void)
231 {
232 const char *base, *tab, *ext;
233
234 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
235 BASE_NAME and SHORT_BASE_NAME are `foo'.
236
237 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
238 SHORT_BASE_NAME is `foo'.
239
240 The precise -o name will be used for FTABLE. For other output
241 files, remove the ".c" or ".tab.c" suffix. */
242 if (spec_outfile)
243 {
244 filename_split (spec_outfile, &base, &tab, &ext);
245
246 /* The full base name goes up the EXT, excluding it. */
247 full_base_name =
248 xstrndup (spec_outfile,
249 (strlen (spec_outfile) - (ext ? strlen (ext) : 0)));
250
251 /* The short base name goes up to TAB, excluding it. */
252 short_base_name =
253 xstrndup (spec_outfile,
254 (strlen (spec_outfile)
255 - (tab ? strlen (tab) : (ext ? strlen (ext) : 0))));
256
257 if (ext)
258 compute_exts_from_src (ext);
259 }
260
261 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
262 and SHORT_BASE_NAME = `foo'.
263
264 Construct names from it. */
265 else
266 {
267 if (spec_file_prefix)
268 {
269 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
270 `foo'. */
271 short_base_name = xstrdup (spec_file_prefix);
272 }
273 else if (yacc_flag)
274 {
275 /* If --yacc, then the output is `y.tab.c'. */
276 short_base_name = xstrdup ("y");
277 }
278 else
279 {
280 /* Otherwise, the short base name is computed from the input
281 grammar: `foo/bar.yy' => `bar'. */
282 filename_split (infile, &base, &tab, &ext);
283 short_base_name =
284 xstrndup (base,
285 (strlen (base) - (ext ? strlen (ext) : 0)));
286 }
287
288 full_base_name = XMALLOC (char,
289 strlen (short_base_name)
290 + strlen (EXT_TAB) + 1);
291 stpcpy (stpcpy (full_base_name, short_base_name), EXT_TAB);
292
293 /* Computes the extensions from the grammar file name. */
294 filename_split (infile, &base, &tab, &ext);
295 if (ext && !yacc_flag)
296 compute_exts_from_gf (ext);
297 }
298 }
299
300 /*-------------------------------------------------------.
301 | Close the open files, compute the output files names. |
302 `-------------------------------------------------------*/
303
304 void
305 compute_output_file_names (void)
306 {
307 compute_base_names ();
308
309 /* If not yet done. */
310 if (!src_extension)
311 src_extension = ".c";
312 if (!header_extension)
313 header_extension = ".h";
314
315 parser_file_name =
316 spec_outfile ? spec_outfile : stringappend (full_base_name, src_extension);
317
318 /* It the defines filename if not given, we create it. */
319 if (!spec_defines_file)
320 spec_defines_file = stringappend (full_base_name, header_extension);
321
322 /* It the graph filename if not given, we create it. */
323 if (!spec_graph_file)
324 spec_graph_file = stringappend (short_base_name, ".vcg");
325
326 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
327 }