add . and .. support to lib.resolvePath, fix bugs in commands not using lib.resolvePath

This commit is contained in:
Lisa Milne 2023-11-08 18:02:55 +10:00
parent 53fedffe87
commit 40efd18938
2 changed files with 24 additions and 12 deletions

View file

@ -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));
}

View file

@ -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) {