-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip-array_main_functions
4443 lines (4219 loc) · 149 KB
/
ip-array_main_functions
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ------------------------------------------------------------------------- #
#*# ###### #
# # # # # ##### ##### ## # #
# # # # # # # # # # # # #
# ###### ##### # # # # # # # # #
# # ####### ##### ##### ###### #
# # # # # # # # # # #
### # # # # # # # # # #
# ------------------------------------------------------------------------- #
#
# Copyright (C) 2005-2018 Mart Frauenlob aka AllKind
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ------------------------------------------------------------------------- #
#
# IP-ARRAY MAIN FUNCTIONS
#
# ------------------------------------------------------------------------- #
cl_get_variants() { # create unique variations of ansi-code names
local -i i_lengl=${#1} i_lengs=${#2}
while ((i_lengl >= i_lengs)); do
printf "%s\n" ${1:0:i_lengl--}
done
} # -------------------------------------------------------------------------
if ((BASH_VERSINFO[0] < 4)); then
# retrieve codes from names (bash prior to v4)
cl_get_fg_code() {
local -i i
for i in ${!ARR_FG_COLORS[@]}; do
[[ ${ARR_FG_COLORS[i]} = $1 ]] && printf "%d\n" $i && return
done
}
cl_get_bg_code() {
local -i i
for i in ${!ARR_BG_COLORS[@]}; do
[[ ${ARR_BG_COLORS[i]} = $1 ]] && printf "%d\n" $i && return
done
}
cl_get_ansi_code() {
local -i i
for i in ${!ARR_ANSI_MODES[@]}; do
[[ ${ARR_ANSI_MODES[i]} = $1 ]] && printf "%d\n" $i && return
done
}
# retrieve codes from names (bash prior to v4) - incl. variants
cl_get_fg_code_all() {
local x
local -i i
for i in ${!ARR_FGNAMES_ALL[@]}; do
for x in ${ARR_FGNAMES_ALL[i]}; do
[[ $x = $1 ]] && printf "%d\n" $i && return
done
done
}
cl_get_bg_code_all() {
local x
local -i i
for i in ${!ARR_BGNAMES_ALL[@]}; do
for x in ${ARR_BGNAMES_ALL[i]}; do
[[ $x = $1 ]] && printf "%d\n" $i && return
done
done
}
cl_get_ansi_code_all() {
local x
local -i i
for i in ${!ARR_MODNAMES_ALL[@]}; do
for x in ${ARR_MODNAMES_ALL[i]}; do
[[ $x = $1 ]] && printf "%d\n" $i && return
done
done
}
cl_set_variants() { # set variations of code names
local name n_long n_short variant
local -i fg_code bg_code
for name in ${!ARR_COLNAMES_MAP[@]}; do
set -- ${ARR_COLNAMES_MAP[$name]}
n_long=$1 n_short=$2 fg_code=$(cl_get_fg_code $1) bg_code=$(cl_get_bg_code $1)
for variant in $(cl_get_variants $n_long $n_short); do
ARR_FGNAMES_ALL[fg_code]="${ARR_FGNAMES_ALL[fg_code]} $variant"
ARR_BGNAMES_ALL[bg_code]="${ARR_BGNAMES_ALL[bg_code]} $variant"
done
done
for name in ${!ARR_MODNAMES_MAP[@]}; do
set -- ${ARR_MODNAMES_MAP[$name]}
n_long=$1 n_short=$2 fg_code=$(cl_get_ansi_code $1)
for variant in $(cl_get_variants $n_long $n_short); do
ARR_MODNAMES_ALL[fg_code]="${ARR_MODNAMES_ALL[fg_code]} $variant"
done
done
readonly ARR_FGNAMES_ALL ARR_BGNAMES_ALL ARR_MODNAMES_ALL
}
cl_save_variants() { # print (for saving) the code name variation arrays
declare -p ARR_FGNAMES_ALL ARR_BGNAMES_ALL ARR_MODNAMES_ALL
}
cl_ismode() { # check if name is a known mode
local x
local -i i
for i in ${!ARR_MODNAMES_ALL[@]}; do
for x in ${ARR_MODNAMES_ALL[i]}; do
[[ $x = $1 ]] && return
done
done
return 1
}
cl_iscolor() { # check if name is a known color
local x
local -i i
for i in ${!ARR_FGNAMES_ALL[@]}; do
for x in ${ARR_FGNAMES_ALL[i]}; do
[[ $x = $1 ]] && return
done
done
return 1
}
else
cl_set_variants() { # set variations of code names
local name n_long n_short variant
for name in ${!ARR_COLNAMES_MAP[@]}; do
set -- ${ARR_COLNAMES_MAP[$name]}
n_long=$1 n_short=$2
for variant in $(cl_get_variants $n_long $n_short); do
AAR_FGNAMES_ALL[$variant]=${AAR_FG_COLORS[$n_long]}
AAR_BGNAMES_ALL[$variant]=${AAR_BG_COLORS[$n_long]}
done
done
for name in ${!ARR_MODNAMES_MAP[@]}; do
set -- ${ARR_MODNAMES_MAP[$name]}
n_long=$1 n_short=$2
for variant in $(cl_get_variants $n_long $n_short); do
AAR_MODNAMES_ALL[$variant]=${AAR_ANSI_MODES[$n_long]}
done
done
readonly AAR_FGNAMES_ALL AAR_BGNAMES_ALL AAR_MODNAMES_ALL
}
cl_save_variants() { # print (for saving) the code name variation arrays
declare -p AAR_FGNAMES_ALL AAR_BGNAMES_ALL AAR_MODNAMES_ALL
}
cl_ismode() { # check if name is a known mode
[[ ${AAR_MODNAMES_ALL[$1]} ]]
}
cl_iscolor() { # check if name is a known color
[[ ${AAR_FGNAMES_ALL[$1]} ]]
}
fi
# -------------------------------------------------------------------------
cl_list() { # List colors and modes
local x fgc bgc
local -i i
if ((BASH_VERSINFO[0] >= 4)); then
local STR_FGNAMES=$(printf "%s\n" "${!AAR_FG_COLORS[@]}" | run_sort)
local STR_MODENAMES=$(printf "%s\n" "${!AAR_ANSI_MODES[@]}" | run_sort)
else
local STR_FGNAMES=$(printf "%s\n" "${ARR_FG_COLORS[@]}" | run_sort)
local STR_MODENAMES=$(printf "%s\n" "${ARR_ANSI_MODES[@]}" | run_sort)
fi
pr_banner "Colors: Long-form | Shortest-form - Fore- / Background"
for x in $STR_FGNAMES; do
for i in ${!ARR_COLNAMES_MAP[@]}; do
set -- ${ARR_COLNAMES_MAP[i]}
if [[ $1 = $x ]]; then
if ((BASH_VERSINFO[0] >= 4)); then
fgc=${AAR_FG_COLORS[$x]} bgc=${AAR_BG_COLORS[$x]}
else
fgc=$(cl_get_fg_code $x) bgc=$(cl_get_bg_code $x)
fi
printf " %-9s | %-6s - %s / %s\n" $x $2 $fgc $bgc
break
fi
done
done
pr_banner "Modes: Long-form | Shortest-form - Code"
for x in $STR_MODENAMES; do
for i in ${!ARR_MODNAMES_MAP[@]}; do
set -- ${ARR_MODNAMES_MAP[i]}
if [[ $1 = $x ]]; then
if ((BASH_VERSINFO[0] >= 4)); then
fgc=${AAR_ANSI_MODES[$x]}
else
fgc=$(cl_get_ansi_code $x)
fi
printf " %-9s | %-6s - %s\n" $x $2 $fgc
break
fi
done
done
} # -------------------------------------------------------------------------
cl_show() { # Print the color table
local x
local -i i_mode i_fg i_bg
if ((BASH_VERSINFO[0] >= 4)); then
eval declare -ar ARR_ANSI_MODES=($(for x in ${!AAR_ANSI_MODES[@]};do printf "[%s]=%s\n" ${AAR_ANSI_MODES[$x]} $x;done))
eval declare -ar ARR_FG_COLORS=($(for x in ${!AAR_FG_COLORS[@]};do printf "[%s]=%s\n" ${AAR_FG_COLORS[$x]} $x;done))
eval declare -ar ARR_BG_COLORS=($(for x in ${!AAR_BG_COLORS[@]};do printf "[%s]=%s\n" ${AAR_BG_COLORS[$x]} $x;done))
fi
for i_mode in ${!ARR_ANSI_MODES[@]}; do
pr_sepstr
printf " Mode: %-12s Code: ESC[%s;Foreground;Background\n" ${ARR_ANSI_MODES[i_mode]} $i_mode
pr_sepstr
for i_fg in ${!ARR_FG_COLORS[@]}; do
for i_bg in ${!ARR_BG_COLORS[@]}; do
printf '\e[%s;%s;%sm %02s;%02s ' ${i_mode} $i_fg $i_bg $i_fg $i_bg
done
printf '\e[0m\n'
done
done
} # -------------------------------------------------------------------------
_cl_add_code() { # add a code sequence to the ansi_code string
str_ansi_seq+="$1;"
} # -------------------------------------------------------------------------
cl() { # assemble the ansi code sequence
local -i i_bg=i_fg=0
local str_ansi_seq=""
if [[ -z $1 || $1 = off ]]; then
str_ansi_seq=0
shift $#
fi
while (( $# )); do
if cl_ismode "$1"; then
if ((BASH_VERSINFO[0] >= 4)); then
_cl_add_code "${AAR_MODNAMES_ALL[$1]}"
else
_cl_add_code $(cl_get_ansi_code_all $1)
fi
elif cl_iscolor "$1"; then
if ! ((i_fg)); then
i_fg=1
if ((BASH_VERSINFO[0] >= 4)); then
_cl_add_code "${AAR_FGNAMES_ALL[$1]}"
else
_cl_add_code $(cl_get_fg_code_all $1)
fi
elif ! ((i_bg)); then
i_bg=1
if ((BASH_VERSINFO[0] >= 4)); then
_cl_add_code "${AAR_BGNAMES_ALL[$1]}"
else
_cl_add_code $(cl_get_bg_code_all $1)
fi
else
printf "Only 2 colors are allowed\n" >&2
return 1
fi
else
printf "\`%s' - unknown descriptor\n" "$1" >&2
return 1
fi
shift
done
printf "\e[${str_ansi_seq%;}m"
} # -------------------------------------------------------------------------
print_msg() { # print a [coloured] message
local fd=stdout msg_prefix="" bg_col="" fg_col="" msg_format=""
OPTIND=1
while getopts b:f:m:oENW option; do
case "$option" in
b) bg_col="$OPTARG" ;; # foreground colour
f) fg_col="$OPTARG" ;; # background colour
o) fd=stderr ;; # send to stdout or stderr
m) msg_format="$OPTARG" ;; # formatting
E) msg_prefix="ERROR: " ;;
N) msg_prefix="NOTICE: " ;;
W) msg_prefix="WARNING: " ;;
*) err_msg "Illegal parameter \`${option}' for function: ${FUNCNAME}()"
return 1
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
if ((ENABLE_COLORS)); then
if [[ $bg_col || $fg_col || $msg_format ]]; then
case "$fd" in
stdout)
printf "%b\n" "$(cl $fg_col $bg_col $msg_format)${msg_prefix}${*}$(cl)"
;;
stderr)
printf "%b\n" "$(cl $fg_col $bg_col $msg_format)${msg_prefix}${*}$(cl)" >&2
;;
esac
fi
else
case "$fd" in
stdout) pr_str "${msg_prefix}${*}" ;;
stderr) pr_err "${msg_prefix}${*}" ;;
esac
fi
} # -------------------------------------------------------------------------
log() { # Logging FUNCTION
[[ $2 ]] || { err_msg "2 parameters required for function: ${FUNCNAME}()"; return 1; }
local log_param="$1"
shift
local log_message="${@//[$'\t']/}"
case "$log_param" in
-d) log -E "Unable to retrieve interface for network: $log_message." ;;
-e)
((VERBOSE >= 6)) && pr_str "$log_message"
((SYSLOG_VERBOSE >= 5)) && syslog $LOG_FACILITY info "$log_message"
;;
-f) log -W "Failed $log_message" ;;
-i) log -E "Illegal value \`$1' for \`$2'." ;;
-l) syslog $LOG_FACILITY notice "$log_message" ;;
-m) pr_str "$log_message"; syslog $LOG_FACILITY notice "$log_message" ;;
-n) # Notice level without prefix
((VERBOSE >= 5)) && print_msg -f "$COLOR_MSG_NOTICE" $log_message
((SYSLOG_VERBOSE >= 4)) && syslog $LOG_FACILITY notice "$log_message"
;;
-o) log -E "Illegal parameter \`$1' for function: ${2}()." ;;
-r) log -E "Array \`$log_message' has no members." ;;
-s) log -E "System is missing the \`$log_message' match extension." ;;
-t) log -E "System is missing the \`$log_message' target." ;;
-u) log -W "\`$log_message' is unset." ;;
-v) log -N "\`$log_message' is unset." ;;
-w)
((VERBOSE >= 7)) && pr_str "$log_message"
((SYSLOG_VERBOSE >= 6)) && syslog $LOG_FACILITY info "$log_message"
;;
-x) log -E "\`$log_message' does not exist." ;;
-C) # Config file loading
((VERBOSE >= 4)) && print_msg -f "$COLOR_MSG_CONFIG_LOAD" $log_message
((SYSLOG_VERBOSE >= 3)) && syslog $LOG_FACILITY notice "$log_message"
;;
-E) # Error message
((VERBOSE >= 1)) && print_msg -o -E -f "$COLOR_MSG_ERROR" $log_message
syslog $LOG_FACILITY error "ERROR: $log_message"
;;
-F) log -E "Failed $log_message" ;;
-I) # Sub-Sub(title) message
((VERBOSE >= 4)) && print_msg -f "$COLOR_MSG_INFO_TITLE" $log_message
((SYSLOG_VERBOSE >= 3)) && syslog $LOG_FACILITY notice "$log_message"
;;
-N) # Notice
((VERBOSE >= 5)) && print_msg -N -f "$COLOR_MSG_NOTICE" $log_message
((SYSLOG_VERBOSE >= 4)) && syslog $LOG_FACILITY notice "NOTICE: $log_message"
;;
-R) # Rule file loading
((VERBOSE >= 4)) && print_msg -f "$COLOR_MSG_RULE_LOAD" $log_message
((SYSLOG_VERBOSE >= 3)) && syslog $LOG_FACILITY notice "$log_message"
;;
-S) # Sub(title) message
((VERBOSE >= 3)) && print_msg -m bold -f "$COLOR_MSG_SUBTITLE" $log_message
((SYSLOG_VERBOSE >= 2)) && syslog $LOG_FACILITY notice "$log_message"
;;
-T) # Main (header) message
((VERBOSE >= 2)) && print_msg -m bold -f "$COLOR_MSG_MAIN_TITLE" $log_message
((SYSLOG_VERBOSE >= 1)) && syslog $LOG_FACILITY notice "$log_message"
;;
-U) # Unknown error
log -E "Unknown error in function \`${1}()'."
;;
-W) # Warning message
((VERBOSE >= 2)) && print_msg -o -W -f "$COLOR_MSG_WARNING" $log_message
((SYSLOG_VERBOSE >= 1)) && syslog $LOG_FACILITY warning "WARNING: $log_message"
;;
*) err_msg "unknow option \`$log_param' for function: ${FUNCNAME}()"
return 1
esac
return 0
} # -------------------------------------------------------------------------
syslog() { # log to syslog using logger utility
if ((ENABLE_SYSLOG == 1)); then # only log if logging to syslog is enabled
[[ $3 ]] || { err_msg "3 parameters required for function: \`${FUNCNAME}()'"; return 1; }
local s_facility="$1" s_level="$2"
shift 2
"${LOGGER}" -p "${s_facility}.${s_level}" -t "${ME}[${PID}]" "$*" || err_msg "failed logging to syslog: \`$*'"
fi
} # -------------------------------------------------------------------------
reqparm() { # announce parameters missing in functions
[[ $2 ]] || { err_msg "2 parameters required for function: \`${FUNCNAME}()'" ; return 1; }
log -E "$2 parameter(s) required for function: \`$1()'"
} # -------------------------------------------------------------------------
file_readable() { # check if a file is really readable
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local file="$1"
read -t $GLOBAL_READ_TIMEOUT < "$file" || {
log -E "File \`$file' is not readable."
return $ER_FAIL
}
} # -------------------------------------------------------------------------
proc_file_writable() { # check if a /proc file is really writable
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local file="$1"
if read -t $GLOBAL_READ_TIMEOUT < "$file"; then
(printf "%s\n" "$REPLY" > "$file") &>/dev/null
else
return $ER_FAIL
fi
} # -------------------------------------------------------------------------
check_prog() { # check for mandatory programs
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ -e "$1" ]] || { log -E "Binary $1 does not exist"; return $ER_NOEX; }
[[ -x "$1" ]] || { log -E "Binary $1 is not executable"; return $ER_FAIL; }
} # -------------------------------------------------------------------------
#set_var() { # set variables to a specific value
#[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
#eval "$1"='"$2"'
#} # -------------------------------------------------------------------------
set_var() { # set variables to a specific value
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ $1 = +([[:word:]]) ]] || {
log -E "\`$1' is not a valid variable name."
return 1
}
#eval "$1"'="$2"'
printf -v "$1" "$2"
} # -------------------------------------------------------------------------
subst_var() { # substitute a variable name to its value
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
printf "%s\n" "${!1}"
} # -------------------------------------------------------------------------
string_negated() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ ${1:0:1} = '!' ]]
} # -------------------------------------------------------------------------
remove_negation() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
printf "%s\n" "${1#\!}"
} # -------------------------------------------------------------------------
demask() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ $1 = */* ]] && printf "%s\n" "${1%/*}" || printf "%s\n" "$1"
} # -------------------------------------------------------------------------
split_val() {
[[ $3 ]] || { reqparm $FUNCNAME 3; return 1; }
local IFS="$1" str_sep="$2"
set -- $3
local IFS="$str_sep"
printf "%s\n" "$*"
} # -------------------------------------------------------------------------
#split_val() { # split separator separated values and echo the result
##set -- ${@}
#local str
#for str in ${@}; do
# printf "%s\n" "${str}"
#done
#} # -------------------------------------------------------------------------
my_cut() {
# show only specified columns of input, separated by an optionaly supplied IFS
local OldIFS="$IFS" colums_to_show=1 NewIFS str_line str_output col_idx option
OPTIND=1
while getopts :d:c: option; do
case "$option" in
d) NewIFS="${OPTARG}" ;;
c) colums_to_show=${OPTARG} ;;
?) return 1 ;;
esac
done
OPTIND=1
while read -r -u 0 -t $GLOBAL_READ_TIMEOUT str_line; do
[[ ${str_line} ]] || continue
local IFS="${NewIFS:-${OldIFS}}"
set -- ${str_line}
local IFS="${OldIFS}"
str_output=""
for col_idx in ${colums_to_show}; do
[[ ${!col_idx} ]] && str_output+="${!col_idx} "
done
printf "%s\n" "${str_output% }"
done
} # -------------------------------------------------------------------------
string_tolower() { # convert upper case characters into lower case
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local to_low_string="$@"
case "${to_low_string}" in
+(*[[:upper:]]*))
if ((BASH_VERSINFO[0] == 3)); then
to_low_string="${to_low_string//A/a}"
to_low_string="${to_low_string//B/b}"
to_low_string="${to_low_string//C/c}"
to_low_string="${to_low_string//D/d}"
to_low_string="${to_low_string//E/e}"
to_low_string="${to_low_string//F/f}"
to_low_string="${to_low_string//G/g}"
to_low_string="${to_low_string//H/h}"
to_low_string="${to_low_string//I/i}"
to_low_string="${to_low_string//J/j}"
to_low_string="${to_low_string//K/k}"
to_low_string="${to_low_string//L/l}"
to_low_string="${to_low_string//M/m}"
to_low_string="${to_low_string//N/n}"
to_low_string="${to_low_string//O/o}"
to_low_string="${to_low_string//P/p}"
to_low_string="${to_low_string//Q/q}"
to_low_string="${to_low_string//R/r}"
to_low_string="${to_low_string//S/s}"
to_low_string="${to_low_string//T/t}"
to_low_string="${to_low_string//U/u}"
to_low_string="${to_low_string//V/v}"
to_low_string="${to_low_string//W/w}"
to_low_string="${to_low_string//X/x}"
to_low_string="${to_low_string//Y/y}"
to_low_string="${to_low_string//Z/z}"
elif ((BASH_VERSINFO[0] >= 4)); then
to_low_string="${to_low_string,,}"
fi
;;
esac
printf "%s\n" "${to_low_string}"
} # -------------------------------------------------------------------------
string_toupper() { # convert lower case characters into upper case
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local to_up_string="$@"
case "${to_up_string}" in
+(*[[:lower:]]*))
if ((BASH_VERSINFO[0] == 3)); then
to_up_string="${to_up_string//a/A}"
to_up_string="${to_up_string//b/B}"
to_up_string="${to_up_string//c/C}"
to_up_string="${to_up_string//d/D}"
to_up_string="${to_up_string//e/E}"
to_up_string="${to_up_string//f/F}"
to_up_string="${to_up_string//g/G}"
to_up_string="${to_up_string//h/H}"
to_up_string="${to_up_string//i/I}"
to_up_string="${to_up_string//j/J}"
to_up_string="${to_up_string//k/K}"
to_up_string="${to_up_string//l/L}"
to_up_string="${to_up_string//m/M}"
to_up_string="${to_up_string//n/N}"
to_up_string="${to_up_string//o/O}"
to_up_string="${to_up_string//p/P}"
to_up_string="${to_up_string//q/Q}"
to_up_string="${to_up_string//r/R}"
to_up_string="${to_up_string//s/S}"
to_up_string="${to_up_string//t/T}"
to_up_string="${to_up_string//u/U}"
to_up_string="${to_up_string//v/V}"
to_up_string="${to_up_string//w/W}"
to_up_string="${to_up_string//x/X}"
to_up_string="${to_up_string//y/Y}"
to_up_string="${to_up_string//z/Z}"
elif ((BASH_VERSINFO[0] >= 4)); then
to_up_string="${to_up_string^^}"
fi
;;
esac
printf "%s\n" "${to_up_string}"
} # -------------------------------------------------------------------------
strip_blanks() { # strip leading and trailing [:blank:] type chars
[[ $1 ]] || return 0
local s_var="$1"
[[ ${s_var:0:1} = [[:blank:]] ]] && s_var="${s_var##+([[:blank:]])}"
[[ ${s_var:$((${#s_var} - 1))} = [[:blank:]] ]] && s_var="${s_var%%+([[:blank:]])}"
printf "%s\n" "${s_var}"
} # -------------------------------------------------------------------------
strip_blanks_var() { # strip leading and trailing [:blank:] type chars
[[ $1 ]] || return 0
local s_var="$1"
[[ ${!s_var} ]] || return 0
local tmp="${!s_var}"
[[ ${tmp:0:1} = [[:blank:]] ]] && tmp="${tmp##+([[:blank:]])}"
[[ ${tmp:$((${#tmp} - 1))} = [[:blank:]] ]] && tmp="${tmp%%+([[:blank:]])}"
eval "${s_var}"='"${tmp}"'
} # -------------------------------------------------------------------------
escape_varname() {
local s_var="$1"
[[ ${!s_var} ]] || return 0
printf "%q\n" "${!s_var}"
} # -------------------------------------------------------------------------
re_normalize() {
local s_var="$1"
[[ ${!s_var} ]] || return 0
local tmp="${!s_var}"
#set -x
#tmp="${tmp//\'/}"
tmp="${tmp//\`/}"
tmp="${tmp//\\/\\\\}"
#if [[ ${tmp} = *"'"* ]]; then
# tmp="${tmp//\'/}"
#fi
#tmp="${tmp//\´/}"
#tmp="${tmp//>/\\>}"
#set +x
printf "%s\n" "${tmp}"
} # -------------------------------------------------------------------------
re_normalize2() {
local s_var="$1"
[[ ${!s_var} ]] || return 0
local tmp="${!s_var}"
##set -x
tmp="${tmp//\'/}"
#tmp="${tmp//\`/}"
#tmp="${tmp//\\/\\\\}"
##tmp="${tmp//\´/}"
##tmp="${tmp//\\/\\\\}"
tmp="${tmp//\\/}"
tmp="${tmp//\"/\\\"}"
##tmp="${tmp//>/\\>}"
##set +x
printf "%s\n" "${tmp}"
#printf "%q\n" "${tmp}"
} # -------------------------------------------------------------------------
lsearch() { # search word in list
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1"
shift
while (($#)); do
[[ $1 = $str_search ]] && return
shift
done
return 1
} # -------------------------------------------------------------------------
lsearch_lines() { # search for a given word in std input
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1"
while read -r -t $GLOBAL_READ_TIMEOUT; do
set -- $REPLY
while (($#)); do
[[ $1 = $str_search ]] && return
shift
done
done
return 1
} # -------------------------------------------------------------------------
lsearch_echo() { # search for the first occurence of a given word in input and echo it back
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1" str_input
while read -r -t $GLOBAL_READ_TIMEOUT; do
set -- $REPLY
str_input="$@"
while (($#)); do
[[ $1 = $str_search ]] && {
printf "%s\n" "$str_input"
return 0
}
shift
done
done
return 1
} # -------------------------------------------------------------------------
lsearch_echo_multiline() { # search for a given word in input (multi line) and echo it back
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1" str_input
while read -r -t $GLOBAL_READ_TIMEOUT; do
set -- $REPLY
str_input="$@"
while (($#)); do
[[ $1 = $str_search ]] && {
printf "%s\n" "$str_input"
break
}
shift
done
done
} # -------------------------------------------------------------------------
lsearch_echo_reverse() { # show only the part of the input, which does not contain 'word'
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1" str_input
while read -r -t $GLOBAL_READ_TIMEOUT; do
set -- $REPLY
str_input="$@"
while (($#)); do
[[ $1 = $str_search ]] && continue 2
shift
done
printf "%s\n" "$str_input"
done
} # -------------------------------------------------------------------------
lsearch_echo_reverse_single() { # search for a given word, remove first occurence, print rest
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1" str_input
local -i found_str=0
while read -r -t $GLOBAL_READ_TIMEOUT; do
set -- $REPLY
while (($#)); do
[[ $1 = $str_search ]] && ((found_str == 0)) && {
found_str=1
shift
continue
}
if [[ -z $str_input ]]; then
str_input="$1"
else
str_input+=" $1"
fi
shift
done
printf "%s\n" "$str_input"
done
} # -------------------------------------------------------------------------
lsearch_regex() { # perform a regex search in input
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1"
while read -r -t $GLOBAL_READ_TIMEOUT; do
[[ $REPLY =~ $str_search ]] && {
printf "%s\n" "$REPLY"
return 0
}
done
return 1
} # -------------------------------------------------------------------------
lsearch_regex_echo_object() { # perform a regex search in input and print result
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_search="$1"
while read -r -t $GLOBAL_READ_TIMEOUT; do
set -- $REPLY
while (($#)); do
[[ $1 =~ $str_search ]] && {
printf "%s\n" "$1"
return 0
}
shift
done
done
return 1
} # -------------------------------------------------------------------------
#lsearch_regex_echo_multiline() # perform a regex search in input
#{
#[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
#local str_search str_line
#str_search="$1"
#while read -r -u 0 -t $GLOBAL_READ_TIMEOUT str_line; do
# [[ "${str_line}" =~ "${str_search}" ]] && {
# echo "${str_line}"
# }
#done
#} # -------------------------------------------------------------------------
find_x_get_y() { # find a word and echo the next one
local str_x="$1"
shift
while (($#)); do
if [[ $1 = $str_x && $2 ]]; then
printf "%s\n" "$2"
return 0
fi
shift
done
return 1
} # -------------------------------------------------------------------------
check_var_overlap() { # check for overlapping variable names
# $1 = name of variable (maybe user config var) to be checked,
# $2 = local function variable name to be checked against
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
[[ $1 != $2 ]] || {
log -E "Name \`$1' is overlapping with the local variable name \`$2'."
return 1
}
} # -------------------------------------------------------------------------
check_unique() { # check for unique words in input
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
local str_s="$1"
shift
if printf "%s\n" "$@" | lsearch_echo_reverse_single "$str_s" | lsearch_lines "$str_s"
then
return 1
fi
} # -------------------------------------------------------------------------
check_str_unique() { # check for unique words in string
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local str_unique="$@" str_search
for str_search in $str_unique; do
check_unique "$str_search" "$str_unique" || {
log -E "Duplicate entry \`$str_search'."
return 1
}
str_unique="${str_unique/${str_search}/}"
done
} # -------------------------------------------------------------------------
val_numeric() { # check if a value is numeric
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ $1 = +([[:digit:]]) ]] || {
log -E "Value \`$1' is not numeric."
return 1
}
} # -------------------------------------------------------------------------
val_numeric_quiet() { # silenlty check if a value is numeric
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ $1 = +([[:digit:]]) ]]
} # -------------------------------------------------------------------------
val_range() {
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
(($1 < $2)) || {
log -E "Starting value \`$1' of range is not lower than end of range value \`$2'."
return 1
}
} # -------------------------------------------------------------------------
#val_xdigit() {
#[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
#[[ $1 = +([[:xdigit:]]) ]] || {
# log -E "Value \`$1' is not hexadecimal."
# return 1
#}
#} # -------------------------------------------------------------------------
members_in_string() { # retrieve the amount of words in a string
set -- $@
printf "%d\n" $#
} # -------------------------------------------------------------------------
show_last_char() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
printf "%s\n" "${1:$((${#1} - 1))}"
} # -------------------------------------------------------------------------
check_vars() { # check for undefined variables and report an error if mising
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
declare -i var_missing=0
while (($#)); do
if ! [[ ${!1} ]]; then
log -E "Variable \`$1' is undefined."
let var_missing+=1
fi
shift
done
((var_missing == 0)) || return 1
} # -------------------------------------------------------------------------
check_vars_no_sep() { # check for undefined variables, or options which must not be a placeholder.
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
declare -i var_missing=var_sep=0
while (($#)); do
if ! [[ ${!1} ]]; then
log -E "\`$1' is not set"
let var_missing+=1
else
if [[ ${!1} = $RULE_SEP ]]; then
let var_sep+=1
log -E "\`$1' is mandatory. Placeholder not allowed."
fi
fi
shift
done
((var_missing == 0 && var_sep == 0))
} # -------------------------------------------------------------------------
check_var_no_sep_silent() { # check for undefined variable, or option which must not be a placeholder.
[[ $1 ]] || return 1
[[ $1 != $RULE_SEP ]]
} # -------------------------------------------------------------------------
var_defined_silent() { # silently check for an undefined variable
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
[[ ${!1} ]]
#eval '[[ ${'"$1"'} ]]'
} # -------------------------------------------------------------------------
build_list() {
local str_sep="$1"
shift
local str_list="$*"
str_list="$(strip_blanks "$str_list")"
printf "%s\n" "${str_list//+([[:blank:]])/$str_sep}"
} # -------------------------------------------------------------------------
construct_assignment() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
local v_name="$1"
printf '%s="%s"\n' "$v_name" "${!v_name}"
} # -------------------------------------------------------------------------
#show_first_line()
#{
#[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
#local str_line
#read -u 0 str_line
#echo "${str_line}"
#} # -------------------------------------------------------------------------
#find_char_in_string()
#{
#[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
#char_search="$1"
#str_search="$2"
#for i in $(seq 0 $((${#str_search}-1))); do
# [ "${str_search:${i}:1}" = "${char_search}" ] && return 0
#done
#return 1
#} # -------------------------------------------------------------------------
arr_members_sum() { # echo the amount of members an array ($1) contains
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
#eval 'echo "${#'"$1"'[@]}"'
eval 'printf "%d\n" "${#'"$1"'[@]}"'
} # -------------------------------------------------------------------------
last_arr_member() { # retrieve the last member of an array
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
eval 'set -- -1 ${!'"$1"'[@]}'
printf "%d\n" "${!#}"
} # -------------------------------------------------------------------------
arr_indicies() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
eval 'printf "%s\n" "${!'"$1"'[*]}"'
} # -------------------------------------------------------------------------
arr_populated() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
(($(arr_members_sum "$1") > 0))
} # -------------------------------------------------------------------------
show_arr_as_string() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
eval 'printf "%s\n" "${'"$1"'[*]}"'
} # -------------------------------------------------------------------------
show_arr_members() {
[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
eval 'printf "%s\n" "${'"$1"'[@]}"'
} # -------------------------------------------------------------------------
dump_arr() {
while read; do
printf "%s=(\n" "$REPLY"
for i in $(arr_indicies "$REPLY"); do
eval 'printf "[$i]='\''%s'\''\n" "${'"$REPLY"'['$i']}"'
done
printf ")\n"
done
} # -------------------------------------------------------------------------
#check_array_empty_indicies() { # check for empty indicies in an array
#[[ $1 ]] || { reqparm $FUNCNAME 1; return 1; }
#(($(($(last_arr_member "$1") + 1)) == $(arr_members_sum "$1")))
#} # -------------------------------------------------------------------------
cp_array() { # command builder function to copy the content from one array to another
[[ $2 ]] || { reqparm $FUNCNAME 2; return 1; }
check_var_overlap "$1" 's_arr' || return 1
check_var_overlap "$2" 'd_arr' || return 1
local -i arr_idx
local s_arr="$1" d_arr="$2"
eval "${d_arr}"'=()'
printf "%s\n" 'eval for arr_idx in ${!'"${s_arr}"'[@]}; do '"${d_arr}"'[arr_idx]="${'"${s_arr}"'[arr_idx]}"; done'