1 ;;;; Guile Debugger UI server
3 ;;; Copyright (C) 2003 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 2.1 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 (define-module (emacs gds-server)
20 #:use-module (emacs gds-client)
21 #:export (run-server))
23 ;; UI is normally via a pipe to Emacs, so make sure to flush output
24 ;; every time we write.
25 (define (write-to-ui form)
31 (write-to-ui (cons '* args)))
33 (define (with-error->eof proc port)
35 (lambda () (proc port))
36 (lambda args the-eof-object)))
38 (define (run-server . ignored-args)
40 (let ((server (socket PF_INET SOCK_STREAM 0)))
42 ;; Initialize server socket.
43 (setsockopt server SOL_SOCKET SO_REUSEADDR 1)
44 (bind server AF_INET INADDR_ANY gds-port-number)
47 (let loop ((clients '()) (readable-sockets '()))
49 (define (do-read port)
50 (cond ((eq? port (current-input-port))
55 (do-read-from-client port))))
57 (define (do-read-from-ui)
58 (trc "reading from ui")
59 (let* ((form (with-error->eof read (current-input-port)))
60 (client (assq-ref (map (lambda (port)
61 (cons (fileno port) port))
64 (with-error->eof read-char (current-input-port))
67 (write (cdr form) client)
69 (trc "client not found")))
72 (define (accept-new-client)
73 (cons (car (accept server)) clients))
75 (define (do-read-from-client port)
76 (trc "reading from client")
77 (let ((next-char (with-error->eof peek-char port)))
78 ;;(trc 'next-char next-char)
79 (cond ((eof-object? next-char)
80 (write-to-ui (list (fileno port) 'closed))
83 ((char=? next-char #\()
84 (write-to-ui (cons (fileno port) (with-error->eof read port)))
87 (with-error->eof read-char port)
90 ;;(trc 'clients clients)
91 ;;(trc 'readable-sockets readable-sockets)
93 (if (null? readable-sockets)
94 (loop clients (car (select (cons (current-input-port)
95 (cons server clients))
98 (loop (do-read (car readable-sockets)) (cdr readable-sockets))))))