-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellWrapper.rb
More file actions
103 lines (86 loc) · 1.69 KB
/
ShellWrapper.rb
File metadata and controls
103 lines (86 loc) · 1.69 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative 'contract'
require 'test/unit'
class ShellWrapper
include Test::Unit::Assertions
def initialize
@arr=Array.new
@errno_state=:NOERROR
post_invariant
end
def main
print "\\>"
while
commands = get_command
parse_command(commands)
print get_errno_message+" \\>"
end
end
def create_manage_child(commands)
pre_create_manage_child(commands)
read,write=IO.pipe
pid = fork {
proc{
# $SAFE=4
read.close
value=execute(commands)
Marshal.dump(value,write)
exit!(0)
}.call
}
Process.wait
write.close
myval=read.read
raise "child is empty" if myval.empty?
myval=Marshal.load(myval)
set_errno(convert_errno(myval))
@arr << pid
post_create_manage_child
end
def convert_errno(myval)
pre_convert_errno(myval)
new_errno = Errno.constants.select{|x| eval("Errno::#{x}::Errno")==myval}[0]
post_convert_errno(new_errno)
return new_errno
end
def set_errno(errno_val)
@errno_state=errno_val
end
def get_errno
@errno_state
end
def get_errno_message
eval("Errno::#{@errno_state}.new.message")
end
def get_command
command = gets
end
def execute(commands)
pre_execute(commands)
myval=system("bash","-c", commands) #+blah
myval=$?.exitstatus
post_execute(myval)
return myval
end
def parse_command(commands)
pre_parse_command(commands)
commands=commands.chomp
case
when /\Acd /.match(commands)
begin
path = commands.split(/\Acd /)[1]
Dir.chdir(path)
rescue
puts "No such directory: " + path
set_errno(:EPERM)
return
end
when /\Aexit$/.match(commands)
exit
else
create_manage_child(commands)
end
post_parse_command
end
end
theone = ShellWrapper.new
theone.main