From 773c59ff89bd08ea2fb45a4f624c3d10dfa1d368 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sat, 12 Mar 2022 21:04:29 +0000 Subject: [PATCH] cdblib: offer posix escaped paths in renderers --- cdblib.lua | 85 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/cdblib.lua b/cdblib.lua index c902360..5da7b44 100644 --- a/cdblib.lua +++ b/cdblib.lua @@ -150,55 +150,16 @@ function _M.iter_table(t) end) end end ------------------------------------------------------------------ }}} ---------------------------------------------- Generator utilities {{{ - --- lazily generate and cache escaped version -local function _renderer_for_esc(t,k) - local nesc - t.f, nesc = escape_gnu_digest(t.u) - t.e = nesc == 0 and "" or "\\" - return t[k] - end - --- Generate a renderer for a choice of common parameters. In the resulting --- template expansion, --- --- $e expands to "\\" (resp. "") if the path was (resp. was not) escaped --- $f expands to the optionally escaped file name (see $e) --- $h expands to the hash --- $u expands to the unescaped file name --- $z expands to the appropriate record separator ("\n" or "\0") --- -function _M.renderer_for(nul, unescape, template) - local v = { z = nul and "\0" or "\n" - , f = unescape and function(t) return t.u end or _renderer_for_esc - , e = unescape and "" or _renderer_for_esc - } - local mt = { __index = - function(t,k) - local x = v[k] - return type(x) == "function" and x(t,k) or x - end - } - return function(hash, path) - return template:substitute(setmetatable({h = hash, u = path}, mt)) - end -end - -function _M.mk_default_render_template() - return (require "pl.text").Template("$e$h $f$z") -end - ----------------------------------------------------------------- }}} ------------------------------------------- Path escape utilities {{{ -- This appears to be pretty safe, even in the presence of non-ASCII bytes. -- That's kind of great and we will use this by default whenever we generate -- text for a shell. -function _M.posix_shell_escape(str) +local function posix_shell_escape(str) return "'" .. str:gsub("'", "'\"'\"'") .. "'" end +_M.posix_shell_escape = posix_shell_escape -- While POSIX shells understand control characters inside single quotes, they -- are unfriendly to read as such. Some shells have a $'...' escape that can @@ -240,6 +201,48 @@ function _M.mk_human_shell_escape(rexlib) end end +----------------------------------------------------------------- }}} +--------------------------------------------- Generator utilities {{{ + +-- lazily generate and cache escaped version +local function _renderer_for_esc(t,k) + local nesc + t.f, nesc = escape_gnu_digest(t.u) + t.e = nesc == 0 and "" or "\\" + return t[k] +end + +-- Generate a renderer for a choice of common parameters. In the resulting +-- template expansion, +-- +-- $e expands to "\\" (resp. "") if the path was (resp. was not) GNU escaped +-- $f expands to the optionally GNU escaped file name (see $e) +-- $h expands to the hash +-- $P expands to the POSIX-shell-escaped file name +-- $u expands to the unescaped file name +-- $z expands to the appropriate record separator ("\n" or "\0") +-- +function _M.renderer_for(nul, unescape, template) + local v = { z = nul and "\0" or "\n" + , f = unescape and function(t) return t.u end or _renderer_for_esc + , e = unescape and "" or _renderer_for_esc + , P = function(t) return posix_shell_escape(t.u) end + } + local mt = { __index = + function(t,k) + local x = v[k] + return type(x) == "function" and x(t,k) or x + end + } + return function(hash, path) + return template:substitute(setmetatable({h = hash, u = path}, mt)) + end +end + +function _M.mk_default_render_template() + return (require "pl.text").Template("$e$h $f$z") +end + ----------------------------------------------------------------- }}} return _M -- 2.50.1