update URI documentation
[bpt/guile.git] / doc / ref / api-io.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
7b041912 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2009, 2010
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
07d83abe
MV
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.
b242715b 20* R6RS I/O Ports:: The R6RS port API.
07d83abe
MV
21* I/O Extensions:: Using and extending ports in C.
22@end menu
23
24
25@node Ports
26@subsection Ports
bf5df489 27@cindex Port
07d83abe
MV
28
29Sequential input/output in Scheme is represented by operations on a
30@dfn{port}. This chapter explains the operations that Guile provides
31for working with ports.
32
33Ports 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
35written to an output port, or both on an input/output port. A port
36can be closed (@pxref{Closing}) when no longer required, after which
37any attempt to read or write is an error.
38
39The formal definition of a port is very generic: an input port is
40simply ``an object which can deliver characters on demand,'' and an
41output port is ``an object which can accept characters.'' Because
42this definition is so loose, it is easy to write functions that
43simulate ports in software. @dfn{Soft ports} and @dfn{string ports}
44are two interesting and powerful examples of this technique.
45(@pxref{Soft Ports}, and @ref{String Ports}.)
46
47Ports are garbage collected in the usual way (@pxref{Memory
48Management}), and will be closed at that time if not already closed.
28cc8dac 49In this case any errors occurring in the close will not be reported.
07d83abe
MV
50Usually a program will want to explicitly close so as to be sure all
51its operations have been successful. Of course if a program has
52abandoned something due to an error or other condition then closing
53problems are probably not of interest.
54
55It is strongly recommended that file ports be closed explicitly when
56no longer required. Most systems have limits on how many files can be
57open, both on a per-process and a system-wide basis. A program that
58uses many files should take care not to hit those limits. The same
59applies to similar system resources such as pipes and sockets.
60
61Note that automatic garbage collection is triggered only by memory
62consumption, not by file or other resource usage, so a program cannot
63rely 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.
65If program flow makes it hard to be certain when to close then this
66may be an acceptable way to control resource usage.
67
40296bab
KR
68All file access uses the ``LFS'' large file support functions when
69available, so files bigger than 2 Gbytes (@math{2^31} bytes) can be
70read and written on a 32-bit system.
71
28cc8dac
MG
72Each port has an associated character encoding that controls how bytes
73read from the port are converted to characters and string and controls
74how characters and strings written to the port are converted to bytes.
75When ports are created, they inherit their character encoding from the
76current locale, but, that can be modified after the port is created.
77
912a8702
MG
78Currently, the ports only work with @emph{non-modal} encodings. Most
79encodings are non-modal, meaning that the conversion of bytes to a
80string doesn't depend on its context: the same byte sequence will always
81return the same string. A couple of modal encodings are in common use,
82like ISO-2022-JP and ISO-2022-KR, and they are not yet supported.
83
28cc8dac
MG
84Each port also has an associated conversion strategy: what to do when
85a Guile character can't be converted to the port's encoded character
86representation for output. There are three possible strategies: to
87raise an error, to replace the character with a hex escape, or to
88replace the character with a substitute character.
89
07d83abe
MV
90@rnindex input-port?
91@deffn {Scheme Procedure} input-port? x
92@deffnx {C Function} scm_input_port_p (x)
93Return @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)
101Return @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)
108Return a boolean indicating whether @var{x} is a port.
109Equivalent to @code{(or (input-port? @var{x}) (output-port?
110@var{x}))}.
111@end deffn
112
28cc8dac
MG
113@deffn {Scheme Procedure} set-port-encoding! port enc
114@deffnx {C Function} scm_set_port_encoding_x (port, enc)
4c7b9975
LC
115Sets 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
117encoding names are those
118@url{http://www.iana.org/assignments/character-sets, defined by IANA}.
28cc8dac 119@end deffn
d6a6989e
LC
120
121@defvr {Scheme Variable} %default-port-encoding
72b3aa56 122A fluid containing @code{#f} or the name of the encoding to
d6a6989e
LC
123be used by default for newly created ports (@pxref{Fluids and Dynamic
124States}). The value @code{#f} is equivalent to @code{"ISO-8859-1"}.
28cc8dac
MG
125
126New ports are created with the encoding appropriate for the current
4c7b9975
LC
127locale if @code{setlocale} has been called or the value specified by
128this fluid otherwise.
129@end defvr
28cc8dac
MG
130
131@deffn {Scheme Procedure} port-encoding port
132@deffnx {C Function} scm_port_encoding
211683cc
MG
133Returns, as a string, the character encoding that @var{port} uses to interpret
134its input and output. The value @code{#f} is equivalent to @code{"ISO-8859-1"}.
28cc8dac
MG
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)
139Sets the behavior of the interpreter when outputting a character that
140is not representable in the port's current encoding. @var{sym} can be
141either @code{'error}, @code{'substitute}, or @code{'escape}. If it is
142@code{'error}, an error will be thrown when an nonconvertible character
143is encountered. If it is @code{'substitute}, then nonconvertible
144characters will be replaced with approximate characters, or with
145question marks if no approximately correct character is available. If
146it is @code{'escape}, it will appear as a hex escape when output.
147
148If @var{port} is an open port, the conversion error behavior
149is set for that port. If it is @code{#f}, it is set as the
150default behavior for any future ports that get created in
151this thread.
152@end deffn
153
154@deffn {Scheme Procedure} port-conversion-strategy port
155@deffnx {C Function} scm_port_conversion_strategy (port)
156Returns the behavior of the port when outputting a character that is
157not representable in the port's current encoding. It returns the
158symbol @code{error} if unrepresentable characters should cause
159exceptions, @code{substitute} if the port should try to replace
160unrepresentable characters with question marks or approximate
161characters, or @code{escape} if unrepresentable characters should be
162converted to string escapes.
163
164If @var{port} is @code{#f}, then the current default behavior will be
165returned. New ports will have this default behavior when they are
166created.
167@end deffn
168
169
07d83abe
MV
170
171@node Reading
172@subsection Reading
bf5df489 173@cindex Reading
07d83abe
MV
174
175[Generic procedures for reading from ports.]
176
1518f649
AW
177These procedures pertain to reading characters and strings from
178ports. To read general S-expressions from ports, @xref{Scheme Read}.
179
07d83abe 180@rnindex eof-object?
bf5df489 181@cindex End of file object
07d83abe
MV
182@deffn {Scheme Procedure} eof-object? x
183@deffnx {C Function} scm_eof_object_p (x)
184Return @code{#t} if @var{x} is an end-of-file object; otherwise
185return @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)
191Return @code{#t} if a character is ready on input @var{port}
192and 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
195port at end of file then @code{char-ready?} returns @code{#t}.
cdf1ad3b
MV
196
197@code{char-ready?} exists to make it possible for a
07d83abe
MV
198program to accept characters from interactive ports without
199getting stuck waiting for input. Any input editors associated
200with such ports must make sure that characters whose existence
201has been asserted by @code{char-ready?} cannot be rubbed out.
202If @code{char-ready?} were to return @code{#f} at end of file,
203a port at end of file would be indistinguishable from an
cdf1ad3b 204interactive port that has no ready characters.
07d83abe
MV
205@end deffn
206
207@rnindex read-char
208@deffn {Scheme Procedure} read-char [port]
209@deffnx {C Function} scm_read_char (port)
210Return the next character available from @var{port}, updating
211@var{port} to point to the following character. If no more
212characters are available, the end-of-file object is returned.
213@end deffn
214
215@deftypefn {C Function} size_t scm_c_read (SCM port, void *buffer, size_t size)
216Read up to @var{size} bytes from @var{port} and store them in
217@var{buffer}. The return value is the number of bytes actually read,
218which can be less than @var{size} if end-of-file has been reached.
219
220Note that this function does not update @code{port-line} and
221@code{port-column} below.
222@end deftypefn
223
224@rnindex peek-char
225@deffn {Scheme Procedure} peek-char [port]
226@deffnx {C Function} scm_peek_char (port)
227Return the next character available from @var{port},
228@emph{without} updating @var{port} to point to the following
229character. If no more characters are available, the
cdf1ad3b
MV
230end-of-file object is returned.
231
232The value returned by
07d83abe
MV
233a call to @code{peek-char} is the same as the value that would
234have been returned by a call to @code{read-char} on the same
235port. The only difference is that the very next call to
236@code{read-char} or @code{peek-char} on that @var{port} will
237return the value returned by the preceding call to
238@code{peek-char}. In particular, a call to @code{peek-char} on
239an interactive port will hang waiting for input whenever a call
cdf1ad3b 240to @code{read-char} would have hung.
07d83abe
MV
241@end deffn
242
243@deffn {Scheme Procedure} unread-char cobj [port]
244@deffnx {C Function} scm_unread_char (cobj, port)
245Place @var{char} in @var{port} so that it will be read by the
246next read operation. If called multiple times, the unread characters
247will be read again in last-in first-out order. If @var{port} is
248not supplied, the current input port is used.
249@end deffn
250
251@deffn {Scheme Procedure} unread-string str port
252@deffnx {C Function} scm_unread_string (str, port)
253Place the string @var{str} in @var{port} so that its characters will
254be read from left-to-right as the next characters from @var{port}
255during subsequent read operations. If called multiple times, the
256unread characters will be read again in last-in first-out order. If
9782da8a 257@var{port} is not supplied, the @code{current-input-port} is used.
07d83abe
MV
258@end deffn
259
260@deffn {Scheme Procedure} drain-input port
261@deffnx {C Function} scm_drain_input (port)
262This procedure clears a port's input buffers, similar
263to the way that force-output clears the output buffer. The
264contents of the buffers are returned as a single string, e.g.,
265
266@lisp
267(define p (open-input-file ...))
268(drain-input p) => empty string, nothing buffered yet.
269(unread-char (read-char p) p)
270(drain-input p) => initial chars from p, up to the buffer size.
271@end lisp
272
273Draining the buffers may be useful for cleanly finishing
274buffered I/O so that the file descriptor can be used directly
275for further input.
276@end deffn
277
278@deffn {Scheme Procedure} port-column port
279@deffnx {Scheme Procedure} port-line port
280@deffnx {C Function} scm_port_column (port)
281@deffnx {C Function} scm_port_line (port)
282Return the current column number or line number of @var{port}.
283If the number is
284unknown, the result is #f. Otherwise, the result is a 0-origin integer
285- i.e.@: the first character of the first line is line 0, column 0.
286(However, when you display a file position, for example in an error
287message, we recommend you add 1 to get 1-origin integers. This is
288because lines and column numbers traditionally start with 1, and that is
289what non-programmers will find most natural.)
290@end deffn
291
292@deffn {Scheme Procedure} set-port-column! port column
293@deffnx {Scheme Procedure} set-port-line! port line
294@deffnx {C Function} scm_set_port_column_x (port, column)
295@deffnx {C Function} scm_set_port_line_x (port, line)
296Set the current column or line number of @var{port}.
297@end deffn
298
299@node Writing
300@subsection Writing
bf5df489 301@cindex Writing
07d83abe
MV
302
303[Generic procedures for writing to ports.]
304
1518f649
AW
305These procedures are for writing characters and strings to
306ports. For more information on writing arbitrary Scheme objects to
307ports, @xref{Scheme Write}.
308
07d83abe
MV
309@deffn {Scheme Procedure} get-print-state port
310@deffnx {C Function} scm_get_print_state (port)
311Return the print state of the port @var{port}. If @var{port}
312has no associated print state, @code{#f} is returned.
313@end deffn
314
07d83abe
MV
315@rnindex newline
316@deffn {Scheme Procedure} newline [port]
317@deffnx {C Function} scm_newline (port)
318Send a newline to @var{port}.
319If @var{port} is omitted, send to the current output port.
320@end deffn
321
cdf1ad3b 322@deffn {Scheme Procedure} port-with-print-state port [pstate]
07d83abe
MV
323@deffnx {C Function} scm_port_with_print_state (port, pstate)
324Create a new port which behaves like @var{port}, but with an
cdf1ad3b
MV
325included print state @var{pstate}. @var{pstate} is optional.
326If @var{pstate} isn't supplied and @var{port} already has
327a print state, the old print state is reused.
07d83abe
MV
328@end deffn
329
330@deffn {Scheme Procedure} print-options-interface [setting]
331@deffnx {C Function} scm_print_options (setting)
332Option interface for the print options. Instead of using
333this procedure directly, use the procedures
334@code{print-enable}, @code{print-disable}, @code{print-set!}
335and @code{print-options}.
336@end deffn
337
338@deffn {Scheme Procedure} simple-format destination message . args
339@deffnx {C Function} scm_simple_format (destination, message, args)
340Write @var{message} to @var{destination}, defaulting to
341the current output port.
342@var{message} can contain @code{~A} (was @code{%s}) and
343@code{~S} (was @code{%S}) escapes. When printed,
344the escapes are replaced with corresponding members of
345@var{ARGS}:
346@code{~A} formats using @code{display} and @code{~S} formats
347using @code{write}.
348If @var{destination} is @code{#t}, then use the current output
349port, if @var{destination} is @code{#f}, then return a string
350containing the formatted text. Does not add a trailing newline.
351@end deffn
352
353@rnindex write-char
354@deffn {Scheme Procedure} write-char chr [port]
355@deffnx {C Function} scm_write_char (chr, port)
356Send character @var{chr} to @var{port}.
357@end deffn
358
359@deftypefn {C Function} void scm_c_write (SCM port, const void *buffer, size_t size)
360Write @var{size} bytes at @var{buffer} to @var{port}.
361
362Note that this function does not update @code{port-line} and
363@code{port-column} (@pxref{Reading}).
364@end deftypefn
365
366@findex fflush
367@deffn {Scheme Procedure} force-output [port]
368@deffnx {C Function} scm_force_output (port)
369Flush the specified output port, or the current output port if @var{port}
370is omitted. The current output buffer contents are passed to the
371underlying port implementation (e.g., in the case of fports, the
372data will be written to the file and the output buffer will be cleared.)
373It has no effect on an unbuffered port.
374
375The return value is unspecified.
376@end deffn
377
378@deffn {Scheme Procedure} flush-all-ports
379@deffnx {C Function} scm_flush_all_ports ()
380Equivalent to calling @code{force-output} on
381all open output ports. The return value is unspecified.
382@end deffn
383
384
385@node Closing
386@subsection Closing
bf5df489
KR
387@cindex Closing ports
388@cindex Port, close
07d83abe
MV
389
390@deffn {Scheme Procedure} close-port port
391@deffnx {C Function} scm_close_port (port)
392Close the specified port object. Return @code{#t} if it
393successfully closes a port or @code{#f} if it was already
394closed. An exception may be raised if an error occurs, for
395example when flushing buffered output. See also @ref{Ports and
396File Descriptors, close}, for a procedure which can close file
397descriptors.
398@end deffn
399
400@deffn {Scheme Procedure} close-input-port port
401@deffnx {Scheme Procedure} close-output-port port
402@deffnx {C Function} scm_close_input_port (port)
403@deffnx {C Function} scm_close_output_port (port)
404@rnindex close-input-port
405@rnindex close-output-port
406Close the specified input or output @var{port}. An exception may be
407raised if an error occurs while closing. If @var{port} is already
408closed, nothing is done. The return value is unspecified.
409
410See also @ref{Ports and File Descriptors, close}, for a procedure
411which can close file descriptors.
412@end deffn
413
414@deffn {Scheme Procedure} port-closed? port
415@deffnx {C Function} scm_port_closed_p (port)
416Return @code{#t} if @var{port} is closed or @code{#f} if it is
417open.
418@end deffn
419
420
421@node Random Access
422@subsection Random Access
bf5df489
KR
423@cindex Random access, ports
424@cindex Port, random access
07d83abe
MV
425
426@deffn {Scheme Procedure} seek fd_port offset whence
427@deffnx {C Function} scm_seek (fd_port, offset, whence)
428Sets the current position of @var{fd/port} to the integer
429@var{offset}, which is interpreted according to the value of
430@var{whence}.
431
432One of the following variables should be supplied for
433@var{whence}:
434@defvar SEEK_SET
435Seek from the beginning of the file.
436@end defvar
437@defvar SEEK_CUR
438Seek from the current position.
439@end defvar
440@defvar SEEK_END
441Seek from the end of the file.
442@end defvar
443If @var{fd/port} is a file descriptor, the underlying system
444call is @code{lseek}. @var{port} may be a string port.
445
446The value returned is the new position in the file. This means
447that the current position of a port can be obtained using:
448@lisp
449(seek port 0 SEEK_CUR)
450@end lisp
451@end deffn
452
453@deffn {Scheme Procedure} ftell fd_port
454@deffnx {C Function} scm_ftell (fd_port)
455Return an integer representing the current position of
456@var{fd/port}, measured from the beginning. Equivalent to:
457
458@lisp
459(seek port 0 SEEK_CUR)
460@end lisp
461@end deffn
462
463@findex truncate
464@findex ftruncate
40296bab
KR
465@deffn {Scheme Procedure} truncate-file file [length]
466@deffnx {C Function} scm_truncate_file (file, length)
467Truncate @var{file} to @var{length} bytes. @var{file} can be a
468filename string, a port object, or an integer file descriptor. The
469return value is unspecified.
470
471For a port or file descriptor @var{length} can be omitted, in which
472case the file is truncated at the current position (per @code{ftell}
473above).
474
475On most systems a file can be extended by giving a length greater than
476the current size, but this is not mandatory in the POSIX standard.
07d83abe
MV
477@end deffn
478
479@node Line/Delimited
480@subsection Line Oriented and Delimited Text
bf5df489
KR
481@cindex Line input/output
482@cindex Port, line input/output
07d83abe
MV
483
484The delimited-I/O module can be accessed with:
485
aba0dff5 486@lisp
07d83abe 487(use-modules (ice-9 rdelim))
aba0dff5 488@end lisp
07d83abe
MV
489
490It can be used to read or write lines of text, or read text delimited by
491a specified set of characters. It's similar to the @code{(scsh rdelim)}
492module from guile-scsh, but does not use multiple values or character
493sets and has an extra procedure @code{write-line}.
494
495@c begin (scm-doc-string "rdelim.scm" "read-line")
496@deffn {Scheme Procedure} read-line [port] [handle-delim]
497Return a line of text from @var{port} if specified, otherwise from the
498value returned by @code{(current-input-port)}. Under Unix, a line of text
499is terminated by the first end-of-line character or by end-of-file.
500
501If @var{handle-delim} is specified, it should be one of the following
502symbols:
503@table @code
504@item trim
505Discard the terminating delimiter. This is the default, but it will
506be impossible to tell whether the read terminated with a delimiter or
507end-of-file.
508@item concat
509Append the terminating delimiter (if any) to the returned string.
510@item peek
511Push the terminating delimiter (if any) back on to the port.
512@item split
513Return a pair containing the string read from the port and the
514terminating delimiter or end-of-file object.
515@end table
516@end deffn
517
518@c begin (scm-doc-string "rdelim.scm" "read-line!")
519@deffn {Scheme Procedure} read-line! buf [port]
520Read a line of text into the supplied string @var{buf} and return the
521number of characters added to @var{buf}. If @var{buf} is filled, then
522@code{#f} is returned.
523Read from @var{port} if
524specified, otherwise from the value returned by @code{(current-input-port)}.
525@end deffn
526
527@c begin (scm-doc-string "rdelim.scm" "read-delimited")
528@deffn {Scheme Procedure} read-delimited delims [port] [handle-delim]
529Read text until one of the characters in the string @var{delims} is found
530or end-of-file is reached. Read from @var{port} if supplied, otherwise
531from the value returned by @code{(current-input-port)}.
532@var{handle-delim} takes the same values as described for @code{read-line}.
533@end deffn
534
535@c begin (scm-doc-string "rdelim.scm" "read-delimited!")
536@deffn {Scheme Procedure} read-delimited! delims buf [port] [handle-delim] [start] [end]
e7fb779f
AW
537Read text into the supplied string @var{buf}.
538
539If a delimiter was found, return the number of characters written,
540except if @var{handle-delim} is @code{split}, in which case the return
541value is a pair, as noted above.
542
543As a special case, if @var{port} was already at end-of-stream, the EOF
544object is returned. Also, if no characters were written because the
545buffer was full, @code{#f} is returned.
546
547It's something of a wacky interface, to be honest.
07d83abe
MV
548@end deffn
549
550@deffn {Scheme Procedure} write-line obj [port]
551@deffnx {C Function} scm_write_line (obj, port)
552Display @var{obj} and a newline character to @var{port}. If
553@var{port} is not specified, @code{(current-output-port)} is
554used. This function is equivalent to:
555@lisp
556(display obj [port])
557(newline [port])
558@end lisp
559@end deffn
560
28cc8dac 561Some of the aforementioned I/O functions rely on the following C
07d83abe
MV
562primitives. These will mainly be of interest to people hacking Guile
563internals.
564
565@deffn {Scheme Procedure} %read-delimited! delims str gobble [port [start [end]]]
566@deffnx {C Function} scm_read_delimited_x (delims, str, gobble, port, start, end)
567Read characters from @var{port} into @var{str} until one of the
568characters in the @var{delims} string is encountered. If
569@var{gobble} is true, discard the delimiter character;
570otherwise, leave it in the input stream for the next read. If
571@var{port} is not specified, use the value of
572@code{(current-input-port)}. If @var{start} or @var{end} are
573specified, store data only into the substring of @var{str}
574bounded by @var{start} and @var{end} (which default to the
575beginning and end of the string, respectively).
576
577 Return a pair consisting of the delimiter that terminated the
578string and the number of characters read. If reading stopped
579at the end of file, the delimiter returned is the
580@var{eof-object}; if the string was filled without encountering
581a delimiter, this value is @code{#f}.
582@end deffn
583
584@deffn {Scheme Procedure} %read-line [port]
585@deffnx {C Function} scm_read_line (port)
586Read a newline-terminated line from @var{port}, allocating storage as
587necessary. The newline terminator (if any) is removed from the string,
588and a pair consisting of the line and its delimiter is returned. The
589delimiter may be either a newline or the @var{eof-object}; if
590@code{%read-line} is called at the end of file, it returns the pair
591@code{(#<eof> . #<eof>)}.
592@end deffn
593
594@node Block Reading and Writing
595@subsection Block reading and writing
bf5df489
KR
596@cindex Block read/write
597@cindex Port, block read/write
07d83abe
MV
598
599The Block-string-I/O module can be accessed with:
600
aba0dff5 601@lisp
07d83abe 602(use-modules (ice-9 rw))
aba0dff5 603@end lisp
07d83abe
MV
604
605It currently contains procedures that help to implement the
606@code{(scsh rw)} module in guile-scsh.
607
608@deffn {Scheme Procedure} read-string!/partial str [port_or_fdes [start [end]]]
609@deffnx {C Function} scm_read_string_x_partial (str, port_or_fdes, start, end)
610Read characters from a port or file descriptor into a
611string @var{str}. A port must have an underlying file
612descriptor --- a so-called fport. This procedure is
613scsh-compatible and can efficiently read large strings.
614It will:
615
616@itemize
617@item
618attempt to fill the entire string, unless the @var{start}
619and/or @var{end} arguments are supplied. i.e., @var{start}
620defaults to 0 and @var{end} defaults to
621@code{(string-length str)}
622@item
623use the current input port if @var{port_or_fdes} is not
624supplied.
625@item
626return fewer than the requested number of characters in some
627cases, e.g., on end of file, if interrupted by a signal, or if
628not all the characters are immediately available.
629@item
630wait indefinitely for some input if no characters are
631currently available,
632unless the port is in non-blocking mode.
633@item
634read characters from the port's input buffers if available,
635instead from the underlying file descriptor.
636@item
637return @code{#f} if end-of-file is encountered before reading
638any characters, otherwise return the number of characters
639read.
640@item
641return 0 if the port is in non-blocking mode and no characters
642are immediately available.
643@item
644return 0 if the request is for 0 bytes, with no
645end-of-file check.
646@end itemize
647@end deffn
648
649@deffn {Scheme Procedure} write-string/partial str [port_or_fdes [start [end]]]
650@deffnx {C Function} scm_write_string_partial (str, port_or_fdes, start, end)
651Write characters from a string @var{str} to a port or file
652descriptor. A port must have an underlying file descriptor
653--- a so-called fport. This procedure is
654scsh-compatible and can efficiently write large strings.
655It will:
656
657@itemize
658@item
659attempt to write the entire string, unless the @var{start}
660and/or @var{end} arguments are supplied. i.e., @var{start}
661defaults to 0 and @var{end} defaults to
662@code{(string-length str)}
663@item
664use the current output port if @var{port_of_fdes} is not
665supplied.
666@item
667in the case of a buffered port, store the characters in the
668port's output buffer, if all will fit. If they will not fit
669then any existing buffered characters will be flushed
670before attempting
671to write the new characters directly to the underlying file
672descriptor. If the port is in non-blocking mode and
673buffered characters can not be flushed immediately, then an
674@code{EAGAIN} system-error exception will be raised (Note:
675scsh does not support the use of non-blocking buffered ports.)
676@item
677write fewer than the requested number of
678characters in some cases, e.g., if interrupted by a signal or
679if not all of the output can be accepted immediately.
680@item
681wait indefinitely for at least one character
682from @var{str} to be accepted by the port, unless the port is
683in non-blocking mode.
684@item
685return the number of characters accepted by the port.
686@item
687return 0 if the port is in non-blocking mode and can not accept
688at least one character from @var{str} immediately
689@item
690return 0 immediately if the request size is 0 bytes.
691@end itemize
692@end deffn
693
694@node Default Ports
695@subsection Default Ports for Input, Output and Errors
bf5df489
KR
696@cindex Default ports
697@cindex Port, default
07d83abe
MV
698
699@rnindex current-input-port
700@deffn {Scheme Procedure} current-input-port
701@deffnx {C Function} scm_current_input_port ()
34846414 702@cindex standard input
07d83abe 703Return the current input port. This is the default port used
3fa0a042
KR
704by many input procedures.
705
706Initially this is the @dfn{standard input} in Unix and C terminology.
707When the standard input is a tty the port is unbuffered, otherwise
708it's fully buffered.
709
710Unbuffered input is good if an application runs an interactive
711subprocess, since any type-ahead input won't go into Guile's buffer
9782da8a 712and be unavailable to the subprocess.
3fa0a042
KR
713
714Note that Guile buffering is completely separate from the tty ``line
9782da8a
KR
715discipline''. In the usual cooked mode on a tty Guile only sees a
716line of input once the user presses @key{Return}.
07d83abe
MV
717@end deffn
718
719@rnindex current-output-port
720@deffn {Scheme Procedure} current-output-port
721@deffnx {C Function} scm_current_output_port ()
34846414 722@cindex standard output
07d83abe 723Return the current output port. This is the default port used
3fa0a042
KR
724by many output procedures.
725
726Initially this is the @dfn{standard output} in Unix and C terminology.
727When the standard output is a tty this port is unbuffered, otherwise
728it's fully buffered.
729
730Unbuffered output to a tty is good for ensuring progress output or a
731prompt is seen. But an application which always prints whole lines
732could change to line buffered, or an application with a lot of output
733could go fully buffered and perhaps make explicit @code{force-output}
734calls (@pxref{Writing}) at selected points.
07d83abe
MV
735@end deffn
736
737@deffn {Scheme Procedure} current-error-port
738@deffnx {C Function} scm_current_error_port ()
34846414 739@cindex standard error output
3fa0a042
KR
740Return the port to which errors and warnings should be sent.
741
742Initially this is the @dfn{standard error} in Unix and C terminology.
743When the standard error is a tty this port is unbuffered, otherwise
744it's fully buffered.
07d83abe
MV
745@end deffn
746
747@deffn {Scheme Procedure} set-current-input-port port
748@deffnx {Scheme Procedure} set-current-output-port port
749@deffnx {Scheme Procedure} set-current-error-port port
750@deffnx {C Function} scm_set_current_input_port (port)
751@deffnx {C Function} scm_set_current_output_port (port)
752@deffnx {C Function} scm_set_current_error_port (port)
753Change the ports returned by @code{current-input-port},
754@code{current-output-port} and @code{current-error-port}, respectively,
755so that they use the supplied @var{port} for input or output.
756@end deffn
757
661ae7ab
MV
758@deftypefn {C Function} void scm_dynwind_current_input_port (SCM port)
759@deftypefnx {C Function} void scm_dynwind_current_output_port (SCM port)
760@deftypefnx {C Function} void scm_dynwind_current_error_port (SCM port)
07d83abe 761These functions must be used inside a pair of calls to
661ae7ab
MV
762@code{scm_dynwind_begin} and @code{scm_dynwind_end} (@pxref{Dynamic
763Wind}). During the dynwind context, the indicated port is set to
07d83abe
MV
764@var{port}.
765
766More precisely, the current port is swapped with a `backup' value
661ae7ab 767whenever the dynwind context is entered or left. The backup value is
07d83abe
MV
768initialized with the @var{port} argument.
769@end deftypefn
770
771@node Port Types
772@subsection Types of Port
bf5df489
KR
773@cindex Types of ports
774@cindex Port, types
07d83abe
MV
775
776[Types of port; how to make them.]
777
778@menu
779* File Ports:: Ports on an operating system file.
780* String Ports:: Ports on a Scheme string.
781* Soft Ports:: Ports on arbitrary Scheme procedures.
782* Void Ports:: Ports on nothing at all.
783@end menu
784
785
786@node File Ports
787@subsubsection File Ports
bf5df489
KR
788@cindex File port
789@cindex Port, file
07d83abe
MV
790
791The following procedures are used to open file ports.
792See also @ref{Ports and File Descriptors, open}, for an interface
793to the Unix @code{open} system call.
794
795Most systems have limits on how many files can be open, so it's
796strongly recommended that file ports be closed explicitly when no
797longer required (@pxref{Ports}).
798
799@deffn {Scheme Procedure} open-file filename mode
800@deffnx {C Function} scm_open_file (filename, mode)
801Open the file whose name is @var{filename}, and return a port
802representing that file. The attributes of the port are
803determined by the @var{mode} string. The way in which this is
804interpreted is similar to C stdio. The first character must be
805one of the following:
c755b861 806
07d83abe
MV
807@table @samp
808@item r
809Open an existing file for input.
810@item w
811Open a file for output, creating it if it doesn't already exist
812or removing its contents if it does.
813@item a
814Open a file for output, creating it if it doesn't already
815exist. All writes to the port will go to the end of the file.
816The "append mode" can be turned off while the port is in use
817@pxref{Ports and File Descriptors, fcntl}
818@end table
c755b861 819
07d83abe 820The following additional characters can be appended:
c755b861 821
07d83abe
MV
822@table @samp
823@item +
824Open the port for both input and output. E.g., @code{r+}: open
825an existing file for both input and output.
826@item 0
827Create an "unbuffered" port. In this case input and output
828operations are passed directly to the underlying port
829implementation without additional buffering. This is likely to
830slow down I/O operations. The buffering mode can be changed
831while a port is in use @pxref{Ports and File Descriptors,
832setvbuf}
833@item l
834Add line-buffering to the port. The port output buffer will be
835automatically flushed whenever a newline character is written.
c755b861
KR
836@item b
837Use binary mode. On DOS systems the default text mode converts CR+LF
838in the file to newline for the program, whereas binary mode reads and
839writes all bytes unchanged. On Unix-like systems there is no such
840distinction, text files already contain just newlines and no
841conversion is ever made. The @code{b} flag is accepted on all
842systems, but has no effect on Unix-like systems.
843
844(For reference, Guile leaves text versus binary up to the C library,
845@code{b} here just adds @code{O_BINARY} to the underlying @code{open}
846call, when that flag is available.)
211683cc
MG
847
848Also, open the file using the 8-bit character encoding "ISO-8859-1",
849ignoring any coding declaration or port encoding.
850
851Note that, when reading or writing binary data with ports, the
852bytevector ports in the @code{(rnrs io ports)} module are preferred,
853as they return vectors, and not strings (@pxref{R6RS I/O Ports}).
07d83abe 854@end table
c755b861 855
092bdcc4
KR
856If a file cannot be opened with the access
857requested, @code{open-file} throws an exception.
858
211683cc
MG
859When the file is opened, this procedure will scan for a coding
860declaration (@pxref{Character Encoding of Source Files}). If present
861will use that encoding for interpreting the file. Otherwise, the
862port's encoding will be used. To supress this behavior, open
863the file in binary mode and then set the port encoding explicitly
864using @code{set-port-encoding!}.
865
07d83abe
MV
866In theory we could create read/write ports which were buffered
867in one direction only. However this isn't included in the
092bdcc4 868current interfaces.
07d83abe
MV
869@end deffn
870
871@rnindex open-input-file
872@deffn {Scheme Procedure} open-input-file filename
873Open @var{filename} for input. Equivalent to
aba0dff5 874@lisp
07d83abe 875(open-file @var{filename} "r")
aba0dff5 876@end lisp
07d83abe
MV
877@end deffn
878
879@rnindex open-output-file
880@deffn {Scheme Procedure} open-output-file filename
881Open @var{filename} for output. Equivalent to
aba0dff5 882@lisp
07d83abe 883(open-file @var{filename} "w")
aba0dff5 884@end lisp
07d83abe
MV
885@end deffn
886
887@deffn {Scheme Procedure} call-with-input-file filename proc
888@deffnx {Scheme Procedure} call-with-output-file filename proc
889@rnindex call-with-input-file
890@rnindex call-with-output-file
891Open @var{filename} for input or output, and call @code{(@var{proc}
892port)} with the resulting port. Return the value returned by
893@var{proc}. @var{filename} is opened as per @code{open-input-file} or
28cc8dac 894@code{open-output-file} respectively, and an error is signaled if it
07d83abe
MV
895cannot be opened.
896
897When @var{proc} returns, the port is closed. If @var{proc} does not
28cc8dac 898return (e.g.@: if it throws an error), then the port might not be
07d83abe
MV
899closed automatically, though it will be garbage collected in the usual
900way if not otherwise referenced.
901@end deffn
902
903@deffn {Scheme Procedure} with-input-from-file filename thunk
904@deffnx {Scheme Procedure} with-output-to-file filename thunk
905@deffnx {Scheme Procedure} with-error-to-file filename thunk
906@rnindex with-input-from-file
907@rnindex with-output-to-file
908Open @var{filename} and call @code{(@var{thunk})} with the new port
909setup as respectively the @code{current-input-port},
910@code{current-output-port}, or @code{current-error-port}. Return the
911value returned by @var{thunk}. @var{filename} is opened as per
912@code{open-input-file} or @code{open-output-file} respectively, and an
28cc8dac 913error is signaled if it cannot be opened.
07d83abe
MV
914
915When @var{thunk} returns, the port is closed and the previous setting
916of the respective current port is restored.
917
918The current port setting is managed with @code{dynamic-wind}, so the
919previous value is restored no matter how @var{thunk} exits (eg.@: an
920exception), and if @var{thunk} is re-entered (via a captured
921continuation) then it's set again to the @var{FILENAME} port.
922
923The port is closed when @var{thunk} returns normally, but not when
924exited via an exception or new continuation. This ensures it's still
925ready for use if @var{thunk} is re-entered by a captured continuation.
926Of course the port is always garbage collected and closed in the usual
927way when no longer referenced anywhere.
928@end deffn
929
930@deffn {Scheme Procedure} port-mode port
931@deffnx {C Function} scm_port_mode (port)
932Return the port modes associated with the open port @var{port}.
933These will not necessarily be identical to the modes used when
934the port was opened, since modes such as "append" which are
935used only during port creation are not retained.
936@end deffn
937
938@deffn {Scheme Procedure} port-filename port
939@deffnx {C Function} scm_port_filename (port)
940Return the filename associated with @var{port}. This function returns
941the strings "standard input", "standard output" and "standard error"
942when called on the current input, output and error ports respectively.
e55abf41
KR
943
944@var{port} must be open, @code{port-filename} cannot be used once the
945port is closed.
07d83abe
MV
946@end deffn
947
948@deffn {Scheme Procedure} set-port-filename! port filename
949@deffnx {C Function} scm_set_port_filename_x (port, filename)
950Change the filename associated with @var{port}, using the current input
951port if none is specified. Note that this does not change the port's
952source of data, but only the value that is returned by
953@code{port-filename} and reported in diagnostic output.
954@end deffn
955
956@deffn {Scheme Procedure} file-port? obj
957@deffnx {C Function} scm_file_port_p (obj)
958Determine whether @var{obj} is a port that is related to a file.
959@end deffn
960
961
962@node String Ports
963@subsubsection String Ports
bf5df489
KR
964@cindex String port
965@cindex Port, string
07d83abe
MV
966
967The following allow string ports to be opened by analogy to R4R*
968file port facilities:
969
28cc8dac
MG
970With string ports, the port-encoding is treated differently than other
971types of ports. When string ports are created, they do not inherit a
972character encoding from the current locale. They are given a
973default locale that allows them to handle all valid string characters.
974Typically one should not modify a string port's character encoding
975away from its default.
976
07d83abe
MV
977@deffn {Scheme Procedure} call-with-output-string proc
978@deffnx {C Function} scm_call_with_output_string (proc)
979Calls the one-argument procedure @var{proc} with a newly created output
980port. When the function returns, the string composed of the characters
981written into the port is returned. @var{proc} should not close the port.
7b041912
LC
982
983Note that which characters can be written to a string port depend on the port's
984encoding. The default encoding of string ports is specified by the
985@code{%default-port-encoding} fluid (@pxref{Ports,
986@code{%default-port-encoding}}). For instance, it is an error to write Greek
987letter alpha to an ISO-8859-1-encoded string port since this character cannot be
988represented with ISO-8859-1:
989
990@example
991(define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
992
993(with-fluids ((%default-port-encoding "ISO-8859-1"))
994 (call-with-output-string
995 (lambda (p)
996 (display alpha p))))
997
998@result{}
999Throw to key `encoding-error'
1000@end example
1001
1002Changing the string port's encoding to a Unicode-capable encoding such as UTF-8
1003solves the problem.
07d83abe
MV
1004@end deffn
1005
1006@deffn {Scheme Procedure} call-with-input-string string proc
1007@deffnx {C Function} scm_call_with_input_string (string, proc)
1008Calls the one-argument procedure @var{proc} with a newly
1009created input port from which @var{string}'s contents may be
1010read. The value yielded by the @var{proc} is returned.
1011@end deffn
1012
1013@deffn {Scheme Procedure} with-output-to-string thunk
1014Calls the zero-argument procedure @var{thunk} with the current output
1015port set temporarily to a new string port. It returns a string
1016composed of the characters written to the current output.
7b041912
LC
1017
1018See @code{call-with-output-string} above for character encoding considerations.
07d83abe
MV
1019@end deffn
1020
1021@deffn {Scheme Procedure} with-input-from-string string thunk
1022Calls the zero-argument procedure @var{thunk} with the current input
1023port set temporarily to a string port opened on the specified
1024@var{string}. The value yielded by @var{thunk} is returned.
1025@end deffn
1026
1027@deffn {Scheme Procedure} open-input-string str
1028@deffnx {C Function} scm_open_input_string (str)
1029Take a string and return an input port that delivers characters
1030from the string. The port can be closed by
1031@code{close-input-port}, though its storage will be reclaimed
1032by the garbage collector if it becomes inaccessible.
1033@end deffn
1034
1035@deffn {Scheme Procedure} open-output-string
1036@deffnx {C Function} scm_open_output_string ()
1037Return an output port that will accumulate characters for
1038retrieval by @code{get-output-string}. The port can be closed
1039by the procedure @code{close-output-port}, though its storage
1040will be reclaimed by the garbage collector if it becomes
1041inaccessible.
1042@end deffn
1043
1044@deffn {Scheme Procedure} get-output-string port
1045@deffnx {C Function} scm_get_output_string (port)
1046Given an output port created by @code{open-output-string},
1047return a string consisting of the characters that have been
1048output to the port so far.
1049
1050@code{get-output-string} must be used before closing @var{port}, once
1051closed the string cannot be obtained.
1052@end deffn
1053
1054A string port can be used in many procedures which accept a port
1055but which are not dependent on implementation details of fports.
1056E.g., seeking and truncating will work on a string port,
1057but trying to extract the file descriptor number will fail.
1058
1059
1060@node Soft Ports
1061@subsubsection Soft Ports
bf5df489
KR
1062@cindex Soft port
1063@cindex Port, soft
07d83abe
MV
1064
1065A @dfn{soft-port} is a port based on a vector of procedures capable of
1066accepting or delivering characters. It allows emulation of I/O ports.
1067
1068@deffn {Scheme Procedure} make-soft-port pv modes
1069@deffnx {C Function} scm_make_soft_port (pv, modes)
1070Return a port capable of receiving or delivering characters as
1071specified by the @var{modes} string (@pxref{File Ports,
1072open-file}). @var{pv} must be a vector of length 5 or 6. Its
1073components are as follows:
1074
1075@enumerate 0
1076@item
1077procedure accepting one character for output
1078@item
1079procedure accepting a string for output
1080@item
1081thunk for flushing output
1082@item
1083thunk for getting one character
1084@item
1085thunk for closing port (not by garbage collection)
1086@item
1087(if present and not @code{#f}) thunk for computing the number of
1088characters that can be read from the port without blocking.
1089@end enumerate
1090
1091For an output-only port only elements 0, 1, 2, and 4 need be
1092procedures. For an input-only port only elements 3 and 4 need
1093be procedures. Thunks 2 and 4 can instead be @code{#f} if
1094there is no useful operation for them to perform.
1095
1096If thunk 3 returns @code{#f} or an @code{eof-object}
1097(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on
1098Scheme}) it indicates that the port has reached end-of-file.
1099For example:
1100
1101@lisp
1102(define stdout (current-output-port))
1103(define p (make-soft-port
1104 (vector
1105 (lambda (c) (write c stdout))
1106 (lambda (s) (display s stdout))
1107 (lambda () (display "." stdout))
1108 (lambda () (char-upcase (read-char)))
1109 (lambda () (display "@@" stdout)))
1110 "rw"))
1111
1112(write p p) @result{} #<input-output: soft 8081e20>
1113@end lisp
1114@end deffn
1115
1116
1117@node Void Ports
1118@subsubsection Void Ports
bf5df489
KR
1119@cindex Void port
1120@cindex Port, void
07d83abe
MV
1121
1122This kind of port causes any data to be discarded when written to, and
1123always returns the end-of-file object when read from.
1124
1125@deffn {Scheme Procedure} %make-void-port mode
1126@deffnx {C Function} scm_sys_make_void_port (mode)
1127Create and return a new void port. A void port acts like
1128@file{/dev/null}. The @var{mode} argument
1129specifies the input/output modes for this port: see the
1130documentation for @code{open-file} in @ref{File Ports}.
1131@end deffn
1132
1133
b242715b
LC
1134@node R6RS I/O Ports
1135@subsection R6RS I/O Ports
1136
1137@cindex R6RS
1138@cindex R6RS ports
1139
1140The I/O port API of the @uref{http://www.r6rs.org/, Revised Report^6 on
1141the Algorithmic Language Scheme (R6RS)} is provided by the @code{(rnrs
1142io ports)} module. It provides features, such as binary I/O and Unicode
1143string I/O, that complement or refine Guile's historical port API
1144presented above (@pxref{Input and Output}).
1145
1146@c FIXME: Update description when implemented.
1147@emph{Note}: The implementation of this R6RS API is currently far from
1148complete, notably due to the lack of support for Unicode I/O and strings.
1149
1150@menu
1151* R6RS End-of-File:: The end-of-file object.
1152* R6RS Port Manipulation:: Manipulating R6RS ports.
1153* R6RS Binary Input:: Binary input.
1154* R6RS Binary Output:: Binary output.
1155@end menu
1156
1157@node R6RS End-of-File
1158@subsubsection The End-of-File Object
1159
1160@cindex EOF
1161@cindex end-of-file
1162
1163R5RS' @code{eof-object?} procedure is provided by the @code{(rnrs io
1164ports)} module:
1165
1166@deffn {Scheme Procedure} eof-object? obj
1167@deffnx {C Function} scm_eof_object_p (obj)
1168Return true if @var{obj} is the end-of-file (EOF) object.
1169@end deffn
1170
1171In addition, the following procedure is provided:
1172
1173@deffn {Scheme Procedure} eof-object
1174@deffnx {C Function} scm_eof_object ()
1175Return the end-of-file (EOF) object.
1176
1177@lisp
1178(eof-object? (eof-object))
1179@result{} #t
1180@end lisp
1181@end deffn
1182
1183
1184@node R6RS Port Manipulation
1185@subsubsection Port Manipulation
1186
1187The procedures listed below operate on any kind of R6RS I/O port.
1188
1189@deffn {Scheme Procedure} port-position port
1190If @var{port} supports it (see below), return the offset (an integer)
1191indicating where the next octet will be read from/written to in
1192@var{port}. If @var{port} does not support this operation, an error
1193condition is raised.
1194
1195This is similar to Guile's @code{seek} procedure with the
1196@code{SEEK_CUR} argument (@pxref{Random Access}).
1197@end deffn
1198
1199@deffn {Scheme Procedure} port-has-port-position? port
1200Return @code{#t} is @var{port} supports @code{port-position}.
1201@end deffn
1202
1203@deffn {Scheme Procedure} set-port-position! port offset
1204If @var{port} supports it (see below), set the position where the next
1205octet will be read from/written to @var{port} to @var{offset} (an
1206integer). If @var{port} does not support this operation, an error
1207condition is raised.
1208
1209This is similar to Guile's @code{seek} procedure with the
1210@code{SEEK_SET} argument (@pxref{Random Access}).
1211@end deffn
1212
1213@deffn {Scheme Procedure} port-has-set-port-position!? port
1214Return @code{#t} is @var{port} supports @code{set-port-position!}.
1215@end deffn
1216
1217@deffn {Scheme Procedure} call-with-port port proc
1218Call @var{proc}, passing it @var{port} and closing @var{port} upon exit
1219of @var{proc}. Return the return values of @var{proc}.
1220@end deffn
1221
1222
1223@node R6RS Binary Input
1224@subsubsection Binary Input
1225
1226@cindex binary input
1227
1228R6RS binary input ports can be created with the procedures described
1229below.
1230
1231@deffn {Scheme Procedure} open-bytevector-input-port bv [transcoder]
1232@deffnx {C Function} scm_open_bytevector_input_port (bv, transcoder)
1233Return an input port whose contents are drawn from bytevector @var{bv}
1234(@pxref{Bytevectors}).
1235
1236@c FIXME: Update description when implemented.
1237The @var{transcoder} argument is currently not supported.
1238@end deffn
1239
1240@cindex custom binary input ports
1241
1242@deffn {Scheme Procedure} make-custom-binary-input-port id read! get-position set-position! close
1243@deffnx {C Function} scm_make_custom_binary_input_port (id, read!, get-position, set-position!, close)
1244Return a new custom binary input port@footnote{This is similar in spirit
1245to Guile's @dfn{soft ports} (@pxref{Soft Ports}).} named @var{id} (a
1246string) whose input is drained by invoking @var{read!} and passing it a
1247bytevector, an index where bytes should be written, and the number of
1248bytes to read. The @code{read!} procedure must return an integer
1249indicating the number of bytes read, or @code{0} to indicate the
1250end-of-file.
1251
1252Optionally, if @var{get-position} is not @code{#f}, it must be a thunk
1253that will be called when @var{port-position} is invoked on the custom
1254binary port and should return an integer indicating the position within
1255the underlying data stream; if @var{get-position} was not supplied, the
1256returned port does not support @var{port-position}.
1257
1258Likewise, if @var{set-position!} is not @code{#f}, it should be a
1259one-argument procedure. When @var{set-port-position!} is invoked on the
1260custom binary input port, @var{set-position!} is passed an integer
1261indicating the position of the next byte is to read.
1262
1263Finally, if @var{close} is not @code{#f}, it must be a thunk. It is
1264invoked when the custom binary input port is closed.
1265
1266Using a custom binary input port, the @code{open-bytevector-input-port}
1267procedure could be implemented as follows:
1268
1269@lisp
1270(define (open-bytevector-input-port source)
1271 (define position 0)
1272 (define length (bytevector-length source))
1273
1274 (define (read! bv start count)
1275 (let ((count (min count (- length position))))
1276 (bytevector-copy! source position
1277 bv start count)
1278 (set! position (+ position count))
1279 count))
1280
1281 (define (get-position) position)
1282
1283 (define (set-position! new-position)
1284 (set! position new-position))
1285
1286 (make-custom-binary-input-port "the port" read!
1287 get-position
1288 set-position!))
1289
1290(read (open-bytevector-input-port (string->utf8 "hello")))
1291@result{} hello
1292@end lisp
1293@end deffn
1294
1295@cindex binary input
1296Binary input is achieved using the procedures below:
1297
1298@deffn {Scheme Procedure} get-u8 port
1299@deffnx {C Function} scm_get_u8 (port)
1300Return an octet read from @var{port}, a binary input port, blocking as
1301necessary, or the end-of-file object.
1302@end deffn
1303
1304@deffn {Scheme Procedure} lookahead-u8 port
1305@deffnx {C Function} scm_lookahead_u8 (port)
1306Like @code{get-u8} but does not update @var{port}'s position to point
1307past the octet.
1308@end deffn
1309
1310@deffn {Scheme Procedure} get-bytevector-n port count
1311@deffnx {C Function} scm_get_bytevector_n (port, count)
1312Read @var{count} octets from @var{port}, blocking as necessary and
1313return a bytevector containing the octets read. If fewer bytes are
1314available, a bytevector smaller than @var{count} is returned.
1315@end deffn
1316
1317@deffn {Scheme Procedure} get-bytevector-n! port bv start count
1318@deffnx {C Function} scm_get_bytevector_n_x (port, bv, start, count)
1319Read @var{count} bytes from @var{port} and store them in @var{bv}
1320starting at index @var{start}. Return either the number of bytes
1321actually read or the end-of-file object.
1322@end deffn
1323
1324@deffn {Scheme Procedure} get-bytevector-some port
1325@deffnx {C Function} scm_get_bytevector_some (port)
1326Read from @var{port}, blocking as necessary, until data are available or
1327and end-of-file is reached. Return either a new bytevector containing
1328the data read or the end-of-file object.
1329@end deffn
1330
1331@deffn {Scheme Procedure} get-bytevector-all port
1332@deffnx {C Function} scm_get_bytevector_all (port)
1333Read from @var{port}, blocking as necessary, until the end-of-file is
1334reached. Return either a new bytevector containing the data read or the
1335end-of-file object (if no data were available).
1336@end deffn
1337
1338@node R6RS Binary Output
1339@subsubsection Binary Output
1340
1341Binary output ports can be created with the procedures below.
1342
1343@deffn {Scheme Procedure} open-bytevector-output-port [transcoder]
1344@deffnx {C Function} scm_open_bytevector_output_port (transcoder)
1345Return two values: a binary output port and a procedure. The latter
1346should be called with zero arguments to obtain a bytevector containing
1347the data accumulated by the port, as illustrated below.
1348
1349@lisp
1350(call-with-values
1351 (lambda ()
1352 (open-bytevector-output-port))
1353 (lambda (port get-bytevector)
1354 (display "hello" port)
1355 (get-bytevector)))
1356
1357@result{} #vu8(104 101 108 108 111)
1358@end lisp
1359
1360@c FIXME: Update description when implemented.
1361The @var{transcoder} argument is currently not supported.
1362@end deffn
1363
1364@cindex custom binary output ports
1365
1366@deffn {Scheme Procedure} make-custom-binary-output-port id write! get-position set-position! close
1367@deffnx {C Function} scm_make_custom_binary_output_port (id, write!, get-position, set-position!, close)
1368Return a new custom binary output port named @var{id} (a string) whose
1369output is sunk by invoking @var{write!} and passing it a bytevector, an
1370index where bytes should be read from this bytevector, and the number of
1371bytes to be ``written''. The @code{write!} procedure must return an
1372integer indicating the number of bytes actually written; when it is
1373passed @code{0} as the number of bytes to write, it should behave as
1374though an end-of-file was sent to the byte sink.
1375
1376The other arguments are as for @code{make-custom-binary-input-port}
1377(@pxref{R6RS Binary Input, @code{make-custom-binary-input-port}}).
1378@end deffn
1379
1380@cindex binary output
1381Writing to a binary output port can be done using the following
1382procedures:
1383
1384@deffn {Scheme Procedure} put-u8 port octet
1385@deffnx {C Function} scm_put_u8 (port, octet)
1386Write @var{octet}, an integer in the 0--255 range, to @var{port}, a
1387binary output port.
1388@end deffn
1389
1390@deffn {Scheme Procedure} put-bytevector port bv [start [count]]
1391@deffnx {C Function} scm_put_bytevector (port, bv, start, count)
1392Write the contents of @var{bv} to @var{port}, optionally starting at
1393index @var{start} and limiting to @var{count} octets.
1394@end deffn
1395
1396
07d83abe
MV
1397@node I/O Extensions
1398@subsection Using and Extending Ports in C
1399
1400@menu
1401* C Port Interface:: Using ports from C.
1402* Port Implementation:: How to implement a new port type in C.
1403@end menu
1404
1405
1406@node C Port Interface
1407@subsubsection C Port Interface
bf5df489
KR
1408@cindex C port interface
1409@cindex Port, C interface
07d83abe
MV
1410
1411This section describes how to use Scheme ports from C.
1412
1413@subsubheading Port basics
1414
3081aee1
KR
1415@cindex ptob
1416@tindex scm_ptob_descriptor
1417@tindex scm_port
1418@findex SCM_PTAB_ENTRY
1419@findex SCM_PTOBNUM
1420@vindex scm_ptobs
07d83abe
MV
1421There are two main data structures. A port type object (ptob) is of
1422type @code{scm_ptob_descriptor}. A port instance is of type
1423@code{scm_port}. Given an @code{SCM} variable which points to a port,
1424the corresponding C port object can be obtained using the
1425@code{SCM_PTAB_ENTRY} macro. The ptob can be obtained by using
1426@code{SCM_PTOBNUM} to give an index into the @code{scm_ptobs}
1427global array.
1428
1429@subsubheading Port buffers
1430
1431An input port always has a read buffer and an output port always has a
1432write buffer. However the size of these buffers is not guaranteed to be
1433more than one byte (e.g., the @code{shortbuf} field in @code{scm_port}
1434which is used when no other buffer is allocated). The way in which the
1435buffers are allocated depends on the implementation of the ptob. For
1436example in the case of an fport, buffers may be allocated with malloc
1437when the port is created, but in the case of an strport the underlying
1438string is used as the buffer.
1439
1440@subsubheading The @code{rw_random} flag
1441
1442Special treatment is required for ports which can be seeked at random.
1443Before various operations, such as seeking the port or changing from
1444input to output on a bidirectional port or vice versa, the port
1445implementation must be given a chance to update its state. The write
1446buffer is updated by calling the @code{flush} ptob procedure and the
1447input buffer is updated by calling the @code{end_input} ptob procedure.
1448In the case of an fport, @code{flush} causes buffered output to be
1449written to the file descriptor, while @code{end_input} causes the
1450descriptor position to be adjusted to account for buffered input which
1451was never read.
1452
1453The special treatment must be performed if the @code{rw_random} flag in
1454the port is non-zero.
1455
1456@subsubheading The @code{rw_active} variable
1457
1458The @code{rw_active} variable in the port is only used if
1459@code{rw_random} is set. It's defined as an enum with the following
1460values:
1461
1462@table @code
1463@item SCM_PORT_READ
1464the read buffer may have unread data.
1465
1466@item SCM_PORT_WRITE
1467the write buffer may have unwritten data.
1468
1469@item SCM_PORT_NEITHER
1470neither the write nor the read buffer has data.
1471@end table
1472
1473@subsubheading Reading from a port.
1474
1475To read from a port, it's possible to either call existing libguile
1476procedures such as @code{scm_getc} and @code{scm_read_line} or to read
1477data from the read buffer directly. Reading from the buffer involves
1478the following steps:
1479
1480@enumerate
1481@item
1482Flush output on the port, if @code{rw_active} is @code{SCM_PORT_WRITE}.
1483
1484@item
1485Fill the read buffer, if it's empty, using @code{scm_fill_input}.
1486
1487@item Read the data from the buffer and update the read position in
1488the buffer. Steps 2) and 3) may be repeated as many times as required.
1489
1490@item Set rw_active to @code{SCM_PORT_READ} if @code{rw_random} is set.
1491
1492@item update the port's line and column counts.
1493@end enumerate
1494
1495@subsubheading Writing to a port.
1496
1497To write data to a port, calling @code{scm_lfwrite} should be sufficient for
1498most purposes. This takes care of the following steps:
1499
1500@enumerate
1501@item
1502End input on the port, if @code{rw_active} is @code{SCM_PORT_READ}.
1503
1504@item
1505Pass the data to the ptob implementation using the @code{write} ptob
1506procedure. The advantage of using the ptob @code{write} instead of
1507manipulating the write buffer directly is that it allows the data to be
1508written in one operation even if the port is using the single-byte
1509@code{shortbuf}.
1510
1511@item
1512Set @code{rw_active} to @code{SCM_PORT_WRITE} if @code{rw_random}
1513is set.
1514@end enumerate
1515
1516
1517@node Port Implementation
1518@subsubsection Port Implementation
28cc8dac 1519@cindex Port implementation
07d83abe
MV
1520
1521This section describes how to implement a new port type in C.
1522
1523As described in the previous section, a port type object (ptob) is
1524a structure of type @code{scm_ptob_descriptor}. A ptob is created by
1525calling @code{scm_make_port_type}.
1526
23f2b9a3
KR
1527@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))
1528Return a new port type object. The @var{name}, @var{fill_input} and
1529@var{write} parameters are initial values for those port type fields,
1530as described below. The other fields are initialized with default
1531values and can be changed later.
1532@end deftypefun
1533
07d83abe
MV
1534All of the elements of the ptob, apart from @code{name}, are procedures
1535which collectively implement the port behaviour. Creating a new port
1536type mostly involves writing these procedures.
1537
07d83abe
MV
1538@table @code
1539@item name
1540A pointer to a NUL terminated string: the name of the port type. This
1541is the only element of @code{scm_ptob_descriptor} which is not
1542a procedure. Set via the first argument to @code{scm_make_port_type}.
1543
1544@item mark
1545Called during garbage collection to mark any SCM objects that a port
1546object may contain. It doesn't need to be set unless the port has
23f2b9a3
KR
1547@code{SCM} components. Set using
1548
1549@deftypefun void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM port))
1550@end deftypefun
07d83abe
MV
1551
1552@item free
1553Called when the port is collected during gc. It
1554should free any resources used by the port.
23f2b9a3
KR
1555Set using
1556
1557@deftypefun void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM port))
1558@end deftypefun
07d83abe
MV
1559
1560@item print
1561Called when @code{write} is called on the port object, to print a
23f2b9a3
KR
1562port description. E.g., for an fport it may produce something like:
1563@code{#<input: /etc/passwd 3>}. Set using
1564
1565@deftypefun void scm_set_port_print (scm_t_bits tc, int (*print) (SCM port, SCM dest_port, scm_print_state *pstate))
1566The first argument @var{port} is the object being printed, the second
1567argument @var{dest_port} is where its description should go.
1568@end deftypefun
07d83abe
MV
1569
1570@item equalp
23f2b9a3
KR
1571Not used at present. Set using
1572
1573@deftypefun void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
1574@end deftypefun
07d83abe
MV
1575
1576@item close
1577Called when the port is closed, unless it was collected during gc. It
1578should free any resources used by the port.
23f2b9a3
KR
1579Set using
1580
1581@deftypefun void scm_set_port_close (scm_t_bits tc, int (*close) (SCM port))
1582@end deftypefun
07d83abe
MV
1583
1584@item write
1585Accept data which is to be written using the port. The port implementation
1586may choose to buffer the data instead of processing it directly.
1587Set via the third argument to @code{scm_make_port_type}.
1588
1589@item flush
1590Complete the processing of buffered output data. Reset the value of
1591@code{rw_active} to @code{SCM_PORT_NEITHER}.
23f2b9a3
KR
1592Set using
1593
1594@deftypefun void scm_set_port_flush (scm_t_bits tc, void (*flush) (SCM port))
1595@end deftypefun
07d83abe
MV
1596
1597@item end_input
1598Perform any synchronization required when switching from input to output
1599on the port. Reset the value of @code{rw_active} to @code{SCM_PORT_NEITHER}.
23f2b9a3
KR
1600Set using
1601
1602@deftypefun void scm_set_port_end_input (scm_t_bits tc, void (*end_input) (SCM port, int offset))
1603@end deftypefun
07d83abe
MV
1604
1605@item fill_input
1606Read new data into the read buffer and return the first character. It
1607can be assumed that the read buffer is empty when this procedure is called.
1608Set via the second argument to @code{scm_make_port_type}.
1609
1610@item input_waiting
1611Return a lower bound on the number of bytes that could be read from the
1612port without blocking. It can be assumed that the current state of
1613@code{rw_active} is @code{SCM_PORT_NEITHER}.
23f2b9a3
KR
1614Set using
1615
1616@deftypefun void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM port))
1617@end deftypefun
07d83abe
MV
1618
1619@item seek
1620Set the current position of the port. The procedure can not make
1621any assumptions about the value of @code{rw_active} when it's
1622called. It can reset the buffers first if desired by using something
1623like:
1624
1625@example
23f2b9a3
KR
1626if (pt->rw_active == SCM_PORT_READ)
1627 scm_end_input (port);
1628else if (pt->rw_active == SCM_PORT_WRITE)
1629 ptob->flush (port);
07d83abe
MV
1630@end example
1631
1632However note that this will have the side effect of discarding any data
1633in the unread-char buffer, in addition to any side effects from the
1634@code{end_input} and @code{flush} ptob procedures. This is undesirable
1635when seek is called to measure the current position of the port, i.e.,
1636@code{(seek p 0 SEEK_CUR)}. The libguile fport and string port
1637implementations take care to avoid this problem.
1638
23f2b9a3
KR
1639The procedure is set using
1640
f1ce9199 1641@deftypefun void scm_set_port_seek (scm_t_bits tc, scm_t_off (*seek) (SCM port, scm_t_off offset, int whence))
23f2b9a3 1642@end deftypefun
07d83abe
MV
1643
1644@item truncate
1645Truncate the port data to be specified length. It can be assumed that the
1646current state of @code{rw_active} is @code{SCM_PORT_NEITHER}.
23f2b9a3
KR
1647Set using
1648
f1ce9199 1649@deftypefun void scm_set_port_truncate (scm_t_bits tc, void (*truncate) (SCM port, scm_t_off length))
23f2b9a3 1650@end deftypefun
07d83abe
MV
1651
1652@end table
1653
1654
1655@c Local Variables:
1656@c TeX-master: "guile.texi"
1657@c End: