]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
31 struct obstack action_obstack
;
32 struct obstack attrs_obstack
;
33 struct obstack output_obstack
;
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. */
42 char *spec_outfile
= NULL
; /* for -o. */
43 char *spec_file_prefix
= NULL
; /* for -b. */
44 const char *spec_name_prefix
= NULL
; /* for -p. */
45 char *spec_verbose_file
= NULL
; /* for --verbose. */
46 char *spec_graph_file
= NULL
; /* for -g. */
47 char *spec_defines_file
= NULL
; /* for --defines. */
48 char *parser_file_name
= NULL
;
51 char *attrsfile
= NULL
;
53 static char *base_name
= NULL
;
54 char *short_base_name
= NULL
;
56 /* C source file extension (the parser source). */
57 const char *src_extension
= NULL
;
58 /* Header file extension (if option ``-d'' is specified). */
59 const char *header_extension
= NULL
;
61 /* Should we insert '.tab' in yacc-compatible parsers? */
62 int tab_extension
= 0;
65 /*--------------------------.
66 | Is SUFFIX ending STRING? |
67 `--------------------------*/
70 strsuffix (const char *string
, const char *suffix
)
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
);
81 /*-----------------------------------------------------------------.
82 | Return a newly allocated string composed of the concatenation of |
83 | STRING1, and STRING2. |
84 `-----------------------------------------------------------------*/
87 stringappend (const char *string1
, const char *string2
)
89 size_t len
= strlen (string1
) + strlen (string2
);
90 char *res
= XMALLOC (char, len
+ 1);
92 cp
= stpcpy (res
, string1
);
93 cp
= stpcpy (cp
, string2
);
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 `-----------------------------------------------------------------*/
106 compute_header_macro (void)
108 const char *prefix
= "BISON_";
109 char *macro_name
, *cp
;
111 if (spec_defines_file
)
113 macro_name
= XMALLOC (char,
115 strlen (spec_defines_file
) + 1);
116 cp
= stpcpy (macro_name
, prefix
);
117 cp
= stpcpy (cp
, spec_defines_file
);
121 macro_name
= XMALLOC (char,
124 strlen (header_extension
) + 1);
125 cp
= stpcpy (macro_name
, prefix
);
126 cp
= stpcpy (cp
, base_name
);
127 cp
= stpcpy (cp
, header_extension
);
130 for (cp
= macro_name
; *cp
; ++cp
)
133 else if (!isalnum (*cp
))
140 /*-----------------------------------------------------------------.
141 | Try to open file NAME with mode MODE, and print an error message |
143 `-----------------------------------------------------------------*/
146 xfopen (const char *name
, const char *mode
)
150 ptr
= fopen (name
, mode
);
152 error (2, errno
, _("cannot open file `%s'"), name
);
157 /*-------------------------------------------------------------.
158 | Try to close file PTR, and print an error message if fails. |
159 `-------------------------------------------------------------*/
169 result
= fclose (ptr
);
171 error (2, errno
, _("cannot close file"));
177 /*------------------------------------------------------------------.
178 | Return the path to the skeleton which locaction might be given in |
179 | ENVVAR, otherwise return SKELETON_NAME. |
180 `------------------------------------------------------------------*/
183 skeleton_find (const char *envvar
, const char *skeleton_name
)
185 const char *res
= getenv (envvar
);
187 #if defined (MSDOS) || defined (_WIN32)
190 /* Skeleton file name without path */
191 const char *skel_name
= strrchr (skeleton_name
, '/');
193 skel_name
= strrchr (skeleton_name
, '\\');
195 skel_name
= skeleton_name
;
199 /* File doesn't exist in current directory; try in INIT directory. */
200 const char *cp
= getenv ("INIT");
203 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
204 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
206 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
210 /* Look in program locations dir. */
211 extern char *program_name
;
212 cp
= strrchr(program_name
, '\\');
214 return skeleton_name
;
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
);
222 #endif /* defined (MSDOS) || defined (_WIN32) */
230 /*----------------------------------------------------------------.
231 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
232 `----------------------------------------------------------------*/
234 /* Replace all characters FROM by TO in the string IN.
235 and returns a new allocated string. */
237 tr (const char *in
, char from
, char to
)
242 out
= XMALLOC (char, strlen (in
) + 1);
244 for (temp
= out
; *in
; in
++, out
++)
253 /* Gets the extension index in FILENAME. Returns 0 if fails to
254 find an extension. */
256 get_extension_index (const char *filename
)
260 len
= strlen (filename
);
262 if (filename
[len
-- - 1] == '.')
265 while ((len
> 0) && (filename
[len
- 1] != '.'))
266 if (filename
[len
- 1] == '/')
274 /* Computes extensions from the grammar file extension. */
276 compute_exts_from_gf (const char *ext
)
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');
284 /* Computes extensions from the given c source file extension. */
286 compute_exts_from_src (const char *ext
)
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');
296 /* FIXME: Should use xstrndup. */
299 compute_base_names (void)
302 size_t short_base_length
;
305 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
306 BASE_NAME and SHORT_BASE_NAME are `foo'.
308 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
309 SHORT_BASE_NAME is `foo'.
311 The precise -o name will be used for FTABLE. For other output
312 files, remove the ".c" or ".tab.c" suffix. */
316 strlwr (spec_outfile
);
318 /* BASE_LENGTH includes ".tab" but not ".c". */
319 base_length
= strlen (spec_outfile
);
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. */
326 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
329 base_length
-= strlen (spec_outfile
+ ext_index
);
330 compute_exts_from_src (spec_outfile
+ ext_index
);
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
);
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
))
348 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
351 Construct names from it. */
352 if (spec_file_prefix
)
355 strlwr (spec_file_prefix
);
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
);
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. */
367 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
369 compute_exts_from_gf (infile
+ ext_index
);
371 /* It seems that when only a prefix is given, '.tab' should always be
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
382 If --yacc is used, do as if the input file was `y.y'. */
384 const char *name_base
= yacc_flag
? "y.y" : infile
;
386 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
388 base_length
= strlen (name_base
);
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. */
394 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
397 base_length
-= strlen (name_base
+ ext_index
);
398 compute_exts_from_gf (name_base
+ ext_index
);
401 short_base_length
= base_length
;
402 short_base_name
= strndup (name_base
, short_base_length
);
404 base_name
= XMALLOC (char,
405 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
406 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
408 /* By default, Bison should insert '.tab' were needed. */
415 /*-------------------------------------------------------.
416 | Close the open files, compute the output files names. |
417 `-------------------------------------------------------*/
420 compute_output_file_names (void)
422 compute_base_names ();
425 spec_outfile
? spec_outfile
: stringappend (base_name
, src_extension
);
427 /* If not yet done. */
429 src_extension
= ".c";
430 if (!header_extension
)
431 header_extension
= ".h";
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
);
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");
441 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
443 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
445 attrsfile
= stringappend (attrsfile
, header_extension
);