Blame subr.rtl/rtl_platform.subr

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
Lucio Andrés Illanes Albornoz aeeaa0
rtl_check_prereqs() {
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
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
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
Lucio Andrés Illanes Albornoz aeeaa0
rtl_get_var_unsafe() {
e9fa07
	local	_rgvu_rvname_out="${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;
e9fa07
	eval ${_rgvu_rvname_out}="\${${_rgvu_vname}:-}";
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_get_vars_fast() {
e9fa07
	local _rgvf_pattern="${1}";
e9fa07
e9fa07
	set | awk -F= '/'"${_rgvf_pattern}"'/{print $1}' | sort;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
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
Lucio Andrés Illanes Albornoz aeeaa0
rtl_prompt() {
e9fa07
	local rp_fmt="${1}" rp_choice=""; shift;
e9fa07
e9fa07
	printf "${rp_fmt}? (y|N) " "${@}";
e9fa07
	read -r rp_choice;
e9fa07
	case "${rp_choice}" in
e9fa07
	[yY])	rp_choice=1; ;;
e9fa07
	*)	rp_choice=0; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	esac;
e9fa07
	return "${rp_choice}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_rc() {
e9fa07
	local _rr_nflag="${1}" _rr_cmd="${2}";
e9fa07
	shift 2;
e9fa07
e9fa07
	case "${_rr_nflag}" in
Lucio Andrés Illanes Albornoz aeeaa0
	1)	     if [ "${#}" -gt 0 ]; then
e9fa07
			rtl_log_msg "verbose" "${MSG_rtl_platform_rc1}" "${_rr_cmd}" "${*}";
Lucio Andrés Illanes Albornoz aeeaa0
		else
e9fa07
			rtl_log_msg "verbose" "${MSG_rtl_platform_rc2}" "${_rr_cmd}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi; ;;
e9fa07
	*)	"${_rr_cmd}" "${@}"; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	esac;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_run_cmd_unsplit() {
e9fa07
	local	_rrcu_cmd="${1}"	\
e9fa07
		_rrcu_cmdline="" _rrcu_rc="" IFS;
e9fa07
	shift;
e9fa07
Lucio Andrés Illanes Albornoz aeeaa0
	while [ ${#} -gt 0 ]; do
e9fa07
		[ "${1:+1}" = 1 ] &&\
e9fa07
			_rrcu_cmdline="${_rrcu_cmdline:+${_rrcu_cmdline}:}${1}";
Lucio Andrés Illanes Albornoz aeeaa0
		shift;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
e9fa07
	IFS=:; ${_rrcu_cmd} ${_rrcu_cmdline}; _rrcu_rc=$?;
e9fa07
	return ${_rrcu_rc};
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
f6f7f2
rtl_set_var_from_cmdline() {
f6f7f2
	local	_rsvfc_rstatus="${1#\$}" _rsvfc_arg="${2}"	\
f6f7f2
		_rsvfc_rc=0 _rsvfc_vname="" _rsvfc_vval="";
f6f7f2
f6f7f2
	_rsvfc_vname="${_rsvfc_arg%%=*}";
f6f7f2
	_rsvfc_vval="${_rsvfc_arg#*=}";
f6f7f2
f6f7f2
	if [ "${_rsvfc_vval:+1}" != 1 ]; then
f6f7f2
		_rsvfc_rc=1;
f6f7f2
		rtl_setrstatus "${_rsvfc_rstatus}" 'empty value specified for \${'"${_rsvfc_vname}"'}.';
f6f7f2
	else
f6f7f2
		rtl_set_var_unsafe "${_rsvfc_vname}" "${_rsvfc_vval}";
f6f7f2
	fi;
f6f7f2
	return "${_rsvfc_rc}";
f6f7f2
};
f6f7f2
Lucio Andrés Illanes Albornoz aeeaa0
rtl_set_vars() {
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