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