Fixed aliases with arguments killing of actual arguments.

Made cp more like real cp (at least the one that's on my Debian), adding alias 'cp -> cp -i' to compensate for the automatic overriding. Closes #384.
This commit is contained in:
Florian Nücke 2014-07-15 15:22:54 +02:00
parent 88710b7b95
commit 4f73a83c76
5 changed files with 38 additions and 14 deletions

View File

@ -3,8 +3,9 @@ local shell = require("shell")
local args, options = shell.parse(...)
if #args < 2 then
io.write("Usage: cp [-frv] <from...> <to>\n")
io.write(" -f: overwrite file if it already exists.\n")
io.write("Usage: cp [-inrv] <from...> <to>\n")
io.write(" -i: prompt before overwrite (overrides -n option).\n")
io.write(" -n: do not overwrite an existing file.\n")
io.write(" -r: copy directories recursively.\n")
io.write(" -v: verbose output.")
return
@ -25,21 +26,25 @@ end
local result, reason
local function prompt(message)
io.write(message .. " ")
local result = io.read()
return result and result:sub(1, 1):lower() == "y"
end
local function recurse(fromPath, toPath)
status(fromPath, toPath)
if fs.isDirectory(fromPath) then
if not options.r then
io.write("omitting directory '" .. fromPath .. "'\n")
io.write("omitting directory `" .. fromPath .. "'\n")
return true
end
if fs.canonical(fromPath) == fs.canonical(fs.path(toPath)) then
return nil, "cannot copy a directory, '" .. fromPath .. "', into itself, '" .. toPath .. "'\n"
return nil, "cannot copy a directory, `" .. fromPath .. "', into itself, `" .. toPath .. "'\n"
end
if fs.exists(toPath) and not fs.isDirectory(toPath) then
if not options.f then
return nil, "target file exists"
end
fs.remove(toPath)
-- my real cp always does this, even with -f, -n or -i.
return nil, "cannot overwrite non-directory `" .. toPath .. "' with directory `" .. fromPath .. "'"
end
fs.makeDirectory(toPath)
for file in fs.list(fromPath) do
@ -52,10 +57,27 @@ local function recurse(fromPath, toPath)
else
if fs.exists(toPath) then
if fs.canonical(fromPath) == fs.canonical(toPath) then
return nil, "'" .. fromPath .. "' and '" .. toPath .. "' are the same file"
return nil, "`" .. fromPath .. "' and `" .. toPath .. "' are the same file"
end
if fs.isDirectory(toPath) or not options.f then
return nil, "target file exists"
if fs.isDirectory(toPath) then
if options.i then
if not prompt("overwrite `" .. toPath .. "'?") then
return true
end
elseif options.n then
return true
else -- yes, even for -f
return nil, "cannot overwrite directory `" .. toPath .. "' with non-directory"
end
else
if options.i then
if not prompt("overwrite `" .. toPath .. "'?") then
return true
end
elseif options.n then
return true
end
-- else: default to overwriting
end
fs.remove(toPath)
end

View File

@ -48,7 +48,7 @@ print("Installing OpenOS to device " .. (choice.getLabel() or choice.address))
os.sleep(0.25)
local boot = computer.getBootAddress():sub(1, 3)
local mnt = choice.address:sub(1, 3)
local result, reason = os.execute("cp -vfr /mnt/" .. boot .. "/* /mnt/" .. mnt .. "/")
local result, reason = os.execute("/bin/cp -vr /mnt/" .. boot .. "/* /mnt/" .. mnt .. "/")
if not result then
error(reason, 0)
end

View File

@ -104,7 +104,7 @@ local function execute(env, command, ...)
if not program then
return false, reason
end
for i = 1, args.n do
for i = 1, #args do
for _, arg in ipairs(evaluate(args[i])) do
table.insert(eargs, arg)
end

View File

@ -13,3 +13,4 @@ shell.setAlias("rs", "redstone")
shell.setAlias("view", "edit -r")
shell.setAlias("help", "man")
shell.setAlias("?", "man")
shell.setAlias("cp", "cp -i")

View File

@ -88,6 +88,7 @@ end
function shell.resolveAlias(command, args)
checkArg(1, command, "string")
checkArg(2, args, "table", "nil")
args = args or {}
local program, lastProgram = command, nil
while true do
local tokens = text.tokenize(shell.getAlias(program) or program)