Created
August 31, 2009 23:17
-
-
Save ingramj/178769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'readline' | |
require 'pp' | |
class Stsh | |
def initialize | |
@prompt = "-$ " | |
@aliases = {} | |
@running = false | |
set_traps | |
end | |
def set_traps | |
Signal.trap("INT") { print "\n#{@prompt}"; "DEFAULT" } | |
end | |
def readcmds | |
@running = true | |
while @running && line = Readline.readline(@prompt, true) | |
runcmd line | |
end | |
puts | |
end | |
def runcmd(s, a = false) | |
cmdline = s.split | |
cmd = cmdline[0] | |
if @aliases[cmd] != nil && a == false | |
runcmd(@aliases[cmd], true) | |
else | |
case cmd | |
when 'cd' then Dir.chdir(File.expand_path(cmdline[1])) | |
when 'alias' then add_alias(cmdline[1..-1]) | |
when 'paliases' then pp @aliases | |
when 'exit' then @running = false | |
when '!' then puts(eval(cmdline[1..-1].join(' '))) | |
when 'logout' then puts "This had better not be a login shell! Try exit." | |
when 'help' then print_help | |
else | |
pid = fork | |
if pid == nil | |
# Child | |
exec s | |
else | |
Signal.trap("INT") { puts; "DEFAULT" } | |
Process.wait | |
set_traps | |
end | |
end | |
end | |
end | |
def add_alias(a) | |
@aliases[a[0]] = a[1..-1].join(' ') | |
end | |
def print_help | |
puts <<EOHELP | |
Stupid Shell: | |
cd - change directory | |
alias name body - add an alias | |
paliases - print defined aliases | |
exit - exit the shell | |
! - evaluate the rest of the line as ruby code | |
help - print this message | |
EOHELP | |
end | |
end | |
if __FILE__ == $0 | |
s = Stsh.new | |
s.readcmds | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment