Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / libguile / ports.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
22a52da1
DH
3#ifndef SCM_PORTS_H
4#define SCM_PORTS_H
8c494e99 5
22a52da1 6/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
8c494e99 7 *
73be1d9e
MV
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
8c494e99 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
0f2d19dd 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
8c494e99 17 *
73be1d9e
MV
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
d3a6bc94 22
0f2d19dd 23\f
8c494e99 24
b4309c3c 25#include "libguile/__scm.h"
0f2d19dd 26
e0d86ad2 27#include "libguile/print.h"
78446828 28#include "libguile/struct.h"
e0d86ad2 29
acdb12da
JB
30/* Not sure if this is a good idea. We need it for off_t. */
31#include <sys/types.h>
32
0f2d19dd
JB
33\f
34
6c951427 35#define SCM_INITIAL_PUTBACK_BUF_SIZE 4
0855ef71 36
61e452ba 37/* values for the rw_active flag. */
92c2555f 38typedef enum scm_t_port_rw_active {
61e452ba
JB
39 SCM_PORT_NEITHER = 0,
40 SCM_PORT_READ = 1,
41 SCM_PORT_WRITE = 2
92c2555f 42} scm_t_port_rw_active;
61e452ba 43
840ae05d
JB
44/* C representation of a Scheme port. */
45
46typedef struct
0f2d19dd 47{
ee149d03 48 SCM port; /* Link back to the port object. */
c014a02e 49 long entry; /* Index in port table. */
0f2d19dd
JB
50 int revealed; /* 0 not revealed, > 1 revealed.
51 * Revealed ports do not get GC'd.
52 */
74a16888 53 /* data for the underlying port implementation as a raw C value. */
92c2555f 54 scm_t_bits stream;
0f2d19dd 55
0855ef71 56 SCM file_name; /* debugging support. */
1be6b49c 57 long line_number; /* debugging support. */
ebf7394e 58 int column_number; /* debugging support. */
0855ef71 59
ee149d03
JB
60 /* port buffers. the buffer(s) are set up for all ports.
61 in the case of string ports, the buffer is the string itself.
62 in the case of unbuffered file ports, the buffer is a
63 single char: shortbuf. */
64
65 /* this buffer is filled from read_buf to read_end using the ptob
66 buffer_fill. then input requests are taken from read_pos until
67 it reaches read_end. */
68
69 unsigned char *read_buf; /* buffer start. */
840ae05d 70 const unsigned char *read_pos;/* the next unread char. */
ee149d03 71 unsigned char *read_end; /* pointer to last buffered char + 1. */
840ae05d 72 off_t read_buf_size; /* size of the buffer. */
ee149d03 73
6c951427
GH
74 /* when chars are put back into the buffer, e.g., using peek-char or
75 unread-string, the read-buffer pointers are switched to cbuf.
76 the original pointers are saved here and restored when the put-back
77 chars have been consumed. */
78 unsigned char *saved_read_buf;
79 const unsigned char *saved_read_pos;
80 unsigned char *saved_read_end;
81 off_t saved_read_buf_size;
82
ee149d03
JB
83 /* write requests are saved into this buffer at write_pos until it
84 reaches write_buf + write_buf_size, then the ptob flush is
85 called. */
86
6c951427 87 unsigned char *write_buf; /* buffer start. */
ee149d03
JB
88 unsigned char *write_pos; /* pointer to last buffered char + 1. */
89 unsigned char *write_end; /* pointer to end of buffer + 1. */
840ae05d 90 off_t write_buf_size; /* size of the buffer. */
ee149d03
JB
91
92 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
93
0de97b83
GH
94 int rw_random; /* true if the port is random access.
95 implies that the buffers must be
96 flushed before switching between
97 reading and writing, seeking, etc. */
98
92c2555f 99 scm_t_port_rw_active rw_active; /* for random access ports,
1be6b49c
ML
100 indicates which of the buffers
101 is currently in use. can be
102 SCM_PORT_WRITE, SCM_PORT_READ,
103 or SCM_PORT_NEITHER. */
ee149d03 104
61e452ba 105
6c951427
GH
106 /* a buffer for un-read chars and strings. */
107 unsigned char *putback_buf;
1be6b49c 108 size_t putback_buf_size; /* allocated size of putback_buf. */
92c2555f 109} scm_t_port;
840ae05d 110
67329a9e
HWN
111SCM_API scm_t_port **scm_i_port_table;
112SCM_API long scm_i_port_table_size; /* Number of ports in scm_i_port_table. */
0f2d19dd 113
6fe692e9 114#define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
0f2d19dd
JB
115
116\f
117
54778cd3 118#define SCM_EOF_OBJECT_P(x) (SCM_EQ_P ((x), SCM_EOF_VAL))
0c32d76c 119
0f2d19dd 120/* PORT FLAGS
dbece3a2 121 * A set of flags characterizes a port.
571031dc
JB
122 * Note that we reserve the bits 1 << 24 and above for use by the
123 * routines in the port's scm_ptobfuns structure.
0f2d19dd
JB
124 */
125#define SCM_OPN (1L<<16) /* Is the port open? */
126#define SCM_RDNG (2L<<16) /* Is it a readable port? */
127#define SCM_WRTNG (4L<<16) /* Is it writable? */
ee149d03 128#define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
ee149d03 129#define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
0f2d19dd 130
8c494e99
DH
131#define SCM_PORTP(x) (!SCM_IMP (x) && (SCM_TYP7 (x) == scm_tc7_port))
132#define SCM_OPPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
133#define SCM_OPINPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
134#define SCM_OPOUTPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
df97587f 135#define SCM_INPUT_PORT_P(x) \
8c494e99 136 (!SCM_IMP(x) \
f9a64404 137 && (((0x7f | SCM_RDNG) & SCM_CELL_WORD_0(x)) == (scm_tc7_port | SCM_RDNG)))
df97587f 138#define SCM_OUTPUT_PORT_P(x) \
8c494e99 139 (!SCM_IMP(x) \
f9a64404 140 && (((0x7f | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_WRTNG)))
8c494e99 141#define SCM_OPENP(x) (!SCM_IMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
0f2d19dd 142#define SCM_CLOSEDP(x) (!SCM_OPENP(x))
22a52da1
DH
143#define SCM_CLR_PORT_OPEN_FLAG(p) \
144 SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
843524cc 145
92c2555f 146#define SCM_PTAB_ENTRY(x) ((scm_t_port *) SCM_CELL_WORD_1 (x))
34d19ef6 147#define SCM_SETPTAB_ENTRY(x, ent) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
843524cc 148#define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
34d19ef6 149#define SCM_SETSTREAM(x, s) (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
843524cc 150#define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
b24b5e13 151#define SCM_SET_FILENAME(x, n) (SCM_PTAB_ENTRY(x)->file_name = (n))
843524cc
DH
152#define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
153#define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
154#define SCM_REVEALED(x) (SCM_PTAB_ENTRY(x)->revealed)
34d19ef6 155#define SCM_SETREVEALED(x, s) (SCM_PTAB_ENTRY(x)->revealed = (s))
0f2d19dd
JB
156
157#define SCM_INCLINE(port) {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
158#define SCM_INCCOL(port) {SCM_COL (port) += 1;}
d14af9f2 159#define SCM_TABCOL(port) {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
0f2d19dd 160
0f2d19dd
JB
161\f
162
affc96b5 163/* port-type description. */
92c2555f 164typedef struct scm_t_ptob_descriptor
0f82baf6 165{
f12733c9 166 char *name;
0c0669cc 167 SCM (*mark) (SCM);
1be6b49c 168 size_t (*free) (SCM);
0c0669cc
JB
169 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
170 SCM (*equalp) (SCM, SCM);
affc96b5
GH
171 int (*close) (SCM port);
172
8aa011a1 173 void (*write) (SCM port, const void *data, size_t size);
affc96b5
GH
174 void (*flush) (SCM port);
175
176 void (*end_input) (SCM port, int offset);
177 int (*fill_input) (SCM port);
178 int (*input_waiting) (SCM port);
179
ee149d03 180 off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
affc96b5
GH
181 void (*truncate) (SCM port, off_t length);
182
92c2555f 183} scm_t_ptob_descriptor;
1be6b49c 184
12a8b769
DH
185#define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
186#define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
f12733c9
MD
187/* SCM_PTOBNAME can be 0 if name is missing */
188#define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
0f82baf6
JB
189
190\f
191
33b001fd
MV
192SCM_API scm_t_ptob_descriptor *scm_ptobs;
193SCM_API long scm_numptob;
67329a9e 194SCM_API long scm_i_port_table_room;
0f2d19dd
JB
195
196\f
0f2d19dd 197
33b001fd
MV
198SCM_API SCM scm_markstream (SCM ptr);
199SCM_API scm_t_bits scm_make_port_type (char *name,
200 int (*fill_input) (SCM port),
201 void (*write) (SCM port,
202 const void *data,
203 size_t size));
204SCM_API void scm_set_port_mark (long tc, SCM (*mark) (SCM));
205SCM_API void scm_set_port_free (long tc, size_t (*free) (SCM));
206SCM_API void scm_set_port_print (long tc,
207 int (*print) (SCM exp,
208 SCM port,
209 scm_print_state *pstate));
210SCM_API void scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM));
211SCM_API void scm_set_port_close (long tc, int (*close) (SCM));
212
213SCM_API void scm_set_port_flush (long tc,
214 void (*flush) (SCM port));
215SCM_API void scm_set_port_end_input (long tc,
216 void (*end_input) (SCM port,
217 int offset));
218SCM_API void scm_set_port_seek (long tc,
219 off_t (*seek) (SCM port,
220 off_t OFFSET,
221 int WHENCE));
222SCM_API void scm_set_port_truncate (long tc,
223 void (*truncate) (SCM port,
224 off_t length));
225SCM_API void scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM));
226SCM_API SCM scm_char_ready_p (SCM port);
c2da2648 227size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
33b001fd
MV
228SCM_API SCM scm_drain_input (SCM port);
229SCM_API SCM scm_current_input_port (void);
230SCM_API SCM scm_current_output_port (void);
231SCM_API SCM scm_current_error_port (void);
232SCM_API SCM scm_current_load_port (void);
233SCM_API SCM scm_set_current_input_port (SCM port);
234SCM_API SCM scm_set_current_output_port (SCM port);
235SCM_API SCM scm_set_current_error_port (SCM port);
da220f27 236SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
33b001fd
MV
237SCM_API void scm_remove_from_port_table (SCM port);
238SCM_API void scm_grow_port_cbuf (SCM port, size_t requested);
239SCM_API SCM scm_pt_size (void);
240SCM_API SCM scm_pt_member (SCM member);
241SCM_API void scm_port_non_buffer (scm_t_port *pt);
242SCM_API int scm_revealed_count (SCM port);
243SCM_API SCM scm_port_revealed (SCM port);
244SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
245SCM_API long scm_mode_bits (char *modes);
246SCM_API SCM scm_port_mode (SCM port);
247SCM_API SCM scm_close_input_port (SCM port);
248SCM_API SCM scm_close_output_port (SCM port);
249SCM_API SCM scm_close_port (SCM port);
250SCM_API SCM scm_port_for_each (SCM proc);
251SCM_API SCM scm_input_port_p (SCM x);
252SCM_API SCM scm_output_port_p (SCM x);
253SCM_API SCM scm_port_p (SCM x);
254SCM_API SCM scm_port_closed_p (SCM port);
255SCM_API SCM scm_eof_object_p (SCM x);
256SCM_API SCM scm_force_output (SCM port);
257SCM_API SCM scm_flush_all_ports (void);
258SCM_API SCM scm_read_char (SCM port);
259SCM_API void scm_putc (char c, SCM port);
260SCM_API void scm_puts (const char *str_data, SCM port);
261SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
262SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
263SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
264SCM_API void scm_flush (SCM port);
265SCM_API void scm_end_input (SCM port);
266SCM_API int scm_fill_input (SCM port);
267SCM_API int scm_getc (SCM port);
268SCM_API void scm_ungetc (int c, SCM port);
269SCM_API void scm_ungets (const char *s, int n, SCM port);
270SCM_API SCM scm_peek_char (SCM port);
271SCM_API SCM scm_unread_char (SCM cobj, SCM port);
272SCM_API SCM scm_unread_string (SCM str, SCM port);
273SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
274SCM_API SCM scm_truncate_file (SCM object, SCM length);
275SCM_API SCM scm_port_line (SCM port);
276SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
277SCM_API SCM scm_port_column (SCM port);
278SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
279SCM_API SCM scm_port_filename (SCM port);
280SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
281SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
282SCM_API void scm_print_port_mode (SCM exp, SCM port);
283SCM_API void scm_ports_prehistory (void);
284SCM_API SCM scm_void_port (char * mode_str);
285SCM_API SCM scm_sys_make_void_port (SCM mode);
286SCM_API void scm_init_ports (void);
0f2d19dd 287
67329a9e
HWN
288
289#if SCM_ENABLE_DEPRECATED==1
290SCM_API scm_t_port * scm_add_to_port_table (SCM port);
291#endif
292
f3667f52 293#ifdef GUILE_DEBUG
33b001fd
MV
294SCM_API SCM scm_pt_size (void);
295SCM_API SCM scm_pt_member (SCM member);
be1cd096 296#endif /* GUILE_DEBUG */
f3667f52 297
22a52da1 298#endif /* SCM_PORTS_H */
89e00824
ML
299
300/*
301 Local Variables:
302 c-file-style: "gnu"
303 End:
304*/