notmuch-emacs-mua: non-forking escape () usage with backslash '\' escape

Use the printf -v convention to give output variable as argument
to escape () function so no subshell needs to be executed for
escaping input. The '-v' option to escape () is just syntactic
sugar for better understanding.

Also, backslash is now escaped with another backslash for emacs. This
ie especially important at the end of string.

`echo` is no longer used to write escaped output -- it might interpret
the escapes itself.
This commit is contained in:
Tomi Ollila 2015-03-29 19:37:34 +03:00 committed by David Bremner
parent 244f873954
commit 0fa9cf75e5

View file

@ -22,9 +22,12 @@
set -eu set -eu
# escape: "expand" '\' as '\\' and '"' as '\"'
# calling convention: escape -v var "$arg" (like in bash printf).
escape () escape ()
{ {
echo "${1//\"/\\\"}" local __escape_arg__=${3//\\/\\\\}
printf -v $2 '%s' "${__escape_arg__//\"/\\\"}"
} }
EMACS=${EMACS-emacs} EMACS=${EMACS-emacs}
@ -72,9 +75,7 @@ while getopts :s:c:b:i:h opt; do
;; ;;
esac esac
escape -v OPTARG "${OPTARG-none}"
OPTARG="${OPTARG-none}"
OPTARG="$(escape "${OPTARG}")"
case "${opt}" in case "${opt}" in
--help|h) --help|h)
@ -117,7 +118,7 @@ done
# Positional parameters. # Positional parameters.
for arg; do for arg; do
arg="$(escape "${arg}")" escape -v arg "${arg}"
ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")" ELISP="${ELISP} (message-goto-to) (insert \"${arg}, \")"
done done