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(...) local args, options = shell.parse(...)
if #args < 2 then if #args < 2 then
io.write("Usage: cp [-frv] <from...> <to>\n") io.write("Usage: cp [-inrv] <from...> <to>\n")
io.write(" -f: overwrite file if it already exists.\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(" -r: copy directories recursively.\n")
io.write(" -v: verbose output.") io.write(" -v: verbose output.")
return return
@ -25,21 +26,25 @@ end
local result, reason 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) local function recurse(fromPath, toPath)
status(fromPath, toPath) status(fromPath, toPath)
if fs.isDirectory(fromPath) then if fs.isDirectory(fromPath) then
if not options.r then if not options.r then
io.write("omitting directory '" .. fromPath .. "'\n") io.write("omitting directory `" .. fromPath .. "'\n")
return true return true
end end
if fs.canonical(fromPath) == fs.canonical(fs.path(toPath)) then 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 end
if fs.exists(toPath) and not fs.isDirectory(toPath) then if fs.exists(toPath) and not fs.isDirectory(toPath) then
if not options.f then -- my real cp always does this, even with -f, -n or -i.
return nil, "target file exists" return nil, "cannot overwrite non-directory `" .. toPath .. "' with directory `" .. fromPath .. "'"
end
fs.remove(toPath)
end end
fs.makeDirectory(toPath) fs.makeDirectory(toPath)
for file in fs.list(fromPath) do for file in fs.list(fromPath) do
@ -52,10 +57,27 @@ local function recurse(fromPath, toPath)
else else
if fs.exists(toPath) then if fs.exists(toPath) then
if fs.canonical(fromPath) == fs.canonical(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 end
if fs.isDirectory(toPath) or not options.f then if fs.isDirectory(toPath) then
return nil, "target file exists" 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 end
fs.remove(toPath) fs.remove(toPath)
end end

View File

@ -48,7 +48,7 @@ print("Installing OpenOS to device " .. (choice.getLabel() or choice.address))
os.sleep(0.25) os.sleep(0.25)
local boot = computer.getBootAddress():sub(1, 3) local boot = computer.getBootAddress():sub(1, 3)
local mnt = choice.address: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 if not result then
error(reason, 0) error(reason, 0)
end end

View File

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

View File

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

View File

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