From 12df3de7dfcb365e3f8c28bc83f03adcff5bfc40 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Sun, 28 Jul 2019 13:03:04 +1000 Subject: [PATCH] added a cd() builtin to the shell, for changing working directory --- exec/shell.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/exec/shell.lua b/exec/shell.lua index a0facd1..e70eca4 100644 --- a/exec/shell.lua +++ b/exec/shell.lua @@ -3,6 +3,25 @@ local shenv = {} function shenv.quit() os.setenv("run",nil) end +function shenv.cd(p) + if p:sub(1,1) == "/" then + if fs.list(p) then + os.setenv("PWD",p) + else + print("no such directory: "..p) + end + else + local np = {} + for k,v in pairs(fs.segments(os.getenv("PWD").."/"..p)) do + if v == ".." then + np[#np] = nil + else + np[#np+1] = v + end + end + os.setenv("PWD","/"..table.concat(np,"/")) + end +end setmetatable(shenv,{__index=function(_,k) if _G[k] then return _G[k]