Advertisement
Guest User

shell

a guest
Oct 25th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1.  
  2. local parentShell = shell
  3.  
  4. local bExit = false
  5. local sDir = (parentShell and parentShell.dir()) or ""
  6. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  7. local tAliases = (parentShell and parentShell.aliases()) or {}
  8. local tProgramStack = {}
  9.  
  10. local shell = {}
  11. local tEnv = {
  12.     ["shell"] = shell,
  13. }
  14.  
  15. -- Colours
  16. local promptColour, textColour, bgColour
  17. if term.isColour() then
  18.     promptColour = colours.yellow
  19.     textColour = colours.white
  20.     bgColour = colours.black
  21. else
  22.     promptColour = colours.white
  23.     textColour = colours.white
  24.     bgColour = colours.black
  25. end
  26.  
  27.  
  28. local function run( _sCommand, ... )
  29.     local sPath = shell.resolveProgram( _sCommand )
  30.     if sPath ~= nil then
  31.         tProgramStack[#tProgramStack + 1] = sPath
  32.         local result = os.run( tEnv, sPath, ... )
  33.         tProgramStack[#tProgramStack] = nil
  34.         return result
  35.     else
  36.         printError( "No such program" )
  37.         return false
  38.     end
  39. end
  40.  
  41. local function runLine( _sLine )
  42.     local tWords = {}
  43.     for match in string.gmatch( _sLine, "[^ \t]+" ) do
  44.         table.insert( tWords, match )
  45.     end
  46.  
  47.     local sCommand = tWords[1]
  48.     if sCommand then
  49.         return run( sCommand, unpack( tWords, 2 ) )
  50.     end
  51.     return false
  52. end
  53.  
  54. -- Install shell API
  55. function shell.run( ... )
  56.     return runLine( table.concat( { ... }, " " ) )
  57. end
  58.  
  59. function shell.exit()
  60.     bExit = true
  61. end
  62.  
  63. function shell.dir()
  64.     return sDir
  65. end
  66.  
  67. function shell.setDir( _sDir )
  68.     sDir = _sDir
  69. end
  70.  
  71. function shell.path()
  72.     return sPath
  73. end
  74.  
  75. function shell.setPath( _sPath )
  76.     sPath = _sPath
  77. end
  78.  
  79. function shell.resolve( _sPath )
  80.     local sStartChar = string.sub( _sPath, 1, 1 )
  81.     if sStartChar == "/" or sStartChar == "\\" then
  82.         return fs.combine( "", _sPath )
  83.     else
  84.         return fs.combine( sDir, _sPath )
  85.     end
  86. end
  87.  
  88. function shell.resolveProgram( _sCommand )
  89.     -- Substitute aliases firsts
  90.     if tAliases[ _sCommand ] ~= nil then
  91.         _sCommand = tAliases[ _sCommand ]
  92.     end
  93.  
  94.     -- If the path is a global path, use it directly
  95.     local sStartChar = string.sub( _sCommand, 1, 1 )
  96.     if sStartChar == "/" or sStartChar == "\\" then
  97.         local sPath = fs.combine( "", _sCommand )
  98.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  99.             return sPath
  100.         end
  101.         return nil
  102.     end
  103.    
  104.     -- Otherwise, look on the path variable
  105.     for sPath in string.gmatch(sPath, "[^:]+") do
  106.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  107.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  108.             return sPath
  109.         end
  110.     end
  111.    
  112.     -- Not found
  113.     return nil
  114. end
  115.  
  116. function shell.programs( _bIncludeHidden )
  117.     local tItems = {}
  118.    
  119.     -- Add programs from the path
  120.     for sPath in string.gmatch(sPath, "[^:]+") do
  121.         sPath = shell.resolve( sPath )
  122.         if fs.isDir( sPath ) then
  123.             local tList = fs.list( sPath )
  124.             for n,sFile in pairs( tList ) do
  125.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  126.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  127.                     tItems[ sFile ] = true
  128.                 end
  129.             end
  130.         end
  131.     end
  132.  
  133.     -- Sort and return
  134.     local tItemList = {}
  135.     for sItem, b in pairs( tItems ) do
  136.         table.insert( tItemList, sItem )
  137.     end
  138.     table.sort( tItemList )
  139.     return tItemList
  140. end
  141.  
  142. function shell.getRunningProgram()
  143.     if #tProgramStack > 0 then
  144.         return tProgramStack[#tProgramStack]
  145.     end
  146.     return nil
  147. end
  148.  
  149. function shell.setAlias( _sCommand, _sProgram )
  150.     tAliases[ _sCommand ] = _sProgram
  151. end
  152.  
  153. function shell.clearAlias( _sCommand )
  154.     tAliases[ _sCommand ] = nil
  155. end
  156.  
  157. function shell.aliases()
  158.     -- Add aliases
  159.     local tCopy = {}
  160.     for sAlias, sCommand in pairs( tAliases ) do
  161.         tCopy[sAlias] = sCommand
  162.     end
  163.     return tCopy
  164. end
  165.    
  166. term.setBackgroundColor( bgColour )
  167. term.setTextColour( promptColour )
  168. print( os.version() )
  169. term.setTextColour( textColour )
  170.  
  171. -- If this is the toplevel shell, run the startup programs
  172. if parentShell == nil then
  173.     -- Run the startup from the ROM first
  174.     local sRomStartup = shell.resolveProgram( "/rom/startup" )
  175.     if sRomStartup then
  176.         shell.run( sRomStartup )
  177.     end
  178.    
  179.     -- Then run the user created startup, from the disks or the root
  180.     local sUserStartup = shell.resolveProgram( "/startup" )
  181.     for n,sSide in pairs( redstone.getSides() ) do
  182.         if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  183.             local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
  184.             if sDiskStartup then
  185.                 sUserStartup = sDiskStartup
  186.                 break
  187.             end
  188.         end
  189.     end
  190.    
  191.     if sUserStartup then
  192.         shell.run( sUserStartup )
  193.     end
  194. end
  195.  
  196. -- Run any programs passed in as arguments
  197. local tArgs = { ... }
  198. if #tArgs > 0 then
  199.     shell.run( ... )
  200. end
  201.  
  202. -- Read commands and execute them
  203. local tCommandHistory = {}
  204. while not bExit do
  205.     term.setBackgroundColor( bgColour )
  206.     term.setTextColour( promptColour )
  207.     write( shell.dir() .. "> " )
  208.     term.setTextColour( textColour )
  209.  
  210.     local sLine = read( nil, tCommandHistory )
  211.     table.insert( tCommandHistory, sLine )
  212.     runLine( sLine )
  213. end
  214.  
  215. -- If this is the toplevel shell, run the shutdown program
  216. if parentShell == nil then
  217.     if shell.resolveProgram( "shutdown" ) then
  218.         shell.run( "shutdown" )
  219.     end
  220.     os.shutdown() -- just in case
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement