Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The Main Bootup Disk.
- do
- _G._OSVERSION = "Script Executer V1.0"
- local component = component
- local computer = computer
- local unicode = unicode
- -- Runlevel information.
- local runlevel, shutdown = "S", computer.shutdown
- computer.runlevel = function() return runlevel end
- computer.shutdown = function(reboot)
- runlevel = reboot and 6 or 0
- if os.sleep then
- computer.pushSignal("shutdown")
- os.sleep(0.1) -- Allow shutdown processing.
- end
- shutdown(reboot)
- end
- -- Low level dofile implementation to read filesystem libraries.
- local rom = {}
- function rom.invoke(method, ...)
- return component.invoke(computer.getBootAddress(), method, ...)
- end
- function rom.open(file) return rom.invoke("open", file) end
- function rom.read(handle) return rom.invoke("read", handle, math.huge) end
- function rom.close(handle) return rom.invoke("close", handle) end
- function rom.inits() return ipairs(rom.invoke("list", "boot")) end
- function rom.isDirectory(path) return rom.invoke("isDirectory", path) end
- local screen = component.list('screen')()
- for address in component.list('screen') do
- if #component.invoke(address, 'getKeyboards') > 0 then
- screen = address
- end
- end
- -- Report boot progress if possible.
- local gpu = component.list("gpu", true)()
- local w, h
- if gpu and screen then
- component.invoke(gpu, "bind", screen)
- w, h = component.invoke(gpu, "getResolution")
- w, h = math.min(70, w), math.min(20, w)
- component.invoke(gpu, "setResolution", w, h)
- component.invoke(gpu, "setBackground", 0x000000)
- component.invoke(gpu, "setForeground", 0xFFFFFF)
- component.invoke(gpu, "fill", 1, 1, w, h, " ")
- end
- local x, y = 1, 1
- local function status(msg)
- if gpu and screen then
- component.invoke(gpu, "set", x, y, msg)
- if y == h then
- component.invoke(gpu, "copy", 1, 2, w, h - 1, 0, -1)
- component.invoke(gpu, "fill", 1, h, w, 1, " ")
- else
- y = y + 1
- end
- x = 1
- end
- end
- local function write(msg)
- if gpu and screen then
- component.invoke(gpu, "set", x, y, msg)
- x = x + #msg
- end
- end
- local screenAPI = {}
- function screenAPI.getResolution()
- return w, h
- end
- function screenAPI.setFgColor(color)
- component.invoke(gpu, "setForeground", color)
- end
- function screenAPI.setBgColor(color)
- component.invoke(gpu, "setBackground", color)
- end
- function screenAPI.set(...)
- component.invoke(gpu, "set", ...)
- end
- function screenAPI.copy(...)
- component.invoke(gpu, "copy", ...)
- end
- function screenAPI.fill(...)
- component.invoke(gpu, "fill", ...)
- end
- function screenAPI.clear()
- component.invoke(gpu, "setBackground", 0x000000)
- component.invoke(gpu, "setForeground", 0xFFFFFF)
- component.invoke(gpu, "fill", 1, 1, w, h, " ")
- end
- function screenAPI.cursor(newX, newY)
- x, y = newX, newY
- end
- function screenAPI.getCursor()
- return x, y
- end
- _G.screen = screenAPI
- _G.print = function(...)
- local args = table.pack(...)
- local stringToWrite = ""
- for i = 1, args.n do
- local arg = tostring(args[i])
- if i > 1 then
- arg = string.rep(" ", 5 - (#stringToWrite%5)) .. arg
- end
- stringToWrite = stringToWrite..arg
- end
- status(stringToWrite)
- end
- _G.write = function(...)
- local args = table.pack(...)
- local stringToWrite = ""
- for i = 1, args.n do
- local arg = tostring(args[i])
- if i > 1 then
- arg = " " .. arg
- end
- stringToWrite = stringToWrite..arg
- end
- write(stringToWrite)
- end
- end
- print(_OSVERSION)
- -- Select a file system from one of the available filesystems.
- local filesystemComponents = {}
- local currentFS = component.list("filesystem")
- for address in component.list("filesystem") do
- filesystemComponents[#filesystemComponents + 1] = address
- end
- local selectedFilesystem = computer.getBootAddress()
- -- Set up the filesystem API
- do
- local filesystem = {}
- for key, value in pairs(component.methods(selectedFilesystem)) do
- filesystem[key] = function(...)
- return component.invoke(selectedFilesystem, key, ...)
- end
- end
- _G.filesystem = filesystem
- end
- -- Set up some extra functions
- do
- function loadfile(path, ...)
- local handle, reason = filesystem.open(path)
- if not handle then
- error(reason, 2)
- end
- local buffer = ""
- repeat
- local data, reason = filesystem.read(handle)
- if not data and reason then
- error(reason)
- end
- buffer = buffer .. (data or "")
- until not data
- filesystem.close(handle)
- return load(buffer, "=" .. path)
- end
- local loadfilefordofile = function(path, ...)
- local handle, reason = filesystem.open(path)
- if not handle then
- return false, reason
- end
- local buffer = ""
- repeat
- local data, reason = filesystem.read(handle, 2048)
- if not data and reason then
- error(reason)
- end
- buffer = buffer .. (data or "")
- until not data
- filesystem.close(handle)
- return load(buffer, "=" .. path)
- end
- function dofile(file, ...)
- local program, reason = loadfilefordofile(file)
- if program then
- local result = table.pack(pcall(program, ...))
- if result[1] then
- return table.unpack(result, 2, result.n)
- else
- error(result[2], 2)
- end
- else
- error(reason, 2)
- end
- end
- end
- local commands = {}
- local keysDown = {}
- local keysToCharsShift = {
- [2] = "!",
- [3] = "@",
- [4] = "#",
- [5] = "$",
- [6] = "%",
- [7] = "^",
- [8] = "&",
- [9] = "*",
- [10] = "(",
- [11] = ")",
- [12] = "_",
- [13] = "+",
- [26] = "{",
- [27] = "}",
- [43] = "|",
- [39] = ":",
- [40] = '"',
- [51] = "<",
- [52] = ">",
- [53] = "?",
- }
- local keysToChars = {
- [2] = "1",
- [3] = "2",
- [4] = "3",
- [5] = "4",
- [6] = "5",
- [7] = "6",
- [8] = "7",
- [9] = "8",
- [10] = "9",
- [11] = "0",
- [12] = "-",
- [13] = "=",
- [16] = "q",
- [17] = "w",
- [18] = "e",
- [19] = "r",
- [20] = "t",
- [21] = "y",
- [22] = "u",
- [23] = "i",
- [24] = "o",
- [25] = "p",
- [26] = "[",
- [27] = "]",
- [43] = "\\",
- [30] = "a",
- [31] = "s",
- [32] = "d",
- [33] = "f",
- [34] = "g",
- [35] = "h",
- [36] = "j",
- [37] = "k",
- [38] = "l",
- [39] = ";",
- [40] = "'",
- [44] = "z",
- [45] = "x",
- [46] = "c",
- [47] = "v",
- [48] = "b",
- [49] = "n",
- [50] = "m",
- [51] = ",",
- [52] = ".",
- [53] = "/",
- [57] = " ",
- }
- -- Allows for obtaining text input from the user.
- function read(history)
- local text, lastText = "", ""
- local inputPosX, inputPosY = screen.getCursor()
- local cursorX = 1
- local selectedOption = 1
- --
- if history then
- selectedOption = #history + 1
- end
- while true do
- screen.cursor(inputPosX, inputPosY)
- write(text.." ")
- screen.cursor(inputPosX + cursorX - 1, inputPosY)
- write(unicode.char(0x2588))
- -- Wait for the next event and then perform an operation relating to the operation
- local event = {computer.pullSignal()}
- if event[1] == "key_up" then
- keysDown[event[4]] = false
- elseif event[1] == "key_down" then
- keysDown[event[4]] = true
- end
- if event[1] == "key_down" then
- -- Try to add the character.
- if keysDown[42] then -- The shift key was held.
- if keysToCharsShift[event[4]] then
- text = text:sub(1, cursorX - 1)..keysToCharsShift[event[4]]..text:sub(cursorX)
- cursorX = cursorX + 1
- lastText = text
- elseif keysToChars[event[4]] then
- text = text:sub(1, cursorX - 1)..string.upper(keysToChars[event[4]])..text:sub(cursorX)
- cursorX = cursorX + 1
- lastText = text
- end
- else -- The shift key was not held.
- if keysToChars[event[4]] then
- text = text:sub(1, cursorX - 1)..keysToChars[event[4]]..text:sub(cursorX)
- cursorX = cursorX + 1
- lastText = text
- end
- end
- if event[4] == 14 then -- The user pressed the backspace button.
- if cursorX > 1 then
- text = text:sub(1, cursorX - 2)..text:sub(cursorX)
- cursorX = math.max(1, cursorX - 1)
- lastText = text
- end
- elseif event[4] == 211 then -- The user pressed the delete button.
- if cursorX < #text then
- text = text:sub(1, cursorX)..text:sub(cursorX + 2)
- cursorX = math.min(cursorX, #text)
- lastText = text
- end
- elseif event[4] == 28 then -- The user pressed the enter button.
- screen.cursor(inputPosX, inputPosY)
- print(text.." ")
- return text
- elseif event[4] == 199 then -- The user pressed the home button.
- cursorX = 1
- elseif event[4] == 207 then -- The user pressed the end button.
- cursorX = #text + 1
- elseif event[4] == 203 then -- The user pressed the left button.
- cursorX = math.max(1, cursorX - 1)
- elseif event[4] == 205 then -- The user pressed the right button.
- cursorX = math.min(#text + 1, cursorX + 1)
- elseif event[4] == 200 then -- The user pressed the up button.
- if type(history) == "table" then
- if #history > 0 then
- selectedOption = math.max(selectedOption - 1, 1)
- text = history[selectedOption]
- end
- end
- elseif event[4] == 208 then -- The user pressed the down button.
- if type(history) == "table" then
- if #history > 0 then
- selectedOption = math.min(selectedOption + 1, #history + 1)
- text = history[selectedOption] or lastText
- end
- end
- end
- end
- -- Shut down the computer if the Ctrl-s key combo is pressed.
- if keysDown[29] and keysDown[31] then
- computer.shutdown(false)
- elseif keysDown[29] and keysDown[19] then
- computer.shutdown(true)
- end
- end
- end
- -- Edit a script --
- commands[18] = function()
- print("Type in the path of the script you want to edit.")
- local path = read()
- if path == "" then
- return
- end
- -- Load the script
- local scriptLines = {}
- do
- local handle, reason = filesystem.open(path)
- if handle then
- local buffer = ""
- repeat
- local data, reason = filesystem.read(handle, 2048)
- if not data and reason then
- error(reason)
- end
- buffer = buffer .. (data or "")
- until not data
- filesystem.close(handle)
- for line in buffer:gmatch("[^\r\n]*") do
- scriptLines[#scriptLines + 1] = line
- end
- end
- end
- -- Set up the commands
- local getArgument = function(value, expectedType, messageString)
- while true do
- if expectedType == "number" and tonumber(value) then
- break
- elseif expectedType == "string" and type(value) == "string" then
- break
- elseif expectedType == "boolean" then
- local evalValue = value:lower()
- if evalValue == ("yes"):sub(1, #evalValue) then
- return true
- elseif evalValue == ("true"):sub(1, #evalValue) then
- return true
- elseif evalValue == ("no"):sub(1, #evalValue) then
- return false
- elseif evalValue == ("false"):sub(1, #evalValue) then
- return false
- elseif evalValue == ("cancel"):sub(1, #evalValue) then
- return false
- end
- end
- print(messageString)
- value = read()
- end
- return value
- end
- local editCommands = {}
- -- Show a list of actions
- function editCommands.actions()
- print("Actions: ")
- print("'Actions' shows this again")
- print("'Edit [line] [text]' edits a line of text")
- print("'View [line]' displays a line of text")
- --print("'Insert [otherpath] [line]' inserts another script into this one")
- print("'Save [newpath]' saves the script")
- print("'Exit' will exit the script editor.")
- print("Save before exiting if you wanted to modify the script.")
- end
- editCommands.help = editCommands.actions
- -- Edits a line of text
- function editCommands.edit(line, text)
- line = getArgument(line, "number", "Type in the line you want to edit.")
- text = getArgument(line, "string", "Type in the text you want to replace line '"..line.."' with.")
- scriptLines[line] = text
- end
- -- Displays a line of text
- function editCommands.view(line)
- line = getArgument(line, "number", "Type in the line you want to view.")
- print(scriptLines[line])
- end
- -- Saves the script
- function editCommands.save(newpath)
- newpath = newpath or path
- local savehandle, errorMessage = filesystem.open(newpath)
- if savehandle then
- filesystem.write(savehandle, scriptLines[1])
- for line = 2, #scriptLines do
- filesystem.write(savehandle, "\n"..scriptLines[line])
- end
- filesystem.close()
- print("Saved to '"..newpath.."'")
- else
- screen.setFgColor(0xFF0000)
- print("Save failed: "..tostring(errorMessage))
- screen.setFgColor(0xFFFFFF)
- end
- end
- -- Open the text editor
- editCommands.actions()
- while true do
- local givenCommand = read()
- local command
- local arguments = {}
- for word in givenCommand:gmatch("%w+") do
- if command then
- arguments[#arguments + 1] = word
- else
- command = word:lower()
- end
- end
- if editCommands[command] then
- editCommands[command](table.unpack(arguments))
- elseif command == "exit" or command == "end" or command == "return" then
- return
- else
- print("That is not a known command.")
- end
- end
- end
- -- Run a script --
- commands[19] = function()
- print("Type in the path of the script you want to run.")
- local scriptName = read()
- local ok, errorMessage = pcall(dofile, scriptName)
- if not ok then
- screen.setFgColor(0xFF0000)
- print(errorMessage)
- screen.setFgColor(0xFFFFFF)
- end
- end
- -- Directory listing --
- commands[32] = function()
- print("Type in the directory to list.")
- local path = read()
- local items = filesystem.list(path)
- if items then
- print((#items == 1 and "One item" or (#items.." items")).." found in "..(path ~= "" and "'"..path.."'" or "the root")..".")
- for key, value in ipairs(items) do
- print(value)
- end
- else
- print("'"..path.."' is not a directory.")
- end
- end
- -- Select a new filesystem --
- commands[31] = function()
- print("List of filesystems")
- local selections = {}
- for key, value in pairs(filesystemComponents) do
- selections[key] = value
- print(key..": "..value, (component.invoke(value, "getLabel") or "No label"))
- local totalSpace, usedSpace = component.invoke(value, "spaceTotal"), component.invoke(value, "spaceUsed")
- local freeSpace = totalSpace - usedSpace
- local freeSpacePercent = math.floor(((totalSpace - usedSpace)/totalSpace)*1000)*.1
- print(freeSpacePercent.."% free", "Total space: "..totalSpace.." bytes", "Used space: "..usedSpace.." bytes")
- end
- print("Select a filesystem: ")
- while true do
- local answer = read(selections)
- if tonumber(answer) then
- answer = selections[tonumber(answer)] or answer
- end
- -- Select the correct address.
- local newSelectedFilesystem
- for key, value in pairs(filesystemComponents) do
- local label = component.invoke(value, "getLabel")
- if answer == value:sub(1, #answer) then
- -- The user typed in the address of the filesystem. We are done, select it.
- newSelectedFilesystem = value
- break
- elseif (answer == label) and label then
- -- Similarly, the user can type in the label of the filesystem.
- answer = value
- break
- end
- end
- -- If the newSelectedFilesystem variable has been set, then we can select it as the filesystem.
- if newSelectedFilesystem then
- -- Check if the filesystem exists first.
- selectedFilesystem = newSelectedFilesystem
- print("The selected filesystem is now "..selectedFilesystem)
- return
- end
- print("That is not a valid filesystem. Please type in the number of the filesystem you want to use.")
- end
- end
- -- Lua prompt --
- commands[38] = function()
- print("Lua prompt")
- local history = {}
- while true do
- local line = read(history)
- -- Insert this line into the history, but only if is not in the history.
- local doInsert = true
- for key = 1, #history do
- if history[key] == line then
- doInsert = false
- end
- end
- if doInsert then
- history[#history + 1] = line
- end
- -- Load the script if possible. If not, return an error.
- local script, errorMessage = load(line, "=script")
- if script then
- local returnedValues = {pcall(script)}
- -- Print returned values unless an error occured, in which case print the error in red.
- if returnedValues[1] then
- table.remove(returnedValues, 1)
- if #returnedValues > 0 then
- print(table.unpack(returnedValues))
- end
- else
- screen.setFgColor(0xFF0000)
- print(returnedValues[2])
- screen.setFgColor(0xFFFFFF)
- end
- else
- screen.setFgColor(0xFF0000)
- print(errorMessage)
- screen.setFgColor(0xFFFFFF)
- end
- end
- end
- -- Print components --
- commands[46] = function()
- for key, value in component.list() do
- print(key, value)
- local methods = component.methods(key)
- if type(methods) == "table" then
- for key2, value2 in pairs(methods) do
- print(" ", key2, ((value2 == true and "Direct") or "Indirect").." function")
- end
- computer.pullSignal("key_up")
- end
- end
- end
- -- Command list --
- commands[35] = function()
- print("Selected filesystem is "..selectedFilesystem)
- print("Press 's' to select a new filesystem")
- print("Press 'e' to edit a script")
- print("Press 'd' to show a list of files")
- print("Press 'l' to open a Lua prompt")
- print("Press 'r' to run a script")
- print("Press 'c' to print all available components")
- print("Press 'h' to show this again")
- print("Hold 'Ctrl' and 'e' to enable or disable the printing of events here")
- print("Hold 'Ctrl' and 'r' to reboot")
- print("Hold 'Ctrl' and 's' to shutdown")
- end
- local ok, errorMessage = pcall(dofile, "autorun.lua")
- if not ok and errorMessage ~= "file not found" then
- screen.setFgColor(0xFF0000)
- print(errorMessage)
- screen.setFgColor(0xFFFFFF)
- end
- local printEvents = false
- commands[35]()
- while true do
- local event = {computer.pullSignal()}
- if printEvents then
- print(table.unpack(event))
- end
- if event[1] == "key_up" then
- keysDown[event[4]] = false
- elseif event[1] == "key_down" then
- keysDown[event[4]] = true
- end
- if keysDown[29] and keysDown[31] then
- computer.shutdown(false)
- elseif keysDown[29] and keysDown[19] then
- computer.shutdown(true)
- elseif keysDown[29] and keysDown[18] then
- printEvents = not printEvents
- print((printEvents and "Now" or "No longer").." printing events on this screen")
- elseif commands[event[4]] then
- print("")
- commands[event[4]]()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement