From 4e8da1f8295d657afa12aacf9bc77fe2aeba4fcf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 27 Apr 2005 05:40:09 +0000 Subject: [PATCH] Unix I/O fixes --- TODO.FACTOR.txt | 49 +++++++++++++++------------------- library/generic/builtin.factor | 2 +- library/io/buffer.factor | 6 +++-- library/tools/telnetd.factor | 12 +++++---- library/unix/io.factor | 44 +++++++++++++++++++++--------- 5 files changed, 66 insertions(+), 47 deletions(-) diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index d225492d4a..69cb61a14e 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -1,17 +1,27 @@ -+ plugin: - -- if external factor is down, don't add tons of random shit to the dictionary -- extract word: if selection empty, move caret to new word after -- introduce quotation command -- set 'end' of artifacts/assets accurately - -+ ui: +0.74: - faster layout - faster repaint +- linux? bsd? words +- forgotten words not removed from cross-reference +- get all-tests to run with -no-compile +- review errno +- mac os x ffi +- linux i/o +- implement fcopy +- fix httpd +- fix jedit plugin +- if external factor is down, don't add tons of random shit to the dictionary +- scalar * matrix, vector * matrix, matrix * vector need to work +- turning vectors into row and column matrices +- make-matrix is slow and ugly +- move 2repeat somewhere else + ++ ui: + - console with presentations - ui browser -- auto-updating inspector +- auto-updating inspector, mirrors abstraction - word preview for parsing words - mouse enter onto overlapping with interior, but not child, gadget - menu dragging @@ -25,7 +35,7 @@ - ffi unicode strings: null char security hole - utf16 string boxing - value type structs -- out parameters +- out parameter cleanup - bitfields in C structs - SDL_Rect** type - setting struct members that are not * @@ -35,7 +45,6 @@ + compiler: - alien primitives need a more general input type -- linux? bsd? words - [ [ dup call ] dup call ] infer hangs - more accurate types for various words - declarations @@ -69,8 +78,7 @@ + kernel: -- forgotten words not removed from cross-reference -- get all-tests to run with -no-compile +- rename prettyprint to pprint - powerpc has weird callstack residue - .factor-rc loading errors are not reported properly - instances: do not use make-list @@ -80,30 +88,24 @@ - code walker & exceptions - string sub-primitives - clean up metaclasses -- condition system with restarts -- nicer way to combine two paths - vectors: ensure its ok with bignum indices - code gc - generational gc - doc comments of generics -- proper ordering for classes - M: object should not inhibit delegation + i/o: -- review errno +- reader syntax for arrays, byte arrays, displaced aliens - separate words for writing characters and strings - perhaps: GENERIC: set-style ( style stream -- ) GENERIC: stream-write GENERIC: stream-write-char -- mac os x ffi - stream server can hang because of exception handler limitations - better i/o scheduler - add a socket timeout - renumber types appopriately -- linux? freebsd? words, linux i/o stuff -- implement fcopy - unify unparse and prettyprint + nice to have libraries: @@ -121,10 +123,3 @@ - virtual hosts - keep alive - -+ matrix lib: - -- scalar * matrix, vector * matrix, matrix * vector need to work -- turning vectors into row and column matrices -- make-matrix is slow and ugly -- move 2repeat somewhere else diff --git a/library/generic/builtin.factor b/library/generic/builtin.factor index 5ce3151259..087ed9e560 100644 --- a/library/generic/builtin.factor +++ b/library/generic/builtin.factor @@ -41,7 +41,7 @@ builtin [ 2drop t ] "class<" set-word-prop ] ifte ; : builtin-class ( symbol type# slotspec -- ) - >r 2dup builtins get nth r> + >r 2dup builtins get set-nth r> >r swap dup intern-symbol 2dup builtin-predicate diff --git a/library/io/buffer.factor b/library/io/buffer.factor index 6e2a93e54b..fc93b1c0ab 100644 --- a/library/io/buffer.factor +++ b/library/io/buffer.factor @@ -88,9 +88,11 @@ C: buffer ( size -- buffer ) : buffer-end ( buffer -- int ) dup buffer-ptr swap buffer-fill + ; +: buffer-peek ( buffer -- char ) + buffer@ 0 alien-unsigned-1 ; + : buffer-pop ( buffer -- char ) - [ buffer@ 0 alien-unsigned-1 1 ] keep - buffer-consume ; + [ buffer-peek 1 ] keep buffer-consume ; : buffer-append ( buffer buffer -- ) #! Append first buffer to second buffer. diff --git a/library/tools/telnetd.factor b/library/tools/telnetd.factor index 6a04afb680..1deffd0e26 100644 --- a/library/tools/telnetd.factor +++ b/library/tools/telnetd.factor @@ -14,11 +14,13 @@ threads parser ; [ accept telnet-connection ] keep telnetd-loop ; : telnetd ( port -- ) - [ - telnetd-loop - ] [ - swap stream-close rethrow - ] catch ; + [ + [ + telnetd-loop + ] [ + swap stream-close rethrow + ] catch + ] with-logging ; IN: shells diff --git a/library/unix/io.factor b/library/unix/io.factor index 7aaa7f6644..848ccbf1ee 100644 --- a/library/unix/io.factor +++ b/library/unix/io.factor @@ -171,13 +171,14 @@ C: reader ( handle -- reader ) drop ] ifte t swap set-reader-ready? ; -: read-step ( port -- ) - >port< - tuck dup buffer-end swap buffer-capacity read - dup 0 >= [ swap n>buffer ] [ drop postpone-error ] ifte ; - -: refill ( reader -- ) - dup buffer-length 0 = [ read-step ] [ drop ] ifte ; +: refill ( port -- ) + dup buffer-length 0 = [ + >port< + tuck dup buffer-end swap buffer-capacity read + dup 0 >= [ swap n>buffer ] [ drop postpone-error ] ifte + ] [ + drop + ] ifte ; : eof? ( buffer -- ? ) buffer-fill 0 = ; @@ -204,22 +205,41 @@ M: read-line-task io-task-events ( task -- events ) M: reader stream-readln ( stream -- line ) dup wait-to-read-line read-fin ; +: trailing-cr ( reader -- ) + #! Handle a corner case. If the previous request was a line + #! read and the line ends with \r\n, the reader stopped + #! reading at \r and set the reader-cr flag to true. But we + #! must ignore the \n. + dup buffer-length 1 >= over reader-cr and [ + dup buffer-peek CHAR: \n = [ + 1 swap buffer-consume + ] [ + drop + ] ifte + ] [ + drop + ] ifte ; + ! Reading character counts -: read-count-step ( count reader -- ? ) +: read-loop ( count reader -- ? ) + dup trailing-cr dup reader-line -rot >r over length - ( remaining) r> - 2dup buffer-fill <= [ + 2dup buffer-length <= [ buffer> nappend t ] [ buffer>> nip nappend f ] ifte ; +: read-step ( count reader -- ? ) + [ read-loop dup ] keep set-reader-ready? ; + : can-read-count? ( count reader -- ? ) dup pending-error 2dup init-reader 2dup reader-line length <= [ t swap set-reader-ready? drop t ] [ - read-count-step + read-step ] ifte ; TUPLE: read-task count ; @@ -234,7 +254,7 @@ M: read-task do-io-task ( task -- ? ) >read-task< dup refill dup eof? [ nip reader-eof t ] [ - [ read-count-step dup ] keep set-reader-ready? + read-step ] ifte ; M: read-task io-task-events ( task -- events ) @@ -333,7 +353,7 @@ M: writer stream-close ( stream -- ) ! Copying from a reader to a writer : can-copy? ( from -- ? ) - dup eof? [ read-step ] [ drop t ] ifte ; + dup eof? [ refill ] [ drop t ] ifte ; : copy-from-task ( from to -- ? ) over can-copy? [