Last active
July 10, 2024 11:56
-
-
Save konsolebox/a908cf13e511abdf05daec89a9cbdd8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> git diff shell.c builtins/evalfile.c | |
diff --git a/builtins/evalfile.c b/builtins/evalfile.c | |
index 17a568a0..204f0044 100644 | |
--- a/builtins/evalfile.c | |
+++ b/builtins/evalfile.c | |
@@ -85,6 +85,7 @@ _evalfile (const char *filename, int flags) | |
size_t file_size; | |
sh_vmsg_func_t *errfunc; | |
#if defined (ARRAY_VARS) | |
+ char *real_filename, realbuf[PATH_MAX+1]; | |
SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v; | |
ARRAY *funcname_a, *bash_source_a, *bash_lineno_a; | |
struct func_array_state *fa; | |
@@ -244,7 +245,8 @@ file_error_and_exit: | |
GET_ARRAY_FROM_VAR ("BASH_ARGC", bash_argc_v, bash_argc_a); | |
# endif | |
- array_push (bash_source_a, (char *)filename); | |
+ real_filename = sh_realpath (filename, realbuf); | |
+ array_push (bash_source_a, real_filename != NULL ? real_filename : (char *)filename); | |
t = itos (executing_line_number ()); | |
array_push (bash_lineno_a, t); | |
free (t); | |
diff --git a/shell.c b/shell.c | |
index 01fffac2..60b2c32d 100644 | |
--- a/shell.c | |
+++ b/shell.c | |
@@ -1578,6 +1578,7 @@ open_shell_script (char *script_name) | |
int sample_len; | |
struct stat sb; | |
#if defined (ARRAY_VARS) | |
+ char *real_filename, realbuf[PATH_MAX+1]; | |
SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v; | |
ARRAY *funcname_a, *bash_source_a, *bash_lineno_a; | |
#endif | |
@@ -1638,7 +1639,8 @@ open_shell_script (char *script_name) | |
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a); | |
GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a); | |
- array_push (bash_source_a, filename); | |
+ real_filename = sh_realpath (filename, realbuf); | |
+ array_push (bash_source_a, real_filename != NULL ? real_filename : filename); | |
if (bash_lineno_a) | |
{ | |
t = itos (executing_line_number ()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ BASH_VERSINFO -ge 5 && ${SOURCE_EXTENSIONS_LOADED-} != true ]]; then | |
function source._die { | |
printf '%s\n' "$1" >&2 | |
exit "${2-1}" | |
} | |
function source._get_calling_script_dir { | |
[[ ${BASH_SOURCE[3]+.} ]] || source._die "Calling script's location unknown." | |
[[ ${BASH_SOURCE[3]} == /* ]] || source._die "Calling script's path not absolute." | |
_calling_script_dir=${BASH_SOURCE[2]%/*} _calling_script_dir=${_calling_script_dir:-/} | |
} | |
function source._a { | |
local paths realpath p _calling_script_dir=() | |
IFS=: read -r paths <<< "$1" | |
for p in "${paths[@]}"; do | |
[[ $p ]] || continue | |
if [[ $p != /* ]]; then | |
[[ ${_calling_script_dir+.} ]] || source._get_calling_script_dir | |
p=${_calling_script_dir}/$p | |
fi | |
realpath=$(realpath -Pm -- "$p") || source._die "Failed to get real path of '$p'." | |
BASH_SOURCE_PATH+=${BASH_SOURCE_PATH:+:}${realpath} | |
done | |
} | |
function source._I { | |
declare -gA BASH_SOURCE_INCLUDED | |
local filename=$2 realpath | |
shift 2 | |
realpath=$(realpath -Pe -- "${filename}") || \ | |
source._die "Failed to get real path of '${filename}'." | |
if [[ -z ${BASH_SOURCE_INCLUDED[${realpath}]+.} ]]; then | |
BASH_SOURCE_INCLUDED[${realpath}]=. | |
command source -- "${realpath}" "$@" | |
fi | |
} | |
function source._i { | |
local callback=$1 filename=$2 main_script_dir=() p _calling_script_dir=() | |
shift 2 | |
if [[ ${filename} == @(/*|./*|../*) ]]; then | |
if [[ ${filename} != /* ]]; then | |
[[ ${_calling_script_dir+.} ]] || source._get_calling_script_dir | |
filename=${_calling_script_dir}/${filename} | |
fi | |
[[ -e ${filename} ]] || source._die "File doesn't exist: ${filename}" | |
"${callback}" -- "${filename}" "$@" | |
else | |
IFS=: read -r paths <<< "${BASH_SOURCE_PATH}" | |
[[ ${#paths[@]} -gt 0 ]] || paths=(.) | |
for p in "${paths[@]}"; do | |
[[ $p ]] || continue | |
if [[ $p != /* ]]; then | |
if [[ -z ${main_script_dir+.} ]]; then | |
[[ ${#BASH_SOURCE[@]} -gt 2 ]] || source._die "Main script's location unknown." | |
[[ ${BASH_SOURCE[-1]} == /* ]] || source._die "Main script's path isn't absolute." | |
main_script_dir=${BASH_SOURCE[-1]%/*} main_script_dir=${main_script_dir:-/} | |
fi | |
p=${main_script_dir}/$p | |
fi | |
if [[ -e $p/${filename} ]]; then | |
"${callback}" -- "$p/${filename}" "$@" | |
return | |
fi | |
done | |
source._die "File not found in BASH_SOURCE_PATH: ${filename}" | |
fi | |
} | |
function source { | |
local mode= | |
while [[ $# -gt 0 ]]; do | |
case $1 ind | |
-[aA]) | |
[[ ${2+.} ]] || source._die "No argument specified to $1." | |
[[ $1 == -A ]] && BASH_SOURCE_PATH= | |
source._a "$2" | |
shift | |
;; | |
-[iI]) | |
mode=${1#-} | |
;; | |
--) | |
shift | |
break | |
;; | |
-?*) | |
source._die "Invalid option: $1" | |
;; | |
*) | |
break | |
;; | |
esac | |
shift | |
done | |
[[ ${1+.} ]] || source._die "Filename argument required" | |
[[ $1 ]] || source._die "Invalid empty filename" | |
if [[ ${mode} == i ]]; then | |
source._i source "$@" | |
elif [[ ${mode} == I ]]; then | |
source._i source._I "$@" | |
else | |
command source -- "$@" | |
fi | |
} | |
SOURCE_EXTENSIONS_LOADED=true | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment