1 /* Open and close files for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2022 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
24 #include <configmake.h> /* PKGDATADIR */
27 #include <get-errno.h>
28 #include <gl_array_list.h>
29 #include <gl_hash_map.h>
34 #include <relocatable.h> /* relocate2 */
35 #include <stdio-safer.h>
37 #include <sys/types.h>
46 /* Initializing some values below (such SPEC_NAME_PREFIX to 'yy') is
47 tempting, but don't do that: for the time being our handling of the
48 %directive vs --option leaves precedence to the options by deciding
49 that if a %directive sets a variable which is really set (i.e., not
50 NULL), then the %directive is ignored. As a result, %name-prefix,
51 for instance, will not be honored. */
53 char const *spec_outfile
= NULL
; /* for -o. */
54 char const *spec_file_prefix
= NULL
; /* for -b. */
55 location spec_file_prefix_loc
= EMPTY_LOCATION_INIT
;
56 char const *spec_name_prefix
= NULL
; /* for -p. */
57 location spec_name_prefix_loc
= EMPTY_LOCATION_INIT
;
58 char *spec_verbose_file
= NULL
; /* for --verbose. */
59 char *spec_graph_file
= NULL
; /* for -g. */
60 char *spec_html_file
= NULL
; /* for --html. */
61 char *spec_xml_file
= NULL
; /* for -x. */
62 char *spec_header_file
= NULL
; /* for --header. */
63 char *parser_file_name
;
65 /* All computed output file names. */
66 typedef struct generated_file
70 /** Whether is a generated source file (e.g., *.c, *.java...), as
71 opposed to the report file (e.g., *.output). When late errors
72 are detected, generated source files are removed. */
75 static generated_file
*generated_files
= NULL
;
76 static int generated_files_size
= 0;
78 uniqstr grammar_file
= NULL
;
80 /* If --output=dir/foo.c was specified,
81 DIR_PREFIX gis 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'.
83 If --output=dir/foo.tab.c was specified, DIR_PREFIX is 'dir/',
84 ALL_BUT_EXT is 'dir/foo.tab', and ALL_BUT_TAB_EXT is 'dir/foo'.
86 If --output was not specified but --file-prefix=dir/foo was specified,
87 ALL_BUT_EXT = 'foo.tab' and ALL_BUT_TAB_EXT = 'foo'.
89 If neither --output nor --file was specified but the input grammar
90 is name dir/foo.y, ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'foo'.
92 If neither --output nor --file was specified, DIR_PREFIX is the
93 empty string (meaning the current directory); otherwise it is
97 static char *all_but_tab_ext
;
100 /* C source file extension (the parser source). */
101 static char *src_extension
= NULL
;
102 /* Header file extension (if option '`-d'' is specified). */
103 static char *header_extension
= NULL
;
111 static gl_list_t prefix_maps
= NULL
;
113 /* Map file names to prefix-mapped file names. */
114 static gl_map_t mapped_files
= NULL
;
117 /*-----------------------------------------------------------------.
118 | Return a newly allocated string composed of the concatenation of |
120 `-----------------------------------------------------------------*/
123 concat2 (char const *str1
, char const *str2
)
125 size_t len
= strlen (str1
) + strlen (str2
);
126 char *res
= xmalloc (len
+ 1);
128 cp
= stpcpy (res
, str1
);
129 cp
= stpcpy (cp
, str2
);
133 /*-----------------------------------------------------------------.
134 | Try to open file NAME with mode MODE, and print an error message |
136 `-----------------------------------------------------------------*/
139 xfopen (const char *name
, const char *mode
)
141 FILE *res
= fopen_safer (name
, mode
);
143 error (EXIT_FAILURE
, get_errno (),
144 _("%s: cannot open"), quotearg_colon (name
));
149 /*-------------------------------------------------------------.
150 | Try to close file PTR, and print an error message if fails. |
151 `-------------------------------------------------------------*/
160 error (EXIT_FAILURE
, 0, _("input/output error"));
162 if (fclose (ptr
) != 0)
163 error (EXIT_FAILURE
, get_errno (), _("cannot close file"));
168 xfdopen (int fd
, char const *mode
)
170 FILE *res
= fdopen (fd
, mode
);
172 error (EXIT_FAILURE
, get_errno (),
173 /* On a separate line to please the "unmarked_diagnostics"
179 /* The mapped name of FILENAME, allocated, if there are prefix maps.
182 map_file_name_alloc (char const *filename
)
184 struct prefix_map
const *p
= NULL
;
185 assert (prefix_maps
);
188 gl_list_iterator_t iter
= gl_list_iterator (prefix_maps
);
189 while (gl_list_iterator_next (&iter
, &ptr
, NULL
))
192 if (strncmp (p
->oldprefix
, filename
, strlen (p
->oldprefix
)) == 0)
196 gl_list_iterator_free (&iter
);
200 return xstrdup (filename
);
202 size_t oldprefix_len
= strlen (p
->oldprefix
);
203 size_t newprefix_len
= strlen (p
->newprefix
);
204 char *res
= xmalloc (newprefix_len
+ strlen (filename
) - oldprefix_len
+ 1);
206 char *end
= stpcpy (res
, p
->newprefix
);
207 stpcpy (end
, filename
+ oldprefix_len
);
213 string_equals (const void *x1
, const void *x2
)
217 return STREQ (s1
, s2
);
220 /* A hash function for NUL-terminated char* strings using
221 the method described by Bruno Haible.
222 See https://www.haible.de/bruno/hashfunc.html. */
224 string_hash (const void *x
)
226 #define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
232 h
= *s
+ ((h
<< 9) | (h
>> (SIZE_BITS
- 9)));
238 string_free (const void *cp
)
240 void *p
= (void*) cp
;
245 map_file_name (char const *filename
)
247 if (!filename
|| !prefix_maps
)
251 = gl_map_nx_create_empty (GL_HASH_MAP
,
252 string_equals
, string_hash
,
253 string_free
, string_free
);
254 const void *res
= gl_map_get (mapped_files
, filename
);
257 res
= map_file_name_alloc (filename
);
258 gl_map_put (mapped_files
, xstrdup (filename
), res
);
264 prefix_map_free (struct prefix_map
*p
)
272 add_prefix_map (char const *oldprefix
, char const *newprefix
)
276 = gl_list_create_empty (GL_ARRAY_LIST
,
279 (gl_listelement_dispose_fn
) prefix_map_free
,
282 struct prefix_map
*p
= xmalloc (sizeof (*p
));
283 p
->oldprefix
= xstrdup (oldprefix
);
284 p
->newprefix
= xstrdup (newprefix
);
286 gl_list_add_last (prefix_maps
, p
);
289 /*------------------------------------------------------------------.
290 | Compute ALL_BUT_EXT, ALL_BUT_TAB_EXT and output files extensions. |
291 `------------------------------------------------------------------*/
293 /* Compute extensions from the grammar file extension. */
295 compute_exts_from_gf (const char *ext
)
297 if (STREQ (ext
, ".y"))
299 src_extension
= xstrdup (language
->src_extension
);
300 header_extension
= xstrdup (language
->header_extension
);
304 src_extension
= xstrdup (ext
);
305 header_extension
= xstrdup (ext
);
306 tr (src_extension
, 'y', 'c');
307 tr (src_extension
, 'Y', 'C');
308 tr (header_extension
, 'y', 'h');
309 tr (header_extension
, 'Y', 'H');
313 /* Compute extensions from the given c source file extension. */
315 compute_exts_from_src (const char *ext
)
317 /* We use this function when the user specifies `-o' or `--output',
318 so the extensions must be computed unconditionally from the file name
319 given by this option. */
320 src_extension
= xstrdup (ext
);
321 header_extension
= xstrdup (ext
);
322 tr (header_extension
, 'c', 'h');
323 tr (header_extension
, 'C', 'H');
327 /* Decompose FILE_NAME in four parts: *BASE, *TAB, and *EXT, the fourth
328 part, (the directory) is ranging from FILE_NAME to the char before
329 *BASE, so we don't need an additional parameter.
331 *EXT points to the last period in the basename, or NULL if none.
333 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to
334 '.tab' or '_tab' if present right before *EXT, or is NULL. *TAB
335 cannot be equal to *BASE.
337 None are allocated, they are simply pointers to parts of FILE_NAME.
340 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT =
343 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c'
345 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c'
347 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c'
349 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab'
351 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL
353 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */
356 file_name_split (const char *file_name
,
357 const char **base
, const char **tab
, const char **ext
)
359 *base
= last_component (file_name
);
361 /* Look for the extension, i.e., look for the last dot. */
362 *ext
= strrchr (*base
, '.');
365 /* If there is an extension, check if there is a '.tab' part right
369 size_t baselen
= *ext
- *base
;
370 size_t dottablen
= sizeof (TAB_EXT
) - 1;
371 if (dottablen
< baselen
372 && STRPREFIX_LIT (TAB_EXT
, *ext
- dottablen
))
373 *tab
= *ext
- dottablen
;
377 /* Compute ALL_BUT_EXT and ALL_BUT_TAB_EXT from SPEC_OUTFILE or
380 The precise -o name will be used for FTABLE. For other output
381 files, remove the ".c" or ".tab.c" suffix. */
384 compute_file_name_parts (void)
388 const char *base
, *tab
, *ext
;
389 file_name_split (spec_outfile
, &base
, &tab
, &ext
);
390 dir_prefix
= xstrndup (spec_outfile
, base
- spec_outfile
);
392 /* ALL_BUT_EXT goes up the EXT, excluding it. */
394 xstrndup (spec_outfile
,
395 (strlen (spec_outfile
) - (ext
? strlen (ext
) : 0)));
397 /* ALL_BUT_TAB_EXT goes up to TAB, excluding it. */
399 xstrndup (spec_outfile
,
400 (strlen (spec_outfile
)
401 - (tab
? strlen (tab
) : (ext
? strlen (ext
) : 0))));
404 compute_exts_from_src (ext
);
408 const char *base
, *tab
, *ext
;
409 file_name_split (grammar_file
, &base
, &tab
, &ext
);
411 if (spec_file_prefix
)
413 /* If --file-prefix=foo was specified, ALL_BUT_TAB_EXT = 'foo'. */
415 xstrndup (spec_file_prefix
,
416 last_component (spec_file_prefix
) - spec_file_prefix
);
417 all_but_tab_ext
= xstrdup (spec_file_prefix
);
419 else if (! location_empty (yacc_loc
))
421 /* If --yacc, then the output is 'y.tab.c'. */
422 dir_prefix
= xstrdup ("");
423 all_but_tab_ext
= xstrdup ("y");
427 /* Otherwise, ALL_BUT_TAB_EXT is computed from the input
428 grammar: 'foo/bar.yy' => 'bar'. */
429 dir_prefix
= xstrdup ("");
431 xstrndup (base
, (strlen (base
) - (ext
? strlen (ext
) : 0)));
434 if (language
->add_tab
)
435 all_but_ext
= concat2 (all_but_tab_ext
, TAB_EXT
);
437 all_but_ext
= xstrdup (all_but_tab_ext
);
439 /* Compute the extensions from the grammar file name. */
440 if (ext
&& location_empty (yacc_loc
))
441 compute_exts_from_gf (ext
);
446 /* Compute the output file names. Warn if we detect conflicting
447 outputs to the same file. */
450 compute_output_file_names (void)
452 compute_file_name_parts ();
454 /* If not yet done. */
456 src_extension
= xstrdup (".c");
457 if (!header_extension
)
458 header_extension
= xstrdup (".h");
462 ? xstrdup (spec_outfile
)
463 : concat2 (all_but_ext
, src_extension
));
467 if (! spec_header_file
)
468 spec_header_file
= concat2 (all_but_ext
, header_extension
);
473 if (! spec_graph_file
)
474 spec_graph_file
= concat2 (all_but_tab_ext
, ".gv");
475 output_file_name_check (&spec_graph_file
, false);
480 if (! spec_html_file
)
481 spec_html_file
= concat2 (all_but_tab_ext
, ".html");
482 output_file_name_check (&spec_html_file
, false);
488 spec_xml_file
= concat2 (all_but_tab_ext
, ".xml");
489 output_file_name_check (&spec_xml_file
, false);
494 if (!spec_verbose_file
)
495 spec_verbose_file
= concat2 (all_but_tab_ext
, OUTPUT_EXT
);
496 output_file_name_check (&spec_verbose_file
, false);
499 free (all_but_tab_ext
);
500 free (src_extension
);
501 free (header_extension
);
505 output_file_name_check (char **file_name
, bool source
)
507 bool conflict
= false;
508 if (STREQ (*file_name
, grammar_file
))
510 complain (NULL
, complaint
, _("refusing to overwrite the input file %s"),
515 for (int i
= 0; i
< generated_files_size
; i
++)
516 if (STREQ (generated_files
[i
].name
, *file_name
))
518 complain (NULL
, Wother
, _("conflicting outputs to file %s"),
519 quote (generated_files
[i
].name
));
525 *file_name
= strdup ("/dev/null");
529 generated_files
= xnrealloc (generated_files
, ++generated_files_size
,
530 sizeof *generated_files
);
531 generated_files
[generated_files_size
-1].name
= xstrdup (*file_name
);
532 generated_files
[generated_files_size
-1].is_source
= source
;
537 unlink_generated_sources (void)
539 for (int i
= 0; i
< generated_files_size
; i
++)
540 if (generated_files
[i
].is_source
)
541 /* Ignore errors. The file might not even exist. */
542 unlink (generated_files
[i
].name
);
545 /* Memory allocated by relocate2, to free. */
546 static char *relocate_buffer
= NULL
;
552 return relocate_buffer
;
555 char const *cp
= getenv ("BISON_PKGDATADIR");
556 return cp
? cp
: relocate2 (PKGDATADIR
, &relocate_buffer
);
563 char const *m4
= getenv ("M4");
567 /* We don't use relocate2() to store the temporary buffer and re-use
568 it, because m4path() is only called once. */
569 char const *m4_relocated
= relocate (M4
);
571 if (stat (m4_relocated
, &buf
) == 0)
578 output_file_names_free (void)
581 free (spec_verbose_file
);
582 free (spec_graph_file
);
583 free (spec_html_file
);
584 free (spec_xml_file
);
585 free (spec_header_file
);
586 free (parser_file_name
);
588 for (int i
= 0; i
< generated_files_size
; i
++)
589 free (generated_files
[i
].name
);
590 free (generated_files
);
591 free (relocate_buffer
);
594 gl_list_free (prefix_maps
);
596 gl_map_free (mapped_files
);