diff --git a/argentum.cfg b/argentum.cfg index 2ad87c4..51b863d 100644 --- a/argentum.cfg +++ b/argentum.cfg @@ -1,7 +1,7 @@ local agcfg = { ["halyde"] = { ["maindir"] = "", - ["version"] = "1.3.1", + ["version"] = "1.4.0", ["description"] = "A universal, customizable and feature-packed operating system for OpenComputers.", ["directories"] = { "halyde/apps", diff --git a/argentum/store/halyde/package.cfg b/argentum/store/halyde/package.cfg index 1adeb39..e39a67d 100644 --- a/argentum/store/halyde/package.cfg +++ b/argentum/store/halyde/package.cfg @@ -4,7 +4,7 @@ Ahalyde/core/ Ahalyde/config/ Ahalyde/apps/helpdb/ Ahalyde/apps/ -V1.3.0 +V1.4.0 Ainit.lua Ahalyde/apps/helpdb/cat.txt Ahalyde/apps/helpdb/cd.txt @@ -43,4 +43,4 @@ Ahalyde/core/termlib.lua Ahalyde/lib/component.lua Ahalyde/lib/event.lua Ahalyde/lib/filesystem.lua -Ahalyde/lib/raster.lua \ No newline at end of file +Ahalyde/lib/raster.lua diff --git a/halyde/apps/cat.lua b/halyde/apps/cat.lua index a1884fc..d59f28b 100644 --- a/halyde/apps/cat.lua +++ b/halyde/apps/cat.lua @@ -1,20 +1,20 @@ -local args = {...} -local file = args[1] -args = nil +local files = {...} local fs = import("filesystem") -if not file then +if not files or not files[1] then shell.run("help cat") return end -if file:sub(1, 1) ~= "/" then - file = shell.workingDirectory .. file +for _, file in ipairs(files) do + if file:sub(1, 1) ~= "/" then + file = fs.concat(shell.workingDirectory, file) + end + if not fs.exists(file) then + print("\27[91mFile does not exist.") + end + local handle = fs.open(file, "r") + local data + repeat + data = handle:read(math.huge or math.maxinteger) + print(data, false) + until not data end -if not fs.exists(file) then - print("\27[91mFile does not exist.") -end -local handle = fs.open(file, "r") -local data -repeat - data = handle:read(math.huge or math.maxinteger) - print(data, false) -until not data diff --git a/halyde/apps/cd.lua b/halyde/apps/cd.lua index b91f0d7..72aee9e 100644 --- a/halyde/apps/cd.lua +++ b/halyde/apps/cd.lua @@ -1,29 +1,14 @@ -local args = {...} -local directory = args[1] -args = nil +local directory = ... local fs = import("filesystem") if not directory then return end -if directory == ".." then - local backDirectory = shell.workingDirectory:match("(.+)/.-/") - if backDirectory then - backDirectory = backDirectory .. "/" - else - backDirectory = "/" - end - shell.workingDirectory = backDirectory +if directory:sub(1, 1) ~= "/" then + directory = fs.concat(shell.workingDirectory, directory) +end +if fs.exists(directory) and fs.isDirectory(directory) then + shell.workingDirectory = fs.canonical(directory) else - if directory:sub(-1, -1) ~= "/" then - directory = directory .. "/" - end - if directory:sub(1, 1) ~= "/" then - directory = shell.workingDirectory .. directory - end - if fs.exists(directory) and fs.isDirectory(directory) or fs.exists(shell.workingDirectory .. directory) and fs.isDirectory(shell.workingDirectory .. directory) then - shell.workingDirectory = directory - else - print("\27[91mNo such directory.") - end + print("\27[91mNo such directory.") end diff --git a/halyde/apps/cp.lua b/halyde/apps/cp.lua index adc6576..cf6a8ab 100644 --- a/halyde/apps/cp.lua +++ b/halyde/apps/cp.lua @@ -1,6 +1,4 @@ -local args = {...} -local fromFile, toFile = args[1], args[2] -args = nil +local fromFile, toFile = ... local fs = import("filesystem") if not fromFile or not toFile then @@ -8,10 +6,10 @@ if not fromFile or not toFile then return end if fromFile:sub(1, 1) ~= "/" then - fromFile = shell.workingDirectory .. fromFile + fromFile = fs.concat(shell.workingDirectory, fromFile) end if toFile:sub(1, 1) ~= "/" then - toFile = shell.workingDirectory .. toFile + toFile = fs.concat(shell.workingDirectory, toFile) end if fromFile == toFile then print("\27[91mSource and destination are the same.") diff --git a/halyde/apps/helpdb/cat.txt b/halyde/apps/helpdb/cat.txt index e6ce488..77fb305 100644 --- a/halyde/apps/helpdb/cat.txt +++ b/halyde/apps/helpdb/cat.txt @@ -1,8 +1,8 @@ -Usage: cat [FILE] +Usage: cat [FILES]... Concatenates and prints a file. - FILE Specifies the path to the file to print. + FILES Specifies the paths to the files to print. Examples: - cat /init.lua Concatenates and prints init.lua in the root directory. - cat help.lua Concatenates and prints help.lua in the current working directory. + cat /init.lua Concatenates and prints init.lua in the root directory. + cat help.lua cat.lua Concatenates and prints help.lua and cat.lua in the current working directory. diff --git a/halyde/apps/helpdb/cp.txt b/halyde/apps/helpdb/cp.txt index e478126..842c747 100644 --- a/halyde/apps/helpdb/cp.txt +++ b/halyde/apps/helpdb/cp.txt @@ -1,9 +1,10 @@ Usage: cp [FLAGS] [SOURCE] [DESTINATION] Copies a file. - -o, --overwrite Allows any file that might be at the destination to be overwritten. - SOURCE Specifies the file to be copied. - DESTINATION Specifies the path to copy the file to. + FLAGS Specifies extra options when executing the command. + -o, --overwrite Allows any file that might be at the destination to be overwritten. + SOURCE Specifies the file to be copied. + DESTINATION Specifies the path to copy the file to. Examples: cp /home/a.txt /b.txt Copies the file at /home/a.txt to /b.txt. diff --git a/halyde/apps/helpdb/mv.txt b/halyde/apps/helpdb/mv.txt index 59e0e31..900744f 100644 --- a/halyde/apps/helpdb/mv.txt +++ b/halyde/apps/helpdb/mv.txt @@ -1,9 +1,10 @@ Usage: mv [FLAGS] [SOURCE] [DESTINATION] Moves/renames a file. - -o, --overwrite Allows any file that might be at the destination to be overwritten. - SOURCE Specifies the file to be moved/renamed. - DESTINATION Specifies the path/filename to move/rename the file to. + FLAGS Specifies extra options when executing the command. + -o, --overwrite Allows any file that might be at the destination to be overwritten. + SOURCE Specifies the file to be moved/renamed. + DESTINATION Specifies the path/filename to move/rename the file to. Examples: mv /home/a.txt /b.txt Moves the file at /home/a.txt to /b.txt. diff --git a/halyde/apps/mkdir.lua b/halyde/apps/mkdir.lua index b5ffb04..6f041fd 100644 --- a/halyde/apps/mkdir.lua +++ b/halyde/apps/mkdir.lua @@ -1,6 +1,4 @@ -local args = {...} -local directory = args[1] -args = nil +local directory = ... local fs = import("filesystem") if not directory then @@ -8,7 +6,7 @@ if not directory then return end if directory:sub(1, 1) ~= "/" then - directory = shell.workingDirectory .. directory + directory = fs.concat(shell.workingDirectory, directory) end if fs.exists(directory) then print("\27[91mAn object already exists at the specified path.") diff --git a/halyde/apps/mv.lua b/halyde/apps/mv.lua index 17c7f5a..8660759 100644 --- a/halyde/apps/mv.lua +++ b/halyde/apps/mv.lua @@ -1,6 +1,4 @@ -local args = {...} -local fromFile, toFile = args[1], args[2] -args = nil +local fromFile, toFile = ... local fs = import("filesystem") if not fromFile or not toFile then @@ -8,10 +6,10 @@ if not fromFile or not toFile then return end if fromFile:sub(1, 1) ~= "/" then - fromFile = shell.workingDirectory .. fromFile + fromFile = fs.concat(shell.workingDirectory, fromFile) end if toFile:sub(1, 1) ~= "/" then - toFile = shell.workingDirectory .. toFile + toFile = fs.concat(shell.workingDirectory, toFile) end if fromFile == toFile then print("\27[91mSource and destination are the same.") diff --git a/halyde/apps/rm.lua b/halyde/apps/rm.lua index 6c19ae0..f83b695 100644 --- a/halyde/apps/rm.lua +++ b/halyde/apps/rm.lua @@ -1,6 +1,4 @@ -local args = {...} -local file = args[1] -args = nil +local file = ... local fs = import("filesystem") if not file then @@ -8,9 +6,10 @@ if not file then return end if file:sub(1, 1) ~= "/" then - file = shell.workingDirectory .. file + file = fs.concat(shell.workingDirectory, file) end if not fs.exists(file) then print("\27[91mFile does not exist.") + return end fs.remove(file) diff --git a/halyde/lib/filesystem.lua b/halyde/lib/filesystem.lua index 1051058..c604ef7 100644 --- a/halyde/lib/filesystem.lua +++ b/halyde/lib/filesystem.lua @@ -9,29 +9,58 @@ end local filesystem = {} -function filesystem.processPath(path) -- returns the address and absolute path of a filesystem path as well as sanitizing it +function filesystem.canonical(path) checkArg(1, path, "string") - absPath = path:gsub("/+", "/") + local segList = {} + if path:sub(1, 1) ~= "/" then + path = "/" .. path + end + path = path:gsub("/+", "/") + for segment in path:gmatch("[^/]+") do + if segment == ".." and segList[1] then + table.remove(segList, #segList) + elseif segment ~= "." then + table.insert(segList, segment) + end + end + return "/" .. table.concat(segList, "/") +end + +function filesystem.concat(path1, path2) + checkArg(1, path1, "string") + checkArg(2, path2, "string") + if path1:sub(-1, -1) == "/" then + path1 = path1:sub(1, -2) + end + if path2:sub(1, 1) ~= "/" then + path2 = "/" .. path2 + end + return path1 .. path2 +end + +function filesystem.absolutePath(path) -- returns the address and absolute path of an object + checkArg(1, path, "string") + path = filesystem.canonical(path) local address = nil - if absPath:find("^/mnt/.../") then + if path:find("^/mnt/.../") then address = component.get(path:sub(6,8)) if not address then address = computer.getBootAddress() else - absPath = absPath:sub(9) + path = path:sub(9) end else address = computer.getBootAddress() end if not address then - return nil, "no such device" + return nil, "no such component" end - return address, absPath + return address, path end function filesystem.exists(path) -- check if path exists checkArg(1, path, "string") - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end @@ -47,7 +76,7 @@ function filesystem.open(path, mode) -- opens a file and returns its handle if not (mode == "r" or mode == "w" or mode == "rb" or mode == "wb") then return nil, "invalid handle type" end - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) local handleArgs = {component.invoke(address, "open", absPath, mode)} local handle = handleArgs[1] if not handle then @@ -81,7 +110,7 @@ function filesystem.list(path) end return returnTable else - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end @@ -91,7 +120,7 @@ end function filesystem.size(path) checkArg(1, path, "string") - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end @@ -100,7 +129,7 @@ end function filesystem.isDirectory(path) checkArg(1, path, "string") - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end @@ -110,8 +139,8 @@ end function filesystem.rename(fromPath, toPath) checkArg(1, fromPath, "string") checkArg(2, toPath, "string") - local fromAddress, fromAbsPath = filesystem.processPath(fromPath) - local toAddress, toAbsPath = filesystem.processPath(toPath) + local fromAddress, fromAbsPath = filesystem.absolutePath(fromPath) + local toAddress, toAbsPath = filesystem.absolutePath(toPath) if not fromAddress or not toAddress then return false end @@ -135,8 +164,8 @@ end function filesystem.copy(fromPath, toPath) checkArg(1, fromPath, "string") checkArg(2, toPath, "string") - local fromAddress, fromAbsPath = filesystem.processPath(fromPath) - local toAddress, toAbsPath = filesystem.processPath(toPath) + local fromAddress, fromAbsPath = filesystem.absolutePath(fromPath) + local toAddress, toAbsPath = filesystem.absolutePath(toPath) if not fromAddress or not toAddress then return false end @@ -154,7 +183,7 @@ end function filesystem.isDirectory(path) checkArg(1, path, "string") - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end @@ -163,7 +192,7 @@ end function filesystem.remove(path) checkArg(1, path, "string") - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end @@ -172,7 +201,7 @@ end function filesystem.makeDirectory(path) checkArg(1, path, "string") - local address, absPath = filesystem.processPath(path) + local address, absPath = filesystem.absolutePath(path) if not address then return false end