More completely document the `(rnrs io ports)' library
[bpt/guile.git] / doc / ref / api-io.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2009,
4 @c 2010, 2011 Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @node Input and Output
8 @section Input and Output
9
10 @menu
11 * Ports:: The idea of the port abstraction.
12 * Reading:: Procedures for reading from a port.
13 * Writing:: Procedures for writing to a port.
14 * Closing:: Procedures to close a port.
15 * Random Access:: Moving around a random access port.
16 * Line/Delimited:: Read and write lines or delimited text.
17 * Block Reading and Writing:: Reading and writing blocks of text.
18 * Default Ports:: Defaults for input, output and errors.
19 * Port Types:: Types of port and how to make them.
20 * R6RS I/O Ports:: The R6RS port API.
21 * I/O Extensions:: Using and extending ports in C.
22 @end menu
23
24
25 @node Ports
26 @subsection Ports
27 @cindex Port
28
29 Sequential input/output in Scheme is represented by operations on a
30 @dfn{port}. This chapter explains the operations that Guile provides
31 for working with ports.
32
33 Ports are created by opening, for instance @code{open-file} for a file
34 (@pxref{File Ports}). Characters can be read from an input port and
35 written to an output port, or both on an input/output port. A port
36 can be closed (@pxref{Closing}) when no longer required, after which
37 any attempt to read or write is an error.
38
39 The formal definition of a port is very generic: an input port is
40 simply ``an object which can deliver characters on demand,'' and an
41 output port is ``an object which can accept characters.'' Because
42 this definition is so loose, it is easy to write functions that
43 simulate ports in software. @dfn{Soft ports} and @dfn{string ports}
44 are two interesting and powerful examples of this technique.
45 (@pxref{Soft Ports}, and @ref{String Ports}.)
46
47 Ports are garbage collected in the usual way (@pxref{Memory
48 Management}), and will be closed at that time if not already closed.
49 In this case any errors occurring in the close will not be reported.
50 Usually a program will want to explicitly close so as to be sure all
51 its operations have been successful. Of course if a program has
52 abandoned something due to an error or other condition then closing
53 problems are probably not of interest.
54
55 It is strongly recommended that file ports be closed explicitly when
56 no longer required. Most systems have limits on how many files can be
57 open, both on a per-process and a system-wide basis. A program that
58 uses many files should take care not to hit those limits. The same
59 applies to similar system resources such as pipes and sockets.
60
61 Note that automatic garbage collection is triggered only by memory
62 consumption, not by file or other resource usage, so a program cannot
63 rely on that to keep it away from system limits. An explicit call to
64 @code{gc} can of course be relied on to pick up unreferenced ports.
65 If program flow makes it hard to be certain when to close then this
66 may be an acceptable way to control resource usage.
67
68 All file access uses the ``LFS'' large file support functions when
69 available, so files bigger than 2 Gbytes (@math{2^31} bytes) can be
70 read and written on a 32-bit system.
71
72 Each port has an associated character encoding that controls how bytes
73 read from the port are converted to characters and string and controls
74 how characters and strings written to the port are converted to bytes.
75 When ports are created, they inherit their character encoding from the
76 current locale, but, that can be modified after the port is created.
77
78 Currently, the ports only work with @emph{non-modal} encodings. Most
79 encodings are non-modal, meaning that the conversion of bytes to a
80 string doesn't depend on its context: the same byte sequence will always
81 return the same string. A couple of modal encodings are in common use,
82 like ISO-2022-JP and ISO-2022-KR, and they are not yet supported.
83
84 Each port also has an associated conversion strategy: what to do when
85 a Guile character can't be converted to the port's encoded character
86 representation for output. There are three possible strategies: to
87 raise an error, to replace the character with a hex escape, or to
88 replace the character with a substitute character.
89
90 @rnindex input-port?
91 @deffn {Scheme Procedure} input-port? x
92 @deffnx {C Function} scm_input_port_p (x)
93 Return @code{#t} if @var{x} is an input port, otherwise return
94 @code{#f}. Any object satisfying this predicate also satisfies
95 @code{port?}.
96 @end deffn
97
98 @rnindex output-port?
99 @deffn {Scheme Procedure} output-port? x
100 @deffnx {C Function} scm_output_port_p (x)
101 Return @code{#t} if @var{x} is an output port, otherwise return
102 @code{#f}. Any object satisfying this predicate also satisfies
103 @code{port?}.
104 @end deffn
105
106 @deffn {Scheme Procedure} port? x
107 @deffnx {C Function} scm_port_p (x)
108 Return a boolean indicating whether @var{x} is a port.
109 Equivalent to @code{(or (input-port? @var{x}) (output-port?
110 @var{x}))}.
111 @end deffn
112
113 @deffn {Scheme Procedure} set-port-encoding! port enc
114 @deffnx {C Function} scm_set_port_encoding_x (port, enc)
115 Sets the character encoding that will be used to interpret all port I/O.
116 @var{enc} is a string containing the name of an encoding. Valid
117 encoding names are those
118 @url{http://www.iana.org/assignments/character-sets, defined by IANA}.
119 @end deffn
120
121 @defvr {Scheme Variable} %default-port-encoding
122 A fluid containing @code{#f} or the name of the encoding to
123 be used by default for newly created ports (@pxref{Fluids and Dynamic
124 States}). The value @code{#f} is equivalent to @code{"ISO-8859-1"}.
125
126 New ports are created with the encoding appropriate for the current
127 locale if @code{setlocale} has been called or the value specified by
128 this fluid otherwise.
129 @end defvr
130
131 @deffn {Scheme Procedure} port-encoding port
132 @deffnx {C Function} scm_port_encoding
133 Returns, as a string, the character encoding that @var{port} uses to interpret
134 its input and output. The value @code{#f} is equivalent to @code{"ISO-8859-1"}.
135 @end deffn
136
137 @deffn {Scheme Procedure} set-port-conversion-strategy! port sym
138 @deffnx {C Function} scm_set_port_conversion_strategy_x (port, sym)
139 Sets the behavior of the interpreter when outputting a character that
140 is not representable in the port's current encoding. @var{sym} can be
141 either @code{'error}, @code{'substitute}, or @code{'escape}. If it is
142 @code{'error}, an error will be thrown when an nonconvertible character
143 is encountered. If it is @code{'substitute}, then nonconvertible
144 characters will be replaced with approximate characters, or with
145 question marks if no approximately correct character is available. If
146 it is @code{'escape}, it will appear as a hex escape when output.
147
148 If @var{port} is an open port, the conversion error behavior
149 is set for that port. If it is @code{#f}, it is set as the
150 default behavior for any future ports that get created in
151 this thread.
152 @end deffn
153
154 @deffn {Scheme Procedure} port-conversion-strategy port
155 @deffnx {C Function} scm_port_conversion_strategy (port)
156 Returns the behavior of the port when outputting a character that is
157 not representable in the port's current encoding. It returns the
158 symbol @code{error} if unrepresentable characters should cause
159 exceptions, @code{substitute} if the port should try to replace
160 unrepresentable characters with question marks or approximate
161 characters, or @code{escape} if unrepresentable characters should be
162 converted to string escapes.
163
164 If @var{port} is @code{#f}, then the current default behavior will be
165 returned. New ports will have this default behavior when they are
166 created.
167 @end deffn
168
169
170
171 @node Reading
172 @subsection Reading
173 @cindex Reading
174
175 [Generic procedures for reading from ports.]
176
177 These procedures pertain to reading characters and strings from
178 ports. To read general S-expressions from ports, @xref{Scheme Read}.
179
180 @rnindex eof-object?
181 @cindex End of file object
182 @deffn {Scheme Procedure} eof-object? x
183 @deffnx {C Function} scm_eof_object_p (x)
184 Return @code{#t} if @var{x} is an end-of-file object; otherwise
185 return @code{#f}.
186 @end deffn
187
188 @rnindex char-ready?
189 @deffn {Scheme Procedure} char-ready? [port]
190 @deffnx {C Function} scm_char_ready_p (port)
191 Return @code{#t} if a character is ready on input @var{port}
192 and return @code{#f} otherwise. If @code{char-ready?} returns
193 @code{#t} then the next @code{read-char} operation on
194 @var{port} is guaranteed not to hang. If @var{port} is a file
195 port at end of file then @code{char-ready?} returns @code{#t}.
196
197 @code{char-ready?} exists to make it possible for a
198 program to accept characters from interactive ports without
199 getting stuck waiting for input. Any input editors associated
200 with such ports must make sure that characters whose existence
201 has been asserted by @code{char-ready?} cannot be rubbed out.
202 If @code{char-ready?} were to return @code{#f} at end of file,
203 a port at end of file would be indistinguishable from an
204 interactive port that has no ready characters.
205 @end deffn
206
207 @rnindex read-char
208 @deffn {Scheme Procedure} read-char [port]
209 @deffnx {C Function} scm_read_char (port)
210 Return the next character available from @var{port}, updating
211 @var{port} to point to the following character. If no more
212 characters are available, the end-of-file object is returned.
213
214 When @var{port}'s data cannot be decoded according to its
215 character encoding, a @code{decoding-error} is raised and
216 @var{port} points past the erroneous byte sequence.
217 @end deffn
218
219 @deftypefn {C Function} size_t scm_c_read (SCM port, void *buffer, size_t size)
220 Read up to @var{size} bytes from @var{port} and store them in
221 @var{buffer}. The return value is the number of bytes actually read,
222 which can be less than @var{size} if end-of-file has been reached.
223
224 Note that this function does not update @code{port-line} and
225 @code{port-column} below.
226 @end deftypefn
227
228 @rnindex peek-char
229 @deffn {Scheme Procedure} peek-char [port]
230 @deffnx {C Function} scm_peek_char (port)
231 Return the next character available from @var{port},
232 @emph{without} updating @var{port} to point to the following
233 character. If no more characters are available, the
234 end-of-file object is returned.
235
236 The value returned by
237 a call to @code{peek-char} is the same as the value that would
238 have been returned by a call to @code{read-char} on the same
239 port. The only difference is that the very next call to
240 @code{read-char} or @code{peek-char} on that @var{port} will
241 return the value returned by the preceding call to
242 @code{peek-char}. In particular, a call to @code{peek-char} on
243 an interactive port will hang waiting for input whenever a call
244 to @code{read-char} would have hung.
245
246 As for @code{read-char}, a @code{decoding-error} may be raised
247 if such a situation occurs. However, unlike with @code{read-char},
248 @var{port} still points at the beginning of the erroneous byte
249 sequence when the error is raised.
250 @end deffn
251
252 @deffn {Scheme Procedure} unread-char cobj [port]
253 @deffnx {C Function} scm_unread_char (cobj, port)
254 Place @var{char} in @var{port} so that it will be read by the
255 next read operation. If called multiple times, the unread characters
256 will be read again in last-in first-out order. If @var{port} is
257 not supplied, the current input port is used.
258 @end deffn
259
260 @deffn {Scheme Procedure} unread-string str port
261 @deffnx {C Function} scm_unread_string (str, port)
262 Place the string @var{str} in @var{port} so that its characters will
263 be read from left-to-right as the next characters from @var{port}
264 during subsequent read operations. If called multiple times, the
265 unread characters will be read again in last-in first-out order. If
266 @var{port} is not supplied, the @code{current-input-port} is used.
267 @end deffn
268
269 @deffn {Scheme Procedure} drain-input port
270 @deffnx {C Function} scm_drain_input (port)
271 This procedure clears a port's input buffers, similar
272 to the way that force-output clears the output buffer. The
273 contents of the buffers are returned as a single string, e.g.,
274
275 @lisp
276 (define p (open-input-file ...))
277 (drain-input p) => empty string, nothing buffered yet.
278 (unread-char (read-char p) p)
279 (drain-input p) => initial chars from p, up to the buffer size.
280 @end lisp
281
282 Draining the buffers may be useful for cleanly finishing
283 buffered I/O so that the file descriptor can be used directly
284 for further input.
285 @end deffn
286
287 @deffn {Scheme Procedure} port-column port
288 @deffnx {Scheme Procedure} port-line port
289 @deffnx {C Function} scm_port_column (port)
290 @deffnx {C Function} scm_port_line (port)
291 Return the current column number or line number of @var{port}.
292 If the number is
293 unknown, the result is #f. Otherwise, the result is a 0-origin integer
294 - i.e.@: the first character of the first line is line 0, column 0.
295 (However, when you display a file position, for example in an error
296 message, we recommend you add 1 to get 1-origin integers. This is
297 because lines and column numbers traditionally start with 1, and that is
298 what non-programmers will find most natural.)
299 @end deffn
300
301 @deffn {Scheme Procedure} set-port-column! port column
302 @deffnx {Scheme Procedure} set-port-line! port line
303 @deffnx {C Function} scm_set_port_column_x (port, column)
304 @deffnx {C Function} scm_set_port_line_x (port, line)
305 Set the current column or line number of @var{port}.
306 @end deffn
307
308 @node Writing
309 @subsection Writing
310 @cindex Writing
311
312 [Generic procedures for writing to ports.]
313
314 These procedures are for writing characters and strings to
315 ports. For more information on writing arbitrary Scheme objects to
316 ports, @xref{Scheme Write}.
317
318 @deffn {Scheme Procedure} get-print-state port
319 @deffnx {C Function} scm_get_print_state (port)
320 Return the print state of the port @var{port}. If @var{port}
321 has no associated print state, @code{#f} is returned.
322 @end deffn
323
324 @rnindex newline
325 @deffn {Scheme Procedure} newline [port]
326 @deffnx {C Function} scm_newline (port)
327 Send a newline to @var{port}.
328 If @var{port} is omitted, send to the current output port.
329 @end deffn
330
331 @deffn {Scheme Procedure} port-with-print-state port [pstate]
332 @deffnx {C Function} scm_port_with_print_state (port, pstate)
333 Create a new port which behaves like @var{port}, but with an
334 included print state @var{pstate}. @var{pstate} is optional.
335 If @var{pstate} isn't supplied and @var{port} already has
336 a print state, the old print state is reused.
337 @end deffn
338
339 @deffn {Scheme Procedure} print-options-interface [setting]
340 @deffnx {C Function} scm_print_options (setting)
341 Option interface for the print options. Instead of using
342 this procedure directly, use the procedures
343 @code{print-enable}, @code{print-disable}, @code{print-set!}
344 and @code{print-options}.
345 @end deffn
346
347 @deffn {Scheme Procedure} simple-format destination message . args
348 @deffnx {C Function} scm_simple_format (destination, message, args)
349 Write @var{message} to @var{destination}, defaulting to
350 the current output port.
351 @var{message} can contain @code{~A} (was @code{%s}) and
352 @code{~S} (was @code{%S}) escapes. When printed,
353 the escapes are replaced with corresponding members of
354 @var{ARGS}:
355 @code{~A} formats using @code{display} and @code{~S} formats
356 using @code{write}.
357 If @var{destination} is @code{#t}, then use the current output
358 port, if @var{destination} is @code{#f}, then return a string
359 containing the formatted text. Does not add a trailing newline.
360 @end deffn
361
362 @rnindex write-char
363 @deffn {Scheme Procedure} write-char chr [port]
364 @deffnx {C Function} scm_write_char (chr, port)
365 Send character @var{chr} to @var{port}.
366 @end deffn
367
368 @deftypefn {C Function} void scm_c_write (SCM port, const void *buffer, size_t size)
369 Write @var{size} bytes at @var{buffer} to @var{port}.
370
371 Note that this function does not update @code{port-line} and
372 @code{port-column} (@pxref{Reading}).
373 @end deftypefn
374
375 @findex fflush
376 @deffn {Scheme Procedure} force-output [port]
377 @deffnx {C Function} scm_force_output (port)
378 Flush the specified output port, or the current output port if @var{port}
379 is omitted. The current output buffer contents are passed to the
380 underlying port implementation (e.g., in the case of fports, the
381 data will be written to the file and the output buffer will be cleared.)
382 It has no effect on an unbuffered port.
383
384 The return value is unspecified.
385 @end deffn
386
387 @deffn {Scheme Procedure} flush-all-ports
388 @deffnx {C Function} scm_flush_all_ports ()
389 Equivalent to calling @code{force-output} on
390 all open output ports. The return value is unspecified.
391 @end deffn
392
393
394 @node Closing
395 @subsection Closing
396 @cindex Closing ports
397 @cindex Port, close
398
399 @deffn {Scheme Procedure} close-port port
400 @deffnx {C Function} scm_close_port (port)
401 Close the specified port object. Return @code{#t} if it
402 successfully closes a port or @code{#f} if it was already
403 closed. An exception may be raised if an error occurs, for
404 example when flushing buffered output. See also @ref{Ports and
405 File Descriptors, close}, for a procedure which can close file
406 descriptors.
407 @end deffn
408
409 @deffn {Scheme Procedure} close-input-port port
410 @deffnx {Scheme Procedure} close-output-port port
411 @deffnx {C Function} scm_close_input_port (port)
412 @deffnx {C Function} scm_close_output_port (port)
413 @rnindex close-input-port
414 @rnindex close-output-port
415 Close the specified input or output @var{port}. An exception may be
416 raised if an error occurs while closing. If @var{port} is already
417 closed, nothing is done. The return value is unspecified.
418
419 See also @ref{Ports and File Descriptors, close}, for a procedure
420 which can close file descriptors.
421 @end deffn
422
423 @deffn {Scheme Procedure} port-closed? port
424 @deffnx {C Function} scm_port_closed_p (port)
425 Return @code{#t} if @var{port} is closed or @code{#f} if it is
426 open.
427 @end deffn
428
429
430 @node Random Access
431 @subsection Random Access
432 @cindex Random access, ports
433 @cindex Port, random access
434
435 @deffn {Scheme Procedure} seek fd_port offset whence
436 @deffnx {C Function} scm_seek (fd_port, offset, whence)
437 Sets the current position of @var{fd/port} to the integer
438 @var{offset}, which is interpreted according to the value of
439 @var{whence}.
440
441 One of the following variables should be supplied for
442 @var{whence}:
443 @defvar SEEK_SET
444 Seek from the beginning of the file.
445 @end defvar
446 @defvar SEEK_CUR
447 Seek from the current position.
448 @end defvar
449 @defvar SEEK_END
450 Seek from the end of the file.
451 @end defvar
452 If @var{fd/port} is a file descriptor, the underlying system
453 call is @code{lseek}. @var{port} may be a string port.
454
455 The value returned is the new position in the file. This means
456 that the current position of a port can be obtained using:
457 @lisp
458 (seek port 0 SEEK_CUR)
459 @end lisp
460 @end deffn
461
462 @deffn {Scheme Procedure} ftell fd_port
463 @deffnx {C Function} scm_ftell (fd_port)
464 Return an integer representing the current position of
465 @var{fd/port}, measured from the beginning. Equivalent to:
466
467 @lisp
468 (seek port 0 SEEK_CUR)
469 @end lisp
470 @end deffn
471
472 @findex truncate
473 @findex ftruncate
474 @deffn {Scheme Procedure} truncate-file file [length]
475 @deffnx {C Function} scm_truncate_file (file, length)
476 Truncate @var{file} to @var{length} bytes. @var{file} can be a
477 filename string, a port object, or an integer file descriptor. The
478 return value is unspecified.
479
480 For a port or file descriptor @var{length} can be omitted, in which
481 case the file is truncated at the current position (per @code{ftell}
482 above).
483
484 On most systems a file can be extended by giving a length greater than
485 the current size, but this is not mandatory in the POSIX standard.
486 @end deffn
487
488 @node Line/Delimited
489 @subsection Line Oriented and Delimited Text
490 @cindex Line input/output
491 @cindex Port, line input/output
492
493 The delimited-I/O module can be accessed with:
494
495 @lisp
496 (use-modules (ice-9 rdelim))
497 @end lisp
498
499 It can be used to read or write lines of text, or read text delimited by
500 a specified set of characters. It's similar to the @code{(scsh rdelim)}
501 module from guile-scsh, but does not use multiple values or character
502 sets and has an extra procedure @code{write-line}.
503
504 @c begin (scm-doc-string "rdelim.scm" "read-line")
505 @deffn {Scheme Procedure} read-line [port] [handle-delim]
506 Return a line of text from @var{port} if specified, otherwise from the
507 value returned by @code{(current-input-port)}. Under Unix, a line of text
508 is terminated by the first end-of-line character or by end-of-file.
509
510 If @var{handle-delim} is specified, it should be one of the following
511 symbols:
512 @table @code
513 @item trim
514 Discard the terminating delimiter. This is the default, but it will
515 be impossible to tell whether the read terminated with a delimiter or
516 end-of-file.
517 @item concat
518 Append the terminating delimiter (if any) to the returned string.
519 @item peek
520 Push the terminating delimiter (if any) back on to the port.
521 @item split
522 Return a pair containing the string read from the port and the
523 terminating delimiter or end-of-file object.
524 @end table
525
526 Like @code{read-char}, this procedure can throw to @code{decoding-error}
527 (@pxref{Reading, @code{read-char}}).
528 @end deffn
529
530 @c begin (scm-doc-string "rdelim.scm" "read-line!")
531 @deffn {Scheme Procedure} read-line! buf [port]
532 Read a line of text into the supplied string @var{buf} and return the
533 number of characters added to @var{buf}. If @var{buf} is filled, then
534 @code{#f} is returned.
535 Read from @var{port} if
536 specified, otherwise from the value returned by @code{(current-input-port)}.
537 @end deffn
538
539 @c begin (scm-doc-string "rdelim.scm" "read-delimited")
540 @deffn {Scheme Procedure} read-delimited delims [port] [handle-delim]
541 Read text until one of the characters in the string @var{delims} is found
542 or end-of-file is reached. Read from @var{port} if supplied, otherwise
543 from the value returned by @code{(current-input-port)}.
544 @var{handle-delim} takes the same values as described for @code{read-line}.
545 @end deffn
546
547 @c begin (scm-doc-string "rdelim.scm" "read-delimited!")
548 @deffn {Scheme Procedure} read-delimited! delims buf [port] [handle-delim] [start] [end]
549 Read text into the supplied string @var{buf}.
550
551 If a delimiter was found, return the number of characters written,
552 except if @var{handle-delim} is @code{split}, in which case the return
553 value is a pair, as noted above.
554
555 As a special case, if @var{port} was already at end-of-stream, the EOF
556 object is returned. Also, if no characters were written because the
557 buffer was full, @code{#f} is returned.
558
559 It's something of a wacky interface, to be honest.
560 @end deffn
561
562 @deffn {Scheme Procedure} write-line obj [port]
563 @deffnx {C Function} scm_write_line (obj, port)
564 Display @var{obj} and a newline character to @var{port}. If
565 @var{port} is not specified, @code{(current-output-port)} is
566 used. This function is equivalent to:
567 @lisp
568 (display obj [port])
569 (newline [port])
570 @end lisp
571 @end deffn
572
573 Some of the aforementioned I/O functions rely on the following C
574 primitives. These will mainly be of interest to people hacking Guile
575 internals.
576
577 @deffn {Scheme Procedure} %read-delimited! delims str gobble [port [start [end]]]
578 @deffnx {C Function} scm_read_delimited_x (delims, str, gobble, port, start, end)
579 Read characters from @var{port} into @var{str} until one of the
580 characters in the @var{delims} string is encountered. If
581 @var{gobble} is true, discard the delimiter character;
582 otherwise, leave it in the input stream for the next read. If
583 @var{port} is not specified, use the value of
584 @code{(current-input-port)}. If @var{start} or @var{end} are
585 specified, store data only into the substring of @var{str}
586 bounded by @var{start} and @var{end} (which default to the
587 beginning and end of the string, respectively).
588
589 Return a pair consisting of the delimiter that terminated the
590 string and the number of characters read. If reading stopped
591 at the end of file, the delimiter returned is the
592 @var{eof-object}; if the string was filled without encountering
593 a delimiter, this value is @code{#f}.
594 @end deffn
595
596 @deffn {Scheme Procedure} %read-line [port]
597 @deffnx {C Function} scm_read_line (port)
598 Read a newline-terminated line from @var{port}, allocating storage as
599 necessary. The newline terminator (if any) is removed from the string,
600 and a pair consisting of the line and its delimiter is returned. The
601 delimiter may be either a newline or the @var{eof-object}; if
602 @code{%read-line} is called at the end of file, it returns the pair
603 @code{(#<eof> . #<eof>)}.
604 @end deffn
605
606 @node Block Reading and Writing
607 @subsection Block reading and writing
608 @cindex Block read/write
609 @cindex Port, block read/write
610
611 The Block-string-I/O module can be accessed with:
612
613 @lisp
614 (use-modules (ice-9 rw))
615 @end lisp
616
617 It currently contains procedures that help to implement the
618 @code{(scsh rw)} module in guile-scsh.
619
620 @deffn {Scheme Procedure} read-string!/partial str [port_or_fdes [start [end]]]
621 @deffnx {C Function} scm_read_string_x_partial (str, port_or_fdes, start, end)
622 Read characters from a port or file descriptor into a
623 string @var{str}. A port must have an underlying file
624 descriptor --- a so-called fport. This procedure is
625 scsh-compatible and can efficiently read large strings.
626 It will:
627
628 @itemize
629 @item
630 attempt to fill the entire string, unless the @var{start}
631 and/or @var{end} arguments are supplied. i.e., @var{start}
632 defaults to 0 and @var{end} defaults to
633 @code{(string-length str)}
634 @item
635 use the current input port if @var{port_or_fdes} is not
636 supplied.
637 @item
638 return fewer than the requested number of characters in some
639 cases, e.g., on end of file, if interrupted by a signal, or if
640 not all the characters are immediately available.
641 @item
642 wait indefinitely for some input if no characters are
643 currently available,
644 unless the port is in non-blocking mode.
645 @item
646 read characters from the port's input buffers if available,
647 instead from the underlying file descriptor.
648 @item
649 return @code{#f} if end-of-file is encountered before reading
650 any characters, otherwise return the number of characters
651 read.
652 @item
653 return 0 if the port is in non-blocking mode and no characters
654 are immediately available.
655 @item
656 return 0 if the request is for 0 bytes, with no
657 end-of-file check.
658 @end itemize
659 @end deffn
660
661 @deffn {Scheme Procedure} write-string/partial str [port_or_fdes [start [end]]]
662 @deffnx {C Function} scm_write_string_partial (str, port_or_fdes, start, end)
663 Write characters from a string @var{str} to a port or file
664 descriptor. A port must have an underlying file descriptor
665 --- a so-called fport. This procedure is
666 scsh-compatible and can efficiently write large strings.
667 It will:
668
669 @itemize
670 @item
671 attempt to write the entire string, unless the @var{start}
672 and/or @var{end} arguments are supplied. i.e., @var{start}
673 defaults to 0 and @var{end} defaults to
674 @code{(string-length str)}
675 @item
676 use the current output port if @var{port_of_fdes} is not
677 supplied.
678 @item
679 in the case of a buffered port, store the characters in the
680 port's output buffer, if all will fit. If they will not fit
681 then any existing buffered characters will be flushed
682 before attempting
683 to write the new characters directly to the underlying file
684 descriptor. If the port is in non-blocking mode and
685 buffered characters can not be flushed immediately, then an
686 @code{EAGAIN} system-error exception will be raised (Note:
687 scsh does not support the use of non-blocking buffered ports.)
688 @item
689 write fewer than the requested number of
690 characters in some cases, e.g., if interrupted by a signal or
691 if not all of the output can be accepted immediately.
692 @item
693 wait indefinitely for at least one character
694 from @var{str} to be accepted by the port, unless the port is
695 in non-blocking mode.
696 @item
697 return the number of characters accepted by the port.
698 @item
699 return 0 if the port is in non-blocking mode and can not accept
700 at least one character from @var{str} immediately
701 @item
702 return 0 immediately if the request size is 0 bytes.
703 @end itemize
704 @end deffn
705
706 @node Default Ports
707 @subsection Default Ports for Input, Output and Errors
708 @cindex Default ports
709 @cindex Port, default
710
711 @rnindex current-input-port
712 @deffn {Scheme Procedure} current-input-port
713 @deffnx {C Function} scm_current_input_port ()
714 @cindex standard input
715 Return the current input port. This is the default port used
716 by many input procedures.
717
718 Initially this is the @dfn{standard input} in Unix and C terminology.
719 When the standard input is a tty the port is unbuffered, otherwise
720 it's fully buffered.
721
722 Unbuffered input is good if an application runs an interactive
723 subprocess, since any type-ahead input won't go into Guile's buffer
724 and be unavailable to the subprocess.
725
726 Note that Guile buffering is completely separate from the tty ``line
727 discipline''. In the usual cooked mode on a tty Guile only sees a
728 line of input once the user presses @key{Return}.
729 @end deffn
730
731 @rnindex current-output-port
732 @deffn {Scheme Procedure} current-output-port
733 @deffnx {C Function} scm_current_output_port ()
734 @cindex standard output
735 Return the current output port. This is the default port used
736 by many output procedures.
737
738 Initially this is the @dfn{standard output} in Unix and C terminology.
739 When the standard output is a tty this port is unbuffered, otherwise
740 it's fully buffered.
741
742 Unbuffered output to a tty is good for ensuring progress output or a
743 prompt is seen. But an application which always prints whole lines
744 could change to line buffered, or an application with a lot of output
745 could go fully buffered and perhaps make explicit @code{force-output}
746 calls (@pxref{Writing}) at selected points.
747 @end deffn
748
749 @deffn {Scheme Procedure} current-error-port
750 @deffnx {C Function} scm_current_error_port ()
751 @cindex standard error output
752 Return the port to which errors and warnings should be sent.
753
754 Initially this is the @dfn{standard error} in Unix and C terminology.
755 When the standard error is a tty this port is unbuffered, otherwise
756 it's fully buffered.
757 @end deffn
758
759 @deffn {Scheme Procedure} set-current-input-port port
760 @deffnx {Scheme Procedure} set-current-output-port port
761 @deffnx {Scheme Procedure} set-current-error-port port
762 @deffnx {C Function} scm_set_current_input_port (port)
763 @deffnx {C Function} scm_set_current_output_port (port)
764 @deffnx {C Function} scm_set_current_error_port (port)
765 Change the ports returned by @code{current-input-port},
766 @code{current-output-port} and @code{current-error-port}, respectively,
767 so that they use the supplied @var{port} for input or output.
768 @end deffn
769
770 @deftypefn {C Function} void scm_dynwind_current_input_port (SCM port)
771 @deftypefnx {C Function} void scm_dynwind_current_output_port (SCM port)
772 @deftypefnx {C Function} void scm_dynwind_current_error_port (SCM port)
773 These functions must be used inside a pair of calls to
774 @code{scm_dynwind_begin} and @code{scm_dynwind_end} (@pxref{Dynamic
775 Wind}). During the dynwind context, the indicated port is set to
776 @var{port}.
777
778 More precisely, the current port is swapped with a `backup' value
779 whenever the dynwind context is entered or left. The backup value is
780 initialized with the @var{port} argument.
781 @end deftypefn
782
783 @node Port Types
784 @subsection Types of Port
785 @cindex Types of ports
786 @cindex Port, types
787
788 [Types of port; how to make them.]
789
790 @menu
791 * File Ports:: Ports on an operating system file.
792 * String Ports:: Ports on a Scheme string.
793 * Soft Ports:: Ports on arbitrary Scheme procedures.
794 * Void Ports:: Ports on nothing at all.
795 @end menu
796
797
798 @node File Ports
799 @subsubsection File Ports
800 @cindex File port
801 @cindex Port, file
802
803 The following procedures are used to open file ports.
804 See also @ref{Ports and File Descriptors, open}, for an interface
805 to the Unix @code{open} system call.
806
807 Most systems have limits on how many files can be open, so it's
808 strongly recommended that file ports be closed explicitly when no
809 longer required (@pxref{Ports}).
810
811 @deffn {Scheme Procedure} open-file filename mode
812 @deffnx {C Function} scm_open_file (filename, mode)
813 Open the file whose name is @var{filename}, and return a port
814 representing that file. The attributes of the port are
815 determined by the @var{mode} string. The way in which this is
816 interpreted is similar to C stdio. The first character must be
817 one of the following:
818
819 @table @samp
820 @item r
821 Open an existing file for input.
822 @item w
823 Open a file for output, creating it if it doesn't already exist
824 or removing its contents if it does.
825 @item a
826 Open a file for output, creating it if it doesn't already
827 exist. All writes to the port will go to the end of the file.
828 The "append mode" can be turned off while the port is in use
829 @pxref{Ports and File Descriptors, fcntl}
830 @end table
831
832 The following additional characters can be appended:
833
834 @table @samp
835 @item +
836 Open the port for both input and output. E.g., @code{r+}: open
837 an existing file for both input and output.
838 @item 0
839 Create an "unbuffered" port. In this case input and output
840 operations are passed directly to the underlying port
841 implementation without additional buffering. This is likely to
842 slow down I/O operations. The buffering mode can be changed
843 while a port is in use @pxref{Ports and File Descriptors,
844 setvbuf}
845 @item l
846 Add line-buffering to the port. The port output buffer will be
847 automatically flushed whenever a newline character is written.
848 @item b
849 Use binary mode. On DOS systems the default text mode converts CR+LF
850 in the file to newline for the program, whereas binary mode reads and
851 writes all bytes unchanged. On Unix-like systems there is no such
852 distinction, text files already contain just newlines and no
853 conversion is ever made. The @code{b} flag is accepted on all
854 systems, but has no effect on Unix-like systems.
855
856 (For reference, Guile leaves text versus binary up to the C library,
857 @code{b} here just adds @code{O_BINARY} to the underlying @code{open}
858 call, when that flag is available.)
859
860 Also, open the file using the 8-bit character encoding "ISO-8859-1",
861 ignoring any coding declaration or port encoding.
862
863 Note that, when reading or writing binary data with ports, the
864 bytevector ports in the @code{(rnrs io ports)} module are preferred,
865 as they return vectors, and not strings (@pxref{R6RS I/O Ports}).
866 @end table
867
868 If a file cannot be opened with the access
869 requested, @code{open-file} throws an exception.
870
871 When the file is opened, this procedure will scan for a coding
872 declaration (@pxref{Character Encoding of Source Files}). If present
873 will use that encoding for interpreting the file. Otherwise, the
874 port's encoding will be used. To suppress this behavior, open
875 the file in binary mode and then set the port encoding explicitly
876 using @code{set-port-encoding!}.
877
878 In theory we could create read/write ports which were buffered
879 in one direction only. However this isn't included in the
880 current interfaces.
881 @end deffn
882
883 @rnindex open-input-file
884 @deffn {Scheme Procedure} open-input-file filename
885 Open @var{filename} for input. Equivalent to
886 @lisp
887 (open-file @var{filename} "r")
888 @end lisp
889 @end deffn
890
891 @rnindex open-output-file
892 @deffn {Scheme Procedure} open-output-file filename
893 Open @var{filename} for output. Equivalent to
894 @lisp
895 (open-file @var{filename} "w")
896 @end lisp
897 @end deffn
898
899 @deffn {Scheme Procedure} call-with-input-file filename proc
900 @deffnx {Scheme Procedure} call-with-output-file filename proc
901 @rnindex call-with-input-file
902 @rnindex call-with-output-file
903 Open @var{filename} for input or output, and call @code{(@var{proc}
904 port)} with the resulting port. Return the value returned by
905 @var{proc}. @var{filename} is opened as per @code{open-input-file} or
906 @code{open-output-file} respectively, and an error is signaled if it
907 cannot be opened.
908
909 When @var{proc} returns, the port is closed. If @var{proc} does not
910 return (e.g.@: if it throws an error), then the port might not be
911 closed automatically, though it will be garbage collected in the usual
912 way if not otherwise referenced.
913 @end deffn
914
915 @deffn {Scheme Procedure} with-input-from-file filename thunk
916 @deffnx {Scheme Procedure} with-output-to-file filename thunk
917 @deffnx {Scheme Procedure} with-error-to-file filename thunk
918 @rnindex with-input-from-file
919 @rnindex with-output-to-file
920 Open @var{filename} and call @code{(@var{thunk})} with the new port
921 setup as respectively the @code{current-input-port},
922 @code{current-output-port}, or @code{current-error-port}. Return the
923 value returned by @var{thunk}. @var{filename} is opened as per
924 @code{open-input-file} or @code{open-output-file} respectively, and an
925 error is signaled if it cannot be opened.
926
927 When @var{thunk} returns, the port is closed and the previous setting
928 of the respective current port is restored.
929
930 The current port setting is managed with @code{dynamic-wind}, so the
931 previous value is restored no matter how @var{thunk} exits (eg.@: an
932 exception), and if @var{thunk} is re-entered (via a captured
933 continuation) then it's set again to the @var{FILENAME} port.
934
935 The port is closed when @var{thunk} returns normally, but not when
936 exited via an exception or new continuation. This ensures it's still
937 ready for use if @var{thunk} is re-entered by a captured continuation.
938 Of course the port is always garbage collected and closed in the usual
939 way when no longer referenced anywhere.
940 @end deffn
941
942 @deffn {Scheme Procedure} port-mode port
943 @deffnx {C Function} scm_port_mode (port)
944 Return the port modes associated with the open port @var{port}.
945 These will not necessarily be identical to the modes used when
946 the port was opened, since modes such as "append" which are
947 used only during port creation are not retained.
948 @end deffn
949
950 @deffn {Scheme Procedure} port-filename port
951 @deffnx {C Function} scm_port_filename (port)
952 Return the filename associated with @var{port}, or @code{#f} if no
953 filename is associated with the port.
954
955 @var{port} must be open, @code{port-filename} cannot be used once the
956 port is closed.
957 @end deffn
958
959 @deffn {Scheme Procedure} set-port-filename! port filename
960 @deffnx {C Function} scm_set_port_filename_x (port, filename)
961 Change the filename associated with @var{port}, using the current input
962 port if none is specified. Note that this does not change the port's
963 source of data, but only the value that is returned by
964 @code{port-filename} and reported in diagnostic output.
965 @end deffn
966
967 @deffn {Scheme Procedure} file-port? obj
968 @deffnx {C Function} scm_file_port_p (obj)
969 Determine whether @var{obj} is a port that is related to a file.
970 @end deffn
971
972
973 @node String Ports
974 @subsubsection String Ports
975 @cindex String port
976 @cindex Port, string
977
978 The following allow string ports to be opened by analogy to R4RS
979 file port facilities:
980
981 With string ports, the port-encoding is treated differently than other
982 types of ports. When string ports are created, they do not inherit a
983 character encoding from the current locale. They are given a
984 default locale that allows them to handle all valid string characters.
985 Typically one should not modify a string port's character encoding
986 away from its default.
987
988 @deffn {Scheme Procedure} call-with-output-string proc
989 @deffnx {C Function} scm_call_with_output_string (proc)
990 Calls the one-argument procedure @var{proc} with a newly created output
991 port. When the function returns, the string composed of the characters
992 written into the port is returned. @var{proc} should not close the port.
993
994 Note that which characters can be written to a string port depend on the port's
995 encoding. The default encoding of string ports is specified by the
996 @code{%default-port-encoding} fluid (@pxref{Ports,
997 @code{%default-port-encoding}}). For instance, it is an error to write Greek
998 letter alpha to an ISO-8859-1-encoded string port since this character cannot be
999 represented with ISO-8859-1:
1000
1001 @example
1002 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
1003
1004 (with-fluids ((%default-port-encoding "ISO-8859-1"))
1005 (call-with-output-string
1006 (lambda (p)
1007 (display alpha p))))
1008
1009 @result{}
1010 Throw to key `encoding-error'
1011 @end example
1012
1013 Changing the string port's encoding to a Unicode-capable encoding such as UTF-8
1014 solves the problem.
1015 @end deffn
1016
1017 @deffn {Scheme Procedure} call-with-input-string string proc
1018 @deffnx {C Function} scm_call_with_input_string (string, proc)
1019 Calls the one-argument procedure @var{proc} with a newly
1020 created input port from which @var{string}'s contents may be
1021 read. The value yielded by the @var{proc} is returned.
1022 @end deffn
1023
1024 @deffn {Scheme Procedure} with-output-to-string thunk
1025 Calls the zero-argument procedure @var{thunk} with the current output
1026 port set temporarily to a new string port. It returns a string
1027 composed of the characters written to the current output.
1028
1029 See @code{call-with-output-string} above for character encoding considerations.
1030 @end deffn
1031
1032 @deffn {Scheme Procedure} with-input-from-string string thunk
1033 Calls the zero-argument procedure @var{thunk} with the current input
1034 port set temporarily to a string port opened on the specified
1035 @var{string}. The value yielded by @var{thunk} is returned.
1036 @end deffn
1037
1038 @deffn {Scheme Procedure} open-input-string str
1039 @deffnx {C Function} scm_open_input_string (str)
1040 Take a string and return an input port that delivers characters
1041 from the string. The port can be closed by
1042 @code{close-input-port}, though its storage will be reclaimed
1043 by the garbage collector if it becomes inaccessible.
1044 @end deffn
1045
1046 @deffn {Scheme Procedure} open-output-string
1047 @deffnx {C Function} scm_open_output_string ()
1048 Return an output port that will accumulate characters for
1049 retrieval by @code{get-output-string}. The port can be closed
1050 by the procedure @code{close-output-port}, though its storage
1051 will be reclaimed by the garbage collector if it becomes
1052 inaccessible.
1053 @end deffn
1054
1055 @deffn {Scheme Procedure} get-output-string port
1056 @deffnx {C Function} scm_get_output_string (port)
1057 Given an output port created by @code{open-output-string},
1058 return a string consisting of the characters that have been
1059 output to the port so far.
1060
1061 @code{get-output-string} must be used before closing @var{port}, once
1062 closed the string cannot be obtained.
1063 @end deffn
1064
1065 A string port can be used in many procedures which accept a port
1066 but which are not dependent on implementation details of fports.
1067 E.g., seeking and truncating will work on a string port,
1068 but trying to extract the file descriptor number will fail.
1069
1070
1071 @node Soft Ports
1072 @subsubsection Soft Ports
1073 @cindex Soft port
1074 @cindex Port, soft
1075
1076 A @dfn{soft-port} is a port based on a vector of procedures capable of
1077 accepting or delivering characters. It allows emulation of I/O ports.
1078
1079 @deffn {Scheme Procedure} make-soft-port pv modes
1080 @deffnx {C Function} scm_make_soft_port (pv, modes)
1081 Return a port capable of receiving or delivering characters as
1082 specified by the @var{modes} string (@pxref{File Ports,
1083 open-file}). @var{pv} must be a vector of length 5 or 6. Its
1084 components are as follows:
1085
1086 @enumerate 0
1087 @item
1088 procedure accepting one character for output
1089 @item
1090 procedure accepting a string for output
1091 @item
1092 thunk for flushing output
1093 @item
1094 thunk for getting one character
1095 @item
1096 thunk for closing port (not by garbage collection)
1097 @item
1098 (if present and not @code{#f}) thunk for computing the number of
1099 characters that can be read from the port without blocking.
1100 @end enumerate
1101
1102 For an output-only port only elements 0, 1, 2, and 4 need be
1103 procedures. For an input-only port only elements 3 and 4 need
1104 be procedures. Thunks 2 and 4 can instead be @code{#f} if
1105 there is no useful operation for them to perform.
1106
1107 If thunk 3 returns @code{#f} or an @code{eof-object}
1108 (@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on
1109 Scheme}) it indicates that the port has reached end-of-file.
1110 For example:
1111
1112 @lisp
1113 (define stdout (current-output-port))
1114 (define p (make-soft-port
1115 (vector
1116 (lambda (c) (write c stdout))
1117 (lambda (s) (display s stdout))
1118 (lambda () (display "." stdout))
1119 (lambda () (char-upcase (read-char)))
1120 (lambda () (display "@@" stdout)))
1121 "rw"))
1122
1123 (write p p) @result{} #<input-output: soft 8081e20>
1124 @end lisp
1125 @end deffn
1126
1127
1128 @node Void Ports
1129 @subsubsection Void Ports
1130 @cindex Void port
1131 @cindex Port, void
1132
1133 This kind of port causes any data to be discarded when written to, and
1134 always returns the end-of-file object when read from.
1135
1136 @deffn {Scheme Procedure} %make-void-port mode
1137 @deffnx {C Function} scm_sys_make_void_port (mode)
1138 Create and return a new void port. A void port acts like
1139 @file{/dev/null}. The @var{mode} argument
1140 specifies the input/output modes for this port: see the
1141 documentation for @code{open-file} in @ref{File Ports}.
1142 @end deffn
1143
1144
1145 @node R6RS I/O Ports
1146 @subsection R6RS I/O Ports
1147
1148 @cindex R6RS
1149 @cindex R6RS ports
1150
1151 The I/O port API of the @uref{http://www.r6rs.org/, Revised Report^6 on
1152 the Algorithmic Language Scheme (R6RS)} is provided by the @code{(rnrs
1153 io ports)} module. It provides features, such as binary I/O and Unicode
1154 string I/O, that complement or refine Guile's historical port API
1155 presented above (@pxref{Input and Output}). Note that R6RS ports are not
1156 disjoint from Guile's native ports, so Guile-specific procedures will
1157 work on ports created using the R6RS API, and vice versa.
1158
1159 The text in this section is taken from the R6RS standard libraries
1160 document, with only minor adaptions for inclusion in this manual. The
1161 Guile developers offer their thanks to the R6RS editors for having
1162 provided the report's text under permissive conditions making this
1163 possible.
1164
1165 @c FIXME: Update description when implemented.
1166 @emph{Note}: The implementation of this R6RS API is not complete yet.
1167
1168 @menu
1169 * R6RS File Names:: File names.
1170 * R6RS File Options:: Options for opening files.
1171 * R6RS Buffer Modes:: Influencing buffering behavior.
1172 * R6RS Transcoders:: Influencing port encoding.
1173 * R6RS End-of-File:: The end-of-file object.
1174 * R6RS Port Manipulation:: Manipulating R6RS ports.
1175 * R6RS Input Ports:: Input Ports.
1176 * R6RS Binary Input:: Binary input.
1177 * R6RS Textual Input:: Textual input.
1178 * R6RS Output Ports:: Output Ports.
1179 * R6RS Binary Output:: Binary output.
1180 * R6RS Textual Output:: Textual output.
1181 @end menu
1182
1183 A subset of the @code{(rnrs io ports)} module is provided by the
1184 @code{(ice-9 binary-ports)} module. It contains binary input/output
1185 procedures and does not rely on R6RS support.
1186
1187 @node R6RS File Names
1188 @subsubsection File Names
1189
1190 Some of the procedures described in this chapter accept a file name as an
1191 argument. Valid values for such a file name include strings that name a file
1192 using the native notation of filesystem paths on an implementation's
1193 underlying operating system, and may include implementation-dependent
1194 values as well.
1195
1196 A @var{filename} parameter name means that the
1197 corresponding argument must be a file name.
1198
1199 @node R6RS File Options
1200 @subsubsection File Options
1201 @cindex file options
1202
1203 When opening a file, the various procedures in this library accept a
1204 @code{file-options} object that encapsulates flags to specify how the
1205 file is to be opened. A @code{file-options} object is an enum-set
1206 (@pxref{rnrs enums}) over the symbols constituting valid file options.
1207
1208 A @var{file-options} parameter name means that the corresponding
1209 argument must be a file-options object.
1210
1211 @deffn {Scheme Syntax} file-options @var{file-options-symbol} ...
1212
1213 Each @var{file-options-symbol} must be a symbol.
1214
1215 The @code{file-options} syntax returns a file-options object that
1216 encapsulates the specified options.
1217
1218 When supplied to an operation that opens a file for output, the
1219 file-options object returned by @code{(file-options)} specifies that the
1220 file is created if it does not exist and an exception with condition
1221 type @code{&i/o-file-already-exists} is raised if it does exist. The
1222 following standard options can be included to modify the default
1223 behavior.
1224
1225 @table @code
1226 @item no-create
1227 If the file does not already exist, it is not created;
1228 instead, an exception with condition type @code{&i/o-file-does-not-exist}
1229 is raised.
1230 If the file already exists, the exception with condition type
1231 @code{&i/o-file-already-exists} is not raised
1232 and the file is truncated to zero length.
1233 @item no-fail
1234 If the file already exists, the exception with condition type
1235 @code{&i/o-file-already-exists} is not raised,
1236 even if @code{no-create} is not included,
1237 and the file is truncated to zero length.
1238 @item no-truncate
1239 If the file already exists and the exception with condition type
1240 @code{&i/o-file-already-exists} has been inhibited by inclusion of
1241 @code{no-create} or @code{no-fail}, the file is not truncated, but
1242 the port's current position is still set to the beginning of the
1243 file.
1244 @end table
1245
1246 These options have no effect when a file is opened only for input.
1247 Symbols other than those listed above may be used as
1248 @var{file-options-symbol}s; they have implementation-specific meaning,
1249 if any.
1250
1251 @quotation Note
1252 Only the name of @var{file-options-symbol} is significant.
1253 @end quotation
1254 @end deffn
1255
1256 @node R6RS Buffer Modes
1257 @subsubsection Buffer Modes
1258
1259 Each port has an associated buffer mode. For an output port, the
1260 buffer mode defines when an output operation flushes the buffer
1261 associated with the output port. For an input port, the buffer mode
1262 defines how much data will be read to satisfy read operations. The
1263 possible buffer modes are the symbols @code{none} for no buffering,
1264 @code{line} for flushing upon line endings and reading up to line
1265 endings, or other implementation-dependent behavior,
1266 and @code{block} for arbitrary buffering. This section uses
1267 the parameter name @var{buffer-mode} for arguments that must be
1268 buffer-mode symbols.
1269
1270 If two ports are connected to the same mutable source, both ports
1271 are unbuffered, and reading a byte or character from that shared
1272 source via one of the two ports would change the bytes or characters
1273 seen via the other port, a lookahead operation on one port will
1274 render the peeked byte or character inaccessible via the other port,
1275 while a subsequent read operation on the peeked port will see the
1276 peeked byte or character even though the port is otherwise unbuffered.
1277
1278 In other words, the semantics of buffering is defined in terms of side
1279 effects on shared mutable sources, and a lookahead operation has the
1280 same side effect on the shared source as a read operation.
1281
1282 @deffn {Scheme Syntax} buffer-mode @var{buffer-mode-symbol}
1283
1284 @var{buffer-mode-symbol} must be a symbol whose name is one of
1285 @code{none}, @code{line}, and @code{block}. The result is the
1286 corresponding symbol, and specifies the associated buffer mode.
1287
1288 @quotation Note
1289 Only the name of @var{buffer-mode-symbol} is significant.
1290 @end quotation
1291 @end deffn
1292
1293 @deffn {Scheme Procedure} buffer-mode? obj
1294 Returns @code{#t} if the argument is a valid buffer-mode symbol, and
1295 returns @code{#f} otherwise.
1296 @end deffn
1297
1298 @node R6RS Transcoders
1299 @subsubsection Transcoders
1300 @cindex codec
1301 @cindex end-of-line style
1302 @cindex transcoder
1303 @cindex binary port
1304 @cindex textual port
1305
1306 Several different Unicode encoding schemes describe standard ways to
1307 encode characters and strings as byte sequences and to decode those
1308 sequences. Within this document, a @dfn{codec} is an immutable Scheme
1309 object that represents a Unicode or similar encoding scheme.
1310
1311 An @dfn{end-of-line style} is a symbol that, if it is not @code{none},
1312 describes how a textual port transcodes representations of line endings.
1313
1314 A @dfn{transcoder} is an immutable Scheme object that combines a codec
1315 with an end-of-line style and a method for handling decoding errors.
1316 Each transcoder represents some specific bidirectional (but not
1317 necessarily lossless), possibly stateful translation between byte
1318 sequences and Unicode characters and strings. Every transcoder can
1319 operate in the input direction (bytes to characters) or in the output
1320 direction (characters to bytes). A @var{transcoder} parameter name
1321 means that the corresponding argument must be a transcoder.
1322
1323 A @dfn{binary port} is a port that supports binary I/O, does not have an
1324 associated transcoder and does not support textual I/O. A @dfn{textual
1325 port} is a port that supports textual I/O, and does not support binary
1326 I/O. A textual port may or may not have an associated transcoder.
1327
1328 @deffn {Scheme Procedure} latin-1-codec
1329 @deffnx {Scheme Procedure} utf-8-codec
1330 @deffnx {Scheme Procedure} utf-16-codec
1331
1332 These are predefined codecs for the ISO 8859-1, UTF-8, and UTF-16
1333 encoding schemes.
1334
1335 A call to any of these procedures returns a value that is equal in the
1336 sense of @code{eqv?} to the result of any other call to the same
1337 procedure.
1338 @end deffn
1339
1340 @deffn {Scheme Syntax} eol-style @var{eol-style-symbol}
1341
1342 @var{eol-style-symbol} should be a symbol whose name is one of
1343 @code{lf}, @code{cr}, @code{crlf}, @code{nel}, @code{crnel}, @code{ls},
1344 and @code{none}.
1345
1346 The form evaluates to the corresponding symbol. If the name of
1347 @var{eol-style-symbol} is not one of these symbols, the effect and
1348 result are implementation-dependent; in particular, the result may be an
1349 eol-style symbol acceptable as an @var{eol-style} argument to
1350 @code{make-transcoder}. Otherwise, an exception is raised.
1351
1352 All eol-style symbols except @code{none} describe a specific
1353 line-ending encoding:
1354
1355 @table @code
1356 @item lf
1357 linefeed
1358 @item cr
1359 carriage return
1360 @item crlf
1361 carriage return, linefeed
1362 @item nel
1363 next line
1364 @item crnel
1365 carriage return, next line
1366 @item ls
1367 line separator
1368 @end table
1369
1370 For a textual port with a transcoder, and whose transcoder has an
1371 eol-style symbol @code{none}, no conversion occurs. For a textual input
1372 port, any eol-style symbol other than @code{none} means that all of the
1373 above line-ending encodings are recognized and are translated into a
1374 single linefeed. For a textual output port, @code{none} and @code{lf}
1375 are equivalent. Linefeed characters are encoded according to the
1376 specified eol-style symbol, and all other characters that participate in
1377 possible line endings are encoded as is.
1378
1379 @quotation Note
1380 Only the name of @var{eol-style-symbol} is significant.
1381 @end quotation
1382 @end deffn
1383
1384 @deffn {Scheme Procedure} native-eol-style
1385 Returns the default end-of-line style of the underlying platform, e.g.,
1386 @code{lf} on Unix and @code{crlf} on Windows.
1387 @end deffn
1388
1389 @deffn {Condition Type} &i/o-decoding
1390 @deffnx {Scheme Procedure} make-i/o-decoding-error port
1391 @deffnx {Scheme Procedure} i/o-decoding-error? obj
1392
1393 This condition type could be defined by
1394
1395 @lisp
1396 (define-condition-type &i/o-decoding &i/o-port
1397 make-i/o-decoding-error i/o-decoding-error?)
1398 @end lisp
1399
1400 An exception with this type is raised when one of the operations for
1401 textual input from a port encounters a sequence of bytes that cannot be
1402 translated into a character or string by the input direction of the
1403 port's transcoder.
1404
1405 When such an exception is raised, the port's position is past the
1406 invalid encoding.
1407 @end deffn
1408
1409 @deffn {Condition Type} &i/o-encoding
1410 @deffnx {Scheme Procedure} make-i/o-encoding-error port char
1411 @deffnx {Scheme Procedure} i/o-encoding-error? obj
1412 @deffnx {Scheme Procedure} i/o-encoding-error-char condition
1413
1414 This condition type could be defined by
1415
1416 @lisp
1417 (define-condition-type &i/o-encoding &i/o-port
1418 make-i/o-encoding-error i/o-encoding-error?
1419 (char i/o-encoding-error-char))
1420 @end lisp
1421
1422 An exception with this type is raised when one of the operations for
1423 textual output to a port encounters a character that cannot be
1424 translated into bytes by the output direction of the port's transcoder.
1425 @var{Char} is the character that could not be encoded.
1426 @end deffn
1427
1428 @deffn {Scheme Syntax} error-handling-mode @var{error-handling-mode-symbol}
1429
1430 @var{error-handling-mode-symbol} should be a symbol whose name is one of
1431 @code{ignore}, @code{raise}, and @code{replace}. The form evaluates to
1432 the corresponding symbol. If @var{error-handling-mode-symbol} is not
1433 one of these identifiers, effect and result are
1434 implementation-dependent: The result may be an error-handling-mode
1435 symbol acceptable as a @var{handling-mode} argument to
1436 @code{make-transcoder}. If it is not acceptable as a
1437 @var{handling-mode} argument to @code{make-transcoder}, an exception is
1438 raised.
1439
1440 @quotation Note
1441 Only the name of @var{error-handling-style-symbol} is significant.
1442 @end quotation
1443
1444 The error-handling mode of a transcoder specifies the behavior
1445 of textual I/O operations in the presence of encoding or decoding
1446 errors.
1447
1448 If a textual input operation encounters an invalid or incomplete
1449 character encoding, and the error-handling mode is @code{ignore}, an
1450 appropriate number of bytes of the invalid encoding are ignored and
1451 decoding continues with the following bytes.
1452
1453 If the error-handling mode is @code{replace}, the replacement
1454 character U+FFFD is injected into the data stream, an appropriate
1455 number of bytes are ignored, and decoding
1456 continues with the following bytes.
1457
1458 If the error-handling mode is @code{raise}, an exception with condition
1459 type @code{&i/o-decoding} is raised.
1460
1461 If a textual output operation encounters a character it cannot encode,
1462 and the error-handling mode is @code{ignore}, the character is ignored
1463 and encoding continues with the next character. If the error-handling
1464 mode is @code{replace}, a codec-specific replacement character is
1465 emitted by the transcoder, and encoding continues with the next
1466 character. The replacement character is U+FFFD for transcoders whose
1467 codec is one of the Unicode encodings, but is the @code{?} character
1468 for the Latin-1 encoding. If the error-handling mode is @code{raise},
1469 an exception with condition type @code{&i/o-encoding} is raised.
1470 @end deffn
1471
1472 @deffn {Scheme Procedure} make-transcoder codec
1473 @deffnx {Scheme Procedure} make-transcoder codec eol-style
1474 @deffnx {Scheme Procedure} make-transcoder codec eol-style handling-mode
1475
1476 @var{codec} must be a codec; @var{eol-style}, if present, an eol-style
1477 symbol; and @var{handling-mode}, if present, an error-handling-mode
1478 symbol.
1479
1480 @var{eol-style} may be omitted, in which case it defaults to the native
1481 end-of-line style of the underlying platform. @var{Handling-mode} may
1482 be omitted, in which case it defaults to @code{replace}. The result is
1483 a transcoder with the behavior specified by its arguments.
1484 @end deffn
1485
1486 @deffn {Scheme procedure} native-transcoder
1487 Returns an implementation-dependent transcoder that represents a
1488 possibly locale-dependent ``native'' transcoding.
1489 @end deffn
1490
1491 @deffn {Scheme Procedure} transcoder-codec transcoder
1492 @deffnx {Scheme Procedure} transcoder-eol-style transcoder
1493 @deffnx {Scheme Procedure} transcoder-error-handling-mode transcoder
1494
1495 These are accessors for transcoder objects; when applied to a
1496 transcoder returned by @code{make-transcoder}, they return the
1497 @var{codec}, @var{eol-style}, and @var{handling-mode} arguments,
1498 respectively.
1499 @end deffn
1500
1501 @deffn {Scheme Procedure} bytevector->string bytevector transcoder
1502
1503 Returns the string that results from transcoding the
1504 @var{bytevector} according to the input direction of the transcoder.
1505 @end deffn
1506
1507 @deffn {Scheme Procedure} string->bytevector string transcoder
1508
1509 Returns the bytevector that results from transcoding the
1510 @var{string} according to the output direction of the transcoder.
1511 @end deffn
1512
1513 @node R6RS End-of-File
1514 @subsubsection The End-of-File Object
1515
1516 @cindex EOF
1517 @cindex end-of-file
1518
1519 R5RS' @code{eof-object?} procedure is provided by the @code{(rnrs io
1520 ports)} module:
1521
1522 @deffn {Scheme Procedure} eof-object? obj
1523 @deffnx {C Function} scm_eof_object_p (obj)
1524 Return true if @var{obj} is the end-of-file (EOF) object.
1525 @end deffn
1526
1527 In addition, the following procedure is provided:
1528
1529 @deffn {Scheme Procedure} eof-object
1530 @deffnx {C Function} scm_eof_object ()
1531 Return the end-of-file (EOF) object.
1532
1533 @lisp
1534 (eof-object? (eof-object))
1535 @result{} #t
1536 @end lisp
1537 @end deffn
1538
1539
1540 @node R6RS Port Manipulation
1541 @subsubsection Port Manipulation
1542
1543 The procedures listed below operate on any kind of R6RS I/O port.
1544
1545 @deffn {Scheme Procedure} port? obj
1546 Returns @code{#t} if the argument is a port, and returns @code{#f}
1547 otherwise.
1548 @end deffn
1549
1550 @deffn {Scheme Procedure} port-transcoder port
1551 Returns the transcoder associated with @var{port} if @var{port} is
1552 textual and has an associated transcoder, and returns @code{#f} if
1553 @var{port} is binary or does not have an associated transcoder.
1554 @end deffn
1555
1556 @deffn {Scheme Procedure} binary-port? port
1557 Return @code{#t} if @var{port} is a @dfn{binary port}, suitable for
1558 binary data input/output.
1559
1560 Note that internally Guile does not differentiate between binary and
1561 textual ports, unlike the R6RS. Thus, this procedure returns true when
1562 @var{port} does not have an associated encoding---i.e., when
1563 @code{(port-encoding @var{port})} is @code{#f} (@pxref{Ports,
1564 port-encoding}). This is the case for ports returned by R6RS procedures
1565 such as @code{open-bytevector-input-port} and
1566 @code{make-custom-binary-output-port}.
1567
1568 However, Guile currently does not prevent use of textual I/O procedures
1569 such as @code{display} or @code{read-char} with binary ports. Doing so
1570 ``upgrades'' the port from binary to textual, under the ISO-8859-1
1571 encoding. Likewise, Guile does not prevent use of
1572 @code{set-port-encoding!} on a binary port, which also turns it into a
1573 ``textual'' port.
1574 @end deffn
1575
1576 @deffn {Scheme Procedure} textual-port? port
1577 Always return @var{#t}, as all ports can be used for textual I/O in
1578 Guile.
1579 @end deffn
1580
1581 @deffn {Scheme Procedure} transcoded-port obj
1582 The @code{transcoded-port} procedure
1583 returns a new textual port with the specified @var{transcoder}.
1584 Otherwise the new textual port's state is largely the same as
1585 that of @var{binary-port}.
1586 If @var{binary-port} is an input port, the new textual
1587 port will be an input port and
1588 will transcode the bytes that have not yet been read from
1589 @var{binary-port}.
1590 If @var{binary-port} is an output port, the new textual
1591 port will be an output port and
1592 will transcode output characters into bytes that are
1593 written to the byte sink represented by @var{binary-port}.
1594
1595 As a side effect, however, @code{transcoded-port}
1596 closes @var{binary-port} in
1597 a special way that allows the new textual port to continue to
1598 use the byte source or sink represented by @var{binary-port},
1599 even though @var{binary-port} itself is closed and cannot
1600 be used by the input and output operations described in this
1601 chapter.
1602 @end deffn
1603
1604 @deffn {Scheme Procedure} port-position port
1605 If @var{port} supports it (see below), return the offset (an integer)
1606 indicating where the next octet will be read from/written to in
1607 @var{port}. If @var{port} does not support this operation, an error
1608 condition is raised.
1609
1610 This is similar to Guile's @code{seek} procedure with the
1611 @code{SEEK_CUR} argument (@pxref{Random Access}).
1612 @end deffn
1613
1614 @deffn {Scheme Procedure} port-has-port-position? port
1615 Return @code{#t} is @var{port} supports @code{port-position}.
1616 @end deffn
1617
1618 @deffn {Scheme Procedure} set-port-position! port offset
1619 If @var{port} supports it (see below), set the position where the next
1620 octet will be read from/written to @var{port} to @var{offset} (an
1621 integer). If @var{port} does not support this operation, an error
1622 condition is raised.
1623
1624 This is similar to Guile's @code{seek} procedure with the
1625 @code{SEEK_SET} argument (@pxref{Random Access}).
1626 @end deffn
1627
1628 @deffn {Scheme Procedure} port-has-set-port-position!? port
1629 Return @code{#t} is @var{port} supports @code{set-port-position!}.
1630 @end deffn
1631
1632 @deffn {Scheme Procedure} call-with-port port proc
1633 Call @var{proc}, passing it @var{port} and closing @var{port} upon exit
1634 of @var{proc}. Return the return values of @var{proc}.
1635 @end deffn
1636
1637 @node R6RS Input Ports
1638 @subsubsection Input Ports
1639
1640 @deffn {Scheme Procedure} input-port? obj@
1641 Returns @code{#t} if the argument is an input port (or a combined input
1642 and output port), and returns @code{#f} otherwise.
1643 @end deffn
1644
1645 @deffn {Scheme Procedure} port-eof? port
1646 Returns @code{#t}
1647 if the @code{lookahead-u8} procedure (if @var{input-port} is a binary port)
1648 or the @code{lookahead-char} procedure (if @var{input-port} is a textual port)
1649 would return
1650 the end-of-file object, and @code{#f} otherwise.
1651 The operation may block indefinitely if no data is available
1652 but the port cannot be determined to be at end of file.
1653 @end deffn
1654
1655 @deffn {Scheme Procedure} open-file-input-port filename
1656 @deffnx {Scheme Procedure} open-file-input-port filename file-options
1657 @deffnx {Scheme Procedure} open-file-input-port filename file-options buffer-mode
1658 @deffnx {Scheme Procedure} open-file-input-port filename file-options buffer-mode maybe-transcoder
1659 @var{Maybe-transcoder} must be either a transcoder or @code{#f}.
1660
1661 The @code{open-file-input-port} procedure returns an
1662 input port for the named file. The @var{file-options} and
1663 @var{maybe-transcoder} arguments are optional.
1664
1665 The @var{file-options} argument, which may determine
1666 various aspects of the returned port (@pxref{R6RS File Options}),
1667 defaults to the value of @code{(file-options)}.
1668
1669 The @var{buffer-mode} argument, if supplied,
1670 must be one of the symbols that name a buffer mode.
1671 The @var{buffer-mode} argument defaults to @code{block}.
1672
1673 If @var{maybe-transcoder} is a transcoder, it becomes the transcoder associated
1674 with the returned port.
1675
1676 If @var{maybe-transcoder} is @code{#f} or absent,
1677 the port will be a binary port and will support the
1678 @code{port-position} and @code{set-port-position!} operations.
1679 Otherwise the port will be a textual port, and whether it supports
1680 the @code{port-position} and @code{set-port-position!} operations
1681 is implementation-dependent (and possibly transcoder-dependent).
1682 @end deffn
1683
1684 @deffn {Scheme Procedure} standard-input-port
1685 Returns a fresh binary input port connected to standard input. Whether
1686 the port supports the @code{port-position} and @code{set-port-position!}
1687 operations is implementation-dependent.
1688 @end deffn
1689
1690 @deffn {Scheme Procedure} current-input-port
1691 This returns a default textual port for input. Normally, this default
1692 port is associated with standard input, but can be dynamically
1693 re-assigned using the @code{with-input-from-file} procedure from the
1694 @code{io simple (6)} library (@pxref{rnrs io simple}). The port may or
1695 may not have an associated transcoder; if it does, the transcoder is
1696 implementation-dependent.
1697 @end deffn
1698
1699 @node R6RS Binary Input
1700 @subsubsection Binary Input
1701
1702 @cindex binary input
1703
1704 R6RS binary input ports can be created with the procedures described
1705 below.
1706
1707 @deffn {Scheme Procedure} open-bytevector-input-port bv [transcoder]
1708 @deffnx {C Function} scm_open_bytevector_input_port (bv, transcoder)
1709 Return an input port whose contents are drawn from bytevector @var{bv}
1710 (@pxref{Bytevectors}).
1711
1712 @c FIXME: Update description when implemented.
1713 The @var{transcoder} argument is currently not supported.
1714 @end deffn
1715
1716 @cindex custom binary input ports
1717
1718 @deffn {Scheme Procedure} make-custom-binary-input-port id read! get-position set-position! close
1719 @deffnx {C Function} scm_make_custom_binary_input_port (id, read!, get-position, set-position!, close)
1720 Return a new custom binary input port@footnote{This is similar in spirit
1721 to Guile's @dfn{soft ports} (@pxref{Soft Ports}).} named @var{id} (a
1722 string) whose input is drained by invoking @var{read!} and passing it a
1723 bytevector, an index where bytes should be written, and the number of
1724 bytes to read. The @code{read!} procedure must return an integer
1725 indicating the number of bytes read, or @code{0} to indicate the
1726 end-of-file.
1727
1728 Optionally, if @var{get-position} is not @code{#f}, it must be a thunk
1729 that will be called when @var{port-position} is invoked on the custom
1730 binary port and should return an integer indicating the position within
1731 the underlying data stream; if @var{get-position} was not supplied, the
1732 returned port does not support @var{port-position}.
1733
1734 Likewise, if @var{set-position!} is not @code{#f}, it should be a
1735 one-argument procedure. When @var{set-port-position!} is invoked on the
1736 custom binary input port, @var{set-position!} is passed an integer
1737 indicating the position of the next byte is to read.
1738
1739 Finally, if @var{close} is not @code{#f}, it must be a thunk. It is
1740 invoked when the custom binary input port is closed.
1741
1742 Using a custom binary input port, the @code{open-bytevector-input-port}
1743 procedure could be implemented as follows:
1744
1745 @lisp
1746 (define (open-bytevector-input-port source)
1747 (define position 0)
1748 (define length (bytevector-length source))
1749
1750 (define (read! bv start count)
1751 (let ((count (min count (- length position))))
1752 (bytevector-copy! source position
1753 bv start count)
1754 (set! position (+ position count))
1755 count))
1756
1757 (define (get-position) position)
1758
1759 (define (set-position! new-position)
1760 (set! position new-position))
1761
1762 (make-custom-binary-input-port "the port" read!
1763 get-position
1764 set-position!))
1765
1766 (read (open-bytevector-input-port (string->utf8 "hello")))
1767 @result{} hello
1768 @end lisp
1769 @end deffn
1770
1771 @cindex binary input
1772 Binary input is achieved using the procedures below:
1773
1774 @deffn {Scheme Procedure} get-u8 port
1775 @deffnx {C Function} scm_get_u8 (port)
1776 Return an octet read from @var{port}, a binary input port, blocking as
1777 necessary, or the end-of-file object.
1778 @end deffn
1779
1780 @deffn {Scheme Procedure} lookahead-u8 port
1781 @deffnx {C Function} scm_lookahead_u8 (port)
1782 Like @code{get-u8} but does not update @var{port}'s position to point
1783 past the octet.
1784 @end deffn
1785
1786 @deffn {Scheme Procedure} get-bytevector-n port count
1787 @deffnx {C Function} scm_get_bytevector_n (port, count)
1788 Read @var{count} octets from @var{port}, blocking as necessary and
1789 return a bytevector containing the octets read. If fewer bytes are
1790 available, a bytevector smaller than @var{count} is returned.
1791 @end deffn
1792
1793 @deffn {Scheme Procedure} get-bytevector-n! port bv start count
1794 @deffnx {C Function} scm_get_bytevector_n_x (port, bv, start, count)
1795 Read @var{count} bytes from @var{port} and store them in @var{bv}
1796 starting at index @var{start}. Return either the number of bytes
1797 actually read or the end-of-file object.
1798 @end deffn
1799
1800 @deffn {Scheme Procedure} get-bytevector-some port
1801 @deffnx {C Function} scm_get_bytevector_some (port)
1802 Read from @var{port}, blocking as necessary, until data are available or
1803 and end-of-file is reached. Return either a new bytevector containing
1804 the data read or the end-of-file object.
1805 @end deffn
1806
1807 @deffn {Scheme Procedure} get-bytevector-all port
1808 @deffnx {C Function} scm_get_bytevector_all (port)
1809 Read from @var{port}, blocking as necessary, until the end-of-file is
1810 reached. Return either a new bytevector containing the data read or the
1811 end-of-file object (if no data were available).
1812 @end deffn
1813
1814 @node R6RS Textual Input
1815 @subsubsection Textual Input
1816
1817 @deffn {Scheme Procedure} get-char port
1818 Reads from @var{textual-input-port}, blocking as necessary, until a
1819 complete character is available from @var{textual-input-port},
1820 or until an end of file is reached.
1821
1822 If a complete character is available before the next end of file,
1823 @code{get-char} returns that character and updates the input port to
1824 point past the character. If an end of file is reached before any
1825 character is read, @code{get-char} returns the end-of-file object.
1826 @end deffn
1827
1828 @deffn {Scheme Procedure} lookahead-char port
1829 The @code{lookahead-char} procedure is like @code{get-char}, but it does
1830 not update @var{textual-input-port} to point past the character.
1831 @end deffn
1832
1833 @deffn {Scheme Procedure} get-string-n port count
1834
1835 @var{Count} must be an exact, non-negative integer object, representing
1836 the number of characters to be read.
1837
1838 The @code{get-string-n} procedure reads from @var{textual-input-port},
1839 blocking as necessary, until @var{count} characters are available, or
1840 until an end of file is reached.
1841
1842 If @var{count} characters are available before end of file,
1843 @code{get-string-n} returns a string consisting of those @var{count}
1844 characters. If fewer characters are available before an end of file, but
1845 one or more characters can be read, @code{get-string-n} returns a string
1846 containing those characters. In either case, the input port is updated
1847 to point just past the characters read. If no characters can be read
1848 before an end of file, the end-of-file object is returned.
1849 @end deffn
1850
1851 @deffn {Scheme Procedure} get-string-n! port string start count
1852
1853 @var{Start} and @var{count} must be exact, non-negative integer objects,
1854 with @var{count} representing the number of characters to be read.
1855 @var{String} must be a string with at least $@var{start} + @var{count}$
1856 characters.
1857
1858 The @code{get-string-n!} procedure reads from @var{textual-input-port}
1859 in the same manner as @code{get-string-n}. If @var{count} characters
1860 are available before an end of file, they are written into @var{string}
1861 starting at index @var{start}, and @var{count} is returned. If fewer
1862 characters are available before an end of file, but one or more can be
1863 read, those characters are written into @var{string} starting at index
1864 @var{start} and the number of characters actually read is returned as an
1865 exact integer object. If no characters can be read before an end of
1866 file, the end-of-file object is returned.
1867 @end deffn
1868
1869 @deffn {Scheme Procedure} get-string-all port count
1870 Reads from @var{textual-input-port} until an end of file, decoding
1871 characters in the same manner as @code{get-string-n} and
1872 @code{get-string-n!}.
1873
1874 If characters are available before the end of file, a string containing
1875 all the characters decoded from that data are returned. If no character
1876 precedes the end of file, the end-of-file object is returned.
1877 @end deffn
1878
1879 @deffn {Scheme Procedure} get-line port
1880 Reads from @var{textual-input-port} up to and including the linefeed
1881 character or end of file, decoding characters in the same manner as
1882 @code{get-string-n} and @code{get-string-n!}.
1883
1884 If a linefeed character is read, a string containing all of the text up
1885 to (but not including) the linefeed character is returned, and the port
1886 is updated to point just past the linefeed character. If an end of file
1887 is encountered before any linefeed character is read, but some
1888 characters have been read and decoded as characters, a string containing
1889 those characters is returned. If an end of file is encountered before
1890 any characters are read, the end-of-file object is returned.
1891
1892 @quotation Note
1893 The end-of-line style, if not @code{none}, will cause all line endings
1894 to be read as linefeed characters. @xref{R6RS Transcoders}.
1895 @end quotation
1896 @end deffn
1897
1898 @deffn {Scheme Procedure} get-datum port count
1899 Reads an external representation from @var{textual-input-port} and returns the
1900 datum it represents. The @code{get-datum} procedure returns the next
1901 datum that can be parsed from the given @var{textual-input-port}, updating
1902 @var{textual-input-port} to point exactly past the end of the external
1903 representation of the object.
1904
1905 Any @emph{interlexeme space} (comment or whitespace, @pxref{Scheme
1906 Syntax}) in the input is first skipped. If an end of file occurs after
1907 the interlexeme space, the end-of-file object (@pxref{R6RS End-of-File})
1908 is returned.
1909
1910 If a character inconsistent with an external representation is
1911 encountered in the input, an exception with condition types
1912 @code{&lexical} and @code{&i/o-read} is raised. Also, if the end of
1913 file is encountered after the beginning of an external representation,
1914 but the external representation is incomplete and therefore cannot be
1915 parsed, an exception with condition types @code{&lexical} and
1916 @code{&i/o-read} is raised.
1917 @end deffn
1918
1919 @node R6RS Output Ports
1920 @subsubsection Output Ports
1921
1922 @deffn {Scheme Procedure} output-port? obj
1923 Returns @code{#t} if the argument is an output port (or a
1924 combined input and output port), @code{#f} otherwise.
1925 @end deffn
1926
1927 @deffn {Scheme Procedure} flush-output-port port
1928 Flushes any buffered output from the buffer of @var{output-port} to the
1929 underlying file, device, or object. The @code{flush-output-port}
1930 procedure returns an unspecified values.
1931 @end deffn
1932
1933 @deffn {Scheme Procedure} open-file-output-port filename
1934 @deffnx {Scheme Procedure} open-file-output-port filename file-options
1935 @deffnx {Scheme Procedure} open-file-output-port filename file-options buffer-mode
1936 @deffnx {Scheme Procedure} open-file-output-port filename file-options buffer-mode maybe-transcoder
1937
1938 @var{maybe-transcoder} must be either a transcoder or @code{#f}.
1939
1940 The @code{open-file-output-port} procedure returns an output port for the named file.
1941
1942 The @var{file-options} argument, which may determine various aspects of
1943 the returned port (@pxref{R6RS File Options}), defaults to the value of
1944 @code{(file-options)}.
1945
1946 The @var{buffer-mode} argument, if supplied,
1947 must be one of the symbols that name a buffer mode.
1948 The @var{buffer-mode} argument defaults to @code{block}.
1949
1950 If @var{maybe-transcoder} is a transcoder, it becomes the transcoder
1951 associated with the port.
1952
1953 If @var{maybe-transcoder} is @code{#f} or absent,
1954 the port will be a binary port and will support the
1955 @code{port-position} and @code{set-port-position!} operations.
1956 Otherwise the port will be a textual port, and whether it supports
1957 the @code{port-position} and @code{set-port-position!} operations
1958 is implementation-dependent (and possibly transcoder-dependent).
1959 @end deffn
1960
1961 @deffn {Scheme Procedure} standard-output-port
1962 @deffnx {Scheme Procedure} standard-error-port
1963 Returns a fresh binary output port connected to the standard output or
1964 standard error respectively. Whether the port supports the
1965 @code{port-position} and @code{set-port-position!} operations is
1966 implementation-dependent.
1967 @end deffn
1968
1969 @deffn {Scheme Procedure} current-output-port
1970 @deffnx {Scheme Procedure} current-error-port
1971 These return default textual ports for regular output and error output.
1972 Normally, these default ports are associated with standard output, and
1973 standard error, respectively. The return value of
1974 @code{current-output-port} can be dynamically re-assigned using the
1975 @code{with-output-to-file} procedure from the @code{io simple (6)}
1976 library (@pxref{rnrs io simple}). A port returned by one of these
1977 procedures may or may not have an associated transcoder; if it does, the
1978 transcoder is implementation-dependent.
1979 @end deffn
1980
1981 @node R6RS Binary Output
1982 @subsubsection Binary Output
1983
1984 Binary output ports can be created with the procedures below.
1985
1986 @deffn {Scheme Procedure} open-bytevector-output-port [transcoder]
1987 @deffnx {C Function} scm_open_bytevector_output_port (transcoder)
1988 Return two values: a binary output port and a procedure. The latter
1989 should be called with zero arguments to obtain a bytevector containing
1990 the data accumulated by the port, as illustrated below.
1991
1992 @lisp
1993 (call-with-values
1994 (lambda ()
1995 (open-bytevector-output-port))
1996 (lambda (port get-bytevector)
1997 (display "hello" port)
1998 (get-bytevector)))
1999
2000 @result{} #vu8(104 101 108 108 111)
2001 @end lisp
2002
2003 @c FIXME: Update description when implemented.
2004 The @var{transcoder} argument is currently not supported.
2005 @end deffn
2006
2007 @cindex custom binary output ports
2008
2009 @deffn {Scheme Procedure} make-custom-binary-output-port id write! get-position set-position! close
2010 @deffnx {C Function} scm_make_custom_binary_output_port (id, write!, get-position, set-position!, close)
2011 Return a new custom binary output port named @var{id} (a string) whose
2012 output is sunk by invoking @var{write!} and passing it a bytevector, an
2013 index where bytes should be read from this bytevector, and the number of
2014 bytes to be ``written''. The @code{write!} procedure must return an
2015 integer indicating the number of bytes actually written; when it is
2016 passed @code{0} as the number of bytes to write, it should behave as
2017 though an end-of-file was sent to the byte sink.
2018
2019 The other arguments are as for @code{make-custom-binary-input-port}
2020 (@pxref{R6RS Binary Input, @code{make-custom-binary-input-port}}).
2021 @end deffn
2022
2023 @cindex binary output
2024 Writing to a binary output port can be done using the following
2025 procedures:
2026
2027 @deffn {Scheme Procedure} put-u8 port octet
2028 @deffnx {C Function} scm_put_u8 (port, octet)
2029 Write @var{octet}, an integer in the 0--255 range, to @var{port}, a
2030 binary output port.
2031 @end deffn
2032
2033 @deffn {Scheme Procedure} put-bytevector port bv [start [count]]
2034 @deffnx {C Function} scm_put_bytevector (port, bv, start, count)
2035 Write the contents of @var{bv} to @var{port}, optionally starting at
2036 index @var{start} and limiting to @var{count} octets.
2037 @end deffn
2038
2039 @node R6RS Textual Output
2040 @subsubsection Textual Output
2041
2042 @deffn {Scheme Procedure} put-char port char
2043 Writes @var{char} to the port. The @code{put-char} procedure returns
2044 @end deffn
2045
2046 @deffn {Scheme Procedure} put-string port string
2047 @deffnx {Scheme Procedure} put-string port string start
2048 @deffnx {Scheme Procedure} put-string port string start count
2049
2050 @var{start} and @var{count} must be non-negative exact integer objects.
2051 @var{string} must have a length of at least @math{@var{start} +
2052 @var{count}}. @var{start} defaults to 0. @var{count} defaults to
2053 @math{@code{(string-length @var{string})} - @var{start}}$. The
2054 @code{put-string} procedure writes the @var{count} characters of
2055 @var{string} starting at index @var{start} to the port. The
2056 @code{put-string} procedure returns an unspecified value.
2057 @end deffn
2058
2059 @deffn {Scheme Procedure} put-datum port datum
2060 @var{datum} should be a datum value. The @code{put-datum} procedure
2061 writes an external representation of @var{datum} to
2062 @var{textual-output-port}. The specific external representation is
2063 implementation-dependent. However, whenever possible, an implementation
2064 should produce a representation for which @code{get-datum}, when reading
2065 the representation, will return an object equal (in the sense of
2066 @code{equal?}) to @var{datum}.
2067
2068 @quotation Note
2069 Not all datums may allow producing an external representation for which
2070 @code{get-datum} will produce an object that is equal to the
2071 original. Specifically, NaNs contained in @var{datum} may make
2072 this impossible.
2073 @end quotation
2074
2075 @quotation Note
2076 The @code{put-datum} procedure merely writes the external
2077 representation, but no trailing delimiter. If @code{put-datum} is
2078 used to write several subsequent external representations to an
2079 output port, care should be taken to delimit them properly so they can
2080 be read back in by subsequent calls to @code{get-datum}.
2081 @end quotation
2082 @end deffn
2083
2084 @node I/O Extensions
2085 @subsection Using and Extending Ports in C
2086
2087 @menu
2088 * C Port Interface:: Using ports from C.
2089 * Port Implementation:: How to implement a new port type in C.
2090 @end menu
2091
2092
2093 @node C Port Interface
2094 @subsubsection C Port Interface
2095 @cindex C port interface
2096 @cindex Port, C interface
2097
2098 This section describes how to use Scheme ports from C.
2099
2100 @subsubheading Port basics
2101
2102 @cindex ptob
2103 @tindex scm_ptob_descriptor
2104 @tindex scm_port
2105 @findex SCM_PTAB_ENTRY
2106 @findex SCM_PTOBNUM
2107 @vindex scm_ptobs
2108 There are two main data structures. A port type object (ptob) is of
2109 type @code{scm_ptob_descriptor}. A port instance is of type
2110 @code{scm_port}. Given an @code{SCM} variable which points to a port,
2111 the corresponding C port object can be obtained using the
2112 @code{SCM_PTAB_ENTRY} macro. The ptob can be obtained by using
2113 @code{SCM_PTOBNUM} to give an index into the @code{scm_ptobs}
2114 global array.
2115
2116 @subsubheading Port buffers
2117
2118 An input port always has a read buffer and an output port always has a
2119 write buffer. However the size of these buffers is not guaranteed to be
2120 more than one byte (e.g., the @code{shortbuf} field in @code{scm_port}
2121 which is used when no other buffer is allocated). The way in which the
2122 buffers are allocated depends on the implementation of the ptob. For
2123 example in the case of an fport, buffers may be allocated with malloc
2124 when the port is created, but in the case of an strport the underlying
2125 string is used as the buffer.
2126
2127 @subsubheading The @code{rw_random} flag
2128
2129 Special treatment is required for ports which can be seeked at random.
2130 Before various operations, such as seeking the port or changing from
2131 input to output on a bidirectional port or vice versa, the port
2132 implementation must be given a chance to update its state. The write
2133 buffer is updated by calling the @code{flush} ptob procedure and the
2134 input buffer is updated by calling the @code{end_input} ptob procedure.
2135 In the case of an fport, @code{flush} causes buffered output to be
2136 written to the file descriptor, while @code{end_input} causes the
2137 descriptor position to be adjusted to account for buffered input which
2138 was never read.
2139
2140 The special treatment must be performed if the @code{rw_random} flag in
2141 the port is non-zero.
2142
2143 @subsubheading The @code{rw_active} variable
2144
2145 The @code{rw_active} variable in the port is only used if
2146 @code{rw_random} is set. It's defined as an enum with the following
2147 values:
2148
2149 @table @code
2150 @item SCM_PORT_READ
2151 the read buffer may have unread data.
2152
2153 @item SCM_PORT_WRITE
2154 the write buffer may have unwritten data.
2155
2156 @item SCM_PORT_NEITHER
2157 neither the write nor the read buffer has data.
2158 @end table
2159
2160 @subsubheading Reading from a port.
2161
2162 To read from a port, it's possible to either call existing libguile
2163 procedures such as @code{scm_getc} and @code{scm_read_line} or to read
2164 data from the read buffer directly. Reading from the buffer involves
2165 the following steps:
2166
2167 @enumerate
2168 @item
2169 Flush output on the port, if @code{rw_active} is @code{SCM_PORT_WRITE}.
2170
2171 @item
2172 Fill the read buffer, if it's empty, using @code{scm_fill_input}.
2173
2174 @item Read the data from the buffer and update the read position in
2175 the buffer. Steps 2) and 3) may be repeated as many times as required.
2176
2177 @item Set rw_active to @code{SCM_PORT_READ} if @code{rw_random} is set.
2178
2179 @item update the port's line and column counts.
2180 @end enumerate
2181
2182 @subsubheading Writing to a port.
2183
2184 To write data to a port, calling @code{scm_lfwrite} should be sufficient for
2185 most purposes. This takes care of the following steps:
2186
2187 @enumerate
2188 @item
2189 End input on the port, if @code{rw_active} is @code{SCM_PORT_READ}.
2190
2191 @item
2192 Pass the data to the ptob implementation using the @code{write} ptob
2193 procedure. The advantage of using the ptob @code{write} instead of
2194 manipulating the write buffer directly is that it allows the data to be
2195 written in one operation even if the port is using the single-byte
2196 @code{shortbuf}.
2197
2198 @item
2199 Set @code{rw_active} to @code{SCM_PORT_WRITE} if @code{rw_random}
2200 is set.
2201 @end enumerate
2202
2203
2204 @node Port Implementation
2205 @subsubsection Port Implementation
2206 @cindex Port implementation
2207
2208 This section describes how to implement a new port type in C.
2209
2210 As described in the previous section, a port type object (ptob) is
2211 a structure of type @code{scm_ptob_descriptor}. A ptob is created by
2212 calling @code{scm_make_port_type}.
2213
2214 @deftypefun scm_t_bits scm_make_port_type (char *name, int (*fill_input) (SCM port), void (*write) (SCM port, const void *data, size_t size))
2215 Return a new port type object. The @var{name}, @var{fill_input} and
2216 @var{write} parameters are initial values for those port type fields,
2217 as described below. The other fields are initialized with default
2218 values and can be changed later.
2219 @end deftypefun
2220
2221 All of the elements of the ptob, apart from @code{name}, are procedures
2222 which collectively implement the port behaviour. Creating a new port
2223 type mostly involves writing these procedures.
2224
2225 @table @code
2226 @item name
2227 A pointer to a NUL terminated string: the name of the port type. This
2228 is the only element of @code{scm_ptob_descriptor} which is not
2229 a procedure. Set via the first argument to @code{scm_make_port_type}.
2230
2231 @item mark
2232 Called during garbage collection to mark any SCM objects that a port
2233 object may contain. It doesn't need to be set unless the port has
2234 @code{SCM} components. Set using
2235
2236 @deftypefun void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM port))
2237 @end deftypefun
2238
2239 @item free
2240 Called when the port is collected during gc. It
2241 should free any resources used by the port.
2242 Set using
2243
2244 @deftypefun void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM port))
2245 @end deftypefun
2246
2247 @item print
2248 Called when @code{write} is called on the port object, to print a
2249 port description. E.g., for an fport it may produce something like:
2250 @code{#<input: /etc/passwd 3>}. Set using
2251
2252 @deftypefun void scm_set_port_print (scm_t_bits tc, int (*print) (SCM port, SCM dest_port, scm_print_state *pstate))
2253 The first argument @var{port} is the object being printed, the second
2254 argument @var{dest_port} is where its description should go.
2255 @end deftypefun
2256
2257 @item equalp
2258 Not used at present. Set using
2259
2260 @deftypefun void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
2261 @end deftypefun
2262
2263 @item close
2264 Called when the port is closed, unless it was collected during gc. It
2265 should free any resources used by the port.
2266 Set using
2267
2268 @deftypefun void scm_set_port_close (scm_t_bits tc, int (*close) (SCM port))
2269 @end deftypefun
2270
2271 @item write
2272 Accept data which is to be written using the port. The port implementation
2273 may choose to buffer the data instead of processing it directly.
2274 Set via the third argument to @code{scm_make_port_type}.
2275
2276 @item flush
2277 Complete the processing of buffered output data. Reset the value of
2278 @code{rw_active} to @code{SCM_PORT_NEITHER}.
2279 Set using
2280
2281 @deftypefun void scm_set_port_flush (scm_t_bits tc, void (*flush) (SCM port))
2282 @end deftypefun
2283
2284 @item end_input
2285 Perform any synchronization required when switching from input to output
2286 on the port. Reset the value of @code{rw_active} to @code{SCM_PORT_NEITHER}.
2287 Set using
2288
2289 @deftypefun void scm_set_port_end_input (scm_t_bits tc, void (*end_input) (SCM port, int offset))
2290 @end deftypefun
2291
2292 @item fill_input
2293 Read new data into the read buffer and return the first character. It
2294 can be assumed that the read buffer is empty when this procedure is called.
2295 Set via the second argument to @code{scm_make_port_type}.
2296
2297 @item input_waiting
2298 Return a lower bound on the number of bytes that could be read from the
2299 port without blocking. It can be assumed that the current state of
2300 @code{rw_active} is @code{SCM_PORT_NEITHER}.
2301 Set using
2302
2303 @deftypefun void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM port))
2304 @end deftypefun
2305
2306 @item seek
2307 Set the current position of the port. The procedure can not make
2308 any assumptions about the value of @code{rw_active} when it's
2309 called. It can reset the buffers first if desired by using something
2310 like:
2311
2312 @example
2313 if (pt->rw_active == SCM_PORT_READ)
2314 scm_end_input (port);
2315 else if (pt->rw_active == SCM_PORT_WRITE)
2316 ptob->flush (port);
2317 @end example
2318
2319 However note that this will have the side effect of discarding any data
2320 in the unread-char buffer, in addition to any side effects from the
2321 @code{end_input} and @code{flush} ptob procedures. This is undesirable
2322 when seek is called to measure the current position of the port, i.e.,
2323 @code{(seek p 0 SEEK_CUR)}. The libguile fport and string port
2324 implementations take care to avoid this problem.
2325
2326 The procedure is set using
2327
2328 @deftypefun void scm_set_port_seek (scm_t_bits tc, scm_t_off (*seek) (SCM port, scm_t_off offset, int whence))
2329 @end deftypefun
2330
2331 @item truncate
2332 Truncate the port data to be specified length. It can be assumed that the
2333 current state of @code{rw_active} is @code{SCM_PORT_NEITHER}.
2334 Set using
2335
2336 @deftypefun void scm_set_port_truncate (scm_t_bits tc, void (*truncate) (SCM port, scm_t_off length))
2337 @end deftypefun
2338
2339 @end table
2340
2341 @c Local Variables:
2342 @c TeX-master: "guile.texi"
2343 @c End: