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