Skip to content

Commit

Permalink
Also handle special characters (that need escaping) in the default ac…
Browse files Browse the repository at this point in the history
…tion

Spaces, quotes, $, and other special shell characters need to be escaped inside the TODOTXT_DEFAULT_ACTION, and consequently eval() used to parse that back into individual arguments.
  • Loading branch information
inkarkat committed Apr 3, 2023
1 parent 6cc8217 commit 18479f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 17 additions & 1 deletion tests/t0002-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ chmod +x foo

cat > foo2 << 'EOF'
shift
echo "TODO: $*"
IFS=-
printf 'TODO: %s\n' "$*"
EOF
chmod +x foo2

Expand Down Expand Up @@ -50,4 +51,19 @@ test_expect_success 'custom action (default action)' '
test_cmp expect output && rm -rf .todo.actions.d
'

test_todo_session 'default custom action with multiple arguments' <<EOF
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
>>> TODOTXT_DEFAULT_ACTION='foo2 foo bar baz' todo.sh
TODO: foo-bar-baz
EOF

export TODOTXT_DEFAULT_ACTION="foo2 foo\\ bar \\\$HOSTNAME O\\'Really\\? \\\"quoted\\\""
test_todo_session 'default custom action with arguments that have special characters' <<EOF
>>> mkdir -p .todo.actions.d && cp foo2 .todo.actions.d/
>>> todo.sh
TODO: foo bar-\$HOSTNAME-O'Really?-"quoted"
EOF

test_done
7 changes: 4 additions & 3 deletions todo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -984,10 +984,11 @@ then
exit $?
else
## Use eventually given parameters
actionarray=($action)
if [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/${actionarray[0]}" ]
eval "actionarray=($ACTION)" # Note: Need to use original $ACTION to avoid that arguments are getting lowercased, too.
action=$( printf "%s\n" "${actionarray[0]}" | tr 'A-Z' 'a-z' )
if [ -d "$TODO_ACTIONS_DIR" -a -x "$TODO_ACTIONS_DIR/$action" ]
then
"$TODO_ACTIONS_DIR/${actionarray[0]}" "${actionarray[@]}"
"$TODO_ACTIONS_DIR/$action" "${actionarray[@]}"
exit $?
fi
fi
Expand Down

0 comments on commit 18479f9

Please sign in to comment.