1 #
2 # Test call stack introspection. There are a bunch of special variables
3 # defined here:
4 #
5 # https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
6 #
7 # - The shell function ${FUNCNAME[$i]} is defined in the file
8 # ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}
9 #
10 # - ${BASH_LINENO[$i]} is the line number in the source file
11 # (${BASH_SOURCE[$i+1]}) where ${FUNCNAME[$i]} was called (or
12 # ${BASH_LINENO[$i-1]} if referenced within another shell function).
13 #
14 # - For instance, ${FUNCNAME[$i]} was called from the file
15 # ${BASH_SOURCE[$i+1]} at line number ${BASH_LINENO[$i]}. The caller builtin
16 # displays the current call stack using this information.
17 #
18 # So ${BASH_SOURCE[@]} doesn't line up with ${BASH_LINENO}. But
19 # ${BASH_SOURCE[0]} does line up with $LINENO!
20 #
21 # Geez.
22 #
23 # In other words, BASH_SOURCE is about the DEFINITION. While FUNCNAME and
24 # BASH_LINENO are about the CALL.
25
26
27 #### ${FUNCNAME[@]} array
28 g() {
29 argv.py "${FUNCNAME[@]}"
30 }
31 f() {
32 argv.py "${FUNCNAME[@]}"
33 g
34 argv.py "${FUNCNAME[@]}"
35 }
36 f
37 ## STDOUT:
38 ['f']
39 ['g', 'f']
40 ['f']
41 ## END
42
43 #### FUNCNAME with source (scalar or array)
44 cd $REPO_ROOT
45
46 # Comments on bash quirk:
47 # https://github.com/oilshell/oil/pull/656#issuecomment-599162211
48
49 f() {
50 . spec/testdata/echo-funcname.sh
51 }
52 g() {
53 f
54 }
55
56 g
57 echo -----
58
59 . spec/testdata/echo-funcname.sh
60 echo -----
61
62 argv.py "${FUNCNAME[@]}"
63
64 # Show bash inconsistency. FUNCNAME doesn't behave like a normal array.
65 case $SH in
66 (bash)
67 echo -----
68 a=('A')
69 argv.py ' @' "${a[@]}"
70 argv.py ' 0' "${a[0]}"
71 argv.py '${}' "${a}"
72 argv.py ' $' "$a"
73 ;;
74 esac
75
76 ## STDOUT:
77 [' @', 'source', 'f', 'g']
78 [' 0', 'source']
79 ['${}', 'source']
80 [' $', 'source']
81 -----
82 [' @', 'source']
83 [' 0', 'source']
84 ['${}', 'source']
85 [' $', 'source']
86 -----
87 []
88 ## END
89 ## BUG bash STDOUT:
90 [' @', 'source', 'f', 'g']
91 [' 0', 'source']
92 ['${}', 'source']
93 [' $', 'source']
94 -----
95 [' @', 'source']
96 [' 0', 'source']
97 ['${}', '']
98 [' $', '']
99 -----
100 []
101 -----
102 [' @', 'A']
103 [' 0', 'A']
104 ['${}', 'A']
105 [' $', 'A']
106 ## END
107
108
109 #### BASH_SOURCE and BASH_LINENO scalar or array (e.g. for virtualenv)
110 cd $REPO_ROOT
111
112 # https://github.com/pypa/virtualenv/blob/master/virtualenv_embedded/activate.sh
113 # https://github.com/akinomyoga/ble.sh/blob/6f6c2e5/ble.pp#L374
114
115 argv.py "$BASH_SOURCE" # SimpleVarSub
116 argv.py "${BASH_SOURCE}" # BracedVarSub
117 argv.py "$BASH_LINENO" # SimpleVarSub
118 argv.py "${BASH_LINENO}" # BracedVarSub
119 argv.py "$FUNCNAME" # SimpleVarSub
120 argv.py "${FUNCNAME}" # BracedVarSub
121 echo __
122 source spec/testdata/bash-source-string.sh
123
124 ## STDOUT:
125 ['']
126 ['']
127 ['']
128 ['']
129 ['']
130 ['']
131 __
132 ['spec/testdata/bash-source-string.sh']
133 ['spec/testdata/bash-source-string.sh']
134 ['11']
135 ['11']
136 ____
137 ['spec/testdata/bash-source-string2.sh']
138 ['spec/testdata/bash-source-string2.sh']
139 ['11']
140 ['11']
141 ## END
142
143
144 #### ${FUNCNAME} with prefix/suffix operators
145
146 check() {
147 argv.py "${#FUNCNAME}"
148 argv.py "${FUNCNAME::1}"
149 argv.py "${FUNCNAME:1}"
150 }
151 check
152 ## STDOUT:
153 ['5']
154 ['c']
155 ['heck']
156 ## END
157
158 #### operators on FUNCNAME
159 check() {
160 argv.py "${FUNCNAME}"
161 argv.py "${#FUNCNAME}"
162 argv.py "${FUNCNAME::1}"
163 argv.py "${FUNCNAME:1}"
164 }
165 check
166 ## status: 0
167 ## STDOUT:
168 ['check']
169 ['5']
170 ['c']
171 ['heck']
172 ## END
173
174 #### ${FUNCNAME} and "set -u" (OSH regression)
175 set -u
176 argv.py "$FUNCNAME"
177 ## status: 1
178 ## stdout-json: ""
179
180 #### $((BASH_LINENO)) (scalar form in arith)
181 check() {
182 echo $((BASH_LINENO))
183 }
184 check
185 ## stdout: 4
186
187 #### ${BASH_SOURCE[@]} with source and function name
188 cd $REPO_ROOT
189
190 argv.py "${BASH_SOURCE[@]}"
191 source spec/testdata/bash-source-simple.sh
192 f
193 ## STDOUT:
194 []
195 ['spec/testdata/bash-source-simple.sh']
196 ['spec/testdata/bash-source-simple.sh']
197 ## END
198
199 #### ${BASH_SOURCE[@]} with line numbers
200 cd $REPO_ROOT
201
202 $SH spec/testdata/bash-source.sh
203 ## STDOUT:
204 ['begin F funcs', 'f', 'main']
205 ['begin F files', 'spec/testdata/bash-source.sh', 'spec/testdata/bash-source.sh']
206 ['begin F lines', '21', '0']
207 ['G funcs', 'g', 'f', 'main']
208 ['G files', 'spec/testdata/bash-source-2.sh', 'spec/testdata/bash-source.sh', 'spec/testdata/bash-source.sh']
209 ['G lines', '15', '21', '0']
210 ['end F funcs', 'f', 'main']
211 ['end F', 'spec/testdata/bash-source.sh', 'spec/testdata/bash-source.sh']
212 ['end F lines', '21', '0']
213 ## END
214
215 #### ${BASH_LINENO[@]} is a stack of line numbers for function calls
216 # note: it's CALLS, not DEFINITIONS.
217 g() {
218 argv.py G "${BASH_LINENO[@]}"
219 }
220 f() {
221 argv.py 'begin F' "${BASH_LINENO[@]}"
222 g # line 6
223 argv.py 'end F' "${BASH_LINENO[@]}"
224 }
225 argv.py ${BASH_LINENO[@]}
226 f # line 9
227 ## STDOUT:
228 []
229 ['begin F', '10']
230 ['G', '6', '10']
231 ['end F', '10']
232 ## END