Skip to content

Commit 3a45f99

Browse files
author
Andy C
committed
[osh] YSH expr sub no longer does dynamic globbing
In YSH, it's a moot point because of simple word evaluation. YSH never does dynamic globbing. In OSH, this behavior is more consistent with simple word evaluation. [refactor] Introduce word_.PieceQuoted() to make this clear.
1 parent fae2be9 commit 3a45f99

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

osh/word_.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from _devbuild.gen.id_kind_asdl import Id, Kind, Id_t, Kind_t
6+
from _devbuild.gen.runtime_asdl import Piece
67
from _devbuild.gen.syntax_asdl import (
78
Token,
89
CompoundWord,
@@ -29,6 +30,15 @@
2930
_ = log
3031

3132

33+
def PieceQuoted(s):
34+
# type: (str) -> Piece
35+
"""
36+
For 'hi' "$x"
37+
and $[myexpr] in YSH
38+
"""
39+
return Piece(s, True, False)
40+
41+
3242
def LiteralId(part):
3343
# type: (word_part_t) -> Id_t
3444
"""If the WordPart consists of a single literal token, return its Id.

osh/word_eval.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ def _EvalDoubleQuoted(self, parts, part_vals):
13801380
# of (DoubleQuoted [Literal '']). This is better but it means we
13811381
# have to check for it.
13821382
if len(parts) == 0:
1383-
v = Piece('', True, False)
1383+
v = word_.PieceQuoted('')
13841384
part_vals.append(v)
13851385
return
13861386

@@ -1782,6 +1782,7 @@ def _EvalExtGlob(self, part, part_vals):
17821782
else:
17831783
op_str = lexer.LazyStr(op)
17841784
# Do NOT split these.
1785+
# TODO: should be PieceQuoted
17851786
part_vals.append(Piece(op_str, False, False))
17861787

17871788
for i, w in enumerate(part.arms):
@@ -1857,18 +1858,18 @@ def _EvalWordPart(self, part, part_vals, flags):
18571858
elif case(word_part_e.BracedRangeDigit):
18581859
part = cast(word_part.BracedRangeDigit, UP_part)
18591860
# This is the '5' in {1..10} - whether it's quoted should not
1860-
# matter
1861+
# matter - it doesn't look like a glob
18611862
v = Piece(part.s, False, False)
18621863
part_vals.append(v)
18631864

18641865
elif case(word_part_e.EscapedLiteral):
18651866
part = cast(word_part.EscapedLiteral, UP_part)
1866-
v = Piece(part.ch, True, False)
1867+
v = word_.PieceQuoted(part.ch)
18671868
part_vals.append(v)
18681869

18691870
elif case(word_part_e.SingleQuoted):
18701871
part = cast(SingleQuoted, UP_part)
1871-
v = Piece(part.sval, True, False)
1872+
v = word_.PieceQuoted(part.sval)
18721873
part_vals.append(v)
18731874

18741875
elif case(word_part_e.DoubleQuoted):
@@ -1904,7 +1905,7 @@ def _EvalWordPart(self, part, part_vals, flags):
19041905
# We never parse a quoted string into a TildeSub.
19051906
assert not quoted
19061907
s = self.tilde_ev.Eval(part)
1907-
v = Piece(s, True, False) # NOT split even when unquoted!
1908+
v = word_.PieceQuoted(s)
19081909
part_vals.append(v)
19091910

19101911
elif case(word_part_e.ArithSub):
@@ -2615,7 +2616,7 @@ def _EvalProcessSub(self, cs_part):
26152616
# type: (CommandSub) -> Piece
26162617
dev_path = self.shell_ex.RunProcessSub(cs_part)
26172618
# pretend it's quoted; no split or glob
2618-
return Piece(dev_path, True, False)
2619+
return word_.PieceQuoted(dev_path)
26192620

26202621

26212622
_DUMMY = '__NO_COMMAND_SUB__'
@@ -2659,7 +2660,7 @@ def _EvalCommandSub(self, cs_part, quoted):
26592660
def _EvalProcessSub(self, cs_part):
26602661
# type: (CommandSub) -> Piece
26612662
# pretend it's quoted; no split or glob
2662-
return Piece('__NO_PROCESS_SUB__', True, False)
2663+
return word_.PieceQuoted('__NO_PROCESS_SUB__')
26632664

26642665

26652666
# vim: sw=4

spec/osh-bugs.test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ pp test_ (x)
2929
## STDOUT:
3030
## END
3131

32+
33+
#### YSH $[expr_sub] in OSH should not do dynamic globbing
34+
35+
touch {foo,bar}.txt
36+
37+
echo $["*.txt"]
38+
39+
## STDOUT:
40+
*.txt
41+
## END

ysh/expr_eval.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
from core import num
5353
from core import pyutil
5454
from core import state
55-
from display import ui
5655
from core import vm
56+
from display import ui
5757
from data_lang import j8
5858
from frontend import lexer
5959
from frontend import match
6060
from frontend import typed_args
6161
from osh import braces
62+
from osh import word_
6263
from mycpp import mops
6364
from mycpp.mylib import log, NewDict, switch, tagswitch, print_stderr
6465
from ysh import func_proc
@@ -412,7 +413,7 @@ def EvalExprSub(self, part):
412413
with switch(part.left.id) as case:
413414
if case(Id.Left_DollarBracket): # $[join(x)]
414415
s = val_ops.Stringify(val, loc.WordPart(part), 'Expr sub ')
415-
return Piece(s, False, False)
416+
return word_.PieceQuoted(s)
416417

417418
elif case(Id.Lit_AtLBracket): # @[split(x)]
418419
strs = val_ops.ToShellArray(val, loc.WordPart(part),

0 commit comments

Comments
 (0)