]>
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. Be sure |
92 | to produce valid CPP names (don't start with digit, remain |
93 | alphanumerical + underscore). |
94 `-----------------------------------------------------------------*/
97 compute_header_macro (void)
99 const char *prefix
= "BISON_";
100 char *macro_name
, *cp
;
102 if (spec_defines_file
)
104 macro_name
= XMALLOC (char,
106 strlen (spec_defines_file
) + 1);
107 cp
= stpcpy (macro_name
, prefix
);
108 cp
= stpcpy (cp
, spec_defines_file
);
112 macro_name
= XMALLOC (char,
115 strlen (header_extension
) + 1);
116 cp
= stpcpy (macro_name
, prefix
);
117 cp
= stpcpy (cp
, base_name
);
118 cp
= stpcpy (cp
, header_extension
);
121 for (cp
= macro_name
; *cp
; ++cp
)
124 else if (!isalnum (*cp
))
131 /*-----------------------------------------------------------------.
132 | Try to open file NAME with mode MODE, and print an error message |
134 `-----------------------------------------------------------------*/
137 xfopen (const char *name
, const char *mode
)
141 ptr
= fopen (name
, mode
);
143 error (2, errno
, _("cannot open file `%s'"), name
);
148 /*-------------------------------------------------------------.
149 | Try to close file PTR, and print an error message if fails. |
150 `-------------------------------------------------------------*/
160 result
= fclose (ptr
);
162 error (2, errno
, _("cannot close file"));
167 /*--------------------------------------------------.
168 | Save the content of the obstack OBS in FILENAME. |
169 `--------------------------------------------------*/
172 obstack_save (struct obstack
*obs
, const char *filename
)
174 FILE *out
= xfopen (filename
, "w");
175 size_t size
= obstack_object_size (obs
);
176 fwrite (obstack_finish (obs
), 1, size
, out
);
180 /*---------------------------------------------------------------------.
181 | Output double inclusion protection macros and saves defines_obstack |
182 `---------------------------------------------------------------------*/
185 defines_obstack_save (const char *filename
)
187 FILE *out
= xfopen (filename
, "w");
188 size_t size
= obstack_object_size (&defines_obstack
);
189 char *macro_name
= compute_header_macro ();
191 fprintf (out
, "#ifndef %s\n", macro_name
);
192 fprintf (out
, "# define %s\n\n", macro_name
);
193 fwrite (obstack_finish (&defines_obstack
), 1, size
, out
);
194 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
200 /*------------------------------------------------------------------.
201 | Return the path to the skeleton which locaction might be given in |
202 | ENVVAR, otherwise return SKELETON_NAME. |
203 `------------------------------------------------------------------*/
206 skeleton_find (const char *envvar
, const char *skeleton_name
)
208 const char *res
= getenv (envvar
);
210 #if defined (MSDOS) || defined (_WIN32)
213 /* Skeleton file name without path */
214 const char *skel_name
= strrchr (skeleton_name
, '/');
216 skel_name
= strrchr (skeleton_name
, '\\');
218 skel_name
= skeleton_name
;
222 /* File doesn't exist in current directory; try in INIT directory. */
223 const char *cp
= getenv ("INIT");
226 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
227 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
229 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
233 /* Look in program locations dir. */
234 extern char *program_name
;
235 cp
= strrchr(program_name
, '\\');
237 return skeleton_name
;
240 res
= XMALLOC (char, cp
- program_name
+ strlen (skel_name
) + 1);
241 strncpy (res
, program_name
, cp
- program_name
);
242 strcpy (res
+ (cp
- program_name
), skel_name
);
245 #endif /* defined (MSDOS) || defined (_WIN32) */
253 /*----------------------------------------------------------------.
254 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
255 `----------------------------------------------------------------*/
257 /* Replace all characters FROM by TO in the string IN.
258 and returns a new allocated string. */
260 tr (const char *in
, char from
, char to
)
265 out
= XMALLOC (char, strlen (in
) + 1);
267 for (temp
= out
; *in
; in
++, out
++)
276 /* Gets the extension index in FILENAME. Returns 0 if fails to
277 find an extension. */
279 get_extension_index (const char *filename
)
283 len
= strlen (filename
);
285 if (filename
[len
-- - 1] == '.')
288 while ((len
> 0) && (filename
[len
- 1] != '.'))
289 if (filename
[len
- 1] == '/')
297 /* Computes extensions from the grammar file extension. */
299 compute_exts_from_gf (const char *ext
)
301 src_extension
= tr (ext
, 'y', 'c');
302 src_extension
= tr (src_extension
, 'Y', 'C');
303 header_extension
= tr (ext
, 'y', 'h');
304 header_extension
= tr (header_extension
, 'Y', 'H');
307 /* Computes extensions from the given c source file extension. */
309 compute_exts_from_src (const char *ext
)
311 /* We use this function when the user specifies `-o' or `--output',
312 so the extenions must be computed unconditionally from the file name
313 given by this option. */
314 src_extension
= xstrdup (ext
);
315 header_extension
= tr (ext
, 'c', 'h');
316 header_extension
= tr (header_extension
, 'C', 'H');
319 /* FIXME: Should use xstrndup. */
322 compute_base_names (void)
325 size_t short_base_length
;
328 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
329 BASE_NAME and SHORT_BASE_NAME are `foo'.
331 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
332 SHORT_BASE_NAME is `foo'.
334 The precise -o name will be used for FTABLE. For other output
335 files, remove the ".c" or ".tab.c" suffix. */
339 strlwr (spec_outfile
);
341 /* BASE_LENGTH includes ".tab" but not ".c". */
342 base_length
= strlen (spec_outfile
);
344 ext_index
= get_extension_index (spec_outfile
);
345 /* If the initial segment of extension contains 'c' or a 'C', I assume
346 that it is a C or C++ source file. */
349 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
352 base_length
-= strlen (spec_outfile
+ ext_index
);
353 compute_exts_from_src (spec_outfile
+ ext_index
);
356 base_name
= strndup (spec_outfile
, base_length
);
357 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
358 short_base_length
= base_length
;
359 if (strsuffix (base_name
, ".tab") || strsuffix (base_name
, "_tab"))
360 short_base_length
-= 4;
361 short_base_name
= strndup (spec_outfile
, short_base_length
);
366 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
369 Construct names from it. */
370 if (spec_file_prefix
)
373 strlwr (spec_file_prefix
);
375 short_base_name
= xstrdup (spec_file_prefix
);
376 base_name
= XMALLOC (char,
377 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
378 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
380 /* Computes the extensions from the garmmar file name. */
381 ext_index
= get_extension_index (infile
);
382 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
383 that it is a yacc or bison grammar file. */
385 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
387 compute_exts_from_gf (infile
+ ext_index
);
392 /* If neither -o nor --file-prefix were specified, and the input
393 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
396 If --yacc is used, do as if the input file was `y.y'. */
398 const char *name_base
= yacc_flag
? "y.y" : infile
;
400 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
402 base_length
= strlen (name_base
);
404 ext_index
= get_extension_index (name_base
);
405 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
406 that it is a yacc or bison grammar file. */
408 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
411 base_length
-= strlen (name_base
+ ext_index
);
412 compute_exts_from_gf (name_base
+ ext_index
);
415 short_base_length
= base_length
;
416 short_base_name
= strndup (name_base
, short_base_length
);
418 base_name
= XMALLOC (char,
419 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
420 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
426 /*-------------------------------------------------------.
427 | Close the open files, compute the output files names. |
428 `-------------------------------------------------------*/
431 compute_output_file_names (void)
433 compute_base_names ();
435 /* If not yet done. */
437 src_extension
= ".c";
438 if (!header_extension
)
439 header_extension
= ".h";
441 /* It the defines filename if not given, we create it. */
442 if (!spec_defines_file
)
443 spec_defines_file
= stringappend (base_name
, header_extension
);
445 /* It the graph filename if not given, we create it. */
446 if (!spec_graph_file
)
447 spec_graph_file
= stringappend (short_base_name
, ".vcg");
449 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
451 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
453 attrsfile
= stringappend (attrsfile
, header_extension
);
458 /*-----------------------------------------------------------------.
459 | Open the input file. Look for the skeletons. Find the names of |
460 | the output files. Prepare the obstacks. |
461 `-----------------------------------------------------------------*/
466 finput
= xfopen (infile
, "r");
468 /* Initialize the obstacks. */
469 obstack_init (&action_obstack
);
470 obstack_init (&attrs_obstack
);
471 obstack_init (&table_obstack
);
472 obstack_init (&defines_obstack
);
473 obstack_init (&guard_obstack
);
474 obstack_init (&output_obstack
);
479 /*-----------------------.
480 | Close the open file.. |
481 `-----------------------*/
489 /*---------------------------.
490 | Produce the output files. |
491 `---------------------------*/
496 /* Output the main file. */
498 obstack_save (&table_obstack
, spec_outfile
);
500 obstack_save (&table_obstack
, stringappend (base_name
, src_extension
));
501 obstack_free (&table_obstack
, NULL
);
503 /* Output the header file if wanted. */
505 defines_obstack_save (spec_defines_file
);
506 obstack_free (&defines_obstack
, NULL
);
509 /* Seems to be invalid now --akim. */
511 /* If we output only the table, dump the actions in ACTFILE. */
513 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
514 obstack_free (&action_obstack
, NULL
);
517 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
518 into its own file, ATTTRSFILE. */
523 obstack_save (&attrs_obstack
, attrsfile
);
524 obstack_free (&attrs_obstack
, NULL
);
525 temp_name
= stringappend (short_base_name
, EXT_GUARD_C
);
527 temp_name
= stringappend (temp_name
, src_extension
);
529 obstack_save (&guard_obstack
, temp_name
);
530 obstack_free (&guard_obstack
, NULL
);