Skip to content

Commit

Permalink
[quote/lighttpdquot] New function to quote strings for pasting in a l…
Browse files Browse the repository at this point in the history
…ighttpd config file
  • Loading branch information
sideeffect42 committed Dec 30, 2023
1 parent b9376d6 commit 3695223
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/quote/lighttpdquot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Quotes all arguments into a single lighttpd config-style string.
#
# Usage:
# - lighttpdquot foo bar baz
# - lighttpdquot 'foo bar baz'

sed -e ':a' -e '$!N' -e '$!b a' -e 's/\n/\\n/g' -e 's/"/\\"/g' -e 's/\r/\\r/g' -e 's/ /\\t/g' -e '$s/^/"/' -e '$s/$/"/' <<EOF
$*
EOF
101 changes: 101 additions & 0 deletions spec/quote/lighttpdquot.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Describe 'quote/lighttpdquot'
EnableSandbox
AllowExternalCommand sed

EnableLeakDetector

SetupCommandFromFile lighttpdquot lib/quote/lighttpdquot.sh

It 'quotes an empty string'
When run command lighttpdquot ''

The status should be success
The stdout should equal '""'
The stderr should equal ''
End

It "quotes 'word'"
When run command lighttpdquot 'word'

The status should be success
The stdout should equal '"word"'
The stderr should equal ''
End

It "quotes 'hello world'"
When run command lighttpdquot 'hello world'

The status should be success
The stdout should equal '"hello world"'
The stderr should equal ''
End

It "leaves alone single quotes in a a string"
When run command lighttpdquot "It's a string"

The status should be success
The stdout should equal "\"It's a string\""
The stderr should equal ''
End

It "escapes double quotes in a string"
When run command lighttpdquot 'This is "a value"'

The status should be success
The stdout should equal '"This is \"a value\""'
The stderr should equal ''
End

It "escapes newlines"
mlstr=$(echo 'line 1'; echo 'line 2'; echo 'line 3')

When run command lighttpdquot "${mlstr}"

The status should be success
The stdout should equal '"line 1\nline 2\nline 3"'
The stderr should equal ''
End

It "escapes carriage returns"
# use >1 CRs to check if the sed command uses /g
crstr=$(printf 'before\rmid\rafter')

When run command lighttpdquot "${crstr}"

The status should be success
The stdout should equal '"before\rmid\rafter"'
The stderr should equal ''
End

It "escapes tabs"
# use >1 tabs to check if the sed command uses /g
tsstr=$(printf 'col 1\tcol 2\tcol 3')

When run command lighttpdquot "${tsstr}"

The status should be success
The stdout should equal '"col 1\tcol 2\tcol 3"'
The stderr should equal ''
End

Context # long strings
Parameters:dynamic
l=2048

while test $((l*=2)) -le 100000
do
%data $l
done
End

It "quotes a $1 bytes long string"
randstr=$(random_string $(($1)) '[:alnum:]')

When run command lighttpdquot "${randstr}"

The status should be success
The stdout should equal "\"${randstr}\""
The stderr should equal ''
End
End
End

0 comments on commit 3695223

Please sign in to comment.