xXm0dzXx

Untitled

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