Lignum

Fake Shell

Feb 21st, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.25 KB | None | 0 0
  1. function FakeShell()
  2.     local shell = {}
  3.     shell.__cdir = ""
  4.     shell.__path = ".:/rom/programs:/rom/programs/advanced:/rom/programs/rednet:/rom/programs/fun:/rom/programs/http:/rom/programs/fun/advanced"
  5.     shell.__stack = {}
  6.  
  7.     shell.dir = function()
  8.         return shell.__cdir
  9.     end
  10.  
  11.     shell.setDir = function(path)
  12.         shell.__cdir = path
  13.     end
  14.  
  15.     shell.resolve = function(localPath)
  16.         return fs.combine(shell.__cdir, localPath)
  17.     end
  18.  
  19.     shell.resolveProgram = function(name)
  20.         local firstChar = name:sub(1, 1)
  21.         if firstChar == "/" or firstChar == "\\" then
  22.             local path = fs.combine("", name)
  23.             if fs.exists(path) and not fs.isDir(path) then return path end
  24.             return nil
  25.         end
  26.  
  27.         for path in shell.__path:gmatch("[^:]+") do
  28.             path = fs.combine(shell.resolve(path), name)
  29.             if fs.exists(path) and not fs.isDir(path) then return path end
  30.         end
  31.  
  32.         return nil
  33.     end
  34.  
  35.     shell.getRunningProgram = function()
  36.         return shell.__stack[#shell.__stack]
  37.     end
  38.  
  39.     shell.run = function(program, ...)
  40.         local path = shell.resolveProgram(program)
  41.         table.insert(shell.__stack, path)
  42.         os.run({ shell = shell }, path, ...)
  43.     end
  44.  
  45.     shell.path = function()
  46.         return shell.__path
  47.     end
  48.  
  49.     shell.setPath = function(path)
  50.         shell.__path = path
  51.     end
  52.  
  53.     return shell
  54. end
Advertisement
Add Comment
Please, Sign In to add comment