midipix / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone

Blame subr/rtl_complex.subr

Lucio Andrés Illanes Albornoz c3bcd7
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz c3bcd7
#
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c6d6e0
rtl_check_path_vars() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _vnames="${1}" _rc=0 _vname="" _vname_val=""; _status="";
Lucio Andrés Illanes Albornoz c3bcd7
	for _vname in ${_vnames}; do
Lucio Andrés Illanes Albornoz c6d6e0
		_vname_val="$(rtl_get_var_unsafe "${_vname}")";
Lucio Andrés Illanes Albornoz c3bcd7
		if [ -z "${_vname_val}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
			_rc=1; _status="Error: variable \`${_vname}' is empty or unset."; break;
Lucio Andrés Illanes Albornoz c3bcd7
		elif [ "${_vname_val#* *}" != "${_vname_val}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
			_rc=2; _status="Error: variable \`${_vname}' contains one or more whitespace characters."; break;
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz c3bcd7
	done;
Lucio Andrés Illanes Albornoz c3bcd7
	return "${_rc}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz 11ef38
rtl_check_prereqs() {
Lucio Andrés Illanes Albornoz 11ef38
	local _cmd="" _cmds_missing="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 11ef38
	for _cmd in "${@}"; do
Lucio Andrés Illanes Albornoz 11ef38
		if ! which "${_cmd}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz 11ef38
			_cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
Lucio Andrés Illanes Albornoz 11ef38
		fi;
Lucio Andrés Illanes Albornoz 11ef38
	done;
Lucio Andrés Illanes Albornoz 11ef38
	if [ -n "${_cmds_missing}" ]; then
Lucio Andrés Illanes Albornoz 11ef38
		_rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
Lucio Andrés Illanes Albornoz 11ef38
	fi;
Lucio Andrés Illanes Albornoz 11ef38
	return "${_rc}";
Lucio Andrés Illanes Albornoz 11ef38
};
Lucio Andrés Illanes Albornoz 11ef38
Lucio Andrés Illanes Albornoz c6d6e0
rtl_clean_env() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _env_vars_except="${1}" _env_var="" _env_vars="";
Lucio Andrés Illanes Albornoz c3bcd7
	_env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
Lucio Andrés Illanes Albornoz c3bcd7
	for _env_var in ${_env_vars}; do
Lucio Andrés Illanes Albornoz c3bcd7
		if [ "${_env_var#DEFAULT_}" != "${_env_var}" ]\
Lucio Andrés Illanes Albornoz c3bcd7
		|| [ "${_env_var#PKG_}" != "${_env_var}" ]; then
Lucio Andrés Illanes Albornoz bf9edf
			_env_vars_except="$(rtl_lconcat "${_env_vars_except}" "${_env_var}")";
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz c3bcd7
	done;
Lucio Andrés Illanes Albornoz c6d6e0
	rtl_unset_vars $(rtl_lfilter "${_env_vars}" "${_env_vars_except}");
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz b6a9a1
rtl_dirname() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _dname="${1%/*}";
Lucio Andrés Illanes Albornoz b6a9a1
	while rtl_matchr "${_dname}" "*/"; do
Lucio Andrés Illanes Albornoz b6a9a1
		_dname="${_dname%/}";
Lucio Andrés Illanes Albornoz b6a9a1
	done; printf "%s" "${_dname:-/}";
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_exists_any() {
Lucio Andrés Illanes Albornoz 2b85d0
	local _subdir="${1}"; shift;
Lucio Andrés Illanes Albornoz 2b85d0
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz 2b85d0
		if [ -e "${_subdir}/${1}" ]; then
Lucio Andrés Illanes Albornoz 2b85d0
			return 0;
Lucio Andrés Illanes Albornoz 2b85d0
		else
Lucio Andrés Illanes Albornoz 2b85d0
			shift;
Lucio Andrés Illanes Albornoz 2b85d0
		fi;
Lucio Andrés Illanes Albornoz 2b85d0
	done; return 1;
Lucio Andrés Illanes Albornoz ae3ff0
};
Lucio Andrés Illanes Albornoz ae3ff0
Lucio Andrés Illanes Albornoz c6d6e0
rtl_export_vars() {
Lucio Andrés Illanes Albornoz 2b85d0
	local _unsetfl=0; [ "x${1}" = "x-u" ] && { _unsetfl=1; shift; };
Lucio Andrés Illanes Albornoz 2b85d0
	while [ "${#}" -ge 2 ]; do
Lucio Andrés Illanes Albornoz 2b85d0
		if [ -n "${2}" ]; then
Lucio Andrés Illanes Albornoz 2b85d0
			if [ "${_unsetfl:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_set_var_unsafe "${1}" "${2}"; export "${1}";
Lucio Andrés Illanes Albornoz 2b85d0
			else
Lucio Andrés Illanes Albornoz 2b85d0
				unset "${1}";
Lucio Andrés Illanes Albornoz c3bcd7
			fi;
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz 2b85d0
		shift 2;
Lucio Andrés Illanes Albornoz 2b85d0
	done;
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz 8a27f9
rtl_filter_vars() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _fn="${1}" _fnfl=0 _qchar="" _var_spec="" _vars="" _vname="" IFS="
Lucio Andrés Illanes Albornoz 8a27f9
";	for _var_spec in $(set); do
Lucio Andrés Illanes Albornoz 8a27f9
		case "${_qchar}" in
Lucio Andrés Illanes Albornoz 8a27f9
		"\"")	if [ "${_var_spec%\"}" != "${_var_spec}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_qchar="";
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
			if [ "${_fnfl:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_vars="$(rtl_lconcat "${_vars}" "${_var_spec}" "\n")";
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
			continue; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		"\'")	if [ "${_var_spec%\'}" != "${_var_spec}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_qchar="";
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
			if [ "${_fnfl:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_vars="$(rtl_lconcat "${_vars}" "${_var_spec}" "\n")";
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
			continue; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		*)	case "${_var_spec}" in
Lucio Andrés Illanes Albornoz 8a27f9
			[^=]*=\"*\")	_qchar=""; _vname="${_var_spec%%=\"*}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			[^=]*=\"*)	_qchar="\""; _vname="${_var_spec%%=\"*}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			[^=]*=\'*\')	_qchar=""; _vname="${_var_spec%%=\'*}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			[^=]*=\'*)	_qchar="\'"; _vname="${_var_spec%%=\'*}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			[^=]*=*)	_qchar=""; _vname="${_var_spec%%=*}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			esac; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac;
Lucio Andrés Illanes Albornoz 8a27f9
		if "${_fn}" "${_vname}"; then
Lucio Andrés Illanes Albornoz b6a9a1
			_vars="${_vars:+${_vars}
Lucio Andrés Illanes Albornoz b6a9a1
}${_var_spec}"; _fnfl=1;
Lucio Andrés Illanes Albornoz 8a27f9
		else
Lucio Andrés Illanes Albornoz 8a27f9
			_fnfl=0;
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	done;
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${_vars}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 847bee
rtl_flock_acquire() {
Lucio Andrés Illanes Albornoz 847bee
	local _fd="${1}" _conflict_exit_code="${2:-622}" _wait="${3:-3600}"
Lucio Andrés Illanes Albornoz 847bee
	while true; do
Lucio Andrés Illanes Albornoz 847bee
		if flock -E "${_conflict_exit_code}" -w "${_wait}" "${_fd}"; then
Lucio Andrés Illanes Albornoz 847bee
			break;
Lucio Andrés Illanes Albornoz 847bee
		elif [ "${?}" -eq "${_conflict_exit_code}" ]; then
Lucio Andrés Illanes Albornoz 847bee
			continue;
Lucio Andrés Illanes Albornoz 847bee
		else
Lucio Andrés Illanes Albornoz 847bee
			return "${?}";
Lucio Andrés Illanes Albornoz 847bee
		fi;
Lucio Andrés Illanes Albornoz 847bee
	done;
Lucio Andrés Illanes Albornoz 847bee
};
Lucio Andrés Illanes Albornoz 847bee
Lucio Andrés Illanes Albornoz 0d1c4f
rtl_get_cpu_count() {
Lucio Andrés Illanes Albornoz 0d1c4f
	local _line="" _ncpus=0 _rc=0 _sname="$(uname -s 2>/dev/null)"; _status="";
Lucio Andrés Illanes Albornoz 0d1c4f
	case "${_sname}" in
Lucio Andrés Illanes Albornoz 0d1c4f
	Linux)	if [ ! -e "/proc/cpuinfo" ]; then
Lucio Andrés Illanes Albornoz 0d1c4f
			_rc=1; _status="Error: /proc/cpuinfo non-existent.";
Lucio Andrés Illanes Albornoz 0d1c4f
		else	while read -r _line; do
Lucio Andrés Illanes Albornoz 0d1c4f
				if rtl_match "${_line}" "processor*:"; then
Lucio Andrés Illanes Albornoz 0d1c4f
					: $((_ncpus+=1));
Lucio Andrés Illanes Albornoz 0d1c4f
				fi;
Lucio Andrés Illanes Albornoz b6a9a1
			done < /proc/cpuinfo; printf "%s" "${_ncpus}";
Lucio Andrés Illanes Albornoz 0d1c4f
		fi; ;;
Lucio Andrés Illanes Albornoz 0d1c4f
	*)	_rc=1; _status="Error: unknown platform \`${_sname}'."; ;;
Lucio Andrés Illanes Albornoz 0d1c4f
	esac; return "${_rc}";
Lucio Andrés Illanes Albornoz 0d1c4f
};
Lucio Andrés Illanes Albornoz 0d1c4f
Lucio Andrés Illanes Albornoz c6d6e0
rtl_get_var_unsafe() {
Lucio Andrés Illanes Albornoz 2b85d0
	local _vname="";
Lucio Andrés Illanes Albornoz 2b85d0
	if [ "x${1}" = "x-u" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
		shift; _vname="$(rtl_toupper "${1}")";
Lucio Andrés Illanes Albornoz 2b85d0
	else
Lucio Andrés Illanes Albornoz 2b85d0
		_vname="${1}";
Lucio Andrés Illanes Albornoz 2b85d0
	fi;
Lucio Andrés Illanes Albornoz e1d469
	eval echo \${${_vname}} 2>/dev/null;
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz b6a9a1
rtl_get_vars_fast() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _pattern="${1}";
Lucio Andrés Illanes Albornoz b6a9a1
	set | awk -F= '/'"${_pattern}"'/{print $1}' | sort;
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_head() {
Lucio Andrés Illanes Albornoz 2b85d0
	local _pattern="${1}" _s="${2}";
Lucio Andrés Illanes Albornoz 2b85d0
	while true; do
Lucio Andrés Illanes Albornoz 2b85d0
		if [ "${_s%%${_pattern}}" = "${_s}" ]; then
Lucio Andrés Illanes Albornoz 2b85d0
			break;
Lucio Andrés Illanes Albornoz 2b85d0
		else
Lucio Andrés Illanes Albornoz 2b85d0
			_s="${_s%%${_pattern}}";
Lucio Andrés Illanes Albornoz 2b85d0
		fi;
Lucio Andrés Illanes Albornoz 2b85d0
	done;
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${_s}";
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz 0d1c4f
rtl_is_newer() {
Lucio Andrés Illanes Albornoz b03d3c
	local _new_fname="${1}" _old_fname="${2}" _new_ts="" _old_ts="";
Lucio Andrés Illanes Albornoz b03d3c
	if ! [ -e "${_old_fname}" ]; then
Lucio Andrés Illanes Albornoz 0d1c4f
		return 0;
Lucio Andrés Illanes Albornoz b03d3c
	else	_new_ts="$(stat -c %Y "${_new_fname}" 2>&1)";
Lucio Andrés Illanes Albornoz b03d3c
		_old_ts="$(stat -c %Y "${_old_fname}" 2>&1)";
Lucio Andrés Illanes Albornoz b03d3c
		if [ "${_new_ts:-0}" -gt "${_old_ts:-0}" ]; then
Lucio Andrés Illanes Albornoz b03d3c
			return 0;
Lucio Andrés Illanes Albornoz b03d3c
		else
Lucio Andrés Illanes Albornoz b03d3c
			return 1;
Lucio Andrés Illanes Albornoz b03d3c
		fi;
Lucio Andrés Illanes Albornoz 0d1c4f
	fi;
Lucio Andrés Illanes Albornoz 0d1c4f
};
Lucio Andrés Illanes Albornoz 0d1c4f
Lucio Andrés Illanes Albornoz c6d6e0
rtl_kill_tree() {
Lucio Andrés Illanes Albornoz fa4a80
	local _pid="${1}" _signal="${2:-TERM}" _pid_child="" _pids="";
Lucio Andrés Illanes Albornoz fa4a80
	if _pids="$(pgrep -P "${_pid}")"\
Lucio Andrés Illanes Albornoz fa4a80
	&& [ -n "${_pids}" ]; then
Lucio Andrés Illanes Albornoz fa4a80
		for _pid_child in ${_pids}; do
Lucio Andrés Illanes Albornoz fa4a80
			rtl_kill_tree "${_pid_child}" "${_signal}";
Lucio Andrés Illanes Albornoz c3bcd7
		done;
Lucio Andrés Illanes Albornoz fa4a80
	fi;
Lucio Andrés Illanes Albornoz fa4a80
	if [ "${_pid:-0}" -ne "${$}" ]\
Lucio Andrés Illanes Albornoz fa4a80
	&& kill "-${_signal}" "${_pid}" 2>/dev/null; then
Lucio Andrés Illanes Albornoz fa4a80
		RTL_KILL_TREE_PIDS="$(rtl_lconcat "${RTL_KILL_TREE_PIDS}" "${_pid}")";
Lucio Andrés Illanes Albornoz fa4a80
	fi;
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz 11ef38
rtl_prompt() {
Lucio Andrés Illanes Albornoz 11ef38
	local _fmt="${1}" _choice=""; shift;
Lucio Andrés Illanes Albornoz 11ef38
	printf "${_fmt}? (y|N) " "${@}";
Lucio Andrés Illanes Albornoz 11ef38
	read -r _choice;
Lucio Andrés Illanes Albornoz 11ef38
	case "${_choice}" in
Lucio Andrés Illanes Albornoz 11ef38
	[yY])	_choice=1; ;;
Lucio Andrés Illanes Albornoz 11ef38
	*)	_choice=0; ;;
Lucio Andrés Illanes Albornoz 11ef38
	esac;
Lucio Andrés Illanes Albornoz 11ef38
	return "${_choice}";
Lucio Andrés Illanes Albornoz 11ef38
};
Lucio Andrés Illanes Albornoz 11ef38
Lucio Andrés Illanes Albornoz 11ef38
rtl_rc() {
Lucio Andrés Illanes Albornoz 11ef38
	local _nflag="${1}" _cmd="${2}"; shift 2;
Lucio Andrés Illanes Albornoz 11ef38
	case "${_nflag}" in
Lucio Andrés Illanes Albornoz 11ef38
	1)	     if [ "${#}" -gt 0 ]; then
Lucio Andrés Illanes Albornoz 11ef38
			rtl_log_msg notice "Command line: %s %s" "${_cmd}" "${*}";
Lucio Andrés Illanes Albornoz 11ef38
		else
Lucio Andrés Illanes Albornoz 11ef38
			rtl_log_msg notice "Command line: %s" "${_cmd}";
Lucio Andrés Illanes Albornoz 11ef38
		fi; ;;
Lucio Andrés Illanes Albornoz 11ef38
	*)	"${_cmd}" "${@}";
Lucio Andrés Illanes Albornoz 11ef38
	esac;
Lucio Andrés Illanes Albornoz 11ef38
};
Lucio Andrés Illanes Albornoz 11ef38
Lucio Andrés Illanes Albornoz c6d6e0
rtl_run_cmd_unsplit() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _cmd="${1}" _cmdline="" _rc="" IFS; shift;
Lucio Andrés Illanes Albornoz c3bcd7
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz c3bcd7
		[ -n "${1}" ] &&\
Lucio Andrés Illanes Albornoz c3bcd7
			_cmdline="${_cmdline:+${_cmdline}:}${1}";
Lucio Andrés Illanes Albornoz c3bcd7
		shift;
Lucio Andrés Illanes Albornoz c3bcd7
	done;
Lucio Andrés Illanes Albornoz c3bcd7
	IFS=:; ${_cmd} ${_cmdline}; _rc=$?;
Lucio Andrés Illanes Albornoz c3bcd7
	return ${_rc};
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c6d6e0
rtl_set_vars() {
Lucio Andrés Illanes Albornoz 2b85d0
	local	_vars_set_vname="${1}" _vname_dst="${2}" _vname_src_tmpls="${3}"	\
Lucio Andrés Illanes Albornoz 2b85d0
		_vars_set_old="" _vars_set_tmp="" _vname_src="";
Lucio Andrés Illanes Albornoz c6d6e0
	for _vname_src in $(rtl_toupper "${_vname_src_tmpls}"); do
Lucio Andrés Illanes Albornoz c3bcd7
		_vname_src="${_vname_src}_${_vname_dst}";
Lucio Andrés Illanes Albornoz c6d6e0
		_vval_src="$(rtl_get_var_unsafe "${_vname_src}")";
Lucio Andrés Illanes Albornoz c3bcd7
		if [ -n "${_vval_src}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_set_var_unsafe "PKG_${_vname_dst}" "${_vval_src}";
Lucio Andrés Illanes Albornoz bf9edf
			_vars_set_tmp="$(rtl_lconcat "${_vars_set_tmp}" "PKG_${_vname_dst}")";
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz c3bcd7
	done;
Lucio Andrés Illanes Albornoz c6d6e0
	_vars_set_old="$(rtl_get_var_unsafe "${_vars_set_vname}")";
Lucio Andrés Illanes Albornoz c6d6e0
	rtl_set_var_unsafe "${_vars_set_vname}"					\
Lucio Andrés Illanes Albornoz bf9edf
		"$(rtl_lconcat "${_vars_set_old}" "${_vars_set_tmp}")";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
# vim:filetype=sh