diff --git a/clite/commands.js b/clite/commands.js index 1c74318..7ecc930 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -64,8 +64,6 @@ Options: io.error('unknown argument: -'+args[i][j]); } } - }else if (args[i][0] == '/') { - dirs.push(args[i]); }else{ dirs.push(clite.lib.resolvePath(args[i],false)); } @@ -128,8 +126,6 @@ Options: io.error('unknown argument: -'+args[i][j]); } } - }else if (args[i][0] == '/') { - files.push(args[i]); }else{ files.push(clite.lib.resolvePath(args[i],false)); } diff --git a/clite/core.js b/clite/core.js index 38eb0a2..dbd5230 100644 --- a/clite/core.js +++ b/clite/core.js @@ -992,16 +992,32 @@ clite.lib = { }, // returns an absolute path: ('foo','/etc') -> '/etc/foo': ('foo','bar') -> $PWD/bar/foo resolvePath:function(txt,base) { - if (txt[0] == '/') + if (txt == '/') return txt; if (txt[0] == '~') - return clite.shell.env.HOME+txt.substring(1); - if (typeof base != 'string') - base = clite.shell.env.PWD; - if (base[0] != '/') - base = clite.shell.env.PWD+'/'+base; - // TODO: support . and .. - return base+'/'+txt; + txt = clite.shell.env.HOME+txt.substring(1); + var path = txt; + if (txt[0] != '/') { + if (typeof base != 'string') + base = clite.shell.env.PWD; + if (base[0] != '/') + base = clite.shell.env.PWD+'/'+base; + path = base+'/'+txt; + } + var parts = path.split('/'); + path = ''; + var i; + while ((i = parts.indexOf('.')) != -1) { + parts.splice(i,1); + } + while ((i = parts.indexOf('')) != -1) { + parts.splice(i,1); + } + while ((i = parts.indexOf('..')) != -1) { + parts.splice(i-1,2); + } + path = '/'+parts.join('/'); + return path; }, // convert a string into an argument array: 'ls /etc' -> ['ls',/etc] strToArgs:function(txt) {