[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 4/8] builtins/source: refactor file searching function
From: |
Matheus Afonso Martins Moreira |
Subject: |
[PATCH v2 4/8] builtins/source: refactor file searching function |
Date: |
Mon, 13 May 2024 07:37:22 -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 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
- Re: Re: [PATCH v2 1/8] findcmd: parameterize path variable in functions, (continued)
- Re: [PATCH v2 1/8] findcmd: parameterize path variable in functions, Chet Ramey, 2024/05/14
- Re: [PATCH v2 1/8] findcmd: parameterize path variable in functions, Chet Ramey, 2024/05/14
- Re: [PATCH v2 1/8] findcmd: parameterize path variable in functions, Matheus Afonso Martins Moreira, 2024/05/14
- Re: [PATCH v2 1/8] findcmd: parameterize path variable in functions, Matheus Afonso Martins Moreira, 2024/05/14
- Re: [PATCH v2 1/8] findcmd: parameterize path variable in functions, Chet Ramey, 2024/05/15
[PATCH v2 2/8] findcmd: define find_in_path_var function, Matheus Afonso Martins Moreira, 2024/05/13
[PATCH v2 3/8] builtins/source: extract file executor function, Matheus Afonso Martins Moreira, 2024/05/13
[PATCH v2 4/8] builtins/source: refactor file searching function,
Matheus Afonso Martins Moreira <=
[PATCH v2 5/8] builtins/source: parse the -i option, Matheus Afonso Martins Moreira, 2024/05/13
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Chet Ramey, 2024/05/14
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Greg Wooledge, 2024/05/14
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Lawrence Velázquez, 2024/05/14
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Koichi Murase, 2024/05/14
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Koichi Murase, 2024/05/14
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Chet Ramey, 2024/05/15
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Koichi Murase, 2024/05/15
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Chet Ramey, 2024/05/16
- Re: [PATCH v2 5/8] builtins/source: parse the -i option, Greg Wooledge, 2024/05/16