(Writing): Add write, revise display.
[bpt/guile.git] / doc / ref / scheme-io.texi
CommitLineData
a0e07ba4
NJ
1@page
2@node Input and Output
3@chapter Input and Output
4
5@menu
6* Ports:: The idea of the port abstraction.
7* Reading:: Procedures for reading from a port.
8* Writing:: Procedures for writing to a port.
9* Closing:: Procedures to close a port.
10* Random Access:: Moving around a random access port.
11* Line/Delimited:: Read and write lines or delimited text.
12* Block Reading and Writing:: Reading and writing blocks of text.
13* Default Ports:: Defaults for input, output and errors.
14* Port Types:: Types of port and how to make them.
9401323e 15* I/O Extensions:: Using and extending ports in C.
a0e07ba4
NJ
16@end menu
17
18
19@node Ports
20@section Ports
21
22[Concept of the port abstraction.]
23
24Sequential input/output in Scheme is represented by operations on a
25@dfn{port}. Characters can be read from an input port and
26written to an output port. This chapter explains the operations
27that Guile provides for working with ports.
28
29The formal definition of a port is very generic: an input port is
30simply ``an object which can deliver characters on command,'' and
31an output port is ``an object which can accept characters.''
32Because this definition is so loose, it is easy to write functions
33that simulate ports in software. @dfn{Soft ports} and @dfn{string
34ports} are two interesting and powerful examples of this technique.
35
36@rnindex input-port?
8f85c0c6
NJ
37@deffn {Scheme Procedure} input-port? x
38@deffnx {C Function} scm_input_port_p (x)
a0e07ba4
NJ
39Return @code{#t} if @var{x} is an input port, otherwise return
40@code{#f}. Any object satisfying this predicate also satisfies
41@code{port?}.
42@end deffn
43
44@rnindex output-port?
8f85c0c6
NJ
45@deffn {Scheme Procedure} output-port? x
46@deffnx {C Function} scm_output_port_p (x)
a0e07ba4
NJ
47Return @code{#t} if @var{x} is an output port, otherwise return
48@code{#f}. Any object satisfying this predicate also satisfies
49@code{port?}.
50@end deffn
51
8f85c0c6
NJ
52@deffn {Scheme Procedure} port? x
53@deffnx {C Function} scm_port_p (x)
a0e07ba4
NJ
54Return a boolean indicating whether @var{x} is a port.
55Equivalent to @code{(or (input-port? @var{x}) (output-port?
56@var{x}))}.
57@end deffn
58
59
60@node Reading
61@section Reading
62
63[Generic procedures for reading from ports.]
64
65@rnindex eof-object?
8f85c0c6
NJ
66@deffn {Scheme Procedure} eof-object? x
67@deffnx {C Function} scm_eof_object_p (x)
a0e07ba4
NJ
68Return @code{#t} if @var{x} is an end-of-file object; otherwise
69return @code{#f}.
70@end deffn
71
72@rnindex char-ready?
8f85c0c6
NJ
73@deffn {Scheme Procedure} char-ready? [port]
74@deffnx {C Function} scm_char_ready_p (port)
a0e07ba4
NJ
75Return @code{#t} if a character is ready on input @var{port}
76and return @code{#f} otherwise. If @code{char-ready?} returns
77@code{#t} then the next @code{read-char} operation on
78@var{port} is guaranteed not to hang. If @var{port} is a file
79port at end of file then @code{char-ready?} returns @code{#t}.
80@footnote{@code{char-ready?} exists to make it possible for a
81program to accept characters from interactive ports without
82getting stuck waiting for input. Any input editors associated
83with such ports must make sure that characters whose existence
84has been asserted by @code{char-ready?} cannot be rubbed out.
85If @code{char-ready?} were to return @code{#f} at end of file,
86a port at end of file would be indistinguishable from an
87interactive port that has no ready characters.}
88@end deffn
89
5a90a263 90@rnindex read-char
8f85c0c6
NJ
91@deffn {Scheme Procedure} read-char [port]
92@deffnx {C Function} scm_read_char (port)
a0e07ba4
NJ
93Return the next character available from @var{port}, updating
94@var{port} to point to the following character. If no more
95characters are available, the end-of-file object is returned.
96@end deffn
97
5a90a263 98@rnindex peek-char
8f85c0c6
NJ
99@deffn {Scheme Procedure} peek-char [port]
100@deffnx {C Function} scm_peek_char (port)
a0e07ba4
NJ
101Return the next character available from @var{port},
102@emph{without} updating @var{port} to point to the following
103character. If no more characters are available, the
104end-of-file object is returned.@footnote{The value returned by
105a call to @code{peek-char} is the same as the value that would
106have been returned by a call to @code{read-char} on the same
107port. The only difference is that the very next call to
108@code{read-char} or @code{peek-char} on that @var{port} will
109return the value returned by the preceding call to
110@code{peek-char}. In particular, a call to @code{peek-char} on
111an interactive port will hang waiting for input whenever a call
112to @code{read-char} would have hung.}
113@end deffn
114
8f85c0c6
NJ
115@deffn {Scheme Procedure} unread-char cobj [port]
116@deffnx {C Function} scm_unread_char (cobj, port)
a0e07ba4
NJ
117Place @var{char} in @var{port} so that it will be read by the
118next read operation. If called multiple times, the unread characters
119will be read again in last-in first-out order. If @var{port} is
120not supplied, the current input port is used.
121@end deffn
122
8f85c0c6
NJ
123@deffn {Scheme Procedure} unread-string str port
124@deffnx {C Function} scm_unread_string (str, port)
fef68e7f
RB
125Place the string @var{str} in @var{port} so that its characters will
126be read from left-to-right as the next characters from @var{port}
127during subsequent read operations. If called multiple times, the
a0e07ba4
NJ
128unread characters will be read again in last-in first-out order. If
129@var{port} is not supplied, the current-input-port is used.
130@end deffn
131
8f85c0c6
NJ
132@deffn {Scheme Procedure} drain-input port
133@deffnx {C Function} scm_drain_input (port)
9401323e
NJ
134This procedure clears a port's input buffers, similar
135to the way that force-output clears the output buffer. The
136contents of the buffers are returned as a single string, e.g.,
137
138@lisp
139(define p (open-input-file ...))
140(drain-input p) => empty string, nothing buffered yet.
141(unread-char (read-char p) p)
142(drain-input p) => initial chars from p, up to the buffer size.
143@end lisp
144
145Draining the buffers may be useful for cleanly finishing
146buffered I/O so that the file descriptor can be used directly
147for further input.
a0e07ba4
NJ
148@end deffn
149
8f85c0c6
NJ
150@deffn {Scheme Procedure} port-column port
151@deffnx {Scheme Procedure} port-line port
152@deffnx {C Function} scm_port_column (port)
153@deffnx {C Function} scm_port_line (port)
a0e07ba4
NJ
154Return the current column number or line number of @var{port},
155using the current input port if none is specified. If the number is
156unknown, the result is #f. Otherwise, the result is a 0-origin integer
5a90a263 157- i.e.@: the first character of the first line is line 0, column 0.
a0e07ba4
NJ
158(However, when you display a file position, for example in an error
159message, we recommend you add 1 to get 1-origin integers. This is
160because lines and column numbers traditionally start with 1, and that is
161what non-programmers will find most natural.)
162@end deffn
163
8f85c0c6
NJ
164@deffn {Scheme Procedure} set-port-column! port column
165@deffnx {Scheme Procedure} set-port-line! port line
166@deffnx {C Function} scm_set_port_column_x (port, column)
167@deffnx {C Function} scm_set_port_line_x (port, line)
a0e07ba4
NJ
168Set the current column or line number of @var{port}, using the
169current input port if none is specified.
170@end deffn
171
172@node Writing
173@section Writing
174
175[Generic procedures for writing to ports.]
176
8f85c0c6
NJ
177@deffn {Scheme Procedure} get-print-state port
178@deffnx {C Function} scm_get_print_state (port)
5a90a263 179Return the print state of the port @var{port}. If @var{port}
a0e07ba4
NJ
180has no associated print state, @code{#f} is returned.
181@end deffn
182
bb6847e8
KR
183@rnindex write
184@deffn {Scheme Procedure} write obj [port]
185Send a representation of @var{obj} to @var{port} or to the current
186output port if not given.
187
188The output is designed to be machine readable, and can be read back
189with @code{read} (@pxref{Reading}). Strings are printed in
190doublequotes, with escapes if necessary, and characters are printed in
191@samp{#\} notation.
192@end deffn
193
9eb96786 194@rnindex display
8f85c0c6 195@deffn {Scheme Procedure} display obj [port]
bb6847e8
KR
196Send a representation of @var{obj} to @var{port} or to the current
197output port if not given.
198
199The output is designed for human readability, it differs from
200@code{write} in that strings are printed without doublequotes and
201escapes, and characters are printed as per @code{write-char}, not in
202@samp{#\} form.
9eb96786
TTN
203@end deffn
204
a0e07ba4 205@rnindex newline
8f85c0c6
NJ
206@deffn {Scheme Procedure} newline [port]
207@deffnx {C Function} scm_newline (port)
208Send a newline to @var{port}.
209If @var{port} is omitted, send to the current output port.
a0e07ba4
NJ
210@end deffn
211
8f85c0c6
NJ
212@deffn {Scheme Procedure} port-with-print-state port pstate
213@deffnx {C Function} scm_port_with_print_state (port, pstate)
a0e07ba4
NJ
214Create a new port which behaves like @var{port}, but with an
215included print state @var{pstate}.
216@end deffn
217
8f85c0c6
NJ
218@deffn {Scheme Procedure} print-options-interface [setting]
219@deffnx {C Function} scm_print_options (setting)
a0e07ba4
NJ
220Option interface for the print options. Instead of using
221this procedure directly, use the procedures
222@code{print-enable}, @code{print-disable}, @code{print-set!}
223and @code{print-options}.
224@end deffn
225
8f85c0c6
NJ
226@deffn {Scheme Procedure} simple-format destination message . args
227@deffnx {C Function} scm_simple_format (destination, message, args)
a0e07ba4
NJ
228Write @var{message} to @var{destination}, defaulting to
229the current output port.
230@var{message} can contain @code{~A} (was @code{%s}) and
231@code{~S} (was @code{%S}) escapes. When printed,
232the escapes are replaced with corresponding members of
233@var{ARGS}:
234@code{~A} formats using @code{display} and @code{~S} formats
235using @code{write}.
236If @var{destination} is @code{#t}, then use the current output
237port, if @var{destination} is @code{#f}, then return a string
238containing the formatted text. Does not add a trailing newline.
239@end deffn
240
241@rnindex write-char
8f85c0c6
NJ
242@deffn {Scheme Procedure} write-char chr [port]
243@deffnx {C Function} scm_write_char (chr, port)
a0e07ba4
NJ
244Send character @var{chr} to @var{port}.
245@end deffn
246
247@findex fflush
8f85c0c6
NJ
248@deffn {Scheme Procedure} force-output [port]
249@deffnx {C Function} scm_force_output (port)
a0e07ba4
NJ
250Flush the specified output port, or the current output port if @var{port}
251is omitted. The current output buffer contents are passed to the
252underlying port implementation (e.g., in the case of fports, the
253data will be written to the file and the output buffer will be cleared.)
254It has no effect on an unbuffered port.
255
256The return value is unspecified.
257@end deffn
258
8f85c0c6
NJ
259@deffn {Scheme Procedure} flush-all-ports
260@deffnx {C Function} scm_flush_all_ports ()
a0e07ba4
NJ
261Equivalent to calling @code{force-output} on
262all open output ports. The return value is unspecified.
263@end deffn
264
265
266@node Closing
267@section Closing
268
8f85c0c6
NJ
269@deffn {Scheme Procedure} close-port port
270@deffnx {C Function} scm_close_port (port)
a0e07ba4
NJ
271Close the specified port object. Return @code{#t} if it
272successfully closes a port or @code{#f} if it was already
273closed. An exception may be raised if an error occurs, for
274example when flushing buffered output. See also @ref{Ports and
275File Descriptors, close}, for a procedure which can close file
276descriptors.
277@end deffn
278
279@rnindex close-input-port
8f85c0c6
NJ
280@deffn {Scheme Procedure} close-input-port port
281@deffnx {C Function} scm_close_input_port (port)
a0e07ba4
NJ
282Close the specified input port object. The routine has no effect if
283the file has already been closed. An exception may be raised if an
284error occurs. The value returned is unspecified.
285
286See also @ref{Ports and File Descriptors, close}, for a procedure
287which can close file descriptors.
288@end deffn
289
290@rnindex close-output-port
8f85c0c6
NJ
291@deffn {Scheme Procedure} close-output-port port
292@deffnx {C Function} scm_close_output_port (port)
a0e07ba4
NJ
293Close the specified output port object. The routine has no effect if
294the file has already been closed. An exception may be raised if an
295error occurs. The value returned is unspecified.
296
297See also @ref{Ports and File Descriptors, close}, for a procedure
298which can close file descriptors.
299@end deffn
300
8f85c0c6
NJ
301@deffn {Scheme Procedure} port-closed? port
302@deffnx {C Function} scm_port_closed_p (port)
a0e07ba4
NJ
303Return @code{#t} if @var{port} is closed or @code{#f} if it is
304open.
305@end deffn
306
307
308@node Random Access
309@section Random Access
310
8f85c0c6
NJ
311@deffn {Scheme Procedure} seek fd_port offset whence
312@deffnx {C Function} scm_seek (fd_port, offset, whence)
a0e07ba4
NJ
313Sets the current position of @var{fd/port} to the integer
314@var{offset}, which is interpreted according to the value of
315@var{whence}.
316
317One of the following variables should be supplied for
318@var{whence}:
319@defvar SEEK_SET
320Seek from the beginning of the file.
321@end defvar
322@defvar SEEK_CUR
323Seek from the current position.
324@end defvar
325@defvar SEEK_END
326Seek from the end of the file.
327@end defvar
328If @var{fd/port} is a file descriptor, the underlying system
329call is @code{lseek}. @var{port} may be a string port.
330
331The value returned is the new position in the file. This means
332that the current position of a port can be obtained using:
333@lisp
334(seek port 0 SEEK_CUR)
335@end lisp
336@end deffn
337
8f85c0c6
NJ
338@deffn {Scheme Procedure} ftell fd_port
339@deffnx {C Function} scm_ftell (fd_port)
a0e07ba4
NJ
340Return an integer representing the current position of
341@var{fd/port}, measured from the beginning. Equivalent to:
342
343@lisp
344(seek port 0 SEEK_CUR)
345@end lisp
346@end deffn
347
348@findex truncate
349@findex ftruncate
8f85c0c6
NJ
350@deffn {Scheme Procedure} truncate-file object [length]
351@deffnx {C Function} scm_truncate_file (object, length)
a0e07ba4
NJ
352Truncates the object referred to by @var{object} to at most
353@var{length} bytes. @var{object} can be a string containing a
354file name or an integer file descriptor or a port.
355@var{length} may be omitted if @var{object} is not a file name,
356in which case the truncation occurs at the current port.
357position. The return value is unspecified.
358@end deffn
359
360@node Line/Delimited
361@section Line Oriented and Delimited Text
362
363The delimited-I/O module can be accessed with:
364
365@smalllisp
366(use-modules (ice-9 rdelim))
367@end smalllisp
368
369It can be used to read or write lines of text, or read text delimited by
370a specified set of characters. It's similar to the @code{(scsh rdelim)}
371module from guile-scsh, but does not use multiple values or character
372sets and has an extra procedure @code{write-line}.
373
374@c begin (scm-doc-string "rdelim.scm" "read-line")
8f85c0c6 375@deffn {Scheme Procedure} read-line [port] [handle-delim]
a0e07ba4
NJ
376Return a line of text from @var{port} if specified, otherwise from the
377value returned by @code{(current-input-port)}. Under Unix, a line of text
378is terminated by the first end-of-line character or by end-of-file.
379
380If @var{handle-delim} is specified, it should be one of the following
381symbols:
382@table @code
383@item trim
384Discard the terminating delimiter. This is the default, but it will
385be impossible to tell whether the read terminated with a delimiter or
386end-of-file.
387@item concat
388Append the terminating delimiter (if any) to the returned string.
389@item peek
390Push the terminating delimiter (if any) back on to the port.
391@item split
392Return a pair containing the string read from the port and the
393terminating delimiter or end-of-file object.
394@end table
395@end deffn
396
397@c begin (scm-doc-string "rdelim.scm" "read-line!")
8f85c0c6 398@deffn {Scheme Procedure} read-line! buf [port]
a0e07ba4
NJ
399Read a line of text into the supplied string @var{buf} and return the
400number of characters added to @var{buf}. If @var{buf} is filled, then
401@code{#f} is returned.
402Read from @var{port} if
403specified, otherwise from the value returned by @code{(current-input-port)}.
404@end deffn
405
406@c begin (scm-doc-string "rdelim.scm" "read-delimited")
8f85c0c6 407@deffn {Scheme Procedure} read-delimited delims [port] [handle-delim]
a0e07ba4
NJ
408Read text until one of the characters in the string @var{delims} is found
409or end-of-file is reached. Read from @var{port} if supplied, otherwise
410from the value returned by @code{(current-input-port)}.
411@var{handle-delim} takes the same values as described for @code{read-line}.
412@end deffn
413
414@c begin (scm-doc-string "rdelim.scm" "read-delimited!")
8f85c0c6 415@deffn {Scheme Procedure} read-delimited! delims buf [port] [handle-delim] [start] [end]
a0e07ba4
NJ
416Read text into the supplied string @var{buf} and return the number of
417characters added to @var{buf} (subject to @var{handle-delim}, which takes
418the same values specified for @code{read-line}. If @var{buf} is filled,
419@code{#f} is returned for both the number of characters read and the
420delimiter. Also terminates if one of the characters in the string
421@var{delims} is found
422or end-of-file is reached. Read from @var{port} if supplied, otherwise
423from the value returned by @code{(current-input-port)}.
424@end deffn
425
8f85c0c6
NJ
426@deffn {Scheme Procedure} write-line obj [port]
427@deffnx {C Function} scm_write_line (obj, port)
a0e07ba4
NJ
428Display @var{obj} and a newline character to @var{port}. If
429@var{port} is not specified, @code{(current-output-port)} is
430used. This function is equivalent to:
431@lisp
432(display obj [port])
433(newline [port])
434@end lisp
435@end deffn
436
437Some of the abovementioned I/O functions rely on the following C
438primitives. These will mainly be of interest to people hacking Guile
439internals.
440
8f85c0c6
NJ
441@deffn {Scheme Procedure} %read-delimited! delims str gobble [port [start [end]]]
442@deffnx {C Function} scm_read_delimited_x (delims, str, gobble, port, start, end)
a0e07ba4
NJ
443Read characters from @var{port} into @var{str} until one of the
444characters in the @var{delims} string is encountered. If
445@var{gobble} is true, discard the delimiter character;
446otherwise, leave it in the input stream for the next read. If
447@var{port} is not specified, use the value of
448@code{(current-input-port)}. If @var{start} or @var{end} are
449specified, store data only into the substring of @var{str}
450bounded by @var{start} and @var{end} (which default to the
451beginning and end of the string, respectively).
452
453 Return a pair consisting of the delimiter that terminated the
454string and the number of characters read. If reading stopped
455at the end of file, the delimiter returned is the
456@var{eof-object}; if the string was filled without encountering
457a delimiter, this value is @code{#f}.
458@end deffn
459
8f85c0c6
NJ
460@deffn {Scheme Procedure} %read-line [port]
461@deffnx {C Function} scm_read_line (port)
a0e07ba4
NJ
462Read a newline-terminated line from @var{port}, allocating storage as
463necessary. The newline terminator (if any) is removed from the string,
464and a pair consisting of the line and its delimiter is returned. The
465delimiter may be either a newline or the @var{eof-object}; if
466@code{%read-line} is called at the end of file, it returns the pair
467@code{(#<eof> . #<eof>)}.
468@end deffn
469
470@node Block Reading and Writing
471@section Block reading and writing
472
473The Block-string-I/O module can be accessed with:
474
475@smalllisp
476(use-modules (ice-9 rw))
477@end smalllisp
478
479It currently contains procedures that help to implement the
480@code{(scsh rw)} module in guile-scsh.
481
8f85c0c6
NJ
482@deffn {Scheme Procedure} read-string!/partial str [port_or_fdes [start [end]]]
483@deffnx {C Function} scm_read_string_x_partial (str, port_or_fdes, start, end)
a0e07ba4
NJ
484Read characters from a port or file descriptor into a
485string @var{str}. A port must have an underlying file
486descriptor --- a so-called fport. This procedure is
487scsh-compatible and can efficiently read large strings.
488It will:
489
490@itemize
491@item
492attempt to fill the entire string, unless the @var{start}
493and/or @var{end} arguments are supplied. i.e., @var{start}
494defaults to 0 and @var{end} defaults to
495@code{(string-length str)}
496@item
497use the current input port if @var{port_or_fdes} is not
498supplied.
499@item
500return fewer than the requested number of characters in some
501cases, e.g., on end of file, if interrupted by a signal, or if
502not all the characters are immediately available.
503@item
504wait indefinitely for some input if no characters are
505currently available,
506unless the port is in non-blocking mode.
507@item
508read characters from the port's input buffers if available,
509instead from the underlying file descriptor.
510@item
511return @code{#f} if end-of-file is encountered before reading
512any characters, otherwise return the number of characters
513read.
514@item
515return 0 if the port is in non-blocking mode and no characters
516are immediately available.
517@item
518return 0 if the request is for 0 bytes, with no
519end-of-file check.
520@end itemize
521@end deffn
522
8f85c0c6
NJ
523@deffn {Scheme Procedure} write-string/partial str [port_or_fdes [start [end]]]
524@deffnx {C Function} scm_write_string_partial (str, port_or_fdes, start, end)
a0e07ba4
NJ
525Write characters from a string @var{str} to a port or file
526descriptor. A port must have an underlying file descriptor
527--- a so-called fport. This procedure is
528scsh-compatible and can efficiently write large strings.
529It will:
530
531@itemize
532@item
533attempt to write the entire string, unless the @var{start}
534and/or @var{end} arguments are supplied. i.e., @var{start}
535defaults to 0 and @var{end} defaults to
536@code{(string-length str)}
537@item
538use the current output port if @var{port_of_fdes} is not
539supplied.
540@item
541in the case of a buffered port, store the characters in the
542port's output buffer, if all will fit. If they will not fit
543then any existing buffered characters will be flushed
544before attempting
545to write the new characters directly to the underlying file
546descriptor. If the port is in non-blocking mode and
547buffered characters can not be flushed immediately, then an
548@code{EAGAIN} system-error exception will be raised (Note:
549scsh does not support the use of non-blocking buffered ports.)
550@item
551write fewer than the requested number of
552characters in some cases, e.g., if interrupted by a signal or
553if not all of the output can be accepted immediately.
554@item
555wait indefinitely for at least one character
556from @var{str} to be accepted by the port, unless the port is
557in non-blocking mode.
558@item
559return the number of characters accepted by the port.
560@item
561return 0 if the port is in non-blocking mode and can not accept
562at least one character from @var{str} immediately
563@item
564return 0 immediately if the request size is 0 bytes.
565@end itemize
566@end deffn
567
568@node Default Ports
569@section Default Ports for Input, Output and Errors
570
571@rnindex current-input-port
8f85c0c6
NJ
572@deffn {Scheme Procedure} current-input-port
573@deffnx {C Function} scm_current_input_port ()
a0e07ba4
NJ
574Return the current input port. This is the default port used
575by many input procedures. Initially, @code{current-input-port}
576returns the @dfn{standard input} in Unix and C terminology.
577@end deffn
578
579@rnindex current-output-port
8f85c0c6
NJ
580@deffn {Scheme Procedure} current-output-port
581@deffnx {C Function} scm_current_output_port ()
a0e07ba4
NJ
582Return the current output port. This is the default port used
583by many output procedures. Initially,
584@code{current-output-port} returns the @dfn{standard output} in
585Unix and C terminology.
586@end deffn
587
8f85c0c6
NJ
588@deffn {Scheme Procedure} current-error-port
589@deffnx {C Function} scm_current_error_port ()
a0e07ba4
NJ
590Return the port to which errors and warnings should be sent (the
591@dfn{standard error} in Unix and C terminology).
592@end deffn
593
8f85c0c6
NJ
594@deffn {Scheme Procedure} set-current-input-port port
595@deffnx {Scheme Procedure} set-current-output-port port
596@deffnx {Scheme Procedure} set-current-error-port port
597@deffnx {C Function} scm_set_current_input_port (port)
598@deffnx {C Function} scm_set_current_output_port (port)
599@deffnx {C Function} scm_set_current_error_port (port)
a0e07ba4
NJ
600Change the ports returned by @code{current-input-port},
601@code{current-output-port} and @code{current-error-port}, respectively,
602so that they use the supplied @var{port} for input or output.
603@end deffn
604
8f85c0c6 605@deffn {Scheme Procedure} set-current-output-port port
a0e07ba4
NJ
606Set the current default output port to PORT.
607@end deffn
608
8f85c0c6 609@deffn {Scheme Procedure} set-current-error-port port
a0e07ba4
NJ
610Set the current default error port to PORT.
611@end deffn
612
613
614@node Port Types
615@section Types of Port
616
617[Types of port; how to make them.]
618
619@menu
620* File Ports:: Ports on an operating system file.
621* String Ports:: Ports on a Scheme string.
622* Soft Ports:: Ports on arbitrary Scheme procedures.
623* Void Ports:: Ports on nothing at all.
624@end menu
625
626
627@node File Ports
628@subsection File Ports
629
630The following procedures are used to open file ports.
631See also @ref{Ports and File Descriptors, open}, for an interface
632to the Unix @code{open} system call.
633
8f85c0c6
NJ
634@deffn {Scheme Procedure} open-file filename mode
635@deffnx {C Function} scm_open_file (filename, mode)
a0e07ba4
NJ
636Open the file whose name is @var{filename}, and return a port
637representing that file. The attributes of the port are
638determined by the @var{mode} string. The way in which this is
639interpreted is similar to C stdio. The first character must be
640one of the following:
641@table @samp
642@item r
643Open an existing file for input.
644@item w
645Open a file for output, creating it if it doesn't already exist
646or removing its contents if it does.
647@item a
648Open a file for output, creating it if it doesn't already
649exist. All writes to the port will go to the end of the file.
650The "append mode" can be turned off while the port is in use
651@pxref{Ports and File Descriptors, fcntl}
652@end table
653The following additional characters can be appended:
654@table @samp
655@item +
656Open the port for both input and output. E.g., @code{r+}: open
657an existing file for both input and output.
658@item 0
659Create an "unbuffered" port. In this case input and output
660operations are passed directly to the underlying port
661implementation without additional buffering. This is likely to
662slow down I/O operations. The buffering mode can be changed
663while a port is in use @pxref{Ports and File Descriptors,
664setvbuf}
665@item l
666Add line-buffering to the port. The port output buffer will be
667automatically flushed whenever a newline character is written.
668@end table
669In theory we could create read/write ports which were buffered
670in one direction only. However this isn't included in the
671current interfaces. If a file cannot be opened with the access
672requested, @code{open-file} throws an exception.
673@end deffn
674
675@rnindex open-input-file
8f85c0c6 676@deffn {Scheme Procedure} open-input-file filename
a0e07ba4
NJ
677Open @var{filename} for input. Equivalent to
678@smalllisp
679(open-file @var{filename} "r")
680@end smalllisp
681@end deffn
682
683@rnindex open-output-file
8f85c0c6 684@deffn {Scheme Procedure} open-output-file filename
a0e07ba4
NJ
685Open @var{filename} for output. Equivalent to
686@smalllisp
687(open-file @var{filename} "w")
688@end smalllisp
689@end deffn
690
691@rnindex call-with-input-file
8f85c0c6 692@deffn {Scheme Procedure} call-with-input-file file proc
a0e07ba4
NJ
693@var{proc} should be a procedure of one argument, and @var{file} should
694be a string naming a file. The file must already exist. These
695procedures call @var{proc} with one argument: the port obtained by
696opening the named file for input or output. If the file cannot be
697opened, an error is signalled. If the procedure returns, then the port
698is closed automatically and the value yielded by the procedure is
699returned. If the procedure does not return, then the port will not be
700closed automatically unless it is possible to prove that the port will
701never again be used for a read or write operation.
702@end deffn
703
704@rnindex call-with-output-file
8f85c0c6 705@deffn {Scheme Procedure} call-with-output-file file proc
a0e07ba4
NJ
706@var{proc} should be a procedure of one argument, and @var{file} should
707be a string naming a file. The behaviour is unspecified if the file
708already exists. These procedures call @var{proc} with one argument: the
709port obtained by opening the named file for input or output. If the
710file cannot be opened, an error is signalled. If the procedure returns,
711then the port is closed automatically and the value yielded by the
712procedure is returned. If the procedure does not return, then the port
713will not be closed automatically unless it is possible to prove that the
714port will never again be used for a read or write operation.
715@end deffn
716
717@rnindex with-input-from-file
8f85c0c6 718@deffn {Scheme Procedure} with-input-from-file file thunk
a0e07ba4
NJ
719@var{thunk} must be a procedure of no arguments, and @var{file} must be
720a string naming a file. The file must already exist. The file is opened
721for input, an input port connected to it is made the default value
722returned by @code{current-input-port}, and the @var{thunk} is called
723with no arguments. When the @var{thunk} returns, the port is closed and
724the previous default is restored. Returns the value yielded by
725@var{thunk}. If an escape procedure is used to escape from the
726continuation of these procedures, their behavior is implementation
727dependent.
728@end deffn
729
730@rnindex with-output-to-file
8f85c0c6 731@deffn {Scheme Procedure} with-output-to-file file thunk
a0e07ba4
NJ
732@var{thunk} must be a procedure of no arguments, and @var{file} must be
733a string naming a file. The effect is unspecified if the file already
734exists. The file is opened for output, an output port connected to it
735is made the default value returned by @code{current-output-port}, and
736the @var{thunk} is called with no arguments. When the @var{thunk}
737returns, the port is closed and the previous default is restored.
738Returns the value yielded by @var{thunk}. If an escape procedure is
739used to escape from the continuation of these procedures, their behavior
740is implementation dependent.
741@end deffn
742
8f85c0c6 743@deffn {Scheme Procedure} with-error-to-file file thunk
a0e07ba4
NJ
744@var{thunk} must be a procedure of no arguments, and @var{file} must be
745a string naming a file. The effect is unspecified if the file already
746exists. The file is opened for output, an output port connected to it
747is made the default value returned by @code{current-error-port}, and the
748@var{thunk} is called with no arguments. When the @var{thunk} returns,
749the port is closed and the previous default is restored. Returns the
750value yielded by @var{thunk}. If an escape procedure is used to escape
751from the continuation of these procedures, their behavior is
752implementation dependent.
753@end deffn
754
8f85c0c6
NJ
755@deffn {Scheme Procedure} port-mode port
756@deffnx {C Function} scm_port_mode (port)
757Return the port modes associated with the open port @var{port}.
758These will not necessarily be identical to the modes used when
759the port was opened, since modes such as "append" which are
760used only during port creation are not retained.
a0e07ba4
NJ
761@end deffn
762
8f85c0c6
NJ
763@deffn {Scheme Procedure} port-filename port
764@deffnx {C Function} scm_port_filename (port)
a0e07ba4
NJ
765Return the filename associated with @var{port}. This function returns
766the strings "standard input", "standard output" and "standard error"
767when called on the current input, output and error ports respectively.
768@end deffn
769
8f85c0c6
NJ
770@deffn {Scheme Procedure} set-port-filename! port filename
771@deffnx {C Function} scm_set_port_filename_x (port, filename)
a0e07ba4
NJ
772Change the filename associated with @var{port}, using the current input
773port if none is specified. Note that this does not change the port's
774source of data, but only the value that is returned by
775@code{port-filename} and reported in diagnostic output.
776@end deffn
777
8f85c0c6
NJ
778@deffn {Scheme Procedure} file-port? obj
779@deffnx {C Function} scm_file_port_p (obj)
a0e07ba4
NJ
780Determine whether @var{obj} is a port that is related to a file.
781@end deffn
782
783
784@node String Ports
785@subsection String Ports
786
787The following allow string ports to be opened by analogy to R4R*
788file port facilities:
789
8f85c0c6
NJ
790@deffn {Scheme Procedure} call-with-output-string proc
791@deffnx {C Function} scm_call_with_output_string (proc)
a0e07ba4
NJ
792Calls the one-argument procedure @var{proc} with a newly created output
793port. When the function returns, the string composed of the characters
794written into the port is returned.
795@end deffn
796
8f85c0c6
NJ
797@deffn {Scheme Procedure} call-with-input-string string proc
798@deffnx {C Function} scm_call_with_input_string (string, proc)
a0e07ba4
NJ
799Calls the one-argument procedure @var{proc} with a newly
800created input port from which @var{string}'s contents may be
801read. The value yielded by the @var{proc} is returned.
802@end deffn
803
8f85c0c6 804@deffn {Scheme Procedure} with-output-to-string thunk
a0e07ba4
NJ
805Calls the zero-argument procedure @var{thunk} with the current output
806port set temporarily to a new string port. It returns a string
807composed of the characters written to the current output.
808@end deffn
809
8f85c0c6 810@deffn {Scheme Procedure} with-input-from-string string thunk
a0e07ba4
NJ
811Calls the zero-argument procedure @var{thunk} with the current input
812port set temporarily to a string port opened on the specified
813@var{string}. The value yielded by @var{thunk} is returned.
814@end deffn
815
8f85c0c6
NJ
816@deffn {Scheme Procedure} open-input-string str
817@deffnx {C Function} scm_open_input_string (str)
a0e07ba4
NJ
818Take a string and return an input port that delivers characters
819from the string. The port can be closed by
820@code{close-input-port}, though its storage will be reclaimed
821by the garbage collector if it becomes inaccessible.
822@end deffn
823
8f85c0c6
NJ
824@deffn {Scheme Procedure} open-output-string
825@deffnx {C Function} scm_open_output_string ()
a0e07ba4
NJ
826Return an output port that will accumulate characters for
827retrieval by @code{get-output-string}. The port can be closed
828by the procedure @code{close-output-port}, though its storage
829will be reclaimed by the garbage collector if it becomes
830inaccessible.
831@end deffn
832
8f85c0c6
NJ
833@deffn {Scheme Procedure} get-output-string port
834@deffnx {C Function} scm_get_output_string (port)
a0e07ba4
NJ
835Given an output port created by @code{open-output-string},
836return a string consisting of the characters that have been
837output to the port so far.
838@end deffn
839
840A string port can be used in many procedures which accept a port
841but which are not dependent on implementation details of fports.
842E.g., seeking and truncating will work on a string port,
843but trying to extract the file descriptor number will fail.
844
845
846@node Soft Ports
847@subsection Soft Ports
848
849A @dfn{soft-port} is a port based on a vector of procedures capable of
850accepting or delivering characters. It allows emulation of I/O ports.
851
8f85c0c6
NJ
852@deffn {Scheme Procedure} make-soft-port pv modes
853@deffnx {C Function} scm_make_soft_port (pv, modes)
a0e07ba4
NJ
854Return a port capable of receiving or delivering characters as
855specified by the @var{modes} string (@pxref{File Ports,
0a50eeaa 856open-file}). @var{pv} must be a vector of length 5 or 6. Its
a0e07ba4
NJ
857components are as follows:
858
859@enumerate 0
860@item
861procedure accepting one character for output
862@item
863procedure accepting a string for output
864@item
865thunk for flushing output
866@item
867thunk for getting one character
868@item
869thunk for closing port (not by garbage collection)
0a50eeaa
NJ
870@item
871(if present and not @code{#f}) thunk for computing the number of
872characters that can be read from the port without blocking.
a0e07ba4
NJ
873@end enumerate
874
875For an output-only port only elements 0, 1, 2, and 4 need be
876procedures. For an input-only port only elements 3 and 4 need
877be procedures. Thunks 2 and 4 can instead be @code{#f} if
878there is no useful operation for them to perform.
879
880If thunk 3 returns @code{#f} or an @code{eof-object}
881(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on
882Scheme}) it indicates that the port has reached end-of-file.
883For example:
884
885@lisp
886(define stdout (current-output-port))
887(define p (make-soft-port
888 (vector
889 (lambda (c) (write c stdout))
890 (lambda (s) (display s stdout))
891 (lambda () (display "." stdout))
892 (lambda () (char-upcase (read-char)))
893 (lambda () (display "@@" stdout)))
894 "rw"))
895
896(write p p) @result{} #<input-output: soft 8081e20>
897@end lisp
898@end deffn
899
900
901@node Void Ports
902@subsection Void Ports
903
904This kind of port causes any data to be discarded when written to, and
905always returns the end-of-file object when read from.
906
8f85c0c6
NJ
907@deffn {Scheme Procedure} %make-void-port mode
908@deffnx {C Function} scm_sys_make_void_port (mode)
a0e07ba4 909Create and return a new void port. A void port acts like
198586ed 910@file{/dev/null}. The @var{mode} argument
8f85c0c6
NJ
911specifies the input/output modes for this port: see the
912documentation for @code{open-file} in @ref{File Ports}.
a0e07ba4
NJ
913@end deffn
914
915
9401323e
NJ
916@node I/O Extensions
917@section Using and Extending Ports in C
918
919@menu
920* C Port Interface:: Using ports from C.
921* Port Implementation:: How to implement a new port type in C.
922@end menu
923
924
925@node C Port Interface
926@subsection C Port Interface
927
928This section describes how to use Scheme ports from C.
929
930@subsubsection Port basics
931
932There are two main data structures. A port type object (ptob) is of
933type @code{scm_ptob_descriptor}. A port instance is of type
934@code{scm_port}. Given an @code{SCM} variable which points to a port,
935the corresponding C port object can be obtained using the
936@code{SCM_PTAB_ENTRY} macro. The ptob can be obtained by using
937@code{SCM_PTOBNUM} to give an index into the @code{scm_ptobs}
938global array.
939
940@subsubsection Port buffers
941
942An input port always has a read buffer and an output port always has a
943write buffer. However the size of these buffers is not guaranteed to be
944more than one byte (e.g., the @code{shortbuf} field in @code{scm_port}
945which is used when no other buffer is allocated). The way in which the
946buffers are allocated depends on the implementation of the ptob. For
947example in the case of an fport, buffers may be allocated with malloc
948when the port is created, but in the case of an strport the underlying
949string is used as the buffer.
950
951@subsubsection The @code{rw_random} flag
952
953Special treatment is required for ports which can be seeked at random.
954Before various operations, such as seeking the port or changing from
955input to output on a bidirectional port or vice versa, the port
85a9b4ed 956implementation must be given a chance to update its state. The write
9401323e
NJ
957buffer is updated by calling the @code{flush} ptob procedure and the
958input buffer is updated by calling the @code{end_input} ptob procedure.
959In the case of an fport, @code{flush} causes buffered output to be
960written to the file descriptor, while @code{end_input} causes the
961descriptor position to be adjusted to account for buffered input which
962was never read.
963
964The special treatment must be performed if the @code{rw_random} flag in
965the port is non-zero.
966
967@subsubsection The @code{rw_active} variable
968
969The @code{rw_active} variable in the port is only used if
970@code{rw_random} is set. It's defined as an enum with the following
971values:
972
973@table @code
974@item SCM_PORT_READ
975the read buffer may have unread data.
976
977@item SCM_PORT_WRITE
978the write buffer may have unwritten data.
979
980@item SCM_PORT_NEITHER
981neither the write nor the read buffer has data.
982@end table
983
984@subsubsection Reading from a port.
985
986To read from a port, it's possible to either call existing libguile
987procedures such as @code{scm_getc} and @code{scm_read_line} or to read
988data from the read buffer directly. Reading from the buffer involves
989the following steps:
990
991@enumerate
992@item
993Flush output on the port, if @code{rw_active} is @code{SCM_PORT_WRITE}.
994
995@item
996Fill the read buffer, if it's empty, using @code{scm_fill_input}.
997
998@item Read the data from the buffer and update the read position in
999the buffer. Steps 2) and 3) may be repeated as many times as required.
1000
1001@item Set rw_active to @code{SCM_PORT_READ} if @code{rw_random} is set.
1002
1003@item update the port's line and column counts.
1004@end enumerate
1005
1006@subsubsection Writing to a port.
1007
1008To write data to a port, calling @code{scm_lfwrite} should be sufficient for
1009most purposes. This takes care of the following steps:
1010
1011@enumerate
1012@item
1013End input on the port, if @code{rw_active} is @code{SCM_PORT_READ}.
1014
1015@item
1016Pass the data to the ptob implementation using the @code{write} ptob
1017procedure. The advantage of using the ptob @code{write} instead of
1018manipulating the write buffer directly is that it allows the data to be
1019written in one operation even if the port is using the single-byte
1020@code{shortbuf}.
1021
1022@item
1023Set @code{rw_active} to @code{SCM_PORT_WRITE} if @code{rw_random}
1024is set.
1025@end enumerate
1026
1027
1028@node Port Implementation
1029@subsection Port Implementation
1030
1031This section describes how to implement a new port type in C.
1032
1033As described in the previous section, a port type object (ptob) is
1034a structure of type @code{scm_ptob_descriptor}. A ptob is created by
1035calling @code{scm_make_port_type}.
1036
1037All of the elements of the ptob, apart from @code{name}, are procedures
1038which collectively implement the port behaviour. Creating a new port
1039type mostly involves writing these procedures.
1040
1041@code{scm_make_port_type} initializes three elements of the structure
1042(@code{name}, @code{fill_input} and @code{write}) from its arguments.
1043The remaining elements are initialized with default values and can be
1044set later if required.
1045
1046@table @code
1047@item name
1048A pointer to a NUL terminated string: the name of the port type. This
1049is the only element of @code{scm_ptob_descriptor} which is not
1050a procedure. Set via the first argument to @code{scm_make_port_type}.
1051
1052@item mark
1053Called during garbage collection to mark any SCM objects that a port
1054object may contain. It doesn't need to be set unless the port has
1055@code{SCM} components. Set using @code{scm_set_port_mark}.
1056
1057@item free
1058Called when the port is collected during gc. It
1059should free any resources used by the port.
1060Set using @code{scm_set_port_free}.
1061
1062@item print
1063Called when @code{write} is called on the port object, to print a
1064port description. e.g., for an fport it may produce something like:
1065@code{#<input: /etc/passwd 3>}. Set using @code{scm_set_port_print}.
1066
1067@item equalp
1068Not used at present. Set using @code{scm_set_port_equalp}.
1069
1070@item close
1071Called when the port is closed, unless it was collected during gc. It
1072should free any resources used by the port.
1073Set using @code{scm_set_port_close}.
1074
1075@item write
1076Accept data which is to be written using the port. The port implementation
1077may choose to buffer the data instead of processing it directly.
1078Set via the third argument to @code{scm_make_port_type}.
1079
1080@item flush
1081Complete the processing of buffered output data. Reset the value of
1082@code{rw_active} to @code{SCM_PORT_NEITHER}.
1083Set using @code{scm_set_port_flush}.
1084
1085@item end_input
85a9b4ed 1086Perform any synchronization required when switching from input to output
9401323e
NJ
1087on the port. Reset the value of @code{rw_active} to @code{SCM_PORT_NEITHER}.
1088Set using @code{scm_set_port_end_input}.
1089
1090@item fill_input
1091Read new data into the read buffer and return the first character. It
1092can be assumed that the read buffer is empty when this procedure is called.
1093Set via the second argument to @code{scm_make_port_type}.
1094
1095@item input_waiting
1096Return a lower bound on the number of bytes that could be read from the
1097port without blocking. It can be assumed that the current state of
1098@code{rw_active} is @code{SCM_PORT_NEITHER}.
1099Set using @code{scm_set_port_input_waiting}.
1100
1101@item seek
1102Set the current position of the port. The procedure can not make
1103any assumptions about the value of @code{rw_active} when it's
1104called. It can reset the buffers first if desired by using something
1105like:
1106
1107@example
1108 if (pt->rw_active == SCM_PORT_READ)
1109 scm_end_input (object);
1110 else if (pt->rw_active == SCM_PORT_WRITE)
1111 ptob->flush (object);
1112@end example
1113
1114However note that this will have the side effect of discarding any data
1115in the unread-char buffer, in addition to any side effects from the
1116@code{end_input} and @code{flush} ptob procedures. This is undesirable
1117when seek is called to measure the current position of the port, i.e.,
1118@code{(seek p 0 SEEK_CUR)}. The libguile fport and string port
1119implementations take care to avoid this problem.
1120
1121The procedure is set using @code{scm_set_port_seek}.
1122
1123@item truncate
1124Truncate the port data to be specified length. It can be assumed that the
1125current state of @code{rw_active} is @code{SCM_PORT_NEITHER}.
1126Set using @code{scm_set_port_truncate}.
1127
1128@end table
1129
1130
a0e07ba4
NJ
1131@c Local Variables:
1132@c TeX-master: "guile.texi"
1133@c End: