-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrish.rb
executable file
·139 lines (118 loc) · 3.25 KB
/
rish.rb
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby #THIS WILL BE REMOVED
# RISH: Ruby Interactive Shell: Unix Shell written in Ruby
# Copyright (c) James Gillman [[email protected]], gitlab: @safetypanda
# Released under the GNU General Public License version 3+
# Refer to LICENSE file for license information.
require 'shellwords' #splits words for me cause I'm lazy
require 'readline' #needed for tab completion
require './customization.rb' #colors
require 'socket' #interact with sockets, get hostname
Readline.completion_proc = proc do |input|
TAB_COMPLETE.select { |name| name.start_with?(input) }
end
##
# Pipes
##
def split_on_pipes(input)
input.scan( /([^"'|]+)|["']([^"']+)["']/ ).flatten.compact
end
##
# spawn_program
##
def spawn_program(program, *args, child_out, child_in)
fork{
unless child_out == $stdout
$stdout.reopen(child_out)
child_out.close
end
unless child_in == $stdout
$stdin.reopen(child_in)
child_in.close
end
exec program, *args
}
end
##
# Main Shell Code
##
def rish
host = Socket.gethostname
loop do
$stdout.print(ENV['USER'].bold, "@".bold, host.bold)
input = Readline.readline("~>".bold, false)
commands = split_on_pipes(input)
child_in = $stdin
child_out = $stdout
pipe = []
commands.each_with_index do |command, index|
program, *args = Shellwords.shellsplit(command)
if COMMANDS[program]
COMMANDS[program].call(*args)
else
if index+1 < commands.size
pipe = IO.pipe
child_out = pipe.last
else
child_out = $stdout
end
spawn_program(program, *args, child_out, child_in)
child_out.close unless child_out == $stdout
child_in.close unless child_in == $stdin
child_in = pipe.first
end
end
Process.waitall
end
end
##
# Work In Progress, Will have to write the AUX args into this.
##
def ps
pids = Dir.glob("/proc/[0-9]*")
puts "PID\tCMD"
puts "-" * 15
pids.each do |pid|
cmd = File.read(pid + "/comm")
pid = pid.scan(/\d+/).first
puts "#{pid}\t#{cmd}"
end
end
##
# Text Before Prompt
##
def firstLoad
puts("----------------------------------------")
puts("RISH: Ruby Interactive Shell Version 0.2")
puts("Created by James Gillman.")
puts("Licensed under GPLV3")
puts("----------------------------------------")
end
##
# Built In Commands. Not ones stored in bin. But ones that can modify the shell
##
COMMANDS = {
'cd' => lambda { |directory| Dir.chdir(directory)},
'exit' => lambda { |code = 0| exit(code.to_i)},
'exec' => lambda { |*command| exec *command},
'echo' => lambda { |*words| printf("%s",*words)
puts()
},
'kill' => lambda { |*program| Process.kill(*program, pid)},
'ps' => lambda { ps() },
'export' => lambda { |args| #just like bash export
key, path = args.split('=')
ENV[key] = path
}
}
##
# List of built in commands
##
TAB_COMPLETE = [
'cd',
'exit',
'exec',
'export',
'echo'
].freeze
firstLoad()
rish()