Blame subr.rtl/rtl_platform.subr

Lucio Andrés Illanes Albornoz aeeaa0
#
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() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _cmd="" _cmds_missing="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz aeeaa0
	for _cmd in "${@}"; do
Lucio Andrés Illanes Albornoz aeeaa0
		if ! which "${_cmd}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz aeeaa0
			_cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
	if [ -n "${_cmds_missing}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		_rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
	return "${_rc}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_clean_env() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _env_vars_except="${1}" _env_var="" _env_vars="";
Lucio Andrés Illanes Albornoz aeeaa0
	_env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
Lucio Andrés Illanes Albornoz aeeaa0
	for _env_var in ${_env_vars}; do
Lucio Andrés Illanes Albornoz aeeaa0
		if [ "${_env_var#DEFAULT_}" != "${_env_var}" ]\
Lucio Andrés Illanes Albornoz aeeaa0
		|| [ "${_env_var#PKG_}" != "${_env_var}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			_env_vars_except="$(rtl_lconcat "${_env_vars_except}" "${_env_var}")";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
	rtl_unset_vars $(rtl_lfilter "${_env_vars}" "${_env_vars_except}");
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_get_cpu_count() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _line="" _ncpus=0 _rc=0 _sname="$(uname -s 2>/dev/null)"; _status="";
Lucio Andrés Illanes Albornoz aeeaa0
	case "${_sname}" in
Lucio Andrés Illanes Albornoz aeeaa0
	Linux)	if [ ! -e "/proc/cpuinfo" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			_rc=1; _status="Error: /proc/cpuinfo non-existent.";
Lucio Andrés Illanes Albornoz aeeaa0
		else	while read -r _line; do
Lucio Andrés Illanes Albornoz aeeaa0
				if rtl_match "${_line}" "processor*:"; then
Lucio Andrés Illanes Albornoz aeeaa0
					: $((_ncpus+=1));
Lucio Andrés Illanes Albornoz aeeaa0
				fi;
Lucio Andrés Illanes Albornoz aeeaa0
			done < /proc/cpuinfo; printf "%s" "${_ncpus}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	*)	_rc=1; _status="Error: unknown platform \`${_sname}'."; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	esac; return "${_rc}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_get_var_unsafe() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _vname="";
Lucio Andrés Illanes Albornoz aeeaa0
	if [ "x${1}" = "x-u" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		shift; _vname="$(rtl_toupper "${1}")";
Lucio Andrés Illanes Albornoz aeeaa0
	else
Lucio Andrés Illanes Albornoz aeeaa0
		_vname="${1}";
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
	eval echo \${${_vname}} 2>/dev/null;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_get_vars_fast() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _pattern="${1}";
Lucio Andrés Illanes Albornoz aeeaa0
	set | awk -F= '/'"${_pattern}"'/{print $1}' | sort;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_kill_tree() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _pid="${1}" _signal="${2:-TERM}" _pid_child="" _pids="";
Lucio Andrés Illanes Albornoz aeeaa0
	if _pids="$(pgrep -P "${_pid}")"\
Lucio Andrés Illanes Albornoz aeeaa0
	&& [ -n "${_pids}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
		for _pid_child in ${_pids}; do
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_kill_tree "${_pid_child}" "${_signal}";
Lucio Andrés Illanes Albornoz aeeaa0
		done;
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
	if [ "${_pid:-0}" -ne "${$}" ]\
Lucio Andrés Illanes Albornoz aeeaa0
	&& kill "-${_signal}" "${_pid}" 2>/dev/null; then
Lucio Andrés Illanes Albornoz aeeaa0
		RTL_KILL_TREE_PIDS="$(rtl_lconcat "${RTL_KILL_TREE_PIDS}" "${_pid}")";
Lucio Andrés Illanes Albornoz aeeaa0
	fi;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_prompt() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _fmt="${1}" _choice=""; shift;
Lucio Andrés Illanes Albornoz aeeaa0
	printf "${_fmt}? (y|N) " "${@}";
Lucio Andrés Illanes Albornoz aeeaa0
	read -r _choice;
Lucio Andrés Illanes Albornoz aeeaa0
	case "${_choice}" in
Lucio Andrés Illanes Albornoz aeeaa0
	[yY])	_choice=1; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	*)	_choice=0; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	esac;
Lucio Andrés Illanes Albornoz aeeaa0
	return "${_choice}";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_rc() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _nflag="${1}" _cmd="${2}"; shift 2;
Lucio Andrés Illanes Albornoz aeeaa0
	case "${_nflag}" in
Lucio Andrés Illanes Albornoz aeeaa0
	1)	     if [ "${#}" -gt 0 ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_log_msg notice "Command line: %s %s" "${_cmd}" "${*}";
Lucio Andrés Illanes Albornoz aeeaa0
		else
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_log_msg notice "Command line: %s" "${_cmd}";
Lucio Andrés Illanes Albornoz aeeaa0
		fi; ;;
Lucio Andrés Illanes Albornoz aeeaa0
	*)	"${_cmd}" "${@}";
Lucio Andrés Illanes Albornoz aeeaa0
	esac;
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_run_cmd_unsplit() {
Lucio Andrés Illanes Albornoz aeeaa0
	local _cmd="${1}" _cmdline="" _rc="" IFS; shift;
Lucio Andrés Illanes Albornoz aeeaa0
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz aeeaa0
		[ -n "${1}" ] &&\
Lucio Andrés Illanes Albornoz aeeaa0
			_cmdline="${_cmdline:+${_cmdline}:}${1}";
Lucio Andrés Illanes Albornoz aeeaa0
		shift;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
	IFS=:; ${_cmd} ${_cmdline}; _rc=$?;
Lucio Andrés Illanes Albornoz aeeaa0
	return ${_rc};
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
rtl_set_vars() {
Lucio Andrés Illanes Albornoz aeeaa0
	local	_vars_set_vname="${1}" _vname_dst="${2}" _vname_src_tmpls="${3}"	\
Lucio Andrés Illanes Albornoz aeeaa0
		_vars_set_old="" _vars_set_tmp="" _vname_src="";
Lucio Andrés Illanes Albornoz aeeaa0
	for _vname_src in $(rtl_toupper "${_vname_src_tmpls}"); do
Lucio Andrés Illanes Albornoz aeeaa0
		_vname_src="${_vname_src}_${_vname_dst}";
Lucio Andrés Illanes Albornoz aeeaa0
		_vval_src="$(rtl_get_var_unsafe "${_vname_src}")";
Lucio Andrés Illanes Albornoz aeeaa0
		if [ -n "${_vval_src}" ]; then
Lucio Andrés Illanes Albornoz aeeaa0
			rtl_set_var_unsafe "PKG_${_vname_dst}" "${_vval_src}";
Lucio Andrés Illanes Albornoz aeeaa0
			_vars_set_tmp="$(rtl_lconcat "${_vars_set_tmp}" "PKG_${_vname_dst}")";
Lucio Andrés Illanes Albornoz aeeaa0
		fi;
Lucio Andrés Illanes Albornoz aeeaa0
	done;
Lucio Andrés Illanes Albornoz aeeaa0
	_vars_set_old="$(rtl_get_var_unsafe "${_vars_set_vname}")";
Lucio Andrés Illanes Albornoz aeeaa0
	rtl_set_var_unsafe "${_vars_set_vname}"					\
Lucio Andrés Illanes Albornoz aeeaa0
		"$(rtl_lconcat "${_vars_set_old}" "${_vars_set_tmp}")";
Lucio Andrés Illanes Albornoz aeeaa0
};
Lucio Andrés Illanes Albornoz aeeaa0
Lucio Andrés Illanes Albornoz aeeaa0
# vim:filetype=sh