Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 711a46f

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Add escape module and tests
1 parent 4555998 commit 711a46f

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

lib/psst/basic.inc.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ fi
2323
# shellcheck source=basic/chkcmd.inc.sh
2424
. "$INCLUDE_PSST/basic/chkcmd.inc.sh"
2525

26+
# shellcheck source=basic/esc.inc.sh
27+
. "$INCLUDE_PSST/basic/esc.inc.sh"
28+
2629
# shellcheck source=basic/conv.inc.sh
2730
. "$INCLUDE_PSST/basic/conv.inc.sh"
2831

lib/psst/basic/esc.inc.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env sh
2+
3+
# Double include protection
4+
case "$INCLUDE_SEEN_PSST" in
5+
*_esc.inc.sh_*)
6+
return
7+
;;
8+
esac
9+
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _esc.inc.sh_"
10+
11+
12+
# shellcheck source=assert.inc.sh
13+
. "$INCLUDE_PSST/basic/assert.inc.sh"
14+
15+
16+
##
17+
# SUBPROCESS
18+
# esc_for_sq_psst <value>
19+
#
20+
# SUMMARY
21+
# Escapes `value` so it can be safely used within single quotes. This
22+
# means it only escapes single quotes themselves as all other characters,
23+
# including backshlash, are safe to be used within single quotes. Basically
24+
# `abc'def'`` becomes `abc'\''def`.
25+
#
26+
#
27+
# PARAMETERS
28+
# value: Value to be escaped for usage within single quotes.
29+
#
30+
# OUTPUT
31+
# stdout: The escaped value.
32+
#
33+
# SAMPLE
34+
# safeValue=$( esc_for_sq_psst "$value" )
35+
#
36+
esc_for_sq_psst()
37+
(
38+
value=$1
39+
40+
func="esc_for_sq_psst"
41+
assert_argc_psst "$func" 1 $#
42+
43+
printf "%s" "$value" | sed "s/'/'\\\''/g"
44+
)

test/basic/bin/test_esc.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env sh
2+
3+
# Exit at once if a command fails and the error isn't caught
4+
set -e
5+
6+
if [ -z "$INCLUDE_PSST" ]
7+
then
8+
cmdBase=$( dirname "$0" )
9+
INCLUDE_PSST="$cmdBase/../../../lib/psst"
10+
fi
11+
12+
# shellcheck source=../../../lib/psst/basic.inc.sh
13+
. "$INCLUDE_PSST/basic.inc.sh"
14+
15+
# =============================================================================
16+
# esc_for_sq
17+
18+
# Must accept exactly one argument
19+
set +e
20+
( esc_for_sq_psst 2>/dev/null )
21+
[ $? = 127 ] || test_fail_psst $LINENO
22+
23+
( esc_for_sq_psst 1 2 2>/dev/null )
24+
[ $? = 127 ] || test_fail_psst $LINENO
25+
set -e
26+
27+
28+
# Test the encoding
29+
[ "$( esc_for_sq_psst "" )" = "" ] || test_fail_psst $LINENO
30+
[ "$( esc_for_sq_psst "abc" )" = "abc" ] || test_fail_psst $LINENO
31+
[ "$( esc_for_sq_psst "ab'cd" )" = "ab'\\''cd" ] || test_fail_psst $LINENO
32+
[ "$( esc_for_sq_psst "'a'b" )" = "'\\''a'\\''b" ] || test_fail_psst $LINENO

test/basic/bin/test_ifs.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ set +e
3131
set -e
3232

3333

34-
3534
# Test push, pop
3635
oldIFS="$IFS"
3736

0 commit comments

Comments
 (0)