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