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