>From a0b22b1670fcfaca39976fec38101bd231f6cd01 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup
Date: Wed, 28 May 2014 12:25:52 +0200 Subject: [PATCH 2/2] modules,inclusions: fix path searching issues When 'm4' directory occurred in M4PATH or current directory, the m4 processor ended up with message 'Is a directory' and did not continue to search for 'm4.so' somewhere else in path. Expanding 'include(Makefile)' for example failed when M4PATH was set because the current directory was not searched. * m4/path.c (try_prefixes): New function. (m4_path_search): Deduplicate suffix trying by try_prefixes. (m4__include_init): Always prepend current directory. * tests/testsuite.at (AT_CHECK_M4): Filter out test output dependant on user's setup. * tests/options.at: Adjust expected stderr output. * doc/m4.texi: likewise. --- doc/m4.texi | 2 + m4/path.c | 111 +++++++++++++++++++++++++++-------------------------- tests/options.at | 6 ++- tests/testsuite.at | 1 + 4 files changed, 64 insertions(+), 56 deletions(-) diff --git a/doc/m4.texi b/doc/m4.texi index e09f3c4..e938a30 100644 --- a/doc/m4.texi +++ b/doc/m4.texi @@ -4683,6 +4683,8 @@ @comment options: -dip @example $ @kbd{m4 -dip -I doc/examples} address@hidden: path search for 'm4' found 'm4.so' address@hidden: path search for 'gnu' found 'gnu.so' @error{}m4debug: input read from 'stdin' define(`foo', `m4wrap(`wrapped text ')dnl') diff --git a/m4/path.c b/m4/path.c index 281795e..fb7084d 100644 --- a/m4/path.c +++ b/m4/path.c @@ -144,6 +144,53 @@ m4_add_include_directory (m4 *context, const char *dir, bool prepend) #endif } +/* FILENAME must contain directory path also */ +static char * +try_prefixes (m4 *context, const char *dirname, const char *filename, + size_t max_suffix_len, const char **suffixes) +{ + int e = 0, i; + char *filepath = dirname ? file_name_concat (dirname, filename, NULL) + : strdup (filename); + size_t mem = strlen (filepath); + filepath = xrealloc (filepath, mem + max_suffix_len + 1); + + /* Try appending each of the suffixes we were given. */ +#ifdef DEBUG_INCL + xfprintf (stderr, "path_search (%s) -- trying %s\n", filename, filepath); +#endif + + + /* If search fails, we'll use the error we got from the first try + access (usually with no suffix). */ + for (i = 0; suffixes && suffixes[i]; !i && (e = errno), ++i) + { + struct stat st; + int rc; + strcpy (filepath + mem, suffixes[i]); + + if (stat (filepath, &st)) + continue; + + if (S_ISREG (st.st_mode)) + { + m4_debug_message (context, M4_DEBUG_TRACE_PATH, + _("path search for %s found %s"), + quotearg_style (locale_quoting_style, filename), + quotearg_n_style (1, locale_quoting_style, + filepath)); + return filepath; + } + if (S_ISDIR (st.st_mode)) + errno = EISDIR; + } + free (filepath); + + /* No such file. */ + errno = e; + return NULL; +} + /* Search for FILENAME according to -B options, `.', -I options, then M4PATH environment. If successful, return the open file, and if @@ -182,61 +229,18 @@ m4_path_search (m4 *context, const char *filename, const char **suffixes) /* If file is absolute, or if we are not searching a path, a single lookup will do the trick. */ if (IS_ABSOLUTE_FILE_NAME (filename)) - { - size_t mem = strlen (filename); - - /* Try appending each of the suffixes we were given. */ - filepath = strncpy (xmalloc (mem + max_suffix_len +1), filename, mem); - for (i = 0; suffixes && suffixes[i]; ++i) - { - strcpy (filepath + mem, suffixes[i]); - if (access (filepath, R_OK) == 0) - return filepath; - - /* If search fails, we'll use the error we got from the first - access (usually with no suffix). */ - if (i == 0) - e = errno; - } - free (filepath); - - /* No such file. */ - errno = e; - return NULL; - } + return try_prefixes (context, NULL, filename, max_suffix_len, suffixes); - for (incl = m4__get_search_path (context)->list; - incl != NULL; incl = incl->next) + for (incl = m4__get_search_path (context)->list, i = 0; + incl != NULL; incl = incl->next, ++i) { - char *pathname = file_name_concat (incl->dir, filename, NULL); - size_t mem = strlen (pathname); - -#ifdef DEBUG_INCL - xfprintf (stderr, "path_search (%s) -- trying %s\n", filename, pathname); -#endif - - if (access (pathname, R_OK) == 0) - { - m4_debug_message (context, M4_DEBUG_TRACE_PATH, - _("path search for %s found %s"), - quotearg_style (locale_quoting_style, filename), - quotearg_n_style (1, locale_quoting_style, pathname)); - return pathname; - } - else if (!incl->len) - /* Capture errno only when searching `.'. */ - e = errno; - - filepath = strncpy (xmalloc (mem + max_suffix_len +1), pathname, mem); - free (pathname); + char *pathname = try_prefixes (context, incl->dir, filename, + max_suffix_len, suffixes); + if (pathname) + return pathname; - for (i = 0; suffixes && suffixes[i]; ++i) - { - strcpy (filepath + mem, suffixes[i]); - if (access (filepath, R_OK) == 0) - return filepath; - } - free (filepath); + if (i == 0) + e = errno; } errno = e; @@ -337,8 +341,7 @@ m4__include_init (m4 *context) /* If M4PATH was not set, then search just the current directory by default. */ assert (info); - if (info->list_end == NULL) - search_path_add (info, "", false); + search_path_add (info, "", true); /* Non-core modules installation directory. */ search_path_add (info, PKGLIBDIR, false); diff --git a/tests/options.at b/tests/options.at index 7084207..8fa19b6 100644 --- a/tests/options.at +++ b/tests/options.at @@ -423,10 +423,12 @@ m4debug: input from m4wrap exhausted dnl Test all flags. AT_CHECK_M4([-dV in], [0], [[3 0 -]], [[m4debug: module m4: opening file +]], [[m4debug: path search for 'm4' found 'm4.so' +m4debug: module m4: opening file m4debug: module m4: init hook called m4debug: module m4: opened m4debug: module m4: builtins loaded +m4debug: path search for 'gnu' found 'gnu.so' m4debug: module gnu: opening file m4debug: module gnu: init hook called m4debug: module gnu: opened @@ -701,7 +703,7 @@ AT_CHECK_M4([-I post -B pre in], [1], [[in pre/foo in ./bar in post/blah -]], [[m4:in:3: include: cannot open file 'bad': No such file or directory +]], [[m4:in:3: include: cannot open file 'bad': Too many levels of symbolic links ]]) AT_CLEANUP diff --git a/tests/testsuite.at b/tests/testsuite.at index aad7f0d..5778c8d 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -89,6 +89,7 @@ m4_case([$4], [], [], [ignore], [], /^m4debug: module/s/opening file.*/opening file/ s/\(cannot open module [^:]*\):.*/\1/ s/Bad file number/Bad file descriptor/ + s|search for \([^ ]*\) found '\''.*\/\([^\.]*\.so\)|search for \1 found '\''\2| s/^m4:.* option .*/m4: bad option/ ' stderr >&2]], [0], [], [$4])]) ]) -- 1.9.3