JoeNoIce

shell

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