]>
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
30 struct obstack action_obstack
;
31 struct obstack attrs_obstack
;
32 struct obstack table_obstack
;
33 struct obstack defines_obstack
;
34 struct obstack guard_obstack
;
35 struct obstack output_obstack
;
37 char *spec_outfile
= NULL
; /* for -o. */
38 char *spec_file_prefix
= NULL
; /* for -b. */
39 char *spec_name_prefix
= NULL
; /* for -p. */
40 char *spec_verbose_file
= NULL
; /* for --verbose. */
41 char *spec_graph_file
= NULL
; /* for -g. */
42 char *spec_defines_file
= NULL
; /* for --defines. */
45 char *attrsfile
= NULL
;
47 static char *base_name
= NULL
;
48 static char *short_base_name
= NULL
;
50 /* C source file extension (the parser source). */
51 const char *src_extension
= NULL
;
52 /* Header file extension (if option ``-d'' is specified). */
53 const char *header_extension
= NULL
;
56 /*--------------------------.
57 | Is SUFFIX ending STRING? |
58 `--------------------------*/
61 strsuffix (const char *string
, const char *suffix
)
63 size_t string_len
= strlen (string
);
64 size_t suffix_len
= strlen (suffix
);
65 if (suffix_len
<= string_len
)
66 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
72 /*-----------------------------------------------------------------.
73 | Return a newly allocated string composed of the concatenation of |
74 | STRING1, and STRING2. |
75 `-----------------------------------------------------------------*/
78 stringappend (const char *string1
, const char *string2
)
80 size_t len
= strlen (string1
) + strlen (string2
);
81 char *res
= XMALLOC (char, len
+ 1);
83 cp
= stpcpy (res
, string1
);
84 cp
= stpcpy (cp
, string2
);
89 /*---------------------------------------------------------------.
90 | Computes the macro name used to avoid double inclusion in the |
91 | header of the parser and store it in header_macro_name. |
92 `---------------------------------------------------------------*/
95 compute_header_macro (void)
100 if (spec_defines_file
)
101 macro_name
= xstrdup (spec_defines_file
);
104 macro_name
= XMALLOC (char,
106 strlen (header_extension
) + 1);
108 stpcpy (macro_name
, base_name
);
109 strcat (macro_name
, header_extension
);
112 for (ite
= 0; macro_name
[ite
]; ite
++)
113 if (macro_name
[ite
] == '.')
114 macro_name
[ite
] = '_';
117 if (islower (macro_name
[ite
]))
118 macro_name
[ite
] -= ('a' - 'A');
124 /*-----------------------------------------------------------------.
125 | Try to open file NAME with mode MODE, and print an error message |
127 `-----------------------------------------------------------------*/
130 xfopen (const char *name
, const char *mode
)
134 ptr
= fopen (name
, mode
);
136 error (2, errno
, _("cannot open file `%s'"), name
);
141 /*-------------------------------------------------------------.
142 | Try to close file PTR, and print an error message if fails. |
143 `-------------------------------------------------------------*/
153 result
= fclose (ptr
);
155 error (2, errno
, _("cannot close file"));
160 /*--------------------------------------------------.
161 | Save the content of the obstack OBS in FILENAME. |
162 `--------------------------------------------------*/
165 obstack_save (struct obstack
*obs
, const char *filename
)
167 FILE *out
= xfopen (filename
, "w");
168 size_t size
= obstack_object_size (obs
);
169 fwrite (obstack_finish (obs
), 1, size
, out
);
173 /*---------------------------------------------------------------------.
174 | Output double inclusion protection macros and saves defines_obstack |
175 `---------------------------------------------------------------------*/
178 defines_obstack_save (const char *filename
)
180 FILE *out
= xfopen (filename
, "w");
181 size_t size
= obstack_object_size (&defines_obstack
);
182 char *macro_name
= compute_header_macro ();
184 fprintf (out
, "#ifndef %s\n", macro_name
);
185 fprintf (out
, "# define %s\n\n", macro_name
);
186 fwrite (obstack_finish (&defines_obstack
), 1, size
, out
);
187 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
193 /*------------------------------------------------------------------.
194 | Return the path to the skeleton which locaction might be given in |
195 | ENVVAR, otherwise return SKELETON_NAME. |
196 `------------------------------------------------------------------*/
199 skeleton_find (const char *envvar
, const char *skeleton_name
)
201 const char *res
= getenv (envvar
);
206 /* File doesn't exist in current directory; try in INIT directory. */
207 if (!res
&& (cp
= getenv ("INIT")))
209 res
= XMALLOC (char, strlen (cp
) + strlen (skeleton_name
) + 2);
210 sprintf (res
, "%s%c%s", cp
, '/', skeleton_name
);
221 /*----------------------------------------------------------------.
222 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
223 `----------------------------------------------------------------*/
225 /* Replace all characters FROM by TO in the string IN.
226 and returns a new allocated string. */
228 tr (const char *in
, char from
, char to
)
233 out
= XMALLOC (char, strlen (in
) + 1);
235 for (temp
= out
; *in
; in
++, out
++)
244 /* Gets the extension index in FILENAME. Returns 0 if fails to
245 find an extension. */
247 get_extension_index (const char *filename
)
251 len
= strlen (filename
);
253 if (filename
[len
-- - 1] == '.')
256 while ((len
> 0) && (filename
[len
- 1] != '.'))
257 if (filename
[len
- 1] == '/')
265 /* Computes extensions from the grammar file extension. */
267 compute_exts_from_gf (const char *ext
)
269 src_extension
= tr (ext
, 'y', 'c');
270 src_extension
= tr (src_extension
, 'Y', 'C');
271 header_extension
= tr (ext
, 'y', 'h');
272 header_extension
= tr (header_extension
, 'Y', 'H');
275 /* Computes extensions from the given c source file extension. */
277 compute_exts_from_src (const char *ext
)
279 /* We use this function when the user specifies `-o' or `--output',
280 so the extenions must be computed unconditionally from the file name
281 given by this option. */
282 src_extension
= xstrdup (ext
);
283 header_extension
= tr (ext
, 'c', 'h');
284 header_extension
= tr (header_extension
, 'C', 'H');
287 /* FIXME: Should use xstrndup. */
290 compute_base_names (void)
293 size_t short_base_length
;
296 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
297 BASE_NAME and SHORT_BASE_NAME are `foo'.
299 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
300 SHORT_BASE_NAME is `foo'.
302 The precise -o name will be used for FTABLE. For other output
303 files, remove the ".c" or ".tab.c" suffix. */
307 strlwr (spec_outfile
);
309 /* BASE_LENGTH includes ".tab" but not ".c". */
310 base_length
= strlen (spec_outfile
);
312 ext_index
= get_extension_index (spec_outfile
);
313 /* If the initial segment of extension contains 'c' or a 'C', I assume
314 that it is a C or C++ source file. */
317 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
320 base_length
-= strlen (spec_outfile
+ ext_index
);
321 compute_exts_from_src (spec_outfile
+ ext_index
);
324 base_name
= strndup (spec_outfile
, base_length
);
325 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
326 short_base_length
= base_length
;
327 if (strsuffix (base_name
, ".tab") || strsuffix (base_name
, "_tab"))
328 short_base_length
-= 4;
329 short_base_name
= strndup (spec_outfile
, short_base_length
);
334 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
337 Construct names from it. */
338 if (spec_file_prefix
)
341 strlwr (spec_file_prefix
);
343 short_base_name
= xstrdup (spec_file_prefix
);
344 base_name
= XMALLOC (char,
345 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
346 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
348 /* Computes the extensions from the garmmar file name. */
349 ext_index
= get_extension_index (infile
);
350 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
351 that it is a yacc or bison grammar file. */
353 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
355 compute_exts_from_gf (infile
+ ext_index
);
360 /* If neither -o nor --file-prefix were specified, and the input
361 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
364 If --yacc is used, do as if the input file was `y.y'. */
366 const char *name_base
= yacc_flag
? "y.y" : infile
;
368 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
370 base_length
= strlen (name_base
);
372 ext_index
= get_extension_index (name_base
);
373 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
374 that it is a yacc or bison grammar file. */
376 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
379 base_length
-= strlen (name_base
+ ext_index
);
380 compute_exts_from_gf (name_base
+ ext_index
);
383 short_base_length
= base_length
;
384 short_base_name
= strndup (name_base
, short_base_length
);
386 base_name
= XMALLOC (char,
387 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
388 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
394 /*-------------------------------------------------------.
395 | Close the open files, compute the output files names. |
396 `-------------------------------------------------------*/
399 compute_output_file_names (void)
401 compute_base_names ();
403 /* If not yet done. */
405 src_extension
= ".c";
406 if (!header_extension
)
407 header_extension
= ".h";
409 /* It the defines filename if not given, we create it. */
410 if (!spec_defines_file
)
411 spec_defines_file
= stringappend (base_name
, header_extension
);
413 /* It the graph filename if not given, we create it. */
414 if (!spec_graph_file
)
415 spec_graph_file
= stringappend (short_base_name
, ".vcg");
417 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
419 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
421 attrsfile
= stringappend (attrsfile
, header_extension
);
426 /*-----------------------------------------------------------------.
427 | Open the input file. Look for the skeletons. Find the names of |
428 | the output files. Prepare the obstacks. |
429 `-----------------------------------------------------------------*/
434 finput
= xfopen (infile
, "r");
436 /* Initialize the obstacks. */
437 obstack_init (&action_obstack
);
438 obstack_init (&attrs_obstack
);
439 obstack_init (&table_obstack
);
440 obstack_init (&defines_obstack
);
441 obstack_init (&guard_obstack
);
442 obstack_init (&output_obstack
);
447 /*-----------------------.
448 | Close the open file.. |
449 `-----------------------*/
457 /*---------------------------.
458 | Produce the output files. |
459 `---------------------------*/
464 /* Output the main file. */
466 obstack_save (&table_obstack
, spec_outfile
);
468 obstack_save (&table_obstack
, stringappend (base_name
, src_extension
));
469 obstack_free (&table_obstack
, NULL
);
471 /* Output the header file if wanted. */
473 defines_obstack_save (spec_defines_file
);
474 obstack_free (&defines_obstack
, NULL
);
476 /* If we output only the table, dump the actions in ACTFILE. */
478 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
479 obstack_free (&action_obstack
, NULL
);
481 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
482 into its own file, ATTTRSFILE. */
487 obstack_save (&attrs_obstack
, attrsfile
);
488 obstack_free (&attrs_obstack
, NULL
);
489 temp_name
= stringappend (short_base_name
, EXT_GUARD_C
);
491 temp_name
= stringappend (temp_name
, src_extension
);
493 obstack_save (&guard_obstack
, temp_name
);
494 obstack_free (&guard_obstack
, NULL
);