|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
#
|
|
|
8d7a8a |
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂa Andrea Illanes Albornoz <lucia@luciaillanes.de>
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
# set +o errexit -o noglob -o nounset is assumed.
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
#
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_check_prereqsV() - check for existence of list of commands
|
|
|
4243a1 |
# @_rstatus: out reference to status string
|
|
|
4243a1 |
# @...: commands list as positional parameters
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
#
|
|
|
4243a1 |
rtl_check_prereqsV() {
|
|
|
e9fa07 |
local _rcp_rstatus="${1#\$}" \
|
|
|
e9fa07 |
_rcp_cmd="" _rcp_cmds_missing="" _rcp_rc=0;
|
|
|
e9fa07 |
shift;
|
|
|
e9fa07 |
|
|
|
e9fa07 |
for _rcp_cmd in "${@}"; do
|
|
|
e9fa07 |
if ! which "${_rcp_cmd}" >/dev/null 2>&1; then
|
|
|
e9fa07 |
_rcp_cmds_missing="${_rcp_cmds_missing:+${_rcp_cmds_missing} }${_rcp_cmd}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
done;
|
|
|
e9fa07 |
if [ "${_rcp_cmds_missing:+1}" = 1 ]; then
|
|
|
e9fa07 |
_rcp_rc=1;
|
|
|
e9fa07 |
rtl_setrstatus "${_rcp_rstatus}" 'Error: missing prerequisite package(s): '"${_rcp_cmds_missing}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
|
e9fa07 |
return "${_rcp_rc}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_clean_env() - unset environment variables w/ exceptions
|
|
|
4243a1 |
# @_env_vars_except: list of environment variables to keep
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
#
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
rtl_clean_env() {
|
|
|
e9fa07 |
local _rce_env_vars_except="${1}" \
|
|
|
e9fa07 |
_rce_env_var="" _rce_env_vars="" _rce_env_vars_unset="";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
_rce_env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
for _rce_env_var in ${_rce_env_vars}; do
|
|
|
e9fa07 |
if [ "${_rce_env_var#DEFAULT_}" != "${_rce_env_var}" ]\
|
|
|
e9fa07 |
|| [ "${_rce_env_var#PKG_}" != "${_rce_env_var}" ]; then
|
|
|
e9fa07 |
rtl_lconcat \$_rce_env_vars_except "${_rce_env_var}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
done;
|
|
|
e9fa07 |
rtl_lfilter2 \$_rce_env_vars \$_rce_env_vars_unset "${_rce_env_vars_except}";
|
|
|
e9fa07 |
rtl_unset_vars ${_rce_env_vars_unset};
|
|
|
e9fa07 |
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_get_cpu_count() - obtain CPU count
|
|
|
4243a1 |
# @_rstatus: out reference to status string
|
|
|
4243a1 |
# @_rcount: out reference to CPU count
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
#
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
rtl_get_cpu_count() {
|
|
|
e9fa07 |
local _rgcc_rstatus="${1#\$}" _rgcc_rcount="${2#\$}" \
|
|
|
e9fa07 |
_rgcc_line="" _rgcc_ncpus=0 _rgcc_rc=0 _rgcc_sname="";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
_rgcc_sname="$(uname -s 2>/dev/null)" || return 1;
|
|
|
e9fa07 |
case "${_rgcc_sname}" in
|
|
|
e9fa07 |
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
Linux) if [ ! -e "/proc/cpuinfo" ]; then
|
|
|
e9fa07 |
_rgcc_rc=1;
|
|
|
e9fa07 |
rtl_setrstatus "${_rgcc_rstatus}" 'Error: /proc/cpuinfo non-existent.';
|
|
|
e9fa07 |
else while read -r _rgcc_line; do
|
|
|
e9fa07 |
if rtl_match "${_rgcc_line}" "processor*:"; then
|
|
|
e9fa07 |
: $((_rgcc_ncpus+=1));
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
|
e9fa07 |
done < /proc/cpuinfo;
|
|
|
e9fa07 |
_rgcc_rc=0;
|
|
|
e9fa07 |
rtl_setrstatus "${_rgcc_rstatus}" "";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi; ;;
|
|
|
e9fa07 |
|
|
|
e9fa07 |
*) _rgcc_rc=1;
|
|
|
e9fa07 |
rtl_setrstatus "${_rgcc_rstatus}" 'Error: unknown platform \`'"${_rgcc_sname}"''\''.';
|
|
|
e9fa07 |
;;
|
|
|
e9fa07 |
|
|
|
e9fa07 |
esac;
|
|
|
e9fa07 |
eval ${_rgcc_rcount}='${_rgcc_ncpus}';
|
|
|
e9fa07 |
return "${_rgcc_rc}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_get_var_unsafe() - get value of variable
|
|
|
4243a1 |
# @_rvval: out reference to variable value
|
|
|
4243a1 |
# @[-u]: optionally convert variable name to upper case
|
|
|
4243a1 |
# @_vname: variable name
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
# N.B.: This function is *unsafe* and impossible to implement otherwise w/o filtering @_vname
|
|
|
4243a1 |
# and implicitly allows for code execution and other undefined behaviour via @_vname.
|
|
|
4243a1 |
# Do *not* pass untrusted input through @_vname.
|
|
|
4243a1 |
#
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
rtl_get_var_unsafe() {
|
|
|
4243a1 |
local _rgvu_rvval="${1#\$}" \
|
|
|
e9fa07 |
_rgvu_vname="";
|
|
|
e9fa07 |
shift;
|
|
|
e9fa07 |
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
if [ "x${1}" = "x-u" ]; then
|
|
|
e9fa07 |
shift; _rgvu_vname="${1}"; rtl_toupper \$_rgvu_vname;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
else
|
|
|
e9fa07 |
_rgvu_vname="${1}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
|
4243a1 |
eval ${_rgvu_rvval}="\${${_rgvu_vname}:-}";
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_get_vars_unsafe_fast() - get values of multiple variables w/ pattern
|
|
|
4243a1 |
# @_pattern: pattern to match against set output
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure, matching variable values on stdout
|
|
|
4243a1 |
# N.B.: This function is *unsafe* and impossible to implement otherwise w/o parsing set
|
|
|
4243a1 |
# output properly and may produce spurious data.
|
|
|
4243a1 |
#
|
|
|
4243a1 |
rtl_get_vars_unsafe_fast() {
|
|
|
4243a1 |
local _rgvuf_pattern="${1}";
|
|
|
e9fa07 |
|
|
|
4243a1 |
set | awk -F= '/'"${_rgvuf_pattern}"'/{print $1}' | sort;
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_kill_tree() - kill tree of processes
|
|
|
4243a1 |
# @_rpids: inout reference to list of PIDs
|
|
|
4243a1 |
# @_pid: top-level PID
|
|
|
4243a1 |
# @_signal: signal(7) to kill with
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
#
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
rtl_kill_tree() {
|
|
|
e9fa07 |
local _rkt_rpids="${1#\$}" _rkt_pid="${2}" _rkt_signal="${3:-TERM}" \
|
|
|
e9fa07 |
_rkt_pid_child="" _rkt_pids="";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
if _rkt_pids="$(pgrep -P "${_rkt_pid}")"\
|
|
|
e9fa07 |
&& [ "${_rkt_pids:+1}" = 1 ]; then
|
|
|
e9fa07 |
for _rkt_pid_child in ${_rkt_pids}; do
|
|
|
e9fa07 |
rtl_kill_tree "${_rkt_rpids}" "${_rkt_pid_child}" "${_rkt_signal}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
done;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
|
e9fa07 |
if [ "${_rkt_pid:-0}" -ne "${$}" ]\
|
|
|
e9fa07 |
&& kill "-${_rkt_signal}" "${_rkt_pid}" 2>/dev/null; then
|
|
|
e9fa07 |
rtl_lconcat "${_rkt_rpids}" "${_rkt_pid}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_run_cmdlineV() - run command line w/ field splitting applied
|
|
|
4243a1 |
# @_sep: single non-zero, possibly multi-character, separator
|
|
|
4243a1 |
# @_cmd: command name
|
|
|
4243a1 |
# @...: command arguments as positional parameters
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
# N.B.: This is required in situations where any of the command arguments 1) are passed from
|
|
|
4243a1 |
# and as a single, expanded parameter (e.g. "${MAKEFLAGS:-}") 2) the value of the parameter
|
|
|
4243a1 |
# contains multiple parameters separated by @_sep that must be passed as separate arguments
|
|
|
4243a1 |
# to @_cmd.
|
|
|
4243a1 |
#
|
|
|
4243a1 |
rtl_run_cmdlineV() {
|
|
|
4243a1 |
local _rrc_sep="${1}" _rrc_cmd="${2}" \
|
|
|
4243a1 |
_rrc_cmdline="" _rrc_rc="" IFS;
|
|
|
e9fa07 |
shift 2;
|
|
|
e9fa07 |
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
while [ ${#} -gt 0 ]; do
|
|
|
e9fa07 |
[ "${1:+1}" = 1 ] &&\
|
|
|
4243a1 |
_rrc_cmdline="${_rrc_cmdline:+${_rrc_cmdline}${_rrc_sep}}${1}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
shift;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
done;
|
|
|
4243a1 |
IFS="${_rrc_sep}"; ${_rrc_cmd} ${_rrc_cmdline}; _rrc_rc=$?;
|
|
|
4243a1 |
return ${_rrc_rc};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_set_var_from_spec() - set variable from name-value specification
|
|
|
4243a1 |
# @_rstatus: out reference to status string
|
|
|
4243a1 |
# @_arg: variable name-value specification separated by single "=" (e.g. name=value)
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
#
|
|
|
4243a1 |
rtl_set_var_from_spec() {
|
|
|
4243a1 |
local _rsvfs_rstatus="${1#\$}" _rsvfs_arg="${2}" \
|
|
|
4243a1 |
_rsvfs_rc=0 _rsvfs_vname="" _rsvfs_vval="";
|
|
|
f6f7f2 |
|
|
|
4243a1 |
_rsvfs_vname="${_rsvfs_arg%%=*}";
|
|
|
4243a1 |
_rsvfs_vval="${_rsvfs_arg#*=}";
|
|
|
f6f7f2 |
|
|
|
4243a1 |
if [ "${_rsvfs_vval:+1}" != 1 ]; then
|
|
|
4243a1 |
_rsvfs_rc=1;
|
|
|
4243a1 |
rtl_setrstatus "${_rsvfs_rstatus}" 'empty value specified for \${'"${_rsvfs_vname}"'}.';
|
|
|
f6f7f2 |
else
|
|
|
4243a1 |
rtl_set_var_unsafe "${_rsvfs_vname}" "${_rsvfs_vval}";
|
|
|
f6f7f2 |
fi;
|
|
|
4243a1 |
return "${_rsvfs_rc}";
|
|
|
f6f7f2 |
};
|
|
|
f6f7f2 |
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# rtl_set_var() - set variable from variables w/ template
|
|
|
4243a1 |
# @_vars_set_vname: list of variable names
|
|
|
4243a1 |
# @_vname_dst: variable name to set
|
|
|
4243a1 |
# @_vname_src_tmpls: variable name template (e.g. "DEFAULT PKG_")
|
|
|
4243a1 |
#
|
|
|
4243a1 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
4243a1 |
#
|
|
|
4243a1 |
rtl_set_var() {
|
|
|
e9fa07 |
local _rsv_vars_set_vname="${1}" _rsv_vname_dst="${2}" _rsv_vname_src_tmpls="${3}" \
|
|
|
e9fa07 |
_rsv_vars_set_old="" _rsv_vars_set_tmp="" _rsv_vname_src="" _rsv_vnames_src="";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
rtl_toupper2 \$_rsv_vname_src_tmpls \$_rsv_vnames_src;
|
|
|
e9fa07 |
for _rsv_vname_src in ${_rsv_vnames_src}; do
|
|
|
e9fa07 |
_rsv_vname_src="${_rsv_vname_src}_${_rsv_vname_dst}";
|
|
|
e9fa07 |
eval _rsv_vval_src="\${${_rsv_vname_src}:-}";
|
|
|
e9fa07 |
if [ "${_rsv_vval_src:+1}" = 1 ]; then
|
|
|
e9fa07 |
eval PKG_${_rsv_vname_dst}='${_rsv_vval_src}';
|
|
|
e9fa07 |
_rsv_vars_set_tmp="${_rsv_vars_set_tmp:+${_rsv_vars_set_tmp} }PKG_${_rsv_vname_dst}";
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
fi;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
done;
|
|
|
e9fa07 |
eval _rsv_vars_set_old="\${${_rsv_vars_set_vname}}";
|
|
|
e9fa07 |
rtl_set_var_unsafe "${_rsv_vars_set_vname}" "${_rsv_vars_set_old:+${_rsv_vars_set_old} }${_rsv_vars_set_tmp}";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
};
|
|
Lucio Andrés Illanes Albornoz |
aeeaa0 |
|
|
|
64844b |
# vim:filetype=sh textwidth=0
|