api-io.texi 47 KB

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