From f40bad3fba14ed398ffbe06a62f4b4f6233db1e6 Mon Sep 17 00:00:00 2001 From: payonel Date: Sat, 24 Jun 2017 20:25:45 -0700 Subject: [PATCH 1/9] protect env.load if defined, else use mt. simplify free by using io.write as print loads buffered_write --- .../opencomputers/loot/openos/bin/free.lua | 4 +-- .../loot/openos/boot/01_process.lua | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/free.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/free.lua index ea3f6d0d0..cc637f3d4 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/free.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/free.lua @@ -1,8 +1,8 @@ local computer = require("computer") local total = computer.totalMemory() local max = 0 -for i=1,40 do +for _=1,40 do max = math.max(max, computer.freeMemory()) os.sleep(0) -- invokes gc end -print(string.format("Total%12d\nUsed%13d\nFree%13d", total, total - max, max)) +io.write(string.format("Total%12d\nUsed%13d\nFree%13d\n", total, total - max, max)) diff --git a/src/main/resources/assets/opencomputers/loot/openos/boot/01_process.lua b/src/main/resources/assets/opencomputers/loot/openos/boot/01_process.lua index 9ae2e27be..6b9943e69 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/boot/01_process.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/boot/01_process.lua @@ -27,21 +27,26 @@ local intercept_load intercept_load = function(source, label, mode, env) if env then local prev_load = env.load or intercept_load - local env_mt = getmetatable(env) or {} - local env_mt_index = env_mt.__index - env_mt.__index = function(tbl, key) - if key == "load" then - return function(_source, _label, _mode, _env) - return prev_load(_source, _label, _mode, _env or env) - end - elseif type(env_mt_index) == "table" then - return env_mt_index[key] - elseif env_mt_index then - return env_mt_index(tbl, key) - end - return nil + local next_load = function(_source, _label, _mode, _env) + return prev_load(_source, _label, _mode, _env or env) + end + if rawget(env, "load") then -- overwrite load + env.load = next_load + else -- else it must be an __index load, or it didn't have one + local env_mt = getmetatable(env) or {} + local env_mt_index = env_mt.__index + env_mt.__index = function(tbl, key) + if key == "load" then + return next_load + elseif type(env_mt_index) == "table" then + return env_mt_index[key] + elseif env_mt_index then + return env_mt_index(tbl, key) + end + return nil + end + setmetatable(env, env_mt) end - setmetatable(env, env_mt) end return kernel_load(source, label, mode, env or process.info().env) end From 6a2586b536ccdd4da2901ae2acc0a1d335179d73 Mon Sep 17 00:00:00 2001 From: payonel Date: Sat, 24 Jun 2017 23:43:19 -0700 Subject: [PATCH 2/9] bisect filesystem lib for memory reduction to boot to shell --- .../loot/openos/boot/90_filesystem.lua | 2 + .../loot/openos/lib/filesystem.lua | 199 +++--------------- .../opencomputers/loot/openos/lib/package.lua | 12 +- .../opencomputers/loot/openos/lib/sh.lua | 1 - .../opencomputers/loot/openos/lib/text.lua | 1 - .../loot/openos/opt/core/full_filesystem.lua | 138 ++++++++++++ 6 files changed, 176 insertions(+), 177 deletions(-) create mode 100644 src/main/resources/assets/opencomputers/loot/openos/opt/core/full_filesystem.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua b/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua index 5b0da6ca7..8c71f2cad 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua @@ -63,3 +63,5 @@ end event.listen("init", onInit) event.listen("component_added", onComponentAdded) event.listen("component_removed", onComponentRemoved) + +require("package").delay(fs, "/opt/core/full_filesystem.lua") diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/filesystem.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/filesystem.lua index 2346e7ae7..2171f4279 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/filesystem.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/filesystem.lua @@ -1,7 +1,7 @@ local component = require("component") local unicode = require("unicode") -local filesystem, fileStream = {}, {} +local filesystem = {} local isAutorunEnabled = nil local mtab = {name="", children={}, links={}} local fstab = {} @@ -116,8 +116,6 @@ function filesystem.setAutorunEnabled(value) saveConfig() end -filesystem.segments = segments - function filesystem.canonical(path) local result = table.concat(segments(path), "/") if unicode.sub(path, 1, 1) == "/" then @@ -165,45 +163,6 @@ function filesystem.realPath(path) return table.concat(parts, "/") end -function filesystem.isLink(path) - local name = filesystem.name(path) - local node, rest, vnode, vrest = findNode(filesystem.path(path), false, true) - if not node then return nil, rest end - local target = vnode.links[name] - -- having vrest here indicates we are not at the - -- owning vnode due to a mount point above this point - -- but we can have a target when there is a link at - -- the mount point root, with the same name - if not vrest and target ~= nil then - return true, target - end - return false -end - -function filesystem.link(target, linkpath) - checkArg(1, target, "string") - checkArg(2, linkpath, "string") - - if filesystem.exists(linkpath) then - return nil, "file already exists" - end - local linkpath_parent = filesystem.path(linkpath) - if not filesystem.exists(linkpath_parent) then - return nil, "no such directory" - end - local linkpath_real, reason = filesystem.realPath(linkpath_parent) - if not linkpath_real then - return nil, reason - end - if not filesystem.isDirectory(linkpath_real) then - return nil, "not a directory" - end - - local _, _, vnode, _ = findNode(linkpath_real, true) - vnode.links[filesystem.name(linkpath)] = target - return true -end - function filesystem.mount(fs, path) checkArg(1, fs, "string", "table") if type(fs) == "string" then @@ -260,17 +219,6 @@ function filesystem.mount(fs, path) return true end -function filesystem.mounts() - local tmp = {} - for path,node in pairs(fstab) do - table.insert(tmp, {node.fs,path}) - end - return function() - local next = table.remove(tmp) - if next then return table.unpack(next) end - end -end - function filesystem.path(path) local parts = segments(path) local result = table.concat(parts, "/", 1, #parts - 1) .. "/" @@ -306,33 +254,6 @@ function filesystem.proxy(filter) return component.proxy(address) end -function filesystem.umount(fsOrPath) - checkArg(1, fsOrPath, "string", "table") - local real - local fs - local addr - if type(fsOrPath) == "string" then - real = filesystem.realPath(fsOrPath) - addr = fsOrPath - else -- table - fs = fsOrPath - end - - local paths = {} - for path,node in pairs(fstab) do - if real == path or addr == node.fs.address or fs == node.fs then - table.insert(paths, path) - end - end - for _,path in ipairs(paths) do - local node = fstab[path] - fstab[path] = nil - node.fs = nil - node.parent.children[node.name] = nil - end - return #paths > 0 -end - function filesystem.exists(path) if not filesystem.realPath(filesystem.path(path)) then return false @@ -346,17 +267,6 @@ function filesystem.exists(path) return false end -function filesystem.size(path) - local node, rest, vnode, vrest = findNode(path, false, true) - if not node or not vnode.fs and (not vrest or vnode.links[vrest]) then - return 0 -- virtual directory or symlink - end - if node.fs and rest then - return node.fs.size(rest) - end - return 0 -- no such file or directory -end - function filesystem.isDirectory(path) local real, reason = filesystem.realPath(path) if not real then return nil, reason end @@ -370,17 +280,6 @@ function filesystem.isDirectory(path) return false end -function filesystem.lastModified(path) - local node, rest, vnode, vrest = findNode(path, false, true) - if not node or not vnode.fs and not vrest then - return 0 -- virtual directory - end - if node.fs and rest then - return node.fs.lastModified(rest) - end - return 0 -- no such file or directory -end - function filesystem.list(path) local node, rest, vnode, vrest = findNode(path, false, true) local result = {} @@ -410,24 +309,6 @@ function filesystem.list(path) end end -function filesystem.makeDirectory(path) - if filesystem.exists(path) then - return nil, "file or directory with that name already exists" - end - local node, rest = findNode(path) - if node.fs and rest then - local success, reason = node.fs.makeDirectory(rest) - if not success and not reason and node.fs.isReadOnly() then - reason = "filesystem is readonly" - end - return success, reason - end - if node.fs then - return nil, "virtual directory with that name already exists" - end - return nil, "cannot create a directory in a virtual directory" -end - function filesystem.remove(path) return require("tools/fsmod").remove(path, findNode) end @@ -436,53 +317,6 @@ function filesystem.rename(oldPath, newPath) return require("tools/fsmod").rename(oldPath, newPath, findNode) end -function filesystem.copy(fromPath, toPath) - local data = false - local input, reason = filesystem.open(fromPath, "rb") - if input then - local output, reason = filesystem.open(toPath, "wb") - if output then - repeat - data, reason = input:read(1024) - if not data then break end - data, reason = output:write(data) - if not data then data, reason = false, "failed to write" end - until not data - output:close() - end - input:close() - end - return data == nil, reason -end - -function fileStream:close() - if self.handle then - self.fs.close(self.handle) - self.handle = nil - end -end - -function fileStream:read(n) - if not self.handle then - return nil, "file is closed" - end - return self.fs.read(self.handle, n) -end - -function fileStream:seek(whence, offset) - if not self.handle then - return nil, "file is closed" - end - return self.fs.seek(self.handle, whence, offset) -end - -function fileStream:write(str) - if not self.handle then - return nil, "file is closed" - end - return self.fs.write(self.handle, str) -end - function filesystem.open(path, mode) checkArg(1, path, "string") mode = tostring(mode or "r") @@ -504,13 +338,36 @@ function filesystem.open(path, mode) return nil, reason end - local stream = {fs = node.fs, handle = handle} + local function create_handle_method(key) + return function(self, ...) + if not self.handle then + return nil, "file is closed" + end + return self.fs[key](self.handle, ...) + end + end - local metatable = {__index = fileStream, - __metatable = "filestream"} - return setmetatable(stream, metatable) + local stream = + { + fs = node.fs, + handle = handle, + close = function(self) + if self.handle then + self.fs.close(self.handle) + self.handle = nil + end + end + } + stream.read = create_handle_method("read") + stream.seek = create_handle_method("seek") + stream.write = create_handle_method("write") + return stream end +filesystem.findNode = findNode +filesystem.segments = segments +filesystem.fstab = fstab + ------------------------------------------------------------------------------- return filesystem diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua index ebecdfd77..3f356ebff 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua @@ -70,14 +70,18 @@ function require(module) end function package.delay(lib, file) - setmetatable(lib, - { + local mt = { __index = function(tbl, key) - setmetatable(tbl, nil) + setmetatable(lib, nil) + setmetatable(lib.internal or {}, nil) dofile(file) return tbl[key] end - }) + } + if lib.internal then + setmetatable(lib.internal, mt) + end + setmetatable(lib, mt) end ------------------------------------------------------------------------------- diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua index a17cf0ddd..3b699607f 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua @@ -273,6 +273,5 @@ function sh.hintHandler(full_line, cursor) end require("package").delay(sh, "/opt/core/full_sh.lua") -require("package").delay(sh.internal, "/opt/core/full_sh.lua") return sh diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua index 764ba408f..1f527ebbb 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua @@ -145,6 +145,5 @@ function text.internal.words(input, options) end require("package").delay(text, "/opt/core/full_text.lua") -require("package").delay(text.internal, "/opt/core/full_text.lua") return text diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_filesystem.lua b/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_filesystem.lua new file mode 100644 index 000000000..eee2ef421 --- /dev/null +++ b/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_filesystem.lua @@ -0,0 +1,138 @@ +local filesystem = require("filesystem") + +function filesystem.makeDirectory(path) + if filesystem.exists(path) then + return nil, "file or directory with that name already exists" + end + local node, rest = filesystem.findNode(path) + if node.fs and rest then + local success, reason = node.fs.makeDirectory(rest) + if not success and not reason and node.fs.isReadOnly() then + reason = "filesystem is readonly" + end + return success, reason + end + if node.fs then + return nil, "virtual directory with that name already exists" + end + return nil, "cannot create a directory in a virtual directory" +end + +function filesystem.lastModified(path) + local node, rest, vnode, vrest = filesystem.findNode(path, false, true) + if not node or not vnode.fs and not vrest then + return 0 -- virtual directory + end + if node.fs and rest then + return node.fs.lastModified(rest) + end + return 0 -- no such file or directory +end + +function filesystem.mounts() + local tmp = {} + for path,node in pairs(filesystem.fstab) do + table.insert(tmp, {node.fs,path}) + end + return function() + local next = table.remove(tmp) + if next then return table.unpack(next) end + end +end + +function filesystem.link(target, linkpath) + checkArg(1, target, "string") + checkArg(2, linkpath, "string") + + if filesystem.exists(linkpath) then + return nil, "file already exists" + end + local linkpath_parent = filesystem.path(linkpath) + if not filesystem.exists(linkpath_parent) then + return nil, "no such directory" + end + local linkpath_real, reason = filesystem.realPath(linkpath_parent) + if not linkpath_real then + return nil, reason + end + if not filesystem.isDirectory(linkpath_real) then + return nil, "not a directory" + end + + local _, _, vnode, _ = filesystem.findNode(linkpath_real, true) + vnode.links[filesystem.name(linkpath)] = target + return true +end + +function filesystem.umount(fsOrPath) + checkArg(1, fsOrPath, "string", "table") + local real + local fs + local addr + if type(fsOrPath) == "string" then + real = filesystem.realPath(fsOrPath) + addr = fsOrPath + else -- table + fs = fsOrPath + end + + local paths = {} + for path,node in pairs(filesystem.fstab) do + if real == path or addr == node.fs.address or fs == node.fs then + table.insert(paths, path) + end + end + for _,path in ipairs(paths) do + local node = filesystem.fstab[path] + filesystem.fstab[path] = nil + node.fs = nil + node.parent.children[node.name] = nil + end + return #paths > 0 +end + +function filesystem.size(path) + local node, rest, vnode, vrest = filesystem.findNode(path, false, true) + if not node or not vnode.fs and (not vrest or vnode.links[vrest]) then + return 0 -- virtual directory or symlink + end + if node.fs and rest then + return node.fs.size(rest) + end + return 0 -- no such file or directory +end + +function filesystem.isLink(path) + local name = filesystem.name(path) + local node, rest, vnode, vrest = filesystem.findNode(filesystem.path(path), false, true) + if not node then return nil, rest end + local target = vnode.links[name] + -- having vrest here indicates we are not at the + -- owning vnode due to a mount point above this point + -- but we can have a target when there is a link at + -- the mount point root, with the same name + if not vrest and target ~= nil then + return true, target + end + return false +end + +function filesystem.copy(fromPath, toPath) + local data = false + local input, reason = filesystem.open(fromPath, "rb") + if input then + local output, reason = filesystem.open(toPath, "wb") + if output then + repeat + data, reason = input:read(1024) + if not data then break end + data, reason = output:write(data) + if not data then data, reason = false, "failed to write" end + until not data + output:close() + end + input:close() + end + return data == nil, reason +end + From e3525d9a00f85d2626e8ce1775e42bfd78f57387 Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 25 Jun 2017 09:11:20 -0700 Subject: [PATCH 3/9] safer to try to load a library full before removing metatable in case of oom --- .../resources/assets/opencomputers/loot/openos/lib/package.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua index 3f356ebff..394247935 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/package.lua @@ -72,9 +72,9 @@ end function package.delay(lib, file) local mt = { __index = function(tbl, key) + dofile(file) setmetatable(lib, nil) setmetatable(lib.internal or {}, nil) - dofile(file) return tbl[key] end } From dc7a1940201c29c99c841b04b0a37208728bb44d Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 25 Jun 2017 09:31:58 -0700 Subject: [PATCH 4/9] moving /opt/core to /lib/core --- .../assets/opencomputers/loot/openos/bin/install.lua | 2 +- .../assets/opencomputers/loot/openos/bin/ls.lua | 2 +- .../assets/opencomputers/loot/openos/bin/lua.lua | 2 +- .../assets/opencomputers/loot/openos/bin/mount.lua | 6 +++--- .../opencomputers/loot/openos/boot/90_filesystem.lua | 2 +- .../assets/opencomputers/loot/openos/init.lua | 2 +- .../loot/openos/{opt => lib}/core/boot.lua | 0 .../loot/openos/{opt => lib}/core/devfs/01_hw.lua | 3 +-- .../loot/openos/{opt => lib}/core/devfs/02_utils.lua | 0 .../{opt => lib}/core/devfs/adapters/computer.lua | 0 .../{opt => lib}/core/devfs/adapters/eeprom.lua | 0 .../{opt => lib}/core/devfs/adapters/filesystem.lua | 0 .../openos/{opt => lib}/core/devfs/adapters/gpu.lua | 0 .../{opt => lib}/core/devfs/adapters/internet.lua | 0 .../{opt => lib}/core/devfs/adapters/modem.lua | 0 .../{opt => lib}/core/devfs/adapters/screen.lua | 0 .../openos/{opt => lib}/core/device_labeling.lua | 0 .../openos/{opt => lib}/core/full_filesystem.lua | 0 .../loot/openos/{opt => lib}/core/full_keyboard.lua | 0 .../loot/openos/{opt => lib}/core/full_ls.lua | 0 .../loot/openos/{opt => lib}/core/full_sh.lua | 0 .../loot/openos/{opt => lib}/core/full_text.lua | 0 .../openos/{opt => lib}/core/full_transforms.lua | 0 .../loot/openos/{opt => lib}/core/full_tty.lua | 0 .../loot/openos/{opt => lib}/core/install_basics.lua | 2 +- .../loot/openos/{opt => lib}/core/install_utils.lua | 0 .../loot/openos/{opt => lib}/core/lua_shell.lua | 0 .../loot/openos/{opt => lib}/core/ro_wrapper.lua | 0 .../assets/opencomputers/loot/openos/lib/devfs.lua | 4 ++-- .../opencomputers/loot/openos/lib/keyboard.lua | 2 +- .../assets/opencomputers/loot/openos/lib/sh.lua | 2 +- .../assets/opencomputers/loot/openos/lib/text.lua | 8 ++++---- .../opencomputers/loot/openos/lib/transforms.lua | 2 +- .../assets/opencomputers/loot/openos/lib/tty.lua | 12 ++++++------ 34 files changed, 25 insertions(+), 26 deletions(-) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/boot.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/01_hw.lua (97%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/02_utils.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/computer.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/eeprom.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/filesystem.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/gpu.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/internet.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/modem.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/devfs/adapters/screen.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/device_labeling.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_filesystem.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_keyboard.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_ls.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_sh.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_text.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_transforms.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/full_tty.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/install_basics.lua (99%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/install_utils.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/lua_shell.lua (100%) rename src/main/resources/assets/opencomputers/loot/openos/{opt => lib}/core/ro_wrapper.lua (100%) diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/install.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/install.lua index 2358ada49..f8410e98b 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/install.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/install.lua @@ -4,7 +4,7 @@ local shell = require("shell") local options do - local basic, reason = loadfile("/opt/core/install_basics.lua", "bt", _G) + local basic, reason = loadfile("/lib/core/install_basics.lua", "bt", _G) if not basic then io.stderr:write("failed to load install: " .. tostring(reason) .. "\n") return 1 diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/ls.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/ls.lua index 4a3db6f9c..25a67968f 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/ls.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/ls.lua @@ -1,7 +1,7 @@ -- load complex, if we can (might be low on memory) local ok, why = pcall(function(...) - return loadfile("/opt/core/full_ls.lua", "bt", _G)(...) + return loadfile("/lib/core/full_ls.lua", "bt", _G)(...) end, ...) if not ok then diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/lua.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/lua.lua index ebd25f995..d534865df 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/lua.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/lua.lua @@ -2,7 +2,7 @@ local shell = require("shell") local args = shell.parse(...) if #args == 0 then - args = {"/opt/core/lua_shell.lua"} + args = {"/lib/core/lua_shell.lua"} end local filename = args[1] diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/mount.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/mount.lua index cfc92bff9..de5eb75b7 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/mount.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/mount.lua @@ -55,12 +55,12 @@ else io.stderr:write(reason,"\n") return 1 elseif ops.r then - proxy = dofile("/opt/core/ro_wrapper.lua").wrap(proxy) + proxy = dofile("/lib/core/ro_wrapper.lua").wrap(proxy) end - local result, reason = fs.mount(proxy, shell.resolve(args[2])) + local result, mount_failure = fs.mount(proxy, shell.resolve(args[2])) if not result then - io.stderr:write(reason,"\n") + io.stderr:write(mount_failure, "\n") return 2 -- error code end end diff --git a/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua b/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua index 8c71f2cad..5e21ed7ed 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/boot/90_filesystem.lua @@ -64,4 +64,4 @@ event.listen("init", onInit) event.listen("component_added", onComponentAdded) event.listen("component_removed", onComponentRemoved) -require("package").delay(fs, "/opt/core/full_filesystem.lua") +require("package").delay(fs, "/lib/core/full_filesystem.lua") diff --git a/src/main/resources/assets/opencomputers/loot/openos/init.lua b/src/main/resources/assets/opencomputers/loot/openos/init.lua index 28d4ba670..0e7b102a6 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/init.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/init.lua @@ -13,7 +13,7 @@ do invoke(addr, "close", handle) return load(buffer, "=" .. file, "bt", _G) end]], "=loadfile", "bt", _G)() - loadfile("/opt/core/boot.lua")(loadfile) + loadfile("/lib/core/boot.lua")(loadfile) end while true do diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/boot.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/boot.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/01_hw.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/01_hw.lua similarity index 97% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/01_hw.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/01_hw.lua index cc4712124..50e4a3147 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/01_hw.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/01_hw.lua @@ -1,10 +1,9 @@ local comp = require("component") -local fs = require("filesystem") local text = require("text") local dcache = {} local pcache = {} -local adapter_pwd = "/opt/core/devfs/adapters/" +local adapter_pwd = "/lib/core/devfs/adapters/" local adapter_api = {} diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/02_utils.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/02_utils.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/02_utils.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/02_utils.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/computer.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/computer.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/computer.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/computer.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/eeprom.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/eeprom.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/eeprom.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/eeprom.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/filesystem.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/filesystem.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/filesystem.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/filesystem.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/gpu.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/gpu.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/gpu.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/gpu.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/internet.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/internet.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/internet.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/internet.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/modem.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/modem.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/modem.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/modem.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/screen.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/screen.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/devfs/adapters/screen.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/devfs/adapters/screen.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/device_labeling.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/device_labeling.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/device_labeling.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/device_labeling.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_filesystem.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_filesystem.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_filesystem.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_filesystem.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_keyboard.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_keyboard.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_keyboard.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_keyboard.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_ls.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_ls.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_ls.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_ls.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_sh.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_sh.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_sh.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_sh.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_text.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_text.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_text.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_text.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_transforms.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_transforms.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_transforms.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_transforms.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/full_tty.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_tty.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/full_tty.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/full_tty.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/install_basics.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua similarity index 99% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/install_basics.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua index 7e4c21be6..6673f1a76 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/opt/core/install_basics.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua @@ -22,7 +22,7 @@ if options.help then return nil -- exit success end -local utils_path = "/opt/core/install_utils.lua" +local utils_path = "/lib/core/install_utils.lua" local utils local rootfs = fs.get("/") diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/install_utils.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_utils.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/install_utils.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/install_utils.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/lua_shell.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/lua_shell.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/opt/core/ro_wrapper.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/ro_wrapper.lua similarity index 100% rename from src/main/resources/assets/opencomputers/loot/openos/opt/core/ro_wrapper.lua rename to src/main/resources/assets/opencomputers/loot/openos/lib/core/ro_wrapper.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/devfs.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/devfs.lua index c1ef26203..66cbb26a5 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/devfs.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/devfs.lua @@ -153,7 +153,7 @@ inject_dynamic_pairs = function(fsnode, path, bStoreUse) }) end -local label_lib = dofile("/opt/core/device_labeling.lua") +local label_lib = dofile("/lib/core/device_labeling.lua") label_lib.loadRules() api.getDeviceLabel = label_lib.getDeviceLabel api.setDeviceLabel = label_lib.setDeviceLabel @@ -163,7 +163,7 @@ function api.register(public_proxy) if registered then return end registered = true - local start_path = "/opt/core/devfs/" + local start_path = "/lib/core/devfs/" for starter in fs.list(start_path) do local full_path = start_path .. starter local _,matched = starter:gsub("%.lua$","") diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/keyboard.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/keyboard.lua index 910a5ca94..70d86893f 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/keyboard.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/keyboard.lua @@ -82,6 +82,6 @@ end ------------------------------------------------------------------------------- -require("package").delay(keyboard.keys, "/opt/core/full_keyboard.lua") +require("package").delay(keyboard.keys, "/lib/core/full_keyboard.lua") return keyboard diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua index 3b699607f..1f923161e 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua @@ -272,6 +272,6 @@ function sh.hintHandler(full_line, cursor) return sh.internal.hintHandlerImpl(full_line, cursor) end -require("package").delay(sh, "/opt/core/full_sh.lua") +require("package").delay(sh, "/lib/core/full_sh.lua") return sh diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua index 1f527ebbb..7c77f4041 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/text.lua @@ -89,10 +89,10 @@ function text.internal.words(input, options) local show_escapes = options.show_escapes local qr = nil quotes = quotes or {{"'","'",true},{'"','"'},{'`','`'}} - local function append(dst, txt, qr) + local function append(dst, txt, _qr) local size = #dst - if size == 0 or dst[size].qr ~= qr then - dst[size+1] = {txt=txt, qr=qr} + if size == 0 or dst[size].qr ~= _qr then + dst[size+1] = {txt=txt, qr=_qr} else dst[size].txt = dst[size].txt..txt end @@ -144,6 +144,6 @@ function text.internal.words(input, options) return tokens end -require("package").delay(text, "/opt/core/full_text.lua") +require("package").delay(text, "/lib/core/full_text.lua") return text diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/transforms.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/transforms.lua index 11e859564..f34a6b6c7 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/transforms.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/transforms.lua @@ -70,6 +70,6 @@ function lib.concat(...) return r end -require("package").delay(lib, "/opt/core/full_transforms.lua") +require("package").delay(lib, "/lib/core/full_transforms.lua") return lib diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua index 8d708282b..c074b8f7d 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua @@ -215,14 +215,14 @@ function tty.internal.build_vertical_reader() end end, clear_tail = function(_) - local oi, width, height, dx, dy, ox, oy = _.index, tty.getViewport() + local oi, width, _, dx, dy, ox, oy = _.index, tty.getViewport() _:move(math.huge) _:move(-1) - local ex, ey = tty.getCursor() + local _, ey = tty.getCursor() tty.setCursor(ox, oy) _.index = oi - local x = oy == ey and ox or 1 - tty.gpu().fill(x + dx, ey + dy, width - x + 1, 1, " ") + local cx = oy == ey and ox or 1 + tty.gpu().fill(cx + dx, ey + dy, width - cx + 1, 1, " ") end, update = function(_, arg) local s1, s2 = tty.internal.split(_) @@ -337,7 +337,7 @@ function tty.drawText(value, nowrap) local uptime = computer.uptime local last_sleep = uptime() local last_index = 1 - local width, height, dx, dy = tty.getViewport() + local width, _, dx, dy = tty.getViewport() while true do if uptime() - last_sleep > 1 then os.sleep(0) @@ -493,6 +493,6 @@ function tty.scroll(number) return lines end -require("package").delay(tty, "/opt/core/full_tty.lua") +require("package").delay(tty, "/lib/core/full_tty.lua") return tty From 5d81f515703835509b7f6bbd3102b89572d27321 Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 25 Jun 2017 10:03:15 -0700 Subject: [PATCH 5/9] rename _ as self in tty cursor as it should be and move tty.write to term.write --- .../opencomputers/loot/openos/bin/sh.lua | 2 +- .../loot/openos/lib/core/lua_shell.lua | 15 ++-- .../opencomputers/loot/openos/lib/term.lua | 11 +++ .../opencomputers/loot/openos/lib/tty.lua | 77 ++++++++----------- 4 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua index aa8d94538..f62987d1f 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua @@ -28,7 +28,7 @@ if #args == 0 and (io.stdin.tty or options.i) and not options.c then local gpu = tty.gpu() while tty.isAvailable() do local foreground = gpu.setForeground(0xFF0000) - tty.write(sh.expand(os.getenv("PS1") or "$ ")) + io.write(sh.expand(os.getenv("PS1") or "$ ")) gpu.setForeground(foreground) tty.setCursorBlink(true) local command = tty.read(history) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua index 053f2ae48..7a29d3300 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua @@ -1,6 +1,5 @@ local package = require("package") local tty = require("tty") -local process = require("process") local gpu = tty.gpu() @@ -92,16 +91,16 @@ function read_handler.hint(line, index) end gpu.setForeground(0xFFFFFF) -tty.write(_VERSION .. " Copyright (C) 1994-2017 Lua.org, PUC-Rio\n") +io.write(_VERSION .. " Copyright (C) 1994-2017 Lua.org, PUC-Rio\n") gpu.setForeground(0xFFFF00) -tty.write("Enter a statement and hit enter to evaluate it.\n") -tty.write("Prefix an expression with '=' to show its value.\n") -tty.write("Press Ctrl+D to exit the interpreter.\n") +io.write("Enter a statement and hit enter to evaluate it.\n") +io.write("Prefix an expression with '=' to show its value.\n") +io.write("Press Ctrl+D to exit the interpreter.\n") gpu.setForeground(0xFFFFFF) while tty.isAvailable() do local foreground = gpu.setForeground(0x00FF00) - tty.write(env._PROMPT) + io.write(env._PROMPT) gpu.setForeground(foreground) local command = tty.read(read_handler) if not command then -- eof @@ -122,10 +121,10 @@ while tty.isAvailable() do io.stderr:write(tostring(result[2]) .. "\n") else for i = 2, result.n do - tty.write(require("serialization").serialize(result[i], true) .. "\t", true) + io.write(require("serialization").serialize(result[i], true) .. "\t", true) end if tty.getCursor() > 1 then - tty.write("\n") + io.write("\n") end end else diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/term.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/term.lua index cd1021d21..826d96d03 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/term.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/term.lua @@ -182,6 +182,17 @@ local function inject_mask(cursor, dobreak, pwchar) end end +-- cannot use term.write = io.write because io.write invokes metatable +function term.write(value, wrap) + local stdout = io.output() + local stream = stdout and stdout.stream + local previous_nowrap = stream.nowrap + stream.nowrap = wrap == false + stdout:write(value) + stdout:flush() + stream.nowrap = previous_nowrap +end + function term.read(history, dobreak, hint, pwchar, filter) if not io.stdin.tty then return io.read("*L") diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua index c074b8f7d..7a2d1b1b4 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua @@ -192,16 +192,16 @@ function tty.internal.build_vertical_reader() index = 0, data = "", sy = 0, - scroll = function(_) - _.sy = _.sy + tty.scroll() + scroll = function(self) + self.sy = self.sy + tty.scroll() end, - move = function(_,n) + move = function(self, n) local win = tty.window - _.index = math.min(math.max(0,_.index + n), unicode.len(_.data)) - local s1, s2 = tty.internal.split(_) + self.index = math.min(math.max(0, self.index + n), unicode.len(self.data)) + local s1, s2 = tty.internal.split(self) s2 = unicode.sub(s2.." ", 1, 1) - local data_remaining = ("_"):rep(_.promptx - 1)..s1..s2 - win.y = _.prompty - _.sy + local data_remaining = ("_"):rep(self.promptx - 1)..s1..s2 + win.y = self.prompty - self.sy while true do local wlen_remaining = unicode.wlen(data_remaining) if wlen_remaining > win.width then @@ -214,53 +214,53 @@ function tty.internal.build_vertical_reader() end end end, - clear_tail = function(_) - local oi, width, _, dx, dy, ox, oy = _.index, tty.getViewport() - _:move(math.huge) - _:move(-1) + clear_tail = function(self) + local oi, width, _, dx, dy, ox, oy = self.index, tty.getViewport() + self:move(math.huge) + self:move(-1) local _, ey = tty.getCursor() tty.setCursor(ox, oy) - _.index = oi + self.index = oi local cx = oy == ey and ox or 1 tty.gpu().fill(cx + dx, ey + dy, width - cx + 1, 1, " ") end, - update = function(_, arg) - local s1, s2 = tty.internal.split(_) + update = function(self, arg) + local s1, s2 = tty.internal.split(self) if type(arg) == "string" then - _.data = s1 .. arg .. s2 - _.index = _.index + unicode.len(arg) - _:draw(arg) + self.data = s1 .. arg .. s2 + self.index = self.index + unicode.len(arg) + self:draw(arg) else -- number if arg < 0 then -- backspace? ignore if at start - if _.index <= 0 then return end - _:move(arg) + if self.index <= 0 then return end + self:move(arg) s1 = unicode.sub(s1, 1, -1 + arg) else -- forward? ignore if at end - if _.index >= unicode.len(_.data) then return end + if self.index >= unicode.len(self.data) then return end s2 = unicode.sub(s2, 1 + arg) end - _:clear_tail() - _.data = s1 .. s2 + self:clear_tail() + self.data = s1 .. s2 end -- redraw suffix if s2 ~= "" then - local ps, px, py = _.sy, tty.getCursor() - _:draw(s2) - tty.setCursor(px, py - (_.sy - ps)) + local ps, px, py = self.sy, tty.getCursor() + self:draw(s2) + tty.setCursor(px, py - (self.sy - ps)) end end, - clear = function(_) - _:move(-math.huge) - _:draw((" "):rep(unicode.wlen(_.data))) - _:move(-math.huge) - _.index = 0 - _.data = "" + clear = function(self) + self:move(-math.huge) + self:draw((" "):rep(unicode.wlen(self.data))) + self:move(-math.huge) + self.index = 0 + self.data = "" end, - draw = function(_, text) - _.sy = _.sy + tty.drawText(text) + draw = function(self, text) + self.sy = self.sy + tty.drawText(text) end } end @@ -306,17 +306,6 @@ function tty.read(handler, cursor) end end --- cannot use tty.write = io.write because io.write invokes metatable -function tty.write(value, wrap) - local stdout = io.output() - local stream = stdout and stdout.stream - local previous_nowrap = stream.nowrap - stream.nowrap = wrap == false - stdout:write(value) - stdout:flush() - stream.nowrap = previous_nowrap -end - function tty.getCursor() local window = tty.window return window.x, window.y From 83a37af8cbdbe43aa4ac73eac43d4c6169f065ad Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 25 Jun 2017 16:18:16 -0700 Subject: [PATCH 6/9] woops - lua_shell using io.write doesn't need to pass true to wrap --- .../assets/opencomputers/loot/openos/lib/core/lua_shell.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua index 7a29d3300..64a2bdcf1 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua @@ -121,7 +121,7 @@ while tty.isAvailable() do io.stderr:write(tostring(result[2]) .. "\n") else for i = 2, result.n do - io.write(require("serialization").serialize(result[i], true) .. "\t", true) + io.write(require("serialization").serialize(result[i], true) .. "\t") end if tty.getCursor() > 1 then io.write("\n") From c988b44f669d49b489b98570e643c1e7b0baf5d4 Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 25 Jun 2017 22:58:54 -0700 Subject: [PATCH 7/9] openos 1.6.6 More memory savings, significant boot time savings **Memory Savings** The largest library in terms of cost in bytes in 1.6.5 was /lib/filesystem. Most of this library is not needed for boot, and in 1.6.6 the library is half loaded for boot with a delay load of the rest of the library when needed. There are also other minor memory improvements, see **Changes** for details **Boot Time** This update also introduces a newly written /bin/sh.lua that takes advantage of a common stdio system. This work was primary done to reduce memory needed to load the shell. However, during development of a simplified /bin/sh, I found that the /bin/source, /etc/profile, and /home/.shrc feature added to openos 1.6 had a significant boot time cost. I began optimizing /bin/source heavily and found that I could not make sufficient speed improvements due to the nature of how shell commands execute. Thus /etc/profile has been removed in favor of /etc/profile.lua, a script that sets up the environment the same but as a lua script. ~/.shrc is still honored if it exists on a system, and is sourced (/bin/source) as a set of shell commands. This change speeds up boot time from floppy by 3s, and on hdds by 1s. **Possible Breaking Changes** /etc/profile has been moved to /etc/profile.lua, but /home/.shrc is still sourced as shell script commands. **Changes** * /bin/sh.lua rewrite that takes advantage of standard io and uses the new /etc/profile.lua shaving 6s off boot time * /bin/source.lua improve performance by reducing the number of processes created * move /etc/profile to /etc/profile.lua and recode the actions in lua-form. * move all ~/.shrc actions to /etc/profile.lua for performance reasons * move motd to start of /etc/profile.lua to get the shell display as soon as possible * update greetings that referenced /etc/profile to properly refer to /etc/profile.lua now * update openos version to 1.6.6 to respect the impact of these changes --- .../opencomputers/loot/openos/bin/alias.lua | 4 +- .../opencomputers/loot/openos/bin/more.lua | 5 +- .../opencomputers/loot/openos/bin/sh.lua | 62 +++++++------------ .../opencomputers/loot/openos/bin/source.lua | 33 +++++----- .../loot/openos/boot/00_base.lua | 4 +- .../opencomputers/loot/openos/etc/profile | 26 -------- .../opencomputers/loot/openos/etc/profile.lua | 38 ++++++++++++ .../opencomputers/loot/openos/home/.shrc | 5 -- .../loot/openos/lib/core/boot.lua | 2 +- .../loot/openos/lib/core/full_ls.lua | 8 +-- .../opencomputers/loot/openos/lib/shell.lua | 4 +- .../loot/openos/usr/misc/greetings.txt | 2 +- 12 files changed, 90 insertions(+), 103 deletions(-) delete mode 100644 src/main/resources/assets/opencomputers/loot/openos/etc/profile create mode 100644 src/main/resources/assets/opencomputers/loot/openos/etc/profile.lua diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/alias.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/alias.lua index a9594a447..4e25b6d6e 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/alias.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/alias.lua @@ -4,7 +4,7 @@ local args, options = shell.parse(...) local ec, error_prefix = 0, "alias:" if options.help then - print(string.format("Usage: alias: [name[=value] ... ]", cmd_name)) + print(string.format("Usage: alias: [name[=value] ... ]")) return end @@ -53,7 +53,7 @@ if not next(args) then -- no args print(string.format("alias %s='%s'", k, v)) end else - for k,v in pairs(args) do + for _,v in ipairs(args) do checkArg(1,v,"string") handlePair(splitPair(v)) end diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/more.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/more.lua index af1150c1d..dfc3d55f6 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/more.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/more.lua @@ -2,7 +2,6 @@ local keyboard = require("keyboard") local shell = require("shell") local term = require("term") -- TODO use tty and cursor position instead of global area and gpu local text = require("text") -local unicode = require("unicode") if not io.output().tty then return loadfile(shell.resolve("cat", "lua"), "bt", _G)(...) @@ -28,7 +27,7 @@ end local line = nil local function readlines(num) - local x, y, w, h = term.getGlobalArea() + local _, _, w, h = term.getGlobalArea() num = num or (h - 1) for _ = 1, num do if not line then @@ -52,7 +51,7 @@ while true do return end while true do - local event, address, char, code = term.pull("key_down") + local _, _, _, code = term.pull("key_down") if code == keyboard.keys.q then term.clearLine() return diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua index f62987d1f..337656e0a 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua @@ -5,70 +5,50 @@ local text = require("text") local sh = require("sh") local input = table.pack(...) -local args, options = shell.parse(select(3,table.unpack(input))) +local args = shell.parse(select(3,table.unpack(input))) if input[2] then table.insert(args, 1, input[2]) end local history = {hint = sh.hintHandler} shell.prime() +local update_gpu = io.output().tty +local interactive = io.input().tty +local foreground -if #args == 0 and (io.stdin.tty or options.i) and not options.c then - -- interactive shell. - -- source profile - if not tty.isAvailable() then event.pull("term_available") end - loadfile(shell.resolve("source","lua"))("/etc/profile") +if #args == 0 then while true do - if not tty.isAvailable() then -- don't clear unless we lost the term + if update_gpu then while not tty.isAvailable() do event.pull("term_available") end - tty.clear() - end - local gpu = tty.gpu() - while tty.isAvailable() do - local foreground = gpu.setForeground(0xFF0000) - io.write(sh.expand(os.getenv("PS1") or "$ ")) - gpu.setForeground(foreground) - tty.setCursorBlink(true) - local command = tty.read(history) - if not command then - if command == false then - break -- soft interrupt - end - io.write("exit\n") -- pipe closed - return -- eof + if not foreground and interactive then -- first time run AND interactive + dofile("/etc/profile.lua") end + foreground = tty.gpu().setForeground(0xFF0000) + io.write(sh.expand(os.getenv("PS1") or "$ ")) + tty.gpu().setForeground(foreground) + tty.setCursorBlink(true) + end + local command = tty.read(history) + if command then command = text.trim(command) if command == "exit" then return elseif command ~= "" then local result, reason = sh.execute(_ENV, command) - if tty.getCursor() > 1 then - print() + if update_gpu and tty.getCursor() > 1 then + io.write("\n") end if not result then io.stderr:write((reason and tostring(reason) or "unknown error") .. "\n") end end - end - end -elseif #args == 0 and not io.stdin.tty then - while true do - io.write(sh.expand(os.getenv("PS1") or "$ ")) - local command = io.read("*l") - if not command then - command = "exit" - io.write(command,"\n") - end - command = text.trim(command) - if command == "exit" then - return - elseif command ~= "" then - local result, reason = os.execute(command) - if not result then - io.stderr:write((reason and tostring(reason) or "unknown error") .. "\n") + elseif command == nil then -- command==false is a soft interrupt, ignore it + if interactive then + io.write("exit\n") -- pipe closed end + return -- eof end end else diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/source.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/source.lua index a1b040716..2b1cc20ec 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/source.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/source.lua @@ -1,6 +1,5 @@ local shell = require("shell") -local fs = require("filesystem") -local sh = require("sh") +local process = require("process") local args, options = shell.parse(...) @@ -9,23 +8,25 @@ if #args ~= 1 then return 1 end -local file, reason = io.open(args[1], "r") +local file, open_reason = io.open(args[1], "r") if not file then if not options.q then - io.stderr:write(string.format("could not source %s because: %s\n", args[1], reason)); + io.stderr:write(string.format("could not source %s because: %s\n", args[1], open_reason)); end return 1 -else - local status, reason = xpcall(function() - repeat - local line = file:read() - if line then - sh.execute(nil, line) - end - until not line - end, function(msg) return {msg, debug.traceback()} end) - - file:close() - if not status and reason then assert(false, tostring(reason[1]) .."\n".. tostring(reason[2])) end end + +local current_data = process.info().data + +local source_proc = process.load((assert(os.getenv("SHELL"), "no $SHELL set"))) +local source_data = process.list[source_proc].data +source_data.aliases = current_data.aliases -- hacks to propogate sub shell env changes +source_data.vars = current_data.vars +source_data.io[0] = file -- set stdin to the file +if options.q then + source_data.io[1] = {tty=false,write=function()end} -- set stdin to the file +end +process.internal.continue(source_proc) + +file:close() -- should have closed when the process closed, but just to be sure diff --git a/src/main/resources/assets/opencomputers/loot/openos/boot/00_base.lua b/src/main/resources/assets/opencomputers/loot/openos/boot/00_base.lua index 76c4c04ae..c8a56ac4b 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/boot/00_base.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/boot/00_base.lua @@ -2,9 +2,9 @@ function loadfile(filename, ...) if filename:sub(1,1) ~= "/" then filename = (os.getenv("PWD") or "/") .. "/" .. filename end - local handle, reason = require("filesystem").open(filename) + local handle, open_reason = require("filesystem").open(filename) if not handle then - return nil, reason + return nil, open_reason end local buffer = {} while true do diff --git a/src/main/resources/assets/opencomputers/loot/openos/etc/profile b/src/main/resources/assets/opencomputers/loot/openos/etc/profile deleted file mode 100644 index a2a387f00..000000000 --- a/src/main/resources/assets/opencomputers/loot/openos/etc/profile +++ /dev/null @@ -1,26 +0,0 @@ -alias dir=ls -alias move=mv -alias rename=mv -alias copy=cp -alias del=rm -alias md=mkdir -alias cls=clear -alias rs=redstone -alias view=edit\ -r -alias help=man -alias cp=cp\ -i - -set EDITOR=/bin/edit -set HISTSIZE=10 -set HOME=/home -set IFS=\ -set MANPATH=/usr/man:. -set PAGER=/bin/more -set PS1='$HOSTNAME$HOSTNAME_SEPARATOR$PWD # ' -set PWD=/ -set LS_COLORS="{FILE=0xFFFFFF,DIR=0x66CCFF,LINK=0xFFAA00,['*.lua']=0x00FF00}" - -cd $HOME -clear -/etc/motd -source $HOME/.shrc -q diff --git a/src/main/resources/assets/opencomputers/loot/openos/etc/profile.lua b/src/main/resources/assets/opencomputers/loot/openos/etc/profile.lua new file mode 100644 index 000000000..400f3485e --- /dev/null +++ b/src/main/resources/assets/opencomputers/loot/openos/etc/profile.lua @@ -0,0 +1,38 @@ +local shell = require("shell") +local tty = require("tty") +local fs = require("filesystem") + +tty.clear() +dofile("/etc/motd") + +shell.setAlias("dir", "ls") +shell.setAlias("move", "mv") +shell.setAlias("rename", "mv") +shell.setAlias("copy", "cp") +shell.setAlias("del", "rm") +shell.setAlias("md", "mkdir") +shell.setAlias("cls", "clear") +shell.setAlias("rs", "redstone") +shell.setAlias("view", "edit -r") +shell.setAlias("help", "man") +shell.setAlias("cp", "cp -i") +shell.setAlias("l", "ls -lhp") +shell.setAlias("..", "cd ..") +shell.setAlias("df", "df -h") +shell.setAlias("grep", "grep --color") + +os.setenv("EDITOR", "/bin/edit") +os.setenv("HISTSIZE", "10") +os.setenv("HOME", "/home") +os.setenv("IFS", " ") +os.setenv("MANPATH", "/usr/man:.") +os.setenv("PAGER", "/bin/more") +os.setenv("PS1", "$HOSTNAME$HOSTNAME_SEPARATOR$PWD # ") +os.setenv("LS_COLORS", "{FILE=0xFFFFFF,DIR=0x66CCFF,LINK=0xFFAA00,['*.lua']=0x00FF00}") + +shell.setWorkingDirectory(os.getenv("HOME")) + +local home_shrc = shell.resolve(".shrc") +if fs.exists(home_shrc) then + loadfile(shell.resolve("source", "lua"))(home_shrc, "-q") +end diff --git a/src/main/resources/assets/opencomputers/loot/openos/home/.shrc b/src/main/resources/assets/opencomputers/loot/openos/home/.shrc index e9e55efa7..e69de29bb 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/home/.shrc +++ b/src/main/resources/assets/opencomputers/loot/openos/home/.shrc @@ -1,5 +0,0 @@ -alias l="ls -lhp" -alias ..="cd .." -alias df="df -h" - -alias grep="grep --color" diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua index 7ad88d429..ed45c023a 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/boot.lua @@ -1,7 +1,7 @@ -- called from /init.lua local raw_loadfile = ... -_G._OSVERSION = "OpenOS 1.6.5" +_G._OSVERSION = "OpenOS 1.6.6" local component = component local computer = computer diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_ls.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_ls.lua index 8f6524305..5e7eb6bec 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_ls.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/full_ls.lua @@ -253,7 +253,7 @@ local function display(names) max_size_width = math.max(max_size_width, formatSize(info.size):len()) max_date_width = math.max(max_date_width, formatDate(info.time):len()) end - mt.__index = function(tbl, index) + mt.__index = function(_, index) local info = stat(names, index) local file_type = info.isLink and 'l' or info.isDir and 'd' or 'f' local link_target = info.isLink and string.format(" -> %s", info.link:gsub("/+$", "") .. (info.isDir and "/" or "")) or "" @@ -267,7 +267,7 @@ local function display(names) end elseif ops["1"] or not fOut then lines.n = #names - mt.__index = function(tbl, index) + mt.__index = function(_, index) local info = stat(names, index) return {{color = colorize(info), name = info.name}} end @@ -302,10 +302,10 @@ local function display(names) end end lines.n = items_per_column - mt.__index=function(tbl, line_index) + mt.__index=function(_, line_index) return setmetatable({},{ __len=function()return num_columns end, - __index=function(tbl, column_index) + __index=function(_, column_index) local ri = real(column_index, line_index) if not ri then return end local info = stat(names, ri) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/shell.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/shell.lua index ea536a71a..0a9dc2e9a 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/shell.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/shell.lua @@ -18,11 +18,11 @@ function shell.getShell() if shells[shellName] then return shells[shellName] end - local sh, reason = loadfile(shellName) + local sh, load_reason = loadfile(shellName) if sh then shells[shellName] = sh end - return sh, reason + return sh, load_reason end ------------------------------------------------------------------------------- diff --git a/src/main/resources/assets/opencomputers/loot/openos/usr/misc/greetings.txt b/src/main/resources/assets/opencomputers/loot/openos/usr/misc/greetings.txt index 1ef0905a9..e645e68ea 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/usr/misc/greetings.txt +++ b/src/main/resources/assets/opencomputers/loot/openos/usr/misc/greetings.txt @@ -22,7 +22,7 @@ Many component methods have a short documentation - use `=component.componentNam You can get a list of all attached components using the `components` program. If you encounter out of memory errors, throw more RAM at your computer. Have you tried turning it off and on again? -To disable this greeting, install OpenOS to a writeable medium and remove the `/etc/motd` line from `/etc/profile`. +To disable this greeting, install OpenOS to a writeable medium and remove the `/etc/motd` line from `/etc/profile.lua`. Did you know OpenComputers has a forum? No? Well, it's at https://oc.cil.li/. Please report bugs on the Github issue tracker, thank you! Beware of cycles when building networks, or you may get duplicate messages! From e57d763f3a3c212f871763e40d2ef13a06382247 Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 25 Jun 2017 23:31:30 -0700 Subject: [PATCH 8/9] some install fixes --- .../opencomputers/loot/openos/lib/core/install_basics.lua | 7 ++++--- .../opencomputers/loot/openos/lib/core/install_utils.lua | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua index 6673f1a76..a193b7cdf 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_basics.lua @@ -64,7 +64,7 @@ local targets = {} local devices = {} for dev, path in fs.mounts() do - devices[dev] = path + devices[dev] = devices[dev] and #devices[dev] < #path and devices[dev] or path end devices[fs.get("/dev/") or false] = nil @@ -81,6 +81,7 @@ for dev, path in pairs(devices) do os.exit(1) end elseif specified or + not (source_filter and address:find(source_filter, 1, true) == 1) and -- specified for source not target_filter and address ~= tmpAddress then table.insert(targets, {dev=dev, path=install_path, specified=specified}) @@ -156,8 +157,8 @@ local cp_args = local source_display = options.label or source.dev.getLabel() or source.path local special_target = "" -if #targets > 1 or target_filter then - special_target = " to " .. cp_args[3] +if #targets > 1 or target_filter or source_filter then + special_target = " to " .. cp_args[4] end io.write("Install " .. source_display .. special_target .. "? [Y/n] ") diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_utils.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_utils.lua index 557f6e521..0e376abfd 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_utils.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/install_utils.lua @@ -13,7 +13,7 @@ local function select_prompt(devs, prompt) for i = 1, num_devs do local src = devs[i] local dev = src.dev - local selection_label = src.prop.label or dev.getLabel() + local selection_label = (src.prop or {}).label or dev.getLabel() if selection_label then selection_label = string.format("%s (%s...)", selection_label, dev.address:sub(1, 8)) else From 0c595ddda7e99c3aade6bdf1b9bbe96333125dcd Mon Sep 17 00:00:00 2001 From: payonel Date: Mon, 26 Jun 2017 09:24:25 -0700 Subject: [PATCH 9/9] remove needless not nots --- .../resources/assets/opencomputers/loot/openos/lib/tty.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua index 7a2d1b1b4..4462eac63 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua @@ -25,8 +25,8 @@ local function ctrl_movement(cursor, dir) local start=index+dir+1 for i=start,last,dir do local a,b = unicode.sub(data, i-1, i-1), unicode.sub(data, i, i) - a = a == "" or not not a:find("%s") - b = b == "" or not not b:find("%s") + a = a == "" or a:find("%s") + b = b == "" or b:find("%s") if a and not b then return i - (index + 1) end end return last - index