]> git.saurik.com Git - bison.git/blame - src/files.c
* configure.in: Invoke AC_FUNC_OBSTACK and AC_FUNC_ERROR_AT_LINE.
[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"
a0f6b076 26#include "complain.h"
54bd0db4
RS
27
28FILE *finput = NULL;
54bd0db4 29
8c7ebe49 30struct obstack action_obstack;
dd60faec 31struct obstack attrs_obstack;
896fe5c1
AD
32struct obstack table_obstack;
33struct obstack defines_obstack;
ea5607fd 34struct obstack guard_obstack;
ff4423cc 35struct obstack output_obstack;
8c7ebe49 36
b0ce6046 37char *spec_outfile = NULL; /* for -o. */
f0130d39
AD
38char *spec_file_prefix = NULL; /* for -b. */
39char *spec_name_prefix = NULL; /* for -p. */
342b8b6e
AD
40char *spec_verbose_file = NULL; /* for --verbose. */
41char *spec_graph_file = NULL; /* for -g. */
42char *spec_defines_file = NULL; /* for --defines. */
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
e795bd73 96static char *
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
0b8afb77
AD
180/*---------------------------------------------------------------------.
181| Output double inclusion protection macros and saves defines_obstack |
182`---------------------------------------------------------------------*/
183
184static void
185defines_obstack_save (const char *filename)
186{
187 FILE *out = xfopen (filename, "w");
188 size_t size = obstack_object_size (&defines_obstack);
189 char *macro_name = compute_header_macro ();
190
191 fprintf (out, "#ifndef %s\n", macro_name);
192 fprintf (out, "# define %s\n\n", macro_name);
193 fwrite (obstack_finish (&defines_obstack), 1, size, out);
194 fprintf (out, "\n#endif /* not %s */\n", macro_name);
195
196 free (macro_name);
197 xfclose (out);
198}
9311529b 199
ff61dabd
AD
200/*------------------------------------------------------------------.
201| Return the path to the skeleton which locaction might be given in |
b0ce6046 202| ENVVAR, otherwise return SKELETON_NAME. |
ff61dabd
AD
203`------------------------------------------------------------------*/
204
205const char *
b0ce6046 206skeleton_find (const char *envvar, const char *skeleton_name)
9311529b
AD
207{
208 const char *res = getenv (envvar);
209
342b8b6e
AD
210#if defined (MSDOS) || defined (_WIN32)
211 if (!res)
9311529b 212 {
342b8b6e 213 /* Skeleton file name without path */
5e147124 214 const char *skel_name = strrchr (skeleton_name, '/');
342b8b6e 215 if (!skel_name)
5e147124 216 skel_name = strrchr (skeleton_name, '\\');
342b8b6e
AD
217 if (!skel_name)
218 skel_name = skeleton_name;
219 else
220 ++skel_name;
221
222 /* File doesn't exist in current directory; try in INIT directory. */
223 const char *cp = getenv ("INIT");
224 if (cp)
225 {
226 res = XMALLOC (char, strlen (cp) + strlen (skel_name) + 2);
227 sprintf (res, "%s%c%s", cp, '\\', skel_name);
228 }
229 else if (access (skel_name, 4) == 0) /* Look in current dir. */
230 res = skel_name;
231 else
232 {
233 /* Look in program locations dir. */
234 extern char *program_name;
235 cp = strrchr(program_name, '\\');
236 if (!cp)
237 return skeleton_name;
238 else
239 ++cp;
240 res = XMALLOC (char, cp - program_name + strlen (skel_name) + 1);
241 strncpy (res, program_name, cp - program_name);
242 strcpy (res + (cp - program_name), skel_name);
243 }
9311529b 244 }
342b8b6e 245#endif /* defined (MSDOS) || defined (_WIN32) */
9311529b 246 if (!res)
b0ce6046 247 res = skeleton_name;
9311529b
AD
248
249 return res;
250}
cfe5fbc0 251\f
f0130d39 252
234a3be3
AD
253/*----------------------------------------------------------------.
254| Compute BASE_NAME, SHORT_BASE_NAME and output files extensions. |
255`----------------------------------------------------------------*/
256
b0ce6046 257/* Replace all characters FROM by TO in the string IN.
f0130d39 258 and returns a new allocated string. */
234a3be3 259static char *
f0130d39 260tr (const char *in, char from, char to)
234a3be3
AD
261{
262 char *temp;
263 char *out;
b0ce6046 264
234a3be3
AD
265 out = XMALLOC (char, strlen (in) + 1);
266
267 for (temp = out; *in; in++, out++)
268 if (*in == from)
269 *out = to;
270 else
271 *out = *in;
272 *out = 0;
273 return (temp);
274}
275
f0130d39
AD
276/* Gets the extension index in FILENAME. Returns 0 if fails to
277 find an extension. */
b0ce6046 278static int
f0130d39 279get_extension_index (const char *filename)
234a3be3 280{
f0130d39 281 int len;
234a3be3
AD
282
283 len = strlen (filename);
b0ce6046 284
234a3be3
AD
285 if (filename[len-- - 1] == '.')
286 return (0);
287
288 while ((len > 0) && (filename[len - 1] != '.'))
289 if (filename[len - 1] == '/')
290 return (0);
291 else
292 len--;
293
294 return (len - 1);
295}
296
297/* Computes extensions from the grammar file extension. */
298static void
f0130d39 299compute_exts_from_gf (const char *ext)
234a3be3 300{
342b8b6e
AD
301 src_extension = tr (ext, 'y', 'c');
302 src_extension = tr (src_extension, 'Y', 'C');
303 header_extension = tr (ext, 'y', 'h');
304 header_extension = tr (header_extension, 'Y', 'H');
234a3be3
AD
305}
306
307/* Computes extensions from the given c source file extension. */
308static void
f0130d39 309compute_exts_from_src (const char *ext)
234a3be3 310{
4ecbf796
MA
311 /* We use this function when the user specifies `-o' or `--output',
312 so the extenions must be computed unconditionally from the file name
313 given by this option. */
f0130d39
AD
314 src_extension = xstrdup (ext);
315 header_extension = tr (ext, 'c', 'h');
316 header_extension = tr (header_extension, 'C', 'H');
234a3be3 317}
d8880f69 318
19c50364
AD
319/* FIXME: Should use xstrndup. */
320
321static void
27110317 322compute_base_names (void)
54bd0db4 323{
19c50364
AD
324 size_t base_length;
325 size_t short_base_length;
234a3be3 326 size_t ext_index;
b0ce6046 327
19c50364
AD
328 /* If --output=foo.c was specified (SPEC_OUTFILE == foo.c),
329 BASE_NAME and SHORT_BASE_NAME are `foo'.
54bd0db4 330
19c50364
AD
331 If --output=foo.tab.c was specified, BASE_NAME is `foo.tab' and
332 SHORT_BASE_NAME is `foo'.
333
334 The precise -o name will be used for FTABLE. For other output
335 files, remove the ".c" or ".tab.c" suffix. */
54bd0db4
RS
336 if (spec_outfile)
337 {
54bd0db4 338#ifdef MSDOS
19c50364 339 strlwr (spec_outfile);
54bd0db4
RS
340#endif /* MSDOS */
341 /* BASE_LENGTH includes ".tab" but not ".c". */
19c50364 342 base_length = strlen (spec_outfile);
b0ce6046 343
234a3be3 344 ext_index = get_extension_index (spec_outfile);
f0130d39
AD
345 /* If the initial segment of extension contains 'c' or a 'C', I assume
346 that it is a C or C++ source file. */
234a3be3 347 if (ext_index)
f0130d39
AD
348 ext_index =
349 (strspn (spec_outfile + ext_index + 1, "cC")) ? ext_index : 0;
234a3be3
AD
350 if (ext_index)
351 {
352 base_length -= strlen (spec_outfile + ext_index);
95fb5662 353 compute_exts_from_src (spec_outfile + ext_index);
234a3be3 354 }
b0ce6046 355
956dba3a 356 base_name = strndup (spec_outfile, base_length);
54bd0db4
RS
357 /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */
358 short_base_length = base_length;
956dba3a 359 if (strsuffix (base_name, ".tab") || strsuffix (base_name, "_tab"))
54bd0db4 360 short_base_length -= 4;
27110317 361 short_base_name = strndup (spec_outfile, short_base_length);
19c50364
AD
362
363 return;
54bd0db4 364 }
19c50364
AD
365
366 /* If --file-prefix=foo was specified, BASE_NAME and SHORT_BASE_NAME
367 are `foo'.
368
369 Construct names from it. */
370 if (spec_file_prefix)
54bd0db4 371 {
54bd0db4 372#ifdef MSDOS
19c50364 373 strlwr (spec_file_prefix);
54bd0db4 374#endif /* MSDOS */
27110317 375 short_base_name = xstrdup (spec_file_prefix);
27110317
AD
376 base_name = XMALLOC (char,
377 strlen (short_base_name) + strlen (EXT_TAB) + 1);
378 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
19c50364 379
95fb5662
MA
380 /* Computes the extensions from the garmmar file name. */
381 ext_index = get_extension_index (infile);
f0130d39
AD
382 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
383 that it is a yacc or bison grammar file. */
95fb5662
MA
384 if (ext_index)
385 ext_index = (strspn (infile + ext_index + 1, "yY")) ? ext_index : 0;
386 if (ext_index)
387 compute_exts_from_gf (infile + ext_index);
f0130d39 388
19c50364 389 return;
54bd0db4 390 }
54bd0db4 391
19c50364
AD
392 /* If neither -o nor --file-prefix were specified, and the input
393 file is foo.y, BASE_NAME is `foo.tab', and SHORT_BASE_NAME is
394 `foo'.
4a120d45 395
19c50364
AD
396 If --yacc is used, do as if the input file was `y.y'. */
397 {
398 const char *name_base = yacc_flag ? "y.y" : infile;
54bd0db4 399
19c50364 400 /* BASE_LENGTH gets length of BASE_NAME, sans ".y" suffix if any. */
54bd0db4 401
19c50364 402 base_length = strlen (name_base);
b0ce6046 403
234a3be3 404 ext_index = get_extension_index (name_base);
f0130d39
AD
405 /* If the initial segment of extension contains a 'y' or a 'Y', I assume
406 that it is a yacc or bison grammar file. */
234a3be3 407 if (ext_index)
95fb5662 408 ext_index = (strspn (name_base + ext_index + 1, "yY")) ? ext_index : 0;
234a3be3
AD
409 if (ext_index)
410 {
411 base_length -= strlen (name_base + ext_index);
95fb5662 412 compute_exts_from_gf (name_base + ext_index);
234a3be3
AD
413 }
414
19c50364 415 short_base_length = base_length;
27110317 416 short_base_name = strndup (name_base, short_base_length);
54bd0db4 417
27110317
AD
418 base_name = XMALLOC (char,
419 strlen (short_base_name) + strlen (EXT_TAB) + 1);
420 stpcpy (stpcpy (base_name, short_base_name), EXT_TAB);
19c50364
AD
421
422 return;
423 }
424}
425
342b8b6e
AD
426/*-------------------------------------------------------.
427| Close the open files, compute the output files names. |
428`-------------------------------------------------------*/
429
430void
431compute_output_file_names (void)
432{
433 compute_base_names ();
434
435 /* If not yet done. */
436 if (!src_extension)
437 src_extension = ".c";
438 if (!header_extension)
439 header_extension = ".h";
440
441 /* It the defines filename if not given, we create it. */
442 if (!spec_defines_file)
443 spec_defines_file = stringappend (base_name, header_extension);
444
445 /* It the graph filename if not given, we create it. */
446 if (!spec_graph_file)
447 spec_graph_file = stringappend (short_base_name, ".vcg");
448
449 spec_verbose_file = stringappend (short_base_name, EXT_OUTPUT);
450
451 attrsfile = stringappend (short_base_name, EXT_STYPE_H);
452#ifndef MSDOS
453 attrsfile = stringappend (attrsfile, header_extension);
454#endif /* MSDOS */
455
456}
457
19c50364
AD
458/*-----------------------------------------------------------------.
459| Open the input file. Look for the skeletons. Find the names of |
460| the output files. Prepare the obstacks. |
461`-----------------------------------------------------------------*/
462
463void
464open_files (void)
465{
8963a27b 466 finput = xfopen (infile, "r");
54bd0db4 467
dd60faec 468 /* Initialize the obstacks. */
8c7ebe49 469 obstack_init (&action_obstack);
dd60faec 470 obstack_init (&attrs_obstack);
896fe5c1
AD
471 obstack_init (&table_obstack);
472 obstack_init (&defines_obstack);
ea5607fd 473 obstack_init (&guard_obstack);
ff4423cc 474 obstack_init (&output_obstack);
54bd0db4
RS
475}
476
477
478
342b8b6e
AD
479/*-----------------------.
480| Close the open file.. |
481`-----------------------*/
d8880f69 482
a693bf18 483void
342b8b6e 484close_files (void)
a693bf18 485{
8963a27b 486 xfclose (finput);
342b8b6e 487}
54bd0db4 488
342b8b6e
AD
489/*---------------------------.
490| Produce the output files. |
491`---------------------------*/
6deb4447 492
342b8b6e
AD
493void
494output_files (void)
495{
d8880f69 496 /* Output the main file. */
27110317
AD
497 if (spec_outfile)
498 obstack_save (&table_obstack, spec_outfile);
499 else
f0130d39 500 obstack_save (&table_obstack, stringappend (base_name, src_extension));
342b8b6e 501 obstack_free (&table_obstack, NULL);
d8880f69
AD
502
503 /* Output the header file if wanted. */
504 if (defines_flag)
342b8b6e
AD
505 defines_obstack_save (spec_defines_file);
506 obstack_free (&defines_obstack, NULL);
507
508#if 0
509 /* Seems to be invalid now --akim. */
54bd0db4 510
27110317 511 /* If we output only the table, dump the actions in ACTFILE. */
8c7ebe49 512 if (no_parser_flag)
27110317 513 obstack_save (&action_obstack, stringappend (short_base_name, ".act"));
342b8b6e
AD
514 obstack_free (&action_obstack, NULL);
515#endif
dd60faec
AD
516
517 /* If we produced a semantic parser ATTRS_OBSTACK must be dumped
518 into its own file, ATTTRSFILE. */
519 if (semantic_parser)
ea5607fd 520 {
234a3be3
AD
521 char *temp_name;
522
ea5607fd 523 obstack_save (&attrs_obstack, attrsfile);
342b8b6e 524 obstack_free (&attrs_obstack, NULL);
234a3be3
AD
525 temp_name = stringappend (short_base_name, EXT_GUARD_C);
526#ifndef MSDOS
527 temp_name = stringappend (temp_name, src_extension);
528#endif /* MSDOS */
529 obstack_save (&guard_obstack, temp_name);
342b8b6e 530 obstack_free (&guard_obstack, NULL);
ea5607fd 531 }
54bd0db4 532}