ports-internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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)
  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 (cur > end || end > scm_port_buffer_size (buf))
  184. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
  185. return end - cur;
  186. }
  187. static inline size_t
  188. scm_port_buffer_can_put (SCM buf)
  189. {
  190. size_t end = scm_to_size_t (scm_port_buffer_end (buf));
  191. if (end > scm_port_buffer_size (buf))
  192. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
  193. return scm_port_buffer_size (buf) - end;
  194. }
  195. static inline size_t
  196. scm_port_buffer_can_putback (SCM buf)
  197. {
  198. size_t cur = scm_to_size_t (scm_port_buffer_cur (buf));
  199. if (cur > scm_port_buffer_size (buf))
  200. scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
  201. return cur;
  202. }
  203. static inline void
  204. scm_port_buffer_did_take (SCM buf, size_t count)
  205. {
  206. scm_port_buffer_set_cur
  207. (buf, SCM_I_MAKINUM (SCM_I_INUM (scm_port_buffer_cur (buf)) + count));
  208. }
  209. static inline void
  210. scm_port_buffer_did_put (SCM buf, size_t count)
  211. {
  212. scm_port_buffer_set_end
  213. (buf, SCM_I_MAKINUM (SCM_I_INUM (scm_port_buffer_end (buf)) + count));
  214. }
  215. static inline const scm_t_uint8 *
  216. scm_port_buffer_take_pointer (SCM buf)
  217. {
  218. signed char *ret = SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf));
  219. return ((scm_t_uint8 *) ret) + scm_to_size_t (scm_port_buffer_cur (buf));
  220. }
  221. static inline scm_t_uint8 *
  222. scm_port_buffer_put_pointer (SCM buf)
  223. {
  224. signed char *ret = SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf));
  225. return ((scm_t_uint8 *) ret) + scm_to_size_t (scm_port_buffer_end (buf));
  226. }
  227. static inline size_t
  228. scm_port_buffer_take (SCM buf, scm_t_uint8 *dst, size_t count)
  229. {
  230. count = min (count, scm_port_buffer_can_take (buf));
  231. if (dst)
  232. memcpy (dst, scm_port_buffer_take_pointer (buf), count);
  233. scm_port_buffer_did_take (buf, count);
  234. return count;
  235. }
  236. static inline size_t
  237. scm_port_buffer_put (SCM buf, const scm_t_uint8 *src, size_t count)
  238. {
  239. count = min (count, scm_port_buffer_can_put (buf));
  240. if (src)
  241. memcpy (scm_port_buffer_put_pointer (buf), src, count);
  242. scm_port_buffer_did_put (buf, count);
  243. return count;
  244. }
  245. static inline void
  246. scm_port_buffer_putback (SCM buf, const scm_t_uint8 *src, size_t count)
  247. {
  248. size_t cur = scm_to_size_t (scm_port_buffer_cur (buf));
  249. assert (count <= cur);
  250. /* Sometimes used to move around data within a buffer, so we must use
  251. memmove. */
  252. cur -= count;
  253. scm_port_buffer_set_cur (buf, scm_from_size_t (cur));
  254. memmove (SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf)) + cur,
  255. src, count);
  256. }
  257. struct scm_t_port
  258. {
  259. /* Source location information. */
  260. SCM file_name;
  261. SCM position;
  262. /* Port buffers. */
  263. SCM read_buf;
  264. SCM write_buf;
  265. SCM write_buf_aux;
  266. /* All ports have read and write buffers; an unbuffered port simply
  267. has a one-byte buffer. However unreading bytes can expand the read
  268. buffer, but that doesn't mean that we want to increase the input
  269. buffering. For that reason `read_buffering' is a separate
  270. indication of how many characters to buffer on the read side.
  271. There isn't a write_buf_size because there isn't an
  272. `unwrite-byte'. */
  273. size_t read_buffering;
  274. /* True if the port is random access. Implies that the buffers must
  275. be flushed before switching between reading and writing, seeking,
  276. and so on. */
  277. unsigned rw_random : 1;
  278. unsigned at_stream_start_for_bom_read : 1;
  279. unsigned at_stream_start_for_bom_write : 1;
  280. /* Character encoding support. */
  281. SCM encoding; /* A symbol of upper-case ASCII. */
  282. SCM conversion_strategy; /* A symbol; either substitute, error, or escape. */
  283. /* This is the same as pt->encoding, except if `encoding' is UTF-16 or
  284. UTF-32, in which case this is UTF-16LE or a similar
  285. byte-order-specialed version of UTF-16 or UTF-32. This is a
  286. separate field from `encoding' because being just plain UTF-16 or
  287. UTF-32 has an additional meaning, being that we should consume and
  288. produce byte order marker codepoints as appropriate. Set to #f
  289. before the iconv descriptors have been opened. */
  290. SCM precise_encoding; /* with iconv_lock */
  291. iconv_t input_cd; /* with iconv_lock */
  292. iconv_t output_cd; /* with iconv_lock */
  293. /* Port properties. */
  294. SCM alist;
  295. };
  296. #define SCM_UNICODE_BOM 0xFEFFUL /* Unicode byte-order mark */
  297. #define SCM_FILENAME(x) (SCM_PORT (x)->file_name)
  298. #define SCM_SET_FILENAME(x, n) (SCM_PORT (x)->file_name = (n))
  299. SCM_INTERNAL void scm_port_acquire_iconv_descriptors (SCM port,
  300. iconv_t *input_cd,
  301. iconv_t *output_cd);
  302. SCM_INTERNAL void scm_port_release_iconv_descriptors (SCM port);
  303. #endif