r6rs-ports.c 34 KB

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