Use string accessors in scm_basename and scm_dirname
[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
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 /* port buffers. the buffer(s) are set up for all ports.
60 in the case of string ports, the buffer is the string itself.
61 in the case of unbuffered file ports, the buffer is a
62 single char: shortbuf. */
63
64 /* this buffer is filled from read_buf to read_end using the ptob
65 buffer_fill. then input requests are taken from read_pos until
66 it reaches read_end. */
67
68 unsigned char *read_buf; /* buffer start. */
69 const unsigned char *read_pos;/* the next unread char. */
70 unsigned char *read_end; /* pointer to last buffered char + 1. */
71 scm_t_off read_buf_size; /* size of the buffer. */
72
73 /* when chars are put back into the buffer, e.g., using peek-char or
74 unread-string, the read-buffer pointers are switched to cbuf.
75 the original pointers are saved here and restored when the put-back
76 chars have been consumed. */
77 unsigned char *saved_read_buf;
78 const unsigned char *saved_read_pos;
79 unsigned char *saved_read_end;
80 scm_t_off saved_read_buf_size;
81
82 /* write requests are saved into this buffer at write_pos until it
83 reaches write_buf + write_buf_size, then the ptob flush is
84 called. */
85
86 unsigned char *write_buf; /* buffer start. */
87 unsigned char *write_pos; /* pointer to last buffered char + 1. */
88 unsigned char *write_end; /* pointer to end of buffer + 1. */
89 scm_t_off write_buf_size; /* size of the buffer. */
90
91 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
92
93 int rw_random; /* true if the port is random access.
94 implies that the buffers must be
95 flushed before switching between
96 reading and writing, seeking, etc. */
97
98 scm_t_port_rw_active rw_active; /* for random access ports,
99 indicates which of the buffers
100 is currently in use. can be
101 SCM_PORT_WRITE, SCM_PORT_READ,
102 or SCM_PORT_NEITHER. */
103
104
105 /* a buffer for un-read chars and strings. */
106 unsigned char *putback_buf;
107 size_t putback_buf_size; /* allocated size of putback_buf. */
108 } scm_t_port;
109
110
111 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_port_table_mutex;
112 SCM_INTERNAL SCM scm_i_port_weak_hash;
113
114
115 #define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
116
117 \f
118
119 #define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
120
121 /* PORT FLAGS
122 * A set of flags characterizes a port.
123 * Note that we reserve the bits 1 << 24 and above for use by the
124 * routines in the port's scm_ptobfuns structure.
125 */
126 #define SCM_OPN (1L<<16) /* Is the port open? */
127 #define SCM_RDNG (2L<<16) /* Is it a readable port? */
128 #define SCM_WRTNG (4L<<16) /* Is it writable? */
129 #define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
130 #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
131
132 #define SCM_PORTP(x) (!SCM_IMP (x) && (SCM_TYP7 (x) == scm_tc7_port))
133 #define SCM_OPPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
134 #define SCM_OPINPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
135 #define SCM_OPOUTPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
136 #define SCM_INPUT_PORT_P(x) \
137 (!SCM_IMP(x) \
138 && (((0x7f | SCM_RDNG) & SCM_CELL_WORD_0(x)) == (scm_tc7_port | SCM_RDNG)))
139 #define SCM_OUTPUT_PORT_P(x) \
140 (!SCM_IMP(x) \
141 && (((0x7f | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_WRTNG)))
142 #define SCM_OPENP(x) (!SCM_IMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
143 #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
144 #define SCM_CLR_PORT_OPEN_FLAG(p) \
145 SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
146
147 #define SCM_PTAB_ENTRY(x) ((scm_t_port *) SCM_CELL_WORD_1 (x))
148 #define SCM_SETPTAB_ENTRY(x, ent) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
149 #define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
150 #define SCM_SETSTREAM(x, s) (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
151 #define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
152 #define SCM_SET_FILENAME(x, n) (SCM_PTAB_ENTRY(x)->file_name = (n))
153 #define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
154 #define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
155 #define SCM_REVEALED(x) (SCM_PTAB_ENTRY(x)->revealed)
156 #define SCM_SETREVEALED(x, s) (SCM_PTAB_ENTRY(x)->revealed = (s))
157
158 #define SCM_INCLINE(port) do {SCM_LINUM (port) += 1; SCM_COL (port) = 0;} while (0)
159 #define SCM_ZEROCOL(port) do {SCM_COL (port) = 0;} while (0)
160 #define SCM_INCCOL(port) do {SCM_COL (port) += 1;} while (0)
161 #define SCM_DECCOL(port) do {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;} while (0)
162 #define SCM_TABCOL(port) do {SCM_COL (port) += 8 - SCM_COL (port) % 8;} while (0)
163
164 /* Maximum number of port types. */
165 #define SCM_I_MAX_PORT_TYPE_COUNT 256
166
167 \f
168
169 /* port-type description. */
170 typedef struct scm_t_ptob_descriptor
171 {
172 char *name;
173 SCM (*mark) (SCM);
174 size_t (*free) (SCM);
175 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
176 SCM (*equalp) (SCM, SCM);
177 int (*close) (SCM port);
178
179 void (*write) (SCM port, const void *data, size_t size);
180 void (*flush) (SCM port);
181
182 void (*end_input) (SCM port, int offset);
183 int (*fill_input) (SCM port);
184 int (*input_waiting) (SCM port);
185
186 scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
187 void (*truncate) (SCM port, scm_t_off length);
188
189 } scm_t_ptob_descriptor;
190
191 #define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
192 #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
193 /* SCM_PTOBNAME can be 0 if name is missing */
194 #define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
195
196 \f
197
198 SCM_API scm_t_ptob_descriptor *scm_ptobs;
199 SCM_API long scm_numptob;
200 SCM_INTERNAL long scm_i_port_table_room;
201
202 \f
203
204 SCM_API SCM scm_markstream (SCM ptr);
205 SCM_API scm_t_bits scm_make_port_type (char *name,
206 int (*fill_input) (SCM port),
207 void (*write) (SCM port,
208 const void *data,
209 size_t size));
210 SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
211 SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
212 SCM_API void scm_set_port_print (scm_t_bits tc,
213 int (*print) (SCM exp,
214 SCM port,
215 scm_print_state *pstate));
216 SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
217 SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
218
219 SCM_API void scm_set_port_flush (scm_t_bits tc,
220 void (*flush) (SCM port));
221 SCM_API void scm_set_port_end_input (scm_t_bits tc,
222 void (*end_input) (SCM port,
223 int offset));
224 SCM_API void scm_set_port_seek (scm_t_bits tc,
225 scm_t_off (*seek) (SCM port,
226 scm_t_off OFFSET,
227 int WHENCE));
228 SCM_API void scm_set_port_truncate (scm_t_bits tc,
229 void (*truncate) (SCM port,
230 scm_t_off length));
231 SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
232 SCM_API SCM scm_char_ready_p (SCM port);
233 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
234 SCM_API SCM scm_drain_input (SCM port);
235 SCM_API SCM scm_current_input_port (void);
236 SCM_API SCM scm_current_output_port (void);
237 SCM_API SCM scm_current_error_port (void);
238 SCM_API SCM scm_current_load_port (void);
239 SCM_API SCM scm_set_current_input_port (SCM port);
240 SCM_API SCM scm_set_current_output_port (SCM port);
241 SCM_API SCM scm_set_current_error_port (SCM port);
242 SCM_API void scm_dynwind_current_input_port (SCM port);
243 SCM_API void scm_dynwind_current_output_port (SCM port);
244 SCM_API void scm_dynwind_current_error_port (SCM port);
245 SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
246 SCM_INTERNAL void scm_i_remove_port (SCM port);
247 SCM_API void scm_grow_port_cbuf (SCM port, size_t requested);
248 SCM_API SCM scm_pt_size (void);
249 SCM_API SCM scm_pt_member (SCM member);
250 SCM_API void scm_port_non_buffer (scm_t_port *pt);
251 SCM_API int scm_revealed_count (SCM port);
252 SCM_API SCM scm_port_revealed (SCM port);
253 SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
254 SCM_API long scm_mode_bits (char *modes);
255 SCM_API SCM scm_port_mode (SCM port);
256 SCM_API SCM scm_close_input_port (SCM port);
257 SCM_API SCM scm_close_output_port (SCM port);
258 SCM_API SCM scm_close_port (SCM port);
259 SCM_API SCM scm_port_for_each (SCM proc);
260 SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
261 SCM_API SCM scm_input_port_p (SCM x);
262 SCM_API SCM scm_output_port_p (SCM x);
263 SCM_API SCM scm_port_p (SCM x);
264 SCM_API SCM scm_port_closed_p (SCM port);
265 SCM_API SCM scm_eof_object_p (SCM x);
266 SCM_API SCM scm_force_output (SCM port);
267 SCM_API SCM scm_flush_all_ports (void);
268 SCM_API SCM scm_read_char (SCM port);
269 SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
270 SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
271 SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
272 SCM_INTERNAL void scm_lfwrite_str (SCM str, SCM port);
273 SCM_INTERNAL void scm_lfwrite_substr (SCM str, size_t start, size_t end,
274 SCM port);
275 SCM_API void scm_flush (SCM port);
276 SCM_API void scm_end_input (SCM port);
277 SCM_API int scm_fill_input (SCM port);
278 SCM_API void scm_ungetc (int c, SCM port);
279 SCM_API void scm_ungets (const char *s, int n, SCM port);
280 SCM_API SCM scm_peek_char (SCM port);
281 SCM_API SCM scm_unread_char (SCM cobj, SCM port);
282 SCM_API SCM scm_unread_string (SCM str, SCM port);
283 SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
284 SCM_API SCM scm_truncate_file (SCM object, SCM length);
285 SCM_API SCM scm_port_line (SCM port);
286 SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
287 SCM_API SCM scm_port_column (SCM port);
288 SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
289 SCM_API SCM scm_port_filename (SCM port);
290 SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
291 SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
292 SCM_API void scm_print_port_mode (SCM exp, SCM port);
293 SCM_API void scm_ports_prehistory (void);
294 SCM_API SCM scm_void_port (char * mode_str);
295 SCM_API SCM scm_sys_make_void_port (SCM mode);
296 SCM_INTERNAL void scm_init_ports (void);
297
298
299 #if SCM_ENABLE_DEPRECATED==1
300 SCM_API scm_t_port * scm_add_to_port_table (SCM port);
301 #endif
302
303 #ifdef GUILE_DEBUG
304 SCM_API SCM scm_pt_size (void);
305 SCM_API SCM scm_pt_member (SCM member);
306 #endif /* GUILE_DEBUG */
307
308 /* internal */
309
310 SCM_INTERNAL long scm_i_mode_bits (SCM modes);
311 SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
312
313
314 #endif /* SCM_PORTS_H */
315
316 /*
317 Local Variables:
318 c-file-style: "gnu"
319 End:
320 */