openOS command line programs

This commit is contained in:
kepler155c@gmail.com
2019-04-07 10:10:30 -04:00
parent 5f2f33f1f2
commit 8f2c97d269
20 changed files with 163 additions and 280 deletions

78
oc/3dprint.lua Normal file
View File

@@ -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

20
oc/sample/sample.3dm Normal file
View File

@@ -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"},
}
}

View File

@@ -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',
}

View File

@@ -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,
}

View File

@@ -5,5 +5,6 @@ local shell = _ENV.shell
return {
getWorkingDirectory = shell.dir,
resolve = shell.resolve,
resolveProgram = shell.resolveProgram,
parse = Util.parse,
}

View File

@@ -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

View File

@@ -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 USAGE =
[===[Usage: find [path] [--type=[dfs]] [--[i]name=EXPR]

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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] <filename1> [<filename2> [...]]"..[[
@@ -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

View File

@@ -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(...)
@@ -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)

View File

@@ -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

View File

@@ -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()

View File

@@ -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(...)

View File

@@ -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(...)

View File

@@ -1,19 +0,0 @@
local shell = require("shell")
local args = shell.parse(...)
if #args < 1 then
io.write("Usage: unalias <name>...\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

View File

@@ -1,9 +0,0 @@
local args = {...}
if #args < 1 then
io.write("Usage: unset <varname>[ <varname2> [...]]\n")
else
for _, k in ipairs(args) do
os.setenv(k, nil)
end
end

View File

@@ -1,4 +1,4 @@
local shell = require("shell")
local shell = require("openos.shell")
local args = shell.parse(...)
if #args == 0 then
@@ -7,7 +7,7 @@ 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])