r6rs-ports.c 32 KB

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