Lucio Andrés Illanes Albornoz c3bcd7
#
Lucio Andrés Illanes Albornoz c3bcd7
# set -o noglob is assumed.
Lucio Andrés Illanes Albornoz c3bcd7
#
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
ex_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 c3bcd7
		_vname_val="$(ex_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 c3bcd7
ex_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 c3bcd7
			_env_vars_except="${_env_vars_except:+${_env_vars_except} }${_env_var}";
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz c3bcd7
	done;
Lucio Andrés Illanes Albornoz c3bcd7
	ex_rtl_unset_vars $(ex_rtl_lfilter "${_env_vars}" "${_env_vars_except}");
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz ae3ff0
ex_rtl_kill_tree() {
Lucio Andrés Illanes Albornoz ae3ff0
	local _pid="${1}" _signal="TERM" _pid_child="" _pid_top="";
Lucio Andrés Illanes Albornoz ae3ff0
	for _pid_top in $(pgrep -P "${_pid}"); do
Lucio Andrés Illanes Albornoz ae3ff0
		for _pid_child in $(pgrep -P "${_pid_top}" 2>/dev/null); do
Lucio Andrés Illanes Albornoz 16bae7
			_pids_killed="${_pids_killed:+${_pids_killed} }${_pid_child}"; kill "-${_signal}" "${_pid_child}" 2>/dev/null;
Lucio Andrés Illanes Albornoz ae3ff0
		done;
Lucio Andrés Illanes Albornoz ae3ff0
		_pids_killed="${_pids_killed:+${_pids_killed} }${_pid_top}"; kill "-${_signal}" "${_pid_top}" 2>/dev/null;
Lucio Andrés Illanes Albornoz ae3ff0
	done;
Lucio Andrés Illanes Albornoz ae3ff0
};
Lucio Andrés Illanes Albornoz ae3ff0
Lucio Andrés Illanes Albornoz c3bcd7
ex_rtl_lfilter() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _list="${1}" _filter="${2}" _lnew="" _litem="" _litem_filter="" _filterfl="";
Lucio Andrés Illanes Albornoz c3bcd7
	if [ -z "${_filter}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
		echo "${_list}"; return 0;
Lucio Andrés Illanes Albornoz c3bcd7
	else for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz c3bcd7
		_filterfl=0;
Lucio Andrés Illanes Albornoz c3bcd7
		for _litem_filter in ${_filter}; do
Lucio Andrés Illanes Albornoz c3bcd7
			if [ "${_litem_filter}" = "${_litem}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
				_filterfl=1; break;
Lucio Andrés Illanes Albornoz c3bcd7
			fi;
Lucio Andrés Illanes Albornoz c3bcd7
		done;
Lucio Andrés Illanes Albornoz c3bcd7
		if [ "${_filterfl:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz c3bcd7
			_lnew="${_lnew:+${_lnew} }${_litem}";
Lucio Andrés Illanes Albornoz c3bcd7
		fi;
Lucio Andrés Illanes Albornoz c3bcd7
	done; fi;
Lucio Andrés Illanes Albornoz c3bcd7
	echo "${_lnew}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
ex_rtl_lmatch() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _cmp="${3}" IFS="${2}"; set -- ${1};
Lucio Andrés Illanes Albornoz c3bcd7
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz c3bcd7
		if [ "${1}" = "${_cmp}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
			return 0;
Lucio Andrés Illanes Albornoz c3bcd7
		fi; shift;
Lucio Andrés Illanes Albornoz c3bcd7
	done; return 1;
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
ex_rtl_lsearch() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _list="${1}" _filter="${2}" _lnew="" _litem="" _litem_filter="";
Lucio Andrés Illanes Albornoz c3bcd7
	if [ -z "${_filter}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
		echo "${_list}"; return 0;
Lucio Andrés Illanes Albornoz c3bcd7
	else for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz c3bcd7
		for _litem_filter in ${_filter}; do
Lucio Andrés Illanes Albornoz c3bcd7
			if [ "${_litem_filter}" = "${_litem}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
				_lnew="${_lnew:+${_lnew} }${_litem}";
Lucio Andrés Illanes Albornoz c3bcd7
				break;
Lucio Andrés Illanes Albornoz c3bcd7
			fi;
Lucio Andrés Illanes Albornoz c3bcd7
		done;
Lucio Andrés Illanes Albornoz c3bcd7
	done; fi;
Lucio Andrés Illanes Albornoz c3bcd7
	echo "${_lnew}";
Lucio Andrés Illanes Albornoz c3bcd7
};
Lucio Andrés Illanes Albornoz c3bcd7
Lucio Andrés Illanes Albornoz c3bcd7
ex_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 c3bcd7
ex_rtl_set_vars() {
Lucio Andrés Illanes Albornoz c3bcd7
	local _vname_dst="${1}" _vname_src_tmpls="${2}"			\
Lucio Andrés Illanes Albornoz c3bcd7
		_vars_set_vname="${3}" _vname_src _vars_set_tmp=""	\
Lucio Andrés Illanes Albornoz c3bcd7
		_vars_set_old="";
Lucio Andrés Illanes Albornoz c3bcd7
	for _vname_src in ${_vname_src_tmpls}; do
Lucio Andrés Illanes Albornoz c3bcd7
		_vname_src="${_vname_src}_${_vname_dst}";
Lucio Andrés Illanes Albornoz c3bcd7
		_vval_src="$(ex_rtl_get_var_unsafe "${_vname_src}")";
Lucio Andrés Illanes Albornoz c3bcd7
		if [ -n "${_vval_src}" ]; then
Lucio Andrés Illanes Albornoz c3bcd7
			ex_rtl_set_var_unsafe "PKG_${_vname_dst}" "${_vval_src}";
Lucio Andrés Illanes Albornoz c3bcd7
			_vars_set_tmp="${_vars_set_tmp:+${_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 c3bcd7
	_vars_set_old="$(ex_rtl_get_var_unsafe "${_vars_set_vname}")";
Lucio Andrés Illanes Albornoz c3bcd7
	ex_rtl_set_var_unsafe "${_vars_set_vname}"			\
Lucio Andrés Illanes Albornoz c3bcd7
		"${_vars_set_old:+${_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