Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local parentShell = shell
- local x,y = term.getSize()
- local bExit = false
- local sDir = (parentShell and parentShell.dir()) or ""
- local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
- local tAliases = (parentShell and parentShell.aliases()) or {}
- local tProgramStack = {}
- local shell = {}
- local tEnv = {
- ["shell"] = shell,
- }
- local function cPrint(text)
- local x2,y2 = term.getCursorPos()
- term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
- print(text)
- end
- local function lPrint(text)
- local x2,y2 = term.getCursorPos()
- term.setCursorPos(x - text:len(), y2)
- write(text)
- end
- -- Install shell API
- function shell.error( _sError )
- term.clear()
- term.setCursorPos(1,4)
- cPrint("BSoD")
- print()
- print(" An error has occured. To continue:\n")
- print(" Press Ctrl+S to shutdown, or\n")
- print(" Press Ctrl+R to restart your computer.")
- print(" You will lose all unsaved information.")
- print("\n Error: " .._sError)
- print()
- cPrint("Press any key to continue")
- os.pullEventRaw( "key" )
- os.reboot()
- end
- function shell.run( _sCommand, ... )
- local sPath = shell.resolveProgram( _sCommand )
- if sPath ~= nil then
- tProgramStack[#tProgramStack + 1] = sPath
- local result = os.run( tEnv, sPath, ... )
- tProgramStack[#tProgramStack] = nil
- return result
- else
- print( "No such program" )
- return false
- end
- end
- function shell.exit()
- bExit = true
- end
- function shell.dir()
- return sDir
- end
- function shell.setDir( _sDir )
- sDir = _sDir
- end
- function shell.path()
- return sPath
- end
- function shell.setPath( _sPath )
- sPath = _sPath
- end
- function shell.resolve( _sPath )
- local sStartChar = string.sub( _sPath, 1, 1 )
- if sStartChar == "/" or sStartChar == "\\" then
- return fs.combine( "", _sPath )
- else
- return fs.combine( sDir, _sPath )
- end
- end
- function shell.resolveProgram( _sCommand )
- -- Substitute aliases firsts
- if tAliases[ _sCommand ] ~= nil then
- _sCommand = tAliases[ _sCommand ]
- end
- -- If the path is a global path, use it directly
- local sStartChar = string.sub( _sCommand, 1, 1 )
- if sStartChar == "/" or sStartChar == "\\" then
- local sPath = fs.combine( "", _sCommand )
- if fs.exists( sPath ) and not fs.isDir( sPath ) then
- return sPath
- end
- return nil
- end
- -- Otherwise, look on the path variable
- for sPath in string.gmatch(sPath, "[^:]+") do
- sPath = fs.combine( shell.resolve( sPath ), _sCommand )
- if fs.exists( sPath ) and not fs.isDir( sPath ) then
- return sPath
- end
- end
- -- Not found
- return nil
- end
- function shell.programs( _bIncludeHidden )
- local tItems = {}
- -- Add programs from the path
- for sPath in string.gmatch(sPath, "[^:]+") do
- sPath = shell.resolve( sPath )
- if fs.isDir( sPath ) then
- local tList = fs.list( sPath )
- for n,sFile in pairs( tList ) do
- if not fs.isDir( fs.combine( sPath, sFile ) ) and
- (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
- tItems[ sFile ] = true
- end
- end
- end
- end
- -- Sort and return
- local tItemList = {}
- for sItem, b in pairs( tItems ) do
- table.insert( tItemList, sItem )
- end
- table.sort( tItemList )
- return tItemList
- end
- function shell.getRunningProgram()
- if #tProgramStack > 0 then
- return tProgramStack[#tProgramStack]
- end
- return nil
- end
- function shell.setAlias( _sCommand, _sProgram )
- tAliases[ _sCommand ] = _sProgram
- end
- function shell.clearAlias( _sCommand )
- tAliases[ _sCommand ] = nil
- end
- function shell.aliases()
- -- Add aliases
- local tCopy = {}
- for sAlias, sCommand in pairs( tAliases ) do
- tCopy[sAlias] = sCommand
- end
- return tCopy
- end
- -- Attempt to boot the disks bios.
- local sUserStartup = nil
- for n,sSide in pairs( redstone.getSides() ) do
- if disk.isPresent( sSide ) and disk.hasData( sSide ) then
- local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "bios.lua") )
- if sDiskStartup then
- sUserStartup = sDiskStartup
- break
- end
- end
- end
- -- Boot your PC
- lPrint("F12 = Boot Menu")
- term.setCursorPos(1,y)
- write("Loading...")
- local timer = os.startTimer( 3 )
- while true do
- local sEvent, param = os.pullEventRaw()
- if param == timer and sEvent == "timer" then
- break
- elseif sEvent == "key" and param == 88 then
- term.setCursorPos(1,4)
- cPrint("+-----------------+")
- cPrint("| Boot Menu |")
- cPrint("+-----------------+")
- cPrint("| 1. DISK |")
- cPrint("| 2. Hard Drive |")
- cPrint("| |")
- cPrint("| |")
- cPrint("| |")
- cPrint("+-----------------+")
- while true do
- local sEvent, param = os.pullEventRaw( "key" )
- if param == 2 then
- break --It already boots out of the disk :P
- elseif param == 3 then
- sHardStartup = shell.resolveProgram( "/bios.lua" )
- if sHardStartup then
- sUserStartup = sHardStartup
- break
- else
- shell.error( "BIOS undetected in hard drive!" )
- end
- end
- end
- break
- end
- end
- if sUserStartup then
- --Store each line as a table
- local tLines = {}
- local file = io.open(sUserStartup, "r")
- local line
- while true do
- line = file:read()
- if not line then break end
- tLines[#tLines+1] = line
- end
- file:close()
- if tLines[1]:find("#") then
- fs.delete(".bios.lua.new")
- local BIOS = fs.open(".bios.lua.new", "w")
- for i = 3, #tLines do
- BIOS.writeLine(tLines[i])
- end
- BIOS.close()
- shell.run("rom/programs/clear")
- shell.run(".bios.lua.new")
- if string.sub(sUserStartup, 1, 5) == "disk/" then
- shell.run("disk/" ..string.sub(tLines[1], 2, string.len(tLines[1])))
- else
- shell.run(string.sub(tLines[1], 2, string.len(tLines[1])))
- end
- else
- shell.error( "Unsupported/Corrupted BIOS!" )
- end
- else
- shell.error( "BIOS undetected! Please insert a boot disk" )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement