-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.html
4735 lines (3585 loc) · 218 KB
/
io.html
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><!-- DO NOT EDIT THIS FILE-->
<!-- Edit the .tex version instead-->
<html>
<head>
<title>Input/Output Operations</title>
<link href="csug.css" rel="stylesheet" type="text/css">
</head>
<body>
<a name="g70"></a>
<a name="./io:h0"></a>
<h1>Chapter 9. Input/Output Operations<a name="CHPTIO"></a></h1>
<p>
This chapter describes <i>Chez Scheme</i>'s generic port facility,
operations on ports, and various <i>Chez Scheme</i> extensions to the
standard set of input/output operations.
See Chapter <a href="http://scheme.com/tspl4/./io.html#g121">7</a> of <i>The Scheme Programming Language, 4th Edition</i> or the Revised<sup>6</sup> Report
on Scheme for a description of standard input/output operations.
Definitions of a few sample generic ports are given in
Section <a href="./io.html#g87">9.17</a>.
<p>
<i>Chez Scheme</i> closes file ports automatically after they become
inaccessible to the program or when the Scheme program exits, but it is
best to close ports explicitly whenever possible.
<p>
<h3><a name="g71"></a><a name="./io:h1"></a>Section 9.1. Generic Ports</h3>
<p>
<i>Chez Scheme</i>'s "<a name="./io:s0"></a>generic port" facility
allows the programmer to add new types of textual ports with
arbitrary input/output semantics.
It may be used, for example, to define any of the built-in
Common Lisp [<a class=citation href="./bibliography.html#g175">29</a>] stream types, i.e.,
<a name="./io:s1"></a>synonym streams,
<a name="./io:s2"></a>broadcast streams,
<a name="./io:s3"></a>concatenated streams,
<a name="./io:s4"></a>two-way streams,
<a name="./io:s5"></a>echo streams, and
<a name="./io:s6"></a>string streams.
It may also be used to define more exotic ports, such as ports that
represent windows on a bit-mapped display or ports that represent
processes connected to the current process via pipes or sockets.
<p>
Each port has an associated <i>port handler</i>.
A port handler is a procedure that accepts messages in an
object-oriented style.
Each message corresponds to one of the low-level Scheme operations
on ports, such as <tt>read-char</tt> and <tt>close-input-port</tt> (but not
<tt>read</tt>, which is defined in terms of the lower-level operations).
Most of these operations simply call the handler immediately with
the corresponding message.
<p>
Standard messages adhere to the following conventions: the message name is
the first argument to the handler.
It is always a symbol, and it is always the name of a primitive
Scheme operation on ports.
The additional arguments are the same as the arguments to the
primitive procedure and occur in the same order.
(The port argument to some of the primitive procedures is optional;
in the case of the messages passed to a handler, the port argument
is always supplied.)
The following messages are defined for built-in ports:
<p>
<p><tt>block-read <i>port</i> <i>string</i> <i>count</i><br>
block-write <i>port</i> <i>string</i> <i>count</i><br>
char-ready? <i>port</i><br>
clear-input-port <i>port</i><br>
clear-output-port <i>port</i><br>
close-port <i>port</i><br>
file-position <i>port</i><br>
file-position <i>port</i> <i>position</i><br>
file-length <i>port</i><br>
flush-output-port <i>port</i><br>
peek-char <i>port</i><br>
port-name <i>port</i><br>
read-char <i>port</i><br>
unread-char <i>char</i> <i>port</i><br>
write-char <i>char</i> <i>port</i></tt>
<p>Additional messages may be accepted by user-defined ports.
<p>
<i>Chez Scheme</i> input and output is normally buffered for efficiency.
To support buffering, each input port contains an input buffer and
each output port contains an output buffer.
Bidirectional ports, ports that are both input ports and output
ports, contain both input and output buffers.
Input is not buffered if the input buffer is the empty string,
and output is not buffered if the output buffer is the empty
string.
In the case of unbuffered input and output, calls to <tt>read-char</tt>,
<tt>write-char</tt>, and similar messages cause the handler to be invoked
immediately with the corresponding message.
For buffered input and output, calls to these procedures cause the
buffer to be updated, and the handler is not called under normal
circumstances until the buffer becomes empty (for input) or full (for
output).
Handlers for buffered ports must <tt><i>not</i></tt> count
on the buffer being empty or full when <tt>read-char</tt>, <tt>write-char</tt>, and
similar messages are received, however, due to the possibility that (a)
the handler is invoked through some other mechanism, or (b) the
call to the handler is interrupted.
<p>
In the presence of keyboard, timer, and other interrupts, it is
possible for a call to a port handler to be interrupted or for the
handler itself to be interrupted.
If the port is accessible outside of the interrupted code, there
is a possibility that the interrupt handler will cause input or
output to be performed on the port.
This is one reason, as stated above, that port handlers must not count
on the input buffer being empty or output buffer being full when a
<tt>read-char</tt>, <tt>write-char</tt>, or similar message is received.
In addition, port handlers may need to manipulate the buffers only
with interrupts disabled (using <tt>with-interrupts-disabled</tt>).
<p>
Generic ports are created via one of the port construction
procedures <tt>make-input-port</tt>,
<tt>make-output-port</tt>, and <tt>make-input/output-port</tt> defined
later in this chapter.
Ports have seven accessible fields:
<p>
<dl compact>
<dt><tt><i>handler</i></tt>,<dd> accessed with <tt>port-handler</tt>;
<dt><tt><i>output-buffer</i></tt>,<dd> accessed with <tt>port-output-buffer</tt>,
<dt><tt><i>output-size</i></tt>,<dd> accessed with <tt>port-output-size</tt>,
<dt><tt><i>output-index</i></tt>,<dd> accessed with <tt>port-output-index</tt>,
<dt><tt><i>input-buffer</i></tt>,<dd> accessed with <tt>port-input-buffer</tt>,
<dt><tt><i>input-size</i></tt>,<dd> accessed with <tt>port-input-size</tt>, and
<dt><tt><i>input-index</i></tt>,<dd> accessed with <tt>port-input-index</tt>.
</dl>
<p>
The output-size and output-index fields are valid only for output
ports, and the input-size and input-index fields are valid only for
input ports.
The output and input size and index fields may be updated as well
using the corresponding "<tt>set-<i>field</i>!</tt>" procedure.
<p>
A port's output size determines how much of the port's output buffer is
actually available for writing by <tt>write-char</tt>.
The output size is often the same as the string length of the port's
output buffer, but it can be set to less (but no less than zero) at the
discretion of the programmer.
The output index determines to which position in the port's
buffer the next character will be written.
The output index should be between 0 and the output size,
inclusive.
If no output has occurred since the buffer was last flushed, the
output index should be 0.
If the index is less than the size, <tt>write-char</tt>
stores its character argument into the specified character
position within the buffer and increments the index.
If the index is equal to the size, <tt>write-char</tt> leaves the fields of
the port unchanged and invokes the handler.
<p>
A port's input size determines how much of the port's input buffer is
actually available for reading by <tt>read-char</tt>.
A port's input size and input index are constrained in the same manner
as output size and index, i.e., the input size must be between
0 and the string length of the input buffer (inclusive), and the input
index must be between 0 and the input size (inclusive).
Often, the input size is less than the length of the input buffer
because there are fewer characters available to read than would fit
in the buffer.
The input index determines from which position in the input buffer the
next character will be read.
If the index is less than the size, <tt>read-char</tt> extracts the character
in this position, increments the index, and returns the character.
If the index is equal to the size, <tt>read-char</tt> leaves the fields of
the port unchanged and invokes the handler.
<p>
The operation of <tt>peek-char</tt> is similar to that of <tt>read-char</tt>, except
that it does not increment the input index.
<tt>unread-char</tt> decrements the input index if it is greater than 0,
otherwise it invokes the handler.
<tt>char-ready?</tt> returns <tt>#t</tt> if the input index is less than the input
size, otherwise it invokes the handler.
<p>
Although the fields shown and discussed above are logically present in
a port, actual implementation details may differ.
The current <i>Chez Scheme</i> implementation uses a different representation
that allows <tt>read-char</tt>, <tt>write-char</tt>, and similar operations to be
open-coded with minimal overhead.
The access and assignment operators perform the conversion between the
actual representation and the one shown above.
<p>
Port handlers receiving a message must return a value appropriate for
the corresponding operation.
For example, a handler receiving a <tt>read-char</tt> message must return a
character or eof object (if it returns).
For operations that return unspecified values, such as <tt>close-port</tt>,
the handler is not required to return any particular value.
<p>
<h3><a name="g72"></a><a name="./io:h2"></a>Section 9.2. File Options<a name="SECTFILEOPTIONS"></a></h3>
<p>
The Revised<sup>6</sup> Report requires that the universe of a file-options
enumeration set must include <tt>no-create</tt>, <tt>no-fail</tt>,
and <tt>no-truncate</tt>, whose meanings are described within the
description of the <tt>file-options</tt> syntax in
Section <a href="http://scheme.com/tspl4/./io.html#g123">7.2</a> of <i>The Scheme Programming Language, 4th Edition</i>.
<i>Chez Scheme</i> defines a number of additional file options:
<p>
<dl compact>
<dt><tt><i>compressed</i></tt>:<dd>
An output file should be compressed when written; and a compressed input
file should be decompressed when read.
<p>
<dt><tt><i>replace</i></tt>:<dd>
For output files only, replace (remove and recreate) the existing file if
it exists.
<p>
<dt><tt><i>exclusive</i></tt>:<dd>
For output files only, lock the file for exclusive access.
On some systems the lock is advisory, i.e., it inhibits access by
other processes only if they also attempt to open exclusively.
<p>
<dt><tt><i>append</i></tt>:<dd>
For output files only, position the output port at the end of the file
before each write so that output to the port is always appended to the
file.
<p>
<dt><tt><i>perm-set-user-id</i></tt>:<dd>
For newly created output files under Unix-based systems only, set
user-id bit.
<p>
<dt><tt><i>perm-set-group-id</i></tt>:<dd>
For newly created output files under Unix-based systems only, set
group-id bit.
<p>
<dt><tt><i>perm-sticky</i></tt>:<dd>
For newly created output files under Unix-based systems only, set
sticky bit.
<p>
<dt><tt><i>perm-no-user-read</i></tt>:<dd>
For newly created output files under Unix-based systems only,
do not set user read bit.
(User read bit is set by default, unless masked by the process umask.)
<p>
<dt><tt><i>perm-no-user-write</i></tt>:<dd>
For newly created output files under Unix-based systems only,
do not set user write bit.
(User write bit is set by default, unless masked by the process umask.)
<p>
<dt><tt><i>perm-user-execute</i></tt>:<dd>
For newly created output files under Unix-based systems only,
set user execute bit unless masked by process umask.
(User execute bit is not set by default.)
<p>
<dt><tt><i>perm-no-group-read</i></tt>:<dd>
For newly created output files under Unix-based systems only,
do not set group read bit.
(Group read bit is set by default, unless masked by the process umask.)
<p>
<dt><tt><i>perm-no-group-write</i></tt>:<dd>
For newly created output files under Unix-based systems only,
do not set group write bit.
(Group write bit is set by default, unless masked by the process umask.)
<p>
<dt><tt><i>perm-group-execute</i></tt>:<dd>
For newly created output files under Unix-based systems only,
set group execute bit unless masked by process umask.
(Group execute bit is not set by default.)
<p>
<dt><tt><i>perm-no-other-read</i></tt>:<dd>
For newly created output files under Unix-based systems only,
do not set other read bit.
(Other read bit is set by default, unless masked by the process umask.)
<p>
<dt><tt><i>perm-no-other-write</i></tt>:<dd>
For newly created output files under Unix-based systems only,
do not set other write bit.
(Other write bit is set by default, unless masked by the process umask.)
<p>
<dt><tt><i>perm-other-execute</i></tt>:<dd>
For newly created output files under Unix-based systems only,
set other execute bit unless masked by process umask.
(Other execute bit is not set by default.)
</dl>
<p>
<h3><a name="g73"></a><a name="./io:h3"></a>Section 9.3. Transcoders<a name="SECTTRANSCODERS"></a></h3>
<p>
The language of the Revised<sup>6</sup> Report provides three built-in codecs:
a latin-1 codec, a utf-8 codec, and a utf-16 codec.
<i>Chez Scheme</i> provides three additional codecs: a utf-16le codec,
utf-16be codec, and an "iconv" codec for non-Unicode character sets.
It also provides an alternative to the standard utf-16 codec that
defaults to little-endian format rather than the default big-endian
format.
This section describes these codecs, plus a <tt>current-transcoder</tt>
parameter that allows the programmer to determine the transcoder
used for a textual port whenever the transcoder is implicit, as for
<tt>open-input-file</tt> or <tt>load</tt>, along with the
predicate <tt>transcoder?</tt>, which should be standard but is not.
<p>
<a name="./io:s7"></a><span class=formdef><b>procedure</b>: <tt>(utf-16-codec)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(utf-16-codec <i>endianness</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(utf-16le-codec)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(utf-16be-codec)</tt></span>
<br>
<b>returns: </b>a codec
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>endianness</i></tt> must be the symbol <tt>big</tt> or the symbol
<tt>little</tt>.
<p>
The codec returned by <tt>utf-16-codec</tt> can be used to create
process data written UFT-16 format.
When called without the <tt><i>endianness</i></tt> argument or with <tt><i>endianness</i></tt>
<tt>big</tt>, <tt>utf-16-codec</tt> returns a codec for standard UTF-16
data, i.e., one that defaults to big-endian format if no byte-order mark
(BOM) is found.
<p>
When output is transcoded with a transcoder based on this codec, a BOM is
emitted just before the first character written, and each character is
written as a UTF-16 character in big-endian format.
For input, a BOM is looked for at the start of the
input and, if present, controls the byte order of the remaining
UTF-16 characters.
If no BOM is present, big-endian order is assumed.
For input-output ports, the BOM is not emitted if the file is
read before written, and a BOM is not looked for if the file is written
before read.
<p>
For textual ports created via <tt>transcoded-port</tt>, a BOM written or
read via the transcoder appears at the beginning of the underlying data
stream or file only if the binary port passed to <tt>transcoded-port</tt>
is positioned at the start of the data stream or file.
When the transcoder can determine this is the case, it sets a flag that
causes <tt>set-port-position!</tt>) to position the port beyond the BOM if
an attempt is made to reposition the port to the start of the data stream
or file, so that the BOM is preserved.
<p>
When called with <tt><i>endianness</i></tt> <tt>little</tt>, <tt>utf-16-codec</tt>
returns a codec that defaults to the little-endian format both for reading
and for writing.
For output-only streams or input/output streams that are written before read,
the result is standard UTF-16, with a BOM that specifies little-endian
format followed by characters in little-endian byte order.
For input-only streams or input/output streams that are read before written,
this codec allows programs to read from input streams that either
begin with a BOM or are encoded in UTF-16LE format.
This is particularly useful for handling files that might have been
produced by older Windows applications that claim to produce UTF-16 files
but actually produce UTF-16LE files.
<p>
The Revised<sup>6</sup> Report version of <tt>utf-16-codec</tt> lacks the
optional <tt><i>endianness</i></tt> argument.
<p>
The codecs returned by <tt>utf-16le-codec</tt> and <tt>utf-16be-codec</tt>
are used to read and write data in the UTF-16LE and UTF-16BE formats,
i.e., UTF-16 with little-endian or big-endian byte order and no BOM.
For output, these codecs are useful for controlling whether and where
the BOM is emitted, since no BOM is emitted implicitly and a BOM
can be emitted explicitly as an ordinary character.
For input, these codecs are useful for processing files known to be
in little-endian or big-endian format with no BOM.
<p>
<a name="./io:s8"></a><span class=formdef><b>procedure</b>: <tt>(iconv-codec <i>code-page</i>)</tt></span>
<br>
<b>returns: </b>a codec
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>code-page</i></tt> must be a string and should identify a codec accepted by
the <tt>iconv</tt> library installed on the target machine.
The codec returned by this procedure can be used to convert from the
non-Unicode single- and multiple-byte character sets supported by
<tt>iconv</tt>.
When used in the input direction, the codec converts byte sequences
into Scheme strings, and when used in the output direction, it converts
Scheme strings to byte sequences.
<p>
The set of supported code pages depends on the version of
<tt>iconv</tt> available; consult the <tt>iconv</tt> documentation
or use the shell command <tt>iconv --list</tt> to obtain a list
of supported code pages.
<p>
While the Windows operating system does not supply an <tt>iconv</tt>
library, it is possible to use <tt>iconv-codec</tt> on Windows systems by
supplying an <tt>iconv</tt> dynamic-link library (named <tt>iconv.dll</tt>,
<tt>libiconv.dll</tt>, or <tt>libiconv-2.dll</tt>) that provides
Posix-conformant <tt>iconv_open</tt>, <tt>iconv</tt>, and
<tt>iconv_close</tt> entry points either under those names or under the
alternative names <tt>libiconv_open</tt>, <tt>libiconv</tt>, and
<tt>libiconv_close</tt>.
The dll must be located in a standard location for dlls or in the
current directory of the process the first time <tt>iconv-codec</tt>
is called.
<p>
<a name="./io:s9"></a><span class=formdef><b>thread parameter</b>: <tt>current-transcoder</tt></span>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The transcoder value of the <tt>current-transcoder</tt> parameter is used
whenever a textual file is opened with an implicit transcoder, e.g., by
<tt>open-input-file</tt> and other convenience I/O procedures,
<tt>compile-file</tt> <tt>include</tt>, <tt>load</tt>, and
<tt>pretty-file</tt>.
Its initial value is the value of the <tt>native-transcoder</tt> procedure.
<p>
<a name="./io:s10"></a><span class=formdef><b>procedure</b>: <tt>(transcoder? <i>obj</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if <tt><i>obj</i></tt> is a transcoder, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>
<h3><a name="g74"></a><a name="./io:h4"></a>Section 9.4. Port Operations<a name="SECTPORTOPERATIONS"></a></h3>
<p>
The procedures used to create, access, and alter ports directly
are described in this section.
Also described are several nonstandard operations on ports.
<p>
Unless otherwise specified, procedures requiring either input ports or
output ports as arguments accept input/output ports as well, i.e., an
input/output port is both an input port and an output port.
<p>
<a name="./io:s11"></a><span class=formdef><b>procedure</b>: <tt>(make-input-port <i>handler</i> <i>input-buffer</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(make-output-port <i>handler</i> <i>output-buffer</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(make-input/output-port <i>handler</i> <i>input-buffer</i> <i>output-buffer</i>)</tt></span>
<br>
<b>returns: </b>a new textual port
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>handler</i></tt> must be a procedure, and
<tt><i>input-buffer</i></tt> and <tt><i>output-buffer</i></tt> must be strings.
Each procedure creates a <a name="./io:s12"></a>generic port.
The handler associated with the port is <tt><i>handler</i></tt>, the
input buffer is <tt><i>input-buffer</i></tt>, and the
output buffer is <tt><i>output-buffer</i></tt>.
For <tt>make-input-port</tt>, the output buffer is undefined, and for
<tt>make-output-port</tt>, the input buffer is undefined.
<p>
The input size of an input or input/output port is initialized to the
string length of the input buffer, and the input index is set to 0.
The output size and index of an output or input/output port are
initialized similarly.
<p>
The length of an input or output buffer may be zero, in which case
buffering is effectively disabled.
<p>
<a name="./io:s13"></a><span class=formdef><b>procedure</b>: <tt>(port-handler <i>port</i>)</tt></span>
<br>
<b>returns: </b>a procedure
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>For generic ports, <tt>port-handler</tt> returns the handler passed to one
of the generic port creation procedures described above.
For ports created by <tt>open-input-file</tt> and similar procedures,
<tt>port-handler</tt> returns an internal handler that may be invoked in
the same manner as any other handler.
<p>
<a name="./io:s14"></a><span class=formdef><b>procedure</b>: <tt>(port-input-buffer <i>input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(port-input-size <i>input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(port-input-index <i>input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-input-buffer <i>textual-input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-input-size <i>textual-input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-input-index <i>textual-input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-input-buffer <i>binary-input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-input-size <i>binary-input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-input-index <i>binary-input-port</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>These procedures return the input buffer, size, or index
of the input port.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
<p>
<a name="./io:s15"></a><span class=formdef><b>procedure</b>: <tt>(set-port-input-index! <i>input-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-port-input-size! <i>input-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-port-input-buffer! <i>input-port</i> <i>x</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-textual-port-input-index! <i>textual-input-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-textual-port-input-size! <i>textual-input-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-textual-port-input-buffer! <i>textual-input-port</i> <i>string</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-binary-port-input-index! <i>binary-input-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-binary-port-input-size! <i>binary-input-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-binary-port-input-buffer! <i>binary-input-port</i> <i>bytevector</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The procedure <tt>set-port-input-index!</tt> sets the input index field of
<tt><i>input-port</i></tt> to <i>n</i>, which must be a nonnegative integer less than
or equal to the port's input size.
<p>
The procedure <tt>set-port-input-size!</tt> sets the input size field of
<tt><i>input-port</i></tt> to <i>n</i>, which must be a nonnegative integer less than
or equal to the string length of the port's input buffer.
It also sets the input index to 0.
<p>
The procedure <tt>set-port-input-buffer!</tt> sets the input buffer field of
<tt><i>input-port</i></tt> to <i>x</i>, which must be a string for textual ports and a
bytevector for binary ports.
It also sets the input size to the length of the string or bytevector
and the input index to 0.
<p>
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
<p>
<a name="./io:s16"></a><span class=formdef><b>procedure</b>: <tt>(port-input-count <i>input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-input-count <i>textual-input-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-input-count <i>binary-input-port</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>These procedures return an exact integer representing the number of
characters or bytes left to be read from the port's input buffer, i.e.,
the difference between the buffer size and index.
<p>
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterpart.
<p>
<a name="./io:s17"></a><span class=formdef><b>procedure</b>: <tt>(port-input-empty? <i>input-port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port's input buffer contains no more data, otherwise <tt>#f</tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>This procedure determines whether the port's input count is zero without
computing or returning the actual count.
<p>
<a name="./io:s18"></a><span class=formdef><b>procedure</b>: <tt>(port-output-buffer <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(port-output-size <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(port-output-index <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-output-buffer <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-output-size <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-output-index <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-output-buffer <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-output-size <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-output-index <i>output-port</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>These procedures return the output buffer, size, or index
of the output port.
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
<p>
<a name="./io:s19"></a><span class=formdef><b>procedure</b>: <tt>(set-port-output-index! <i>output-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-port-output-size! <i>output-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-port-output-buffer! <i>output-port</i> <i>x</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-textual-port-output-index! <i>textual-output-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-textual-port-output-size! <i>textual-output-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-textual-port-output-buffer! <i>textual-output-port</i> <i>string</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-binary-port-output-index! <i>output-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-binary-port-output-size! <i>output-port</i> <i>n</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(set-binary-port-output-buffer! <i>binary-output-port</i> <i>bytevector</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The procedure <tt>set-port-output-index!</tt> sets the output index field of
the output port to <i>n</i>, which must be a nonnegative integer less than
or equal to the port's output size.
<p>
The procedure <tt>set-port-output-size!</tt> sets the output size field of
the output port to <i>n</i>, which must be a nonnegative integer less than
or equal to the string length of the port's output buffer.
It also sets the output index to 0.
<p>
The procedure <tt>set-port-output-buffer!</tt> sets the output buffer field of
<tt><i>output-port</i></tt> to <i>x</i>, which must be a string for textual ports and a
bytevector for binary ports.
It also sets the output size to the length of the string or bytevector
and the output index to 0.
<p>
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterparts.
<p>
<a name="./io:s20"></a><span class=formdef><b>procedure</b>: <tt>(port-output-count <i>output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(textual-port-output-count <i>textual-output-port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(binary-port-output-count <i>binary-output-port</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>These procedures return an exact integer representing the amount of
space in characters or bytes available to be written in the
port's output buffer, i.e., the difference between the buffer size
and index.
<p>
The variants specialized to textual or binary ports are slightly
more efficient than their generic counterpart.
<p>
<a name="./io:s21"></a><span class=formdef><b>procedure</b>: <tt>(port-output-full? <i>output-port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port's input buffer has no more room, otherwise <tt>#f</tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>This procedure determines whether the port's output count is zero without
computing or returning the actual count.
<p>
<a name="./io:s22"></a><span class=formdef><b>procedure</b>: <tt>(mark-port-closed! <i>port</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>This procedure directly marks the port closed so that no further
input or output operations are allowed on it.
It is typically used by handlers upon receipt of a <tt>close-port</tt>
message.
<p>
<a name="./io:s23"></a><span class=formdef><b>procedure</b>: <tt>(port-closed? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if <tt><i>port</i></tt> is closed, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>
<p><tt>(let ([p (open-output-string)])<br>
(port-closed? p)) <img src="math/csug/0.gif" alt="<graphic>"> #f
<br>
<br>
(let ([p (open-output-string)])<br>
(close-port p)<br>
(port-closed? p)) <img src="math/csug/0.gif" alt="<graphic>"> #t</tt>
<p><a name="./io:s24"></a><a name="desc:set-port-bol"></a>
<span class=formdef><b>procedure</b>: <tt>(set-port-bol! <i>output-port</i> <i>obj</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>When When <tt><i>obj</i></tt> is <tt>#f</tt>, the port's beginning-of-line (BOL)
flag is cleared; otherwise, the port's BOL flag is set.
<p>
The BOL flag is consulted by <tt>fresh-line</tt>
(page <a href="./io.html#desc:fresh-line">230</a>) to determine if it needs to emit a
newline.
This flag is maintained automatically for file output ports, string output
ports, and transcript ports.
The flag is set for newly created file and string output ports, except
for file output ports created with the <tt>append</tt> option, for which
the flag is reset.
The BOL flag is clear for newly created generic ports and never set
automatically, but may be set explicitly using <tt>set-port-bol!</tt>.
The port is always flushed immediately before the flag is consulted, so it
need not be maintained on a per-character basis for buffered ports.
<p>
<a name="./io:s25"></a><span class=formdef><b>procedure</b>: <tt>(port-bol? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if <tt><i>port</i></tt>'s BOL flag is set, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><a name="./io:s26"></a><span class=formdef><b>procedure</b>: <tt>(set-port-eof! <i>input-port</i> <i>obj</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>When <tt><i>obj</i></tt> is not <tt>#f</tt>, <tt>set-port-eof!</tt> marks <tt><i>input-port</i></tt>
so that, once its buffer is empty, the port is treated as if it were at
eof even if more data is available in the underlying byte or character
stream.
Once this artificial eof has been read, the eof mark is cleared, making
any additional data in the stream available beyond the eof.
This feature can be used by a generic port to simulate a stream consisting
of multiple input files.
<p>
When <tt><i>obj</i></tt> is <tt>#f</tt>, the eof mark is cleared.
<p>
The following example assumes /dev/zero provides an infinite stream of
zero bytes.
<p>
<p><tt>(define p<br>
(parameterize ([file-buffer-size 3])<br>
(open-file-input-port "/dev/zero")))<br>
(set-port-eof! p #t)<br>
(get-u8 p) <img src="math/csug/0.gif" alt="<graphic>"> #!eof<br>
(get-u8 p) <img src="math/csug/0.gif" alt="<graphic>"> 0<br>
(set-port-eof! p #t)<br>
(get-u8 p) <img src="math/csug/0.gif" alt="<graphic>"> 0<br>
(get-u8 p) <img src="math/csug/0.gif" alt="<graphic>"> 0<br>
(get-u8 p) <img src="math/csug/0.gif" alt="<graphic>"> #!eof<br>
(get-u8 p) <img src="math/csug/0.gif" alt="<graphic>"> 0</tt>
<p><a name="./io:s27"></a><span class=formdef><b>procedure</b>: <tt>(port-name <i>port</i>)</tt></span>
<br>
<b>returns: </b>the name associated with <tt><i>port</i></tt>
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>The name may be any object but is usually a string or <tt>#f</tt>
(denoting no name).
For file ports, the name is typically a string naming the file.
<p>
<p><tt>(let ([p (open-input-file "myfile.ss")])<br>
(port-name p)) <img src="math/csug/0.gif" alt="<graphic>"> "myfile.ss"
<br>
<br>
(let ([p (open-output-string)])<br>
(port-name p)) <img src="math/csug/0.gif" alt="<graphic>"> "string"</tt>
<p><a name="./io:s28"></a><span class=formdef><b>procedure</b>: <tt>(set-port-name! <i>port</i> <i>obj</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>This procedure sets <tt><i>port</i></tt>'s name to <tt><i>obj</i></tt>, which should be
a string or <tt>#f</tt> (denoting no name).
<p>
<a name="./io:s29"></a><span class=formdef><b>procedure</b>: <tt>(port-length <i>port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(file-length <i>port</i>)</tt></span>
<br>
<b>returns: </b>the length of the file or other object to which <tt><i>port</i></tt> refers
<br><span class=formdef><b>procedure</b>: <tt>(port-has-port-length? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port supports <tt>port-length</tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>A port may allow the length of the underlying stream of characters or bytes
to be determined.
If so, the procedure <tt>port-has-port-length?</tt> returns
<tt>#t</tt> and <tt>port-length</tt> returns the current length.
For binary ports, the length is always an exact nonnegative integer byte
count.
For textual ports, the representation of a length is unspecified; it
may not be an exact nonnegative integer and, even if it is, it may not
represent either a byte or character count.
The length may be used at some later time to reset the length if the
port supports <tt>set-port-length!</tt>.
If <tt>port-length</tt> is called on a port that does not support it,
an exception with condition type <tt>&assertion</tt> is raised.
<p>
File lengths beyond 2<sup>32</sup> might not be reported property
for compressed files on 32-bit versions of the system.
<p>
<tt>file-length</tt> is identical to <tt>port-length</tt>.
<p>
<a name="./io:s30"></a><span class=formdef><b>procedure</b>: <tt>(set-port-length! <i>port</i> <i>len</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br><span class=formdef><b>procedure</b>: <tt>(port-has-set-port-length!? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port supports <tt>set-port-length!</tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>A port may allow the length of the underlying stream of characters or bytes
to be set, i.e., extended or truncated.
If so, the procedure <tt>port-has-set-port-length!?</tt> returns
<tt>#t</tt> and <tt>set-port-length!</tt> changes the length.
For binary ports, the length <tt><i>len</i></tt> must be an exact nonnegative integer byte
count.
For textual ports, the representation of a length is unspecified, as
described in the entry for <tt>port-length</tt> above, but <tt><i>len</i></tt> must be
an appropriate length for the textual port, which is usually guaranteed
to be the case only if it was obtained from a call to <tt>port-length</tt>
on the same port.
If <tt>set-port-length!</tt> is called on a port that does not support it,
an exception with condition type <tt>&assertion</tt> is raised.
<p>
It is not possible to set the length of a port opened with compression
to an arbitrary position, and the result of an attempt to set the length
of a compressed file beyond 2<sup>32</sup> on 32-bit versions of the system is
undefined.
<p>
<a name="./io:s31"></a><span class=formdef><b>procedure</b>: <tt>(port-nonblocking? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port is in nonblocking mode, <tt>#f</tt> otherwise
<br><span class=formdef><b>procedure</b>: <tt>(port-has-port-nonblocking?? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port supports <tt>port-nonblocking?</tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>A port may allow the nonblocking status of the port to be determined.
If so, the procedure <tt>port-has-port-nonblocking??</tt> returns
<tt>#t</tt> and <tt>port-nonblocking?</tt> returns a boolean value
reflecting whether the port is in nonblocking mode.
<p>
<a name="./io:s32"></a><span class=formdef><b>procedure</b>: <tt>(set-port-nonblocking! <i>port</i> <i>obj</i>)</tt></span>
<br>
<b>returns: </b>unspecified
<br><span class=formdef><b>procedure</b>: <tt>(port-has-set-port-nonblocking!? <i>port</i>)</tt></span>
<br>
<b>returns: </b><tt>#t</tt> if the port supports <tt>set-port-nonblocking!</tt>, <tt>#f</tt> otherwise
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>A port may allow reads or writes to be performed in a "nonblocking" fashion.
If so, the procedure <tt>port-has-set-port-nonblocking!?</tt> returns
<tt>#t</tt> and <tt>set-port-nonblocking!</tt> sets the port to nonblocking
mode (if <tt><i>obj</i></tt> is a true value) or blocking mode (if <tt><i>obj</i></tt> is <tt>#f</tt>).
If <tt>set-port-nonblocking!</tt> is called on a port that does not support it,
an exception with condition type <tt>&assertion</tt> is raised.
<p>
Ports created by the standard Revised<sup>6</sup> port opening procedures are
initially set in blocking mode by default.
The same is true for most of the procedures described in this document.
A generic port based on a nonblocking source may be nonblocking
initially.
A port returned by <tt>open-fd-input-port</tt>,
<tt>open-fd-output-port</tt>, or <tt>open-fd-input/output-port</tt>
is initially in nonblocking mode if the file-descriptor passed in is in
nonblocking mode.
Similarly, a port returned by <tt>standard-input-port</tt>,
<tt>standard-output-port</tt>, or <tt>standard-error-port</tt> is
initially in nonblocking mode if the underlying stdin, stdout,
or stderr file descriptor is in nonblocking mode.
<p>
Although <tt>get-bytevector-some</tt> and <tt>get-string-some</tt> normally
cannot return an empty bytevector or empty string, they can if the port
is in nonblocking mode and no input is available.
Also, <tt>get-bytevector-some!</tt>, <tt>get-string-some!</tt>
may not read any data if the port is in nonblocking mode and
no data is available.
Similarly, <tt>put-bytevector-some</tt> and <tt>put-string-some</tt>
may not write any data if the port is in nonblocking mode and
no room is available.
<p>
Nonblocking mode is not supported under Windows.
<p>
<a name="./io:s33"></a><span class=formdef><b>procedure</b>: <tt>(file-position <i>port</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(file-position <i>port</i> <i>pos</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p>When the second argument is omitted, this procedure behaves like the R6RS
<tt>port-position</tt> procedure, and when present, like the R6RS
<tt>set-port-position!</tt> procedure.
<p>
For compressed files opened with the <tt>compressed</tt> flag,
<tt>file-position</tt> returns the position in the
uncompressed stream of data.
Changing the position of a compressed input file opened with the
<tt>compressed</tt> flag generally requires rewinding and rereading the
file and might thus be slow.
The position of a compressed output file opened with the
<tt>compressed</tt> flag can be moved forward only; this is
accomplished by writing a (compressed) sequence of zeros.
File positions beyond 2<sup>32</sup> might not be reported property
for compressed files on 32-bit versions of the system.
<p>
<a name="./io:s34"></a><span class=formdef><b>procedure</b>: <tt>(clear-input-port)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(clear-input-port <i>input-port</i>)</tt></span>