Renamed the "frames" that are related to dynamic-wind to "dynamic
[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 @code{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.
620
621 Initially this is the @dfn{standard input} in Unix and C terminology.
622 When the standard input is a tty the port is unbuffered, otherwise
623 it's fully buffered.
624
625 Unbuffered input is good if an application runs an interactive
626 subprocess, since any type-ahead input won't go into Guile's buffer
627 and be unavailable to the subprocess.
628
629 Note that Guile buffering is completely separate from the tty ``line
630 discipline''. In the usual cooked mode on a tty Guile only sees a
631 line of input once the user presses @key{Return}.
632 @end deffn
633
634 @rnindex current-output-port
635 @deffn {Scheme Procedure} current-output-port
636 @deffnx {C Function} scm_current_output_port ()
637 @cindex standard output
638 Return the current output port. This is the default port used
639 by many output procedures.
640
641 Initially this is the @dfn{standard output} in Unix and C terminology.
642 When the standard output is a tty this port is unbuffered, otherwise
643 it's fully buffered.
644
645 Unbuffered output to a tty is good for ensuring progress output or a
646 prompt is seen. But an application which always prints whole lines
647 could change to line buffered, or an application with a lot of output
648 could go fully buffered and perhaps make explicit @code{force-output}
649 calls (@pxref{Writing}) at selected points.
650 @end deffn
651
652 @deffn {Scheme Procedure} current-error-port
653 @deffnx {C Function} scm_current_error_port ()
654 @cindex standard error output
655 Return the port to which errors and warnings should be sent.
656
657 Initially this is the @dfn{standard error} in Unix and C terminology.
658 When the standard error is a tty this port is unbuffered, otherwise
659 it's fully buffered.
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)
668 Change the ports returned by @code{current-input-port},
669 @code{current-output-port} and @code{current-error-port}, respectively,
670 so that they use the supplied @var{port} for input or output.
671 @end deffn
672
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)
676 These functions must be used inside a pair of calls to
677 @code{scm_dynwind_begin} and @code{scm_dynwind_end} (@pxref{Dynamic
678 Wind}). During the dynwind context, the indicated port is set to
679 @var{port}.
680
681 More precisely, the current port is swapped with a `backup' value
682 whenever the dynwind context is entered or left. The backup value is
683 initialized 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
702 The following procedures are used to open file ports.
703 See also @ref{Ports and File Descriptors, open}, for an interface
704 to the Unix @code{open} system call.
705
706 Most systems have limits on how many files can be open, so it's
707 strongly recommended that file ports be closed explicitly when no
708 longer required (@pxref{Ports}).
709
710 @deffn {Scheme Procedure} open-file filename mode
711 @deffnx {C Function} scm_open_file (filename, mode)
712 Open the file whose name is @var{filename}, and return a port
713 representing that file. The attributes of the port are
714 determined by the @var{mode} string. The way in which this is
715 interpreted is similar to C stdio. The first character must be
716 one of the following:
717
718 @table @samp
719 @item r
720 Open an existing file for input.
721 @item w
722 Open a file for output, creating it if it doesn't already exist
723 or removing its contents if it does.
724 @item a
725 Open a file for output, creating it if it doesn't already
726 exist. All writes to the port will go to the end of the file.
727 The "append mode" can be turned off while the port is in use
728 @pxref{Ports and File Descriptors, fcntl}
729 @end table
730
731 The following additional characters can be appended:
732
733 @table @samp
734 @item +
735 Open the port for both input and output. E.g., @code{r+}: open
736 an existing file for both input and output.
737 @item 0
738 Create an "unbuffered" port. In this case input and output
739 operations are passed directly to the underlying port
740 implementation without additional buffering. This is likely to
741 slow down I/O operations. The buffering mode can be changed
742 while a port is in use @pxref{Ports and File Descriptors,
743 setvbuf}
744 @item l
745 Add line-buffering to the port. The port output buffer will be
746 automatically flushed whenever a newline character is written.
747 @item b
748 Use binary mode. On DOS systems the default text mode converts CR+LF
749 in the file to newline for the program, whereas binary mode reads and
750 writes all bytes unchanged. On Unix-like systems there is no such
751 distinction, text files already contain just newlines and no
752 conversion is ever made. The @code{b} flag is accepted on all
753 systems, 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}
757 call, when that flag is available.)
758 @end table
759
760 If a file cannot be opened with the access
761 requested, @code{open-file} throws an exception.
762
763 In theory we could create read/write ports which were buffered
764 in one direction only. However this isn't included in the
765 current interfaces.
766 @end deffn
767
768 @rnindex open-input-file
769 @deffn {Scheme Procedure} open-input-file filename
770 Open @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
778 Open @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
788 Open @var{filename} for input or output, and call @code{(@var{proc}
789 port)} 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
792 cannot be opened.
793
794 When @var{proc} returns, the port is closed. If @var{proc} does not
795 return (eg.@: if it throws an error), then the port might not be
796 closed automatically, though it will be garbage collected in the usual
797 way 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
805 Open @var{filename} and call @code{(@var{thunk})} with the new port
806 setup as respectively the @code{current-input-port},
807 @code{current-output-port}, or @code{current-error-port}. Return the
808 value returned by @var{thunk}. @var{filename} is opened as per
809 @code{open-input-file} or @code{open-output-file} respectively, and an
810 error is signalled if it cannot be opened.
811
812 When @var{thunk} returns, the port is closed and the previous setting
813 of the respective current port is restored.
814
815 The current port setting is managed with @code{dynamic-wind}, so the
816 previous value is restored no matter how @var{thunk} exits (eg.@: an
817 exception), and if @var{thunk} is re-entered (via a captured
818 continuation) then it's set again to the @var{FILENAME} port.
819
820 The port is closed when @var{thunk} returns normally, but not when
821 exited via an exception or new continuation. This ensures it's still
822 ready for use if @var{thunk} is re-entered by a captured continuation.
823 Of course the port is always garbage collected and closed in the usual
824 way when no longer referenced anywhere.
825 @end deffn
826
827 @deffn {Scheme Procedure} port-mode port
828 @deffnx {C Function} scm_port_mode (port)
829 Return the port modes associated with the open port @var{port}.
830 These will not necessarily be identical to the modes used when
831 the port was opened, since modes such as "append" which are
832 used 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)
837 Return the filename associated with @var{port}. This function returns
838 the strings "standard input", "standard output" and "standard error"
839 when called on the current input, output and error ports respectively.
840
841 @var{port} must be open, @code{port-filename} cannot be used once the
842 port is closed.
843 @end deffn
844
845 @deffn {Scheme Procedure} set-port-filename! port filename
846 @deffnx {C Function} scm_set_port_filename_x (port, filename)
847 Change the filename associated with @var{port}, using the current input
848 port if none is specified. Note that this does not change the port's
849 source 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)
855 Determine 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
862 The following allow string ports to be opened by analogy to R4R*
863 file port facilities:
864
865 @deffn {Scheme Procedure} call-with-output-string proc
866 @deffnx {C Function} scm_call_with_output_string (proc)
867 Calls the one-argument procedure @var{proc} with a newly created output
868 port. When the function returns, the string composed of the characters
869 written 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)
874 Calls the one-argument procedure @var{proc} with a newly
875 created input port from which @var{string}'s contents may be
876 read. The value yielded by the @var{proc} is returned.
877 @end deffn
878
879 @deffn {Scheme Procedure} with-output-to-string thunk
880 Calls the zero-argument procedure @var{thunk} with the current output
881 port set temporarily to a new string port. It returns a string
882 composed of the characters written to the current output.
883 @end deffn
884
885 @deffn {Scheme Procedure} with-input-from-string string thunk
886 Calls the zero-argument procedure @var{thunk} with the current input
887 port 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)
893 Take a string and return an input port that delivers characters
894 from the string. The port can be closed by
895 @code{close-input-port}, though its storage will be reclaimed
896 by 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 ()
901 Return an output port that will accumulate characters for
902 retrieval by @code{get-output-string}. The port can be closed
903 by the procedure @code{close-output-port}, though its storage
904 will be reclaimed by the garbage collector if it becomes
905 inaccessible.
906 @end deffn
907
908 @deffn {Scheme Procedure} get-output-string port
909 @deffnx {C Function} scm_get_output_string (port)
910 Given an output port created by @code{open-output-string},
911 return a string consisting of the characters that have been
912 output to the port so far.
913
914 @code{get-output-string} must be used before closing @var{port}, once
915 closed the string cannot be obtained.
916 @end deffn
917
918 A string port can be used in many procedures which accept a port
919 but which are not dependent on implementation details of fports.
920 E.g., seeking and truncating will work on a string port,
921 but trying to extract the file descriptor number will fail.
922
923
924 @node Soft Ports
925 @subsubsection Soft Ports
926
927 A @dfn{soft-port} is a port based on a vector of procedures capable of
928 accepting 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)
932 Return a port capable of receiving or delivering characters as
933 specified by the @var{modes} string (@pxref{File Ports,
934 open-file}). @var{pv} must be a vector of length 5 or 6. Its
935 components are as follows:
936
937 @enumerate 0
938 @item
939 procedure accepting one character for output
940 @item
941 procedure accepting a string for output
942 @item
943 thunk for flushing output
944 @item
945 thunk for getting one character
946 @item
947 thunk for closing port (not by garbage collection)
948 @item
949 (if present and not @code{#f}) thunk for computing the number of
950 characters that can be read from the port without blocking.
951 @end enumerate
952
953 For an output-only port only elements 0, 1, 2, and 4 need be
954 procedures. For an input-only port only elements 3 and 4 need
955 be procedures. Thunks 2 and 4 can instead be @code{#f} if
956 there is no useful operation for them to perform.
957
958 If thunk 3 returns @code{#f} or an @code{eof-object}
959 (@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on
960 Scheme}) it indicates that the port has reached end-of-file.
961 For 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
982 This kind of port causes any data to be discarded when written to, and
983 always 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)
987 Create and return a new void port. A void port acts like
988 @file{/dev/null}. The @var{mode} argument
989 specifies the input/output modes for this port: see the
990 documentation 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
1006 This section describes how to use Scheme ports from C.
1007
1008 @subsubheading Port basics
1009
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
1016 There are two main data structures. A port type object (ptob) is of
1017 type @code{scm_ptob_descriptor}. A port instance is of type
1018 @code{scm_port}. Given an @code{SCM} variable which points to a port,
1019 the 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}
1022 global array.
1023
1024 @subsubheading Port buffers
1025
1026 An input port always has a read buffer and an output port always has a
1027 write buffer. However the size of these buffers is not guaranteed to be
1028 more than one byte (e.g., the @code{shortbuf} field in @code{scm_port}
1029 which is used when no other buffer is allocated). The way in which the
1030 buffers are allocated depends on the implementation of the ptob. For
1031 example in the case of an fport, buffers may be allocated with malloc
1032 when the port is created, but in the case of an strport the underlying
1033 string is used as the buffer.
1034
1035 @subsubheading The @code{rw_random} flag
1036
1037 Special treatment is required for ports which can be seeked at random.
1038 Before various operations, such as seeking the port or changing from
1039 input to output on a bidirectional port or vice versa, the port
1040 implementation must be given a chance to update its state. The write
1041 buffer is updated by calling the @code{flush} ptob procedure and the
1042 input buffer is updated by calling the @code{end_input} ptob procedure.
1043 In the case of an fport, @code{flush} causes buffered output to be
1044 written to the file descriptor, while @code{end_input} causes the
1045 descriptor position to be adjusted to account for buffered input which
1046 was never read.
1047
1048 The special treatment must be performed if the @code{rw_random} flag in
1049 the port is non-zero.
1050
1051 @subsubheading The @code{rw_active} variable
1052
1053 The @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
1055 values:
1056
1057 @table @code
1058 @item SCM_PORT_READ
1059 the read buffer may have unread data.
1060
1061 @item SCM_PORT_WRITE
1062 the write buffer may have unwritten data.
1063
1064 @item SCM_PORT_NEITHER
1065 neither the write nor the read buffer has data.
1066 @end table
1067
1068 @subsubheading Reading from a port.
1069
1070 To read from a port, it's possible to either call existing libguile
1071 procedures such as @code{scm_getc} and @code{scm_read_line} or to read
1072 data from the read buffer directly. Reading from the buffer involves
1073 the following steps:
1074
1075 @enumerate
1076 @item
1077 Flush output on the port, if @code{rw_active} is @code{SCM_PORT_WRITE}.
1078
1079 @item
1080 Fill 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
1083 the 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
1092 To write data to a port, calling @code{scm_lfwrite} should be sufficient for
1093 most purposes. This takes care of the following steps:
1094
1095 @enumerate
1096 @item
1097 End input on the port, if @code{rw_active} is @code{SCM_PORT_READ}.
1098
1099 @item
1100 Pass the data to the ptob implementation using the @code{write} ptob
1101 procedure. The advantage of using the ptob @code{write} instead of
1102 manipulating the write buffer directly is that it allows the data to be
1103 written in one operation even if the port is using the single-byte
1104 @code{shortbuf}.
1105
1106 @item
1107 Set @code{rw_active} to @code{SCM_PORT_WRITE} if @code{rw_random}
1108 is set.
1109 @end enumerate
1110
1111
1112 @node Port Implementation
1113 @subsubsection Port Implementation
1114
1115 This section describes how to implement a new port type in C.
1116
1117 As described in the previous section, a port type object (ptob) is
1118 a structure of type @code{scm_ptob_descriptor}. A ptob is created by
1119 calling @code{scm_make_port_type}.
1120
1121 All of the elements of the ptob, apart from @code{name}, are procedures
1122 which collectively implement the port behaviour. Creating a new port
1123 type 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.
1127 The remaining elements are initialized with default values and can be
1128 set later if required.
1129
1130 @table @code
1131 @item name
1132 A pointer to a NUL terminated string: the name of the port type. This
1133 is the only element of @code{scm_ptob_descriptor} which is not
1134 a procedure. Set via the first argument to @code{scm_make_port_type}.
1135
1136 @item mark
1137 Called during garbage collection to mark any SCM objects that a port
1138 object 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
1142 Called when the port is collected during gc. It
1143 should free any resources used by the port.
1144 Set using @code{scm_set_port_free}.
1145
1146 @item print
1147 Called when @code{write} is called on the port object, to print a
1148 port 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
1152 Not used at present. Set using @code{scm_set_port_equalp}.
1153
1154 @item close
1155 Called when the port is closed, unless it was collected during gc. It
1156 should free any resources used by the port.
1157 Set using @code{scm_set_port_close}.
1158
1159 @item write
1160 Accept data which is to be written using the port. The port implementation
1161 may choose to buffer the data instead of processing it directly.
1162 Set via the third argument to @code{scm_make_port_type}.
1163
1164 @item flush
1165 Complete the processing of buffered output data. Reset the value of
1166 @code{rw_active} to @code{SCM_PORT_NEITHER}.
1167 Set using @code{scm_set_port_flush}.
1168
1169 @item end_input
1170 Perform any synchronization required when switching from input to output
1171 on the port. Reset the value of @code{rw_active} to @code{SCM_PORT_NEITHER}.
1172 Set using @code{scm_set_port_end_input}.
1173
1174 @item fill_input
1175 Read new data into the read buffer and return the first character. It
1176 can be assumed that the read buffer is empty when this procedure is called.
1177 Set via the second argument to @code{scm_make_port_type}.
1178
1179 @item input_waiting
1180 Return a lower bound on the number of bytes that could be read from the
1181 port without blocking. It can be assumed that the current state of
1182 @code{rw_active} is @code{SCM_PORT_NEITHER}.
1183 Set using @code{scm_set_port_input_waiting}.
1184
1185 @item seek
1186 Set the current position of the port. The procedure can not make
1187 any assumptions about the value of @code{rw_active} when it's
1188 called. It can reset the buffers first if desired by using something
1189 like:
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
1198 However note that this will have the side effect of discarding any data
1199 in the unread-char buffer, in addition to any side effects from the
1200 @code{end_input} and @code{flush} ptob procedures. This is undesirable
1201 when 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
1203 implementations take care to avoid this problem.
1204
1205 The procedure is set using @code{scm_set_port_seek}.
1206
1207 @item truncate
1208 Truncate the port data to be specified length. It can be assumed that the
1209 current state of @code{rw_active} is @code{SCM_PORT_NEITHER}.
1210 Set 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: