-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdraw-mzscheme-r200.scm
386 lines (378 loc) · 12.8 KB
/
xdraw-mzscheme-r200.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
;;;
;;; xdraw-mzscheme.scm -- Interface to xdraw for MzScheme
;;;
;;; This file is placed in the public domain by the author,
;;; Riku Saikkonen <[email protected]>.
;;;
;;; $Id: xdraw-mzscheme.scm,v 1.45 2000/11/28 20:16:29 rjs Exp $
;;;
;;; See README.Scheme for documentation.
;;;
;;; These non-R5RS features of MzScheme are used: error, process*,
;;; flush-output, sleep, semaphores, exceptions, find-executable-path
;;;
;;; ---
;;;
;;; This file is originally from the xdraw package by Riku Saikkonen, available
;;; at http://users.tkk.fi/u/rsaikkon/software/xdraw.html. It has been modified
;;; to work with PLT Scheme v200. -Paul Wagner
;; FORMAT-XDRAW-COMMAND: Formats a command into a string to send to
;; xdraw
(define (format-xdraw-command . command)
(define (flatten l)
(apply append l))
(define (string->xdraw s)
(define (char->escaped-char-list c)
(cond ((eq? c #\newline) (list #\\ #\n))
((eq? c #\\) (list #\\ #\\))
((eq? c #\") (list #\\ #\"))
(else (list c))))
(list->string (flatten (append (list (list #\"))
(map char->escaped-char-list
(string->list s))
(list (list #\"))))))
(define (symbol->xdraw sym)
(symbol->string sym))
(define (number->xdraw x)
(number->string (inexact->exact (round x))))
(define (list->xdraw x)
(string-append "( " (list-contents->xdraw x) " )"))
(define (any->xdraw x)
(cond ((number? x) (number->xdraw x))
((symbol? x) (symbol->xdraw x))
((string? x) (string->xdraw x))
((pair? x) (list->xdraw x))
(else (error "XDRAW: Invalid element in command:" x))))
(define (list-contents->xdraw l)
(define (insert-spaces l) ; insert " " between elements
(cond ((null? l) '())
((null? (cdr l)) l)
(else (cons (car l) (cons " " (insert-spaces (cdr l)))))))
(apply string-append (insert-spaces (map any->xdraw l))))
(string-append (list-contents->xdraw command) (string #\newline)))
;; START-XDRAW: Starts a copy of xdraw, returning an xdraw object.
;; filename = path and file name for the xdraw executable
;; cmdline-args = strings to give as command-line arguments
;;
;; Notes/bugs:
;; * send, send-with-result and get-event may block (but shouldn't
;; block in practice)
;; * wait-for-errors might not wait long enough for xdraw to react
;; * if there is an error that aborts the evaluator, the semaphore is
;; not released, so further messages to xdraw will block
(define (start-xdraw filename . cmdline-args)
(define (make-queue) ; a simple FIFO queue
(let ((front-ptr '())
(rear-ptr '())
(len 0))
(define (insert! item)
(let ((old-rear rear-ptr))
(set! rear-ptr (cons item '()))
(if (empty?)
(set! front-ptr rear-ptr)
(set-cdr! old-rear rear-ptr)))
(set! len (+ len 1)))
(define (remove!)
(if (empty?)
(error "XDRAW: QUEUE: Tried to remove from empty queue"))
(let ((item (car front-ptr)))
(set! front-ptr (cdr front-ptr))
(set! len (- len 1))
(if (empty?)
(set! rear-ptr '())) ; for garbage collection
item))
(define (peek)
(if (empty?)
(error "XDRAW: QUEUE: Tried to peek from empty queue"))
(car front-ptr))
(define (flush!)
(let ((elems front-ptr))
(set! front-ptr '())
(set! rear-ptr '())
(set! len 0)
elems))
(define (empty?)
(null? front-ptr))
(define (queue-dispatch m . args)
(cond ((eq? m 'insert!) (apply insert! args))
((eq? m 'peek) (peek))
((eq? m 'remove!) (remove!))
((eq? m 'empty?) (empty?))
((eq? m 'length) len)
((eq? m 'flush!) (flush!))
(else (error "XDRAW: QUEUE: Invalid message received" m))))
queue-dispatch))
(let ((synchronizer (make-semaphore 1))
(display-commands #f)
(error-handler display)
(wait-for-errors #f)
(command-buffer-maxsize 0)
(command-buffer (make-queue))
(cleaned-up #f)
(event-queue (make-queue)))
(let-values (((proc-handle stdout-port stdin-port stderr-port) (apply subprocess #f #f #f filename cmdline-args)))
(define (read-all port) ; read all there is to a string
(define (read-chars)
(if (char-ready? port)
(let ((c (read-char port)))
(if (eof-object? c)
(begin (clean-up!)
'())
(cons c (read-chars))))
'()))
(if cleaned-up
""
(list->string (read-chars))))
(define (display-cmd cmdstring)
(if display-commands
(begin
(display "xdraw sent: ")
(display cmdstring))
'ok))
(define (display-data data)
(if display-commands
(begin
(display "xdraw got: ")
(display data)
(newline))
'ok))
(define (get-stderr)
(read-all stderr-port))
(define (report-error from errmsg)
(if (and error-handler (> (string-length errmsg) 0))
(error-handler (string-append "xdraw error from "
from
errmsg
(string #\newline)))
'ignored))
(define (check-cleaned-up cont-thunk)
(if cleaned-up
(begin (if error-handler
(error-handler (string-append
"xdraw error: xdraw has exited!"
(string #\newline))))
'())
(cont-thunk)))
(define (handle-errors last-command)
(if wait-for-errors
(begin
(sleep 0.2) ; wait a while for xdraw
(report-error last-command (get-stderr)))
'ok))
(define (handle-previous-errors)
(report-error (string-append "a previous command"
(string #\newline))
(get-stderr)))
(define (send-string str)
(handle-previous-errors)
(display-cmd str)
(check-cleaned-up
(lambda ()
(display str stdin-port)
(flush-output stdin-port)
(handle-errors str)
'sent)))
(define (buffered-send-string str)
(command-buffer 'insert! str)
(if (>= (command-buffer 'length) command-buffer-maxsize)
(flush-command-buffer))
'ok)
(define (flush-command-buffer)
(if (command-buffer 'empty?)
'nothing-done
(send-string (apply string-append (command-buffer 'flush!)))))
(define (send-cmd . cmd)
(cond ((null? cmd)
'done)
((string? (car cmd)) ; a list of preformatted strings
(buffered-send-string (car cmd))
(apply send-cmd (cdr cmd)))
(else ; a list containing the command
(buffered-send-string (apply format-xdraw-command cmd)))))
(define (process-stdout want-return wait-for-input)
(define (warn msg value)
(if error-handler
(begin
(display "xdraw warning: ")
(display msg)
(display value)
(newline))
'ok))
(define (skip-whitespace-char-ready? port)
(and (char-ready? port)
(if (memv (peek-char port) '(#\newline #\space))
(begin
(read-char port)
(skip-whitespace-char-ready? port))
#t)))
(if (and (not cleaned-up)
(or wait-for-input
(skip-whitespace-char-ready? stdout-port)))
(let ((val (read stdout-port)))
(cond ((eof-object? val)
(clean-up!)
'())
((and (pair? val) (eq? (car val) 'event))
(event-queue 'insert! val)
(process-stdout want-return (and wait-for-input
want-return)))
((and (pair? val) (eq? (car val) 'return))
(if want-return
val
(begin
(warn "ignored return value: " val)
(process-stdout want-return wait-for-input))))
(else
(warn "got an invalid value: " val)
(process-stdout want-return wait-for-input))))
'()))
(define (send-with-result . cmd)
(check-cleaned-up
(lambda ()
(flush-command-buffer)
(process-stdout #f #f) ; ignore old stdout, if any
(apply send-cmd cmd)
(flush-command-buffer)
(process-stdout #t #t)))) ; return new stdout (parsed)
(define (get-event)
(check-cleaned-up
(lambda ()
(process-stdout #f #f)
(if (event-queue 'empty?)
'()
(event-queue 'remove!)))))
(define (peek-event)
(check-cleaned-up
(lambda ()
(process-stdout #f #f)
(if (event-queue 'empty?)
'()
(event-queue 'peek)))))
(define (wait-for-event)
(define (loop)
(if cleaned-up
'()
(if (event-queue 'empty?)
(begin
(flush-command-buffer)
(process-stdout #f #t)
(loop))
(event-queue 'remove!))))
(check-cleaned-up loop))
(define (error-handling new)
(cond ((eq? new 'display)
(set! error-handler display))
((eq? new 'error)
(set! error-handler error))
((not new)
(set! error-handler #f))
(else
(error "XDRAW: Invalid argument to error-handling:" new))))
(define (clean-up!)
(close-output-port stdin-port)
(close-input-port stdout-port)
(close-input-port stderr-port)
(set! cleaned-up #t)
'done)
(define (exit-xdraw)
(if cleaned-up
'already-cleaned-up
(begin (set! wait-for-errors #f) ; don't want this now...
(if (alive?)
(send-cmd (format-xdraw-command 'Exit)))
(clean-up!))))
(define (alive?)
(and (not cleaned-up)
(eq? (subprocess-status proc-handle) 'running)))
(define (cleanup-if-dead)
(if (or cleaned-up (alive?))
'ok
(clean-up!)))
(let ((dispatch-table ; (symbol procedure)
`((send ,send-cmd)
(send-with-result ,send-with-result)
(get-event ,get-event)
(peek-event ,peek-event)
(wait-for-event ,wait-for-event)
(flush-command-buffer ,flush-command-buffer)
(whatami ,(lambda ()
'xdraw-object))
(alive? ,alive?)
(display-commands ,(lambda (new)
(set! display-commands new)))
(error-handling ,error-handling)
(wait-for-errors ,(lambda (new)
(set! wait-for-errors new)))
(buffer-commands ,(lambda (newlen)
(flush-command-buffer)
(set! command-buffer-maxsize newlen)))
(exit ,exit-xdraw)
(cleanup-if-dead ,cleanup-if-dead))))
(define (real-dispatch m . args) ; dispatch based on table
(let ((table-entry (assq m dispatch-table)))
(if (not table-entry)
(error "XDRAW: object got an invalid message" m))
(apply (cadr table-entry) args)))
(define (serialize thunk)
(let ((result #f))
(semaphore-wait synchronizer)
(with-handlers
((exn? (lambda (e)
(semaphore-post synchronizer)
(raise e))))
(set! result (thunk)))
(semaphore-post synchronizer)
result))
(define (xdraw-dispatch . args)
(serialize (lambda () (apply real-dispatch args))))
xdraw-dispatch))))
;;; Simple interface
;; set! this to the location of the xdraw binary, if necessary. For
;; example, (set! *xdraw-path* "/usr/local/bin/xdraw").
;; (find-executable-path is an MzScheme primitive which looks for the
;; named command in the PATH.)
(define *xdraw-path* (find-executable-path "xdraw" "xdraw"))
;; An internal list of the objects created by new-xdraw
(define *xdraw-objects* '())
;; NEW-XDRAW: Starts a new copy of xdraw, cleaning up old, dead ones.
;; Returns the new xdraw object (inside a small wrapper object).
(define (new-xdraw . cmdline-args)
(define (add-to-xdraw-objects! obj)
(set! *xdraw-objects*
(cons obj *xdraw-objects*)))
(define (remove-from-xdraw-objects! obj)
(define (remove o l)
(cond ((null? l) '())
((eq? (car l) o) (remove o (cdr l)))
(else (cons (car l) (remove o (cdr l))))))
(set! *xdraw-objects* (remove obj *xdraw-objects*)))
(cleanup-xdraws!)
(let ((the-obj (apply start-xdraw (cons *xdraw-path* cmdline-args))))
(define (new-xdraw-dispatch . args)
(if (and (= (length args) 1) (eq? (car args) 'exit))
(begin
(remove-from-xdraw-objects! the-obj)
(the-obj 'exit))
(apply the-obj args)))
(add-to-xdraw-objects! the-obj)
new-xdraw-dispatch))
;; CLEANUP-XDRAWS!: Cleans up dead xdraw processes that were started
;; with new-xdraw. This is called automatically whenever new-xdraw is
;; called.
(define (cleanup-xdraws!)
(define (clean left) ; keep only live objects
(if (null? left)
'()
(let ((this-obj (car left))
(rest (cdr left)))
(this-obj 'cleanup-if-dead)
(if (this-obj 'alive?)
(cons this-obj (clean rest))
(clean rest)))))
(set! *xdraw-objects* (clean *xdraw-objects*))
'done)
;; EXIT-ALL-XDRAWS!: Exits all xdraw processes started with new-xdraw
(define (exit-all-xdraws!)
(for-each (lambda (obj) (obj 'exit)) *xdraw-objects*)
(set! *xdraw-objects* '())
'done)