r6rs-ports.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /* Copyright (C) 2009 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. port = scm_new_port_table_entry (bytevector_input_port_type);
  70. /* Prevent BV from being GC'd. */
  71. SCM_SETSTREAM (port, SCM_UNPACK (bv));
  72. /* Have the port directly access the bytevector. */
  73. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  74. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  75. c_port = SCM_PTAB_ENTRY (port);
  76. c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
  77. c_port->read_end = (unsigned char *) c_bv + c_len;
  78. c_port->read_buf_size = c_len;
  79. /* Mark PORT as open, readable and unbuffered (hmm, how elegant...). */
  80. SCM_SET_CELL_TYPE (port, bytevector_input_port_type | mode_bits);
  81. return port;
  82. }
  83. static SCM
  84. bip_mark (SCM port)
  85. {
  86. /* Mark the underlying bytevector. */
  87. return (SCM_PACK (SCM_STREAM (port)));
  88. }
  89. static int
  90. bip_fill_input (SCM port)
  91. {
  92. int result;
  93. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  94. if (c_port->read_pos >= c_port->read_end)
  95. result = EOF;
  96. else
  97. result = (int) *c_port->read_pos;
  98. return result;
  99. }
  100. static scm_t_off
  101. bip_seek (SCM port, scm_t_off offset, int whence)
  102. #define FUNC_NAME "bip_seek"
  103. {
  104. scm_t_off c_result = 0;
  105. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  106. switch (whence)
  107. {
  108. case SEEK_CUR:
  109. offset += c_port->read_pos - c_port->read_buf;
  110. /* Fall through. */
  111. case SEEK_SET:
  112. if (c_port->read_buf + offset < c_port->read_end)
  113. {
  114. c_port->read_pos = c_port->read_buf + offset;
  115. c_result = offset;
  116. }
  117. else
  118. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  119. break;
  120. case SEEK_END:
  121. if (c_port->read_end - offset >= c_port->read_buf)
  122. {
  123. c_port->read_pos = c_port->read_end - offset;
  124. c_result = c_port->read_pos - c_port->read_buf;
  125. }
  126. else
  127. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  128. break;
  129. default:
  130. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  131. "invalid `seek' parameter");
  132. }
  133. return c_result;
  134. }
  135. #undef FUNC_NAME
  136. /* Instantiate the bytevector input port type. */
  137. static inline void
  138. initialize_bytevector_input_ports (void)
  139. {
  140. bytevector_input_port_type =
  141. scm_make_port_type ("r6rs-bytevector-input-port", bip_fill_input,
  142. NULL);
  143. scm_set_port_mark (bytevector_input_port_type, bip_mark);
  144. scm_set_port_seek (bytevector_input_port_type, bip_seek);
  145. }
  146. SCM_DEFINE (scm_open_bytevector_input_port,
  147. "open-bytevector-input-port", 1, 1, 0,
  148. (SCM bv, SCM transcoder),
  149. "Return an input port whose contents are drawn from "
  150. "bytevector @var{bv}.")
  151. #define FUNC_NAME s_scm_open_bytevector_input_port
  152. {
  153. SCM_VALIDATE_BYTEVECTOR (1, bv);
  154. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  155. transcoders_not_implemented ();
  156. return (make_bip (bv));
  157. }
  158. #undef FUNC_NAME
  159. /* Custom binary ports. The following routines are shared by input and
  160. output custom binary ports. */
  161. #define SCM_CBP_GET_POSITION_PROC(_port) \
  162. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 1)
  163. #define SCM_CBP_SET_POSITION_PROC(_port) \
  164. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 2)
  165. #define SCM_CBP_CLOSE_PROC(_port) \
  166. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 3)
  167. static SCM
  168. cbp_mark (SCM port)
  169. {
  170. /* Mark the underlying method and object vector. */
  171. if (SCM_OPENP (port))
  172. return SCM_PACK (SCM_STREAM (port));
  173. else
  174. return SCM_BOOL_F;
  175. }
  176. static scm_t_off
  177. cbp_seek (SCM port, scm_t_off offset, int whence)
  178. #define FUNC_NAME "cbp_seek"
  179. {
  180. SCM result;
  181. scm_t_off c_result = 0;
  182. switch (whence)
  183. {
  184. case SEEK_CUR:
  185. {
  186. SCM get_position_proc;
  187. get_position_proc = SCM_CBP_GET_POSITION_PROC (port);
  188. if (SCM_LIKELY (scm_is_true (get_position_proc)))
  189. result = scm_call_0 (get_position_proc);
  190. else
  191. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  192. "R6RS custom binary port does not "
  193. "support `port-position'");
  194. offset += scm_to_int (result);
  195. /* Fall through. */
  196. }
  197. case SEEK_SET:
  198. {
  199. SCM set_position_proc;
  200. set_position_proc = SCM_CBP_SET_POSITION_PROC (port);
  201. if (SCM_LIKELY (scm_is_true (set_position_proc)))
  202. result = scm_call_1 (set_position_proc, scm_from_int (offset));
  203. else
  204. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  205. "R6RS custom binary port does not "
  206. "support `set-port-position!'");
  207. /* Assuming setting the position succeeded. */
  208. c_result = offset;
  209. break;
  210. }
  211. default:
  212. /* `SEEK_END' cannot be supported. */
  213. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  214. "R6RS custom binary ports do not "
  215. "support `SEEK_END'");
  216. }
  217. return c_result;
  218. }
  219. #undef FUNC_NAME
  220. static int
  221. cbp_close (SCM port)
  222. {
  223. SCM close_proc;
  224. close_proc = SCM_CBP_CLOSE_PROC (port);
  225. if (scm_is_true (close_proc))
  226. /* Invoke the `close' thunk. */
  227. scm_call_0 (close_proc);
  228. return 1;
  229. }
  230. /* Custom binary input port ("cbip" for short). */
  231. static scm_t_bits custom_binary_input_port_type = 0;
  232. /* Size of the buffer embedded in custom binary input ports. */
  233. #define CBIP_BUFFER_SIZE 4096
  234. /* Return the bytevector associated with PORT. */
  235. #define SCM_CBIP_BYTEVECTOR(_port) \
  236. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 4)
  237. /* Return the various procedures of PORT. */
  238. #define SCM_CBIP_READ_PROC(_port) \
  239. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
  240. static inline SCM
  241. make_cbip (SCM read_proc, SCM get_position_proc,
  242. SCM set_position_proc, SCM close_proc)
  243. {
  244. SCM port, bv, method_vector;
  245. char *c_bv;
  246. unsigned c_len;
  247. scm_t_port *c_port;
  248. const unsigned long mode_bits = SCM_OPN | SCM_RDNG;
  249. /* Use a bytevector as the underlying buffer. */
  250. c_len = CBIP_BUFFER_SIZE;
  251. bv = scm_c_make_bytevector (c_len);
  252. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  253. /* Store the various methods and bytevector in a vector. */
  254. method_vector = scm_c_make_vector (5, SCM_BOOL_F);
  255. SCM_SIMPLE_VECTOR_SET (method_vector, 4, bv);
  256. SCM_SIMPLE_VECTOR_SET (method_vector, 0, read_proc);
  257. SCM_SIMPLE_VECTOR_SET (method_vector, 1, get_position_proc);
  258. SCM_SIMPLE_VECTOR_SET (method_vector, 2, set_position_proc);
  259. SCM_SIMPLE_VECTOR_SET (method_vector, 3, close_proc);
  260. port = scm_new_port_table_entry (custom_binary_input_port_type);
  261. /* Attach it the method vector. */
  262. SCM_SETSTREAM (port, SCM_UNPACK (method_vector));
  263. /* Have the port directly access the buffer (bytevector). */
  264. c_port = SCM_PTAB_ENTRY (port);
  265. c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
  266. c_port->read_end = (unsigned char *) c_bv;
  267. c_port->read_buf_size = c_len;
  268. /* Mark PORT as open, readable and unbuffered (hmm, how elegant...). */
  269. SCM_SET_CELL_TYPE (port, custom_binary_input_port_type | mode_bits);
  270. return port;
  271. }
  272. static int
  273. cbip_fill_input (SCM port)
  274. #define FUNC_NAME "cbip_fill_input"
  275. {
  276. int result;
  277. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  278. again:
  279. if (c_port->read_pos >= c_port->read_end)
  280. {
  281. /* Invoke the user's `read!' procedure. */
  282. unsigned c_octets;
  283. SCM bv, read_proc, octets;
  284. /* Use the bytevector associated with PORT as the buffer passed to the
  285. `read!' procedure, thereby avoiding additional allocations. */
  286. bv = SCM_CBIP_BYTEVECTOR (port);
  287. read_proc = SCM_CBIP_READ_PROC (port);
  288. /* The assumption here is that C_PORT's internal buffer wasn't changed
  289. behind our back. */
  290. assert (c_port->read_buf ==
  291. (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv));
  292. assert ((unsigned) c_port->read_buf_size
  293. == SCM_BYTEVECTOR_LENGTH (bv));
  294. octets = scm_call_3 (read_proc, bv, SCM_INUM0,
  295. SCM_I_MAKINUM (CBIP_BUFFER_SIZE));
  296. c_octets = scm_to_uint (octets);
  297. c_port->read_pos = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  298. c_port->read_end = (unsigned char *) c_port->read_pos + c_octets;
  299. if (c_octets > 0)
  300. goto again;
  301. else
  302. result = EOF;
  303. }
  304. else
  305. result = (int) *c_port->read_pos;
  306. return result;
  307. }
  308. #undef FUNC_NAME
  309. SCM_DEFINE (scm_make_custom_binary_input_port,
  310. "make-custom-binary-input-port", 5, 0, 0,
  311. (SCM id, SCM read_proc, SCM get_position_proc,
  312. SCM set_position_proc, SCM close_proc),
  313. "Return a new custom binary input port whose input is drained "
  314. "by invoking @var{read_proc} and passing it a bytevector, an "
  315. "index where octets should be written, and an octet count.")
  316. #define FUNC_NAME s_scm_make_custom_binary_input_port
  317. {
  318. SCM_VALIDATE_STRING (1, id);
  319. SCM_VALIDATE_PROC (2, read_proc);
  320. if (!scm_is_false (get_position_proc))
  321. SCM_VALIDATE_PROC (3, get_position_proc);
  322. if (!scm_is_false (set_position_proc))
  323. SCM_VALIDATE_PROC (4, set_position_proc);
  324. if (!scm_is_false (close_proc))
  325. SCM_VALIDATE_PROC (5, close_proc);
  326. return (make_cbip (read_proc, get_position_proc, set_position_proc,
  327. close_proc));
  328. }
  329. #undef FUNC_NAME
  330. /* Instantiate the custom binary input port type. */
  331. static inline void
  332. initialize_custom_binary_input_ports (void)
  333. {
  334. custom_binary_input_port_type =
  335. scm_make_port_type ("r6rs-custom-binary-input-port",
  336. cbip_fill_input, NULL);
  337. scm_set_port_mark (custom_binary_input_port_type, cbp_mark);
  338. scm_set_port_seek (custom_binary_input_port_type, cbp_seek);
  339. scm_set_port_close (custom_binary_input_port_type, cbp_close);
  340. }
  341. /* Binary input. */
  342. /* We currently don't support specific binary input ports. */
  343. #define SCM_VALIDATE_BINARY_INPUT_PORT SCM_VALIDATE_OPINPORT
  344. SCM_DEFINE (scm_get_u8, "get-u8", 1, 0, 0,
  345. (SCM port),
  346. "Read an octet from @var{port}, a binary input port, "
  347. "blocking as necessary.")
  348. #define FUNC_NAME s_scm_get_u8
  349. {
  350. SCM result;
  351. int c_result;
  352. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  353. c_result = scm_getc (port);
  354. if (c_result == EOF)
  355. result = SCM_EOF_VAL;
  356. else
  357. result = SCM_I_MAKINUM ((unsigned char) c_result);
  358. return result;
  359. }
  360. #undef FUNC_NAME
  361. SCM_DEFINE (scm_lookahead_u8, "lookahead-u8", 1, 0, 0,
  362. (SCM port),
  363. "Like @code{get-u8} but does not update @var{port} to "
  364. "point past the octet.")
  365. #define FUNC_NAME s_scm_lookahead_u8
  366. {
  367. SCM result;
  368. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  369. result = scm_peek_char (port);
  370. if (SCM_CHARP (result))
  371. result = SCM_I_MAKINUM ((signed char) SCM_CHAR (result));
  372. else
  373. result = SCM_EOF_VAL;
  374. return result;
  375. }
  376. #undef FUNC_NAME
  377. SCM_DEFINE (scm_get_bytevector_n, "get-bytevector-n", 2, 0, 0,
  378. (SCM port, SCM count),
  379. "Read @var{count} octets from @var{port}, blocking as "
  380. "necessary and return a bytevector containing the octets "
  381. "read. If fewer bytes are available, a bytevector smaller "
  382. "than @var{count} is returned.")
  383. #define FUNC_NAME s_scm_get_bytevector_n
  384. {
  385. SCM result;
  386. char *c_bv;
  387. unsigned c_count;
  388. size_t c_read;
  389. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  390. c_count = scm_to_uint (count);
  391. result = scm_c_make_bytevector (c_count);
  392. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (result);
  393. if (SCM_LIKELY (c_count > 0))
  394. /* XXX: `scm_c_read ()' does not update the port position. */
  395. c_read = scm_c_read (port, c_bv, c_count);
  396. else
  397. /* Don't invoke `scm_c_read ()' since it may block. */
  398. c_read = 0;
  399. if ((c_read == 0) && (c_count > 0))
  400. {
  401. if (SCM_EOF_OBJECT_P (scm_peek_char (port)))
  402. result = SCM_EOF_VAL;
  403. else
  404. result = scm_null_bytevector;
  405. }
  406. else
  407. {
  408. if (c_read < c_count)
  409. result = scm_c_shrink_bytevector (result, c_read);
  410. }
  411. return result;
  412. }
  413. #undef FUNC_NAME
  414. SCM_DEFINE (scm_get_bytevector_n_x, "get-bytevector-n!", 4, 0, 0,
  415. (SCM port, SCM bv, SCM start, SCM count),
  416. "Read @var{count} bytes from @var{port} and store them "
  417. "in @var{bv} starting at index @var{start}. Return either "
  418. "the number of bytes actually read or the end-of-file "
  419. "object.")
  420. #define FUNC_NAME s_scm_get_bytevector_n_x
  421. {
  422. SCM result;
  423. char *c_bv;
  424. unsigned c_start, c_count, c_len;
  425. size_t c_read;
  426. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  427. SCM_VALIDATE_BYTEVECTOR (2, bv);
  428. c_start = scm_to_uint (start);
  429. c_count = scm_to_uint (count);
  430. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  431. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  432. if (SCM_UNLIKELY (c_start + c_count > c_len))
  433. scm_out_of_range (FUNC_NAME, count);
  434. if (SCM_LIKELY (c_count > 0))
  435. c_read = scm_c_read (port, c_bv + c_start, c_count);
  436. else
  437. /* Don't invoke `scm_c_read ()' since it may block. */
  438. c_read = 0;
  439. if ((c_read == 0) && (c_count > 0))
  440. {
  441. if (SCM_EOF_OBJECT_P (scm_peek_char (port)))
  442. result = SCM_EOF_VAL;
  443. else
  444. result = SCM_I_MAKINUM (0);
  445. }
  446. else
  447. result = scm_from_size_t (c_read);
  448. return result;
  449. }
  450. #undef FUNC_NAME
  451. SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-some", 1, 0, 0,
  452. (SCM port),
  453. "Read from @var{port}, blocking as necessary, until data "
  454. "are available or and end-of-file is reached. Return either "
  455. "a new bytevector containing the data read or the "
  456. "end-of-file object.")
  457. #define FUNC_NAME s_scm_get_bytevector_some
  458. {
  459. /* Read at least one byte, unless the end-of-file is already reached, and
  460. read while characters are available (buffered). */
  461. SCM result;
  462. char *c_bv;
  463. unsigned c_len;
  464. size_t c_total;
  465. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  466. c_len = 4096;
  467. c_bv = (char *) scm_gc_malloc (c_len, SCM_GC_BYTEVECTOR);
  468. c_total = 0;
  469. do
  470. {
  471. int c_chr;
  472. if (c_total + 1 > c_len)
  473. {
  474. /* Grow the bytevector. */
  475. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
  476. SCM_GC_BYTEVECTOR);
  477. c_len *= 2;
  478. }
  479. /* We can't use `scm_c_read ()' since it blocks. */
  480. c_chr = scm_getc (port);
  481. if (c_chr != EOF)
  482. {
  483. c_bv[c_total] = (char) c_chr;
  484. c_total++;
  485. }
  486. }
  487. while ((scm_is_true (scm_char_ready_p (port)))
  488. && (!SCM_EOF_OBJECT_P (scm_peek_char (port))));
  489. if (c_total == 0)
  490. {
  491. result = SCM_EOF_VAL;
  492. scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
  493. }
  494. else
  495. {
  496. if (c_len > c_total)
  497. {
  498. /* Shrink the bytevector. */
  499. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
  500. SCM_GC_BYTEVECTOR);
  501. c_len = (unsigned) c_total;
  502. }
  503. result = scm_c_take_bytevector ((signed char *) c_bv, c_len);
  504. }
  505. return result;
  506. }
  507. #undef FUNC_NAME
  508. SCM_DEFINE (scm_get_bytevector_all, "get-bytevector-all", 1, 0, 0,
  509. (SCM port),
  510. "Read from @var{port}, blocking as necessary, until "
  511. "the end-of-file is reached. Return either "
  512. "a new bytevector containing the data read or the "
  513. "end-of-file object (if no data were available).")
  514. #define FUNC_NAME s_scm_get_bytevector_all
  515. {
  516. SCM result;
  517. char *c_bv;
  518. unsigned c_len, c_count;
  519. size_t c_read, c_total;
  520. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  521. c_len = c_count = 4096;
  522. c_bv = (char *) scm_gc_malloc (c_len, SCM_GC_BYTEVECTOR);
  523. c_total = c_read = 0;
  524. do
  525. {
  526. if (c_total + c_read > c_len)
  527. {
  528. /* Grow the bytevector. */
  529. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
  530. SCM_GC_BYTEVECTOR);
  531. c_count = c_len;
  532. c_len *= 2;
  533. }
  534. /* `scm_c_read ()' blocks until C_COUNT bytes are available or EOF is
  535. reached. */
  536. c_read = scm_c_read (port, c_bv + c_total, c_count);
  537. c_total += c_read, c_count -= c_read;
  538. }
  539. while (!SCM_EOF_OBJECT_P (scm_peek_char (port)));
  540. if (c_total == 0)
  541. {
  542. result = SCM_EOF_VAL;
  543. scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
  544. }
  545. else
  546. {
  547. if (c_len > c_total)
  548. {
  549. /* Shrink the bytevector. */
  550. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
  551. SCM_GC_BYTEVECTOR);
  552. c_len = (unsigned) c_total;
  553. }
  554. result = scm_c_take_bytevector ((signed char *) c_bv, c_len);
  555. }
  556. return result;
  557. }
  558. #undef FUNC_NAME
  559. /* Binary output. */
  560. /* We currently don't support specific binary input ports. */
  561. #define SCM_VALIDATE_BINARY_OUTPUT_PORT SCM_VALIDATE_OPOUTPORT
  562. SCM_DEFINE (scm_put_u8, "put-u8", 2, 0, 0,
  563. (SCM port, SCM octet),
  564. "Write @var{octet} to binary port @var{port}.")
  565. #define FUNC_NAME s_scm_put_u8
  566. {
  567. scm_t_uint8 c_octet;
  568. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  569. c_octet = scm_to_uint8 (octet);
  570. scm_putc ((char) c_octet, port);
  571. return SCM_UNSPECIFIED;
  572. }
  573. #undef FUNC_NAME
  574. SCM_DEFINE (scm_put_bytevector, "put-bytevector", 2, 2, 0,
  575. (SCM port, SCM bv, SCM start, SCM count),
  576. "Write the contents of @var{bv} to @var{port}, optionally "
  577. "starting at index @var{start} and limiting to @var{count} "
  578. "octets.")
  579. #define FUNC_NAME s_scm_put_bytevector
  580. {
  581. char *c_bv;
  582. unsigned c_start, c_count, c_len;
  583. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  584. SCM_VALIDATE_BYTEVECTOR (2, bv);
  585. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  586. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  587. if (start != SCM_UNDEFINED)
  588. {
  589. c_start = scm_to_uint (start);
  590. if (count != SCM_UNDEFINED)
  591. {
  592. c_count = scm_to_uint (count);
  593. if (SCM_UNLIKELY (c_start + c_count > c_len))
  594. scm_out_of_range (FUNC_NAME, count);
  595. }
  596. else
  597. {
  598. if (SCM_UNLIKELY (c_start >= c_len))
  599. scm_out_of_range (FUNC_NAME, start);
  600. else
  601. c_count = c_len - c_start;
  602. }
  603. }
  604. else
  605. c_start = 0, c_count = c_len;
  606. scm_c_write (port, c_bv + c_start, c_count);
  607. return SCM_UNSPECIFIED;
  608. }
  609. #undef FUNC_NAME
  610. /* Bytevector output port ("bop" for short). */
  611. /* Implementation of "bops".
  612. Each bop has an internal buffer, of type `scm_t_bop_buffer', attached to
  613. it. The procedure returned along with the output port is actually an
  614. applicable SMOB. The SMOB holds a reference to the port. When applied,
  615. the SMOB swallows the port's internal buffer, turning it into a
  616. bytevector, and resets it.
  617. XXX: Access to a bop's internal buffer is not thread-safe. */
  618. static scm_t_bits bytevector_output_port_type = 0;
  619. SCM_SMOB (bytevector_output_port_procedure,
  620. "r6rs-bytevector-output-port-procedure",
  621. 0);
  622. #define SCM_GC_BOP "r6rs-bytevector-output-port"
  623. #define SCM_BOP_BUFFER_INITIAL_SIZE 4096
  624. /* Representation of a bop's internal buffer. */
  625. typedef struct
  626. {
  627. size_t total_len;
  628. size_t len;
  629. size_t pos;
  630. char *buffer;
  631. } scm_t_bop_buffer;
  632. /* Accessing a bop's buffer. */
  633. #define SCM_BOP_BUFFER(_port) \
  634. ((scm_t_bop_buffer *) SCM_STREAM (_port))
  635. #define SCM_SET_BOP_BUFFER(_port, _buf) \
  636. (SCM_SETSTREAM ((_port), (scm_t_bits) (_buf)))
  637. static inline void
  638. bop_buffer_init (scm_t_bop_buffer *buf)
  639. {
  640. buf->total_len = buf->len = buf->pos = 0;
  641. buf->buffer = NULL;
  642. }
  643. static inline void
  644. bop_buffer_grow (scm_t_bop_buffer *buf, size_t min_size)
  645. {
  646. char *new_buf;
  647. size_t new_size;
  648. for (new_size = buf->total_len
  649. ? buf->total_len : SCM_BOP_BUFFER_INITIAL_SIZE;
  650. new_size < min_size;
  651. new_size *= 2);
  652. if (buf->buffer)
  653. new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len,
  654. new_size, SCM_GC_BOP);
  655. else
  656. new_buf = scm_gc_malloc (new_size, SCM_GC_BOP);
  657. buf->buffer = new_buf;
  658. buf->total_len = new_size;
  659. }
  660. static inline SCM
  661. make_bop (void)
  662. {
  663. SCM port, bop_proc;
  664. scm_t_port *c_port;
  665. scm_t_bop_buffer *buf;
  666. const unsigned long mode_bits = SCM_OPN | SCM_WRTNG;
  667. port = scm_new_port_table_entry (bytevector_output_port_type);
  668. buf = (scm_t_bop_buffer *) scm_gc_malloc (sizeof (* buf), SCM_GC_BOP);
  669. bop_buffer_init (buf);
  670. c_port = SCM_PTAB_ENTRY (port);
  671. c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
  672. c_port->write_buf_size = 0;
  673. SCM_SET_BOP_BUFFER (port, buf);
  674. /* Mark PORT as open and writable. */
  675. SCM_SET_CELL_TYPE (port, bytevector_output_port_type | mode_bits);
  676. /* Make the bop procedure. */
  677. SCM_NEWSMOB (bop_proc, bytevector_output_port_procedure,
  678. SCM_PACK (port));
  679. return (scm_values (scm_list_2 (port, bop_proc)));
  680. }
  681. static size_t
  682. bop_free (SCM port)
  683. {
  684. /* The port itself is necessarily freed _after_ the bop proc, since the bop
  685. proc holds a reference to it. Thus we can safely free the internal
  686. buffer when the bop becomes unreferenced. */
  687. scm_t_bop_buffer *buf;
  688. buf = SCM_BOP_BUFFER (port);
  689. if (buf->buffer)
  690. scm_gc_free (buf->buffer, buf->total_len, SCM_GC_BOP);
  691. scm_gc_free (buf, sizeof (* buf), SCM_GC_BOP);
  692. return 0;
  693. }
  694. /* Write SIZE octets from DATA to PORT. */
  695. static void
  696. bop_write (SCM port, const void *data, size_t size)
  697. {
  698. scm_t_bop_buffer *buf;
  699. buf = SCM_BOP_BUFFER (port);
  700. if (buf->pos + size > buf->total_len)
  701. bop_buffer_grow (buf, buf->pos + size);
  702. memcpy (buf->buffer + buf->pos, data, size);
  703. buf->pos += size;
  704. buf->len = (buf->len > buf->pos) ? buf->len : buf->pos;
  705. }
  706. static scm_t_off
  707. bop_seek (SCM port, scm_t_off offset, int whence)
  708. #define FUNC_NAME "bop_seek"
  709. {
  710. scm_t_bop_buffer *buf;
  711. buf = SCM_BOP_BUFFER (port);
  712. switch (whence)
  713. {
  714. case SEEK_CUR:
  715. offset += (scm_t_off) buf->pos;
  716. /* Fall through. */
  717. case SEEK_SET:
  718. if (offset < 0 || (unsigned) offset > buf->len)
  719. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  720. else
  721. buf->pos = offset;
  722. break;
  723. case SEEK_END:
  724. if (offset < 0 || (unsigned) offset >= buf->len)
  725. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  726. else
  727. buf->pos = buf->len - (offset + 1);
  728. break;
  729. default:
  730. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  731. "invalid `seek' parameter");
  732. }
  733. return buf->pos;
  734. }
  735. #undef FUNC_NAME
  736. /* Fetch data from a bop. */
  737. SCM_SMOB_APPLY (bytevector_output_port_procedure,
  738. bop_proc_apply, 0, 0, 0, (SCM bop_proc))
  739. {
  740. SCM port, bv;
  741. scm_t_bop_buffer *buf, result_buf;
  742. port = SCM_PACK (SCM_SMOB_DATA (bop_proc));
  743. buf = SCM_BOP_BUFFER (port);
  744. result_buf = *buf;
  745. bop_buffer_init (buf);
  746. if (result_buf.len == 0)
  747. bv = scm_c_take_bytevector (NULL, 0);
  748. else
  749. {
  750. if (result_buf.total_len > result_buf.len)
  751. /* Shrink the buffer. */
  752. result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer,
  753. result_buf.total_len,
  754. result_buf.len,
  755. SCM_GC_BOP);
  756. bv = scm_c_take_bytevector ((signed char *) result_buf.buffer,
  757. result_buf.len);
  758. }
  759. return bv;
  760. }
  761. SCM_SMOB_MARK (bytevector_output_port_procedure, bop_proc_mark,
  762. bop_proc)
  763. {
  764. /* Mark the port associated with BOP_PROC. */
  765. return (SCM_PACK (SCM_SMOB_DATA (bop_proc)));
  766. }
  767. SCM_DEFINE (scm_open_bytevector_output_port,
  768. "open-bytevector-output-port", 0, 1, 0,
  769. (SCM transcoder),
  770. "Return two values: an output port and a procedure. The latter "
  771. "should be called with zero arguments to obtain a bytevector "
  772. "containing the data accumulated by the port.")
  773. #define FUNC_NAME s_scm_open_bytevector_output_port
  774. {
  775. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  776. transcoders_not_implemented ();
  777. return (make_bop ());
  778. }
  779. #undef FUNC_NAME
  780. static inline void
  781. initialize_bytevector_output_ports (void)
  782. {
  783. bytevector_output_port_type =
  784. scm_make_port_type ("r6rs-bytevector-output-port",
  785. NULL, bop_write);
  786. scm_set_port_seek (bytevector_output_port_type, bop_seek);
  787. scm_set_port_free (bytevector_output_port_type, bop_free);
  788. }
  789. /* Custom binary output port ("cbop" for short). */
  790. static scm_t_bits custom_binary_output_port_type;
  791. /* Return the various procedures of PORT. */
  792. #define SCM_CBOP_WRITE_PROC(_port) \
  793. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
  794. static inline SCM
  795. make_cbop (SCM write_proc, SCM get_position_proc,
  796. SCM set_position_proc, SCM close_proc)
  797. {
  798. SCM port, method_vector;
  799. scm_t_port *c_port;
  800. const unsigned long mode_bits = SCM_OPN | SCM_WRTNG;
  801. /* Store the various methods and bytevector in a vector. */
  802. method_vector = scm_c_make_vector (4, SCM_BOOL_F);
  803. SCM_SIMPLE_VECTOR_SET (method_vector, 0, write_proc);
  804. SCM_SIMPLE_VECTOR_SET (method_vector, 1, get_position_proc);
  805. SCM_SIMPLE_VECTOR_SET (method_vector, 2, set_position_proc);
  806. SCM_SIMPLE_VECTOR_SET (method_vector, 3, close_proc);
  807. port = scm_new_port_table_entry (custom_binary_output_port_type);
  808. /* Attach it the method vector. */
  809. SCM_SETSTREAM (port, SCM_UNPACK (method_vector));
  810. /* Have the port directly access the buffer (bytevector). */
  811. c_port = SCM_PTAB_ENTRY (port);
  812. c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
  813. c_port->write_buf_size = c_port->read_buf_size = 0;
  814. /* Mark PORT as open, writable and unbuffered. */
  815. SCM_SET_CELL_TYPE (port, custom_binary_output_port_type | mode_bits);
  816. return port;
  817. }
  818. /* Write SIZE octets from DATA to PORT. */
  819. static void
  820. cbop_write (SCM port, const void *data, size_t size)
  821. #define FUNC_NAME "cbop_write"
  822. {
  823. long int c_result;
  824. size_t c_written;
  825. SCM bv, write_proc, result;
  826. /* XXX: Allocating a new bytevector at each `write' call is inefficient,
  827. but necessary since (1) we don't control the lifetime of the buffer
  828. pointed to by DATA, and (2) the `write!' procedure could capture the
  829. bytevector it is passed. */
  830. bv = scm_c_make_bytevector (size);
  831. memcpy (SCM_BYTEVECTOR_CONTENTS (bv), data, size);
  832. write_proc = SCM_CBOP_WRITE_PROC (port);
  833. /* Since the `write' procedure of Guile's ports has type `void', it must
  834. try hard to write exactly SIZE bytes, regardless of how many bytes the
  835. sink can handle. */
  836. for (c_written = 0;
  837. c_written < size;
  838. c_written += c_result)
  839. {
  840. result = scm_call_3 (write_proc, bv,
  841. scm_from_size_t (c_written),
  842. scm_from_size_t (size - c_written));
  843. c_result = scm_to_long (result);
  844. if (SCM_UNLIKELY (c_result < 0
  845. || (size_t) c_result > (size - c_written)))
  846. scm_wrong_type_arg_msg (FUNC_NAME, 0, result,
  847. "R6RS custom binary output port `write!' "
  848. "returned a incorrect integer");
  849. }
  850. }
  851. #undef FUNC_NAME
  852. SCM_DEFINE (scm_make_custom_binary_output_port,
  853. "make-custom-binary-output-port", 5, 0, 0,
  854. (SCM id, SCM write_proc, SCM get_position_proc,
  855. SCM set_position_proc, SCM close_proc),
  856. "Return a new custom binary output port whose output is drained "
  857. "by invoking @var{write_proc} and passing it a bytevector, an "
  858. "index where octets should be written, and an octet count.")
  859. #define FUNC_NAME s_scm_make_custom_binary_output_port
  860. {
  861. SCM_VALIDATE_STRING (1, id);
  862. SCM_VALIDATE_PROC (2, write_proc);
  863. if (!scm_is_false (get_position_proc))
  864. SCM_VALIDATE_PROC (3, get_position_proc);
  865. if (!scm_is_false (set_position_proc))
  866. SCM_VALIDATE_PROC (4, set_position_proc);
  867. if (!scm_is_false (close_proc))
  868. SCM_VALIDATE_PROC (5, close_proc);
  869. return (make_cbop (write_proc, get_position_proc, set_position_proc,
  870. close_proc));
  871. }
  872. #undef FUNC_NAME
  873. /* Instantiate the custom binary output port type. */
  874. static inline void
  875. initialize_custom_binary_output_ports (void)
  876. {
  877. custom_binary_output_port_type =
  878. scm_make_port_type ("r6rs-custom-binary-output-port",
  879. NULL, cbop_write);
  880. scm_set_port_mark (custom_binary_output_port_type, cbp_mark);
  881. scm_set_port_seek (custom_binary_output_port_type, cbp_seek);
  882. scm_set_port_close (custom_binary_output_port_type, cbp_close);
  883. }
  884. /* Initialization. */
  885. void
  886. scm_init_r6rs_ports (void)
  887. {
  888. #include "libguile/r6rs-ports.x"
  889. initialize_bytevector_input_ports ();
  890. initialize_custom_binary_input_ports ();
  891. initialize_bytevector_output_ports ();
  892. initialize_custom_binary_output_ports ();
  893. }