File: port.scm

package info (click to toggle)
scheme48 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,232 kB
  • sloc: lisp: 88,907; ansic: 87,519; sh: 3,224; makefile: 771
file content (428 lines) | stat: -rw-r--r-- 12,387 bytes parent folder | download | duplicates (4)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
; Part of Scheme 48 1.9.  See file COPYING for notices and license.

; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Robert Ransom

; Ports and port handlers

;  (discloser <port>) -> (<symbol> <value> ...)
;  (close <port>) -> whatever
;
; Input ports
;  (byte <port> <read?>) -> <byte>
;  (char <port> <mode>) -> <char>
;     <mode> says whether we're doing ...
;     - #t: a PEEK
;     - #f: a READ
;     - (): CHAR-READY? 
;  (block <port> <buffer> <start> <count>) -> <byte count>
;  (ready? <port>) -> <boolean>
;
; Output ports
;  (byte <port> <byte>) -> whatever
;  (char <port> <char>) -> whatever
;  (block <port> <buffer> <start> <count>) -> whatever
;  (ready? <port>) -> <boolean>
;  (force-output <port>) -> whatever

(define-record-type port-handler :port-handler
  (make-port-handler discloser close byte char block ready? force)
  port-handler?
  (discloser port-handler-discloser)
  (close     port-handler-close)
  (byte      port-handler-byte)
  (char	     port-handler-char)
  (block     port-handler-block)
  (ready?    port-handler-ready?)
  (force     port-handler-force))	; only used for output

;----------------
; Disclosing ports by calling the disclose handler.

(define (disclose-port port)
  ((port-handler-discloser (port-handler port))
     port))

(define-method &disclose ((port <input-port>))
  (disclose-port port))

(define-method &disclose ((port <output-port>))
  (disclose-port port))

;----------------
; Set up VM exception handlers for the three unnecessary I/O primitives,
; READ-BYTE, PEEK-BYTE, and WRITE-BYTE.  These do the right thing in
; the case of unbuffered ports or buffer overflow or underflow.
;
; This is abstracted to avoid a circular module dependency.

(define (initialize-i/o-handlers! define-vm-exception-handler signal-exception)
  (define-vm-exception-handler (enum op read-byte)
    (one-arg-proc->handler (lambda (port)
			     ((port-handler-byte (port-handler port))
			      port
			      #t))))
    
  (define-vm-exception-handler (enum op peek-byte)
    (one-arg-proc->handler (lambda (port)
			     ((port-handler-byte (port-handler port))
			      port
			      #f))))

  (define-vm-exception-handler (enum op read-char)
    (one-arg-proc->handler (lambda (port)
			     ((port-handler-char (port-handler port))
			      port
			      #f))))
    
  (define-vm-exception-handler (enum op peek-char)
    (one-arg-proc->handler (lambda (port)
			     ((port-handler-char (port-handler port))
			      port
			      #t))))
  
  (define-vm-exception-handler (enum op write-byte)
    (two-arg-proc->handler (lambda (byte port)
			     ((port-handler-byte (port-handler port))
			      port
			      byte))))

  (define-vm-exception-handler (enum op write-char)
    (two-arg-proc->handler (lambda (ch port)
			     ((port-handler-char (port-handler port))
			      port
			      ch)))))

; Check the VM exception and then lock the port.

(define (one-arg-proc->handler proc)
  (lambda (opcode reason port)
    (if (= reason (enum exception buffer-full/empty))
	(proc port)
	;; note this must be an assertion violation---these only look at the buffer
	(signal-vm-exception opcode reason port))))

; This could be combined with one-arg-... if the port were the first argument.

(define (two-arg-proc->handler proc)
  (lambda (opcode reason arg port)
    (if (= reason (enum exception buffer-full/empty))
	(proc arg port)
	;; note this must be an assertion violation---these only look at the buffer
	(signal-vm-exception opcode reason arg port))))

;----------------
; Wrappers for the various port operations.  These check types and arguments
; and then call the appropriate handler procedure.

(define (real-char-ready? port)
  (cond
   ((open-input-port? port)
    ((port-handler-char (port-handler port)) port '()))
   (else
    (assertion-violation 'char-ready? "invalid argument" port))))

; See if there is a character available.  BYTE-READY? itself is defined
; in current-port.scm as it needs CURRENT-INPUT-PORT when called with
; no arguments.

(define (real-byte-ready? port)
  (if (open-input-port? port)
      ((port-handler-ready? (port-handler port))
         port)
      (assertion-violation 'real-byte-ready? "invalid argument" port)))

; Reading in a block of characters at once.

(define (read-block buffer start count port . maybe-wait?)
  (if (and (port? port)
	   (open-input-port? port)
	   (okay-limits? buffer
			 start
			 count))
      (if (= count 0)
	  0
	 ((port-handler-block (port-handler port))
	    port
	    buffer
	    start
	    count
	    (or (null? maybe-wait?)
		(car maybe-wait?))))
      (assertion-violation 'read-block "invalid argument" buffer start count port)))

; Write the COUNT bytes beginning at START from BUFFER to PORT.

(define (write-block buffer start count port)
  (if (and (port? port)
	   (open-output-port? port)
	   (okay-limits? buffer start count))
      (if (< 0 count)
	  ((port-handler-block (port-handler port))
	     port
	     buffer
	     start
	     count))
      (assertion-violation 'write-block "invalid argument" buffer start count port)))

(define (write-string string port)
  (do ((size (string-length string))
       (i 0 (+ 1 i)))
      ((>= i size) (unspecific))
    (write-char (string-ref string i) port)))

; BYTE-READY? for output ports.

(define (output-port-ready? port)
  (if (open-output-port? port)
      ((port-handler-ready? (port-handler port))
         port)
      (assertion-violation 'output-port-ready? "invalid argument" port)))

; Forcing output.

(define (force-output port)
  (if (open-output-port? port)
      ((port-handler-force (port-handler port))
         port
	 #t)			; raise error if PORT is not open
      (assertion-violation 'force-output "invalid argument" port)))

(define (force-output-if-open port)
  (if (open-output-port? port)
      ((port-handler-force (port-handler port))
         port
	 #f)))			; do not raise error if PORT is not open

; Closing input and output ports.
; RnRS says that CLOSE-{IN|OUT}PUT-PORT is idempotent.

(define (close-input-port port)
  (if (input-port? port)
      (begin
	(if (open-input-port? port)
	    ((port-handler-close (port-handler port))
	       port))
	(unspecific))
      (assertion-violation 'close-input-port "invalid argument" port)))

(define (close-output-port port)
  (if (output-port? port)
      (begin
	(if (open-output-port? port)
	    ((port-handler-close (port-handler port))
	       port))
	(unspecific))
      (assertion-violation 'close-output-port "invalid argument" port)))

;----------------

(define (port-text-codec p)
  (spec->text-codec (port-text-codec-spec p)))

(define (set-port-text-codec! p codec)
  (set-port-text-codec-spec! p (text-codec->spec codec)))

;----------------
; Check that BUFFER contains COUNT characters starting from START.

(define (okay-limits? buffer start count)
  (and (integer? start)
       (exact? start)
       (<= 0 start)
       (integer? count)
       (exact? count)
       (<= 0 count)
       (<= (+ start count)
	   (cond ((byte-vector? buffer)
		  (byte-vector-length buffer))
		 (else
		  -1)))))

;----------------
; Is PORT open?

(define (open-port? port)
  (not (= 0 (bitwise-and open-port-mask (provisional-port-status port)))))

(define open-port-mask
  (bitwise-ior (arithmetic-shift 1 (enum port-status-options open-for-input))
	       (arithmetic-shift 1 (enum port-status-options open-for-output))))

;----------------
; Input ports

(define input-port-mask
  (arithmetic-shift 1
		    (enum port-status-options input)))

(define open-input-port-mask
  (arithmetic-shift 1
		    (enum port-status-options open-for-input)))

(define open-input-port-status
  (bitwise-ior input-port-mask
	       open-input-port-mask))

(define (open-input-port? port)
  (not (= 0 (bitwise-and open-input-port-mask
			 (provisional-port-status port)))))

(define (make-input-port-closed! port)
  (provisional-set-port-status! port
				(bitwise-and (provisional-port-status port)
					     (bitwise-not open-input-port-mask))))

;----------------
; Output ports

(define output-port-mask
  (arithmetic-shift 1
		    (enum port-status-options output)))

(define open-output-port-mask
  (arithmetic-shift 1
		    (enum port-status-options open-for-output)))

(define open-output-port-status
  (bitwise-ior output-port-mask
	       open-output-port-mask))

(define (open-output-port? port)
  (not (= 0 (bitwise-and open-output-port-mask
			 (provisional-port-status port)))))

(define (make-output-port-closed! port)
  (provisional-set-port-status! port
				(bitwise-and (provisional-port-status port)
					     (bitwise-not open-output-port-mask))))

(define (make-unbuffered-output-port handler data)
  (if (port-handler? handler)
      (make-port handler
		 (enum text-encoding-option latin-1)
		 #f
		 open-output-port-status
		 #f		; lock     (not used in unbuffered ports)
		 data
		 (make-byte-vector 128 0) ; buffer
		 #f		; index
		 #f		; limit
		 #f             ; pending-cr?
		 #f)            ; pending-eof?
      (assertion-violation 'make-unbuffered-output-port "invalid argument"
			   handler data)))

(define (make-one-byte-handler write-block)
  (lambda (port byte)
    (let ((buffer (port-buffer port)))
      (byte-vector-set! buffer 0 byte)
      (let loop ()
	(if (= 0 (write-block port buffer 0 1))
	    (loop))))))

(define (make-one-char-handler write-block)
  (lambda (port ch)
    (let ((buffer (port-buffer port))
	  (encode-char
	   (text-codec-encode-char-proc (port-text-codec port))))
      (let ((encode-count
	     (if (and (port-crlf? port)
		      (char=? ch #\newline))
		 (atomically
		  (call-with-values
		      (lambda ()
			(encode-char cr
				     buffer 0 (byte-vector-length buffer)))
		    (lambda (ok? encode-count-cr)
		      ;; OK? must be true
		      (call-with-values
			  (lambda ()
			    (encode-char #\newline
					 buffer 
					 encode-count-cr
					 (- (byte-vector-length buffer) encode-count-cr)))
			(lambda (ok? encode-count-lf)
			  ;; OK? must be true
			  (+ encode-count-cr encode-count-lf))))))
		 (atomically
		  (call-with-values
		      (lambda ()
			(encode-char ch
				     buffer 0 (byte-vector-length buffer)))
		    (lambda (ok? encode-count)
		      (if ok?
			  encode-count
			  ;; hrmpfl ...
			  (call-with-values
			      (lambda ()
				(encode-char #\?
					     buffer 0 (byte-vector-length buffer)))
			    (lambda (ok? encode-count)
			      encode-count)))))))))
	(let loop ((index 0))
	  (let* ((to-write (- encode-count index))
		 (written
		  (write-block port buffer index to-write)))
	    (if (< written to-write)
		(loop (+ index written)))))))))

(define cr (ascii->char 13))

(define (make-write-block-handler write-block)
  (lambda (port buffer start count)
    (let loop ((sent 0))
      (let ((sent (+ sent
		     (write-block port
				  buffer
				  (+ start sent)
				  (- count sent)))))
	(if (< sent count)
	    (loop sent))))))

(define (make-unbuffered-output-port-handler discloser closer! write-block ready?)
  (make-port-handler discloser
		     closer!
		     (make-one-byte-handler write-block)
		     (make-one-char-handler write-block)
		     (make-write-block-handler write-block)
		     ready?
		     (lambda (port error-if-closed?)	; output forcer
		       (unspecific))))

;----------------
; Output ports that just discard any output.

(define null-output-port-handler
  (make-port-handler
    (lambda (ignore)			; disclose
      (list 'null-output-port))
    make-output-port-closed!		; close
    (lambda (port byte)			; one-byte (we just empty the buffer)
      (set-port-index! port 0))
    (lambda (port char)                 ; one-char (we just empty the buffer)
      (set-port-index! port 0))
    (lambda (port buffer start count)	; write-block
      count)
    (lambda (port)			; ready?
      #t)
    (lambda (port error-if-closed?)	; force-output
      (unspecific))))

; They can all share a buffer.  The buffer is needed because the WRITE-BYTE
; byte code actually wants to put characters somewhere.

(define null-output-buffer
  (make-byte-vector 1024 0))

(define (make-null-output-port)
  (make-port null-output-port-handler
	     null-text-codec
	     #f
	     open-output-port-status
	     #f		; timestamp
	     #f		; data
	     null-output-buffer
	     0		; index
	     (byte-vector-length null-output-buffer)	; limit
	     #f         ; pending-cr?
	     #f))	; pending-eof?