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