From 944c1ab558ae57a788c2392d74e20d3d1505c02b Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Tue, 24 May 2022 11:17:12 +0200 Subject: [PATCH] libstd.bash: upvar replaced with reference variables --- Pmodules/libstd.bash | 73 +++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/Pmodules/libstd.bash b/Pmodules/libstd.bash index cbe2121..3a8dae4 100644 --- a/Pmodules/libstd.bash +++ b/Pmodules/libstd.bash @@ -78,41 +78,42 @@ std::get_abspath() { } std::append_path () { - local -r P=$1 - local -r d=$2 + local -nr P="$1" + shift 1 + local dir + local dirs='' + for dir in "$@"; do + [[ "${P}" == @(|*:)${dir}@(|:*) ]] && continue + dirs+=":${dir}" + done - if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then - if [[ -z ${!P} ]]; then - export "$P=${d}" - else - export "$P=${!P}:${d}" - fi - fi + if [[ -z ${P} ]]; then + P="${dirs:1}" # remove leading ':' + else + P="${P}${dirs}" + fi } std::prepend_path () { local -nr P="$1" shift 1 - local path="$1" - shift 1 + local dir + local dirs='' + for dir in "$@"; do + [[ "${P}" == @(|*:)${dir}@(|:*) ]] && continue + dirs+="${dir}:" + done if [[ -z ${P} ]]; then - for dir in "$@"; do - path+=":${dir}" - done - P="${path}" + P="${dirs:0:-1}" # remove trailing ':' else - for dir in "$@"; do - [[ "${P}" == @(|*:)${dir}@(|:*) ]] && continue - path+=":${dir}" - done - P="${path}:${P}" + P="${dirs}${P}" fi } std::remove_path() { - local -r P=$1 + local -nr P="$1" shift 1 local -ar dirs="$@" local new_path='' @@ -124,8 +125,7 @@ std::remove_path() { [[ "${entry}" != "${dir}" ]] && new_path+=":${entry}" done done - # remove leading ':' - std::upvar "$P" "${new_path:1}" + P="${new_path:1}" # remove leading ':' } # @@ -175,22 +175,23 @@ std::replace_path () { # analog to std::split_abspath() with a relative path. # std::split_path() { - local parts="$1" - local -r path="$2" + local -n parts="$1" + local -r path="$2" IFS='/' local std__split_path_result=( ${std__split_path_tmp} ) unset IFS - std::upvar ${parts} "${std__split_path_result[@]}" + parts="${std__split_path_result[@]}" if (( $# >= 3 )); then # return number of parts - std::upvar "$3" ${#std__split_path_result[@]} + local -n num="$3" + num="${#std__split_path_result[@]}" fi } std::split_abspath() { - local parts="$1" - local -r path="$2" + local -n parts="$1" + local -r path="$2" if [[ "${path:0:1}" == '/' ]]; then local -r std__split_path_tmp="${path:1}" else @@ -200,16 +201,17 @@ std::split_abspath() { IFS='/' local std__split_path_result=( ${std__split_path_tmp} ) unset IFS - std::upvar ${parts} "${std__split_path_result[@]}" + parts="${std__split_path_result[@]}" if (( $# >= 3 )); then # return number of parts - std::upvar "$3" ${#std__split_path_result[@]} + local -n num="$3" + num="${#std__split_path_result[@]}" fi } std::split_relpath() { - local parts="$1" - local -r path="$2" + local -n parts="$1" + local -r path="$2" if [[ "${path:0:1}" == '/' ]]; then std::die 255 "Oops: Internal error in '${FUNCNAME[0]}' called by '${FUNCNAME[1]}' }" else @@ -219,10 +221,11 @@ std::split_relpath() { IFS='/' local std__split_path_result=( ${std__split_path_tmp} ) unset IFS - std::upvar ${parts} "${std__split_path_result[@]}" + parts="${std__split_path_result[@]}" if (( $# >= 3 )); then # return number of parts - std::upvar "$3" ${#std__split_path_result[@]} + local -n num="$3" + num="${#std__split_path_result[@]}" fi }