api-io.texi 88 KB

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