]>
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 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 const char *spec_name_prefix
= "yy"; /* 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. */
43 char *parser_file_name
= NULL
;
46 char *attrsfile
= NULL
;
48 static char *base_name
= NULL
;
49 static char *short_base_name
= NULL
;
51 /* C source file extension (the parser source). */
52 const char *src_extension
= NULL
;
53 /* Header file extension (if option ``-d'' is specified). */
54 const char *header_extension
= NULL
;
57 /*--------------------------.
58 | Is SUFFIX ending STRING? |
59 `--------------------------*/
62 strsuffix (const char *string
, const char *suffix
)
64 size_t string_len
= strlen (string
);
65 size_t suffix_len
= strlen (suffix
);
66 if (suffix_len
<= string_len
)
67 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
73 /*-----------------------------------------------------------------.
74 | Return a newly allocated string composed of the concatenation of |
75 | STRING1, and STRING2. |
76 `-----------------------------------------------------------------*/
79 stringappend (const char *string1
, const char *string2
)
81 size_t len
= strlen (string1
) + strlen (string2
);
82 char *res
= XMALLOC (char, len
+ 1);
84 cp
= stpcpy (res
, string1
);
85 cp
= stpcpy (cp
, string2
);
90 /*-----------------------------------------------------------------.
91 | Computes the macro name used to avoid double inclusion in the |
92 | header of the parser and store it in header_macro_name. Be sure |
93 | to produce valid CPP names (don't start with digit, remain |
94 | alphanumerical + underscore). |
95 `-----------------------------------------------------------------*/
98 compute_header_macro (void)
100 const char *prefix
= "BISON_";
101 char *macro_name
, *cp
;
103 if (spec_defines_file
)
105 macro_name
= XMALLOC (char,
107 strlen (spec_defines_file
) + 1);
108 cp
= stpcpy (macro_name
, prefix
);
109 cp
= stpcpy (cp
, spec_defines_file
);
113 macro_name
= XMALLOC (char,
116 strlen (header_extension
) + 1);
117 cp
= stpcpy (macro_name
, prefix
);
118 cp
= stpcpy (cp
, base_name
);
119 cp
= stpcpy (cp
, header_extension
);
122 for (cp
= macro_name
; *cp
; ++cp
)
125 else if (!isalnum (*cp
))
132 /*-----------------------------------------------------------------.
133 | Try to open file NAME with mode MODE, and print an error message |
135 `-----------------------------------------------------------------*/
138 xfopen (const char *name
, const char *mode
)
142 ptr
= fopen (name
, mode
);
144 error (2, errno
, _("cannot open file `%s'"), name
);
149 /*-------------------------------------------------------------.
150 | Try to close file PTR, and print an error message if fails. |
151 `-------------------------------------------------------------*/
161 result
= fclose (ptr
);
163 error (2, errno
, _("cannot close file"));
168 /*--------------------------------------------------.
169 | Save the content of the obstack OBS in FILENAME. |
170 `--------------------------------------------------*/
173 obstack_save (struct obstack
*obs
, const char *filename
)
175 FILE *out
= xfopen (filename
, "w");
176 size_t size
= obstack_object_size (obs
);
177 fwrite (obstack_finish (obs
), 1, size
, out
);
181 /*---------------------------------------------------------------------.
182 | Output double inclusion protection macros and saves defines_obstack |
183 `---------------------------------------------------------------------*/
186 defines_obstack_save (const char *filename
)
188 FILE *out
= xfopen (filename
, "w");
189 size_t size
= obstack_object_size (&defines_obstack
);
190 char *macro_name
= compute_header_macro ();
192 fprintf (out
, "#ifndef %s\n", macro_name
);
193 fprintf (out
, "# define %s\n\n", macro_name
);
194 fwrite (obstack_finish (&defines_obstack
), 1, size
, out
);
195 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
201 /*------------------------------------------------------------------.
202 | Return the path to the skeleton which locaction might be given in |
203 | ENVVAR, otherwise return SKELETON_NAME. |
204 `------------------------------------------------------------------*/
207 skeleton_find (const char *envvar
, const char *skeleton_name
)
209 const char *res
= getenv (envvar
);
211 #if defined (MSDOS) || defined (_WIN32)
214 /* Skeleton file name without path */
215 const char *skel_name
= strrchr (skeleton_name
, '/');
217 skel_name
= strrchr (skeleton_name
, '\\');
219 skel_name
= skeleton_name
;
223 /* File doesn't exist in current directory; try in INIT directory. */
224 const char *cp
= getenv ("INIT");
227 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
228 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
230 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
234 /* Look in program locations dir. */
235 extern char *program_name
;
236 cp
= strrchr(program_name
, '\\');
238 return skeleton_name
;
241 res
= XMALLOC (char, cp
- program_name
+ strlen (skel_name
) + 1);
242 strncpy (res
, program_name
, cp
- program_name
);
243 strcpy (res
+ (cp
- program_name
), skel_name
);
246 #endif /* defined (MSDOS) || defined (_WIN32) */
254 /*----------------------------------------------------------------.
255 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
256 `----------------------------------------------------------------*/
258 /* Replace all characters FROM by TO in the string IN.
259 and returns a new allocated string. */
261 tr (const char *in
, char from
, char to
)
266 out
= XMALLOC (char, strlen (in
) + 1);
268 for (temp
= out
; *in
; in
++, out
++)
277 /* Gets the extension index in FILENAME. Returns 0 if fails to
278 find an extension. */
280 get_extension_index (const char *filename
)
284 len
= strlen (filename
);
286 if (filename
[len
-- - 1] == '.')
289 while ((len
> 0) && (filename
[len
- 1] != '.'))
290 if (filename
[len
- 1] == '/')
298 /* Computes extensions from the grammar file extension. */
300 compute_exts_from_gf (const char *ext
)
302 src_extension
= tr (ext
, 'y', 'c');
303 src_extension
= tr (src_extension
, 'Y', 'C');
304 header_extension
= tr (ext
, 'y', 'h');
305 header_extension
= tr (header_extension
, 'Y', 'H');
308 /* Computes extensions from the given c source file extension. */
310 compute_exts_from_src (const char *ext
)
312 /* We use this function when the user specifies `-o' or `--output',
313 so the extenions must be computed unconditionally from the file name
314 given by this option. */
315 src_extension
= xstrdup (ext
);
316 header_extension
= tr (ext
, 'c', 'h');
317 header_extension
= tr (header_extension
, 'C', 'H');
320 /* FIXME: Should use xstrndup. */
323 compute_base_names (void)
326 size_t short_base_length
;
329 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
330 BASE_NAME and SHORT_BASE_NAME are `foo'.
332 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
333 SHORT_BASE_NAME is `foo'.
335 The precise -o name will be used for FTABLE. For other output
336 files, remove the ".c" or ".tab.c" suffix. */
340 strlwr (spec_outfile
);
342 /* BASE_LENGTH includes ".tab" but not ".c". */
343 base_length
= strlen (spec_outfile
);
345 ext_index
= get_extension_index (spec_outfile
);
346 /* If the initial segment of extension contains 'c' or a 'C', I assume
347 that it is a C or C++ source file. */
350 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
353 base_length
-= strlen (spec_outfile
+ ext_index
);
354 compute_exts_from_src (spec_outfile
+ ext_index
);
357 base_name
= strndup (spec_outfile
, base_length
);
358 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
359 short_base_length
= base_length
;
360 if (strsuffix (base_name
, ".tab") || strsuffix (base_name
, "_tab"))
361 short_base_length
-= 4;
362 short_base_name
= strndup (spec_outfile
, short_base_length
);
367 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
370 Construct names from it. */
371 if (spec_file_prefix
)
374 strlwr (spec_file_prefix
);
376 short_base_name
= xstrdup (spec_file_prefix
);
377 base_name
= XMALLOC (char,
378 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
379 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
381 /* Computes the extensions from the garmmar file name. */
382 ext_index
= get_extension_index (infile
);
383 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
384 that it is a yacc or bison grammar file. */
386 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
388 compute_exts_from_gf (infile
+ ext_index
);
393 /* If neither -o nor --file-prefix were specified, and the input
394 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
397 If --yacc is used, do as if the input file was `y.y'. */
399 const char *name_base
= yacc_flag
? "y.y" : infile
;
401 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
403 base_length
= strlen (name_base
);
405 ext_index
= get_extension_index (name_base
);
406 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
407 that it is a yacc or bison grammar file. */
409 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
412 base_length
-= strlen (name_base
+ ext_index
);
413 compute_exts_from_gf (name_base
+ ext_index
);
416 short_base_length
= base_length
;
417 short_base_name
= strndup (name_base
, short_base_length
);
419 base_name
= XMALLOC (char,
420 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
421 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
427 /*-------------------------------------------------------.
428 | Close the open files, compute the output files names. |
429 `-------------------------------------------------------*/
432 compute_output_file_names (void)
434 compute_base_names ();
437 spec_outfile
? spec_outfile
: stringappend (base_name
, src_extension
);
439 /* If not yet done. */
441 src_extension
= ".c";
442 if (!header_extension
)
443 header_extension
= ".h";
445 /* It the defines filename if not given, we create it. */
446 if (!spec_defines_file
)
447 spec_defines_file
= stringappend (base_name
, header_extension
);
449 /* It the graph filename if not given, we create it. */
450 if (!spec_graph_file
)
451 spec_graph_file
= stringappend (short_base_name
, ".vcg");
453 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
455 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
457 attrsfile
= stringappend (attrsfile
, header_extension
);
461 /*-----------------------------------------------------------------.
462 | Open the input file. Look for the skeletons. Find the names of |
463 | the output files. Prepare the obstacks. |
464 `-----------------------------------------------------------------*/
469 finput
= xfopen (infile
, "r");
471 /* Initialize the obstacks. */
472 obstack_init (&action_obstack
);
473 obstack_init (&attrs_obstack
);
474 obstack_init (&defines_obstack
);
475 obstack_init (&guard_obstack
);
476 obstack_init (&output_obstack
);
481 /*-----------------------.
482 | Close the open file.. |
483 `-----------------------*/
491 /*---------------------------.
492 | Produce the output files. |
493 `---------------------------*/
498 /* Output the header file if wanted. */
500 defines_obstack_save (spec_defines_file
);
501 obstack_free (&defines_obstack
, NULL
);
504 /* Seems to be invalid now --akim. */
506 /* If we output only the table, dump the actions in ACTFILE. */
508 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
509 obstack_free (&action_obstack
, NULL
);
512 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
513 into its own file, ATTTRSFILE. */
518 obstack_save (&attrs_obstack
, attrsfile
);
519 obstack_free (&attrs_obstack
, NULL
);
520 temp_name
= stringappend (short_base_name
, EXT_GUARD_C
);
522 temp_name
= stringappend (temp_name
, src_extension
);
524 obstack_save (&guard_obstack
, temp_name
);
525 obstack_free (&guard_obstack
, NULL
);