1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel kernel.private namespaces make io io.encodings
4 sequences math generic threads.private classes io.backend
5 io.files continuations destructors byte-arrays accessors ;
8 TUPLE: c-writer handle disposed ;
10 : <c-writer> ( handle -- stream ) f c-writer boa ;
12 M: c-writer stream-write1
16 M: c-writer stream-write
20 M: c-writer stream-flush
27 TUPLE: c-reader handle disposed ;
29 : <c-reader> ( handle -- stream ) f c-reader boa ;
31 M: c-reader stream-read
35 M: c-reader stream-read-partial
38 M: c-reader stream-read1
42 : read-until-loop ( stream delim -- ch )
43 over stream-read1 dup [
44 dup pick memq? [ 2nip ] [ , read-until-loop ] if
49 M: c-reader stream-read-until
51 [ swap read-until-loop ] B{ } make swap
52 over empty? over not and [ 2drop f f ] when ;
57 M: c-io-backend init-io ;
59 : stdin-handle ( -- alien ) 11 getenv ;
60 : stdout-handle ( -- alien ) 12 getenv ;
61 : stderr-handle ( -- alien ) 61 getenv ;
63 : init-c-stdio ( -- stdin stdout stderr )
64 stdin-handle <c-reader>
65 stdout-handle <c-writer>
66 stderr-handle <c-writer> ;
68 M: c-io-backend (init-stdio) init-c-stdio t ;
70 M: c-io-backend io-multiplex 60 60 * 1000 * 1000 * or (sleep) ;
72 M: c-io-backend (file-reader)
73 "rb" fopen <c-reader> ;
75 M: c-io-backend (file-writer)
76 "wb" fopen <c-writer> ;
78 M: c-io-backend (file-appender)
79 "ab" fopen <c-writer> ;
82 #! A word which directly calls primitives. It is used to
83 #! print stuff from contexts where the I/O system would
84 #! otherwise not work (tools.deploy.shaker, the I/O
85 #! multiplexer thread).
86 "\n" append >byte-array
88 stdout-handle fflush ;