r6rs-ports.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /* Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #ifdef HAVE_UNISTD_H
  22. # include <unistd.h>
  23. #endif
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include "libguile/_scm.h"
  28. #include "libguile/bytevectors.h"
  29. #include "libguile/chars.h"
  30. #include "libguile/eval.h"
  31. #include "libguile/r6rs-ports.h"
  32. #include "libguile/strings.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/values.h"
  35. #include "libguile/vectors.h"
  36. /* Unimplemented features. */
  37. /* Transoders are currently not implemented since Guile 1.8 is not
  38. Unicode-capable. Thus, most of the code here assumes the use of the
  39. binary transcoder. */
  40. static inline void
  41. transcoders_not_implemented (void)
  42. {
  43. fprintf (stderr, "%s: warning: transcoders not implemented\n",
  44. PACKAGE_NAME);
  45. }
  46. /* End-of-file object. */
  47. SCM_DEFINE (scm_eof_object, "eof-object", 0, 0, 0,
  48. (void),
  49. "Return the end-of-file object.")
  50. #define FUNC_NAME s_scm_eof_object
  51. {
  52. return (SCM_EOF_VAL);
  53. }
  54. #undef FUNC_NAME
  55. /* Input ports. */
  56. #ifndef MIN
  57. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  58. #endif
  59. /* Bytevector input ports or "bip" for short. */
  60. static scm_t_bits bytevector_input_port_type = 0;
  61. static inline SCM
  62. make_bip (SCM bv)
  63. {
  64. SCM port;
  65. char *c_bv;
  66. unsigned c_len;
  67. scm_t_port *c_port;
  68. const unsigned long mode_bits = SCM_OPN | SCM_RDNG;
  69. scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
  70. port = scm_new_port_table_entry (bytevector_input_port_type);
  71. c_port = SCM_PTAB_ENTRY (port);
  72. /* Match the expectation of `binary-port?'. */
  73. c_port->encoding = NULL;
  74. /* Prevent BV from being GC'd. */
  75. SCM_SETSTREAM (port, SCM_UNPACK (bv));
  76. /* Have the port directly access the bytevector. */
  77. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  78. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  79. c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
  80. c_port->read_end = (unsigned char *) c_bv + c_len;
  81. c_port->read_buf_size = c_len;
  82. /* Mark PORT as open, readable and unbuffered (hmm, how elegant...). */
  83. SCM_SET_CELL_TYPE (port, bytevector_input_port_type | mode_bits);
  84. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  85. return port;
  86. }
  87. static int
  88. bip_fill_input (SCM port)
  89. {
  90. int result;
  91. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  92. if (c_port->read_pos >= c_port->read_end)
  93. result = EOF;
  94. else
  95. result = (int) *c_port->read_pos;
  96. return result;
  97. }
  98. static scm_t_off
  99. bip_seek (SCM port, scm_t_off offset, int whence)
  100. #define FUNC_NAME "bip_seek"
  101. {
  102. scm_t_off c_result = 0;
  103. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  104. switch (whence)
  105. {
  106. case SEEK_CUR:
  107. offset += c_port->read_pos - c_port->read_buf;
  108. /* Fall through. */
  109. case SEEK_SET:
  110. if (c_port->read_buf + offset <= c_port->read_end)
  111. {
  112. c_port->read_pos = c_port->read_buf + offset;
  113. c_result = offset;
  114. }
  115. else
  116. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  117. break;
  118. case SEEK_END:
  119. if (c_port->read_end - offset >= c_port->read_buf)
  120. {
  121. c_port->read_pos = c_port->read_end - offset;
  122. c_result = c_port->read_pos - c_port->read_buf;
  123. }
  124. else
  125. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  126. break;
  127. default:
  128. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  129. "invalid `seek' parameter");
  130. }
  131. return c_result;
  132. }
  133. #undef FUNC_NAME
  134. /* Instantiate the bytevector input port type. */
  135. static inline void
  136. initialize_bytevector_input_ports (void)
  137. {
  138. bytevector_input_port_type =
  139. scm_make_port_type ("r6rs-bytevector-input-port", bip_fill_input,
  140. NULL);
  141. scm_set_port_seek (bytevector_input_port_type, bip_seek);
  142. }
  143. SCM_DEFINE (scm_open_bytevector_input_port,
  144. "open-bytevector-input-port", 1, 1, 0,
  145. (SCM bv, SCM transcoder),
  146. "Return an input port whose contents are drawn from "
  147. "bytevector @var{bv}.")
  148. #define FUNC_NAME s_scm_open_bytevector_input_port
  149. {
  150. SCM_VALIDATE_BYTEVECTOR (1, bv);
  151. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  152. transcoders_not_implemented ();
  153. return (make_bip (bv));
  154. }
  155. #undef FUNC_NAME
  156. /* Custom binary ports. The following routines are shared by input and
  157. output custom binary ports. */
  158. #define SCM_CBP_GET_POSITION_PROC(_port) \
  159. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 1)
  160. #define SCM_CBP_SET_POSITION_PROC(_port) \
  161. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 2)
  162. #define SCM_CBP_CLOSE_PROC(_port) \
  163. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 3)
  164. static scm_t_off
  165. cbp_seek (SCM port, scm_t_off offset, int whence)
  166. #define FUNC_NAME "cbp_seek"
  167. {
  168. SCM result;
  169. scm_t_off c_result = 0;
  170. switch (whence)
  171. {
  172. case SEEK_CUR:
  173. {
  174. SCM get_position_proc;
  175. get_position_proc = SCM_CBP_GET_POSITION_PROC (port);
  176. if (SCM_LIKELY (scm_is_true (get_position_proc)))
  177. result = scm_call_0 (get_position_proc);
  178. else
  179. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  180. "R6RS custom binary port does not "
  181. "support `port-position'");
  182. offset += scm_to_int (result);
  183. /* Fall through. */
  184. }
  185. case SEEK_SET:
  186. {
  187. SCM set_position_proc;
  188. set_position_proc = SCM_CBP_SET_POSITION_PROC (port);
  189. if (SCM_LIKELY (scm_is_true (set_position_proc)))
  190. result = scm_call_1 (set_position_proc, scm_from_int (offset));
  191. else
  192. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  193. "R6RS custom binary port does not "
  194. "support `set-port-position!'");
  195. /* Assuming setting the position succeeded. */
  196. c_result = offset;
  197. break;
  198. }
  199. default:
  200. /* `SEEK_END' cannot be supported. */
  201. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  202. "R6RS custom binary ports do not "
  203. "support `SEEK_END'");
  204. }
  205. return c_result;
  206. }
  207. #undef FUNC_NAME
  208. static int
  209. cbp_close (SCM port)
  210. {
  211. SCM close_proc;
  212. close_proc = SCM_CBP_CLOSE_PROC (port);
  213. if (scm_is_true (close_proc))
  214. /* Invoke the `close' thunk. */
  215. scm_call_0 (close_proc);
  216. return 1;
  217. }
  218. /* Custom binary input port ("cbip" for short). */
  219. static scm_t_bits custom_binary_input_port_type = 0;
  220. /* Size of the buffer embedded in custom binary input ports. */
  221. #define CBIP_BUFFER_SIZE 4096
  222. /* Return the bytevector associated with PORT. */
  223. #define SCM_CBIP_BYTEVECTOR(_port) \
  224. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 4)
  225. /* Return the various procedures of PORT. */
  226. #define SCM_CBIP_READ_PROC(_port) \
  227. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
  228. static inline SCM
  229. make_cbip (SCM read_proc, SCM get_position_proc,
  230. SCM set_position_proc, SCM close_proc)
  231. {
  232. SCM port, bv, method_vector;
  233. char *c_bv;
  234. unsigned c_len;
  235. scm_t_port *c_port;
  236. const unsigned long mode_bits = SCM_OPN | SCM_RDNG;
  237. /* Use a bytevector as the underlying buffer. */
  238. c_len = CBIP_BUFFER_SIZE;
  239. bv = scm_c_make_bytevector (c_len);
  240. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  241. /* Store the various methods and bytevector in a vector. */
  242. method_vector = scm_c_make_vector (5, SCM_BOOL_F);
  243. SCM_SIMPLE_VECTOR_SET (method_vector, 4, bv);
  244. SCM_SIMPLE_VECTOR_SET (method_vector, 0, read_proc);
  245. SCM_SIMPLE_VECTOR_SET (method_vector, 1, get_position_proc);
  246. SCM_SIMPLE_VECTOR_SET (method_vector, 2, set_position_proc);
  247. SCM_SIMPLE_VECTOR_SET (method_vector, 3, close_proc);
  248. scm_i_pthread_mutex_lock (&scm_i_port_table_mutex);
  249. port = scm_new_port_table_entry (custom_binary_input_port_type);
  250. c_port = SCM_PTAB_ENTRY (port);
  251. /* Match the expectation of `binary-port?'. */
  252. c_port->encoding = NULL;
  253. /* Attach it the method vector. */
  254. SCM_SETSTREAM (port, SCM_UNPACK (method_vector));
  255. /* Have the port directly access the buffer (bytevector). */
  256. c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
  257. c_port->read_end = (unsigned char *) c_bv;
  258. c_port->read_buf_size = c_len;
  259. /* Mark PORT as open, readable and unbuffered (hmm, how elegant...). */
  260. SCM_SET_CELL_TYPE (port, custom_binary_input_port_type | mode_bits);
  261. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  262. return port;
  263. }
  264. static int
  265. cbip_fill_input (SCM port)
  266. #define FUNC_NAME "cbip_fill_input"
  267. {
  268. int result;
  269. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  270. again:
  271. if (c_port->read_pos >= c_port->read_end)
  272. {
  273. /* Invoke the user's `read!' procedure. */
  274. unsigned c_octets;
  275. SCM bv, read_proc, octets;
  276. /* Use the bytevector associated with PORT as the buffer passed to the
  277. `read!' procedure, thereby avoiding additional allocations. */
  278. bv = SCM_CBIP_BYTEVECTOR (port);
  279. read_proc = SCM_CBIP_READ_PROC (port);
  280. /* The assumption here is that C_PORT's internal buffer wasn't changed
  281. behind our back. */
  282. assert (c_port->read_buf ==
  283. (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv));
  284. assert ((unsigned) c_port->read_buf_size
  285. == SCM_BYTEVECTOR_LENGTH (bv));
  286. octets = scm_call_3 (read_proc, bv, SCM_INUM0,
  287. SCM_I_MAKINUM (CBIP_BUFFER_SIZE));
  288. c_octets = scm_to_uint (octets);
  289. c_port->read_pos = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  290. c_port->read_end = (unsigned char *) c_port->read_pos + c_octets;
  291. if (c_octets > 0)
  292. goto again;
  293. else
  294. result = EOF;
  295. }
  296. else
  297. result = (int) *c_port->read_pos;
  298. return result;
  299. }
  300. #undef FUNC_NAME
  301. SCM_DEFINE (scm_make_custom_binary_input_port,
  302. "make-custom-binary-input-port", 5, 0, 0,
  303. (SCM id, SCM read_proc, SCM get_position_proc,
  304. SCM set_position_proc, SCM close_proc),
  305. "Return a new custom binary input port whose input is drained "
  306. "by invoking @var{read_proc} and passing it a bytevector, an "
  307. "index where octets should be written, and an octet count.")
  308. #define FUNC_NAME s_scm_make_custom_binary_input_port
  309. {
  310. SCM_VALIDATE_STRING (1, id);
  311. SCM_VALIDATE_PROC (2, read_proc);
  312. if (!scm_is_false (get_position_proc))
  313. SCM_VALIDATE_PROC (3, get_position_proc);
  314. if (!scm_is_false (set_position_proc))
  315. SCM_VALIDATE_PROC (4, set_position_proc);
  316. if (!scm_is_false (close_proc))
  317. SCM_VALIDATE_PROC (5, close_proc);
  318. return (make_cbip (read_proc, get_position_proc, set_position_proc,
  319. close_proc));
  320. }
  321. #undef FUNC_NAME
  322. /* Instantiate the custom binary input port type. */
  323. static inline void
  324. initialize_custom_binary_input_ports (void)
  325. {
  326. custom_binary_input_port_type =
  327. scm_make_port_type ("r6rs-custom-binary-input-port",
  328. cbip_fill_input, NULL);
  329. scm_set_port_seek (custom_binary_input_port_type, cbp_seek);
  330. scm_set_port_close (custom_binary_input_port_type, cbp_close);
  331. }
  332. /* Binary input. */
  333. /* We currently don't support specific binary input ports. */
  334. #define SCM_VALIDATE_BINARY_INPUT_PORT SCM_VALIDATE_OPINPORT
  335. SCM_DEFINE (scm_get_u8, "get-u8", 1, 0, 0,
  336. (SCM port),
  337. "Read an octet from @var{port}, a binary input port, "
  338. "blocking as necessary.")
  339. #define FUNC_NAME s_scm_get_u8
  340. {
  341. SCM result;
  342. int c_result;
  343. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  344. c_result = scm_get_byte_or_eof (port);
  345. if (c_result == EOF)
  346. result = SCM_EOF_VAL;
  347. else
  348. result = SCM_I_MAKINUM ((unsigned char) c_result);
  349. return result;
  350. }
  351. #undef FUNC_NAME
  352. SCM_DEFINE (scm_lookahead_u8, "lookahead-u8", 1, 0, 0,
  353. (SCM port),
  354. "Like @code{get-u8} but does not update @var{port} to "
  355. "point past the octet.")
  356. #define FUNC_NAME s_scm_lookahead_u8
  357. {
  358. int u8;
  359. SCM result;
  360. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  361. u8 = scm_peek_byte_or_eof (port);
  362. if (u8 == EOF)
  363. result = SCM_EOF_VAL;
  364. else
  365. result = SCM_I_MAKINUM ((scm_t_uint8) u8);
  366. return result;
  367. }
  368. #undef FUNC_NAME
  369. SCM_DEFINE (scm_get_bytevector_n, "get-bytevector-n", 2, 0, 0,
  370. (SCM port, SCM count),
  371. "Read @var{count} octets from @var{port}, blocking as "
  372. "necessary and return a bytevector containing the octets "
  373. "read. If fewer bytes are available, a bytevector smaller "
  374. "than @var{count} is returned.")
  375. #define FUNC_NAME s_scm_get_bytevector_n
  376. {
  377. SCM result;
  378. char *c_bv;
  379. unsigned c_count;
  380. size_t c_read;
  381. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  382. c_count = scm_to_uint (count);
  383. result = scm_c_make_bytevector (c_count);
  384. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (result);
  385. if (SCM_LIKELY (c_count > 0))
  386. /* XXX: `scm_c_read ()' does not update the port position. */
  387. c_read = scm_c_read (port, c_bv, c_count);
  388. else
  389. /* Don't invoke `scm_c_read ()' since it may block. */
  390. c_read = 0;
  391. if ((c_read == 0) && (c_count > 0))
  392. {
  393. if (SCM_EOF_OBJECT_P (scm_peek_char (port)))
  394. result = SCM_EOF_VAL;
  395. else
  396. result = scm_null_bytevector;
  397. }
  398. else
  399. {
  400. if (c_read < c_count)
  401. result = scm_c_shrink_bytevector (result, c_read);
  402. }
  403. return result;
  404. }
  405. #undef FUNC_NAME
  406. SCM_DEFINE (scm_get_bytevector_n_x, "get-bytevector-n!", 4, 0, 0,
  407. (SCM port, SCM bv, SCM start, SCM count),
  408. "Read @var{count} bytes from @var{port} and store them "
  409. "in @var{bv} starting at index @var{start}. Return either "
  410. "the number of bytes actually read or the end-of-file "
  411. "object.")
  412. #define FUNC_NAME s_scm_get_bytevector_n_x
  413. {
  414. SCM result;
  415. char *c_bv;
  416. unsigned c_start, c_count, c_len;
  417. size_t c_read;
  418. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  419. SCM_VALIDATE_BYTEVECTOR (2, bv);
  420. c_start = scm_to_uint (start);
  421. c_count = scm_to_uint (count);
  422. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  423. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  424. if (SCM_UNLIKELY (c_start + c_count > c_len))
  425. scm_out_of_range (FUNC_NAME, count);
  426. if (SCM_LIKELY (c_count > 0))
  427. c_read = scm_c_read (port, c_bv + c_start, c_count);
  428. else
  429. /* Don't invoke `scm_c_read ()' since it may block. */
  430. c_read = 0;
  431. if ((c_read == 0) && (c_count > 0))
  432. {
  433. if (SCM_EOF_OBJECT_P (scm_peek_char (port)))
  434. result = SCM_EOF_VAL;
  435. else
  436. result = SCM_I_MAKINUM (0);
  437. }
  438. else
  439. result = scm_from_size_t (c_read);
  440. return result;
  441. }
  442. #undef FUNC_NAME
  443. SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-some", 1, 0, 0,
  444. (SCM port),
  445. "Read from @var{port}, blocking as necessary, until data "
  446. "are available or and end-of-file is reached. Return either "
  447. "a new bytevector containing the data read or the "
  448. "end-of-file object.")
  449. #define FUNC_NAME s_scm_get_bytevector_some
  450. {
  451. /* Read at least one byte, unless the end-of-file is already reached, and
  452. read while characters are available (buffered). */
  453. SCM result;
  454. char *c_bv;
  455. unsigned c_len;
  456. size_t c_total;
  457. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  458. c_len = 4096;
  459. c_bv = (char *) scm_gc_malloc_pointerless (c_len, SCM_GC_BYTEVECTOR);
  460. c_total = 0;
  461. do
  462. {
  463. int c_chr;
  464. if (c_total + 1 > c_len)
  465. {
  466. /* Grow the bytevector. */
  467. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
  468. SCM_GC_BYTEVECTOR);
  469. c_len *= 2;
  470. }
  471. /* We can't use `scm_c_read ()' since it blocks. */
  472. c_chr = scm_getc (port);
  473. if (c_chr != EOF)
  474. {
  475. c_bv[c_total] = (char) c_chr;
  476. c_total++;
  477. }
  478. }
  479. while ((scm_is_true (scm_char_ready_p (port)))
  480. && (!SCM_EOF_OBJECT_P (scm_peek_char (port))));
  481. if (c_total == 0)
  482. {
  483. result = SCM_EOF_VAL;
  484. scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
  485. }
  486. else
  487. {
  488. if (c_len > c_total)
  489. {
  490. /* Shrink the bytevector. */
  491. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
  492. SCM_GC_BYTEVECTOR);
  493. c_len = (unsigned) c_total;
  494. }
  495. result = scm_c_take_bytevector ((signed char *) c_bv, c_len,
  496. SCM_BOOL_F);
  497. }
  498. return result;
  499. }
  500. #undef FUNC_NAME
  501. SCM_DEFINE (scm_get_bytevector_all, "get-bytevector-all", 1, 0, 0,
  502. (SCM port),
  503. "Read from @var{port}, blocking as necessary, until "
  504. "the end-of-file is reached. Return either "
  505. "a new bytevector containing the data read or the "
  506. "end-of-file object (if no data were available).")
  507. #define FUNC_NAME s_scm_get_bytevector_all
  508. {
  509. SCM result;
  510. char *c_bv;
  511. unsigned c_len, c_count;
  512. size_t c_read, c_total;
  513. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  514. c_len = c_count = 4096;
  515. c_bv = (char *) scm_gc_malloc_pointerless (c_len, SCM_GC_BYTEVECTOR);
  516. c_total = c_read = 0;
  517. do
  518. {
  519. if (c_total + c_read > c_len)
  520. {
  521. /* Grow the bytevector. */
  522. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
  523. SCM_GC_BYTEVECTOR);
  524. c_count = c_len;
  525. c_len *= 2;
  526. }
  527. /* `scm_c_read ()' blocks until C_COUNT bytes are available or EOF is
  528. reached. */
  529. c_read = scm_c_read (port, c_bv + c_total, c_count);
  530. c_total += c_read, c_count -= c_read;
  531. }
  532. while (!SCM_EOF_OBJECT_P (scm_peek_char (port)));
  533. if (c_total == 0)
  534. {
  535. result = SCM_EOF_VAL;
  536. scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
  537. }
  538. else
  539. {
  540. if (c_len > c_total)
  541. {
  542. /* Shrink the bytevector. */
  543. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
  544. SCM_GC_BYTEVECTOR);
  545. c_len = (unsigned) c_total;
  546. }
  547. result = scm_c_take_bytevector ((signed char *) c_bv, c_len,
  548. SCM_BOOL_F);
  549. }
  550. return result;
  551. }
  552. #undef FUNC_NAME
  553. /* Binary output. */
  554. /* We currently don't support specific binary input ports. */
  555. #define SCM_VALIDATE_BINARY_OUTPUT_PORT SCM_VALIDATE_OPOUTPORT
  556. SCM_DEFINE (scm_put_u8, "put-u8", 2, 0, 0,
  557. (SCM port, SCM octet),
  558. "Write @var{octet} to binary port @var{port}.")
  559. #define FUNC_NAME s_scm_put_u8
  560. {
  561. scm_t_uint8 c_octet;
  562. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  563. c_octet = scm_to_uint8 (octet);
  564. scm_putc ((char) c_octet, port);
  565. return SCM_UNSPECIFIED;
  566. }
  567. #undef FUNC_NAME
  568. SCM_DEFINE (scm_put_bytevector, "put-bytevector", 2, 2, 0,
  569. (SCM port, SCM bv, SCM start, SCM count),
  570. "Write the contents of @var{bv} to @var{port}, optionally "
  571. "starting at index @var{start} and limiting to @var{count} "
  572. "octets.")
  573. #define FUNC_NAME s_scm_put_bytevector
  574. {
  575. char *c_bv;
  576. unsigned c_start, c_count, c_len;
  577. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  578. SCM_VALIDATE_BYTEVECTOR (2, bv);
  579. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  580. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  581. if (!scm_is_eq (start, SCM_UNDEFINED))
  582. {
  583. c_start = scm_to_uint (start);
  584. if (!scm_is_eq (count, SCM_UNDEFINED))
  585. {
  586. c_count = scm_to_uint (count);
  587. if (SCM_UNLIKELY (c_start + c_count > c_len))
  588. scm_out_of_range (FUNC_NAME, count);
  589. }
  590. else
  591. {
  592. if (SCM_UNLIKELY (c_start >= c_len))
  593. scm_out_of_range (FUNC_NAME, start);
  594. else
  595. c_count = c_len - c_start;
  596. }
  597. }
  598. else
  599. c_start = 0, c_count = c_len;
  600. scm_c_write (port, c_bv + c_start, c_count);
  601. return SCM_UNSPECIFIED;
  602. }
  603. #undef FUNC_NAME
  604. /* Bytevector output port ("bop" for short). */
  605. /* Implementation of "bops".
  606. Each bop has an internal buffer, of type `scm_t_bop_buffer', attached to
  607. it. The procedure returned along with the output port is actually an
  608. applicable SMOB. The SMOB holds a reference to the port. When applied,
  609. the SMOB swallows the port's internal buffer, turning it into a
  610. bytevector, and resets it.
  611. XXX: Access to a bop's internal buffer is not thread-safe. */
  612. static scm_t_bits bytevector_output_port_type = 0;
  613. SCM_SMOB (bytevector_output_port_procedure,
  614. "r6rs-bytevector-output-port-procedure",
  615. 0);
  616. #define SCM_GC_BOP "r6rs-bytevector-output-port"
  617. #define SCM_BOP_BUFFER_INITIAL_SIZE 4096
  618. /* Representation of a bop's internal buffer. */
  619. typedef struct
  620. {
  621. size_t total_len;
  622. size_t len;
  623. size_t pos;
  624. char *buffer;
  625. } scm_t_bop_buffer;
  626. /* Accessing a bop's buffer. */
  627. #define SCM_BOP_BUFFER(_port) \
  628. ((scm_t_bop_buffer *) SCM_STREAM (_port))
  629. #define SCM_SET_BOP_BUFFER(_port, _buf) \
  630. (SCM_SETSTREAM ((_port), (scm_t_bits) (_buf)))
  631. static inline void
  632. bop_buffer_init (scm_t_bop_buffer *buf)
  633. {
  634. buf->total_len = buf->len = buf->pos = 0;
  635. buf->buffer = NULL;
  636. }
  637. static inline void
  638. bop_buffer_grow (scm_t_bop_buffer *buf, size_t min_size)
  639. {
  640. char *new_buf;
  641. size_t new_size;
  642. for (new_size = buf->total_len
  643. ? buf->total_len : SCM_BOP_BUFFER_INITIAL_SIZE;
  644. new_size < min_size;
  645. new_size *= 2);
  646. if (buf->buffer)
  647. new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len,
  648. new_size, SCM_GC_BOP);
  649. else
  650. new_buf = scm_gc_malloc_pointerless (new_size, SCM_GC_BOP);
  651. buf->buffer = new_buf;
  652. buf->total_len = new_size;
  653. }
  654. static inline SCM
  655. make_bop (void)
  656. {
  657. SCM port, bop_proc;
  658. scm_t_port *c_port;
  659. scm_t_bop_buffer *buf;
  660. const unsigned long mode_bits = SCM_OPN | SCM_WRTNG;
  661. scm_i_pthread_mutex_lock (&scm_i_port_table_mutex);
  662. port = scm_new_port_table_entry (bytevector_output_port_type);
  663. c_port = SCM_PTAB_ENTRY (port);
  664. /* Match the expectation of `binary-port?'. */
  665. c_port->encoding = NULL;
  666. buf = (scm_t_bop_buffer *) scm_gc_malloc (sizeof (* buf), SCM_GC_BOP);
  667. bop_buffer_init (buf);
  668. c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
  669. c_port->write_buf_size = 0;
  670. SCM_SET_BOP_BUFFER (port, buf);
  671. /* Mark PORT as open and writable. */
  672. SCM_SET_CELL_TYPE (port, bytevector_output_port_type | mode_bits);
  673. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  674. /* Make the bop procedure. */
  675. SCM_NEWSMOB (bop_proc, bytevector_output_port_procedure, buf);
  676. return (scm_values (scm_list_2 (port, bop_proc)));
  677. }
  678. /* Write SIZE octets from DATA to PORT. */
  679. static void
  680. bop_write (SCM port, const void *data, size_t size)
  681. {
  682. scm_t_bop_buffer *buf;
  683. buf = SCM_BOP_BUFFER (port);
  684. if (buf->pos + size > buf->total_len)
  685. bop_buffer_grow (buf, buf->pos + size);
  686. memcpy (buf->buffer + buf->pos, data, size);
  687. buf->pos += size;
  688. buf->len = (buf->len > buf->pos) ? buf->len : buf->pos;
  689. }
  690. static scm_t_off
  691. bop_seek (SCM port, scm_t_off offset, int whence)
  692. #define FUNC_NAME "bop_seek"
  693. {
  694. scm_t_bop_buffer *buf;
  695. buf = SCM_BOP_BUFFER (port);
  696. switch (whence)
  697. {
  698. case SEEK_CUR:
  699. offset += (scm_t_off) buf->pos;
  700. /* Fall through. */
  701. case SEEK_SET:
  702. if (offset < 0 || (unsigned) offset > buf->len)
  703. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  704. else
  705. buf->pos = offset;
  706. break;
  707. case SEEK_END:
  708. if (offset < 0 || (unsigned) offset >= buf->len)
  709. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  710. else
  711. buf->pos = buf->len - (offset + 1);
  712. break;
  713. default:
  714. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  715. "invalid `seek' parameter");
  716. }
  717. return buf->pos;
  718. }
  719. #undef FUNC_NAME
  720. /* Fetch data from a bop. */
  721. SCM_SMOB_APPLY (bytevector_output_port_procedure,
  722. bop_proc_apply, 0, 0, 0, (SCM bop_proc))
  723. {
  724. SCM bv;
  725. scm_t_bop_buffer *buf, result_buf;
  726. buf = (scm_t_bop_buffer *) SCM_SMOB_DATA (bop_proc);
  727. result_buf = *buf;
  728. bop_buffer_init (buf);
  729. if (result_buf.len == 0)
  730. bv = scm_c_take_bytevector (NULL, 0, SCM_BOOL_F);
  731. else
  732. {
  733. if (result_buf.total_len > result_buf.len)
  734. /* Shrink the buffer. */
  735. result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer,
  736. result_buf.total_len,
  737. result_buf.len,
  738. SCM_GC_BOP);
  739. bv = scm_c_take_bytevector ((signed char *) result_buf.buffer,
  740. result_buf.len, SCM_BOOL_F);
  741. }
  742. return bv;
  743. }
  744. SCM_DEFINE (scm_open_bytevector_output_port,
  745. "open-bytevector-output-port", 0, 1, 0,
  746. (SCM transcoder),
  747. "Return two values: an output port and a procedure. The latter "
  748. "should be called with zero arguments to obtain a bytevector "
  749. "containing the data accumulated by the port.")
  750. #define FUNC_NAME s_scm_open_bytevector_output_port
  751. {
  752. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  753. transcoders_not_implemented ();
  754. return (make_bop ());
  755. }
  756. #undef FUNC_NAME
  757. static inline void
  758. initialize_bytevector_output_ports (void)
  759. {
  760. bytevector_output_port_type =
  761. scm_make_port_type ("r6rs-bytevector-output-port",
  762. NULL, bop_write);
  763. scm_set_port_seek (bytevector_output_port_type, bop_seek);
  764. }
  765. /* Custom binary output port ("cbop" for short). */
  766. static scm_t_bits custom_binary_output_port_type;
  767. /* Return the various procedures of PORT. */
  768. #define SCM_CBOP_WRITE_PROC(_port) \
  769. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
  770. static inline SCM
  771. make_cbop (SCM write_proc, SCM get_position_proc,
  772. SCM set_position_proc, SCM close_proc)
  773. {
  774. SCM port, method_vector;
  775. scm_t_port *c_port;
  776. const unsigned long mode_bits = SCM_OPN | SCM_WRTNG;
  777. /* Store the various methods and bytevector in a vector. */
  778. method_vector = scm_c_make_vector (4, SCM_BOOL_F);
  779. SCM_SIMPLE_VECTOR_SET (method_vector, 0, write_proc);
  780. SCM_SIMPLE_VECTOR_SET (method_vector, 1, get_position_proc);
  781. SCM_SIMPLE_VECTOR_SET (method_vector, 2, set_position_proc);
  782. SCM_SIMPLE_VECTOR_SET (method_vector, 3, close_proc);
  783. scm_i_pthread_mutex_lock (&scm_i_port_table_mutex);
  784. port = scm_new_port_table_entry (custom_binary_output_port_type);
  785. c_port = SCM_PTAB_ENTRY (port);
  786. /* Match the expectation of `binary-port?'. */
  787. c_port->encoding = NULL;
  788. /* Attach it the method vector. */
  789. SCM_SETSTREAM (port, SCM_UNPACK (method_vector));
  790. /* Have the port directly access the buffer (bytevector). */
  791. c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
  792. c_port->write_buf_size = c_port->read_buf_size = 0;
  793. /* Mark PORT as open, writable and unbuffered. */
  794. SCM_SET_CELL_TYPE (port, custom_binary_output_port_type | mode_bits);
  795. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  796. return port;
  797. }
  798. /* Write SIZE octets from DATA to PORT. */
  799. static void
  800. cbop_write (SCM port, const void *data, size_t size)
  801. #define FUNC_NAME "cbop_write"
  802. {
  803. long int c_result;
  804. size_t c_written;
  805. SCM bv, write_proc, result;
  806. /* XXX: Allocating a new bytevector at each `write' call is inefficient,
  807. but necessary since (1) we don't control the lifetime of the buffer
  808. pointed to by DATA, and (2) the `write!' procedure could capture the
  809. bytevector it is passed. */
  810. bv = scm_c_make_bytevector (size);
  811. memcpy (SCM_BYTEVECTOR_CONTENTS (bv), data, size);
  812. write_proc = SCM_CBOP_WRITE_PROC (port);
  813. /* Since the `write' procedure of Guile's ports has type `void', it must
  814. try hard to write exactly SIZE bytes, regardless of how many bytes the
  815. sink can handle. */
  816. for (c_written = 0;
  817. c_written < size;
  818. c_written += c_result)
  819. {
  820. result = scm_call_3 (write_proc, bv,
  821. scm_from_size_t (c_written),
  822. scm_from_size_t (size - c_written));
  823. c_result = scm_to_long (result);
  824. if (SCM_UNLIKELY (c_result < 0
  825. || (size_t) c_result > (size - c_written)))
  826. scm_wrong_type_arg_msg (FUNC_NAME, 0, result,
  827. "R6RS custom binary output port `write!' "
  828. "returned a incorrect integer");
  829. }
  830. }
  831. #undef FUNC_NAME
  832. SCM_DEFINE (scm_make_custom_binary_output_port,
  833. "make-custom-binary-output-port", 5, 0, 0,
  834. (SCM id, SCM write_proc, SCM get_position_proc,
  835. SCM set_position_proc, SCM close_proc),
  836. "Return a new custom binary output port whose output is drained "
  837. "by invoking @var{write_proc} and passing it a bytevector, an "
  838. "index where octets should be written, and an octet count.")
  839. #define FUNC_NAME s_scm_make_custom_binary_output_port
  840. {
  841. SCM_VALIDATE_STRING (1, id);
  842. SCM_VALIDATE_PROC (2, write_proc);
  843. if (!scm_is_false (get_position_proc))
  844. SCM_VALIDATE_PROC (3, get_position_proc);
  845. if (!scm_is_false (set_position_proc))
  846. SCM_VALIDATE_PROC (4, set_position_proc);
  847. if (!scm_is_false (close_proc))
  848. SCM_VALIDATE_PROC (5, close_proc);
  849. return (make_cbop (write_proc, get_position_proc, set_position_proc,
  850. close_proc));
  851. }
  852. #undef FUNC_NAME
  853. /* Instantiate the custom binary output port type. */
  854. static inline void
  855. initialize_custom_binary_output_ports (void)
  856. {
  857. custom_binary_output_port_type =
  858. scm_make_port_type ("r6rs-custom-binary-output-port",
  859. NULL, cbop_write);
  860. scm_set_port_seek (custom_binary_output_port_type, cbp_seek);
  861. scm_set_port_close (custom_binary_output_port_type, cbp_close);
  862. }
  863. /* Transcoded ports ("tp" for short). */
  864. static scm_t_bits transcoded_port_type = 0;
  865. #define TP_INPUT_BUFFER_SIZE 4096
  866. #define SCM_TP_BINARY_PORT(_port) SCM_PACK (SCM_STREAM (_port))
  867. static inline SCM
  868. make_tp (SCM binary_port, unsigned long mode)
  869. {
  870. SCM port;
  871. scm_t_port *c_port;
  872. const unsigned long mode_bits = SCM_OPN | mode;
  873. scm_i_pthread_mutex_lock (&scm_i_port_table_mutex);
  874. port = scm_new_port_table_entry (transcoded_port_type);
  875. SCM_SETSTREAM (port, SCM_UNPACK (binary_port));
  876. SCM_SET_CELL_TYPE (port, transcoded_port_type | mode_bits);
  877. if (SCM_INPUT_PORT_P (port))
  878. {
  879. c_port = SCM_PTAB_ENTRY (port);
  880. c_port->read_buf = scm_gc_malloc_pointerless (TP_INPUT_BUFFER_SIZE,
  881. "port buffer");
  882. c_port->read_pos = c_port->read_end = c_port->read_buf;
  883. c_port->read_buf_size = TP_INPUT_BUFFER_SIZE;
  884. SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) & ~SCM_BUF0);
  885. }
  886. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  887. return port;
  888. }
  889. static void
  890. tp_write (SCM port, const void *data, size_t size)
  891. {
  892. scm_c_write (SCM_TP_BINARY_PORT (port), data, size);
  893. }
  894. static int
  895. tp_fill_input (SCM port)
  896. {
  897. size_t count;
  898. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  899. SCM bport = SCM_TP_BINARY_PORT (port);
  900. scm_t_port *c_bport = SCM_PTAB_ENTRY (bport);
  901. /* We can't use `scm_c_read' here, since it blocks until the whole
  902. block has been read or EOF. */
  903. if (c_bport->rw_active == SCM_PORT_WRITE)
  904. scm_force_output (bport);
  905. if (c_bport->read_pos >= c_bport->read_end)
  906. scm_fill_input (bport);
  907. count = c_bport->read_end - c_bport->read_pos;
  908. if (count > c_port->read_buf_size)
  909. count = c_port->read_buf_size;
  910. memcpy (c_port->read_buf, c_bport->read_pos, count);
  911. c_bport->read_pos += count;
  912. if (c_bport->rw_random)
  913. c_bport->rw_active = SCM_PORT_READ;
  914. if (count == 0)
  915. return EOF;
  916. else
  917. {
  918. c_port->read_pos = c_port->read_buf;
  919. c_port->read_end = c_port->read_buf + count;
  920. return *c_port->read_buf;
  921. }
  922. }
  923. static void
  924. tp_flush (SCM port)
  925. {
  926. SCM binary_port = SCM_TP_BINARY_PORT (port);
  927. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  928. size_t count = c_port->write_pos - c_port->write_buf;
  929. scm_c_write (binary_port, c_port->write_buf, count);
  930. c_port->write_pos = c_port->write_buf;
  931. c_port->rw_active = SCM_PORT_NEITHER;
  932. scm_force_output (binary_port);
  933. }
  934. static int
  935. tp_close (SCM port)
  936. {
  937. if (SCM_OUTPUT_PORT_P (port))
  938. tp_flush (port);
  939. return scm_is_true (scm_close_port (SCM_TP_BINARY_PORT (port))) ? 0 : -1;
  940. }
  941. static inline void
  942. initialize_transcoded_ports (void)
  943. {
  944. transcoded_port_type =
  945. scm_make_port_type ("r6rs-transcoded-port", tp_fill_input, tp_write);
  946. scm_set_port_flush (transcoded_port_type, tp_flush);
  947. scm_set_port_close (transcoded_port_type, tp_close);
  948. }
  949. SCM_DEFINE (scm_i_make_transcoded_port,
  950. "%make-transcoded-port", 1, 0, 0,
  951. (SCM port),
  952. "Return a new port which reads and writes to @var{port}")
  953. #define FUNC_NAME s_scm_i_make_transcoded_port
  954. {
  955. SCM result;
  956. unsigned long mode = 0;
  957. SCM_VALIDATE_PORT (SCM_ARG1, port);
  958. if (scm_is_true (scm_output_port_p (port)))
  959. mode |= SCM_WRTNG;
  960. else if (scm_is_true (scm_input_port_p (port)))
  961. mode |= SCM_RDNG;
  962. result = make_tp (port, mode);
  963. /* FIXME: We should actually close `port' "in a special way" here,
  964. according to R6RS. As there is no way to do that in Guile without
  965. rendering the underlying port unusable for our purposes as well, we
  966. just leave it open. */
  967. return result;
  968. }
  969. #undef FUNC_NAME
  970. /* Textual I/O */
  971. SCM_DEFINE (scm_get_string_n_x,
  972. "get-string-n!", 4, 0, 0,
  973. (SCM port, SCM str, SCM start, SCM count),
  974. "Read up to @var{count} characters from @var{port} into "
  975. "@var{str}, starting at @var{start}. If no characters "
  976. "can be read before the end of file is encountered, the end "
  977. "of file object is returned. Otherwise, the number of "
  978. "characters read is returned.")
  979. #define FUNC_NAME s_scm_get_string_n_x
  980. {
  981. size_t c_start, c_count, c_len, c_end, j;
  982. scm_t_wchar c;
  983. SCM_VALIDATE_OPINPORT (1, port);
  984. SCM_VALIDATE_STRING (2, str);
  985. c_len = scm_c_string_length (str);
  986. c_start = scm_to_size_t (start);
  987. c_count = scm_to_size_t (count);
  988. c_end = c_start + c_count;
  989. if (SCM_UNLIKELY (c_end > c_len))
  990. scm_out_of_range (FUNC_NAME, count);
  991. for (j = c_start; j < c_end; j++)
  992. {
  993. c = scm_getc (port);
  994. if (c == EOF)
  995. {
  996. size_t chars_read = j - c_start;
  997. return chars_read == 0 ? SCM_EOF_VAL : scm_from_size_t (chars_read);
  998. }
  999. scm_c_string_set_x (str, j, SCM_MAKE_CHAR (c));
  1000. }
  1001. return count;
  1002. }
  1003. #undef FUNC_NAME
  1004. /* Initialization. */
  1005. void
  1006. scm_register_r6rs_ports (void)
  1007. {
  1008. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1009. "scm_init_r6rs_ports",
  1010. (scm_t_extension_init_func) scm_init_r6rs_ports,
  1011. NULL);
  1012. }
  1013. void
  1014. scm_init_r6rs_ports (void)
  1015. {
  1016. #include "libguile/r6rs-ports.x"
  1017. initialize_bytevector_input_ports ();
  1018. initialize_custom_binary_input_ports ();
  1019. initialize_bytevector_output_ports ();
  1020. initialize_custom_binary_output_ports ();
  1021. initialize_transcoded_ports ();
  1022. }