Bash scripts can be tricky to get right so reusing proven solutions as shell script libraries is of immense value. However, the existing shell script sourcing mechanisms are suboptimal for this task.
The source builtin uses the PATH variable for resolving file names which means they would have to be placed alongside normal executables which could cause false positives: executables and commands might be accidentally sourced instead, causing hard to debug problems. This could be fixed by overriding PATH so that it contains only library directories but this interferes with the normal execution of the sourced scripts: they are no longer able to run commands normally because the commands are not in the PATH. This is an undesirable and ultimately unnecessary limitation. This patch set adds a special operating mode to the existing source builtin to make it behave in the desired way. When source is passed the options --library or -l, it will search for files in the directories given by the BASH_LIBRARIES_PATH environment variable, and only in those directories. The PATH will not be modified. A build time configurable default value is defined which includes the user's home directory in addition to system directories, enabling users to easily develop their own scripting systems. This forms the basis of a rudimentary module loading system. In order to support this feature, existing code was refactored so as to be more functional and parameterized. Bash's existing facilities were reused as much as possible. Signed-off-by: Matheus Afonso Martins Moreira <math...@matheusmoreira.com> Matheus Afonso Martins Moreira (9): builtins: extract file content executor function findcmd: parameterize path variable in functions findcmd: define the user library finder function bashgetopt: define long option shortener function builtins/source: refactor file searching function builtins/source: define library search function builtins/source: add the -l|--library options builtins/source: search libraries in library mode variables: define default BASH_LIBRARIES_PATH builtins/bashgetopt.c | 24 +++++++ builtins/bashgetopt.h | 7 ++ builtins/common.c | 64 ++++++++++++++++++ builtins/common.h | 1 + builtins/source.def | 153 ++++++++++++++++++++---------------------- config-top.h | 7 ++ findcmd.c | 37 ++++++---- findcmd.h | 1 + variables.c | 1 + 9 files changed, 203 insertions(+), 92 deletions(-) -- 2.44.0