diff options
| author | Philipp Janda <siffiejoe@gmx.net> | 2013-10-14 18:28:34 +0200 |
|---|---|---|
| committer | Philipp Janda <siffiejoe@gmx.net> | 2013-10-14 18:28:34 +0200 |
| commit | ef0653619052d02035e2cf6cc0b95effc543f3b1 (patch) | |
| tree | 159fe08c9bac40c360c83642720424cdc90f1c5f /src | |
| parent | f728b1b2118cdf66a1e17778d49fe8b1b3648041 (diff) | |
| download | luarocks-ef0653619052d02035e2cf6cc0b95effc543f3b1.tar.gz luarocks-ef0653619052d02035e2cf6cc0b95effc543f3b1.tar.bz2 luarocks-ef0653619052d02035e2cf6cc0b95effc543f3b1.zip | |
better shell escaping on windows
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/fs/lua.lua | 12 | ||||
| -rw-r--r-- | src/luarocks/fs/win32.lua | 47 |
2 files changed, 53 insertions, 6 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 0e81d877..119fcbe4 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -42,6 +42,18 @@ function Q(arg) | |||
| 42 | return "'" .. arg:gsub("'", "'\\''") .. "'" | 42 | return "'" .. arg:gsub("'", "'\\''") .. "'" |
| 43 | end | 43 | end |
| 44 | 44 | ||
| 45 | --- Quote argument for shell processing in batch files/scripts. | ||
| 46 | -- By default calls fs.Q() | ||
| 47 | -- @param arg string: Unquoted argument. | ||
| 48 | -- @return string: Quoted argument. | ||
| 49 | function Qb(arg) | ||
| 50 | assert(type(arg) == "string") | ||
| 51 | |||
| 52 | -- only strange platforms (aka Windows) need special | ||
| 53 | -- escape handling for scripts/batch files | ||
| 54 | return fs.Q(arg) | ||
| 55 | end | ||
| 56 | |||
| 45 | --- Test is file/dir is writable. | 57 | --- Test is file/dir is writable. |
| 46 | -- Warning: testing if a file/dir is writable does not guarantee | 58 | -- Warning: testing if a file/dir is writable does not guarantee |
| 47 | -- that it will remain writable and therefore it is no replacement | 59 | -- that it will remain writable and therefore it is no replacement |
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 4a105e8d..f7555439 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
| @@ -16,18 +16,53 @@ function quiet(cmd) | |||
| 16 | return cmd.." 2> NUL 1> NUL" | 16 | return cmd.." 2> NUL 1> NUL" |
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | |||
| 20 | local win_escape_chars = { | ||
| 21 | ["%"] = "%%", | ||
| 22 | ['"'] = '\\"', | ||
| 23 | } | ||
| 24 | |||
| 25 | local function q_escaper(bs, q) | ||
| 26 | return ("\\"):rep(2*#bs-1) .. (q or "\\") | ||
| 27 | end | ||
| 28 | |||
| 29 | local function p_escaper(bs) | ||
| 30 | return bs .. bs .. '"%"' | ||
| 31 | end | ||
| 32 | |||
| 19 | --- Quote argument for shell processing. Fixes paths on Windows. | 33 | --- Quote argument for shell processing. Fixes paths on Windows. |
| 20 | -- Adds single quotes and escapes. | 34 | -- Adds double quotes and escapes. |
| 21 | -- @param arg string: Unquoted argument. | 35 | -- @param arg string: Unquoted argument. |
| 22 | -- @return string: Quoted argument. | 36 | -- @return string: Quoted argument. |
| 23 | function Q(arg) | 37 | function Q(arg) |
| 24 | assert(type(arg) == "string") | 38 | assert(type(arg) == "string") |
| 25 | -- Quote DIR for Windows | 39 | -- Quote DIR for Windows |
| 26 | if arg:match("^[%.a-zA-Z]?:?[\\/]") then | 40 | if arg:match("^[%.a-zA-Z]?:?[\\/]") then |
| 27 | return '"' .. arg:gsub("/", "\\"):gsub('"', '\\"') .. '"' | 41 | arg = arg:gsub("/", "\\") |
| 28 | end | 42 | end |
| 29 | -- URLs and anything else | 43 | -- URLs and anything else |
| 30 | return '"' .. arg:gsub('"', '\\"') .. '"' | 44 | arg = arg:gsub('(\\+)(")', q_escaper) |
| 45 | arg = arg:gsub('(\\+)$', q_escaper) | ||
| 46 | arg = arg:gsub('"', win_escape_chars) | ||
| 47 | arg = arg:gsub('(\\*)%%', p_escaper) | ||
| 48 | return '"' .. arg .. '"' | ||
| 49 | end | ||
| 50 | |||
| 51 | --- Quote argument for shell processing in batch files. | ||
| 52 | -- Adds double quotes and escapes. | ||
| 53 | -- @param arg string: Unquoted argument. | ||
| 54 | -- @return string: Quoted argument. | ||
| 55 | function Qb(arg) | ||
| 56 | assert(type(arg) == "string") | ||
| 57 | -- Quote DIR for Windows | ||
| 58 | if arg:match("^[%.a-zA-Z]?:?[\\/]") then | ||
| 59 | arg = arg:gsub("/", "\\") | ||
| 60 | end | ||
| 61 | -- URLs and anything else | ||
| 62 | arg = arg:gsub('(\\+)(")', q_escaper) | ||
| 63 | arg = arg:gsub('(\\+)$', q_escaper) | ||
| 64 | arg = arg:gsub('[%%"]', win_escape_chars) | ||
| 65 | return '"' .. arg .. '"' | ||
| 31 | end | 66 | end |
| 32 | 67 | ||
| 33 | --- Return an absolute pathname from a potentially relative one. | 68 | --- Return an absolute pathname from a potentially relative one. |
| @@ -73,7 +108,7 @@ function wrap_script(file, dest, name, version) | |||
| 73 | local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) | 108 | local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) |
| 74 | local ppaths = "package.path="..util.LQ(lpath..";").."..package.path; package.cpath="..util.LQ(lcpath..";").."..package.cpath" | 109 | local ppaths = "package.path="..util.LQ(lpath..";").."..package.path; package.cpath="..util.LQ(lcpath..";").."..package.cpath" |
| 75 | local addctx = "luarocks.loader.add_context("..util.LQ(name)..","..util.LQ(version)..")" | 110 | local addctx = "luarocks.loader.add_context("..util.LQ(name)..","..util.LQ(version)..")" |
| 76 | wrapper:write(fs.Q(lua)..' -e '..fs.Q(ppaths)..' -lluarocks.loader -e '..fs.Q(addctx)..' '..fs.Q(file)..' %*\n') | 111 | wrapper:write(fs.Qb(lua)..' -e '..fs.Qb(ppaths)..' -lluarocks.loader -e '..fs.Qb(addctx)..' '..fs.Qb(file)..' %*\n') |
| 77 | wrapper:close() | 112 | wrapper:close() |
| 78 | return true | 113 | return true |
| 79 | end | 114 | end |
