+++ /dev/null
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-!
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-!
-! 1. Redistributions of source code must retain the above copyright notice,
-! this list of conditions and the following disclaimer.
-!
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-! this list of conditions and the following disclaimer in the documentation
-! and/or other materials provided with the distribution.
-!
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-IN: inferior
-USE: combinators
-USE: errors
-USE: listener
-USE: kernel
-USE: lists
-USE: logic
-USE: namespaces
-USE: parser
-USE: prettyprint
-USE: stack
-USE: stdio
-USE: streams
-USE: strings
-USE: presentation
-
-! The purpose of this library is to allow CFactor to be embedded
-! inside the Java Factor listener in jEdit.
-!
-! Eg, in Java Factor, you could evaluate this after fixing the
-! paths accordingly:
-!
-! : (inf
-! [
-! "/home/slava/Factor/f"
-! "/home/slava/Factor/factor.image"
-! "-no-ansi"
-! ] pipe inferior-client ;
-!
-! Details:
-!
-! Packets have the following form:
-! 1 byte -- type. CHAR: w: write, CHAR: r: read
-! 4 bytes -- for write only -- length of write request
-! remaining -- unparsed write request -- string then style
-
-! After a read line request, the server reads a response from
-! the client:
-! 4 bytes -- length. -1 means EOF
-! remaining -- input
-
-! All multi-byte integers are big endian signed.
-
-: inferior-server-read ( -- str )
- CHAR: r write flush read-big-endian-32 read# ;
-
-: inferior-server-write-attr ( str style -- )
- CHAR: w write
- [ swap . . ] with-string
- dup str-length write-big-endian-32
- write ;
-
-: inferior-server-flush ( -- )
- CHAR: f write flush ;
-
-: <inferior-server-stream> ( stream -- stream )
- <extend-stream> [
- ( -- str )
- [ inferior-server-read ] "freadln" set
- ( str -- )
- [
- default-style inferior-server-write-attr
- ] "fwrite" set
- ( str style -- )
- [ inferior-server-write-attr ] "fwrite-attr" set
- ( string -- )
- [
- "\n" cat2 default-style inferior-server-write-attr
- ] "fprint" set
- ( -- )
- [ inferior-server-flush ] "fflush" set
- ] extend ;
-
-: inferior-client-read ( stream -- ? )
- freadln dup [
- dup str-length write-big-endian-32 write flush t
- ] [
- drop 0 write-big-endian-32 flush f
- ] ifte ;
-
-: inferior-client-write ( stream -- ? )
- read-big-endian-32 read# dup [
- parse dup [
- uncons car rot fwrite-attr t
- ] [
- 2drop f
- ] ifte
- ] when ;
-
-: inferior-client-packet ( stream -- ? )
- #! Read from an inferior client socket and print attributed
- #! strings that were read to standard output.
- read1 [
- [ not ] [ 2drop f ( EOF ) ]
- [ CHAR: r = ] [ drop inferior-client-read ]
- [ CHAR: w = ] [ drop inferior-client-write ]
- [ CHAR: f = ] [ drop fflush t ]
- [ drop t ] [ "Invalid packet type: " swap cat2 throw ]
- ] cond ;
-
-: inferior-client-loop ( stream -- )
- #! The stream is the stream to write to.
- dup inferior-client-packet [
- inferior-client-loop
- ] [
- drop
- ] ifte ;
-
-: inferior-server ( -- )
- #! Execute this in the inferior Factor.
- terpri
- "inferior-ack" print flush
- "stdio" get <inferior-server-stream> "stdio" set ;
-
-: inferior-read-ack ( -- )
- read [
- "inferior-ack" = [ inferior-read-ack ] unless
- ] when* ;
-
-: inferior-client ( from -- )
- #! Execute this in the superior Factor, with a socket to
- #! the inferior Factor as a parameter.
- "stdio" get swap [
- "USE: inferior inferior-server" print flush
- inferior-read-ack
- inferior-client-loop
- ] with-stream ;
--- /dev/null
+! :folding=indent:collapseFolds=1:
+
+! $Id$
+!
+! Copyright (C) 2004 Slava Pestov.
+!
+! Redistribution and use in source and binary forms, with or without
+! modification, are permitted provided that the following conditions are met:
+!
+! 1. Redistributions of source code must retain the above copyright notice,
+! this list of conditions and the following disclaimer.
+!
+! 2. Redistributions in binary form must reproduce the above copyright notice,
+! this list of conditions and the following disclaimer in the documentation
+! and/or other materials provided with the distribution.
+!
+! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+IN: jedit
+USE: stdio
+USE: stack
+USE: strings
+USE: combinators
+USE: parser
+USE: namespaces
+USE: presentation
+USE: streams
+USE: prettyprint
+
+! Wire protocol for jEdit to evaluate Factor code.
+! Packets are of the form:
+!
+! 4 bytes length
+! <n> bytes data
+!
+! jEdit sends a packet with code to eval, it receives the output
+! captured with with-string.
+: write-packet ( string -- )
+ dup str-length write-big-endian-32 write flush ;
+
+: read-packet ( -- string )
+ read-big-endian-32 read# ;
+
+: wire-server ( -- )
+ #! Repeatedly read jEdit requests and execute them. Return
+ #! on EOF.
+ read-packet [ eval>string write-packet wire-server ] when* ;
+
+! Stream protocol for jEdit allows user to interact with a
+! Factor listener.
+!
+! Packets have the following form:
+!
+! 1 byte -- type. CHAR: w: write, CHAR: r: read CHAR: f flush
+! 4 bytes -- for write only -- length of write request
+! remaining -- unparsed write request -- string then style
+
+! After a read line request, the server reads a response from
+! the client:
+! 4 bytes -- length. -1 means EOF
+! remaining -- input
+: jedit-read ( -- str )
+ CHAR: r write flush read-big-endian-32 read# ;
+
+: jedit-write-attr ( str style -- )
+ CHAR: w write
+ [ swap . . ] with-string
+ dup str-length write-big-endian-32
+ write ;
+
+: jedit-flush ( -- )
+ CHAR: f write flush ;
+
+: <jedit-stream> ( stream -- stream )
+ <extend-stream> [
+ ( -- str )
+ [ jedit-read ] "freadln" set
+ ( str -- )
+ [
+ default-style jedit-write-attr
+ ] "fwrite" set
+ ( str style -- )
+ [ jedit-write-attr ] "fwrite-attr" set
+ ( string -- )
+ [
+ "\n" cat2 default-style jedit-write-attr
+ ] "fprint" set
+ ( -- )
+ [ jedit-flush ] "fflush" set
+ ] extend ;
+
+: stream-server ( -- )
+ #! Execute this in the inferior Factor.
+ "stdio" get <jedit-stream> "stdio" set ;