From bc294508b9d6566add6605bb7da09d3f6b74f9bb Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 15 Jul 2019 13:12:04 -0400 Subject: Corrections and new API! --- system/apis/json.lua | 209 +++++++++++++++++++++++++++++++++++++++++++ system/boot.lua | 2 +- system/post-setup.lua | 16 ++-- system/recovery/main.lua | 27 ++++++ system/recovery/reset.lua | 36 ++++++++ system/recovery/transfer.lua | 29 ++++++ system/reset.lua | 36 -------- system/transfer.lua | 13 --- 8 files changed, 313 insertions(+), 55 deletions(-) create mode 100644 system/apis/json.lua create mode 100644 system/recovery/main.lua create mode 100644 system/recovery/reset.lua create mode 100644 system/recovery/transfer.lua delete mode 100644 system/reset.lua delete mode 100644 system/transfer.lua (limited to 'system') diff --git a/system/apis/json.lua b/system/apis/json.lua new file mode 100644 index 0000000..3bef482 --- /dev/null +++ b/system/apis/json.lua @@ -0,0 +1,209 @@ +------------------------------------------------------------------ utils +local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"} + +local function isArray(t) + local max = 0 + for k,v in pairs(t) do + if type(k) ~= "number" then + return false + elseif k > max then + max = k + end + end + return max == #t +end + +local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true} +function removeWhite(str) + while whites[str:sub(1, 1)] do + str = str:sub(2) + end + return str +end + +------------------------------------------------------------------ encoding + +local function encodeCommon(val, pretty, tabLevel, tTracking) + local str = "" + + -- Tabbing util + local function tab(s) + str = str .. ("\t"):rep(tabLevel) .. s + end + + local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc) + str = str .. bracket + if pretty then + str = str .. "\n" + tabLevel = tabLevel + 1 + end + for k,v in iterator(val) do + tab("") + loopFunc(k,v) + str = str .. "," + if pretty then str = str .. "\n" end + end + if pretty then + tabLevel = tabLevel - 1 + end + if str:sub(-2) == ",\n" then + str = str:sub(1, -3) .. "\n" + elseif str:sub(-1) == "," then + str = str:sub(1, -2) + end + tab(closeBracket) + end + + -- Table encoding + if type(val) == "table" then + assert(not tTracking[val], "Cannot encode a table holding itself recursively") + tTracking[val] = true + if isArray(val) then + arrEncoding(val, "[", "]", ipairs, function(k,v) + str = str .. encodeCommon(v, pretty, tabLevel, tTracking) + end) + else + arrEncoding(val, "{", "}", pairs, function(k,v) + assert(type(k) == "string", "JSON object keys must be strings", 2) + str = str .. encodeCommon(k, pretty, tabLevel, tTracking) + str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking) + end) + end + -- String encoding + elseif type(val) == "string" then + str = '"' .. val:gsub("[%c\"\\]", controls) .. '"' + -- Number encoding + elseif type(val) == "number" or type(val) == "boolean" then + str = tostring(val) + else + error("JSON only supports arrays, objects, numbers, booleans, and strings", 2) + end + return str +end + +function encode(val) + return encodeCommon(val, false, 0, {}) +end + +function encodePretty(val) + return encodeCommon(val, true, 0, {}) +end + +------------------------------------------------------------------ decoding + +local decodeControls = {} +for k,v in pairs(controls) do + decodeControls[v] = k +end + +function parseBoolean(str) + if str:sub(1, 4) == "true" then + return true, removeWhite(str:sub(5)) + else + return false, removeWhite(str:sub(6)) + end +end + +function parseNull(str) + return nil, removeWhite(str:sub(5)) +end + +local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true} +function parseNumber(str) + local i = 1 + while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do + i = i + 1 + end + local val = tonumber(str:sub(1, i - 1)) + str = removeWhite(str:sub(i)) + return val, str +end + +function parseString(str) + str = str:sub(2) + local s = "" + while str:sub(1,1) ~= "\"" do + local next = str:sub(1,1) + str = str:sub(2) + assert(next ~= "\n", "Unclosed string") + + if next == "\\" then + local escape = str:sub(1,1) + str = str:sub(2) + + next = assert(decodeControls[next..escape], "Invalid escape character") + end + + s = s .. next + end + return s, removeWhite(str:sub(2)) +end + +function parseArray(str) + str = removeWhite(str:sub(2)) + + local val = {} + local i = 1 + while str:sub(1, 1) ~= "]" do + local v = nil + v, str = parseValue(str) + val[i] = v + i = i + 1 + str = removeWhite(str) + end + str = removeWhite(str:sub(2)) + return val, str +end + +function parseObject(str) + str = removeWhite(str:sub(2)) + + local val = {} + while str:sub(1, 1) ~= "}" do + local k, v = nil, nil + k, v, str = parseMember(str) + val[k] = v + str = removeWhite(str) + end + str = removeWhite(str:sub(2)) + return val, str +end + +function parseMember(str) + local k = nil + k, str = parseValue(str) + local val = nil + val, str = parseValue(str) + return k, val, str +end + +function parseValue(str) + local fchar = str:sub(1, 1) + if fchar == "{" then + return parseObject(str) + elseif fchar == "[" then + return parseArray(str) + elseif tonumber(fchar) ~= nil or numChars[fchar] then + return parseNumber(str) + elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then + return parseBoolean(str) + elseif fchar == "\"" then + return parseString(str) + elseif str:sub(1, 4) == "null" then + return parseNull(str) + end + return nil +end + +function decode(str) + str = removeWhite(str) + t = parseValue(str) + return t +end + +function decodeFromFile(path) + local file = assert(fs.open(path, "r")) + local decoded = decode(file.readAll()) + file.close() + return decoded +end \ No newline at end of file diff --git a/system/boot.lua b/system/boot.lua index 08d5d64..5418427 100644 --- a/system/boot.lua +++ b/system/boot.lua @@ -80,7 +80,7 @@ end sleep(2) term.setTextColor(colors.green) -print("[DONE] Boot sequence is completed...") +print("[DONE] Boot sequence has been completed...") term.setTextColor(colors.white) sleep(2) shell.run(desktop) \ No newline at end of file diff --git a/system/post-setup.lua b/system/post-setup.lua index 1e6bfe0..9ff14d6 100644 --- a/system/post-setup.lua +++ b/system/post-setup.lua @@ -38,12 +38,18 @@ print("Welcome to the bits-UI Post Setup!") sleep(2) print("Please enter your password.") print("(Don't set your real password in servers.)") + local passPath = "/etc/passwd.pwd" -local passwd = read(" ") -local insertPasswd = fs.open(passPath, "a") -insertPasswd.writeLine(passwd) -insertPasswd.close() -print("Thanks, I will save that.") +if fs.exists(passPath) then + print("[INFO] Password file exists! Skipping.") + sleep(2) +else + local passwd = read(" ") + local insertPasswd = fs.open(passPath, "a") + insertPasswd.writeLine(passwd) + insertPasswd.close() + print("Thanks, I will save that.") +end sleep(1) shell.run("/system/desktop.lua") \ No newline at end of file diff --git a/system/recovery/main.lua b/system/recovery/main.lua new file mode 100644 index 0000000..39c1ec5 --- /dev/null +++ b/system/recovery/main.lua @@ -0,0 +1,27 @@ +--[[ + bits-UI Recovery Script: A script that's going to show you to either reset the computer or transfer files. + Copyright (C) 2019 Alee14 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +]]-- +term.clear() +term.setCursorPos(1,1) +term.setTextColor(colors.white) +print("Welcome to the bits-UI recovery mode!") +sleep(2) +print("Do you want to either reset or transfer?") +local input = read(); +if input == "reset" then + shell.run("/system/recovery/reset.lua") +elseif input == "transfer" then + shell.run("/system/recovery/transfer.lua") +end \ No newline at end of file diff --git a/system/recovery/reset.lua b/system/recovery/reset.lua new file mode 100644 index 0000000..9b8efb4 --- /dev/null +++ b/system/recovery/reset.lua @@ -0,0 +1,36 @@ +--[[ + bits-UI Reset Script: A script that will wipe the system to the default factory settings. + Copyright (C) 2019 Alee14 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +]]-- +function clear() + term.clear() + term.setCursorPos(1,1) + term.setTextColor(colors.white) +end +clear() +print("Are you sure you want to reset bits-UI? (y/n)") +local input = read() +if input == "y" then + print("Erasing all user stored data...") + fs.delete("/home") + fs.delete("/etc/passwd.pwd") + sleep(2) + print("Erased all data...") + sleep(2) + print("Rebooting...") + sleep(3) + os.reboot() +else + shell.run("startup.lua") +end \ No newline at end of file diff --git a/system/recovery/transfer.lua b/system/recovery/transfer.lua new file mode 100644 index 0000000..56be78c --- /dev/null +++ b/system/recovery/transfer.lua @@ -0,0 +1,29 @@ +--[[ + bits-UI Transfer Script: A script that will transfer files from one system to the other. + Copyright (C) 2019 Alee14 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +]]-- +term.clear() +term.setCursorPos(1,1) +term.setTextColor(colors.white) +print("Welcome to the bits-UI transfer!") +sleep(2) +if fs.exists("/disk") then + shell.run("copy", "/home", "/disk") + shell.run("copy", "/etc/passwd.pwd", "/disk") +else + print("You need a floppy disk to copy over data.") + print("Transfer has been halted.") + sleep(2) + shell.run("/startup.lua") +end \ No newline at end of file diff --git a/system/reset.lua b/system/reset.lua deleted file mode 100644 index 9b8efb4..0000000 --- a/system/reset.lua +++ /dev/null @@ -1,36 +0,0 @@ ---[[ - bits-UI Reset Script: A script that will wipe the system to the default factory settings. - Copyright (C) 2019 Alee14 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - -]]-- -function clear() - term.clear() - term.setCursorPos(1,1) - term.setTextColor(colors.white) -end -clear() -print("Are you sure you want to reset bits-UI? (y/n)") -local input = read() -if input == "y" then - print("Erasing all user stored data...") - fs.delete("/home") - fs.delete("/etc/passwd.pwd") - sleep(2) - print("Erased all data...") - sleep(2) - print("Rebooting...") - sleep(3) - os.reboot() -else - shell.run("startup.lua") -end \ No newline at end of file diff --git a/system/transfer.lua b/system/transfer.lua deleted file mode 100644 index cc25dac..0000000 --- a/system/transfer.lua +++ /dev/null @@ -1,13 +0,0 @@ -term.clear() -term.setCursorPos(1,1) -term.setTextColor(colors.white) -print("Welcome to the bits-UI transfer!") -sleep(2) -if fs.exists("/disk") then - shell.run("copy", "/home", "/disk") - shell.run("copy", "/etc/passwd.pwd", "/disk") -else - print("You need a floppy disk to copy over data.") - print("Installation has been halted.") - sleep(2) -end \ No newline at end of file -- cgit v1.2.3