[prev in list] [next in list] [prev in thread] [next in thread]
List: bash-bug
Subject: [PATCH v2 4/8] builtins/source: refactor file searching function
From: Matheus Afonso Martins Moreira <matheus.a.m.moreira () gmail ! com>
Date: 2024-05-13 10:37:22
Message-ID: 20240513103726.17085-5-matheus () matheusmoreira ! com
[Download RAW message or body]
Extract the file searching algorithm of the source builtin into
a static helper function. Makes the code easier to understand
and separates the searching from the error handling logic.
Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
---
builtins/source.def | 59 +++++++++++++++++++++++++++------------------
1 file changed, 36 insertions(+), 23 deletions(-)
diff --git a/builtins/source.def b/builtins/source.def
index 334404bd..ba5e1596 100644
--- a/builtins/source.def
+++ b/builtins/source.def
@@ -82,6 +82,7 @@ extern int errno;
static void uw_maybe_pop_dollar_vars (void *);
static int execute_file_contents (WORD_LIST *, char *, char *);
+static char *search_for_file (WORD_LIST *);
/* If non-zero, `.' uses $PATH to look up the script to be sourced. */
int source_uses_path = 1;
@@ -153,6 +154,29 @@ execute_file_contents (WORD_LIST *list, char *filename, char *framename)
return (result);
}
+static char *
+search_for_file (WORD_LIST *list)
+{
+ char *filename = NULL, *x;
+
+ /* XXX -- should this be absolute_pathname? */
+ if (posixly_correct && strchr (list->word->word, '/'))
+ filename = savestring (list->word->word);
+ else if (absolute_pathname (list->word->word))
+ filename = savestring (list->word->word);
+ else if (source_uses_path)
+ filename = find_path_file (list->word->word);
+ if (filename == 0)
+ {
+ if (source_searches_cwd == 0)
+ return (NULL);
+ else
+ filename = savestring (list->word->word);
+ }
+
+ return (filename);
+}
+
/* Read and execute commands from the file passed as argument. */
int
source_builtin (WORD_LIST *list)
@@ -178,31 +202,20 @@ source_builtin (WORD_LIST *list)
}
#endif
- filename = (char *)NULL;
- /* XXX -- should this be absolute_pathname? */
- if (posixly_correct && strchr (list->word->word, '/'))
- filename = savestring (list->word->word);
- else if (absolute_pathname (list->word->word))
- filename = savestring (list->word->word);
- else if (source_uses_path)
- filename = find_path_file (list->word->word);
+ filename = search_for_file (list);
+
if (filename == 0)
{
- if (source_searches_cwd == 0)
- {
- x = printable_filename (list->word->word, 0);
- builtin_error (_("%s: file not found"), x);
- if (x != list->word->word)
- free (x);
- if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0)
- {
- last_command_exit_value = EXECUTION_FAILURE;
- jump_to_top_level (EXITPROG);
- }
- return (EXECUTION_FAILURE);
- }
- else
- filename = savestring (list->word->word);
+ x = printable_filename (list->word->word, 0);
+ builtin_error (_("%s: file not found"), x);
+ if (x != list->word->word)
+ free (x);
+ if (posixly_correct && interactive_shell == 0 && executing_command_builtin == 0)
+ {
+ last_command_exit_value = EXECUTION_FAILURE;
+ jump_to_top_level (EXITPROG);
+ }
+ return (EXECUTION_FAILURE);
}
return execute_file_contents (list, filename, "source");
--
2.44.0
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic