]> git.decadent.org.uk Git - ion3.git/blob - mod_query/mod_query_chdir.lua
c3eeb6ac9336d64e8b5e7aec93a8814dacfe66fc
[ion3.git] / mod_query / mod_query_chdir.lua
1 --
2 -- ion/query/mod_query_chdir.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2008.
5 -- 
6 -- See the included file LICENSE for details.
7 --
8
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
17
18 local function relative_path(path)
19     return not string.find(path, "^/")
20 end
21
22 local function empty_path(path)
23     return (not path or path=="")
24 end
25
26 local function ws_chdir(mplex, params)
27     local nwd=params[1]
28     
29     ws=assert(ioncore.find_manager(mplex, "WGroupWS"))
30     
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
45
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
53
54 mod_query.defcmd("cd", ws_chdir)
55 mod_query.defcmd("pwd", ws_showdir)