forked from dagon666/napi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
napi_common.sh
480 lines (376 loc) · 9.44 KB
/
napi_common.sh
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
# force indendation settings
# vim: ts=4 shiftwidth=4 expandtab
#
# version for the whole bundle (napi.sh & subotage.sh)
#
declare -r g_revision="v1.3.4"
########################################################################
########################################################################
########################################################################
# Copyright (C) 2014 Tomasz Wisniewski aka
# DAGON <[email protected]>
#
# http:/dagon666
# http://pcarduino.blogspot.co.ul
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
########################################################################
########################################################################
########################################################################
################################## GLOBALS #####################################
declare -r ___VERBOSITY=0
declare -r ___LOG=1
declare -r ___FORK=2
declare -r ___CNT=3
declare -r ___LOG_OWR=4
#
# 0 - verbosity
# - 0 - be quiet (prints only errors)
# - 1 - standard level (prints errors, warnings, statuses & msg)
# - 2 - info level (prints errors, warnings, statuses, msg & info's)
# - 3 - debug level (prints errors, warnings, statuses, msg, info's and debugs)
#
# 1 - the name of the file containing the output
#
# 2 - fork id
# 3 - msg counter
# 4 - flag defining whether to overwrite the log or not
#
declare -a g_output=( 1 'none' 0 1 0 )
################################## RETVAL ######################################
# success
declare -r RET_OK=0
# function failed
declare -r RET_FAIL=255
# parameter error
declare -r RET_PARAM=254
# parameter/result will cause the script to break
declare -r RET_BREAK=253
# resource unavailable
declare -r RET_UNAV=252
# no action taken
declare -r RET_NOACT=251
################################################################################
#
# @brief count the number of lines in a file
#
count_lines() {
# it is being executed in a subshell to strip any leading white spaces
# which some of the wc versions produce
# shellcheck disable=SC2046
# shellcheck disable=SC2005
echo $(wc -l)
}
#
# @brief lowercase the input
#
lcase() {
# some old busybox implementations have problems with locales
# which renders that syntax unusable
# tr '[:upper:]' '[:lower:]'
# deliberately reverted to old syntax
# shellcheck disable=SC2021
tr '[A-Z]' '[a-z]'
}
#
# @brief get rid of the newline/carriage return
#
strip_newline() {
tr -d '\r\n'
}
#
# @brief get the extension of the input
#
get_ext() {
echo "${1##*.}"
}
#
# @brief strip the extension of the input
#
strip_ext() {
echo "${1%.*}"
}
#
# @brief get the value from strings like key=value
#
get_value() {
echo "${1##*=}"
}
#
# @brief get the key from strings like key=value
#
get_key() {
echo "${1%=*}"
}
#
# @brief search for specified key and return it's value
# @param key
# @param array
#
lookup_value() {
local i=''
local rv=$RET_FAIL
local key="$1" && shift
local tk=''
# using $* is deliberate to allow parsing either array or space delimited strings
# shellcheck disable=SC2048
for i in $*; do
tk=$(get_key "$i")
if [ "$tk" = "$key" ]; then
get_value "$i"
rv=$RET_OK
break
fi
done
return $rv
}
#
# @brief lookup index in the array for given value
# returns the index of the value and 0 on success
#
lookup_key() {
local i=''
local idx=0
local rv=$RET_FAIL
local key="$1"
shift
for i in "$@"; do
[ "$i" = "$key" ] && rv=$RET_OK && break
idx=$(( idx + 1 ))
done
echo $idx
return $rv
}
#
# @brief modify value in the array (it will be added if the key doesn't exist)
# @param key
# @param value
# @param array
#
modify_value() {
local key=$1 && shift
local value=$1 && shift
local i=0
local k=''
declare -a rv=()
# shellcheck disable=SC2048
for i in $*; do
k=$(get_key "$i")
# unfortunately old shells don't support rv+=( "$i" )
[ "$k" != "$key" ] && rv=( "${rv[@]}" "$i" )
done
rv=( "${rv[@]}" "$key=$value" )
echo ${rv[*]}
return $RET_OK
}
#
# determines number of available cpu's in the system
#
get_cores() {
local os="${1:-linux}"
if [ "$os" = "darwin" ]; then
sysctl hw.ncpu | cut -d ' ' -f 2
else
grep -i processor /proc/cpuinfo | wc -l
fi
}
#
# @brief detects running system type
#
get_system() {
uname | lcase
}
#
# @brief extracts http status from the http headers
#
get_http_status() {
# grep -o "HTTP/[\.0-9]* [0-9]*"
awk '{ m = match($0, /HTTP\/[\.0-9]* [0-9]*/); if (m) print substr($0, m, RLENGTH) }'
}
################################## STDOUT ######################################
#
# @brief produce output
#
_blit() {
printf "#%02d:%04d %s\n" ${g_output[$___FORK]} ${g_output[$___CNT]} "$*"
g_output[$___CNT]=$(( g_output[$___CNT] + 1 ))
}
#
# @brief set insane verbosity
#
_debug_insane() {
PS4='+ [${LINENO}] ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x
}
#
# @brief print a debug verbose information
#
_debug() {
local line="${1:-0}" && shift
[ "${g_output[$___VERBOSITY]}" -ge 3 ] && _blit "--- $line: $*"
return $RET_OK
}
#
# @brief print information
#
_info() {
local line=${1:-0} && shift
[ "${g_output[$___VERBOSITY]}" -ge 2 ] && _blit "-- $line: $*"
return $RET_OK
}
#
# @brief print warning
#
_warning() {
_status "WARNING" "$*"
return $RET_OK
}
#
# @brief print error message
#
_error() {
local tmp="${g_output[$___VERBOSITY]}"
g_output[$___VERBOSITY]=1
_status "ERROR" "$*" | to_stderr
g_output[$___VERBOSITY]="$tmp"
return $RET_OK
}
#
# @brief print standard message
#
_msg() {
[ "${g_output[$___VERBOSITY]}" -ge 1 ] && _blit "- $*"
return $RET_OK
}
#
# @brief print status type message
#
_status() {
[ "${g_output[$___VERBOSITY]}" -ge 1 ] && _blit "$1 -> $2"
return $RET_OK
}
#
# @brief redirect errors to standard error output
#
to_stderr() {
if [ -n "${g_output[$___LOG]}" ] && [ "${g_output[$___LOG]}" != "none" ]; then
cat
else
cat > /dev/stderr
fi
}
#
# @brief redirect stdout to logfile
#
redirect_to_logfile() {
if [ -n "${g_output[$___LOG]}" ] && [ "${g_output[$___LOG]}" != "none" ]; then
# truncate
cat /dev/null > "${g_output[$___LOG]}"
# append instead of ">" to assure that children won't mangle the output
exec 3>&1 4>&2 1>> "${g_output[$___LOG]}" 2>&1
fi
}
#
# @brief redirect output to stdout
#
redirect_to_stdout() {
# restore everything
[ -n "${g_output[$___LOG]}" ] &&
[ "${g_output[$___LOG]}" != "none" ] &&
exec 1>&3 2>&4 4>&- 3>&-
}
# EOF
################################## FLOAT CMP ###################################
float_lt() {
awk -v n1="$1" -v n2="$2" 'BEGIN{ if (n1<n2) exit 0; exit 1}'
}
float_gt() {
awk -v n1="$1" -v n2="$2" 'BEGIN{ if (n1>n2) exit 0; exit 1}'
}
float_le() {
awk -v n1="$1" -v n2="$2" 'BEGIN{ if (n1<=n2) exit 0; exit 1}'
}
float_ge() {
awk -v n1="$1" -v n2="$2" 'BEGIN{ if (n1>=n2) exit 0; exit 1}'
}
float_eq() {
awk -v n1="$1" -v n2="$2" 'BEGIN{ if (n1==n2) exit 0; exit 1}'
}
float_div() {
awk -v n1="$1" -v n2="$2" 'BEGIN { print n1/n2 }'
}
float_mul() {
awk -v n1="$1" -v n2="$2" 'BEGIN { print n1*n2 }'
}
#################################### ENV #######################################
#
# @brief checks if the tool is available in the PATH
#
verify_tool_presence() {
local tool=$(builtin type -p "$1")
local rv=$RET_UNAV
# make sure it's really there
if [ -z "$tool" ]; then
type "$1" > /dev/null 2>&1
rv=$?
else
rv=$RET_OK
fi
return $rv
}
#
# @brief check function presence
#
verify_function_presence() {
local tool=$(builtin type -t "$1")
local rv=$RET_UNAV
local status=0
# make sure it's really there
if [ -z "$tool" ]; then
type "$1" > /dev/null 2>&1;
status=$?
[ "$status" -ne $RET_OK ] && tool='empty'
fi
# check the output
[ "$tool" = "function" ] && rv=$RET_OK
return $rv
}
################################## DB ##########################################
# that was an experiment which I decided to drop after all.
# those functions provide a mechanism to generate consistently named global vars
# i.e. _db_set "abc" 1 will create glob. var ___g_db___abc=1
# left as a reference - do not use it
## #
## # @global prefix for the global variables generation
## #
## g_GlobalPrefix="___g_db___"
##
##
## #
## # @brief get variable from the db
## #
## _db_get() {
## eval "echo \$${g_GlobalPrefix}_$1"
## }
##
##
## #
## # @brief set variable in the db
## #
## _db_set() {
## eval "${g_GlobalPrefix}_${1/./_}=\$2"
## }
########################################################################