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