1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Commands to be executed directly by this shell.
= {}
# The builtin `cd` for changing the shell's working directory.
BUILTINS[] = lambda do
# Change to the home directory by default.
args << if args.empty?
# Read the destination path, doing very basic path expansion.
dest = args.pop.gsub(, )
# Try to change this shell's working directory.
begin
Dir.chdir(dest)
rescue Exception
puts()
end
end
# The builtin to exit the shell.
BUILTINS[] = lambda do
# Exit with a status of 0 by default.
args << 0 if args.empty?
# Exit the shell.
exit args.pop.to_i
end
# Print a prompt, then read a line from STDIN.
print()
# Try to read a line, exiting if there's no more lines.
line = STDIN.gets
if line
line.chomp.split()
else
exit
end
end
# Run the given command in a subprocess.
if BUILTINS[argv[0]]
BUILTINS[argv[0]].call(argv[1..-1])
else
success = system(*argv)
unless success
puts()
end
end
end
# Don't exit on `SIGINT`, instead simply return a new prompt.
trap() { print() }
# The glorious REPL itself.
loop { evaluate(read) }