Advertisement
Pinkishu

cshell

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