xXm0dzXx

Custom Shell

Sep 29th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.24 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. local tSplash = {
  15.     [1] = "Welcome to the server!",
  16.     [2] = "Go to computercraft.info for the forums!",
  17.     [3] = "Check out our website!",
  18.     [4] = "Do /buy 1 to buy vip",
  19.     [5] = "Did you know there is a secret movie in CC?",
  20.     [6] = "CallMeGavin's real username is gavsminecraft",
  21.     [7] = "Type programs in the console for programs!",
  22.     [8] = "CC uses LUA to code",
  23.     [9] = "He's watching you.",
  24.     [10] = "What are you looking at?",
  25.     [11] = "Minecraft is by notch!",
  26.     [12] = "ComputerCraft is by dan200!",
  27.     [13] = "Rednet lets you send messages wirelessly",
  28.     [14] = "Type shutdown to get free diamonds!",
  29.     [15] = "Never gonna give you up!",
  30.     [16] = "I used to be a coder like you, UNTIL I TOOK A ARROW TO THE KNEE",
  31.     [17] = "This server is owned by gavin!",
  32. }
  33.  
  34. -- Install shell API
  35. function shell.run( _sCommand, ... )
  36.     local sPath = shell.resolveProgram( _sCommand )
  37.     if sPath ~= nil then
  38.         tProgramStack[#tProgramStack + 1] = sPath
  39.         local result = os.run( tEnv, sPath, ... )
  40.         tProgramStack[#tProgramStack] = nil
  41.         return result
  42.     else
  43.         print( "No such program" )
  44.         return false
  45.     end
  46. end
  47.  
  48. function shell.exit()
  49.     bExit = true
  50. end
  51.  
  52. function shell.dir()
  53.     return sDir
  54. end
  55.  
  56. function shell.setDir( _sDir )
  57.     sDir = _sDir
  58. end
  59.  
  60. function shell.path()
  61.     return sPath
  62. end
  63.  
  64. function shell.setPath( _sPath )
  65.     sPath = _sPath
  66. end
  67.  
  68. function shell.resolve( _sPath )
  69.     local sStartChar = string.sub( _sPath, 1, 1 )
  70.     if sStartChar == "/" or sStartChar == "\\" then
  71.         return fs.combine( "", _sPath )
  72.     else
  73.         return fs.combine( sDir, _sPath )
  74.     end
  75. end
  76.  
  77. function shell.resolveProgram( _sCommand )
  78.     -- Substitute aliases firsts
  79.     if tAliases[ _sCommand ] ~= nil then
  80.         _sCommand = tAliases[ _sCommand ]
  81.     end
  82.  
  83.     -- If the path is a global path, use it directly
  84.     local sStartChar = string.sub( _sCommand, 1, 1 )
  85.     if sStartChar == "/" or sStartChar == "\\" then
  86.         local sPath = fs.combine( "", _sCommand )
  87.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  88.             return sPath
  89.         end
  90.         return nil
  91.     end
  92.    
  93.     -- Otherwise, look on the path variable
  94.     for sPath in string.gmatch(sPath, "[^:]+") do
  95.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  96.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  97.             return sPath
  98.         end
  99.     end
  100.    
  101.     -- Not found
  102.     return nil
  103. end
  104.  
  105. function shell.programs( _bIncludeHidden )
  106.     local tItems = {}
  107.    
  108.     -- Add programs from the path
  109.     for sPath in string.gmatch(sPath, "[^:]+") do
  110.         sPath = shell.resolve( sPath )
  111.         if fs.isDir( sPath ) then
  112.             local tList = fs.list( sPath )
  113.             for n,sFile in pairs( tList ) do
  114.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  115.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  116.                     tItems[ sFile ] = true
  117.                 end
  118.             end
  119.         end
  120.     end
  121.  
  122.     -- Sort and return
  123.     local tItemList = {}
  124.     for sItem, b in pairs( tItems ) do
  125.         table.insert( tItemList, sItem )
  126.     end
  127.     table.sort( tItemList )
  128.     return tItemList
  129. end
  130.  
  131. function shell.getRunningProgram()
  132.     if #tProgramStack > 0 then
  133.         return tProgramStack[#tProgramStack]
  134.     end
  135.     return nil
  136. end
  137.  
  138. function shell.setAlias( _sCommand, _sProgram )
  139.     tAliases[ _sCommand ] = _sProgram
  140. end
  141.  
  142. function shell.clearAlias( _sCommand )
  143.     tAliases[ _sCommand ] = nil
  144. end
  145.  
  146. function shell.aliases()
  147.     -- Add aliases
  148.     local tCopy = {}
  149.     for sAlias, sCommand in pairs( tAliases ) do
  150.         tCopy[sAlias] = sCommand
  151.     end
  152.     return tCopy
  153. end
  154.    
  155. print( os.version().. " - " ..tSplash[math.random(1,#tSlpash)] )
  156.  
  157. -- If this is the toplevel shell, run the startup programs
  158. if parentShell == nil then
  159.     -- Run the startup from the ROM first
  160.     local sRomStartup = shell.resolveProgram( "/rom/startup" )
  161.     if sRomStartup then
  162.         shell.run( sRomStartup )
  163.     end
  164.    
  165.     -- Then run the user created startup, from the disks or the root
  166.     local sUserStartup = shell.resolveProgram( "/startup" )
  167.     for n,sSide in pairs( redstone.getSides() ) do
  168.         if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  169.             local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
  170.             if sDiskStartup then
  171.                 sUserStartup = sDiskStartup
  172.                 break
  173.             end
  174.         end
  175.     end
  176.    
  177.     if sUserStartup then
  178.         shell.run( sUserStartup )
  179.     end
  180. end
  181.  
  182. -- Run any programs passed in as arguments
  183. local tArgs = { ... }
  184. if #tArgs > 0 then
  185.     shell.run( ... )
  186. end
  187.  
  188. -- Read commands and execute them
  189. local tCommandHistory = {}
  190. while not bExit do
  191.     write( shell.dir() .. "> " )
  192.  
  193.     local sLine = read( nil, tCommandHistory )
  194.     table.insert( tCommandHistory, sLine )
  195.    
  196.     local tWords = {}
  197.     for match in string.gmatch(sLine, "[^ \t]+") do
  198.         table.insert( tWords, match )
  199.     end
  200.  
  201.     local sCommand = tWords[1]
  202.     if sCommand then
  203.         shell.run( sCommand, unpack( tWords, 2 ) )
  204.     end
  205. end
  206.  
  207. -- If this is the toplevel shell, run the shutdown program
  208. if parentShell == nil then
  209.     if shell.resolveProgram( "shutdown" ) then
  210.         shell.run( "shutdown" )
  211.     end
  212.     os.shutdown() -- just in case
  213. end
Advertisement
Add Comment
Please, Sign In to add comment