Skip to content

Commit 0daa6db

Browse files
author
Andy C
committed
[builtin/declare] Implement shopt -s extdebug format for declare -F
- Document it - Tested with bin/osh build/py.sh - this now works! task-five.sh uses extdebug.
1 parent 513bf99 commit 0daa6db

File tree

8 files changed

+100
-12
lines changed

8 files changed

+100
-12
lines changed

build/py.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ show-cpython-patches() {
453453
git diff 1d2b97384e14d65b1241f67fd995277f5508db28..HEAD Python-2.7.13/
454454
}
455455

456-
if test $(basename $0) = 'py.sh'; then
456+
name=$(basename $0)
457+
if test "$name" = 'py.sh'; then
457458
task-five "$@"
458459
fi

builtin/assign_osh.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from core.error import e_usage
1717
from core import state
1818
from core import vm
19+
from display import ui
1920
from frontend import flag_util
2021
from frontend import args
2122
from mycpp.mylib import log, NewDict
@@ -27,7 +28,6 @@
2728
if TYPE_CHECKING:
2829
from core.state import Mem
2930
from core import optview
30-
from display import ui
3131
from frontend.args import _Attributes
3232

3333
_ = log
@@ -353,8 +353,17 @@ def _PrintFuncs(self, names):
353353
# type: (List[str]) -> int
354354
status = 0
355355
for name in names:
356-
if self.procs.GetShellFunc(name):
357-
print(name)
356+
proc_val = self.procs.GetShellFunc(name)
357+
if proc_val:
358+
if self.exec_opts.extdebug():
359+
tok = proc_val.name_tok
360+
assert tok is not None, tok
361+
assert tok.line is not None, tok.line
362+
filename_str = ui.GetFilenameString(tok.line)
363+
line = '%s %s %s' % (name, tok.line.line_num, filename_str)
364+
print(line)
365+
else:
366+
print(name)
358367
# TODO: Could print LST for -f, or render LST. Bash does this. 'trap'
359368
# could use that too.
360369
else:

display/ui.py

+34
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,40 @@ def _PrintTokenTooLong(loc_tok, f):
154154
f.write(buf.getvalue())
155155

156156

157+
def GetFilenameString(line):
158+
# type: (SourceLine) -> str
159+
"""Get the path of the file that a line appears in.
160+
161+
Returns "main" if it's stdin or -c
162+
Returns "?" if it's not in a file.
163+
164+
Used by declare -F, with shopt -s extdebug.
165+
"""
166+
src = line.src
167+
UP_src = src
168+
169+
filename_str = '?' # default
170+
with tagswitch(src) as case:
171+
# Copying bash, it uses the string 'main'.
172+
# I think ? would be better here, because this can get confused with a
173+
# file 'main'. But it's fine for our task file usage.
174+
if case(source_e.CFlag):
175+
filename_str = 'main'
176+
elif case(source_e.Stdin):
177+
filename_str = 'main'
178+
179+
elif case(source_e.MainFile):
180+
src = cast(source.MainFile, UP_src)
181+
filename_str = src.path
182+
elif case(source_e.OtherFile):
183+
src = cast(source.OtherFile, UP_src)
184+
filename_str = src.path
185+
186+
else:
187+
pass
188+
return filename_str
189+
190+
157191
def GetLineSourceString(line, quote_filename=False):
158192
# type: (SourceLine, bool) -> str
159193
"""Returns a human-readable string for dev tools.

doc/ref/chap-option.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,28 @@ called `-rf`.
8989

9090
## Debugging
9191

92-
These options are from POSIX shell:
92+
<h3 id="xtrace">xtrace (-x)</h3>
9393

94-
xtrace verbose
94+
Show execution traces.
9595

96-
From bash:
96+
- In OSH, the [PS4][] variables control the display.
97+
- In YSH, the `SHX_*` variables control the display.
98+
99+
[PS4]: chap-special-var.html#PS4
100+
101+
This option is also `set -x`. It's required by POSIX shell.
102+
103+
### verbose
104+
105+
Not implemented.
106+
107+
This option is from POSIX shell.
108+
109+
110+
### extdebug
97111

98-
extdebug
112+
Show more info in when printing functions with `declare -f`. Used by
113+
`task-five.sh`.
99114

100115
## Interactive
101116

doc/ref/toc-osh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ X [Unsupported] enable
187187
[Errors] nounset -u errexit -e inherit_errexit pipefail
188188
[Globbing] noglob -f nullglob failglob X dotglob
189189
dashglob (true)
190-
[Other Option] noclobber -C errtrace -E
191-
[Debugging] xtrace X verbose X extdebug
190+
[Other Option] noclobber -C errtrace -E
191+
[Debugging] xtrace -x X verbose extdebug
192192
[Interactive] emacs vi
193193
[Compat] eval_unsafe_arith ignore_flags_not_impl
194194
ignore_shopt_not_impl

frontend/option_def.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ def DoneWithImplementedOptions(self):
218218
'direxpand',
219219
'dirspell',
220220
'execfail',
221-
'extdebug', # for --debugger?
222221
'extquote',
223222
'force_fignore',
224223
'globasciiranges',
@@ -286,6 +285,8 @@ def _Init(opt_def):
286285
opt_def.Add('nocasematch')
287286
opt_def.Add('dotglob')
288287

288+
opt_def.Add('extdebug') # for task files
289+
289290
# recursive parsing and evaluation - for compatibility, ble.sh, etc.
290291
opt_def.Add('eval_unsafe_arith')
291292

spec/assign-extended.test.sh

+17-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,23 @@ declare -f g
102102
add 7 main
103103
g 3 ROOT/spec/testdata/bash-source-2.sh
104104
## END
105-
## N-I mksh stdout-json: ""
105+
## N-I mksh STDOUT:
106+
## END
107+
108+
#### declare -F with shopt -s extdebug and main file
109+
case $SH in mksh) exit ;; esac
110+
111+
$SH $REPO_ROOT/spec/testdata/extdebug.sh | sed "s;$REPO_ROOT;ROOT;g"
112+
113+
## STDOUT:
114+
declare -f add
115+
declare -f g
116+
117+
add 5 ROOT/spec/testdata/extdebug.sh
118+
g 3 ROOT/spec/testdata/bash-source-2.sh
119+
## END
120+
## N-I mksh STDOUT:
121+
## END
106122

107123
#### declare -p var (exit status)
108124
var1() { echo func; } # function names are NOT found.

spec/testdata/extdebug.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
source $REPO_ROOT/spec/testdata/bash-source-2.sh
2+
3+
shopt -s extdebug
4+
5+
add () { expr 4 + 4; }
6+
7+
declare -F
8+
echo
9+
10+
declare -F add
11+
# in bash-source-2
12+
declare -F g | sed "s;$REPO_ROOT;ROOT;g"

0 commit comments

Comments
 (0)