* sexp oriented select
@ 2003-06-18 20:53 Ondrej Zajicek
2003-06-18 21:32 ` Paul Jarc
2003-06-19 0:20 ` Christopher Cramer
0 siblings, 2 replies; 3+ messages in thread
From: Ondrej Zajicek @ 2003-06-18 20:53 UTC (permalink / raw)
[-- Attachment #1.1: Type: text/plain, Size: 851 bytes --]
Hello
I am thinking about something like s-expression oriented select - it accepts
list of ports (or file descriptors) and returns first arrived s-expression (or
ends because of timeout) and some identifier of port sexp is from.
Is there any clever way to implement this in guile (without reimplementing
'read')? My best idea is to use select, collect chars from ports, count
parenthesis for each port and when something sexp-like appears then read it
(from string created from collected chars) and unget rest of chars (or pass it
like some state between calls to this function).
--
Elen sila lumenn' omentielvo
Ondrej 'SanTiago' Zajicek (email: santiago@mail.cz, jabber: santiago@jabber.cz)
OpenPGP encrypted e-mails preferred (KeyID 0x11DEADC3, wwwkeys.pgp.net)
"To err is human -- to blame it on a computer is even more so."
[-- Attachment #1.2: Type: application/pgp-signature, Size: 197 bytes --]
[-- Attachment #2: Type: text/plain, Size: 139 bytes --]
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: sexp oriented select
2003-06-18 20:53 sexp oriented select Ondrej Zajicek
@ 2003-06-18 21:32 ` Paul Jarc
2003-06-19 0:20 ` Christopher Cramer
1 sibling, 0 replies; 3+ messages in thread
From: Paul Jarc @ 2003-06-18 21:32 UTC (permalink / raw)
Cc: guile-user
Ondrej Zajicek <santiago@mail.cz> wrote:
> My best idea is to use select, collect chars from ports, count
> parenthesis for each port and when something sexp-like appears then
> read it (from string created from collected chars)
Collect characters in a string buffer for each port. After you append
some characters from the port, use something like the following
function to read an object from that string, and then replace the
buffer with the remaining characters. Beware that this function does
not handle other kinds of read errors.
(define (pop-object buffer)
"\
Read an object from BUFFER. Return a pair of the object and the rest
of the buffer. If BUFFER does not yet contain a complete object,
return #f."
(with-input-from-string
buffer
(lambda ()
(define obj #f)
(define exception #f)
(catch 'misc-error
(lambda () (set! obj (read)))
(lambda exc (set! exception exc)))
(if (and (list? exception)
(equal? "end of file" (caddr exception)))
#f
(let loop ((new-buffer "")
(char (read-char)))
(if (eof-object? char)
(cons obj new-buffer)
(loop (string-append new-buffer (make-string 1 char))
(read-char))))))))
paul
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: sexp oriented select
2003-06-18 20:53 sexp oriented select Ondrej Zajicek
2003-06-18 21:32 ` Paul Jarc
@ 2003-06-19 0:20 ` Christopher Cramer
1 sibling, 0 replies; 3+ messages in thread
From: Christopher Cramer @ 2003-06-19 0:20 UTC (permalink / raw)
Cc: guile-user
On Wed, Jun 18, 2003 at 10:53:43PM +0200, Ondrej Zajicek wrote:
> I am thinking about something like s-expression oriented select - it accepts
> list of ports (or file descriptors) and returns first arrived s-expression (or
> ends because of timeout) and some identifier of port sexp is from.
>
> Is there any clever way to implement this in guile (without reimplementing
> 'read')?
You could do it with threads, like this:
(use-modules (ice-9 threads))
(define select-sexpr
(let (
(m (make-mutex))
(c (make-condition-variable))
(e '()))
(for-each
(lambda (port)
(begin-thread
(let ((x (read port)))
(lock-mutex m)
(if (null? e)
(begin
(set! e (cons (cons port x) e))
(signal-condition-variable c)
(unlock-mutex m))
(begin
(unlock-mutex m)
(or
(eof-object? x)
(unread-string
(object->string x))))))))
ports)
(lock-mutex m)
(while (null? e)
(wait-condition-variable c m))
(let ((return e))
(unlock-mutex)
return)))
This code has problems (leaving threads running even after they're not
needed anymore) so you really need to use more elaborate data structures.
You couldn't use that code as is.
The other method would be storing characters in a string and then calling
read on a string port and catching exceptions etc. but I think it would
be a little wasteful if you are reading large expressions. OTOH it's the
only way to implement a timeout.
--
Christopher Cramer <crayc@pyro.net> <http://www.pyro.net/~crayc/>
"People said it couldn't be that our soldiers would do such things. Now
you read worse things in the mainstream media and people don't care. We
used to say that if only people know about it, it would stop. Now they
know about it, and it hasn't stopped." - Adam Keller
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-06-19 0:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-06-18 20:53 sexp oriented select Ondrej Zajicek
2003-06-18 21:32 ` Paul Jarc
2003-06-19 0:20 ` Christopher Cramer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).