r6rs-ports.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /* Copyright 2009-2011,2013-2015,2018-2019,2023,2024
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <assert.h>
  19. #include <intprops.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "boolean.h"
  24. #include "bytevectors.h"
  25. #include "chars.h"
  26. #include "eval.h"
  27. #include "extensions.h"
  28. #include "gsubr.h"
  29. #include "modules.h"
  30. #include "numbers.h"
  31. #include "ports-internal.h"
  32. #include "procs.h"
  33. #include "smob.h"
  34. #include "strings.h"
  35. #include "symbols.h"
  36. #include "syscalls.h"
  37. #include "threads.h"
  38. #include "values.h"
  39. #include "variable.h"
  40. #include "vectors.h"
  41. #include "version.h"
  42. #include "r6rs-ports.h"
  43. SCM_SYMBOL (sym_ISO_8859_1, "ISO-8859-1");
  44. SCM_SYMBOL (sym_error, "error");
  45. /* Unimplemented features. */
  46. /* Transoders are currently not implemented since Guile 1.8 is not
  47. Unicode-capable. Thus, most of the code here assumes the use of the
  48. binary transcoder. */
  49. static inline void
  50. transcoders_not_implemented (void)
  51. {
  52. fprintf (stderr, "%s: warning: transcoders not implemented\n",
  53. PACKAGE_NAME);
  54. }
  55. /* End-of-file object. */
  56. SCM_DEFINE (scm_eof_object, "eof-object", 0, 0, 0,
  57. (void),
  58. "Return the end-of-file object.")
  59. #define FUNC_NAME s_scm_eof_object
  60. {
  61. return SCM_EOF_VAL;
  62. }
  63. #undef FUNC_NAME
  64. /* Input ports. */
  65. #define MAX(A, B) ((A) >= (B) ? (A) : (B))
  66. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  67. /* Bytevector input ports. */
  68. static scm_t_port_type *bytevector_input_port_type = 0;
  69. struct bytevector_input_port {
  70. SCM bytevector;
  71. size_t pos;
  72. };
  73. static inline SCM
  74. make_bytevector_input_port (SCM bv)
  75. {
  76. const unsigned long mode_bits = SCM_RDNG;
  77. struct bytevector_input_port *stream;
  78. stream = scm_gc_typed_calloc (struct bytevector_input_port);
  79. stream->bytevector = bv;
  80. stream->pos = 0;
  81. return scm_c_make_port_with_encoding (bytevector_input_port_type, mode_bits,
  82. sym_ISO_8859_1, sym_error,
  83. (scm_t_bits) stream);
  84. }
  85. static size_t
  86. bytevector_input_port_read (SCM port, SCM dst, size_t start, size_t count)
  87. {
  88. size_t remaining;
  89. struct bytevector_input_port *stream = (void *) SCM_STREAM (port);
  90. if (stream->pos >= SCM_BYTEVECTOR_LENGTH (stream->bytevector))
  91. return 0;
  92. remaining = SCM_BYTEVECTOR_LENGTH (stream->bytevector) - stream->pos;
  93. if (remaining < count)
  94. count = remaining;
  95. memcpy (SCM_BYTEVECTOR_CONTENTS (dst) + start,
  96. SCM_BYTEVECTOR_CONTENTS (stream->bytevector) + stream->pos,
  97. count);
  98. stream->pos += count;
  99. return count;
  100. }
  101. static scm_t_off
  102. bytevector_input_port_seek (SCM port, scm_t_off offset, int whence)
  103. #define FUNC_NAME "bytevector_input_port_seek"
  104. {
  105. struct bytevector_input_port *stream = (void *) SCM_STREAM (port);
  106. size_t base;
  107. scm_t_off target;
  108. if (whence == SEEK_CUR)
  109. base = stream->pos;
  110. else if (whence == SEEK_SET)
  111. base = 0;
  112. else if (whence == SEEK_END)
  113. base = SCM_BYTEVECTOR_LENGTH (stream->bytevector);
  114. else
  115. scm_wrong_type_arg_msg (FUNC_NAME, 0, port, "invalid `seek' parameter");
  116. if (base > SCM_T_OFF_MAX
  117. || INT_ADD_OVERFLOW ((scm_t_off) base, offset))
  118. scm_num_overflow (FUNC_NAME);
  119. target = (scm_t_off) base + offset;
  120. if (target >= 0 && target <= SCM_BYTEVECTOR_LENGTH (stream->bytevector))
  121. stream->pos = target;
  122. else
  123. scm_out_of_range (FUNC_NAME, scm_from_off_t (offset));
  124. return target;
  125. }
  126. #undef FUNC_NAME
  127. /* Instantiate the bytevector input port type. */
  128. static inline void
  129. initialize_bytevector_input_ports (void)
  130. {
  131. bytevector_input_port_type =
  132. scm_make_port_type ("r6rs-bytevector-input-port",
  133. bytevector_input_port_read,
  134. NULL);
  135. scm_set_port_seek (bytevector_input_port_type, bytevector_input_port_seek);
  136. }
  137. SCM_DEFINE (scm_open_bytevector_input_port,
  138. "open-bytevector-input-port", 1, 1, 0,
  139. (SCM bv, SCM transcoder),
  140. "Return an input port whose contents are drawn from "
  141. "bytevector @var{bv}.")
  142. #define FUNC_NAME s_scm_open_bytevector_input_port
  143. {
  144. SCM_VALIDATE_BYTEVECTOR (1, bv);
  145. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  146. transcoders_not_implemented ();
  147. return make_bytevector_input_port (bv);
  148. }
  149. #undef FUNC_NAME
  150. /* Binary input. */
  151. /* We currently don't support specific binary input ports. */
  152. #define SCM_VALIDATE_BINARY_INPUT_PORT SCM_VALIDATE_OPINPORT
  153. SCM_DEFINE (scm_get_u8, "get-u8", 1, 0, 0,
  154. (SCM port),
  155. "Read an octet from @var{port}, a binary input port, "
  156. "blocking as necessary.")
  157. #define FUNC_NAME s_scm_get_u8
  158. {
  159. SCM result;
  160. int c_result;
  161. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  162. c_result = scm_get_byte_or_eof (port);
  163. if (c_result == EOF)
  164. result = SCM_EOF_VAL;
  165. else
  166. result = SCM_I_MAKINUM ((unsigned char) c_result);
  167. return result;
  168. }
  169. #undef FUNC_NAME
  170. SCM_DEFINE (scm_lookahead_u8, "lookahead-u8", 1, 0, 0,
  171. (SCM port),
  172. "Like @code{get-u8} but does not update @var{port} to "
  173. "point past the octet.")
  174. #define FUNC_NAME s_scm_lookahead_u8
  175. {
  176. int u8;
  177. SCM result;
  178. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  179. u8 = scm_peek_byte_or_eof (port);
  180. if (u8 == EOF)
  181. result = SCM_EOF_VAL;
  182. else
  183. result = SCM_I_MAKINUM ((uint8_t) u8);
  184. return result;
  185. }
  186. #undef FUNC_NAME
  187. SCM_DEFINE (scm_get_bytevector_n, "get-bytevector-n", 2, 0, 0,
  188. (SCM port, SCM count),
  189. "Read @var{count} octets from @var{port}, blocking as "
  190. "necessary and return a bytevector containing the octets "
  191. "read. If fewer bytes are available, a bytevector smaller "
  192. "than @var{count} is returned.")
  193. #define FUNC_NAME s_scm_get_bytevector_n
  194. {
  195. SCM result;
  196. size_t c_count;
  197. size_t c_read;
  198. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  199. c_count = scm_to_size_t (count);
  200. result = scm_c_make_bytevector (c_count);
  201. if (SCM_LIKELY (c_count > 0))
  202. /* XXX: `scm_c_read ()' does not update the port position. */
  203. c_read = scm_c_read_bytes (port, result, 0, c_count);
  204. else
  205. /* Don't invoke `scm_c_read ()' since it may block. */
  206. c_read = 0;
  207. if (c_read < c_count)
  208. {
  209. if (c_read == 0)
  210. result = SCM_EOF_VAL;
  211. else
  212. result = scm_c_shrink_bytevector (result, c_read);
  213. }
  214. return result;
  215. }
  216. #undef FUNC_NAME
  217. SCM_DEFINE (scm_get_bytevector_n_x, "get-bytevector-n!", 4, 0, 0,
  218. (SCM port, SCM bv, SCM start, SCM count),
  219. "Read @var{count} bytes from @var{port} and store them "
  220. "in @var{bv} starting at index @var{start}. Return either "
  221. "the number of bytes actually read or the end-of-file "
  222. "object.")
  223. #define FUNC_NAME s_scm_get_bytevector_n_x
  224. {
  225. SCM result;
  226. size_t c_start, c_count, c_len;
  227. size_t c_read;
  228. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  229. SCM_VALIDATE_BYTEVECTOR (2, bv);
  230. c_start = scm_to_size_t (start);
  231. c_count = scm_to_size_t (count);
  232. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  233. if (SCM_UNLIKELY (c_len < c_start
  234. || (c_len - c_start < c_count)))
  235. scm_out_of_range (FUNC_NAME, count);
  236. if (SCM_LIKELY (c_count > 0))
  237. c_read = scm_c_read_bytes (port, bv, c_start, c_count);
  238. else
  239. /* Don't invoke `scm_c_read ()' since it may block. */
  240. c_read = 0;
  241. if (c_read == 0 && c_count > 0)
  242. result = SCM_EOF_VAL;
  243. else
  244. result = scm_from_size_t (c_read);
  245. return result;
  246. }
  247. #undef FUNC_NAME
  248. SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-some", 1, 0, 0,
  249. (SCM port),
  250. "Read from @var{port}, blocking as necessary, until bytes "
  251. "are available or an end-of-file is reached. Return either "
  252. "the end-of-file object or a new bytevector containing some "
  253. "of the available bytes (at least one), and update the port "
  254. "position to point just past these bytes.")
  255. #define FUNC_NAME s_scm_get_bytevector_some
  256. {
  257. SCM buf;
  258. size_t cur, avail;
  259. SCM bv;
  260. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  261. buf = scm_fill_input (port, 0, &cur, &avail);
  262. if (avail == 0)
  263. {
  264. scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F);
  265. return SCM_EOF_VAL;
  266. }
  267. bv = scm_c_make_bytevector (avail);
  268. scm_port_buffer_take (buf, (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv),
  269. avail, cur, avail);
  270. return bv;
  271. }
  272. #undef FUNC_NAME
  273. SCM_DEFINE (scm_get_bytevector_some_x, "get-bytevector-some!", 4, 0, 0,
  274. (SCM port, SCM bv, SCM start, SCM count),
  275. "Read up to @var{count} bytes from @var{port}, blocking "
  276. "as necessary until at least one byte is available or an "
  277. "end-of-file is reached. Store them in @var{bv} starting "
  278. "at index @var{start}. Return the number of bytes actually "
  279. "read, or an end-of-file object.")
  280. #define FUNC_NAME s_scm_get_bytevector_some_x
  281. {
  282. SCM buf;
  283. size_t c_start, c_count, c_len;
  284. size_t cur, avail, transfer_size;
  285. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  286. SCM_VALIDATE_BYTEVECTOR (2, bv);
  287. c_start = scm_to_size_t (start);
  288. c_count = scm_to_size_t (count);
  289. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  290. if (SCM_UNLIKELY (c_len < c_start
  291. || c_len - c_start < c_count))
  292. scm_out_of_range (FUNC_NAME, count);
  293. if (c_count == 0)
  294. return SCM_INUM0;
  295. buf = scm_fill_input (port, 0, &cur, &avail);
  296. if (avail == 0)
  297. {
  298. scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F);
  299. return SCM_EOF_VAL;
  300. }
  301. transfer_size = MIN (avail, c_count);
  302. scm_port_buffer_take (buf,
  303. (uint8_t *) SCM_BYTEVECTOR_CONTENTS (bv) + c_start,
  304. transfer_size, cur, avail);
  305. return scm_from_size_t (transfer_size);
  306. }
  307. #undef FUNC_NAME
  308. static SCM get_bytevector_all_var;
  309. static void
  310. init_bytevector_io_vars (void)
  311. {
  312. get_bytevector_all_var =
  313. scm_c_public_lookup ("ice-9 binary-port", "get-bytevector-all");
  314. }
  315. SCM
  316. scm_get_bytevector_all (SCM port)
  317. {
  318. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  319. scm_i_pthread_once (&once, init_bytevector_io_vars);
  320. return scm_call_1 (scm_variable_ref (get_bytevector_all_var), port);
  321. }
  322. /* Binary output. */
  323. /* We currently don't support specific binary input ports. */
  324. #define SCM_VALIDATE_BINARY_OUTPUT_PORT SCM_VALIDATE_OPOUTPORT
  325. SCM_DEFINE (scm_put_u8, "put-u8", 2, 0, 0,
  326. (SCM port, SCM octet),
  327. "Write @var{octet} to binary port @var{port}.")
  328. #define FUNC_NAME s_scm_put_u8
  329. {
  330. uint8_t c_octet;
  331. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  332. c_octet = scm_to_uint8 (octet);
  333. scm_c_write (port, &c_octet, 1);
  334. return SCM_UNSPECIFIED;
  335. }
  336. #undef FUNC_NAME
  337. SCM_DEFINE (scm_put_bytevector, "put-bytevector", 2, 2, 0,
  338. (SCM port, SCM bv, SCM start, SCM count),
  339. "Write the contents of @var{bv} to @var{port}, optionally "
  340. "starting at index @var{start} and limiting to @var{count} "
  341. "octets.")
  342. #define FUNC_NAME s_scm_put_bytevector
  343. {
  344. size_t c_start, c_count, c_len;
  345. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  346. SCM_VALIDATE_BYTEVECTOR (2, bv);
  347. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  348. if (!scm_is_eq (start, SCM_UNDEFINED))
  349. {
  350. c_start = scm_to_size_t (start);
  351. if (SCM_UNLIKELY (c_start > c_len))
  352. scm_out_of_range (FUNC_NAME, start);
  353. if (!scm_is_eq (count, SCM_UNDEFINED))
  354. {
  355. c_count = scm_to_size_t (count);
  356. if (SCM_UNLIKELY (c_count > c_len - c_start))
  357. scm_out_of_range (FUNC_NAME, count);
  358. }
  359. else
  360. c_count = c_len - c_start;
  361. }
  362. else
  363. c_start = 0, c_count = c_len;
  364. scm_c_write_bytes (port, bv, c_start, c_count);
  365. return SCM_UNSPECIFIED;
  366. }
  367. #undef FUNC_NAME
  368. SCM_DEFINE (scm_unget_bytevector, "unget-bytevector", 2, 2, 0,
  369. (SCM port, SCM bv, SCM start, SCM count),
  370. "Unget the contents of @var{bv} to @var{port}, optionally "
  371. "starting at index @var{start} and limiting to @var{count} "
  372. "octets.")
  373. #define FUNC_NAME s_scm_unget_bytevector
  374. {
  375. unsigned char *c_bv;
  376. size_t c_start, c_count, c_len;
  377. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  378. SCM_VALIDATE_BYTEVECTOR (2, bv);
  379. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  380. c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  381. if (!scm_is_eq (start, SCM_UNDEFINED))
  382. {
  383. c_start = scm_to_size_t (start);
  384. if (SCM_UNLIKELY (c_start > c_len))
  385. scm_out_of_range (FUNC_NAME, start);
  386. if (!scm_is_eq (count, SCM_UNDEFINED))
  387. {
  388. c_count = scm_to_size_t (count);
  389. if (SCM_UNLIKELY (c_count > c_len - c_start))
  390. scm_out_of_range (FUNC_NAME, count);
  391. }
  392. else
  393. c_count = c_len - c_start;
  394. }
  395. else
  396. c_start = 0, c_count = c_len;
  397. scm_unget_bytes (c_bv + c_start, c_count, port);
  398. return SCM_UNSPECIFIED;
  399. }
  400. #undef FUNC_NAME
  401. /* Bytevector output port. */
  402. /* Implementation of "bytevector output ports".
  403. Each bytevector output port has an internal buffer, of type
  404. `scm_t_bytevector_output_port_buffer', attached to it. The procedure
  405. returned along with the output port is actually an applicable SMOB.
  406. The SMOB holds a reference to the port. When applied, the SMOB
  407. swallows the port's internal buffer, turning it into a bytevector,
  408. and resets it.
  409. XXX: Access to a bytevector output port's internal buffer is not
  410. thread-safe. */
  411. static scm_t_port_type *bytevector_output_port_type = 0;
  412. SCM_SMOB (bytevector_output_port_procedure,
  413. "r6rs-bytevector-output-port-procedure",
  414. 0);
  415. #define SCM_GC_BYTEVECTOR_OUTPUT_PORT "r6rs-bytevector-output-port"
  416. #define SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER_INITIAL_SIZE 4096
  417. /* Representation of a bytevector output port's internal buffer. */
  418. typedef struct
  419. {
  420. size_t total_len;
  421. size_t len;
  422. size_t pos;
  423. char *buffer;
  424. /* The get-bytevector procedure will flush this port, if it's
  425. open. */
  426. SCM port;
  427. } scm_t_bytevector_output_port_buffer;
  428. /* Accessing a bytevector output port's buffer. */
  429. #define SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER(_port) \
  430. ((scm_t_bytevector_output_port_buffer *) SCM_STREAM (_port))
  431. #define SCM_SET_BYTEVECTOR_OUTPUT_PORT_BUFFER(_port, _buf) \
  432. (SCM_SETSTREAM ((_port), (scm_t_bits) (_buf)))
  433. static inline void
  434. bytevector_output_port_buffer_init (scm_t_bytevector_output_port_buffer *buf)
  435. {
  436. buf->total_len = buf->len = buf->pos = 0;
  437. buf->buffer = NULL;
  438. /* Don't clear the port. */
  439. }
  440. static inline void
  441. bytevector_output_port_buffer_grow (scm_t_bytevector_output_port_buffer *buf,
  442. size_t min_size)
  443. {
  444. char *new_buf;
  445. size_t new_size;
  446. if (buf->buffer)
  447. {
  448. if (INT_ADD_OVERFLOW (buf->total_len, buf->total_len))
  449. scm_num_overflow ("bytevector_output_port_buffer_grow");
  450. new_size = MAX (min_size, buf->total_len * 2);
  451. new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len,
  452. new_size, SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  453. }
  454. else
  455. {
  456. new_size = MAX (min_size, SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER_INITIAL_SIZE);
  457. new_buf = scm_gc_malloc_pointerless (new_size,
  458. SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  459. }
  460. buf->buffer = new_buf;
  461. buf->total_len = new_size;
  462. }
  463. static inline SCM
  464. make_bytevector_output_port (void)
  465. {
  466. SCM port, proc;
  467. scm_t_bytevector_output_port_buffer *buf;
  468. const unsigned long mode_bits = SCM_WRTNG;
  469. buf = (scm_t_bytevector_output_port_buffer *)
  470. scm_gc_malloc (sizeof (* buf), SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  471. bytevector_output_port_buffer_init (buf);
  472. port = scm_c_make_port_with_encoding (bytevector_output_port_type,
  473. mode_bits,
  474. sym_ISO_8859_1, sym_error,
  475. (scm_t_bits)buf);
  476. buf->port = port;
  477. SCM_NEWSMOB (proc, bytevector_output_port_procedure, buf);
  478. return scm_values_2 (port, proc);
  479. }
  480. /* Write octets from WRITE_BUF to the backing store. */
  481. static size_t
  482. bytevector_output_port_write (SCM port, SCM src, size_t start, size_t count)
  483. #define FUNC_NAME "bytevector_output_port_write"
  484. {
  485. scm_t_bytevector_output_port_buffer *buf;
  486. buf = SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER (port);
  487. if (count > buf->total_len - buf->pos)
  488. {
  489. if (INT_ADD_OVERFLOW (buf->pos, count))
  490. scm_num_overflow (FUNC_NAME);
  491. bytevector_output_port_buffer_grow (buf, buf->pos + count);
  492. }
  493. memcpy (buf->buffer + buf->pos, SCM_BYTEVECTOR_CONTENTS (src) + start, count);
  494. buf->pos += count;
  495. buf->len = (buf->len > buf->pos) ? buf->len : buf->pos;
  496. return count;
  497. }
  498. #undef FUNC_NAME
  499. static scm_t_off
  500. bytevector_output_port_seek (SCM port, scm_t_off offset, int whence)
  501. #define FUNC_NAME "bytevector_output_port_seek"
  502. {
  503. scm_t_bytevector_output_port_buffer *buf;
  504. size_t base;
  505. scm_t_off target;
  506. buf = SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER (port);
  507. if (whence == SEEK_CUR)
  508. base = buf->pos;
  509. else if (whence == SEEK_SET)
  510. base = 0;
  511. else if (whence == SEEK_END)
  512. base = buf->len;
  513. else
  514. scm_wrong_type_arg_msg (FUNC_NAME, 0, port, "invalid `seek' parameter");
  515. if (base > SCM_T_OFF_MAX
  516. || INT_ADD_OVERFLOW ((scm_t_off) base, offset))
  517. scm_num_overflow (FUNC_NAME);
  518. target = (scm_t_off) base + offset;
  519. if (target >= 0 && target <= buf->len)
  520. buf->pos = target;
  521. else
  522. scm_out_of_range (FUNC_NAME, scm_from_off_t (offset));
  523. return target;
  524. }
  525. #undef FUNC_NAME
  526. /* Fetch data from a bytevector output port. */
  527. SCM_SMOB_APPLY (bytevector_output_port_procedure,
  528. bytevector_output_port_proc_apply, 0, 0, 0, (SCM proc))
  529. {
  530. SCM bv;
  531. scm_t_bytevector_output_port_buffer *buf, result_buf;
  532. buf = (scm_t_bytevector_output_port_buffer *) SCM_SMOB_DATA (proc);
  533. if (SCM_OPPORTP (buf->port))
  534. scm_flush (buf->port);
  535. result_buf = *buf;
  536. bytevector_output_port_buffer_init (buf);
  537. if (result_buf.len == 0)
  538. bv = scm_c_take_gc_bytevector (NULL, 0, SCM_BOOL_F);
  539. else
  540. {
  541. if (result_buf.total_len > result_buf.len)
  542. /* Shrink the buffer. */
  543. result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer,
  544. result_buf.total_len,
  545. result_buf.len,
  546. SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  547. bv = scm_c_take_gc_bytevector ((signed char *) result_buf.buffer,
  548. result_buf.len, SCM_BOOL_F);
  549. }
  550. return bv;
  551. }
  552. SCM_DEFINE (scm_open_bytevector_output_port,
  553. "open-bytevector-output-port", 0, 1, 0,
  554. (SCM transcoder),
  555. "Return two values: an output port and a procedure. The latter "
  556. "should be called with zero arguments to obtain a bytevector "
  557. "containing the data accumulated by the port.")
  558. #define FUNC_NAME s_scm_open_bytevector_output_port
  559. {
  560. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  561. transcoders_not_implemented ();
  562. return make_bytevector_output_port ();
  563. }
  564. #undef FUNC_NAME
  565. static inline void
  566. initialize_bytevector_output_ports (void)
  567. {
  568. bytevector_output_port_type =
  569. scm_make_port_type ("r6rs-bytevector-output-port",
  570. NULL, bytevector_output_port_write);
  571. scm_set_port_seek (bytevector_output_port_type, bytevector_output_port_seek);
  572. }
  573. /* Custom ports. */
  574. static SCM make_custom_binary_input_port_var;
  575. static SCM make_custom_binary_output_port_var;
  576. static SCM make_custom_binary_input_output_port_var;
  577. static scm_i_pthread_once_t make_custom_binary_port_vars =
  578. SCM_I_PTHREAD_ONCE_INIT;
  579. static void
  580. init_make_custom_binary_port_vars (void)
  581. {
  582. SCM mod = scm_c_resolve_module ("ice-9 binary-ports");
  583. SCM iface = scm_module_public_interface (mod);
  584. make_custom_binary_input_port_var =
  585. scm_c_module_lookup (iface, "make-custom-binary-input-port");
  586. make_custom_binary_output_port_var =
  587. scm_c_module_lookup (iface, "make-custom-binary-output-port");
  588. make_custom_binary_input_output_port_var =
  589. scm_c_module_lookup (iface, "make-custom-binary-input/output-port");
  590. }
  591. SCM scm_make_custom_binary_input_port (SCM id, SCM read_proc,
  592. SCM get_position_proc,
  593. SCM set_position_proc, SCM close_proc) {
  594. scm_i_pthread_once (&make_custom_binary_port_vars,
  595. init_make_custom_binary_port_vars);
  596. return scm_call_5 (scm_variable_ref (make_custom_binary_input_port_var),
  597. id, read_proc, get_position_proc, set_position_proc,
  598. close_proc);
  599. }
  600. SCM scm_make_custom_binary_output_port (SCM id, SCM write_proc,
  601. SCM get_position_proc,
  602. SCM set_position_proc, SCM close_proc) {
  603. scm_i_pthread_once (&make_custom_binary_port_vars,
  604. init_make_custom_binary_port_vars);
  605. return scm_call_5 (scm_variable_ref (make_custom_binary_output_port_var),
  606. id, write_proc, get_position_proc, set_position_proc,
  607. close_proc);
  608. }
  609. SCM scm_make_custom_binary_input_output_port (SCM id, SCM read_proc,
  610. SCM write_proc,
  611. SCM get_position_proc,
  612. SCM set_position_proc,
  613. SCM close_proc) {
  614. scm_i_pthread_once (&make_custom_binary_port_vars,
  615. init_make_custom_binary_port_vars);
  616. return scm_call_6 (scm_variable_ref (make_custom_binary_input_output_port_var),
  617. id, read_proc, write_proc, get_position_proc,
  618. set_position_proc, close_proc);
  619. }
  620. /* Transcoded ports. */
  621. static scm_t_port_type *transcoded_port_type = 0;
  622. #define SCM_TRANSCODED_PORT_BINARY_PORT(_port) SCM_PACK (SCM_STREAM (_port))
  623. static inline SCM
  624. make_transcoded_port (SCM binary_port, unsigned long mode)
  625. {
  626. return scm_c_make_port (transcoded_port_type, mode,
  627. SCM_UNPACK (binary_port));
  628. }
  629. static size_t
  630. transcoded_port_write (SCM port, SCM src, size_t start, size_t count)
  631. {
  632. SCM bport = SCM_TRANSCODED_PORT_BINARY_PORT (port);
  633. scm_c_write_bytes (bport, src, start, count);
  634. return count;
  635. }
  636. static size_t
  637. transcoded_port_read (SCM port, SCM dst, size_t start, size_t count)
  638. {
  639. SCM bport = SCM_TRANSCODED_PORT_BINARY_PORT (port);
  640. return scm_c_read_bytes (bport, dst, start, count);
  641. }
  642. static void
  643. transcoded_port_close (SCM port)
  644. {
  645. scm_close_port (SCM_TRANSCODED_PORT_BINARY_PORT (port));
  646. }
  647. static inline void
  648. initialize_transcoded_ports (void)
  649. {
  650. transcoded_port_type =
  651. scm_make_port_type ("r6rs-transcoded-port", transcoded_port_read,
  652. transcoded_port_write);
  653. scm_set_port_close (transcoded_port_type, transcoded_port_close);
  654. scm_set_port_needs_close_on_gc (transcoded_port_type, 1);
  655. }
  656. SCM_INTERNAL SCM scm_i_make_transcoded_port (SCM);
  657. SCM_DEFINE (scm_i_make_transcoded_port,
  658. "%make-transcoded-port", 1, 0, 0,
  659. (SCM port),
  660. "Return a new port which reads and writes to @var{port}")
  661. #define FUNC_NAME s_scm_i_make_transcoded_port
  662. {
  663. SCM result;
  664. unsigned long mode = 0;
  665. SCM_VALIDATE_PORT (SCM_ARG1, port);
  666. if (scm_is_true (scm_output_port_p (port)))
  667. mode |= SCM_WRTNG;
  668. if (scm_is_true (scm_input_port_p (port)))
  669. mode |= SCM_RDNG;
  670. result = make_transcoded_port (port, mode);
  671. /* FIXME: We should actually close `port' "in a special way" here,
  672. according to R6RS. As there is no way to do that in Guile without
  673. rendering the underlying port unusable for our purposes as well, we
  674. just leave it open. */
  675. return result;
  676. }
  677. #undef FUNC_NAME
  678. /* Textual I/O */
  679. SCM_DEFINE (scm_get_string_n_x,
  680. "get-string-n!", 4, 0, 0,
  681. (SCM port, SCM str, SCM start, SCM count),
  682. "Read up to @var{count} characters from @var{port} into "
  683. "@var{str}, starting at @var{start}. If no characters "
  684. "can be read before the end of file is encountered, the end "
  685. "of file object is returned. Otherwise, the number of "
  686. "characters read is returned.")
  687. #define FUNC_NAME s_scm_get_string_n_x
  688. {
  689. size_t c_start, c_count, c_len, c_end, j;
  690. scm_t_wchar c;
  691. SCM_VALIDATE_OPINPORT (1, port);
  692. SCM_VALIDATE_STRING (2, str);
  693. c_len = scm_c_string_length (str);
  694. c_start = scm_to_size_t (start);
  695. c_count = scm_to_size_t (count);
  696. c_end = c_start + c_count;
  697. if (SCM_UNLIKELY (c_end > c_len))
  698. scm_out_of_range (FUNC_NAME, count);
  699. for (j = c_start; j < c_end; j++)
  700. {
  701. c = scm_getc (port);
  702. if (c == EOF)
  703. {
  704. size_t chars_read = j - c_start;
  705. return chars_read == 0 ? SCM_EOF_VAL : scm_from_size_t (chars_read);
  706. }
  707. scm_c_string_set_x (str, j, SCM_MAKE_CHAR (c));
  708. }
  709. return count;
  710. }
  711. #undef FUNC_NAME
  712. /* Initialization. */
  713. void
  714. scm_register_r6rs_ports (void)
  715. {
  716. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  717. "scm_init_r6rs_ports",
  718. (scm_t_extension_init_func) scm_init_r6rs_ports,
  719. NULL);
  720. initialize_bytevector_input_ports ();
  721. initialize_bytevector_output_ports ();
  722. initialize_transcoded_ports ();
  723. }
  724. void
  725. scm_init_r6rs_ports (void)
  726. {
  727. #include "r6rs-ports.x"
  728. }