! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types alien.data alien.syntax generic
assocs kernel kernel.private math io.ports sequences strings
-sbufs threads unix unix.ffi vectors io.buffers io.backend
+sbufs threads unix unix.ffi unix.stat vectors io.buffers io.backend
io.encodings math.parser continuations system libc namespaces
make io.timeouts io.encodings.utf8 destructors
destructors.private accessors summary combinators locals
-unix.time unix.types fry io.backend.unix.multiplexers ;
+unix.time unix.types fry io.backend.unix.multiplexers
+classes.struct hints ;
QUALIFIED: io
IN: io.backend.unix
} case
[ fd>> swap ] dip [ lseek ] unix-system-call drop ;
+M: unix can-seek-handle? ( handle -- ? )
+ fd>> SEEK_CUR 0 lseek -1 = not ;
+
+M: unix handle-length ( handle -- n/f )
+ fd>> \ stat <struct> [ fstat -1 = not ] keep
+ swap [ st_size>> ] [ drop f ] if ;
+
SYMBOL: +retry+ ! just try the operation again without blocking
SYMBOL: +input+
SYMBOL: +output+
'[ handle>> _ wait-for-fd ] with-timeout ;
! Some general stuff
-CONSTANT: file-mode OCT: 0666
+CONSTANT: file-mode 0o0666
! Readers
: (refill) ( port -- n )
[ (io-error) ]
} cond ;
+HINTS: M\ fd refill
+ { buffered-port fd } ;
+
M: unix (wait-to-read) ( port -- )
dup
dup handle>> dup check-disposed refill dup
size-read-fd <fd> init-fd <input-port> >>size
data-read-fd <fd> >>data ;
+SYMBOL: dispatch-signal-hook
+
+dispatch-signal-hook [ [ drop ] ] initialize
+
+: signal-pipe-fd ( -- n )
+ OBJ-SIGNAL-PIPE special-object ; inline
+
+: signal-pipe-loop ( port -- )
+ '[
+ int heap-size _ io:stream-read
+ dup [ int deref dispatch-signal-hook get call( x -- ) ] when*
+ ] loop ;
+
+: start-signal-pipe-thread ( -- )
+ signal-pipe-fd [
+ <fd> init-fd <input-port>
+ '[ _ signal-pipe-loop ] "Signals" spawn drop
+ ] when* ;
+
M: unix init-stdio
<stdin> <input-port>
1 <fd> <output-port>