Advertisement
pepeknamornik

Command line

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