Advertisement
Guest User

Untitled

a guest
Jun 15th, 2012
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local parentShell = shell
  2. local x,y = term.getSize()
  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 cPrint(text)
  16. local x2,y2 = term.getCursorPos()
  17. term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  18. print(text)
  19. end
  20.  
  21. local function lPrint(text)
  22. local x2,y2 = term.getCursorPos()
  23. term.setCursorPos(x - text:len(), y2)
  24. write(text)
  25. end
  26.  
  27. -- Install shell API
  28.  
  29. function shell.error( _sError )
  30. term.clear()
  31. term.setCursorPos(1,4)
  32. cPrint("BSoD")
  33. print()
  34. print(" An error has occured. To continue:\n")
  35. print(" Press Ctrl+S to shutdown, or\n")
  36. print(" Press Ctrl+R to restart your computer.")
  37. print(" You will lose all unsaved information.")
  38. print("\n Error: " .._sError)
  39. print()
  40. cPrint("Press any key to continue")
  41. os.pullEventRaw( "key" )
  42. os.reboot()
  43. end
  44.  
  45.  
  46. function shell.run( _sCommand, ... )
  47. local sPath = shell.resolveProgram( _sCommand )
  48. if sPath ~= nil then
  49. tProgramStack[#tProgramStack + 1] = sPath
  50. local result = os.run( tEnv, sPath, ... )
  51. tProgramStack[#tProgramStack] = nil
  52. return result
  53. else
  54. print( "No such program" )
  55. return false
  56. end
  57. end
  58.  
  59. function shell.exit()
  60. bExit = true
  61. end
  62.  
  63. function shell.dir()
  64. return sDir
  65. end
  66.  
  67. function shell.setDir( _sDir )
  68. sDir = _sDir
  69. end
  70.  
  71. function shell.path()
  72. return sPath
  73. end
  74.  
  75. function shell.setPath( _sPath )
  76. sPath = _sPath
  77. end
  78.  
  79. function shell.resolve( _sPath )
  80. local sStartChar = string.sub( _sPath, 1, 1 )
  81. if sStartChar == "/" or sStartChar == "\\" then
  82. return fs.combine( "", _sPath )
  83. else
  84. return fs.combine( sDir, _sPath )
  85. end
  86. end
  87.  
  88. function shell.resolveProgram( _sCommand )
  89. -- Substitute aliases firsts
  90. if tAliases[ _sCommand ] ~= nil then
  91. _sCommand = tAliases[ _sCommand ]
  92. end
  93.  
  94. -- If the path is a global path, use it directly
  95. local sStartChar = string.sub( _sCommand, 1, 1 )
  96. if sStartChar == "/" or sStartChar == "\\" then
  97. local sPath = fs.combine( "", _sCommand )
  98. if fs.exists( sPath ) and not fs.isDir( sPath ) then
  99. return sPath
  100. end
  101. return nil
  102. end
  103.  
  104. -- Otherwise, look on the path variable
  105. for sPath in string.gmatch(sPath, "[^:]+") do
  106. sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  107. if fs.exists( sPath ) and not fs.isDir( sPath ) then
  108. return sPath
  109. end
  110. end
  111.  
  112. -- Not found
  113. return nil
  114. end
  115.  
  116. function shell.programs( _bIncludeHidden )
  117. local tItems = {}
  118.  
  119. -- Add programs from the path
  120. for sPath in string.gmatch(sPath, "[^:]+") do
  121. sPath = shell.resolve( sPath )
  122. if fs.isDir( sPath ) then
  123. local tList = fs.list( sPath )
  124. for n,sFile in pairs( tList ) do
  125. if not fs.isDir( fs.combine( sPath, sFile ) ) and
  126. (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  127. tItems[ sFile ] = true
  128. end
  129. end
  130. end
  131. end
  132.  
  133. -- Sort and return
  134. local tItemList = {}
  135. for sItem, b in pairs( tItems ) do
  136. table.insert( tItemList, sItem )
  137. end
  138. table.sort( tItemList )
  139. return tItemList
  140. end
  141.  
  142. function shell.getRunningProgram()
  143. if #tProgramStack > 0 then
  144. return tProgramStack[#tProgramStack]
  145. end
  146. return nil
  147. end
  148.  
  149. function shell.setAlias( _sCommand, _sProgram )
  150. tAliases[ _sCommand ] = _sProgram
  151. end
  152.  
  153. function shell.clearAlias( _sCommand )
  154. tAliases[ _sCommand ] = nil
  155. end
  156.  
  157. function shell.aliases()
  158. -- Add aliases
  159. local tCopy = {}
  160. for sAlias, sCommand in pairs( tAliases ) do
  161. tCopy[sAlias] = sCommand
  162. end
  163. return tCopy
  164. end
  165.  
  166. -- Attempt to boot the disks bios.
  167. local sUserStartup = nil
  168.  
  169. for n,sSide in pairs( redstone.getSides() ) do
  170. if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  171. local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "bios.lua") )
  172. if sDiskStartup then
  173. sUserStartup = sDiskStartup
  174. break
  175. end
  176. end
  177. end
  178.  
  179. -- Boot your PC
  180. lPrint("F12 = Boot Menu")
  181. term.setCursorPos(1,y)
  182. write("Loading...")
  183.  
  184.  
  185. local timer = os.startTimer( 3 )
  186. while true do
  187. local sEvent, param = os.pullEventRaw()
  188. if param == timer and sEvent == "timer" then
  189. break
  190. elseif sEvent == "key" and param == 88 then
  191. term.setCursorPos(1,4)
  192. cPrint("+-----------------+")
  193. cPrint("| Boot Menu |")
  194. cPrint("+-----------------+")
  195. cPrint("| 1. DISK |")
  196. cPrint("| 2. Hard Drive |")
  197. cPrint("| |")
  198. cPrint("| |")
  199. cPrint("| |")
  200. cPrint("+-----------------+")
  201. while true do
  202. local sEvent, param = os.pullEventRaw( "key" )
  203. if param == 2 then
  204. break --It already boots out of the disk :P
  205. elseif param == 3 then
  206. sHardStartup = shell.resolveProgram( "/bios.lua" )
  207. if sHardStartup then
  208. sUserStartup = sHardStartup
  209. break
  210. else
  211. shell.error( "BIOS undetected in hard drive!" )
  212. end
  213. end
  214. end
  215.  
  216. break
  217. end
  218. end
  219.  
  220. if sUserStartup then
  221. --Store each line as a table
  222. local tLines = {}
  223. local file = io.open(sUserStartup, "r")
  224. local line
  225. while true do
  226. line = file:read()
  227. if not line then break end
  228. tLines[#tLines+1] = line
  229. end
  230. file:close()
  231.  
  232. if tLines[1]:find("#") then
  233. fs.delete(".bios.lua.new")
  234. local BIOS = fs.open(".bios.lua.new", "w")
  235. for i = 3, #tLines do
  236. BIOS.writeLine(tLines[i])
  237. end
  238. BIOS.close()
  239. shell.run("rom/programs/clear")
  240. shell.run(".bios.lua.new")
  241. if string.sub(sUserStartup, 1, 5) == "disk/" then
  242. shell.run("disk/" ..string.sub(tLines[1], 2, string.len(tLines[1])))
  243. else
  244. shell.run(string.sub(tLines[1], 2, string.len(tLines[1])))
  245. end
  246. else
  247. shell.error( "Unsupported/Corrupted BIOS!" )
  248. end
  249. else
  250. shell.error( "BIOS undetected! Please insert a boot disk" )
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement