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