Sanitize sed input

This commit is contained in:
lambtho12
2022-01-20 13:32:11 +01:00
parent d5c107c195
commit 44ad8d87be

View File

@@ -206,6 +206,26 @@ local function escape(s)
return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1")
end end
-- sanitize strings
local escape_chars = function(string)
return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%/]", {
["\\"] = "\\\\",
["-"] = "\\-",
["("] = "\\(",
[")"] = "\\)",
["["] = "\\[",
["]"] = "\\]",
["{"] = "\\{",
["}"] = "\\}",
["?"] = "\\?",
["+"] = "\\+",
["*"] = "\\*",
["^"] = "\\^",
["$"] = "\\$",
["/"] = "\\/",
})
end
local function recursive_substitution(dir, old, new) local function recursive_substitution(dir, old, new)
if not global_dir_check() then if not global_dir_check() then
return return
@@ -220,17 +240,21 @@ local function recursive_substitution(dir, old, new)
return return
end end
os.execute( old = escape_chars(old)
"find " new = escape_chars(new)
local replace_cmd = "find "
.. dir .. dir
.. " -type f -name '*" .. " -type f -name '*"
.. M.Cfg.extension .. M.Cfg.extension
.. "' -exec sed -i 's|" .. "' -exec sed -i 's/"
.. old .. old
.. "|" .. "/"
.. new .. new
.. "|g' {} +" .. "/g' {} +"
)
print(replace_cmd)
os.execute(replace_cmd)
end end
local function save_all_tk_buffers() local function save_all_tk_buffers()
@@ -1477,8 +1501,8 @@ local function RenameNote()
if M.Cfg.rename_update_links == true then if M.Cfg.rename_update_links == true then
-- Only look for the first part of the link, so we do not touch to #heading or #^paragraph -- Only look for the first part of the link, so we do not touch to #heading or #^paragraph
-- Should use regex instead to ensure it is a proper link -- Should use regex instead to ensure it is a proper link
local oldlink = "\\[\\[" .. oldfile.title local oldlink = "[[" .. oldfile.title
local newlink = "\\[\\[" .. newname local newlink = "[[" .. newname
-- Save open telekasten buffers before looking for links to replace -- Save open telekasten buffers before looking for links to replace
if if