Created
February 23, 2012 14:15
-
-
Save tcolgate/1893027 to your computer and use it in GitHub Desktop.
guile filter output via an external command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (use-modules (ice-9 rdelim)) | |
| (define (filter inproc outproc cmd . args) | |
| "filter the output of inproc via cmd to outproc through an external cmd" | |
| (let* ((read-pipe (pipe)) | |
| (read-in (car read-pipe)) | |
| (read-out (cdr read-pipe)) | |
| (write-pipe (pipe)) | |
| (write-in (car write-pipe)) | |
| (write-out (cdr write-pipe)) | |
| (child-pid (primitive-fork))) | |
| (if (= child-pid 0) | |
| (begin | |
| (close-output-port read-out) | |
| (close-input-port write-in) | |
| (dup2 (fileno read-in) 0) | |
| (dup2 (fileno write-out) 1) | |
| (apply execlp (append (list cmd) (append (list cmd) args)))) | |
| (begin | |
| (close-input-port read-in) | |
| (close-output-port write-out) | |
| (parameterize ((current-output-port read-out)) | |
| (inproc)) | |
| (close-output-port read-out) | |
| (parameterize ((current-input-port write-in)) | |
| (outproc)) | |
| (close-input-port write-in) | |
| (waitpid child-pid 0))))) | |
| ; To use | |
| > (filter | |
| (lambda() | |
| (display "Hello World!")(newline)) | |
| (lambda() | |
| (let loop ((line (read-line))) | |
| (if (not (eof-object? line)) | |
| (begin | |
| (write-line line) | |
| (loop (read-line)))))) | |
| "sed" "s/World/Planet/") | |
| Hello Planet! | |
| $4 = (3892 . 0) | |
| > |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment