-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_dbg.sh
1110 lines (972 loc) · 31.3 KB
/
lib_dbg.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
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
#!/bin/bash -u
# ------------------------------------------------------------------------------
# DEBUG LIBRARY
# ------------------------------------------------------------------------------
#
# ~/config/lib_dbg.sh
#
# PURPOSE: the function "line echo" or lecho() prints the line from which the
# function is called. This is designed to be used in place of commands like
# echo "here", etc.
#
# May 2024 JCL
#
# ------------------------------------------------------------------------------
# define variable status keywords
export UNSET='\x1B[1;33munset\x1B[0m' # unbound variable
export NULL='\x1B[1;36mnull\x1B[0m' # empty variable
export SPACE='\x1B[30;106m' # highlight white space
export TRUE='\x1B[1;32mtrue\x1B[0m' #
export FALSE='\x1B[1;31mfalse\x1B[0m' #
libDEBUG=1
#unset libDEBUG
function tl() {
# re-worked "this line"
# turn printing on or off
local -i do_print=1
if [ $do_print = 1 ]; then
# print grep-like file and line number
print_stack
echo -en "${TAB}${GRF}"
if [[ ${#FUNCNAME[@]} -gt 1 ]]; then
echo -en "${BASH_SOURCE[1]##*/}"
else
echo -en "${SHELL##*/}"
fi
echo -en "${GRS}:${GRL}${BASH_LINENO[0]}${RESET}"
# check if caller is function
if [[ ${#FUNCNAME[@]} -gt 1 ]]; then
this_func=${FUNCNAME[1]}
# check if caller is CLI, print function name if applicable
if [[ "${this_func}" == "main" ]] || [[ "${this_func}" == "source" ]]; then
: #echo
else
echo -en "${GRS}: ${GRH}${this_func}()${RESET}"
fi
fi
# print argument, if present
if [[ ! -z "$@" ]]; then
echo -e " ${INVERT}$@${RESET}"
else
echo
fi
fi
return 0
}
function set_fcol() {
local -i N=9
# use argument to manually set color
if [ $# -eq 1 ]; then
N=$1
fi
local -i idx
dbg2idx $N idx
export fcol=${dcolor[idx]}
# set color
echo -ne "${fcol}"
}
function in_line() {
set_fcol
echo -en "${TAB}${fcol}"
if [[ ! -z "$@" ]]; then
echo -en "${INVERT}$@${NORMAL} <- ${fcol}"
fi
}
function lec_mes () {
if [ $N_BASH -gt 1 ]; then
echo -en "line ${BASH_LINENO[1]} "
fi
echo -en "called on line ${GRL}"
}
# This function print the result to STDOUT. All outher output is printed to
# STDERR. To use the function, redirect function call output to /dev/null. To
# view degugging information, do no redirect function all output.
function find_func_line() {
# set local debug level
local -i DEBUG=${libDEBUG:-0}
DEBUG=0
# print THIS function name
dddecho -e "${TAB}${INVERT}${FUNCNAME}${RESET}" >&2
# set function name
local func=$1
decho -n "${TAB}function: $func... " >&2
# get function definition
declare -f ${func} >/dev/null
local -i RETVAL=$?
if [ $RETVAL -eq 0 ]; then
if [ -z "$(declare -f ${func})" ]; then
decho -e "${BAD}FAIL${RESET} empty" >&2
return 1
else
decho -e "${GOOD}OK${RESET}" >&2
fi
else
decho -e "${BAD}FAIL${RESET} not defined" >&2
return 1
fi
# define search pattern
# DECLARE - print function definition
# HEAD - get declaration line
# SED - remove whitespace
local pat="$(declare -f ${func} | head -1 | sed 's/ /[ ]*/;s/[ ]*$//;s/)/&[ ]*#*.*[ \\n\\r{]*$/')"
decho "${TAB}matching pattern..." >&2
itab
decho "${TAB}base pattern: $pat" >&2
# get source file
local src=$2
decho "${TAB}source: $src" >&2
# print matching line
if [ $DEBUG -gt 0 ]; then
grep -n "${pat}" "${src}" --color=always | sed "s/^/${TAB}grep: /" >&2
fi
dtab
# reset shell options
if [[ "$-" == *e* ]]; then
old_opts=$(echo "$-")
set +e
fi
unset_traps 0
# look for function definition without 'function' prefix
decho "${TAB}matching pattern without function prefix..." >&2
local epat="^[ ]*${pat}"
itab
decho "${TAB}extended pattern: $epat" >&2
# grep -n "${epat}" "${src}" --color=always | sed "s/^/${TAB}/" >&2
if [ ! -z "$(grep "${epat}" "${src}")" ]; then
decho "${TAB}found without function" >&2
else
decho "${TAB}not found" >&2
dtab
# look for function definition with 'function' prefix
decho "${TAB}matching pattern with function prefix..." >&2
itab
epat="^function[ ]\+${pat}"
decho "${TAB}extended pattern: $epat" >&2
#grep -n "${epat}" "${src}" --color=always | sed "s/^/${TAB}/" >&2
if [ ! -z "$(grep "${epat}" "${src}")" ]; then
decho "${TAB}found with function" >&2
else
decho "${TAB}pattern not found" >&2
return 1
fi
fi
dtab
# display results
decho "${TAB}matching line:" >&2
itab
if [ $DEBUG -gt 0 ]; then
grep -n "${epat}" "${src}" --color=always | sed "s/^/${TAB}grep: /" >&2
fi
if [ $(grep -n "${epat}" "${src}" | wc -l) -eq 1 ]; then
decho -e "${TAB}${GOOD}OK${RESET} pattern unique" >&2
else
decho -e "${TAB}${BAD}FAIL${RESET} pattern not unique" >&2
return 1
fi
reset_shell ${old_opts-''}
reset_traps 0
dtab
# get line number
local -i lin=$(grep -n "${epat}" "${src}" | awk -F: '{print$1}')
decho -e "${TAB}${BOLD}pattern found on line ${lin}${RESET}" >&2
itab
decho "${TAB}output: $lin" >&2
dtab
# The function line number counts from the open bracket, not the function
# name declaration line. If the two are not on the same line, increment the
# line counter.
lin_txt=$(sed -n "${lin}p" "${src}")
if [[ ! "${lin_txt}" =~ .*"{" ]]; then
((++lin))
fi
# return the result to STDOUT
echo $lin
}
function get_caller() {
# clear variables
unset this_func
unset this_source
unset this_file
unset caller_func
unset caller_source
unset caller_file
# define stack level number
local -i lev
if [ $# -eq 0 ]; then
lev=1
else
lev=$1
fi
#print_stack
fecho " in lev = $lev"
# increment level so that index matches that of calling function
((++lev))
fecho "out lev = $lev"
local -i fN_FUNC=${#FUNCNAME[@]}
fecho " N_FUNC = $fN_FUNC"
local -i this_lev=$((lev-1))
# define stack level for "this" function; actually the calling function
# (default) or the function one level below the target stack level
# (argument)
if [ ${fN_FUNC} -ge 1 ]; then
# get function
this_func=${FUNCNAME[this_lev]}
# get function source
this_source=${BASH_SOURCE[this_lev]}
# get function source file
this_file=${this_source##*/}
fi
if [ ${lev} -lt ${fN_FUNC} ]; then
fecho "$lev -lt $fN_FUNC"
# get calling function source
caller_source=${BASH_SOURCE[lev]}
# get calling function source file
caller_file=${BASH_SOURCE[lev]##*/}
# get calling function name
caller_func=${FUNCNAME[lev]}
fi
fecho "done with get caller"
}
function get_caller_def() {
# clear variables
unset this_func
unset this_source
unset this_file
unset caller_func
unset caller_source
unset caller_file
unset this_def
unset line_def
unset caller_func_line
unset get_file_line
declare -ig this_def
declare -ig line_def
declare -ig caller_func_line
declare -ig get_file_line
# define stack level number
local -i lev
if [ $# -eq 0 ]; then
lev=1
else
lev=$1
fi
#print_stack
fecho " in lev = $lev"
# increment level so that index matches that of calling function
((++lev))
fecho "out lev = $lev"
local -i fN_FUNC=${#FUNCNAME[@]}
fecho " N_FUNC = $fN_FUNC"
local -i this_lev=$((lev-1))
# define stack level for "this" function; actually the calling function
# (default) or the function one level below the target stack level
# (argument)
if [ ${fN_FUNC} -ge 1 ]; then
# get function
this_func=${FUNCNAME[this_lev]}
# get function source
this_source=${BASH_SOURCE[this_lev]}
# get line definition
if [[ "${this_func}" == "main" ]] || [[ "${this_func}" == "source" ]]; then
this_def=-1
else
this_def=$(find_func_line "${this_func}" "${this_source}");
fi
# get function source file
this_file=${this_source##*/}
fi
if [ ${lev} -lt ${fN_FUNC} ]; then
fecho "$lev -lt $fN_FUNC"
# get calling function source
caller_source=${BASH_SOURCE[lev]}
# get calling function source file
caller_file=${BASH_SOURCE[lev]##*/}
# get calling function name
caller_func=${FUNCNAME[lev]}
# get line in calling function where this tunction was called
caller_func_line=${BASH_LINENO[this_lev]}
fecho "get func line = $caller_func_line"
# get calling function definition line
if [[ "${caller_func}" == "main" ]] || [[ "${caller_func}" == "source" ]]; then
line_def=-1
get_file_line=-1
else
fecho "func: ${caller_func}"
fecho "file: ${caller_source}"
line_def=$(find_func_line "${caller_func}" "${caller_source}")
# get line in calling function source file
get_file_line=$((${line_def}+${caller_func_line}-1))
fi
else
caller_func_line=0
fi
fecho "done with get caller"
}
function this_line() {
set -u
export TAB=${TAB-}
# DEBUG = 0 print line in file only
# DEBUG = 1 print calling function
# DEBUG = 2 print calling function line def
# DEBUG = 3 print calling function line
# set local debug level
local -i DEBUG=${DEBUG:-0}
# manual
DEBUG=3
#DEBUG=${libDEBUG:-0}
local -i funcDEBUG=0
#funcDEBUG=${libDEBUG:-0}
local do_grep=true;
local do_before=true;
local do_extra=true;
local do_defs=false;
local do_invo=true;
# set function color
set_fcol
local -ir fN_STACK=${#FUNCNAME[@]}
fecho "${TAB}fN_STACK = $fN_STACK"
if $do_defs && [ $DEBUG -gt 1 ]; then
for ((lev = 0; lev < $fN_STACK ; lev++)); do
fecho "lev = $lev (definition)"
get_caller_def $lev
# print this function definition line
ddecho -n "${TAB}"
ddecho -n "FUNCNAME[$lev] = "
ddecho -n "$caller_func() "
ddecho -n "defined on line $line_def "
ddecho -n "in file ${caller_file}"
ddecho
done
fecho "done printing definitions"
fi
if $do_invo && [ $DEBUG -gt 1 ] ; then
for ((lev = 1; lev < $fN_STACK; lev++)); do
fecho "lev = $lev (invocation)"
get_caller_def $lev
# print calling function line in source file
ddecho -n "${TAB}${this_func}() called by "
dddecho -n "line $caller_func_line of "
ddecho -n "$caller_func() "
ddecho -n "on line $get_file_line of ${caller_file}"
ddecho
done
fecho "done printing invocations"
fi
set +e
set_traps 0
get_caller_def
if $do_grep; then
# print grep-like match
echo -en "${TAB}"
[[ ! -z "$@" ]] && [ $do_before = true ] && echo -en "${GRH}${INVERT}$@${NORMAL} "
echo -en "${GRF}${caller_file}${GRS}:${GRL}${get_file_line}${GRS}: ${GRH}"
if [[ -z "$@" ]] || [ $do_before = true ]; then
echo -en "${this_func}() "
else
echo -en "${INVERT}$@${NORMAL} "
fi
ddecho -n "called by "
dddecho -n "line $caller_func_line of "
decho -n "${caller_func}"
if [[ "${caller_func}" == "main" ]] || [[ "${caller_func}" == "source" ]]; then
:
else
ddecho -n "() "
ddecho -n "defined on line $line_def"
fi
else
# print argument
start_new_line
in_line "$@"
# print the line number where THIS function was called in the PARENT
# function
[ $DEBUG -gt 0 ] && echo -n "${caller_func}() "
echo -en "${fcol}on line $get_file_line in ${caller_file} "
ddecho -n "defined on line $line_def"
dddecho -n ", function line $caller_func_line"
fi
echo -e ${RESET}
if [[ "$-" == *u* ]]; then
set +u
fi
return 0
}
# line echo
function lecho() {
# save shell options
old_opts=$(echo "$-")
# set local debug level
local -i DEBUG=${DEBUG:-1}
local -i oldDEBUG=$DEBUG
if [ $DEBUG -lt 1 ]; then
idb >/dev/null
fi
# manual
DEBUG=0
#DEBUG=${libDEBUG:-0}
set_fcol
start_new_line
echo -ne "${fcol}"
dddecho -e "${TAB}${INVERT}${FUNCNAME}${fcol}"
set -u
declare -i hr_wid=15
if [ ${DEBUG:-0} -gt 0 ] || [ ! -z "$@" ] ; then
local -i tab_wid=${#TAB}
local -i term_wid=${COLUMNS:-72}
hr_wid=$(($term_wid - (2 * $tab_wid)))
hline $hr_wid
trap 'echo -ne "${fcol}";hline $hr_wid;DEBUG=$oldDEBUG;echo -en "$RESET";trap -- RETURN' RETURN
fi
if [ ${DEBUG:-0} -gt 1 ]; then
print_stack
fi
# get the lenght of the execution stack
local -i N_BASH=${#BASH_SOURCE[@]}
ddecho "${TAB}there are ${N_BASH} entries in the ${FUNCNAME}() call stack"
# get the line where THIS function is defined
line_def=$(find_func_line "${FUNCNAME}" "${BASH_SOURCE}"); # 2>/dev/null
if [ $N_BASH -eq 1 ]; then
in_line "$@"
echo -e "${FUNCNAME}() $(lec_mes)${BASH_LINENO}${fcol} from ${BASH##*/}"
ddecho "${TAB}exiting ${FUNCNAME}() on line $((${line_def}+${LINENO}-1))..."
return 0
fi
itab
ddecho "${TAB}${FUNCNAME}"
ddecho "${TAB}FUNCNAME stack = ${FUNCNAME[@]}"
# define stack ofset
local -i N_off
N_off=1;
dddecho "${TAB}showing regular lecho results"
# check offset agaisnt stack depth
if [ $N_BASH -ge $N_off ]; then
N_BASHo=$(($N_BASH - $N_off))
(
ddecho "N_BASH:${N_BASH}"
ddecho "N_off:${N_off}"
decho "N_BASHo:${N_BASHo}"
) | column -t -s: -o" = " | sed "s/^/${TAB}/"
fi
# set level of the calling function
local -i sour_lev=$N_off
ddecho "${TAB}source level = ${sour_lev}"
if [ ! ${sour_lev} -eq 1 ]; then
echo "check source level"
fi
# get the file of the calling function
local sour=${BASH_SOURCE[1]}
local sour_dir=$(dirname "${sour}")
local sour_fil=$(basename "${sour}")
ddecho "${TAB}source is ${sour}"
ddecho "${TAB}source is ${sour_fil} in ${sour_dir}"
dtab
decho "${TAB}there are ${N_BASHo} entries in the ${FUNCNAME[$N_off]} call stack"
itab
local -i pare_lev=$(($N_BASHo - 1 + ${N_off}))
decho "${TAB}parent level = $pare_lev"
decho "${TAB}parent function is ${FUNCNAME[$pare_lev]}"
dtab
# get the line of of the calling function
local -i func_line=${BASH_LINENO[0]}
# BASH_SOURCE counts from zero; get the bottom of the stack
local bottom=${FUNCNAME[$(($N_BASH - 1))]##*/}
ddecho "${TAB}bottom of stack is $bottom"
# get the calling function
local func=${FUNCNAME[1]}
ddecho "${TAB}calling function is $func"
if [[ "${func}" == "main" ]] || [[ "${func}" == "source" ]]; then
# the function is call from bash
ddecho -e "${TAB}called by ${SHELL##*/}"
ddecho "${TAB}N_BASH = ${N_BASH}"
ddecho "${TAB}function level = $pare_lev"
in_line "$@"
echo -e "${FUNCNAME}() $(lec_mes)${BASH_LINENO[(($N_BASH-2))]}${fcol} in file ${YELLOW}${BASH_SOURCE[(($N_BASH-1))]##*/}${fcol}"
echo -e "${FUNCNAME[(($pare_lev-1))]}() called on line ${GRL}${BASH_LINENO[(($pare_lev-1))]}${RESET} in file ${YELLOW}${BASH_SOURCE[$pare_lev]##*/}${RESET}"
((--pare_lev))
decho -e "${TAB}${BASH_SOURCE[$pare_lev]##*/}${RESET} called by ${SHELL##*/}"
decho -e "${TAB}${FUNCNAME[(($pare_lev-1))]}() called on line ${GRL}${BASH_LINENO[(($pare_lev-1))]}${RESET} in file ${YELLOW}${BASH_SOURCE[$pare_lev]##*/}${RESET}"
decho "${TAB}exiting ${FUNCNAME}() on line $((${line_def}+${LINENO}-1))..."
dtab
return 0
else
ddecho "${TAB}not called by ${SHELL##*/}"
# get the line where the function is defined
local line_func_def=$(find_func_line "${func}" "${sour}"); # 2>/dev/null
fi
if [[ "${bottom}" == "main" ]] || [[ "${bottom}" == "source" ]]; then
ddecho "${TAB}BASH_LINENO refers to file"
local -i call_line=$func_line
# print file line
in_line "$@"
echo -en "${FUNCNAME}() $(lec_mes)${call_line}${fcol} "
echo -e "in file ${fcol}${YELLOW}${sour_fil}${fcol}"
else
ddecho "${TAB}BASH_LINENO refers to function"
# get the line in the file where the function is called
# add the line where to the function is defined within the file and the
# line within the function
local -i call_line=$(($line_func_def + $func_line -1 ))
# print file line
in_line "$@"
echo -en "${FUNCNAME}() $(lec_mes)${call_line}${fcol} "
echo -e "in file ${YELLOW}${sour_fil}${fcol}"
itab
# print function line
ddecho -n "${TAB}$(lec_mes)${func_line} "
ddecho "in function ${func}()"
dtab
echo -en "${RESET}"
fi
itab
# print definition line
ddecho -n "${TAB}function ${func}() "
ddecho -ne "defined on line ${GRL}${line_func_def}${fcol} "
ddecho -e "in file ${YELLOW}${sour_fil}${fcol}"
ddecho -e "${TAB}file ${YELLOW}${sour_fil}${fcol} located in ${DIR}${sour_dir}${fcol}"
ddecho "${TAB}exiting ${FUNCNAME}() on line $((${line_def}+${LINENO}-1))..."
dtab
if [ ${DEBUG:-0} -gt 2 ]; then
print_stack
fi
# reset shell options
reset_shell ${old_opts}
}
# parent line echo
function plecho() {
# save shell options
old_opts=$(echo "$-")
# set local debug level
local -i DEBUG=${DEBUG:-1}
DEBUG=0
# DEBUG=${libDEBUG:-0}
set_fcol
start_new_line
echo -ne "${fcol}"
dddecho -e "${TAB}${INVERT}${FUNCNAME}${fcol}"
set -u
if [ ${DEBUG:-0} -gt 0 ] || [ ! -z "$@" ] ; then
hline
trap 'echo -ne "${fcol}";hline;echo -en "$RESET";trap -- RETURN' RETURN
fi
if [ ${DEBUG:-0} -gt 0 ]; then
print_stack
fi
# get the lenght of the execution stack
local -i N_BASH=${#BASH_SOURCE[@]}
ddecho "${TAB}there are ${N_BASH} entries in the ${FUNCNAME}() call stack"
# get the line where THIS function is defined
line_def=$(find_func_line "${FUNCNAME}" "${BASH_SOURCE}"); # 2>/dev/null
if [ $N_BASH -eq 1 ]; then
in_line "$@"
echo -e "${FUNCNAME}() $(lec_mes)${BASH_LINENO}${fcol} from ${BASH##*/}"
ddecho "${TAB}exiting ${FUNCNAME}() on line $((${line_def}+${LINENO}-1))..."
return 0
else
# define ofset
local -i N_off
if [ $N_BASH -ge 3 ]; then
N_off=2;
ddecho "${TAB}showing parent line"
else
N_off=1;
decho "${TAB}showing regular lecho results"
fi
if [ $N_BASH -ge $N_off ]; then
ddecho "${TAB}N_BASH = ${N_BASH}"
ddecho "${TAB} N_off = ${N_off}"
N_BASH=$(($N_BASH - $N_off))
ddecho "${TAB}N_BASH = ${N_BASH}"
fi
ddecho "${TAB}there are ${N_BASH} entries in the (parent) call stack"
ddecho "${TAB} FUNCNAME stack = ${FUNCNAME[@]}"
ddecho "${TAB}BASH_SOURCE stack = ${BASH_SOURCE[@]##*/}"
ddecho "${TAB}BASH_LINENO stack = ${BASH_LINENO[@]}"
fi
# get the file of the calling function
local sour=${BASH_SOURCE[(($N_off))]}
local sour_dir=$(dirname "${sour}")
local sour_fil=$(basename "${sour}")
ddecho "${TAB}source is ${sour}"
ddecho "${TAB}source is ${sour_fil} in ${sour_dir}"
ddecho "${TAB}${FUNCNAME}"
ddecho "${TAB}FUNCNAME stack = ${FUNCNAME[@]}"
ddecho "${TAB}N_BASH = ${N_BASH}"
itab
local -i func_lev=1
if [ $N_BASH -gt 1 ]; then
func_lev=$(($N_BASH + ${N_off}))
fi
decho "${TAB}function level = $func_lev"
decho "${TAB}function : ${FUNCNAME[$func_lev]}"
dtab
# get the line of of the calling function
local -i func_line=${BASH_LINENO[0]}
# BASH_SOURCE counts from zero; get the bottom of the stack
local bottom=${FUNCNAME[${func_lev}]##*/}
ddecho "${TAB}bottom of stack is $bottom"
# get the calling function
local func=${FUNCNAME[${func_lev}]}
ddecho "${TAB}calling function is $func"
if [[ "${func}" == "main" ]] || [[ "${func}" == "source" ]]; then
# the function is call from bash
ddecho -e "${TAB}called by ${SHELL##*/}"
ddecho "${TAB}N_BASH = ${N_BASH}"
ddecho "${TAB}function level = $func_lev"
in_line "$@"
echo -e "${FUNCNAME[(($func_lev-1))]}() $(lec_mes)${BASH_LINENO[(($func_lev-1))]}${fcol} in file ${YELLOW}${BASH_SOURCE[$func_lev]##*/}${RESET}"
itab
((--func_lev))
ddecho -e "${TAB}${BASH_SOURCE[$func_lev]##*/}${RESET} called by ${SHELL##*/}"
ddecho -e "${TAB}${FUNCNAME[(($func_lev-1))]}() $(lec_mes)${BASH_LINENO[(($func_lev-1))]}${fcol} in file ${YELLOW}${BASH_SOURCE[$func_lev]##*/}${RESET}"
decho "${TAB}exiting ${FUNCNAME}() on line $((${line_def}+${LINENO}-1))..."
dtab
return 0
else
ddecho "${TAB}not called by ${SHELL##*/}"
# get the line where the function is defined
local line_func_def=$(find_func_line "${func}" "${sour}"); # 2>/dev/null
fi
if [[ "${bottom}" == "main" ]] || [[ "${bottom}" == "source" ]]; then
ddecho "${TAB}BASH_LINENO refers to file"
local -i call_line=$func_line
# print file line
in_line "$@"
echo -en "${FUNCNAME}() $(lec_mes)${call_line}${fcol} "
echo -e "in file ${fcol}${YELLOW}${sour_fil}${fcol}"
else
ddecho "${TAB}BASH_LINENO refers to function"
# get the line in the file where the function is called
# add the line where to the function is defined within the file and the
# line within the function
local -i call_line=$(($line_func_def + $func_line -1 ))
# print file line
in_line "$@"
echo -en "${FUNCNAME}() $(lec_mes)${call_line}${fcol} "
echo -e "in file ${YELLOW}${sour_fil}${fcol}"
itab
# print function line
ddecho -n "${TAB}$(lec_mes)${func_line} "
ddecho "in function ${func}()"
dtab
echo -en "${RESET}"
fi
itab
# print definition line
ddecho -n "${TAB}function ${func}() "
ddecho -ne "defined on line ${GRL}${line_func_def}${fcol} "
ddecho -e "in file ${YELLOW}${sour_fil}${fcol}"
ddecho -e "${TAB}file ${YELLOW}${sour_fil}${fcol} located in ${DIR}${sour_dir}${fcol}"
ddecho "${TAB}exiting ${FUNCNAME}() on line $((${line_def}+${LINENO}-1))..."
dtab
if [ ${DEBUG:-0} -gt 2 ]; then
print_stack
fi
# reset shell options
reset_shell ${old_opts}
}
function print_debug() {
set -u
funcDEBUG=0
# check if DEBUG is unset
if [ -z ${DEBUG+dummy} ]; then
echo -e "${TAB}${BOLD}DEBUG is ${UNSET}"
return 0
fi
# check if DEBUG is set but NULL
if [ -z ${DEBUG:+dummy} ]; then
echo -e "${TAB}${BOLD}DEBUG is ${NULL}"
return 0
fi
# check if DEBUG is zero
if [ $DEBUG -eq 0 ]; then
echo -e "${TAB}${GRAY}DEBUG is 0${RESET}"
return 0
fi
# constuct debug print function name
local -i i
local prefix=$(for ((i = 1; i <= $DEBUG; i++)); do echo -n "d"; done)
fecho "DEBUG = $DEBUG"
fecho "prefix: $prefix"
local fun_name="${prefix}echo"
fecho "function: $fun_name"
# check if function is defined
if command -v $fun_name &>/dev/null; then
fecho "${fun_name} is defined"
else
fecho "${fun_name}() is NOT defined"
fecho "defining ${fun_name}()..."
eval "${fun_name}() { xecho \"\$@\"; }"
fi
$fun_name "${TAB}DEBUG = $DEBUG"
if [[ "$-" == *u* ]]; then
set +u
fi
}
function cdb() {
funcDEBUG=0
fecho "clearing DEBUG..."
export DEBUG=''
print_debug
}
function rdb() {
funcDEBUG=0
fecho "resetting DEBUG..."
export DEBUG=0
print_debug
}
function idb() {
set -u
funcDEBUG=0
# check if DEBUG is unset
if [ -z ${DEBUG+dummy} ]; then
fecho -e "${BOLD}DEBUG is ${UNSET}"
fecho "setting DEBUG..."
export DEBUG=
fi
# check if DEBUG is set but NULL
if [ -z ${DEBUG:+dummy} ]; then
fecho -e "${BOLD}DEBUG is ${NULL}"
fecho "setting DEBUG to zero..."
unset DEBUG
declare -igx DEBUG=0
fi
# check if DEBUG is a number
local num='^[0-9]+$'
fecho -e "DEBUG = ${DEBUG}"
if [[ "$DEBUG" =~ $num ]]; then
fecho "DEBUG is a number"
else
fecho "DEBUG is not a number"
fecho "setting DEBUG to zero..."
unset DEBUG
declare -igx DEBUG=0
fi
# determine how many iteration to run
local -i N
if [ $# -eq 0 ]; then
N=1
else
N=$1
fi
fecho "incrementing DEBUG by $N..."
# increment DEBUG by N
local -i i
for ((i = 1; i <= $N; i++)); do
((++DEBUG))
done
export DEBUG
fecho -e "DEBUG = ${DEBUG}"
if [ $funcDEBUG -eq 0 ];then
start_new_line
print_debug
fi
if [[ "$-" == *u* ]]; then
set +u
fi
}
function ddb() {
set -u
funcDEBUG=0
# check if DEBUG is unset
if [ -z ${DEBUG+dummy} ]; then
fecho -e "${BOLD}DEBUG is ${UNSET}"
fecho "setting DEBUG..."
export DEBUG=
fi
# check if DEBUG is set but NULL
if [ -z ${DEBUG:+dummy} ]; then
fecho -e "${BOLD}DEBUG is ${NULL}"
fecho "setting DEBUG to zero..."
unset DEBUG
declare -igx DEBUG=0
fi
# check if DEBUG is a number
local num='^[0-9]+$'
fecho -e "DEBUG = ${DEBUG}"
if [[ "$DEBUG" =~ $num ]]; then
fecho "DEBUG is a number"
else
fecho "DEBUG is not a number"
fecho "setting DEBUG to zero..."
unset DEBUG
declare -igx DEBUG=0
fi
# check if DEBUG is zero
if [ $DEBUG -eq 0 ]; then
fecho "no action needed"
if [ $funcDEBUG -eq 0 ];then
start_new_line
print_debug
fi
return 0
fi
# determine how many iteration to run
local -i i
local -i N
if [ $# -eq 0 ]; then
N=1
else
N=$1
fi
if [ $N -gt $DEBUG ]; then
fecho "DEBUG level exceeded"
fecho "resetting DEBUG to zero..."
DEBUG=0
else
# increment DEBUG by N
fecho "decrementing DEBUG by $N"
for ((i = 1; i <= $N; i++)); do
((DEBUG--))
done
fi
export DEBUG
fecho -e "DEBUG = ${DEBUG}"
if [ $funcDEBUG -eq 0 ];then
start_new_line
print_debug
fi
if [[ "$-" == *u* ]]; then
set +u
fi
}
function print_() {
set -u
local -i DEBUG=0
DEBUG=${libDEBUG:-0}
# this function modifies the value of TAB, so save the value if it is
# included in the arguments
if [[ "$@" =~ "TAB" ]]; then
decho -e "${TAB-}${YELLOW}argument contain TAB${RESET}"
# check if TAB is set
if [ -z ${TAB+dummy} ]; then
decho -e "TAB is $UNSET"
unset TAB_input
else
decho "${TAB}TAB is set"
local TAB_input=$TAB
fi
fi
# set tab
set_tab_shell
# parse inputs
if [ $# -eq 0 ]; then
echo "${TAB}no input received"
echo "${TAB}possibilities:"
itab
echo "${TAB}- no argument given"
echo "${TAB}- value passed (e.g. \$VB) instead of name (e.g. VB) and:"
itab
echo -e "${TAB}- input is ${UNSET}"
echo -e "${TAB}- input is ${NULL}"
dtab
if ! (return 0 2>/dev/null); then
echo "${TAB}- input is not exported"
fi
dtab
return 1
else
decho -n "${TAB}argument"
if [ $# -gt 1 ]; then
decho -n "s"
fi
decho ": $@ "
input=$@
fi
if [ $# -gt 1 ]; then