Merge commit '8571dbde639e0ee9885bad49c9e180474bd23646'
[bpt/guile.git] / libguile / ports.h
1 /* classes: h_files */
2
3 #ifndef SCM_PORTS_H
4 #define SCM_PORTS_H
5
6 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004,
7 * 2006, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25 \f
26
27 #include "libguile/__scm.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include "libguile/gc.h"
33 #include "libguile/tags.h"
34 #include "libguile/error.h"
35 #include "libguile/print.h"
36 #include "libguile/struct.h"
37 #include "libguile/threads.h"
38 #include "libguile/strings.h"
39
40 \f
41
42 #define SCM_INITIAL_PUTBACK_BUF_SIZE 4
43
44 /* values for the rw_active flag. */
45 typedef enum scm_t_port_rw_active {
46 SCM_PORT_NEITHER = 0,
47 SCM_PORT_READ = 1,
48 SCM_PORT_WRITE = 2
49 } scm_t_port_rw_active;
50
51 /* An internal-only structure defined in ports-internal.h. */
52 struct scm_port_internal;
53
54 /* C representation of a Scheme port. */
55
56 typedef struct
57 {
58 SCM port; /* Link back to the port object. */
59 scm_i_pthread_mutex_t *lock; /* A recursive lock for this port. */
60
61 /* pointer to internal-only port structure */
62 struct scm_port_internal *internal;
63
64 /* data for the underlying port implementation as a raw C value. */
65 scm_t_bits stream;
66
67 SCM file_name; /* debugging support. */
68 long line_number; /* debugging support. */
69 int column_number; /* debugging support. */
70
71 /* port buffers. the buffer(s) are set up for all ports.
72 in the case of string ports, the buffer is the string itself.
73 in the case of unbuffered file ports, the buffer is a
74 single char: shortbuf. */
75
76 /* this buffer is filled from read_buf to read_end using the ptob
77 buffer_fill. then input requests are taken from read_pos until
78 it reaches read_end. */
79
80 unsigned char *read_buf; /* buffer start. */
81 const unsigned char *read_pos;/* the next unread char. */
82 unsigned char *read_end; /* pointer to last buffered char + 1. */
83 scm_t_off read_buf_size; /* size of the buffer. */
84
85 /* when chars are put back into the buffer, e.g., using peek-char or
86 unread-string, the read-buffer pointers are switched to cbuf.
87 the original pointers are saved here and restored when the put-back
88 chars have been consumed. */
89 unsigned char *saved_read_buf;
90 const unsigned char *saved_read_pos;
91 unsigned char *saved_read_end;
92 scm_t_off saved_read_buf_size;
93
94 /* write requests are saved into this buffer at write_pos until it
95 reaches write_buf + write_buf_size, then the ptob flush is
96 called. */
97
98 unsigned char *write_buf; /* buffer start. */
99 unsigned char *write_pos; /* pointer to last buffered char + 1. */
100 unsigned char *write_end; /* pointer to end of buffer + 1. */
101 scm_t_off write_buf_size; /* size of the buffer. */
102
103 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
104
105 int rw_random; /* true if the port is random access.
106 implies that the buffers must be
107 flushed before switching between
108 reading and writing, seeking, etc. */
109
110 scm_t_port_rw_active rw_active; /* for random access ports,
111 indicates which of the buffers
112 is currently in use. can be
113 SCM_PORT_WRITE, SCM_PORT_READ,
114 or SCM_PORT_NEITHER. */
115
116
117 /* a buffer for un-read chars and strings. */
118 unsigned char *putback_buf;
119 size_t putback_buf_size; /* allocated size of putback_buf. */
120
121 /* Character encoding support */
122 char *encoding;
123 scm_t_string_failed_conversion_handler ilseq_handler;
124 } scm_t_port;
125
126
127 SCM_INTERNAL SCM scm_i_port_weak_set;
128
129
130 #define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
131
132 \f
133
134 #define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
135
136 /* PORT FLAGS
137 * A set of flags characterizes a port.
138 * Note that we reserve the bits 1 << 24 and above for use by the
139 * routines in the port's scm_ptobfuns structure.
140 */
141 #define SCM_OPN (1L<<16) /* Is the port open? */
142 #define SCM_RDNG (2L<<16) /* Is it a readable port? */
143 #define SCM_WRTNG (4L<<16) /* Is it writable? */
144 #define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
145 #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
146
147 #define SCM_PORTP(x) (SCM_HAS_TYP7 (x, scm_tc7_port))
148 #define SCM_OPPORTP(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_OPN))
149 #define SCM_INPUT_PORT_P(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_RDNG))
150 #define SCM_OUTPUT_PORT_P(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_WRTNG))
151 #define SCM_OPINPORTP(x) (SCM_OPPORTP (x) && SCM_INPUT_PORT_P (x))
152 #define SCM_OPOUTPORTP(x) (SCM_OPPORTP (x) && SCM_OUTPUT_PORT_P (x))
153 #define SCM_OPENP(x) (SCM_OPPORTP (x))
154 #define SCM_CLOSEDP(x) (!SCM_OPENP (x))
155 #define SCM_CLR_PORT_OPEN_FLAG(p) \
156 SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
157
158 #define SCM_PTAB_ENTRY(x) ((scm_t_port *) SCM_CELL_WORD_1 (x))
159 #define SCM_PORT_DESCRIPTOR(port) ((scm_t_ptob_descriptor *) SCM_CELL_WORD_2 (port))
160 #define SCM_SETPTAB_ENTRY(x, ent) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
161 #define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
162 #define SCM_SETSTREAM(x, s) (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
163 #define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
164 #define SCM_SET_FILENAME(x, n) (SCM_PTAB_ENTRY(x)->file_name = (n))
165 #define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
166 #define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
167
168 #define SCM_INCLINE(port) do {SCM_LINUM (port) += 1; SCM_COL (port) = 0;} while (0)
169 #define SCM_ZEROCOL(port) do {SCM_COL (port) = 0;} while (0)
170 #define SCM_INCCOL(port) do {SCM_COL (port) += 1;} while (0)
171 #define SCM_DECCOL(port) do {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;} while (0)
172 #define SCM_TABCOL(port) do {SCM_COL (port) += 8 - SCM_COL (port) % 8;} while (0)
173
174 /* Maximum number of port types. */
175 #define SCM_I_MAX_PORT_TYPE_COUNT 256
176
177 \f
178
179 typedef enum scm_t_port_type_flags {
180 SCM_PORT_TYPE_HAS_FLUSH = 1 << 0
181 } scm_t_port_type_flags;
182
183 /* port-type description. */
184 typedef struct scm_t_ptob_descriptor
185 {
186 char *name;
187 SCM (*mark) (SCM);
188 size_t (*free) (SCM);
189 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
190 SCM (*equalp) (SCM, SCM);
191 int (*close) (SCM port);
192
193 void (*write) (SCM port, const void *data, size_t size);
194 void (*flush) (SCM port);
195
196 void (*end_input) (SCM port, int offset);
197 int (*fill_input) (SCM port);
198 int (*input_waiting) (SCM port);
199
200 scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
201 void (*truncate) (SCM port, scm_t_off length);
202
203 unsigned flags;
204 } scm_t_ptob_descriptor;
205
206 #define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
207 #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
208 /* SCM_PTOBNAME can be 0 if name is missing */
209 #define SCM_PTOBNAME(ptobnum) (scm_c_port_type_ref (ptobnum)->name)
210
211 /* Port types, and their vtables. */
212 SCM_INTERNAL long scm_c_num_port_types (void);
213 SCM_API scm_t_ptob_descriptor* scm_c_port_type_ref (long ptobnum);
214 SCM_API long scm_c_port_type_add_x (scm_t_ptob_descriptor *desc);
215 SCM_API scm_t_bits scm_make_port_type (char *name,
216 int (*fill_input) (SCM port),
217 void (*write) (SCM port,
218 const void *data,
219 size_t size));
220 SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
221 SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
222 SCM_API void scm_set_port_print (scm_t_bits tc,
223 int (*print) (SCM exp,
224 SCM port,
225 scm_print_state *pstate));
226 SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
227 SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
228
229 SCM_API void scm_set_port_flush (scm_t_bits tc, void (*flush) (SCM port));
230 SCM_API void scm_set_port_end_input (scm_t_bits tc,
231 void (*end_input) (SCM port,
232 int offset));
233 SCM_API void scm_set_port_seek (scm_t_bits tc,
234 scm_t_off (*seek) (SCM port,
235 scm_t_off OFFSET,
236 int WHENCE));
237 SCM_API void scm_set_port_truncate (scm_t_bits tc,
238 void (*truncate) (SCM port,
239 scm_t_off length));
240 SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
241
242 /* The input, output, error, and load ports. */
243 SCM_API SCM scm_current_input_port (void);
244 SCM_API SCM scm_current_output_port (void);
245 SCM_API SCM scm_current_error_port (void);
246 SCM_API SCM scm_current_warning_port (void);
247 SCM_API SCM scm_current_load_port (void);
248 SCM_API SCM scm_set_current_input_port (SCM port);
249 SCM_API SCM scm_set_current_output_port (SCM port);
250 SCM_API SCM scm_set_current_error_port (SCM port);
251 SCM_API SCM scm_set_current_warning_port (SCM port);
252 SCM_API void scm_dynwind_current_input_port (SCM port);
253 SCM_API void scm_dynwind_current_output_port (SCM port);
254 SCM_API void scm_dynwind_current_error_port (SCM port);
255 SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
256
257 /* Mode bits. */
258 SCM_INTERNAL long scm_i_mode_bits (SCM modes);
259 SCM_API long scm_mode_bits (char *modes);
260 SCM_API SCM scm_port_mode (SCM port);
261
262 /* Low-level constructors. */
263 SCM_API SCM
264 scm_c_make_port_with_encoding (scm_t_bits tag,
265 unsigned long mode_bits,
266 const char *encoding,
267 scm_t_string_failed_conversion_handler handler,
268 scm_t_bits stream);
269 SCM_API SCM scm_c_make_port (scm_t_bits tag, unsigned long mode_bits,
270 scm_t_bits stream);
271 SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
272
273 /* Predicates. */
274 SCM_API SCM scm_port_p (SCM x);
275 SCM_API SCM scm_input_port_p (SCM x);
276 SCM_API SCM scm_output_port_p (SCM x);
277 SCM_API SCM scm_port_closed_p (SCM port);
278 SCM_API SCM scm_eof_object_p (SCM x);
279
280 /* Closing ports. */
281 SCM_API SCM scm_close_port (SCM port);
282 SCM_API SCM scm_close_input_port (SCM port);
283 SCM_API SCM scm_close_output_port (SCM port);
284
285 /* Encoding characters to byte streams, and decoding byte streams to
286 characters. */
287 SCM_INTERNAL const char *scm_i_default_port_encoding (void);
288 SCM_INTERNAL void scm_i_set_default_port_encoding (const char *);
289 SCM_INTERNAL scm_t_string_failed_conversion_handler
290 scm_i_default_port_conversion_handler (void);
291 SCM_INTERNAL void
292 scm_i_set_default_port_conversion_handler (scm_t_string_failed_conversion_handler);
293 SCM_INTERNAL void scm_i_set_port_encoding_x (SCM port, const char *str);
294 SCM_API SCM scm_port_encoding (SCM port);
295 SCM_API SCM scm_set_port_encoding_x (SCM port, SCM encoding);
296 SCM_API SCM scm_port_conversion_strategy (SCM port);
297 SCM_API SCM scm_set_port_conversion_strategy_x (SCM port, SCM behavior);
298
299 /* Acquiring and releasing the port lock. */
300 SCM_API void scm_dynwind_lock_port (SCM port);
301 SCM_INLINE int scm_c_lock_port (SCM port, scm_i_pthread_mutex_t **lock);
302 SCM_INLINE int scm_c_try_lock_port (SCM port, scm_i_pthread_mutex_t **lock);
303
304 /* Input. */
305 SCM_API int scm_get_byte_or_eof (SCM port);
306 SCM_INLINE int scm_get_byte_or_eof_unlocked (SCM port);
307 SCM_API int scm_slow_get_byte_or_eof_unlocked (SCM port);
308 SCM_API int scm_peek_byte_or_eof (SCM port);
309 SCM_INLINE int scm_peek_byte_or_eof_unlocked (SCM port);
310 SCM_API int scm_slow_peek_byte_or_eof_unlocked (SCM port);
311 SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
312 SCM_API size_t scm_c_read_unlocked (SCM port, void *buffer, size_t size);
313 SCM_API scm_t_wchar scm_getc (SCM port);
314 SCM_API scm_t_wchar scm_getc_unlocked (SCM port);
315 SCM_API SCM scm_read_char (SCM port);
316
317 /* Pushback. */
318 SCM_API void scm_unget_bytes (const unsigned char *buf, size_t len, SCM port);
319 SCM_API void scm_unget_bytes_unlocked (const unsigned char *buf, size_t len, SCM port);
320 SCM_API void scm_unget_byte (int c, SCM port);
321 SCM_API void scm_unget_byte_unlocked (int c, SCM port);
322 SCM_API void scm_ungetc (scm_t_wchar c, SCM port);
323 SCM_API void scm_ungetc_unlocked (scm_t_wchar c, SCM port);
324 SCM_API void scm_ungets (const char *s, int n, SCM port);
325 SCM_API void scm_ungets_unlocked (const char *s, int n, SCM port);
326 SCM_API SCM scm_peek_char (SCM port);
327 SCM_API SCM scm_unread_char (SCM cobj, SCM port);
328 SCM_API SCM scm_unread_string (SCM str, SCM port);
329
330 /* Manipulating the buffers. */
331 SCM_API void scm_port_non_buffer (scm_t_port *pt);
332 SCM_API int scm_fill_input (SCM port);
333 SCM_API int scm_fill_input_unlocked (SCM port);
334 SCM_INTERNAL size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
335 SCM_API SCM scm_drain_input (SCM port);
336 SCM_API void scm_end_input (SCM port);
337 SCM_API void scm_end_input_unlocked (SCM port);
338 SCM_API SCM scm_force_output (SCM port);
339 SCM_API void scm_flush (SCM port);
340 SCM_API void scm_flush_unlocked (SCM port);
341
342 /* Output. */
343 SCM_API void scm_putc (char c, SCM port);
344 SCM_INLINE void scm_putc_unlocked (char c, SCM port);
345 SCM_API void scm_puts (const char *str_data, SCM port);
346 SCM_INLINE void scm_puts_unlocked (const char *str_data, SCM port);
347 SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
348 SCM_API void scm_c_write_unlocked (SCM port, const void *buffer, size_t size);
349 SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
350 SCM_API void scm_lfwrite_unlocked (const char *ptr, size_t size, SCM port);
351 SCM_INTERNAL void scm_lfwrite_substr (SCM str, size_t start, size_t end,
352 SCM port);
353
354 /* Querying and setting positions, and character availability. */
355 SCM_API SCM scm_char_ready_p (SCM port);
356 SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
357 SCM_API SCM scm_truncate_file (SCM object, SCM length);
358 SCM_API SCM scm_port_line (SCM port);
359 SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
360 SCM_API SCM scm_port_column (SCM port);
361 SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
362 SCM_API SCM scm_port_filename (SCM port);
363 SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
364
365 /* Port alist. */
366 SCM_INTERNAL SCM scm_i_port_alist (SCM port);
367 SCM_INTERNAL void scm_i_set_port_alist_x (SCM port, SCM alist);
368
369 /* Implementation helpers for port printing functions. */
370 SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
371 SCM_API void scm_print_port_mode (SCM exp, SCM port);
372
373 /* Iterating over all ports. */
374 SCM_API SCM scm_port_for_each (SCM proc);
375 SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
376 SCM_API SCM scm_flush_all_ports (void);
377
378 /* Void ports. */
379 SCM_API SCM scm_void_port (char * mode_str);
380 SCM_API SCM scm_sys_make_void_port (SCM mode);
381
382 /* Initialization. */
383 SCM_INTERNAL void scm_init_ports (void);
384
385
386 /* Inline function implementations. */
387
388 #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
389 SCM_INLINE_IMPLEMENTATION int
390 scm_c_lock_port (SCM port, scm_i_pthread_mutex_t **lock)
391 {
392 *lock = SCM_PTAB_ENTRY (port)->lock;
393
394 if (*lock)
395 return scm_i_pthread_mutex_lock (*lock);
396 else
397 return 0;
398 }
399
400 SCM_INLINE_IMPLEMENTATION int
401 scm_c_try_lock_port (SCM port, scm_i_pthread_mutex_t **lock)
402 {
403 *lock = SCM_PTAB_ENTRY (port)->lock;
404 if (*lock)
405 {
406 int ret = scm_i_pthread_mutex_trylock (*lock);
407 if (ret != 0)
408 *lock = NULL;
409 return ret;
410 }
411 else
412 return 0;
413 }
414
415 SCM_INLINE_IMPLEMENTATION int
416 scm_get_byte_or_eof_unlocked (SCM port)
417 {
418 scm_t_port *pt = SCM_PTAB_ENTRY (port);
419
420 if (SCM_LIKELY ((pt->rw_active == SCM_PORT_READ || !pt->rw_random)
421 && pt->read_pos < pt->read_end))
422 return *pt->read_pos++;
423 else
424 return scm_slow_get_byte_or_eof_unlocked (port);
425 }
426
427 /* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */
428 SCM_INLINE_IMPLEMENTATION int
429 scm_peek_byte_or_eof_unlocked (SCM port)
430 {
431 scm_t_port *pt = SCM_PTAB_ENTRY (port);
432
433 if (SCM_LIKELY ((pt->rw_active == SCM_PORT_READ || !pt->rw_random)
434 && pt->read_pos < pt->read_end))
435 return *pt->read_pos;
436 else
437 return scm_slow_peek_byte_or_eof_unlocked (port);
438 }
439
440 SCM_INLINE_IMPLEMENTATION void
441 scm_putc_unlocked (char c, SCM port)
442 {
443 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
444 scm_lfwrite_unlocked (&c, 1, port);
445 }
446
447 SCM_INLINE_IMPLEMENTATION void
448 scm_puts_unlocked (const char *s, SCM port)
449 {
450 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
451 scm_lfwrite_unlocked (s, strlen (s), port);
452 }
453 #endif /* SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES */
454
455 #endif /* SCM_PORTS_H */
456
457 /*
458 Local Variables:
459 c-file-style: "gnu"
460 End:
461 */