Advertisement
PaymentOption

noDiskBoot

May 18th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. oldpullEvent = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3.  
  4. local parentShell = shell
  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. local shell = {}
  11. local tEnv = {
  12. ["shell"] = shell,
  13. }
  14. -- Install shell API
  15. function shell.setDiskBootPassword(password)
  16. if fs.exists(".diskboot") then
  17. fs.delete(".diskboot")
  18. end
  19. file = fs.open(".diskboot", "w")
  20. file.write(password)
  21. file.close()
  22. end
  23. function shell.setBootPassword(password)
  24. if fs.exists(".boot") then
  25. fs.delete(".boot")
  26. end
  27. file = fs.open(".boot", "w")
  28. file.write(password)
  29. file.close()
  30. end
  31. function shell.run( _sCommand, ... )
  32. local sPath = shell.resolveProgram( _sCommand )
  33. if sPath ~= nil then
  34. tProgramStack[#tProgramStack + 1] = sPath
  35. local result = os.run( tEnv, sPath, ... )
  36. tProgramStack[#tProgramStack] = nil
  37. return result
  38. else
  39. print( "No such program" )
  40. return false
  41. end
  42. end
  43. function shell.exit()
  44. bExit = true
  45. end
  46. function shell.dir()
  47. return sDir
  48. end
  49. function shell.setDir( _sDir )
  50. sDir = _sDir
  51. end
  52. function shell.path()
  53. return sPath
  54. end
  55. function shell.setPath( _sPath )
  56. sPath = _sPath
  57. end
  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. function shell.resolveProgram( _sCommand )
  67. -- Substitute aliases firsts
  68. if tAliases[ _sCommand ] ~= nil then
  69. _sCommand = tAliases[ _sCommand ]
  70. end
  71. -- If the path is a global path, use it directly
  72. local sStartChar = string.sub( _sCommand, 1, 1 )
  73. if sStartChar == "/" or sStartChar == "\\" then
  74. local sPath = fs.combine( "", _sCommand )
  75. if fs.exists( sPath ) and not fs.isDir( sPath ) then
  76. return sPath
  77. end
  78. return nil
  79. end
  80.  
  81. -- Otherwise, look on the path variable
  82. for sPath in string.gmatch(sPath, "[^:]+") do
  83. sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  84. if fs.exists( sPath ) and not fs.isDir( sPath ) then
  85. return sPath
  86. end
  87. end
  88.  
  89. -- Not found
  90. return nil
  91. end
  92. function shell.programs( _bIncludeHidden )
  93. local tItems = {}
  94.  
  95. -- Add programs from the path
  96. for sPath in string.gmatch(sPath, "[^:]+") do
  97. sPath = shell.resolve( sPath )
  98. if fs.isDir( sPath ) then
  99. local tList = fs.list( sPath )
  100. for n,sFile in pairs( tList ) do
  101. if not fs.isDir( fs.combine( sPath, sFile ) ) and
  102. (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  103. tItems[ sFile ] = true
  104. end
  105. end
  106. end
  107. end
  108. -- Sort and return
  109. local tItemList = {}
  110. for sItem, b in pairs( tItems ) do
  111. table.insert( tItemList, sItem )
  112. end
  113. table.sort( tItemList )
  114. return tItemList
  115. end
  116. function shell.getRunningProgram()
  117. if #tProgramStack > 0 then
  118. return tProgramStack[#tProgramStack]
  119. end
  120. return nil
  121. end
  122. function shell.setAlias( _sCommand, _sProgram )
  123. tAliases[ _sCommand ] = _sProgram
  124. end
  125. function shell.clearAlias( _sCommand )
  126. tAliases[ _sCommand ] = nil
  127. end
  128. function shell.aliases()
  129. -- Add aliases
  130. local tCopy = {}
  131. for sAlias, sCommand in pairs( tAliases ) do
  132. tCopy[sAlias] = sCommand
  133. end
  134. return tCopy
  135. end
  136.  
  137. print( os.version() )
  138. if fs.exists(".boot") then
  139. file = fs.open(".boot", "r")
  140. password = file.readAll()
  141. file.close()
  142. write("I'm not an idiot either, dumbass.\nTry and guess the password: ")
  143. if password == read("*") then else
  144. os.reboot()
  145. end
  146. end
  147.  
  148. -- If this is the toplevel shell, run the startup programs
  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. if fs.exists(".diskboot") then
  163. file = fs.open(".diskboot", "r")
  164. password = file.readAll()
  165. file.close()
  166. write("Password: ")
  167. input = read("*")
  168. end
  169. if input == password then
  170. sUserStartup = sDiskStartup
  171. else
  172. os.reboot()
  173. end
  174. break
  175. end
  176. end
  177. end
  178.  
  179. if sUserStartup then
  180. shell.run( sUserStartup )
  181. end
  182. end
  183. -- Run any programs passed in as arguments
  184. local tArgs = { ... }
  185. if #tArgs > 0 then
  186. shell.run( ... )
  187. end
  188. -- Read commands and execute them
  189. local tCommandHistory = {}
  190. while not bExit do
  191. write( shell.dir() .. "> " )
  192. local sLine = read( nil, tCommandHistory )
  193. table.insert( tCommandHistory, sLine )
  194.  
  195. local tWords = {}
  196. for match in string.gmatch(sLine, "[^ \t]+") do
  197. table.insert( tWords, match )
  198. end
  199. local sCommand = tWords[1]
  200. if sCommand then
  201. shell.run( sCommand, unpack( tWords, 2 ) )
  202. end
  203. end
  204. -- If this is the toplevel shell, run the shutdown program
  205. if parentShell == nil then
  206. if shell.resolveProgram( "shutdown" ) then
  207. shell.run( "shutdown" )
  208. end
  209. os.shutdown() -- just in case
  210. end
  211.  
  212. os.pullEvent = oldpullEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement