[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 3/8] builtins/source: extract file executor function
From: |
Matheus Afonso Martins Moreira |
Subject: |
[PATCH v2 3/8] builtins/source: extract file executor function |
Date: |
Mon, 13 May 2024 07:37:21 -0300 |
Extract into a dedicated helper function the code which loads
the contents of a file and executes it in the current shell.
This separates this useful functionality from the path resolution
mechanism used by the source builtin.
Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
---
builtins/source.def | 78 +++++++++++++++++++++++++--------------------
1 file changed, 44 insertions(+), 34 deletions(-)
diff --git a/builtins/source.def b/builtins/source.def
index b68d16a5..334404bd 100644
--- a/builtins/source.def
+++ b/builtins/source.def
@@ -81,6 +81,7 @@ extern int errno;
#endif /* !errno */
static void uw_maybe_pop_dollar_vars (void *);
+static int execute_file_contents (WORD_LIST *, char *, char *);
/* If non-zero, `.' uses $PATH to look up the script to be sourced. */
int source_uses_path = 1;
@@ -112,11 +113,51 @@ uw_maybe_pop_dollar_vars (void *ignore)
This cannot be done in a subshell, since things like variable assignments
take place in there. So, I open the file, place it into a large string,
close the file, and then execute the string. */
+static int
+execute_file_contents (WORD_LIST *list, char *filename, char *framename)
+{
+ int result;
+ char *debug_trap;
+
+ begin_unwind_frame (framename);
+ add_unwind_protect (xfree, filename);
+
+ if (list->next)
+ {
+ push_dollar_vars ();
+ add_unwind_protect (uw_maybe_pop_dollar_vars, NULL);
+ if (debugging_mode || shell_compatibility_level <= 44)
+ init_bash_argv (); /* Initialize BASH_ARGV and BASH_ARGC */
+ remember_args (list->next, 1);
+ if (debugging_mode)
+ push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */
+ }
+ set_dollar_vars_unchanged ();
+
+ /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded)
+ is set. XXX - should sourced files inherit the RETURN trap? Functions
+ don't. */
+ debug_trap = TRAP_STRING (DEBUG_TRAP);
+ if (debug_trap && function_trace_mode == 0)
+ {
+ debug_trap = savestring (debug_trap);
+ add_unwind_protect (xfree, debug_trap);
+ add_unwind_protect (uw_maybe_set_debug_trap, debug_trap);
+ restore_default_signal (DEBUG_TRAP);
+ }
+
+ result = source_file (filename, (list && list->next));
+
+ run_unwind_frame (framename);
+
+ return (result);
+}
+
+/* Read and execute commands from the file passed as argument. */
int
source_builtin (WORD_LIST *list)
{
- int result;
- char *filename, *debug_trap, *x;
+ char *filename, *x;
if (no_options (list))
return (EX_USAGE);
@@ -164,36 +205,5 @@ source_builtin (WORD_LIST *list)
filename = savestring (list->word->word);
}
- begin_unwind_frame ("source");
- add_unwind_protect (xfree, filename);
-
- if (list->next)
- {
- push_dollar_vars ();
- add_unwind_protect (uw_maybe_pop_dollar_vars, NULL);
- if (debugging_mode || shell_compatibility_level <= 44)
- init_bash_argv (); /* Initialize BASH_ARGV and BASH_ARGC */
- remember_args (list->next, 1);
- if (debugging_mode)
- push_args (list->next); /* Update BASH_ARGV and BASH_ARGC */
- }
- set_dollar_vars_unchanged ();
-
- /* Don't inherit the DEBUG trap unless function_trace_mode (overloaded)
- is set. XXX - should sourced files inherit the RETURN trap? Functions
- don't. */
- debug_trap = TRAP_STRING (DEBUG_TRAP);
- if (debug_trap && function_trace_mode == 0)
- {
- debug_trap = savestring (debug_trap);
- add_unwind_protect (xfree, debug_trap);
- add_unwind_protect (uw_maybe_set_debug_trap, debug_trap);
- restore_default_signal (DEBUG_TRAP);
- }
-
- result = source_file (filename, (list && list->next));
-
- run_unwind_frame ("source");
-
- return (result);
+ return execute_file_contents (list, filename, "source");
}
--
2.44.0
- 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 <=
[PATCH v2 4/8] builtins/source: refactor file searching function, Matheus Afonso Martins Moreira, 2024/05/13
[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