ports-internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * ports-internal.h - internal-only declarations for ports.
  3. *
  4. * Copyright (C) 2013 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #ifndef SCM_PORTS_INTERNAL
  22. #define SCM_PORTS_INTERNAL
  23. #include <assert.h>
  24. #include <iconv.h>
  25. #include "libguile/_scm.h"
  26. #include "libguile/ports.h"
  27. typedef enum scm_t_port_type_flags {
  28. /* Indicates that the port should be closed if it is garbage collected
  29. while it is open. */
  30. SCM_PORT_TYPE_NEEDS_CLOSE_ON_GC = 1 << 0
  31. } scm_t_port_type_flags;
  32. /* port-type description. */
  33. struct scm_t_port_type
  34. {
  35. char *name;
  36. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  37. size_t (*c_read) (SCM port, SCM dst, size_t start, size_t count);
  38. size_t (*c_write) (SCM port, SCM src, size_t start, size_t count);
  39. SCM scm_read;
  40. SCM scm_write;
  41. int (*read_wait_fd) (SCM port);
  42. int (*write_wait_fd) (SCM port);
  43. scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
  44. void (*close) (SCM port);
  45. void (*get_natural_buffer_sizes) (SCM port, size_t *read_size,
  46. size_t *write_size);
  47. int (*random_access_p) (SCM port);
  48. int (*input_waiting) (SCM port);
  49. void (*truncate) (SCM port, scm_t_off length);
  50. unsigned flags;
  51. /* GOOPS tomfoolery. */
  52. SCM input_class, output_class, input_output_class;
  53. };
  54. /* Port buffers.
  55. It's important to avoid calling into the kernel too many times. For
  56. that reason we buffer the input and output, using "port buffer"
  57. objects. Port buffers are represented as vectors containing the
  58. buffer, two cursors, and a flag. The bytes in a read buffer are laid
  59. out like this:
  60. |already read | not yet | invalid
  61. | data | read | data
  62. readbuf: #vu8(|r r r r r r r|u u u u u|x x x x x|)
  63. ^buf ^cur ^end ^size(buf)
  64. Similarly for a write buffer:
  65. |already written | not yet | invalid
  66. | data | written | data
  67. writebuf: #vu8(|w w w w w w w w |u u u u u|x x x x x|)
  68. ^buf ^cur ^end ^size(buf)
  69. We use the same port buffer data structure for both purposes. Port
  70. buffers are implemented as their own object so that they can be
  71. atomically swapped in or out of ports, and as Scheme vectors so they
  72. can be manipulated from Scheme. */
  73. enum scm_port_buffer_field {
  74. SCM_PORT_BUFFER_FIELD_BYTEVECTOR,
  75. SCM_PORT_BUFFER_FIELD_CUR,
  76. SCM_PORT_BUFFER_FIELD_END,
  77. SCM_PORT_BUFFER_FIELD_HAS_EOF_P,
  78. SCM_PORT_BUFFER_FIELD_POSITION,
  79. SCM_PORT_BUFFER_FIELD_COUNT
  80. };
  81. /* The port buffers are exposed to Scheme, which can mutate their
  82. fields. We have to do dynamic checks to ensure that
  83. potentially-malicious Scheme doesn't invalidate our invariants.
  84. However these dynamic checks are slow, so we need to avoid them where
  85. they are unnecessary. An unnecessary check is a check which has
  86. already been performed, or one which would already be performed by
  87. the time that memory is accessed. Given that the "can_take",
  88. "can_put", or "can_putback" functions are eventually called before
  89. any access to the buffer, we hoist the necessary type checks the
  90. can_foo and size functions, and otherwise assume that the cur and end
  91. values are inums within the right ranges. */
  92. static inline SCM
  93. scm_port_buffer_bytevector (SCM buf)
  94. {
  95. return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
  96. }
  97. static inline SCM
  98. scm_port_buffer_cur (SCM buf)
  99. {
  100. return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
  101. }
  102. static inline void
  103. scm_port_buffer_set_cur (SCM buf, SCM cur)
  104. {
  105. SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_CUR, cur);
  106. }
  107. static inline SCM
  108. scm_port_buffer_end (SCM buf)
  109. {
  110. return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
  111. }
  112. static inline void
  113. scm_port_buffer_set_end (SCM buf, SCM end)
  114. {
  115. SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_END, end);
  116. }
  117. static inline SCM
  118. scm_port_buffer_has_eof_p (SCM buf)
  119. {
  120. return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_HAS_EOF_P);
  121. }
  122. static inline void
  123. scm_port_buffer_set_has_eof_p (SCM buf, SCM has_eof_p)
  124. {
  125. SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_HAS_EOF_P,
  126. has_eof_p);
  127. }
  128. /* The port position object is a pair that is referenced by the port.
  129. To make things easier for Scheme port code, it is also referenced by
  130. port buffers. */
  131. static inline SCM
  132. scm_port_buffer_position (SCM buf)
  133. {
  134. return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_POSITION);
  135. }
  136. static inline SCM
  137. scm_port_position_line (SCM position)
  138. {
  139. return scm_car (position);
  140. }
  141. static inline void
  142. scm_port_position_set_line (SCM position, SCM line)
  143. {
  144. scm_set_car_x (position, line);
  145. }
  146. static inline SCM
  147. scm_port_position_column (SCM position)
  148. {
  149. return scm_cdr (position);
  150. }
  151. static inline void
  152. scm_port_position_set_column (SCM position, SCM column)
  153. {
  154. scm_set_cdr_x (position, column);
  155. }
  156. static inline size_t
  157. scm_port_buffer_size (SCM buf)
  158. {
  159. SCM bv = scm_port_buffer_bytevector (buf);
  160. if (SCM_LIKELY (SCM_BYTEVECTOR_P (bv)))
  161. return SCM_BYTEVECTOR_LENGTH (bv);
  162. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (bv));
  163. return -1;
  164. }
  165. static inline void
  166. scm_port_buffer_reset (SCM buf)
  167. {
  168. scm_port_buffer_set_cur (buf, SCM_INUM0);
  169. scm_port_buffer_set_end (buf, SCM_INUM0);
  170. }
  171. static inline void
  172. scm_port_buffer_reset_end (SCM buf)
  173. {
  174. scm_port_buffer_set_cur (buf, scm_from_size_t (scm_port_buffer_size (buf)));
  175. scm_port_buffer_set_end (buf, scm_from_size_t (scm_port_buffer_size (buf)));
  176. }
  177. static inline size_t
  178. scm_port_buffer_can_take (SCM buf, size_t *cur_out)
  179. {
  180. size_t cur, end;
  181. cur = scm_to_size_t (scm_port_buffer_cur (buf));
  182. end = scm_to_size_t (scm_port_buffer_end (buf));
  183. if (end > scm_port_buffer_size (buf))
  184. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
  185. /* If something races and we end up with end < cur, signal the caller
  186. to do a fill_input and centralize there. */
  187. *cur_out = cur;
  188. return end < cur ? 0 : end - cur;
  189. }
  190. static inline size_t
  191. scm_port_buffer_can_put (SCM buf, size_t *end_out)
  192. {
  193. size_t end = scm_to_size_t (scm_port_buffer_end (buf));
  194. if (end > scm_port_buffer_size (buf))
  195. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
  196. *end_out = end;
  197. return scm_port_buffer_size (buf) - end;
  198. }
  199. static inline size_t
  200. scm_port_buffer_can_putback (SCM buf)
  201. {
  202. size_t cur = scm_to_size_t (scm_port_buffer_cur (buf));
  203. if (cur > scm_port_buffer_size (buf))
  204. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
  205. return cur;
  206. }
  207. static inline void
  208. scm_port_buffer_did_take (SCM buf, size_t prev_cur, size_t count)
  209. {
  210. scm_port_buffer_set_cur (buf, SCM_I_MAKINUM (prev_cur + count));
  211. }
  212. static inline void
  213. scm_port_buffer_did_put (SCM buf, size_t prev_end, size_t count)
  214. {
  215. scm_port_buffer_set_end (buf, SCM_I_MAKINUM (prev_end + count));
  216. }
  217. static inline const scm_t_uint8 *
  218. scm_port_buffer_take_pointer (SCM buf, size_t cur)
  219. {
  220. signed char *ret = SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf));
  221. return ((scm_t_uint8 *) ret) + cur;
  222. }
  223. static inline scm_t_uint8 *
  224. scm_port_buffer_put_pointer (SCM buf, size_t end)
  225. {
  226. signed char *ret = SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf));
  227. return ((scm_t_uint8 *) ret) + end;
  228. }
  229. static inline size_t
  230. scm_port_buffer_take (SCM buf, scm_t_uint8 *dst, size_t count,
  231. size_t cur, size_t avail)
  232. {
  233. if (avail < count)
  234. count = avail;
  235. if (dst)
  236. memcpy (dst, scm_port_buffer_take_pointer (buf, cur), count);
  237. scm_port_buffer_did_take (buf, cur, count);
  238. return count;
  239. }
  240. static inline size_t
  241. scm_port_buffer_put (SCM buf, const scm_t_uint8 *src, size_t count,
  242. size_t end, size_t avail)
  243. {
  244. if (avail < count)
  245. count = avail;
  246. if (src)
  247. memcpy (scm_port_buffer_put_pointer (buf, end), src, count);
  248. scm_port_buffer_did_put (buf, end, count);
  249. return count;
  250. }
  251. static inline void
  252. scm_port_buffer_putback (SCM buf, const scm_t_uint8 *src, size_t count,
  253. size_t cur)
  254. {
  255. assert (count <= cur);
  256. /* Sometimes used to move around data within a buffer, so we must use
  257. memmove. */
  258. cur -= count;
  259. scm_port_buffer_set_cur (buf, scm_from_size_t (cur));
  260. memmove (SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf)) + cur,
  261. src, count);
  262. }
  263. struct scm_t_port
  264. {
  265. /* Source location information. */
  266. SCM file_name;
  267. SCM position;
  268. /* Port buffers. */
  269. SCM read_buf;
  270. SCM write_buf;
  271. SCM write_buf_aux;
  272. /* All ports have read and write buffers; an unbuffered port simply
  273. has a one-byte buffer. However unreading bytes can expand the read
  274. buffer, but that doesn't mean that we want to increase the input
  275. buffering. For that reason `read_buffering' is a separate
  276. indication of how many characters to buffer on the read side.
  277. There isn't a write_buf_size because there isn't an
  278. `unwrite-byte'. */
  279. size_t read_buffering;
  280. /* Reads and writes can proceed concurrently, but we don't want to
  281. start any read or write after close() has been called. So we have
  282. a refcount which is positive if close has not yet been called.
  283. Reading, writing, and the like temporarily increments this
  284. refcount, provided it was nonzero to start with. */
  285. scm_t_uint32 refcount;
  286. /* True if the port is random access. Implies that the buffers must
  287. be flushed before switching between reading and writing, seeking,
  288. and so on. */
  289. scm_t_uint32 rw_random : 1;
  290. scm_t_uint32 at_stream_start_for_bom_read : 1;
  291. scm_t_uint32 at_stream_start_for_bom_write : 1;
  292. /* Character encoding support. */
  293. SCM encoding; /* A symbol of upper-case ASCII. */
  294. SCM conversion_strategy; /* A symbol; either substitute, error, or escape. */
  295. /* This is the same as pt->encoding, except if `encoding' is UTF-16 or
  296. UTF-32, in which case this is UTF-16LE or a similar
  297. byte-order-specialed version of UTF-16 or UTF-32. This is a
  298. separate field from `encoding' because being just plain UTF-16 or
  299. UTF-32 has an additional meaning, being that we should consume and
  300. produce byte order marker codepoints as appropriate. Set to #f
  301. before the iconv descriptors have been opened. */
  302. SCM precise_encoding; /* with iconv_lock */
  303. iconv_t input_cd; /* with iconv_lock */
  304. iconv_t output_cd; /* with iconv_lock */
  305. /* Port properties. */
  306. SCM alist;
  307. };
  308. #define SCM_UNICODE_BOM 0xFEFFUL /* Unicode byte-order mark */
  309. #define SCM_FILENAME(x) (SCM_PORT (x)->file_name)
  310. #define SCM_SET_FILENAME(x, n) (SCM_PORT (x)->file_name = (n))
  311. SCM_INTERNAL void scm_port_acquire_iconv_descriptors (SCM port,
  312. iconv_t *input_cd,
  313. iconv_t *output_cd);
  314. SCM_INTERNAL void scm_port_release_iconv_descriptors (SCM port);
  315. #endif