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