Advertisement
Pinkishu

mtshell

Aug 13th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1.  
  2.  
  3. local bExit = false
  4. local sDir = (parentShell and parentShell.dir()) or ""
  5. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  6. local tAliases = (parentShell and parentShell.aliases()) or {}
  7.  
  8. local shell = {}
  9.  
  10. -- Install shell API
  11. function shell.run( _sCommand, ... )
  12.     local sPath = shell.resolveProgram( _sCommand )
  13.     if sPath and fs.exists(sPath) and not fs.isDir(sPath) then
  14.         if not mt.switchTask(_sCommand) then
  15.             mt.newTask(_sCommand,sPath,unpack(arg))
  16.             log("CLEAR!")
  17.             os.queueEvent("blah")
  18.             coroutine.yield()
  19.             term.clear()
  20.             term.setCursorPos(1,1)
  21.         end
  22.     else
  23.         print("Can't find program")
  24.     end
  25. end
  26.  
  27. function shell.exit()
  28.     bExit = true
  29. end
  30.  
  31. function shell.dir()
  32.     return sDir
  33. end
  34.  
  35. function shell.setDir( _sDir )
  36.     sDir = _sDir
  37. end
  38.  
  39. function shell.path()
  40.     return sPath
  41. end
  42.  
  43. function shell.setPath( _sPath )
  44.     sPath = _sPath
  45. end
  46.  
  47. function shell.resolve( _sPath )
  48.     local sStartChar = string.sub( _sPath, 1, 1 )
  49.     if sStartChar == "/" or sStartChar == "\\" then
  50.         return fs.combine( "", _sPath )
  51.     else
  52.         return fs.combine( sDir, _sPath )
  53.     end
  54. end
  55.  
  56. function shell.resolveProgram( _sCommand )
  57.     -- Substitute aliases firsts
  58.     if tAliases[ _sCommand ] ~= nil then
  59.         _sCommand = tAliases[ _sCommand ]
  60.     end
  61.  
  62.     -- If the path is a global path, use it directly
  63.     local sStartChar = string.sub( _sCommand, 1, 1 )
  64.     if sStartChar == "/" or sStartChar == "\\" then
  65.         local sPath = fs.combine( "", _sCommand )
  66.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  67.             return sPath
  68.         end
  69.         return nil
  70.     end
  71.    
  72.     -- Otherwise, look on the path variable
  73.     for sPath in string.gmatch(sPath, "[^:]+") do
  74.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  75.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  76.             return sPath
  77.         end
  78.     end
  79.    
  80.     -- Not found
  81.     return nil
  82. end
  83.  
  84. function shell.programs( _bIncludeHidden )
  85.     local tItems = {}
  86.    
  87.     -- Add programs from the path
  88.     for sPath in string.gmatch(sPath, "[^:]+") do
  89.         sPath = shell.resolve( sPath )
  90.         if fs.isDir( sPath ) then
  91.             local tList = fs.list( sPath )
  92.             for n,sFile in pairs( tList ) do
  93.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  94.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  95.                     tItems[ sFile ] = true
  96.                 end
  97.             end
  98.         end
  99.     end
  100.  
  101.     -- Sort and return
  102.     local tItemList = {}
  103.     for sItem, b in pairs( tItems ) do
  104.         table.insert( tItemList, sItem )
  105.     end
  106.     table.sort( tItemList )
  107.     return tItemList
  108. end
  109.  
  110. function shell.getRunningProgram()
  111.     return mt.getActiveTask()
  112. end
  113.  
  114. function shell.setAlias( _sCommand, _sProgram )
  115.     tAliases[ _sCommand ] = _sProgram
  116. end
  117.  
  118. function shell.clearAlias( _sCommand )
  119.     tAliases[ _sCommand ] = nil
  120. end
  121.  
  122. function shell.aliases()
  123.     -- Add aliases
  124.     local tCopy = {}
  125.     for sAlias, sCommand in pairs( tAliases ) do
  126.         tCopy[sAlias] = sCommand
  127.     end
  128.     return tCopy
  129. end
  130.    
  131. print( os.version() .. " - MT" )
  132.  
  133.  
  134.  
  135. -- Run any programs passed in as arguments
  136.  
  137. -- Read commands and execute them
  138. local tCommandHistory = {}
  139. while not bExit do
  140.     write( shell.dir() .. "> " )
  141.  
  142.     local sLine = read( nil, tCommandHistory )
  143.     table.insert( tCommandHistory, sLine )
  144.    
  145.     local tWords = {}
  146.     for match in string.gmatch(sLine, "[^ \t]+") do
  147.         table.insert( tWords, match )
  148.     end
  149.  
  150.     local sCommand = tWords[1]
  151.     if sCommand then
  152.         shell.run( sCommand, unpack( tWords, 2 ) )
  153.     end
  154. end
  155.  
  156. -- If this is the toplevel shell, run the shutdown program
  157. if parentShell == nil then
  158.     if shell.resolveProgram( "shutdown" ) then
  159.         shell.run( "shutdown" )
  160.     end
  161.     os.shutdown() -- just in case
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement