summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_complex.subr
blob: 8866484f73805c22613fa4e24fff58c99fd20715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
# 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