Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolving of relative script paths #62

Draft
wants to merge 2 commits into
base: bash-5.2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions lib/file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
# with bashdb; see the file COPYING. If not, write to the Free Software
# Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.

# Directory search patch for unqualified file names

# Directory search patch for unqualified file names.
# We resolve against the current working directory first because that's most likely what the script does at runtime.
# cwd is the current working directory, _Dbg_cdir is the directory in which the script is located.
typeset -a _Dbg_dir
_Dbg_dir=('\$cdir' '\$cwd' )
_Dbg_dir=( '\$cwd' '\$cdir' )

# _Dbg_cdir is the directory in which the script is located.
[[ -z ${_Dbg_cdir} ]] && typeset _Dbg_cdir=${_Dbg_source_file%/*}
[[ -z ${_Dbg_cdir} ]] && typeset _Dbg_cdir=$(pwd)
[[ -z ${_Dbg_cdir} ]] && typeset _Dbg_cdir="${_Dbg_source_file%/*}"
[[ -z ${_Dbg_cdir} ]] && typeset _Dbg_cdir="$(pwd)"

# Either fill out or strip filename as determined by "basename_only"
# and annotate settings
Expand Down Expand Up @@ -66,57 +67,56 @@ _Dbg_tilde_expand_filename() {
# Resolve $1 to a full file name which exists. First see if filename has been
# mentioned in a debugger "file" command. If not and the file name
# is a relative name use _Dbg_dir to substitute a relative directory name.
# '' is echo'd if no file found. Return 0 (in $?) if found, 1 if not.
#
function _Dbg_resolve_expand_filename {

if (( $# == 0 )) ; then
_Dbg_errmsg \
"Internal debug error _Dbg_resolve_expand_filename(): null file to find"
_Dbg_errmsg "Internal debug error _Dbg_resolve_expand_filename(): null file to find"
echo ''
return 1
fi
typeset find_file="$1"

# Is this one of the files we've that has been specified in a debugger
# "FILE" command?
typeset found_file
found_file="${_Dbg_file2canonic[$find_file]}"
if [[ -n $found_file ]] ; then
echo "$found_file"
typeset find_file="$1"
# Is this one of the files we've that has been specified in a debugger # "FILE" command?
typeset try_find_file
try_find_file="${_Dbg_file2canonic["$find_file"]}"
if [[ -n "$try_find_file" ]] ; then
echo "$try_find_file"
return 0
fi

# Absolute path name
if [[ ${find_file:0:1} == '/' ]] ; then
# Absolute file name
full_find_file=$(_Dbg_expand_filename "$find_file")
echo "$full_find_file"
return 0
elif [[ ${find_file:0:1} == '.' ]] ; then
# Relative file name
full_find_file=$(_Dbg_expand_filename "${_Dbg_init_cwd}/$find_file")
if [[ -z "$full_find_file" ]] || [[ ! -r $full_find_file ]]; then
# Try using cwd rather than Dbg_init_cwd
full_find_file=$(_Dbg_expand_filename "$find_file")
try_find_file="$(_Dbg_expand_filename "$find_file")"
if [[ -e "$try_find_file" ]] ; then
echo "$try_find_file"
return 0
fi
echo "$full_find_file"
return 0
else
# Resolve file using _Dbg_dir
typeset -i n=${#_Dbg_dir[@]}
typeset -i i
for (( i=0 ; i < n; i++ )) ; do
typeset dirname="${_Dbg_dir[i]}"
if [[ "$dirname" == '\$cdir' ]] ; then
dirname="$_Dbg_cdir"
elif [[ "$dirname" == '\$cwd' ]] ; then
dirname="$(pwd)"
fi
if [[ -f "$dirname/$find_file" ]] ; then
echo "$dirname/$find_file"
return 0
fi
done

# Return error because absolute path does not exist
echo ''
return 1
fi

# Resolve file using _Dbg_dir
typeset -i n=${#_Dbg_dir[@]}
typeset -i i
for (( i=0 ; i < n; i++ )) ; do
typeset basedir="${_Dbg_dir[i]}"
if [[ "$basedir" == '\$cdir' ]] ; then
basedir="$_Dbg_cdir"
elif [[ "$basedir" == '\$cwd' ]] ; then
basedir="$(pwd -P)"
fi

try_find_file="$(_Dbg_expand_filename "$basedir/$find_file")"
if [[ -e "$try_find_file" ]] ; then
echo "$try_find_file"
return 0
fi
done

# Return error because no file was found
echo ''
return 1
}
44 changes: 4 additions & 40 deletions lib/filecache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,50 +126,14 @@ function _Dbg_is_file {
echo ''
return 1
fi
# first character might be encoded as \057 == '/',
# find_file:0:1 == "\" , true story
typeset find_file="$(printf "$1")"
typeset try_find_file

if [[ -z $find_file ]] ; then
_Dbg_errmsg "Internal debug error _Dbg_is_file(): file argument null"
echo ''
return 1
fi

if [[ ${find_file:0:1} == '/' ]] ; then
# Absolute file name
try_find_file=$(_Dbg_expand_filename "$find_file")
if [[ -n ${_Dbg_filenames[$try_find_file]} ]] ; then
echo "$try_find_file"
return 0
fi
elif [[ ${find_file:0:1} == '.' ]] ; then
# Relative file name
try_find_file=$(_Dbg_expand_filename "${_Dbg_init_cwd}/$find_file")
# FIXME: turn into common subroutine
if [[ -n ${_Dbg_filenames[$try_find_file]} ]] ; then
echo "$try_find_file"
return 0
fi
else
# Resolve file using _Dbg_dir
typeset -i n=${#_Dbg_dir[@]}
typeset -i i
for (( i=0 ; i < n; i++ )) ; do
typeset basename="${_Dbg_dir[i]}"
if [[ $basename == '\$cdir' ]] ; then
basename=$_Dbg_cdir
elif [[ $basename == '\$cwd' ]] ; then
basename=$(pwd)
fi
try_find_file="$basename/$find_file"
if [[ -f "$try_find_file" ]] ; then
typeset try_find_file
try_find_file="$(_Dbg_resolve_expand_filename "$1")"
if [[ -n "$try_find_file" && -n "${_Dbg_filenames["$try_find_file"]}" ]] ; then
echo "$try_find_file"
return 0
fi
done
fi

echo ''
return 1
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test-file.sh.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!@SH_PROG@
# -*- shell-script -*-
this_script=test-filecache.sh

# Test _Dbg_file_canonic
test_file_file_canonic()
Expand Down Expand Up @@ -88,10 +89,9 @@ test_file_resolve_expand_filename()
filename=$(_Dbg_resolve_expand_filename .)
assertEquals '0' $?
assertEquals '/' "${filename:0:1}"
filename=$(_Dbg_resolve_expand_filename ./fdafdsa)
filename=$(_Dbg_resolve_expand_filename ./$this_script)
assertEquals '0' $?
typeset -i size=${#filename}
# assertEquals 'fdafdsas' "${filename:$size-8}"
assertNotEquals '.' "${filename:$size-1}"
}

Expand Down
Loading