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