diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-08-11 11:42:23 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-08-11 11:54:06 +0300 |
commit | cd788243496b69e548998f9e3f9ff80e34977652 (patch) | |
tree | 0fd50f566b69bc2cfd0d9c4c18eea1b77d5ec276 /contrib/tools/bison/src/files.c | |
parent | c7230d56fb1b7998da0edb829f1751640da9c8b4 (diff) | |
download | ydb-cd788243496b69e548998f9e3f9ff80e34977652.tar.gz |
Update contrib/tools/bison to 3.7.6
583623e1fb299df0a04a0aecdc47eb759ef412b9
Diffstat (limited to 'contrib/tools/bison/src/files.c')
-rw-r--r-- | contrib/tools/bison/src/files.c | 112 |
1 files changed, 108 insertions, 4 deletions
diff --git a/contrib/tools/bison/src/files.c b/contrib/tools/bison/src/files.c index d383a0ede0..b4f57e656e 100644 --- a/contrib/tools/bison/src/files.c +++ b/contrib/tools/bison/src/files.c @@ -1,6 +1,6 @@ /* Open and close files for Bison. - Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2020 Free + Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2021 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. @@ -16,19 +16,24 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. */ + along with this program. If not, see <https://www.gnu.org/licenses/>. */ #include <config.h> #include "system.h" #include <configmake.h> /* PKGDATADIR */ -#include <error.h> #include <dirname.h> +#include <error.h> #include <get-errno.h> +#include <gl_array_list.h> +#include <gl_xlist.h> #include <quote.h> #include <quotearg.h> #include <relocatable.h> /* relocate2 */ #include <stdio-safer.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> #include <xstrndup.h> #include "complain.h" @@ -52,6 +57,7 @@ char *spec_verbose_file = NULL; /* for --verbose. */ char *spec_graph_file = NULL; /* for -g. */ char *spec_xml_file = NULL; /* for -x. */ char *spec_header_file = NULL; /* for --defines. */ +char *spec_mapped_header_file = NULL; char *parser_file_name; /* All computed output file names. */ @@ -70,7 +76,7 @@ static int generated_files_size = 0; uniqstr grammar_file = NULL; /* If --output=dir/foo.c was specified, - DIR_PREFIX is 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'. + DIR_PREFIX gis 'dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are 'dir/foo'. If --output=dir/foo.tab.c was specified, DIR_PREFIX is 'dir/', ALL_BUT_EXT is 'dir/foo.tab', and ALL_BUT_TAB_EXT is 'dir/foo'. @@ -88,11 +94,20 @@ uniqstr grammar_file = NULL; char *all_but_ext; static char *all_but_tab_ext; char *dir_prefix; +char *mapped_dir_prefix; /* C source file extension (the parser source). */ static char *src_extension = NULL; /* Header file extension (if option '`-d'' is specified). */ static char *header_extension = NULL; + +struct prefix_map +{ + char *oldprefix; + char *newprefix; +}; + +static gl_list_t prefix_maps = NULL; /*-----------------------------------------------------------------. | Return a newly allocated string composed of the concatenation of | @@ -160,6 +175,70 @@ xfdopen (int fd, char const *mode) return res; } +/* Given an input file path, returns a dynamically allocated string that + contains the path with the file prefix mapping rules applied, or NULL if + the input was NULL. */ +char * +map_file_name (char const *filename) +{ + if (!filename) + return NULL; + + struct prefix_map const *p = NULL; + if (prefix_maps) + { + void const *ptr; + gl_list_iterator_t iter = gl_list_iterator (prefix_maps); + while (gl_list_iterator_next (&iter, &ptr, NULL)) + { + p = ptr; + if (strncmp (p->oldprefix, filename, strlen (p->oldprefix)) == 0) + break; + p = NULL; + } + gl_list_iterator_free (&iter); + } + + if (!p) + return xstrdup (filename); + + size_t oldprefix_len = strlen (p->oldprefix); + size_t newprefix_len = strlen (p->newprefix); + char *s = xmalloc (newprefix_len + strlen (filename) - oldprefix_len + 1); + + char *end = stpcpy (s, p->newprefix); + stpcpy (end, filename + oldprefix_len); + + return s; +} + +static void +prefix_map_free (struct prefix_map *p) +{ + free (p->oldprefix); + free (p->newprefix); + free (p); +} + +/* Adds a new file prefix mapping. If a file path starts with oldprefix, it + will be replaced with newprefix */ +void +add_prefix_map (char const *oldprefix, char const *newprefix) +{ + if (!prefix_maps) + prefix_maps = gl_list_create_empty (GL_ARRAY_LIST, + /* equals */ NULL, + /* hashcode */ NULL, + (gl_listelement_dispose_fn) prefix_map_free, + true); + + struct prefix_map *p = xmalloc (sizeof (*p)); + p->oldprefix = xstrdup (oldprefix); + p->newprefix = xstrdup (newprefix); + + gl_list_add_last (prefix_maps, p); +} + /*------------------------------------------------------------------. | Compute ALL_BUT_EXT, ALL_BUT_TAB_EXT and output files extensions. | `------------------------------------------------------------------*/ @@ -370,6 +449,9 @@ compute_output_file_names (void) output_file_name_check (&spec_verbose_file, false); } + spec_mapped_header_file = map_file_name (spec_header_file); + mapped_dir_prefix = map_file_name (dir_prefix); + free (all_but_tab_ext); free (src_extension); free (header_extension); @@ -431,6 +513,23 @@ pkgdatadir (void) } } +char const * +m4path (void) +{ + char const *m4 = getenv ("M4"); + if (m4) + return m4; + + /* We don't use relocate2() to store the temporary buffer and re-use + it, because m4path() is only called once. */ + char const *m4_relocated = relocate (M4); + struct stat buf; + if (stat (m4_relocated, &buf) == 0) + return m4_relocated; + + return M4; +} + void output_file_names_free (void) { @@ -439,10 +538,15 @@ output_file_names_free (void) free (spec_graph_file); free (spec_xml_file); free (spec_header_file); + free (spec_mapped_header_file); free (parser_file_name); free (dir_prefix); + free (mapped_dir_prefix); for (int i = 0; i < generated_files_size; i++) free (generated_files[i].name); free (generated_files); free (relocate_buffer); + + if (prefix_maps) + gl_list_free (prefix_maps); } |