split_shell: fix escape handling by unwinding escaping by 1 level

This commit is contained in:
Starbeamrainbowlabs 2021-07-28 17:54:44 +01:00
parent c282e286bb
commit 6a77b3cd21
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2

@ -2,6 +2,15 @@ function is_whitespace(char)
return char == " " or char == "\t" or char == "\r" or char == "\n" return char == " " or char == "\t" or char == "\r" or char == "\n"
end end
local function table_map(tbl, func)
local result = {}
for i,value in ipairs(tbl) do
local newval = func(value, i)
if newval ~= nil then table.insert(result, newval) end
end
return result
end
function split_shell(text) function split_shell(text)
local text_length = #text local text_length = #text
local scan_pos = 1 local scan_pos = 1
@ -60,7 +69,11 @@ function split_shell(text)
if #acc > 0 then if #acc > 0 then
table.insert(result, table.concat(acc, "")) table.insert(result, table.concat(acc, ""))
end end
return result
-- Unwind all escapes by 1 level
return table_map(result, function(str)
return str:gsub("\\([\"'\\])", "%1")
end)
end end
function test(text) function test(text)
@ -74,7 +87,7 @@ end
test("yay yay yay") test("yay yay yay")
test("yay \"yay yay\" yay") test("yay \"yay yay\" yay")
test("yay \"yay\\\" yay\" yay") test("yay \"yay\\\" yay\" yay")
test("yay \"yay 'inside quotes' yay\" yay") test("yay \"yay 'inside quotes' yay\\\"\" yay")
test("yay 'inside quotes' another") test("yay 'inside quotes' another")
test("y\"ay \"yay 'in\\\"side quotes' yay\" y\\\"ay") test("y\"ay \"yay 'in\\\"side quotes' yay\" y\\\"ay")