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