mirror of
https://github.com/samsonjs/csc360-a1-shell.git
synced 2026-04-26 14:47:45 +00:00
Add brace expansion
This commit is contained in:
parent
a1410cb5da
commit
a323ea89fe
2 changed files with 26 additions and 5 deletions
|
|
@ -18,11 +18,19 @@ module Shell
|
||||||
protected_line = protect_escaped_dollars(line)
|
protected_line = protect_escaped_dollars(line)
|
||||||
substituted_line = expand_command_substitution(protected_line)
|
substituted_line = expand_command_substitution(protected_line)
|
||||||
shellsplit(substituted_line)
|
shellsplit(substituted_line)
|
||||||
.map do |word|
|
.flat_map do |word|
|
||||||
expand_variables(word)
|
expanded = expand_variables(word)
|
||||||
.tr(ESCAPED_DOLLAR, "$")
|
.tr(ESCAPED_DOLLAR, "$")
|
||||||
.tr(ESCAPED_BACKTICK, "`")
|
.tr(ESCAPED_BACKTICK, "`")
|
||||||
# TODO: expand globs
|
expand_braces(expanded)
|
||||||
|
end
|
||||||
|
.flat_map do |word|
|
||||||
|
if word =~ /[*?\[]/
|
||||||
|
glob_words = expand_globs(word)
|
||||||
|
glob_words.empty? ? [word] : glob_words
|
||||||
|
else
|
||||||
|
[word]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -268,6 +276,20 @@ module Shell
|
||||||
stdout.tr("\n", " ")
|
stdout.tr("\n", " ")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def expand_braces(word)
|
||||||
|
# Simple, non-nested brace expansion: pre{a,b}post -> preapost, prebpost
|
||||||
|
match = word.match(/(.*?)\{([^{}]*)\}(.*)/)
|
||||||
|
return [word] unless match
|
||||||
|
|
||||||
|
prefix = match[1]
|
||||||
|
body = match[2]
|
||||||
|
suffix = match[3]
|
||||||
|
return [word] unless body.include?(",")
|
||||||
|
|
||||||
|
parts = body.split(",", -1)
|
||||||
|
parts.flat_map { |part| expand_braces(prefix + part + suffix) }
|
||||||
|
end
|
||||||
|
|
||||||
def escaped_replacement(char)
|
def escaped_replacement(char)
|
||||||
case char
|
case char
|
||||||
when "$"
|
when "$"
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,6 @@ class ShellTest < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_expands_brace_expansion
|
def test_expands_brace_expansion
|
||||||
skip "brace expansion not implemented"
|
|
||||||
assert_equal "a b", `#{A1_PATH} -c 'echo {a,b}'`.chomp
|
assert_equal "a b", `#{A1_PATH} -c 'echo {a,b}'`.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -99,7 +98,7 @@ class ShellTest < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_does_not_expand_escaped_command_substitution_dollar_paren_in_double_quotes
|
def test_does_not_expand_escaped_command_substitution_dollar_paren_in_double_quotes
|
||||||
assert_equal "$(echo hi)", %x(#{A1_PATH} -c 'echo "\\$(echo hi)"').chomp
|
assert_equal "$(echo hi)", `#{A1_PATH} -c 'echo "\\$(echo hi)"'`.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_does_not_expand_escaped_command_substitution_backticks_in_double_quotes
|
def test_does_not_expand_escaped_command_substitution_backticks_in_double_quotes
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue