Shell quote zero-length strings.

This commit is contained in:
Ray Miller 2025-01-04 12:13:40 +00:00
parent 61524f3e79
commit 428c6ed4a5
Signed by: ray
GPG key ID: 043F786C4CD681B8

View file

@ -27,15 +27,16 @@
(define (needs-escape? s)
(irregex-search unsafe-characters s))
(define (squash-quotes m)
(let ((n (/ (- (irregex-match-end-index m)
(irregex-match-start-index m))
4)))
(list->string (append
'(#\' #\")
(make-list n #\')
'(#\" #\')))))
(define (escape s)
(define (squash-quotes m)
(let ((n (/ (- (irregex-match-end-index m)
(irregex-match-start-index m))
4)))
(list->string (append
'(#\' #\")
(make-list n #\')
'(#\" #\')))))
(chain s
;; ' -> '\''
(irregex-replace/all (irregex "'") _ "'\\''")
@ -50,4 +51,7 @@
(define (shell-quote-string s)
"Quote strings for passing through the shell"
(if (needs-escape? s) (escape s) s))
(cond
((zero? (string-length s)) "''")
((needs-escape? s) (escape s))
(else s)))