diff options
Diffstat (limited to 'src/syslua/lx')
-rw-r--r-- | src/syslua/lx/lxinit.lua | 48 | ||||
-rw-r--r-- | src/syslua/lx/shell.lua | 88 | ||||
-rw-r--r-- | src/syslua/lx/sysdef.lua | 36 |
3 files changed, 157 insertions, 15 deletions
diff --git a/src/syslua/lx/lxinit.lua b/src/syslua/lx/lxinit.lua index 6b23aa8..1c8d116 100644 --- a/src/syslua/lx/lxinit.lua +++ b/src/syslua/lx/lxinit.lua @@ -3,20 +3,38 @@ print "Lua eXtended helpers for Kogata v1" do local old_tostring = tostring function tostring(x) - if type(x) == "table" then - if next(x) == nil then - return '{}' - end - local q = '{\n ' - for k, v in pairs(x) do - if q:len() > 4 then - q = q .. ',\n ' - end - q = q .. k .. ': ' .. tostring(v):gsub('\n', '\n ') - end - return q .. '\n}' - else - return old_tostring(x) - end + local seen = {} + function aux(x) + if type(x) == "table" then + if next(x) == nil then + return '{}' + end + + if seen[x] then + return '...' + end + seen[x] = true + + local q = '{\n ' + for k, v in pairs(x) do + if q:len() > 4 then + q = q .. ',\n ' + end + q = q .. k .. ': ' .. aux(v):gsub('\n', '\n ') + end + return q .. '\n}' + else + return old_tostring(x) + end + end + return aux(x) end end + + +function string.split(str, sep) + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]*)", sep) + str:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields +end diff --git a/src/syslua/lx/shell.lua b/src/syslua/lx/shell.lua new file mode 100644 index 0000000..62f210f --- /dev/null +++ b/src/syslua/lx/shell.lua @@ -0,0 +1,88 @@ +local sys = require 'lx.sys' +local sysdef = require 'lx.sysdef' + +local _cwd = 'root:/' + +function pathcat(path1, path2) + assert(path1, "invalid argument") + if not path2 then + return path1 + end + + function explode_path(path) + local _, _, dr, p = string.find(path, '^(%w+):(.*)$') + if not dr or not p then + dr, p = nil, path + end + local pp = string.split(p, '/') + if #pp > 1 and pp[#pp] == '' then + table.remove(pp) + end + return dr, pp + end + + function implode_path(dr, p) + assert(p[1] == '', 'bad first path component') + return dr .. ':' .. table.concat(p, '/') + end + + local dr2, p2 = explode_path(path2) + if dr2 then + return implode_path(dr2, p2) + else + local dr, p1 = explode_path(path1) + assert(p1[1] == '', 'bad path1!') + local p = p1 + if p2[1] == '' then + p = {} + end + for _, v in pairs(p2) do + if v == '..' then + table.remove(p) + elseif v ~= '.' then + table.insert(p, v) + end + end + return implode_path(dr, p) + end +end + +function cd(path) + -- TODO path simplification etc + local newcwd = pathcat(_cwd, path) + local s = sys.stat(newcwd) + if not s then + print("not found: " .. newcwd) + elseif s.type & sysdef.FT_DIR == 0 then + print("not a directory: " .. newcwd) + else + _cwd = newcwd + print(_cwd) + end +end + +function cwd() + return _cwd +end + +function ls(path) + path = pathcat(_cwd, path) + + local fd = sys.open(path, sysdef.FM_READDIR) + if not fd then + print("Could not open " .. path) + else + local i = 0 + while true do + x = sys.readdir(fd, i) + if not x then break end + if x.type & sysdef.FT_DIR ~= 0 then + x.name = x.name .. '/' + end + print(x.type, x.access, x.size, x.name) + i = i + 1 + end + sys.close(fd) + end +end + diff --git a/src/syslua/lx/sysdef.lua b/src/syslua/lx/sysdef.lua new file mode 100644 index 0000000..7c0d14f --- /dev/null +++ b/src/syslua/lx/sysdef.lua @@ -0,0 +1,36 @@ +-- Constant definitions based on common/include/proto/fs.h + +return { + -- FILE TYPES + FT_REGULAR = 0, + FT_DIR = 0x01, + FT_DEV = 0x02, + FT_BLOCKDEV = 0x04, + FT_CHARDEV = 0x08, + FT_CHANNEL = 0x10, + FT_FRAMEBUFFER = 0x20, + + -- FILE MODES + FM_READ = 0x01, + FM_WRITE = 0x02, + FM_READDIR = 0x04, + FM_MMAP = 0x08, + FM_CREATE = 0x10, + FM_TRUNC = 0x20, + FM_APPEND = 0x40, + FM_IOCTL = 0x100, + FM_BLOCKING = 0x200, + FM_DCREATE = 0x1000, + FM_DMOVE = 0x2000, + FM_DDELETE = 0x4000, + FM_ALL_MODES = 0xFFFF, + + -- IOCTL calls + IOCTL_BLOCKDEV_GET_BLOCK_SIZE = 40, + IOCTL_BLOCKDEV_GET_BLOCK_COUNT = 41, + + -- Modes for select call + SEL_READ = 0x01, + SEL_WRITE = 0x02, + SEL_ERROR = 0x04, +} |