1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators concurrency.combinators destructors
4 fry grouping io io.backend io.ports io.streams.duplex kernel
5 math namespaces quotations sequences simple-tokenizer splitting
6 strings system vocabs ;
13 [ in>> &dispose drop ]
14 [ out>> &dispose drop ] bi
17 HOOK: (pipe) io-backend ( -- pipe )
19 : <pipe> ( encoding -- stream )
23 [ in>> <input-port> ] [ out>> <output-port> ] bi
24 ] dip <encoder-duplex>
29 : ?reader ( handle/f -- stream )
30 [ <input-port> &dispose ] [ input-stream get ] if* ;
32 : ?writer ( handle/f -- stream )
33 [ <output-port> &dispose ] [ output-stream get ] if* ;
35 GENERIC: run-pipeline-element ( input-fd output-fd obj -- result )
37 M: callable run-pipeline-element
39 [ [ ?reader ] [ ?writer ] bi* ] dip
40 '[ _ call( -- result ) ] with-streams*
43 GENERIC: <pipes> ( obj -- pipes )
47 [ (pipe) |dispose ] replicate
48 T{ pipe } [ prefix ] [ suffix ] bi
53 [ { } ] [ length 1 - <pipes> ] if-empty ;
55 : pipeline-args ( seq -- args )
56 dup string? [ tokenize { "|" } split ] when ;
60 : run-pipeline ( seq -- results )
61 pipeline-args [ <pipes> ] keep
63 [ [ first in>> ] [ second out>> ] bi ] dip
68 { [ os unix? ] [ "io.pipes.unix" require ] }
69 { [ os windows? ] [ "io.pipes.windows" require ] }