split_shell: correctly handle escaping edge cases

This commit is contained in:
Starbeamrainbowlabs 2021-07-28 17:49:18 +01:00
parent 31a4d89a49
commit c282e286bb
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
1 changed files with 13 additions and 2 deletions

View File

@ -36,7 +36,10 @@ function split_shell(text)
if curchar == "\"" and prevchar ~= "\\" and is_whitespace(nextchar) then
-- It's the end of a quote!
mode = "NORMAL"
elseif (curchar == "\\" and nextchar ~= "\"" and not is_whitespace(nextnextchar)) or curchar ~= "\\" then
elseif (curchar == "\\" and (
nextchar ~= "\""
or (nextchar == "\"" and not is_whitespace(nextnextchar))
)) or curchar ~= "\\" then
-- It's a regular character
table.insert(acc, curchar)
end
@ -44,7 +47,10 @@ function split_shell(text)
if curchar == "'" and prevchar ~= "\\" and is_whitespace(nextchar) then
-- It's the end of a quote!
mode = "NORMAL"
elseif (curchar == "\\" and nextchar ~= "'" and not is_whitespace(nextnextchar)) or curchar ~= "\\" then
elseif (curchar == "\\" and (
nextchar ~= "'"
or (nextchar == "'" and not is_whitespace(nextnextchar))
)) or curchar ~= "\\" then
-- It's a regular character
table.insert(acc, curchar)
end
@ -70,3 +76,8 @@ test("yay \"yay yay\" yay")
test("yay \"yay\\\" yay\" yay")
test("yay \"yay 'inside quotes' yay\" yay")
test("yay 'inside quotes' another")
test("y\"ay \"yay 'in\\\"side quotes' yay\" y\\\"ay")
-- for i=1,10000 do
-- split_shell("y\"ay \"yay 'in\\\"side quotes' yay\" y\\\"ay")
-- end