* Automatic docstring updates.
[bpt/guile.git] / doc / scheme-io.texi
1 @page
2 @node Input and Output
3 @chapter Input and Output
4
5 @menu
6 * Ports:: The idea of the port abstraction.
7 * Reading:: Procedures for reading from a port.
8 * Writing:: Procedures for writing to a port.
9 * Closing:: Procedures to close a port.
10 * Random Access:: Moving around a random access port.
11 * Line/Delimited:: Read and write lines or delimited text.
12 * Binary IO:: Save and restore Scheme objects.
13 * Default Ports:: Defaults for input, output and errors.
14 * Port Types:: Types of port and how to make them.
15 @end menu
16
17
18 @node Ports
19 @section Ports
20
21 [Concept of the port abstraction.]
22
23 Sequential input/output in Scheme is represented by operations on a
24 @dfn{port}. Characters can be read from an input port and
25 written to an output port. This chapter explains the operations
26 that Guile provides for working with ports.
27
28 The formal definition of a port is very generic: an input port is
29 simply ``an object which can deliver characters on command,'' and
30 an output port is ``an object which can accept characters.''
31 Because this definition is so loose, it is easy to write functions
32 that simulate ports in software. @dfn{Soft ports} and @dfn{string
33 ports} are two interesting and powerful examples of this technique.
34
35 @r5index input-port?
36 @c docstring begin (texi-doc-string "guile" "input-port?")
37 @deffn primitive input-port? x
38 Returns @code{#t} if @var{x} is an input port, otherwise returns
39 @code{#f}. Any object satisfying this predicate also satisfies
40 @code{port?}.
41 @end deffn
42
43 @r5index output-port?
44 @c docstring begin (texi-doc-string "guile" "output-port?")
45 @deffn primitive output-port? x
46 Returns @code{#t} if @var{x} is an output port, otherwise returns
47 @code{#f}. Any object satisfying this predicate also satisfies
48 @code{port?}.
49 @end deffn
50
51 @c docstring begin (texi-doc-string "guile" "port?")
52 @deffn primitive port? x
53 Returns a boolean indicating whether @var{x} is a port.
54 Equivalent to @code{(or (input-port? @var{x}) (output-port?
55 @var{x}))}.
56 @end deffn
57
58
59 @node Reading
60 @section Reading
61
62 [Generic procedures for reading from ports.]
63
64 @r5index eof-object?
65 @c docstring begin (texi-doc-string "guile" "eof-object?")
66 @deffn primitive eof-object? x
67 Returns @code{#t} if @var{x} is an end-of-file object; otherwise
68 returns @code{#f}.
69 @end deffn
70
71 @r5index char-ready?
72 @c docstring begin (texi-doc-string "guile" "char-ready?")
73 @deffn primitive char-ready? [port]
74 Returns @code{#t} if a character is ready on input @var{port} and
75 returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}
76 then the next @code{read-char} operation on @var{port} is
77 guaranteed not to hang. If @var{port} is a file port at end of
78 file then @code{char-ready?} returns @code{#t}.
79 @footnote{@code{char-ready?} exists to make it possible for a
80 program to accept characters from interactive ports without getting
81 stuck waiting for input. Any input editors associated with such ports
82 must make sure that characters whose existence has been asserted by
83 @code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to
84 return @code{#f} at end of file, a port at end of file would be
85 indistinguishable from an interactive port that has no ready
86 characters.}
87 @end deffn
88
89 @r5index read-char?
90 @c docstring begin (texi-doc-string "guile" "read-char")
91 @deffn primitive read-char [port]
92 Returns the next character available from @var{port}, updating
93 @var{port} to point to the following character. If no more
94 characters are available, an end-of-file object is returned.
95 @end deffn
96
97 @r5index peek-char?
98 @c docstring begin (texi-doc-string "guile" "peek-char")
99 @deffn primitive peek-char [port]
100 Returns the next character available from @var{port},
101 @emph{without} updating @var{port} to point to the following
102 character. If no more characters are available, an end-of-file object
103 is returned.@footnote{The value returned by a call to @code{peek-char}
104 is the same as the value that would have been returned by a call to
105 @code{read-char} on the same port. The only difference is that the very
106 next call to @code{read-char} or @code{peek-char} on that
107 @var{port} will return the value returned by the preceding call to
108 @code{peek-char}. In particular, a call to @code{peek-char} on an
109 interactive port will hang waiting for input whenever a call to
110 @code{read-char} would have hung.}
111 @end deffn
112
113 @c docstring begin (texi-doc-string "guile" "unread-char")
114 @deffn primitive unread-char cobj port
115 Place @var{char} in @var{port} so that it will be read by the
116 next read operation. If called multiple times, the unread characters
117 will be read again in last-in first-out order. If @var{port} is
118 not supplied, the current input port is used.
119 @end deffn
120
121 @c docstring begin (texi-doc-string "guile" "unread-string")
122 @deffn primitive unread-string str port
123 Place the string @var{str} in @var{port} so that its characters will be
124 read in subsequent read operations. If called multiple times, the
125 unread characters will be read again in last-in first-out order. If
126 @var{port} is not supplied, the current-input-port is used.
127 @end deffn
128
129 @c docstring begin (texi-doc-string "guile" "drain-input")
130 @deffn primitive drain-input port
131 Drain @var{port}'s read buffers (including any pushed-back
132 characters) and returns the content as a single string.
133 @end deffn
134
135 @c ARGFIXME port/input-port
136 @c docstring begin (texi-doc-string "guile" "port-column")
137 @c docstring begin (texi-doc-string "guile" "port-line")
138 @deffn primitive port-column port
139 @deffnx primitive port-line port
140 Return the current column number or line number of @var{port},
141 using the current input port if none is specified. If the number is
142 unknown, the result is #f. Otherwise, the result is a 0-origin integer
143 - i.e. the first character of the first line is line 0, column 0.
144 (However, when you display a file position, for example in an error
145 message, we recommend you add 1 to get 1-origin integers. This is
146 because lines and column numbers traditionally start with 1, and that is
147 what non-programmers will find most natural.)
148 @end deffn
149
150 @c ARGFIXME port/input-port
151 @c docstring begin (texi-doc-string "guile" "set-port-column!")
152 @c docstring begin (texi-doc-string "guile" "set-port-line!")
153 @deffn primitive set-port-column! port column
154 @deffnx primitive set-port-line! port line
155 Set the current column or line number of @var{port}, using the
156 current input port if none is specified.
157 @end deffn
158
159 @deffn primitive read-string!/partial str [port_or_fdes [start [end]]]
160 Read characters from an fport or file descriptor into a
161 string @var{str}. This procedure is scsh-compatible
162 and can efficiently read large strings. It will:
163
164 @itemize
165 @item
166 attempt to fill the entire string, unless the @var{start}
167 and/or @var{end} arguments are supplied. i.e., @var{start}
168 defaults to 0 and @var{end} defaults to
169 @code{(string-length str)}
170 @item
171 use the current input port if @var{port_or_fdes} is not
172 supplied.
173 @item
174 read any characters that are currently available,
175 without waiting for the rest (short reads are possible).
176
177 @item
178 wait for as long as it needs to for the first character to
179 become available, unless the port is in non-blocking mode
180 @item
181 return @code{#f} if end-of-file is encountered before reading
182 any characters, otherwise return the number of characters
183 read.
184 @item
185 return 0 if the port is in non-blocking mode and no characters
186 are immediately available.
187 @item
188 return 0 if the request is for 0 bytes, with no
189 end-of-file check
190 @end itemize
191 @end deffn
192
193
194 @node Writing
195 @section Writing
196
197 [Generic procedures for writing to ports.]
198
199 @c docstring begin (texi-doc-string "guile" "get-print-state")
200 @deffn primitive get-print-state port
201 Return the print state of the port @var{port}. If @var{port}
202 has no associated print state, @code{#f} is returned.
203 @end deffn
204
205 @r5index newline
206 @c docstring begin (texi-doc-string "guile" "newline")
207 @deffn primitive newline [port]
208 Send a newline to @var{port}.
209 @end deffn
210
211 @c docstring begin (texi-doc-string "guile" "port-with-print-state")
212 @deffn primitive port-with-print-state port pstate
213 Create a new port which behaves like @var{port}, but with an
214 included print state @var{pstate}.
215 @end deffn
216
217 @c docstring begin (texi-doc-string "guile" "print-options-interface")
218 @deffn primitive print-options-interface [setting]
219 Option interface for the print options. Instead of using
220 this procedure directly, use the procedures
221 @code{print-enable}, @code{print-disable}, @code{print-set!}
222 and @code{print-options}.
223 @end deffn
224
225 @c docstring begin (texi-doc-string "guile" "simple-format")
226 @deffn primitive simple-format destination message . args
227 Write @var{message} to @var{destination}, defaulting to
228 the current output port.
229 @var{message} can contain @code{~A} (was @code{%s}) and
230 @code{~S} (was @code{%S}) escapes. When printed,
231 the escapes are replaced with corresponding members of
232 @var{ARGS}:
233 @code{~A} formats using @code{display} and @code{~S} formats
234 using @code{write}.
235 If @var{destination} is @code{#t}, then use the current output
236 port, if @var{destination} is @code{#f}, then return a string
237 containing the formatted text. Does not add a trailing newline.
238 @end deffn
239
240 @r5index write-char
241 @c docstring begin (texi-doc-string "guile" "write-char")
242 @deffn primitive write-char chr [port]
243 Send character @var{chr} to @var{port}.
244 @end deffn
245
246 @findex fflush
247 @c docstring begin (texi-doc-string "guile" "force-output")
248 @deffn primitive force-output [port]
249 Flush the specified output port, or the current output port if @var{port}
250 is omitted. The current output buffer contents are passed to the
251 underlying port implementation (e.g., in the case of fports, the
252 data will be written to the file and the output buffer will be cleared.)
253 It has no effect on an unbuffered port.
254
255 The return value is unspecified.
256 @end deffn
257
258 @c docstring begin (texi-doc-string "guile" "flush-all-ports")
259 @deffn primitive flush-all-ports
260 Equivalent to calling @code{force-output} on
261 all open output ports. The return value is unspecified.
262 @end deffn
263
264
265 @node Closing
266 @section Closing
267
268 @c docstring begin (texi-doc-string "guile" "close-port")
269 @deffn primitive close-port port
270 Close the specified port object. Returns @code{#t} if it successfully
271 closes a port or @code{#f} if it was already
272 closed. An exception may be raised if an error occurs, for example
273 when flushing buffered output.
274 See also @ref{Ports and File Descriptors, close}, for a procedure
275 which can close file descriptors.
276 @end deffn
277
278 @r5index close-input-port
279 @c docstring begin (texi-doc-string "guile" "close-input-port")
280 @deffn primitive close-input-port port
281 Close the specified input port object. The routine has no effect if
282 the file has already been closed. An exception may be raised if an
283 error occurs. The value returned is unspecified.
284
285 See also @ref{Ports and File Descriptors, close}, for a procedure
286 which can close file descriptors.
287 @end deffn
288
289 @r5index close-output-port
290 @c docstring begin (texi-doc-string "guile" "close-output-port")
291 @deffn primitive close-output-port port
292 Close the specified output port object. The routine has no effect if
293 the file has already been closed. An exception may be raised if an
294 error occurs. The value returned is unspecified.
295
296 See also @ref{Ports and File Descriptors, close}, for a procedure
297 which can close file descriptors.
298 @end deffn
299
300 @c docstring begin (texi-doc-string "guile" "port-closed?")
301 @deffn primitive port-closed? port
302 Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.
303 @end deffn
304
305
306 @node Random Access
307 @section Random Access
308
309 @c ARGFIXME object/fd/port
310 @c docstring begin (texi-doc-string "guile" "seek")
311 @deffn primitive seek object offset whence
312 Sets the current position of @var{fd/port} to the integer @var{offset},
313 which is interpreted according to the value of @var{whence}.
314
315 One of the following variables should be supplied
316 for @var{whence}:
317 @defvar SEEK_SET
318 Seek from the beginning of the file.
319 @end defvar
320 @defvar SEEK_CUR
321 Seek from the current position.
322 @end defvar
323 @defvar SEEK_END
324 Seek from the end of the file.
325 @end defvar
326
327 If @var{fd/port} is a file descriptor, the underlying system call is
328 @code{lseek}. @var{port} may be a string port.
329
330 The value returned is the new position in the file. This means that
331 the current position of a port can be obtained using:
332 @smalllisp
333 (seek port 0 SEEK_CUR)
334 @end smalllisp
335 @end deffn
336
337 @c ARGFIXME object/fd/port
338 @c docstring begin (texi-doc-string "guile" "fseek")
339 @deffn primitive fseek object offset whence
340 Obsolete. Almost the same as seek, above, but the return value is
341 unspecified.
342 @end deffn
343
344 @c ARGFIXME object/fd/port
345 @c docstring begin (texi-doc-string "guile" "ftell")
346 @deffn primitive ftell object
347 Returns an integer representing the current position of @var{fd/port},
348 measured from the beginning. Equivalent to:
349 @smalllisp
350 (seek port 0 SEEK_CUR)
351 @end smalllisp
352 @end deffn
353
354 @findex truncate
355 @findex ftruncate
356 @c ARGFIXME obj/object size/length
357 @c docstring begin (texi-doc-string "guile" "truncate-file")
358 @deffn primitive truncate-file object [length]
359 Truncates the object referred to by @var{obj} to at most @var{size} bytes.
360 @var{obj} can be a string containing a file name or an integer file
361 descriptor or a port. @var{size} may be omitted if @var{obj} is not
362 a file name, in which case the truncation occurs at the current port.
363 position.
364
365 The return value is unspecified.
366 @end deffn
367
368
369 @node Line/Delimited
370 @section Handling Line Oriented and Delimited Text
371
372 [Line-oriented and delimited IO. Or should this be merged into the
373 previous two sections?]
374
375 Extended I/O procedures are available which read or write lines of text
376 or read text delimited by a specified set of characters.
377
378 @findex fwrite
379 @findex fread
380 Interfaces to @code{read}/@code{fread} and @code{write}/@code{fwrite} are
381 also available, as @code{uniform-array-read!} and @code{uniform-array-write!},
382 @ref{Uniform Arrays}.
383
384 @c begin (scm-doc-string "boot-9.scm" "read-line")
385 @deffn procedure read-line [port] [handle-delim]
386 Return a line of text from @var{port} if specified, otherwise from the
387 value returned by @code{(current-input-port)}. Under Unix, a line of text
388 is terminated by the first end-of-line character or by end-of-file.
389
390 If @var{handle-delim} is specified, it should be one of the following
391 symbols:
392 @table @code
393 @item trim
394 Discard the terminating delimiter. This is the default, but it will
395 be impossible to tell whether the read terminated with a delimiter or
396 end-of-file.
397 @item concat
398 Append the terminating delimiter (if any) to the returned string.
399 @item peek
400 Push the terminating delimiter (if any) back on to the port.
401 @item split
402 Return a pair containing the string read from the port and the
403 terminating delimiter or end-of-file object.
404
405 NOTE: if the scsh module is loaded then
406 multiple values are returned instead of a pair.
407 @end table
408 @end deffn
409
410 @c begin (scm-doc-string "boot-9.scm" "read-line!")
411 @deffn procedure read-line! buf [port]
412 Read a line of text into the supplied string @var{buf} and return the
413 number of characters added to @var{buf}. If @var{buf} is filled, then
414 @code{#f} is returned.
415 Read from @var{port} if
416 specified, otherwise from the value returned by @code{(current-input-port)}.
417 @end deffn
418
419 @c begin (scm-doc-string "boot-9.scm" "read-delimited")
420 @deffn procedure read-delimited delims [port] [handle-delim]
421 Read text until one of the characters in the string @var{delims} is found
422 or end-of-file is reached. Read from @var{port} if supplied, otherwise
423 from the value returned by @code{(current-input-port)}.
424 @var{handle-delim} takes the same values as described for @code{read-line}.
425
426 NOTE: if the scsh module is loaded then @var{delims} must be an scsh
427 char-set, not a string.
428 @end deffn
429
430 @c begin (scm-doc-string "boot-9.scm" "read-delimited!")
431 @deffn procedure read-delimited! delims buf [port] [handle-delim] [start] [end]
432 Read text into the supplied string @var{buf} and return the number of
433 characters added to @var{buf} (subject to @var{handle-delim}, which takes
434 the same values specified for @code{read-line}. If @var{buf} is filled,
435 @code{#f} is returned for both the number of characters read and the
436 delimiter. Also terminates if one of the characters in the string
437 @var{delims} is found
438 or end-of-file is reached. Read from @var{port} if supplied, otherwise
439 from the value returned by @code{(current-input-port)}.
440
441 NOTE: if the scsh module is loaded then @var{delims} must be an scsh
442 char-set, not a string.
443 @end deffn
444
445 @c docstring begin (texi-doc-string "guile" "write-line")
446 @deffn primitive write-line obj [port]
447 Display @var{obj} and a newline character to @var{port}. If @var{port}
448 is not specified, @code{(current-output-port)} is used. This function
449 is equivalent to:
450
451 @smalllisp
452 (display obj [port])
453 (newline [port])
454 @end smalllisp
455 @end deffn
456
457 Some of the abovementioned I/O functions rely on the following C
458 primitives. These will mainly be of interest to people hacking Guile
459 internals.
460
461 @c ARGFIXME gobble/gobble?
462 @c docstring begin (texi-doc-string "guile" "%read-delimited!")
463 @deffn primitive %read-delimited! delims str gobble [port [start [end]]]
464 Read characters from @var{port} into @var{str} until one of the
465 characters in the @var{delims} string is encountered. If
466 @var{gobble} is true, discard the delimiter character;
467 otherwise, leave it in the input stream for the next read. If
468 @var{port} is not specified, use the value of
469 @code{(current-input-port)}. If @var{start} or @var{end} are
470 specified, store data only into the substring of @var{str}
471 bounded by @var{start} and @var{end} (which default to the
472 beginning and end of the string, respectively).
473 Return a pair consisting of the delimiter that terminated the
474 string and the number of characters read. If reading stopped
475 at the end of file, the delimiter returned is the
476 @var{eof-object}; if the string was filled without encountering
477 a delimiter, this value is @code{#f}.
478 @end deffn
479
480 @c docstring begin (texi-doc-string "guile" "%read-line")
481 @deffn primitive %read-line [port]
482 Read a newline-terminated line from @var{port}, allocating storage as
483 necessary. The newline terminator (if any) is removed from the string,
484 and a pair consisting of the line and its delimiter is returned. The
485 delimiter may be either a newline or the @var{eof-object}; if
486 @code{%read-line} is called at the end of file, it returns the pair
487 @code{(#<eof> . #<eof>)}.
488 @end deffn
489
490 @node Binary IO
491 @section Saving and Restoring Scheme Objects
492
493 @deffn primitive binary-read [port]
494 Read and return an object from @var{port} in a binary format.
495 If omitted, @var{port} defaults to the current output port.
496 @end deffn
497
498 @deffn primitive binary-write obj [port]
499 Write @var{obj} to @var{port} in a binary format.
500 If omitted, @var{port} defaults to the current output port.
501 @end deffn
502
503
504 @node Default Ports
505 @section Default Ports for Input, Output and Errors
506
507 @r5index current-input-port
508 @c docstring begin (texi-doc-string "guile" "current-input-port")
509 @deffn primitive current-input-port
510 Return the current input port. This is the default port used
511 by many input procedures. Initially, @code{current-input-port}
512 returns the @dfn{standard input} in Unix and C terminology.
513 @end deffn
514
515 @r5index current-output-port
516 @c docstring begin (texi-doc-string "guile" "current-output-port")
517 @deffn primitive current-output-port
518 Return the current output port. This is the default port used
519 by many output procedures. Initially,
520 @code{current-output-port} returns the @dfn{standard output} in
521 Unix and C terminology.
522 @end deffn
523
524 @c docstring begin (texi-doc-string "guile" "current-error-port")
525 @deffn primitive current-error-port
526 Return the port to which errors and warnings should be sent (the
527 @dfn{standard error} in Unix and C terminology).
528 @end deffn
529
530 @c docstring begin (texi-doc-string "guile" "set-current-input-port")
531 @deffn primitive set-current-input-port port
532 @deffnx primitive set-current-output-port port
533 @deffnx primitive set-current-error-port port
534 Change the ports returned by @code{current-input-port},
535 @code{current-output-port} and @code{current-error-port}, respectively,
536 so that they use the supplied @var{port} for input or output.
537 @end deffn
538
539 @c docstring begin (texi-doc-string "guile" "set-current-output-port")
540 @deffn primitive set-current-output-port port
541 Set the current default output port to PORT.
542 @end deffn
543
544 @c docstring begin (texi-doc-string "guile" "set-current-error-port")
545 @deffn primitive set-current-error-port port
546 Set the current default error port to PORT.
547 @end deffn
548
549
550 @node Port Types
551 @section Types of Port
552
553 [Types of port; how to make them.]
554
555 @menu
556 * File Ports:: Ports on an operating system file.
557 * String Ports:: Ports on a Scheme string.
558 * Soft Ports:: Ports on arbitrary Scheme procedures.
559 * Void Ports:: Ports on nothing at all.
560 @end menu
561
562
563 @node File Ports
564 @subsection File Ports
565
566 The following procedures are used to open file ports.
567 See also @ref{Ports and File Descriptors, open}, for an interface
568 to the Unix @code{open} system call.
569
570 @c ARGFIXME string/filename mode/modes
571 @c docstring begin (texi-doc-string "guile" "open-file")
572 @deffn primitive open-file filename modes
573 Open the file whose name is @var{string}, and return a port
574 representing that file. The attributes of the port are
575 determined by the @var{mode} string. The way in
576 which this is interpreted is similar to C stdio:
577
578 The first character must be one of the following:
579
580 @table @samp
581 @item r
582 Open an existing file for input.
583 @item w
584 Open a file for output, creating it if it doesn't already exist
585 or removing its contents if it does.
586 @item a
587 Open a file for output, creating it if it doesn't already exist.
588 All writes to the port will go to the end of the file.
589 The "append mode" can be turned off while the port is in use
590 @pxref{Ports and File Descriptors, fcntl}
591 @end table
592
593 The following additional characters can be appended:
594
595 @table @samp
596 @item +
597 Open the port for both input and output. E.g., @code{r+}: open
598 an existing file for both input and output.
599 @item 0
600 Create an "unbuffered" port. In this case input and output operations
601 are passed directly to the underlying port implementation without
602 additional buffering. This is likely to slow down I/O operations.
603 The buffering mode can be changed while a port is in use
604 @pxref{Ports and File Descriptors, setvbuf}
605 @item l
606 Add line-buffering to the port. The port output buffer will be
607 automatically flushed whenever a newline character is written.
608 @end table
609
610 In theory we could create read/write ports which were buffered in one
611 direction only. However this isn't included in the current interfaces.
612
613 If a file cannot be opened with the access requested,
614 @code{open-file} throws an exception.
615 @end deffn
616
617 @r5index open-input-file
618 @c begin (scm-doc-string "r4rs.scm" "open-input-file")
619 @deffn procedure open-input-file filename
620 Open @var{filename} for input. Equivalent to
621 @smalllisp
622 (open-file @var{filename} "r")
623 @end smalllisp
624 @end deffn
625
626 @r5index open-output-file
627 @c begin (scm-doc-string "r4rs.scm" "open-output-file")
628 @deffn procedure open-output-file filename
629 Open @var{filename} for output. Equivalent to
630 @smalllisp
631 (open-file @var{filename} "w")
632 @end smalllisp
633 @end deffn
634
635 @r5index call-with-input-file
636 @c begin (scm-doc-string "r4rs.scm" "call-with-input-file")
637 @deffn procedure call-with-input-file file proc
638 @var{proc} should be a procedure of one argument, and @var{file} should
639 be a string naming a file. The file must already exist. These
640 procedures call @var{proc} with one argument: the port obtained by
641 opening the named file for input or output. If the file cannot be
642 opened, an error is signalled. If the procedure returns, then the port
643 is closed automatically and the value yielded by the procedure is
644 returned. If the procedure does not return, then the port will not be
645 closed automatically unless it is possible to prove that the port will
646 never again be used for a read or write operation.
647 @end deffn
648
649 @r5index call-with-output-file
650 @c begin (scm-doc-string "r4rs.scm" "call-with-output-file")
651 @deffn procedure call-with-output-file file proc
652 @var{proc} should be a procedure of one argument, and @var{file} should
653 be a string naming a file. The behaviour is unspecified if the file
654 already exists. These procedures call @var{proc} with one argument: the
655 port obtained by opening the named file for input or output. If the
656 file cannot be opened, an error is signalled. If the procedure returns,
657 then the port is closed automatically and the value yielded by the
658 procedure is returned. If the procedure does not return, then the port
659 will not be closed automatically unless it is possible to prove that the
660 port will never again be used for a read or write operation.
661 @end deffn
662
663 @r5index with-input-from-file
664 @c begin (scm-doc-string "r4rs.scm" "with-input-from-file")
665 @deffn procedure with-input-from-file file thunk
666 @var{thunk} must be a procedure of no arguments, and @var{file} must be
667 a string naming a file. The file must already exist. The file is opened
668 for input, an input port connected to it is made the default value
669 returned by @code{current-input-port}, and the @var{thunk} is called
670 with no arguments. When the @var{thunk} returns, the port is closed and
671 the previous default is restored. Returns the value yielded by
672 @var{thunk}. If an escape procedure is used to escape from the
673 continuation of these procedures, their behavior is implementation
674 dependent.
675 @end deffn
676
677 @r5index with-output-to-file
678 @c begin (scm-doc-string "r4rs.scm" "with-output-to-file")
679 @deffn procedure with-output-to-file file thunk
680 @var{thunk} must be a procedure of no arguments, and @var{file} must be
681 a string naming a file. The effect is unspecified if the file already
682 exists. The file is opened for output, an output port connected to it
683 is made the default value returned by @code{current-output-port}, and
684 the @var{thunk} is called with no arguments. When the @var{thunk}
685 returns, the port is closed and the previous default is restored.
686 Returns the value yielded by @var{thunk}. If an escape procedure is
687 used to escape from the continuation of these procedures, their behavior
688 is implementation dependent.
689 @end deffn
690
691 @c begin (scm-doc-string "r4rs.scm" "with-error-to-file")
692 @deffn procedure with-error-to-file file thunk
693 @var{thunk} must be a procedure of no arguments, and @var{file} must be
694 a string naming a file. The effect is unspecified if the file already
695 exists. The file is opened for output, an output port connected to it
696 is made the default value returned by @code{current-error-port}, and the
697 @var{thunk} is called with no arguments. When the @var{thunk} returns,
698 the port is closed and the previous default is restored. Returns the
699 value yielded by @var{thunk}. If an escape procedure is used to escape
700 from the continuation of these procedures, their behavior is
701 implementation dependent.
702 @end deffn
703
704 @c docstring begin (texi-doc-string "guile" "port-mode")
705 @deffn primitive port-mode port
706 Returns the port modes associated with the open port @var{port}. These
707 will not necessarily be identical to the modes used when the port was
708 opened, since modes such as "append" which are used only during
709 port creation are not retained.
710 @end deffn
711
712 @c docstring begin (texi-doc-string "guile" "port-filename")
713 @deffn primitive port-filename port
714 Return the filename associated with @var{port}. This function returns
715 the strings "standard input", "standard output" and "standard error"
716 when called on the current input, output and error ports respectively.
717 @end deffn
718
719 @c docstring begin (texi-doc-string "guile" "set-port-filename!")
720 @deffn primitive set-port-filename! port filename
721 Change the filename associated with @var{port}, using the current input
722 port if none is specified. Note that this does not change the port's
723 source of data, but only the value that is returned by
724 @code{port-filename} and reported in diagnostic output.
725 @end deffn
726
727 @deffn primitive file-port? obj
728 Determine whether @var{obj} is a port that is related to a file.
729 @end deffn
730
731
732 @node String Ports
733 @subsection String Ports
734
735 The following allow string ports to be opened by analogy to R4R*
736 file port facilities:
737
738 @c docstring begin (texi-doc-string "guile" "call-with-output-string")
739 @deffn primitive call-with-output-string proc
740 Calls the one-argument procedure @var{proc} with a newly created output
741 port. When the function returns, the string composed of the characters
742 written into the port is returned.
743 @end deffn
744
745 @c ARGFIXME str/string
746 @c docstring begin (texi-doc-string "guile" "call-with-input-string")
747 @deffn primitive call-with-input-string str proc
748 Calls the one-argument procedure @var{proc} with a newly created input
749 port from which @var{string}'s contents may be read. The value yielded
750 by the @var{proc} is returned.
751 @end deffn
752
753 @c begin (scm-doc-string "r4rs.scm" "with-output-to-string")
754 @deffn procedure with-output-to-string thunk
755 Calls the zero-argument procedure @var{thunk} with the current output
756 port set temporarily to a new string port. It returns a string
757 composed of the characters written to the current output.
758 @end deffn
759
760 @c begin (scm-doc-string "r4rs.scm" "with-input-from-string")
761 @deffn procedure with-input-from-string string thunk
762 Calls the zero-argument procedure @var{thunk} with the current input
763 port set temporarily to a string port opened on the specified
764 @var{string}. The value yielded by @var{thunk} is returned.
765 @end deffn
766
767 @c docstring begin (texi-doc-string "guile" "open-input-string")
768 @deffn primitive open-input-string str
769 Takes a string and returns an input port that delivers
770 characters from the string. The port can be closed by
771 @code{close-input-port}, though its storage will be reclaimed
772 by the garbage collector if it becomes inaccessible.
773 @end deffn
774
775 @c docstring begin (texi-doc-string "guile" "open-output-string")
776 @deffn primitive open-output-string
777 Returns an output port that will accumulate characters for
778 retrieval by @code{get-output-string}. The port can be closed
779 by the procedure @code{close-output-port}, though its storage
780 will be reclaimed by the garbage collector if it becomes
781 inaccessible.
782 @end deffn
783
784 @c docstring begin (texi-doc-string "guile" "get-output-string")
785 @deffn primitive get-output-string port
786 Given an output port created by @code{open-output-string},
787 returns a string consisting of the characters that have been
788 output to the port so far.
789 @end deffn
790
791 A string port can be used in many procedures which accept a port
792 but which are not dependent on implementation details of fports.
793 E.g., seeking and truncating will work on a string port,
794 but trying to extract the file descriptor number will fail.
795
796
797 @node Soft Ports
798 @subsection Soft Ports
799
800 A @dfn{soft-port} is a port based on a vector of procedures capable of
801 accepting or delivering characters. It allows emulation of I/O ports.
802
803 @c ARGFIXME pv/vector
804 @c docstring begin (texi-doc-string "guile" "make-soft-port")
805 @deffn primitive make-soft-port pv modes
806 Returns a port capable of receiving or delivering characters as
807 specified by the @var{modes} string (@pxref{File Ports,
808 open-file}). @var{vector} must be a vector of length 6. Its components
809 are as follows:
810
811 @enumerate 0
812 @item
813 procedure accepting one character for output
814 @item
815 procedure accepting a string for output
816 @item
817 thunk for flushing output
818 @item
819 thunk for getting one character
820 @item
821 thunk for closing port (not by garbage collection)
822 @end enumerate
823
824 For an output-only port only elements 0, 1, 2, and 4 need be
825 procedures. For an input-only port only elements 3 and 4 need be
826 procedures. Thunks 2 and 4 can instead be @code{#f} if there is no useful
827 operation for them to perform.
828
829 If thunk 3 returns @code{#f} or an @code{eof-object} (@pxref{Input,
830 eof-object?, ,r4rs, The Revised^4 Report on Scheme}) it indicates that
831 the port has reached end-of-file. For example:
832
833 @example
834 (define stdout (current-output-port))
835 (define p (make-soft-port
836 (vector
837 (lambda (c) (write c stdout))
838 (lambda (s) (display s stdout))
839 (lambda () (display "." stdout))
840 (lambda () (char-upcase (read-char)))
841 (lambda () (display "@@" stdout)))
842 "rw"))
843
844 (write p p) @result{} #<input-output: soft 8081e20>
845 @end example
846 @end deffn
847
848
849 @node Void Ports
850 @subsection Void Ports
851
852 This kind of port causes any data to be discarded when written to, and
853 always returns the end-of-file object when read from.
854
855 @c docstring begin (texi-doc-string "guile" "%make-void-port")
856 @deffn primitive %make-void-port mode
857 Create and return a new void port. A void port acts like
858 /dev/null. The @var{mode} argument
859 specifies the input/output modes for this port: see the
860 documentation for @code{open-file} in @ref{File Ports}.
861 @end deffn
862
863
864 @c Local Variables:
865 @c TeX-master: "guile.texi"
866 @c End: