Skip to content

Instantly share code, notes, and snippets.

@shirok
Created January 20, 2015 06:38
Show Gist options
  • Save shirok/f32b4af2081aa9309570 to your computer and use it in GitHub Desktop.
Save shirok/f32b4af2081aa9309570 to your computer and use it in GitHub Desktop.
shiro@scherzo:~/src$ gosh ./tt.scm a b c gosh ./t.scm
shiro@scherzo:~/src$ cat a
out
out
out
out
out
out
out
out
out
out
shiro@scherzo:~/src$ cat b
err
err
err
err
err
err
err
err
err
err
shiro@scherzo:~/src$ cat c
oerurt
out
err
ouet
rr
oute
rr
oeut
rr
oeut
rr
oerurt
oute
rr
oeut
rr
oute
rr
shiro@scherzo:~/src$
(use gauche.process)
(use gauche.threads)
(use util.match)
(define (tee in out1 out2)
(generator-for-each (^c (write-char c out1)
(write-char c out2))
(cut read-char in)))
(define (main args)
(match-let1 (file-a file-b file-c . command) (cdr args)
(let ([a (open-output-file file-a)]
[b (open-output-file file-b)]
[c (open-output-file file-c)]
[p (run-process command :output :pipe :error :pipe :wait #f)])
(unwind-protect
(let ([cout (process-output p)]
[cerr (process-error p)])
(set! (port-buffering cout) :none)
(set! (port-buffering cerr) :none)
(let ([t0 (thread-start! (make-thread (^[] (tee cout a c))))]
[t1 (thread-start! (make-thread (^[] (tee cerr b c))))])
(thread-join! t0)
(thread-join! t1)))
(close-output-port a)
(close-output-port b)
(close-output-port c)
(process-kill p)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment