| 1 | #!/bin/bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | set -eu |
| 18 | |
| 19 | : "${SELFTEST:=}" |
| 20 | if [ -n "${SELFTEST}" ]; then |
| 21 | unset SELFTEST |
| 22 | tab=$(printf '\t') |
| 23 | check() { |
| 24 | local desc=$1 input=$2 want=$3 got |
| 25 | got=$(printf '%s\n' "${input}" | "$0") |
| 26 | if [ "${got}" = "${want}" ]; then |
| 27 | echo "ok: ${desc}" |
| 28 | else |
| 29 | echo "FAIL: ${desc}" >&2 |
| 30 | printf ' want: %q\n got: %q\n' "${want}" "${got}" >&2 |
| 31 | return 1 |
| 32 | fi |
| 33 | } |
| 34 | echo >&2 "=== Running tests ===" |
| 35 | check "spaces are decoded" '/tmp/dir\040with\040spaces' "/tmp/dir with spaces" |
| 36 | check "tab is decoded" '/a\011b' "/a${tab}b" |
| 37 | check "newline is decoded" 'a\012b' "$(printf 'a\nb')" |
| 38 | check "backslash is decoded" '/a\134b' '/a\b' |
| 39 | check "hash is decoded" '/a\043b' "/a#b" |
| 40 | check "a string without escapes is unchanged" "/mnt/nospace" "/mnt/nospace" |
| 41 | check "mixed escapes are decoded" '/a\040b\134c' '/a b\c' |
| 42 | echo >&2 "=== All tests passed ===" |
| 43 | exit 0 |
| 44 | fi |
| 45 | |
| 46 | sed -e 's/\\040/ /g; s/\\011/\t/g; s/\\012/\n/g; s/\\134/\\/g; s/\\043/#/g' |