]> git.saurik.com Git - bison.git/blob - src/files.c
* configure.in (WERROR_CFLAGS): Compute it.
[bison.git] / src / files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21
22 #include "system.h"
23 #include "getargs.h"
24 #include "files.h"
25 #include "gram.h"
26 #include "error.h"
27 #include "complain.h"
28
29 /* From basename.c. Almost a lie, as it returns a char *. */
30 const char *base_name PARAMS ((char const *name));
31
32 FILE *finput = NULL;
33
34 struct obstack action_obstack;
35 struct obstack attrs_obstack;
36 struct obstack output_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 char *attrsfile = NULL;
55
56 static char *full_base_name = NULL;
57 char *short_base_name = NULL;
58
59 /* C source file extension (the parser source). */
60 const char *src_extension = NULL;
61 /* Header file extension (if option ``-d'' is specified). */
62 const char *header_extension = NULL;
63
64 /* Should we insert '.tab' in yacc-compatible parsers? */
65 int tab_extension = 0;
66 \f
67
68 /*--------------------------.
69 | Is SUFFIX ending STRING? |
70 `--------------------------*/
71
72 int
73 strsuffix (const char *string, const char *suffix)
74 {
75 size_t string_len = strlen (string);
76 size_t suffix_len = strlen (suffix);
77 if (suffix_len <= string_len)
78 return !strcmp (string + string_len - suffix_len, suffix);
79 else
80 return 0;
81 }
82
83
84 /*-----------------------------------------------------------------.
85 | Return a newly allocated string composed of the concatenation of |
86 | STRING1, and STRING2. |
87 `-----------------------------------------------------------------*/
88
89 char*
90 stringappend (const char *string1, const char *string2)
91 {
92 size_t len = strlen (string1) + strlen (string2);
93 char *res = XMALLOC (char, len + 1);
94 char *cp;
95 cp = stpcpy (res, string1);
96 cp = stpcpy (cp, string2);
97 return res;
98 }
99
100
101 /*-----------------------------------------------------------------.
102 | Computes the macro name used to avoid double inclusion in the |
103 | header of the parser and store it in header_macro_name. Be sure |
104 | to produce valid CPP names (don't start with digit, remain |
105 | alphanumerical + underscore). |
106 `-----------------------------------------------------------------*/
107
108 char *
109 compute_header_macro (void)
110 {
111 const char *prefix = "BISON_";
112 char *macro_name, *cp;
113
114 if (spec_defines_file)
115 {
116 macro_name = XMALLOC (char,
117 strlen (prefix) +
118 strlen (spec_defines_file) + 1);
119 cp = stpcpy (macro_name, prefix);
120 cp = stpcpy (cp, spec_defines_file);
121 }
122 else
123 {
124 macro_name = XMALLOC (char,
125 strlen (prefix) +
126 strlen (full_base_name) +
127 strlen (header_extension) + 1);
128 cp = stpcpy (macro_name, prefix);
129 cp = stpcpy (cp, full_base_name);
130 cp = stpcpy (cp, header_extension);
131 }
132
133 for (cp = macro_name; *cp; ++cp)
134 if (islower (*cp))
135 *cp = toupper (*cp);
136 else if (!isalnum (*cp))
137 *cp = '_';
138
139 return macro_name;
140 }
141
142
143 /*-----------------------------------------------------------------.
144 | Try to open file NAME with mode MODE, and print an error message |
145 | if fails. |
146 `-----------------------------------------------------------------*/
147
148 FILE *
149 xfopen (const char *name, const char *mode)
150 {
151 FILE *ptr;
152
153 ptr = fopen (name, mode);
154 if (!ptr)
155 error (2, errno, _("cannot open file `%s'"), name);
156
157 return ptr;
158 }
159
160 /*-------------------------------------------------------------.
161 | Try to close file PTR, and print an error message if fails. |
162 `-------------------------------------------------------------*/
163
164 int
165 xfclose (FILE *ptr)
166 {
167 int result;
168
169 if (ptr == NULL)
170 return 0;
171
172 result = fclose (ptr);
173 if (result == EOF)
174 error (2, errno, _("cannot close file"));
175
176 return result;
177 }
178
179
180 /*------------------------------------------------------------------.
181 | Return the path to the skeleton which locaction might be given in |
182 | ENVVAR, otherwise return SKELETON_NAME. |
183 `------------------------------------------------------------------*/
184
185 const char *
186 skeleton_find (const char *envvar, const char *skeleton_name)
187 {
188 const char *res = getenv (envvar);
189
190 #if defined (MSDOS) || defined (_WIN32)
191 if (!res)
192 {
193 /* Skeleton file name without path */
194 const char *skel_name = strrchr (skeleton_name, '/');
195 if (!skel_name)
196 skel_name = strrchr (skeleton_name, '\\');
197 if (!skel_name)
198 skel_name = skeleton_name;
199 else
200 ++skel_name;
201
202 /* File doesn't exist in current directory; try in INIT directory. */
203 const char *cp = getenv ("INIT");
204 if (cp)
205 {
206 res = XMALLOC (char, strlen (cp) + strlen (skel_name) + 2);
207 sprintf (res, "%s%c%s", cp, '\\', skel_name);
208 }
209 else if (access (skel_name, 4) == 0) /* Look in current dir. */
210 res = skel_name;
211 else
212 {
213 /* Look in program locations dir. */
214 extern char *program_name;
215 cp = strrchr(program_name, '\\');
216 if (!cp)
217 return skeleton_name;
218 else
219 ++cp;
220 res = XMALLOC (char, cp - program_name + strlen (skel_name) + 1);
221 strncpy (res, program_name, cp - program_name);
222 strcpy (res + (cp - program_name), skel_name);
223 }
224 }
225 #endif /* defined (MSDOS) || defined (_WIN32) */
226 if (!res)
227 res = skeleton_name;
228
229 return res;
230 }
231 \f
232
233 /*----------------------------------------------------------------.
234 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
235 `----------------------------------------------------------------*/
236
237 /* Replace all characters FROM by TO in the string IN.
238 and returns a new allocated string. */
239 static char *
240 tr (const char *in, char from, char to)
241 {
242 char *temp;
243 char *out;
244
245 out = XMALLOC (char, strlen (in) + 1);
246
247 for (temp = out; *in; in++, out++)
248 if (*in == from)
249 *out = to;
250 else
251 *out = *in;
252 *out = 0;
253 return (temp);
254 }
255
256 /* Computes extensions from the grammar file extension. */
257 static void
258 compute_exts_from_gf (const char *ext)
259 {
260 src_extension = tr (ext, 'y', 'c');
261 src_extension = tr (src_extension, 'Y', 'C');
262 header_extension = tr (ext, 'y', 'h');
263 header_extension = tr (header_extension, 'Y', 'H');
264 }
265
266 /* Computes extensions from the given c source file extension. */
267 static void
268 compute_exts_from_src (const char *ext)
269 {
270 /* We use this function when the user specifies `-o' or `--output',
271 so the extenions must be computed unconditionally from the file name
272 given by this option. */
273 src_extension = xstrdup (ext);
274 header_extension = tr (ext, 'c', 'h');
275 header_extension = tr (header_extension, 'C', 'H');
276 }
277
278
279 /* Decompose FILENAME in four parts: *BASE, *TAB, and *EXT, the fourth
280 part, (the directory) is ranging from FILENAME to the char before
281 *BASE, so we don't need an additional parameter.
282
283 *EXT points to the last period in the basename, or NULL if none.
284
285 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
286 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB
287 cannot be equal to *BASE.
288
289 None are allocated, they are simply pointers to parts of FILENAME.
290 Examples:
291
292 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
293 '.c'
294
295 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
296
297 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
298
299 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
300
301 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
302
303 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
304
305 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
306
307 static void
308 filename_split (const char *filename,
309 const char **base, const char **tab, const char **ext)
310 {
311 *base = base_name (filename);
312
313 /* Look for the extension, i.e., look for the last dot. */
314 *ext = strrchr (*base, '.');
315 *tab = NULL;
316
317 /* If there is an exentension, check if there is a `.tab' part right
318 before. */
319 if (*ext
320 && (*ext - *base) > (int) strlen (".tab")
321 && (!strncmp (*ext - strlen (".tab"), ".tab", strlen (".tab"))
322 || !strncmp (*ext - strlen ("_tab"), "_tab", strlen ("_tab"))))
323 *tab = *ext - strlen (".tab");
324 }
325
326
327 /* FIXME: Should use xstrndup. */
328
329 static void
330 compute_base_names (void)
331 {
332 const char *base, *tab, *ext;
333
334 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
335 BASE_NAME and SHORT_BASE_NAME are `foo'.
336
337 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
338 SHORT_BASE_NAME is `foo'.
339
340 The precise -o name will be used for FTABLE. For other output
341 files, remove the ".c" or ".tab.c" suffix. */
342 if (spec_outfile)
343 {
344 filename_split (spec_outfile, &base, &tab, &ext);
345
346 /* The full base name goes up the EXT, excluding it. */
347 full_base_name =
348 xstrndup (spec_outfile,
349 (strlen (spec_outfile) - (ext ? strlen (ext) : 0)));
350 /* The short base name goes up to TAB, excluding it. */
351 short_base_name =
352 xstrndup (spec_outfile,
353 (strlen (spec_outfile)
354 - (tab ? strlen (tab) : (ext ? strlen (ext) : 0))));
355
356 /* Do we have a tab part? */
357 tab_extension = !!tab;
358
359 if (ext)
360 compute_exts_from_src (ext);
361 }
362
363 /* If --file-prefix=foo was specified, FULL_BASE_NAME = `foo.tab'
364 and SHORT_BASE_NAME = `foo'.
365
366 Construct names from it. */
367 else
368 {
369 if (spec_file_prefix)
370 {
371 /* If --file-prefix=foo was specified, SHORT_BASE_NAME =
372 `foo'. */
373 short_base_name = xstrdup (spec_file_prefix);
374 }
375 else if (yacc_flag)
376 {
377 /* If --yacc, then the output is `y.tab.c'. */
378 short_base_name = xstrdup ("y");
379 }
380 else
381 {
382 /* Otherwise, the short base name is computed from the input
383 grammar: `foo.yy' => `foo'. */
384 filename_split (infile, &base, &tab, &ext);
385 short_base_name =
386 xstrndup (infile,
387 (strlen (infile) - (ext ? strlen (ext) : 0)));
388 }
389
390 /* In these cases, always append `.tab'. */
391 tab_extension = 1;
392 full_base_name = XMALLOC (char,
393 strlen (short_base_name)
394 + strlen (EXT_TAB) + 1);
395 stpcpy (stpcpy (full_base_name, short_base_name), EXT_TAB);
396
397 /* Computes the extensions from the grammar file name. */
398 filename_split (infile, &base, &tab, &ext);
399 if (ext)
400 compute_exts_from_gf (ext);
401 }
402 }
403
404 /*-------------------------------------------------------.
405 | Close the open files, compute the output files names. |
406 `-------------------------------------------------------*/
407
408 void
409 compute_output_file_names (void)
410 {
411 compute_base_names ();
412
413 parser_file_name =
414 spec_outfile ? spec_outfile : stringappend (full_base_name, src_extension);
415
416 /* If not yet done. */
417 if (!src_extension)
418 src_extension = ".c";
419 if (!header_extension)
420 header_extension = ".h";
421
422 /* It the defines filename if not given, we create it. */
423 if (!spec_defines_file)
424 spec_defines_file = stringappend (full_base_name, header_extension);
425
426 /* It the graph filename if not given, we create it. */
427 if (!spec_graph_file)
428 spec_graph_file = stringappend (short_base_name, ".vcg");
429
430 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
431
432 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
433 }