Skip to content

Commit

Permalink
🏃 merge pull request #87 from FWDekker/escape-sed~
Browse files Browse the repository at this point in the history
🏃 mommy escapes confusing scenarios~
  • Loading branch information
FWDekker authored Dec 3, 2023
2 parents 464d9e1 + 930c408 commit 0fddd97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### fixed
* 🚒 mommy fixes the description url in her manual page~ ([#81](https://github.com/FWDekker/mommy/issues/81)) ([#82](https://github.com/FWDekker/mommy/issues/82))
* 🕰️ mommy uses the correct types of changelogs in the right places ([#83](https://github.com/FWDekker/mommy/issues/83))
* 🏃 mommy no longer crashes when using `\` or `&` in variables~ ([#84](https://github.com/FWDekker/mommy/issues/84)) ([#87](https://github.com/FWDekker/mommy/issues/87))


## [1.2.6] -- 2023-11-29
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,10 @@ elements that contain whitespace only, and elements that start with a `#` are ig
then mommy will never use templates that contain `cat`, and will never use templates that contain `dog`~

### 🧬 custom templates
you can add a [list](#-lists) of your own compliments to either `MOMMY_COMPLIMENTS` or `MOMMY_COMPLIMENTS_EXTRA`, but
there is a slight difference:
you can add a [list](#-lists) of your own compliments to either `MOMMY_COMPLIMENTS` or `MOMMY_COMPLIMENTS_EXTRA`.
there is a slight difference between the two lists:

* if you want both the default _and_ your own compliments, add your own compliments to `MOMMY_COMPLIMENTS_EXTRA`, but
* if you want both the default _and_ your own compliments, add your own compliments to `MOMMY_COMPLIMENTS_EXTRA`~
* if you want your own compliments and _not_ the default compliments, add your own compliments to `MOMMY_COMPLIMENTS`~

and similarly so for encouragements~
Expand Down
27 changes: 17 additions & 10 deletions src/main/sh/mommy
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ list_contains_any() {
# Takes the list in stdin and (1) removes all lines starting with `#`, (2) replaces each `/` with a newline, (3) removes
# all blank lines, and (4) removes all entries that contain any of the entries in the sanitized list `$1` as a
# substring.
list_sanitize() {
list_normalize() {
cat |
grep -v "^#" |
tr "/" "$n" |
Expand All @@ -113,6 +113,13 @@ else
}
fi

# Escapes stdin to be used as a replacement string in sed.
#
# Only the characters `\` and `&` need replacement, since we assume that `/` does not occur.
escape_sed_replacement() {
cat | sed -e 's/\\/\\\\/g' -e 's/&/\\\&/g'
}


## Functions
# Prints `$2`, but with color depending on `$1`. If `$1` equals `lolcat`, stdin is piped to `lolcat`. If `$1` is empty,
Expand Down Expand Up @@ -170,12 +177,12 @@ split_pronouns() {
# 4. prepends `$4` and appends `$5`; and
# 5. writes to stdout.
fill_template() {
sweetie="$(echo "$1" | list_sanitize | list_choose)"
split_pronouns "$(echo "$2" | list_sanitize | list_choose)"
caregiver="$(echo "$3" | list_sanitize | list_choose)"
sweetie="$(echo "$1" | list_normalize | list_choose | escape_sed_replacement)"
split_pronouns "$(echo "$2" | list_normalize | list_choose | escape_sed_replacement)"
caregiver="$(echo "$3" | list_normalize | list_choose | escape_sed_replacement)"

prefix="$(echo "$4" | list_sanitize | list_choose)"
suffix="$(echo "$5" | list_sanitize | list_choose)"
prefix="$(echo "$4" | list_normalize | list_choose | escape_sed_replacement)"
suffix="$(echo "$5" | list_normalize | list_choose | escape_sed_replacement)"

template="$(cat | sed -e "s/%%SWEETIE%%/$sweetie/g" \
-e "s/%%THEY%%/$they/g" \
Expand Down Expand Up @@ -254,7 +261,7 @@ else
fi

# Choose and fill template (if enabled)
if list_contains_any "$command_exit_code" "$(echo "$MOMMY_IGNORED_STATUSES" | list_sanitize)"; then
if list_contains_any "$command_exit_code" "$(echo "$MOMMY_IGNORED_STATUSES" | list_normalize)"; then
exit "$command_exit_code"
elif [ "$command_exit_code" -eq 0 ] && [ "$MOMMY_COMPLIMENTS_ENABLED" = "1" ]; then
templates="$MOMMY_COMPLIMENTS/$MOMMY_COMPLIMENTS_EXTRA"
Expand All @@ -264,11 +271,11 @@ else
exit "$command_exit_code"
fi

color="$(echo "$MOMMY_COLOR" | list_sanitize | list_choose)"
forbidden_words="$(echo "$MOMMY_FORBIDDEN_WORDS" | list_sanitize)"
color="$(echo "$MOMMY_COLOR" | list_normalize | list_choose)"
forbidden_words="$(echo "$MOMMY_FORBIDDEN_WORDS" | list_normalize)"

response="$(echo "$templates" |
list_sanitize "$forbidden_words" |
list_normalize "$forbidden_words" |
list_choose |
fill_template "$MOMMY_SWEETIE" "$MOMMY_PRONOUNS" "$MOMMY_CAREGIVER" "$MOMMY_PREFIX" \
"$MOMMY_SUFFIX" "$MOMMY_CAPITALIZE")"
Expand Down
8 changes: 8 additions & 0 deletions src/test/sh/unit_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ Describe "mommy"
End

Describe "template variables"
It "escapes sed-specific characters"
set_config "MOMMY_COMPLIMENTS='>%%SWEETIE%%<';MOMMY_SWEETIE='&\\'"

When run "$MOMMY_EXEC" -c "$MOMMY_CONFIG_FILE" true
The error should equal ">&\\<"
The status should be success
End

It "replaces %%SWEETIE%%"
set_config "MOMMY_COMPLIMENTS='>%%SWEETIE%%<';MOMMY_SWEETIE='attempt'"

Expand Down

0 comments on commit 0fddd97

Please sign in to comment.