]>
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 table_obstack
;
34 struct obstack defines_obstack
;
35 struct obstack guard_obstack
;
36 struct obstack output_obstack
;
37 struct obstack graph_obstack
;
39 char *spec_outfile
= NULL
; /* for -o. */
40 char *spec_file_prefix
= NULL
; /* for -b. */
41 char *spec_name_prefix
= NULL
; /* for -p. */
42 char *spec_verbose_file
= NULL
; /* for --verbose. */
43 char *spec_graph_file
= NULL
; /* for -g. */
44 char *spec_defines_file
= NULL
; /* for --defines. */
47 char *attrsfile
= NULL
;
49 static char *base_name
= NULL
;
50 static char *short_base_name
= NULL
;
52 /* C source file extension (the parser source). */
53 const char *src_extension
= NULL
;
54 /* Header file extension (if option ``-d'' is specified). */
55 const char *header_extension
= NULL
;
58 /*--------------------------.
59 | Is SUFFIX ending STRING? |
60 `--------------------------*/
63 strsuffix (const char *string
, const char *suffix
)
65 size_t string_len
= strlen (string
);
66 size_t suffix_len
= strlen (suffix
);
67 if (suffix_len
<= string_len
)
68 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
74 /*-----------------------------------------------------------------.
75 | Return a newly allocated string composed of the concatenation of |
76 | STRING1, and STRING2. |
77 `-----------------------------------------------------------------*/
80 stringappend (const char *string1
, const char *string2
)
82 size_t len
= strlen (string1
) + strlen (string2
);
83 char *res
= XMALLOC (char, len
+ 1);
85 cp
= stpcpy (res
, string1
);
86 cp
= stpcpy (cp
, string2
);
91 /*---------------------------------------------------------------.
92 | Computes the macro name used to avoid double inclusion in the |
93 | header of the parser and store it in header_macro_name. |
94 `---------------------------------------------------------------*/
97 compute_header_macro (void)
102 if (spec_defines_file
)
103 macro_name
= xstrdup (spec_defines_file
);
106 macro_name
= XMALLOC (char,
108 strlen (header_extension
) + 1);
110 stpcpy (macro_name
, base_name
);
111 strcat (macro_name
, header_extension
);
114 for (ite
= 0; macro_name
[ite
]; ite
++)
115 if (macro_name
[ite
] == '.')
116 macro_name
[ite
] = '_';
119 if (islower (macro_name
[ite
]))
120 macro_name
[ite
] -= ('a' - 'A');
126 /*-----------------------------------------------------------------.
127 | Try to open file NAME with mode MODE, and print an error message |
129 `-----------------------------------------------------------------*/
132 xfopen (const char *name
, const char *mode
)
136 ptr
= fopen (name
, mode
);
138 error (2, errno
, _("cannot open file `%s'"), name
);
143 /*-------------------------------------------------------------.
144 | Try to close file PTR, and print an error message if fails. |
145 `-------------------------------------------------------------*/
155 result
= fclose (ptr
);
157 error (2, errno
, _("cannot close file"));
162 /*--------------------------------------------------.
163 | Save the content of the obstack OBS in FILENAME. |
164 `--------------------------------------------------*/
167 obstack_save (struct obstack
*obs
, const char *filename
)
169 FILE *out
= xfopen (filename
, "w");
170 size_t size
= obstack_object_size (obs
);
171 fwrite (obstack_finish (obs
), 1, size
, out
);
175 /*---------------------------------------------------------------------.
176 | Output double inclusion protection macros and saves defines_obstack |
177 `---------------------------------------------------------------------*/
180 defines_obstack_save (const char *filename
)
182 FILE *out
= xfopen (filename
, "w");
183 size_t size
= obstack_object_size (&defines_obstack
);
184 char *macro_name
= compute_header_macro ();
186 fprintf (out
, "#ifndef %s\n", macro_name
);
187 fprintf (out
, "# define %s\n\n", macro_name
);
188 fwrite (obstack_finish (&defines_obstack
), 1, size
, out
);
189 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
195 /*------------------------------------------------------------------.
196 | Return the path to the skeleton which locaction might be given in |
197 | ENVVAR, otherwise return SKELETON_NAME. |
198 `------------------------------------------------------------------*/
201 skeleton_find (const char *envvar
, const char *skeleton_name
)
203 const char *res
= getenv (envvar
);
208 /* File doesn't exist in current directory; try in INIT directory. */
209 if (!res
&& (cp
= getenv ("INIT")))
211 res
= XMALLOC (char, strlen (cp
) + strlen (skeleton_name
) + 2);
212 sprintf (res
, "%s%c%s", cp
, '/', skeleton_name
);
223 /*----------------------------------------------------------------.
224 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
225 `----------------------------------------------------------------*/
227 /* Replace all characters FROM by TO in the string IN.
228 and returns a new allocated string. */
230 tr (const char *in
, char from
, char to
)
235 out
= XMALLOC (char, strlen (in
) + 1);
237 for (temp
= out
; *in
; in
++, out
++)
246 /* Gets the extension index in FILENAME. Returns 0 if fails to
247 find an extension. */
249 get_extension_index (const char *filename
)
253 len
= strlen (filename
);
255 if (filename
[len
-- - 1] == '.')
258 while ((len
> 0) && (filename
[len
- 1] != '.'))
259 if (filename
[len
- 1] == '/')
267 /* Computes extensions from the grammar file extension. */
269 compute_exts_from_gf (const char *ext
)
271 src_extension
= tr (ext
, 'y', 'c');
272 src_extension
= tr (src_extension
, 'Y', 'C');
273 header_extension
= tr (ext
, 'y', 'h');
274 header_extension
= tr (header_extension
, 'Y', 'H');
277 /* Computes extensions from the given c source file extension. */
279 compute_exts_from_src (const char *ext
)
281 /* We use this function when the user specifies `-o' or `--output',
282 so the extenions must be computed unconditionally from the file name
283 given by this option. */
284 src_extension
= xstrdup (ext
);
285 header_extension
= tr (ext
, 'c', 'h');
286 header_extension
= tr (header_extension
, 'C', 'H');
289 /* FIXME: Should use xstrndup. */
292 compute_base_names (void)
295 size_t short_base_length
;
298 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
299 BASE_NAME and SHORT_BASE_NAME are `foo'.
301 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
302 SHORT_BASE_NAME is `foo'.
304 The precise -o name will be used for FTABLE. For other output
305 files, remove the ".c" or ".tab.c" suffix. */
309 strlwr (spec_outfile
);
311 /* BASE_LENGTH includes ".tab" but not ".c". */
312 base_length
= strlen (spec_outfile
);
314 ext_index
= get_extension_index (spec_outfile
);
315 /* If the initial segment of extension contains 'c' or a 'C', I assume
316 that it is a C or C++ source file. */
319 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
322 base_length
-= strlen (spec_outfile
+ ext_index
);
323 compute_exts_from_src (spec_outfile
+ ext_index
);
326 base_name
= strndup (spec_outfile
, base_length
);
327 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
328 short_base_length
= base_length
;
329 if (strsuffix (base_name
, ".tab") || strsuffix (base_name
, "_tab"))
330 short_base_length
-= 4;
331 short_base_name
= strndup (spec_outfile
, short_base_length
);
336 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
339 Construct names from it. */
340 if (spec_file_prefix
)
343 strlwr (spec_file_prefix
);
345 short_base_name
= xstrdup (spec_file_prefix
);
346 base_name
= XMALLOC (char,
347 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
348 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
350 /* Computes the extensions from the garmmar file name. */
351 ext_index
= get_extension_index (infile
);
352 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
353 that it is a yacc or bison grammar file. */
355 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
357 compute_exts_from_gf (infile
+ ext_index
);
362 /* If neither -o nor --file-prefix were specified, and the input
363 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
366 If --yacc is used, do as if the input file was `y.y'. */
368 const char *name_base
= yacc_flag
? "y.y" : infile
;
370 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
372 base_length
= strlen (name_base
);
374 ext_index
= get_extension_index (name_base
);
375 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
376 that it is a yacc or bison grammar file. */
378 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
381 base_length
-= strlen (name_base
+ ext_index
);
382 compute_exts_from_gf (name_base
+ ext_index
);
385 short_base_length
= base_length
;
386 short_base_name
= strndup (name_base
, short_base_length
);
388 base_name
= XMALLOC (char,
389 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
390 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
396 /*-------------------------------------------------------.
397 | Close the open files, compute the output files names. |
398 `-------------------------------------------------------*/
401 compute_output_file_names (void)
403 compute_base_names ();
405 /* If not yet done. */
407 src_extension
= ".c";
408 if (!header_extension
)
409 header_extension
= ".h";
411 /* It the defines filename if not given, we create it. */
412 if (!spec_defines_file
)
413 spec_defines_file
= stringappend (base_name
, header_extension
);
415 /* It the graph filename if not given, we create it. */
416 if (!spec_graph_file
)
417 spec_graph_file
= stringappend (short_base_name
, ".vcg");
419 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
421 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
423 stringappend (attrsfile
, header_extension
);
428 /*-----------------------------------------------------------------.
429 | Open the input file. Look for the skeletons. Find the names of |
430 | the output files. Prepare the obstacks. |
431 `-----------------------------------------------------------------*/
436 finput
= xfopen (infile
, "r");
438 /* Initialize the obstacks. */
439 obstack_init (&action_obstack
);
440 obstack_init (&attrs_obstack
);
441 obstack_init (&table_obstack
);
442 obstack_init (&defines_obstack
);
443 obstack_init (&guard_obstack
);
444 obstack_init (&output_obstack
);
445 obstack_init (&graph_obstack
);
450 /*-----------------------.
451 | Close the open file.. |
452 `-----------------------*/
460 /*---------------------------.
461 | Produce the output files. |
462 `---------------------------*/
467 /* Output the main file. */
469 obstack_save (&table_obstack
, spec_outfile
);
471 obstack_save (&table_obstack
, stringappend (base_name
, src_extension
));
473 /* Output the header file if wanted. */
475 defines_obstack_save (spec_defines_file
);
477 /* If we output only the table, dump the actions in ACTFILE. */
479 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
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 temp_name
= stringappend (short_base_name
, EXT_GUARD_C
);
490 temp_name
= stringappend (temp_name
, src_extension
);
492 obstack_save (&guard_obstack
, temp_name
);
496 obstack_save (&graph_obstack
, spec_graph_file
);