* ports.h: #include <sys/types.h>, to get a definition for `off_t'.
[bpt/guile.git] / libguile / ports.h
1 /* classes: h_files */
2
3 #ifndef PORTSH
4 #define PORTSH
5 /* Copyright (C) 1995,1996,1997,1998,1999 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 \f
46 #include "libguile/__scm.h"
47
48 #include "libguile/print.h"
49 #include "libguile/struct.h"
50
51 /* Not sure if this is a good idea. We need it for off_t. */
52 #include <sys/types.h>
53
54 \f
55
56 #define SCM_INITIAL_CBUF_SIZE 4
57
58 /* C representation of a Scheme port. */
59
60 typedef struct
61 {
62 SCM port; /* Link back to the port object. */
63 int entry; /* Index in port table. */
64 int revealed; /* 0 not revealed, > 1 revealed.
65 * Revealed ports do not get GC'd.
66 */
67 /* data for the underlying port implementation. may be an SCM cell or
68 cast to a pointer to C data. */
69 SCM stream;
70
71 SCM file_name; /* debugging support. */
72 int line_number; /* debugging support. */
73 int column_number; /* debugging support. */
74
75 /* port buffers. the buffer(s) are set up for all ports.
76 in the case of string ports, the buffer is the string itself.
77 in the case of unbuffered file ports, the buffer is a
78 single char: shortbuf. */
79
80 /* this buffer is filled from read_buf to read_end using the ptob
81 buffer_fill. then input requests are taken from read_pos until
82 it reaches read_end. */
83
84 unsigned char *read_buf; /* buffer start. */
85 const unsigned char *read_pos;/* the next unread char. */
86 unsigned char *read_end; /* pointer to last buffered char + 1. */
87 off_t read_buf_size; /* size of the buffer. */
88
89 /* write requests are saved into this buffer at write_pos until it
90 reaches write_buf + write_buf_size, then the ptob flush is
91 called. */
92
93 unsigned char *write_buf; /* buffer start. */
94 unsigned char *write_pos; /* pointer to last buffered char + 1. */
95 unsigned char *write_end; /* pointer to end of buffer + 1. */
96 off_t write_buf_size; /* size of the buffer. */
97
98 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
99
100 int rw_random; /* true if the port is bidirectional and
101 random access. implies that the buffers
102 must be flushed before switching between
103 reading and writing. */
104
105 int rw_active; /* for bidirectional random ports, indicates
106 which of the buffers is currently in use.
107 can be SCM_PORT_WRITE, SCM_PORT_READ,
108 or 0. */
109
110 /* a completely separate buffer which is only used for un-read chars
111 and strings. */
112 unsigned char *cp; /* where to put and get unget chars */
113 unsigned char *cbufend; /* points after this struct */
114 unsigned char cbuf[SCM_INITIAL_CBUF_SIZE]; /* must be last: may grow */
115 } scm_port;
116
117 /* values for the rw_active flag. */
118 #define SCM_PORT_READ 1
119 #define SCM_PORT_WRITE 2
120
121 extern scm_port **scm_port_table;
122 extern int scm_port_table_size; /* Number of ports in scm_port_table. */
123
124
125 \f
126
127 #define SCM_EOF_OBJECT_P(x) ((x) == SCM_EOF_VAL)
128
129 /* PORT FLAGS
130 * A set of flags characterizes a port.
131 * Note that we reserve the bits 1 << 24 and above for use by the
132 * routines in the port's scm_ptobfuns structure.
133 */
134 #define SCM_OPN (1L<<16) /* Is the port open? */
135 #define SCM_RDNG (2L<<16) /* Is it a readable port? */
136 #define SCM_WRTNG (4L<<16) /* Is it writable? */
137 #define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
138 #define SCM_CRDY (32L<<16) /* Are there pushed back characters? */
139 #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
140
141 /* A mask used to clear the char-ready port flag. */
142 #define SCM_CUC (~SCM_CRDY)
143
144 #define SCM_PORTP(x) (SCM_TYP7(x)==scm_tc7_port)
145 #define SCM_OPPORTP(x) (((0x7f | SCM_OPN) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN))
146 #define SCM_OPINPORTP(x) (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG))
147 #define SCM_OPOUTPORTP(x) (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG))
148 #define SCM_INPORTP(x) (((0x7f | SCM_RDNG) & SCM_CAR(x))==(scm_tc7_port | SCM_RDNG))
149 #define SCM_OUTPORTP(x) (((0x7f | SCM_WRTNG) & SCM_CAR(x))==(scm_tc7_port | SCM_WRTNG))
150 #define SCM_OPENP(x) (SCM_OPN & SCM_CAR(x))
151 #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
152 #define SCM_PTAB_ENTRY(x) ((scm_port *) SCM_CDR(x))
153 #define SCM_SETPTAB_ENTRY(x,ent) SCM_SETCDR ((x), (SCM)(ent))
154 #define SCM_STREAM(x) SCM_PTAB_ENTRY(x)->stream
155 #define SCM_SETSTREAM(x,s) (SCM_PTAB_ENTRY(x)->stream = (SCM) s)
156 #define SCM_FILENAME(x) SCM_PTAB_ENTRY(x)->file_name
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 #define SCM_CRDYP(port) (SCM_CAR (port) & SCM_CRDY)
162 #define SCM_SETRDY(port) {SCM_SETOR_CAR (port, SCM_CRDY);}
163 #define SCM_CUNGET(c, port) \
164 { \
165 if (SCM_CRDYP (port)) \
166 { \
167 if (++SCM_PTAB_ENTRY (port)->cp == SCM_PTAB_ENTRY (port)->cbufend) \
168 scm_grow_port_cbuf (port, 1); \
169 *SCM_PTAB_ENTRY (port)->cp = c; \
170 } \
171 else \
172 { \
173 SCM_PTAB_ENTRY (port)->cbuf[0] = c; \
174 SCM_SETRDY (port); \
175 } \
176 } \
177
178 #define SCM_CGETUN(port) (*SCM_PTAB_ENTRY (port)->cp)
179 #define SCM_CLRDY(port) \
180 { \
181 SCM_PTAB_ENTRY (port)->cp = SCM_PTAB_ENTRY (port)->cbuf; \
182 SCM_SETAND_CAR (port, SCM_CUC); \
183 } \
184
185 #define SCM_TRY_CLRDY(port) \
186 { \
187 if (SCM_PTAB_ENTRY (port)->cp == SCM_PTAB_ENTRY (port)->cbuf) \
188 SCM_SETAND_CAR (port, SCM_CUC); \
189 else \
190 --SCM_PTAB_ENTRY (port)->cp; \
191 } \
192
193 /* Returns number of unread characters in a port.
194 Returns wrong answer if SCM_CRDYP is false. */
195 #define SCM_N_READY_CHARS(port) \
196 (SCM_PTAB_ENTRY (port)->cp - SCM_PTAB_ENTRY (port)->cbuf + 1)
197
198 #define SCM_INCLINE(port) {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
199 #define SCM_INCCOL(port) {SCM_COL (port) += 1;}
200 #define SCM_TABCOL(port) {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
201
202
203
204 \f
205
206 typedef struct scm_ptobfuns
207 {
208 SCM (*mark) (SCM);
209 int (*free) (SCM);
210 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
211 SCM (*equalp) (SCM, SCM);
212 void (*fflush) (SCM port);
213 void (*read_flush) (SCM port);
214 int (*fclose) (SCM port);
215 int (*fill_buffer) (SCM port);
216 off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
217 void (*ftruncate) (SCM port, off_t length);
218 int (*input_waiting_p) (SCM port);
219 } scm_ptobfuns;
220
221 #define SCM_PTOBNUM(x) (0x0ff & (SCM_CAR(x)>>8))
222
223 \f
224
225 extern scm_ptobfuns *scm_ptobs;
226 extern int scm_numptob;
227 extern int scm_port_table_room;
228
229 \f
230
231 extern SCM scm_markstream SCM_P ((SCM ptr));
232 extern long scm_newptob SCM_P ((scm_ptobfuns *ptob));
233 extern SCM scm_char_ready_p SCM_P ((SCM port));
234 extern SCM scm_drain_input (SCM port);
235 extern SCM scm_current_input_port SCM_P ((void));
236 extern SCM scm_current_output_port SCM_P ((void));
237 extern SCM scm_current_error_port SCM_P ((void));
238 extern SCM scm_current_load_port SCM_P ((void));
239 extern SCM scm_set_current_input_port SCM_P ((SCM port));
240 extern SCM scm_set_current_output_port SCM_P ((SCM port));
241 extern SCM scm_set_current_error_port SCM_P ((SCM port));
242 extern scm_port * scm_add_to_port_table SCM_P ((SCM port));
243 extern void scm_remove_from_port_table SCM_P ((SCM port));
244 extern void scm_grow_port_cbuf SCM_P ((SCM port, size_t requested));
245 extern SCM scm_pt_size SCM_P ((void));
246 extern SCM scm_pt_member SCM_P ((SCM member));
247 extern int scm_revealed_count SCM_P ((SCM port));
248 extern SCM scm_port_revealed SCM_P ((SCM port));
249 extern SCM scm_set_port_revealed_x SCM_P ((SCM port, SCM rcount));
250 extern long scm_mode_bits SCM_P ((char *modes));
251 extern SCM scm_port_mode SCM_P ((SCM port));
252 extern SCM scm_close_port SCM_P ((SCM port));
253 extern SCM scm_close_all_ports_except SCM_P ((SCM ports));
254 extern SCM scm_input_port_p SCM_P ((SCM x));
255 extern SCM scm_output_port_p SCM_P ((SCM x));
256 extern SCM scm_eof_object_p SCM_P ((SCM x));
257 extern SCM scm_force_output SCM_P ((SCM port));
258 extern SCM scm_flush_all_ports SCM_P ((void));
259 extern SCM scm_read_char SCM_P ((SCM port));
260 extern void scm_putc SCM_P ((int c, SCM port));
261 extern void scm_puts SCM_P ((char *str_data, SCM port));
262 extern void scm_lfwrite SCM_P ((char *ptr, scm_sizet size, SCM port));
263 extern void scm_fflush SCM_P ((SCM port));
264 extern int scm_getc SCM_P ((SCM port));
265 extern void scm_ungetc SCM_P ((int c, SCM port));
266 extern void scm_ungets SCM_P ((char *s, int n, SCM port));
267 extern SCM scm_peek_char SCM_P ((SCM port));
268 extern SCM scm_unread_char SCM_P ((SCM cobj, SCM port));
269 extern SCM scm_unread_string SCM_P ((SCM str, SCM port));
270 extern char *scm_generic_fgets SCM_P ((SCM port, int *len));
271 extern SCM scm_lseek (SCM object, SCM offset, SCM whence);
272 extern SCM scm_ftruncate (SCM port, SCM length);
273 extern SCM scm_port_line SCM_P ((SCM port));
274 extern SCM scm_set_port_line_x SCM_P ((SCM port, SCM line));
275 extern SCM scm_port_column SCM_P ((SCM port));
276 extern SCM scm_set_port_column_x SCM_P ((SCM port, SCM line));
277 extern SCM scm_port_filename SCM_P ((SCM port));
278 extern SCM scm_set_port_filename_x SCM_P ((SCM port, SCM filename));
279 extern void scm_prinport SCM_P ((SCM exp, SCM port, char *type));
280 extern void scm_ports_prehistory SCM_P ((void));
281 extern SCM scm_void_port SCM_P ((char * mode_str));
282 extern SCM scm_sys_make_void_port SCM_P ((SCM mode));
283 extern void scm_init_ports SCM_P ((void));
284
285 #ifdef GUILE_DEBUG
286 extern SCM scm_pt_size SCM_P ((void));
287 extern SCM scm_pt_member SCM_P ((SCM member));
288 #endif /* GUILE_DEBUG */
289
290 #endif /* PORTSH */