maxderopnl

Shell of craftos

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