X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=mod_query%2Fmod_query_chdir.lua;h=1207aa79b18884a441c13a0f7403220b12843371;hb=HEAD;hp=dcf4d2a7e5120faaafbde08b3b83607e0752014c;hpb=d2a43a53786878c1273313249d3b49f6cd559b00;p=ion3.git diff --git a/mod_query/mod_query_chdir.lua b/mod_query/mod_query_chdir.lua index dcf4d2a..1207aa7 100644 --- a/mod_query/mod_query_chdir.lua +++ b/mod_query/mod_query_chdir.lua @@ -1,22 +1,54 @@ -- -- ion/query/mod_query_chdir.lua -- --- Copyright (c) Tuomo Valkonen 2004-2007. +-- Copyright (c) Tuomo Valkonen 2004-2009. -- -- See the included file LICENSE for details. -- +local function simplify_path(path) + local npath=string.gsub(path, "([^/]+)/+%.%./+", "") + if npath~=path then + return simplify_path(npath) + else + return string.gsub(string.gsub(path, "([^/]+)/+%.%.$", ""), "/+", "/") + end +end + +local function relative_path(path) + return not string.find(path, "^/") +end + +local function empty_path(path) + return (not path or path=="") +end + local function ws_chdir(mplex, params) + local nwd=params[1] + ws=assert(ioncore.find_manager(mplex, "WGroupWS")) - local ok, err=ioncore.chdir_for(ws, params[1] or "") + + if not empty_path(nwd) and relative_path(nwd) then + local owd=ioncore.get_dir_for(ws) + if empty_path(owd) then + owd=os.getenv("PWD") + end + if owd then + nwd=owd.."/"..nwd + end + end + local ok, err=ioncore.chdir_for(ws, nwd and simplify_path(nwd)) if not ok then mod_query.warn(mplex, err) end end local function ws_showdir(mplex, params) - local dir=assert(ioncore.get_dir_for(mplex) or os.getenv("PWD")) - mod_query.message(mplex, dir) + local dir=ioncore.get_dir_for(mplex) + if empty_path(dir) then + dir=os.getenv("PWD") + end + mod_query.message(mplex, dir or "(?)") end mod_query.defcmd("cd", ws_chdir)