Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Config
- root = "/fsdsh"
- ver = "0.1.0"
- -- Environment Variables
- running = true
- user = ""
- pass = ""
- path = "/"
- shvar = {}
- pipe = ""
- input = ""
- output = ""
- last = ""
- -- Helper Functions
- function abs_path(dir)
- if dir:sub(1, 1) == "/" then
- return dir
- else
- return path..dir
- end
- end
- function true_path(dir)
- return root..abs_path(dir)
- end
- -- Main Functions
- function parse(cmd)
- pipe = ""
- local cmds = {""}
- local cmdm = {""}
- local i = 1
- while i <= cmd:len() do
- if cmd:sub(i, i) == "\n" or cmd:sub(i, i) == "\r" then
- cmds[#cmds + 1] = ""
- cmdm[#cmdm + 1] = ""
- elseif cmd:sub(i, i+1) == "&&" then
- cmds[#cmds + 1] = ""
- cmdm[#cmdm + 1] = ""
- i = i + 2
- elseif cmd:sub(i, i) == "|" then
- cmds[#cmds] = cmds[#cmds]..cmd:sub(i, i)
- cmds[#cmds + 1] = ""
- cmdm[#cmdm + 1] = ""
- i = i + 1
- else
- cmds[#cmds] = cmds[#cmds]..cmd:sub(i, i)
- end
- i = i + 1
- end
- for i = 1, #cmds do
- run(cmds[i])
- end
- end
- function run(cmd)
- output = ""
- -- Output Path Detection
- local outpath = "stdout"
- local sep = cmd:find(">")
- if sep ~= nil then
- outpath = cmd:sub(sep, cmd:len())
- cmd = cmd:sub(1, sep - 1)
- else
- sep = cmd:find("|")
- if sep ~= nil then
- outpath = "pipe"
- cmd = cmd:sub(1, sep - 1)
- end
- end
- -- Main Command Checking
- if cmd:sub(1,2) == "sh" then
- if cmd:len() >= 4 then
- local cmdpath = true_path(cmd:sub(4, cmd:len()))
- if fs.exists(cmdpath) and not fs.isDir(cmdpath) then
- local f = io.open(cmdpath, "r")
- local c = f:read("*a")
- f:close()
- parse(c)
- end
- end
- elseif cmd:sub(1, 3) == "lua" then
- if cmd:len() >= 5 then
- local cmdpath = true_path(cmd:sub(5, cmd:len()))
- if fs.exists(cmdpath) and not fs.isDir(cmdpath) then
- shell.run(cmdpath)
- end
- end
- elseif cmd:sub(1, 3) == "pwd" then
- print(path)
- elseif cmd:sub(1, 4) == "exit" then
- shell.run("clear")
- running = false
- end
- -- Output Handling
- if outpath == "stdout" then
- io.write(output)
- elseif outpath == "pipe" then
- pipe = output
- else
- local p = true_path(outpath)
- if not fs.isDir(p) then
- local f = io.open(p, "w")
- f:write(output)
- f:close()
- end
- end
- end
- -- Environment Setup
- if not fs.exists(root) or not fs.isDir(root) then
- shell.run("mkdir "..root)
- local dshrc = fs.open(root.."/.dshrc", "w")
- dshrc:close()
- end
- -- Greeting Header
- shell.run("clear")
- print("-----------------------------")
- print("Digi's Shell (dsh) ver. "..ver)
- print("-----------------------------")
- print("")
- -- User Login
- io.write("Username: ")
- user = read()
- io.write("Password: ")
- pass = read("*")
- print("")
- -- Password Checking
- -- Autorun
- parse("sh /.dshrc")
- -- Main Loop
- while running do
- io.write(user.."@dsh:"..path.."$ ")
- io.flush()
- input = io.read()
- parse(input)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement