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

Commit a009ab3

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Add new escape functions
Also rename the existing one and adapt code that has been using it
1 parent 92a70dc commit a009ab3

File tree

3 files changed

+129
-14
lines changed

3 files changed

+129
-14
lines changed

lib/psst/basic/esc.inc.sh

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:esc:"
1616

1717
##
1818
# SUBPROCESS
19-
# esc_for_sq_psst <value>
19+
# esc_squotes_psst <value>
2020
#
2121
# SUMMARY
2222
# Escapes `value` so it can be safely used within single quotes. This
@@ -32,14 +32,77 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:esc:"
3232
# stdout: The escaped value.
3333
#
3434
# SAMPLE
35-
# safeValue=$( esc_for_sq_psst "$value" )
35+
# safeValue=$( esc_squotes_psst "$value" )
36+
# execLater="someCommand '$safeValue'"
37+
# exec "$execLater"
3638
#
37-
esc_for_sq_psst()
39+
esc_squotes_psst()
3840
(
39-
func='esc_for_sq_psst'
41+
func='esc_squotes_psst'
4042
assert_argc_psst "$func" 1 $#
4143

42-
value=$1
44+
printf '%s' "$1" | sed "s/'/'\\\''/g"
45+
)
4346

44-
printf "%s" "$value" | sed "s/'/'\\\''/g"
47+
48+
49+
##
50+
# SUBPROCESS
51+
# esc_cstring_psst <value>
52+
#
53+
# SUMMARY
54+
# Escapes `value` so it can be safely used within a C-string. A C-string
55+
# allows all values except for double quote, backslash, and line break.
56+
#
57+
# PARAMETERS
58+
# value: Value to be escaped for usage within a C-string.
59+
#
60+
# OUTPUT
61+
# stdout: The escaped value.
62+
#
63+
# SAMPLE
64+
# safeValue=$( esc_cstring_psst "$value" )
65+
#
66+
#
67+
esc_cstring_psst()
68+
(
69+
func='esc_cstring_psst'
70+
assert_argc_psst "$func" 1 $#
71+
72+
# shellcheck disable=SC2016
73+
awkProg='
74+
NR > 1 { printf "\\n" }
75+
{ printf "%s", $0 }
76+
'
77+
78+
printf '%s' "$1" | sed 's/\(["\\]\)/\\\1/g' | awk "$awkProg"
79+
)
80+
81+
82+
83+
##
84+
# SUBPROCESS
85+
# esc_printf_psst <value>
86+
#
87+
# SUMMARY
88+
# Escapes `value` so it can be safely used within a printf string. A printf
89+
# allows all values except for double quote, backslash, and line break.
90+
# Additionally % has a special meaing and must be escaped as %%.
91+
#
92+
# PARAMETERS
93+
# value: Value to be escaped for usage within a printf string
94+
#
95+
# OUTPUT
96+
# stdout: The escaped value.
97+
#
98+
# SAMPLE
99+
# safeValue=$( esc_cstring_psst "$value" )
100+
#
101+
#
102+
esc_printf_psst()
103+
(
104+
func='esc_printf_psst'
105+
assert_argc_psst "$func" 1 $#
106+
107+
esc_cstring_psst "$1" | sed 's/%/%%/g'
45108
)

lib/psst/basic/proc.inc.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ proc_start_psst()
9595

9696
for _arg_psst in "$@"
9797
do
98-
_evalCMD_proc_psst="$_evalCMD_proc_psst '$( esc_for_sq_psst "$_arg_psst" )'"
98+
_safeArg_proc_psst="$( esc_squotes_psst "$_arg_psst" )"
99+
_evalCMD_proc_psst="$_evalCMD_proc_psst '$_safeArg_proc_psst'"
99100
done
100101
unset _arg_psst
102+
unset _safeArg_proc_psst
101103

102104
printf "%s" "$_evalCMD_proc_psst" >"$_tmpdir_proc_psst/eval"
103105

test/basic/bin/test_esc.sh

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,71 @@ fi
1414
# shellcheck source=../../../lib/psst/basic.inc.sh
1515
. "$INCLUDE_PSST/basic.inc.sh"
1616

17+
18+
# =============================================================================
19+
# esc_squotes
20+
21+
# Must accept exactly one argument
22+
set +e
23+
( esc_squotes_psst 2>/dev/null )
24+
[ $? = 127 ] || test_fail_psst $LINENO
25+
26+
( esc_squotes_psst 1 2 2>/dev/null )
27+
[ $? = 127 ] || test_fail_psst $LINENO
28+
set -e
29+
30+
31+
# Test the encoding
32+
[ "$( esc_squotes_psst '' )" = '' ] || test_fail_psst $LINENO
33+
[ "$( esc_squotes_psst "abc" )" = "abc" ] || test_fail_psst $LINENO
34+
[ "$( esc_squotes_psst "ab'cd" )" = "ab'\\''cd" ] || test_fail_psst $LINENO
35+
[ "$( esc_squotes_psst "'a'b" )" = "'\\''a'\\''b" ] || test_fail_psst $LINENO
36+
37+
38+
1739
# =============================================================================
18-
# esc_for_sq
40+
# esc_cstring_psst
1941

2042
# Must accept exactly one argument
2143
set +e
22-
( esc_for_sq_psst 2>/dev/null )
44+
( esc_cstring_psst 2>/dev/null )
2345
[ $? = 127 ] || test_fail_psst $LINENO
2446

25-
( esc_for_sq_psst 1 2 2>/dev/null )
47+
( esc_cstring_psst 1 2 2>/dev/null )
2648
[ $? = 127 ] || test_fail_psst $LINENO
2749
set -e
2850

2951

3052
# Test the encoding
31-
[ "$( esc_for_sq_psst '' )" = '' ] || test_fail_psst $LINENO
32-
[ "$( esc_for_sq_psst "abc" )" = "abc" ] || test_fail_psst $LINENO
33-
[ "$( esc_for_sq_psst "ab'cd" )" = "ab'\\''cd" ] || test_fail_psst $LINENO
34-
[ "$( esc_for_sq_psst "'a'b" )" = "'\\''a'\\''b" ] || test_fail_psst $LINENO
53+
[ "$( esc_cstring_psst '' )" = '' ] || test_fail_psst $LINENO
54+
[ "$( esc_cstring_psst 'abc' )" = 'abc' ] || test_fail_psst $LINENO
55+
[ "$( esc_cstring_psst 'a"b' )" = 'a\"b' ] || test_fail_psst $LINENO
56+
[ "$( esc_cstring_psst 'a\b' )" = 'a\\b' ] || test_fail_psst $LINENO
57+
58+
[ "$( esc_cstring_psst "a${NL_CHAR_PSST}b" )" = 'a\nb' ] \
59+
|| test_fail_psst $LINENO
60+
61+
62+
63+
# =============================================================================
64+
# esc_printf_psst
65+
66+
# Must accept exactly one argument
67+
set +e
68+
( esc_printf_psst 2>/dev/null )
69+
[ $? = 127 ] || test_fail_psst $LINENO
70+
71+
( esc_printf_psst 1 2 2>/dev/null )
72+
[ $? = 127 ] || test_fail_psst $LINENO
73+
set -e
74+
75+
76+
# Test the encoding
77+
[ "$( esc_printf_psst '' )" = '' ] || test_fail_psst $LINENO
78+
[ "$( esc_printf_psst 'abc' )" = 'abc' ] || test_fail_psst $LINENO
79+
[ "$( esc_printf_psst 'a"b' )" = 'a\"b' ] || test_fail_psst $LINENO
80+
[ "$( esc_printf_psst 'a\b' )" = 'a\\b' ] || test_fail_psst $LINENO
81+
[ "$( esc_printf_psst 'a%b' )" = 'a%%b' ] || test_fail_psst $LINENO
82+
83+
[ "$( esc_printf_psst "a${NL_CHAR_PSST}b" )" = 'a\nb' ] \
84+
|| test_fail_psst $LINENO

0 commit comments

Comments
 (0)