Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 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:len() 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:len())
  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:len() >= 4 then
  81.          local cmdpath = true_path(cmd:sub(4, cmd:len()))
  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:len() >= 5 then
  91.          local cmdpath = true_path(cmd:sub(5, cmd:len()))
  92.          if fs.exists(cmdpath) and not fs.isDir(cmdpath) then
  93.             shell.run(cmdpath)
  94.          end
  95.       end
  96.    elseif cmd:sub(1, 3) == "pwd" then
  97.       print(path)
  98.    elseif cmd:sub(1, 4) == "exit" then
  99.       shell.run("clear")
  100.       running = false
  101.    end
  102.          
  103.    -- Output Handling
  104.    if outpath == "stdout" then
  105.       io.write(output)
  106.    elseif outpath == "pipe" then
  107.       pipe = output
  108.    else
  109.       local p = true_path(outpath)
  110.       if not fs.isDir(p) then
  111.          local f = io.open(p, "w")
  112.          f:write(output)
  113.          f:close()
  114.       end
  115.    end
  116. end
  117.  
  118. -- Environment Setup
  119. if not fs.exists(root) or not fs.isDir(root) then
  120.    shell.run("mkdir "..root)
  121.    local dshrc = fs.open(root.."/.dshrc", "w")
  122.    dshrc:close()
  123. end
  124.  
  125. -- Greeting Header
  126. shell.run("clear")
  127. print("-----------------------------")
  128. print("Digi's Shell (dsh) ver. "..ver)
  129. print("-----------------------------")
  130. print("")
  131.  
  132. -- User Login
  133. io.write("Username: ")
  134. user = read()
  135. io.write("Password: ")
  136. pass = read("*")
  137. print("")
  138.  
  139. -- Password Checking
  140.  
  141. -- Autorun
  142.  
  143. parse("sh /.dshrc")
  144.  
  145. -- Main Loop
  146. while running do
  147.    io.write(user.."@dsh:"..path.."$ ")
  148.    io.flush()
  149.    input = io.read()
  150.    parse(input)
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement