aboutsummaryrefslogtreecommitdiff
path: root/src/syslua/lx/shell.lua
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2017-04-19 15:34:04 +0200
committerAlex Auvolat <alex@adnab.me>2017-04-19 15:34:04 +0200
commite53a39d9ec28b24ea0d408f1500e987d005cd651 (patch)
tree639c93f2a17aabee7759cd16645b4d7da693ae4c /src/syslua/lx/shell.lua
parentd4a89538d381bb62b4c7c864b09d3d8274cf0bdb (diff)
downloadkogata-e53a39d9ec28b24ea0d408f1500e987d005cd651.tar.gz
kogata-e53a39d9ec28b24ea0d408f1500e987d005cd651.zip
Lua shell :)
Diffstat (limited to 'src/syslua/lx/shell.lua')
-rw-r--r--src/syslua/lx/shell.lua88
1 files changed, 88 insertions, 0 deletions
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
+