Released version 3-2014010900
[notion/jeffpc.git] / mod_query / mod_query_chdir.lua
blob1207aa79b18884a441c13a0f7403220b12843371
1 --
2 -- ion/query/mod_query_chdir.lua
3 --
4 -- Copyright (c) Tuomo Valkonen 2004-2009.
5 --
6 -- See the included file LICENSE for details.
7 --
9 local function simplify_path(path)
10 local npath=string.gsub(path, "([^/]+)/+%.%./+", "")
11 if npath~=path then
12 return simplify_path(npath)
13 else
14 return string.gsub(string.gsub(path, "([^/]+)/+%.%.$", ""), "/+", "/")
15 end
16 end
18 local function relative_path(path)
19 return not string.find(path, "^/")
20 end
22 local function empty_path(path)
23 return (not path or path=="")
24 end
26 local function ws_chdir(mplex, params)
27 local nwd=params[1]
29 ws=assert(ioncore.find_manager(mplex, "WGroupWS"))
31 if not empty_path(nwd) and relative_path(nwd) then
32 local owd=ioncore.get_dir_for(ws)
33 if empty_path(owd) then
34 owd=os.getenv("PWD")
35 end
36 if owd then
37 nwd=owd.."/"..nwd
38 end
39 end
40 local ok, err=ioncore.chdir_for(ws, nwd and simplify_path(nwd))
41 if not ok then
42 mod_query.warn(mplex, err)
43 end
44 end
46 local function ws_showdir(mplex, params)
47 local dir=ioncore.get_dir_for(mplex)
48 if empty_path(dir) then
49 dir=os.getenv("PWD")
50 end
51 mod_query.message(mplex, dir or "(?)")
52 end
54 mod_query.defcmd("cd", ws_chdir)
55 mod_query.defcmd("pwd", ws_showdir)