summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2020-04-04 19:52:57 -0400
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2020-04-04 19:52:57 -0400
commit6638e53935ee1c5f3bfb23c08bd2aae2994b6219 (patch)
tree6da694b231da9ed7bf406496bd114522cfd686cd
parent2d64adb0f339a3344e880bc6add832abd707ce8a (diff)
downloadnextbin-6638e53935ee1c5f3bfb23c08bd2aae2994b6219.tar.xz
nextbin-6638e53935ee1c5f3bfb23c08bd2aae2994b6219.zip
change algorithm, fix edge cases, add tests
-rwxr-xr-xnextbin116
-rwxr-xr-xtest/a/x3
-rwxr-xr-xtest/b/x3
-rwxr-xr-xtest/c/x3
-rwxr-xr-xtest/start59
5 files changed, 162 insertions, 22 deletions
diff --git a/nextbin b/nextbin
index 2e1d9dd..fd8faa5 100755
--- a/nextbin
+++ b/nextbin
@@ -1,31 +1,103 @@
#!/bin/sh
-orig_exe=$(realpath -s "$1")
+set -e
+
+die() {
+ fmt=$1
+ shift
+ printf "nextbin %s failed: $fmt\n" "$orig_exe" "$@" >&2
+ exit 1
+}
+
+usage() {
+ # shellcheck disable=SC2016
+ echo 'usage: nextbin [-v] "$0" "$@"' >&2
+}
+
+case "$1" in
+ -v) action="echo"; shift;;
+ -*|'') usage; exit 1;;
+ *) action="exec"
+esac
+orig_exe=$1
+shift
exe_name=${orig_exe##*/}
+
+[ -n "$PATH" ] || die 'PATH is empty'
+
case "$orig_exe" in
- */*) ;;
+ */)
+ die 'ends with /'
+ ;;
+
+ */*)
+ orig_dir=${orig_exe%/*}
+ ;;
+
*)
- printf 'error: cannot find next executable: argv[0] is %s, missing /\n' "$orig_exe"
- exit 1
+ # either shell has not resolved script-file into a path, or script is
+ # in cwd and PATH has . or empty entries
+ if ! [ -d "$orig_exe" ] && [ -x "$orig_exe" ]; then
+ case ":$PATH:" in
+ *::*:.:*|*:.:*::*)
+ die 'exe in cwd and PATH has both . and empty entries'
+ ;;
+
+ *::*)
+ orig_dir=
+ ;;
+
+ *:.:*)
+ orig_dir=.
+ ;;
+
+ *)
+ die 'missing /'
+ esac
+ fi
esac
-paths=
-passed_paths=
-set -f
-_IFS=$IFS
-IFS=:
-for path in $PATH; do
- file=$path/$exe_name
- if [ "$file" = "$orig_exe" ]; then
- if [ -n "$passed_paths" ] && [ -n "$paths" ]; then
- printf 'error: ambiguous next executable for %s past %s, due to multiple non-consecutive instances of %s in PATH' "$exe_name" "$orig_exe" "$(dirname "$orig_exe")"
- exit 1
+
+tmp_path=:$PATH:
+new_path=${tmp_path#*:$orig_dir:}
+[ -n "$new_path" ] \
+ || die '%s is last PATH entry' "$orig_dir"
+if [ "$new_path" != "$tmp_path" ]; then
+ new_path_2=${new_path#*:$orig_dir:}
+ [ "$new_path" = "$new_path_2" ] \
+ || die 'duplicate entry in PATH: %s' \
+ "$orig_exe" "$orig_dir"
+else
+ # try to find equivalent path
+ found_alt_dir=
+ new_orig_dir=
+ equiv_orig_dir=$(cd "${orig_dir:-.}" && pwd -L)
+ tmp_path_2=$PATH
+ while :; do
+ p=${tmp_path_2%%:*}
+ if [ "$(cd "$p" >/dev/null 2>&1 && pwd -L)" = "$equiv_orig_dir" ]; then
+ [ -z "$found_alt_dir" ] \
+ || die "duplicate equivalent path in PATH: '%s' = '%s'" \
+ "$new_orig_dir" "$p"
+ found_alt_dir=1
+ new_orig_dir=$p
fi
- passed_paths=${paths+$paths:}
- paths=
+ case "$tmp_path_2" in
+ *:*) tmp_path_2=${tmp_path_2#*:};;
+ *) break
+ esac
+ done
+ if [ -n "$found_alt_dir" ]; then
+ new_path=${tmp_path#*:$new_orig_dir:}
+ [ -n "$new_path" ] \
+ || die '%s is last PATH entry' "$new_orig_dir"
else
- paths="${paths+$paths:}$path"
+ new_path=$PATH:
fi
-done
-IFS=$_IFS
-PATH="$passed_paths$paths" exec "$exe_name"
-exit 1
+fi
+
+exe=$(PATH="${new_path%:}" command -v "$exe_name") || true
+[ -n "$exe" ] \
+ || die 'PATH lookup failed using %s' \
+ "${new_path%:}"
+
+$action "$exe" "$@"
diff --git a/test/a/x b/test/a/x
new file mode 100755
index 0000000..e7d6387
--- /dev/null
+++ b/test/a/x
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "in a with $0, next"
+nextbin "$0" "$@"
diff --git a/test/b/x b/test/b/x
new file mode 100755
index 0000000..0355509
--- /dev/null
+++ b/test/b/x
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "in b with $0, next"
+nextbin "$0" "$@"
diff --git a/test/c/x b/test/c/x
new file mode 100755
index 0000000..cc0070b
--- /dev/null
+++ b/test/c/x
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "in c with $0, done"
+exit 0
diff --git a/test/start b/test/start
new file mode 100755
index 0000000..4cf0d9d
--- /dev/null
+++ b/test/start
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+set -e
+
+cd "${0%/*}"
+exec 2>&1
+
+check() {
+ for arg in $1; do
+ read -r line || { echo "FAIL: insufficient output" >&2; return 1; }
+ echo "GOT: $line"
+ case "$arg" in
+ [a-z]) case "$line" in "in $arg "*) ;; *) echo "FAIL: expected $arg" >&2; return 1; esac;;
+ err) case "$line" in nextbin*failed*) ;; *) echo "FAIL: expected err" >&2; return 1; esac;;
+ *) echo "INVALID TEST" >&2; exit 1
+ esac
+ done
+ if read -r line; then
+ echo "FAIL: too much output" >&2
+ return 1
+ fi
+ echo "OK"
+}
+
+try() {
+ # shellcheck disable=SC2016
+ printf 'TEST: PATH="$PATH:%s" %s, expect %s\n' "$@"
+ PATH="$PATH:$1" "$2" 2>&1 | check "$3"
+ # shellcheck disable=SC2016
+ printf 'TEST: PATH="%s:$PATH" %s, expect %s\n' "$@"
+ PATH="$1:$PATH" "$2" 2>&1 | check "$3"
+}
+
+do_try() {
+ try "$1" "$2/x" "$3"
+ try "$1" "./$2/x" "$3"
+ try "$1" "$PWD/$2/x" "$3"
+ try "$1" "$2/../$2/x" "$3"
+ try "$1" "./$2/../$2/x" "$3"
+ try "$1" "$PWD/$2/../$2/x" "$3"
+ echo
+}
+
+do_try2() {
+ try "$1" "x" "$3"
+ do_try "$@"
+}
+
+export PATH="$PWD/..:$PATH"
+
+do_try "" a "a err"
+do_try2 "$PWD/a" a "a err"
+do_try "$PWD/b" a "a b err"
+do_try2 "$PWD/a:$PWD/b" a "a b err"
+do_try "$PWD/a:$PWD/b" b "b err"
+do_try2 "$PWD/a:$PWD/b:$PWD/c" a "a b c"
+do_try "$PWD/a:$PWD/b:$PWD/c" b "b c"
+
+echo "ALL TESTS COMPLETED SUCCESSFULLY :)"