mirror of
https://github.com/samsonjs/csc360-a1-shell.git
synced 2026-03-25 08:45:52 +00:00
Add sequencing and cd dash
This commit is contained in:
parent
df480e3cc1
commit
2b78487c74
3 changed files with 36 additions and 10 deletions
|
|
@ -52,11 +52,16 @@ module Shell
|
|||
|
||||
def builtin_cd(args)
|
||||
dir = args.first
|
||||
if dir.nil?
|
||||
Dir.chdir Dir.home
|
||||
oldpwd = Dir.pwd
|
||||
target = if dir.nil?
|
||||
Dir.home
|
||||
elsif dir == "-"
|
||||
ENV["OLDPWD"] || oldpwd
|
||||
else
|
||||
Dir.chdir dir
|
||||
dir
|
||||
end
|
||||
Dir.chdir target
|
||||
ENV["OLDPWD"] = oldpwd
|
||||
ENV["PWD"] = Dir.pwd
|
||||
0
|
||||
end
|
||||
|
|
|
|||
|
|
@ -51,8 +51,12 @@ module Shell
|
|||
|
||||
logger.verbose "Processing command: #{line.inspect}"
|
||||
commands = parse_line(line)
|
||||
result = nil
|
||||
commands.each do |command|
|
||||
result = 0
|
||||
commands.each do |entry|
|
||||
command = entry[:command]
|
||||
next if command.strip.empty?
|
||||
next if entry[:op] == :and && result != 0
|
||||
|
||||
args = word_expander.expand(command)
|
||||
program = args.shift
|
||||
logger.verbose "Parsed command: #{program} #{args.inspect}"
|
||||
|
|
@ -79,13 +83,29 @@ module Shell
|
|||
commands = []
|
||||
command = "".dup
|
||||
state = :unquoted
|
||||
line.each_char do |c|
|
||||
next_op = :always
|
||||
i = 0
|
||||
while i < line.length
|
||||
c = line[i]
|
||||
case state
|
||||
when :unquoted
|
||||
case c
|
||||
when ";"
|
||||
commands << command
|
||||
commands << {command: command, op: next_op}
|
||||
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 "'"
|
||||
command << c
|
||||
state = :single_quoted
|
||||
|
|
@ -124,8 +144,9 @@ module Shell
|
|||
else
|
||||
raise "Unknown state #{state}"
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
commands << command
|
||||
commands << {command: command, op: next_op}
|
||||
commands
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -167,11 +167,11 @@ class ShellTest < Minitest::Test
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
def test_builtin_pwd
|
||||
|
|
|
|||
Loading…
Reference in a new issue