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