*.[ch]: make a distinction between SCM as a generic
[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
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 \f
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 enum scm_port_rw_active {
63 SCM_PORT_NEITHER = 0,
64 SCM_PORT_READ = 1,
65 SCM_PORT_WRITE = 2
66 };
67
68 /* C representation of a Scheme port. */
69
70 typedef struct
71 {
72 SCM port; /* Link back to the port object. */
73 int 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. may be an SCM cell or
78 cast to a pointer to C data. */
79 SCM stream;
80
81 SCM file_name; /* debugging support. */
82 int line_number; /* debugging support. */
83 int column_number; /* debugging support. */
84
85 /* port buffers. the buffer(s) are set up for all ports.
86 in the case of string ports, the buffer is the string itself.
87 in the case of unbuffered file ports, the buffer is a
88 single char: shortbuf. */
89
90 /* this buffer is filled from read_buf to read_end using the ptob
91 buffer_fill. then input requests are taken from read_pos until
92 it reaches read_end. */
93
94 unsigned char *read_buf; /* buffer start. */
95 const unsigned char *read_pos;/* the next unread char. */
96 unsigned char *read_end; /* pointer to last buffered char + 1. */
97 off_t read_buf_size; /* size of the buffer. */
98
99 /* when chars are put back into the buffer, e.g., using peek-char or
100 unread-string, the read-buffer pointers are switched to cbuf.
101 the original pointers are saved here and restored when the put-back
102 chars have been consumed. */
103 unsigned char *saved_read_buf;
104 const unsigned char *saved_read_pos;
105 unsigned char *saved_read_end;
106 off_t saved_read_buf_size;
107
108 /* write requests are saved into this buffer at write_pos until it
109 reaches write_buf + write_buf_size, then the ptob flush is
110 called. */
111
112 unsigned char *write_buf; /* buffer start. */
113 unsigned char *write_pos; /* pointer to last buffered char + 1. */
114 unsigned char *write_end; /* pointer to end of buffer + 1. */
115 off_t write_buf_size; /* size of the buffer. */
116
117 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
118
119 int rw_random; /* true if the port is random access.
120 implies that the buffers must be
121 flushed before switching between
122 reading and writing, seeking, etc. */
123
124 enum scm_port_rw_active rw_active; /* for random access ports,
125 indicates which of the buffers
126 is currently in use. can be
127 SCM_PORT_WRITE, SCM_PORT_READ,
128 or SCM_PORT_NEITHER. */
129
130
131 /* a buffer for un-read chars and strings. */
132 unsigned char *putback_buf;
133 int putback_buf_size; /* allocated size of putback_buf. */
134 } scm_port;
135
136 extern scm_port **scm_port_table;
137 extern int scm_port_table_size; /* Number of ports in scm_port_table. */
138
139
140 \f
141
142 #define SCM_EOF_OBJECT_P(x) ((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_CRDY (32L<<16) obsolete, for pushed back characters */
154 #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
155
156 #define SCM_PORTP(x) (SCM_NIMP(x) && (SCM_TYP7(x)==scm_tc7_port))
157 #define SCM_OPPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN) & SCM_CARW(x))==(scm_tc7_port | SCM_OPN)))
158 #define SCM_OPINPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CARW(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
159 #define SCM_OPOUTPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CARW(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
160 #define SCM_INPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_RDNG) & SCM_CARW(x))==(scm_tc7_port | SCM_RDNG)))
161 #define SCM_OUTPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_WRTNG) & SCM_CARW(x))==(scm_tc7_port | SCM_WRTNG)))
162 #define SCM_OPENP(x) (SCM_NIMP(x) && (SCM_OPN & SCM_CARW (x)))
163 #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
164 #define SCM_PTAB_ENTRY(x) ((scm_port *) SCM_CDR(x))
165 #define SCM_SETPTAB_ENTRY(x,ent) SCM_SETCDR ((x), (SCM)(ent))
166 #define SCM_STREAM(x) SCM_PTAB_ENTRY(x)->stream
167 #define SCM_SETSTREAM(x,s) (SCM_PTAB_ENTRY(x)->stream = (SCM) s)
168 #define SCM_FILENAME(x) SCM_PTAB_ENTRY(x)->file_name
169 #define SCM_LINUM(x) SCM_PTAB_ENTRY(x)->line_number
170 #define SCM_COL(x) SCM_PTAB_ENTRY(x)->column_number
171 #define SCM_REVEALED(x) SCM_PTAB_ENTRY(x)->revealed
172 #define SCM_SETREVEALED(x,s) (SCM_PTAB_ENTRY(x)->revealed = s)
173
174 #define SCM_INCLINE(port) {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
175 #define SCM_INCCOL(port) {SCM_COL (port) += 1;}
176 #define SCM_TABCOL(port) {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
177
178 \f
179
180 /* port-type description. */
181 typedef struct scm_ptob_descriptor
182 {
183 char *name;
184 SCM (*mark) (SCM);
185 scm_sizet (*free) (SCM);
186 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
187 SCM (*equalp) (SCM, SCM);
188 int (*close) (SCM port);
189
190 void (*write) (SCM port, const void *data, size_t size);
191 void (*flush) (SCM port);
192
193 void (*end_input) (SCM port, int offset);
194 int (*fill_input) (SCM port);
195 int (*input_waiting) (SCM port);
196
197 off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
198 void (*truncate) (SCM port, off_t length);
199
200 } scm_ptob_descriptor;
201
202 #define SCM_TC2PTOBNUM(x) (0x0ff & (SCM_ASWORD(x) >> 8))
203 #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CAR (x)))
204 /* SCM_PTOBNAME can be 0 if name is missing */
205 #define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
206
207 \f
208
209 extern scm_ptob_descriptor *scm_ptobs;
210 extern int scm_numptob;
211 extern int scm_port_table_room;
212
213 \f
214
215 extern SCM scm_markstream (SCM ptr);
216 extern long scm_make_port_type (char *name,
217 int (*fill_input) (SCM port),
218 void (*write) (SCM port, const void *data,
219 size_t size));
220 extern void scm_set_port_mark (long tc, SCM (*mark) (SCM));
221 extern void scm_set_port_free (long tc, scm_sizet (*free) (SCM));
222 extern void scm_set_port_print (long tc,
223 int (*print) (SCM exp,
224 SCM port,
225 scm_print_state *pstate));
226 extern void scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM));
227 extern void scm_set_port_close (long tc, int (*close) (SCM));
228
229 extern void scm_set_port_flush (long tc,
230 void (*flush) (SCM port));
231 extern void scm_set_port_end_input (long tc,
232 void (*end_input) (SCM port,
233 int offset));
234 extern void scm_set_port_seek (long tc,
235 off_t (*seek) (SCM port,
236 off_t OFFSET,
237 int WHENCE));
238 extern void scm_set_port_truncate (long tc,
239 void (*truncate) (SCM port,
240 off_t length));
241 extern void scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM));
242 extern SCM scm_char_ready_p (SCM port);
243 extern SCM scm_drain_input (SCM port);
244 extern SCM scm_current_input_port (void);
245 extern SCM scm_current_output_port (void);
246 extern SCM scm_current_error_port (void);
247 extern SCM scm_current_load_port (void);
248 extern SCM scm_set_current_input_port (SCM port);
249 extern SCM scm_set_current_output_port (SCM port);
250 extern SCM scm_set_current_error_port (SCM port);
251 extern scm_port * scm_add_to_port_table (SCM port);
252 extern void scm_remove_from_port_table (SCM port);
253 extern void scm_grow_port_cbuf (SCM port, size_t requested);
254 extern SCM scm_pt_size (void);
255 extern SCM scm_pt_member (SCM member);
256 extern int scm_revealed_count (SCM port);
257 extern SCM scm_port_revealed (SCM port);
258 extern SCM scm_set_port_revealed_x (SCM port, SCM rcount);
259 extern long scm_mode_bits (char *modes);
260 extern SCM scm_port_mode (SCM port);
261 extern SCM scm_close_port (SCM port);
262 extern SCM scm_close_all_ports_except (SCM ports);
263 extern SCM scm_input_port_p (SCM x);
264 extern SCM scm_output_port_p (SCM x);
265 extern SCM scm_port_closed_p (SCM port);
266 extern SCM scm_eof_object_p (SCM x);
267 extern SCM scm_force_output (SCM port);
268 extern SCM scm_flush_all_ports (void);
269 extern SCM scm_read_char (SCM port);
270 extern void scm_putc (char c, SCM port);
271 extern void scm_puts (const char *str_data, SCM port);
272 extern void scm_lfwrite (const char *ptr, scm_sizet size, SCM port);
273 extern void scm_flush (SCM port);
274 extern void scm_end_input (SCM port);
275 extern int scm_fill_input (SCM port);
276 extern int scm_getc (SCM port);
277 extern void scm_ungetc (int c, SCM port);
278 extern void scm_ungets (const char *s, int n, SCM port);
279 extern SCM scm_peek_char (SCM port);
280 extern SCM scm_unread_char (SCM cobj, SCM port);
281 extern SCM scm_unread_string (SCM str, SCM port);
282 extern char *scm_generic_fgets (SCM port, int *len);
283 extern SCM scm_seek (SCM object, SCM offset, SCM whence);
284 extern SCM scm_truncate_file (SCM object, SCM length);
285 extern SCM scm_port_line (SCM port);
286 extern SCM scm_set_port_line_x (SCM port, SCM line);
287 extern SCM scm_port_column (SCM port);
288 extern SCM scm_set_port_column_x (SCM port, SCM line);
289 extern SCM scm_port_filename (SCM port);
290 extern SCM scm_set_port_filename_x (SCM port, SCM filename);
291 extern int scm_port_print (SCM exp, SCM port, scm_print_state *);
292 extern void scm_print_port_mode (SCM exp, SCM port);
293 extern void scm_ports_prehistory (void);
294 extern SCM scm_void_port (char * mode_str);
295 extern SCM scm_sys_make_void_port (SCM mode);
296 extern void scm_init_ports (void);
297
298 #ifdef GUILE_DEBUG
299 extern SCM scm_pt_size (void);
300 extern SCM scm_pt_member (SCM member);
301 #endif /* GUILE_DEBUG */
302
303 #endif /* PORTSH */