Skip to content

Commit

Permalink
2024.05.27:
Browse files Browse the repository at this point in the history
* changed: _externals, bash: back merge
  • Loading branch information
andry81 committed May 27, 2024
1 parent 9056253 commit a24182c
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 51 deletions.
2 changes: 1 addition & 1 deletion _externals/tacklelib/bash/tacklelib/baselib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# pargs - list of not unique wildcard pattern items which passed in function arguments ($@).

# Script can be ONLY included by "source" command.
[[ -z "$BASH" || (-n "$BASH_LINENO" && BASH_LINENO[0] -le 0) || (-n "$SOURCE_TACKLELIB_BASELIB_SH" && SOURCE_TACKLELIB_BASELIB_SH -ne 0) ]] && return
[[ -n "$BASH" && (-z "$BASH_LINENO" || BASH_LINENO[0] -gt 0) && (-z "$SOURCE_TACKLELIB_BASELIB_SH" || SOURCE_TACKLELIB_BASELIB_SH -eq 0) ]] || return 0 || exit 0 # exit to avoid continue if the return can not be called

SOURCE_TACKLELIB_BASELIB_SH=1 # including guard

Expand Down
212 changes: 167 additions & 45 deletions _externals/tacklelib/bash/tacklelib/bash_tacklelib
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,50 @@
# Tacklelib project library bash shell script module
#
# Repositories:
#
# * https://github.com/andry81/tacklelib
# * https://sf.net/p/tacklelib
# * https://bitbucket.org/andry81/tacklelib
# * https://gitlab.com/andry81/tacklelib
#
# Base bash shell library module with a basic set of functionality.
# Designed to be included through the absolute path and the source command -
# `source '/bin/bash_tacklelib'`.
# Designed to be included through the absolute path and the source command:
#
# >
# source '/bin/bash_tacklelib'
#
# Or by using a builtin search paths logic with include guards:
#
# >
# if [[ -z "$SOURCE_TACKLELIB_BASH_TACKLELIB_SH" || SOURCE_TACKLELIB_BASH_TACKLELIB_SH -eq 0 ]]; then
# # builtin search
# for BASH_SOURCE_DIR in "/usr/local/bin" "/usr/bin" "/bin"; do
# if [[ -f "$BASH_SOURCE_DIR/bash_tacklelib" ]]; then
# source "$BASH_SOURCE_DIR/bash_tacklelib" || exit $?
# break
# fi
# done
# fi
#
# After that all other scripts can be included through the alternative
# inclusion function - `tkl_include` or `tkl_include_or_abort`.
# inclusion function:
#
# The `tkl_include*` function does use an improved search logic and instead
# of a current directory path a being included shell script is searched through
# the inclusion path variable - `BASH_SOURCE_PATH` or a current script
# >
# tkl_include <path>
#
# Or:
#
# >
# tkl_include_or_abort <path>
#
# The `tkl_include*` function does use an improved search paths logic and
# instead of a current directory path a being included shell script is searched
# through the inclusion path variable - `BASH_SOURCE_PATH` or a current script
# directory in case if a relative inclusion path is not applicable to
# paths from the `BASH_SOURCE_PATH` variable.
#

# Script can be ONLY included by "source" command.
[[ -z "$BASH" || (-n "$BASH_LINENO" && BASH_LINENO[0] -le 0) || (-n "$SOURCE_TACKLELIB_BASH_TACKLELIB_SH" && SOURCE_TACKLELIB_BASH_TACKLELIB_SH -ne 0) ]] && return 0
[[ -n "$BASH" && (-z "$BASH_LINENO" || BASH_LINENO[0] -gt 0) && (-z "$SOURCE_TACKLELIB_BASH_TACKLELIB_SH" || SOURCE_TACKLELIB_BASH_TACKLELIB_SH -eq 0) ]] || return 0 || exit 0 # exit to avoid continue if the return can not be called

if (( BASH_VERSINFO[0] < 3 )); then
echo "$0: error: script designed only for the Bash version 3.x or higher." >&2
Expand Down Expand Up @@ -163,6 +188,52 @@ function tkl_declare_array_as_expr_from_args()
eval "unset out_var expr; tkl_declare_array '$out_var' $expr"
}

function tkl_if_math_expr()
{
(( "$*" || ! "$*" )) 2> /dev/null && return 0
return 1
}

function tkl_if_int()
{
if (( "$*" || ! "$*" )) 2> /dev/null; then
local __tmp
(( __tmp = "$*" )) 2> /dev/null # assignment with evaluation and deduction
[[ "$__tmp" == "$*" || "+$__tmp" == "$*" ]] && return 0 # test on not deduced expression
fi
return 1
}

# casts any not integer and not deduced value to 0, otherwise leave as is
function tkl_cast_to_int()
{
local __tmp
local __var
local __value
for __var in "$@"; do
if (( "$__var" || ! "$__var" )) 2> /dev/null; then
if [[ -n "$__var" ]]; then
eval "__value=\"\$$__var\""
else
__value=''
fi
#echo "__value=$__value"
if [[ -n "$__value" && "$__value" != "__value" ]]; then # bash issue workaround for variable '_'
__tmp=''
(( __tmp = "$__var" )) 2> /dev/null # assignment with evaluation and deduction
#echo "__tmp=$__tmp"
if [[ "$__tmp" != "$__value" && "+$__tmp" != "$__value" ]]; then # test on not deduced expression
(( "$__var" = 0 )) # reset to 0 because deduced expression still is not an integer
fi
else
(( "$__var" = 0 )) # reset to 0 if empty
fi
else
(( "$__var" = 0 )) # reset to 0 because is not a math expression
fi
done
}

# NOTE:
#
# * `tkl_*trim*`
Expand Down Expand Up @@ -406,9 +477,11 @@ function tkl_exit()
{
local last_error=${1:-$?}

[[ -n "$NEST_LVL" ]] && (( NEST_LVL-- ))
[[ -z "$NEST_LVL" ]] || (( NEST_LVL-- ))

[[ -n "$2" ]] && { echo "$2"; }
if [[ -n "$2" ]]; then
echo "$2"
fi

tkl_declare_global tkl__last_error $last_error

Expand All @@ -427,23 +500,78 @@ function tkl_exit_if_error()
}

# NOTE:
# Exit with custom user exit code or the $? variable and optional user stdout message.
# Exit with custom user exit code or the $? variable and optional user stderr message.
#
function tkl_abort()
{
local last_error=${1:-$?}

[[ -n "$NEST_LVL" ]] && (( NEST_LVL-- ))
[[ -z "$NEST_LVL" ]] || (( NEST_LVL-- ))

echo "${FUNCNAME[0]}: ${FUNCNAME[1]}: code=\`$last_error\`; line=\`${BASH_LINENO[1]}\`; source=\`${BASH_SOURCE[1]}\`" >&2
if [[ -n "$2" ]]; then
echo "${FUNCNAME[0]}: code=$last_error; msg=\`$2\`" >&2
else
echo "${FUNCNAME[0]}: code=$last_error" >&2
fi

[[ -n "$2" ]] && { echo "$2" >&2; }
tkl_dump_call_stack >&2

echo

tkl_dump_includes_stack >&2

tkl_declare_global tkl__last_error $last_error

exit $last_error
}

function tkl_dump_call_stack()
{
local stack_size=${#FUNCNAME[@]}
local i
local zeros=0

echo 'Call stack dump:'

for (( i=1; i < stack_size; i++ )); do
echo " [${zeros:${#i}-1}$i] ${FUNCNAME[i]}:"$'\n'" ${BASH_SOURCE[i+1]:-.} : ${BASH_LINENO[i]}"
done

echo '---'
}

function tkl_dump_includes_stack()
{
local stack_size=$tkl__vars_stack__global__BASH_SOURCE_CMD_LINE__size
local i j k
local zeros=0
local cmd_line_arr

tkl_cast_to_int stack_size

echo 'tkl_include* stack dump:'

echo " [00] ${BASH_SOURCE_CMD_LINE_ARR[0]}:"
for (( i=1; i < ${#BASH_SOURCE_CMD_LINE_ARR[@]}; i++ )); do
echo " [${zeros:${#k}-1}$k]=\`${BASH_SOURCE_CMD_LINE_ARR[k]}\`"
done

for (( i=0; i < stack_size; i++ )); do
if (( tkl__vars_stack__global__BASH_SOURCE_CMD_LINE__${i}__defined )); then
eval tkl_deserialize_array "\${tkl__vars_stack__global__BASH_SOURCE_CMD_LINE__$i}" cmd_line_arr

(( j = i + 1 ))
echo " [${zeros:${#j}-1}$j] ${cmd_line_arr[0]}:"
for (( j=1; j < ${#cmd_line_arr[@]}; j++ )); do
(( k = j - 1 ))
echo " [${zeros:${#k}-1}$k] \`${cmd_line_arr[j]}\`"
done
fi
done

echo '---'
}

function tkl_pushd()
{
if [[ -z "$@" ]]; then
Expand Down Expand Up @@ -668,7 +796,7 @@ function tkl_get_source_file_path()
local ScriptFilePath="${0//\\//}"
fi

tkl_get_abs_path_from_dir "$ScriptFilePath" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s
tkl_get_abs_path "$ScriptFilePath" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s
}

function tkl_make_source_file_components()
Expand Down Expand Up @@ -783,25 +911,25 @@ function tkl_unset_show_includes()
# Alternative inclusion command additionally to the `source` command.
function tkl_include()
{
local last_error=$?
local _84CB4B34_last_error=$?

local IFS=$' \t\r\n' # workaround for the bug in the "[@]:i" expression under the bash version lower than 4.1

# CAUTION:
# 1. All variables here must be unique irrespective to the function scope,
# 1. All variables from here must be unique irrespective to the function scope,
# because `source "..."` still can remove or change a local variable!

local is_first_time_include=0
local _84CB4B34_is_first_time_include=0
if [[ -z "$BASH_SOURCE_NEST_LVL" ]]; then
tkl_export BASH_SOURCE_NEST_LVL 0
is_first_time_include=1
_84CB4B34_is_first_time_include=1
fi

if tkl_is_abs_path "$1"; then
if tkl_get_abs_path_from_dir "$1" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s; then
tkl_set_return $last_error
if tkl_get_abs_path "$1" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s; then
tkl_set_return $_84CB4B34_last_error
tkl_include_local_impl "$RETURN_VALUE" "${@:2}"
last_error=$?
_84CB4B34_last_error=$?
fi
else
local _84CB4B34_included=0
Expand All @@ -817,35 +945,34 @@ function tkl_include()
;;
esac
for _84CB4B34_path_prefix in $BASH_SOURCE_PATH; do
if tkl_get_abs_path_from_dir "$_84CB4B34_path_prefix/$1" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s; then
if tkl_get_abs_path "$_84CB4B34_path_prefix/$1" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s; then
if [[ -f "$RETURN_VALUE" ]]; then
_84CB4B34_included=1
tkl_set_return $last_error
tkl_set_return $_84CB4B34_last_error
tkl_include_local_impl "$RETURN_VALUE" "${@:2}"
last_error=$?
_84CB4B34_last_error=$?
break
fi
fi
done
fi

if (( ! _84CB4B34_included )); then
(( is_first_time_include )) && tkl_make_source_file_components "${@:2}"

if tkl_get_abs_path_from_dir "$BASH_SOURCE_DIR" "$1" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s; then
tkl_set_return $last_error
(( ! _84CB4B34_is_first_time_include )) || tkl_make_source_file_components "${@:2}"
if tkl_get_abs_path "$BASH_SOURCE_DIR" "$1" && tkl_convert_backend_path_to_native "$RETURN_VALUE" -s; then
tkl_set_return $_84CB4B34_last_error
tkl_include_local_impl "$RETURN_VALUE" "${@:2}"
last_error=$?
_84CB4B34_last_error=$?
fi
fi
fi

return $last_error
return $_84CB4B34_last_error
}

function tkl_include_local_impl()
{
local last_error=$?
local _84CB4B34_last_error=$?

local IFS=$' \t\r\n' # workaround for the bug in the "[@]:i" expression under the bash version lower than 4.1

Expand Down Expand Up @@ -880,7 +1007,7 @@ function tkl_include_local_impl()
# NOTE:
# Set `trap RETURN` handler in case of erly exist before the `source` command.
#
builtin trap "tkl_local_return_${#FUNCNAME[@]}; builtin trap - RETURN" RETURN || return $last_error
builtin trap "tkl_local_return_${#FUNCNAME[@]}; builtin trap - RETURN" RETURN || return $_84CB4B34_last_error

#echo "tkl_include_local_impl: ${BASH_SOURCE_FILE} -> $1"

Expand All @@ -898,7 +1025,7 @@ function tkl_include_local_impl()
echo "${source_prefix// /| }source: \`$BASH_SOURCE_CMD_LINE\`"
fi

tkl_declare_global tkl__last_error $last_error
tkl_declare_global tkl__last_error $_84CB4B34_last_error

# CAUTION:
# We must avoid call to `trap RETURN` handler by the `source` command return itself, so we reset the handler.
Expand Down Expand Up @@ -941,14 +1068,9 @@ function tkl_get_native_parent_dir()
return 0
}

function tkl_abort_include()
{
tkl_abort $? "${FUNCNAME[0]}: ${FUNCNAME[1]}: code=\`$?\`; line=\`${BASH_LINENO[1]}\`; source=\`${BASH_SOURCE[1]}\`"
}

function tkl_include_or_abort()
{
tkl_include "$@" || tkl_abort $? "${FUNCNAME[0]}: ${FUNCNAME[1]}: code=\`$?\`; line=\`${BASH_LINENO[1]}\`; source=\`${BASH_SOURCE[1]}\`"
tkl_include "$@" || tkl_abort
}

function tkl_read_command_line_flags()
Expand Down Expand Up @@ -978,21 +1100,21 @@ function tkl_read_command_line_flags()
done
}

function tkl_get_abs_path_from_dir()
function tkl_get_abs_path()
{
# drop return value
RETURN_VALUE=''

local DirPath="$1"
local BasePath="$1"
local RelativePath="$2"

# drop line returns
DirPath="${DirPath//[$'\r\n']}"
BasePath="${BasePath//[$'\r\n']}"
RelativePath="${RelativePath//[$'\r\n']}"

if [[ -n "$DirPath" ]]; then
if [[ -n "$BasePath" ]]; then
if [[ "${RelativePath:0:1}" != '/' ]]; then
tkl_normalize_path -a -- "$DirPath${RelativePath:+/}$RelativePath" || return 2
tkl_normalize_path -a -- "$BasePath${RelativePath:+/}$RelativePath" || return 2
else
tkl_normalize_path -a -- "$RelativePath" || return 3
fi
Expand Down
2 changes: 1 addition & 1 deletion _externals/tacklelib/bash/tacklelib/funclib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Script library to support function object.

# Script can be ONLY included by "source" command.
[[ -z "$BASH" || (-n "$BASH_LINENO" && BASH_LINENO[0] -le 0) || (-n "$SOURCE_TACKLELIB_FUNCLIB_SH" && SOURCE_TACKLELIB_FUNCLIB_SH -ne 0) ]] && return
[[ -n "$BASH" && (-z "$BASH_LINENO" || BASH_LINENO[0] -gt 0) && (-z "$SOURCE_TACKLELIB_FUNCLIB_SH" || SOURCE_TACKLELIB_FUNCLIB_SH -eq 0) ]] || return 0 || exit 0 # exit to avoid continue if the return can not be called

SOURCE_TACKLELIB_FUNCLIB_SH=1 # including guard

Expand Down
2 changes: 1 addition & 1 deletion _externals/tacklelib/bash/tacklelib/hashlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Script library of hash functions.

# Script can be ONLY included by "source" command.
[[ -z "$BASH" || (-n "$BASH_LINENO" && BASH_LINENO[0] -le 0) || (-n "$SOURCE_TACKLELIB_HASHLIB_SH" && SOURCE_TACKLELIB_HASHLIB_SH -ne 0) ]] && return
[[ -n "$BASH" && (-z "$BASH_LINENO" || BASH_LINENO[0] -gt 0) && (-z "$SOURCE_TACKLELIB_HASHLIB_SH" || SOURCE_TACKLELIB_HASHLIB_SH -eq 0) ]] || return 0 || exit 0 # exit to avoid continue if the return can not be called

SOURCE_TACKLELIB_HASHLIB_SH=1 # including guard

Expand Down
2 changes: 1 addition & 1 deletion _externals/tacklelib/bash/tacklelib/traplib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
#

# Script can be ONLY included by "source" command.
[[ -z "$BASH" || (-n "$BASH_LINENO" && BASH_LINENO[0] -le 0) || (-n "$SOURCE_TACKLELIB_TRAPLIB_SH" && SOURCE_TACKLELIB_TRAPLIB_SH -ne 0) ]] && return
[[ -n "$BASH" && (-z "$BASH_LINENO" || BASH_LINENO[0] -gt 0) && (-z "$SOURCE_TACKLELIB_TRAPLIB_SH" || SOURCE_TACKLELIB_TRAPLIB_SH -eq 0) ]] || return 0 || exit 0 # exit to avoid continue if the return can not be called

SOURCE_TACKLELIB_TRAPLIB_SH=1 # including guard

Expand Down
Loading

0 comments on commit a24182c

Please sign in to comment.