Advertisement
Guest User

dsh

a guest
Jul 30th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. -- Config
  2. root = "/fsdsh"
  3. ver = "0.1.0"
  4.  
  5. -- Environment Variables
  6. running = true
  7. user = ""
  8. pass = ""
  9. path = "/"
  10. shvar = {}
  11. pipe = ""
  12. input = ""
  13. output = ""
  14. last = ""
  15.  
  16. -- Helper Functions
  17. function abs_path(dir)
  18.    if dir:sub(1, 1) == "/" then
  19.       return dir  
  20.    else
  21.       return path..dir
  22.    end
  23. end
  24.  
  25. function true_path(dir)
  26.    return root..abs_path(dir)
  27. end
  28.  
  29. -- Main Functions
  30. function parse(cmd)
  31.    pipe = ""
  32.    local cmds = {""}
  33.    local cmdm = {""}      
  34.    local i = 1
  35.    
  36.    while i <= #cmd do
  37.       if cmd:sub(i, i) == "\n" or cmd:sub(i, i) == "\r" then
  38.          cmds[#cmds + 1] = ""
  39.          cmdm[#cmdm + 1] = ""
  40.       elseif cmd:sub(i, i+1) == "&&" then
  41.          cmds[#cmds + 1] = ""
  42.          cmdm[#cmdm + 1] = ""
  43.          i = i + 2
  44.       elseif cmd:sub(i, i) == "|" then
  45.          cmds[#cmds] = cmds[#cmds]..cmd:sub(i, i)
  46.          cmds[#cmds + 1] = ""
  47.          cmdm[#cmdm + 1] = ""
  48.          i = i + 1
  49.       else
  50.          cmds[#cmds] = cmds[#cmds]..cmd:sub(i, i)
  51.       end
  52.       i = i + 1
  53.    end
  54.    
  55.    for i = 1, #cmds do
  56.       run(cmds[i])      
  57.    end
  58. end
  59.  
  60. function run(cmd)
  61.    output = ""
  62.    
  63.    -- Output Path Detection
  64.    local outpath = "stdout"
  65.    local sep = cmd:find(">")
  66.    
  67.    if sep ~= nil then
  68.       outpath = cmd:sub(sep, #cmd)
  69.       cmd = cmd:sub(1, sep - 1)
  70.    else
  71.       sep = cmd:find("|")
  72.       if sep ~= nil then
  73.          outpath = "pipe"
  74.          cmd = cmd:sub(1, sep - 1)
  75.       end
  76.    end
  77.    
  78.    -- Main Command Checking  
  79.    if cmd:sub(1,2) == "sh" then
  80.       if #cmd >= 4 then
  81.          local cmdpath = true_path(cmd:sub(4, #cmd))
  82.          if fs.exists(cmdpath) and not fs.isDir(cmdpath) then
  83.             local f = io.open(cmdpath, "r")
  84.             local c = f:read("*a")
  85.             f:close()
  86.             parse(c)
  87.          end
  88.       end
  89.    elseif cmd:sub(1, 3) == "lua" then
  90.       if #cmd >= 5 then
  91.          local cmdpath = true_path(cmd:sub(5, #cmd))
  92.          if fs.exists(cmdpath) and not fs.isDir(cmdpath) then
  93.             shell.run(cmdpath)
  94.          end
  95.       end
  96.    elseif cmd:sub(1, 5) == "mkdir" then
  97.       local p = ""
  98.       if cmd:len() >= 7 then
  99.          p = cmd:sub(7, #cmd)
  100.       elseif pipe ~= "" then
  101.          p = pipe
  102.       end
  103.       shell.run("mkdir "..true_path(p))
  104.    elseif cmd:sub(1, 2) == "ls" then
  105.       local oldDir = shell.dir()
  106.       shell.setDir(true_path(path))
  107.       shell.run("ls")
  108.       shell.setDir(oldDir)
  109.    elseif cmd:sub(1, 2) == "cd" then
  110.       if cmd:len() >= 4 then
  111.          local p = cmd:sub(4, #cmd)
  112.          if p == ".." then
  113.             parse("cd "..path:sub(1, path:match(".*/()") - 1))
  114.          else
  115.             if fs.isDir(true_path(p)) then
  116.                path = abs_path(p)
  117.             end
  118.          end
  119.       end
  120.    elseif cmd:sub(1, 3) == "pwd" then
  121.       print(path)
  122.    elseif cmd:sub(1, 4) == "echo" then
  123.       if cmd:len() >= 6 then
  124.          output = cmd:sub(6, #cmd).."\n"
  125.       end
  126.       output = output..pipe
  127.    elseif cmd:sub(1, 4) == "exit" then
  128.       shell.run("clear")
  129.       running = false
  130.    end
  131.    
  132.          
  133.    -- Output Handling
  134.    if outpath == "stdout" then
  135.       io.write(output)
  136.    elseif outpath == "pipe" then
  137.       pipe = output
  138.    else
  139.       local p = true_path(outpath)
  140.       if not fs.isDir(p) then
  141.          local f = io.open(p, "w")
  142.          f:write(output)
  143.          f:close()
  144.       end
  145.    end
  146. end
  147.  
  148. -- Environment Setup
  149. if not fs.exists(root) or not fs.isDir(root) then
  150.    shell.run("mkdir "..root)
  151.    local dshrc = fs.open(root.."/.dshrc", "w")
  152.    dshrc:close()
  153. end
  154.  
  155. -- Greeting Header
  156. shell.run("clear")
  157. print("-----------------------------")
  158. print("Digi's Shell (dsh) ver. "..ver)
  159. print("-----------------------------")
  160. print("")
  161.  
  162. -- User Login
  163. io.write("Username: ")
  164. user = read()
  165. io.write("Password: ")
  166. pass = read("*")
  167. print("")
  168.  
  169. -- Password Checking
  170.  
  171. -- Autorun
  172.  
  173. parse("sh /.dshrc")
  174.  
  175. -- Main Loop
  176. while running do
  177.    io.write(user.."@dsh:"..path.."$ ")
  178.    io.flush()
  179.    input = io.read()
  180.    parse(input)
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement