]>
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 | 39 | char *spec_outfile = NULL; /* for -o. */ |
f0130d39 AD |
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 | |
f0130d39 | 49 | /* C source file extension (the parser source). */ |
7333d403 | 50 | const char *src_extension = NULL; |
f0130d39 | 51 | /* Header file extension (if option ``-d'' is specified). */ |
7333d403 | 52 | const char *header_extension = NULL; |
54bd0db4 | 53 | \f |
f0130d39 | 54 | |
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 | ||
0b8afb77 AD |
87 | |
88 | /*---------------------------------------------------------------. | |
89 | | Computes the macro name used to avoid double inclusion in the | | |
90 | | header of the parser and store it in header_macro_name. | | |
91 | `---------------------------------------------------------------*/ | |
92 | ||
e795bd73 | 93 | static char * |
0b8afb77 AD |
94 | compute_header_macro (void) |
95 | { | |
96 | int ite; | |
97 | char *macro_name; | |
98 | ||
99 | macro_name = XMALLOC (char, | |
100 | strlen (base_name) + | |
101 | strlen (header_extension) + 1); | |
102 | ||
103 | stpcpy (macro_name, base_name); | |
104 | strcat (macro_name, header_extension); | |
105 | ||
106 | for (ite = 0; macro_name[ite]; ite++) | |
107 | if (macro_name[ite] == '.') | |
108 | macro_name[ite] = '_'; | |
109 | else | |
110 | { | |
111 | if (islower (macro_name[ite])) | |
112 | macro_name[ite] -= ('a' - 'A'); | |
113 | } | |
114 | return macro_name; | |
115 | } | |
116 | ||
117 | ||
cfe5fbc0 AD |
118 | /*-----------------------------------------------------------------. |
119 | | Try to open file NAME with mode MODE, and print an error message | | |
120 | | if fails. | | |
121 | `-----------------------------------------------------------------*/ | |
54bd0db4 | 122 | |
ff61dabd | 123 | FILE * |
8963a27b | 124 | xfopen (const char *name, const char *mode) |
cfe5fbc0 | 125 | { |
8963a27b | 126 | FILE *ptr; |
cfe5fbc0 AD |
127 | |
128 | ptr = fopen (name, mode); | |
129 | if (!ptr) | |
130 | error (2, errno, _("cannot open file `%s'"), name); | |
131 | ||
132 | return ptr; | |
133 | } | |
134 | ||
135 | /*-------------------------------------------------------------. | |
136 | | Try to close file PTR, and print an error message if fails. | | |
137 | `-------------------------------------------------------------*/ | |
138 | ||
ff61dabd | 139 | int |
8963a27b | 140 | xfclose (FILE *ptr) |
cfe5fbc0 AD |
141 | { |
142 | int result; | |
143 | ||
144 | if (ptr == NULL) | |
145 | return 0; | |
146 | ||
147 | result = fclose (ptr); | |
148 | if (result == EOF) | |
149 | error (2, errno, _("cannot close file")); | |
150 | ||
151 | return result; | |
152 | } | |
d8880f69 AD |
153 | |
154 | /*--------------------------------------------------. | |
155 | | Save the content of the obstack OBS in FILENAME. | | |
156 | `--------------------------------------------------*/ | |
157 | ||
9311529b | 158 | static void |
d8880f69 AD |
159 | obstack_save (struct obstack *obs, const char *filename) |
160 | { | |
161 | FILE *out = xfopen (filename, "w"); | |
162 | size_t size = obstack_object_size (obs); | |
163 | fwrite (obstack_finish (obs), 1, size, out); | |
164 | xfclose (out); | |
165 | } | |
166 | ||
0b8afb77 AD |
167 | /*---------------------------------------------------------------------. |
168 | | Output double inclusion protection macros and saves defines_obstack | | |
169 | `---------------------------------------------------------------------*/ | |
170 | ||
171 | static void | |
172 | defines_obstack_save (const char *filename) | |
173 | { | |
174 | FILE *out = xfopen (filename, "w"); | |
175 | size_t size = obstack_object_size (&defines_obstack); | |
176 | char *macro_name = compute_header_macro (); | |
177 | ||
178 | fprintf (out, "#ifndef %s\n", macro_name); | |
179 | fprintf (out, "# define %s\n\n", macro_name); | |
180 | fwrite (obstack_finish (&defines_obstack), 1, size, out); | |
181 | fprintf (out, "\n#endif /* not %s */\n", macro_name); | |
182 | ||
183 | free (macro_name); | |
184 | xfclose (out); | |
185 | } | |
9311529b | 186 | |
ff61dabd AD |
187 | /*------------------------------------------------------------------. |
188 | | Return the path to the skeleton which locaction might be given in | | |
b0ce6046 | 189 | | ENVVAR, otherwise return SKELETON_NAME. | |
ff61dabd AD |
190 | `------------------------------------------------------------------*/ |
191 | ||
192 | const char * | |
b0ce6046 | 193 | skeleton_find (const char *envvar, const char *skeleton_name) |
9311529b AD |
194 | { |
195 | const char *res = getenv (envvar); | |
196 | ||
197 | #ifdef MSDOS | |
198 | const char *cp; | |
199 | ||
200 | /* File doesn't exist in current directory; try in INIT directory. */ | |
201 | if (!res && (cp = getenv ("INIT"))) | |
202 | { | |
b0ce6046 AD |
203 | res = XMALLOC (char, strlen (cp) + strlen (skeleton_name) + 2); |
204 | sprintf (res, "%s%c%s", cp, '/', skeleton_name); | |
9311529b AD |
205 | } |
206 | #endif /* !MSDOS */ | |
207 | ||
208 | if (!res) | |
b0ce6046 | 209 | res = skeleton_name; |
9311529b AD |
210 | |
211 | return res; | |
212 | } | |
cfe5fbc0 | 213 | \f |
f0130d39 | 214 | |
234a3be3 AD |
215 | /*----------------------------------------------------------------. |
216 | | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. | | |
217 | `----------------------------------------------------------------*/ | |
218 | ||
b0ce6046 | 219 | /* Replace all characters FROM by TO in the string IN. |
f0130d39 | 220 | and returns a new allocated string. */ |
234a3be3 | 221 | static char * |
f0130d39 | 222 | tr (const char *in, char from, char to) |
234a3be3 AD |
223 | { |
224 | char *temp; | |
225 | char *out; | |
b0ce6046 | 226 | |
234a3be3 AD |
227 | out = XMALLOC (char, strlen (in) + 1); |
228 | ||
229 | for (temp = out; *in; in++, out++) | |
230 | if (*in == from) | |
231 | *out = to; | |
232 | else | |
233 | *out = *in; | |
234 | *out = 0; | |
235 | return (temp); | |
236 | } | |
237 | ||
f0130d39 AD |
238 | /* Gets the extension index in FILENAME. Returns 0 if fails to |
239 | find an extension. */ | |
b0ce6046 | 240 | static int |
f0130d39 | 241 | get_extension_index (const char *filename) |
234a3be3 | 242 | { |
f0130d39 | 243 | int len; |
234a3be3 AD |
244 | |
245 | len = strlen (filename); | |
b0ce6046 | 246 | |
234a3be3 AD |
247 | if (filename[len-- - 1] == '.') |
248 | return (0); | |
249 | ||
250 | while ((len > 0) && (filename[len - 1] != '.')) | |
251 | if (filename[len - 1] == '/') | |
252 | return (0); | |
253 | else | |
254 | len--; | |
255 | ||
256 | return (len - 1); | |
257 | } | |
258 | ||
259 | /* Computes extensions from the grammar file extension. */ | |
260 | static void | |
f0130d39 | 261 | compute_exts_from_gf (const char *ext) |
234a3be3 | 262 | { |
f0130d39 | 263 | /* Checks if SRC_EXTENSION is NULL. In the other case, %source_extension |
4ecbf796 MA |
264 | was specified in the grammar file. */ |
265 | if (src_extension == NULL) | |
7333d403 | 266 | { |
f0130d39 AD |
267 | src_extension = tr (ext, 'y', 'c'); |
268 | src_extension = tr (src_extension, 'Y', 'C'); | |
22c2cbc0 | 269 | } |
f0130d39 | 270 | /* Checks if HEADER_EXTENSION is NULL. In the other case, |
4ecbf796 MA |
271 | %header_extension was specified in the grammar file. */ |
272 | if (header_extension == NULL) | |
7333d403 | 273 | { |
f0130d39 AD |
274 | header_extension = tr (ext, 'y', 'h'); |
275 | header_extension = tr (header_extension, 'Y', 'H'); | |
7333d403 | 276 | } |
234a3be3 AD |
277 | } |
278 | ||
279 | /* Computes extensions from the given c source file extension. */ | |
280 | static void | |
f0130d39 | 281 | compute_exts_from_src (const char *ext) |
234a3be3 | 282 | { |
4ecbf796 MA |
283 | /* We use this function when the user specifies `-o' or `--output', |
284 | so the extenions must be computed unconditionally from the file name | |
285 | given by this option. */ | |
f0130d39 AD |
286 | src_extension = xstrdup (ext); |
287 | header_extension = tr (ext, 'c', 'h'); | |
288 | header_extension = tr (header_extension, 'C', 'H'); | |
234a3be3 | 289 | } |
d8880f69 | 290 | |
19c50364 AD |
291 | /* FIXME: Should use xstrndup. */ |
292 | ||
293 | static void | |
27110317 | 294 | compute_base_names (void) |
54bd0db4 | 295 | { |
19c50364 AD |
296 | size_t base_length; |
297 | size_t short_base_length; | |
234a3be3 | 298 | size_t ext_index; |
b0ce6046 | 299 | |
19c50364 AD |
300 | /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c), |
301 | BASE_NAME and SHORT_BASE_NAME are `foo'. | |
54bd0db4 | 302 | |
19c50364 AD |
303 | If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and |
304 | SHORT_BASE_NAME is `foo'. | |
305 | ||
306 | The precise -o name will be used for FTABLE. For other output | |
307 | files, remove the ".c" or ".tab.c" suffix. */ | |
54bd0db4 RS |
308 | if (spec_outfile) |
309 | { | |
54bd0db4 | 310 | #ifdef MSDOS |
19c50364 | 311 | strlwr (spec_outfile); |
54bd0db4 RS |
312 | #endif /* MSDOS */ |
313 | /* BASE_LENGTH includes ".tab" but not ".c". */ | |
19c50364 | 314 | base_length = strlen (spec_outfile); |
b0ce6046 | 315 | |
234a3be3 | 316 | ext_index = get_extension_index (spec_outfile); |
f0130d39 AD |
317 | /* If the initial segment of extension contains 'c' or a 'C', I assume |
318 | that it is a C or C++ source file. */ | |
234a3be3 | 319 | if (ext_index) |
f0130d39 AD |
320 | ext_index = |
321 | (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0; | |
234a3be3 AD |
322 | if (ext_index) |
323 | { | |
324 | base_length -= strlen (spec_outfile + ext_index); | |
95fb5662 | 325 | compute_exts_from_src (spec_outfile + ext_index); |
234a3be3 | 326 | } |
b0ce6046 | 327 | |
956dba3a | 328 | base_name = strndup (spec_outfile, base_length); |
54bd0db4 RS |
329 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ |
330 | short_base_length = base_length; | |
956dba3a | 331 | if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab")) |
54bd0db4 | 332 | short_base_length -= 4; |
27110317 | 333 | short_base_name = strndup (spec_outfile, short_base_length); |
19c50364 AD |
334 | |
335 | return; | |
54bd0db4 | 336 | } |
19c50364 AD |
337 | |
338 | /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME | |
339 | are `foo'. | |
340 | ||
341 | Construct names from it. */ | |
342 | if (spec_file_prefix) | |
54bd0db4 | 343 | { |
54bd0db4 | 344 | #ifdef MSDOS |
19c50364 | 345 | strlwr (spec_file_prefix); |
54bd0db4 | 346 | #endif /* MSDOS */ |
27110317 | 347 | short_base_name = xstrdup (spec_file_prefix); |
27110317 AD |
348 | base_name = XMALLOC (char, |
349 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
350 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 | 351 | |
95fb5662 MA |
352 | /* Computes the extensions from the garmmar file name. */ |
353 | ext_index = get_extension_index (infile); | |
f0130d39 AD |
354 | /* If the initial segment of extension contains a 'y' or a 'Y', I assume |
355 | that it is a yacc or bison grammar file. */ | |
95fb5662 MA |
356 | if (ext_index) |
357 | ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0; | |
358 | if (ext_index) | |
359 | compute_exts_from_gf (infile + ext_index); | |
f0130d39 | 360 | |
19c50364 | 361 | return; |
54bd0db4 | 362 | } |
54bd0db4 | 363 | |
19c50364 AD |
364 | /* If neither -o nor --file-prefix were specified, and the input |
365 | file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is | |
366 | `foo'. | |
4a120d45 | 367 | |
19c50364 AD |
368 | If --yacc is used, do as if the input file was `y.y'. */ |
369 | { | |
370 | const char *name_base = yacc_flag ? "y.y" : infile; | |
54bd0db4 | 371 | |
19c50364 | 372 | /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */ |
54bd0db4 | 373 | |
19c50364 | 374 | base_length = strlen (name_base); |
b0ce6046 | 375 | |
234a3be3 | 376 | ext_index = get_extension_index (name_base); |
f0130d39 AD |
377 | /* If the initial segment of extension contains a 'y' or a 'Y', I assume |
378 | that it is a yacc or bison grammar file. */ | |
234a3be3 | 379 | if (ext_index) |
95fb5662 | 380 | ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0; |
234a3be3 AD |
381 | if (ext_index) |
382 | { | |
383 | base_length -= strlen (name_base + ext_index); | |
95fb5662 | 384 | compute_exts_from_gf (name_base + ext_index); |
234a3be3 AD |
385 | } |
386 | ||
19c50364 | 387 | short_base_length = base_length; |
27110317 | 388 | short_base_name = strndup (name_base, short_base_length); |
54bd0db4 | 389 | |
27110317 AD |
390 | base_name = XMALLOC (char, |
391 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
392 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 AD |
393 | |
394 | return; | |
395 | } | |
396 | } | |
397 | ||
398 | /*-----------------------------------------------------------------. | |
399 | | Open the input file. Look for the skeletons. Find the names of | | |
400 | | the output files. Prepare the obstacks. | | |
401 | `-----------------------------------------------------------------*/ | |
402 | ||
403 | void | |
404 | open_files (void) | |
405 | { | |
8963a27b | 406 | finput = xfopen (infile, "r"); |
54bd0db4 | 407 | |
dd60faec | 408 | /* Initialize the obstacks. */ |
8c7ebe49 | 409 | obstack_init (&action_obstack); |
dd60faec | 410 | obstack_init (&attrs_obstack); |
896fe5c1 AD |
411 | obstack_init (&table_obstack); |
412 | obstack_init (&defines_obstack); | |
ea5607fd | 413 | obstack_init (&guard_obstack); |
ff4423cc | 414 | obstack_init (&output_obstack); |
c4b66126 | 415 | obstack_init (&graph_obstack); |
54bd0db4 RS |
416 | } |
417 | ||
418 | ||
419 | ||
d8880f69 AD |
420 | /*-----------------------------------------------------. |
421 | | Close the open files, produce all the output files. | | |
422 | `-----------------------------------------------------*/ | |
423 | ||
a693bf18 | 424 | void |
d8880f69 | 425 | output_files (void) |
a693bf18 | 426 | { |
8963a27b | 427 | xfclose (finput); |
54bd0db4 | 428 | |
6deb4447 | 429 | compute_base_names (); |
234a3be3 | 430 | |
7333d403 AD |
431 | /* Set default extensions */ |
432 | if (!src_extension) | |
433 | src_extension = ".c"; | |
434 | if (!header_extension) | |
435 | header_extension = ".h"; | |
436 | ||
6deb4447 | 437 | attrsfile = stringappend (short_base_name, EXT_STYPE_H); |
234a3be3 AD |
438 | #ifndef MSDOS |
439 | stringappend (attrsfile, header_extension); | |
b0ce6046 | 440 | #endif /* MSDOS */ |
6deb4447 | 441 | |
d8880f69 | 442 | /* Output the main file. */ |
27110317 AD |
443 | if (spec_outfile) |
444 | obstack_save (&table_obstack, spec_outfile); | |
445 | else | |
f0130d39 | 446 | obstack_save (&table_obstack, stringappend (base_name, src_extension)); |
d8880f69 AD |
447 | |
448 | /* Output the header file if wanted. */ | |
449 | if (defines_flag) | |
0b8afb77 | 450 | defines_obstack_save (stringappend (base_name, header_extension)); |
54bd0db4 | 451 | |
27110317 | 452 | /* If we output only the table, dump the actions in ACTFILE. */ |
8c7ebe49 | 453 | if (no_parser_flag) |
27110317 | 454 | obstack_save (&action_obstack, stringappend (short_base_name, ".act")); |
dd60faec AD |
455 | |
456 | /* If we produced a semantic parser ATTRS_OBSTACK must be dumped | |
457 | into its own file, ATTTRSFILE. */ | |
458 | if (semantic_parser) | |
ea5607fd | 459 | { |
234a3be3 AD |
460 | char *temp_name; |
461 | ||
ea5607fd | 462 | obstack_save (&attrs_obstack, attrsfile); |
234a3be3 AD |
463 | temp_name = stringappend (short_base_name, EXT_GUARD_C); |
464 | #ifndef MSDOS | |
465 | temp_name = stringappend (temp_name, src_extension); | |
466 | #endif /* MSDOS */ | |
467 | obstack_save (&guard_obstack, temp_name); | |
ea5607fd | 468 | } |
ff4423cc AD |
469 | |
470 | if (verbose_flag) | |
471 | /* We used to use just .out if spec_name_prefix (-p) was used, but | |
472 | that conflicts with Posix. */ | |
f0130d39 AD |
473 | obstack_save (&output_obstack, |
474 | stringappend (short_base_name, EXT_OUTPUT)); | |
c4b66126 AD |
475 | |
476 | if (graph_flag) | |
477 | obstack_save (&graph_obstack, stringappend (short_base_name, ".vcg")); | |
54bd0db4 | 478 | } |