[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 5/9] builtins/source: refactor file searching function
From: |
Matheus Afonso Martins Moreira |
Subject: |
[PATCH 5/9] builtins/source: refactor file searching function |
Date: |
Sun, 5 May 2024 06:54:58 -0300 |
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 b1567aab..f7c37a14 100644
--- a/builtins/source.def
+++ b/builtins/source.def
@@ -87,6 +87,30 @@ int source_uses_path = 1;
is not found in the $PATH. */
int source_searches_cwd = 1;
+static char *
+search_for_file (list)
+ 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 (list)
@@ -113,31 +137,20 @@ source_builtin (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
- Re: [PATCH 2/9] findcmd: parameterize path variable in functions, (continued)
[PATCH 3/9] findcmd: define the user library finder function, Matheus Afonso Martins Moreira, 2024/05/05
[PATCH 4/9] bashgetopt: define long option shortener function, Matheus Afonso Martins Moreira, 2024/05/05
Re: [PATCH 4/9] bashgetopt: define long option shortener function, Chet Ramey, 2024/05/08
[PATCH 5/9] builtins/source: refactor file searching function,
Matheus Afonso Martins Moreira <=
[PATCH 6/9] builtins/source: define library search function, Matheus Afonso Martins Moreira, 2024/05/05
[PATCH 7/9] builtins/source: add the -l|--library options, Matheus Afonso Martins Moreira, 2024/05/05
[PATCH 8/9] builtins/source: search libraries in library mode, Matheus Afonso Martins Moreira, 2024/05/05
[PATCH 9/9] variables: define default BASH_LIBRARIES_PATH, Matheus Afonso Martins Moreira, 2024/05/05
Re: [PATCH 0/9] Add library mode to source builtin, Lawrence Velázquez, 2024/05/05