Skip to content

Commit

Permalink
fix!: Use %b in generated printf statements (vs %s)
Browse files Browse the repository at this point in the history
%b is a posix printf standard, so this should just be an improvement, correcting behavior for shells like DASH.

Adds new function, escape_string, which creates a string compatible with %b
  • Loading branch information
TekWizely committed Sep 2, 2024
1 parent 087c2d9 commit a9eb321
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 11 deletions.
3 changes: 2 additions & 1 deletion bash-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ function escape_string {
# Escape '\' first since we'll be adding more later
local e="${!1//$'\\'/\\}"
# Some man pages mention \0NNN but in practice it seems \NNN is also works.
e="${e//\\/\\\\}"
e="${e//\'/\\0047}"
e="${e//$'\a'/\\a}"
e="${e//$'\b'/\\b}"
Expand Down Expand Up @@ -575,7 +576,7 @@ function process_tags() {
# Assume next character is TEXT - extract and process remainder
#
elif [[ "${line}" =~ (.)(.*) ]]; then
# echo "# DEFAULT: Assuming first char is TEXT: $(declare -p line)"
# echo "# DEFAULT: Assuming first char is TEXT: $(declare -p line)" >&2
quoted="${BASH_REMATCH[1]}"
escape_string quoted
#printf -v quoted "%q" "${BASH_REMATCH[1]}"
Expand Down
5 changes: 5 additions & 0 deletions test/tpl/text_with_dollar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
printf "%b\n" '$Hello'
printf "%b\n" '<$Hello'
printf "%b%b\n" '<' '%$Hello'
printf "%b%b\n" '<' '<$Hello'
printf "%b\n" '%$Hello'
5 changes: 5 additions & 0 deletions test/tpl/text_with_dollar.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$Hello
<$Hello
<%$Hello
<<$Hello
%$Hello
5 changes: 5 additions & 0 deletions test/tpl/text_with_dollar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$Hello
<$Hello
<%$Hello
<<$Hello
%$Hello
10 changes: 5 additions & 5 deletions test/tpl/text_with_escapes.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
printf "%b\n" '\tHello'
printf "%b\n" '<\tHello'
printf "%b%b\n" '<' '%\tHello'
printf "%b%b\n" '<' '<\tHello'
printf "%b\n" '%\tHello'
printf "%b\n" '\\tHello'
printf "%b\n" '<\\tHello'
printf "%b%b\n" '<' '%\\tHello'
printf "%b%b\n" '<' '<\\tHello'
printf "%b\n" '%\\tHello'
10 changes: 5 additions & 5 deletions test/tpl/text_with_escapes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Hello
< Hello
<% Hello
<< Hello
% Hello
\tHello
<\tHello
<%\tHello
<<\tHello
%\tHello
12 changes: 12 additions & 0 deletions test/tpl/text_with_quotes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
printf "%b\n" '\0047Hello'
printf "%b\n" '<\0047Hello'
printf "%b%b\n" '<' '%\0047Hello'
printf "%b%b\n" '<' '<\0047Hello'
printf "%b\n" '%\0047Hello'
printf "%b\n" '"Hello'
printf "%b\n" '<"Hello'
printf "%b%b\n" '<' '%"Hello'
printf "%b%b\n" '<' '<"Hello'
printf "%b\n" '%"Hello'
printf "%b\n" 'Hello \0047World\0047'
printf "%b\n" 'Hello "World"'
12 changes: 12 additions & 0 deletions test/tpl/text_with_quotes.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'Hello
<'Hello
<%'Hello
<<'Hello
%'Hello
"Hello
<"Hello
<%"Hello
<<"Hello
%"Hello
Hello 'World'
Hello "World"
12 changes: 12 additions & 0 deletions test/tpl/text_with_quotes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'Hello
<'Hello
<%'Hello
<<'Hello
%'Hello
"Hello
<"Hello
<%"Hello
<<"Hello
%"Hello
Hello 'World'
Hello "World"
5 changes: 5 additions & 0 deletions test/tpl/text_with_tabs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
printf "%b\n" '\tHello'
printf "%b\n" '<\tHello'
printf "%b%b\n" '<' '%\tHello'
printf "%b%b\n" '<' '<\tHello'
echo Hello
5 changes: 5 additions & 0 deletions test/tpl/text_with_tabs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello
< Hello
<% Hello
<< Hello
% echo Hello
5 changes: 5 additions & 0 deletions test/tpl/text_with_tabs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello
< Hello
<% Hello
<< Hello
Hello

0 comments on commit a9eb321

Please sign in to comment.