1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.encodings io.backend io.ports io.streams.duplex
4 io splitting grouping sequences namespaces kernel
5 destructors math concurrency.combinators accessors fry
6 arrays continuations quotations system vocabs.loader combinators ;
11 M: pipe dispose ( pipe -- )
12 [ in>> dispose ] [ out>> dispose ] bi ;
14 HOOK: (pipe) io-backend ( -- pipe )
16 : <pipe> ( encoding -- stream )
20 [ in>> <input-port> ] [ out>> <output-port> ] bi
21 ] dip <encoder-duplex>
26 : ?reader ( handle/f -- stream )
27 [ <input-port> &dispose ] [ input-stream get ] if* ;
29 : ?writer ( handle/f -- stream )
30 [ <output-port> &dispose ] [ output-stream get ] if* ;
32 GENERIC: run-pipeline-element ( input-fd output-fd obj -- result )
34 M: callable run-pipeline-element
36 [ [ ?reader ] [ ?writer ] bi* ] dip
37 '[ _ call( -- result ) ] with-streams*
40 : <pipes> ( n -- pipes )
42 [ (pipe) |dispose ] replicate
43 T{ pipe } [ prefix ] [ suffix ] bi
49 : run-pipeline ( seq -- results )
50 [ length dup zero? [ drop { } ] [ 1 - <pipes> ] if ] keep
52 [ [ first in>> ] [ second out>> ] bi ] dip
57 { [ os unix? ] [ "io.pipes.unix" require ] }
58 { [ os winnt? ] [ "io.pipes.windows.nt" require ] }