]> git.saurik.com Git - bison.git/blame_incremental - src/files.c
Regen.
[bison.git] / src / files.c
... / ...
CommitLineData
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
29FILE *finput = NULL;
30
31struct obstack action_obstack;
32struct obstack attrs_obstack;
33struct obstack output_obstack;
34
35/* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is
36 tempting, but don't do that: for the time being our handling of the
37 %directive vs --option leaves precedence to the options by deciding
38 that if a %directive sets a variable which is really set (i.e., not
39 NULL), then the %directive is ignored. As a result, %name-prefix,
40 for instance, will not be honored. */
41
42char *spec_outfile = NULL; /* for -o. */
43char *spec_file_prefix = NULL; /* for -b. */
44const char *spec_name_prefix = NULL; /* for -p. */
45char *spec_verbose_file = NULL; /* for --verbose. */
46char *spec_graph_file = NULL; /* for -g. */
47char *spec_defines_file = NULL; /* for --defines. */
48char *parser_file_name = NULL;
49
50char *infile = NULL;
51char *attrsfile = NULL;
52
53static char *base_name = NULL;
54char *short_base_name = NULL;
55
56/* C source file extension (the parser source). */
57const char *src_extension = NULL;
58/* Header file extension (if option ``-d'' is specified). */
59const char *header_extension = NULL;
60
61/* Should we insert '.tab' in yacc-compatible parsers? */
62int tab_extension = 0;
63\f
64
65/*--------------------------.
66| Is SUFFIX ending STRING? |
67`--------------------------*/
68
69int
70strsuffix (const char *string, const char *suffix)
71{
72 size_t string_len = strlen (string);
73 size_t suffix_len = strlen (suffix);
74 if (suffix_len <= string_len)
75 return !strcmp (string + string_len - suffix_len, suffix);
76 else
77 return 0;
78}
79
80
81/*-----------------------------------------------------------------.
82| Return a newly allocated string composed of the concatenation of |
83| STRING1, and STRING2. |
84`-----------------------------------------------------------------*/
85
86char*
87stringappend (const char *string1, const char *string2)
88{
89 size_t len = strlen (string1) + strlen (string2);
90 char *res = XMALLOC (char, len + 1);
91 char *cp;
92 cp = stpcpy (res, string1);
93 cp = stpcpy (cp, string2);
94 return res;
95}
96
97
98/*-----------------------------------------------------------------.
99| Computes the macro name used to avoid double inclusion in the |
100| header of the parser and store it in header_macro_name. Be sure |
101| to produce valid CPP names (don't start with digit, remain |
102| alphanumerical + underscore). |
103`-----------------------------------------------------------------*/
104
105char *
106compute_header_macro (void)
107{
108 const char *prefix = "BISON_";
109 char *macro_name, *cp;
110
111 if (spec_defines_file)
112 {
113 macro_name = XMALLOC (char,
114 strlen (prefix) +
115 strlen (spec_defines_file) + 1);
116 cp = stpcpy (macro_name, prefix);
117 cp = stpcpy (cp, spec_defines_file);
118 }
119 else
120 {
121 macro_name = XMALLOC (char,
122 strlen (prefix) +
123 strlen (base_name) +
124 strlen (header_extension) + 1);
125 cp = stpcpy (macro_name, prefix);
126 cp = stpcpy (cp, base_name);
127 cp = stpcpy (cp, header_extension);
128 }
129
130 for (cp = macro_name; *cp; ++cp)
131 if (islower (*cp))
132 *cp = toupper (*cp);
133 else if (!isalnum (*cp))
134 *cp = '_';
135
136 return macro_name;
137}
138
139
140/*-----------------------------------------------------------------.
141| Try to open file NAME with mode MODE, and print an error message |
142| if fails. |
143`-----------------------------------------------------------------*/
144
145FILE *
146xfopen (const char *name, const char *mode)
147{
148 FILE *ptr;
149
150 ptr = fopen (name, mode);
151 if (!ptr)
152 error (2, errno, _("cannot open file `%s'"), name);
153
154 return ptr;
155}
156
157/*-------------------------------------------------------------.
158| Try to close file PTR, and print an error message if fails. |
159`-------------------------------------------------------------*/
160
161int
162xfclose (FILE *ptr)
163{
164 int result;
165
166 if (ptr == NULL)
167 return 0;
168
169 result = fclose (ptr);
170 if (result == EOF)
171 error (2, errno, _("cannot close file"));
172
173 return result;
174}
175
176
177/*------------------------------------------------------------------.
178| Return the path to the skeleton which locaction might be given in |
179| ENVVAR, otherwise return SKELETON_NAME. |
180`------------------------------------------------------------------*/
181
182const char *
183skeleton_find (const char *envvar, const char *skeleton_name)
184{
185 const char *res = getenv (envvar);
186
187#if defined (MSDOS) || defined (_WIN32)
188 if (!res)
189 {
190 /* Skeleton file name without path */
191 const char *skel_name = strrchr (skeleton_name, '/');
192 if (!skel_name)
193 skel_name = strrchr (skeleton_name, '\\');
194 if (!skel_name)
195 skel_name = skeleton_name;
196 else
197 ++skel_name;
198
199 /* File doesn't exist in current directory; try in INIT directory. */
200 const char *cp = getenv ("INIT");
201 if (cp)
202 {
203 res = XMALLOC (char, strlen (cp) + strlen (skel_name) + 2);
204 sprintf (res, "%s%c%s", cp, '\\', skel_name);
205 }
206 else if (access (skel_name, 4) == 0) /* Look in current dir. */
207 res = skel_name;
208 else
209 {
210 /* Look in program locations dir. */
211 extern char *program_name;
212 cp = strrchr(program_name, '\\');
213 if (!cp)
214 return skeleton_name;
215 else
216 ++cp;
217 res = XMALLOC (char, cp - program_name + strlen (skel_name) + 1);
218 strncpy (res, program_name, cp - program_name);
219 strcpy (res + (cp - program_name), skel_name);
220 }
221 }
222#endif /* defined (MSDOS) || defined (_WIN32) */
223 if (!res)
224 res = skeleton_name;
225
226 return res;
227}
228\f
229
230/*----------------------------------------------------------------.
231| Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
232`----------------------------------------------------------------*/
233
234/* Replace all characters FROM by TO in the string IN.
235 and returns a new allocated string. */
236static char *
237tr (const char *in, char from, char to)
238{
239 char *temp;
240 char *out;
241
242 out = XMALLOC (char, strlen (in) + 1);
243
244 for (temp = out; *in; in++, out++)
245 if (*in == from)
246 *out = to;
247 else
248 *out = *in;
249 *out = 0;
250 return (temp);
251}
252
253/* Gets the extension index in FILENAME. Returns 0 if fails to
254 find an extension. */
255static int
256get_extension_index (const char *filename)
257{
258 int len;
259
260 len = strlen (filename);
261
262 if (filename[len-- - 1] == '.')
263 return (0);
264
265 while ((len > 0) && (filename[len - 1] != '.'))
266 if (filename[len - 1] == '/')
267 return (0);
268 else
269 len--;
270
271 return (len - 1);
272}
273
274/* Computes extensions from the grammar file extension. */
275static void
276compute_exts_from_gf (const char *ext)
277{
278 src_extension = tr (ext, 'y', 'c');
279 src_extension = tr (src_extension, 'Y', 'C');
280 header_extension = tr (ext, 'y', 'h');
281 header_extension = tr (header_extension, 'Y', 'H');
282}
283
284/* Computes extensions from the given c source file extension. */
285static void
286compute_exts_from_src (const char *ext)
287{
288 /* We use this function when the user specifies `-o' or `--output',
289 so the extenions must be computed unconditionally from the file name
290 given by this option. */
291 src_extension = xstrdup (ext);
292 header_extension = tr (ext, 'c', 'h');
293 header_extension = tr (header_extension, 'C', 'H');
294}
295
296/* FIXME: Should use xstrndup. */
297
298static void
299compute_base_names (void)
300{
301 size_t base_length;
302 size_t short_base_length;
303 size_t ext_index;
304
305 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
306 BASE_NAME and SHORT_BASE_NAME are `foo'.
307
308 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
309 SHORT_BASE_NAME is `foo'.
310
311 The precise -o name will be used for FTABLE. For other output
312 files, remove the ".c" or ".tab.c" suffix. */
313 if (spec_outfile)
314 {
315#ifdef MSDOS
316 strlwr (spec_outfile);
317#endif /* MSDOS */
318 /* BASE_LENGTH includes ".tab" but not ".c". */
319 base_length = strlen (spec_outfile);
320
321 ext_index = get_extension_index (spec_outfile);
322 /* If the initial segment of extension contains 'c' or a 'C', I assume
323 that it is a C or C++ source file. */
324 if (ext_index)
325 ext_index =
326 (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
327 if (ext_index)
328 {
329 base_length -= strlen (spec_outfile + ext_index);
330 compute_exts_from_src (spec_outfile + ext_index);
331 }
332
333 base_name = strndup (spec_outfile, base_length);
334 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
335 short_base_length = base_length;
336 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
337 short_base_length -= 4;
338 short_base_name = strndup (spec_outfile, short_base_length);
339
340 /* FIXME: This is a quick and dirty way for me to find out if we
341 should .tab or not, using the computations above. */
342 if (strcmp (base_name, short_base_name))
343 tab_extension = 1;
344
345 return;
346 }
347
348 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
349 are `foo'.
350
351 Construct names from it. */
352 if (spec_file_prefix)
353 {
354#ifdef MSDOS
355 strlwr (spec_file_prefix);
356#endif /* MSDOS */
357 short_base_name = xstrdup (spec_file_prefix);
358 base_name = XMALLOC (char,
359 strlen (short_base_name) + strlen (EXT_TAB) + 1);
360 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
361
362 /* Computes the extensions from the garmmar file name. */
363 ext_index = get_extension_index (infile);
364 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
365 that it is a yacc or bison grammar file. */
366 if (ext_index)
367 ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0;
368 if (ext_index)
369 compute_exts_from_gf (infile + ext_index);
370
371 /* It seems that when only a prefix is given, '.tab' should always be
372 used. */
373 tab_extension = 1;
374
375 return;
376 }
377
378 /* If neither -o nor --file-prefix were specified, and the input
379 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
380 `foo'.
381
382 If --yacc is used, do as if the input file was `y.y'. */
383 {
384 const char *name_base = yacc_flag ? "y.y" : infile;
385
386 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
387
388 base_length = strlen (name_base);
389
390 ext_index = get_extension_index (name_base);
391 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
392 that it is a yacc or bison grammar file. */
393 if (ext_index)
394 ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0;
395 if (ext_index)
396 {
397 base_length -= strlen (name_base + ext_index);
398 compute_exts_from_gf (name_base + ext_index);
399 }
400
401 short_base_length = base_length;
402 short_base_name = strndup (name_base, short_base_length);
403
404 base_name = XMALLOC (char,
405 strlen (short_base_name) + strlen (EXT_TAB) + 1);
406 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
407
408 /* By default, Bison should insert '.tab' were needed. */
409 tab_extension = 1;
410
411 return;
412 }
413}
414
415/*-------------------------------------------------------.
416| Close the open files, compute the output files names. |
417`-------------------------------------------------------*/
418
419void
420compute_output_file_names (void)
421{
422 compute_base_names ();
423
424 parser_file_name =
425 spec_outfile ? spec_outfile : stringappend (base_name, src_extension);
426
427 /* If not yet done. */
428 if (!src_extension)
429 src_extension = ".c";
430 if (!header_extension)
431 header_extension = ".h";
432
433 /* It the defines filename if not given, we create it. */
434 if (!spec_defines_file)
435 spec_defines_file = stringappend (base_name, header_extension);
436
437 /* It the graph filename if not given, we create it. */
438 if (!spec_graph_file)
439 spec_graph_file = stringappend (short_base_name, ".vcg");
440
441 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
442
443 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
444#ifndef MSDOS
445 attrsfile = stringappend (attrsfile, header_extension);
446#endif /* MSDOS */
447}