cd into relative path

This commit is contained in:
minjaesong
2020-11-07 22:12:35 +09:00
parent f01345fb6f
commit d2416e7dbc
2 changed files with 21 additions and 14 deletions

View File

@@ -140,20 +140,20 @@ shell.coreutils = {
// replace slashes into revslashes
let pathstr = args[1].replaceAll('/','\\\\');
let startsWithSlash = args[1].startsWith('\\');
// split them into an array while filtering empty elements except for the root 'head'
let newPwd = [""].concat(pathstr.split("\\").filter(function(it) { return (it.length > 0); }));
let newPwd = (startsWithSlash ? [""] : shell_pwd).concat(pathstr.split("\\").filter(function(it) { return (it.length > 0); }));
// construct new pathstr from pwd arr so it will be sanitised
pathstr = newPwd.join('\\').substring(1);
if (DEBUG_PRINT) serial.println("command.js > pathstr = "+pathstr);
// check if path is valid
let dirOpened = filesystem.open(CURRENT_DRIVE, pathstr, 'R');
if (!dirOpened) { printerrln("CHDIR failed for '"+pathstr+"'"); return; }
// check if path is directory
let isDir = filesystem.isDirectory(CURRENT_DRIVE);
if (!isDir) { printerrln("CHDIR failed for '"+pathstr+"'"); return; }
filesystem.open(CURRENT_DRIVE, pathstr, 'R');
let dirOpened = filesystem.isDirectory(CURRENT_DRIVE); // open a dir; if path is nonexistent, file won't actually be opened
if (!dirOpened) { printerrln("CHDIR failed for '"+pathstr+"'"); return; } // if file is not opened, FALSE will be returned
shell_pwd = newPwd;
},
@@ -208,8 +208,15 @@ shell.coreutils = {
}
},
dir: function(args) {
let path = (args[1] !== undefined) ? args[1] : "\\"+shell_pwd.join("\\");
throw Error("TODO");
let pathstr = (args[1] !== undefined) ? args[1] : "\\"+shell_pwd.join("\\");
// check if path is valid
let pathOpened = filesystem.open(CURRENT_DRIVE, pathstr, 'R');
if (!pathOpened) { printerrln("CHDIR failed for '"+pathstr+"'"); return; }
let port = filesystem._toPorts(CURRENT_DRIVE)[0]
com.sendMessage(port, "LIST");
println(com.pullMessage(port));
}
};
Object.freeze(shell.coreutils);

View File

@@ -355,10 +355,9 @@ class TestDiskDrive(private val driveNum: Int, theRootPath: File? = null) : Bloc
sb.append('\n')
}
sb.append('\n')
}
return sb.toString()
return if (sb.last() == '\n') sb.substring(0, sb.lastIndex) else sb.toString()
}
private fun sanitisePath(s: String) = s.replace('\\','/').replace(Regex("""\?<>:\*\|"""),"-")
@@ -371,9 +370,10 @@ class TestDiskDrive(private val driveNum: Int, theRootPath: File? = null) : Bloc
val paths = path.split('/')
val newPaths = ArrayList<String>()
paths.forEach {
if (it.isBlank() || it.isEmpty()) throw IllegalArgumentException("Path cannot contain whitespaces: $paths")
if (it == "..") {
if (it.isBlank() || it.isEmpty()) {
/*do nothing*/
}
else if (it == "..") {
parentCount -= -1
}
else if (it != ".") {