r6rs-ports.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /* Copyright (C) 2009-2011, 2013-2015, 2018, 2019
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <assert.h>
  26. #include <intprops.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. #include "libguile/ports-internal.h"
  37. SCM_SYMBOL (sym_ISO_8859_1, "ISO-8859-1");
  38. SCM_SYMBOL (sym_error, "error");
  39. /* Unimplemented features. */
  40. /* Transoders are currently not implemented since Guile 1.8 is not
  41. Unicode-capable. Thus, most of the code here assumes the use of the
  42. binary transcoder. */
  43. static inline void
  44. transcoders_not_implemented (void)
  45. {
  46. fprintf (stderr, "%s: warning: transcoders not implemented\n",
  47. PACKAGE_NAME);
  48. }
  49. /* End-of-file object. */
  50. SCM_DEFINE (scm_eof_object, "eof-object", 0, 0, 0,
  51. (void),
  52. "Return the end-of-file object.")
  53. #define FUNC_NAME s_scm_eof_object
  54. {
  55. return SCM_EOF_VAL;
  56. }
  57. #undef FUNC_NAME
  58. /* Input ports. */
  59. #ifndef MIN
  60. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  61. #endif
  62. /* Bytevector input ports. */
  63. static scm_t_port_type *bytevector_input_port_type = 0;
  64. struct bytevector_input_port {
  65. SCM bytevector;
  66. size_t pos;
  67. };
  68. static inline SCM
  69. make_bytevector_input_port (SCM bv)
  70. {
  71. const unsigned long mode_bits = SCM_RDNG;
  72. struct bytevector_input_port *stream;
  73. stream = scm_gc_typed_calloc (struct bytevector_input_port);
  74. stream->bytevector = bv;
  75. stream->pos = 0;
  76. return scm_c_make_port_with_encoding (bytevector_input_port_type, mode_bits,
  77. sym_ISO_8859_1, sym_error,
  78. (scm_t_bits) stream);
  79. }
  80. static size_t
  81. bytevector_input_port_read (SCM port, SCM dst, size_t start, size_t count)
  82. {
  83. size_t remaining;
  84. struct bytevector_input_port *stream = (void *) SCM_STREAM (port);
  85. if (stream->pos >= SCM_BYTEVECTOR_LENGTH (stream->bytevector))
  86. return 0;
  87. remaining = SCM_BYTEVECTOR_LENGTH (stream->bytevector) - stream->pos;
  88. if (remaining < count)
  89. count = remaining;
  90. memcpy (SCM_BYTEVECTOR_CONTENTS (dst) + start,
  91. SCM_BYTEVECTOR_CONTENTS (stream->bytevector) + stream->pos,
  92. count);
  93. stream->pos += count;
  94. return count;
  95. }
  96. static scm_t_off
  97. bytevector_input_port_seek (SCM port, scm_t_off offset, int whence)
  98. #define FUNC_NAME "bytevector_input_port_seek"
  99. {
  100. struct bytevector_input_port *stream = (void *) SCM_STREAM (port);
  101. size_t base;
  102. scm_t_off target;
  103. if (whence == SEEK_CUR)
  104. base = stream->pos;
  105. else if (whence == SEEK_SET)
  106. base = 0;
  107. else if (whence == SEEK_END)
  108. base = SCM_BYTEVECTOR_LENGTH (stream->bytevector);
  109. else
  110. scm_wrong_type_arg_msg (FUNC_NAME, 0, port, "invalid `seek' parameter");
  111. if (base > SCM_T_OFF_MAX
  112. || INT_ADD_OVERFLOW ((scm_t_off) base, offset))
  113. scm_num_overflow (FUNC_NAME);
  114. target = (scm_t_off) base + offset;
  115. if (target >= 0 && target <= SCM_BYTEVECTOR_LENGTH (stream->bytevector))
  116. stream->pos = target;
  117. else
  118. scm_out_of_range (FUNC_NAME, scm_from_off_t (offset));
  119. return target;
  120. }
  121. #undef FUNC_NAME
  122. /* Instantiate the bytevector input port type. */
  123. static inline void
  124. initialize_bytevector_input_ports (void)
  125. {
  126. bytevector_input_port_type =
  127. scm_make_port_type ("r6rs-bytevector-input-port",
  128. bytevector_input_port_read,
  129. NULL);
  130. scm_set_port_seek (bytevector_input_port_type, bytevector_input_port_seek);
  131. }
  132. SCM_DEFINE (scm_open_bytevector_input_port,
  133. "open-bytevector-input-port", 1, 1, 0,
  134. (SCM bv, SCM transcoder),
  135. "Return an input port whose contents are drawn from "
  136. "bytevector @var{bv}.")
  137. #define FUNC_NAME s_scm_open_bytevector_input_port
  138. {
  139. SCM_VALIDATE_BYTEVECTOR (1, bv);
  140. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  141. transcoders_not_implemented ();
  142. return make_bytevector_input_port (bv);
  143. }
  144. #undef FUNC_NAME
  145. /* Custom binary ports. The following routines are shared by input and
  146. output custom binary ports. */
  147. struct custom_binary_port {
  148. SCM read;
  149. SCM write;
  150. SCM get_position;
  151. SCM set_position_x;
  152. SCM close;
  153. };
  154. static int
  155. custom_binary_port_random_access_p (SCM port)
  156. {
  157. struct custom_binary_port *stream = (void *) SCM_STREAM (port);
  158. return scm_is_true (stream->set_position_x);
  159. }
  160. static scm_t_off
  161. custom_binary_port_seek (SCM port, scm_t_off offset, int whence)
  162. #define FUNC_NAME "custom_binary_port_seek"
  163. {
  164. SCM result;
  165. struct custom_binary_port *stream = (void *) SCM_STREAM (port);
  166. scm_t_off c_result = 0;
  167. switch (whence)
  168. {
  169. case SEEK_CUR:
  170. {
  171. if (SCM_LIKELY (scm_is_true (stream->get_position)))
  172. result = scm_call_0 (stream->get_position);
  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_off_t (result);
  178. if (offset == 0)
  179. /* We just want to know the current position. */
  180. break;
  181. if (INT_ADD_OVERFLOW (offset, c_result))
  182. scm_num_overflow (FUNC_NAME);
  183. offset += c_result;
  184. /* Fall through. */
  185. }
  186. case SEEK_SET:
  187. {
  188. if (SCM_LIKELY (scm_is_true (stream->set_position_x)))
  189. result = scm_call_1 (stream->set_position_x, scm_from_off_t (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 void
  207. custom_binary_port_close (SCM port)
  208. {
  209. struct custom_binary_port *stream = (void *) SCM_STREAM (port);
  210. if (scm_is_true (stream->close))
  211. /* Invoke the `close' thunk. */
  212. scm_call_0 (stream->close);
  213. }
  214. /* Custom binary input ports. */
  215. static scm_t_port_type *custom_binary_input_port_type = 0;
  216. static inline SCM
  217. make_custom_binary_input_port (SCM read_proc, SCM get_position_proc,
  218. SCM set_position_proc, SCM close_proc)
  219. {
  220. struct custom_binary_port *stream;
  221. const unsigned long mode_bits = SCM_RDNG;
  222. stream = scm_gc_typed_calloc (struct custom_binary_port);
  223. stream->read = read_proc;
  224. stream->write = SCM_BOOL_F;
  225. stream->get_position = get_position_proc;
  226. stream->set_position_x = set_position_proc;
  227. stream->close = close_proc;
  228. return scm_c_make_port_with_encoding (custom_binary_input_port_type,
  229. mode_bits,
  230. sym_ISO_8859_1, sym_error,
  231. (scm_t_bits) stream);
  232. }
  233. static size_t
  234. custom_binary_input_port_read (SCM port, SCM dst, size_t start, size_t count)
  235. #define FUNC_NAME "custom_binary_input_port_read"
  236. {
  237. struct custom_binary_port *stream = (void *) SCM_STREAM (port);
  238. SCM octets;
  239. size_t c_octets;
  240. octets = scm_call_3 (stream->read, dst, scm_from_size_t (start),
  241. scm_from_size_t (count));
  242. c_octets = scm_to_size_t (octets);
  243. if (c_octets > count)
  244. scm_out_of_range (FUNC_NAME, octets);
  245. return c_octets;
  246. }
  247. #undef FUNC_NAME
  248. SCM_DEFINE (scm_make_custom_binary_input_port,
  249. "make-custom-binary-input-port", 5, 0, 0,
  250. (SCM id, SCM read_proc, SCM get_position_proc,
  251. SCM set_position_proc, SCM close_proc),
  252. "Return a new custom binary input port whose input is drained "
  253. "by invoking @var{read_proc} and passing it a bytevector, an "
  254. "index where octets should be written, and an octet count.")
  255. #define FUNC_NAME s_scm_make_custom_binary_input_port
  256. {
  257. SCM_VALIDATE_STRING (1, id);
  258. SCM_VALIDATE_PROC (2, read_proc);
  259. if (!scm_is_false (get_position_proc))
  260. SCM_VALIDATE_PROC (3, get_position_proc);
  261. if (!scm_is_false (set_position_proc))
  262. SCM_VALIDATE_PROC (4, set_position_proc);
  263. if (!scm_is_false (close_proc))
  264. SCM_VALIDATE_PROC (5, close_proc);
  265. return make_custom_binary_input_port (read_proc, get_position_proc,
  266. set_position_proc, close_proc);
  267. }
  268. #undef FUNC_NAME
  269. /* Instantiate the custom binary input port type. */
  270. static inline void
  271. initialize_custom_binary_input_ports (void)
  272. {
  273. custom_binary_input_port_type =
  274. scm_make_port_type ("r6rs-custom-binary-input-port",
  275. custom_binary_input_port_read, NULL);
  276. scm_set_port_seek (custom_binary_input_port_type, custom_binary_port_seek);
  277. scm_set_port_random_access_p (custom_binary_input_port_type,
  278. custom_binary_port_random_access_p);
  279. scm_set_port_close (custom_binary_input_port_type, custom_binary_port_close);
  280. }
  281. /* Binary input. */
  282. /* We currently don't support specific binary input ports. */
  283. #define SCM_VALIDATE_BINARY_INPUT_PORT SCM_VALIDATE_OPINPORT
  284. SCM_DEFINE (scm_get_u8, "get-u8", 1, 0, 0,
  285. (SCM port),
  286. "Read an octet from @var{port}, a binary input port, "
  287. "blocking as necessary.")
  288. #define FUNC_NAME s_scm_get_u8
  289. {
  290. SCM result;
  291. int c_result;
  292. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  293. c_result = scm_get_byte_or_eof (port);
  294. if (c_result == EOF)
  295. result = SCM_EOF_VAL;
  296. else
  297. result = SCM_I_MAKINUM ((unsigned char) c_result);
  298. return result;
  299. }
  300. #undef FUNC_NAME
  301. SCM_DEFINE (scm_lookahead_u8, "lookahead-u8", 1, 0, 0,
  302. (SCM port),
  303. "Like @code{get-u8} but does not update @var{port} to "
  304. "point past the octet.")
  305. #define FUNC_NAME s_scm_lookahead_u8
  306. {
  307. int u8;
  308. SCM result;
  309. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  310. u8 = scm_peek_byte_or_eof (port);
  311. if (u8 == EOF)
  312. result = SCM_EOF_VAL;
  313. else
  314. result = SCM_I_MAKINUM ((scm_t_uint8) u8);
  315. return result;
  316. }
  317. #undef FUNC_NAME
  318. SCM_DEFINE (scm_get_bytevector_n, "get-bytevector-n", 2, 0, 0,
  319. (SCM port, SCM count),
  320. "Read @var{count} octets from @var{port}, blocking as "
  321. "necessary and return a bytevector containing the octets "
  322. "read. If fewer bytes are available, a bytevector smaller "
  323. "than @var{count} is returned.")
  324. #define FUNC_NAME s_scm_get_bytevector_n
  325. {
  326. SCM result;
  327. size_t c_count;
  328. size_t c_read;
  329. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  330. c_count = scm_to_size_t (count);
  331. result = scm_c_make_bytevector (c_count);
  332. if (SCM_LIKELY (c_count > 0))
  333. /* XXX: `scm_c_read ()' does not update the port position. */
  334. c_read = scm_c_read_bytes (port, result, 0, c_count);
  335. else
  336. /* Don't invoke `scm_c_read ()' since it may block. */
  337. c_read = 0;
  338. if (c_read < c_count)
  339. {
  340. if (c_read == 0)
  341. result = SCM_EOF_VAL;
  342. else
  343. result = scm_c_shrink_bytevector (result, c_read);
  344. }
  345. return result;
  346. }
  347. #undef FUNC_NAME
  348. SCM_DEFINE (scm_get_bytevector_n_x, "get-bytevector-n!", 4, 0, 0,
  349. (SCM port, SCM bv, SCM start, SCM count),
  350. "Read @var{count} bytes from @var{port} and store them "
  351. "in @var{bv} starting at index @var{start}. Return either "
  352. "the number of bytes actually read or the end-of-file "
  353. "object.")
  354. #define FUNC_NAME s_scm_get_bytevector_n_x
  355. {
  356. SCM result;
  357. size_t c_start, c_count, c_len;
  358. size_t c_read;
  359. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  360. SCM_VALIDATE_BYTEVECTOR (2, bv);
  361. c_start = scm_to_size_t (start);
  362. c_count = scm_to_size_t (count);
  363. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  364. if (SCM_UNLIKELY (c_len < c_start
  365. || (c_len - c_start < c_count)))
  366. scm_out_of_range (FUNC_NAME, count);
  367. if (SCM_LIKELY (c_count > 0))
  368. c_read = scm_c_read_bytes (port, bv, c_start, c_count);
  369. else
  370. /* Don't invoke `scm_c_read ()' since it may block. */
  371. c_read = 0;
  372. if (c_read == 0 && c_count > 0)
  373. result = SCM_EOF_VAL;
  374. else
  375. result = scm_from_size_t (c_read);
  376. return result;
  377. }
  378. #undef FUNC_NAME
  379. SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-some", 1, 0, 0,
  380. (SCM port),
  381. "Read from @var{port}, blocking as necessary, until bytes "
  382. "are available or an end-of-file is reached. Return either "
  383. "the end-of-file object or a new bytevector containing some "
  384. "of the available bytes (at least one), and update the port "
  385. "position to point just past these bytes.")
  386. #define FUNC_NAME s_scm_get_bytevector_some
  387. {
  388. SCM buf;
  389. size_t cur, avail;
  390. SCM bv;
  391. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  392. buf = scm_fill_input (port, 0, &cur, &avail);
  393. if (avail == 0)
  394. {
  395. scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F);
  396. return SCM_EOF_VAL;
  397. }
  398. bv = scm_c_make_bytevector (avail);
  399. scm_port_buffer_take (buf, (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv),
  400. avail, cur, avail);
  401. return bv;
  402. }
  403. #undef FUNC_NAME
  404. SCM_DEFINE (scm_get_bytevector_some_x, "get-bytevector-some!", 4, 0, 0,
  405. (SCM port, SCM bv, SCM start, SCM count),
  406. "Read up to @var{count} bytes from @var{port}, blocking "
  407. "as necessary until at least one byte is available or an "
  408. "end-of-file is reached. Store them in @var{bv} starting "
  409. "at index @var{start}. Return the number of bytes actually "
  410. "read, or an end-of-file object.")
  411. #define FUNC_NAME s_scm_get_bytevector_some_x
  412. {
  413. SCM buf;
  414. size_t c_start, c_count, c_len;
  415. size_t cur, avail, transfer_size;
  416. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  417. SCM_VALIDATE_BYTEVECTOR (2, bv);
  418. c_start = scm_to_size_t (start);
  419. c_count = scm_to_size_t (count);
  420. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  421. if (SCM_UNLIKELY (c_len < c_start
  422. || c_len - c_start < c_count))
  423. scm_out_of_range (FUNC_NAME, count);
  424. if (c_count == 0)
  425. return SCM_INUM0;
  426. buf = scm_fill_input (port, 0, &cur, &avail);
  427. if (avail == 0)
  428. {
  429. scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F);
  430. return SCM_EOF_VAL;
  431. }
  432. transfer_size = min (avail, c_count);
  433. scm_port_buffer_take (buf,
  434. (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv) + c_start,
  435. transfer_size, cur, avail);
  436. return scm_from_size_t (transfer_size);
  437. }
  438. #undef FUNC_NAME
  439. SCM_DEFINE (scm_get_bytevector_all, "get-bytevector-all", 1, 0, 0,
  440. (SCM port),
  441. "Read from @var{port}, blocking as necessary, until "
  442. "the end-of-file is reached. Return either "
  443. "a new bytevector containing the data read or the "
  444. "end-of-file object (if no data were available).")
  445. #define FUNC_NAME s_scm_get_bytevector_all
  446. {
  447. SCM result;
  448. size_t c_len, c_count;
  449. size_t c_read, c_total;
  450. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  451. c_len = c_count = 4096;
  452. result = scm_c_make_bytevector (c_count);
  453. c_total = c_read = 0;
  454. do
  455. {
  456. if (c_read > c_len - c_total)
  457. {
  458. /* Grow the bytevector. */
  459. SCM prev = result;
  460. if (INT_ADD_OVERFLOW (c_len, c_len))
  461. scm_num_overflow (FUNC_NAME);
  462. result = scm_c_make_bytevector (c_len * 2);
  463. memcpy (SCM_BYTEVECTOR_CONTENTS (result),
  464. SCM_BYTEVECTOR_CONTENTS (prev),
  465. c_total);
  466. c_count = c_len;
  467. c_len *= 2;
  468. }
  469. /* `scm_c_read ()' blocks until C_COUNT bytes are available or EOF is
  470. reached. */
  471. c_read = scm_c_read_bytes (port, result, c_total, c_count);
  472. c_total += c_read, c_count -= c_read;
  473. }
  474. while (c_count == 0);
  475. if (c_total == 0)
  476. return SCM_EOF_VAL;
  477. if (c_len > c_total)
  478. return scm_c_shrink_bytevector (result, c_total);
  479. return result;
  480. }
  481. #undef FUNC_NAME
  482. /* Binary output. */
  483. /* We currently don't support specific binary input ports. */
  484. #define SCM_VALIDATE_BINARY_OUTPUT_PORT SCM_VALIDATE_OPOUTPORT
  485. SCM_DEFINE (scm_put_u8, "put-u8", 2, 0, 0,
  486. (SCM port, SCM octet),
  487. "Write @var{octet} to binary port @var{port}.")
  488. #define FUNC_NAME s_scm_put_u8
  489. {
  490. scm_t_uint8 c_octet;
  491. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  492. c_octet = scm_to_uint8 (octet);
  493. scm_c_write (port, &c_octet, 1);
  494. return SCM_UNSPECIFIED;
  495. }
  496. #undef FUNC_NAME
  497. SCM_DEFINE (scm_put_bytevector, "put-bytevector", 2, 2, 0,
  498. (SCM port, SCM bv, SCM start, SCM count),
  499. "Write the contents of @var{bv} to @var{port}, optionally "
  500. "starting at index @var{start} and limiting to @var{count} "
  501. "octets.")
  502. #define FUNC_NAME s_scm_put_bytevector
  503. {
  504. size_t c_start, c_count, c_len;
  505. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  506. SCM_VALIDATE_BYTEVECTOR (2, bv);
  507. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  508. if (!scm_is_eq (start, SCM_UNDEFINED))
  509. {
  510. c_start = scm_to_size_t (start);
  511. if (SCM_UNLIKELY (c_start > c_len))
  512. scm_out_of_range (FUNC_NAME, start);
  513. if (!scm_is_eq (count, SCM_UNDEFINED))
  514. {
  515. c_count = scm_to_size_t (count);
  516. if (SCM_UNLIKELY (c_count > c_len - c_start))
  517. scm_out_of_range (FUNC_NAME, count);
  518. }
  519. else
  520. c_count = c_len - c_start;
  521. }
  522. else
  523. c_start = 0, c_count = c_len;
  524. scm_c_write_bytes (port, bv, c_start, c_count);
  525. return SCM_UNSPECIFIED;
  526. }
  527. #undef FUNC_NAME
  528. SCM_DEFINE (scm_unget_bytevector, "unget-bytevector", 2, 2, 0,
  529. (SCM port, SCM bv, SCM start, SCM count),
  530. "Unget the contents of @var{bv} to @var{port}, optionally "
  531. "starting at index @var{start} and limiting to @var{count} "
  532. "octets.")
  533. #define FUNC_NAME s_scm_unget_bytevector
  534. {
  535. unsigned char *c_bv;
  536. size_t c_start, c_count, c_len;
  537. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  538. SCM_VALIDATE_BYTEVECTOR (2, bv);
  539. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  540. c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  541. if (!scm_is_eq (start, SCM_UNDEFINED))
  542. {
  543. c_start = scm_to_size_t (start);
  544. if (SCM_UNLIKELY (c_start > c_len))
  545. scm_out_of_range (FUNC_NAME, start);
  546. if (!scm_is_eq (count, SCM_UNDEFINED))
  547. {
  548. c_count = scm_to_size_t (count);
  549. if (SCM_UNLIKELY (c_count > c_len - c_start))
  550. scm_out_of_range (FUNC_NAME, count);
  551. }
  552. else
  553. c_count = c_len - c_start;
  554. }
  555. else
  556. c_start = 0, c_count = c_len;
  557. scm_unget_bytes (c_bv + c_start, c_count, port);
  558. return SCM_UNSPECIFIED;
  559. }
  560. #undef FUNC_NAME
  561. /* Bytevector output port. */
  562. /* Implementation of "bytevector output ports".
  563. Each bytevector output port has an internal buffer, of type
  564. `scm_t_bytevector_output_port_buffer', attached to it. The procedure
  565. returned along with the output port is actually an applicable SMOB.
  566. The SMOB holds a reference to the port. When applied, the SMOB
  567. swallows the port's internal buffer, turning it into a bytevector,
  568. and resets it.
  569. XXX: Access to a bytevector output port's internal buffer is not
  570. thread-safe. */
  571. static scm_t_port_type *bytevector_output_port_type = 0;
  572. SCM_SMOB (bytevector_output_port_procedure,
  573. "r6rs-bytevector-output-port-procedure",
  574. 0);
  575. #define SCM_GC_BYTEVECTOR_OUTPUT_PORT "r6rs-bytevector-output-port"
  576. #define SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER_INITIAL_SIZE 4096
  577. /* Representation of a bytevector output port's internal buffer. */
  578. typedef struct
  579. {
  580. size_t total_len;
  581. size_t len;
  582. size_t pos;
  583. char *buffer;
  584. /* The get-bytevector procedure will flush this port, if it's
  585. open. */
  586. SCM port;
  587. } scm_t_bytevector_output_port_buffer;
  588. /* Accessing a bytevector output port's buffer. */
  589. #define SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER(_port) \
  590. ((scm_t_bytevector_output_port_buffer *) SCM_STREAM (_port))
  591. #define SCM_SET_BYTEVECTOR_OUTPUT_PORT_BUFFER(_port, _buf) \
  592. (SCM_SETSTREAM ((_port), (scm_t_bits) (_buf)))
  593. static inline void
  594. bytevector_output_port_buffer_init (scm_t_bytevector_output_port_buffer *buf)
  595. {
  596. buf->total_len = buf->len = buf->pos = 0;
  597. buf->buffer = NULL;
  598. /* Don't clear the port. */
  599. }
  600. static inline void
  601. bytevector_output_port_buffer_grow (scm_t_bytevector_output_port_buffer *buf,
  602. size_t min_size)
  603. {
  604. char *new_buf;
  605. size_t new_size;
  606. if (buf->buffer)
  607. {
  608. if (INT_ADD_OVERFLOW (buf->total_len, buf->total_len))
  609. scm_num_overflow ("bytevector_output_port_buffer_grow");
  610. new_size = max (min_size, buf->total_len * 2);
  611. new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len,
  612. new_size, SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  613. }
  614. else
  615. {
  616. new_size = max (min_size, SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER_INITIAL_SIZE);
  617. new_buf = scm_gc_malloc_pointerless (new_size,
  618. SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  619. }
  620. buf->buffer = new_buf;
  621. buf->total_len = new_size;
  622. }
  623. static inline SCM
  624. make_bytevector_output_port (void)
  625. {
  626. SCM port, proc;
  627. scm_t_bytevector_output_port_buffer *buf;
  628. const unsigned long mode_bits = SCM_WRTNG;
  629. buf = (scm_t_bytevector_output_port_buffer *)
  630. scm_gc_malloc (sizeof (* buf), SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  631. bytevector_output_port_buffer_init (buf);
  632. port = scm_c_make_port_with_encoding (bytevector_output_port_type,
  633. mode_bits,
  634. sym_ISO_8859_1, sym_error,
  635. (scm_t_bits)buf);
  636. buf->port = port;
  637. SCM_NEWSMOB (proc, bytevector_output_port_procedure, buf);
  638. return scm_values (scm_list_2 (port, proc));
  639. }
  640. /* Write octets from WRITE_BUF to the backing store. */
  641. static size_t
  642. bytevector_output_port_write (SCM port, SCM src, size_t start, size_t count)
  643. #define FUNC_NAME "bytevector_output_port_write"
  644. {
  645. scm_t_bytevector_output_port_buffer *buf;
  646. buf = SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER (port);
  647. if (count > buf->total_len - buf->pos)
  648. {
  649. if (INT_ADD_OVERFLOW (buf->pos, count))
  650. scm_num_overflow (FUNC_NAME);
  651. bytevector_output_port_buffer_grow (buf, buf->pos + count);
  652. }
  653. memcpy (buf->buffer + buf->pos, SCM_BYTEVECTOR_CONTENTS (src) + start, count);
  654. buf->pos += count;
  655. buf->len = (buf->len > buf->pos) ? buf->len : buf->pos;
  656. return count;
  657. }
  658. #undef FUNC_NAME
  659. static scm_t_off
  660. bytevector_output_port_seek (SCM port, scm_t_off offset, int whence)
  661. #define FUNC_NAME "bytevector_output_port_seek"
  662. {
  663. scm_t_bytevector_output_port_buffer *buf;
  664. size_t base;
  665. scm_t_off target;
  666. buf = SCM_BYTEVECTOR_OUTPUT_PORT_BUFFER (port);
  667. if (whence == SEEK_CUR)
  668. base = buf->pos;
  669. else if (whence == SEEK_SET)
  670. base = 0;
  671. else if (whence == SEEK_END)
  672. base = buf->len;
  673. else
  674. scm_wrong_type_arg_msg (FUNC_NAME, 0, port, "invalid `seek' parameter");
  675. if (base > SCM_T_OFF_MAX
  676. || INT_ADD_OVERFLOW ((scm_t_off) base, offset))
  677. scm_num_overflow (FUNC_NAME);
  678. target = (scm_t_off) base + offset;
  679. if (target >= 0 && target <= buf->len)
  680. buf->pos = target;
  681. else
  682. scm_out_of_range (FUNC_NAME, scm_from_off_t (offset));
  683. return target;
  684. }
  685. #undef FUNC_NAME
  686. /* Fetch data from a bytevector output port. */
  687. SCM_SMOB_APPLY (bytevector_output_port_procedure,
  688. bytevector_output_port_proc_apply, 0, 0, 0, (SCM proc))
  689. {
  690. SCM bv;
  691. scm_t_bytevector_output_port_buffer *buf, result_buf;
  692. buf = (scm_t_bytevector_output_port_buffer *) SCM_SMOB_DATA (proc);
  693. if (SCM_OPPORTP (buf->port))
  694. scm_flush (buf->port);
  695. result_buf = *buf;
  696. bytevector_output_port_buffer_init (buf);
  697. if (result_buf.len == 0)
  698. bv = scm_c_take_gc_bytevector (NULL, 0, SCM_BOOL_F);
  699. else
  700. {
  701. if (result_buf.total_len > result_buf.len)
  702. /* Shrink the buffer. */
  703. result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer,
  704. result_buf.total_len,
  705. result_buf.len,
  706. SCM_GC_BYTEVECTOR_OUTPUT_PORT);
  707. bv = scm_c_take_gc_bytevector ((signed char *) result_buf.buffer,
  708. result_buf.len, SCM_BOOL_F);
  709. }
  710. return bv;
  711. }
  712. SCM_DEFINE (scm_open_bytevector_output_port,
  713. "open-bytevector-output-port", 0, 1, 0,
  714. (SCM transcoder),
  715. "Return two values: an output port and a procedure. The latter "
  716. "should be called with zero arguments to obtain a bytevector "
  717. "containing the data accumulated by the port.")
  718. #define FUNC_NAME s_scm_open_bytevector_output_port
  719. {
  720. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  721. transcoders_not_implemented ();
  722. return make_bytevector_output_port ();
  723. }
  724. #undef FUNC_NAME
  725. static inline void
  726. initialize_bytevector_output_ports (void)
  727. {
  728. bytevector_output_port_type =
  729. scm_make_port_type ("r6rs-bytevector-output-port",
  730. NULL, bytevector_output_port_write);
  731. scm_set_port_seek (bytevector_output_port_type, bytevector_output_port_seek);
  732. }
  733. /* Custom binary output ports. */
  734. static scm_t_port_type *custom_binary_output_port_type;
  735. static inline SCM
  736. make_custom_binary_output_port (SCM write_proc, SCM get_position_proc,
  737. SCM set_position_proc, SCM close_proc)
  738. {
  739. struct custom_binary_port *stream;
  740. const unsigned long mode_bits = SCM_WRTNG;
  741. stream = scm_gc_typed_calloc (struct custom_binary_port);
  742. stream->read = SCM_BOOL_F;
  743. stream->write = write_proc;
  744. stream->get_position = get_position_proc;
  745. stream->set_position_x = set_position_proc;
  746. stream->close = close_proc;
  747. return scm_c_make_port_with_encoding (custom_binary_output_port_type,
  748. mode_bits,
  749. sym_ISO_8859_1, sym_error,
  750. (scm_t_bits) stream);
  751. }
  752. /* Flush octets from BUF to the backing store. */
  753. static size_t
  754. custom_binary_output_port_write (SCM port, SCM src, size_t start, size_t count)
  755. #define FUNC_NAME "custom_binary_output_port_write"
  756. {
  757. struct custom_binary_port *stream = (void *) SCM_STREAM (port);
  758. size_t written;
  759. SCM result;
  760. result = scm_call_3 (stream->write, src, scm_from_size_t (start),
  761. scm_from_size_t (count));
  762. written = scm_to_size_t (result);
  763. if (written > count)
  764. scm_wrong_type_arg_msg (FUNC_NAME, 0, result,
  765. "R6RS custom binary output port `write!' "
  766. "returned a incorrect integer");
  767. return written;
  768. }
  769. #undef FUNC_NAME
  770. SCM_DEFINE (scm_make_custom_binary_output_port,
  771. "make-custom-binary-output-port", 5, 0, 0,
  772. (SCM id, SCM write_proc, SCM get_position_proc,
  773. SCM set_position_proc, SCM close_proc),
  774. "Return a new custom binary output port whose output is drained "
  775. "by invoking @var{write_proc} and passing it a bytevector, an "
  776. "index where octets should be written, and an octet count.")
  777. #define FUNC_NAME s_scm_make_custom_binary_output_port
  778. {
  779. SCM_VALIDATE_STRING (1, id);
  780. SCM_VALIDATE_PROC (2, write_proc);
  781. if (!scm_is_false (get_position_proc))
  782. SCM_VALIDATE_PROC (3, get_position_proc);
  783. if (!scm_is_false (set_position_proc))
  784. SCM_VALIDATE_PROC (4, set_position_proc);
  785. if (!scm_is_false (close_proc))
  786. SCM_VALIDATE_PROC (5, close_proc);
  787. return make_custom_binary_output_port (write_proc, get_position_proc,
  788. set_position_proc, close_proc);
  789. }
  790. #undef FUNC_NAME
  791. /* Instantiate the custom binary output port type. */
  792. static inline void
  793. initialize_custom_binary_output_ports (void)
  794. {
  795. custom_binary_output_port_type =
  796. scm_make_port_type ("r6rs-custom-binary-output-port",
  797. NULL, custom_binary_output_port_write);
  798. scm_set_port_seek (custom_binary_output_port_type, custom_binary_port_seek);
  799. scm_set_port_random_access_p (custom_binary_output_port_type,
  800. custom_binary_port_random_access_p);
  801. scm_set_port_close (custom_binary_output_port_type, custom_binary_port_close);
  802. }
  803. /* Custom binary input_output ports. */
  804. static scm_t_port_type *custom_binary_input_output_port_type;
  805. static inline SCM
  806. make_custom_binary_input_output_port (SCM read_proc, SCM write_proc,
  807. SCM get_position_proc,
  808. SCM set_position_proc, SCM close_proc)
  809. {
  810. struct custom_binary_port *stream;
  811. const unsigned long mode_bits = SCM_WRTNG | SCM_RDNG;
  812. stream = scm_gc_typed_calloc (struct custom_binary_port);
  813. stream->read = read_proc;
  814. stream->write = write_proc;
  815. stream->get_position = get_position_proc;
  816. stream->set_position_x = set_position_proc;
  817. stream->close = close_proc;
  818. return scm_c_make_port_with_encoding (custom_binary_input_output_port_type,
  819. mode_bits, sym_ISO_8859_1, sym_error,
  820. (scm_t_bits) stream);
  821. }
  822. SCM_DEFINE (scm_make_custom_binary_input_output_port,
  823. "make-custom-binary-input/output-port", 6, 0, 0,
  824. (SCM id, SCM read_proc, SCM write_proc, SCM get_position_proc,
  825. SCM set_position_proc, SCM close_proc),
  826. "Return a new custom binary input/output port. The port's input\n"
  827. "is drained by invoking @var{read_proc} and passing it a\n"
  828. "bytevector, an index where octets should be written, and an\n"
  829. "octet count. The output is drained by invoking @var{write_proc}\n"
  830. "and passing it a bytevector, an index where octets should be\n"
  831. "written, and an octet count.")
  832. #define FUNC_NAME s_scm_make_custom_binary_input_output_port
  833. {
  834. SCM_VALIDATE_STRING (1, id);
  835. SCM_VALIDATE_PROC (2, read_proc);
  836. SCM_VALIDATE_PROC (3, write_proc);
  837. if (!scm_is_false (get_position_proc))
  838. SCM_VALIDATE_PROC (4, get_position_proc);
  839. if (!scm_is_false (set_position_proc))
  840. SCM_VALIDATE_PROC (5, set_position_proc);
  841. if (!scm_is_false (close_proc))
  842. SCM_VALIDATE_PROC (6, close_proc);
  843. return make_custom_binary_input_output_port
  844. (read_proc, write_proc, get_position_proc, set_position_proc, close_proc);
  845. }
  846. #undef FUNC_NAME
  847. /* Instantiate the custom binary input_output port type. */
  848. static inline void
  849. initialize_custom_binary_input_output_ports (void)
  850. {
  851. custom_binary_input_output_port_type =
  852. scm_make_port_type ("r6rs-custom-binary-input/output-port",
  853. custom_binary_input_port_read,
  854. custom_binary_output_port_write);
  855. scm_set_port_seek (custom_binary_input_output_port_type,
  856. custom_binary_port_seek);
  857. scm_set_port_random_access_p (custom_binary_input_output_port_type,
  858. custom_binary_port_random_access_p);
  859. scm_set_port_close (custom_binary_input_output_port_type,
  860. custom_binary_port_close);
  861. }
  862. /* Transcoded ports. */
  863. static scm_t_port_type *transcoded_port_type = 0;
  864. #define SCM_TRANSCODED_PORT_BINARY_PORT(_port) SCM_PACK (SCM_STREAM (_port))
  865. static inline SCM
  866. make_transcoded_port (SCM binary_port, unsigned long mode)
  867. {
  868. return scm_c_make_port (transcoded_port_type, mode,
  869. SCM_UNPACK (binary_port));
  870. }
  871. static size_t
  872. transcoded_port_write (SCM port, SCM src, size_t start, size_t count)
  873. {
  874. SCM bport = SCM_TRANSCODED_PORT_BINARY_PORT (port);
  875. scm_c_write_bytes (bport, src, start, count);
  876. return count;
  877. }
  878. static size_t
  879. transcoded_port_read (SCM port, SCM dst, size_t start, size_t count)
  880. {
  881. SCM bport = SCM_TRANSCODED_PORT_BINARY_PORT (port);
  882. return scm_c_read_bytes (bport, dst, start, count);
  883. }
  884. static void
  885. transcoded_port_close (SCM port)
  886. {
  887. scm_close_port (SCM_TRANSCODED_PORT_BINARY_PORT (port));
  888. }
  889. static inline void
  890. initialize_transcoded_ports (void)
  891. {
  892. transcoded_port_type =
  893. scm_make_port_type ("r6rs-transcoded-port", transcoded_port_read,
  894. transcoded_port_write);
  895. scm_set_port_close (transcoded_port_type, transcoded_port_close);
  896. scm_set_port_needs_close_on_gc (transcoded_port_type, 1);
  897. }
  898. SCM_INTERNAL SCM scm_i_make_transcoded_port (SCM);
  899. SCM_DEFINE (scm_i_make_transcoded_port,
  900. "%make-transcoded-port", 1, 0, 0,
  901. (SCM port),
  902. "Return a new port which reads and writes to @var{port}")
  903. #define FUNC_NAME s_scm_i_make_transcoded_port
  904. {
  905. SCM result;
  906. unsigned long mode = 0;
  907. SCM_VALIDATE_PORT (SCM_ARG1, port);
  908. if (scm_is_true (scm_output_port_p (port)))
  909. mode |= SCM_WRTNG;
  910. else if (scm_is_true (scm_input_port_p (port)))
  911. mode |= SCM_RDNG;
  912. result = make_transcoded_port (port, mode);
  913. /* FIXME: We should actually close `port' "in a special way" here,
  914. according to R6RS. As there is no way to do that in Guile without
  915. rendering the underlying port unusable for our purposes as well, we
  916. just leave it open. */
  917. return result;
  918. }
  919. #undef FUNC_NAME
  920. /* Textual I/O */
  921. SCM_DEFINE (scm_get_string_n_x,
  922. "get-string-n!", 4, 0, 0,
  923. (SCM port, SCM str, SCM start, SCM count),
  924. "Read up to @var{count} characters from @var{port} into "
  925. "@var{str}, starting at @var{start}. If no characters "
  926. "can be read before the end of file is encountered, the end "
  927. "of file object is returned. Otherwise, the number of "
  928. "characters read is returned.")
  929. #define FUNC_NAME s_scm_get_string_n_x
  930. {
  931. size_t c_start, c_count, c_len, c_end, j;
  932. scm_t_wchar c;
  933. SCM_VALIDATE_OPINPORT (1, port);
  934. SCM_VALIDATE_STRING (2, str);
  935. c_len = scm_c_string_length (str);
  936. c_start = scm_to_size_t (start);
  937. c_count = scm_to_size_t (count);
  938. c_end = c_start + c_count;
  939. if (SCM_UNLIKELY (c_end > c_len))
  940. scm_out_of_range (FUNC_NAME, count);
  941. for (j = c_start; j < c_end; j++)
  942. {
  943. c = scm_getc (port);
  944. if (c == EOF)
  945. {
  946. size_t chars_read = j - c_start;
  947. return chars_read == 0 ? SCM_EOF_VAL : scm_from_size_t (chars_read);
  948. }
  949. scm_c_string_set_x (str, j, SCM_MAKE_CHAR (c));
  950. }
  951. return count;
  952. }
  953. #undef FUNC_NAME
  954. /* Initialization. */
  955. void
  956. scm_register_r6rs_ports (void)
  957. {
  958. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  959. "scm_init_r6rs_ports",
  960. (scm_t_extension_init_func) scm_init_r6rs_ports,
  961. NULL);
  962. initialize_bytevector_input_ports ();
  963. initialize_custom_binary_input_ports ();
  964. initialize_bytevector_output_ports ();
  965. initialize_custom_binary_output_ports ();
  966. initialize_custom_binary_input_output_ports ();
  967. initialize_transcoded_ports ();
  968. }
  969. void
  970. scm_init_r6rs_ports (void)
  971. {
  972. #include "libguile/r6rs-ports.x"
  973. }