]>
git.saurik.com Git - bison.git/blob - src/files.c
1 /* Open and close files for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000 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
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
;
38 char *spec_outfile
; /* for -o. */
39 char *spec_file_prefix
; /* for -b. */
40 char *spec_name_prefix
; /* for -p. */
45 static char *base_name
;
46 static char *short_base_name
;
49 /*--------------------------.
50 | Is SUFFIX ending STRING? |
51 `--------------------------*/
54 strsuffix (const char *string
, const char *suffix
)
56 size_t string_len
= strlen (string
);
57 size_t suffix_len
= strlen (suffix
);
58 if (suffix_len
<= string_len
)
59 return !strcmp (string
+ string_len
- suffix_len
, suffix
);
65 /*-----------------------------------------------------------------.
66 | Return a newly allocated string composed of the concatenation of |
67 | STRING1, and STRING2. |
68 `-----------------------------------------------------------------*/
71 stringappend (const char *string1
, const char *string2
)
73 size_t len
= strlen (string1
) + strlen (string2
);
74 char *res
= XMALLOC (char, len
+ 1);
76 cp
= stpcpy (res
, string1
);
77 cp
= stpcpy (cp
, string2
);
81 /*-----------------------------------------------------------------.
82 | Try to open file NAME with mode MODE, and print an error message |
84 `-----------------------------------------------------------------*/
87 xfopen (const char *name
, const char *mode
)
91 ptr
= fopen (name
, mode
);
93 error (2, errno
, _("cannot open file `%s'"), name
);
98 /*-------------------------------------------------------------.
99 | Try to close file PTR, and print an error message if fails. |
100 `-------------------------------------------------------------*/
110 result
= fclose (ptr
);
112 error (2, errno
, _("cannot close file"));
117 /*--------------------------------------------------.
118 | Save the content of the obstack OBS in FILENAME. |
119 `--------------------------------------------------*/
122 obstack_save (struct obstack
*obs
, const char *filename
)
124 FILE *out
= xfopen (filename
, "w");
125 size_t size
= obstack_object_size (obs
);
126 fwrite (obstack_finish (obs
), 1, size
, out
);
131 /*------------------------------------------------------------------.
132 | Return the path to the skeleton which locaction might be given in |
133 | ENVVAR, otherwise return SKELETON. |
134 `------------------------------------------------------------------*/
137 skeleton_find (const char *envvar
, const char *skeleton
)
139 const char *res
= getenv (envvar
);
144 /* File doesn't exist in current directory; try in INIT directory. */
145 if (!res
&& (cp
= getenv ("INIT")))
147 res
= XMALLOC (char, strlen (cp
) + strlen (skeleton
) + 2);
148 sprintf (res
, "%s%c%s", cp
, '/', skeleton
);
159 /*----------------------------------------.
160 | Compute BASE_NAME and SHORT_BASE_NAME. |
161 `----------------------------------------*/
163 /* FIXME: Should use xstrndup. */
166 compute_base_names (void)
169 size_t short_base_length
;
171 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
172 BASE_NAME and SHORT_BASE_NAME are `foo'.
174 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
175 SHORT_BASE_NAME is `foo'.
177 The precise -o name will be used for FTABLE. For other output
178 files, remove the ".c" or ".tab.c" suffix. */
182 strlwr (spec_outfile
);
184 /* BASE_LENGTH includes ".tab" but not ".c". */
185 base_length
= strlen (spec_outfile
);
186 if (strsuffix (spec_outfile
, ".c"))
188 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
189 short_base_length
= base_length
;
190 if (strsuffix (spec_outfile
, ".tab") || strsuffix (spec_outfile
, "_tab"))
191 short_base_length
-= 4;
192 base_name
= strndup (spec_outfile
, base_length
);
193 short_base_name
= strndup (spec_outfile
, short_base_length
);
198 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
201 Construct names from it. */
202 if (spec_file_prefix
)
205 strlwr (spec_file_prefix
);
207 short_base_name
= xstrdup (spec_file_prefix
);
209 base_name
= XMALLOC (char,
210 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
211 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
216 /* If neither -o nor --file-prefix were specified, and the input
217 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
220 If --yacc is used, do as if the input file was `y.y'. */
222 const char *name_base
= yacc_flag
? "y.y" : infile
;
224 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
226 base_length
= strlen (name_base
);
227 if (strsuffix (name_base
, ".y"))
229 short_base_length
= base_length
;
230 short_base_name
= strndup (name_base
, short_base_length
);
232 base_name
= XMALLOC (char,
233 strlen (short_base_name
) + strlen (EXT_TAB
) + 1);
234 stpcpy (stpcpy (base_name
, short_base_name
), EXT_TAB
);
240 /*-----------------------------------------------------------------.
241 | Open the input file. Look for the skeletons. Find the names of |
242 | the output files. Prepare the obstacks. |
243 `-----------------------------------------------------------------*/
248 compute_base_names ();
250 finput
= xfopen (infile
, "r");
253 /* We used to use just .out if spec_name_prefix (-p) was used, but
254 that conflicts with Posix. */
255 foutput
= xfopen (stringappend (base_name
, EXT_OUTPUT
), "w");
257 attrsfile
= stringappend (short_base_name
, EXT_STYPE_H
);
259 /* Initialize the obstacks. */
260 obstack_init (&action_obstack
);
261 obstack_init (&attrs_obstack
);
262 obstack_init (&table_obstack
);
263 obstack_init (&defines_obstack
);
264 obstack_init (&guard_obstack
);
269 /*-----------------------------------------------------.
270 | Close the open files, produce all the output files. |
271 `-----------------------------------------------------*/
279 /* Output the main file. */
281 obstack_save (&table_obstack
, spec_outfile
);
283 obstack_save (&table_obstack
, stringappend (base_name
, ".c"));
285 /* Output the header file if wanted. */
287 obstack_save (&defines_obstack
, stringappend (base_name
, ".h"));
289 /* If we output only the table, dump the actions in ACTFILE. */
291 obstack_save (&action_obstack
, stringappend (short_base_name
, ".act"));
293 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
294 into its own file, ATTTRSFILE. */
297 obstack_save (&attrs_obstack
, attrsfile
);
298 obstack_save (&guard_obstack
,
299 stringappend (short_base_name
, EXT_GUARD_C
));