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

Commit f6363e2

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Fix list escaping
Properly deal with " and \ as a delimiter and within values to be removed/filtered
1 parent a009ab3 commit f6363e2

File tree

1 file changed

+73
-24
lines changed

1 file changed

+73
-24
lines changed

lib/psst/basic/list.inc.sh

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:list:"
1616
# shellcheck source=const.inc.sh
1717
. "$INCLUDE_PSST/basic/const.inc.sh"
1818

19+
# shellcheck source=esc.inc.sh
20+
. "$INCLUDE_PSST/basic/esc.inc.sh"
21+
1922
# shellcheck source=test.inc.sh
2023
. "$INCLUDE_PSST/basic/test.inc.sh"
2124

@@ -179,9 +182,12 @@ list_count_psst()
179182
|| assert_func_fail_psst "$func" \
180183
'List delimiter must be single character'
181184

182-
! [ "$del" = "$NL_CHAR_PSST" ] \
183-
|| assert_func_fail_psst "$func" \
185+
case $del in
186+
\\) del=\\\\ ;;
187+
\") del='\"' ;;
188+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
184189
'List delimiter must not be newline'
190+
esac
185191
fi
186192

187193
case $list in
@@ -236,9 +242,12 @@ list_first_psst()
236242
|| assert_func_fail_psst "$func" \
237243
'List delimiter must be single character'
238244

239-
! [ "$del" = "$NL_CHAR_PSST" ] \
240-
|| assert_func_fail_psst "$func" \
245+
case $del in
246+
\\) del=\\\\ ;;
247+
\") del='\"' ;;
248+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
241249
'List delimiter must not be newline'
250+
esac
242251
fi
243252

244253
case $list in
@@ -294,9 +303,12 @@ list_last_psst()
294303
|| assert_func_fail_psst "$func" \
295304
'List delimiter must be single character'
296305

297-
! [ "$del" = "$NL_CHAR_PSST" ] \
298-
|| assert_func_fail_psst "$func" \
306+
case $del in
307+
\\) del=\\\\ ;;
308+
\") del='\"' ;;
309+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
299310
'List delimiter must not be newline'
311+
esac
300312
fi
301313

302314
[ ${#list} -eq 0 ] && return 2
@@ -358,9 +370,12 @@ list_get_psst()
358370
|| assert_func_fail_psst "$func" \
359371
'List delimiter must be single character'
360372

361-
! [ "$del" = "$NL_CHAR_PSST" ] \
362-
|| assert_func_fail_psst "$func" \
373+
case $del in
374+
\\) del=\\\\ ;;
375+
\") del='\"' ;;
376+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
363377
'List delimiter must not be newline'
378+
esac
364379
fi
365380

366381
case $list in
@@ -422,9 +437,12 @@ list_remove_first_psst()
422437
|| assert_func_fail_psst "$func" \
423438
'List delimiter must be single character'
424439

425-
! [ "$del" = "$NL_CHAR_PSST" ] \
426-
|| assert_func_fail_psst "$func" \
440+
case $del in
441+
\\) del=\\\\ ;;
442+
\") del='\"' ;;
443+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
427444
'List delimiter must not be newline'
445+
esac
428446
fi
429447

430448
case $list in
@@ -482,9 +500,12 @@ list_remove_last_psst()
482500
|| assert_func_fail_psst "$func" \
483501
'List delimiter must be single character'
484502

485-
! [ "$del" = "$NL_CHAR_PSST" ] \
486-
|| assert_func_fail_psst "$func" \
503+
case $del in
504+
\\) del=\\\\ ;;
505+
\") del='\"' ;;
506+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
487507
'List delimiter must not be newline'
508+
esac
488509
fi
489510

490511
case $list in
@@ -547,9 +568,12 @@ list_remove_psst()
547568
|| assert_func_fail_psst "$func" \
548569
'List delimiter must be single character'
549570

550-
! [ "$del" = "$NL_CHAR_PSST" ] \
551-
|| assert_func_fail_psst "$func" \
571+
case $del in
572+
\\) del=\\\\ ;;
573+
\") del='\"' ;;
574+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
552575
'List delimiter must not be newline'
576+
esac
553577
fi
554578

555579
case $list in
@@ -617,9 +641,12 @@ list_remove_value_psst()
617641
|| assert_func_fail_psst "$func" \
618642
'List delimiter must be single character'
619643

620-
! [ "$del" = "$NL_CHAR_PSST" ] \
621-
|| assert_func_fail_psst "$func" \
644+
case $del in
645+
\\) del=\\\\ ;;
646+
\") del='\"' ;;
647+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
622648
'List delimiter must not be newline'
649+
esac
623650
fi
624651

625652
case $list in
@@ -628,6 +655,8 @@ list_remove_value_psst()
628655
'Argument "list" is not a valid list'
629656
esac
630657

658+
value=$( esc_cstring_psst "$value" )
659+
631660
# shellcheck disable=SC2016
632661
awkProg='
633662
BEGIN { RS = "'$del'"; FS = "" }
@@ -683,9 +712,12 @@ list_remove_all_values_psst()
683712
|| assert_func_fail_psst "$func" \
684713
'List delimiter must be single character'
685714

686-
! [ "$del" = "$NL_CHAR_PSST" ] \
687-
|| assert_func_fail_psst "$func" \
715+
case $del in
716+
\\) del=\\\\ ;;
717+
\") del='\"' ;;
718+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
688719
'List delimiter must not be newline'
720+
esac
689721
fi
690722

691723
case $list in
@@ -694,6 +726,8 @@ list_remove_all_values_psst()
694726
'Argument "list" is not a valid list'
695727
esac
696728

729+
value=$( esc_cstring_psst "$value" )
730+
697731
# shellcheck disable=SC2016
698732
awkProg='
699733
BEGIN { RS = "'$del'"; FS = "" }
@@ -746,9 +780,12 @@ list_select_psst()
746780
|| assert_func_fail_psst "$func" \
747781
'List delimiter must be single character'
748782

749-
! [ "$del" = "$NL_CHAR_PSST" ] \
750-
|| assert_func_fail_psst "$func" \
783+
case $del in
784+
\\) del=\\\\ ;;
785+
\") del='\"' ;;
786+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
751787
'List delimiter must not be newline'
788+
esac
752789
fi
753790

754791
case $list in
@@ -763,6 +800,8 @@ list_select_psst()
763800
'Argument "filter" is not a valid regex'
764801
esac
765802

803+
filter=$( esc_cstring_psst "$filter" )
804+
766805
# shellcheck disable=SC2016
767806
awkProg='
768807
BEGIN { RS = "'$del'"; FS = "" }
@@ -812,9 +851,12 @@ list_filter_psst()
812851
|| assert_func_fail_psst "$func" \
813852
'List delimiter must be single character'
814853

815-
! [ "$del" = "$NL_CHAR_PSST" ] \
816-
|| assert_func_fail_psst "$func" \
854+
case $del in
855+
\\) del=\\\\ ;;
856+
\") del='\"' ;;
857+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
817858
'List delimiter must not be newline'
859+
esac
818860
fi
819861

820862
case $list in
@@ -829,6 +871,8 @@ list_filter_psst()
829871
'Argument "filter" is not a valid regex'
830872
esac
831873

874+
filter=$( esc_cstring_psst "$filter" )
875+
832876
# shellcheck disable=SC2016
833877
awkProg='
834878
BEGIN { RS = "'$del'"; FS = "" }
@@ -877,9 +921,12 @@ list_find_psst()
877921
|| assert_func_fail_psst "$func" \
878922
'List delimiter must be single character'
879923

880-
! [ "$del" = "$NL_CHAR_PSST" ] \
881-
|| assert_func_fail_psst "$func" \
924+
case $del in
925+
\\) del=\\\\ ;;
926+
\") del='\"' ;;
927+
"$NL_CHAR_PSST") assert_func_fail_psst "$func" \
882928
'List delimiter must not be newline'
929+
esac
883930
fi
884931

885932
case $list in
@@ -894,6 +941,8 @@ list_find_psst()
894941
'Argument "filter" is not a valid regex'
895942
esac
896943

944+
filter=$( esc_cstring_psst "$filter" )
945+
897946
# shellcheck disable=SC2016
898947
awkProg='
899948
BEGIN { RS = "'$del'"; FS = "" }

0 commit comments

Comments
 (0)