Add sequencing and cd dash

This commit is contained in:
Sami Samhuri 2026-02-03 03:57:40 -08:00
parent df480e3cc1
commit 2b78487c74
No known key found for this signature in database
3 changed files with 36 additions and 10 deletions

View file

@ -52,11 +52,16 @@ module Shell
def builtin_cd(args) def builtin_cd(args)
dir = args.first dir = args.first
if dir.nil? oldpwd = Dir.pwd
Dir.chdir Dir.home target = if dir.nil?
Dir.home
elsif dir == "-"
ENV["OLDPWD"] || oldpwd
else else
Dir.chdir dir dir
end end
Dir.chdir target
ENV["OLDPWD"] = oldpwd
ENV["PWD"] = Dir.pwd ENV["PWD"] = Dir.pwd
0 0
end end

View file

@ -51,8 +51,12 @@ module Shell
logger.verbose "Processing command: #{line.inspect}" logger.verbose "Processing command: #{line.inspect}"
commands = parse_line(line) commands = parse_line(line)
result = nil result = 0
commands.each do |command| commands.each do |entry|
command = entry[:command]
next if command.strip.empty?
next if entry[:op] == :and && result != 0
args = word_expander.expand(command) args = word_expander.expand(command)
program = args.shift program = args.shift
logger.verbose "Parsed command: #{program} #{args.inspect}" logger.verbose "Parsed command: #{program} #{args.inspect}"
@ -79,13 +83,29 @@ module Shell
commands = [] commands = []
command = "".dup command = "".dup
state = :unquoted state = :unquoted
line.each_char do |c| next_op = :always
i = 0
while i < line.length
c = line[i]
case state case state
when :unquoted when :unquoted
case c case c
when ";" when ";"
commands << command commands << {command: command, op: next_op}
command = "".dup command = "".dup
next_op = :always
i += 1
next
when "&"
if line[i + 1] == "&"
commands << {command: command, op: next_op}
command = "".dup
next_op = :and
i += 2
next
else
command << c
end
when "'" when "'"
command << c command << c
state = :single_quoted state = :single_quoted
@ -124,8 +144,9 @@ module Shell
else else
raise "Unknown state #{state}" raise "Unknown state #{state}"
end end
i += 1
end end
commands << command commands << {command: command, op: next_op}
commands commands
end end
end end

View file

@ -167,11 +167,11 @@ class ShellTest < Minitest::Test
end end
def test_builtin_cd_dash def test_builtin_cd_dash
skip "cannot easily implement without sequencing with ; or &&" assert_equal Dir.pwd, `#{A1_PATH} -c 'mkdir -p blah; cd blah; cd -; rm -rf blah; echo $PWD'`.strip
end end
def test_builtin_cd_parent def test_builtin_cd_parent
skip "cannot easily implement without sequencing with ; or &&" assert_equal Dir.pwd, `#{A1_PATH} -c 'mkdir -p blah; cd blah; cd ..; rm -rf blah; echo $PWD'`.strip
end end
def test_builtin_pwd def test_builtin_pwd