]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/io/backend/unix/unix.factor
use radix literals
[factor.git] / basis / io / backend / unix / unix.factor
index 502b135872d71e6fd8fbabb9048068a475046996..60890b6fc09c1222ffdefe1dc59bfb03d36534fc 100755 (executable)
@@ -2,11 +2,12 @@
 ! 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
 
@@ -57,6 +58,13 @@ M: unix seek-handle ( n seek-type handle -- )
     } 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+
@@ -78,7 +86,7 @@ M: io-timeout summary drop "I/O operation timed out" ;
     '[ handle>> _ wait-for-fd ] with-timeout ;
 
 ! Some general stuff
-CONSTANT: file-mode OCT: 0666
+CONSTANT: file-mode 0o0666
  
 ! Readers
 : (refill) ( port -- n )
@@ -99,6 +107,9 @@ M: fd refill
         [ (io-error) ]
     } cond ;
 
+HINTS: M\ fd refill
+    { buffered-port fd } ;
+
 M: unix (wait-to-read) ( port -- )
     dup
     dup handle>> dup check-disposed refill dup
@@ -179,6 +190,25 @@ M: stdin cancel-operation
         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>