ports.h: remove unimplemented declarations
[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
fca14149
AW
29#include <stdio.h>
30#include <string.h>
0c247c33 31#include <unistd.h>
fca14149
AW
32#include "libguile/gc.h"
33#include "libguile/tags.h"
34#include "libguile/error.h"
e0d86ad2 35#include "libguile/print.h"
78446828 36#include "libguile/struct.h"
b9ad392e 37#include "libguile/threads.h"
889975e5 38#include "libguile/strings.h"
acdb12da 39
0f2d19dd
JB
40\f
41
6c951427 42#define SCM_INITIAL_PUTBACK_BUF_SIZE 4
0855ef71 43
61e452ba 44/* values for the rw_active flag. */
92c2555f 45typedef enum scm_t_port_rw_active {
61e452ba
JB
46 SCM_PORT_NEITHER = 0,
47 SCM_PORT_READ = 1,
48 SCM_PORT_WRITE = 2
92c2555f 49} scm_t_port_rw_active;
61e452ba 50
840ae05d
JB
51/* C representation of a Scheme port. */
52
53typedef struct
0f2d19dd 54{
ee149d03 55 SCM port; /* Link back to the port object. */
30b126d2
AW
56#if SCM_USE_PTHREAD_THREADS
57 scm_i_pthread_mutex_t lock; /* A recursive lock for this port. */
58#endif
59
0f2d19dd
JB
60 int revealed; /* 0 not revealed, > 1 revealed.
61 * Revealed ports do not get GC'd.
62 */
74a16888 63 /* data for the underlying port implementation as a raw C value. */
92c2555f 64 scm_t_bits stream;
0f2d19dd 65
0855ef71 66 SCM file_name; /* debugging support. */
1be6b49c 67 long line_number; /* debugging support. */
ebf7394e 68 int column_number; /* debugging support. */
0855ef71 69
889975e5
MG
70 /* Character encoding support */
71 char *encoding;
72 scm_t_string_failed_conversion_handler ilseq_handler;
73
ee149d03
JB
74 /* port buffers. the buffer(s) are set up for all ports.
75 in the case of string ports, the buffer is the string itself.
76 in the case of unbuffered file ports, the buffer is a
77 single char: shortbuf. */
78
79 /* this buffer is filled from read_buf to read_end using the ptob
80 buffer_fill. then input requests are taken from read_pos until
81 it reaches read_end. */
82
83 unsigned char *read_buf; /* buffer start. */
840ae05d 84 const unsigned char *read_pos;/* the next unread char. */
ee149d03 85 unsigned char *read_end; /* pointer to last buffered char + 1. */
f1ce9199 86 scm_t_off read_buf_size; /* size of the buffer. */
ee149d03 87
6c951427
GH
88 /* when chars are put back into the buffer, e.g., using peek-char or
89 unread-string, the read-buffer pointers are switched to cbuf.
90 the original pointers are saved here and restored when the put-back
91 chars have been consumed. */
92 unsigned char *saved_read_buf;
93 const unsigned char *saved_read_pos;
94 unsigned char *saved_read_end;
f1ce9199 95 scm_t_off saved_read_buf_size;
6c951427 96
ee149d03
JB
97 /* write requests are saved into this buffer at write_pos until it
98 reaches write_buf + write_buf_size, then the ptob flush is
99 called. */
100
6c951427 101 unsigned char *write_buf; /* buffer start. */
ee149d03
JB
102 unsigned char *write_pos; /* pointer to last buffered char + 1. */
103 unsigned char *write_end; /* pointer to end of buffer + 1. */
f1ce9199 104 scm_t_off write_buf_size; /* size of the buffer. */
ee149d03
JB
105
106 unsigned char shortbuf; /* buffer for "unbuffered" streams. */
107
0de97b83
GH
108 int rw_random; /* true if the port is random access.
109 implies that the buffers must be
110 flushed before switching between
111 reading and writing, seeking, etc. */
112
92c2555f 113 scm_t_port_rw_active rw_active; /* for random access ports,
1be6b49c
ML
114 indicates which of the buffers
115 is currently in use. can be
116 SCM_PORT_WRITE, SCM_PORT_READ,
117 or SCM_PORT_NEITHER. */
ee149d03 118
61e452ba 119
6c951427
GH
120 /* a buffer for un-read chars and strings. */
121 unsigned char *putback_buf;
1be6b49c 122 size_t putback_buf_size; /* allocated size of putback_buf. */
f4bc4e59
LC
123
124 /* input/output iconv conversion descriptors */
125 void *input_cd;
126 void *output_cd;
92c2555f 127} scm_t_port;
840ae05d 128
5dbc6c06 129
2721f918 130SCM_INTERNAL SCM scm_i_port_weak_set;
5dbc6c06 131
0f2d19dd 132
6fe692e9 133#define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
0f2d19dd
JB
134
135\f
136
bc36d050 137#define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
0c32d76c 138
0f2d19dd 139/* PORT FLAGS
dbece3a2 140 * A set of flags characterizes a port.
571031dc
JB
141 * Note that we reserve the bits 1 << 24 and above for use by the
142 * routines in the port's scm_ptobfuns structure.
0f2d19dd
JB
143 */
144#define SCM_OPN (1L<<16) /* Is the port open? */
145#define SCM_RDNG (2L<<16) /* Is it a readable port? */
146#define SCM_WRTNG (4L<<16) /* Is it writable? */
ee149d03 147#define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
ee149d03 148#define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
0f2d19dd 149
dc7da0be
AW
150#define SCM_PORTP(x) (SCM_HAS_TYP7 (x, scm_tc7_port))
151#define SCM_OPPORTP(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_OPN))
152#define SCM_INPUT_PORT_P(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_RDNG))
153#define SCM_OUTPUT_PORT_P(x) (SCM_PORTP (x) && (SCM_CELL_WORD_0 (x) & SCM_WRTNG))
154#define SCM_OPINPORTP(x) (SCM_OPPORTP (x) && SCM_INPUT_PORT_P (x))
155#define SCM_OPOUTPORTP(x) (SCM_OPPORTP (x) && SCM_OUTPUT_PORT_P (x))
156#define SCM_OPENP(x) (SCM_OPPORTP (x))
157#define SCM_CLOSEDP(x) (!SCM_OPENP (x))
22a52da1
DH
158#define SCM_CLR_PORT_OPEN_FLAG(p) \
159 SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
843524cc 160
92c2555f 161#define SCM_PTAB_ENTRY(x) ((scm_t_port *) SCM_CELL_WORD_1 (x))
34d19ef6 162#define SCM_SETPTAB_ENTRY(x, ent) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
843524cc 163#define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
34d19ef6 164#define SCM_SETSTREAM(x, s) (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
843524cc 165#define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
b24b5e13 166#define SCM_SET_FILENAME(x, n) (SCM_PTAB_ENTRY(x)->file_name = (n))
843524cc
DH
167#define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
168#define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
169#define SCM_REVEALED(x) (SCM_PTAB_ENTRY(x)->revealed)
34d19ef6 170#define SCM_SETREVEALED(x, s) (SCM_PTAB_ENTRY(x)->revealed = (s))
0f2d19dd 171
b0799290
MG
172#define SCM_INCLINE(port) do {SCM_LINUM (port) += 1; SCM_COL (port) = 0;} while (0)
173#define SCM_ZEROCOL(port) do {SCM_COL (port) = 0;} while (0)
174#define SCM_INCCOL(port) do {SCM_COL (port) += 1;} while (0)
175#define SCM_DECCOL(port) do {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;} while (0)
176#define SCM_TABCOL(port) do {SCM_COL (port) += 8 - SCM_COL (port) % 8;} while (0)
0f2d19dd 177
0953b549
LC
178/* Maximum number of port types. */
179#define SCM_I_MAX_PORT_TYPE_COUNT 256
180
0f2d19dd
JB
181\f
182
affc96b5 183/* port-type description. */
92c2555f 184typedef struct scm_t_ptob_descriptor
0f82baf6 185{
f12733c9 186 char *name;
0c0669cc 187 SCM (*mark) (SCM);
1be6b49c 188 size_t (*free) (SCM);
0c0669cc
JB
189 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
190 SCM (*equalp) (SCM, SCM);
affc96b5
GH
191 int (*close) (SCM port);
192
8aa011a1 193 void (*write) (SCM port, const void *data, size_t size);
affc96b5
GH
194 void (*flush) (SCM port);
195
196 void (*end_input) (SCM port, int offset);
197 int (*fill_input) (SCM port);
198 int (*input_waiting) (SCM port);
199
f1ce9199
LC
200 scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
201 void (*truncate) (SCM port, scm_t_off length);
affc96b5 202
92c2555f 203} scm_t_ptob_descriptor;
1be6b49c 204
12a8b769
DH
205#define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
206#define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
f12733c9
MD
207/* SCM_PTOBNAME can be 0 if name is missing */
208#define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
0f82baf6
JB
209
210\f
211
fcfbe5f9
AW
212/* Hey you! Yes you, reading the header file! We're going to deprecate
213 scm_ptobs in 2.2, so please don't write any new code that uses it.
214 Thanks. */
33b001fd
MV
215SCM_API scm_t_ptob_descriptor *scm_ptobs;
216SCM_API long scm_numptob;
0f2d19dd
JB
217
218\f
0f2d19dd 219
33b001fd
MV
220SCM_API SCM scm_markstream (SCM ptr);
221SCM_API scm_t_bits scm_make_port_type (char *name,
222 int (*fill_input) (SCM port),
223 void (*write) (SCM port,
224 const void *data,
225 size_t size));
23f2b9a3
KR
226SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
227SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
228SCM_API void scm_set_port_print (scm_t_bits tc,
33b001fd
MV
229 int (*print) (SCM exp,
230 SCM port,
231 scm_print_state *pstate));
23f2b9a3
KR
232SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
233SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
33b001fd 234
23f2b9a3 235SCM_API void scm_set_port_flush (scm_t_bits tc,
33b001fd 236 void (*flush) (SCM port));
23f2b9a3 237SCM_API void scm_set_port_end_input (scm_t_bits tc,
33b001fd
MV
238 void (*end_input) (SCM port,
239 int offset));
23f2b9a3 240SCM_API void scm_set_port_seek (scm_t_bits tc,
f1ce9199
LC
241 scm_t_off (*seek) (SCM port,
242 scm_t_off OFFSET,
243 int WHENCE));
23f2b9a3 244SCM_API void scm_set_port_truncate (scm_t_bits tc,
33b001fd 245 void (*truncate) (SCM port,
f1ce9199 246 scm_t_off length));
23f2b9a3 247SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
33b001fd 248SCM_API SCM scm_char_ready_p (SCM port);
c2da2648 249size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
33b001fd
MV
250SCM_API SCM scm_drain_input (SCM port);
251SCM_API SCM scm_current_input_port (void);
252SCM_API SCM scm_current_output_port (void);
253SCM_API SCM scm_current_error_port (void);
254SCM_API SCM scm_current_load_port (void);
255SCM_API SCM scm_set_current_input_port (SCM port);
256SCM_API SCM scm_set_current_output_port (SCM port);
257SCM_API SCM scm_set_current_error_port (SCM port);
661ae7ab
MV
258SCM_API void scm_dynwind_current_input_port (SCM port);
259SCM_API void scm_dynwind_current_output_port (SCM port);
260SCM_API void scm_dynwind_current_error_port (SCM port);
2721f918
AW
261
262SCM_API SCM
263scm_c_make_port_with_encoding (scm_t_bits tag,
264 unsigned long mode_bits,
265 const char *encoding,
266 scm_t_string_failed_conversion_handler handler,
267 scm_t_bits stream);
268SCM_API SCM scm_c_make_port (scm_t_bits tag, unsigned long mode_bits,
269 scm_t_bits stream);
891702ea 270SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
2721f918 271
30b126d2
AW
272SCM_INLINE int scm_c_lock_port (scm_t_port *entry);
273SCM_INLINE int scm_c_try_lock_port (scm_t_port *entry);
274SCM_INLINE int scm_c_unlock_port (scm_t_port *entry);
275
33b001fd
MV
276SCM_API void scm_port_non_buffer (scm_t_port *pt);
277SCM_API int scm_revealed_count (SCM port);
278SCM_API SCM scm_port_revealed (SCM port);
279SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
280SCM_API long scm_mode_bits (char *modes);
281SCM_API SCM scm_port_mode (SCM port);
282SCM_API SCM scm_close_input_port (SCM port);
283SCM_API SCM scm_close_output_port (SCM port);
284SCM_API SCM scm_close_port (SCM port);
285SCM_API SCM scm_port_for_each (SCM proc);
c536b4b3 286SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
33b001fd
MV
287SCM_API SCM scm_input_port_p (SCM x);
288SCM_API SCM scm_output_port_p (SCM x);
289SCM_API SCM scm_port_p (SCM x);
290SCM_API SCM scm_port_closed_p (SCM port);
291SCM_API SCM scm_eof_object_p (SCM x);
292SCM_API SCM scm_force_output (SCM port);
293SCM_API SCM scm_flush_all_ports (void);
294SCM_API SCM scm_read_char (SCM port);
889975e5 295SCM_API scm_t_wchar scm_getc (SCM port);
fca14149
AW
296SCM_INLINE int scm_get_byte_or_eof (SCM port);
297SCM_INLINE int scm_peek_byte_or_eof (SCM port);
33b001fd 298SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
fca14149
AW
299SCM_INLINE void scm_putc (char c, SCM port);
300SCM_INLINE void scm_puts (const char *str_data, SCM port);
33b001fd
MV
301SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
302SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
9c44cd45
MG
303SCM_INTERNAL void scm_lfwrite_substr (SCM str, size_t start, size_t end,
304 SCM port);
33b001fd
MV
305SCM_API void scm_flush (SCM port);
306SCM_API void scm_end_input (SCM port);
307SCM_API int scm_fill_input (SCM port);
889975e5
MG
308SCM_INTERNAL void scm_unget_byte (int c, SCM port);
309SCM_API void scm_ungetc (scm_t_wchar c, SCM port);
33b001fd
MV
310SCM_API void scm_ungets (const char *s, int n, SCM port);
311SCM_API SCM scm_peek_char (SCM port);
312SCM_API SCM scm_unread_char (SCM cobj, SCM port);
313SCM_API SCM scm_unread_string (SCM str, SCM port);
314SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
315SCM_API SCM scm_truncate_file (SCM object, SCM length);
316SCM_API SCM scm_port_line (SCM port);
317SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
318SCM_API SCM scm_port_column (SCM port);
319SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
320SCM_API SCM scm_port_filename (SCM port);
321SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
9d9c66ba
LC
322SCM_INTERNAL const char *scm_i_default_port_encoding (void);
323SCM_INTERNAL void scm_i_set_default_port_encoding (const char *);
889975e5
MG
324SCM_INTERNAL void scm_i_set_port_encoding_x (SCM port, const char *str);
325SCM_API SCM scm_port_encoding (SCM port);
326SCM_API SCM scm_set_port_encoding_x (SCM port, SCM encoding);
327SCM_INTERNAL scm_t_string_failed_conversion_handler scm_i_get_conversion_strategy (SCM port);
328SCM_INTERNAL void scm_i_set_conversion_strategy_x (SCM port,
329 scm_t_string_failed_conversion_handler h);
330SCM_API SCM scm_port_conversion_strategy (SCM port);
331SCM_API SCM scm_set_port_conversion_strategy_x (SCM port, SCM behavior);
33b001fd
MV
332SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
333SCM_API void scm_print_port_mode (SCM exp, SCM port);
33b001fd
MV
334SCM_API SCM scm_void_port (char * mode_str);
335SCM_API SCM scm_sys_make_void_port (SCM mode);
102dbb6f 336SCM_INTERNAL void scm_init_ports (void);
0f2d19dd 337
d617ee18
MV
338/* internal */
339
102dbb6f
LC
340SCM_INTERNAL long scm_i_mode_bits (SCM modes);
341SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
d617ee18
MV
342
343
fca14149
AW
344/* Inline function implementations. */
345
346#if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
347SCM_INLINE_IMPLEMENTATION int
348scm_c_lock_port (scm_t_port *entry)
349{
350#if SCM_USE_PTHREAD_THREADS
351 return scm_i_pthread_mutex_lock (&entry->lock);
352#else
353 return 0;
354#endif
355}
356
357SCM_INLINE_IMPLEMENTATION int
358scm_c_try_lock_port (scm_t_port *entry)
359{
360#if SCM_USE_PTHREAD_THREADS
361 return scm_i_pthread_mutex_trylock (&entry->lock);
362#else
363 return 0;
364#endif
365}
366
367SCM_INLINE_IMPLEMENTATION int
368scm_c_unlock_port (scm_t_port *entry)
369{
370#if SCM_USE_PTHREAD_THREADS
371 return scm_i_pthread_mutex_unlock (&entry->lock);
372#else
373 return 0;
374#endif
375}
376
377SCM_INLINE_IMPLEMENTATION int
378scm_get_byte_or_eof (SCM port)
379{
380 int c;
381 scm_t_port *pt = SCM_PTAB_ENTRY (port);
382
383 if (pt->rw_active == SCM_PORT_WRITE)
384 /* may be marginally faster than calling scm_flush. */
385 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
386
387 if (pt->rw_random)
388 pt->rw_active = SCM_PORT_READ;
389
390 if (pt->read_pos >= pt->read_end)
391 {
392 if (SCM_UNLIKELY (scm_fill_input (port) == EOF))
393 return EOF;
394 }
395
396 c = *(pt->read_pos++);
397
398 return c;
399}
400
401/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */
402SCM_INLINE_IMPLEMENTATION int
403scm_peek_byte_or_eof (SCM port)
404{
405 int c;
406 scm_t_port *pt = SCM_PTAB_ENTRY (port);
407
408 if (pt->rw_active == SCM_PORT_WRITE)
409 /* may be marginally faster than calling scm_flush. */
410 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
411
412 if (pt->rw_random)
413 pt->rw_active = SCM_PORT_READ;
414
415 if (pt->read_pos >= pt->read_end)
416 {
417 if (SCM_UNLIKELY (scm_fill_input (port) == EOF))
418 return EOF;
419 }
420
421 c = *pt->read_pos;
422
423 return c;
424}
425
426SCM_INLINE_IMPLEMENTATION void
427scm_putc (char c, SCM port)
428{
429 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
430 scm_lfwrite (&c, 1, port);
431}
432
433SCM_INLINE_IMPLEMENTATION void
434scm_puts (const char *s, SCM port)
435{
436 SCM_ASSERT_TYPE (SCM_OPOUTPORTP (port), port, 0, NULL, "output port");
437 scm_lfwrite (s, strlen (s), port);
438}
439#endif /* SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES */
440
22a52da1 441#endif /* SCM_PORTS_H */
89e00824
ML
442
443/*
444 Local Variables:
445 c-file-style: "gnu"
446 End:
447*/