| 1 |
;;; |
| 2 |
;;; text.sh - Shell-compatible text processing |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; This module provides utilities that emulate text manipulation operations |
| 35 |
;; seen in typical Unix shells. It is handy to port shell scripts to |
| 36 |
;; Gauche. |
| 37 |
|
| 38 |
(define-module text.sh |
| 39 |
(use srfi.13) |
| 40 |
(use util.match) |
| 41 |
(export shell-escape-string shell-tokenize-string |
| 42 |
shell-match shell-case) |
| 43 |
) |
| 44 |
(select-module text.sh) |
| 45 |
|
| 46 |
;; |
| 47 |
;; Argument processing |
| 48 |
;; |
| 49 |
|
| 50 |
;; API |
| 51 |
(define (shell-escape-string str :optional (flavor |
| 52 |
(cond-expand |
| 53 |
[gauche.os.windows 'windows] |
| 54 |
[else 'posix]))) |
| 55 |
(ecase flavor |
| 56 |
;; Windows flavors is supported in src/libsys.scm. See the comment in it. |
| 57 |
[(windows) |
| 58 |
(%sys-escape-windows-command-line str #f)] |
| 59 |
[(windows-batchfile) |
| 60 |
(%sys-escape-windows-command-line str #t)] |
| 61 |
[(posix) |
| 62 |
;; We follow standard unix shell convention: if STR contains special |
| 63 |
;; chars, we quote the entire STR by single-quotes. If STR contains |
| 64 |
;; a single quote, we replace it with '"'"'. |
| 65 |
(cond [(string-null? str) "''"] |
| 66 |
[(string-index str #[\s\\\"\'`*?$<>!\[\](){}]) |
| 67 |
(string-append "'" (regexp-replace-all #/'/ str "'\"'\"'") "'")] |
| 68 |
[else str])])) |
| 69 |
|
| 70 |
;; API |
| 71 |
;; Aux procedure to tokenize command-line string into command name and |
| 72 |
;; arglist. We don't deal with shell metacharacters; just recognize |
| 73 |
;; quotes. For the time being, we reject any unquoted shell metacharacters. |
| 74 |
;; POSIX shell syntax: |
| 75 |
;; http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 76 |
;; About wicked MS shell syntax, see the comment of |
| 77 |
;; %sys-escape-windows-command-line in src/libsys.scm. |
| 78 |
;; NB: We don't expect STR to be too long, so just recurse. |
| 79 |
(define (shell-tokenize-string str :optional (flavor |
| 80 |
(cond-expand |
| 81 |
[gauche.os.windows 'windows] |
| 82 |
[else 'posix]))) |
| 83 |
(define (skip-ws) |
| 84 |
(let loop ([c (peek-char)]) |
| 85 |
(when (and (char? c) (char-whitespace? c)) |
| 86 |
(begin (read-char) (loop (peek-char)))))) |
| 87 |
(define (err fmt . args) |
| 88 |
(apply errorf #"Bad shell syntax - ~fmt" args)) |
| 89 |
(define (err-meta c) |
| 90 |
(err "shell metacharacter ~s not allowed, in ~s" c str)) |
| 91 |
(define (err-backslash) |
| 92 |
(err "stray backslash in ~s" str)) |
| 93 |
|
| 94 |
(define (tokenize-posix) |
| 95 |
(with-input-from-string str |
| 96 |
(^[] |
| 97 |
(define (read-next) |
| 98 |
(let1 c (read-char) |
| 99 |
(cond [(eof-object? c) '()] |
| 100 |
[(char-whitespace? c) (skip-ws) '()] |
| 101 |
[(eqv? c #\') (read-sq)] |
| 102 |
[(eqv? c #\") (read-dq)] |
| 103 |
[(eqv? c #\\) (read-escaped)] |
| 104 |
[(#[|&\;<>()$`] c) (err-meta c)] |
| 105 |
[else (read-unquoted c)]))) |
| 106 |
(define (read-escaped) |
| 107 |
(let1 c (read-char) |
| 108 |
(if (eof-object? c) |
| 109 |
(err-backslash) |
| 110 |
(read-unquoted c)))) |
| 111 |
(define (read-unquoted c0) |
| 112 |
(let1 c (peek-char) |
| 113 |
(cond [(eof-object? c) (list c0)] |
| 114 |
[(char-whitespace? c) (skip-ws) (list c0)] |
| 115 |
[(eqv? c #\') (read-char) (cons c0 (read-sq))] |
| 116 |
[(eqv? c #\") (read-char) (cons c0 (read-dq))] |
| 117 |
[(eqv? c #\\) (read-char) (cons c0 (read-escaped))] |
| 118 |
[(#[|&\;<>()$`] c) (err-meta c)] |
| 119 |
[else (read-char) (cons c0 (read-unquoted c))]))) |
| 120 |
(define (read-sq) |
| 121 |
(let loop ([c (read-char)] [cs '()]) |
| 122 |
(cond [(eof-object? c) (err "unclosed single quote: ~s" str)] |
| 123 |
[(eqv? c #\') (reverse cs (read-next))] |
| 124 |
[else (loop (read-char) (cons c cs))]))) |
| 125 |
(define (read-dq) |
| 126 |
(let loop ([c (read-char)] [cs '()]) |
| 127 |
(cond [(eof-object? c) (err "unclosed double quote: ~s" str)] |
| 128 |
[(eqv? c #\") (reverse cs (read-next))] |
| 129 |
[(eqv? c #\\) (let1 c (read-char) ; section 2.2.3 |
| 130 |
(cond [(eof-object? c) (err-backslash)] |
| 131 |
[(#[$`\"\\\n] c) |
| 132 |
(loop (read-char) (cons c cs))] |
| 133 |
[else |
| 134 |
(loop (read-char) (cons* c #\\ cs))]))] |
| 135 |
[(#[$`] c) (err-meta c)] |
| 136 |
[else (loop (read-char) (cons c cs))]))) |
| 137 |
|
| 138 |
;; body |
| 139 |
(skip-ws) |
| 140 |
(let loop ([words '()]) |
| 141 |
(if (eof-object? (peek-char)) |
| 142 |
(reverse words) |
| 143 |
(loop (cons (list->string (read-next)) words))))))) |
| 144 |
|
| 145 |
(define (tokenize-windows-cmd) |
| 146 |
(with-input-from-string str |
| 147 |
(^[] |
| 148 |
(define (read-next) |
| 149 |
(let1 c (read-char) |
| 150 |
(cond [(eof-object? c) '()] |
| 151 |
[(char-whitespace? c) (skip-ws) '()] |
| 152 |
[(eqv? c #\") (read-quoted)] |
| 153 |
[else (read-unquoted c)]))) |
| 154 |
(define (read-unquoted c) |
| 155 |
(let loop ([c c] [cs '()]) |
| 156 |
(cond [(eof-object? c) (reverse cs)] |
| 157 |
[(char-whitespace? c) (skip-ws) (reverse cs)] |
| 158 |
[(eqv? c #\\) (let1 cs (read-backslash cs) |
| 159 |
(loop (read-char) cs))] |
| 160 |
[(eqv? c #\") (reverse cs (read-quoted))] |
| 161 |
[else (loop (read-char) (cons c cs))]))) |
| 162 |
(define (read-quoted) |
| 163 |
(let loop ([c (read-char)] [cs '()]) |
| 164 |
(cond [(eof-object? c) (reverse cs)] ;implicitly closed |
| 165 |
[(eqv? c #\\) (let1 cs (read-backslash cs) |
| 166 |
(loop (read-char) cs))] |
| 167 |
[(eqv? c #\") (read-after-closing cs)] |
| 168 |
[else (loop (read-char) (cons c cs))]))) |
| 169 |
(define (read-backslash cs) |
| 170 |
(let loop ([n 1] [cs cs]) |
| 171 |
(let1 c (peek-char) |
| 172 |
(cond [(eqv? c #\\) (read-char) (loop (+ n 1) cs)] |
| 173 |
[(eqv? c #\") |
| 174 |
(if (even? n) |
| 175 |
(append (make-list (ash n -1) #\\) cs) |
| 176 |
(cons (read-char) |
| 177 |
(append (make-list (ash n -1) #\\) cs)))] |
| 178 |
[else (append (make-list n #\\) cs)])))) |
| 179 |
(define (read-after-closing cs) |
| 180 |
(let1 c (peek-char) |
| 181 |
(if (eqv? c #\") ; this is treated as literal dq |
| 182 |
(begin (read-char) |
| 183 |
(reverse (cons c cs) (read-next))) |
| 184 |
(reverse cs (read-next))))) |
| 185 |
;; body |
| 186 |
(skip-ws) |
| 187 |
(let loop ([words '()]) |
| 188 |
(if (eof-object? (peek-char)) |
| 189 |
(reverse words) |
| 190 |
(loop (cons (list->string (read-next)) words))))))) |
| 191 |
|
| 192 |
(ecase flavor |
| 193 |
[(windows) (tokenize-windows-cmd)] |
| 194 |
[(posix) (tokenize-posix)])) |
| 195 |
|
| 196 |
;; Shell-style string pattern match |
| 197 |
;; |
| 198 |
;; PATTERN+ := PATTERN | (PATTERN PATTERN ...) |
| 199 |
;; |
| 200 |
;; PATTERN may contain the following special characters: |
| 201 |
;; |
| 202 |
;; * Matches zero or more characters. |
| 203 |
;; ? Matches any single character. |
| 204 |
;; [...] Character class. It is interpreted as #[...] in Gauche, |
| 205 |
;; except that when '!' appears first, it is understood as |
| 206 |
;; complement operator, the same as '^'. |
| 207 |
;; POSIX character class [:classs:] is supported, but not |
| 208 |
;; equivalence class [=c=], nor collating symbol [.symbol.] |
| 209 |
;; are supported yet. |
| 210 |
;; \c same as c. |
| 211 |
|
| 212 |
|
| 213 |
;; API |
| 214 |
;; (shell-match pattern-expr string) => boolean |
| 215 |
;; pattern-expr must evaluate to a string or a list of strings, |
| 216 |
(define (shell-match pattern+ str) |
| 217 |
(cond |
| 218 |
[(string? pattern+) |
| 219 |
(boolean ((glob-component->regexp pattern+ :mode :shell) str))] |
| 220 |
[(of-type? pattern+ (<List> <string>)) |
| 221 |
(boolean (any (^p ((glob-component->regexp p :mode :shell) str)) pattern+))] |
| 222 |
[else (error "Invalid pattern for shell-match:" pattern+)])) |
| 223 |
|
| 224 |
;; API |
| 225 |
;; Like shell's 'case' |
| 226 |
(define-syntax shell-case |
| 227 |
(syntax-rules () |
| 228 |
[(_ str clause ...) |
| 229 |
(let ((tmp str)) |
| 230 |
(%shell-case tmp clause ...))])) |
| 231 |
|
| 232 |
(define-syntax %shell-case |
| 233 |
(syntax-rules (else) |
| 234 |
[(_ str) (undefined)] |
| 235 |
[(_ str (else expr ...)) (begin expr ...)] |
| 236 |
[(_ str (pattern+ expr ...) . rest) |
| 237 |
(if (shell-match 'pattern+ str) |
| 238 |
(begin expr ...) |
| 239 |
(%shell-case str . rest))])) |