- alignment of gadgets inside their bounding boxes needs thought\r
- faster completion\r
- ppc register decls\r
-- rename f* words to stream-*\r
\r
- port leak\r
- references primitive\r
TUPLE: html-stream delegate ;
-M: html-stream fwrite-attr ( str style stream -- )
+M: html-stream stream-write-attr ( str style stream -- )
wrapper-stream-scope [
[
[
": " split1 dup [ cons swons ] [ 2drop ] ifte ;
: (read-header) ( alist -- alist )
- read dup
+ read-line dup
f-or-"" [ drop ] [ header-line (read-header) ] ifte ;
: read-header ( -- alist )
] when ;
: read-post-request ( header -- alist )
- content-length dup [ read# query>alist ] when ;
+ content-length dup [ read query>alist ] when ;
: log-user-agent ( alist -- )
"User-Agent" swap assoc* [
: httpd-client ( socket -- )
[
[
- stdio get log-client read [ parse-request ] when*
+ stdio get log-client read-line [ parse-request ] when*
] with-stream
] try ;
<server> "http-server" set [
httpd-loop
] [
- "http-server" get fclose rethrow
+ "http-server" get stream-close rethrow
] catch ;
: httpd ( port -- )
"quit-prohibited" get [
quit-prohibited
] [
- "http-server" get fclose
+ "http-server" get stream-close
] ifte ;
TUPLE: ansi-stream delegate ;
-M: ansi-stream fwrite-attr ( string style stream -- )
+M: ansi-stream stream-write-attr ( string style stream -- )
>r [ default-style ] unless* ansi-attr-string r>
- ansi-stream-delegate fwrite ;
+ ansi-stream-delegate stream-write ;
IN: shells
: blocking-read-line ( port -- line )
dup wait-to-read-line read-line-fd-8 dup [ sbuf>str ] when ;
-: fill-fd# ( count port -- )
+: fill-fd ( count port -- )
[ add-read-count-io-task (yield) ] callcc0 2drop ;
-: wait-to-read# ( count port -- )
- 2dup can-read-count? [ 2drop ] [ fill-fd# ] ifte ;
+: wait-to-read ( count port -- )
+ 2dup can-read-count? [ 2drop ] [ fill-fd ] ifte ;
-: blocking-read# ( count port -- str )
- 2dup wait-to-read# read-count-fd-8 dup [ sbuf>str ] when ;
+: blocking-read ( count port -- str )
+ 2dup wait-to-read read-count-fd-8 dup [ sbuf>str ] when ;
: wait-to-accept ( socket -- )
[ add-accept-io-task (yield) ] callcc0 drop ;
USE: unparser
: log ( msg -- )
- "log" get dup [ tuck fprint fflush ] [ 2drop ] ifte ;
+ "log" get dup [ tuck stream-print stream-flush ] [ 2drop ] ifte ;
: log-error ( error -- )
"Error: " swap cat2 log ;
TUPLE: server port ;
GENERIC: accept
-M: server fclose ( stream -- )
+M: server stream-close ( stream -- )
server-port close-port ;
C: server ( port -- stream )
#! Starts listening on localhost:port. Returns a stream that
- #! you can close with fclose, and accept connections from
+ #! you can close with stream-close, and accept connections from
#! with accept. No other stream operations are supported.
[ >r server-socket r> set-server-port ] keep ;
TUPLE: client-stream delegate host ;
C: client-stream ( host port in out -- stream )
- #! fflush yields until connection is established.
+ #! stream-flush yields until connection is established.
[ >r <fd-stream> r> set-client-stream-delegate ] keep
[ >r ":" swap unparse cat3 r> set-client-stream-host ] keep
- dup fflush ;
+ dup stream-flush ;
: <client> ( host port -- stream )
2dup client-socket <client-stream> ;
[ uncons >r over " " r> cat3 cons ] map nip ;
! A style is an alist whose key/value pairs hold
-! significance to the 'fwrite-attr' word when applied to a
+! significance to the 'stream-write-attr' word when applied to a
! stream that supports attributed string output.
: (style) ( name -- style ) "styles" get hash ;
SYMBOL: stdio
-: flush ( -- ) stdio get fflush ;
-: read ( -- string ) stdio get freadln ;
-: read1 ( count -- string ) stdio get fread1 ;
-: read# ( count -- string ) stdio get fread# ;
-: write ( string -- ) stdio get fwrite ;
-: write-attr ( string style -- ) stdio get fwrite-attr ;
-: print ( string -- ) stdio get fprint ;
+: flush ( -- ) stdio get stream-flush ;
+: read-line ( -- string ) stdio get stream-readln ;
+: read1 ( -- char ) stdio get stream-read1 ;
+: read ( count -- string ) stdio get stream-read ;
+: write ( string -- ) stdio get stream-write ;
+: write-attr ( string style -- ) stdio get stream-write-attr ;
+: print ( string -- ) stdio get stream-print ;
: terpri ( -- ) "\n" write ;
-: close ( -- ) stdio get fclose ;
+: close ( -- ) stdio get stream-close ;
: write-icon ( resource -- )
#! Write an icon. Eg, /library/icons/File.png
TUPLE: stdio-stream delegate ;
-M: stdio-stream fauto-flush ( -- )
- stdio-stream-delegate fflush ;
+M: stdio-stream stream-auto-flush ( -- )
+ stdio-stream-delegate stream-flush ;
-M: stdio-stream fclose ( -- )
+M: stdio-stream stream-close ( -- )
drop ;
TUPLE: fd-stream in out ;
-M: fd-stream fwrite-attr ( str style stream -- )
+M: fd-stream stream-write-attr ( str style stream -- )
nip fd-stream-out blocking-write ;
-M: fd-stream freadln ( stream -- str )
+M: fd-stream stream-readln ( stream -- str )
fd-stream-in dup [ blocking-read-line ] when ;
-M: fd-stream fread# ( count stream -- str )
- fd-stream-in dup [ blocking-read# ] [ nip ] ifte ;
+M: fd-stream stream-read ( count stream -- str )
+ fd-stream-in dup [ blocking-read ] [ nip ] ifte ;
-M: fd-stream fflush ( stream -- )
+M: fd-stream stream-flush ( stream -- )
fd-stream-out [ blocking-flush ] when* ;
-M: fd-stream fauto-flush ( stream -- )
+M: fd-stream stream-auto-flush ( stream -- )
drop ;
-M: fd-stream fclose ( stream -- )
+M: fd-stream stream-close ( stream -- )
dup fd-stream-out [ dup blocking-flush close-port ] when*
fd-stream-in [ close-port ] when* ;
: fcopy ( from to -- )
#! Copy the contents of the fd-stream 'from' to the
#! fd-stream 'to'.
- [ 2dup (fcopy) ] [ -rot fclose fclose rethrow ] catch ;
+ [ 2dup (fcopy) ] [ -rot stream-close stream-close rethrow ] catch ;
: resource-path ( -- path )
"resource-path" get [ "." ] unless* ;
IN: streams
USING: errors kernel namespaces strings generic lists ;
-GENERIC: fflush ( stream -- )
-GENERIC: fauto-flush ( stream -- )
-GENERIC: freadln ( stream -- string )
-GENERIC: fread# ( count stream -- string )
-GENERIC: fwrite-attr ( string style stream -- )
-GENERIC: fclose ( stream -- )
-
-: fread1 ( stream -- char/f )
- 1 swap fread#
+GENERIC: stream-flush ( stream -- )
+GENERIC: stream-auto-flush ( stream -- )
+GENERIC: stream-readln ( stream -- string )
+GENERIC: stream-read ( count stream -- string )
+GENERIC: stream-write-attr ( string style stream -- )
+GENERIC: stream-close ( stream -- )
+
+: stream-read1 ( stream -- char/f )
+ 1 swap stream-read
dup f-or-"" [ drop f ] [ 0 swap str-nth ] ifte ;
-: fwrite ( string stream -- )
- f swap fwrite-attr ;
+: stream-write ( string stream -- )
+ f swap stream-write-attr ;
-: fprint ( string stream -- )
- [ fwrite ] keep
- [ "\n" swap fwrite ] keep
- fauto-flush ;
+: stream-print ( string stream -- )
+ [ stream-write ] keep
+ [ "\n" swap stream-write ] keep
+ stream-auto-flush ;
! A stream that builds a string of all text written to it.
TUPLE: string-output buf ;
-M: string-output fwrite-attr ( string style stream -- )
+M: string-output stream-write-attr ( string style stream -- )
nip string-output-buf sbuf-append ;
-M: string-output fclose ( stream -- ) drop ;
-M: string-output fflush ( stream -- ) drop ;
-M: string-output fauto-flush ( stream -- ) drop ;
+M: string-output stream-close ( stream -- ) drop ;
+M: string-output stream-flush ( stream -- ) drop ;
+M: string-output stream-auto-flush ( stream -- ) drop ;
: stream>str ( stream -- string )
#! Returns the string written to the given string output
win32-io-thread ;
TUPLE: null-stream ;
-M: null-stream fflush drop ;
-M: null-stream fauto-flush drop ;
-M: null-stream fread# 2drop f ;
-M: null-stream freadln drop f ;
-M: null-stream fwrite-attr 3drop ;
-M: null-stream fclose drop ;
+M: null-stream stream-flush drop ;
+M: null-stream stream-auto-flush drop ;
+M: null-stream stream-read 2drop f ;
+M: null-stream stream-readln drop f ;
+M: null-stream stream-write-attr 3drop ;
+M: null-stream stream-close drop ;
: win32-init-stdio ( -- )
INVALID_HANDLE_VALUE NULL NULL 1 CreateIoCompletionPort
socket set
] extend over set-win32-server-this ;
-M: win32-server fclose ( server -- )
+M: win32-server stream-close ( server -- )
win32-server-this [ socket get CloseHandle drop ] bind ;
M: win32-server accept ( server -- client )
] ifte
] ifte ;
-M: win32-stream fwrite-attr ( str style stream -- )
+M: win32-stream stream-write-attr ( str style stream -- )
win32-stream-this nip [ do-write ] bind ;
-M: win32-stream freadln ( stream -- str )
+M: win32-stream stream-readln ( stream -- str )
win32-stream-this [ 80 <sbuf> do-read-line ] bind ;
-M: win32-stream fread# ( count stream -- str )
+M: win32-stream stream-read ( count stream -- str )
win32-stream-this [ dup <sbuf> swap do-read-count ] bind ;
-M: win32-stream fflush ( stream -- )
+M: win32-stream stream-flush ( stream -- )
win32-stream-this [ maybe-flush-output ] bind ;
-M: win32-stream fauto-flush ( stream -- )
+M: win32-stream stream-auto-flush ( stream -- )
drop ;
-M: win32-stream fclose ( stream -- )
+M: win32-stream stream-close ( stream -- )
win32-stream-this [
maybe-flush-output
handle get CloseHandle drop
! parse-stream
: next-line ( -- str )
- "parse-stream" get freadln
+ "parse-stream" get stream-readln
"line-number" [ 1 + ] change ;
: (read-lines) ( quot -- )
swap [
"parse-stream" set 0 "line-number" set (read-lines)
] [
- "parse-stream" get fclose rethrow
+ "parse-stream" get stream-close rethrow
] catch ;
: file-vocabs ( -- )
[ 4 ] [ "/library/test/io/no-trailing-eol.factor" run-resource ] unit-test
: lines-test ( stream -- line1 line2 )
- [ read read ] with-stream ;
+ [ read-line read-line ] with-stream ;
[
"This is a line."
"This is a line.\rThis is another line.\r"
] [
"/library/test/io/mac-os-eol.txt" <resource-stream>
- [ 500 read# ] with-stream
+ [ 500 read ] with-stream
] unit-test
[
! Make sure we use correct to_c_string form when writing
[ ] [ "\0" write ] unit-test
-[ -1 read# ] unit-test-fails
+[ -1 read ] unit-test-fails
--- /dev/null
+IN: scratchpad
+USE: kernel
+USE: math
+USE: memory
+USE: generic
+USE: lists
+
+num-types [
+ [
+ instances [
+ class drop
+ ] each
+ ] keep
+] repeat
"alien"
"line-editor"
"gadgets"
+ "memory"
] [
test
] each
dup str-length write-big-endian-32 write flush ;
: read-packet ( -- string )
- read-big-endian-32 read# ;
+ read-big-endian-32 read ;
: wire-server ( -- )
#! Repeatedly read jEdit requests and execute them. Return
TUPLE: jedit-stream delegate ;
-M: jedit-stream freadln ( stream -- str )
+M: jedit-stream stream-readln ( stream -- str )
wrapper-stream-scope
- [ CHAR: r write flush read-big-endian-32 read# ] bind ;
+ [ CHAR: r write flush read-big-endian-32 read ] bind ;
-M: jedit-stream fwrite-attr ( str style stream -- )
+M: jedit-stream stream-write-attr ( str style stream -- )
wrapper-stream-scope
[ [ default-style ] unless* jedit-write-attr ] bind ;
-M: jedit-stream fflush ( stream -- )
+M: jedit-stream stream-flush ( stream -- )
wrapper-stream-scope
[ CHAR: f write flush ] bind ;
: jedit-server-info ( -- port auth )
jedit-server-file <file-reader> [
- read drop
- read parse-number
- read parse-number
+ read-line drop
+ read-line parse-number
+ read-line parse-number
] with-stream ;
: make-jedit-request ( files params -- code )
: (read-multiline) ( quot depth -- quot ? )
#! Flag indicates EOF.
- >r read dup [
+ >r read-line dup [
(parse) depth r> dup >r <= [
( we're done ) r> drop t
] [
: telnetd ( port -- )
[
- <server> [ telnetd-loop ] [ swap fclose rethrow ] catch
+ <server> [ telnetd-loop ] [ swap stream-close rethrow ] catch
] with-logging ;
IN: shells
! The console stream
! Restoring this continuation with a string on the stack returns
-! to the caller of freadln.
+! to the caller of stream-readln.
SYMBOL: input-continuation
TUPLE: console-stream console redraw-continuation ;
-M: console-stream fflush ( stream -- )
- fauto-flush ;
+M: console-stream stream-flush ( stream -- )
+ stream-auto-flush ;
-M: console-stream fauto-flush ( stream -- )
+M: console-stream stream-auto-flush ( stream -- )
console-stream-console [ redraw-console on ] bind ;
-M: console-stream freadln ( stream -- line )
+M: console-stream stream-readln ( stream -- line )
[
swap [
console-stream-console
] ifte
] callcc1 nip ;
-M: console-stream fwrite-attr ( string style stream -- )
+M: console-stream stream-write-attr ( string style stream -- )
nip console-stream-console [ console-write ] bind ;
-M: console-stream fclose ( stream -- ) drop ;
+M: console-stream stream-close ( stream -- ) drop ;
! Event handling
SYMBOL: event