From ff321804eeb092fe672c1a56cab0525504bbfe98 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Wed, 6 Nov 2019 14:28:40 +1100 Subject: [PATCH] moved cd out of the shell and into the os library as os.chdir --- exec/init.lua | 4 ++-- exec/shell.lua | 20 +------------------- module/init.lua | 1 + module/osutil.lua | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 module/osutil.lua diff --git a/exec/init.lua b/exec/init.lua index e8701f4..ffd4781 100644 --- a/exec/init.lua +++ b/exec/init.lua @@ -1,12 +1,12 @@ xpcall(function() +os.setenv("PWD","/boot") os.spawnfile("/boot/service/getty.lua") coroutine.yield() for k,v in pairs(fs.list("/dev/")) do if v:sub(1,3) == "tty" then dprint(tostring(io.input("/dev/"..v))) dprint(tostring(io.output("/dev/"..v))) - io.write("PsychOS v2.0a1 - ") - print(tostring(math.floor(computer.totalMemory()/1024)).."K RAM") + io.write(_OSVERSION.." - "..tostring(math.floor(computer.totalMemory()/1024)).."K RAM") os.spawnfile("/boot/exec/shell.lua") end end diff --git a/exec/shell.lua b/exec/shell.lua index a2c827c..4b3d0c8 100644 --- a/exec/shell.lua +++ b/exec/shell.lua @@ -3,25 +3,7 @@ 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 +shenv.cd = os.chdir setmetatable(shenv,{__index=function(_,k) if _G[k] then return _G[k] diff --git a/module/init.lua b/module/init.lua index 0ea6a09..829324b 100644 --- a/module/init.lua +++ b/module/init.lua @@ -1,6 +1,7 @@ --#include "module/chatbox-dprint.lua" --#include "module/syslog.lua" --#include "module/sched.lua" +--#include "module/osutil.lua" --#include "module/fs.lua" --#include "module/newio.lua" --#include "module/devfs.lua" diff --git a/module/osutil.lua b/module/osutil.lua new file mode 100644 index 0000000..aa3bdf9 --- /dev/null +++ b/module/osutil.lua @@ -0,0 +1,18 @@ +function os.chdir(p) -- changes the current working directory of the calling process to the directory specified in *p*, returning true or false, error + if not (p:sub(1,1) == "/") then + 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 + p = "/"..table.concat(np,"/") + end + if fs.exists(p) and fs.list(p) then + os.setenv("PWD",p) + else + return false, "no such directory" + end +end