diff --git a/sys/apis/fs/linkfs.lua b/sys/apis/fs/linkfs.lua index e46342c..38cceac 100644 --- a/sys/apis/fs/linkfs.lua +++ b/sys/apis/fs/linkfs.lua @@ -2,6 +2,8 @@ local fs = _G.fs local linkfs = { } +-- TODO: implement broken links + local methods = { 'exists', 'getFreeSpace', 'getSize', 'isDir', 'isReadOnly', 'list', 'listEx', 'makeDir', 'open', 'getDrive' } diff --git a/sys/apis/peripheral.lua b/sys/apis/peripheral.lua index a176268..6e73a0e 100644 --- a/sys/apis/peripheral.lua +++ b/sys/apis/peripheral.lua @@ -17,53 +17,59 @@ end function Peripheral.addDevice(deviceList, side) local name = side - local ptype = Peripheral.getType(side) + pcall(function() + local ptype = Peripheral.getType(side) + local dev = Peripheral.wrap(side) - if not ptype then - return - end - - if ptype == 'modem' then - if not Peripheral.call(name, 'isWireless') then --- ptype = 'wireless_modem' --- else - ptype = 'wired_modem' + if not ptype or not dev then + return end - end - local sides = { - front = true, - back = true, - top = true, - bottom = true, - left = true, - right = true - } - - if sides[name] then - local i = 1 - local uniqueName = ptype - while deviceList[uniqueName] do - uniqueName = ptype .. '_' .. i - i = i + 1 + if ptype == 'modem' then + if not Peripheral.call(name, 'isWireless') then + -- ptype = 'wireless_modem' + -- else + ptype = 'wired_modem' + if dev.getMetadata then + -- avoid open computer relays being registered + -- as 'wired_modem' + ptype = dev.getMetadata().name or 'wired_modem' + end + end end - name = uniqueName - end - -- this can randomly fail - if not deviceList[name] then - pcall(function() - deviceList[name] = Peripheral.wrap(side) - end) + local sides = { + front = true, + back = true, + top = true, + bottom = true, + left = true, + right = true + } - if deviceList[name] then - Util.merge(deviceList[name], { - name = name, - type = ptype, - side = side, - }) + if sides[name] then + local i = 1 + local uniqueName = ptype + while deviceList[uniqueName] do + uniqueName = ptype .. '_' .. i + i = i + 1 + end + name = uniqueName end - end + + -- this can randomly fail + if not deviceList[name] then + deviceList[name] = dev + + if deviceList[name] then + Util.merge(deviceList[name], { + name = name, + type = ptype, + side = side, + }) + end + end + end) return deviceList[name] end @@ -117,4 +123,4 @@ function Peripheral.get(args) end end -return Peripheral +return Peripheral \ No newline at end of file diff --git a/sys/apis/util.lua b/sys/apis/util.lua index 2a3fb64..a15e1e2 100644 --- a/sys/apis/util.lua +++ b/sys/apis/util.lua @@ -659,6 +659,37 @@ function Util.wordWrap(str, limit) return lines end +-- https://github.com/MightyPirates/OpenComputers +function Util.parse(...) + local params = table.pack(...) + local args = {} + local options = {} + local doneWithOptions = false + for i = 1, params.n do + local param = params[i] + if not doneWithOptions and type(param) == "string" then + if param == "--" then + doneWithOptions = true -- stop processing options at `--` + elseif param:sub(1, 2) == "--" then + local key, value = param:match("%-%-(.-)=(.*)") + if not key then + key, value = param:sub(3), true + end + options[key] = value + elseif param:sub(1, 1) == "-" and param ~= "-" then + for j = 2, string.len(param) do + options[string.sub(param, j, j)] = true + end + else + table.insert(args, param) + end + else + table.insert(args, param) + end + end + return args, options +end + function Util.args(arg) local options, args = { }, { } diff --git a/sys/apps/Help.lua b/sys/apps/Help.lua index cdb5de7..b43d23a 100644 --- a/sys/apps/Help.lua +++ b/sys/apps/Help.lua @@ -1,8 +1,8 @@ local UI = require('ui') local Util = require('util') -local colors = _G.colors -local help = _G.help +local colors = _G.colors +local help = _G.help UI:configure('Help', ...) @@ -40,7 +40,7 @@ local topicPage = UI.Page { backgroundColor = colors.black, titleBar = UI.TitleBar { title = 'text', - previousPage = true, + event = 'back', }, helpText = UI.TextArea { backgroundColor = colors.black, @@ -52,9 +52,18 @@ local topicPage = UI.Page { }, } +function topicPage:enable(name) + local f = help.lookup(name) + + self.titleBar.title = name + self.helpText:setText(f and Util.readFile(f) or 'No help available for ' .. name) + + return UI.Page.enable(self) +end + function topicPage:eventHandler(event) if event.type == 'back' then - UI:setPreviousPage() + UI:setPage(page) end return UI.Page.eventHandler(self, event) end @@ -66,12 +75,8 @@ function page:eventHandler(event) elseif event.type == 'grid_select' then if self.grid:getSelected() then local name = self.grid:getSelected().name - local f = help.lookup(name) - topicPage.titleBar.title = name - topicPage.helpText:setText(Util.readFile(f)) - - UI:setPage(topicPage) + UI:setPage(topicPage, name) end elseif event.type == 'text_change' then @@ -93,5 +98,6 @@ function page:eventHandler(event) end end -UI:setPage(page) +local args = { ... } +UI:setPage(#args[1] and topicPage or page, args[1]) UI:pullEvents() diff --git a/sys/apps/autorun.lua b/sys/apps/autorun.lua index e7febc1..40e0e74 100644 --- a/sys/apps/autorun.lua +++ b/sys/apps/autorun.lua @@ -55,7 +55,7 @@ runDir('usr/autorun') if not success then if multishell then - --multishell.setFocus(multishell.getCurrent()) + multishell.setFocus(multishell.getCurrent()) end _G.printError('A startup program has errored') print('Press enter to continue') diff --git a/sys/apps/system/aliases.lua b/sys/apps/system/aliases.lua index 0523f27..3a98bc6 100644 --- a/sys/apps/system/aliases.lua +++ b/sys/apps/system/aliases.lua @@ -1,6 +1,8 @@ local Config = require('config') local UI = require('ui') +local kernel = _G.kernel + local aliasTab = UI.Tab { tabTitle = 'Aliases', description = 'Shell aliases', @@ -33,8 +35,12 @@ local aliasTab = UI.Tab { function aliasTab.grid:draw() self.values = { } local env = Config.load('shell') + for k in pairs(kernel.getShell().aliases()) do + kernel.getShell().clearAlias(k) + end for k,v in pairs(env.aliases) do table.insert(self.values, { alias = k, path = v }) + kernel.getShell().setAlias(k, v) end self:update() UI.Grid.draw(self) @@ -42,23 +48,23 @@ end function aliasTab:eventHandler(event) if event.type == 'delete_alias' then - local env = Config.load('shell') + local env = Config.load('shell', { aliases = { } }) env.aliases[self.grid:getSelected().alias] = nil + Config.update('shell', env) self.grid:setIndex(self.grid:getIndex()) self.grid:draw() - Config.update('shell', env) - self:emit({ type = 'success_message', message = 'reboot to take effect' }) + self:emit({ type = 'success_message', message = 'Aliases updated' }) return true elseif event.type == 'new_alias' then - local env = Config.load('shell') + local env = Config.load('shell', { aliases = { } }) env.aliases[self.alias.value] = self.path.value + Config.update('shell', env) self.alias:reset() self.path:reset() self:draw() self:setFocus(self.alias) - Config.update('shell', env) - self:emit({ type = 'success_message', message = 'reboot to take effect' }) + self:emit({ type = 'success_message', message = 'Aliases updated' }) return true end end diff --git a/sys/init/2.vfs.lua b/sys/init/2.vfs.lua index d80474c..a42e59e 100644 --- a/sys/init/2.vfs.lua +++ b/sys/init/2.vfs.lua @@ -287,6 +287,7 @@ function fs.mount(path, fstype, ...) tp.nodes[targetName] = node end end +_debug(node) return node end diff --git a/sys/init/3.relay.lua b/sys/init/3.relay.lua new file mode 100644 index 0000000..8a65f36 --- /dev/null +++ b/sys/init/3.relay.lua @@ -0,0 +1,27 @@ +local device = _G.device +local kernel = _G.kernel + +local function register(v) + if v and v.isWireless and v.getMetadata and v.getNamesRemote then + v.children = { } + for _, name in pairs(v.getNamesRemote()) do + local dev = v.getMethodsRemote(name) + if dev then + dev.name = name + dev.side = name + dev.type = v.getTypeRemote(name) + device[name] = dev + table.insert(v._children, dev) + end + end + end +end + +for _,v in pairs(device) do + register(v) +end + +-- register oc devices as peripherals +kernel.hook('device_attach', function(_, eventData) + register(device[eventData[2]]) +end) diff --git a/sys/init/7.multishell.lua b/sys/init/7.multishell.lua index 9d48ce0..d9a2536 100644 --- a/sys/init/7.multishell.lua +++ b/sys/init/7.multishell.lua @@ -123,23 +123,25 @@ function multishell.openTab(tab) local routine = kernel.newRoutine(tab) routine.co = coroutine.create(function() - local result, err, stacktrace + local result, err if tab.fn then result, err = Util.runFunction(routine.env, tab.fn, table.unpack(tab.args or { } )) elseif tab.path then - result, err, stacktrace = xprun(routine.env, tab.path, table.unpack(tab.args or { } )) + result, err = xprun(routine.env, tab.path, table.unpack(tab.args or { } )) else err = 'multishell: invalid tab' end - if not result and err and err ~= 'Terminated' then - if err then + if not result and err and err ~= 'Terminated' or (err and err ~= 0) then + tab.terminal.setBackgroundColor(colors.black) + if tonumber(err) then + tab.terminal.setTextColor(colors.orange) + print('Process exited with error code: ' .. err) + elseif err then printError(tostring(err)) - if stacktrace then -- alternatively log stack to _debug - --print('\n' .. stacktrace) - end end + tab.terminal.setTextColor(colors.white) print('\nPress enter to close') routine.isDead = true routine.hidden = false