xXm0dzXx

Untitled

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