utils: Change `substitute' and `substitute*' to work with several regexps.
* guix/build/utils.scm (substitute): Change to accept a `pattern+procs' parameter. Iterate over it. (substitute*): Adjust accordingly. * distro/base.scm (guile-1.8): Adjust accordingly.master
parent
03f9609ad9
commit
4fa697e932
|
@ -132,11 +132,11 @@ code.")
|
||||||
;; `libguile-readline.so' & co. are in the
|
;; `libguile-readline.so' & co. are in the
|
||||||
;; loader's search path.
|
;; loader's search path.
|
||||||
(substitute* "libguile/dynl.c"
|
(substitute* "libguile/dynl.c"
|
||||||
("lt_dlinit.*$" match)
|
(("lt_dlinit.*$" match)
|
||||||
(format #f
|
(format #f
|
||||||
" ~a~% lt_dladdsearchdir(\"~a/lib\");~%"
|
" ~a~% lt_dladdsearchdir(\"~a/lib\");~%"
|
||||||
match
|
match
|
||||||
(assoc-ref outputs "out"))))
|
(assoc-ref outputs "out")))))
|
||||||
%standard-phases)))
|
%standard-phases)))
|
||||||
(inputs `(("patch/snarf"
|
(inputs `(("patch/snarf"
|
||||||
,(search-path %load-path "distro/guile-1.8-cpp-4.5.patch"))
|
,(search-path %load-path "distro/guile-1.8-cpp-4.5.patch"))
|
||||||
|
|
|
@ -147,12 +147,17 @@ An error is raised when no such pair exists."
|
||||||
;;; Text substitution (aka. sed).
|
;;; Text substitution (aka. sed).
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
(define (substitute file pattern match-proc)
|
(define (substitute file pattern+procs)
|
||||||
"For each line of FILE that matches PATTERN, a regexp, call (MATCH-PROC
|
"PATTERN+PROCS is a list of regexp/two-argument procedure. For each line
|
||||||
MATCH OUTPUT-PORT)."
|
of FILE, and for each PATTERN that it matches, call the corresponding PROC
|
||||||
(let* ((regexp (if (regexp? pattern)
|
as (PROC MATCH OUTPUT-PORT)."
|
||||||
pattern
|
(let* ((rx+proc (map (match-lambda
|
||||||
(make-regexp pattern regexp/extended)))
|
(((? regexp? pattern) . proc)
|
||||||
|
(cons pattern proc))
|
||||||
|
((pattern . proc)
|
||||||
|
(cons (make-regexp pattern regexp/extended)
|
||||||
|
proc)))
|
||||||
|
pattern+procs))
|
||||||
(template (string-append file ".XXXXXX"))
|
(template (string-append file ".XXXXXX"))
|
||||||
(out (mkstemp! template)))
|
(out (mkstemp! template)))
|
||||||
(with-throw-handler #t
|
(with-throw-handler #t
|
||||||
|
@ -163,13 +168,16 @@ MATCH OUTPUT-PORT)."
|
||||||
(if (eof-object? line)
|
(if (eof-object? line)
|
||||||
#t
|
#t
|
||||||
(begin
|
(begin
|
||||||
|
(for-each (match-lambda
|
||||||
|
((regexp . proc)
|
||||||
(cond ((regexp-exec regexp line)
|
(cond ((regexp-exec regexp line)
|
||||||
=>
|
=>
|
||||||
(lambda (m)
|
(lambda (m)
|
||||||
(match-proc m out)))
|
(proc m out)))
|
||||||
(else
|
(else
|
||||||
(display line out)
|
(display line out)
|
||||||
(newline out)))
|
(newline out)))))
|
||||||
|
rx+proc)
|
||||||
(loop (read-line in)))))))
|
(loop (read-line in)))))))
|
||||||
(close out)
|
(close out)
|
||||||
(rename-file template file))
|
(rename-file template file))
|
||||||
|
@ -190,27 +198,32 @@ MATCH OUTPUT-PORT)."
|
||||||
((let-matches index match () body ...)
|
((let-matches index match () body ...)
|
||||||
(begin body ...))))
|
(begin body ...))))
|
||||||
|
|
||||||
(define-syntax-rule (substitute* file (regexp whole-match match ...)
|
(define-syntax-rule (substitute* file
|
||||||
body ...)
|
((regexp match-var ...) body ...)
|
||||||
|
...)
|
||||||
"Substitute REGEXP in FILE by the string returned by BODY. BODY is
|
"Substitute REGEXP in FILE by the string returned by BODY. BODY is
|
||||||
evaluated with each MATCH-VAR bound to the corresponding positional regexp
|
evaluated with each MATCH-VAR bound to the corresponding positional regexp
|
||||||
sub-expression. For example:
|
sub-expression. For example:
|
||||||
|
|
||||||
(substitute* file (\"foo([a-z]+)bar(.*)$\" all letters end)
|
(substitute* file
|
||||||
(string-append \"baz\" letters end))
|
((\"hello\")
|
||||||
|
\"good morning\\n\")
|
||||||
|
((\"foo([a-z]+)bar(.*)$\" all letters end)
|
||||||
|
(string-append \"baz\" letter end)))
|
||||||
|
|
||||||
Here, anytime a line of FILE matches the regexp, ALL is bound to the complete
|
Here, anytime a line of FILE contains \"hello\", it is replaced by \"good
|
||||||
match, LETTERS is bound to the first sub-expression, and END is bound to the
|
morning\". Anytime a line of FILE matches the second regexp, ALL is bound to
|
||||||
last one. Alternatively, given that `all' is not used, one can write:
|
the complete match, LETTERS is bound to the first sub-expression, and END is
|
||||||
|
bound to the last one.
|
||||||
|
|
||||||
(substitute* file (\"foo([a-z]+)bar(.*)$\" _ letters end)
|
When one of the MATCH-VAR is `_', no variable is bound to the corresponding
|
||||||
(string-append \"baz\" letter end))
|
match substring."
|
||||||
|
(substitute file
|
||||||
"
|
(list (cons regexp
|
||||||
(substitute file regexp
|
|
||||||
(lambda (m p)
|
(lambda (m p)
|
||||||
(let-matches 0 m (whole-match match ...)
|
(let-matches 0 m (match-var ...)
|
||||||
(display (begin body ...) p)))))
|
(display (begin body ...) p))))
|
||||||
|
...)))
|
||||||
|
|
||||||
|
|
||||||
;;; Local Variables:
|
;;; Local Variables:
|
||||||
|
|
Reference in New Issue