Hyperion v1.2.0

This commit is contained in:
2026-02-22 21:53:02 -06:00
parent dd2437d4af
commit 40c97ca000
37 changed files with 6736 additions and 1329 deletions

View File

@@ -1,24 +1,33 @@
local args = {...}
local name = syscall.getTask(syscall.getpid()).name
local fs = require("sys.fs")
local filePath = (args[1] or "")
if filePath:sub(1, 1) ~= "/" then
filePath = syscall.getcwd().."/"..filePath
end
if not fs.exists(filePath) and args[1] then
print(name..": Cannot access '"..args[1].."': No such file.")
if not args[1] then
while true do
local content = syscall.read(0, 1024)
if not content or content == "" then break end
printInline(content)
end
print("")
return
end
local file = 0
if args[1] then
file = syscall.open(filePath, "r")
for _, arg in ipairs(args) do
local filePath = arg
if filePath:sub(1,1) ~= "/" then
filePath = syscall.getcwd().."/"..filePath
end
if not fs.exists(filePath) then
print(name..": Cannot access '"..arg.."': No such file.")
else
local fd = syscall.open(filePath, "r")
while true do
local content = syscall.read(fd, 1024)
if not content or content == "" then break end
printInline(content)
end
syscall.close(fd)
end
end
local content=""
while content~=nil or file == 0 do
content=syscall.read(file, 1024)
printInline(content)
end
syscall.close(file)
print("")
print("")

View File

@@ -0,0 +1,162 @@
--:Minify:--
-- supports +i/-i (immutable) stored in the file's cmeta/xattr field
local name = syscall.getTask(syscall.getpid()).name
local cloptions = { R = false, help = false }
local args = {}
local modeStr = nil
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name .. ": unrecognized option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" and not v:match("^%-[%+%-]") then
local isFlag = true
for i = 2, #v do
local c = v:sub(i,i)
if cloptions[c] ~= nil then
cloptions[c] = true
else
isFlag = false; break
end
end
if not isFlag then
modeStr = v
end
elseif v:sub(1,1) == "+" or (v:sub(1,1) == "-" and v:match("^%-[a-zA-Z]")) then
modeStr = v
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " [OPTION]... +-= ATTRS FILE...")
print("Change file attributes on a filesystem.")
print("")
print("Attributes:")
print(" i immutable: file cannot be modified, renamed, or deleted")
print(" a append-only: file can only be appended to")
print("")
print("Operators: +attr add, -attr remove")
print("Example: " .. name .. " +i /etc/passwd")
print("")
print("Options:")
print(" -R operate on files and directories recursively")
print(" --help display this help and exit")
return
end
if not modeStr or #args < 1 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
local op = modeStr:sub(1, 1)
local attrs = modeStr:sub(2)
if op ~= "+" and op ~= "-" then
print(name .. ": invalid operator '" .. op .. "' (use + or -)")
syscall.exit(1); return
end
local function getXattr(path)
local stat = pcall(function() return syscall.stat(path) end) and syscall.stat(path)
if stat then return stat.xattr or "" end
return ""
end
local IMMUTABLE_TAG = "|i"
local APPENDONLY_TAG = "|a"
local function attrTag(c)
if c == "i" then return IMMUTABLE_TAG
elseif c == "a" then return APPENDONLY_TAG
else return nil end
end
local function chattrPath(path)
local stat = syscall.stat(path)
if not stat then
print(name .. ": cannot stat '" .. path .. "': No such file or directory")
return false
end
if stat.etype == 0x01 then
return true
end
if not syscall.setxattr then
print(name .. ": kernel does not expose setxattr syscall; cannot modify attributes")
syscall.exit(1); return false
end
local xattr = stat.xattr or ""
for i = 1, #attrs do
local c = attrs:sub(i, i)
local tag = attrTag(c)
if not tag then
print(name .. ": unsupported attribute '" .. c .. "'")
syscall.exit(1); return false
end
local hasTag = xattr:find(tag, 1, true)
if op == "+" and not hasTag then
xattr = xattr .. tag
elseif op == "-" and hasTag then
xattr = xattr:gsub(tag:gsub("|", "%%|"), "")
end
end
local ok, err = pcall(syscall.setxattr, path, xattr)
if not ok then
print(name .. ": cannot set attributes on '" .. path .. "': " .. tostring(err))
return false
end
return true
end
local function chattrRecursive(path)
if not chattrPath(path) then return end
if syscall.type(path) == "directory" then
local ok, list = pcall(syscall.listdir, path)
if ok then
for _, entry in ipairs(list) do
local child = path
if child:sub(-1) ~= "/" then child = child .. "/" end
chattrRecursive(child .. entry)
end
end
end
end
local cwd = syscall.getcwd()
local function absPath(p)
if p:sub(1,1) ~= "/" then p = cwd .. "/" .. p end
return p
end
if not syscall.setxattr then
print(name .. ": kernel does not expose setxattr; attributes cannot be persisted")
print(name .. ": add sys[\"setxattr\"] = vfs.setxattr to 10_vfs.kmod to enable this")
syscall.exit(1); return
end
local exitCode = 0
for i = 1, #args do
local path = absPath(args[i])
if not syscall.exists(path) then
print(name .. ": cannot access '" .. args[i] .. "': No such file or directory")
exitCode = 1
elseif cloptions.R then
chattrRecursive(path)
else
if not chattrPath(path) then exitCode = 1 end
end
end
syscall.exit(exitCode)

111
Src/Hyperion-bash/bin/chgrp Normal file
View File

@@ -0,0 +1,111 @@
--:Minify:--
-- chgrp: change group ownership
local name = syscall.getTask(syscall.getpid()).name
local cloptions = { R = false, help = false }
local args = {}
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name .. ": unrecognized option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" then
for i = 2, #v do
local opt = v:sub(i, i)
if cloptions[opt] == nil then
print(name .. ": invalid option '-" .. opt .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
end
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " [OPTION]... GROUP FILE...")
print("Change the group of each FILE to GROUP.")
print("GROUP may be a group name or numeric ID.")
print("")
print("Options:")
print(" -R operate on files and directories recursively")
print(" --help display this help and exit")
return
end
if #args < 2 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
local groupStr = args[1]
local function resolveGid(s)
local n = tonumber(s)
if n then return n end
local uid = syscall.getuidbyname and syscall.getuidbyname(s)
if uid then
local pwent = syscall.getpasswd(uid)
if pwent then return pwent.gid end
end
print(name .. ": invalid group: '" .. s .. "'")
syscall.exit(1)
end
local newGid = resolveGid(groupStr)
local function chgrpPath(path)
local stat = syscall.stat(path)
if not stat then
print(name .. ": cannot stat '" .. path .. "': No such file or directory")
return false
end
local ok, err = pcall(syscall.chown, path, stat.owner, newGid)
if not ok then
print(name .. ": cannot change group of '" .. path .. "': " .. tostring(err))
return false
end
return true
end
local function chgrpRecursive(path)
if not chgrpPath(path) then return end
if syscall.type(path) == "directory" then
local ok, list = pcall(syscall.listdir, path)
if ok then
for _, entry in ipairs(list) do
local child = path
if child:sub(-1) ~= "/" then child = child .. "/" end
chgrpRecursive(child .. entry)
end
end
end
end
local cwd = syscall.getcwd()
local function absPath(p)
if p:sub(1,1) ~= "/" then p = cwd .. "/" .. p end
return p
end
local exitCode = 0
for i = 2, #args do
local path = absPath(args[i])
if not syscall.exists(path) then
print(name .. ": cannot access '" .. args[i] .. "': No such file or directory")
exitCode = 1
elseif cloptions.R then
chgrpRecursive(path)
else
if not chgrpPath(path) then exitCode = 1 end
end
end
syscall.exit(exitCode)

262
Src/Hyperion-bash/bin/chmod Normal file
View File

@@ -0,0 +1,262 @@
--:Minify:--
local name = syscall.getTask(syscall.getpid()).name
local cloptions = { R = false, help = false }
local args = {}
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name .. ": unrecognized option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" then
for i = 2, #v do
local opt = v:sub(i, i)
if cloptions[opt] == nil then
print(name .. ": invalid option '-" .. opt .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
end
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " [OPTION]... MODE FILE...")
print("Change the file mode bits of each FILE to MODE.")
print("")
print("MODE may be octal (e.g. 755) or symbolic (e.g. u+x, go-w, a=r).")
print("")
print("Octal bit layout (Hyperion):")
print(" owner: r=32 w=16 x=512 group: r=8 w=4 x=256")
print(" world: r=2 w=1 x=128 suid=64")
print(" Common: 644=rw-r--r-- 755=rwxr-xr-x 700=rwx------")
print("")
print("Symbolic: [ugoa][+-=][rwxs] (comma-separated list)")
print("")
print("Options:")
print(" -R change files and directories recursively")
print(" --help display this help and exit")
return
end
if #args < 2 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
local modeArg = args[1]
local P = {
OWNER_R = 32, OWNER_W = 16, OWNER_X = 512,
GROUP_R = 8, GROUP_W = 4, GROUP_X = 256,
WORLD_R = 2, WORLD_W = 1, WORLD_X = 128,
SUID = 64,
}
local function bit_is_set(num, bit)
return math.floor(num / (2 ^ bit)) % 2 == 1
end
local function parseOctal(s)
local n = tonumber(s, 8)
if not n then return nil end
local result = 0
if bit_is_set(n, 8) then result = result + P.OWNER_R end -- 0400
if bit_is_set(n, 7) then result = result + P.OWNER_W end -- 0200
if bit_is_set(n, 6) then result = result + P.OWNER_X end -- 0100
if bit_is_set(n, 5) then result = result + P.GROUP_R end -- 040
if bit_is_set(n, 4) then result = result + P.GROUP_W end -- 020
if bit_is_set(n, 3) then result = result + P.GROUP_X end -- 010
if bit_is_set(n, 2) then result = result + P.WORLD_R end -- 004
if bit_is_set(n, 1) then result = result + P.WORLD_W end -- 002
if bit_is_set(n, 0) then result = result + P.WORLD_X end -- 001
if bit_is_set(n, 11) then result = result + P.SUID end
return result
end
local function applySymbolic(modeStr, existingPerms)
local perms = existingPerms
for clause in (modeStr .. ","):gmatch("([^,]+),") do
local who_str, rest = clause:match("^([ugoa]*)([+%-=].+)$")
if not who_str then
print(name .. ": invalid mode: '" .. clause .. "'")
syscall.exit(1); return nil
end
if who_str == "" or who_str == "a" then who_str = "ugo" end
local op = rest:sub(1, 1)
local bits_str = rest:sub(2)
local mask = 0
for i = 1, #bits_str do
local c = bits_str:sub(i, i)
for j = 1, #who_str do
local w = who_str:sub(j, j)
if c == "r" then
if w == "u" then mask = mask + P.OWNER_R
elseif w == "g" then mask = mask + P.GROUP_R
elseif w == "o" then mask = mask + P.WORLD_R end
elseif c == "w" then
if w == "u" then mask = mask + P.OWNER_W
elseif w == "g" then mask = mask + P.GROUP_W
elseif w == "o" then mask = mask + P.WORLD_W end
elseif c == "x" then
if w == "u" then mask = mask + P.OWNER_X
elseif w == "g" then mask = mask + P.GROUP_X
elseif w == "o" then mask = mask + P.WORLD_X end
elseif c == "s" then
if w == "u" then mask = mask + P.SUID end
end
end
end
if op == "+" then
perms = perms + (mask - (perms % (mask + 1) - perms % mask > 0 and 0 or 0))
perms = perms - (perms % 1)
local function bor(a, b)
local result, bit = 0, 1
while a > 0 or b > 0 do
if (a % 2 == 1) or (b % 2 == 1) then result = result + bit end
a = math.floor(a / 2); b = math.floor(b / 2); bit = bit * 2
end
return result
end
perms = bor(perms, mask)
elseif op == "-" then
local function band(a, b)
local result, bit = 0, 1
while a > 0 and b > 0 do
if (a % 2 == 1) and (b % 2 == 1) then result = result + bit end
a = math.floor(a / 2); b = math.floor(b / 2); bit = bit * 2
end
return result
end
local function bxor(a, b)
local result, bit = 0, 1
while a > 0 or b > 0 do
if (a % 2 == 1) ~= (b % 2 == 1) then result = result + bit end
a = math.floor(a / 2); b = math.floor(b / 2); bit = bit * 2
end
return result
end
perms = bxor(perms, band(perms, mask))
elseif op == "=" then
local clearMask = 0
for j = 1, #who_str do
local w = who_str:sub(j, j)
if w == "u" then clearMask = clearMask + P.OWNER_R + P.OWNER_W + P.OWNER_X + P.SUID
elseif w == "g" then clearMask = clearMask + P.GROUP_R + P.GROUP_W + P.GROUP_X
elseif w == "o" then clearMask = clearMask + P.WORLD_R + P.WORLD_W + P.WORLD_X end
end
local function bxor(a, b)
local result, bit = 0, 1
while a > 0 or b > 0 do
if (a % 2 == 1) ~= (b % 2 == 1) then result = result + bit end
a = math.floor(a / 2); b = math.floor(b / 2); bit = bit * 2
end
return result
end
local function band(a, b)
local result, bit = 0, 1
while a > 0 and b > 0 do
if (a % 2 == 1) and (b % 2 == 1) then result = result + bit end
a = math.floor(a / 2); b = math.floor(b / 2); bit = bit * 2
end
return result
end
local function bor(a, b)
local result, bit = 0, 1
while a > 0 or b > 0 do
if (a % 2 == 1) or (b % 2 == 1) then result = result + bit end
a = math.floor(a / 2); b = math.floor(b / 2); bit = bit * 2
end
return result
end
perms = bxor(perms, band(perms, clearMask))
perms = bor(perms, mask)
else
print(name .. ": invalid operator in mode: '" .. clause .. "'")
syscall.exit(1); return nil
end
end
return perms
end
local function resolveMode(modeStr, existingPerms)
if modeStr:match("^[0-7]+$") then
local p = parseOctal(modeStr)
if p then return p end
end
return applySymbolic(modeStr, existingPerms)
end
local function chmodPath(path)
local stat, err = pcall(syscall.stat, path)
local existingPerms = 0
if stat then
local s = syscall.stat(path)
existingPerms = s and s.perms or 0
end
local newPerms = resolveMode(modeArg, existingPerms)
if newPerms == nil then return false end
local ok, cerr = pcall(syscall.chmod, path, newPerms)
if not ok then
print(name .. ": cannot change permissions of '" .. path .. "': " .. tostring(cerr))
return false
end
return true
end
local function chmodRecursive(path)
if not chmodPath(path) then return end
if syscall.type(path) == "directory" then
local ok, list = pcall(syscall.listdir, path)
if ok then
for _, entry in ipairs(list) do
local child = path
if child:sub(-1) ~= "/" then child = child .. "/" end
chmodRecursive(child .. entry)
end
end
end
end
local cwd = syscall.getcwd()
local function absPath(p)
if p:sub(1,1) ~= "/" then p = cwd .. "/" .. p end
return p
end
local exitCode = 0
for i = 2, #args do
local path = absPath(args[i])
if not syscall.exists(path) then
print(name .. ": cannot access '" .. args[i] .. "': No such file or directory")
exitCode = 1
elseif cloptions.R then
chmodRecursive(path)
else
if not chmodPath(path) then exitCode = 1 end
end
end
syscall.exit(exitCode)

144
Src/Hyperion-bash/bin/chown Normal file
View File

@@ -0,0 +1,144 @@
--:Minify:--
local name = syscall.getTask(syscall.getpid()).name
local cloptions = { R = false, help = false }
local args = {}
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name .. ": unrecognized option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" then
for i = 2, #v do
local opt = v:sub(i, i)
if cloptions[opt] == nil then
print(name .. ": invalid option '-" .. opt .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
end
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " [OPTION]... OWNER[:GROUP] FILE...")
print(" " .. name .. " [OPTION]... :GROUP FILE...")
print("Change the owner and/or group of each FILE.")
print("OWNER and GROUP may be names or numeric IDs.")
print("")
print("Options:")
print(" -R operate on files and directories recursively")
print(" --help display this help and exit")
return
end
if #args < 2 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
local spec = args[1]
local ownerStr, groupStr
if spec:sub(1,1) == ":" then
groupStr = spec:sub(2)
else
local colon = spec:find(":", 1, true)
if colon then
ownerStr = spec:sub(1, colon - 1)
groupStr = spec:sub(colon + 1)
if groupStr == "" then groupStr = nil end
else
ownerStr = spec
end
end
local function resolveUid(s)
if not s or s == "" then return nil end
local n = tonumber(s)
if n then return n end
local uid = syscall.getuidbyname and syscall.getuidbyname(s)
if uid then return uid end
print(name .. ": invalid user: '" .. s .. "'")
syscall.exit(1)
end
local function resolveGid(s)
if not s or s == "" then return nil end
local n = tonumber(s)
if n then return n end
local uid = syscall.getuidbyname and syscall.getuidbyname(s)
if uid then
local pwent = syscall.getpasswd(uid)
if pwent then return pwent.gid end
end
print(name .. ": invalid group: '" .. s .. "'")
syscall.exit(1)
end
local newUid = resolveUid(ownerStr)
local newGid = resolveGid(groupStr)
if newUid == nil and newGid == nil then
print(name .. ": no owner or group specified")
syscall.exit(1); return
end
local function chownPath(path)
local stat = syscall.stat(path)
if not stat then
print(name .. ": cannot stat '" .. path .. "': No such file or directory")
return false
end
local uid = newUid ~= nil and newUid or stat.owner
local gid = newGid ~= nil and newGid or stat.group
local ok, err = pcall(syscall.chown, path, uid, gid)
if not ok then
print(name .. ": cannot change owner of '" .. path .. "': " .. tostring(err))
return false
end
return true
end
local function chownRecursive(path)
if not chownPath(path) then return end
if syscall.type(path) == "directory" then
local ok, list = pcall(syscall.listdir, path)
if ok then
for _, entry in ipairs(list) do
local child = path
if child:sub(-1) ~= "/" then child = child .. "/" end
chownRecursive(child .. entry)
end
end
end
end
local cwd = syscall.getcwd()
local function absPath(p)
if p:sub(1,1) ~= "/" then p = cwd .. "/" .. p end
return p
end
local exitCode = 0
for i = 2, #args do
local path = absPath(args[i])
if not syscall.exists(path) then
print(name .. ": cannot access '" .. args[i] .. "': No such file or directory")
exitCode = 1
elseif cloptions.R then
chownRecursive(path)
else
if not chownPath(path) then exitCode = 1 end
end
end
syscall.exit(exitCode)

View File

@@ -0,0 +1,83 @@
--:Minify:--
local name = syscall.getTask(syscall.getpid()).name
local args = {}
local cloptions = { help = false }
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if opt == "help" then
cloptions.help = true
else
print(name .. ": unrecognized option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
elseif v:sub(1, 1) == "-" then
print(name .. ": invalid option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " NEWROOT [COMMAND [ARG]...]")
print("Run COMMAND with root directory set to NEWROOT.")
print("If COMMAND is omitted, runs the current user's shell.")
print("")
print("Requires root (uid 0).")
return
end
if #args < 1 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
local euid = syscall.geteuid and syscall.geteuid() or syscall.getuid()
if euid ~= 0 then
print(name .. ": cannot change root directory: Permission denied")
syscall.exit(1); return
end
local newRoot = args[1]
if newRoot:sub(1,1) ~= "/" then
newRoot = syscall.getcwd() .. "/" .. newRoot
end
if not syscall.exists(newRoot) then
print(name .. ": cannot change root directory to '" .. args[1] .. "': No such file or directory")
syscall.exit(1); return
end
if syscall.type(newRoot) ~= "directory" then
print(name .. ": '" .. args[1] .. "': Not a directory")
syscall.exit(1); return
end
local ok, err = pcall(syscall.chroot, newRoot)
if not ok then
print(name .. ": cannot change root directory to '" .. args[1] .. "': " .. tostring(err))
syscall.exit(1); return
end
local shell
if #args >= 2 then
shell = args[2]
else
local uid = syscall.getuid()
local pwent = syscall.getpasswd(uid)
shell = (pwent and pwent.shell) or "/bin/hysh"
end
local execArgs = {}
for i = 3, #args do table.insert(execArgs, args[i]) end
local execOk, execErr = pcall(syscall.exec, shell, execArgs)
if not execOk then
print(name .. ": failed to run command '" .. shell .. "': " .. tostring(execErr))
syscall.exit(127)
end

View File

@@ -1,174 +1,205 @@
--:Minify:--
-- help: display command reference with paged scrolling
local COMMANDS = {
-- {name, usage, description, {flags...}}
-- flags: {flag, desc}
{
name = "cat",
usage = "cat [file]",
desc = "Print file contents to stdout. Reads from stdin if no file given.",
flags = {}
},
{
name = "cd",
usage = "cd [dir]",
desc = "Change working directory. Use '-' to return to previous directory.",
flags = {}
},
{
name = "clear",
usage = "clear",
desc = "Clear the terminal screen.",
flags = {}
},
{
name = "echo",
usage = "echo [text...]",
desc = "Print arguments to stdout.",
flags = {}
},
{
name = "hfetch",
usage = "hfetch",
desc = "Display system information in a neofetch-style layout.",
flags = {}
},
{
name = "id",
usage = "id [username]",
desc = "Print user identity (uid, gid). Defaults to current user.",
flags = {}
},
{
name = "login",
usage = "login",
desc = "System login prompt. Launched automatically at boot.",
flags = {}
},
{
name = "ls",
usage = "ls [-alh] [dir]",
desc = "List directory contents.",
flags = {
{"-a", "Show hidden files (starting with .)"},
{"-l", "Long format: permissions, owner, size"},
{"-h", "Human-readable file sizes"},
}
},
{
name = "lsusers",
usage = "lsusers",
desc = "List all user accounts with uid, gid, home, and shell.",
flags = {}
},
{
name = "lua",
usage = "lua",
desc = "Interactive Lua REPL prompt.",
flags = {}
},
{
name = "mkdir",
usage = "mkdir <dir>",
desc = "Create a directory.",
flags = {}
},
{
name = "passwd",
usage = "passwd [username]",
desc = "Change a user password. Non-root must verify current password first.",
flags = {}
},
{
name = "ps",
usage = "ps",
desc = "List running tasks with pid, user, name, and status.",
flags = {}
},
{
name = "pwd",
usage = "pwd",
desc = "Print current working directory.",
flags = {}
},
{
name = "su",
usage = "su [username]",
desc = "Switch user. Defaults to root. Root can switch without a password.",
flags = {}
},
{
name = "sudo",
usage = "sudo [-u user] <command> [args...]",
desc = "Run a command as another user (default root). Authenticates as current user.",
flags = {
{"-u user", "Run as the specified user (name or uid)"},
}
},
{
name = "sysdump",
usage = "sysdump",
desc = "List all registered kernel syscalls.",
flags = {}
},
{
name = "useradd",
usage = "useradd [-p pw] [-g gid] [-d home] [-s shell] [-M] <username>",
desc = "Create a new user account.",
flags = {
{"-p pw", "Set password (prompted interactively if omitted)"},
{"-g gid", "Set primary group id"},
{"-d home", "Set home directory (default: /home/username)"},
{"-s shell", "Set login shell (default: /bin/hysh)"},
{"-M", "Do not create home directory"},
}
},
{
name = "userdel",
usage = "userdel [-r] <username>",
desc = "Delete a user account.",
flags = {
{"-r", "Also recursively remove the user's home directory"},
}
},
{
name = "usermod",
usage = "usermod [-l name] [-p pw] [-g gid] [-d home] [-s shell] [-L] [-U] <username>",
desc = "Modify an existing user account.",
flags = {
{"-l name", "Rename the user"},
{"-p pw", "Set new password"},
{"-g gid", "Change primary group id"},
{"-d home", "Change home directory"},
{"-s shell", "Change login shell"},
{"-L", "Lock the account (disable login)"},
{"-U", "Unlock the account"},
}
},
{
name = "whoami",
usage = "whoami",
desc = "Print the current username.",
flags = {}
},
{
name = "yes",
usage = "yes [text]",
desc = "Repeatedly print 'y' (or given text) until interrupted with Ctrl+C.",
flags = {}
},
{ name="cd", usage="cd [dir]", desc="Change working directory. Use '-' to return to previous directory.", flags={} },
{ name="pwd", usage="pwd", desc="Print current working directory.", flags={} },
{ name="ls", usage="ls [-alh] [dir]", desc="List directory contents. Coloured by type: dirs=blue, symlinks=cyan, executables=green.", flags={
{"-a","Show hidden files (starting with .)"},
{"-l","Long format: permissions, owner, group, size, mtime, name"},
{"-h","Human-readable file sizes (with -l)"},
{"--help","Display help and exit"},
}},
{ name="find", usage="find [path] [-name PAT] [-type f|d|l] [-maxdepth N]", desc="Walk the filesystem tree and print matching paths.", flags={
{"-name PAT", "Match filename against shell glob (* and ?)"},
{"-type f|d|l","Filter by file, directory, or symlink"},
{"-maxdepth N","Descend at most N directory levels"},
{"-mindepth N","Skip entries shallower than N levels"},
{"-empty", "Match empty files or empty directories"},
}},
{ name="cp", usage="cp [-rRp] SOURCE... DEST", desc="Copy files or directories.", flags={
{"-r,-R","Recurse into directories"},
{"-p", "Preserve permissions"},
{"--help","Display help and exit"},
}},
{ name="mv", usage="mv [-f] SOURCE... DEST", desc="Move or rename files and directories.", flags={
{"-f", "Do not prompt before overwriting (default)"},
{"--help","Display help and exit"},
}},
{ name="rm", usage="rm [-rRf] FILE...", desc="Remove files or directories.", flags={
{"-r,-R","Recursively remove directories and their contents"},
{"-f", "Ignore nonexistent files, never prompt"},
{"--help","Display help and exit"},
}},
{ name="touch", usage="touch FILE...", desc="Create an empty file, or no-op if it already exists.", flags={} },
{ name="mkdir", usage="mkdir <dir>", desc="Create a directory.", flags={} },
{ name="ln", usage="ln -s [-f] TARGET LINK", desc="Create a symbolic link. Multiple targets can be linked into a directory.", flags={
{"-s", "Create a symbolic link (required; hard links not supported)"},
{"-f", "Remove existing destination before creating link"},
{"--help","Display help and exit"},
}},
{ name="cat", usage="cat [file...]", desc="Print file(s) to stdout. Reads stdin if no file given.", flags={} },
{ name="head", usage="head [-n N] [file...]", desc="Print the first N lines of each file (default 10).", flags={
{"-n N","Number of lines to print"},
{"--help","Display help and exit"},
}},
{ name="tail", usage="tail [-n N] [file...]", desc="Print the last N lines of each file (default 10).", flags={
{"-n N","Number of lines to print"},
{"--help","Display help and exit"},
}},
{ name="wc", usage="wc [-lwc] [file...]", desc="Count lines, words, and bytes in files.", flags={
{"-l","Print line count"},
{"-w","Print word count"},
{"-c","Print byte count"},
{"--help","Display help and exit"},
}},
{ name="grep", usage="grep [-ivnlcrR] PATTERN [file...]", desc="Search for lines matching a Lua pattern.", flags={
{"-i","Ignore case"},
{"-v","Invert: select non-matching lines"},
{"-n","Prefix output with line numbers"},
{"-l","Print only filenames that contain a match"},
{"-c","Print count of matching lines per file"},
{"-r,-R","Recurse into directories"},
{"--help","Display help and exit"},
}},
{ name="sed", usage="sed 's/PAT/REPL/' [file...]", desc="Stream editor. Applies substitution commands to each line.", flags={} },
{ name="sort", usage="sort [-rnu] [file...]", desc="Sort lines of text.", flags={
{"-r","Reverse the sort order"},
{"-n","Numeric sort"},
{"-u","Suppress duplicate lines"},
{"--help","Display help and exit"},
}},
{ name="uniq", usage="uniq [-cdui] [input [output]]", desc="Filter adjacent duplicate lines.", flags={
{"-c","Prefix each line with its repetition count"},
{"-d","Print only lines that appear more than once"},
{"-u","Print only lines that appear exactly once"},
{"-i","Ignore case when comparing"},
{"--help","Display help and exit"},
}},
{ name="tee", usage="tee [-a] [file...]", desc="Copy stdin to stdout and to each FILE simultaneously.", flags={
{"-a","Append to files instead of overwriting"},
{"--help","Display help and exit"},
}},
{ name="basename", usage="basename STRING [SUFFIX]", desc="Strip directory and optional suffix from a path.", flags={} },
{ name="dirname", usage="dirname STRING...", desc="Strip the last component from a path.", flags={} },
{ name="readlink", usage="readlink [-fenq] file...", desc="Print the target of a symbolic link.", flags={
{"-f","Canonicalize: follow every symlink component"},
{"-e","Like -f but all components must exist"},
{"-n","Do not output trailing newline"},
{"--help","Display help and exit"},
}},
{ name="stat", usage="stat file...", desc="Display file type, size, owner, group, and permissions.", flags={
{"--help","Display help and exit"},
}},
{ name="chmod", usage="chmod [-R] MODE file...", desc="Change file permissions. MODE may be octal (755) or symbolic (u+x).", flags={
{"-R", "Recurse into directories"},
{"--help","Display help and exit"},
}},
{ name="chown", usage="chown [-R] USER[:GROUP] file...", desc="Change file owner and/or group.", flags={
{"-R","Recurse into directories"},
{"--help","Display help and exit"},
}},
{ name="chgrp", usage="chgrp [-R] GROUP file...", desc="Change file group ownership.", flags={
{"-R","Recurse into directories"},
{"--help","Display help and exit"},
}},
{ name="chattr", usage="chattr [+-=][attrs] file...", desc="Change file attributes.", flags={} },
{ name="echo", usage="echo [text...]", desc="Print arguments to stdout.", flags={} },
{ name="whoami", usage="whoami", desc="Print the current username.", flags={} },
{ name="id", usage="id [username]", desc="Print user identity (uid, gid).", flags={} },
{ name="ps", usage="ps", desc="List running tasks with pid, user, name, and status.", flags={} },
{ name="hostname", usage="hostname [NAME]", desc="Print or set the system hostname.", flags={} },
{ name="uname", usage="uname [-asnrm]", desc="Print system information (OS name, hostname, release, machine).", flags={
{"-a","Print all fields"},
{"-s","Kernel name"},
{"-n","Node hostname"},
{"-r","Kernel release"},
{"-m","Machine hardware name"},
{"--help","Display help and exit"},
}},
{ name="df", usage="df [-h] [path...]", desc="Report filesystem disk space usage.", flags={
{"-h","Human-readable sizes (K, M, G)"},
{"--help","Display help and exit"},
}},
{ name="stat", usage="stat file...", desc="Display file status: type, size, permissions, owner.", flags={} },
{ name="env", usage="env [KEY=VAL]... [CMD]", desc="Print the environment, or run a command with modified environment.", flags={} },
{ name="printenv", usage="printenv [NAME...]", desc="Print environment variable values (all if no names given).", flags={} },
{ name="sleep", usage="sleep N[smhd]", desc="Pause for N seconds (or minutes/hours/days with m/h/d suffix).", flags={} },
{ name="true", usage="true", desc="Do nothing, exit successfully (status 0).", flags={} },
{ name="false", usage="false", desc="Do nothing, exit unsuccessfully (status 1).", flags={} },
{ name="yes", usage="yes [text]", desc="Repeatedly print 'y' (or given text) until interrupted.", flags={} },
{ name="mount", usage="mount [-o loop] [SRC DEST | ID MNT]", desc="Mount a loop device or show all current mounts.", flags={
{"-o loop","Attach SRC as a loop device and mount at DEST in one step"},
{"--help","Display help and exit"},
}},
{ name="umount", usage="umount [--no-detach] MOUNTPOINT | -l LOOPID", desc="Unmount a filesystem and auto-detach its loop device.", flags={
{"--no-detach","Unmount but keep loop device attached"},
{"-l LOOPID","Force-detach a loop device without unmounting"},
{"--help","Display help and exit"},
}},
{ name="losetup", usage="losetup [-dil] [path]", desc="Attach a directory or .hfs image as a loop device.", flags={
{"-d ID","Detach loop device"},
{"-i path","Force image mode (even without .hfs extension)"},
{"-l","List all attached loop devices"},
{"--help","Display help and exit"},
}},
{ name="loimgcreate", usage="loimgcreate [-x] SRC DEST", desc="Pack a directory into a portable HFS image, or extract one.", flags={
{"-x","Extract image to destination directory"},
{"--help","Display help and exit"},
}},
{ name="useradd", usage="useradd [-p pw] [-g gid] [-d home] [-s shell] [-M] <user>", desc="Create a new user account.", flags={
{"-p pw", "Set password"},
{"-g gid", "Set primary group id"},
{"-d home", "Set home directory (default /home/username)"},
{"-s shell","Set login shell (default /bin/hysh)"},
{"-M", "Do not create home directory"},
}},
{ name="userdel", usage="userdel [-r] <user>", desc="Delete a user account.", flags={
{"-r","Also remove the user's home directory"},
}},
{ name="usermod", usage="usermod [-l name] [-p pw] [-g gid] [-d home] [-s shell] [-LU] <user>", desc="Modify an existing user account.", flags={
{"-l name", "Rename the user"},
{"-p pw", "Set new password"},
{"-g gid", "Change primary group id"},
{"-d home", "Change home directory"},
{"-s shell","Change login shell"},
{"-L", "Lock the account"},
{"-U", "Unlock the account"},
}},
{ name="passwd", usage="passwd [username]", desc="Change a user password.", flags={} },
{ name="lsusers", usage="lsusers", desc="List all user accounts with uid, gid, home, and shell.", flags={} },
{ name="su", usage="su [username]", desc="Switch user. Defaults to root. Root can switch without a password.", flags={} },
{ name="sudo", usage="sudo [-u user] CMD [args...]", desc="Run a command as another user (default root).", flags={
{"-u user","Run as the specified user (name or uid)"},
}},
{ name="exit", usage="exit [N]", desc="Exit the shell with optional status code N.", flags={} },
{ name="clear", usage="clear", desc="Clear the terminal screen.", flags={} },
{ name="help", usage="help [command]", desc="Display this command reference. Pass a command name to filter.", flags={} },
{ name="lua", usage="lua", desc="Interactive Lua REPL prompt.", flags={} },
{ name="micro", usage="micro [file]", desc="Full-screen terminal text editor.", flags={} },
{ name="hfetch", usage="hfetch", desc="Display system information in a neofetch-style layout.", flags={} },
{ name="sysdump", usage="sysdump", desc="List all registered kernel syscalls.", flags={} },
{ name="chroot", usage="chroot DIR [CMD]", desc="Run a command with a different root directory.", flags={} },
}
-- Build lines to display
local C_HEAD = 7 -- yellow (section headers)
local C_CMD = 5 -- cyan (command name)
local C_USAGE = 1 -- white (usage line)
local C_DESC = 13 -- grey (description)
local C_FLAG = 3 -- green (flag name)
local C_DIM = 12 -- dark grey
do
local seen = {}
local deduped = {}
for _, cmd in ipairs(COMMANDS) do
if not seen[cmd.name] then
seen[cmd.name] = true
table.insert(deduped, cmd)
end
end
COMMANDS = deduped
end
local C_HEAD = 7
local C_CMD = 5
local C_USAGE = 1
local C_DESC = 13
local C_FLAG = 3
local C_DIM = 12
-- Each entry is {text, color}
local lines = {}
local function push(text, col) lines[#lines+1] = {text, col or 1} end
@@ -177,7 +208,7 @@ push(string.rep("=", 50), C_DIM)
push("", 1)
local args = {...}
local filter = args[1] -- optional: help <command>
local filter = args[1]
local function addCmd(cmd)
push(cmd.name, C_CMD)
@@ -207,11 +238,10 @@ else
for _, cmd in ipairs(COMMANDS) do addCmd(cmd) end
end
-- Pager
local sizeStr = syscall.devctl(1, "size")
local screenW = tonumber(sizeStr:sub(1, sizeStr:find(";")-1)) or 51
local screenH = tonumber(sizeStr:sub(sizeStr:find(";")+1)) or 19
local pageSize = screenH - 2 -- reserve bottom row for status bar
local screenW = tonumber(sizeStr:match("^(%d+)")) or 51
local screenH = tonumber(sizeStr:match(";(%d+)")) or 19
local pageSize = screenH - 2
local scroll = 0
local totalLines = #lines
@@ -225,14 +255,12 @@ local function render()
if li <= totalLines then
local text, col = lines[li][1], lines[li][2]
syscall.devctl(1, "sfgc", col)
-- truncate to screen width
if #text > screenW then text = text:sub(1, screenW) end
syscall.write(1, text .. "\n")
else
syscall.write(1, "\n")
end
end
-- status bar
syscall.devctl(1, "sfgc", 16)
syscall.devctl(1, "sbgc", 13)
local pct = math.floor(math.min(100, (scroll + pageSize) / totalLines * 100))
@@ -246,7 +274,6 @@ local function render()
dirty = false
end
-- If output fits on one screen, just print without pager
if totalLines <= pageSize then
syscall.devctl(1, "clear")
syscall.devctl(1, "spos", 1, 1)
@@ -262,30 +289,20 @@ render()
while true do
local ch = syscall.read(0)
if not ch or ch == "" then
-- idle
elseif ch == "q" or ch == "Q" then
break
elseif ch == "\17" then -- up arrow
if scroll > 0 then
scroll = scroll - 1
dirty = true
end
elseif ch == "\18" then -- down arrow
if scroll + pageSize < totalLines then
scroll = scroll + 1
dirty = true
end
elseif ch == "\19" then -- left / page up (reuse left arrow)
scroll = math.max(0, scroll - pageSize)
dirty = true
elseif ch == "\20" then -- right / page down
scroll = math.min(totalLines - pageSize, scroll + pageSize)
dirty = true
elseif ch == "\17" then
if scroll > 0 then scroll = scroll - 1; dirty = true end
elseif ch == "\18" then
if scroll + pageSize < totalLines then scroll = scroll + 1; dirty = true end
elseif ch == "\19" then
scroll = math.max(0, scroll - pageSize); dirty = true
elseif ch == "\20" then
scroll = math.min(totalLines - pageSize, scroll + pageSize); dirty = true
end
if dirty then render() end
end
-- Restore screen
syscall.devctl(1, "clear")
syscall.devctl(1, "spos", 1, 1)
syscall.devctl(1, "sfgc", 1)

File diff suppressed because it is too large Load Diff

96
Src/Hyperion-bash/bin/ln Normal file
View File

@@ -0,0 +1,96 @@
--:Minify:--
local name = syscall.getTask(syscall.getpid()).name
local cloptions = { s = false, f = false, help = false }
local args = {}
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name .. ": unrecognized option '" .. v .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" then
for i = 2, #v do
local opt = v:sub(i, i)
if cloptions[opt] == nil then
print(name .. ": invalid option '-" .. opt .. "'")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
cloptions[opt] = true
end
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " [OPTION]... TARGET LINK_NAME")
print(" " .. name .. " [OPTION]... TARGET... DIRECTORY")
print("Create links between files.")
print("")
print("Options:")
print(" -s make symbolic links instead of hard links")
print(" -f remove existing destination files")
print(" --help display this help and exit")
print("")
print("With no -s, hard links are not supported (filesystem limitation).")
print("Use -s for symbolic links.")
return
end
if #args < 2 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
if not cloptions.s then
print(name .. ": hard links are not supported; use -s for symbolic links")
syscall.exit(1); return
end
local dest = args[#args]
local destDir = syscall.type(dest) == "directory"
local function cwd()
local d = syscall.getcwd()
if d:sub(-1) ~= "/" then d = d .. "/" end
return d
end
local function absPath(p)
if p:sub(1,1) ~= "/" then p = cwd() .. p end
return p
end
for i = 1, #args - 1 do
local target = args[i]
local linkPath
if destDir then
local basename = target:match("[^/]+$") or target
linkPath = absPath(dest)
if linkPath:sub(-1) ~= "/" then linkPath = linkPath .. "/" end
linkPath = linkPath .. basename
else
linkPath = absPath(dest)
end
if cloptions.f and syscall.exists(linkPath) then
local ok, err = pcall(syscall.remove, linkPath)
if not ok then
print(name .. ": cannot remove '" .. linkPath .. "': " .. tostring(err))
syscall.exit(1); return
end
end
local ok, err = pcall(syscall.symlink, target, linkPath)
if not ok then
print(name .. ": failed to create symlink '" .. linkPath .. "' -> '" .. target .. "': " .. tostring(err))
syscall.exit(1); return
end
end

View File

@@ -3,7 +3,6 @@ syscall.open("/dev/tty/TTY1", "r") --stdin (fd 0)
syscall.open("/dev/tty/TTY1", "w") --stdout (fd 1)
syscall.open("/dev/null", "w") --stderr (fd 2)
local fs = require("sys.fs")
local MAX_ATTEMPTS = 3
@@ -12,7 +11,6 @@ local function readLine(mask)
while true do
local ch = syscall.read(0)
if not ch or ch == "" then
-- buffer empty, spin
elseif ch == "\n" then
syscall.write(1, "\n")
return input
@@ -29,7 +27,12 @@ local function readLine(mask)
end
local function firstBoot()
local shadow = fs.readAllText("/etc/shadow") or ""
local shadow = ""
local _fd, _fderr = pcall(function()
local fd = syscall.open("/etc/shadow", "r")
shadow = syscall.read(fd, 65535) or ""
syscall.close(fd)
end)
if shadow:match("%S") then return end
syscall.devctl(1, "clear")
@@ -71,35 +74,36 @@ local function firstBoot()
end
local function spawnShell(username, uid, shell, homedir)
local shellText = fs.readAllText(shell)
if not shellText then
local existsOk, existsErr = pcall(syscall.exists, shell)
if not existsOk or not existsErr then
syscall.write(1, "login: shell not found: " .. shell .. "\n")
sleep(2)
return false
end
local errFifo = {}
local accessOk, accessErr = pcall(syscall.access, shell, "rx")
local proc = syscall.spawn(function()
syscall.setuid(uid)
syscall.chdir(homedir)
syscall.setEnviron("HOME", homedir)
syscall.setEnviron("USER", username)
syscall.setEnviron("SHELL", shell)
syscall.setEnviron("PATH", "/bin/")
syscall.setEnviron("HOME", homedir)
syscall.setEnviron("USER", username)
syscall.setEnviron("SHELL", shell)
syscall.setEnviron("PATH", "/bin/")
local shellFn, loadErr = load(shellText, "@" .. shell)
if not shellFn then
syscall.log("login: shell load error: " .. tostring(loadErr), "ERROR")
syscall.exit(-1)
return
end
local setuidOk, setuidErr = pcall(syscall.setuid, uid)
if not setuidOk then
syscall.write(1, "login: setuid failed: " .. tostring(setuidErr) .. "\n")
sleep(2)
return false
end
local chdirOk, chdirErr = pcall(syscall.chdir, homedir)
local ok, err = pcall(syscall.execspawn, shell, username .. ":shell")
if not ok then
syscall.write(1, "login: failed to launch shell: " .. tostring(err) .. "\n")
sleep(2)
return false
end
local ok, runErr = xpcall(shellFn, debug.traceback)
if not ok then
syscall.log("login: shell runtime error: " .. tostring(runErr), "ERROR")
end
end, username .. ":shell")
syscall.exit(0)
end
@@ -126,9 +130,10 @@ local function doLogin()
local ok, err = syscall.login(username, password)
if ok then
local uid = syscall.getuid(username)
local pwent = uid and syscall.getpasswd(uid)
local shell = (pwent and pwent.shell) or "/bin/hysh"
local uid = syscall.getuid()
local pwent = syscall.getpasswd(uid)
local shell = (pwent and pwent.shell) or "/bin/hysh"
local homedir = (pwent and pwent.homedir) or "/"
syscall.devctl(1, "sfgc", 3)

View File

@@ -0,0 +1,157 @@
--:Minify:--
-- Usage:
-- loimgcreate <srcdir> <image.hfs> create image from directory
-- loimgcreate -x <image.hfs> <dest> extract image back to a directory
-- loimgcreate --help
local name = syscall.getTask(syscall.getpid()).name
local args, opts = {}, { x=false, help=false }
for _, v in ipairs({...}) do
if v:sub(1,2) == "--" then
local o = v:sub(3)
if o == "help" then opts.help = true
else print(name..": unrecognised option '"..v.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
elseif v:sub(1,1) == "-" then
for i = 2, #v do
local c = v:sub(i,i)
if opts[c] ~= nil then opts[c] = true
else print(name..": invalid option '-"..c.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
end
else
table.insert(args, v)
end
end
if opts.help then
print("Usage: "..name.." <srcdir> <image.hfs>")
print(" "..name.." -x <image.hfs> <destdir>")
print("")
print("Pack a directory into a portable HFS image file, or extract one.")
print("")
print(" <srcdir> <image.hfs> recursively pack srcdir into image.hfs")
print(" -x <image.hfs> <dest> extract image.hfs into dest (created if needed)")
print("")
print("HFS images can be mounted with:")
print(" mount -o loop /path/to/image.hfs /mnt/point")
print("")
print("Requires root.")
return
end
local fs = require("sys.fs")
if opts.x then
if #args < 2 then
print(name..": -x requires <image.hfs> and <destdir>")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local imgPath = args[1]
local destPath = args[2]
if imgPath:sub(1,1) ~= "/" then imgPath = syscall.getcwd().."/"..imgPath end
if destPath:sub(1,1) ~= "/" then destPath = syscall.getcwd().."/"..destPath end
local tmpMnt = "/tmp/._loimgcreate_"..tostring(math.random(100000,999999))
local ok1, loopId = pcall(syscall.losetup, imgPath, true)
if not ok1 then
print(name..": losetup: "..tostring(loopId)); syscall.exit(1); return
end
local ok2, merr = pcall(syscall.mount, tmpMnt, loopId)
if not ok2 then
pcall(syscall.lodetach, loopId)
print(name..": mount: "..tostring(merr)); syscall.exit(1); return
end
if not fs.isDir(destPath) then
local ok3, derr = pcall(syscall.mkdir, destPath)
if not ok3 then
pcall(syscall.umount, tmpMnt); pcall(syscall.lodetach, loopId)
print(name..": mkdir '"..args[2].."': "..tostring(derr))
syscall.exit(1); return
end
end
local count = 0
local function copyTree(src, dst)
local entries = fs.list(src)
if not entries then return end
for _, ent in ipairs(entries) do
local srcFull = src:gsub("/$","").."/"..ent
local dstFull = dst:gsub("/$","").."/"..ent
if fs.isDir(srcFull) then
pcall(syscall.mkdir, dstFull)
copyTree(srcFull, dstFull)
else
local ok, rfd = pcall(syscall.open, srcFull, "r")
if ok then
local ok2, wfd = pcall(syscall.open, dstFull, "w")
if ok2 then
local ok3, data = pcall(syscall.read, rfd, 65536*16)
if ok3 and data then pcall(syscall.write, wfd, data) end
pcall(syscall.close, wfd)
count = count + 1
end
pcall(syscall.close, rfd)
end
end
end
end
copyTree(tmpMnt, destPath)
pcall(syscall.umount, tmpMnt)
pcall(syscall.lodetach, loopId)
syscall.devctl(1, "sfgc", 10)
print(name..": extracted "..count.." file(s) to "..destPath)
syscall.devctl(1, "sfgc", 1)
return
end
if #args < 2 then
print(name..": missing operands — need <srcdir> and <image.hfs>")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local srcPath = args[1]
local imgPath = args[2]
if srcPath:sub(1,1) ~= "/" then srcPath = syscall.getcwd().."/"..srcPath end
if imgPath:sub(1,1) ~= "/" then imgPath = syscall.getcwd().."/"..imgPath end
if not fs.isDir(srcPath) then
print(name..": '"..args[1].."': not a directory")
syscall.exit(1); return
end
local ok, imgStr = pcall(syscall.loimgcreate, srcPath)
if not ok then
local msg = tostring(imgStr)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("ENOTDIR") then msg = "'"..args[1].."': not a directory" end
print(name..": "..msg); syscall.exit(1); return
end
local ok2, werr = pcall(syscall.loimgwrite, imgStr, imgPath)
if not ok2 then
print(name..": write '"..args[2].."': "..tostring(werr))
syscall.exit(1); return
end
local lineCount = 0
for _ in imgStr:gmatch("\n") do lineCount = lineCount + 1 end
local byteCount = #imgStr
syscall.devctl(1, "sfgc", 10)
print(name..": image written to "..imgPath)
syscall.devctl(1, "sfgc", 14)
print(string.format(" %d records, %d bytes", lineCount - 1, byteCount))
syscall.devctl(1, "sfgc", 1)

View File

@@ -0,0 +1,427 @@
--:Minify:--
local passed, failed = 0, 0
local function pass(msg) syscall.devctl(1,"sfgc",10); print(" PASS "..msg); syscall.devctl(1,"sfgc",1) end
local function fail(msg) syscall.devctl(1,"sfgc",2); print(" FAIL "..msg); syscall.devctl(1,"sfgc",1) end
local function info(msg) syscall.devctl(1,"sfgc",14); print(" .... "..msg); syscall.devctl(1,"sfgc",1) end
local function head(msg) syscall.devctl(1,"sfgc",4); print("\n"..msg); syscall.devctl(1,"sfgc",1) end
local function check(label, ok, err)
if ok then passed = passed + 1; pass(label)
else failed = failed + 1; fail(label.." - "..tostring(err)) end
end
local function writeFile(path, data)
local ok, fd = pcall(syscall.open, path, "w")
if not ok then return false, fd end
local ok2, err = pcall(syscall.write, fd, data)
pcall(syscall.close, fd)
return ok2, err
end
local function readFile(path)
local ok, fd = pcall(syscall.open, path, "r")
if not ok then return false, fd end
local ok2, data = pcall(syscall.read, fd, 65536)
pcall(syscall.close, fd)
return ok2, data
end
local function rmrf(path)
local t = syscall.type(path)
if t == "directory" then
local ok, entries = pcall(syscall.listdir, path)
if ok then
for _, name in ipairs(entries) do
rmrf(path:gsub("/$","").."/"..name)
end
end
pcall(syscall.remove, path)
elseif t == "file" then
pcall(syscall.remove, path)
end
end
local SCRATCH = "/tmp/looptest_scratch"
local SRC_DIR = SCRATCH.."/src"
local BIND_MNT = SCRATCH.."/bind_mnt"
local IMG_PATH = SCRATCH.."/test.hfs"
local IMG_MNT = SCRATCH.."/img_mnt"
local BIND_LOOP = nil
local IMG_LOOP = nil
rmrf(SCRATCH)
pcall(syscall.mkdir, SCRATCH)
pcall(syscall.mkdir, SRC_DIR)
pcall(syscall.mkdir, BIND_MNT)
pcall(syscall.mkdir, IMG_MNT)
pcall(syscall.mkdir, SRC_DIR.."/subdir")
writeFile(SRC_DIR.."/hello.txt", "hello from hyperion\n")
writeFile(SRC_DIR.."/data.txt", "line1\nline2\nline3\n")
writeFile(SRC_DIR.."/subdir/deep.txt", "deep file\n")
head("[ 1 ] bind mode - losetup on a directory")
do
local ok, id = pcall(syscall.losetup, SRC_DIR)
check("losetup(dir) returns a loop id", ok and type(id) == "string" and id:sub(1,4) == "loop", id)
if ok then
BIND_LOOP = id
info("attached as "..id)
end
end
head("[ 2 ] bind mode - mount and read files")
do
if BIND_LOOP then
local mok = pcall(syscall.mount, BIND_MNT, BIND_LOOP)
check("mount(bind_mnt, "..BIND_LOOP..")", mok, "mount failed")
if mok then
local lok, entries = pcall(syscall.listdir, BIND_MNT)
check("listdir through bind mount", lok and type(entries) == "table", entries)
local rok, data = readFile(BIND_MNT.."/hello.txt")
check("read hello.txt through bind", rok and data == "hello from hyperion\n",
rok and ("got: "..tostring(data):sub(1,40)) or tostring(data))
local rok2, data2 = readFile(BIND_MNT.."/subdir/deep.txt")
check("read subdir/deep.txt through bind", rok2 and data2 == "deep file\n",
rok2 and ("got: "..tostring(data2)) or tostring(data2))
end
else
check("mount (skipped - no loop id)", false, "losetup failed in [1]")
end
end
head("[ 3 ] bind mode - write through loop mount, verify on host")
do
if BIND_LOOP then
local wok, werr = writeFile(BIND_MNT.."/written.txt", "written via loop\n")
check("write new file through bind mount", wok, werr)
local rok, data = readFile(BIND_MNT.."/written.txt")
check("read back through bind mount", rok and data == "written via loop\n",
rok and ("got: "..tostring(data)) or tostring(data))
local rok2, data2 = readFile(SRC_DIR.."/written.txt")
check("file visible on host path (bind is transparent)", rok2 and data2 == "written via loop\n",
rok2 and ("got: "..tostring(data2)) or tostring(data2))
else
check("write (skipped)", false, "bind mount not set up")
end
end
head("[ 4 ] bind mode - lodetach while mounted returns EBUSY")
do
if BIND_LOOP then
local ok = pcall(syscall.lodetach, BIND_LOOP)
check("lodetach while mounted is refused (EBUSY)", not ok, "should have errored")
else
check("lodetach busy check (skipped)", false, "no bind loop")
end
end
head("[ 5 ] bind mode - umount then lodetach")
do
if BIND_LOOP then
local uok = pcall(syscall.umount, BIND_MNT)
check("umount(bind_mnt)", uok, "umount failed")
local dok = pcall(syscall.lodetach, BIND_LOOP)
check("lodetach after umount", dok, "lodetach failed")
if dok then BIND_LOOP = nil end
else
check("umount+lodetach (skipped)", false, "no bind loop")
end
end
head("[ 6 ] loimgcreate - serialise directory to HFS image")
do
local ok, imgStr = pcall(syscall.loimgcreate, SRC_DIR)
check("loimgcreate(srcdir) returns a string", ok and type(imgStr) == "string" and #imgStr > 0, imgStr)
if ok then
info("image size: "..#imgStr.." bytes")
local isBHFS = imgStr:sub(1, 4) == "BHFS"
check("image has BHFS magic header", isBHFS,
"got: "..imgStr:sub(1,4):gsub(".", function(c) return string.format("%02X ", c:byte()) end))
check("image has correct version byte (0x01)", imgStr:byte(5) == 1,
"version byte: "..tostring(imgStr:byte(5)))
check("image contains FILE record (type=0x01)", imgStr:find("\001", 9, true) ~= nil, "no FILE type byte found")
check("image contains DIR record (type=0x02)", imgStr:find("\002", 9, true) ~= nil, "no DIR type byte found")
check("image ends with END record (type=0xFF)", imgStr:byte(#imgStr) == 0xFF,
"last byte: 0x"..string.format("%02X", imgStr:byte(#imgStr)))
local wok, werr = pcall(syscall.loimgwrite, imgStr, IMG_PATH)
check("loimgwrite writes image file", wok, werr)
check("image file exists on disk", syscall.type(IMG_PATH) == "file", "file not found")
end
end
head("[ 7 ] HFS image - losetup attaches image file")
do
if syscall.type(IMG_PATH) == "file" then
local ok, id = pcall(syscall.losetup, IMG_PATH)
check("losetup(image.hfs) returns loop id", ok and type(id) == "string", id)
if ok then
IMG_LOOP = id
info("image attached as "..id)
local lok, devs = pcall(syscall.lolist)
local found = false
if lok then
for lid, info_entry in pairs(devs) do
if lid == id then found = true end
end
end
check("lolist() contains new image device", found, "not found in lolist")
end
else
check("losetup image (skipped - no image file)", false, "image not created in [6]")
end
end
head("[ 8 ] HFS image - mount and read files")
do
if IMG_LOOP then
local mok = pcall(syscall.mount, IMG_MNT, IMG_LOOP)
check("mount(img_mnt, "..IMG_LOOP..")", mok, "mount failed")
if mok then
local lok, entries = pcall(syscall.listdir, IMG_MNT)
check("listdir through image mount returns table", lok and type(entries) == "table", entries)
if lok then
local hasHello = false
local hasSubdir = false
for _, e in ipairs(entries) do
if e == "hello.txt" then hasHello = true end
if e == "subdir" then hasSubdir = true end
end
check("hello.txt visible in image root", hasHello, "not listed")
check("subdir/ visible in image root", hasSubdir, "not listed")
end
local rok, data = readFile(IMG_MNT.."/hello.txt")
check("read hello.txt from image", rok and data == "hello from hyperion\n",
rok and ("got: "..tostring(data):sub(1,40)) or tostring(data))
local rok2, data2 = readFile(IMG_MNT.."/data.txt")
check("read data.txt from image", rok2 and data2 == "line1\nline2\nline3\n",
rok2 and ("got: "..tostring(data2)) or tostring(data2))
end
else
check("image mount read (skipped)", false, "no image loop")
end
end
head("[ 9 ] HFS image - write new files into image mount")
do
if IMG_LOOP then
local wok, werr = writeFile(IMG_MNT.."/newfile.txt", "created inside image\n")
check("write new file into image mount", wok, werr)
local rok, data = readFile(IMG_MNT.."/newfile.txt")
check("read back newly written file", rok and data == "created inside image\n",
rok and ("got: "..tostring(data)) or tostring(data))
local wok2, werr2 = writeFile(IMG_MNT.."/hello.txt", "overwritten\n")
check("overwrite existing file in image", wok2, werr2)
local rok2, data2 = readFile(IMG_MNT.."/hello.txt")
check("overwritten content reads back correctly", rok2 and data2 == "overwritten\n",
rok2 and ("got: "..tostring(data2)) or tostring(data2))
local rok3, orig = readFile(IMG_PATH)
check("disk image file is unchanged after in-memory write",
rok3 and orig and orig:find("/hello%.txt") ~= nil,
rok3 and "filename record missing from image" or tostring(orig))
else
check("image write test (skipped)", false, "image not mounted")
end
end
head("[ 10 ] HFS image - sub-directory traversal")
do
if IMG_LOOP then
local t = syscall.type(IMG_MNT.."/subdir")
check("type(subdir) == 'directory'", t == "directory", "got: "..tostring(t))
local lok, entries = pcall(syscall.listdir, IMG_MNT.."/subdir")
check("listdir(subdir) works", lok and type(entries) == "table", entries)
local rok, data = readFile(IMG_MNT.."/subdir/deep.txt")
check("read subdir/deep.txt from image", rok and data == "deep file\n",
rok and ("got: "..tostring(data)) or tostring(data))
local mok = pcall(syscall.mkdir, IMG_MNT.."/subdir/newdir")
check("mkdir inside image mount", mok, "mkdir failed")
check("new dir has type 'directory'",
syscall.type(IMG_MNT.."/subdir/newdir") == "directory",
"wrong type")
local wok, werr = writeFile(IMG_MNT.."/subdir/newdir/x.txt", "x\n")
check("write file in newly created subdir", wok, werr)
else
check("subdir traversal (skipped)", false, "image not mounted")
end
end
head("[ 11 ] HFS image - lodetach while mounted returns EBUSY")
do
if IMG_LOOP then
local ok = pcall(syscall.lodetach, IMG_LOOP)
check("lodetach image while mounted is refused", not ok, "should have errored EBUSY")
else
check("lodetach busy (skipped)", false, "no image loop")
end
end
head("[ 12 ] HFS image - umount then lodetach")
do
if IMG_LOOP then
local uok = pcall(syscall.umount, IMG_MNT)
check("umount(img_mnt)", uok, "umount failed")
local dok = pcall(syscall.lodetach, IMG_LOOP)
check("lodetach after umount", dok, "lodetach failed")
if dok then
local lok, devs = pcall(syscall.lolist)
local found = false
if lok then
for lid in pairs(devs) do
if lid == IMG_LOOP then found = true end
end
end
check("lolist no longer shows detached device", not found, "still present in lolist")
IMG_LOOP = nil
end
else
check("image umount+lodetach (skipped)", false, "no image loop")
end
end
head("[ 13 ] lolist - reflects attached device count")
do
local ok1, id1 = pcall(syscall.losetup, SRC_DIR)
local ok2, id2 = pcall(syscall.losetup, SRC_DIR)
check("attach first device for lolist test", ok1, id1)
check("attach second device for lolist test", ok2, id2)
if ok1 and ok2 then
local lok, devs = pcall(syscall.lolist)
check("lolist() succeeds", lok, devs)
if lok then
local found1, found2 = false, false
for lid in pairs(devs) do
if lid == id1 then found1 = true end
if lid == id2 then found2 = true end
end
check("lolist contains first device", found1, "missing "..id1)
check("lolist contains second device", found2, "missing "..id2)
local count = 0
for _ in pairs(devs) do count = count + 1 end
info("lolist shows "..count.." device(s)")
end
end
if ok1 then pcall(syscall.lodetach, id1) end
if ok2 then pcall(syscall.lodetach, id2) end
end
head("[ 14 ] losetup - non-existent path returns error")
do
local ok = pcall(syscall.losetup, "/tmp/does_not_exist_xyz_looptest")
check("losetup on missing path errors", not ok, "should have errored")
end
head("[ 15 ] mount - same loop device cannot be mounted twice")
do
local ok, id = pcall(syscall.losetup, SRC_DIR)
check("losetup for double-mount test", ok, id)
if ok then
local m1ok = pcall(syscall.mount, BIND_MNT, id)
check("first mount succeeds", m1ok, "mount failed")
if m1ok then
local m2ok = pcall(syscall.mount, IMG_MNT, id)
check("second mount of same device is refused", not m2ok, "should have errored EBUSY")
pcall(syscall.umount, BIND_MNT)
end
pcall(syscall.lodetach, id)
end
end
head("[ 16 ] loimgcreate - on a regular file returns ENOTDIR")
do
local ok = pcall(syscall.loimgcreate, IMG_PATH)
check("loimgcreate on a file errors (ENOTDIR)", not ok, "should have errored")
end
head("[ 17 ] HFS image - binary round-trip (all byte values)")
do
local bytes = {}
for i = 0, 255 do bytes[i+1] = string.char(i) end
local binData = table.concat(bytes)
local binSrc = SCRATCH.."/binsrc"
pcall(syscall.mkdir, binSrc)
writeFile(binSrc.."/binary.bin", binData)
local ok1, imgStr = pcall(syscall.loimgcreate, binSrc)
check("loimgcreate handles binary content", ok1, imgStr)
if ok1 then
local binImg = SCRATCH.."/binary.hfs"
pcall(syscall.loimgwrite, imgStr, binImg)
local ok2, lid = pcall(syscall.losetup, binImg)
check("losetup on binary image", ok2, lid)
if ok2 then
local mnt = SCRATCH.."/binmnt"
pcall(syscall.mkdir, mnt)
local mok = pcall(syscall.mount, mnt, lid)
check("mount binary image", mok, "mount failed")
if mok then
local rok, readBack = readFile(mnt.."/binary.bin")
check("binary file readable from image", rok, readBack)
check("binary data round-trips without corruption",
rok and readBack == binData,
rok and string.format("length in=%d out=%d", #binData, #(readBack or "")) or tostring(readBack))
pcall(syscall.umount, mnt)
end
pcall(syscall.lodetach, lid)
end
end
end
head("[ 18 ] second-run safety - lolist is empty after full cleanup")
do
local lok, devs = pcall(syscall.lolist)
check("lolist() call succeeds", lok, devs)
if lok then
local count = 0
for _ in pairs(devs) do count = count + 1 end
check("no leftover loop devices after all tests", count == 0,
count.." device(s) still attached: "..
(function()
local ids = {}
for id in pairs(devs) do ids[#ids+1] = id end
return table.concat(ids, ", ")
end)())
end
end
rmrf(SCRATCH)
print("")
syscall.devctl(1, "sfgc", failed == 0 and 10 or 2)
print(string.format("Results: %d passed, %d failed", passed, failed))
syscall.devctl(1, "sfgc", 1)
if failed > 0 then syscall.exit(1) end

View File

@@ -0,0 +1,129 @@
--:Minify:--
-- Usage:
-- losetup <path> attach directory or .hfs image; print loop id
-- losetup -d <id> detach loop device
-- losetup -l list attached loop devices
-- losetup -i <path> force image mode (even without .hfs extension)
-- losetup --help
local name = syscall.getTask(syscall.getpid()).name
local args, opts = {}, { d=false, l=false, i=false, help=false }
for _, v in ipairs({...}) do
if v:sub(1,2) == "--" then
local o = v:sub(3)
if o == "help" then opts.help = true
else print(name..": unrecognised option '"..v.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
elseif v:sub(1,1) == "-" then
for i = 2, #v do
local c = v:sub(i,i)
if opts[c] ~= nil then opts[c] = true
else print(name..": invalid option '-"..c.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
end
else
table.insert(args, v)
end
end
if opts.help then
print("Usage: "..name.." <path>")
print(" "..name.." -i <path>")
print(" "..name.." -d <id>")
print(" "..name.." -l")
print("")
print("Manage loop devices.")
print("")
print(" <path> attach a directory (bind) or .hfs image file")
print(" -i <path> force image mode for the given file")
print(" -d <id> detach loop device by id (must be unmounted first)")
print(" -l list all currently attached loop devices")
print("")
print("Requires root. Loop device ids look like loop0, loop1, …")
return
end
if opts.l then
local ok, devs = pcall(syscall.lolist)
if not ok then
print(name..": "..tostring(devs)); syscall.exit(1); return
end
local any = false
local ids = {}
for id in pairs(devs) do ids[#ids+1] = id end
table.sort(ids)
for _, id in ipairs(ids) do
any = true
local info = devs[id]
local mode = (type(info) == "table" and info.mode) or "bind"
local path = (type(info) == "table" and info.path) or tostring(info)
local colour = mode == "image" and 5 or 4
syscall.devctl(1, "sfgc", 3)
printInline(string.format("%-10s", id))
syscall.devctl(1, "sfgc", colour)
printInline(string.format("%-7s", "["..mode.."]"))
syscall.devctl(1, "sfgc", 1)
print(" "..path)
end
if not any then
syscall.devctl(1, "sfgc", 14)
print(name..": no loop devices attached")
syscall.devctl(1, "sfgc", 1)
end
return
end
if opts.d then
if #args < 1 then
print(name..": -d requires a loop device id")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local id = args[1]
local ok, err = pcall(syscall.lodetach, id)
if not ok then
local msg = tostring(err)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("ENXIO") then msg = "no such loop device '"..id.."'"
elseif msg:find("EBUSY") then msg = "device '"..id.."' is still mounted, unmount first"
end
print(name..": "..msg); syscall.exit(1); return
end
syscall.devctl(1, "sfgc", 10)
print(name..": detached "..id)
syscall.devctl(1, "sfgc", 1)
return
end
if #args < 1 then
print(name..": missing path operand")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local path = args[1]
if path:sub(1,1) ~= "/" then
path = syscall.getcwd().."/"..path
end
local ftype = syscall.type and syscall.type(path)
if not (ftype == "file" or ftype == "directory") then
print(name..": '"..args[1].."': no such file or directory")
syscall.exit(1); return
end
local ok, result = pcall(syscall.losetup, path, opts.i or nil)
if not ok then
local msg = tostring(result)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("ENOENT") then msg = "'"..args[1].."': no such file"
elseif msg:find("EINVAL") then msg = "'"..args[1].."': not a directory or .hfs image"
elseif msg:find("EIO") then msg = "'"..args[1].."': I/O error reading image"
end
print(name..": "..msg); syscall.exit(1); return
end
print(result)

View File

@@ -1,44 +1,32 @@
--:Minify:--
local cloptions = {
a = false,
h = false,
l = false,
a = false,
h = false,
l = false,
help = false,
}
local inpArgs = { ... }
local args = {}
local name = syscall.getTask(syscall.getpid()).name
local inpArgs = {...}
local args = {}
local name = syscall.getTask(syscall.getpid()).name
local optToSet = false
for _, v in pairs(inpArgs) do
if optToSet then
cloptions[optToSet] = v
optToSet = false
elseif v:sub(1, 2) == "--" then
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name..": unrecognized option '"..v.."'.")
if cloptions.help ~= nil then
print("try '"..name.." --help' for more information.")
end
print(name .. ": unrecognized option '" .. v .. "'.")
print("try '" .. name .. " --help' for more information.")
return
elseif cloptions[opt] == false then
cloptions[opt] = true
else
optToSet = opt
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" then
for i = 2, #v do
local opt = v:sub(i, i)
if cloptions[opt] == nil then
print(name..": invalid option '-"..opt.."'.")
if cloptions.help ~= nil then
print("try '"..name.." --help' for more information.")
end
print(name .. ": invalid option '-" .. opt .. "'.")
print("try '" .. name .. " --help' for more information.")
return
else
cloptions[opt] = true
end
cloptions[opt] = true
end
else
table.insert(args, v)
@@ -46,100 +34,160 @@ for _, v in pairs(inpArgs) do
end
if cloptions.help then
print("Usage: "..name.." [OPTION]... [DIR]")
print("List all entries in the specified DIRectory, or cwd if not specified")
print("\nOptions:")
print(" -a Do not ignore entries starting with .")
print(" -h with -l, print sizes in a human readble format")
print(" -l Use a long listing format")
print(" --help Display this help and exit")
print("Usage: " .. name .. " [OPTION]... [DIR]")
print("List all entries in the specified DIRectory, or cwd if not specified.")
print("")
print("Options:")
print(" -a do not ignore entries starting with .")
print(" -h with -l, print sizes in human readable format")
print(" -l use a long listing format")
print(" --help display this help and exit")
return
end
local fs = require("sys.fs")
local dir = (args[1] or "")
local fs = require("sys.fs")
local dir = args[1] or ""
if dir:sub(1, 1) ~= "/" then
dir = syscall.getcwd().."/"..dir
end
if dir:sub(#dir, #dir) ~= "/" then
dir = dir.."/"
dir = syscall.getcwd() .. "/" .. dir
end
if dir:sub(-1) ~= "/" then dir = dir .. "/" end
if not fs.isDir(dir) then
print(name..": Cannot access '"..args[1].."': No such directory.")
print(name .. ": cannot access '" .. (args[1] or dir) .. "': no such directory")
return
end
local function permStr(perms, etype)
local function b(n) return math.floor(perms / (2^n)) % 2 == 1 end
local t
if etype == 0x01 then t = "l"
elseif etype == nil then t = "-"
else t = "-" end
local ur = b(5) and "r" or "-"
local uw = b(4) and "w" or "-"
local ux = b(9) and (b(6) and "s" or "x") or (b(6) and "S" or "-")
local gr = b(3) and "r" or "-"
local gw = b(2) and "w" or "-"
local gx = b(8) and "x" or "-"
local wr = b(1) and "r" or "-"
local ww = b(0) and "w" or "-"
local wx = b(7) and "x" or "-"
return t .. ur .. uw .. ux .. gr .. gw .. gx .. wr .. ww .. wx
end
local sizePrefixes = { "K", "M", "G", "T" }
local function humanSize(size)
local scale = 0
while size >= 1024 and scale < #sizePrefixes do
size = size / 1024
scale = scale + 1
end
if scale == 0 then return tostring(size) end
if size < 10 then
return string.format("%.1f%s", size, sizePrefixes[scale])
end
return math.floor(size) .. sizePrefixes[scale]
end
local screenSizeStr = syscall.devctl(1, "size")
local sizeX = tonumber(screenSizeStr:sub(1, screenSizeStr:find(";")-1))
local sizeY = tonumber(screenSizeStr:sub(screenSizeStr:find(";")+1))
local sizeX = tonumber(screenSizeStr:match("^(%d+)")) or 80
local list = fs.list(dir)
if not cloptions.a then
for i = #list, 1, -1 do
if list[i]:sub(1, 1) == "." then
table.remove(list, i)
end
if list[i]:sub(1, 1) == "." then table.remove(list, i) end
end
end
table.sort(list)
if #list == 0 then return end
if cloptions.l then
for _, v in ipairs(list) do
local fullPath = dir .. v
local stat = syscall.lstat and syscall.lstat(fullPath) or syscall.stat(fullPath)
local isDir = fs.isDir(fullPath)
local isSym = stat and stat.etype == 0x01
local typeChar
if isSym then typeChar = "l"
elseif isDir then typeChar = "d"
else typeChar = "-" end
local pstr
if stat and stat.perms then
pstr = permStr(stat.perms, stat.etype)
else
pstr = typeChar .. "---------"
end
local size = (stat and stat.size) or 0
local sizeStr = cloptions.h and humanSize(size) or tostring(size)
local mtime = (stat and stat.modified) and math.floor(stat.modified / 1000) or 0
local owner = (stat and tostring(stat.owner)) or "0"
local group = (stat and tostring(stat.group)) or "0"
printInline(pstr .. " " .. owner .. " " .. group .. " ")
printInline(string.format("%6s", sizeStr) .. " ")
printInline(tostring(mtime) .. " ")
if isSym then
syscall.devctl(1, "sfgc", 6)
printInline(v)
syscall.devctl(1, "sfgc", 1)
local ok, target = pcall(syscall.readlink, fullPath)
if ok then
printInline(" -> ")
local targetExists = pcall(syscall.stat, fullPath)
syscall.devctl(1, "sfgc", targetExists and 6 or 2)
printInline(target)
syscall.devctl(1, "sfgc", 1)
end
elseif isDir then
syscall.devctl(1, "sfgc", 4)
printInline(v)
syscall.devctl(1, "sfgc", 1)
else
local isExec = stat and stat.perms and (math.floor(stat.perms / (2^9)) % 2 == 1)
syscall.devctl(1, "sfgc", isExec and 3 or 1)
printInline(v)
syscall.devctl(1, "sfgc", 1)
end
print("")
end
return
end
local colWidth = 0
local numCols = 1
if not cloptions.l then
for _, item in pairs(list) do
if #item + 2 > colWidth then
colWidth = #item + 2
end
end
numCols = math.floor(sizeX / colWidth)
for _, v in ipairs(list) do
if #v + 2 > colWidth then colWidth = #v + 2 end
end
local numCols = math.max(1, math.floor(sizeX / colWidth))
local sizePrefixes = {"K", "M", "G"}
for i, v in ipairs(list) do
local fullPath = dir .. v
local isDir = fs.isDir(fullPath)
local stat = syscall.lstat and syscall.lstat(fullPath) or syscall.stat(fullPath)
local isSym = stat and stat.etype == 0x01
for i,v in ipairs(list) do
local fileStats = syscall.stat(dir..v)
local isDir = fs.isDir(dir..v)
if cloptions.l then
if isDir then
printInline("d")
else
printInline("-")
end
printInline("------ ")
printInline(fileStats.owner.." ")
printInline(fileStats.group.." ")
local size = fileStats.size
if cloptions.h then
local scale = 0
while size > 1024 do
size = size / 1024
scale = scale + 1
end
if scale > 0 then
if size < 10 then
size = math.floor(size).."."..math.floor((size * 10) % 10)..sizePrefixes[scale]
else
size = math.floor(size)..sizePrefixes[scale]
end
end
end
printInline(size.." ")
printInline(math.floor(fileStats.modified / 1000).." ")
end
if isDir then
syscall.devctl(1,"sfgc",4)
if isSym then
syscall.devctl(1, "sfgc", 6)
elseif isDir then
syscall.devctl(1, "sfgc", 4)
else
syscall.devctl(1,"sfgc",1)
local isExec = stat and stat.perms and (math.floor(stat.perms / (2^9)) % 2 == 1)
syscall.devctl(1, "sfgc", isExec and 3 or 1)
end
printInline(v)
syscall.devctl(1, "sfgc", 1)
printInline((" "):rep(colWidth - #v))
syscall.devctl(1,"sfgc",1)
if i % numCols == 0 then
print("")
end
if i % numCols == 0 then print("") end
end
if #list % numCols ~= 0 then
print("")
end
if #list % numCols ~= 0 then print("") end

423
Src/Hyperion-bash/bin/micro Normal file
View File

@@ -0,0 +1,423 @@
--:Minify:--
-- Arrows move cursor Home/End line start/end
-- PgUp/PgDn page up/down Backspace delete left
-- Ctrl-D/Delete delete right Tab 4 spaces
-- Ctrl-W save Ctrl-X save + quit
-- Ctrl-P quit Ctrl-K cut line
-- Ctrl-U paste Ctrl-F find
-- Ctrl-N find next Ctrl-G go to line
-- Ctrl-A line start Ctrl-E line end
-- Ctrl-B page up Ctrl-L page down
local args = { ... }
local function termSize()
local s = syscall.devctl(1, "size")
return tonumber(s:match("^(%d+)")) or 80,
tonumber(s:match(";(%d+)$")) or 24
end
local function tpos(x,y) syscall.devctl(1,"spos",x,y) end
local function tfg(c) syscall.devctl(1,"sfgc",c) end
local function tbg(c) syscall.devctl(1,"sbgc",c) end
local function twrite(s) if s and s~="" then syscall.write(1,s) end end
local function tclear() syscall.devctl(1,"clear") end
local W, H = termSize()
local ROWS = H - 2
local lines = {""}
local cx = 1
local cy = 1
local scrollY = 0
local dirty = true
local fname = nil
local msg = ""
local msgErr = false
local clip = nil
local sPat = ""
local sLine = 0
local blinkState = false
local function absPath(p)
if p:sub(1,1) == "/" then return p end
local cwd = syscall.getcwd()
cwd = cwd:gsub("/+$", "")
return cwd .. "/" .. p
end
local function loadFile(path)
if not syscall.exists(path) then
lines = {""}; msg = "[new file]"; return
end
local fd = syscall.open(path, "r")
local buf = ""
while true do
local c = syscall.read(fd, 4096)
if not c or c == "" then break end
buf = buf .. c
end
syscall.close(fd)
lines = {}
for ln in (buf.."\n"):gmatch("([^\n]*)\n") do
table.insert(lines, ln)
end
if #lines > 1 and lines[#lines] == "" and buf:sub(-1) == "\n" then
table.remove(lines)
end
if #lines == 0 then lines = {""} end
end
local function saveFile(path)
local ok, err = pcall(function()
local fd = syscall.open(path, "w")
for i, ln in ipairs(lines) do
syscall.write(fd, ln)
if i < #lines then syscall.write(fd, "\n") end
end
syscall.write(fd, "\n")
syscall.close(fd)
end)
if ok then
msg = "Saved: "..path; msgErr = false
else
msg = "Save failed: "..tostring(err); msgErr = true
end
end
local function wrappedRows(lineStr)
return math.max(1, math.ceil(#lineStr / W))
end
local function logicalToScreen(li, col)
return math.floor((col - 1) / W)
end
local function buildScreenMap()
local map = {}
local sr = 0
for li = 1, #lines do
local len = #lines[li]
local nrows = wrappedRows(lines[li])
for r = 0, nrows - 1 do
sr = sr + 1
map[sr] = {li, r * W + 1}
end
end
return map, sr
end
local function cursorScreenRow(map)
local offset = logicalToScreen(cy, cx)
for sr, entry in ipairs(map) do
if entry[1] == cy and math.floor((entry[2]-1)/W) == offset then
return sr
end
end
return 1
end
local function clampCx()
local m = #lines[cy] + 1
if cx > m then cx = m end
if cx < 1 then cx = 1 end
end
local function clampScroll(map)
local csr = cursorScreenRow(map)
if csr - 1 < scrollY then scrollY = csr - 1 end
if csr - 1 >= scrollY + ROWS then scrollY = csr - ROWS end
if scrollY < 0 then scrollY = 0 end
end
local function pad(s, w)
if #s >= w then return s:sub(1, w) end
return s .. string.rep(" ", w - #s)
end
local function drawTop()
tpos(1,1); tbg(4); tfg(16)
local left = " edit" .. (fname and (" - "..fname) or "")
if dirty then left = left .. " [+]" end
local right = tostring(cy)..","..tostring(cx).." "
twrite(pad(left..string.rep(" ", math.max(1, W-#left-#right))..right, W))
tbg(16); tfg(1)
end
local function drawBottom()
tpos(1, H); tbg(4); tfg(16)
if msg ~= "" then
if msgErr then tbg(2) end
twrite(pad(" "..msg, W))
msg = ""; msgErr = false
else
twrite(pad(" ^W Save ^X Quit+Save ^P Quit ^K Cut ^U Paste ^F Find ^G Go", W))
end
tbg(16); tfg(1)
end
local function drawLines(map)
local curSR = cursorScreenRow(map)
local curRowOffset = logicalToScreen(cy, cx)
local curColInRow = cx - curRowOffset * W
for row = 1, ROWS do
local sr = scrollY + row
tpos(1, row + 1)
local entry = map[sr]
if entry then
local li = entry[1]
local startCol = entry[2]
local seg = lines[li]:sub(startCol, startCol + W - 1)
local isCursorRow = (sr == curSR)
if isCursorRow then
local ci = curColInRow
ci = math.min(ci, #seg + 1)
local before = seg:sub(1, ci-1)
local curCh = ci > #seg and " " or seg:sub(ci, ci)
local after = seg:sub(ci+1)
tfg(1); tbg(16); twrite(before)
if blinkState then tfg(16); tbg(1) else tfg(1); tbg(16) end
twrite(curCh)
tfg(1); tbg(16); twrite(after)
local drawn = #before + 1 + #after
if drawn < W then twrite(string.rep(" ", W - drawn)) end
else
if li == sLine and sPat ~= "" and entry[2] == 1 then
local s, e = seg:find(sPat)
if s then
tfg(1); tbg(16); twrite(seg:sub(1,s-1))
tfg(16); tbg(3); twrite(seg:sub(s,e))
tfg(1); tbg(16); twrite(seg:sub(e+1))
twrite(string.rep(" ", W - #seg))
else
tfg(1); tbg(16); twrite(pad(seg, W))
end
else
tfg(1); tbg(16); twrite(pad(seg, W))
end
end
else
tfg(13); tbg(16); twrite(pad("~", W)); tfg(1)
end
end
end
local function redraw()
W, H = termSize(); ROWS = H - 2
local map = buildScreenMap()
clampScroll(map)
drawTop()
drawLines(map)
drawBottom()
tpos(1, H)
tbg(16); tfg(1)
end
local function prompt(label, default)
local inp = default or ""
while true do
tpos(1, H); tbg(3); tfg(16)
twrite(pad(" "..label..inp.." ", W))
tbg(16); tfg(1)
local key = syscall.read(0)
if not key or key == "" then sleep(0.02)
elseif key == "\27" then return nil
elseif key == "\n" then return inp
elseif key == "\b" then if #inp > 0 then inp = inp:sub(1,-2) end
else
local b = key:byte(1)
if b >= 32 and b < 127 then inp = inp..key:sub(1,1) end
end
end
end
local function insChar(c)
local ln = lines[cy]
lines[cy] = ln:sub(1,cx-1)..c..ln:sub(cx)
cx = cx+1; dirty = true
end
local function delLeft()
if cx > 1 then
local ln = lines[cy]
lines[cy] = ln:sub(1,cx-2)..ln:sub(cx)
cx = cx-1; dirty = true
elseif cy > 1 then
local above = lines[cy-1]
cx = #above+1
lines[cy-1] = above..lines[cy]
table.remove(lines, cy)
cy = cy-1; dirty = true
end
end
local function delRight()
local ln = lines[cy]
if cx <= #ln then
lines[cy] = ln:sub(1,cx-1)..ln:sub(cx+1); dirty = true
elseif cy < #lines then
lines[cy] = ln..lines[cy+1]
table.remove(lines, cy+1); dirty = true
end
end
local function newline()
local ln = lines[cy]
local pre = ln:sub(1,cx-1)
local post = ln:sub(cx)
local ind = pre:match("^(%s*)") or ""
lines[cy] = pre
table.insert(lines, cy+1, ind..post)
cy = cy+1; cx = #ind+1; dirty = true
end
local function cutLine()
clip = lines[cy]
table.remove(lines, cy)
if #lines == 0 then lines = {""} end
if cy > #lines then cy = #lines end
cx = 1; dirty = true; msg = "Cut"
end
local function pasteLine()
if not clip then msg = "Nothing to paste"; return end
table.insert(lines, cy, clip)
cy = cy+1; cx = 1; dirty = true; msg = "Pasted"
end
local function findNext()
if sPat == "" then
local p = prompt("Find: ", "")
if not p or p == "" then dirty = true; return end
sPat = p; sLine = 0
end
local start = sLine > 0 and sLine or cy
for i = 1, #lines do
local idx = (start-1+i) % #lines + 1
if lines[idx]:find(sPat) then
cy = idx; sLine = idx
cx = lines[idx]:find(sPat) or 1
msg = "Found: line "..idx; dirty = true; return
end
end
msg = "Not found: "..sPat; msgErr = true; dirty = true
end
local function goToLine()
local p = prompt("Go to line: ", "")
if not p then dirty = true; return end
local n = tonumber(p)
if not n then msg = "Not a number"; msgErr = true; dirty = true; return end
cy = math.max(1, math.min(#lines, math.floor(n)))
cx = 1; msg = "Line "..cy; dirty = true
end
local function doSave()
if not fname then
local p = prompt("Save as: ", "")
dirty = true
if not p or p == "" then return false end
fname = absPath(p)
end
saveFile(fname); dirty = true; return not msgErr
end
local function moveCursorUp(map)
local csr = cursorScreenRow(map)
if csr <= 1 then return end
local prev = map[csr - 1]
if not prev then return end
local newLi = prev[1]
local newCol = prev[2] + (cx - 1) % W
cx = math.min(newCol, #lines[newLi] + 1)
cy = newLi
end
local function moveCursorDown(map)
local csr = cursorScreenRow(map)
local next = map[csr + 1]
if not next then return end
local newLi = next[1]
local newCol = next[2] + (cx - 1) % W
cx = math.min(newCol, #lines[newLi] + 1)
cy = newLi
end
if args[1] then
fname = absPath(args[1])
loadFile(fname)
end
tclear()
local running = true
while running do
local map = buildScreenMap()
local key = syscall.read(0)
if key and key ~= "" then
local b = key:byte(1)
if key == "\17" then moveCursorUp(map); dirty=true
elseif key == "\18" then moveCursorDown(map); dirty=true
elseif key == "\19" then
if cx > 1 then cx=cx-1
elseif cy > 1 then cy=cy-1; cx=#lines[cy]+1 end
dirty=true
elseif key == "\20" then
if cx <= #lines[cy] then cx=cx+1
elseif cy < #lines then cy=cy+1; cx=1 end
dirty=true
elseif key == "\n" then newline()
elseif key == "\b" then delLeft()
elseif key == "\t" then for _=1,4 do insChar(" ") end
elseif b == 1 then cx=1; dirty=true
elseif b == 2 then
for _=1,ROWS do moveCursorUp(map) end; dirty=true
elseif b == 4 then delRight()
elseif b == 5 then cx=#lines[cy]+1; dirty=true
elseif b == 6 then
local p=prompt("Find: ",sPat); dirty=true
if p then sPat=p; sLine=0; findNext() end
elseif b == 7 then goToLine()
elseif b == 11 then cutLine()
elseif b == 12 then
for _=1,ROWS do moveCursorDown(map) end; dirty=true
elseif b == 14 then
if sPat=="" then
local p=prompt("Find: ",""); dirty=true
if p then sPat=p; sLine=0 end
end
findNext()
elseif b == 16 then
if dirty then
local p=prompt("Unsaved changes. Quit? [y/N] ","")
dirty=true
if p and p:lower()=="y" then running=false end
else running=false end
elseif b == 21 then pasteLine()
elseif b == 23 then doSave()
elseif b == 24 then doSave(); running=false
else
if b >= 32 and b < 127 then insChar(key:sub(1,1)) end
end
end
local curBlink = (math.floor(syscall.getUptime() / 500) % 2) == 0
if curBlink ~= blinkState then
blinkState = curBlink
dirty = true
end
if dirty then
clampCx()
redraw()
dirty = false
end
sleep(0.05)
end
tclear(); tfg(1); tbg(16); tpos(1,1)
print("edit: exited"..(fname and (" - "..fname) or ""))

152
Src/Hyperion-bash/bin/mount Normal file
View File

@@ -0,0 +1,152 @@
--:Minify:--
-- Usage:
-- mount list all current mounts
-- mount <id> <mountpoint> mount loop device id at mountpoint
-- mount -o loop <src> <dest> attach <src> as loop device and mount at <dest>
-- mount --help
local name = syscall.getTask(syscall.getpid()).name
local args, opts = {}, { help=false, o=nil }
local i = 1
local rawArgs = {...}
while i <= #rawArgs do
local v = rawArgs[i]
if v:sub(1,2) == "--" then
local o = v:sub(3)
if o == "help" then opts.help = true
else print(name..": unrecognised option '"..v.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
elseif v == "-o" then
i = i + 1
opts.o = rawArgs[i]
elseif v:sub(1,1) == "-" then
local rest = v:sub(2)
if rest:sub(1,1) == "o" then
if #rest > 1 then opts.o = rest:sub(2)
else i = i + 1; opts.o = rawArgs[i] end
else
print(name..": invalid option '"..v.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
else
table.insert(args, v)
end
i = i + 1
end
if opts.help then
print("Usage: "..name)
print(" "..name.." <id> <mountpoint>")
print(" "..name.." -o loop <source> <mountpoint>")
print("")
print("Mount a loop device or filesystem.")
print("")
print(" (no args) list all active mount points")
print(" <id> <mountpoint> mount an already-attached loop device")
print(" -o loop <src> <dest> attach src as loop device and mount at dest")
print(" src can be a directory (bind) or .hfs image")
print("")
print("Requires root for all operations except listing.")
return
end
if #args == 0 and not opts.o then
local ok, mounts = pcall(syscall.mounts or function()
error("ENOSYS")
end)
local loDevs = {}
local lok, ld = pcall(syscall.lolist)
if lok then
for id, info in pairs(ld) do
local path = (type(info)=="table" and info.path) or tostring(info)
local mode = (type(info)=="table" and info.mode) or "bind"
loDevs[id] = { path=path, mode=mode }
end
end
if next(loDevs) == nil then
syscall.devctl(1, "sfgc", 14)
print("(no loop devices attached)")
syscall.devctl(1, "sfgc", 1)
return
end
for id, info in pairs(loDevs) do
local colour = info.mode == "image" and 5 or 4
syscall.devctl(1, "sfgc", colour)
printInline(info.mode.." "..id)
syscall.devctl(1, "sfgc", 1)
print(" on "..info.path)
end
return
end
if opts.o and opts.o:lower() == "loop" then
if #args < 2 then
print(name..": -o loop requires <source> and <mountpoint>")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local src = args[1]
local dest = args[2]
if src:sub(1,1) ~= "/" then src = syscall.getcwd().."/"..src end
if dest:sub(1,1) ~= "/" then dest = syscall.getcwd().."/"..dest end
local ok, loopId = pcall(syscall.losetup, src)
if not ok then
local msg = tostring(loopId)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("EINVAL") then msg = "'"..args[1].."': not a directory or .hfs image"
elseif msg:find("EIO") then msg = "'"..args[1].."': I/O error reading image"
end
print(name..": losetup: "..msg); syscall.exit(1); return
end
local ok2, merr = pcall(syscall.mount, dest, loopId)
if not ok2 then
pcall(syscall.lodetach, loopId)
local msg = tostring(merr)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("EBUSY") then msg = "'"..dest.."' is already a mount point"
elseif msg:find("ENODEV") then msg = "loop device not found (internal error)"
end
print(name..": mount: "..msg); syscall.exit(1); return
end
syscall.devctl(1, "sfgc", 10)
print(name..": "..loopId.." mounted at "..dest)
syscall.devctl(1, "sfgc", 1)
return
end
if #args == 2 then
local loopId = args[1]
local dest = args[2]
if dest:sub(1,1) ~= "/" then dest = syscall.getcwd().."/"..dest end
local ok, err = pcall(syscall.mount, dest, loopId)
if not ok then
local msg = tostring(err)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("ENODEV") then msg = "'"..loopId.."': no such device - use losetup first"
elseif msg:find("EBUSY") then msg = "'"..dest.."' is already a mount point"
elseif msg:find("EINVAL") then msg = "invalid arguments"
end
print(name..": "..msg); syscall.exit(1); return
end
syscall.devctl(1, "sfgc", 10)
print(name..": "..loopId.." mounted at "..dest)
syscall.devctl(1, "sfgc", 1)
return
end
print(name..": wrong number of arguments")
print("try '"..name.." --help' for more information.")
syscall.exit(1)

View File

@@ -1,18 +0,0 @@
local userhost = (syscall.getUsername() or "Unknown").."@"..(syscall.getHostname() or "Unknown")
print(".. *. .. | "..userhost)
print(" *= +@* +* | "..string.rep("-",#userhost))
print(" .@#. -@@@= :#@. | OS: "..(syscall.version() or "Unknown"))
print(" =@@+ *@@@# +@@= | Host: "..(syscall.getHost() or "Unknown"))
print(" %@@%: *@@@# -%@@% | Uptime: "..(syscall.getUptime() or "Unknown"))
print(" :@@@@+ *@@@# .*@@@@: | Tasks: "..tostring((#syscall.getTasks() or "Unknown")))
print(" :*@@@%- *@@@# -@@@@*: | Packages: ".."Unknown")
print(" =%@@#. *@@@# .#@@%= | Shell: "..(syscall.getEnviron("SHELL") or "Unknown"))
print(" :=. :*@@= *@@@# =@@+: .=: | ")
print(" %@#=..*# +@@@# #*..=#@# | ")
print(" .@@@@+=# .%@%: #=+@@@@. | ")
print(" .....=# -@= *+...:. | ")
print(" -*%*-@= - =@-*%*- | ")
print(" -@*. -@%. :%@- :*@- | ")
print(" .#@#@* | ")
print(" -#- | ")
print(" | ")

View File

@@ -0,0 +1,82 @@
--:Minify:--
local name = syscall.getTask(syscall.getpid()).name
local cloptions = { n = false, f = false, e = false, help = false }
local args = {}
for _, v in ipairs({ ... }) do
if v:sub(1, 2) == "--" then
local opt = v:sub(3)
if cloptions[opt] == nil then
print(name .. ": unrecognized option '" .. v .. "'")
syscall.exit(1); return
end
cloptions[opt] = true
elseif v:sub(1, 1) == "-" then
for i = 2, #v do
local opt = v:sub(i, i)
if cloptions[opt] == nil then
print(name .. ": invalid option '-" .. opt .. "'")
syscall.exit(1); return
end
cloptions[opt] = true
end
else
table.insert(args, v)
end
end
if cloptions.help then
print("Usage: " .. name .. " [OPTION]... FILE...")
print("Print the resolved target of symbolic links.")
print("")
print("Options:")
print(" -f canonicalize: follow every symlink; last component need not exist")
print(" -e like -f but all components must exist")
print(" -n do not output trailing newline")
print(" --help display this help and exit")
return
end
if #args == 0 then
print(name .. ": missing operand")
print("try '" .. name .. " --help' for more information.")
syscall.exit(1); return
end
local function absPath(p)
if p:sub(1,1) ~= "/" then
local d = syscall.getcwd()
if d:sub(-1) ~= "/" then d = d .. "/" end
p = d .. p
end
return p
end
local anyErr = false
for _, path in ipairs(args) do
path = absPath(path)
if cloptions.f or cloptions.e then
local ok, stat = pcall(syscall.stat, path)
if not ok then
if cloptions.e then
print(name .. ": " .. path .. ": " .. tostring(stat))
anyErr = true
else
if not cloptions.n then print(path) else printInline(path) end
end
else
if not cloptions.n then print(path) else printInline(path) end
end
else
local ok, target = pcall(syscall.readlink, path)
if not ok then
print(name .. ": " .. path .. ": " .. tostring(target))
anyErr = true
else
if not cloptions.n then print(target) else printInline(target) end
end
end
end
if anyErr then syscall.exit(1) end

429
Src/Hyperion-bash/bin/sed Normal file
View File

@@ -0,0 +1,429 @@
--:Minify:--
-- Supports: s/pat/repl/[gip], d, p, q, =, addr1[,addr2]cmd
-- Addressing: line numbers, $, /regex/
-- Flags: -n (silent), -e script, -i (in-place)
local name = syscall.getTask(syscall.getpid()).name
local scripts = {}
local files = {}
local silent = false
local inplace = false
local args = { ... }
local i = 1
while i <= #args do
local a = args[i]
if a == "-n" then
silent = true
elseif a == "-i" then
inplace = true
elseif a == "-e" then
i = i + 1
if not args[i] then
print(name .. ": option -e requires an argument"); syscall.exit(1); return
end
table.insert(scripts, args[i])
elseif a:sub(1,2) == "-e" then
table.insert(scripts, a:sub(3))
elseif a == "--help" then
print("Usage: " .. name .. " [OPTION]... SCRIPT [FILE...]")
print(" " .. name .. " [OPTION]... -e SCRIPT... [FILE...]")
print("Stream editor. Reads FILE(s) (or stdin) line by line,")
print("applies SCRIPT, and writes results to stdout.")
print("")
print("Commands:")
print(" s/REGEX/REPL/[flags] substitute (flags: g global, i ignore-case, p print)")
print(" d delete line (skip to next)")
print(" p print current line")
print(" q quit")
print(" = print current line number")
print(" y/src/dst/ transliterate characters")
print("")
print("Addressing (prefix any command):")
print(" N line number N")
print(" $ last line")
print(" /REGEX/ lines matching regex")
print(" N,M line range")
print(" N,/REGEX/ from line N until regex match")
print("")
print("Options:")
print(" -n suppress default output")
print(" -e SCRIPT add script expression")
print(" -i edit file in-place")
print(" --help display this help and exit")
return
elseif a:sub(1,1) == "-" then
print(name .. ": unknown option: " .. a)
syscall.exit(1); return
else
if #scripts == 0 then
table.insert(scripts, a)
else
table.insert(files, a)
end
end
i = i + 1
end
if #scripts == 0 then
print(name .. ": no script specified"); syscall.exit(1); return
end
local script = table.concat(scripts, "\n")
local function patEscape(s)
return s:gsub("([%(%)%.%%%+%-%*%?%[%^%$])", "%%%1")
end
local function sedPatToLua(pat, icase)
pat = pat:gsub("\\%(", "("):gsub("\\%)", ")")
pat = pat:gsub("\\1", "%%1"):gsub("\\2", "%%2")
return pat
end
local function parseDelim(s, pos, delim)
local out = {}
while pos <= #s do
local c = s:sub(pos, pos)
if c == "\\" and pos < #s then
pos = pos + 1
local nc = s:sub(pos, pos)
if nc == delim then
table.insert(out, delim)
elseif nc == "n" then
table.insert(out, "\n")
else
table.insert(out, "\\" .. nc)
end
elseif c == delim then
return table.concat(out), pos + 1
else
table.insert(out, c)
end
pos = pos + 1
end
return table.concat(out), pos
end
local function parseAddr(s, pos)
local c = s:sub(pos, pos)
if c == "" then return nil, pos end
if c:match("%d") then
local numstr = s:match("^(%d+)", pos)
return { type="line", n=tonumber(numstr) }, pos + #numstr
elseif c == "$" then
return { type="last" }, pos + 1
elseif c == "/" then
local pat, npos = parseDelim(s, pos + 1, "/")
return { type="regex", pat=pat }, npos
end
return nil, pos
end
local function parseCommands(src)
local cmds = {}
local pos = 1
local len = #src
local function skip()
while pos <= len and (src:sub(pos,pos) == " " or src:sub(pos,pos) == "\t") do
pos = pos + 1
end
end
while pos <= len do
skip()
if pos > len then break end
local c = src:sub(pos, pos)
if c == "\n" or c == ";" then
pos = pos + 1
goto continue
end
if c == "#" then
while pos <= len and src:sub(pos,pos) ~= "\n" do pos = pos + 1 end
goto continue
end
local addr1, addr2
addr1, pos = parseAddr(src, pos)
skip()
if addr1 and pos <= len and src:sub(pos,pos) == "," then
pos = pos + 1
skip()
addr2, pos = parseAddr(src, pos)
end
skip()
if pos > len then break end
local cmd = src:sub(pos, pos)
pos = pos + 1
if cmd == "s" then
local delim = src:sub(pos, pos); pos = pos + 1
local pat, p1 = parseDelim(src, pos, delim); pos = p1
local repl, p2 = parseDelim(src, pos, delim); pos = p2
local flags = ""
while pos <= len and src:sub(pos,pos):match("[giIp]") do
flags = flags .. src:sub(pos,pos); pos = pos + 1
end
table.insert(cmds, { addr1=addr1, addr2=addr2, cmd="s",
pat=pat, repl=repl, flags=flags })
elseif cmd == "y" then
local delim = src:sub(pos, pos); pos = pos + 1
local srcch, p1 = parseDelim(src, pos, delim); pos = p1
local dstch, p2 = parseDelim(src, pos, delim); pos = p2
table.insert(cmds, { addr1=addr1, addr2=addr2, cmd="y",
src=srcch, dst=dstch })
elseif cmd == "d" or cmd == "p" or cmd == "q" or cmd == "=" then
table.insert(cmds, { addr1=addr1, addr2=addr2, cmd=cmd })
elseif cmd == "{" then
local depth = 1
local start = pos
while pos <= len and depth > 0 do
local ch = src:sub(pos,pos)
if ch == "{" then depth = depth + 1
elseif ch == "}" then depth = depth - 1 end
pos = pos + 1
end
local inner = src:sub(start, pos - 2)
local innerCmds = parseCommands(inner)
for _, ic in ipairs(innerCmds) do
ic.addr1 = ic.addr1 or addr1
ic.addr2 = ic.addr2 or addr2
end
for _, ic in ipairs(innerCmds) do
table.insert(cmds, ic)
end
elseif cmd == "\n" or cmd == ";" then
else
end
::continue::
end
return cmds
end
local cmds = parseCommands(script)
local inRange = {}
local function addrMatch(cmd, lineNum, line, isLast, ci)
local a1 = cmd.addr1
local a2 = cmd.addr2
if not a1 then return true end
local function matchOne(addr, ln, l)
if addr.type == "line" then return ln == addr.n
elseif addr.type == "last" then return isLast
elseif addr.type == "regex" then return l:find(sedPatToLua(addr.pat)) ~= nil
end
return false
end
if not a2 then
return matchOne(a1, lineNum, line)
end
if inRange[ci] then
local endMatch
if a2.type == "line" then endMatch = (lineNum >= a2.n)
elseif a2.type == "last" then endMatch = isLast
elseif a2.type == "regex" then endMatch = (line:find(sedPatToLua(a2.pat)) ~= nil)
end
if endMatch then inRange[ci] = false end
return true
else
if matchOne(a1, lineNum, line) then
if a2.type == "line" and a2.n <= lineNum then
else
inRange[ci] = true
end
return true
end
return false
end
end
local function doSubst(line, pat, repl, flags)
local global = flags:find("g") ~= nil
local icase = flags:find("[iI]") ~= nil
local luaPat = sedPatToLua(pat, icase)
local function buildRepl(whole, ...)
local caps = { ... }
local out = {}
local rp = repl
local ri = 1
while ri <= #rp do
local rc = rp:sub(ri, ri)
if rc == "&" then
table.insert(out, whole)
elseif rc == "\\" and ri < #rp then
ri = ri + 1
local nc = rp:sub(ri, ri)
if nc:match("%d") then
local idx = tonumber(nc)
table.insert(out, caps[idx] or "")
elseif nc == "n" then
table.insert(out, "\n")
else
table.insert(out, nc)
end
else
table.insert(out, rc)
end
ri = ri + 1
end
return table.concat(out)
end
local result
local changed = false
if global then
result = line:gsub(luaPat, buildRepl)
changed = (result ~= line)
else
local s, e, whole
local parts = { line:find(luaPat) }
if parts[1] then
s = parts[1]; e = parts[2]
local caps = {}
for ci = 3, #parts do caps[#caps+1] = parts[ci] end
local wmatch = line:sub(s, e)
local replStr = buildRepl(wmatch, table.unpack(caps))
result = line:sub(1, s-1) .. replStr .. line:sub(e+1)
changed = true
else
result = line
end
end
return result, changed
end
local function doTranslit(line, src, dst)
local out = {}
for ci = 1, #line do
local c = line:sub(ci, ci)
local idx = src:find(c, 1, true)
if idx and idx <= #dst then
table.insert(out, dst:sub(idx, idx))
else
table.insert(out, c)
end
end
return table.concat(out)
end
local function processLines(lines, outputLines)
local total = #lines
for lineNum, line in ipairs(lines) do
local isLast = (lineNum == total)
local deleted = false
local printed = false
local quit = false
local bare = line:gsub("\n$", "")
for ci, cmd in ipairs(cmds) do
if addrMatch(cmd, lineNum, bare, isLast, ci) then
if cmd.cmd == "d" then
deleted = true; break
elseif cmd.cmd == "p" then
table.insert(outputLines, bare)
elseif cmd.cmd == "=" then
table.insert(outputLines, tostring(lineNum))
elseif cmd.cmd == "q" then
if not silent then table.insert(outputLines, bare) end
quit = true; break
elseif cmd.cmd == "s" then
local newLine, changed = doSubst(bare, cmd.pat, cmd.repl, cmd.flags)
bare = newLine
if changed and cmd.flags:find("p") then
table.insert(outputLines, bare)
end
elseif cmd.cmd == "y" then
bare = doTranslit(bare, cmd.src, cmd.dst)
end
end
end
if quit then break end
if not deleted and not silent then
table.insert(outputLines, bare)
end
end
end
local function readLines(fd)
local lines = {}
local buf = ""
while true do
local chunk = syscall.read(fd, 1024)
if not chunk or chunk == "" then break end
buf = buf .. chunk
end
for line in (buf .. "\n"):gmatch("([^\n]*)\n") do
table.insert(lines, line)
end
if buf ~= "" and buf:sub(-1) ~= "\n" and lines[#lines] == "" then
table.remove(lines)
end
return lines
end
local function runOnFile(path)
local fd
if path then
local ok, err = pcall(function() fd = syscall.open(path, "r") end)
if not ok then
print(name .. ": " .. path .. ": " .. tostring(err))
return false
end
else
fd = 0
end
local lines = readLines(fd)
if path then syscall.close(fd) end
inRange = {}
local outputLines = {}
processLines(lines, outputLines)
if inplace and path then
local wfd = syscall.open(path, "w")
for _, ol in ipairs(outputLines) do
syscall.write(wfd, ol .. "\n")
end
syscall.close(wfd)
else
for _, ol in ipairs(outputLines) do
print(ol)
end
end
return true
end
if #files == 0 then
runOnFile(nil)
else
for _, f in ipairs(files) do
local absf = f
if absf:sub(1,1) ~= "/" then absf = syscall.getcwd() .. "/" .. f end
runOnFile(absf)
end
end

View File

@@ -0,0 +1,151 @@
--:Minify:--
local args = { ... }
local target = args[1] or "http://example.com"
local function pass(msg) syscall.devctl(1,"sfgc",10); print(" PASS " .. msg); syscall.devctl(1,"sfgc",1) end
local function fail(msg) syscall.devctl(1,"sfgc",2); print(" FAIL " .. msg); syscall.devctl(1,"sfgc",1) end
local function info(msg) syscall.devctl(1,"sfgc",14); print(" .... " .. msg); syscall.devctl(1,"sfgc",1) end
local function head(msg) syscall.devctl(1,"sfgc",4); print("\n" .. msg); syscall.devctl(1,"sfgc",1) end
local passed, failed = 0, 0
local function check(name, ok, err)
if ok then passed = passed + 1; pass(name)
else failed = failed + 1; fail(name .. " - " .. tostring(err)) end
end
head("[ 1 ] socket() creation")
do
local ok, fd = pcall(syscall.socket, "inet", "stream")
check("socket(inet, stream) returns fd", ok and type(fd) == "number", fd)
if ok then
local cok = pcall(syscall.close, fd)
check("close() on socket fd", cok, "close failed")
end
local ok2, fd2 = pcall(syscall.socket, "unix", "stream")
check("socket(unix, stream) returns fd", ok2 and type(fd2) == "number", fd2)
if ok2 then pcall(syscall.close, fd2) end
local ok3 = pcall(syscall.socket, "ax25", "stream")
check("socket(ax25) returns EAFNOSUPPORT", not ok3, "should have errored")
end
head("[ 2 ] connect() to " .. target)
local sockfd
do
local ok, fd = pcall(syscall.socket, "inet", "stream")
check("socket() before connect", ok, fd)
if ok then
sockfd = fd
local cok, cerr = pcall(syscall.connect, fd, target)
check("connect(" .. target .. ")", cok, cerr)
end
end
head("[ 3 ] send() HTTP GET via socket")
do
if sockfd then
local sok, serr = pcall(syscall.send, sockfd, "")
check("send() does not error", sok, serr)
else
check("send() skipped (no socket)", false, "socket creation failed")
end
end
head("[ 4 ] recv() reads HTTP response")
do
if sockfd then
info("waiting for response (recv blocks up to 10s)...")
local ok, body = pcall(syscall.recv, sockfd, 65536)
check("recv() returns non-empty body", ok and body and #body > 0,
ok and "empty response" or tostring(body))
if ok and body and #body > 0 then
info("received " .. #body .. " bytes")
local preview = body:sub(1, 120):gsub("\r", ""):gsub("\n", " ")
info("preview: " .. preview)
end
pcall(syscall.close, sockfd)
sockfd = nil
else
check("recv() skipped (no socket)", false, "socket creation failed")
end
end
head("[ 5 ] httpget() convenience wrapper")
do
info("GET " .. target .. " ...")
local ok, body = pcall(syscall.httpget, target)
check("httpget() succeeds", ok, body)
if ok then
check("httpget() returns non-empty string", type(body) == "string" and #body > 0, "empty")
if type(body) == "string" and #body > 0 then
info("received " .. #body .. " bytes")
local preview = body:sub(1, 120):gsub("\r", ""):gsub("\n", " ")
info("preview: " .. preview)
local hasHtml = body:lower():find("<html") ~= nil
or body:lower():find("<!doctype") ~= nil
or body:find("{") ~= nil
or body:find("HTTP") ~= nil
check("body looks like HTTP content", hasHtml, "no recognisable content markers")
end
end
end
head("[ 6 ] UNIX socket loopback IPC")
do
local sockPath = "/tmp/socktest.sock"
pcall(syscall.remove, sockPath)
local sok, sfd = pcall(syscall.socket, "unix", "stream")
check("server socket(unix,stream)", sok, sfd)
if sok then
local bok = pcall(syscall.bind, sfd, sockPath)
check("bind(" .. sockPath .. ")", bok, "bind failed")
local lok = pcall(syscall.listen, sfd, 1)
check("listen()", lok, "listen failed")
local cok, cfd = pcall(syscall.socket, "unix", "stream")
check("client socket(unix,stream)", cok, cfd)
if cok then
local connok = pcall(syscall.connect, cfd, sockPath)
check("client connect(" .. sockPath .. ")", connok, "connect failed")
local aok, afd = pcall(syscall.accept, sfd)
check("accept() returns client fd", aok, afd)
if connok and aok then
local sendok = pcall(syscall.send, cfd, "hello hyperion")
check("send() from client", sendok, "send failed")
local rok, data = pcall(syscall.recv, afd, 1024)
check("recv() on server side", rok and data == "hello hyperion",
rok and ("got: " .. tostring(data)) or tostring(data))
local repok = pcall(syscall.send, afd, "hello back")
check("send() reply from server", repok, "send failed")
local rok2, data2 = pcall(syscall.recv, cfd, 1024)
check("recv() reply on client", rok2 and data2 == "hello back",
rok2 and ("got: " .. tostring(data2)) or tostring(data2))
pcall(syscall.close, afd)
end
pcall(syscall.close, cfd)
end
pcall(syscall.close, sfd)
pcall(syscall.remove, sockPath)
end
end
print("")
syscall.devctl(1,"sfgc", failed == 0 and 10 or 2)
print(string.format("Results: %d passed, %d failed", passed, failed))
syscall.devctl(1,"sfgc",1)
if failed > 0 then syscall.exit(1) end

View File

@@ -1 +0,0 @@
syscall.chown("/bin", 0, 0)

View File

@@ -1,21 +1,15 @@
--:Minify:--
local fs = require("sys.fs")
local targetUser = ({ ... })[1] or "root"
local currentUid = syscall.getuid()
local currentUser = syscall.getUsername(currentUid) or tostring(currentUid)
local targetUid = syscall.getuidbyname(targetUser)
local targetUid = syscall.getuid(targetUser)
if not targetUid then
print("su: user '" .. targetUser .. "' does not exist")
syscall.exit(1)
return
end
local ok, err
if currentUid == 0 then
ok = true
else
if currentUid ~= 0 then
printInline("Password: ")
local pw = ""
while true do
@@ -25,16 +19,13 @@ else
syscall.write(1, "\n")
break
elseif ch == "\b" then
if #pw > 0 then
pw = pw:sub(1, -2); syscall.write(1, "\b \b")
end
if #pw > 0 then pw = pw:sub(1, -2); syscall.write(1, "\b \b") end
else
pw = pw .. ch
syscall.write(1, "*")
pw = pw .. ch; syscall.write(1, "*")
end
end
ok, err = syscall.elevate(targetUser, pw)
local ok, err = syscall.elevate(targetUser, pw)
if not ok then
sleep(1)
print("su: Authentication failure")
@@ -43,38 +34,23 @@ else
end
end
if currentUid == 0 then
syscall.setuid(targetUid)
end
syscall.setuid(targetUid)
local pwent = syscall.getpasswd(targetUid)
local shell = (pwent and pwent.shell) or "/bin/hysh"
local shell = (pwent and pwent.shell) or "/bin/hysh"
local homedir = (pwent and pwent.homedir) or "/"
syscall.chdir(homedir)
syscall.setEnviron("HOME", homedir)
syscall.setEnviron("USER", targetUser)
local ok_cd, err_cd = pcall(syscall.chdir, homedir)
if not ok_cd then
homedir = "/"
syscall.chdir(homedir)
end
syscall.setEnviron("HOME", homedir)
syscall.setEnviron("USER", targetUser)
syscall.setEnviron("SHELL", shell)
local shellText = fs.readAllText(shell)
if not shellText then
print("su: shell not found: " .. shell)
local ok, err = pcall(syscall.exec, shell)
if not ok then
print("su: cannot exec shell '" .. shell .. "': " .. tostring(err))
syscall.exit(1)
return
end
local shellFn, loadErr = load(shellText, "@" .. shell)
if not shellFn then
print("su: cannot load shell: " .. tostring(loadErr))
syscall.exit(1)
return
end
local success, err = syscall.kill(syscall.getppid())
if success then
syscall.spawn(shellFn, targetUser .. ":" .. shell, syscall.getEnviron())
syscall.exit(0)
else
print("su: "..err)
end

View File

@@ -0,0 +1,111 @@
--:Minify:--
-- Usage:
-- umount <mountpoint> unmount; auto-detach loop device if one is found
-- umount -l <id> detach loop device without unmounting (force)
-- umount --no-detach <mpt> unmount but leave loop device attached
-- umount --help
local name = syscall.getTask(syscall.getpid()).name
local args, opts = {}, { l=false, ["no-detach"]=false, help=false }
for _, v in ipairs({...}) do
if v:sub(1,2) == "--" then
local o = v:sub(3)
if opts[o] ~= nil then opts[o] = true
else print(name..": unrecognised option '"..v.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
elseif v:sub(1,1) == "-" then
for i = 2, #v do
local c = v:sub(i,i)
if opts[c] ~= nil then opts[c] = true
else print(name..": invalid option '-"..c.."'")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return end
end
else
table.insert(args, v)
end
end
if opts.help then
print("Usage: "..name.." <mountpoint>")
print(" "..name.." --no-detach <mountpoint>")
print(" "..name.." -l <loopid>")
print("")
print("Unmount a filesystem mounted at <mountpoint>.")
print("")
print(" <mountpoint> unmount and auto-detach any loop device")
print(" --no-detach unmount but keep the loop device attached")
print(" -l <loopid> forcibly detach a loop device (no unmount)")
print("")
print("Requires root.")
return
end
if opts.l then
if #args < 1 then
print(name..": -l requires a loop device id")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local id = args[1]
local ok, err = pcall(syscall.lodetach, id)
if not ok then
local msg = tostring(err)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("ENXIO") then msg = "no such loop device '"..id.."'"
elseif msg:find("EBUSY") then msg = "'"..id.."' is still mounted - unmount first or omit -l"
end
print(name..": "..msg); syscall.exit(1); return
end
syscall.devctl(1, "sfgc", 10)
print(name..": detached "..id)
syscall.devctl(1, "sfgc", 1)
return
end
if #args < 1 then
print(name..": missing mount point operand")
print("try '"..name.." --help' for more information.")
syscall.exit(1); return
end
local mpt = args[1]
if mpt:sub(1,1) ~= "/" then mpt = syscall.getcwd().."/"..mpt end
local loopIdToDetach = nil
if not opts["no-detach"] then
local lok, devs = pcall(syscall.lolist)
if lok then
loopIdToDetach = {}
for id in pairs(devs) do
loopIdToDetach[#loopIdToDetach + 1] = id
end
end
end
local ok, err = pcall(syscall.umount, mpt)
if not ok then
local msg = tostring(err)
if msg:find("EPERM") then msg = "Permission denied"
elseif msg:find("EINVAL") then msg = "'"..args[1].."' is not a mount point"
elseif msg:find("EBUSY") then msg = "'"..args[1].."' is busy - close open files first"
end
print(name..": "..msg); syscall.exit(1); return
end
syscall.devctl(1, "sfgc", 10)
print(name..": unmounted "..mpt)
syscall.devctl(1, "sfgc", 1)
if loopIdToDetach then
for _, id in ipairs(loopIdToDetach) do
local dok = pcall(syscall.lodetach, id)
if dok then
syscall.devctl(1, "sfgc", 14)
print(name..": auto-detached "..id)
syscall.devctl(1, "sfgc", 1)
end
end
end