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

Contents of /slime/swank-rpc.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Tue Nov 8 08:15:34 2011 UTC (2 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.8: +16 -24 lines
Restore old header format.

* swank-rpc.lisp (parse-header, write-header)
* slime.el (slime-net-decode-length, slime-net-encode-length)
1 tnorderhaug 1.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 heller 1.6 (defpackage #:swank-rpc
12 sboukarev 1.3 (:use :cl)
13 tnorderhaug 1.2 (:export
14 heller 1.6 #:read-message
15     #:swank-reader-error
16     #:swank-reader-error.packet
17     #:swank-reader-error.cause
18     #:write-message))
19 tnorderhaug 1.2
20     (in-package :swank-rpc)
21 tnorderhaug 1.1
22 heller 1.6
23 tnorderhaug 1.1 ;;;;; Input
24    
25 heller 1.6 (define-condition swank-reader-error (reader-error)
26 heller 1.8 ((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 heller 1.6
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 heller 1.8 (error (make-condition 'swank-reader-error
36     :packet packet :cause c))))))
37 heller 1.6
38     (defun read-packet (stream)
39 heller 1.9 (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 heller 1.8
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 heller 1.9 (parse-integer (map 'string #'code-char (read-chunk stream 6))
57     :radix 16))
58    
59 heller 1.6 (defun read-chunk (stream length)
60 heller 1.8 (let* ((buffer (make-array length :element-type '(unsigned-byte 8)))
61 heller 1.6 (count (read-sequence buffer stream)))
62     (assert (= count length) () "Short read: length=~D count=~D" length count)
63     buffer))
64    
65     ;; FIXME: no one ever tested this and will probably not work.
66     (defparameter *validate-input* nil
67     "Set to true to require input that strictly conforms to the protocol")
68    
69     (defun read-form (string package)
70     (with-standard-io-syntax
71     (let ((*package* package))
72     (if *validate-input*
73     (validating-read string)
74     (read-from-string string)))))
75    
76     (defun validating-read (string)
77     (with-input-from-string (*standard-input* string)
78     (simple-read)))
79    
80 tnorderhaug 1.1 (defun simple-read ()
81 heller 1.6 "Read a form that conforms to the protocol, otherwise signal an error."
82 tnorderhaug 1.1 (let ((c (read-char)))
83     (case c
84     (#\" (with-output-to-string (*standard-output*)
85     (loop for c = (read-char) do
86     (case c
87     (#\" (return))
88     (#\\ (write-char (read-char)))
89     (t (write-char c))))))
90     (#\( (loop collect (simple-read)
91     while (ecase (read-char)
92     (#\) nil)
93     (#\space t))))
94     (#\' `(quote ,(simple-read)))
95     (t (let ((string (with-output-to-string (*standard-output*)
96     (loop for ch = c then (read-char nil nil) do
97     (case ch
98     ((nil) (return))
99     (#\\ (write-char (read-char)))
100     ((#\space #\)) (unread-char ch)(return))
101     (t (write-char ch)))))))
102     (cond ((digit-char-p c) (parse-integer string))
103     ((intern string))))))))
104    
105 heller 1.6
106 tnorderhaug 1.1 ;;;;; Output
107    
108 heller 1.6 (defun write-message (message package stream)
109     (let* ((string (prin1-to-string-for-emacs message package))
110 heller 1.8 (octets (handler-case (swank-backend:string-to-utf8 string)
111     (error (c) (encoding-error c string))))
112     (length (length octets)))
113 heller 1.9 (write-header stream length)
114 heller 1.8 (write-sequence octets stream)
115 heller 1.6 (finish-output stream)))
116 tnorderhaug 1.1
117 heller 1.8 ;; FIXME: for now just tell emacs that we and an encoding problem.
118     (defun encoding-error (condition string)
119     (swank-backend:string-to-utf8
120     (prin1-to-string-for-emacs
121     `(:reader-error
122     ,(asciify string)
123     ,(format nil "Error during string-to-utf8: ~a"
124     (or (ignore-errors (asciify (princ-to-string condition)))
125     (asciify (princ-to-string (type-of condition))))))
126     (find-package :cl))))
127    
128 heller 1.9 (defun write-header (stream length)
129     (declare (type (unsigned-byte 24) length))
130     ;;(format *trace-output* "length: ~d (#x~x)~%" length length)
131     (loop for c across (format nil "~6,'0x" length)
132     do (write-byte (char-code c) stream)))
133 heller 1.8
134 heller 1.6 (defun prin1-to-string-for-emacs (object package)
135 tnorderhaug 1.1 (with-standard-io-syntax
136     (let ((*print-case* :downcase)
137     (*print-readably* nil)
138     (*print-pretty* nil)
139 heller 1.6 (*package* package))
140 tnorderhaug 1.1 (prin1-to-string object))))
141    
142 heller 1.6
143 tnorderhaug 1.1 #| TEST/DEMO:
144    
145     (defparameter *transport*
146     (with-output-to-string (out)
147 heller 1.6 (write-message '(:message (hello "world")) *package* out)
148     (write-message '(:return 5) *package* out)
149     (write-message '(:emacs-rex NIL) *package* out)))
150 tnorderhaug 1.1
151     *transport*
152    
153     (with-input-from-string (in *transport*)
154     (loop while (peek-char T in NIL)
155 heller 1.6 collect (read-message in *package*)))
156 tnorderhaug 1.1
157     |#

  ViewVC Help
Powered by ViewVC 1.1.5