diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2013-10-14 12:03:19 -0700 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2013-10-14 12:03:19 -0700 |
| commit | 08e694edcb7e7a8afc5046455d4ad8d2a7f3923b (patch) | |
| tree | f012856cc6ebbf9a3b9909ebbbb6e3dd38b82044 /src | |
| parent | f728b1b2118cdf66a1e17778d49fe8b1b3648041 (diff) | |
| parent | 8adef07fb0393a809478c5b3148010f78db377c8 (diff) | |
| download | luarocks-08e694edcb7e7a8afc5046455d4ad8d2a7f3923b.tar.gz luarocks-08e694edcb7e7a8afc5046455d4ad8d2a7f3923b.tar.bz2 luarocks-08e694edcb7e7a8afc5046455d4ad8d2a7f3923b.zip | |
Merge pull request #166 from siffiejoe/win32-escapes
Win32 escapes
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/fs/win32.lua | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 4a105e8d..78a9f730 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
| @@ -16,18 +16,59 @@ 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 |
| 43 | if arg == "\\" then | ||
| 44 | return '\\' -- CHDIR needs special handling for root dir | ||
| 45 | end | ||
| 29 | -- URLs and anything else | 46 | -- URLs and anything else |
| 30 | return '"' .. arg:gsub('"', '\\"') .. '"' | 47 | arg = arg:gsub('(\\+)(")', q_escaper) |
| 48 | arg = arg:gsub('(\\+)$', q_escaper) | ||
| 49 | arg = arg:gsub('"', win_escape_chars) | ||
| 50 | arg = arg:gsub('(\\*)%%', p_escaper) | ||
| 51 | return '"' .. arg .. '"' | ||
| 52 | end | ||
| 53 | |||
| 54 | --- Quote argument for shell processing in batch files. | ||
| 55 | -- Adds double quotes and escapes. | ||
| 56 | -- @param arg string: Unquoted argument. | ||
| 57 | -- @return string: Quoted argument. | ||
| 58 | function Qb(arg) | ||
| 59 | assert(type(arg) == "string") | ||
| 60 | -- Quote DIR for Windows | ||
| 61 | if arg:match("^[%.a-zA-Z]?:?[\\/]") then | ||
| 62 | arg = arg:gsub("/", "\\") | ||
| 63 | end | ||
| 64 | if arg == "\\" then | ||
| 65 | return '\\' -- CHDIR needs special handling for root dir | ||
| 66 | end | ||
| 67 | -- URLs and anything else | ||
| 68 | arg = arg:gsub('(\\+)(")', q_escaper) | ||
| 69 | arg = arg:gsub('(\\+)$', q_escaper) | ||
| 70 | arg = arg:gsub('[%%"]', win_escape_chars) | ||
| 71 | return '"' .. arg .. '"' | ||
| 31 | end | 72 | end |
| 32 | 73 | ||
| 33 | --- Return an absolute pathname from a potentially relative one. | 74 | --- Return an absolute pathname from a potentially relative one. |
| @@ -73,7 +114,7 @@ function wrap_script(file, dest, name, version) | |||
| 73 | local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) | 114 | 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" | 115 | 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)..")" | 116 | 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') | 117 | wrapper:write(fs.Qb(lua)..' -e '..fs.Qb(ppaths)..' -lluarocks.loader -e '..fs.Qb(addctx)..' '..fs.Qb(file)..' %*\n') |
| 77 | wrapper:close() | 118 | wrapper:close() |
| 78 | return true | 119 | return true |
| 79 | end | 120 | end |
