]>
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" |
54bd0db4 | 25 | #include "gram.h" |
26cfe0be | 26 | #include "error.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; |
ff4423cc | 33 | struct obstack output_obstack; |
8c7ebe49 | 34 | |
b5b61c61 AD |
35 | /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is |
36 | tempting, but don't do that: for the time being our handling of the | |
37 | %directive vs --option leaves precedence to the options by deciding | |
38 | that if a %directive sets a variable which is really set (i.e., not | |
39 | NULL), then the %directive is ignored. As a result, %name-prefix, | |
40 | for instance, will not be honored. */ | |
41 | ||
b0ce6046 | 42 | char *spec_outfile = NULL; /* for -o. */ |
f0130d39 | 43 | char *spec_file_prefix = NULL; /* for -b. */ |
b5b61c61 | 44 | const char *spec_name_prefix = NULL; /* for -p. */ |
342b8b6e AD |
45 | char *spec_verbose_file = NULL; /* for --verbose. */ |
46 | char *spec_graph_file = NULL; /* for -g. */ | |
47 | char *spec_defines_file = NULL; /* for --defines. */ | |
ea52d706 | 48 | char *parser_file_name = NULL; |
54bd0db4 | 49 | |
b0ce6046 AD |
50 | char *infile = NULL; |
51 | char *attrsfile = NULL; | |
4a120d45 | 52 | |
b0ce6046 | 53 | static char *base_name = NULL; |
9b3add5b | 54 | char *short_base_name = NULL; |
27110317 | 55 | |
f0130d39 | 56 | /* C source file extension (the parser source). */ |
7333d403 | 57 | const char *src_extension = NULL; |
f0130d39 | 58 | /* Header file extension (if option ``-d'' is specified). */ |
7333d403 | 59 | const char *header_extension = NULL; |
9b3add5b RA |
60 | |
61 | /* Should we insert '.tab' in yacc-compatible parsers? */ | |
62 | int tab_extension = 0; | |
54bd0db4 | 63 | \f |
f0130d39 | 64 | |
19c50364 AD |
65 | /*--------------------------. |
66 | | Is SUFFIX ending STRING? | | |
67 | `--------------------------*/ | |
68 | ||
9b3add5b | 69 | int |
19c50364 AD |
70 | strsuffix (const char *string, const char *suffix) |
71 | { | |
72 | size_t string_len = strlen (string); | |
73 | size_t suffix_len = strlen (suffix); | |
74 | if (suffix_len <= string_len) | |
75 | return !strcmp (string + string_len - suffix_len, suffix); | |
76 | else | |
77 | return 0; | |
78 | } | |
79 | ||
80 | ||
358c15b7 AD |
81 | /*-----------------------------------------------------------------. |
82 | | Return a newly allocated string composed of the concatenation of | | |
19c50364 | 83 | | STRING1, and STRING2. | |
358c15b7 | 84 | `-----------------------------------------------------------------*/ |
54bd0db4 | 85 | |
9b3add5b | 86 | char* |
19c50364 | 87 | stringappend (const char *string1, const char *string2) |
54bd0db4 | 88 | { |
19c50364 AD |
89 | size_t len = strlen (string1) + strlen (string2); |
90 | char *res = XMALLOC (char, len + 1); | |
358c15b7 | 91 | char *cp; |
19c50364 AD |
92 | cp = stpcpy (res, string1); |
93 | cp = stpcpy (cp, string2); | |
358c15b7 | 94 | return res; |
54bd0db4 RS |
95 | } |
96 | ||
0b8afb77 | 97 | |
270a173c AD |
98 | /*-----------------------------------------------------------------. |
99 | | Computes the macro name used to avoid double inclusion in the | | |
100 | | header of the parser and store it in header_macro_name. Be sure | | |
101 | | to produce valid CPP names (don't start with digit, remain | | |
102 | | alphanumerical + underscore). | | |
103 | `-----------------------------------------------------------------*/ | |
0b8afb77 | 104 | |
93ede233 | 105 | char * |
0b8afb77 AD |
106 | compute_header_macro (void) |
107 | { | |
270a173c | 108 | const char *prefix = "BISON_"; |
342b8b6e | 109 | char *macro_name, *cp; |
0b8afb77 | 110 | |
342b8b6e | 111 | if (spec_defines_file) |
270a173c AD |
112 | { |
113 | macro_name = XMALLOC (char, | |
114 | strlen (prefix) + | |
115 | strlen (spec_defines_file) + 1); | |
116 | cp = stpcpy (macro_name, prefix); | |
117 | cp = stpcpy (cp, spec_defines_file); | |
118 | } | |
342b8b6e AD |
119 | else |
120 | { | |
121 | macro_name = XMALLOC (char, | |
270a173c | 122 | strlen (prefix) + |
342b8b6e AD |
123 | strlen (base_name) + |
124 | strlen (header_extension) + 1); | |
270a173c AD |
125 | cp = stpcpy (macro_name, prefix); |
126 | cp = stpcpy (cp, base_name); | |
127 | cp = stpcpy (cp, header_extension); | |
342b8b6e AD |
128 | } |
129 | ||
130 | for (cp = macro_name; *cp; ++cp) | |
131 | if (islower (*cp)) | |
132 | *cp = toupper (*cp); | |
133 | else if (!isalnum (*cp)) | |
134 | *cp = '_'; | |
0b8afb77 | 135 | |
0b8afb77 AD |
136 | return macro_name; |
137 | } | |
138 | ||
139 | ||
cfe5fbc0 AD |
140 | /*-----------------------------------------------------------------. |
141 | | Try to open file NAME with mode MODE, and print an error message | | |
142 | | if fails. | | |
143 | `-----------------------------------------------------------------*/ | |
54bd0db4 | 144 | |
ff61dabd | 145 | FILE * |
8963a27b | 146 | xfopen (const char *name, const char *mode) |
cfe5fbc0 | 147 | { |
8963a27b | 148 | FILE *ptr; |
cfe5fbc0 AD |
149 | |
150 | ptr = fopen (name, mode); | |
151 | if (!ptr) | |
152 | error (2, errno, _("cannot open file `%s'"), name); | |
153 | ||
154 | return ptr; | |
155 | } | |
156 | ||
157 | /*-------------------------------------------------------------. | |
158 | | Try to close file PTR, and print an error message if fails. | | |
159 | `-------------------------------------------------------------*/ | |
160 | ||
ff61dabd | 161 | int |
8963a27b | 162 | xfclose (FILE *ptr) |
cfe5fbc0 AD |
163 | { |
164 | int result; | |
165 | ||
166 | if (ptr == NULL) | |
167 | return 0; | |
168 | ||
169 | result = fclose (ptr); | |
170 | if (result == EOF) | |
171 | error (2, errno, _("cannot close file")); | |
172 | ||
173 | return result; | |
174 | } | |
d8880f69 | 175 | |
d8880f69 | 176 | |
ff61dabd AD |
177 | /*------------------------------------------------------------------. |
178 | | Return the path to the skeleton which locaction might be given in | | |
b0ce6046 | 179 | | ENVVAR, otherwise return SKELETON_NAME. | |
ff61dabd AD |
180 | `------------------------------------------------------------------*/ |
181 | ||
182 | const char * | |
b0ce6046 | 183 | skeleton_find (const char *envvar, const char *skeleton_name) |
9311529b AD |
184 | { |
185 | const char *res = getenv (envvar); | |
186 | ||
342b8b6e AD |
187 | #if defined (MSDOS) || defined (_WIN32) |
188 | if (!res) | |
9311529b | 189 | { |
342b8b6e | 190 | /* Skeleton file name without path */ |
5e147124 | 191 | const char *skel_name = strrchr (skeleton_name, '/'); |
342b8b6e | 192 | if (!skel_name) |
5e147124 | 193 | skel_name = strrchr (skeleton_name, '\\'); |
342b8b6e AD |
194 | if (!skel_name) |
195 | skel_name = skeleton_name; | |
196 | else | |
197 | ++skel_name; | |
198 | ||
199 | /* File doesn't exist in current directory; try in INIT directory. */ | |
200 | const char *cp = getenv ("INIT"); | |
201 | if (cp) | |
202 | { | |
203 | res = XMALLOC (char, strlen (cp) + strlen (skel_name) + 2); | |
204 | sprintf (res, "%s%c%s", cp, '\\', skel_name); | |
205 | } | |
206 | else if (access (skel_name, 4) == 0) /* Look in current dir. */ | |
207 | res = skel_name; | |
208 | else | |
209 | { | |
210 | /* Look in program locations dir. */ | |
211 | extern char *program_name; | |
212 | cp = strrchr(program_name, '\\'); | |
213 | if (!cp) | |
214 | return skeleton_name; | |
215 | else | |
216 | ++cp; | |
217 | res = XMALLOC (char, cp - program_name + strlen (skel_name) + 1); | |
218 | strncpy (res, program_name, cp - program_name); | |
219 | strcpy (res + (cp - program_name), skel_name); | |
220 | } | |
9311529b | 221 | } |
342b8b6e | 222 | #endif /* defined (MSDOS) || defined (_WIN32) */ |
9311529b | 223 | if (!res) |
b0ce6046 | 224 | res = skeleton_name; |
9311529b AD |
225 | |
226 | return res; | |
227 | } | |
cfe5fbc0 | 228 | \f |
f0130d39 | 229 | |
234a3be3 AD |
230 | /*----------------------------------------------------------------. |
231 | | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. | | |
232 | `----------------------------------------------------------------*/ | |
233 | ||
b0ce6046 | 234 | /* Replace all characters FROM by TO in the string IN. |
f0130d39 | 235 | and returns a new allocated string. */ |
234a3be3 | 236 | static char * |
f0130d39 | 237 | tr (const char *in, char from, char to) |
234a3be3 AD |
238 | { |
239 | char *temp; | |
240 | char *out; | |
b0ce6046 | 241 | |
234a3be3 AD |
242 | out = XMALLOC (char, strlen (in) + 1); |
243 | ||
244 | for (temp = out; *in; in++, out++) | |
245 | if (*in == from) | |
246 | *out = to; | |
247 | else | |
248 | *out = *in; | |
249 | *out = 0; | |
250 | return (temp); | |
251 | } | |
252 | ||
f0130d39 AD |
253 | /* Gets the extension index in FILENAME. Returns 0 if fails to |
254 | find an extension. */ | |
b0ce6046 | 255 | static int |
f0130d39 | 256 | get_extension_index (const char *filename) |
234a3be3 | 257 | { |
f0130d39 | 258 | int len; |
234a3be3 AD |
259 | |
260 | len = strlen (filename); | |
b0ce6046 | 261 | |
234a3be3 AD |
262 | if (filename[len-- - 1] == '.') |
263 | return (0); | |
264 | ||
265 | while ((len > 0) && (filename[len - 1] != '.')) | |
266 | if (filename[len - 1] == '/') | |
267 | return (0); | |
268 | else | |
269 | len--; | |
270 | ||
271 | return (len - 1); | |
272 | } | |
273 | ||
274 | /* Computes extensions from the grammar file extension. */ | |
275 | static void | |
f0130d39 | 276 | compute_exts_from_gf (const char *ext) |
234a3be3 | 277 | { |
342b8b6e AD |
278 | src_extension = tr (ext, 'y', 'c'); |
279 | src_extension = tr (src_extension, 'Y', 'C'); | |
280 | header_extension = tr (ext, 'y', 'h'); | |
281 | header_extension = tr (header_extension, 'Y', 'H'); | |
234a3be3 AD |
282 | } |
283 | ||
284 | /* Computes extensions from the given c source file extension. */ | |
285 | static void | |
f0130d39 | 286 | compute_exts_from_src (const char *ext) |
234a3be3 | 287 | { |
4ecbf796 MA |
288 | /* We use this function when the user specifies `-o' or `--output', |
289 | so the extenions must be computed unconditionally from the file name | |
290 | given by this option. */ | |
f0130d39 AD |
291 | src_extension = xstrdup (ext); |
292 | header_extension = tr (ext, 'c', 'h'); | |
293 | header_extension = tr (header_extension, 'C', 'H'); | |
234a3be3 | 294 | } |
d8880f69 | 295 | |
19c50364 AD |
296 | /* FIXME: Should use xstrndup. */ |
297 | ||
298 | static void | |
27110317 | 299 | compute_base_names (void) |
54bd0db4 | 300 | { |
19c50364 AD |
301 | size_t base_length; |
302 | size_t short_base_length; | |
234a3be3 | 303 | size_t ext_index; |
b0ce6046 | 304 | |
19c50364 AD |
305 | /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c), |
306 | BASE_NAME and SHORT_BASE_NAME are `foo'. | |
54bd0db4 | 307 | |
19c50364 AD |
308 | If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and |
309 | SHORT_BASE_NAME is `foo'. | |
310 | ||
311 | The precise -o name will be used for FTABLE. For other output | |
312 | files, remove the ".c" or ".tab.c" suffix. */ | |
54bd0db4 RS |
313 | if (spec_outfile) |
314 | { | |
54bd0db4 | 315 | #ifdef MSDOS |
19c50364 | 316 | strlwr (spec_outfile); |
54bd0db4 RS |
317 | #endif /* MSDOS */ |
318 | /* BASE_LENGTH includes ".tab" but not ".c". */ | |
19c50364 | 319 | base_length = strlen (spec_outfile); |
b0ce6046 | 320 | |
234a3be3 | 321 | ext_index = get_extension_index (spec_outfile); |
f0130d39 AD |
322 | /* If the initial segment of extension contains 'c' or a 'C', I assume |
323 | that it is a C or C++ source file. */ | |
234a3be3 | 324 | if (ext_index) |
f0130d39 AD |
325 | ext_index = |
326 | (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0; | |
234a3be3 AD |
327 | if (ext_index) |
328 | { | |
329 | base_length -= strlen (spec_outfile + ext_index); | |
95fb5662 | 330 | compute_exts_from_src (spec_outfile + ext_index); |
234a3be3 | 331 | } |
b0ce6046 | 332 | |
956dba3a | 333 | base_name = strndup (spec_outfile, base_length); |
54bd0db4 RS |
334 | /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ |
335 | short_base_length = base_length; | |
956dba3a | 336 | if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab")) |
54bd0db4 | 337 | short_base_length -= 4; |
27110317 | 338 | short_base_name = strndup (spec_outfile, short_base_length); |
19c50364 | 339 | |
9b3add5b RA |
340 | /* FIXME: This is a quick and dirty way for me to find out if we |
341 | should .tab or not, using the computations above. */ | |
342 | if (strcmp (base_name, short_base_name)) | |
343 | tab_extension = 1; | |
344 | ||
19c50364 | 345 | return; |
54bd0db4 | 346 | } |
19c50364 AD |
347 | |
348 | /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME | |
349 | are `foo'. | |
350 | ||
351 | Construct names from it. */ | |
352 | if (spec_file_prefix) | |
54bd0db4 | 353 | { |
54bd0db4 | 354 | #ifdef MSDOS |
19c50364 | 355 | strlwr (spec_file_prefix); |
54bd0db4 | 356 | #endif /* MSDOS */ |
27110317 | 357 | short_base_name = xstrdup (spec_file_prefix); |
27110317 AD |
358 | base_name = XMALLOC (char, |
359 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
360 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 | 361 | |
95fb5662 MA |
362 | /* Computes the extensions from the garmmar file name. */ |
363 | ext_index = get_extension_index (infile); | |
f0130d39 AD |
364 | /* If the initial segment of extension contains a 'y' or a 'Y', I assume |
365 | that it is a yacc or bison grammar file. */ | |
95fb5662 MA |
366 | if (ext_index) |
367 | ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0; | |
368 | if (ext_index) | |
369 | compute_exts_from_gf (infile + ext_index); | |
f0130d39 | 370 | |
9b3add5b RA |
371 | /* It seems that when only a prefix is given, '.tab' should always be |
372 | used. */ | |
373 | tab_extension = 1; | |
374 | ||
19c50364 | 375 | return; |
54bd0db4 | 376 | } |
54bd0db4 | 377 | |
19c50364 AD |
378 | /* If neither -o nor --file-prefix were specified, and the input |
379 | file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is | |
380 | `foo'. | |
4a120d45 | 381 | |
19c50364 AD |
382 | If --yacc is used, do as if the input file was `y.y'. */ |
383 | { | |
384 | const char *name_base = yacc_flag ? "y.y" : infile; | |
54bd0db4 | 385 | |
19c50364 | 386 | /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */ |
54bd0db4 | 387 | |
19c50364 | 388 | base_length = strlen (name_base); |
b0ce6046 | 389 | |
234a3be3 | 390 | ext_index = get_extension_index (name_base); |
f0130d39 AD |
391 | /* If the initial segment of extension contains a 'y' or a 'Y', I assume |
392 | that it is a yacc or bison grammar file. */ | |
234a3be3 | 393 | if (ext_index) |
95fb5662 | 394 | ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0; |
234a3be3 AD |
395 | if (ext_index) |
396 | { | |
397 | base_length -= strlen (name_base + ext_index); | |
95fb5662 | 398 | compute_exts_from_gf (name_base + ext_index); |
234a3be3 AD |
399 | } |
400 | ||
19c50364 | 401 | short_base_length = base_length; |
27110317 | 402 | short_base_name = strndup (name_base, short_base_length); |
54bd0db4 | 403 | |
27110317 AD |
404 | base_name = XMALLOC (char, |
405 | strlen (short_base_name) + strlen (EXT_TAB) + 1); | |
406 | stpcpy (stpcpy (base_name, short_base_name), EXT_TAB); | |
19c50364 | 407 | |
9b3add5b RA |
408 | /* By default, Bison should insert '.tab' were needed. */ |
409 | tab_extension = 1; | |
410 | ||
19c50364 AD |
411 | return; |
412 | } | |
413 | } | |
414 | ||
342b8b6e AD |
415 | /*-------------------------------------------------------. |
416 | | Close the open files, compute the output files names. | | |
417 | `-------------------------------------------------------*/ | |
418 | ||
419 | void | |
420 | compute_output_file_names (void) | |
421 | { | |
422 | compute_base_names (); | |
423 | ||
ea52d706 AD |
424 | parser_file_name = |
425 | spec_outfile ? spec_outfile : stringappend (base_name, src_extension); | |
426 | ||
342b8b6e AD |
427 | /* If not yet done. */ |
428 | if (!src_extension) | |
429 | src_extension = ".c"; | |
430 | if (!header_extension) | |
431 | header_extension = ".h"; | |
432 | ||
433 | /* It the defines filename if not given, we create it. */ | |
434 | if (!spec_defines_file) | |
435 | spec_defines_file = stringappend (base_name, header_extension); | |
436 | ||
437 | /* It the graph filename if not given, we create it. */ | |
438 | if (!spec_graph_file) | |
439 | spec_graph_file = stringappend (short_base_name, ".vcg"); | |
440 | ||
441 | spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT); | |
442 | ||
443 | attrsfile = stringappend (short_base_name, EXT_STYPE_H); | |
444 | #ifndef MSDOS | |
445 | attrsfile = stringappend (attrsfile, header_extension); | |
446 | #endif /* MSDOS */ | |
342b8b6e | 447 | } |