From d2416e7dbc477ca91fe334664a1d716c6e1617c8 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 7 Nov 2020 22:12:35 +0900 Subject: [PATCH] cd into relative path --- assets/tvdos/bin/command.js | 25 ++++++++++++------- .../torvald/tsvm/peripheral/TestDiskDrive.kt | 10 ++++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/assets/tvdos/bin/command.js b/assets/tvdos/bin/command.js index a4abd80..4cf4efb 100644 --- a/assets/tvdos/bin/command.js +++ b/assets/tvdos/bin/command.js @@ -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); diff --git a/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt b/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt index 5a31a16..87229ad 100644 --- a/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt +++ b/src/net/torvald/tsvm/peripheral/TestDiskDrive.kt @@ -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() 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 != ".") {