ports.h 11 KB

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