blob: 2c23cfe828cf794f489d269308ca46b786233c85 (
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#
# set +o errexit -o noglob -o nounset is assumed.
#
rtl_check_path_vars() {
local _vnames="${1}" _rc=0 _vname="" _vname_val=""; _status="";
for _vname in ${_vnames}; do
_vname_val="$(rtl_get_var_unsafe "${_vname}")";
if [ -z "${_vname_val}" ]; then
_rc=1; _status="Error: variable \`${_vname}' is empty or unset."; break;
elif [ "${_vname_val#* *}" != "${_vname_val}" ]; then
_rc=2; _status="Error: variable \`${_vname}' contains one or more whitespace characters."; break;
fi;
done;
return "${_rc}";
};
rtl_check_prereqs() {
local _cmd="" _cmds_missing="" _rc=0; _status="";
for _cmd in "${@}"; do
if ! which "${_cmd}" >/dev/null 2>&1; then
_cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
fi;
done;
if [ -n "${_cmds_missing}" ]; then
_rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
fi;
return "${_rc}";
};
rtl_clean_env() {
local _env_vars_except="${1}" _env_var="" _env_vars="";
_env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
for _env_var in ${_env_vars}; do
if [ "${_env_var#DEFAULT_}" != "${_env_var}" ]\
|| [ "${_env_var#PKG_}" != "${_env_var}" ]; then
_env_vars_except="$(rtl_lconcat "${_env_vars_except}" "${_env_var}")";
fi;
done;
rtl_unset_vars $(rtl_lfilter "${_env_vars}" "${_env_vars_except}");
};
rtl_dirname() {
local _dname="${1%/*}";
while rtl_matchr "${_dname}" "*/"; do
_dname="${_dname%/}";
done; printf "%s" "${_dname:-/}";
};
rtl_exists_any() {
local _subdir="${1}"; shift;
while [ "${#}" -gt 0 ]; do
if [ -e "${_subdir}/${1}" ]; then
return 0;
else
shift;
fi;
done; return 1;
};
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="
"; 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_flock_acquire() {
local _fd="${1}" _conflict_exit_code="${2:-622}" _wait="${3:-3600}"
while true; do
if flock -E "${_conflict_exit_code}" -w "${_wait}" "${_fd}"; then
break;
elif [ "${?}" -eq "${_conflict_exit_code}" ]; then
continue;
else
return "${?}";
fi;
done;
};
rtl_get_cpu_count() {
local _line="" _ncpus=0 _rc=0 _sname="$(uname -s 2>/dev/null)"; _status="";
case "${_sname}" in
Linux) if [ ! -e "/proc/cpuinfo" ]; then
_rc=1; _status="Error: /proc/cpuinfo non-existent.";
else while read -r _line; do
if rtl_match "${_line}" "processor*:"; then
: $((_ncpus+=1));
fi;
done < /proc/cpuinfo; printf "%s" "${_ncpus}";
fi; ;;
*) _rc=1; _status="Error: unknown platform \`${_sname}'."; ;;
esac; return "${_rc}";
};
rtl_get_var_unsafe() {
local _vname="";
if [ "x${1}" = "x-u" ]; then
shift; _vname="$(rtl_toupper "${1}")";
else
_vname="${1}";
fi;
eval echo \${${_vname}} 2>/dev/null;
};
rtl_get_vars_fast() {
local _pattern="${1}";
set | awk -F= '/'"${_pattern}"'/{print $1}' | sort;
};
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_is_newer() {
local _new_fname="${1}" _old_fname="${2}";
if [ -n "$(find -name "${_new_fname}" -newer "${_old_fname}" 2>/dev/null)" ]; then
return 0;
else
return 1;
fi;
};
rtl_kill_tree() {
local _pid="${1}" _signal="${2:-TERM}" _pid_child="" _pids="";
if _pids="$(pgrep -P "${_pid}")"\
&& [ -n "${_pids}" ]; then
for _pid_child in ${_pids}; do
rtl_kill_tree "${_pid_child}" "${_signal}";
done;
fi;
if [ "${_pid:-0}" -ne "${$}" ]\
&& kill "-${_signal}" "${_pid}" 2>/dev/null; then
RTL_KILL_TREE_PIDS="$(rtl_lconcat "${RTL_KILL_TREE_PIDS}" "${_pid}")";
fi;
};
rtl_prompt() {
local _fmt="${1}" _choice=""; shift;
printf "${_fmt}? (y|N) " "${@}";
read -r _choice;
case "${_choice}" in
[yY]) _choice=1; ;;
*) _choice=0; ;;
esac;
return "${_choice}";
};
rtl_rc() {
local _nflag="${1}" _cmd="${2}"; shift 2;
case "${_nflag}" in
1) if [ "${#}" -gt 0 ]; then
rtl_log_msg notice "Command line: %s %s" "${_cmd}" "${*}";
else
rtl_log_msg notice "Command line: %s" "${_cmd}";
fi; ;;
*) "${_cmd}" "${@}";
esac;
};
rtl_run_cmd_unsplit() {
local _cmd="${1}" _cmdline="" _rc="" IFS; shift;
while [ ${#} -gt 0 ]; do
[ -n "${1}" ] &&\
_cmdline="${_cmdline:+${_cmdline}:}${1}";
shift;
done;
IFS=:; ${_cmd} ${_cmdline}; _rc=$?;
return ${_rc};
};
rtl_set_vars() {
local _vars_set_vname="${1}" _vname_dst="${2}" _vname_src_tmpls="${3}" \
_vars_set_old="" _vars_set_tmp="" _vname_src="";
for _vname_src in $(rtl_toupper "${_vname_src_tmpls}"); do
_vname_src="${_vname_src}_${_vname_dst}";
_vval_src="$(rtl_get_var_unsafe "${_vname_src}")";
if [ -n "${_vval_src}" ]; then
rtl_set_var_unsafe "PKG_${_vname_dst}" "${_vval_src}";
_vars_set_tmp="$(rtl_lconcat "${_vars_set_tmp}" "PKG_${_vname_dst}")";
fi;
done;
_vars_set_old="$(rtl_get_var_unsafe "${_vars_set_vname}")";
rtl_set_var_unsafe "${_vars_set_vname}" \
"$(rtl_lconcat "${_vars_set_old}" "${_vars_set_tmp}")";
};
# vim:filetype=sh
|