]> git.saurik.com Git - bison.git/blob - src/files.c
Turn off %{source,header}_extension feature.
[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 "xalloc.h"
26 #include "gram.h"
27 #include "complain.h"
28
29 FILE *finput = NULL;
30
31 struct obstack action_obstack;
32 struct obstack attrs_obstack;
33 struct obstack table_obstack;
34 struct obstack defines_obstack;
35 struct obstack guard_obstack;
36 struct obstack output_obstack;
37 struct obstack graph_obstack;
38
39 char *spec_outfile = NULL; /* for -o. */
40 char *spec_file_prefix = NULL; /* for -b. */
41 char *spec_name_prefix = NULL; /* for -p. */
42
43 char *infile = NULL;
44 char *attrsfile = NULL;
45
46 static char *base_name = NULL;
47 static char *short_base_name = NULL;
48
49 /* C source file extension (the parser source). */
50 const char *src_extension = NULL;
51 /* Header file extension (if option ``-d'' is specified). */
52 const char *header_extension = NULL;
53 \f
54
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
71 /*-----------------------------------------------------------------.
72 | Return a newly allocated string composed of the concatenation of |
73 | STRING1, and STRING2. |
74 `-----------------------------------------------------------------*/
75
76 static char *
77 stringappend (const char *string1, const char *string2)
78 {
79 size_t len = strlen (string1) + strlen (string2);
80 char *res = XMALLOC (char, len + 1);
81 char *cp;
82 cp = stpcpy (res, string1);
83 cp = stpcpy (cp, string2);
84 return res;
85 }
86
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
93 static char *
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
118 /*-----------------------------------------------------------------.
119 | Try to open file NAME with mode MODE, and print an error message |
120 | if fails. |
121 `-----------------------------------------------------------------*/
122
123 FILE *
124 xfopen (const char *name, const char *mode)
125 {
126 FILE *ptr;
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
139 int
140 xfclose (FILE *ptr)
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 }
153
154 /*--------------------------------------------------.
155 | Save the content of the obstack OBS in FILENAME. |
156 `--------------------------------------------------*/
157
158 static void
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
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 }
186
187 /*------------------------------------------------------------------.
188 | Return the path to the skeleton which locaction might be given in |
189 | ENVVAR, otherwise return SKELETON_NAME. |
190 `------------------------------------------------------------------*/
191
192 const char *
193 skeleton_find (const char *envvar, const char *skeleton_name)
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 {
203 res = XMALLOC (char, strlen (cp) + strlen (skeleton_name) + 2);
204 sprintf (res, "%s%c%s", cp, '/', skeleton_name);
205 }
206 #endif /* !MSDOS */
207
208 if (!res)
209 res = skeleton_name;
210
211 return res;
212 }
213 \f
214
215 /*----------------------------------------------------------------.
216 | Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
217 `----------------------------------------------------------------*/
218
219 /* Replace all characters FROM by TO in the string IN.
220 and returns a new allocated string. */
221 static char *
222 tr (const char *in, char from, char to)
223 {
224 char *temp;
225 char *out;
226
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
238 /* Gets the extension index in FILENAME. Returns 0 if fails to
239 find an extension. */
240 static int
241 get_extension_index (const char *filename)
242 {
243 int len;
244
245 len = strlen (filename);
246
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
261 compute_exts_from_gf (const char *ext)
262 {
263 src_extension = tr (ext, 'y', 'c');
264 src_extension = tr (src_extension, 'Y', 'C');
265 header_extension = tr (ext, 'y', 'h');
266 header_extension = tr (header_extension, 'Y', 'H');
267 }
268
269 /* Computes extensions from the given c source file extension. */
270 static void
271 compute_exts_from_src (const char *ext)
272 {
273 /* We use this function when the user specifies `-o' or `--output',
274 so the extenions must be computed unconditionally from the file name
275 given by this option. */
276 src_extension = xstrdup (ext);
277 header_extension = tr (ext, 'c', 'h');
278 header_extension = tr (header_extension, 'C', 'H');
279 }
280
281 /* FIXME: Should use xstrndup. */
282
283 static void
284 compute_base_names (void)
285 {
286 size_t base_length;
287 size_t short_base_length;
288 size_t ext_index;
289
290 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
291 BASE_NAME and SHORT_BASE_NAME are `foo'.
292
293 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
294 SHORT_BASE_NAME is `foo'.
295
296 The precise -o name will be used for FTABLE. For other output
297 files, remove the ".c" or ".tab.c" suffix. */
298 if (spec_outfile)
299 {
300 #ifdef MSDOS
301 strlwr (spec_outfile);
302 #endif /* MSDOS */
303 /* BASE_LENGTH includes ".tab" but not ".c". */
304 base_length = strlen (spec_outfile);
305
306 ext_index = get_extension_index (spec_outfile);
307 /* If the initial segment of extension contains 'c' or a 'C', I assume
308 that it is a C or C++ source file. */
309 if (ext_index)
310 ext_index =
311 (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
312 if (ext_index)
313 {
314 base_length -= strlen (spec_outfile + ext_index);
315 compute_exts_from_src (spec_outfile + ext_index);
316 }
317
318 base_name = strndup (spec_outfile, base_length);
319 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
320 short_base_length = base_length;
321 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
322 short_base_length -= 4;
323 short_base_name = strndup (spec_outfile, short_base_length);
324
325 return;
326 }
327
328 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
329 are `foo'.
330
331 Construct names from it. */
332 if (spec_file_prefix)
333 {
334 #ifdef MSDOS
335 strlwr (spec_file_prefix);
336 #endif /* MSDOS */
337 short_base_name = xstrdup (spec_file_prefix);
338 base_name = XMALLOC (char,
339 strlen (short_base_name) + strlen (EXT_TAB) + 1);
340 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
341
342 /* Computes the extensions from the garmmar file name. */
343 ext_index = get_extension_index (infile);
344 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
345 that it is a yacc or bison grammar file. */
346 if (ext_index)
347 ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0;
348 if (ext_index)
349 compute_exts_from_gf (infile + ext_index);
350
351 return;
352 }
353
354 /* If neither -o nor --file-prefix were specified, and the input
355 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
356 `foo'.
357
358 If --yacc is used, do as if the input file was `y.y'. */
359 {
360 const char *name_base = yacc_flag ? "y.y" : infile;
361
362 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
363
364 base_length = strlen (name_base);
365
366 ext_index = get_extension_index (name_base);
367 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
368 that it is a yacc or bison grammar file. */
369 if (ext_index)
370 ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0;
371 if (ext_index)
372 {
373 base_length -= strlen (name_base + ext_index);
374 compute_exts_from_gf (name_base + ext_index);
375 }
376
377 short_base_length = base_length;
378 short_base_name = strndup (name_base, short_base_length);
379
380 base_name = XMALLOC (char,
381 strlen (short_base_name) + strlen (EXT_TAB) + 1);
382 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
383
384 return;
385 }
386 }
387
388 /*-----------------------------------------------------------------.
389 | Open the input file. Look for the skeletons. Find the names of |
390 | the output files. Prepare the obstacks. |
391 `-----------------------------------------------------------------*/
392
393 void
394 open_files (void)
395 {
396 finput = xfopen (infile, "r");
397
398 /* Initialize the obstacks. */
399 obstack_init (&action_obstack);
400 obstack_init (&attrs_obstack);
401 obstack_init (&table_obstack);
402 obstack_init (&defines_obstack);
403 obstack_init (&guard_obstack);
404 obstack_init (&output_obstack);
405 obstack_init (&graph_obstack);
406 }
407
408
409
410 /*-----------------------------------------------------.
411 | Close the open files, produce all the output files. |
412 `-----------------------------------------------------*/
413
414 void
415 output_files (void)
416 {
417 xfclose (finput);
418
419 compute_base_names ();
420
421 #if 0
422 /* Set default extensions */
423 if (!src_extension)
424 src_extension = ".c";
425 if (!header_extension)
426 header_extension = ".h";
427 #endif
428
429 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
430 #ifndef MSDOS
431 stringappend (attrsfile, header_extension);
432 #endif /* MSDOS */
433
434 /* Output the main file. */
435 if (spec_outfile)
436 obstack_save (&table_obstack, spec_outfile);
437 else
438 obstack_save (&table_obstack, stringappend (base_name, src_extension));
439
440 /* Output the header file if wanted. */
441 if (defines_flag)
442 defines_obstack_save (stringappend (base_name, header_extension));
443
444 /* If we output only the table, dump the actions in ACTFILE. */
445 if (no_parser_flag)
446 obstack_save (&action_obstack, stringappend (short_base_name, ".act"));
447
448 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
449 into its own file, ATTTRSFILE. */
450 if (semantic_parser)
451 {
452 char *temp_name;
453
454 obstack_save (&attrs_obstack, attrsfile);
455 temp_name = stringappend (short_base_name, EXT_GUARD_C);
456 #ifndef MSDOS
457 temp_name = stringappend (temp_name, src_extension);
458 #endif /* MSDOS */
459 obstack_save (&guard_obstack, temp_name);
460 }
461
462 if (verbose_flag)
463 /* We used to use just .out if spec_name_prefix (-p) was used, but
464 that conflicts with Posix. */
465 obstack_save (&output_obstack,
466 stringappend (short_base_name, EXT_OUTPUT));
467
468 if (graph_flag)
469 obstack_save (&graph_obstack, stringappend (short_base_name, ".vcg"));
470 }