xXm0dzXx

Untitled

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