File: portio.tex

package info (click to toggle)
r6rs-doc 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,868 kB
  • ctags: 2,046
  • sloc: lisp: 5,409; makefile: 190
file content (1392 lines) | stat: -rw-r--r-- 56,103 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
\section{Port I/O}
\label{portsiosection}

The \defrsixlibrary{io ports} library defines an I/O layer for
conventional, imperative buffered input and output.
A \defining{port} represents a buffered access object
for a data sink or source or both simultaneously.
The library allows ports to be created from arbitrary data sources
and sinks.

The \rsixlibrary{io ports} library distinguishes between \textit{input
  ports\mainindex{input port}} and \textit{output
  ports\mainindex{output ports}}.  An input port is a source for data,
whereas an output port is a sink for data.  A port may be both an
input port and an output port; such a port typically provides
simultaneous read and write access to a file or other data.

The \rsixlibrary{io ports} library also distinguishes between
\textit{binary ports\mainindex{binary port}}, which are sources
or sinks for uninterpreted bytes, and
\textit{textual ports\mainindex{textual ports}}, which are sources
or sinks for characters and strings.

This section uses \var{input-port}, \var{output-port},
\var{binary-port}, \var{textual-port},
\var{binary-input-port}, \var{textual-input-port},
\var{binary-output-port}, \var{textual-output-port},
and \var{port} as
parameter names for arguments that must be input ports (or combined
input/output ports), output ports (or combined input/output ports),
binary ports, textual ports, binary input ports, textual input ports,
binary output ports, textual output ports, or any kind of port,
respectively.

\subsection{File names}
\label{filenamesection}

Some of the procedures described in this chapter accept a file name as an
argument. Valid values for such a file name include strings that name a file
using the native notation of filesystem paths on an implementation's
underlying operating system, and may include implementation-dependent
values as well.

A \var{filename} parameter name means that the
corresponding argument must be a file name.

\subsection{File options}
\label{fileoptionssection}

\mainindex{file options}
When opening a file, the various procedures in this library accept a
{\cf file-options} object that encapsulates flags to specify how
the file is to be opened. A {\cf file-options} object is an enum-set
(see chapter~\ref{enumerationschapter}) over the symbols constituting
valid file options.
A \var{file-options} parameter name means that the
corresponding argument must be a file-options object.

\begin{entry}{%
\proto{file-options}{ \hyper{file-options symbol} \dotsfoo}{\exprtype}}

\domain{Each \hyper{file-options symbol} must be a symbol.}
The {\cf file-options} syntax returns a file-options object that 
encapsulates the
specified options.

When supplied to an operation that opens a file for output, the
file-options object returned by {\cf (file-options)} specifies that the
file is created if it does not exist and an exception with condition type
{\cf\&i/o-file-already-exists} is raised if it does exist.
The following standard options can be included to modify the default behavior.

\begin{itemize}   
\item {\cf no-create}
      If the file does not already exist, it is not created;
      instead, an exception with condition type {\cf\&i/o-file-does-not-exist}
      is raised.
      If the file already exists, the exception with condition type
      {\cf\&i/o-file-already-exists} is not raised
      and the file is truncated to zero length.
\item {\cf no-fail}
      If the file already exists, the exception with condition type
      {\cf\&i/o-file-already-exists} is not raised,
      even if {\cf no-create} is not included,
      and the file is truncated to zero length.
\item {\cf no-truncate}
      If the file already exists and the exception with condition type
      {\cf\&i/o-file-already-exists} has been inhibited by inclusion of
      {\cf no-create} or {\cf no-fail}, the file is not truncated, but
      the port's current position is still set to the beginning of the
      file.
\end{itemize}

These options have no effect when a file is opened only for input.
Symbols
other than those listed above may be used as \hyper{file-options symbol}s;
they have implementation-specific meaning, if any.

\begin{note}
  Only the name of \hyper{file-options symbol} is significant.
\end{note}
\end{entry}   

\subsection{Buffer modes}

Each port has an associated buffer mode.  For an output port, the
buffer mode defines when an output operation flushes the buffer
associated with the output port.  For an input port, the buffer mode
defines how much data will be read to satisfy read operations.  The
possible buffer modes are the symbols {\cf none} for no buffering,
{\cf line} for flushing upon line endings and reading up to line
endings, or other implementation-dependent behavior,
and {\cf block} for arbitrary buffering.  This section uses
the parameter name \var{buffer-mode} for arguments that must be
buffer-mode symbols.

If two ports are connected to the same mutable source, both ports
are unbuffered, and reading a byte or character from that shared
source via one of the two ports would change the bytes or characters
seen via the other port, a lookahead operation on one port will
render the peeked byte or character inaccessible via the other port,
while a subsequent read operation on the peeked port will see the
peeked byte or character even though the port is otherwise unbuffered.

In other words, the semantics of buffering is defined in terms of side
effects on shared mutable sources, and a lookahead operation has the
same side effect on the shared source as a read operation.

\begin{entry}{%
\proto{buffer-mode}{ \hyper{buffer-mode symbol}}{\exprtype}}
   
\domain{\hyper{Buffer-mode symbol} must be a symbol whose name is one
  of {\cf none}, {\cf line}, and {\cf block}.} The result is the
corresponding symbol, and specifies the associated buffer mode.

\begin{note}
  Only the name of \hyper{buffer-mode symbol} is significant.
\end{note}
\end{entry}

\begin{entry}{%
\proto{buffer-mode?}{ obj}{procedure}}
   
Returns \schtrue{} if the argument is a valid buffer-mode symbol,
and returns \schfalse{} otherwise.
\end{entry}

\subsection{Transcoders}
\label{transcoderssection}

Several different Unicode encoding schemes describe standard ways to
encode characters and strings as byte sequences and to decode those
sequences~\cite{Unicode}.
Within this document, a \defining{codec} is an immutable Scheme
object that represents a Unicode or similar encoding scheme.

An \defining{end-of-line style} is a symbol that, if it is not {\cf
  none}, describes how a textual port transcodes representations of
line endings.

A \defining{transcoder} is an immutable Scheme object that combines
a codec with an end-of-line style and a method for handling
decoding errors.
Each transcoder represents some specific bidirectional (but not
necessarily lossless), possibly stateful translation between byte
sequences and Unicode characters and strings.
Every transcoder can operate in the input direction (bytes to characters)
or in the output direction (characters to bytes). 
A \var{transcoder} parameter name means that the corresponding
argument must be a transcoder.

A \defining{binary port} is a port that supports binary I/O, does not
have an associated transcoder and does not support textual I/O.  A
\defining{textual port} is a port that supports textual I/O, and does
not support binary I/O.  A textual port may or may not have an
associated transcoder.

\begin{entry}{%
\proto{latin-1-codec}{}{procedure}
\proto{utf-8-codec}{}{procedure}
\proto{utf-16-codec}{}{procedure}}

These are predefined codecs for the ISO 8859-1, UTF-8,
and UTF-16 encoding schemes \cite{Unicode}.

A call to any of these procedures returns a value that is equal in the
sense of {\cf eqv?} to the result of any other call to the same
procedure.
\end{entry}

\begin{entry}{%
\proto{eol-style}{ \hyper{eol-style symbol}}{\exprtype}}

\domain{\hyper{Eol-style symbol} should be a symbol whose name is one
  of {\cf lf}, {\cf cr}, {\cf crlf}, {\cf nel},
  {\cf crnel}, {\cf ls}, and {\cf none}.}  The form evaluates to the
corresponding symbol.  If the name of \var{eol-style symbol} is not
one of these symbols, the effect and result are
implementation-dependent; in particular, the result may be an
eol-style symbol acceptable as an \var{eol-style} argument to {\cf
  make-transcoder}.  Otherwise, an exception is raised.

All eol-style symbols except {\cf none} describe a specific
line-ending encoding:

\noindent\begin{tabular}{ll}
{\cf lf} & \meta{linefeed}\\
{\cf cr} & \meta{carriage return}\\
{\cf crlf} & \meta{carriage return} \meta{linefeed}\\
{\cf nel} & \meta{next line}\\
{\cf crnel} & \meta{carriage return} \meta{next line}\\
{\cf ls} & \meta{line separator}
\end{tabular}

For a textual port with a transcoder, and whose transcoder has an eol-style symbol {\cf
  none}, no conversion occurs.  For a textual input port, any
eol-style symbol other than {\cf none} means that all of the above
line-ending encodings are recognized and are translated into a single
linefeed.  For a textual output port, {\cf none} and {\cf lf} are
equivalent.  Linefeed characters are encoded according to the
specified eol-style symbol, and all other characters that participate
in possible line endings are encoded as is.

\begin{note}
  Only the name of \hyper{eol-style symbol} is significant.
\end{note}
\end{entry}

\begin{entry}{%
\proto{native-eol-style}{}{procedure}}

Returns the default end-of-line style of the underlying platform, e.g.,
{\cf lf} on Unix and {\cf crlf} on Windows.
\end{entry}

\begin{entry}{%
\ctproto{i/o-decoding}
\proto{make-i/o-decoding-error}{ port}{procedure}
\proto{i/o-decoding-error?}{ obj}{procedure}}

This condition type could be defined by
%
\begin{scheme}
(define-condition-type \&i/o-decoding \&i/o-port
  make-i/o-decoding-error i/o-decoding-error?)%
\end{scheme}

An exception with this type is raised when one of the operations for
textual input from a port encounters a sequence of bytes that cannot
be translated into a character or string by the input direction of the
port's transcoder.

When such an exception is raised, the port's position is past
the invalid encoding.
\end{entry}

\begin{entry}{% 
\ctproto{i/o-encoding}
\proto{make-i/o-encoding-error}{ port char}{procedure}
\proto{i/o-encoding-error?}{ obj}{procedure}
\proto{i/o-encoding-error-char}{ condition}{procedure}}

This condition type could be defined by
%
\begin{scheme}
(define-condition-type \&i/o-encoding \&i/o-port
  make-i/o-encoding-error i/o-encoding-error?
  (char i/o-encoding-error-char))%
\end{scheme}

An exception with this type is raised when one of the operations for
textual output to a port encounters a character that cannot be
translated into bytes by the output direction of the port's transcoder.
\var{Char} is the character that could not be encoded.
\end{entry}

\begin{entry}{%
\proto{error-handling-mode}{ \hyper{error-handling-mode symbol}}{\exprtype}}

\domain{\hyper{Error-handling-mode symbol} should be a symbol whose
  name is one of {\cf ignore}, {\cf raise}, and {\cf replace}.} The
form evaluates to the corresponding symbol.  If
\var{error-handling-mode symbol} is not one of these identifiers,
effect and result are implementation-dependent: The result may be an
error-handling-mode symbol acceptable as a \var{handling-mode}
argument to {\cf make-transcoder}.  If it is not acceptable as a
\var{handling-mode} argument to {\cf make-transcoder}, an exception is
raised.

\begin{note}
  Only the name of \hyper{error-handling-style symbol} is significant.
\end{note}

The error-handling mode of a transcoder specifies the behavior
of textual I/O operations in the presence of encoding or decoding
errors.

If a textual input operation encounters an invalid or incomplete
character encoding, and the error-handling mode is {\cf ignore},
an appropriate number of bytes of the
invalid encoding are ignored and decoding continues with the
following bytes.
If the error-handling mode is {\cf replace}, the replacement
character U+FFFD is injected into the data stream, an appropriate
number of bytes are ignored, and decoding
continues with the following bytes.
If the error-handling mode is {\cf raise}, an 
exception with condition type {\cf\&i/o-decoding} is raised.

If a textual output operation encounters a character it cannot encode,
and the error-handling mode is {\cf ignore}, the character is
ignored and encoding continues with the next character.
If the error-handling mode is {\cf replace}, a codec-specific
replacement character is emitted by the transcoder, and encoding
continues with the next character.
The replacement character is U+FFFD for transcoders whose codec
is one of the Unicode encodings, but is the {\cf ?}
character for the Latin-1 encoding.
If the error-handling mode is {\cf raise}, an
exception with condition type {\cf\&i/o-encoding} is raised.
\end{entry}

\begin{entry}{%
\proto{make-transcoder}{ codec}{procedure}
\rproto{make-transcoder}{ codec eol-style}{procedure}
\rproto{make-transcoder}{ codec eol-style handling-mode}{procedure}}

\domain{\var{Codec} must be a codec; \var{eol-style}, if present, an
  eol-style symbol; and \var{handling-mode}, if present, an
  error-handling-mode symbol.}  \var{Eol-style} may be omitted, in
which case it defaults to the native end-of-line style of the
underlying platform.  \var{Handling-mode} may be omitted, in which
case it defaults to {\cf replace}.  The result is a transcoder with the
behavior specified by its arguments.
\end{entry}

\begin{entry}{
\proto{native-transcoder}{}{procedure}}

Returns an implementation-dependent transcoder that represents a
possibly locale-dependent ``native'' transcoding.
\end{entry}

\begin{entry}{%
\proto{transcoder-codec}{ transcoder}{procedure}
\proto{transcoder-eol-style}{ transcoder}{procedure}
\proto{transcoder-error-handling-mode}{ transcoder}{procedure}}

These are accessors for transcoder objects; when applied to a
transcoder returned by {\cf make-transcoder}, they return the
\var{codec}, \var{eol-style}, and \var{handling-mode} arguments,
respectively.
\end{entry}

\begin{entry}{%
\proto{bytevector->string}{ bytevector transcoder}{procedure}}

Returns the string that results from transcoding the
\var{bytevector} according to the input direction of the transcoder.
\end{entry}

\begin{entry}{%
\proto{string->bytevector}{ string transcoder}{procedure}}

Returns the bytevector that results from transcoding the
\var{string} according to the output direction of the transcoder.
\end{entry}

\subsection{End-of-file object}
\label{eofsection}

The end-of-file object is returned by various I/O procedures when they
reach end of file.\index{end-of-file object}

\begin{entry}{%
\proto{eof-object}{}{procedure}}

Returns the end-of-file object.
\begin{scheme}
(eqv? (eof-object) (eof-object)) \lev  \schtrue
(eq? (eof-object) (eof-object)) \lev  \schtrue%
\end{scheme}
\end{entry}

\begin{note}
  The end-of-file object  is not a datum value, and thus has no external
  representation.
\end{note}

\begin{entry}{%
\proto{eof-object?}{ obj}{procedure}}
  
Returns \schtrue{} if \var{obj} is the end-of-file object, \schfalse{} otherwise.
\end{entry}

\subsection{Input and output ports}

The operations described in this section are common to input and
output ports, both binary and textual.  A port may also have an
associated \defining{position} that specifies a particular place
within its data sink or source, and may also provide operations for
inspecting and setting that place.

\begin{entry}{%
\proto{port?}{ obj}{procedure}}
   
Returns \schtrue{} if the argument is a port, and returns \schfalse{}
otherwise.
\end{entry}

\begin{entry}{%
\proto{port-transcoder}{ port}{procedure}}

Returns the transcoder associated with \var{port} if \var{port} is
textual and has an associated transcoder, and returns \schfalse{} if
\var{port} is binary or does not have an associated transcoder.
\end{entry}

\begin{entry}{%
\proto{textual-port?}{ port}{procedure}
\proto{binary-port?}{ port}{procedure}}

The {\cf textual-port?} procedure returns \schtrue{} if \var{port} is
textual, and returns \schfalse{} otherwise.
The {\cf binary-port?} procedure returns \schtrue{} if \var{port} is
binary, and returns \schfalse{} otherwise.
\end{entry}

\begin{entry}{%
\proto{transcoded-port}{ binary-port transcoder}{procedure}}

The {\cf transcoded-port} procedure
returns a new textual port with the specified \var{transcoder}.
Otherwise the new textual port's state is largely the same as
that of \var{binary-port}.
If \var{binary-port} is an input port, the new textual
port will be an input port and
will transcode the bytes that have not yet been read from
\var{binary-port}.
If \var{binary-port} is an output port, the new textual
port will be an output port and
will transcode output characters into bytes that are
written to the byte sink represented by \var{binary-port}.

As a side effect, however, {\cf transcoded-port}
closes \var{binary-port} in
a special way that allows the new textual port to continue to
use the byte source or sink represented by \var{binary-port},
even though \var{binary-port} itself is closed and cannot
be used by the input and output operations described in this
chapter.
\end{entry}

\begin{entry}{%
\proto{port-has-port-position?}{ port}{procedure}
\proto{port-position}{ port}{procedure}}

The {\cf port-has-port-position?} procedure returns \schtrue{} if the
port supports the {\cf port-position} operation, and \schfalse{}
otherwise.

For a binary port, the {\cf port-position} procedure returns the index
of the position at which the next byte would be read from or written
to the port as an exact non-negative integer object.  For a textual
port, {\cf port-position} returns a value of some implementation-dependent
type representing the port's position; this value may be useful only as
the \var{pos} argument to {\cf set-port-position!}, if the latter is
supported on the port (see below).

If the port does not support the operation, {\cf port-position} raises
an exception with condition type {\cf\&assertion}.

\begin{note}
  For a textual port, the port position may or may not be an integer
  object.  If it is an integer object, the integer object does not
  necessarily correspond to a byte or character position.
\end{note}
\end{entry}   

\begin{entry}{%
\proto{port-has-set-port-position!?}{ port}{procedure}
\proto{set-port-position!}{ port pos}{procedure}}

\domain{If \var{port} is a binary port, \var{pos} should be a
  non-negative exact integer object.  If \var{port} is a textual port,
  \var{pos} should be the return value of a call to {\cf
    port-position} on \var{port}.}
   
The {\cf port-has-set-port-position!?} procedure returns \schtrue{} if the port
supports the {\cf set-port-position!} operation, and \schfalse{}
otherwise.
   
The {\cf set-port-position!} procedure raises an
exception with condition type {\cf\&assertion}
if the port does not support the operation, 
and an exception with condition type {\cf\&i/o-invalid-position} if
\var{pos} is not in the range of valid positions of \var{port}.
Otherwise, it sets the current position
of the port to \var{pos}.  If \var{port} is an output
port, {\cf set-port-position!} first flushes \var{port}.  (See {\cf
  flush-output-port}, section~\ref{flush-output-port}.)

If \var{port} is a binary output port and the current position is set
beyond the current end of the data in the underlying data sink, the object is
not extended until new data is written at that position.
The contents of any intervening positions are unspecified.
Binary ports created by {\cf open-file-output-port} and
{\cf open-file-input/output-port} can always be extended in this manner
within the limits of the underlying operating system.
In other cases, attempts to set the port beyond the current end of data
in the underlying object may result in an exception with condition
type {\cf\&i/o-invalid-position}.
\end{entry}

\begin{entry}{%
\proto{close-port}{ port}{procedure}}
   
Closes the port, rendering the port incapable of delivering or
accepting data. If \var{port} is an output port, it is flushed before
being closed.  This has no effect if the port has already been closed.
A closed port is still a port.  The {\cf close-port} procedure returns
\unspecifiedreturn.
\end{entry}

\begin{entry}{%
\proto{call-with-port}{ port proc}{procedure}}
   
\domain{\var{Proc} must accept one argument.}
The {\cf call-with-port} procedure
calls \var{proc} with \var{port} as an argument. If
\var{proc} returns, \var{port} is closed automatically and
the values returned by \var{proc} are returned. If \var{proc} does not
return, \var{port} is not closed automatically, except perhaps when it is
possible to prove that \var{port} will never again be used for an
input or output operation.
\end{entry}

\subsection{Input ports}

An input port allows the reading of an infinite sequence of bytes
or characters punctuated
by end-of-file objects. An input port connected to a finite data
source ends in an infinite sequence of end-of-file objects.

It is unspecified whether a character encoding consisting of several
bytes may have an end of file between the bytes.  If, for example,
{\cf get-char} raises an {\cf\&i/o-decoding} exception because the
character encoding at the port's position is incomplete up to the next
end of file, a subsequent call to {\cf get-char} may successfully
decode a character if bytes completing the encoding are available
after the end of file.

\begin{entry}{%
\proto{input-port?}{ obj}{procedure}}

Returns \schtrue{} if the argument is an input port (or a combined input
and output port), and returns \schfalse{} otherwise.
\end{entry}

\begin{entry}{%
\proto{port-eof?}{ input-port}{procedure}}
   
Returns \schtrue{}
if the {\cf lookahead-u8} procedure (if \var{input-port} is a binary port)
or the {\cf lookahead-char} procedure (if \var{input-port} is a textual port)
would return
the end-of-file object, and \schfalse{} otherwise.
The operation may block indefinitely if no data is available
but the port cannot be determined to be at end of file.
\end{entry}

\begin{entry}{%
\proto{open-file-input-port}{ filename}{procedure}
\rproto{open-file-input-port}{ filename file-options}{procedure}
\pproto{(open-file-input-port \var{filename}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{file-options} \var{buffer-mode})}\\
\pproto{(open-file-input-port \var{filename}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{file-options} \var{buffer-mode} \var{maybe-transcoder})}}
   
\domain{\var{Maybe-transcoder} must be either a transcoder or \schfalse.}

The {\cf open-file-input-port} procedure returns an
input port for the named file. The \var{file-options} and
\var{maybe-transcoder} arguments are optional.

The \var{file-options} argument, which may determine
various aspects of the returned port (see section~\ref{fileoptionssection}),
defaults to the value of {\cf (file-options)}.

The \var{buffer-mode} argument, if supplied,
must be one of the symbols that name a buffer mode.
The \var{buffer-mode} argument defaults to {\cf block}.

If \var{maybe-transcoder} is a transcoder, it becomes the transcoder associated
with the returned port.

If \var{maybe-transcoder} is \schfalse{} or absent,
the port will be a binary port and will support the
{\cf port-position} and {\cf set-port-position!}  operations.
Otherwise the port will be a textual port, and whether it supports
the {\cf port-position} and {\cf set-port-position!} operations
is implementation-dependent (and possibly transcoder-dependent).
\end{entry}

\begin{entry}{%
\proto{open-bytevector-input-port}{ bytevector}{procedure}
\pproto{(open-bytevector-input-port \var{bytevector}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{maybe-transcoder})}}

\domain{\var{Maybe-transcoder} must be either a transcoder or \schfalse.}
   
The {\cf open-bytevector-input-port} procedure returns an input port whose bytes are drawn from 
\var{bytevector}.
If \var{transcoder} is specified, it becomes the transcoder associated
with the returned port.

If \var{maybe-transcoder} is \schfalse{} or absent,
the port will be a binary port and will support the
{\cf port-position} and {\cf set-port-position!}  operations.
Otherwise the port will be a textual port, and whether it supports
the {\cf port-position} and {\cf set-port-position!} operations
will be implementation-dependent (and possibly transcoder-dependent).

If \var{bytevector} is modified after {\cf open-\hp{}bytevector-\hp{}input-\hp{}port}
has been called, the effect on the returned
port is unspecified.
\end{entry}

\begin{entry}{%
\proto{open-string-input-port}{ string}{procedure}}

Returns a textual input port whose characters are drawn from
\var{string}.  The port may or may not have an associated transcoder;
if it does, the transcoder is implementation-dependent.
The port should support the
{\cf port-position} and {\cf set-port-position!} operations.

If \var{string} is modified after {\cf open-string-input-port}
has been called, the effect on the returned port is unspecified.
\end{entry}

\begin{entry}{%
\proto{standard-input-port}{}{procedure}}
   
Returns a fresh binary input port connected to standard input.
Whether the port supports the {\cf port-position} and {\cf
  set-port-position!} operations is implementation-dependent.
\end{entry}

\begin{entry}{%
\proto{current-input-port}{}{procedure}}
 
This returns a default textual port for input.  Normally, this default port
is associated with standard input, but can be dynamically re-assigned
using the {\cf with-input-from-file} procedure from the
\rsixlibrary{io simple} library (see section~\ref{with-input-from-file}).
The port may or may not have an associated transcoder; if it does, the
transcoder is implementation-dependent.
\end{entry}

\begin{entry}{%
\pproto{(make-custom-binary-input-port \var{id} \var{read!}}{procedure}}
\mainschindex{make-custom-binary-input-port}{\tt\obeyspaces\\
  \var{get-position} \var{set-position!} \var{close})}

Returns a newly created binary input port whose byte source is
an arbitrary algorithm represented by the \var{read!} procedure.
\var{Id} must be a string naming the new port,
provided for informational purposes only.
\var{Read!} must be a procedure and should behave as specified
below; it will be called by operations that perform binary input.

Each of the remaining arguments may be \schfalse{}; if any of
those arguments is not \schfalse{}, it must be a procedure and
should behave as specified below.
   
\begin{itemize}
\item {\cf (\var{read!} \var{bytevector} \var{start} \var{count})}
       
  \domain{\var{Start} will be a non-negative exact integer object,
  \var{count} will be a positive exact integer object,}
  and \var{bytevector} will be a bytevector whose length is at least
  $\var{start} + \var{count}$.
  The \var{read!} procedure should obtain up to \var{count} bytes
  from the byte source, and should write those bytes
  into \var{bytevector} starting at index \var{start}.
  The \var{read!} procedure should return an exact integer object.  This
  integer object should represent the number of bytes that it has read.
  To indicate an end of file, the \var{read!}
  procedure should write no bytes and return 0.

\item {\cf (\var{get-position})}
       
  The \var{get-position} procedure (if supplied) should return an exact
  integer object representing the current position of
  the input port.  If not supplied, the custom port will not support
  the {\cf port-position} operation.
  
\item {\cf (\var{set-position!} \var{pos})}
       
  \domain{\var{Pos} will be a non-negative exact integer object.}
  The \var{set-position!} procedure (if supplied) should set the
  position of the input port to \var{pos}.  If not supplied, the custom
  port will not support the {\cf set-port-position!} operation.
       
\item {\cf (\var{close})}
       
  The \var{close} procedure (if supplied) should perform any actions
  that are necessary when the input port is closed.
\end{itemize}

\implresp The implementation must check the return
values of \var{read!} and \var{get-position} only when it actually calls
them as part of an I/O operation requested by the program.  The
implementation is not required to check that these procedures
otherwise behave as described.  If they do not, however, the behavior
of the resulting port is unspecified.
\end{entry}

\begin{entry}{%
\pproto{(make-custom-textual-input-port \var{id} \var{read!}}{procedure}}
\mainschindex{make-custom-textual-input-port}{\tt\obeyspaces\\
  \var{get-position} \var{set-position!} \var{close})}

Returns a newly created textual input port whose character source is
an arbitrary algorithm represented by the \var{read!} procedure.
\var{Id} must be a string naming the new port,
provided for informational purposes only.
\var{Read!} must be a procedure and should behave as specified
below; it will be called by operations that perform textual input.

Each of the remaining arguments may be \schfalse{}; if any of
those arguments is not \schfalse{}, it must be a procedure and
should behave as specified below.
   
\begin{itemize}
\item {\cf (\var{read!} \var{string} \var{start} \var{count})}
       
  \domain{\var{Start} will be a non-negative exact integer object,
  \var{count} will be a positive exact integer object,}
  and \var{string} will be a string whose length is at least
  $\var{start} + \var{count}$.
  The \var{read!} procedure should obtain up to \var{count} characters
  from the character source, and should write those characters
  into \var{string} starting at index \var{start}.
  The \var{read!} procedure should return an exact integer object
  representing the number of characters that it has written.
  To indicate an end of file, the \var{read!}
  procedure should write no bytes and return 0.

\item {\cf (\var{get-position})}
       
  The \var{get-position} procedure (if supplied) should return a single
  value.  The return value should represent the current position of
  the input port.  If not supplied, the custom port will not support
  the {\cf port-position} operation.
  
\item {\cf (\var{set-position!} \var{pos})}
       
  The \var{set-position!} procedure (if supplied) should set the
  position of the input port to \var{pos} if \var{pos} is the return
  value of a call to \var{get-position}.  If not supplied, the custom
  port will not support the {\cf set-port-position!} operation.
       
\item {\cf (\var{close})}
       
  The \var{close} procedure (if supplied) should perform any actions
  that are necessary when the input port is closed.
\end{itemize}

The port may or may not have an an associated transcoder; if it does,
the transcoder is implementation-dependent.

\implresp The implementation must check the return
values of \var{read!} and \var{get-position} only when it actually calls
them as part of an I/O operation requested by the program.  The
implementation is not required to check that these procedures
otherwise behave as described.  If they do not, however, the behavior
of the resulting port is unspecified.
\end{entry}

\subsection{Binary input}\unsection

\begin{entry}{%
\proto{get-u8}{ binary-input-port}{procedure}}
   
Reads from \var{binary-input-port}, blocking as necessary, until a
byte is available from \var{binary-input-port} or until an end of file is reached.
If a byte becomes available, {\cf get-u8} returns the byte as an octet and
updates \var{binary-input-port} to point just past that byte. If no input
byte is seen before an end of file is reached, the end-of-file
object is returned.
\end{entry}

\begin{entry}{%
\proto{lookahead-u8}{ binary-input-port}{procedure}}
   
The {\cf lookahead-u8} procedure is like {\cf get-u8}, but it does not 
update \var{binary-input-port} to point past the byte.
\end{entry}

\begin{entry}{%
\proto{get-bytevector-n}{ binary-input-port count}{procedure}}
   
\domain{\var{Count} must be an exact, non-negative integer object representing
  the number of bytes to be read.}
The {\cf get-bytevector-n} procedure reads
from \var{binary-input-port}, blocking as necessary, until \var{count}
bytes are available from \var{binary-input-port} or until an end of file is
reached. If \var{count} bytes are available before an end
of file, {\cf get-bytevector-n} returns a bytevector of size \var{count}.
If fewer bytes are available before an end of file, {\cf get-bytevector-n}
returns a bytevector
containing those bytes. In either case, the input port is updated to
point just past the bytes read.  If an end of file is reached before
any bytes are available, {\cf get-bytevector-n} returns the end-of-file object.
\end{entry}

\begin{entry}{%
\pproto{(get-bytevector-n! \var{binary-input-port}}{procedure}}
\mainschindex{get-bytevector-n!}{\tt\obeyspaces\\
    \var{bytevector} \var{start} \var{count})}

\domain{\var{Count} must be an exact, non-negative integer object, representing
  the number of bytes to be read. \var{bytevector} must be a bytevector
  with at
  least $\var{start} + \var{count}$ elements.}
   
The {\cf get-bytevector-n!} procedure reads from \var{binary-input-port},
blocking as necessary, until
\var{count} bytes are available from \var{binary-input-port} or until
an end of file is
reached. If \var{count} bytes are available before an end of file,
they are written into \var{bytevector} starting at index \var{start}, and
the result is \var{count}. If fewer bytes are available before
the next end of file, the available bytes are written into \var{bytevector}
starting at index \var{start}, and the result is a number object
representing the number of bytes actually
read. In either case, the input port is updated to point just past the
bytes read. If an end of file is reached before any bytes
are available, {\cf get-bytevector-n!} returns the end-of-file object.
\end{entry}

\begin{entry}{%
\proto{get-bytevector-some}{ binary-input-port}{procedure}}
   
Reads from \var{binary-input-port}, blocking as necessary, until bytes are
available from \var{binary-input-port} or until an end of file is reached.
If bytes become available,
{\cf get-bytevector-some} returns a freshly allocated
bytevector containing the initial available bytes (at least one),
and it updates \var{binary-input-port} to point just past these bytes.
If no input bytes are seen before an end
of file is reached, the end-of-file object is returned.
\end{entry}

\begin{entry}{%
\proto{get-bytevector-all}{ binary-input-port}{procedure}}
   
Attempts to read all bytes until the next end of file, blocking as
necessary. If one or more bytes are read, {\cf get-bytevector-all} returns
a bytevector
containing all bytes up to the next end of file.  Otherwise, {\cf
  get-bytevector-all} returns the end-of-file object.
The operation may block indefinitely waiting to see if more bytes
will become available, even if some bytes are already available.
\end{entry}

\subsection{Textual input}\unsection

\begin{entry}{%
\proto{get-char}{ textual-input-port}{procedure}}
   
Reads from \var{textual-input-port}, blocking as necessary, until a
complete character is available from \var{textual-input-port},
or until an end of file is reached.

If a complete character is available before the next end of file, {\cf
  get-char} returns that character and updates the input port to
point past the character. If an end of file is
reached before any character is read, {\cf get-char} returns the
end-of-file object.
\end{entry}

\begin{entry}{%
\proto{lookahead-char}{ textual-input-port}{procedure}}
  
The {\cf lookahead-char} procedure is like {\cf get-char}, but it does not 
update \var{textual-input-port} to point past the 
character.

\begin{note}
  With some of the standard transcoders
  described in this document, up to four bytes of lookahead are
  needed. Nonstandard transcoders may need even more lookahead.
\end{note}
\end{entry}

\begin{entry}{%
\proto{get-string-n}{ textual-input-port count}{procedure}}
   
\domain{\var{Count} must be an exact, non-negative integer object, representing
  the number of characters to be read.}

The {\cf get-string-n} procedure reads
from \var{textual-input-port}, blocking as necessary, until 
\var{count} characters are available, or until an end of
file is reached.
   
If \var{count} characters are available before end of file, {\cf
  get-string-n} returns a string consisting of those \var{count}
characters. If fewer characters are available before an end of file,
but one or more characters can be read,
{\cf get-string-n} returns a string containing
those characters. In either case, the input port is updated to point
just past the characters read. If no characters can be read before an 
end of file, the end-of-file object is returned.
\end{entry}

\begin{entry}{%
\proto{get-string-n!}{ textual-input-port string start count}{procedure}}

\domain{\var{Start} and \var{count} must be exact, non-negative
  integer objects, with \var{count} representing the number of characters to be read.
  \var{String} must be a string with at least $\var{start} +
  \var{count}$ characters.}

The {\cf get-string-n!} procedure reads from \var{textual-input-port} in the same manner as {\cf
  get-string-n}.  If \var{count} characters are available
before an end of file, they are written into \var{string}
starting at index \var{start}, and \var{count} is returned. If fewer
characters are available before an end of file, but one
or more can be read, those characters are written into \var{string}
starting at index \var{start} and the number of characters actually read is
returned as an exact integer object. If no characters can be read before an end of file,
the end-of-file object is returned.
\end{entry}   

\begin{entry}{%
\proto{get-string-all}{ textual-input-port}{procedure}}
   
Reads from \var{textual-input-port} until an end of file, decoding
characters in the same manner as {\cf get-string-n} and {\cf get-string-n!}.
   
If characters are available before the end of file, a string
containing all the characters decoded from that data are returned. If no character
precedes the end of file, the end-of-file object is
returned.
\end{entry}

\begin{entry}{%
\proto{get-line}{ textual-input-port}{procedure}}
   
Reads from \var{textual-input-port} up to and including the linefeed
character or end of file, decoding characters in the same manner as
{\cf get-string-n} and {\cf get-string-n!}.
   
If a linefeed character is read, a string
containing all of the text up to (but not including) the linefeed
character is returned, and the port is updated to point just past the
linefeed character. If an end of file is
encountered before any linefeed character is read, but some characters
have been read and decoded as characters, a string containing
those characters is returned. If an end of file is encountered before
any characters are read, the end-of-file object is
returned.

\begin{note}
  The end-of-line style, if not {\cf none}, will cause all line
  endings to be read as linefeed characters.  See
  section~\ref{transcoderssection}.
\end{note}
\end{entry}

\begin{entry}{%
\proto{get-datum}{ textual-input-port}{procedure}}
 
Reads an external representation from \var{textual-input-port} and returns the
datum it represents.  The {\cf get-datum} procedure returns the next
datum that can be parsed from the given \var{textual-input-port}, updating
\var{textual-input-port} to point exactly past the end of the external
representation of the object.

Any \meta{interlexeme space}
(see report section~\extref{report:lexicalsyntaxsection}{Lexical syntax}) in
the input is first skipped.  If an end of file occurs after the
\meta{interlexeme space}, the end-of-file object (see
section~\ref{eofsection}) is returned.

If a character inconsistent with an external representation is
encountered in the input, an exception with condition types
{\cf\&lexical} and {\cf\&i/o-read} is raised.
Also, if the end of file is encountered
after the beginning of an external representation, but the external
representation is incomplete and therefore cannot be parsed, an exception
with condition types {\cf\&lexical} and {\cf\&i/o-read} is raised.
\end{entry}

\subsection{Output ports}

An output port is a sink to which bytes or characters are written.
The written data may control
external devices or may produce files and other objects that may
subsequently be opened for input.

\begin{entry}{%
\proto{output-port?}{ obj}{procedure}}
   
Returns \schtrue{} if the argument is an output port (or a
combined input and output port), \schfalse{} otherwise.
\end{entry}   

\begin{entry}{%
\proto{flush-output-port}{ output-port}{procedure}}
   
Flushes any buffered output from the buffer of \var{output-port} to the
underlying file, device, or object. The {\cf flush-output-port}
procedure returns \unspecifiedreturn.
\end{entry}

\begin{entry}{%
\proto{output-port-buffer-mode}{ output-port}{procedure}}
   
Returns the symbol that represents the buffer mode of
\var{output-port}.
\end{entry}

\begin{entry}{%
\proto{open-file-output-port}{ filename}{procedure}
\rproto{open-file-output-port}{ filename file-options}{procedure}
\pproto{(open-file-output-port \var{filename}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{file-options} \var{buffer-mode})}\\
\pproto{(open-file-output-port \var{filename}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{file-options} \var{buffer-mode} \var{maybe-transcoder})}}

\domain{\var{Maybe-transcoder} must be either a transcoder or \schfalse.}

The {\cf open-file-output-port} procedure returns an output port for the named file.

The \var{file-options} argument, which may determine
various aspects of the returned port (see section~\ref{fileoptionssection}),
defaults to the value of {\cf (file-options)}.

The \var{buffer-mode} argument, if supplied,
must be one of the symbols that name a buffer mode.
The \var{buffer-mode} argument defaults to {\cf block}.

If \var{maybe-transcoder} is a transcoder, it becomes the transcoder
associated with the port.

If \var{maybe-transcoder} is \schfalse{} or absent,
the port will be a binary port and will support the
{\cf port-position} and {\cf set-port-position!}  operations.
Otherwise the port will be a textual port, and whether it supports
the {\cf port-position} and {\cf set-port-position!} operations
is implementation-dependent (and possibly transcoder-dependent).
\end{entry}   

\begin{entry}{%
\proto{open-bytevector-output-port}{}{procedure}
\rproto{open-bytevector-output-port}{ maybe-transcoder}{procedure}}

\domain{\var{Maybe-transcoder} must be either a transcoder or \schfalse.}

The {\cf open-bytevector-output-port} procedure returns 
two values: an output port and an extraction procedure.
The output port accumulates the bytes written to it for
later extraction by the procedure.

If \var{maybe-transcoder} is a transcoder, it becomes
the transcoder associated with the port.
If \var{maybe-transcoder} is \schfalse{} or absent,
the port will be a binary port and will support the
{\cf port-position} and {\cf set-port-position!}  operations.
Otherwise the port will be a textual port, and whether it supports
the {\cf port-position} and {\cf set-port-position!} operations
is implementation-dependent (and possibly transcoder-dependent).

The extraction procedure takes no arguments.
When called, it returns a
bytevector consisting of all the port's accumulated bytes (regardless
of the port's current position), removes
the accumulated bytes from the port, and resets the port's position.
\end{entry}

\begin{entry}{%
\proto{call-with-bytevector-output-port}{ proc}{procedure}
\pproto{(call-with-bytevector-output-port \var{proc}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{maybe-transcoder})}}

\domain{\var{Proc} must accept one argument.
  \var{Maybe-transcoder} must be either a transcoder or \schfalse.}

The {\cf call-with-bytevector-output-port} procedure creates an output
port that accumulates the bytes written to it and calls \var{proc} with
that output port as an argument. Whenever \var{proc} returns, a
bytevector consisting of all of the port's accumulated bytes (regardless
of the port's current position) is returned and
the port is closed.

The transcoder associated with the output port is determined
as for a call to {\cf open-bytevector-output-port}.
\end{entry}

\begin{entry}{%
\proto{open-string-output-port}{}{procedure}}

Returns two values: a textual output port and an extraction procedure.
The output port accumulates the characters written to it for
later extraction by the procedure.

The port may or may not have an associated transcoder; if it does, the
transcoder is implementation-dependent.  The port should support the {\cf
  port-position} and {\cf set-port-position!} operations.

The extraction procedure takes no arguments.
When called, it returns a string consisting of all of the port's
accumulated characters (regardless of the current position),
removes the accumulated characters from the port, and resets
the port's position.
\end{entry}

\begin{entry}{%
\proto{call-with-string-output-port}{ proc}{procedure}}

\domain{\var{Proc} must accept one argument.}
The {\cf call-\hp{}with-\hp{}string-\hp{}output-\hp{}port} procedure creates a textual output port that accumulates the
characters written to it and calls \var{proc} with that output port
as an argument.  Whenever \var{proc} returns, a string consisting of all
of the port's accumulated characters (regardless of the port's current
position) is returned and the port is closed.

The port may or may not have an associated transcoder; if it does, the
transcoder is implementation-dependent.  The port should support the
{\cf port-position} and {\cf set-port-position!} operations.
\end{entry}

\begin{entry}{%
\proto{standard-output-port}{}{procedure}
\proto{standard-error-port}{}{procedure}}
   
Returns a fresh binary output port connected to the standard output or
standard error respectively.  Whether the port supports the {\cf
  port-position} and {\cf set-port-position!} operations is
implementation-dependent.
\end{entry}

\begin{entry}{%
\proto{current-output-port}{}{procedure}
\proto{current-error-port}{}{procedure}}
 
These return default textual ports for regular output and error
output.  Normally, these default ports are associated with standard
output, and standard error, respectively.  The return value of {\cf
  current-output-port} can be dynamically re-assigned using the {\cf
  with-output-to-file} procedure from the \rsixlibrary{io simple}
library (see section~\ref{with-output-to-file}).  A port returned by
one of these procedures may or may not have an associated transcoder;
if it does, the transcoder is implementation-dependent.
\end{entry}


\begin{entry}{%
\pproto{(make-custom-binary-output-port \var{id}}{procedure}}
\mainschindex{make-custom-binary-output-port}{\tt\obeyspaces\\
  \var{write!} \var{get-position} \var{set-position!} \var{close})}

Returns a newly created binary output port whose byte sink is
an arbitrary algorithm represented by the \var{write!} procedure.
\var{Id} must be a string naming the new port,
provided for informational purposes only.
\var{Write!} must be a procedure and should behave as specified
below; it will be called by operations that perform binary output.

Each of the remaining arguments may be \schfalse{}; if any of
those arguments is not \schfalse{}, it must be a procedure and
should behave as specified in the description of
{\cf make-custom-binary-input-port}.
   
\begin{itemize}
\item {\cf (\var{write!} \var{bytevector} \var{start} \var{count})}
       
  \domain{\var{Start} and \var{count} will be non-negative exact
    integer objects,
  and \var{bytevector} will be a bytevector whose length is at least
  $\var{start} + \var{count}$.}
  The \var{write!} procedure should write up to \var{count} bytes
  from \var{bytevector} starting at index \var{start} 
  to the byte sink.
  If \var{count} is 0, the \var{write!} procedure should
  have the effect of passing an end-of-file object to the byte sink.
  In any case, the \var{write!} procedure should return the number of
  bytes that it wrote, as an exact integer object.
\end{itemize}

\implresp The implementation must check the return
values of \var{write!} only when it actually calls \var{write!} as part of
an I/O operation requested by the program.  The implementation is not
required to check that \var{write!} otherwise behaves as described.
If it does not, however, the behavior of the resulting port is
unspecified.
\end{entry}

\begin{entry}{%
\pproto{(make-custom-textual-output-port \var{id}}{procedure}}
\mainschindex{make-custom-textual-output-port}{\tt\obeyspaces\\
  \var{write!} \var{get-position} \var{set-position!} \var{close})}

Returns a newly created textual output port whose byte sink is
an arbitrary algorithm represented by the \var{write!} procedure.
\var{Id} must be a string naming the new port,
provided for informational purposes only.
\var{Write!} must be a procedure and should behave as specified
below; it will be called by operations that perform textual output.

Each of the remaining arguments may be \schfalse{}; if any of
those arguments is not \schfalse{}, it must be a procedure and
should behave as specified in the description of
{\cf make-custom-textual-input-port}.
   
\begin{itemize}
\item {\cf (\var{write!} \var{string} \var{start} \var{count})}
       
  \domain{\var{Start} and \var{count} will be non-negative exact
    integer objects,
  and \var{string} will be a string whose length is at least
  $\var{start} + \var{count}$.}
  The \var{write!} procedure should write up to \var{count} characters
  from \var{string} starting at index \var{start} 
  to the character sink.
  If \var{count} is 0, the \var{write!} procedure should
  have the effect of passing an end-of-file object to the character sink.
  In any case, the \var{write!} procedure should return the number of
  characters that it wrote, as an exact integer object.
\end{itemize}

The port may or may not have an associated transcoder; if it does, the
transcoder is implementation-dependent.

\implresp The implementation must check the return
values of \var{write!} only when it actually calls \var{write!} as part of
an I/O operation requested by the program.  The implementation is not
required to check that \var{write!} otherwise behaves as described.
If it does not, however, the behavior of the resulting port is
unspecified.
\end{entry}

\subsection{Binary output}\unsection

\begin{entry}{%
\proto{put-u8}{ binary-output-port octet}{procedure}}

Writes \var{octet} to the output port and returns \unspecifiedreturn.
\end{entry}

\begin{entry}{%
\proto{put-bytevector}{ binary-output-port bytevector}{procedure}
\rproto{put-bytevector}{ binary-output-port bytevector start}{procedure}
\pproto{(put-bytevector \var{binary-output-port}}{procedure}}
{\tt\obeyspaces\\
     \var{bytevector} \var{start} \var{count})}
   
\domain{\var{Start} and \var{count} must be non-negative exact
  integer objects that default to 0 and $\texttt{(bytevector-length \var{bytevector})}
  - \var{start}$, respectively. \var{Bytevector} must have a length of at
  least $\var{start} + \var{count}$.}  The {\cf put-bytevector} procedure writes
the \var{count} bytes of the bytevector \var{bytevector} starting at index
\var{start} to the output port.  The {\cf put-bytevector} procedure
returns \unspecifiedreturn.
\end{entry}

\subsection{Textual output}\unsection

\begin{entry}{%
\proto{put-char}{ textual-output-port char}{procedure}}
   
Writes \var{char} to the port. The {\cf put-char} procedure returns
\unspecifiedreturn.
\end{entry}

\begin{entry}{%
\proto{put-string}{ textual-output-port string}{procedure}
\rproto{put-string}{ textual-output-port string start}{procedure}
\rproto{put-string}{ textual-output-port string start count}{procedure}}
   
\domain{\var{Start} and \var{count} must be non-negative exact
  integer objects.  \var{String} must have a length of at least $\var{start} +
  \var{count}$.}  \var{Start} defaults to 0.  \var{Count} defaults to
$\texttt{(string-length \var{string})} - \var{start}$.  The {\cf
  put-string} procedure writes the
\var{count} characters of \var{string} starting at
index \var{start} to the port.  The {\cf put-string} procedure
returns \unspecifiedreturn.
\end{entry}


\begin{entry}{%
\proto{put-datum}{ textual-output-port datum}{procedure}}

\domain{\var{Datum} should be a datum value.}  The {\cf put-datum}
procedure writes an external representation of \var{datum} to
\var{textual-output-port}.  The specific external representation is
implementation-dependent.  However, whenever possible, an
implementation should produce a representation for which {\cf
  get-datum}, when reading the representation, will return an object
equal (in the sense of {\cf equal?}) to \var{datum}.

\begin{note}
  Not all datums may allow producing an external representation for which
  {\cf get-datum} will produce an object that is equal to the
  original.  Specifically, NaNs contained in \var{datum} may make
  this impossible.
\end{note}

\begin{note}
  The {\cf put-datum} procedure merely writes the external
  representation, but no trailing delimiter.  If {\cf put-datum} is
  used to write several subsequent external representations to an
  output port, care should be taken to delimit them properly so they can
  be read back in by subsequent calls to {\cf get-datum}.
\end{note}
\end{entry}


\subsection{Input/output ports}\unsection

\begin{entry}{%
\proto{open-file-input/output-port}{ filename}{procedure}
\rproto{open-file-input/output-port}{ filename file-options}{procedure}
\pproto{(open-file-input/output-port \var{filename}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{file-options} \var{buffer-mode})}\\
\pproto{(open-file-input/output-port \var{filename}}{procedure}{\tt\obeyspaces%
\hspace*{2em}\var{file-options} \var{buffer-mode} \var{transcoder})}}
   
Returns a single port that is both an input port and an
output port for the named file.
The optional arguments default as described in the specification
of {\cf open-file-output-port}.
If the input/output port supports {\cf port-position} and/or
{\cf set-port-position!}, the same port position is used
for both input and output.
\end{entry}

\begin{entry}{%
\pproto{(make-custom-binary-input/output-port}{procedure}}
\mainschindex{make-custom-binary-input/output-port}{\tt\obeyspaces\\
  \var{id} \var{read!} \var{write!} \var{get-position} \var{set-position!} \var{close})}

Returns a newly created binary input/output port whose
byte source and sink are
arbitrary algorithms represented by the \var{read!} and \var{write!}
procedures.
\var{Id} must be a string naming the new port,
provided for informational purposes only.
\var{Read!} and \var{write!} must be procedures,
and should behave as specified for the
{\cf make-custom-binary-input-port} and
{\cf make-custom-binary-output-port} procedures.

Each of the remaining arguments may be \schfalse{}; if any of
those arguments is not \schfalse{}, it must be a procedure and
should behave as specified in the description of
{\cf make-custom-binary-input-port}.
\end{entry}

\begin{entry}{%
\pproto{(make-custom-textual-input/output-port}{procedure}}
\mainschindex{make-custom-textual-input/output-port}{\tt\obeyspaces\\
  \var{id} \var{read!} \var{write!} \var{get-position} \var{set-position!} \var{close})}

Returns a newly created textual input/output port whose
textual source and sink are
arbitrary algorithms represented by the \var{read!} and \var{write!}
procedures.
\var{Id} must be a string naming the new port,
provided for informational purposes only.
\var{Read!} and \var{write!} must be procedures,
and should behave as specified for the
{\cf make-custom-textual-input-port} and
{\cf make-custom-textual-output-port} procedures.

Each of the remaining arguments may be \schfalse{}; if any of
those arguments is not \schfalse{}, it must be a procedure and
should behave as specified in the description of
{\cf make-custom-textual-input-port}.
\end{entry}


%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "r6rs-lib"
%%% End: