ports.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #ifndef SCM_PORTS_H
  2. #define SCM_PORTS_H
  3. /* Copyright 1995-2001,2003-2004,2006,2008-2014,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/gc.h"
  18. #include "libguile/error.h"
  19. #include "libguile/print.h"
  20. #include "libguile/strings.h"
  21. SCM_INTERNAL SCM scm_i_port_weak_set;
  22. #define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
  23. /* A port's first word contains its tag, which is a tc7 value. Above
  24. there is a flag indicating whether the port is open or not, and then
  25. some "mode bits": flags indicating whether the port is an input
  26. and/or an output port and how Guile should buffer the port. */
  27. #define SCM_OPN (1U<<8) /* Is the port open? */
  28. #define SCM_RDNG (1U<<9) /* Is it a readable port? */
  29. #define SCM_WRTNG (1U<<10) /* Is it writable? */
  30. #define SCM_BUF0 (1U<<11) /* Is it unbuffered? */
  31. #define SCM_BUFLINE (1U<<12) /* Is it line-buffered? */
  32. #ifdef BUILDING_LIBGUILE
  33. #define SCM_F_PORT_FINALIZING (1U<<13) /* Port is being closed via GC. */
  34. #endif
  35. #define SCM_PORTP(x) (SCM_HAS_TYP7 (x, scm_tc7_port))
  36. #define SCM_OPPORTP(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_OPN))
  37. #define SCM_INPUT_PORT_P(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_RDNG))
  38. #define SCM_OUTPUT_PORT_P(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_WRTNG))
  39. #define SCM_OPINPORTP(x) (SCM_OPPORTP (x) && SCM_INPUT_PORT_P (x))
  40. #define SCM_OPOUTPORTP(x) (SCM_OPPORTP (x) && SCM_OUTPUT_PORT_P (x))
  41. #define SCM_OPENP(x) (SCM_OPPORTP (x))
  42. #define SCM_CLOSEDP(x) (!SCM_OPENP (x))
  43. #define SCM_CLR_PORT_OPEN_FLAG(p) \
  44. SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
  45. #ifdef BUILDING_LIBGUILE
  46. #define SCM_PORT_FINALIZING_P(x) \
  47. (SCM_CELL_WORD_0 (x) & SCM_F_PORT_FINALIZING)
  48. #define SCM_SET_PORT_FINALIZING(p) \
  49. SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) | SCM_F_PORT_FINALIZING)
  50. #endif
  51. typedef struct scm_t_port_type scm_t_port_type;
  52. typedef struct scm_t_port scm_t_port;
  53. #define SCM_STREAM(port) (SCM_CELL_WORD_1 (port))
  54. #define SCM_SETSTREAM(port, stream) (SCM_SET_CELL_WORD_1 (port, stream))
  55. #define SCM_PORT(x) ((scm_t_port *) SCM_CELL_WORD_2 (x))
  56. #define SCM_PORT_TYPE(port) ((scm_t_port_type *) SCM_CELL_WORD_3 (port))
  57. #define SCM_VALIDATE_PORT(pos, port) \
  58. SCM_MAKE_VALIDATE_MSG (pos, port, PORTP, "port")
  59. #define SCM_VALIDATE_INPUT_PORT(pos, port) \
  60. SCM_MAKE_VALIDATE_MSG (pos, port, INPUT_PORT_P, "input port")
  61. #define SCM_VALIDATE_OUTPUT_PORT(pos, port) \
  62. SCM_MAKE_VALIDATE_MSG (pos, port, OUTPUT_PORT_P, "output port")
  63. #define SCM_VALIDATE_OPINPORT(pos, port) \
  64. SCM_MAKE_VALIDATE_MSG (pos, port, OPINPORTP, "open input port")
  65. #define SCM_VALIDATE_OPENPORT(pos, port) \
  66. do { \
  67. SCM_ASSERT (SCM_PORTP (port) && SCM_OPENP (port), \
  68. port, pos, FUNC_NAME); \
  69. } while (0)
  70. #define SCM_VALIDATE_OPPORT(pos, port) \
  71. SCM_MAKE_VALIDATE_MSG (pos, port, OPPORTP, "open port")
  72. #define SCM_VALIDATE_OPOUTPORT(pos, port) \
  73. SCM_MAKE_VALIDATE_MSG (pos, port, OPOUTPORTP, "open output port")
  74. /* Port types, and their vtables. */
  75. SCM_API scm_t_port_type *scm_make_port_type
  76. (char *name,
  77. size_t (*read) (SCM port, SCM dst, size_t start, size_t count),
  78. size_t (*write) (SCM port, SCM src, size_t start, size_t count));
  79. SCM_API void scm_set_port_scm_read (scm_t_port_type *ptob, SCM read);
  80. SCM_API void scm_set_port_scm_write (scm_t_port_type *ptob, SCM write);
  81. SCM_API void scm_set_port_read_wait_fd (scm_t_port_type *ptob,
  82. int (*wait_fd) (SCM port));
  83. SCM_API void scm_set_port_write_wait_fd (scm_t_port_type *ptob,
  84. int (*wait_fd) (SCM port));
  85. SCM_API void scm_set_port_print (scm_t_port_type *ptob,
  86. int (*print) (SCM exp,
  87. SCM port,
  88. scm_print_state *pstate));
  89. SCM_API void scm_set_port_close (scm_t_port_type *ptob, void (*close) (SCM));
  90. SCM_API void scm_set_port_needs_close_on_gc (scm_t_port_type *ptob,
  91. int needs_close_p);
  92. SCM_API void scm_set_port_seek (scm_t_port_type *ptob,
  93. scm_t_off (*seek) (SCM port,
  94. scm_t_off OFFSET,
  95. int WHENCE));
  96. SCM_API void scm_set_port_truncate (scm_t_port_type *ptob,
  97. void (*truncate) (SCM port,
  98. scm_t_off length));
  99. SCM_API void scm_set_port_input_waiting (scm_t_port_type *ptob,
  100. int (*input_waiting) (SCM));
  101. SCM_API void scm_set_port_get_natural_buffer_sizes
  102. (scm_t_port_type *ptob,
  103. void (*get_natural_buffer_sizes) (SCM, size_t *, size_t *));
  104. SCM_API void scm_set_port_random_access_p (scm_t_port_type *ptob,
  105. int (*random_access_p) (SCM port));
  106. /* The input, output, error, and load ports. */
  107. SCM_API SCM scm_current_input_port (void);
  108. SCM_API SCM scm_current_output_port (void);
  109. SCM_API SCM scm_current_error_port (void);
  110. SCM_API SCM scm_current_warning_port (void);
  111. SCM_API SCM scm_current_load_port (void);
  112. SCM_API SCM scm_set_current_input_port (SCM port);
  113. SCM_API SCM scm_set_current_output_port (SCM port);
  114. SCM_API SCM scm_set_current_error_port (SCM port);
  115. SCM_API SCM scm_set_current_warning_port (SCM port);
  116. SCM_API void scm_dynwind_current_input_port (SCM port);
  117. SCM_API void scm_dynwind_current_output_port (SCM port);
  118. SCM_API void scm_dynwind_current_error_port (SCM port);
  119. SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
  120. /* Mode bits. */
  121. SCM_INTERNAL long scm_i_mode_bits (SCM modes);
  122. SCM_API long scm_mode_bits (char *modes);
  123. SCM_API SCM scm_port_mode (SCM port);
  124. /* Low-level constructors. */
  125. SCM_API SCM scm_c_make_port_with_encoding (scm_t_port_type *ptob,
  126. unsigned long mode_bits,
  127. SCM encoding,
  128. SCM conversion_strategy,
  129. scm_t_bits stream);
  130. SCM_API SCM scm_c_make_port (scm_t_port_type *ptob, unsigned long mode_bits,
  131. scm_t_bits stream);
  132. /* Predicates. */
  133. SCM_API SCM scm_port_p (SCM x);
  134. SCM_API SCM scm_input_port_p (SCM x);
  135. SCM_API SCM scm_output_port_p (SCM x);
  136. SCM_API SCM scm_port_closed_p (SCM port);
  137. SCM_API SCM scm_eof_object_p (SCM x);
  138. /* Closing ports. */
  139. SCM_API SCM scm_close_port (SCM port);
  140. SCM_API SCM scm_close_input_port (SCM port);
  141. SCM_API SCM scm_close_output_port (SCM port);
  142. /* Encoding characters to byte streams, and decoding byte streams to
  143. characters. */
  144. SCM_INTERNAL scm_t_string_failed_conversion_handler
  145. scm_i_string_failed_conversion_handler (SCM conversion_strategy);
  146. SCM_INTERNAL SCM scm_i_default_port_encoding (void);
  147. SCM_INTERNAL void scm_i_set_default_port_encoding (const char *encoding);
  148. SCM_INTERNAL SCM scm_i_default_port_conversion_strategy (void);
  149. SCM_INTERNAL void scm_i_set_default_port_conversion_strategy (SCM strategy);
  150. SCM_INTERNAL void scm_i_set_port_encoding_x (SCM port, const char *str);
  151. SCM_INTERNAL SCM scm_sys_port_encoding (SCM port);
  152. SCM_INTERNAL SCM scm_sys_set_port_encoding_x (SCM port, SCM encoding);
  153. SCM_API SCM scm_port_encoding (SCM port);
  154. SCM_API SCM scm_set_port_encoding_x (SCM port, SCM encoding);
  155. SCM_API SCM scm_port_conversion_strategy (SCM port);
  156. SCM_API SCM scm_set_port_conversion_strategy_x (SCM port, SCM behavior);
  157. /* Input. */
  158. SCM_INTERNAL SCM scm_port_maybe_consume_initial_byte_order_mark (SCM, SCM, SCM);
  159. SCM_API int scm_get_byte_or_eof (SCM port);
  160. SCM_API int scm_peek_byte_or_eof (SCM port);
  161. SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
  162. SCM_API size_t scm_c_read_bytes (SCM port, SCM dst, size_t start, size_t count);
  163. SCM_API scm_t_wchar scm_getc (SCM port);
  164. SCM_API SCM scm_read_char (SCM port);
  165. /* Pushback. */
  166. SCM_API void scm_unget_bytes (const unsigned char *buf, size_t len, SCM port);
  167. SCM_API void scm_unget_byte (int c, SCM port);
  168. SCM_API void scm_ungetc (scm_t_wchar c, SCM port);
  169. SCM_API void scm_ungets (const char *s, int n, SCM port);
  170. SCM_API SCM scm_peek_char (SCM port);
  171. SCM_API SCM scm_unread_char (SCM cobj, SCM port);
  172. SCM_API SCM scm_unread_string (SCM str, SCM port);
  173. /* Manipulating the buffers. */
  174. SCM_API SCM scm_setvbuf (SCM port, SCM mode, SCM size);
  175. SCM_INTERNAL SCM scm_fill_input (SCM port, size_t minimum_size,
  176. size_t *cur_out, size_t *avail_out);
  177. SCM_INTERNAL size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
  178. SCM_API SCM scm_drain_input (SCM port);
  179. SCM_API void scm_end_input (SCM port);
  180. SCM_API SCM scm_force_output (SCM port);
  181. SCM_API void scm_flush (SCM port);
  182. SCM_INTERNAL SCM scm_port_random_access_p (SCM port);
  183. SCM_INTERNAL SCM scm_port_read_buffering (SCM port);
  184. SCM_INTERNAL SCM scm_expand_port_read_buffer_x (SCM port, SCM size,
  185. SCM putback_p);
  186. SCM_INTERNAL SCM scm_port_read (SCM port);
  187. SCM_INTERNAL SCM scm_port_write (SCM port);
  188. SCM_INTERNAL SCM scm_port_read_buffer (SCM port);
  189. SCM_INTERNAL SCM scm_port_write_buffer (SCM port);
  190. SCM_INTERNAL SCM scm_port_auxiliary_write_buffer (SCM port);
  191. /* Output. */
  192. SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
  193. SCM_API void scm_c_write_bytes (SCM port, SCM src, size_t start, size_t count);
  194. SCM_API void scm_c_put_latin1_chars (SCM port, const uint8_t *buf,
  195. size_t len);
  196. SCM_API void scm_c_put_utf32_chars (SCM port, const uint32_t *buf,
  197. size_t len);
  198. SCM_API void scm_c_put_string (SCM port, SCM str, size_t start, size_t count);
  199. SCM_API SCM scm_put_string (SCM port, SCM str, SCM start, SCM count);
  200. SCM_API void scm_c_put_char (SCM port, scm_t_wchar ch);
  201. SCM_API SCM scm_put_char (SCM port, SCM ch);
  202. SCM_INTERNAL void scm_c_put_escaped_char (SCM port, scm_t_wchar ch);
  203. SCM_INTERNAL int scm_c_can_put_char (SCM port, scm_t_wchar ch);
  204. SCM_API void scm_putc (char c, SCM port);
  205. SCM_API void scm_puts (const char *str_data, SCM port);
  206. SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
  207. SCM_INTERNAL void scm_lfwrite_substr (SCM str, size_t start, size_t end,
  208. SCM port);
  209. /* Querying and setting positions, and character availability. */
  210. SCM_API SCM scm_char_ready_p (SCM port);
  211. SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
  212. SCM_API SCM scm_truncate_file (SCM object, SCM length);
  213. SCM_API SCM scm_port_line (SCM port);
  214. SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
  215. SCM_API SCM scm_port_column (SCM port);
  216. SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
  217. SCM_API SCM scm_port_filename (SCM port);
  218. SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
  219. /* Port properties. */
  220. SCM_INTERNAL SCM scm_i_port_property (SCM port, SCM key);
  221. SCM_INTERNAL SCM scm_i_set_port_property_x (SCM port, SCM key, SCM value);
  222. /* Implementation helpers for port printing functions. */
  223. SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
  224. SCM_API void scm_print_port_mode (SCM exp, SCM port);
  225. /* Iterating over all ports. */
  226. SCM_API SCM scm_port_for_each (SCM proc);
  227. SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
  228. SCM_API SCM scm_flush_all_ports (void);
  229. /* Void ports. */
  230. SCM_API SCM scm_void_port (char * mode_str);
  231. SCM_API SCM scm_sys_make_void_port (SCM mode);
  232. /* Initialization. */
  233. SCM_INTERNAL void scm_init_ports (void);
  234. #endif /* SCM_PORTS_H */