Created
October 21, 2012 00:36
-
-
Save severak/3925339 to your computer and use it in GitHub Desktop.
ONYX shell - unix-like shell simulator
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
---- | |
-- ONYX shell | |
---- | |
-- needs TableFS (https://gist.github.com/3925335) | |
---- | |
-- unix-like shell simulator | |
-- by Severak 2012 | |
-- license WTFPL | |
---- | |
require "tfs" | |
command={} | |
command["ver"]=function() | |
return "ONYX v 1.0" | |
end | |
command["exit"]=function() os.exit() end | |
command["echo"]=function(argz) | |
return table.concat(argz," ") | |
end | |
command["ls"]=function(argz) | |
local out={} | |
local dir=argz[1] or "." | |
if not(type(tfs.load(dir))=="table") then | |
print(string.format("%s is not a dir.", dir)) | |
return "" | |
end | |
for file in tfs.dir(dir) do | |
out[#out+1]=file | |
end | |
return table.concat(out,"\n") | |
end | |
command["cd"]=function(argz) | |
local ok,err=tfs.chdir(argz[1] or ".") | |
if not ok then | |
print("-onyx: ",err) | |
end | |
end | |
command["pwd"]=function(argz) | |
return tfs.pwd() | |
end | |
command["mkdir"]=function(argz) | |
local ok,err=tfs.mkdir(argz[1] or ".") | |
if ok then | |
return 0 | |
else | |
print(err) | |
end | |
end | |
command["rmrf"]=function(argz) | |
if #argz>0 then | |
tfs.save(argz,nil) | |
end | |
end | |
function splitWords(sentence) | |
local ret={} | |
for word in sentence:gmatch("([^%s]+)") do | |
ret[#ret+1]=word | |
end | |
return ret | |
end | |
local tree=tDir{ | |
home=tDir{ | |
help="Work with it as you will work with unix.", | |
examples=tDir{ | |
a="echo ok|reverse", | |
b="cat /doc/readme", | |
c="ls>dir.txt", | |
d="echo ok|reverse|tee reversed.txt|reverse>normal.txt" | |
} | |
}, | |
doc=tDir{ | |
readme="There is no readme today\n:-(" | |
}, | |
bin=tDir{}, | |
dev=tDir{} | |
} | |
devMT={ | |
__index=function(dir,file) | |
if file=="random" then | |
return math.random(255) | |
elseif file=="null" then | |
return "" | |
end | |
end, | |
__newindex=function(dir,file,data) | |
if file=="null" or file=="random" then | |
return nil | |
else | |
rawset(dir,file,data) | |
end | |
end | |
} | |
setmetatable(tree["dev"],devMT) | |
tfs=TableFS(tree,"home") | |
tfs.save("/bin/cat", function(argz,input) | |
local out={} | |
if #argz>0 then | |
for _,name in pairs(argz) do | |
local data,err=tfs.load(name) | |
if type(data)=="table" then | |
print(string.format("cat: %s: Is directory",name)) | |
elseif data then | |
out[#out+1]=tostring(data) | |
else | |
print(string.format("cat: %s: %s",name,err or "File not found!")) | |
end | |
end | |
else | |
out[#out+1]=io.read() | |
end | |
return table.concat(out,"\n") | |
end) | |
tfs.save("/bin/reverse", function(argz,input) | |
return string.reverse(input or "") | |
end) | |
tfs.save("/bin/tee", function(argz,input) | |
for _,name in pairs(argz) do | |
tfs.save(name,input) | |
end | |
return input | |
end) | |
tfs.save("/bin/help", function() | |
print("Use it like unix.") | |
print("There is no other help...") | |
end) | |
function processPipe(cmds) | |
local out="" | |
for input in string.gmatch(cmds,"([^|]+)") do | |
local args=splitWords(input) | |
local cmd=table.remove(args,1) | |
if command[cmd] then | |
out=command[cmd](args) | |
else | |
local fun=tfs.load("/bin/"..cmd) | |
if type(fun)=="function" then | |
out=fun(args,out) | |
else | |
print(string.format("An attemp to call %s (not a function)",cmd)) | |
break | |
end | |
end | |
end | |
return out or "" | |
end | |
function main() | |
while true do | |
io.write("> ") | |
local outfile=false | |
local input=io.read() | |
if string.match(input,"(.+)>(.+)") then | |
input,outfile=string.match(input,"(.+)>(.+)") | |
end | |
local out=processPipe(input) | |
if not(out=="") then | |
if outfile then | |
tfs.save(outfile,out) | |
else | |
print(out) | |
end | |
end | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment