-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_stack.sh
More file actions
525 lines (456 loc) · 15 KB
/
lib_stack.sh
File metadata and controls
525 lines (456 loc) · 15 KB
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
#!/bin/bash -eu
# -----------------------------------------------------------------------------
# EXECUTION CALL STACK LIBRARY
# -----------------------------------------------------------------------------
#
# ~/config/lib_stack.sh
#
# PURPOSE: define fuctions to print the contents of the execution stack.
#
# May 2024 JCL
#
# -----------------------------------------------------------------------------
function get_run_type() {
# set local debug value
if [ $# -eq 1 ]; then
# use argument to manually set DEBUG
local -i DEBUG=$1
else
# substitute default value if DEBUG is unset or null
local -i DEBUG=${DEBUG:-2}
fi
# manual
#local -i DEBUG=0
local -i N_BOTTOM=$((${#FUNCNAME[@]} - 1))
ddecho "N_BOTTOM = $N_BOTTOM"
decho "${TAB}FUNCNAME[$N_BOTTOM] = ${FUNCNAME[$N_BOTTOM]}"
if [[ "${FUNCNAME[$N_BOTTOM]}" =~ "main" ]]; then
decho "bottom of stack is main"
decho "root invocation ${BASH_SOURCE[$N_BOTTOM]} is a script"
RUN_TYPE="executing"
export IS_SOURCED=false
else
if [[ "${FUNCNAME[$N_BOTTOM]}" =~ "source" ]]; then
decho "${TAB}bottom of stack is source"
decho "${TAB}root invocation ${BASH_SOURCE[$N_BOTTOM]} is... sourced?"
else
decho "${TAB}bottom of stack is not main: ${FUNCNAME[$N_BOTTOM]}"
decho "${TAB}root invocation ${BASH_SOURCE[$N_BOTTOM]} is a shell function"
fi
RUN_TYPE="sourcing"
export IS_SOURCED=true
fi
# set default value
RUN_TYPE=${RUN_TYPE:-"running"}
}
function print_source() {
local -i DEBUG
# set local debug value
if [ $# -eq 1 ]; then
# use argument to manually set DEBUG
DEBUG=$1
else
# substitute default value if DEBUG is unset or null
DEBUG=${DEBUG:-2}
# set manually
DEBUG=0
fi
local -i funcDEBUG=$((${DEBUG:-1} - 1))
ddecho -e "${TAB}${INVERT}${FUNCNAME}${RESET}"
fecho "DEBUG = $DEBUG"
get_source
# define default run type
get_run_type
# set tab
if [[ "${RUN_TYPE}" =~ "sourcing" ]]; then
set_tab
decho -e "${TAB}${DIM}${FUNCNAME}${NORMAL}: ${RUN_TYPE}: reducing tab..."
dtab
else
set_tab_shell
fi
export TAB
# conditionally add stack level prefix
if [ ${DEBUG:-0} -gt 1 ]; then
# since print_source is itself part of the stack, remove the top of the
# stack
((N_BASH--))
local prefix="\x1b[7;38;5;132m${N_BASH}"
if [[ "$-" == *i* ]]; then
prefix+="i"
fi
prefix+="\x1b[0m"
RUN_TYPE="${prefix} ${RUN_TYPE}"
fi
# strip escapes from run type
local msgne
strip_pretty msgne $RUN_TYPE
# get message length
local -i lnr=${#msgne}
# define prefix prototype
prefix="${VALID}link${RESET} -> "
strip_pretty msgne $prefix
local -i lnp=${#msgne}
# subtract link/path prefix length
local -i sp=$(($lnr - $lnp))
fecho "space: $lnr, $lnp, $sp"
# if run type is shorter than prefix, adjust
if [ $sp -le 0 ]; then
fecho "space: $lnr, $lnp, $sp"
if [ $sp -eq 0 ]; then
sp=-1
else
for ((i = $sp; i < 0; i++)); do
RUN_TYPE=" $RUN_TYPE"
fecho "$RUN_TYPE: $i"
done
fi
fi
# define indent
local spx=$(echo -ne "\E[${sp}C")
# print run type and source name
vecho -e "${TAB}${RUN_TYPE} ${PSDIR}${BASH_SOURCE[1]}${RESET}..."
if [ ! "${BASH_SOURCE[1]}" = "$src_name" ]; then
vecho -e "${TAB}${spx}${prefix}$src_name"
fi
if [ ${DEBUG:-0} -gt 1 ]; then
# print source path
if [[ "${src_dir_logi}" != "${src_dir_phys}" ]]; then
## logical
vecho -e "${TAB}${spx}${GRAY}logi -> $src_dir_logi${RESET}"
## physical
vecho -e "${TAB}${spx}${GRAY}phys -> $src_dir_phys${RESET}"
fi
fi
# deallocate variables
unset prefix
}
# stack echo
function secho() {
print_stack 2
}
function print_ribbon() {
get_source
# since this function is part of the stack, reduce N_BASH by one
((N_BASH--))
local decor="${N_BASH}"
if [[ "$-" == *i* ]]; then
: #decor+="i"
fi
# print source
decho -e "${TAB}\x1b[7;38;5;132m${decor}: ${src_base} :${decor}\x1b[0m"
}
function print_bar() {
get_source
# define message
msg=$(echo "this file is ${src_base}!")
# define line
ln=$(for ((i = 1; i <= ${#msg}; i++)); do echo -n "-"; done)
# print source
decho -e "$ln\n$msg\n$ln" | sed "$ ! s/^/${TAB}/"
}
function print_stack() {
# set local debug value
if [ $# -eq 1 ]; then
# use argument to manually set DEBUG
local -i DEBUG=$1
else
# substitute default value if DEBUG is unset or null
local -i DEBUG=${DEBUG:-1}
fi
start_new_line
[ $DEBUG -gt 0 ] && (decho -n "${TAB}${FUNCNAME}: "; print_debug | sed 's/^ *//' )
# initialize variables
unset N_FUNC
unset N_BASH
unset N_LINE
# get length of function stack
local -gi N_FUNC=${#FUNCNAME[@]}
local -gi N_BASH=${#BASH_SOURCE[@]}
local -gi N_LINE=${#BASH_LINENO[@]}
# get color index
local -i idx
dbg2idx $N_BASH idx
# set color
echo -ne "${dcolor[idx]}"
# print length of stack
echo "${TAB}There are N=$N_FUNC entries in the execution call stack"
N_STACK=$N_FUNC
# check that all stacks have the same length
if [ $N_FUNC -ne $N_BASH ]; then
echo "${TAB}There are N=$N_BASH entries in the source file name stack"
if [ $N_BASH -gt $N_STACK ]; then
N_STACK=$N_BASH
fi
fi
if [ $N_FUNC -ne $N_LINE ]; then
echo "${TAB}There are N=$N_LINE entries in the line number stack"
fi
if [ $N_FUNC -ne $N_BASH ] || [ $N_FUNC -ne $N_LINE ]; then
echo "${TAB}${N_FUNC} functions, ${N_BASH} files, ${N_LINE} lines"
if [ $N_LINE -gt $N_STACK ]; then
N_STACK=$N_LINE
fi
fi
local -ga BASH_CANON
# resolve symbolic links (canonicalize)
for ((i = 0; i < $N_BASH; i++)); do
BASH_CANON[$i]="$(readlink -f "${BASH_SOURCE[$i]}")"
done
local -ga BASH_FNAME
# strip directories
for ((i = 0; i < $N_BASH; i++)); do
BASH_FNAME[$i]=${BASH_SOURCE[$i]##*/}
done
# get directories (logical, link names possible)
local -ga BASH_DIR
for ((i = 0; i < $N_BASH; i++)); do
BASH_DIR[$i]="$(dirname "${BASH_SOURCE[$i]}")"
done
# get directories (physical, canonical)
local -ga BASH_CANON_DIR
for ((i = 0; i < $N_BASH; i++)); do
BASH_CANON_DIR[$i]="$(dirname "${BASH_CANON[$i]}")"
done
# print call stack
echo "${TAB}call stack:"
local -i i
local -i MAX_DIR_LEN=50
itab
(
for ((i = 0; i < $N_STACK ; i++)); do
if [ $i == 0 ]; then
j="-"
else
j=$(($i-1))
fi
# print stack element
local dir="${BASH_DIR[i]}"
if [ ${#dir} -gt $MAX_DIR_LEN ]; then
local start="${dir:0:$(( (MAX_DIR_LEN - 3) / 2 ))}"
local end="${dir: -$(( (MAX_DIR_LEN - 3) / 2 ))}"
dir="${start}...${end}"
fi
echo "$j:$i:${FUNCNAME[i]}:$dir:${BASH_FNAME[i]}:${BASH_LINENO[i]}"
# check if source is linked
if [[ "${BASH_SOURCE[$i]}" != "${BASH_CANON[$i]}" ]] && [ $DEBUG -gt 0 ]; then
# set color
((idx++))
#echo -ne "${dcolor[idx]}"
# print link
local canon_dir="${BASH_CANON_DIR[i]}"
if [ ${#canon_dir} -gt $MAX_DIR_LEN ]; then
local start="${canon_dir:0:$(( (MAX_DIR_LEN - 3) / 2 ))}"
local end="${canon_dir: -$(( (MAX_DIR_LEN - 3) / 2 ))}"
canon_dir="${start}...${end}"
fi
echo -n "$j:$i:${FUNCNAME[i]}:$canon_dir:${BASH_CANON[i]##*/}:${BASH_LINENO[i]}"
# reset color
((idx--))
echo -e "${dcolor[idx]}"
fi
done
) | column -t -s: -N "index,index,function,directory,source,line no" -R1 | sed "s/^/${TAB}/"
dtab
# unset color
echo -ne "\e[0m"
}
# print function stack
function print_stack_devel() {
local -ir DEBUG=2
print_stack
echo "BASH_ARGC = $BASH_ARGC"
echo "BASH_ARGV = $BASH_ARGV"
echo "BASH_COMMAND = $BASH_COMMAND"
echo "BASH_SUBSHELL = $BASH_SUBSHELL"
echo "${TAB}list of invocations (links):"
(
if [ $N_BASH -gt 1 ]; then
for ((i = 1; i < $N_BASH; i++)); do
vecho "$((i - 1)):+${BASH_SOURCE[$((i - 1))]}+invoked by+${BASH_SOURCE[$i]}"
done
else
called_by=$(ps -o comm= $PPID)
echo "0:+${BASH_SOURCE[0]}+invoked by+${called_by}"
fi
) | column -t -s + -o " " | sed "s,${BASH_SOURCE[0]},\x1b[1;36m&\x1b[0m,;s,${BASH_CANON[0]},\x1b[0;33m&\x1b[0m,;s/^/${TAB}${fTAB}/"
echo "${TAB}list of invocations (canonicalized):"
(
if [ $N_BASH -gt 1 ]; then
for ((i = 1; i < $N_BASH; i++)); do
vecho "$((i - 1)):+${BASH_CANON[$((i - 1))]}+invoked by+${BASH_CANON[$i]}"
done
else
called_by=$(ps -o comm= $PPID)
echo "0:+${BASH_CANON[0]}+invoked by+${called_by}"
fi
) | column -t -s + -o " " | sed "s,${BASH_SOURCE[0]},\x1b[1;36m&\x1b[0m,;s,${BASH_CANON[0]},\x1b[0;33m&\x1b[0m,;s/^/${TAB}${fTAB}/"
if [ $N_BASH -gt 1 ]; then
echo "${TAB}invoking source source:"
itab
echo "${TAB}BASH_SOURCE[1] = ${BASH_SOURCE[1]##*/}"
echo "${TAB}BASH_SOURCE[(($N_BASH-1))] = ${BASH_SOURCE[$N_BASH-1]##*/}"
dtab
fi
itab
# (
# for ((i = 0; i < $N_FUNC; i++)); do
# echo -n ":defined in: ${BASH_SOURCE[$i]##*/} "
# echo -n ":called by: "
# if [ -z "${BASH_SOURCE[$i+1]}" ]; then
# echo -n "NULL "
# else
# echo -n "${BASH_SOURCE[$i+1]##*/} "
# fi
# done
# ) | column -t -s : -o ""
dtab
if [ ${#FUNCNAME[@]} -gt 1 ]; then
echo "${TAB}FUNCNAME[$((N_FUNC-2))]=${FUNCNAME[$((N_FUNC-2))]}"
fi
# print_stack() should only be called from other functions, so the length of
# FUNCNAME should always be 2 or greater. Start with color 0
local -ir IDX=$(( N_FUNC - 2 ))
# set color
echo -ne "${dcolor[$IDX]}"
local -i x
local -i start
local -i stop
# define function stack printing limits
if [[ ${FUNCNAME[1]} == "xecho" ]]; then
# xecho
start=$(( $N_FUNC - 2 ))
stop=2
else
# function
start=$(( $N_FUNC - 5 ))
stop=0
fi
# print size of function stack
echo -ne "$N_FUNC"
# print debug type
if [[ ${FUNCNAME[1]} == "xecho" ]]; then
# xecho
echo -ne "x"
else
# function
echo -ne "f"
fi
if [[ ${FUNCNAME[$N_FUNC]} == "main" ]]; then
echo "main"
fi
for (( i=0; i<$N_FUNC; i++ )); do
echo "$i ${FUNCNAME[i]} defined in $(basename ${BASH_SOURCE[i]}) on line ${BASH_LINENO[i]}; and called from $(basename ${BASH_SOURCE[i+1]})"
done
# print contents of function stack...
echo -en "["
for (( x=(( $N_FUNC - 0 )); x>-1; x-- )); do
if [[ ${FUNCNAME[x]} == "xecho" ]]; then
break
fi
echo -en "$(basename ${BASH_SOURCE[x+1]} 2>/dev/null) ${BASH_LINENO[x]}: ${FUNCNAME[x]}\e[0m${dcolor[$IDX]} -> "
done
echo -en "$(basename ${BASH_SOURCE[x]}) ${BASH_LINENO[x]}] "
echo
# print contents of function stack...
echo -en "["
for (( x=$start; x>$stop; x-- )); do
echo -en "$(basename ${BASH_SOURCE[x+1]}) ${BASH_LINENO[x]}: ${FUNCNAME[x]}\e[0m${dcolor[$IDX]} -> "
done
echo -en "$(basename ${BASH_SOURCE[x]}) ${BASH_LINENO[x]}] "
}
function print_invo() {
local DEBUG=9
print_stack
# since print stack is itself part of the stack, remove the top of the stack
((N_FUNC--))
((N_BASH--))
((N_LINE--))
echo "${TAB}invocations:"
itab
for ((i = 0; i < (($N_FUNC -2)); i++)); do
echo "${TAB}$i: ${FUNCNAME[i]} ${BASH_FNAME[i]} ${BASH_LINENO[i+1]}"
done
dtab
echo "${TAB}shell function invocations:"
itab
for ((i = 0; i < (($N_FUNC - 1 )); i++)); do
echo "${TAB}$i: ${FUNCNAME[i]} ${BASH_FNAME[i]} ${BASH_LINENO[i+1]}"
done
dtab
echo "caller:"
caller
(
itab
for ((i = 0; i < (($N_FUNC + 1 )); i++)); do
echo -en "${TAB}$i\t"
caller $i
done
dtab
) | column -t -s "\t" -N "index,line,subroutine,filename" -R1
local -i N_BOTTOM=$(($N_FUNC - 1))
echo "N_BOTTOM = $N_BOTTOM"
echo "FUNCNAME[$N_BOTTOM] = ${F_BOTTOM}"
if [[ "${F_BOTTOM}" =~ "main" ]]; then
echo "bottom of stack is main"
echo "root invocation ${BASH_FNAME[$N_BOTTOM]} is a script"
else
echo "bottom of stack is not main: ${F_BOTTOM}"
echo "root invocation ${BASH_FNAME[$N_BOTTOM]} is a shell function"
fi
echo "${TAB}script invocations:"
itab
for ((i = 0; i < (($N_FUNC-1)) ; i++)); do
echo "${TAB}$i: ${FUNCNAME[i]} called from ${BASH_FNAME[i+1]} line ${BASH_LINENO[i]}"
done
dtab
echo "${TAB}shell function invocations:"
if [ $N_FUNC -gt 1 ]; then
echo "${FUNCNAME[0]} (defined in ${BASH_FNAME[0]}) called from ${FUNCNAME[1]} (defined in ${BASH_FNAME[1]}) line ${BASH_LINENO[0]}"
else
echo "${FUNCNAME[0]} (defined in ${BASH_FNAME[0]}) called from $SHELL line ${BASH_LINENO[0]}"
fi
itab
for ((i = 0; i < $N_FUNC ; i++)); do
echo "${TAB}$i: ${FUNCNAME[i]} defined in ${BASH_FNAME[i]} ${BASH_LINENO[i]}"
done
dtab
echo $LINENO
dtab
}
function print_shlvl() {
echo -e "${TAB}shell level = ${PSSH}$SHLVL${RESET}"
}
# source list of files
function source_list() {
local oldDEBUG=${DEBUG-0}
local DEBUG=0
if [ -z ${LIST:+dummy} ]; then
vecho "${TAB}LIST not defined"
return
fi
decho "${TAB}LIST defined"
itab
local iname
for iname in ${LIST[@]}; do
decho "${TAB}${iname}"
done
dtab
local fname
DEBUG=$oldDEBUG
for fname in ${LIST[@]}; do
vecho "${TAB}loading $fname..."
if [ -f $fname ]; then
source $fname
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
vecho -e "${TAB}$fname ${GOOD}OK${RESET}"
else
echo -e "${TAB}$fname ${BAD}FAIL${RESET} ${GRAY}RETVAL=$RETVAL${RESET}"
fi
else
echo -e "${TAB}$fname ${UL}not found${RESET}"
fi
done
unset LIST
}