0f46e7f51caef0acb5f0191021c959ef07740c09
[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, 2006, 2008, 2009 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 #include "libguile/__scm.h"
27
28 #include "libguile/print.h"
29 #include "libguile/struct.h"
30 #include "libguile/threads.h"
31 #include "libguile/strings.h"
32
33 \f
34
35 #define SCM_INITIAL_PUTBACK_BUF_SIZE 4
36
37 /* values for the rw_active flag. */
38 typedef enum scm_t_port_rw_active {
39 SCM_PORT_NEITHER = 0,
40 SCM_PORT_READ = 1,
41 SCM_PORT_WRITE = 2
42 } scm_t_port_rw_active;
43
44 /* C representation of a Scheme port. */
45
46 typedef struct
47 {
48 SCM port; /* Link back to the port object. */
49 int revealed; /* 0 not revealed, > 1 revealed.
50 * Revealed ports do not get GC'd.
51 */
52 /* data for the underlying port implementation as a raw C value. */
53 scm_t_bits stream;
54
55 SCM file_name; /* debugging support. */
56 long line_number; /* debugging support. */
57 int column_number; /* debugging support. */
58
59 /* Character encoding support */
60 char *encoding;
61 scm_t_string_failed_conversion_handler ilseq_handler;
62
63 /* port buffers. the buffer(s) are set up for all ports.
64 in the case of string ports, the buffer is the string itself.
65 in the case of unbuffered file ports, the buffer is a
66 single char: shortbuf. */
67
68 /* this buffer is filled from read_buf to read_end using the ptob
69 buffer_fill. then input requests are taken from read_pos until
70 it reaches read_end. */
71
72 unsigned char *read_buf; /* buffer start. */
73 const unsigned char *read_pos;/* the next unread char. */
74 unsigned char *read_end; /* pointer to last buffered char + 1. */
75 scm_t_off read_buf_size; /* size of the buffer. */
76
77 /* when chars are put back into the buffer, e.g., using peek-char or
78 unread-string, the read-buffer pointers are switched to cbuf.
79 the original pointers are saved here and restored when the put-back
80 chars have been consumed. */
81 unsigned char *saved_read_buf;
82 const unsigned char *saved_read_pos;
83 unsigned char *saved_read_end;
84 scm_t_off saved_read_buf_size;
85
86 /* write requests are saved into this buffer at write_pos until it
87 reaches write_buf + write_buf_size, then the ptob flush is
88 called. */
89
90 unsigned char *write_buf; /* buffer start. */
91 unsigned char *write_pos; /* pointer to last buffered char + 1. */
92 unsigned char *write_end; /* pointer to end of buffer + 1. */
93 scm_t_off write_buf_size; /* size of the buffer. */
94
95 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
96
97 int rw_random; /* true if the port is random access.
98 implies that the buffers must be
99 flushed before switching between
100 reading and writing, seeking, etc. */
101
102 scm_t_port_rw_active rw_active; /* for random access ports,
103 indicates which of the buffers
104 is currently in use. can be
105 SCM_PORT_WRITE, SCM_PORT_READ,
106 or SCM_PORT_NEITHER. */
107
108
109 /* a buffer for un-read chars and strings. */
110 unsigned char *putback_buf;
111 size_t putback_buf_size; /* allocated size of putback_buf. */
112 } scm_t_port;
113
114
115 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_port_table_mutex;
116 SCM_INTERNAL SCM scm_i_port_weak_hash;
117
118
119 #define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
120
121 \f
122
123 #define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
124
125 /* PORT FLAGS
126 * A set of flags characterizes a port.
127 * Note that we reserve the bits 1 << 24 and above for use by the
128 * routines in the port's scm_ptobfuns structure.
129 */
130 #define SCM_OPN (1L<<16) /* Is the port open? */
131 #define SCM_RDNG (2L<<16) /* Is it a readable port? */
132 #define SCM_WRTNG (4L<<16) /* Is it writable? */
133 #define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
134 #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
135
136 #define SCM_PORTP(x) (!SCM_IMP (x) && (SCM_TYP7 (x) == scm_tc7_port))
137 #define SCM_OPPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
138 #define SCM_OPINPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
139 #define SCM_OPOUTPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
140 #define SCM_INPUT_PORT_P(x) \
141 (!SCM_IMP(x) \
142 && (((0x7f | SCM_RDNG) & SCM_CELL_WORD_0(x)) == (scm_tc7_port | SCM_RDNG)))
143 #define SCM_OUTPUT_PORT_P(x) \
144 (!SCM_IMP(x) \
145 && (((0x7f | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_WRTNG)))
146 #define SCM_OPENP(x) (!SCM_IMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
147 #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
148 #define SCM_CLR_PORT_OPEN_FLAG(p) \
149 SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
150
151 #define SCM_PTAB_ENTRY(x) ((scm_t_port *) SCM_CELL_WORD_1 (x))
152 #define SCM_SETPTAB_ENTRY(x, ent) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
153 #define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
154 #define SCM_SETSTREAM(x, s) (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
155 #define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
156 #define SCM_SET_FILENAME(x, n) (SCM_PTAB_ENTRY(x)->file_name = (n))
157 #define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
158 #define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
159 #define SCM_REVEALED(x) (SCM_PTAB_ENTRY(x)->revealed)
160 #define SCM_SETREVEALED(x, s) (SCM_PTAB_ENTRY(x)->revealed = (s))
161
162 #define SCM_INCLINE(port) do {SCM_LINUM (port) += 1; SCM_COL (port) = 0;} while (0)
163 #define SCM_ZEROCOL(port) do {SCM_COL (port) = 0;} while (0)
164 #define SCM_INCCOL(port) do {SCM_COL (port) += 1;} while (0)
165 #define SCM_DECCOL(port) do {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;} while (0)
166 #define SCM_TABCOL(port) do {SCM_COL (port) += 8 - SCM_COL (port) % 8;} while (0)
167
168 /* Maximum number of port types. */
169 #define SCM_I_MAX_PORT_TYPE_COUNT 256
170
171 \f
172
173 /* port-type description. */
174 typedef struct scm_t_ptob_descriptor
175 {
176 char *name;
177 SCM (*mark) (SCM);
178 size_t (*free) (SCM);
179 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
180 SCM (*equalp) (SCM, SCM);
181 int (*close) (SCM port);
182
183 void (*write) (SCM port, const void *data, size_t size);
184 void (*flush) (SCM port);
185
186 void (*end_input) (SCM port, int offset);
187 int (*fill_input) (SCM port);
188 int (*input_waiting) (SCM port);
189
190 scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
191 void (*truncate) (SCM port, scm_t_off length);
192
193 } scm_t_ptob_descriptor;
194
195 #define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
196 #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
197 /* SCM_PTOBNAME can be 0 if name is missing */
198 #define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
199
200 \f
201
202 SCM_API scm_t_ptob_descriptor *scm_ptobs;
203 SCM_API long scm_numptob;
204
205 \f
206
207 SCM_API SCM scm_markstream (SCM ptr);
208 SCM_API scm_t_bits scm_make_port_type (char *name,
209 int (*fill_input) (SCM port),
210 void (*write) (SCM port,
211 const void *data,
212 size_t size));
213 SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
214 SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
215 SCM_API void scm_set_port_print (scm_t_bits tc,
216 int (*print) (SCM exp,
217 SCM port,
218 scm_print_state *pstate));
219 SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
220 SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
221
222 SCM_API void scm_set_port_flush (scm_t_bits tc,
223 void (*flush) (SCM port));
224 SCM_API void scm_set_port_end_input (scm_t_bits tc,
225 void (*end_input) (SCM port,
226 int offset));
227 SCM_API void scm_set_port_seek (scm_t_bits tc,
228 scm_t_off (*seek) (SCM port,
229 scm_t_off OFFSET,
230 int WHENCE));
231 SCM_API void scm_set_port_truncate (scm_t_bits tc,
232 void (*truncate) (SCM port,
233 scm_t_off length));
234 SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
235 SCM_API SCM scm_char_ready_p (SCM port);
236 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
237 SCM_API SCM scm_drain_input (SCM port);
238 SCM_API SCM scm_current_input_port (void);
239 SCM_API SCM scm_current_output_port (void);
240 SCM_API SCM scm_current_error_port (void);
241 SCM_API SCM scm_current_load_port (void);
242 SCM_API SCM scm_set_current_input_port (SCM port);
243 SCM_API SCM scm_set_current_output_port (SCM port);
244 SCM_API SCM scm_set_current_error_port (SCM port);
245 SCM_API void scm_dynwind_current_input_port (SCM port);
246 SCM_API void scm_dynwind_current_output_port (SCM port);
247 SCM_API void scm_dynwind_current_error_port (SCM port);
248 SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
249 SCM_INTERNAL void scm_i_remove_port (SCM port);
250 SCM_API void scm_grow_port_cbuf (SCM port, size_t requested);
251 SCM_API SCM scm_pt_size (void);
252 SCM_API SCM scm_pt_member (SCM member);
253 SCM_API void scm_port_non_buffer (scm_t_port *pt);
254 SCM_API int scm_revealed_count (SCM port);
255 SCM_API SCM scm_port_revealed (SCM port);
256 SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
257 SCM_API long scm_mode_bits (char *modes);
258 SCM_API SCM scm_port_mode (SCM port);
259 SCM_API SCM scm_close_input_port (SCM port);
260 SCM_API SCM scm_close_output_port (SCM port);
261 SCM_API SCM scm_close_port (SCM port);
262 SCM_API SCM scm_port_for_each (SCM proc);
263 SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
264 SCM_API SCM scm_input_port_p (SCM x);
265 SCM_API SCM scm_output_port_p (SCM x);
266 SCM_API SCM scm_port_p (SCM x);
267 SCM_API SCM scm_port_closed_p (SCM port);
268 SCM_API SCM scm_eof_object_p (SCM x);
269 SCM_API SCM scm_force_output (SCM port);
270 SCM_API SCM scm_flush_all_ports (void);
271 SCM_API SCM scm_read_char (SCM port);
272 SCM_API scm_t_wchar scm_getc (SCM port);
273 SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
274 SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
275 SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
276 SCM_INTERNAL void scm_lfwrite_str (SCM str, SCM port);
277 SCM_INTERNAL void scm_lfwrite_substr (SCM str, size_t start, size_t end,
278 SCM port);
279 SCM_API void scm_flush (SCM port);
280 SCM_API void scm_end_input (SCM port);
281 SCM_API int scm_fill_input (SCM port);
282 SCM_INTERNAL void scm_unget_byte (int c, SCM port);
283 SCM_API void scm_ungetc (scm_t_wchar c, SCM port);
284 SCM_API void scm_ungets (const char *s, int n, SCM port);
285 SCM_API SCM scm_peek_char (SCM port);
286 SCM_API SCM scm_unread_char (SCM cobj, SCM port);
287 SCM_API SCM scm_unread_string (SCM str, SCM port);
288 SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
289 SCM_API SCM scm_truncate_file (SCM object, SCM length);
290 SCM_API SCM scm_port_line (SCM port);
291 SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
292 SCM_API SCM scm_port_column (SCM port);
293 SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
294 SCM_API SCM scm_port_filename (SCM port);
295 SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
296 SCM_INTERNAL const char *scm_i_get_port_encoding (SCM port);
297 SCM_INTERNAL void scm_i_set_port_encoding_x (SCM port, const char *str);
298 SCM_API SCM scm_port_encoding (SCM port);
299 SCM_API SCM scm_set_port_encoding_x (SCM port, SCM encoding);
300 SCM_INTERNAL scm_t_string_failed_conversion_handler scm_i_get_conversion_strategy (SCM port);
301 SCM_INTERNAL void scm_i_set_conversion_strategy_x (SCM port,
302 scm_t_string_failed_conversion_handler h);
303 SCM_API SCM scm_port_conversion_strategy (SCM port);
304 SCM_API SCM scm_set_port_conversion_strategy_x (SCM port, SCM behavior);
305 SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
306 SCM_API void scm_print_port_mode (SCM exp, SCM port);
307 SCM_API void scm_ports_prehistory (void);
308 SCM_API SCM scm_void_port (char * mode_str);
309 SCM_API SCM scm_sys_make_void_port (SCM mode);
310 SCM_INTERNAL void scm_init_ports (void);
311
312 #if SCM_ENABLE_DEPRECATED==1
313 SCM_API scm_t_port * scm_add_to_port_table (SCM port);
314 #endif
315
316 #ifdef GUILE_DEBUG
317 SCM_API SCM scm_pt_size (void);
318 SCM_API SCM scm_pt_member (SCM member);
319 #endif /* GUILE_DEBUG */
320
321 /* internal */
322
323 SCM_INTERNAL long scm_i_mode_bits (SCM modes);
324 SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
325
326
327 #endif /* SCM_PORTS_H */
328
329 /*
330 Local Variables:
331 c-file-style: "gnu"
332 End:
333 */