Advertisement
xXm0dzXx

pastebin test

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