Blob Blame History Raw
#
# set +o errexit -o noglob -o nounset is assumed.
#

rtl_export_vars() {
	local _unsetfl=0; [ "x${1}" = "x-u" ] && { _unsetfl=1; shift; };

	while [ "${#}" -ge 2 ]; do
		if [ "${2:+1}" = 1 ]\
		|| [ "${_unsetfl}" -eq 1 ]; then
			case "${_unsetfl}" in
			0)	rtl_set_var_unsafe "${1}" "${2}"; export "${1}"; ;;
			1)	unset "${1}"; ;;
			esac;
		fi; shift 2;
	done;
};

rtl_filter_vars() {
	local _fn="${1}" _fnfl=0 _qchar="" _var_spec="" _vars="" _vname="" IFS;
	rtl_set_IFS_nl;
	for _var_spec in $(set); do
		case "${_qchar}" in
		"\"")	if [ "${_var_spec%\"}" != "${_var_spec}" ]; then
				_qchar="";
			fi;
			if [ "${_fnfl:-0}" -eq 1 ]; then
				_vars="$(rtl_lconcat "${_vars}" "${_var_spec}" "\n")";
			fi;
			continue; ;;
		"\'")	if [ "${_var_spec%\'}" != "${_var_spec}" ]; then
				_qchar="";
			fi;
			if [ "${_fnfl:-0}" -eq 1 ]; then
				_vars="$(rtl_lconcat "${_vars}" "${_var_spec}" "\n")";
			fi;
			continue; ;;
		*)	case "${_var_spec}" in
			[!=]*=\"*\")	_qchar=""; _vname="${_var_spec%%=\"*}"; ;;
			[!=]*=\"*)	_qchar="\""; _vname="${_var_spec%%=\"*}"; ;;
			[!=]*=\'*\')	_qchar=""; _vname="${_var_spec%%=\'*}"; ;;
			[!=]*=\'*)	_qchar="\'"; _vname="${_var_spec%%=\'*}"; ;;
			[!=]*=*)	_qchar=""; _vname="${_var_spec%%=*}"; ;;
			esac; ;;
		esac;
		if "${_fn}" "${_vname}"; then
			_vars="${_vars:+${_vars}
}${_var_spec}"; _fnfl=1;
		else
			_fnfl=0;
		fi;
	done;
	printf "%s" "${_vars}";
};

rtl_head() {
	local _pattern="${1}" _s="${2}";
	while true; do
		if [ "${_s%%${_pattern}}" = "${_s}" ]; then
			break;
		else
			_s="${_s%%${_pattern}}";
		fi;
	done;
	printf "%s" "${_s}";
};

rtl_percentage() {
	local _in="${1}" _max="${2}" _perc;
	_perc="$((100 * ${_in} + ${_max} / 2))";
	_perc="$((${_perc} ? ${_perc} / ${_max} : ${_perc}))";
	printf "%d\n" "${_perc}";
};

rtl_sunset() {
	local _rs_rset="${1#\$}" _rs_kname="" IFS=" 	";
	eval set -- '${'"${_rs_rset}"'}';
	while [ "${#}" -gt 0 ]; do
		unset "${_rs_rset}${_rs_kname}"; shift;
	done; unset "${_rs_rset}";
};

# vim:filetype=sh