api-io.texi 88 KB

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