Escape the substitution separator in cleaninput().

Improving on the previous fix and commit, no search for an unused substitution separator is actually needed; sed supports escaping of the substitution separator. Revert the $inputSep abstraction in favor of the former, hard-coded "|" character.
This commit is contained in:
Ingo Karkat
2011-05-31 15:10:55 +02:00
parent a97144d0e8
commit 267e771ba8

18
todo.sh
View File

@@ -272,16 +272,10 @@ cleaninput()
input=`echo $input | tr -d '\r\n'`
if [ "$1" = "for sed" ]; then
# This action uses sed and & as the matched string so escape it.
# This action uses sed with "|" as the substitution separator, and & as
# the matched string; these must be escaped.
# Backslashes must be escaped, too.
input=`echo $input | sed -e 's+\\\+\\\\\\\\+g' -e 's/\&/\\\&/g'`
# Find a separator that doesn't occur in $input, sed cannot handle it, and escaping doesn't help.
# In the worst case (no separator found), the replacement text after the "|" is lost.
for inputSep in '%' '#' '@' ':' '!' '|'
do
[[ "$input" = *${inputSep}* ]] || break
done
input=`echo $input | sed -e 's+\\\+\\\\\\\\+g' -e 's/\&/\\\&/g' -e 's/|/\\\\|/g'`
fi
}
@@ -327,7 +321,6 @@ replaceOrPrepend()
input=$*
fi
cleaninput "for sed"
sep=${inputSep:-|}
# Retrieve existing priority and prepended date
priority=$(sed -e "$item!d" -e $item's/^\(([A-Z]) \)\{0,1\}\([0-9]\{2,4\}-[0-9]\{2\}-[0-9]\{2\} \)\{0,1\}.*/\1/' "$TODO_FILE")
@@ -342,7 +335,7 @@ replaceOrPrepend()
# Temporarily remove any existing priority and prepended date, perform the
# change (replace/prepend) and re-insert the existing priority and prepended
# date again.
sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s${sep}^.*${sep}${priority}${prepdate}${input}${backref}${sep}" "$TODO_FILE"
sed -i.bak -e "$item s/^${priority}${prepdate}//" -e "$item s|^.*|${priority}${prepdate}${input}${backref}|" "$TODO_FILE"
if [ $TODOTXT_VERBOSE -gt 0 ]; then
newtodo=$(sed "$item!d" "$TODO_FILE")
case "$action" in
@@ -849,9 +842,8 @@ case $action in
*) appendspace=" ";;
esac
cleaninput "for sed"
sep=${inputSep:-|}
if sed -i.bak $item" s${sep}^.*${sep}&${appendspace}${input}${sep}" "$TODO_FILE"; then
if sed -i.bak $item" s|^.*|&${appendspace}${input}|" "$TODO_FILE"; then
if [ $TODOTXT_VERBOSE -gt 0 ]; then
newtodo=$(sed "$item!d" "$TODO_FILE")
echo "$item $newtodo"