From 8f2c97d2693f21f3a60d153817b2fe603d647922 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sun, 7 Apr 2019 10:10:30 -0400 Subject: [PATCH] openOS command line programs --- oc/3dprint.lua | 78 +++++++++++++++++++ oc/sample/sample.3dm | 20 +++++ openos/.package | 2 +- openos/apis/filesystem.lua | 21 +++-- openos/apis/shell.lua | 1 + openos/autorun/startup.lua | 13 ++++ openos/find.lua | 10 +-- openos/ln.lua | 7 +- openos/ls.lua | 6 +- openos/mv.lua | 6 +- openos/ps.lua | 155 ------------------------------------- openos/rm.lua | 7 +- openos/rmdir.lua | 10 +-- openos/set.lua | 23 ------ openos/source.lua | 36 --------- openos/touch.lua | 4 +- openos/tree.lua | 10 +-- openos/unalias.lua | 19 ----- openos/unset.lua | 9 --- openos/which.lua | 6 +- 20 files changed, 163 insertions(+), 280 deletions(-) create mode 100644 oc/3dprint.lua create mode 100644 oc/sample/sample.3dm delete mode 100644 openos/ps.lua delete mode 100644 openos/set.lua delete mode 100644 openos/source.lua delete mode 100644 openos/unalias.lua delete mode 100644 openos/unset.lua diff --git a/oc/3dprint.lua b/oc/3dprint.lua new file mode 100644 index 0000000..6631f90 --- /dev/null +++ b/oc/3dprint.lua @@ -0,0 +1,78 @@ +local printer = peripheral.find "printer3d" + +local args = {...} +if #args < 1 then + error("Usage: print3d FILE [count]\n") + +end + +if args[1] == "reset" then printer.reset() end + +local count = 1 +if #args > 1 then + count = assert(tonumber(args[2]), tostring(args[2]) .. " is not a valid count") +end + +local file = fs.open(args[1], "r") +if not file then + error("Failed opening file\n") + return 1 +end + +local rawdata = file.readAll() +file.close() +local data, reason = loadstring("return " .. rawdata) +if not data then + error("Failed loading model: " .. reason .. "\n") + return 2 +end +data = data() + +io.write("Configuring...\n") + +printer.reset() +if data.label then + printer.setLabel(data.label) +end +if data.tooltip then + printer.setTooltip(data.tooltip) +end +if data.lightLevel and printer.setLightLevel then -- as of OC 1.5.7 + printer.setLightLevel(data.lightLevel) +end +if data.emitRedstone then + printer.setRedstoneEmitter(data.emitRedstone) +end +if data.buttonMode then + printer.setButtonMode(data.buttonMode) +end +if data.collidable and printer.setCollidable then + printer.setCollidable(not not data.collidable[1], not not data.collidable[2]) +end +for i, shape in ipairs(data.shapes or {}) do + local result, reason = printer.addShape(shape[1], shape[2], shape[3], shape[4], shape[5], shape[6], shape.texture, shape.state, shape.tint) + if not result then + io.write("Failed adding shape: " .. tostring(reason) .. "\n") + end +end + +io.write("Label: '" .. (printer.getLabel() or "not set") .. "'\n") +io.write("Tooltip: '" .. (printer.getTooltip() or "not set") .. "'\n") +if printer.getLightLevel then -- as of OC 1.5.7 + io.write("Light level: " .. printer.getLightLevel() .. "\n") +end +io.write("Redstone level: " .. select(2, printer.isRedstoneEmitter()) .. "\n") +io.write("Button mode: " .. tostring(printer.isButtonMode()) .. "\n") +if printer.isCollidable then -- as of OC 1.5.something + io.write("Collidable: " .. tostring(select(1, printer.isCollidable())) .. "/" .. tostring(select(2, printer.isCollidable())) .. "\n") +end +io.write("Shapes: " .. printer.getShapeCount() .. " inactive, " .. select(2, printer.getShapeCount()) .. " active\n") + +local result, reason = printer.commit(count) +if result then + io.write("Job successfully committed!\n") + print('Printed!') + os.sleep(3) +else + error("Failed committing job: " .. tostring(reason) .. "\n") +end \ No newline at end of file diff --git a/oc/sample/sample.3dm b/oc/sample/sample.3dm new file mode 100644 index 0000000..92410ac --- /dev/null +++ b/oc/sample/sample.3dm @@ -0,0 +1,20 @@ +{ + label = "Basic Ceiling Lamp Prototype", + tooltip = "ยง7Herb's Basic Ceiling Lamp Prototype.", + lightlevel=8, + shapes={ + + {7,10,7,9,15,9,texture="log_spruce"}, + {6,15,6,10,16,10,texture="iron_block"}, + {2,9,2,14,10,14,texture="wool_colored_red"}, + + {2,7,13,14,10,14,texture="wool_colored_red"}, + {2,7,2,14,10,3,texture="wool_colored_red"}, + + {2,7,3,3,10,14,texture="wool_colored_red"}, + {13,7,3,14,10,14,texture="wool_colored_red"}, + + {3,8,3,13,9,13,texture="glowstone"}, +} + +} \ No newline at end of file diff --git a/openos/.package b/openos/.package index a7219a0..d352c6c 100644 --- a/openos/.package +++ b/openos/.package @@ -1,6 +1,6 @@ { title = 'Shell utilities', repository = 'kepler155c/opus-apps/{{OPUS_BRANCH}}/openos', - description = [[ Utilties for shell: grep, cat, touch, etc ]], + description = [[ Experimental! Utilties for shell: grep, cat, touch, etc ]], licence = 'MIT', } diff --git a/openos/apis/filesystem.lua b/openos/apis/filesystem.lua index 6d6cc75..8023288 100644 --- a/openos/apis/filesystem.lua +++ b/openos/apis/filesystem.lua @@ -1,6 +1,10 @@ local fs = _G.fs local function get(path) + while not fs.exists(path) do + path = fs.getDir(path) + end + local label = fs.getDrive(path) if label then @@ -35,25 +39,32 @@ local function mounts() end local function list(path) - local set = fs.list(path) - return function() - local key, value = next(set) - set[key or false] = nil - return value + local success, set = pcall(fs.list, path) + if success then + return function() + local key, value = next(set) + set[key or false] = nil + return value + end end + return success, set end return { canonical = function(...) return ... end, concat = fs.combine, + copy = function(...) fs.copy(...) return true end, exists = fs.exists, get = get, isDirectory = fs.isDir, isLink = function() return false end, + link = function(s, t) return fs.mount(t, 'linkfs', s) end, list = list, + makeDirectory = fs.makeDir, mounts = mounts, name = fs.getName, open = function(n, m) return fs.open(n, m or 'r') end, realPath = function(...) return ... end, + remove = function(a) fs.delete(a) return true end, size = fs.getSize, } diff --git a/openos/apis/shell.lua b/openos/apis/shell.lua index a7bb257..ee5c6d9 100644 --- a/openos/apis/shell.lua +++ b/openos/apis/shell.lua @@ -5,5 +5,6 @@ local shell = _ENV.shell return { getWorkingDirectory = shell.dir, resolve = shell.resolve, + resolveProgram = shell.resolveProgram, parse = Util.parse, } diff --git a/openos/autorun/startup.lua b/openos/autorun/startup.lua index 28c983b..0ffd74f 100644 --- a/openos/autorun/startup.lua +++ b/openos/autorun/startup.lua @@ -1,3 +1,6 @@ +local Config = require('config') + +local kernel = _G.kernel local os = _G.os local settings = _G.settings @@ -9,3 +12,13 @@ end function os.getenv(k) return settings.get(k) end + +local config = Config.load('shell', { aliases = { } }) +if not config.openOsInit then + config.openOsInit = true + for _, alias in pairs({ 'ls', 'rm', 'cp', 'mv' }) do + config.aliases[alias] = nil + kernel.getShell().clearAlias(alias) + end + Config.update('shell', config) +end diff --git a/openos/find.lua b/openos/find.lua index d0cb53b..d8c4972 100644 --- a/openos/find.lua +++ b/openos/find.lua @@ -1,8 +1,8 @@ -local shell = require("shell") -local fs = require("filesystem") -local text = require("text") +local shell = require("openos.shell") +local fs = require("openos.filesystem") +local text = require("openos.text") -local USAGE = +local USAGE = [===[Usage: find [path] [--type=[dfs]] [--[i]name=EXPR] --path if not specified, path is assumed to be current working directory --type returns results of a given type, d:directory, f:file, and s:symlinks @@ -74,7 +74,7 @@ if options.iname or options.name then -- prefix any * with . for gnu find glob matching fileNamePattern = text.escapeMagic(fileNamePattern) fileNamePattern = fileNamePattern:gsub("%%%*", ".*") -end +end local function isValidType(spath) if not fs.exists(spath) then diff --git a/openos/ln.lua b/openos/ln.lua index b89ca53..0dbc0fa 100644 --- a/openos/ln.lua +++ b/openos/ln.lua @@ -1,6 +1,5 @@ -local component = require("component") -local fs = require("filesystem") -local shell = require("shell") +local fs = require("openos.filesystem") +local shell = require("openos.shell") local args = shell.parse(...) if #args == 0 then @@ -28,7 +27,7 @@ if fs.isDirectory(linkpath) then linkpath = fs.concat(linkpath, fs.name(target)) end -local result, reason = fs.link(target_name, linkpath) +local result, reason = fs.link(target, linkpath) if not result then io.stderr:write(reason..'\n') return 1 diff --git a/openos/ls.lua b/openos/ls.lua index 93ac93c..591af9d 100644 --- a/openos/ls.lua +++ b/openos/ls.lua @@ -235,6 +235,7 @@ local first_display = true local function display(names) local mt={} local lines = setmetatable({}, mt) + local columns if ops.l then lines.n = #names local max_size_width = 1 @@ -291,9 +292,10 @@ local function display(names) end end lines.n = items_per_column + columns = num_columns mt.__index=function(_, line_index) return setmetatable({},{ - __len=function()return num_columns end, + __len=function()return num_columns end, -- no can do in 5.1 __index=function(_, column_index) local ri = real(column_index, line_index) if not ri then return end @@ -307,7 +309,7 @@ local function display(names) for line_index=1,lines.n do local line = lines[line_index] - for element_index=1,#line do + for element_index=1,columns or #line do local e = line[element_index] if not e then break end first_display = false diff --git a/openos/mv.lua b/openos/mv.lua index 9567d6f..a50acc9 100644 --- a/openos/mv.lua +++ b/openos/mv.lua @@ -1,5 +1,5 @@ -local shell = require("shell") -local transfer = require("tools/transfer") +local shell = require("openos.shell") +local transfer = require("openos.transfer") local args, options = shell.parse(...) options.h = options.h or options.help @@ -17,7 +17,7 @@ if #args < 2 or options.h then end -- clean options for move (as opposed to copy) -options = +options = { cmd = "mv", f = options.f, diff --git a/openos/ps.lua b/openos/ps.lua deleted file mode 100644 index 2c96027..0000000 --- a/openos/ps.lua +++ /dev/null @@ -1,155 +0,0 @@ -local process = require("process") -local unicode = require("unicode") -local event = require("event") -local thread = require("thread") -local event_mt = getmetatable(event.handlers) - --- WARNING this code does not use official kernel API and is likely to change - -local data = {} -local widths = {} -local sorted = {} -local moved_indexes = {} - -local elbow = unicode.char(0x2514) - -local function thread_id(t,p) - if t then - return tostring(t):gsub("^thread: 0x", "") - end - -- find the parent thread - for k,v in pairs(process.list) do - if v == p then - return thread_id(k) - end - end - return "-" -end - -local cols = -{ - {"PID", thread_id}, - {"EVENTS", function(_,p) - local handlers = {} - if event_mt.threaded then - handlers = rawget(p.data, "handlers") or {} - elseif not p.parent then - handlers = event.handlers - end - local count = 0 - for _ in pairs(handlers) do - count = count + 1 - end - return count == 0 and "-" or tostring(count) - end}, - {"THREADS", function(_,p) - -- threads are handles with mt.close == thread.waitForAll - local count = 0 - for h in pairs(p.data.handles) do - local mt = getmetatable(h) - if mt and mt.__status then - count = count + 1 - end - end - return count == 0 and "-" or tostring(count) - end}, - {"PARENT", function(_,p) - for _,process_info in pairs(process.list) do - for handle in pairs(process_info.data.handles) do - local mt = getmetatable(handle) - if mt and mt.__status then - if mt.process == p then - return thread_id(nil, process_info) - end - end - end - end - return thread_id(nil, p.parent) - end}, - {"HANDLES", function(_, p) - local count = 0 - for _,closure in pairs(p.data.handles) do - if closure then - count = count + 1 - end - end - return count == 0 and "-" or tostring(count) - end}, - {"CMD", function(_,p) return p.command end}, -} - -local function add_field(key, value) - if not data[key] then data[key] = {} end - table.insert(data[key], value) - widths[key] = math.max(widths[key] or 0, #value) -end - -for _,key in ipairs(cols) do - add_field(key[1], key[1]) -end - -for thread_handle, process_info in pairs(process.list) do - for _,key in ipairs(cols) do - add_field(key[1], key[2](thread_handle, process_info)) - end -end - -local parent_index -for index,set in ipairs(cols) do - if set[1] == "PARENT" then - parent_index = index - break - end -end -assert(parent_index, "did not find a parent column") - -local function move_to_sorted(index) - if moved_indexes[index] then - return false - end - local entry = {} - for k,v in pairs(data) do - entry[k] = v[index] - end - sorted[#sorted + 1] = entry - moved_indexes[index] = true - return true -end - -local function make_elbow(depth) - return (" "):rep(depth - 1) .. (depth > 0 and elbow or "") -end - --- remove COLUMN labels to simplify sort -move_to_sorted(1) - -local function update_family(parent, depth) - depth = depth or 0 - parent = parent or "-" - for index in ipairs(data.PID) do - local this_parent = data[cols[parent_index][1]][index] - if this_parent == parent then - local dash_cmd = make_elbow(depth) .. data.CMD[index] - data.CMD[index] = dash_cmd - widths.CMD = math.max(widths.CMD or 0, #dash_cmd) - if move_to_sorted(index) then - update_family(data.PID[index], depth + 1) - end - end - end -end - -update_family() -table.remove(cols, parent_index) -- don't show parent id - -for _,set in ipairs(sorted) do - local split = "" - for _,key in ipairs(cols) do - local label = key[1] - local format = split .. "%-" .. tostring(widths[label]) .. "s" - io.write(string.format(format, set[label])) - split = " " - end - print() -end - diff --git a/openos/rm.lua b/openos/rm.lua index 48ed9d0..fa7ed17 100644 --- a/openos/rm.lua +++ b/openos/rm.lua @@ -1,5 +1,5 @@ -local fs = require("filesystem") -local shell = require("shell") +local fs = require("openos.filesystem") +local shell = require("openos.shell") local function usage() print("Usage: rm [options] [ [...]]"..[[ @@ -60,7 +60,7 @@ local function createMeta(origin, rel) end local function unlink(path) - os.remove(path) + fs.remove(path) return true end @@ -87,6 +87,7 @@ local function remove_all(parent) for file in fs.list(_path(parent)) do local child = createMeta(parent.origin, parent.rel .. file) all_ok = remove(child) and all_ok + -- uh ? end end diff --git a/openos/rmdir.lua b/openos/rmdir.lua index 98ef870..1a11c27 100644 --- a/openos/rmdir.lua +++ b/openos/rmdir.lua @@ -1,6 +1,6 @@ -local shell = require("shell") -local fs = require("filesystem") -local text = require("text") +local shell = require("openos.shell") +local fs = require("openos.filesystem") +local text = require("openos.text") local args, options = shell.parse(...) @@ -59,7 +59,7 @@ local function remove(path, ...) return ec_bump() else local list, reason = fs.list(rpath) - + if not list then io.stderr:write(tostring(reason)..'\n') return ec_bump() @@ -88,7 +88,7 @@ for _,path in ipairs(args) do local segments = {} if options.p and path:len() > 1 and path:find('/') then - chain = text.split(path, {'/'}, true) + local chain = text.split(path, {'/'}, true) local prefix = '' for _,e in ipairs(chain) do table.insert(segments, 1, prefix .. e) diff --git a/openos/set.lua b/openos/set.lua deleted file mode 100644 index 6608af5..0000000 --- a/openos/set.lua +++ /dev/null @@ -1,23 +0,0 @@ -local args = {...} - -if #args < 1 then - for k,v in pairs(os.getenv()) do - io.write(k .. "='" .. string.gsub(v, "'", [['"'"']]) .. "'\n") - end -else - local count = 0 - for _, expr in ipairs(args) do - local e = expr:find('=') - if e then - os.setenv(expr:sub(1,e-1), expr:sub(e+1)) - else - if count == 0 then - for i = 1, os.getenv('#') do - os.setenv(i, nil) - end - end - count = count + 1 - os.setenv(count, expr) - end - end -end diff --git a/openos/source.lua b/openos/source.lua deleted file mode 100644 index 58d8f20..0000000 --- a/openos/source.lua +++ /dev/null @@ -1,36 +0,0 @@ -local shell = require("shell") -local process = require("process") - -local args, options = shell.parse(...) - -if #args ~= 1 then - io.stderr:write("specify a single file to source\n"); - return 1 -end - -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], open_reason)); - end - return 1 -end - -local lines = file:lines() - -while true do - local line = lines() - if not line then - break - 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 - process.internal.continue(source_proc, _ENV, line) -end - -file:close() diff --git a/openos/touch.lua b/openos/touch.lua index 74e541d..8f4f094 100644 --- a/openos/touch.lua +++ b/openos/touch.lua @@ -1,6 +1,6 @@ --[[Lua implementation of the UN*X touch command--]] -local shell = require("shell") -local fs = require("filesystem") +local shell = require("openos.shell") +local fs = require("openos.filesystem") local args, options = shell.parse(...) diff --git a/openos/tree.lua b/openos/tree.lua index e67246c..1d37799 100644 --- a/openos/tree.lua +++ b/openos/tree.lua @@ -1,8 +1,8 @@ -local computer = require("computer") -local shell = require("shell") -local fs = require("filesystem") -local tx = require("transforms") -local text = require("text") +local computer = require("openos.computer") +local shell = require("openos.shell") +local fs = require("openos.filesystem") +local tx = require("openos.transforms") +local text = require("openos.text") local args, opts = shell.parse(...) diff --git a/openos/unalias.lua b/openos/unalias.lua deleted file mode 100644 index ceea94d..0000000 --- a/openos/unalias.lua +++ /dev/null @@ -1,19 +0,0 @@ -local shell = require("shell") - -local args = shell.parse(...) -if #args < 1 then - io.write("Usage: unalias ...\n") - return 2 -end -local e = 0 - -for _,arg in ipairs(args) do - local result = shell.getAlias(arg) - if not result then - io.stderr:write(string.format("unalias: %s: not found\n", arg)) - e = 1 - else - shell.setAlias(arg, nil) - end -end -return e diff --git a/openos/unset.lua b/openos/unset.lua deleted file mode 100644 index 1fb68e4..0000000 --- a/openos/unset.lua +++ /dev/null @@ -1,9 +0,0 @@ -local args = {...} - -if #args < 1 then - io.write("Usage: unset [ [...]]\n") -else - for _, k in ipairs(args) do - os.setenv(k, nil) - end -end diff --git a/openos/which.lua b/openos/which.lua index 503d442..fed369c 100644 --- a/openos/which.lua +++ b/openos/which.lua @@ -1,4 +1,4 @@ -local shell = require("shell") +local shell = require("openos.shell") local args = shell.parse(...) if #args == 0 then @@ -7,8 +7,8 @@ if #args == 0 then end for i = 1, #args do - local result, reason = shell.resolve(args[i], "lua") - + local result, reason = shell.resolveProgram(args[i]) + if not result then result = shell.getAlias(args[i]) if result then