unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Filter IO through an external command
@ 2012-02-23 14:26 Tristan Colgate
  2012-02-23 17:16 ` Thien-Thi Nguyen
  2012-02-23 17:16 ` Thien-Thi Nguyen
  0 siblings, 2 replies; 6+ messages in thread
From: Tristan Colgate @ 2012-02-23 14:26 UTC (permalink / raw)
  To: guile-user

Hi,

  I just got this working and thought people mind find it useful. It
passes IO through an external command (I'm using it to get svg output
from dot to include inline in html). I tried using
open-input-output-pipe but hit issues.

  It could probably handle errors nicely. Comments very welcome:

(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)
>

-- 
Tristan Colgate-McFarlane
----
  "You can get all your daily vitamins from 52 pints of guiness, and a
glass of milk"



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-02-27  6:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-23 14:26 Filter IO through an external command Tristan Colgate
2012-02-23 17:16 ` Thien-Thi Nguyen
2012-02-23 17:16 ` Thien-Thi Nguyen
2012-02-24  3:31   ` Daniel Hartwig
2012-02-24 10:25     ` Andy Wingo
2012-02-27  6:33       ` Daniel Hartwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).