]>
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 static 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
;
62 /*--------------------------.
63 | Is SUFFIX ending STRING? |
64 `--------------------------*/
67 strsuffix (const char *string
, const char *suffix
)
69 size_t string_len
= strlen (string
);
70 size_t suffix_len
= strlen (suffix
);
71 if (suffix_len
<= string_len
)
72 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
78 /*-----------------------------------------------------------------.
79 | Return a newly allocated string composed of the concatenation of |
80 | STRING1, and STRING2. |
81 `-----------------------------------------------------------------*/
84 stringappend (const char *string1
, const char *string2
)
86 size_t len
= strlen (string1
) + strlen (string2
);
87 char *res
= XMALLOC (char, len
+ 1);
89 cp
= stpcpy (res
, string1
);
90 cp
= stpcpy (cp
, string2
);
95 /*-----------------------------------------------------------------.
96 | Computes the macro name used to avoid double inclusion in the |
97 | header of the parser and store it in header_macro_name. Be sure |
98 | to produce valid CPP names (don't start with digit, remain |
99 | alphanumerical + underscore). |
100 `-----------------------------------------------------------------*/
103 compute_header_macro (void)
105 const char *prefix
= "BISON_";
106 char *macro_name
, *cp
;
108 if (spec_defines_file
)
110 macro_name
= XMALLOC (char,
112 strlen (spec_defines_file
) + 1);
113 cp
= stpcpy (macro_name
, prefix
);
114 cp
= stpcpy (cp
, spec_defines_file
);
118 macro_name
= XMALLOC (char,
121 strlen (header_extension
) + 1);
122 cp
= stpcpy (macro_name
, prefix
);
123 cp
= stpcpy (cp
, base_name
);
124 cp
= stpcpy (cp
, header_extension
);
127 for (cp
= macro_name
; *cp
; ++cp
)
130 else if (!isalnum (*cp
))
137 /*-----------------------------------------------------------------.
138 | Try to open file NAME with mode MODE, and print an error message |
140 `-----------------------------------------------------------------*/
143 xfopen (const char *name
, const char *mode
)
147 ptr
= fopen (name
, mode
);
149 error (2, errno
, _("cannot open file `%s'"), name
);
154 /*-------------------------------------------------------------.
155 | Try to close file PTR, and print an error message if fails. |
156 `-------------------------------------------------------------*/
166 result
= fclose (ptr
);
168 error (2, errno
, _("cannot close file"));
174 /*------------------------------------------------------------------.
175 | Return the path to the skeleton which locaction might be given in |
176 | ENVVAR, otherwise return SKELETON_NAME. |
177 `------------------------------------------------------------------*/
180 skeleton_find (const char *envvar
, const char *skeleton_name
)
182 const char *res
= getenv (envvar
);
184 #if defined (MSDOS) || defined (_WIN32)
187 /* Skeleton file name without path */
188 const char *skel_name
= strrchr (skeleton_name
, '/');
190 skel_name
= strrchr (skeleton_name
, '\\');
192 skel_name
= skeleton_name
;
196 /* File doesn't exist in current directory; try in INIT directory. */
197 const char *cp
= getenv ("INIT");
200 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
201 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
203 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
207 /* Look in program locations dir. */
208 extern char *program_name
;
209 cp
= strrchr(program_name
, '\\');
211 return skeleton_name
;
214 res
= XMALLOC (char, cp
- program_name
+ strlen (skel_name
) + 1);
215 strncpy (res
, program_name
, cp
- program_name
);
216 strcpy (res
+ (cp
- program_name
), skel_name
);
219 #endif /* defined (MSDOS) || defined (_WIN32) */
227 /*----------------------------------------------------------------.
228 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
229 `----------------------------------------------------------------*/
231 /* Replace all characters FROM by TO in the string IN.
232 and returns a new allocated string. */
234 tr (const char *in
, char from
, char to
)
239 out
= XMALLOC (char, strlen (in
) + 1);
241 for (temp
= out
; *in
; in
++, out
++)
250 /* Gets the extension index in FILENAME. Returns 0 if fails to
251 find an extension. */
253 get_extension_index (const char *filename
)
257 len
= strlen (filename
);
259 if (filename
[len
-- - 1] == '.')
262 while ((len
> 0) && (filename
[len
- 1] != '.'))
263 if (filename
[len
- 1] == '/')
271 /* Computes extensions from the grammar file extension. */
273 compute_exts_from_gf (const char *ext
)
275 src_extension
= tr (ext
, 'y', 'c');
276 src_extension
= tr (src_extension
, 'Y', 'C');
277 header_extension
= tr (ext
, 'y', 'h');
278 header_extension
= tr (header_extension
, 'Y', 'H');
281 /* Computes extensions from the given c source file extension. */
283 compute_exts_from_src (const char *ext
)
285 /* We use this function when the user specifies `-o' or `--output',
286 so the extenions must be computed unconditionally from the file name
287 given by this option. */
288 src_extension
= xstrdup (ext
);
289 header_extension
= tr (ext
, 'c', 'h');
290 header_extension
= tr (header_extension
, 'C', 'H');
293 /* FIXME: Should use xstrndup. */
296 compute_base_names (void)
299 size_t short_base_length
;
302 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
303 BASE_NAME and SHORT_BASE_NAME are `foo'.
305 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
306 SHORT_BASE_NAME is `foo'.
308 The precise -o name will be used for FTABLE. For other output
309 files, remove the ".c" or ".tab.c" suffix. */
313 strlwr (spec_outfile
);
315 /* BASE_LENGTH includes ".tab" but not ".c". */
316 base_length
= strlen (spec_outfile
);
318 ext_index
= get_extension_index (spec_outfile
);
319 /* If the initial segment of extension contains 'c' or a 'C', I assume
320 that it is a C or C++ source file. */
323 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
326 base_length
-= strlen (spec_outfile
+ ext_index
);
327 compute_exts_from_src (spec_outfile
+ ext_index
);
330 base_name
= strndup (spec_outfile
, base_length
);
331 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
332 short_base_length
= base_length
;
333 if (strsuffix (base_name
, ".tab") || strsuffix (base_name
, "_tab"))
334 short_base_length
-= 4;
335 short_base_name
= strndup (spec_outfile
, short_base_length
);
340 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
343 Construct names from it. */
344 if (spec_file_prefix
)
347 strlwr (spec_file_prefix
);
349 short_base_name
= xstrdup (spec_file_prefix
);
350 base_name
= XMALLOC (char,
351 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
352 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
354 /* Computes the extensions from the garmmar file name. */
355 ext_index
= get_extension_index (infile
);
356 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
357 that it is a yacc or bison grammar file. */
359 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
361 compute_exts_from_gf (infile
+ ext_index
);
366 /* If neither -o nor --file-prefix were specified, and the input
367 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
370 If --yacc is used, do as if the input file was `y.y'. */
372 const char *name_base
= yacc_flag
? "y.y" : infile
;
374 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
376 base_length
= strlen (name_base
);
378 ext_index
= get_extension_index (name_base
);
379 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
380 that it is a yacc or bison grammar file. */
382 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
385 base_length
-= strlen (name_base
+ ext_index
);
386 compute_exts_from_gf (name_base
+ ext_index
);
389 short_base_length
= base_length
;
390 short_base_name
= strndup (name_base
, short_base_length
);
392 base_name
= XMALLOC (char,
393 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
394 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
400 /*-------------------------------------------------------.
401 | Close the open files, compute the output files names. |
402 `-------------------------------------------------------*/
405 compute_output_file_names (void)
407 compute_base_names ();
410 spec_outfile
? spec_outfile
: stringappend (base_name
, src_extension
);
412 /* If not yet done. */
414 src_extension
= ".c";
415 if (!header_extension
)
416 header_extension
= ".h";
418 /* It the defines filename if not given, we create it. */
419 if (!spec_defines_file
)
420 spec_defines_file
= stringappend (base_name
, header_extension
);
422 /* It the graph filename if not given, we create it. */
423 if (!spec_graph_file
)
424 spec_graph_file
= stringappend (short_base_name
, ".vcg");
426 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
428 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
430 attrsfile
= stringappend (attrsfile
, header_extension
);