]>
Commit | Line | Data |
---|---|---|
54bd0db4 | 1 | /* Open and close files for bison, |
22c2cbc0 | 2 | Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc. |
54bd0db4 | 3 | |
ceed8467 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
54bd0db4 | 5 | |
ceed8467 AD |
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) | |
9 | any later version. | |
54bd0db4 | 10 | |
ceed8467 AD |
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. | |
54bd0db4 | 15 | |
ceed8467 AD |
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 | |
19 | 02111-1307, USA. */ | |
54bd0db4 RS |
20 | |
21 | ||
9eceb6c6 | 22 | #include "system.h" |
ceed8467 | 23 | #include "getargs.h" |
54bd0db4 | 24 | #include "files.h" |
d7913476 | 25 | #include "xalloc.h" |
54bd0db4 | 26 | #include "gram.h" |
a0f6b076 | 27 | #include "complain.h" |
54bd0db4 RS |
28 | |
29 | FILE *finput = NULL; | |
54bd0db4 | 30 | |
8c7ebe49 | 31 | struct obstack action_obstack; |
dd60faec | 32 | struct obstack attrs_obstack; |
896fe5c1 AD |
33 | struct obstack table_obstack; |
34 | struct obstack defines_obstack; | |
ea5607fd | 35 | struct obstack guard_obstack; |
ff4423cc | 36 | struct obstack output_obstack; |
22c2cbc0 | 37 | struct obstack graph_obstack; |
8c7ebe49 | 38 | |
b0ce6046 AD |
39 | char *spec_outfile = NULL; /* for -o. */ |
40 | char *spec_file_prefix = NULL; /* for -b. */ | |
41 | char *spec_name_prefix = NULL; /* for -p. */ | |
54bd0db4 | 42 | |
b0ce6046 AD |
43 | char *infile = NULL; |
44 | char *attrsfile = NULL; | |
4a120d45 | 45 | |
b0ce6046 AD |
46 | static char *base_name = NULL; |
47 | static char *short_base_name = NULL; | |
27110317 | 48 | |
234a3be3 | 49 | /* C source file extension (the parser source). */ |
7333d403 | 50 | const char *src_extension = NULL; |
234a3be3 | 51 | /* Header file extension (if option ``-d'' is specified). */ |
7333d403 | 52 | const char *header_extension = NULL; |
234a3be3 | 53 | |
54bd0db4 | 54 | \f |
19c50364 AD |
55 | /*--------------------------. |
56 | | Is SUFFIX ending STRING? | | |
57 | `--------------------------*/ | |
58 | ||
59 | static int | |
60 | strsuffix (const char *string, const char *suffix) | |
61 | { | |
62 | size_t string_len = strlen (string); | |
63 | size_t suffix_len = strlen (suffix); | |
64 | if (suffix_len <= string_len) | |
65 | return !strcmp (string + string_len - suffix_len, suffix); | |
66 | else | |
67 | return 0; | |
68 | } | |
69 | ||
70 | ||
358c15b7 AD |
71 | /*-----------------------------------------------------------------. |
72 | | Return a newly allocated string composed of the concatenation of | | |
19c50364 | 73 | | STRING1, and STRING2. | |
358c15b7 | 74 | `-----------------------------------------------------------------*/ |
54bd0db4 | 75 | |
8963a27b | 76 | static char * |
19c50364 | 77 | stringappend (const char *string1, const char *string2) |
54bd0db4 | 78 | { |
19c50364 AD |
79 | size_t len = strlen (string1) + strlen (string2); |
80 | char *res = XMALLOC (char, len + 1); | |
358c15b7 | 81 | char *cp; |
19c50364 AD |
82 | cp = stpcpy (res, string1); |
83 | cp = stpcpy (cp, string2); | |
358c15b7 | 84 | return res; |
54bd0db4 RS |
85 | } |
86 | ||
cfe5fbc0 AD |
87 | /*-----------------------------------------------------------------. |
88 | | Try to open file NAME with mode MODE, and print an error message | | |
89 | | if fails. | | |
90 | `-----------------------------------------------------------------*/ | |
54bd0db4 | 91 | |
ff61dabd | 92 | FILE * |
8963a27b | 93 | xfopen (const char *name, const char *mode) |
cfe5fbc0 | 94 | { |
8963a27b | 95 | FILE *ptr; |
cfe5fbc0 AD |
96 | |
97 | ptr = fopen (name, mode); | |
98 | if (!ptr) | |
99 | error (2, errno, _("cannot open file `%s'"), name); | |
100 | ||
101 | return ptr; | |
102 | } | |
103 | ||
104 | /*-------------------------------------------------------------. | |
105 | | Try to close file PTR, and print an error message if fails. | | |
106 | `-------------------------------------------------------------*/ | |
107 | ||
ff61dabd | 108 | int |
8963a27b | 109 | xfclose (FILE *ptr) |
cfe5fbc0 AD |
110 | { |
111 | int result; | |
112 | ||
113 | if (ptr == NULL) | |
114 | return 0; | |
115 | ||
116 | result = fclose (ptr); | |
117 | if (result == EOF) | |
118 | error (2, errno, _("cannot close file")); | |
119 | ||
120 | return result; | |
121 | } | |
d8880f69 AD |
122 | |
123 | /*--------------------------------------------------. | |
124 | | Save the content of the obstack OBS in FILENAME. | | |
125 | `--------------------------------------------------*/ | |
126 | ||
9311529b | 127 | static void |
d8880f69 AD |
128 | obstack_save (struct obstack *obs, const char *filename) |
129 | { | |
130 | FILE *out = xfopen (filename, "w"); | |
131 | size_t size = obstack_object_size (obs); | |
132 | fwrite (obstack_finish (obs), 1, size, out); | |
133 | xfclose (out); | |
134 | } | |
135 | ||
9311529b | 136 | |
ff61dabd AD |
137 | /*------------------------------------------------------------------. |
138 | | Return the path to the skeleton which locaction might be given in | | |
b0ce6046 | 139 | | ENVVAR, otherwise return SKELETON_NAME. | |
ff61dabd AD |
140 | `------------------------------------------------------------------*/ |
141 | ||
142 | const char * | |
b0ce6046 | 143 | skeleton_find (const char *envvar, const char *skeleton_name) |
9311529b AD |
144 | { |
145 | const char *res = getenv (envvar); | |
146 | ||
147 | #ifdef MSDOS | |
148 | const char *cp; | |
149 | ||
150 | /* File doesn't exist in current directory; try in INIT directory. */ | |
151 | if (!res && (cp = getenv ("INIT"))) | |
152 | { | |
b0ce6046 AD |
153 | res = XMALLOC (char, strlen (cp) + strlen (skeleton_name) + 2); |
154 | sprintf (res, "%s%c%s", cp, '/', skeleton_name); | |
9311529b AD |
155 | } |
156 | #endif /* !MSDOS */ | |
157 | ||
158 | if (!res) | |
b0ce6046 | 159 | res = skeleton_name; |
9311529b AD |
160 | |
161 | return res; | |
162 | } | |
163 | ||
cfe5fbc0 | 164 | \f |
234a3be3 AD |
165 | /*----------------------------------------------------------------. |
166 | | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. | | |
167 | `----------------------------------------------------------------*/ | |
168 | ||
b0ce6046 AD |
169 | /* Replace all characters FROM by TO in the string IN. |
170 | and returns a new allocated string. */ | |
234a3be3 AD |
171 | static char * |
172 | tr(const char *in, char from, char to) | |
173 | { | |
174 | char *temp; | |
175 | char *out; | |
b0ce6046 | 176 | |
234a3be3 AD |
177 | out = XMALLOC (char, strlen (in) + 1); |
178 | ||
179 | for (temp = out; *in; in++, out++) | |
180 | if (*in == from) | |
181 | *out = to; | |
182 | else | |
183 | *out = *in; | |
184 | *out = 0; | |
185 | return (temp); | |
186 | } | |
187 | ||
b0ce6046 | 188 | /* Gets the extension index in FILENAME. Returns 0 if fails to |
234a3be3 | 189 | find an extension. */ |
b0ce6046 | 190 | static int |
234a3be3 AD |
191 | get_extension_index(const char *filename) |
192 | { | |
193 | int len; | |
194 | ||
195 | len = strlen (filename); | |
b0ce6046 | 196 | |
234a3be3 AD |
197 | if (filename[len-- - 1] == '.') |
198 | return (0); | |
199 | ||
200 | while ((len > 0) && (filename[len - 1] != '.')) | |
201 | if (filename[len - 1] == '/') | |
202 | return (0); | |
203 | else | |
204 | len--; | |
205 | ||
206 | return (len - 1); | |
207 | } | |
208 | ||
209 | /* Computes extensions from the grammar file extension. */ | |
210 | static void | |
211 | compute_exts_from_gf(const char *ext) | |
212 | { | |
7333d403 AD |
213 | if (!src_extension) |
214 | { | |
215 | src_extension = tr(ext, 'y', 'c'); | |
216 | src_extension = tr(src_extension, 'Y', 'C'); | |
22c2cbc0 | 217 | } |
7333d403 AD |
218 | if (!header_extension) |
219 | { | |
220 | header_extension = tr(ext, 'y', 'h'); | |
221 | header_extension = tr(header_extension, 'Y', 'H'); | |
222 | } | |
234a3be3 AD |
223 | } |
224 | ||
225 | /* Computes extensions from the given c source file extension. */ | |
226 | static void | |
227 | compute_exts_from_src(const char *ext) | |
228 | { | |
7333d403 AD |
229 | if (!src_extension) |
230 | src_extension = xstrdup(ext); | |
231 | if (!header_extension) | |
22c2cbc0 | 232 | { |
7333d403 AD |
233 | header_extension = tr(ext, 'c', 'h'); |
234 | header_extension = tr(header_extension, 'C', 'H'); | |
235 | } | |
234a3be3 | 236 | } |
d8880f69 | 237 | |
19c50364 AD |
238 | /* FIXME: Should use xstrndup. */ |
239 | ||
240 | static void | |
27110317 | 241 | compute_base_names (void) |
54bd0db4 | 242 | { |
19c50364 AD |
243 | size_t base_length; |
244 | size_t short_base_length; | |
234a3be3 | 245 | size_t ext_index; |
b0ce6046 | 246 | |
19c50364 AD |
247 | /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c), |
248 | BASE_NAME and SHORT_BASE_NAME are `foo'. | |
54bd0db4 | 249 | |
19c50364 AD |
250 | If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and |
251 | SHORT_BASE_NAME is `foo'. | |
252 | ||
253 | The precise -o name will be used for FTABLE. For other output | |
254 | files, remove the ".c" or ".tab.c" suffix. */ | |
54bd0db4 RS |
255 | if (spec_outfile) |
256 | { | |
54bd0db4 | 257 | #ifdef MSDOS |
19c50364 | 258 | strlwr (spec_outfile); |
54bd0db4 RS |
259 | #endif /* MSDOS */ |
260 | /* BASE_LENGTH includes ".tab" but not ".c". */ | |
19c50364 | 261 | base_length = strlen (spec_outfile); |
b0ce6046 | 262 | |
234a3be3 | 263 | ext_index = get_extension_index (spec_outfile); |
b0ce6046 | 264 | /* if the initial segment of extension contains 'c' or a 'C', I assume |
234a3be3 AD |
265 | that it is a C or C++ source file */ |
266 | if (ext_index) | |
267 | ext_index = (strspn(spec_outfile + ext_index + 1, "cC")) ? ext_index : 0; | |
268 | if (ext_index) | |
269 | { | |
270 | base_length -= strlen (spec_outfile + ext_index); | |
271 | compute_exts_from_src(spec_outfile + ext_index); | |
272 | } | |
b0ce6046 | 273 | |
956dba3a | 274 | base_name = strndup (spec_outfile, base_length); |
54bd0db4 RS |
275 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ |
276 | short_base_length = base_length; | |
956dba3a | 277 | if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab")) |
54bd0db4 | 278 | short_base_length -= 4; |
27110317 | 279 | short_base_name = strndup (spec_outfile, short_base_length); |
19c50364 AD |
280 | |
281 | return; | |
54bd0db4 | 282 | } |
19c50364 AD |
283 | |
284 | /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME | |
285 | are `foo'. | |
286 | ||
287 | Construct names from it. */ | |
288 | if (spec_file_prefix) | |
54bd0db4 | 289 | { |
54bd0db4 | 290 | #ifdef MSDOS |
19c50364 | 291 | strlwr (spec_file_prefix); |
54bd0db4 | 292 | #endif /* MSDOS */ |
27110317 | 293 | short_base_name = xstrdup (spec_file_prefix); |
27110317 AD |
294 | base_name = XMALLOC (char, |
295 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
296 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 AD |
297 | |
298 | return; | |
54bd0db4 | 299 | } |
54bd0db4 | 300 | |
19c50364 AD |
301 | /* If neither -o nor --file-prefix were specified, and the input |
302 | file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is | |
303 | `foo'. | |
4a120d45 | 304 | |
19c50364 AD |
305 | If --yacc is used, do as if the input file was `y.y'. */ |
306 | { | |
307 | const char *name_base = yacc_flag ? "y.y" : infile; | |
54bd0db4 | 308 | |
19c50364 | 309 | /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */ |
54bd0db4 | 310 | |
19c50364 | 311 | base_length = strlen (name_base); |
b0ce6046 | 312 | |
234a3be3 | 313 | ext_index = get_extension_index (name_base); |
b0ce6046 | 314 | /* if the initial segment of extension contains a 'y' or a 'Y', I assume |
234a3be3 AD |
315 | that it is a yacc or bison grammar file */ |
316 | if (ext_index) | |
317 | ext_index = (strspn(name_base + ext_index + 1, "yY")) ? ext_index : 0; | |
318 | if (ext_index) | |
319 | { | |
320 | base_length -= strlen (name_base + ext_index); | |
321 | compute_exts_from_gf(name_base + ext_index); | |
322 | } | |
323 | ||
19c50364 | 324 | short_base_length = base_length; |
27110317 | 325 | short_base_name = strndup (name_base, short_base_length); |
54bd0db4 | 326 | |
27110317 AD |
327 | base_name = XMALLOC (char, |
328 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
329 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 AD |
330 | |
331 | return; | |
332 | } | |
333 | } | |
334 | ||
335 | /*-----------------------------------------------------------------. | |
336 | | Open the input file. Look for the skeletons. Find the names of | | |
337 | | the output files. Prepare the obstacks. | | |
338 | `-----------------------------------------------------------------*/ | |
339 | ||
340 | void | |
341 | open_files (void) | |
342 | { | |
8963a27b | 343 | finput = xfopen (infile, "r"); |
54bd0db4 | 344 | |
dd60faec | 345 | /* Initialize the obstacks. */ |
8c7ebe49 | 346 | obstack_init (&action_obstack); |
dd60faec | 347 | obstack_init (&attrs_obstack); |
896fe5c1 AD |
348 | obstack_init (&table_obstack); |
349 | obstack_init (&defines_obstack); | |
ea5607fd | 350 | obstack_init (&guard_obstack); |
ff4423cc | 351 | obstack_init (&output_obstack); |
54bd0db4 RS |
352 | } |
353 | ||
354 | ||
355 | ||
d8880f69 AD |
356 | /*-----------------------------------------------------. |
357 | | Close the open files, produce all the output files. | | |
358 | `-----------------------------------------------------*/ | |
359 | ||
a693bf18 | 360 | void |
d8880f69 | 361 | output_files (void) |
a693bf18 | 362 | { |
8963a27b | 363 | xfclose (finput); |
54bd0db4 | 364 | |
6deb4447 | 365 | compute_base_names (); |
234a3be3 | 366 | |
7333d403 AD |
367 | /* Set default extensions */ |
368 | if (!src_extension) | |
369 | src_extension = ".c"; | |
370 | if (!header_extension) | |
371 | header_extension = ".h"; | |
372 | ||
6deb4447 | 373 | attrsfile = stringappend (short_base_name, EXT_STYPE_H); |
234a3be3 AD |
374 | #ifndef MSDOS |
375 | stringappend (attrsfile, header_extension); | |
b0ce6046 | 376 | #endif /* MSDOS */ |
6deb4447 | 377 | |
d8880f69 | 378 | /* Output the main file. */ |
27110317 AD |
379 | if (spec_outfile) |
380 | obstack_save (&table_obstack, spec_outfile); | |
381 | else | |
234a3be3 | 382 | obstack_save (&table_obstack, stringappend (base_name, src_extension)); |
d8880f69 AD |
383 | |
384 | /* Output the header file if wanted. */ | |
385 | if (defines_flag) | |
234a3be3 | 386 | obstack_save (&defines_obstack, stringappend (base_name, header_extension)); |
54bd0db4 | 387 | |
27110317 | 388 | /* If we output only the table, dump the actions in ACTFILE. */ |
8c7ebe49 | 389 | if (no_parser_flag) |
27110317 | 390 | obstack_save (&action_obstack, stringappend (short_base_name, ".act")); |
dd60faec AD |
391 | |
392 | /* If we produced a semantic parser ATTRS_OBSTACK must be dumped | |
393 | into its own file, ATTTRSFILE. */ | |
394 | if (semantic_parser) | |
ea5607fd | 395 | { |
234a3be3 AD |
396 | char *temp_name; |
397 | ||
ea5607fd | 398 | obstack_save (&attrs_obstack, attrsfile); |
234a3be3 AD |
399 | temp_name = stringappend (short_base_name, EXT_GUARD_C); |
400 | #ifndef MSDOS | |
401 | temp_name = stringappend (temp_name, src_extension); | |
402 | #endif /* MSDOS */ | |
403 | obstack_save (&guard_obstack, temp_name); | |
ea5607fd | 404 | } |
ff4423cc AD |
405 | |
406 | if (verbose_flag) | |
407 | /* We used to use just .out if spec_name_prefix (-p) was used, but | |
408 | that conflicts with Posix. */ | |
409 | obstack_save (&output_obstack, stringappend (short_base_name, EXT_OUTPUT)); | |
54bd0db4 | 410 | } |