Redxone

[CC]Shell v1.73 for 1.74

Jul 14th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1.  
  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 sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  13. local tAliases = (parentShell and parentShell.aliases()) or {}
  14. local tProgramStack = {}
  15.  
  16. local shell = {}
  17. local tEnv = {
  18. [ "shell" ] = shell,
  19. [ "multishell" ] = multishell,
  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. local tArgs = { ... }
  213. if #tArgs > 0 then
  214. -- "shell x y z"
  215. -- Run the program specified on the commandline
  216. shell.run( ... )
  217.  
  218. else
  219. -- "shell"
  220. -- Print the header
  221. term.setBackgroundColor( bgColour )
  222. term.setTextColour( promptColour )
  223. print( os.version() )
  224. term.setTextColour( textColour )
  225.  
  226. -- Run the startup program
  227. if parentShell == nil then
  228. shell.run( "/rom/startup" )
  229. end
  230.  
  231. -- Read commands and execute them
  232. local tCommandHistory = {}
  233. while not bExit do
  234. term.redirect( parentTerm )
  235. term.setBackgroundColor( bgColour )
  236. term.setTextColour( promptColour )
  237. write( shell.dir() .. "> " )
  238. term.setTextColour( textColour )
  239.  
  240. local sLine = read( nil, tCommandHistory )
  241. table.insert( tCommandHistory, sLine )
  242. shell.run( sLine )
  243. end
  244. end
Add Comment
Please, Sign In to add comment