From ce28a461bbd15e5a89c1625de0543ca94cf30dd7 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 29 Jun 2022 12:35:06 +0530 Subject: utils: Add call-with-output-pipe. * tissue/utils.scm (call-with-output-pipe): New function. --- tissue/utils.scm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tissue/utils.scm b/tissue/utils.scm index aed729b..2eccc4f 100644 --- a/tissue/utils.scm +++ b/tissue/utils.scm @@ -24,6 +24,7 @@ #:export (string-remove-prefix human-date-string call-with-current-directory + call-with-output-pipe get-line-dos-or-unix memoize-thunk)) @@ -59,6 +60,20 @@ directory after THUNK returns." thunk (cut chdir original-current-directory)))) +(define (call-with-output-pipe proc program . args) + "Execute PROGRAM ARGS ... in a subprocess with a pipe to it. Call PROC +with an output port to that pipe. Close the pipe once PROC exits, even +if it exits non-locally. Return the value returned by PROC." + (let ((port #f)) + (dynamic-wind + (cut set! port (apply open-pipe* OPEN_WRITE program args)) + (cut proc port) + (lambda () + (let ((return-value (status:exit-val (close-pipe port)))) + (unless (and return-value + (zero? return-value)) + (error "Invocation of program failed" (cons program args)))))))) + (define (get-line-dos-or-unix port) "Read line from PORT. This differs from `get-line' in (rnrs io ports) in that it also supports DOS line endings." -- cgit v1.2.3