]>
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, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32 struct obstack action_obstack
;
33 struct obstack attrs_obstack
;
34 struct obstack table_obstack
;
35 struct obstack defines_obstack
;
36 struct obstack guard_obstack
;
37 struct obstack output_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. Be sure |
94 | to produce valid CPP names (don't start with digit, remain |
95 | alphanumerical + underscore). |
96 `-----------------------------------------------------------------*/
99 compute_header_macro (void)
101 const char *prefix
= "BISON_";
102 char *macro_name
, *cp
;
104 if (spec_defines_file
)
106 macro_name
= XMALLOC (char,
108 strlen (spec_defines_file
) + 1);
109 cp
= stpcpy (macro_name
, prefix
);
110 cp
= stpcpy (cp
, spec_defines_file
);
114 macro_name
= XMALLOC (char,
117 strlen (header_extension
) + 1);
118 cp
= stpcpy (macro_name
, prefix
);
119 cp
= stpcpy (cp
, base_name
);
120 cp
= stpcpy (cp
, header_extension
);
123 for (cp
= macro_name
; *cp
; ++cp
)
126 else if (!isalnum (*cp
))
133 /*-----------------------------------------------------------------.
134 | Try to open file NAME with mode MODE, and print an error message |
136 `-----------------------------------------------------------------*/
139 xfopen (const char *name
, const char *mode
)
143 ptr
= fopen (name
, mode
);
145 error (2, errno
, _("cannot open file `%s'"), name
);
150 /*-------------------------------------------------------------.
151 | Try to close file PTR, and print an error message if fails. |
152 `-------------------------------------------------------------*/
162 result
= fclose (ptr
);
164 error (2, errno
, _("cannot close file"));
169 /*--------------------------------------------------.
170 | Save the content of the obstack OBS in FILENAME. |
171 `--------------------------------------------------*/
174 obstack_save (struct obstack
*obs
, const char *filename
)
176 FILE *out
= xfopen (filename
, "w");
177 size_t size
= obstack_object_size (obs
);
178 fwrite (obstack_finish (obs
), 1, size
, out
);
182 /*---------------------------------------------------------------------.
183 | Output double inclusion protection macros and saves defines_obstack |
184 `---------------------------------------------------------------------*/
187 defines_obstack_save (const char *filename
)
189 FILE *out
= xfopen (filename
, "w");
190 size_t size
= obstack_object_size (&defines_obstack
);
191 char *macro_name
= compute_header_macro ();
193 fprintf (out
, "#ifndef %s\n", macro_name
);
194 fprintf (out
, "# define %s\n\n", macro_name
);
195 fwrite (obstack_finish (&defines_obstack
), 1, size
, out
);
196 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
202 /*------------------------------------------------------------------.
203 | Return the path to the skeleton which locaction might be given in |
204 | ENVVAR, otherwise return SKELETON_NAME. |
205 `------------------------------------------------------------------*/
208 skeleton_find (const char *envvar
, const char *skeleton_name
)
210 const char *res
= getenv (envvar
);
212 #if defined (MSDOS) || defined (_WIN32)
215 /* Skeleton file name without path */
216 const char *skel_name
= strrchr (skeleton_name
, '/');
218 skel_name
= strrchr (skeleton_name
, '\\');
220 skel_name
= skeleton_name
;
224 /* File doesn't exist in current directory; try in INIT directory. */
225 const char *cp
= getenv ("INIT");
228 res
= XMALLOC (char, strlen (cp
) + strlen (skel_name
) + 2);
229 sprintf (res
, "%s%c%s", cp
, '\\', skel_name
);
231 else if (access (skel_name
, 4) == 0) /* Look in current dir. */
235 /* Look in program locations dir. */
236 extern char *program_name
;
237 cp
= strrchr(program_name
, '\\');
239 return skeleton_name
;
242 res
= XMALLOC (char, cp
- program_name
+ strlen (skel_name
) + 1);
243 strncpy (res
, program_name
, cp
- program_name
);
244 strcpy (res
+ (cp
- program_name
), skel_name
);
247 #endif /* defined (MSDOS) || defined (_WIN32) */
255 /*----------------------------------------------------------------.
256 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
257 `----------------------------------------------------------------*/
259 /* Replace all characters FROM by TO in the string IN.
260 and returns a new allocated string. */
262 tr (const char *in
, char from
, char to
)
267 out
= XMALLOC (char, strlen (in
) + 1);
269 for (temp
= out
; *in
; in
++, out
++)
278 /* Gets the extension index in FILENAME. Returns 0 if fails to
279 find an extension. */
281 get_extension_index (const char *filename
)
285 len
= strlen (filename
);
287 if (filename
[len
-- - 1] == '.')
290 while ((len
> 0) && (filename
[len
- 1] != '.'))
291 if (filename
[len
- 1] == '/')
299 /* Computes extensions from the grammar file extension. */
301 compute_exts_from_gf (const char *ext
)
303 src_extension
= tr (ext
, 'y', 'c');
304 src_extension
= tr (src_extension
, 'Y', 'C');
305 header_extension
= tr (ext
, 'y', 'h');
306 header_extension
= tr (header_extension
, 'Y', 'H');
309 /* Computes extensions from the given c source file extension. */
311 compute_exts_from_src (const char *ext
)
313 /* We use this function when the user specifies `-o' or `--output',
314 so the extenions must be computed unconditionally from the file name
315 given by this option. */
316 src_extension
= xstrdup (ext
);
317 header_extension
= tr (ext
, 'c', 'h');
318 header_extension
= tr (header_extension
, 'C', 'H');
321 /* FIXME: Should use xstrndup. */
324 compute_base_names (void)
327 size_t short_base_length
;
330 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
331 BASE_NAME and SHORT_BASE_NAME are `foo'.
333 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
334 SHORT_BASE_NAME is `foo'.
336 The precise -o name will be used for FTABLE. For other output
337 files, remove the ".c" or ".tab.c" suffix. */
341 strlwr (spec_outfile
);
343 /* BASE_LENGTH includes ".tab" but not ".c". */
344 base_length
= strlen (spec_outfile
);
346 ext_index
= get_extension_index (spec_outfile
);
347 /* If the initial segment of extension contains 'c' or a 'C', I assume
348 that it is a C or C++ source file. */
351 (strspn (spec_outfile
+ ext_index
+ 1, "cC")) ? ext_index
: 0;
354 base_length
-= strlen (spec_outfile
+ ext_index
);
355 compute_exts_from_src (spec_outfile
+ ext_index
);
358 base_name
= strndup (spec_outfile
, base_length
);
359 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
360 short_base_length
= base_length
;
361 if (strsuffix (base_name
, ".tab") || strsuffix (base_name
, "_tab"))
362 short_base_length
-= 4;
363 short_base_name
= strndup (spec_outfile
, short_base_length
);
368 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
371 Construct names from it. */
372 if (spec_file_prefix
)
375 strlwr (spec_file_prefix
);
377 short_base_name
= xstrdup (spec_file_prefix
);
378 base_name
= XMALLOC (char,
379 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
380 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
382 /* Computes the extensions from the garmmar file name. */
383 ext_index
= get_extension_index (infile
);
384 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
385 that it is a yacc or bison grammar file. */
387 ext_index
= (strspn (infile
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
389 compute_exts_from_gf (infile
+ ext_index
);
394 /* If neither -o nor --file-prefix were specified, and the input
395 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
398 If --yacc is used, do as if the input file was `y.y'. */
400 const char *name_base
= yacc_flag
? "y.y" : infile
;
402 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
404 base_length
= strlen (name_base
);
406 ext_index
= get_extension_index (name_base
);
407 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
408 that it is a yacc or bison grammar file. */
410 ext_index
= (strspn (name_base
+ ext_index
+ 1, "yY")) ? ext_index
: 0;
413 base_length
-= strlen (name_base
+ ext_index
);
414 compute_exts_from_gf (name_base
+ ext_index
);
417 short_base_length
= base_length
;
418 short_base_name
= strndup (name_base
, short_base_length
);
420 base_name
= XMALLOC (char,
421 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
422 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
428 /*-------------------------------------------------------.
429 | Close the open files, compute the output files names. |
430 `-------------------------------------------------------*/
433 compute_output_file_names (void)
435 compute_base_names ();
437 /* If not yet done. */
439 src_extension
= ".c";
440 if (!header_extension
)
441 header_extension
= ".h";
443 /* It the defines filename if not given, we create it. */
444 if (!spec_defines_file
)
445 spec_defines_file
= stringappend (base_name
, header_extension
);
447 /* It the graph filename if not given, we create it. */
448 if (!spec_graph_file
)
449 spec_graph_file
= stringappend (short_base_name
, ".vcg");
451 spec_verbose_file
= stringappend (short_base_name
, EXT_OUTPUT
);
453 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
455 attrsfile
= stringappend (attrsfile
, header_extension
);
460 /*-----------------------------------------------------------------.
461 | Open the input file. Look for the skeletons. Find the names of |
462 | the output files. Prepare the obstacks. |
463 `-----------------------------------------------------------------*/
468 finput
= xfopen (infile
, "r");
470 /* Initialize the obstacks. */
471 obstack_init (&action_obstack
);
472 obstack_init (&attrs_obstack
);
473 obstack_init (&table_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 main file. */
500 obstack_save (&table_obstack
, spec_outfile
);
502 obstack_save (&table_obstack
, stringappend (base_name
, src_extension
));
503 obstack_free (&table_obstack
, NULL
);
505 /* Output the header file if wanted. */
507 defines_obstack_save (spec_defines_file
);
508 obstack_free (&defines_obstack
, NULL
);
510 /* If we output only the table, dump the actions in ACTFILE. */
512 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
513 obstack_free (&action_obstack
, NULL
);
515 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
516 into its own file, ATTTRSFILE. */
521 obstack_save (&attrs_obstack
, attrsfile
);
522 obstack_free (&attrs_obstack
, NULL
);
523 temp_name
= stringappend (short_base_name
, EXT_GUARD_C
);
525 temp_name
= stringappend (temp_name
, src_extension
);
527 obstack_save (&guard_obstack
, temp_name
);
528 obstack_free (&guard_obstack
, NULL
);