r6rs-ports.c 36 KB

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