dummix dumbshell: fixed erratic 'cd' behaviours

Former-commit-id: 82698355d2566d579d2461bc1f1b7a4799f60163
Former-commit-id: 033a2a4f690b8e968e7626c46b998e3bba91031a
This commit is contained in:
Song Minjae
2016-10-04 22:43:58 +09:00
parent 862034b6c1
commit a757b12cab
5 changed files with 49 additions and 22 deletions

View File

@@ -21,8 +21,8 @@ class StateVTTest : BasicGameState() {
// HiRes: 100x64, LoRes: 80x25
val computerInside = BaseTerrarumComputer(8)
val vt = SimpleTextTerminal(SimpleTextTerminal.AMETHYST_NOVELTY, 100, 64,
computerInside, colour = true, hires = true)
val vt = SimpleTextTerminal(SimpleTextTerminal.AMETHYST_NOVELTY, 80, 25,
computerInside, colour = true, hires = false)
val vtUI = Image(vt.displayW, vt.displayH)

View File

@@ -40,6 +40,10 @@ local function printErr(msg)
print(DLE..msg)
end
local function shallowCopy(t)
return {table.unpack(t)}
end
-- BUILTINS -------------------------------------------------------------------
local function cd(tArgs)
@@ -47,14 +51,7 @@ local function cd(tArgs)
if (dir == nil or #dir < 1) then return end
-- check if the directory exists
local chkdir = expandPath(dir)
if not fs.exists(chkdir) then
os.errorNoSuchFileOrDir("cd: "..dir)
return
end
local oldWorkingDir = shallowCopy(os.workingDir)
-- parse dir by delimeter '/'
if (dir:byte(1) == 47) then -- if dir begins with '/'
@@ -69,6 +66,8 @@ local function cd(tArgs)
if (word == "..") then
if (#os.workingDir > 1) then
os.workingDir[#os.workingDir] = nil -- pops an element to oblivion
else
-- pass
end
elseif (word == ".") then
-- pass
@@ -76,6 +75,14 @@ local function cd(tArgs)
table.insert(os.workingDir, word)
end
end
-- check if the directory exists
if not fs.isDir(os.fullWorkPath()) then
os.errorNoSuchFileOrDir("cd: "..dir)
os.workingDir = oldWorkingDir
return
end
end
local function exit(tArgs)
@@ -98,10 +105,10 @@ local function exec(tArgs)
-- do some sophisticated file-matching
-- step 1: exact file
if fs.exists(fullFilePath) and fs.isFile(fullFilePath) then
if fs.isFile(fullFilePath) then
shell.run(fullFilePath, execArgs)
-- step 2: try appending ".lua"
elseif fs.exists(fullFilePath..".lua") and fs.isFile(fullFilePath..".lua") then
elseif fs.isFile(fullFilePath..".lua") then
shell.run(fullFilePath..".lua", execArgs)
-- step 3: parse os.path (just like $PATH)
-- step 3.1: exact file; step 3.2: append ".lua"
@@ -112,11 +119,11 @@ local function exec(tArgs)
debug(path..filePath)
if fs.exists(path..filePath) and fs.isFile(path..filePath) then
if fs.isFile(path..filePath) then
execByPathArg = path..filePath
execByPathFileExists = true
break
elseif fs.exists(path..filePath..".lua") and fs.isFile(path..filePath..".lua") then
elseif fs.isFile(path..filePath..".lua") then
execByPathArg = path..filePath..".lua"
execByPathFileExists = true
break
@@ -196,7 +203,10 @@ end
exitshell = false
-- load up aliases
if fs.exists("/etc/.dshrc") then fs.dofile("/etc/.dshrc") end
if fs.isFile("/etc/.dshrc") then
fs.dofile("/etc/.dshrc")
machine.println("[dummix/dsh.lua] Dsh aliases successfully loaded.")
end
-- END OF INIT SHELL ----------------------------------------------------------

View File

@@ -25,7 +25,9 @@ os.version = "0.0"
os.EXIT_SUCCESS = 0
os.workingDir = {"", "home"} -- index 1 must be ""!
os.path = "home/bin/;/usr/bin/;/bin/" -- infamous $path
os.fullWorkPath = function()
return table.concat(os.workingDir, "/")
end
-- @param "path/of/arbitrary"
-- @return /working/dir/path/of/arbitrary
-- input path's trailing '/' is PRESERVED.
@@ -35,10 +37,7 @@ os.expandPath = function(p)
return p
end
return table.concat(os.workingDir, "/").."/"..p
end
os.fullWorkPath = function()
return table.concat(os.workingDir, "/")
return os.fullWorkPath().."/"..p
end
os.defaultshell = "/bin/dsh.lua"
os.clock = function() return machine.milliTime() / 1000 end -- uptime of the computer, in seconds

View File

@@ -9,6 +9,7 @@ import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
import net.torvald.terrarum.virtualcomputer.luaapi.Term.Companion.checkIBM437
import java.io.*
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths
import java.util.*
@@ -145,6 +146,7 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
}
}
/** Don't use this. Use isFile */
class FileExists(val computer: BaseTerrarumComputer) : OneArgFunction() {
override fun call(path: LuaValue) : LuaValue {
Filesystem.ensurePathSanity(path)
@@ -165,7 +167,23 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
override fun call(path: LuaValue) : LuaValue {
Filesystem.ensurePathSanity(path)
return LuaValue.valueOf(!Files.isDirectory(Paths.get(computer.getRealPath(path)).toAbsolutePath()))
// check if the path is file by checking:
// 1. isfile
// 2. canwrite
// 3. length
// Why? Our Java simply wants to fuck you.
val path = Paths.get(computer.getRealPath(path)).toAbsolutePath()
var result = false
result = Files.isRegularFile(path)
if (!result) result = Files.isWritable(path)
if (!result)
try { result = Files.size(path) > 0 }
catch (e: NoSuchFileException) { result = false }
return LuaValue.valueOf(result)
}
}

View File

@@ -10,7 +10,7 @@ The path for the argument of functions blocks `\,.\,.\,' to be passed, preventin
\endhead
fs.list(\textbf{path}: string) & table & Returns list of files in \textbf{path}, in lua table.
\\ \\
fs.exists(\textbf{path}: string) & bool & Checks if \textbf{path} exists on the filesystem.
fs.exists(\textbf{path}: string) & bool & Checks if \textbf{path} exists on the filesystem. NOTE: avoid using it as much as you can; it's somewhat erratic.
\\ \\
fs.isDir(\textbf{path}: string) & bool & Checks if \textbf{path} is a directory.
\\ \\