mypal125

CraftOS Shell modified for CWCOS init and kernel

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