/[slime]/slime/swank-rpc.lisp
ViewVC logotype

Contents of /slime/swank-rpc.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Sun Dec 4 14:54:35 2011 UTC (2 years, 4 months ago) by heller
Branch: MAIN
Changes since 1.9: +7 -2 lines
* swank-rpc.lisp (read-chunk): Signal end-of-file we had no input.
1 ;;; -*- indent-tabs-mode:nil coding:latin-1-unix -*-
2 ;;;
3 ;;; swank-rpc.lisp -- Pass remote calls and responses between lisp systems.
4 ;;;
5 ;;; Created 2010, Terje Norderhaug <terje@in-progress.com>
6 ;;;
7 ;;; This code has been placed in the Public Domain. All warranties
8 ;;; are disclaimed.
9 ;;;
10
11 (defpackage #:swank-rpc
12 (:use :cl)
13 (:export
14 #:read-message
15 #:swank-reader-error
16 #:swank-reader-error.packet
17 #:swank-reader-error.cause
18 #:write-message))
19
20 (in-package :swank-rpc)
21
22
23 ;;;;; Input
24
25 (define-condition swank-reader-error (reader-error)
26 ((packet :type string :initarg :packet
27 :reader swank-reader-error.packet)
28 (cause :type reader-error :initarg :cause
29 :reader swank-reader-error.cause)))
30
31 (defun read-message (stream package)
32 (let ((packet (read-packet stream)))
33 (handler-case (values (read-form packet package))
34 (reader-error (c)
35 (error (make-condition 'swank-reader-error
36 :packet packet :cause c))))))
37
38 (defun read-packet (stream)
39 (let* ((length (parse-header stream))
40 (octets (read-chunk stream length)))
41 (handler-case (swank-backend:utf8-to-string octets)
42 (error (c)
43 (error (make-condition 'swank-reader-error
44 :packet (asciify octets)
45 :cause c))))))
46
47 (defun asciify (packet)
48 (with-output-to-string (*standard-output*)
49 (loop for code across (etypecase packet
50 (string (map 'vector #'char-code packet))
51 (vector packet))
52 do (cond ((<= code #x7f) (write-char (code-char code)))
53 (t (format t "\\x~x" code))))))
54
55 (defun parse-header (stream)
56 (parse-integer (map 'string #'code-char (read-chunk stream 6))
57 :radix 16))
58
59 (defun read-chunk (stream length)
60 (let* ((buffer (make-array length :element-type '(unsigned-byte 8)))
61 (count (read-sequence buffer stream)))
62 (cond ((= count length)
63 buffer)
64 ((zerop count)
65 (error (make-condition 'end-of-file :stream stream)))
66 (t
67 (error "Short read: length=~D count=~D" length count)))))
68
69 ;; end-of-file
70 ;; FIXME: no one ever tested this and will probably not work.
71 (defparameter *validate-input* nil
72 "Set to true to require input that strictly conforms to the protocol")
73
74 (defun read-form (string package)
75 (with-standard-io-syntax
76 (let ((*package* package))
77 (if *validate-input*
78 (validating-read string)
79 (read-from-string string)))))
80
81 (defun validating-read (string)
82 (with-input-from-string (*standard-input* string)
83 (simple-read)))
84
85 (defun simple-read ()
86 "Read a form that conforms to the protocol, otherwise signal an error."
87 (let ((c (read-char)))
88 (case c
89 (#\" (with-output-to-string (*standard-output*)
90 (loop for c = (read-char) do
91 (case c
92 (#\" (return))
93 (#\\ (write-char (read-char)))
94 (t (write-char c))))))
95 (#\( (loop collect (simple-read)
96 while (ecase (read-char)
97 (#\) nil)
98 (#\space t))))
99 (#\' `(quote ,(simple-read)))
100 (t (let ((string (with-output-to-string (*standard-output*)
101 (loop for ch = c then (read-char nil nil) do
102 (case ch
103 ((nil) (return))
104 (#\\ (write-char (read-char)))
105 ((#\space #\)) (unread-char ch)(return))
106 (t (write-char ch)))))))
107 (cond ((digit-char-p c) (parse-integer string))
108 ((intern string))))))))
109
110
111 ;;;;; Output
112
113 (defun write-message (message package stream)
114 (let* ((string (prin1-to-string-for-emacs message package))
115 (octets (handler-case (swank-backend:string-to-utf8 string)
116 (error (c) (encoding-error c string))))
117 (length (length octets)))
118 (write-header stream length)
119 (write-sequence octets stream)
120 (finish-output stream)))
121
122 ;; FIXME: for now just tell emacs that we and an encoding problem.
123 (defun encoding-error (condition string)
124 (swank-backend:string-to-utf8
125 (prin1-to-string-for-emacs
126 `(:reader-error
127 ,(asciify string)
128 ,(format nil "Error during string-to-utf8: ~a"
129 (or (ignore-errors (asciify (princ-to-string condition)))
130 (asciify (princ-to-string (type-of condition))))))
131 (find-package :cl))))
132
133 (defun write-header (stream length)
134 (declare (type (unsigned-byte 24) length))
135 ;;(format *trace-output* "length: ~d (#x~x)~%" length length)
136 (loop for c across (format nil "~6,'0x" length)
137 do (write-byte (char-code c) stream)))
138
139 (defun prin1-to-string-for-emacs (object package)
140 (with-standard-io-syntax
141 (let ((*print-case* :downcase)
142 (*print-readably* nil)
143 (*print-pretty* nil)
144 (*package* package))
145 (prin1-to-string object))))
146
147
148 #| TEST/DEMO:
149
150 (defparameter *transport*
151 (with-output-to-string (out)
152 (write-message '(:message (hello "world")) *package* out)
153 (write-message '(:return 5) *package* out)
154 (write-message '(:emacs-rex NIL) *package* out)))
155
156 *transport*
157
158 (with-input-from-string (in *transport*)
159 (loop while (peek-char T in NIL)
160 collect (read-message in *package*)))
161
162 |#

  ViewVC Help
Powered by ViewVC 1.1.5