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 <math...@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