From e603ebb6269dedb73aaf779afecf3625e56ac861 Mon Sep 17 00:00:00 2001 From: mainzelm Date: Mon, 9 Feb 2004 12:16:02 +0000 Subject: [PATCH] + Preseve quotation of [],? and * while processing braces. + Unquote constant patterns before file existence check. --- scsh/glob.scm | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/scsh/glob.scm b/scsh/glob.scm index 6358757..d6e1ade 100644 --- a/scsh/glob.scm +++ b/scsh/glob.scm @@ -69,7 +69,7 @@ (cond ((string=? pat "") (values '() #t)) ((constant-glob? pat) - (values (cons pat '()) #f)) ; Don't check filesys. + (values (cons (glob-unquote pat) '()) #f)) ; Don't check filesys. (else (let* ((dots? (char=? #\. (string-ref pat 0))) ; Match dot files? (candidates (maybe-directory-files fname dots?)) @@ -249,12 +249,16 @@ (parse-comma-sequence pattern (+ i 1)) (lp i (cross-append prefixes pats) '())))) ((#\\) - (let ((i (+ i 1))) - (if (= i pattern-len) + (let ((next-i (+ i 1))) + (if (= next-i pattern-len) (error "Dangling escape char in glob pattern" pattern) - (lp (+ i 1) - prefixes - (cons (string-ref pattern i) pat))))) + (if (memv (string-ref pattern next-i) '(#\{, #\,, #\},#\\)) + (lp (+ next-i 1) + prefixes + (cons (string-ref pattern next-i) pat)) + (lp (+ i 1) + prefixes + (cons (string-ref pattern i) pat)))))) ((#\,) (if comma-terminates? (values (finish prefixes pat) i) @@ -313,4 +317,21 @@ (cons #\\ result) result)))))) +(define (glob-unquote string) + (let ((len (string-length string))) + (let lp ((i 0) + (result '())) + (if (= i len) + (list->string (reverse result)) + (let* ((c (string-ref string i))) + (if (char=? c #\\) + (let ((next-i (+ i 1))) + (if (= next-i len) + (error "Dangling escape char in glob pattern" string) + (let ((quoted (string-ref string next-i))) + (lp (+ i 2) + (cons quoted result))))) + (lp (+ i 1) + (cons c result)))))))) +