#
# 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 [ -n "${2}" ]; then
if [ "${_unsetfl:-0}" -eq 0 ]; then
rtl_set_var_unsafe "${1}" "${2}"; export "${1}";
else
unset "${1}";
fi;
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}";
};
# vim:filetype=sh