Commit | Line | Data |
---|---|---|
07d83abe MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
7b041912 | 3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2009, 2010 |
07d83abe MV |
4 | @c Free Software Foundation, Inc. |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
07d83abe MV |
7 | @node Input and Output |
8 | @section Input and Output | |
9 | ||
10 | @menu | |
11 | * Ports:: The idea of the port abstraction. | |
12 | * Reading:: Procedures for reading from a port. | |
13 | * Writing:: Procedures for writing to a port. | |
14 | * Closing:: Procedures to close a port. | |
15 | * Random Access:: Moving around a random access port. | |
16 | * Line/Delimited:: Read and write lines or delimited text. | |
17 | * Block Reading and Writing:: Reading and writing blocks of text. | |
18 | * Default Ports:: Defaults for input, output and errors. | |
19 | * Port Types:: Types of port and how to make them. | |
b242715b | 20 | * R6RS I/O Ports:: The R6RS port API. |
07d83abe MV |
21 | * I/O Extensions:: Using and extending ports in C. |
22 | @end menu | |
23 | ||
24 | ||
25 | @node Ports | |
26 | @subsection Ports | |
bf5df489 | 27 | @cindex Port |
07d83abe MV |
28 | |
29 | Sequential input/output in Scheme is represented by operations on a | |
30 | @dfn{port}. This chapter explains the operations that Guile provides | |
31 | for working with ports. | |
32 | ||
33 | Ports are created by opening, for instance @code{open-file} for a file | |
34 | (@pxref{File Ports}). Characters can be read from an input port and | |
35 | written to an output port, or both on an input/output port. A port | |
36 | can be closed (@pxref{Closing}) when no longer required, after which | |
37 | any attempt to read or write is an error. | |
38 | ||
39 | The formal definition of a port is very generic: an input port is | |
40 | simply ``an object which can deliver characters on demand,'' and an | |
41 | output port is ``an object which can accept characters.'' Because | |
42 | this definition is so loose, it is easy to write functions that | |
43 | simulate ports in software. @dfn{Soft ports} and @dfn{string ports} | |
44 | are two interesting and powerful examples of this technique. | |
45 | (@pxref{Soft Ports}, and @ref{String Ports}.) | |
46 | ||
47 | Ports are garbage collected in the usual way (@pxref{Memory | |
48 | Management}), and will be closed at that time if not already closed. | |
28cc8dac | 49 | In this case any errors occurring in the close will not be reported. |
07d83abe MV |
50 | Usually a program will want to explicitly close so as to be sure all |
51 | its operations have been successful. Of course if a program has | |
52 | abandoned something due to an error or other condition then closing | |
53 | problems are probably not of interest. | |
54 | ||
55 | It is strongly recommended that file ports be closed explicitly when | |
56 | no longer required. Most systems have limits on how many files can be | |
57 | open, both on a per-process and a system-wide basis. A program that | |
58 | uses many files should take care not to hit those limits. The same | |
59 | applies to similar system resources such as pipes and sockets. | |
60 | ||
61 | Note that automatic garbage collection is triggered only by memory | |
62 | consumption, not by file or other resource usage, so a program cannot | |
63 | rely on that to keep it away from system limits. An explicit call to | |
64 | @code{gc} can of course be relied on to pick up unreferenced ports. | |
65 | If program flow makes it hard to be certain when to close then this | |
66 | may be an acceptable way to control resource usage. | |
67 | ||
40296bab KR |
68 | All file access uses the ``LFS'' large file support functions when |
69 | available, so files bigger than 2 Gbytes (@math{2^31} bytes) can be | |
70 | read and written on a 32-bit system. | |
71 | ||
28cc8dac MG |
72 | Each port has an associated character encoding that controls how bytes |
73 | read from the port are converted to characters and string and controls | |
74 | how characters and strings written to the port are converted to bytes. | |
75 | When ports are created, they inherit their character encoding from the | |
76 | current locale, but, that can be modified after the port is created. | |
77 | ||
912a8702 MG |
78 | Currently, the ports only work with @emph{non-modal} encodings. Most |
79 | encodings are non-modal, meaning that the conversion of bytes to a | |
80 | string doesn't depend on its context: the same byte sequence will always | |
81 | return the same string. A couple of modal encodings are in common use, | |
82 | like ISO-2022-JP and ISO-2022-KR, and they are not yet supported. | |
83 | ||
28cc8dac MG |
84 | Each port also has an associated conversion strategy: what to do when |
85 | a Guile character can't be converted to the port's encoded character | |
86 | representation for output. There are three possible strategies: to | |
87 | raise an error, to replace the character with a hex escape, or to | |
88 | replace the character with a substitute character. | |
89 | ||
07d83abe MV |
90 | @rnindex input-port? |
91 | @deffn {Scheme Procedure} input-port? x | |
92 | @deffnx {C Function} scm_input_port_p (x) | |
93 | Return @code{#t} if @var{x} is an input port, otherwise return | |
94 | @code{#f}. Any object satisfying this predicate also satisfies | |
95 | @code{port?}. | |
96 | @end deffn | |
97 | ||
98 | @rnindex output-port? | |
99 | @deffn {Scheme Procedure} output-port? x | |
100 | @deffnx {C Function} scm_output_port_p (x) | |
101 | Return @code{#t} if @var{x} is an output port, otherwise return | |
102 | @code{#f}. Any object satisfying this predicate also satisfies | |
103 | @code{port?}. | |
104 | @end deffn | |
105 | ||
106 | @deffn {Scheme Procedure} port? x | |
107 | @deffnx {C Function} scm_port_p (x) | |
108 | Return a boolean indicating whether @var{x} is a port. | |
109 | Equivalent to @code{(or (input-port? @var{x}) (output-port? | |
110 | @var{x}))}. | |
111 | @end deffn | |
112 | ||
28cc8dac MG |
113 | @deffn {Scheme Procedure} set-port-encoding! port enc |
114 | @deffnx {C Function} scm_set_port_encoding_x (port, enc) | |
4c7b9975 LC |
115 | Sets the character encoding that will be used to interpret all port I/O. |
116 | @var{enc} is a string containing the name of an encoding. Valid | |
117 | encoding names are those | |
118 | @url{http://www.iana.org/assignments/character-sets, defined by IANA}. | |
28cc8dac | 119 | @end deffn |
d6a6989e LC |
120 | |
121 | @defvr {Scheme Variable} %default-port-encoding | |
72b3aa56 | 122 | A fluid containing @code{#f} or the name of the encoding to |
d6a6989e LC |
123 | be used by default for newly created ports (@pxref{Fluids and Dynamic |
124 | States}). The value @code{#f} is equivalent to @code{"ISO-8859-1"}. | |
28cc8dac MG |
125 | |
126 | New ports are created with the encoding appropriate for the current | |
4c7b9975 LC |
127 | locale if @code{setlocale} has been called or the value specified by |
128 | this fluid otherwise. | |
129 | @end defvr | |
28cc8dac MG |
130 | |
131 | @deffn {Scheme Procedure} port-encoding port | |
132 | @deffnx {C Function} scm_port_encoding | |
211683cc MG |
133 | Returns, as a string, the character encoding that @var{port} uses to interpret |
134 | its input and output. The value @code{#f} is equivalent to @code{"ISO-8859-1"}. | |
28cc8dac MG |
135 | @end deffn |
136 | ||
137 | @deffn {Scheme Procedure} set-port-conversion-strategy! port sym | |
138 | @deffnx {C Function} scm_set_port_conversion_strategy_x (port, sym) | |
139 | Sets the behavior of the interpreter when outputting a character that | |
140 | is not representable in the port's current encoding. @var{sym} can be | |
141 | either @code{'error}, @code{'substitute}, or @code{'escape}. If it is | |
142 | @code{'error}, an error will be thrown when an nonconvertible character | |
143 | is encountered. If it is @code{'substitute}, then nonconvertible | |
144 | characters will be replaced with approximate characters, or with | |
145 | question marks if no approximately correct character is available. If | |
146 | it is @code{'escape}, it will appear as a hex escape when output. | |
147 | ||
148 | If @var{port} is an open port, the conversion error behavior | |
149 | is set for that port. If it is @code{#f}, it is set as the | |
150 | default behavior for any future ports that get created in | |
151 | this thread. | |
152 | @end deffn | |
153 | ||
154 | @deffn {Scheme Procedure} port-conversion-strategy port | |
155 | @deffnx {C Function} scm_port_conversion_strategy (port) | |
156 | Returns the behavior of the port when outputting a character that is | |
157 | not representable in the port's current encoding. It returns the | |
158 | symbol @code{error} if unrepresentable characters should cause | |
159 | exceptions, @code{substitute} if the port should try to replace | |
160 | unrepresentable characters with question marks or approximate | |
161 | characters, or @code{escape} if unrepresentable characters should be | |
162 | converted to string escapes. | |
163 | ||
164 | If @var{port} is @code{#f}, then the current default behavior will be | |
165 | returned. New ports will have this default behavior when they are | |
166 | created. | |
167 | @end deffn | |
168 | ||
169 | ||
07d83abe MV |
170 | |
171 | @node Reading | |
172 | @subsection Reading | |
bf5df489 | 173 | @cindex Reading |
07d83abe MV |
174 | |
175 | [Generic procedures for reading from ports.] | |
176 | ||
1518f649 AW |
177 | These procedures pertain to reading characters and strings from |
178 | ports. To read general S-expressions from ports, @xref{Scheme Read}. | |
179 | ||
07d83abe | 180 | @rnindex eof-object? |
bf5df489 | 181 | @cindex End of file object |
07d83abe MV |
182 | @deffn {Scheme Procedure} eof-object? x |
183 | @deffnx {C Function} scm_eof_object_p (x) | |
184 | Return @code{#t} if @var{x} is an end-of-file object; otherwise | |
185 | return @code{#f}. | |
186 | @end deffn | |
187 | ||
188 | @rnindex char-ready? | |
189 | @deffn {Scheme Procedure} char-ready? [port] | |
190 | @deffnx {C Function} scm_char_ready_p (port) | |
191 | Return @code{#t} if a character is ready on input @var{port} | |
192 | and return @code{#f} otherwise. If @code{char-ready?} returns | |
193 | @code{#t} then the next @code{read-char} operation on | |
194 | @var{port} is guaranteed not to hang. If @var{port} is a file | |
195 | port at end of file then @code{char-ready?} returns @code{#t}. | |
cdf1ad3b MV |
196 | |
197 | @code{char-ready?} exists to make it possible for a | |
07d83abe MV |
198 | program to accept characters from interactive ports without |
199 | getting stuck waiting for input. Any input editors associated | |
200 | with such ports must make sure that characters whose existence | |
201 | has been asserted by @code{char-ready?} cannot be rubbed out. | |
202 | If @code{char-ready?} were to return @code{#f} at end of file, | |
203 | a port at end of file would be indistinguishable from an | |
cdf1ad3b | 204 | interactive port that has no ready characters. |
07d83abe MV |
205 | @end deffn |
206 | ||
207 | @rnindex read-char | |
208 | @deffn {Scheme Procedure} read-char [port] | |
209 | @deffnx {C Function} scm_read_char (port) | |
210 | Return the next character available from @var{port}, updating | |
211 | @var{port} to point to the following character. If no more | |
212 | characters are available, the end-of-file object is returned. | |
213 | @end deffn | |
214 | ||
215 | @deftypefn {C Function} size_t scm_c_read (SCM port, void *buffer, size_t size) | |
216 | Read up to @var{size} bytes from @var{port} and store them in | |
217 | @var{buffer}. The return value is the number of bytes actually read, | |
218 | which can be less than @var{size} if end-of-file has been reached. | |
219 | ||
220 | Note that this function does not update @code{port-line} and | |
221 | @code{port-column} below. | |
222 | @end deftypefn | |
223 | ||
224 | @rnindex peek-char | |
225 | @deffn {Scheme Procedure} peek-char [port] | |
226 | @deffnx {C Function} scm_peek_char (port) | |
227 | Return the next character available from @var{port}, | |
228 | @emph{without} updating @var{port} to point to the following | |
229 | character. If no more characters are available, the | |
cdf1ad3b MV |
230 | end-of-file object is returned. |
231 | ||
232 | The value returned by | |
07d83abe MV |
233 | a call to @code{peek-char} is the same as the value that would |
234 | have been returned by a call to @code{read-char} on the same | |
235 | port. The only difference is that the very next call to | |
236 | @code{read-char} or @code{peek-char} on that @var{port} will | |
237 | return the value returned by the preceding call to | |
238 | @code{peek-char}. In particular, a call to @code{peek-char} on | |
239 | an interactive port will hang waiting for input whenever a call | |
cdf1ad3b | 240 | to @code{read-char} would have hung. |
07d83abe MV |
241 | @end deffn |
242 | ||
243 | @deffn {Scheme Procedure} unread-char cobj [port] | |
244 | @deffnx {C Function} scm_unread_char (cobj, port) | |
245 | Place @var{char} in @var{port} so that it will be read by the | |
246 | next read operation. If called multiple times, the unread characters | |
247 | will be read again in last-in first-out order. If @var{port} is | |
248 | not supplied, the current input port is used. | |
249 | @end deffn | |
250 | ||
251 | @deffn {Scheme Procedure} unread-string str port | |
252 | @deffnx {C Function} scm_unread_string (str, port) | |
253 | Place the string @var{str} in @var{port} so that its characters will | |
254 | be read from left-to-right as the next characters from @var{port} | |
255 | during subsequent read operations. If called multiple times, the | |
256 | unread characters will be read again in last-in first-out order. If | |
9782da8a | 257 | @var{port} is not supplied, the @code{current-input-port} is used. |
07d83abe MV |
258 | @end deffn |
259 | ||
260 | @deffn {Scheme Procedure} drain-input port | |
261 | @deffnx {C Function} scm_drain_input (port) | |
262 | This procedure clears a port's input buffers, similar | |
263 | to the way that force-output clears the output buffer. The | |
264 | contents of the buffers are returned as a single string, e.g., | |
265 | ||
266 | @lisp | |
267 | (define p (open-input-file ...)) | |
268 | (drain-input p) => empty string, nothing buffered yet. | |
269 | (unread-char (read-char p) p) | |
270 | (drain-input p) => initial chars from p, up to the buffer size. | |
271 | @end lisp | |
272 | ||
273 | Draining the buffers may be useful for cleanly finishing | |
274 | buffered I/O so that the file descriptor can be used directly | |
275 | for further input. | |
276 | @end deffn | |
277 | ||
278 | @deffn {Scheme Procedure} port-column port | |
279 | @deffnx {Scheme Procedure} port-line port | |
280 | @deffnx {C Function} scm_port_column (port) | |
281 | @deffnx {C Function} scm_port_line (port) | |
282 | Return the current column number or line number of @var{port}. | |
283 | If the number is | |
284 | unknown, the result is #f. Otherwise, the result is a 0-origin integer | |
285 | - i.e.@: the first character of the first line is line 0, column 0. | |
286 | (However, when you display a file position, for example in an error | |
287 | message, we recommend you add 1 to get 1-origin integers. This is | |
288 | because lines and column numbers traditionally start with 1, and that is | |
289 | what non-programmers will find most natural.) | |
290 | @end deffn | |
291 | ||
292 | @deffn {Scheme Procedure} set-port-column! port column | |
293 | @deffnx {Scheme Procedure} set-port-line! port line | |
294 | @deffnx {C Function} scm_set_port_column_x (port, column) | |
295 | @deffnx {C Function} scm_set_port_line_x (port, line) | |
296 | Set the current column or line number of @var{port}. | |
297 | @end deffn | |
298 | ||
299 | @node Writing | |
300 | @subsection Writing | |
bf5df489 | 301 | @cindex Writing |
07d83abe MV |
302 | |
303 | [Generic procedures for writing to ports.] | |
304 | ||
1518f649 AW |
305 | These procedures are for writing characters and strings to |
306 | ports. For more information on writing arbitrary Scheme objects to | |
307 | ports, @xref{Scheme Write}. | |
308 | ||
07d83abe MV |
309 | @deffn {Scheme Procedure} get-print-state port |
310 | @deffnx {C Function} scm_get_print_state (port) | |
311 | Return the print state of the port @var{port}. If @var{port} | |
312 | has no associated print state, @code{#f} is returned. | |
313 | @end deffn | |
314 | ||
07d83abe MV |
315 | @rnindex newline |
316 | @deffn {Scheme Procedure} newline [port] | |
317 | @deffnx {C Function} scm_newline (port) | |
318 | Send a newline to @var{port}. | |
319 | If @var{port} is omitted, send to the current output port. | |
320 | @end deffn | |
321 | ||
cdf1ad3b | 322 | @deffn {Scheme Procedure} port-with-print-state port [pstate] |
07d83abe MV |
323 | @deffnx {C Function} scm_port_with_print_state (port, pstate) |
324 | Create a new port which behaves like @var{port}, but with an | |
cdf1ad3b MV |
325 | included print state @var{pstate}. @var{pstate} is optional. |
326 | If @var{pstate} isn't supplied and @var{port} already has | |
327 | a print state, the old print state is reused. | |
07d83abe MV |
328 | @end deffn |
329 | ||
330 | @deffn {Scheme Procedure} print-options-interface [setting] | |
331 | @deffnx {C Function} scm_print_options (setting) | |
332 | Option interface for the print options. Instead of using | |
333 | this procedure directly, use the procedures | |
334 | @code{print-enable}, @code{print-disable}, @code{print-set!} | |
335 | and @code{print-options}. | |
336 | @end deffn | |
337 | ||
338 | @deffn {Scheme Procedure} simple-format destination message . args | |
339 | @deffnx {C Function} scm_simple_format (destination, message, args) | |
340 | Write @var{message} to @var{destination}, defaulting to | |
341 | the current output port. | |
342 | @var{message} can contain @code{~A} (was @code{%s}) and | |
343 | @code{~S} (was @code{%S}) escapes. When printed, | |
344 | the escapes are replaced with corresponding members of | |
345 | @var{ARGS}: | |
346 | @code{~A} formats using @code{display} and @code{~S} formats | |
347 | using @code{write}. | |
348 | If @var{destination} is @code{#t}, then use the current output | |
349 | port, if @var{destination} is @code{#f}, then return a string | |
350 | containing the formatted text. Does not add a trailing newline. | |
351 | @end deffn | |
352 | ||
353 | @rnindex write-char | |
354 | @deffn {Scheme Procedure} write-char chr [port] | |
355 | @deffnx {C Function} scm_write_char (chr, port) | |
356 | Send character @var{chr} to @var{port}. | |
357 | @end deffn | |
358 | ||
359 | @deftypefn {C Function} void scm_c_write (SCM port, const void *buffer, size_t size) | |
360 | Write @var{size} bytes at @var{buffer} to @var{port}. | |
361 | ||
362 | Note that this function does not update @code{port-line} and | |
363 | @code{port-column} (@pxref{Reading}). | |
364 | @end deftypefn | |
365 | ||
366 | @findex fflush | |
367 | @deffn {Scheme Procedure} force-output [port] | |
368 | @deffnx {C Function} scm_force_output (port) | |
369 | Flush the specified output port, or the current output port if @var{port} | |
370 | is omitted. The current output buffer contents are passed to the | |
371 | underlying port implementation (e.g., in the case of fports, the | |
372 | data will be written to the file and the output buffer will be cleared.) | |
373 | It has no effect on an unbuffered port. | |
374 | ||
375 | The return value is unspecified. | |
376 | @end deffn | |
377 | ||
378 | @deffn {Scheme Procedure} flush-all-ports | |
379 | @deffnx {C Function} scm_flush_all_ports () | |
380 | Equivalent to calling @code{force-output} on | |
381 | all open output ports. The return value is unspecified. | |
382 | @end deffn | |
383 | ||
384 | ||
385 | @node Closing | |
386 | @subsection Closing | |
bf5df489 KR |
387 | @cindex Closing ports |
388 | @cindex Port, close | |
07d83abe MV |
389 | |
390 | @deffn {Scheme Procedure} close-port port | |
391 | @deffnx {C Function} scm_close_port (port) | |
392 | Close the specified port object. Return @code{#t} if it | |
393 | successfully closes a port or @code{#f} if it was already | |
394 | closed. An exception may be raised if an error occurs, for | |
395 | example when flushing buffered output. See also @ref{Ports and | |
396 | File Descriptors, close}, for a procedure which can close file | |
397 | descriptors. | |
398 | @end deffn | |
399 | ||
400 | @deffn {Scheme Procedure} close-input-port port | |
401 | @deffnx {Scheme Procedure} close-output-port port | |
402 | @deffnx {C Function} scm_close_input_port (port) | |
403 | @deffnx {C Function} scm_close_output_port (port) | |
404 | @rnindex close-input-port | |
405 | @rnindex close-output-port | |
406 | Close the specified input or output @var{port}. An exception may be | |
407 | raised if an error occurs while closing. If @var{port} is already | |
408 | closed, nothing is done. The return value is unspecified. | |
409 | ||
410 | See also @ref{Ports and File Descriptors, close}, for a procedure | |
411 | which can close file descriptors. | |
412 | @end deffn | |
413 | ||
414 | @deffn {Scheme Procedure} port-closed? port | |
415 | @deffnx {C Function} scm_port_closed_p (port) | |
416 | Return @code{#t} if @var{port} is closed or @code{#f} if it is | |
417 | open. | |
418 | @end deffn | |
419 | ||
420 | ||
421 | @node Random Access | |
422 | @subsection Random Access | |
bf5df489 KR |
423 | @cindex Random access, ports |
424 | @cindex Port, random access | |
07d83abe MV |
425 | |
426 | @deffn {Scheme Procedure} seek fd_port offset whence | |
427 | @deffnx {C Function} scm_seek (fd_port, offset, whence) | |
428 | Sets the current position of @var{fd/port} to the integer | |
429 | @var{offset}, which is interpreted according to the value of | |
430 | @var{whence}. | |
431 | ||
432 | One of the following variables should be supplied for | |
433 | @var{whence}: | |
434 | @defvar SEEK_SET | |
435 | Seek from the beginning of the file. | |
436 | @end defvar | |
437 | @defvar SEEK_CUR | |
438 | Seek from the current position. | |
439 | @end defvar | |
440 | @defvar SEEK_END | |
441 | Seek from the end of the file. | |
442 | @end defvar | |
443 | If @var{fd/port} is a file descriptor, the underlying system | |
444 | call is @code{lseek}. @var{port} may be a string port. | |
445 | ||
446 | The value returned is the new position in the file. This means | |
447 | that the current position of a port can be obtained using: | |
448 | @lisp | |
449 | (seek port 0 SEEK_CUR) | |
450 | @end lisp | |
451 | @end deffn | |
452 | ||
453 | @deffn {Scheme Procedure} ftell fd_port | |
454 | @deffnx {C Function} scm_ftell (fd_port) | |
455 | Return an integer representing the current position of | |
456 | @var{fd/port}, measured from the beginning. Equivalent to: | |
457 | ||
458 | @lisp | |
459 | (seek port 0 SEEK_CUR) | |
460 | @end lisp | |
461 | @end deffn | |
462 | ||
463 | @findex truncate | |
464 | @findex ftruncate | |
40296bab KR |
465 | @deffn {Scheme Procedure} truncate-file file [length] |
466 | @deffnx {C Function} scm_truncate_file (file, length) | |
467 | Truncate @var{file} to @var{length} bytes. @var{file} can be a | |
468 | filename string, a port object, or an integer file descriptor. The | |
469 | return value is unspecified. | |
470 | ||
471 | For a port or file descriptor @var{length} can be omitted, in which | |
472 | case the file is truncated at the current position (per @code{ftell} | |
473 | above). | |
474 | ||
475 | On most systems a file can be extended by giving a length greater than | |
476 | the current size, but this is not mandatory in the POSIX standard. | |
07d83abe MV |
477 | @end deffn |
478 | ||
479 | @node Line/Delimited | |
480 | @subsection Line Oriented and Delimited Text | |
bf5df489 KR |
481 | @cindex Line input/output |
482 | @cindex Port, line input/output | |
07d83abe MV |
483 | |
484 | The delimited-I/O module can be accessed with: | |
485 | ||
aba0dff5 | 486 | @lisp |
07d83abe | 487 | (use-modules (ice-9 rdelim)) |
aba0dff5 | 488 | @end lisp |
07d83abe MV |
489 | |
490 | It can be used to read or write lines of text, or read text delimited by | |
491 | a specified set of characters. It's similar to the @code{(scsh rdelim)} | |
492 | module from guile-scsh, but does not use multiple values or character | |
493 | sets and has an extra procedure @code{write-line}. | |
494 | ||
495 | @c begin (scm-doc-string "rdelim.scm" "read-line") | |
496 | @deffn {Scheme Procedure} read-line [port] [handle-delim] | |
497 | Return a line of text from @var{port} if specified, otherwise from the | |
498 | value returned by @code{(current-input-port)}. Under Unix, a line of text | |
499 | is terminated by the first end-of-line character or by end-of-file. | |
500 | ||
501 | If @var{handle-delim} is specified, it should be one of the following | |
502 | symbols: | |
503 | @table @code | |
504 | @item trim | |
505 | Discard the terminating delimiter. This is the default, but it will | |
506 | be impossible to tell whether the read terminated with a delimiter or | |
507 | end-of-file. | |
508 | @item concat | |
509 | Append the terminating delimiter (if any) to the returned string. | |
510 | @item peek | |
511 | Push the terminating delimiter (if any) back on to the port. | |
512 | @item split | |
513 | Return a pair containing the string read from the port and the | |
514 | terminating delimiter or end-of-file object. | |
515 | @end table | |
516 | @end deffn | |
517 | ||
518 | @c begin (scm-doc-string "rdelim.scm" "read-line!") | |
519 | @deffn {Scheme Procedure} read-line! buf [port] | |
520 | Read a line of text into the supplied string @var{buf} and return the | |
521 | number of characters added to @var{buf}. If @var{buf} is filled, then | |
522 | @code{#f} is returned. | |
523 | Read from @var{port} if | |
524 | specified, otherwise from the value returned by @code{(current-input-port)}. | |
525 | @end deffn | |
526 | ||
527 | @c begin (scm-doc-string "rdelim.scm" "read-delimited") | |
528 | @deffn {Scheme Procedure} read-delimited delims [port] [handle-delim] | |
529 | Read text until one of the characters in the string @var{delims} is found | |
530 | or end-of-file is reached. Read from @var{port} if supplied, otherwise | |
531 | from the value returned by @code{(current-input-port)}. | |
532 | @var{handle-delim} takes the same values as described for @code{read-line}. | |
533 | @end deffn | |
534 | ||
535 | @c begin (scm-doc-string "rdelim.scm" "read-delimited!") | |
536 | @deffn {Scheme Procedure} read-delimited! delims buf [port] [handle-delim] [start] [end] | |
e7fb779f AW |
537 | Read text into the supplied string @var{buf}. |
538 | ||
539 | If a delimiter was found, return the number of characters written, | |
540 | except if @var{handle-delim} is @code{split}, in which case the return | |
541 | value is a pair, as noted above. | |
542 | ||
543 | As a special case, if @var{port} was already at end-of-stream, the EOF | |
544 | object is returned. Also, if no characters were written because the | |
545 | buffer was full, @code{#f} is returned. | |
546 | ||
547 | It's something of a wacky interface, to be honest. | |
07d83abe MV |
548 | @end deffn |
549 | ||
550 | @deffn {Scheme Procedure} write-line obj [port] | |
551 | @deffnx {C Function} scm_write_line (obj, port) | |
552 | Display @var{obj} and a newline character to @var{port}. If | |
553 | @var{port} is not specified, @code{(current-output-port)} is | |
554 | used. This function is equivalent to: | |
555 | @lisp | |
556 | (display obj [port]) | |
557 | (newline [port]) | |
558 | @end lisp | |
559 | @end deffn | |
560 | ||
28cc8dac | 561 | Some of the aforementioned I/O functions rely on the following C |
07d83abe MV |
562 | primitives. These will mainly be of interest to people hacking Guile |
563 | internals. | |
564 | ||
565 | @deffn {Scheme Procedure} %read-delimited! delims str gobble [port [start [end]]] | |
566 | @deffnx {C Function} scm_read_delimited_x (delims, str, gobble, port, start, end) | |
567 | Read characters from @var{port} into @var{str} until one of the | |
568 | characters in the @var{delims} string is encountered. If | |
569 | @var{gobble} is true, discard the delimiter character; | |
570 | otherwise, leave it in the input stream for the next read. If | |
571 | @var{port} is not specified, use the value of | |
572 | @code{(current-input-port)}. If @var{start} or @var{end} are | |
573 | specified, store data only into the substring of @var{str} | |
574 | bounded by @var{start} and @var{end} (which default to the | |
575 | beginning and end of the string, respectively). | |
576 | ||
577 | Return a pair consisting of the delimiter that terminated the | |
578 | string and the number of characters read. If reading stopped | |
579 | at the end of file, the delimiter returned is the | |
580 | @var{eof-object}; if the string was filled without encountering | |
581 | a delimiter, this value is @code{#f}. | |
582 | @end deffn | |
583 | ||
584 | @deffn {Scheme Procedure} %read-line [port] | |
585 | @deffnx {C Function} scm_read_line (port) | |
586 | Read a newline-terminated line from @var{port}, allocating storage as | |
587 | necessary. The newline terminator (if any) is removed from the string, | |
588 | and a pair consisting of the line and its delimiter is returned. The | |
589 | delimiter may be either a newline or the @var{eof-object}; if | |
590 | @code{%read-line} is called at the end of file, it returns the pair | |
591 | @code{(#<eof> . #<eof>)}. | |
592 | @end deffn | |
593 | ||
594 | @node Block Reading and Writing | |
595 | @subsection Block reading and writing | |
bf5df489 KR |
596 | @cindex Block read/write |
597 | @cindex Port, block read/write | |
07d83abe MV |
598 | |
599 | The Block-string-I/O module can be accessed with: | |
600 | ||
aba0dff5 | 601 | @lisp |
07d83abe | 602 | (use-modules (ice-9 rw)) |
aba0dff5 | 603 | @end lisp |
07d83abe MV |
604 | |
605 | It currently contains procedures that help to implement the | |
606 | @code{(scsh rw)} module in guile-scsh. | |
607 | ||
608 | @deffn {Scheme Procedure} read-string!/partial str [port_or_fdes [start [end]]] | |
609 | @deffnx {C Function} scm_read_string_x_partial (str, port_or_fdes, start, end) | |
610 | Read characters from a port or file descriptor into a | |
611 | string @var{str}. A port must have an underlying file | |
612 | descriptor --- a so-called fport. This procedure is | |
613 | scsh-compatible and can efficiently read large strings. | |
614 | It will: | |
615 | ||
616 | @itemize | |
617 | @item | |
618 | attempt to fill the entire string, unless the @var{start} | |
619 | and/or @var{end} arguments are supplied. i.e., @var{start} | |
620 | defaults to 0 and @var{end} defaults to | |
621 | @code{(string-length str)} | |
622 | @item | |
623 | use the current input port if @var{port_or_fdes} is not | |
624 | supplied. | |
625 | @item | |
626 | return fewer than the requested number of characters in some | |
627 | cases, e.g., on end of file, if interrupted by a signal, or if | |
628 | not all the characters are immediately available. | |
629 | @item | |
630 | wait indefinitely for some input if no characters are | |
631 | currently available, | |
632 | unless the port is in non-blocking mode. | |
633 | @item | |
634 | read characters from the port's input buffers if available, | |
635 | instead from the underlying file descriptor. | |
636 | @item | |
637 | return @code{#f} if end-of-file is encountered before reading | |
638 | any characters, otherwise return the number of characters | |
639 | read. | |
640 | @item | |
641 | return 0 if the port is in non-blocking mode and no characters | |
642 | are immediately available. | |
643 | @item | |
644 | return 0 if the request is for 0 bytes, with no | |
645 | end-of-file check. | |
646 | @end itemize | |
647 | @end deffn | |
648 | ||
649 | @deffn {Scheme Procedure} write-string/partial str [port_or_fdes [start [end]]] | |
650 | @deffnx {C Function} scm_write_string_partial (str, port_or_fdes, start, end) | |
651 | Write characters from a string @var{str} to a port or file | |
652 | descriptor. A port must have an underlying file descriptor | |
653 | --- a so-called fport. This procedure is | |
654 | scsh-compatible and can efficiently write large strings. | |
655 | It will: | |
656 | ||
657 | @itemize | |
658 | @item | |
659 | attempt to write the entire string, unless the @var{start} | |
660 | and/or @var{end} arguments are supplied. i.e., @var{start} | |
661 | defaults to 0 and @var{end} defaults to | |
662 | @code{(string-length str)} | |
663 | @item | |
664 | use the current output port if @var{port_of_fdes} is not | |
665 | supplied. | |
666 | @item | |
667 | in the case of a buffered port, store the characters in the | |
668 | port's output buffer, if all will fit. If they will not fit | |
669 | then any existing buffered characters will be flushed | |
670 | before attempting | |
671 | to write the new characters directly to the underlying file | |
672 | descriptor. If the port is in non-blocking mode and | |
673 | buffered characters can not be flushed immediately, then an | |
674 | @code{EAGAIN} system-error exception will be raised (Note: | |
675 | scsh does not support the use of non-blocking buffered ports.) | |
676 | @item | |
677 | write fewer than the requested number of | |
678 | characters in some cases, e.g., if interrupted by a signal or | |
679 | if not all of the output can be accepted immediately. | |
680 | @item | |
681 | wait indefinitely for at least one character | |
682 | from @var{str} to be accepted by the port, unless the port is | |
683 | in non-blocking mode. | |
684 | @item | |
685 | return the number of characters accepted by the port. | |
686 | @item | |
687 | return 0 if the port is in non-blocking mode and can not accept | |
688 | at least one character from @var{str} immediately | |
689 | @item | |
690 | return 0 immediately if the request size is 0 bytes. | |
691 | @end itemize | |
692 | @end deffn | |
693 | ||
694 | @node Default Ports | |
695 | @subsection Default Ports for Input, Output and Errors | |
bf5df489 KR |
696 | @cindex Default ports |
697 | @cindex Port, default | |
07d83abe MV |
698 | |
699 | @rnindex current-input-port | |
700 | @deffn {Scheme Procedure} current-input-port | |
701 | @deffnx {C Function} scm_current_input_port () | |
34846414 | 702 | @cindex standard input |
07d83abe | 703 | Return the current input port. This is the default port used |
3fa0a042 KR |
704 | by many input procedures. |
705 | ||
706 | Initially this is the @dfn{standard input} in Unix and C terminology. | |
707 | When the standard input is a tty the port is unbuffered, otherwise | |
708 | it's fully buffered. | |
709 | ||
710 | Unbuffered input is good if an application runs an interactive | |
711 | subprocess, since any type-ahead input won't go into Guile's buffer | |
9782da8a | 712 | and be unavailable to the subprocess. |
3fa0a042 KR |
713 | |
714 | Note that Guile buffering is completely separate from the tty ``line | |
9782da8a KR |
715 | discipline''. In the usual cooked mode on a tty Guile only sees a |
716 | line of input once the user presses @key{Return}. | |
07d83abe MV |
717 | @end deffn |
718 | ||
719 | @rnindex current-output-port | |
720 | @deffn {Scheme Procedure} current-output-port | |
721 | @deffnx {C Function} scm_current_output_port () | |
34846414 | 722 | @cindex standard output |
07d83abe | 723 | Return the current output port. This is the default port used |
3fa0a042 KR |
724 | by many output procedures. |
725 | ||
726 | Initially this is the @dfn{standard output} in Unix and C terminology. | |
727 | When the standard output is a tty this port is unbuffered, otherwise | |
728 | it's fully buffered. | |
729 | ||
730 | Unbuffered output to a tty is good for ensuring progress output or a | |
731 | prompt is seen. But an application which always prints whole lines | |
732 | could change to line buffered, or an application with a lot of output | |
733 | could go fully buffered and perhaps make explicit @code{force-output} | |
734 | calls (@pxref{Writing}) at selected points. | |
07d83abe MV |
735 | @end deffn |
736 | ||
737 | @deffn {Scheme Procedure} current-error-port | |
738 | @deffnx {C Function} scm_current_error_port () | |
34846414 | 739 | @cindex standard error output |
3fa0a042 KR |
740 | Return the port to which errors and warnings should be sent. |
741 | ||
742 | Initially this is the @dfn{standard error} in Unix and C terminology. | |
743 | When the standard error is a tty this port is unbuffered, otherwise | |
744 | it's fully buffered. | |
07d83abe MV |
745 | @end deffn |
746 | ||
747 | @deffn {Scheme Procedure} set-current-input-port port | |
748 | @deffnx {Scheme Procedure} set-current-output-port port | |
749 | @deffnx {Scheme Procedure} set-current-error-port port | |
750 | @deffnx {C Function} scm_set_current_input_port (port) | |
751 | @deffnx {C Function} scm_set_current_output_port (port) | |
752 | @deffnx {C Function} scm_set_current_error_port (port) | |
753 | Change the ports returned by @code{current-input-port}, | |
754 | @code{current-output-port} and @code{current-error-port}, respectively, | |
755 | so that they use the supplied @var{port} for input or output. | |
756 | @end deffn | |
757 | ||
661ae7ab MV |
758 | @deftypefn {C Function} void scm_dynwind_current_input_port (SCM port) |
759 | @deftypefnx {C Function} void scm_dynwind_current_output_port (SCM port) | |
760 | @deftypefnx {C Function} void scm_dynwind_current_error_port (SCM port) | |
07d83abe | 761 | These functions must be used inside a pair of calls to |
661ae7ab MV |
762 | @code{scm_dynwind_begin} and @code{scm_dynwind_end} (@pxref{Dynamic |
763 | Wind}). During the dynwind context, the indicated port is set to | |
07d83abe MV |
764 | @var{port}. |
765 | ||
766 | More precisely, the current port is swapped with a `backup' value | |
661ae7ab | 767 | whenever the dynwind context is entered or left. The backup value is |
07d83abe MV |
768 | initialized with the @var{port} argument. |
769 | @end deftypefn | |
770 | ||
771 | @node Port Types | |
772 | @subsection Types of Port | |
bf5df489 KR |
773 | @cindex Types of ports |
774 | @cindex Port, types | |
07d83abe MV |
775 | |
776 | [Types of port; how to make them.] | |
777 | ||
778 | @menu | |
779 | * File Ports:: Ports on an operating system file. | |
780 | * String Ports:: Ports on a Scheme string. | |
781 | * Soft Ports:: Ports on arbitrary Scheme procedures. | |
782 | * Void Ports:: Ports on nothing at all. | |
783 | @end menu | |
784 | ||
785 | ||
786 | @node File Ports | |
787 | @subsubsection File Ports | |
bf5df489 KR |
788 | @cindex File port |
789 | @cindex Port, file | |
07d83abe MV |
790 | |
791 | The following procedures are used to open file ports. | |
792 | See also @ref{Ports and File Descriptors, open}, for an interface | |
793 | to the Unix @code{open} system call. | |
794 | ||
795 | Most systems have limits on how many files can be open, so it's | |
796 | strongly recommended that file ports be closed explicitly when no | |
797 | longer required (@pxref{Ports}). | |
798 | ||
799 | @deffn {Scheme Procedure} open-file filename mode | |
800 | @deffnx {C Function} scm_open_file (filename, mode) | |
801 | Open the file whose name is @var{filename}, and return a port | |
802 | representing that file. The attributes of the port are | |
803 | determined by the @var{mode} string. The way in which this is | |
804 | interpreted is similar to C stdio. The first character must be | |
805 | one of the following: | |
c755b861 | 806 | |
07d83abe MV |
807 | @table @samp |
808 | @item r | |
809 | Open an existing file for input. | |
810 | @item w | |
811 | Open a file for output, creating it if it doesn't already exist | |
812 | or removing its contents if it does. | |
813 | @item a | |
814 | Open a file for output, creating it if it doesn't already | |
815 | exist. All writes to the port will go to the end of the file. | |
816 | The "append mode" can be turned off while the port is in use | |
817 | @pxref{Ports and File Descriptors, fcntl} | |
818 | @end table | |
c755b861 | 819 | |
07d83abe | 820 | The following additional characters can be appended: |
c755b861 | 821 | |
07d83abe MV |
822 | @table @samp |
823 | @item + | |
824 | Open the port for both input and output. E.g., @code{r+}: open | |
825 | an existing file for both input and output. | |
826 | @item 0 | |
827 | Create an "unbuffered" port. In this case input and output | |
828 | operations are passed directly to the underlying port | |
829 | implementation without additional buffering. This is likely to | |
830 | slow down I/O operations. The buffering mode can be changed | |
831 | while a port is in use @pxref{Ports and File Descriptors, | |
832 | setvbuf} | |
833 | @item l | |
834 | Add line-buffering to the port. The port output buffer will be | |
835 | automatically flushed whenever a newline character is written. | |
c755b861 KR |
836 | @item b |
837 | Use binary mode. On DOS systems the default text mode converts CR+LF | |
838 | in the file to newline for the program, whereas binary mode reads and | |
839 | writes all bytes unchanged. On Unix-like systems there is no such | |
840 | distinction, text files already contain just newlines and no | |
841 | conversion is ever made. The @code{b} flag is accepted on all | |
842 | systems, but has no effect on Unix-like systems. | |
843 | ||
844 | (For reference, Guile leaves text versus binary up to the C library, | |
845 | @code{b} here just adds @code{O_BINARY} to the underlying @code{open} | |
846 | call, when that flag is available.) | |
211683cc MG |
847 | |
848 | Also, open the file using the 8-bit character encoding "ISO-8859-1", | |
849 | ignoring any coding declaration or port encoding. | |
850 | ||
851 | Note that, when reading or writing binary data with ports, the | |
852 | bytevector ports in the @code{(rnrs io ports)} module are preferred, | |
853 | as they return vectors, and not strings (@pxref{R6RS I/O Ports}). | |
07d83abe | 854 | @end table |
c755b861 | 855 | |
092bdcc4 KR |
856 | If a file cannot be opened with the access |
857 | requested, @code{open-file} throws an exception. | |
858 | ||
211683cc MG |
859 | When the file is opened, this procedure will scan for a coding |
860 | declaration (@pxref{Character Encoding of Source Files}). If present | |
861 | will use that encoding for interpreting the file. Otherwise, the | |
862 | port's encoding will be used. To supress this behavior, open | |
863 | the file in binary mode and then set the port encoding explicitly | |
864 | using @code{set-port-encoding!}. | |
865 | ||
07d83abe MV |
866 | In theory we could create read/write ports which were buffered |
867 | in one direction only. However this isn't included in the | |
092bdcc4 | 868 | current interfaces. |
07d83abe MV |
869 | @end deffn |
870 | ||
871 | @rnindex open-input-file | |
872 | @deffn {Scheme Procedure} open-input-file filename | |
873 | Open @var{filename} for input. Equivalent to | |
aba0dff5 | 874 | @lisp |
07d83abe | 875 | (open-file @var{filename} "r") |
aba0dff5 | 876 | @end lisp |
07d83abe MV |
877 | @end deffn |
878 | ||
879 | @rnindex open-output-file | |
880 | @deffn {Scheme Procedure} open-output-file filename | |
881 | Open @var{filename} for output. Equivalent to | |
aba0dff5 | 882 | @lisp |
07d83abe | 883 | (open-file @var{filename} "w") |
aba0dff5 | 884 | @end lisp |
07d83abe MV |
885 | @end deffn |
886 | ||
887 | @deffn {Scheme Procedure} call-with-input-file filename proc | |
888 | @deffnx {Scheme Procedure} call-with-output-file filename proc | |
889 | @rnindex call-with-input-file | |
890 | @rnindex call-with-output-file | |
891 | Open @var{filename} for input or output, and call @code{(@var{proc} | |
892 | port)} with the resulting port. Return the value returned by | |
893 | @var{proc}. @var{filename} is opened as per @code{open-input-file} or | |
28cc8dac | 894 | @code{open-output-file} respectively, and an error is signaled if it |
07d83abe MV |
895 | cannot be opened. |
896 | ||
897 | When @var{proc} returns, the port is closed. If @var{proc} does not | |
28cc8dac | 898 | return (e.g.@: if it throws an error), then the port might not be |
07d83abe MV |
899 | closed automatically, though it will be garbage collected in the usual |
900 | way if not otherwise referenced. | |
901 | @end deffn | |
902 | ||
903 | @deffn {Scheme Procedure} with-input-from-file filename thunk | |
904 | @deffnx {Scheme Procedure} with-output-to-file filename thunk | |
905 | @deffnx {Scheme Procedure} with-error-to-file filename thunk | |
906 | @rnindex with-input-from-file | |
907 | @rnindex with-output-to-file | |
908 | Open @var{filename} and call @code{(@var{thunk})} with the new port | |
909 | setup as respectively the @code{current-input-port}, | |
910 | @code{current-output-port}, or @code{current-error-port}. Return the | |
911 | value returned by @var{thunk}. @var{filename} is opened as per | |
912 | @code{open-input-file} or @code{open-output-file} respectively, and an | |
28cc8dac | 913 | error is signaled if it cannot be opened. |
07d83abe MV |
914 | |
915 | When @var{thunk} returns, the port is closed and the previous setting | |
916 | of the respective current port is restored. | |
917 | ||
918 | The current port setting is managed with @code{dynamic-wind}, so the | |
919 | previous value is restored no matter how @var{thunk} exits (eg.@: an | |
920 | exception), and if @var{thunk} is re-entered (via a captured | |
921 | continuation) then it's set again to the @var{FILENAME} port. | |
922 | ||
923 | The port is closed when @var{thunk} returns normally, but not when | |
924 | exited via an exception or new continuation. This ensures it's still | |
925 | ready for use if @var{thunk} is re-entered by a captured continuation. | |
926 | Of course the port is always garbage collected and closed in the usual | |
927 | way when no longer referenced anywhere. | |
928 | @end deffn | |
929 | ||
930 | @deffn {Scheme Procedure} port-mode port | |
931 | @deffnx {C Function} scm_port_mode (port) | |
932 | Return the port modes associated with the open port @var{port}. | |
933 | These will not necessarily be identical to the modes used when | |
934 | the port was opened, since modes such as "append" which are | |
935 | used only during port creation are not retained. | |
936 | @end deffn | |
937 | ||
938 | @deffn {Scheme Procedure} port-filename port | |
939 | @deffnx {C Function} scm_port_filename (port) | |
940 | Return the filename associated with @var{port}. This function returns | |
941 | the strings "standard input", "standard output" and "standard error" | |
942 | when called on the current input, output and error ports respectively. | |
e55abf41 KR |
943 | |
944 | @var{port} must be open, @code{port-filename} cannot be used once the | |
945 | port is closed. | |
07d83abe MV |
946 | @end deffn |
947 | ||
948 | @deffn {Scheme Procedure} set-port-filename! port filename | |
949 | @deffnx {C Function} scm_set_port_filename_x (port, filename) | |
950 | Change the filename associated with @var{port}, using the current input | |
951 | port if none is specified. Note that this does not change the port's | |
952 | source of data, but only the value that is returned by | |
953 | @code{port-filename} and reported in diagnostic output. | |
954 | @end deffn | |
955 | ||
956 | @deffn {Scheme Procedure} file-port? obj | |
957 | @deffnx {C Function} scm_file_port_p (obj) | |
958 | Determine whether @var{obj} is a port that is related to a file. | |
959 | @end deffn | |
960 | ||
961 | ||
962 | @node String Ports | |
963 | @subsubsection String Ports | |
bf5df489 KR |
964 | @cindex String port |
965 | @cindex Port, string | |
07d83abe MV |
966 | |
967 | The following allow string ports to be opened by analogy to R4R* | |
968 | file port facilities: | |
969 | ||
28cc8dac MG |
970 | With string ports, the port-encoding is treated differently than other |
971 | types of ports. When string ports are created, they do not inherit a | |
972 | character encoding from the current locale. They are given a | |
973 | default locale that allows them to handle all valid string characters. | |
974 | Typically one should not modify a string port's character encoding | |
975 | away from its default. | |
976 | ||
07d83abe MV |
977 | @deffn {Scheme Procedure} call-with-output-string proc |
978 | @deffnx {C Function} scm_call_with_output_string (proc) | |
979 | Calls the one-argument procedure @var{proc} with a newly created output | |
980 | port. When the function returns, the string composed of the characters | |
981 | written into the port is returned. @var{proc} should not close the port. | |
7b041912 LC |
982 | |
983 | Note that which characters can be written to a string port depend on the port's | |
984 | encoding. The default encoding of string ports is specified by the | |
985 | @code{%default-port-encoding} fluid (@pxref{Ports, | |
986 | @code{%default-port-encoding}}). For instance, it is an error to write Greek | |
987 | letter alpha to an ISO-8859-1-encoded string port since this character cannot be | |
988 | represented with ISO-8859-1: | |
989 | ||
990 | @example | |
991 | (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA | |
992 | ||
993 | (with-fluids ((%default-port-encoding "ISO-8859-1")) | |
994 | (call-with-output-string | |
995 | (lambda (p) | |
996 | (display alpha p)))) | |
997 | ||
998 | @result{} | |
999 | Throw to key `encoding-error' | |
1000 | @end example | |
1001 | ||
1002 | Changing the string port's encoding to a Unicode-capable encoding such as UTF-8 | |
1003 | solves the problem. | |
07d83abe MV |
1004 | @end deffn |
1005 | ||
1006 | @deffn {Scheme Procedure} call-with-input-string string proc | |
1007 | @deffnx {C Function} scm_call_with_input_string (string, proc) | |
1008 | Calls the one-argument procedure @var{proc} with a newly | |
1009 | created input port from which @var{string}'s contents may be | |
1010 | read. The value yielded by the @var{proc} is returned. | |
1011 | @end deffn | |
1012 | ||
1013 | @deffn {Scheme Procedure} with-output-to-string thunk | |
1014 | Calls the zero-argument procedure @var{thunk} with the current output | |
1015 | port set temporarily to a new string port. It returns a string | |
1016 | composed of the characters written to the current output. | |
7b041912 LC |
1017 | |
1018 | See @code{call-with-output-string} above for character encoding considerations. | |
07d83abe MV |
1019 | @end deffn |
1020 | ||
1021 | @deffn {Scheme Procedure} with-input-from-string string thunk | |
1022 | Calls the zero-argument procedure @var{thunk} with the current input | |
1023 | port set temporarily to a string port opened on the specified | |
1024 | @var{string}. The value yielded by @var{thunk} is returned. | |
1025 | @end deffn | |
1026 | ||
1027 | @deffn {Scheme Procedure} open-input-string str | |
1028 | @deffnx {C Function} scm_open_input_string (str) | |
1029 | Take a string and return an input port that delivers characters | |
1030 | from the string. The port can be closed by | |
1031 | @code{close-input-port}, though its storage will be reclaimed | |
1032 | by the garbage collector if it becomes inaccessible. | |
1033 | @end deffn | |
1034 | ||
1035 | @deffn {Scheme Procedure} open-output-string | |
1036 | @deffnx {C Function} scm_open_output_string () | |
1037 | Return an output port that will accumulate characters for | |
1038 | retrieval by @code{get-output-string}. The port can be closed | |
1039 | by the procedure @code{close-output-port}, though its storage | |
1040 | will be reclaimed by the garbage collector if it becomes | |
1041 | inaccessible. | |
1042 | @end deffn | |
1043 | ||
1044 | @deffn {Scheme Procedure} get-output-string port | |
1045 | @deffnx {C Function} scm_get_output_string (port) | |
1046 | Given an output port created by @code{open-output-string}, | |
1047 | return a string consisting of the characters that have been | |
1048 | output to the port so far. | |
1049 | ||
1050 | @code{get-output-string} must be used before closing @var{port}, once | |
1051 | closed the string cannot be obtained. | |
1052 | @end deffn | |
1053 | ||
1054 | A string port can be used in many procedures which accept a port | |
1055 | but which are not dependent on implementation details of fports. | |
1056 | E.g., seeking and truncating will work on a string port, | |
1057 | but trying to extract the file descriptor number will fail. | |
1058 | ||
1059 | ||
1060 | @node Soft Ports | |
1061 | @subsubsection Soft Ports | |
bf5df489 KR |
1062 | @cindex Soft port |
1063 | @cindex Port, soft | |
07d83abe MV |
1064 | |
1065 | A @dfn{soft-port} is a port based on a vector of procedures capable of | |
1066 | accepting or delivering characters. It allows emulation of I/O ports. | |
1067 | ||
1068 | @deffn {Scheme Procedure} make-soft-port pv modes | |
1069 | @deffnx {C Function} scm_make_soft_port (pv, modes) | |
1070 | Return a port capable of receiving or delivering characters as | |
1071 | specified by the @var{modes} string (@pxref{File Ports, | |
1072 | open-file}). @var{pv} must be a vector of length 5 or 6. Its | |
1073 | components are as follows: | |
1074 | ||
1075 | @enumerate 0 | |
1076 | @item | |
1077 | procedure accepting one character for output | |
1078 | @item | |
1079 | procedure accepting a string for output | |
1080 | @item | |
1081 | thunk for flushing output | |
1082 | @item | |
1083 | thunk for getting one character | |
1084 | @item | |
1085 | thunk for closing port (not by garbage collection) | |
1086 | @item | |
1087 | (if present and not @code{#f}) thunk for computing the number of | |
1088 | characters that can be read from the port without blocking. | |
1089 | @end enumerate | |
1090 | ||
1091 | For an output-only port only elements 0, 1, 2, and 4 need be | |
1092 | procedures. For an input-only port only elements 3 and 4 need | |
1093 | be procedures. Thunks 2 and 4 can instead be @code{#f} if | |
1094 | there is no useful operation for them to perform. | |
1095 | ||
1096 | If thunk 3 returns @code{#f} or an @code{eof-object} | |
1097 | (@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on | |
1098 | Scheme}) it indicates that the port has reached end-of-file. | |
1099 | For example: | |
1100 | ||
1101 | @lisp | |
1102 | (define stdout (current-output-port)) | |
1103 | (define p (make-soft-port | |
1104 | (vector | |
1105 | (lambda (c) (write c stdout)) | |
1106 | (lambda (s) (display s stdout)) | |
1107 | (lambda () (display "." stdout)) | |
1108 | (lambda () (char-upcase (read-char))) | |
1109 | (lambda () (display "@@" stdout))) | |
1110 | "rw")) | |
1111 | ||
1112 | (write p p) @result{} #<input-output: soft 8081e20> | |
1113 | @end lisp | |
1114 | @end deffn | |
1115 | ||
1116 | ||
1117 | @node Void Ports | |
1118 | @subsubsection Void Ports | |
bf5df489 KR |
1119 | @cindex Void port |
1120 | @cindex Port, void | |
07d83abe MV |
1121 | |
1122 | This kind of port causes any data to be discarded when written to, and | |
1123 | always returns the end-of-file object when read from. | |
1124 | ||
1125 | @deffn {Scheme Procedure} %make-void-port mode | |
1126 | @deffnx {C Function} scm_sys_make_void_port (mode) | |
1127 | Create and return a new void port. A void port acts like | |
1128 | @file{/dev/null}. The @var{mode} argument | |
1129 | specifies the input/output modes for this port: see the | |
1130 | documentation for @code{open-file} in @ref{File Ports}. | |
1131 | @end deffn | |
1132 | ||
1133 | ||
b242715b LC |
1134 | @node R6RS I/O Ports |
1135 | @subsection R6RS I/O Ports | |
1136 | ||
1137 | @cindex R6RS | |
1138 | @cindex R6RS ports | |
1139 | ||
1140 | The I/O port API of the @uref{http://www.r6rs.org/, Revised Report^6 on | |
1141 | the Algorithmic Language Scheme (R6RS)} is provided by the @code{(rnrs | |
1142 | io ports)} module. It provides features, such as binary I/O and Unicode | |
1143 | string I/O, that complement or refine Guile's historical port API | |
1144 | presented above (@pxref{Input and Output}). | |
1145 | ||
1146 | @c FIXME: Update description when implemented. | |
1147 | @emph{Note}: The implementation of this R6RS API is currently far from | |
1148 | complete, notably due to the lack of support for Unicode I/O and strings. | |
1149 | ||
1150 | @menu | |
1151 | * R6RS End-of-File:: The end-of-file object. | |
1152 | * R6RS Port Manipulation:: Manipulating R6RS ports. | |
1153 | * R6RS Binary Input:: Binary input. | |
1154 | * R6RS Binary Output:: Binary output. | |
1155 | @end menu | |
1156 | ||
1157 | @node R6RS End-of-File | |
1158 | @subsubsection The End-of-File Object | |
1159 | ||
1160 | @cindex EOF | |
1161 | @cindex end-of-file | |
1162 | ||
1163 | R5RS' @code{eof-object?} procedure is provided by the @code{(rnrs io | |
1164 | ports)} module: | |
1165 | ||
1166 | @deffn {Scheme Procedure} eof-object? obj | |
1167 | @deffnx {C Function} scm_eof_object_p (obj) | |
1168 | Return true if @var{obj} is the end-of-file (EOF) object. | |
1169 | @end deffn | |
1170 | ||
1171 | In addition, the following procedure is provided: | |
1172 | ||
1173 | @deffn {Scheme Procedure} eof-object | |
1174 | @deffnx {C Function} scm_eof_object () | |
1175 | Return the end-of-file (EOF) object. | |
1176 | ||
1177 | @lisp | |
1178 | (eof-object? (eof-object)) | |
1179 | @result{} #t | |
1180 | @end lisp | |
1181 | @end deffn | |
1182 | ||
1183 | ||
1184 | @node R6RS Port Manipulation | |
1185 | @subsubsection Port Manipulation | |
1186 | ||
1187 | The procedures listed below operate on any kind of R6RS I/O port. | |
1188 | ||
1189 | @deffn {Scheme Procedure} port-position port | |
1190 | If @var{port} supports it (see below), return the offset (an integer) | |
1191 | indicating where the next octet will be read from/written to in | |
1192 | @var{port}. If @var{port} does not support this operation, an error | |
1193 | condition is raised. | |
1194 | ||
1195 | This is similar to Guile's @code{seek} procedure with the | |
1196 | @code{SEEK_CUR} argument (@pxref{Random Access}). | |
1197 | @end deffn | |
1198 | ||
1199 | @deffn {Scheme Procedure} port-has-port-position? port | |
1200 | Return @code{#t} is @var{port} supports @code{port-position}. | |
1201 | @end deffn | |
1202 | ||
1203 | @deffn {Scheme Procedure} set-port-position! port offset | |
1204 | If @var{port} supports it (see below), set the position where the next | |
1205 | octet will be read from/written to @var{port} to @var{offset} (an | |
1206 | integer). If @var{port} does not support this operation, an error | |
1207 | condition is raised. | |
1208 | ||
1209 | This is similar to Guile's @code{seek} procedure with the | |
1210 | @code{SEEK_SET} argument (@pxref{Random Access}). | |
1211 | @end deffn | |
1212 | ||
1213 | @deffn {Scheme Procedure} port-has-set-port-position!? port | |
1214 | Return @code{#t} is @var{port} supports @code{set-port-position!}. | |
1215 | @end deffn | |
1216 | ||
1217 | @deffn {Scheme Procedure} call-with-port port proc | |
1218 | Call @var{proc}, passing it @var{port} and closing @var{port} upon exit | |
1219 | of @var{proc}. Return the return values of @var{proc}. | |
1220 | @end deffn | |
1221 | ||
1222 | ||
1223 | @node R6RS Binary Input | |
1224 | @subsubsection Binary Input | |
1225 | ||
1226 | @cindex binary input | |
1227 | ||
1228 | R6RS binary input ports can be created with the procedures described | |
1229 | below. | |
1230 | ||
1231 | @deffn {Scheme Procedure} open-bytevector-input-port bv [transcoder] | |
1232 | @deffnx {C Function} scm_open_bytevector_input_port (bv, transcoder) | |
1233 | Return an input port whose contents are drawn from bytevector @var{bv} | |
1234 | (@pxref{Bytevectors}). | |
1235 | ||
1236 | @c FIXME: Update description when implemented. | |
1237 | The @var{transcoder} argument is currently not supported. | |
1238 | @end deffn | |
1239 | ||
1240 | @cindex custom binary input ports | |
1241 | ||
1242 | @deffn {Scheme Procedure} make-custom-binary-input-port id read! get-position set-position! close | |
1243 | @deffnx {C Function} scm_make_custom_binary_input_port (id, read!, get-position, set-position!, close) | |
1244 | Return a new custom binary input port@footnote{This is similar in spirit | |
1245 | to Guile's @dfn{soft ports} (@pxref{Soft Ports}).} named @var{id} (a | |
1246 | string) whose input is drained by invoking @var{read!} and passing it a | |
1247 | bytevector, an index where bytes should be written, and the number of | |
1248 | bytes to read. The @code{read!} procedure must return an integer | |
1249 | indicating the number of bytes read, or @code{0} to indicate the | |
1250 | end-of-file. | |
1251 | ||
1252 | Optionally, if @var{get-position} is not @code{#f}, it must be a thunk | |
1253 | that will be called when @var{port-position} is invoked on the custom | |
1254 | binary port and should return an integer indicating the position within | |
1255 | the underlying data stream; if @var{get-position} was not supplied, the | |
1256 | returned port does not support @var{port-position}. | |
1257 | ||
1258 | Likewise, if @var{set-position!} is not @code{#f}, it should be a | |
1259 | one-argument procedure. When @var{set-port-position!} is invoked on the | |
1260 | custom binary input port, @var{set-position!} is passed an integer | |
1261 | indicating the position of the next byte is to read. | |
1262 | ||
1263 | Finally, if @var{close} is not @code{#f}, it must be a thunk. It is | |
1264 | invoked when the custom binary input port is closed. | |
1265 | ||
1266 | Using a custom binary input port, the @code{open-bytevector-input-port} | |
1267 | procedure could be implemented as follows: | |
1268 | ||
1269 | @lisp | |
1270 | (define (open-bytevector-input-port source) | |
1271 | (define position 0) | |
1272 | (define length (bytevector-length source)) | |
1273 | ||
1274 | (define (read! bv start count) | |
1275 | (let ((count (min count (- length position)))) | |
1276 | (bytevector-copy! source position | |
1277 | bv start count) | |
1278 | (set! position (+ position count)) | |
1279 | count)) | |
1280 | ||
1281 | (define (get-position) position) | |
1282 | ||
1283 | (define (set-position! new-position) | |
1284 | (set! position new-position)) | |
1285 | ||
1286 | (make-custom-binary-input-port "the port" read! | |
1287 | get-position | |
1288 | set-position!)) | |
1289 | ||
1290 | (read (open-bytevector-input-port (string->utf8 "hello"))) | |
1291 | @result{} hello | |
1292 | @end lisp | |
1293 | @end deffn | |
1294 | ||
1295 | @cindex binary input | |
1296 | Binary input is achieved using the procedures below: | |
1297 | ||
1298 | @deffn {Scheme Procedure} get-u8 port | |
1299 | @deffnx {C Function} scm_get_u8 (port) | |
1300 | Return an octet read from @var{port}, a binary input port, blocking as | |
1301 | necessary, or the end-of-file object. | |
1302 | @end deffn | |
1303 | ||
1304 | @deffn {Scheme Procedure} lookahead-u8 port | |
1305 | @deffnx {C Function} scm_lookahead_u8 (port) | |
1306 | Like @code{get-u8} but does not update @var{port}'s position to point | |
1307 | past the octet. | |
1308 | @end deffn | |
1309 | ||
1310 | @deffn {Scheme Procedure} get-bytevector-n port count | |
1311 | @deffnx {C Function} scm_get_bytevector_n (port, count) | |
1312 | Read @var{count} octets from @var{port}, blocking as necessary and | |
1313 | return a bytevector containing the octets read. If fewer bytes are | |
1314 | available, a bytevector smaller than @var{count} is returned. | |
1315 | @end deffn | |
1316 | ||
1317 | @deffn {Scheme Procedure} get-bytevector-n! port bv start count | |
1318 | @deffnx {C Function} scm_get_bytevector_n_x (port, bv, start, count) | |
1319 | Read @var{count} bytes from @var{port} and store them in @var{bv} | |
1320 | starting at index @var{start}. Return either the number of bytes | |
1321 | actually read or the end-of-file object. | |
1322 | @end deffn | |
1323 | ||
1324 | @deffn {Scheme Procedure} get-bytevector-some port | |
1325 | @deffnx {C Function} scm_get_bytevector_some (port) | |
1326 | Read from @var{port}, blocking as necessary, until data are available or | |
1327 | and end-of-file is reached. Return either a new bytevector containing | |
1328 | the data read or the end-of-file object. | |
1329 | @end deffn | |
1330 | ||
1331 | @deffn {Scheme Procedure} get-bytevector-all port | |
1332 | @deffnx {C Function} scm_get_bytevector_all (port) | |
1333 | Read from @var{port}, blocking as necessary, until the end-of-file is | |
1334 | reached. Return either a new bytevector containing the data read or the | |
1335 | end-of-file object (if no data were available). | |
1336 | @end deffn | |
1337 | ||
1338 | @node R6RS Binary Output | |
1339 | @subsubsection Binary Output | |
1340 | ||
1341 | Binary output ports can be created with the procedures below. | |
1342 | ||
1343 | @deffn {Scheme Procedure} open-bytevector-output-port [transcoder] | |
1344 | @deffnx {C Function} scm_open_bytevector_output_port (transcoder) | |
1345 | Return two values: a binary output port and a procedure. The latter | |
1346 | should be called with zero arguments to obtain a bytevector containing | |
1347 | the data accumulated by the port, as illustrated below. | |
1348 | ||
1349 | @lisp | |
1350 | (call-with-values | |
1351 | (lambda () | |
1352 | (open-bytevector-output-port)) | |
1353 | (lambda (port get-bytevector) | |
1354 | (display "hello" port) | |
1355 | (get-bytevector))) | |
1356 | ||
1357 | @result{} #vu8(104 101 108 108 111) | |
1358 | @end lisp | |
1359 | ||
1360 | @c FIXME: Update description when implemented. | |
1361 | The @var{transcoder} argument is currently not supported. | |
1362 | @end deffn | |
1363 | ||
1364 | @cindex custom binary output ports | |
1365 | ||
1366 | @deffn {Scheme Procedure} make-custom-binary-output-port id write! get-position set-position! close | |
1367 | @deffnx {C Function} scm_make_custom_binary_output_port (id, write!, get-position, set-position!, close) | |
1368 | Return a new custom binary output port named @var{id} (a string) whose | |
1369 | output is sunk by invoking @var{write!} and passing it a bytevector, an | |
1370 | index where bytes should be read from this bytevector, and the number of | |
1371 | bytes to be ``written''. The @code{write!} procedure must return an | |
1372 | integer indicating the number of bytes actually written; when it is | |
1373 | passed @code{0} as the number of bytes to write, it should behave as | |
1374 | though an end-of-file was sent to the byte sink. | |
1375 | ||
1376 | The other arguments are as for @code{make-custom-binary-input-port} | |
1377 | (@pxref{R6RS Binary Input, @code{make-custom-binary-input-port}}). | |
1378 | @end deffn | |
1379 | ||
1380 | @cindex binary output | |
1381 | Writing to a binary output port can be done using the following | |
1382 | procedures: | |
1383 | ||
1384 | @deffn {Scheme Procedure} put-u8 port octet | |
1385 | @deffnx {C Function} scm_put_u8 (port, octet) | |
1386 | Write @var{octet}, an integer in the 0--255 range, to @var{port}, a | |
1387 | binary output port. | |
1388 | @end deffn | |
1389 | ||
1390 | @deffn {Scheme Procedure} put-bytevector port bv [start [count]] | |
1391 | @deffnx {C Function} scm_put_bytevector (port, bv, start, count) | |
1392 | Write the contents of @var{bv} to @var{port}, optionally starting at | |
1393 | index @var{start} and limiting to @var{count} octets. | |
1394 | @end deffn | |
1395 | ||
1396 | ||
07d83abe MV |
1397 | @node I/O Extensions |
1398 | @subsection Using and Extending Ports in C | |
1399 | ||
1400 | @menu | |
1401 | * C Port Interface:: Using ports from C. | |
1402 | * Port Implementation:: How to implement a new port type in C. | |
1403 | @end menu | |
1404 | ||
1405 | ||
1406 | @node C Port Interface | |
1407 | @subsubsection C Port Interface | |
bf5df489 KR |
1408 | @cindex C port interface |
1409 | @cindex Port, C interface | |
07d83abe MV |
1410 | |
1411 | This section describes how to use Scheme ports from C. | |
1412 | ||
1413 | @subsubheading Port basics | |
1414 | ||
3081aee1 KR |
1415 | @cindex ptob |
1416 | @tindex scm_ptob_descriptor | |
1417 | @tindex scm_port | |
1418 | @findex SCM_PTAB_ENTRY | |
1419 | @findex SCM_PTOBNUM | |
1420 | @vindex scm_ptobs | |
07d83abe MV |
1421 | There are two main data structures. A port type object (ptob) is of |
1422 | type @code{scm_ptob_descriptor}. A port instance is of type | |
1423 | @code{scm_port}. Given an @code{SCM} variable which points to a port, | |
1424 | the corresponding C port object can be obtained using the | |
1425 | @code{SCM_PTAB_ENTRY} macro. The ptob can be obtained by using | |
1426 | @code{SCM_PTOBNUM} to give an index into the @code{scm_ptobs} | |
1427 | global array. | |
1428 | ||
1429 | @subsubheading Port buffers | |
1430 | ||
1431 | An input port always has a read buffer and an output port always has a | |
1432 | write buffer. However the size of these buffers is not guaranteed to be | |
1433 | more than one byte (e.g., the @code{shortbuf} field in @code{scm_port} | |
1434 | which is used when no other buffer is allocated). The way in which the | |
1435 | buffers are allocated depends on the implementation of the ptob. For | |
1436 | example in the case of an fport, buffers may be allocated with malloc | |
1437 | when the port is created, but in the case of an strport the underlying | |
1438 | string is used as the buffer. | |
1439 | ||
1440 | @subsubheading The @code{rw_random} flag | |
1441 | ||
1442 | Special treatment is required for ports which can be seeked at random. | |
1443 | Before various operations, such as seeking the port or changing from | |
1444 | input to output on a bidirectional port or vice versa, the port | |
1445 | implementation must be given a chance to update its state. The write | |
1446 | buffer is updated by calling the @code{flush} ptob procedure and the | |
1447 | input buffer is updated by calling the @code{end_input} ptob procedure. | |
1448 | In the case of an fport, @code{flush} causes buffered output to be | |
1449 | written to the file descriptor, while @code{end_input} causes the | |
1450 | descriptor position to be adjusted to account for buffered input which | |
1451 | was never read. | |
1452 | ||
1453 | The special treatment must be performed if the @code{rw_random} flag in | |
1454 | the port is non-zero. | |
1455 | ||
1456 | @subsubheading The @code{rw_active} variable | |
1457 | ||
1458 | The @code{rw_active} variable in the port is only used if | |
1459 | @code{rw_random} is set. It's defined as an enum with the following | |
1460 | values: | |
1461 | ||
1462 | @table @code | |
1463 | @item SCM_PORT_READ | |
1464 | the read buffer may have unread data. | |
1465 | ||
1466 | @item SCM_PORT_WRITE | |
1467 | the write buffer may have unwritten data. | |
1468 | ||
1469 | @item SCM_PORT_NEITHER | |
1470 | neither the write nor the read buffer has data. | |
1471 | @end table | |
1472 | ||
1473 | @subsubheading Reading from a port. | |
1474 | ||
1475 | To read from a port, it's possible to either call existing libguile | |
1476 | procedures such as @code{scm_getc} and @code{scm_read_line} or to read | |
1477 | data from the read buffer directly. Reading from the buffer involves | |
1478 | the following steps: | |
1479 | ||
1480 | @enumerate | |
1481 | @item | |
1482 | Flush output on the port, if @code{rw_active} is @code{SCM_PORT_WRITE}. | |
1483 | ||
1484 | @item | |
1485 | Fill the read buffer, if it's empty, using @code{scm_fill_input}. | |
1486 | ||
1487 | @item Read the data from the buffer and update the read position in | |
1488 | the buffer. Steps 2) and 3) may be repeated as many times as required. | |
1489 | ||
1490 | @item Set rw_active to @code{SCM_PORT_READ} if @code{rw_random} is set. | |
1491 | ||
1492 | @item update the port's line and column counts. | |
1493 | @end enumerate | |
1494 | ||
1495 | @subsubheading Writing to a port. | |
1496 | ||
1497 | To write data to a port, calling @code{scm_lfwrite} should be sufficient for | |
1498 | most purposes. This takes care of the following steps: | |
1499 | ||
1500 | @enumerate | |
1501 | @item | |
1502 | End input on the port, if @code{rw_active} is @code{SCM_PORT_READ}. | |
1503 | ||
1504 | @item | |
1505 | Pass the data to the ptob implementation using the @code{write} ptob | |
1506 | procedure. The advantage of using the ptob @code{write} instead of | |
1507 | manipulating the write buffer directly is that it allows the data to be | |
1508 | written in one operation even if the port is using the single-byte | |
1509 | @code{shortbuf}. | |
1510 | ||
1511 | @item | |
1512 | Set @code{rw_active} to @code{SCM_PORT_WRITE} if @code{rw_random} | |
1513 | is set. | |
1514 | @end enumerate | |
1515 | ||
1516 | ||
1517 | @node Port Implementation | |
1518 | @subsubsection Port Implementation | |
28cc8dac | 1519 | @cindex Port implementation |
07d83abe MV |
1520 | |
1521 | This section describes how to implement a new port type in C. | |
1522 | ||
1523 | As described in the previous section, a port type object (ptob) is | |
1524 | a structure of type @code{scm_ptob_descriptor}. A ptob is created by | |
1525 | calling @code{scm_make_port_type}. | |
1526 | ||
23f2b9a3 KR |
1527 | @deftypefun scm_t_bits scm_make_port_type (char *name, int (*fill_input) (SCM port), void (*write) (SCM port, const void *data, size_t size)) |
1528 | Return a new port type object. The @var{name}, @var{fill_input} and | |
1529 | @var{write} parameters are initial values for those port type fields, | |
1530 | as described below. The other fields are initialized with default | |
1531 | values and can be changed later. | |
1532 | @end deftypefun | |
1533 | ||
07d83abe MV |
1534 | All of the elements of the ptob, apart from @code{name}, are procedures |
1535 | which collectively implement the port behaviour. Creating a new port | |
1536 | type mostly involves writing these procedures. | |
1537 | ||
07d83abe MV |
1538 | @table @code |
1539 | @item name | |
1540 | A pointer to a NUL terminated string: the name of the port type. This | |
1541 | is the only element of @code{scm_ptob_descriptor} which is not | |
1542 | a procedure. Set via the first argument to @code{scm_make_port_type}. | |
1543 | ||
1544 | @item mark | |
1545 | Called during garbage collection to mark any SCM objects that a port | |
1546 | object may contain. It doesn't need to be set unless the port has | |
23f2b9a3 KR |
1547 | @code{SCM} components. Set using |
1548 | ||
1549 | @deftypefun void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM port)) | |
1550 | @end deftypefun | |
07d83abe MV |
1551 | |
1552 | @item free | |
1553 | Called when the port is collected during gc. It | |
1554 | should free any resources used by the port. | |
23f2b9a3 KR |
1555 | Set using |
1556 | ||
1557 | @deftypefun void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM port)) | |
1558 | @end deftypefun | |
07d83abe MV |
1559 | |
1560 | @item print | |
1561 | Called when @code{write} is called on the port object, to print a | |
23f2b9a3 KR |
1562 | port description. E.g., for an fport it may produce something like: |
1563 | @code{#<input: /etc/passwd 3>}. Set using | |
1564 | ||
1565 | @deftypefun void scm_set_port_print (scm_t_bits tc, int (*print) (SCM port, SCM dest_port, scm_print_state *pstate)) | |
1566 | The first argument @var{port} is the object being printed, the second | |
1567 | argument @var{dest_port} is where its description should go. | |
1568 | @end deftypefun | |
07d83abe MV |
1569 | |
1570 | @item equalp | |
23f2b9a3 KR |
1571 | Not used at present. Set using |
1572 | ||
1573 | @deftypefun void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM)) | |
1574 | @end deftypefun | |
07d83abe MV |
1575 | |
1576 | @item close | |
1577 | Called when the port is closed, unless it was collected during gc. It | |
1578 | should free any resources used by the port. | |
23f2b9a3 KR |
1579 | Set using |
1580 | ||
1581 | @deftypefun void scm_set_port_close (scm_t_bits tc, int (*close) (SCM port)) | |
1582 | @end deftypefun | |
07d83abe MV |
1583 | |
1584 | @item write | |
1585 | Accept data which is to be written using the port. The port implementation | |
1586 | may choose to buffer the data instead of processing it directly. | |
1587 | Set via the third argument to @code{scm_make_port_type}. | |
1588 | ||
1589 | @item flush | |
1590 | Complete the processing of buffered output data. Reset the value of | |
1591 | @code{rw_active} to @code{SCM_PORT_NEITHER}. | |
23f2b9a3 KR |
1592 | Set using |
1593 | ||
1594 | @deftypefun void scm_set_port_flush (scm_t_bits tc, void (*flush) (SCM port)) | |
1595 | @end deftypefun | |
07d83abe MV |
1596 | |
1597 | @item end_input | |
1598 | Perform any synchronization required when switching from input to output | |
1599 | on the port. Reset the value of @code{rw_active} to @code{SCM_PORT_NEITHER}. | |
23f2b9a3 KR |
1600 | Set using |
1601 | ||
1602 | @deftypefun void scm_set_port_end_input (scm_t_bits tc, void (*end_input) (SCM port, int offset)) | |
1603 | @end deftypefun | |
07d83abe MV |
1604 | |
1605 | @item fill_input | |
1606 | Read new data into the read buffer and return the first character. It | |
1607 | can be assumed that the read buffer is empty when this procedure is called. | |
1608 | Set via the second argument to @code{scm_make_port_type}. | |
1609 | ||
1610 | @item input_waiting | |
1611 | Return a lower bound on the number of bytes that could be read from the | |
1612 | port without blocking. It can be assumed that the current state of | |
1613 | @code{rw_active} is @code{SCM_PORT_NEITHER}. | |
23f2b9a3 KR |
1614 | Set using |
1615 | ||
1616 | @deftypefun void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM port)) | |
1617 | @end deftypefun | |
07d83abe MV |
1618 | |
1619 | @item seek | |
1620 | Set the current position of the port. The procedure can not make | |
1621 | any assumptions about the value of @code{rw_active} when it's | |
1622 | called. It can reset the buffers first if desired by using something | |
1623 | like: | |
1624 | ||
1625 | @example | |
23f2b9a3 KR |
1626 | if (pt->rw_active == SCM_PORT_READ) |
1627 | scm_end_input (port); | |
1628 | else if (pt->rw_active == SCM_PORT_WRITE) | |
1629 | ptob->flush (port); | |
07d83abe MV |
1630 | @end example |
1631 | ||
1632 | However note that this will have the side effect of discarding any data | |
1633 | in the unread-char buffer, in addition to any side effects from the | |
1634 | @code{end_input} and @code{flush} ptob procedures. This is undesirable | |
1635 | when seek is called to measure the current position of the port, i.e., | |
1636 | @code{(seek p 0 SEEK_CUR)}. The libguile fport and string port | |
1637 | implementations take care to avoid this problem. | |
1638 | ||
23f2b9a3 KR |
1639 | The procedure is set using |
1640 | ||
f1ce9199 | 1641 | @deftypefun void scm_set_port_seek (scm_t_bits tc, scm_t_off (*seek) (SCM port, scm_t_off offset, int whence)) |
23f2b9a3 | 1642 | @end deftypefun |
07d83abe MV |
1643 | |
1644 | @item truncate | |
1645 | Truncate the port data to be specified length. It can be assumed that the | |
1646 | current state of @code{rw_active} is @code{SCM_PORT_NEITHER}. | |
23f2b9a3 KR |
1647 | Set using |
1648 | ||
f1ce9199 | 1649 | @deftypefun void scm_set_port_truncate (scm_t_bits tc, void (*truncate) (SCM port, scm_t_off length)) |
23f2b9a3 | 1650 | @end deftypefun |
07d83abe MV |
1651 | |
1652 | @end table | |
1653 | ||
1654 | ||
1655 | @c Local Variables: | |
1656 | @c TeX-master: "guile.texi" | |
1657 | @c End: |