* numbers.c (scm_logand, scm_logior, scm_logxor, scm_logtest,
[bpt/guile.git] / libguile / ports.h
1 /* classes: h_files */
2
3 #ifndef PORTSH
4 #define PORTSH
5 /* Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
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
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
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.
44 * If you do not wish that, delete this exception notice. */
45 \f
46
47 #include "libguile/__scm.h"
48
49 #include "libguile/print.h"
50 #include "libguile/struct.h"
51
52 \f
53
54 #define SCM_INITIAL_CBUF_SIZE 4
55
56 struct scm_port_table
57 {
58 SCM port; /* Open port. */
59 int entry; /* Index in port table. */
60 int revealed; /* 0 not revealed, > 1 revealed.
61 * Revealed ports do not get GC'd.
62 */
63
64 SCM stream;
65
66 SCM file_name; /* debugging support. */
67 int line_number; /* debugging support. */
68 int column_number; /* debugging support. */
69
70 char *cp; /* where to put and get unget chars */
71 char *cbufend; /* points after this struct */
72 char cbuf[SCM_INITIAL_CBUF_SIZE]; /* must be last: may grow */
73 };
74
75 extern struct scm_port_table **scm_port_table;
76 extern int scm_port_table_size; /* Number of ports in scm_port_table. */
77
78
79 \f
80
81 #define SCM_EOF_OBJECT_P(x) ((x) == SCM_EOF_VAL)
82
83 /* PORT FLAGS
84 * A set of flags characterizes a port.
85 * Note that we reserve the bits 1 << 24 and above for use by the
86 * routines in the port's scm_ptobfuns structure.
87 */
88 #define SCM_OPN (1L<<16) /* Is the port open? */
89 #define SCM_RDNG (2L<<16) /* Is it a readable port? */
90 #define SCM_WRTNG (4L<<16) /* Is it writable? */
91 #define SCM_BUF0 (8L<<16)
92 #define SCM_NOFTELL (16L<<16) /* Does ftell work on this? Yuck! */
93 #define SCM_CRDY (32L<<16) /* Should char-ready? return #t? */
94
95 /* A mask used to clear the char-ready port flag. */
96 #define SCM_CUC (~SCM_CRDY)
97
98 #define SCM_PORTP(x) (SCM_TYP7(x)==scm_tc7_port)
99 #define SCM_OPPORTP(x) (((0x7f | SCM_OPN) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN))
100 #define SCM_OPINPORTP(x) (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG))
101 #define SCM_OPOUTPORTP(x) (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG))
102 #define SCM_FPORTP(x) (SCM_TYP16S(x)==scm_tc7_port)
103 #define SCM_OPFPORTP(x) (((0xfeff | SCM_OPN) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN))
104 #define SCM_OPINFPORTP(x) (((0xfeff | SCM_OPN | SCM_RDNG) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG))
105 #define SCM_OPOUTFPORTP(x) (((0xfeff | SCM_OPN | SCM_WRTNG) & SCM_CAR(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG))
106
107 #define SCM_INPORTP(x) (((0x7f | SCM_RDNG) & SCM_CAR(x))==(scm_tc7_port | SCM_RDNG))
108 #define SCM_OUTPORTP(x) (((0x7f | SCM_WRTNG) & SCM_CAR(x))==(scm_tc7_port | SCM_WRTNG))
109 #define SCM_OPENP(x) (SCM_OPN & SCM_CAR(x))
110 #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
111 #define SCM_PTAB_ENTRY(x) ((struct scm_port_table *)SCM_CDR(x))
112 #define SCM_SETPTAB_ENTRY(x,ent) SCM_SETCDR ((x), (SCM)(ent))
113 #define SCM_STREAM(x) SCM_PTAB_ENTRY(x)->stream
114 #define SCM_SETSTREAM(x,s) (SCM_PTAB_ENTRY(x)->stream = s)
115 #define SCM_FILENAME(x) SCM_PTAB_ENTRY(x)->file_name
116 #define SCM_LINUM(x) SCM_PTAB_ENTRY(x)->line_number
117 #define SCM_COL(x) SCM_PTAB_ENTRY(x)->column_number
118 #define SCM_REVEALED(x) SCM_PTAB_ENTRY(x)->revealed
119 #define SCM_SETREVEALED(x,s) (SCM_PTAB_ENTRY(x)->revealed = s)
120 #define SCM_CRDYP(port) (SCM_CAR (port) & SCM_CRDY)
121 #define SCM_SETRDY(port) {SCM_SETOR_CAR (port, SCM_CRDY);}
122 #define SCM_CUNGET(c, port) \
123 { \
124 if (SCM_CRDYP (port)) \
125 { \
126 if (++SCM_PTAB_ENTRY (port)->cp == SCM_PTAB_ENTRY (port)->cbufend) \
127 scm_grow_port_cbuf (port, 1); \
128 *SCM_PTAB_ENTRY (port)->cp = c; \
129 } \
130 else \
131 { \
132 SCM_PTAB_ENTRY (port)->cbuf[0] = c; \
133 SCM_SETRDY (port); \
134 } \
135 } \
136
137 #define SCM_CGETUN(port) (*SCM_PTAB_ENTRY (port)->cp)
138 #define SCM_CLRDY(port) \
139 { \
140 SCM_PTAB_ENTRY (port)->cp = SCM_PTAB_ENTRY (port)->cbuf; \
141 SCM_SETAND_CAR (port, SCM_CUC); \
142 } \
143
144 #define SCM_TRY_CLRDY(port) \
145 { \
146 if (SCM_PTAB_ENTRY (port)->cp == SCM_PTAB_ENTRY (port)->cbuf) \
147 SCM_SETAND_CAR (port, SCM_CUC); \
148 else \
149 --SCM_PTAB_ENTRY (port)->cp; \
150 } \
151
152 /* Returns number of unread characters in a port.
153 Returns wrong answer if SCM_CRDYP is false. */
154 #define SCM_N_READY_CHARS(port) \
155 (SCM_PTAB_ENTRY (port)->cp - SCM_PTAB_ENTRY (port)->cbuf + 1)
156
157 #define SCM_INCLINE(port) {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
158 #define SCM_INCCOL(port) {SCM_COL (port) += 1;}
159 #define SCM_TABCOL(port) {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
160
161
162
163 \f
164
165 typedef struct scm_ptobfuns
166 {
167 SCM (*mark) (SCM);
168 int (*free) (SCM);
169 int (*print) (SCM exp, SCM port, scm_print_state *pstate);
170 SCM (*equalp) (SCM, SCM);
171 int (*fputc) (int, SCM port);
172 int (*fputs) (char *, SCM port);
173 scm_sizet (*fwrite) SCM_P ((char *ptr,
174 scm_sizet size,
175 scm_sizet nitems,
176 SCM port));
177 int (*fflush) (SCM port);
178 int (*fgetc) (SCM port);
179 char * (*fgets) (SCM port, int *len);
180 int (*fclose) (SCM port);
181 } scm_ptobfuns;
182
183 #define SCM_PTOBNUM(x) (0x0ff & (SCM_CAR(x)>>8))
184
185 \f
186
187 extern scm_ptobfuns *scm_ptobs;
188 extern int scm_numptob;
189 extern int scm_port_table_room;
190
191 \f
192
193 extern SCM scm_markstream SCM_P ((SCM ptr));
194 extern long scm_newptob SCM_P ((scm_ptobfuns *ptob));
195 extern SCM scm_char_ready_p SCM_P ((SCM port));
196 extern SCM scm_current_input_port SCM_P ((void));
197 extern SCM scm_current_output_port SCM_P ((void));
198 extern SCM scm_current_error_port SCM_P ((void));
199 extern SCM scm_current_load_port SCM_P ((void));
200 extern SCM scm_set_current_input_port SCM_P ((SCM port));
201 extern SCM scm_set_current_output_port SCM_P ((SCM port));
202 extern SCM scm_set_current_error_port SCM_P ((SCM port));
203 extern struct scm_port_table * scm_add_to_port_table SCM_P ((SCM port));
204 extern void scm_remove_from_port_table SCM_P ((SCM port));
205 extern void scm_grow_port_cbuf SCM_P ((SCM port, size_t requested));
206 extern SCM scm_pt_size SCM_P ((void));
207 extern SCM scm_pt_member SCM_P ((SCM member));
208 extern int scm_revealed_count SCM_P ((SCM port));
209 extern SCM scm_port_revealed SCM_P ((SCM port));
210 extern SCM scm_set_port_revealed_x SCM_P ((SCM port, SCM rcount));
211 extern long scm_mode_bits SCM_P ((char *modes));
212 extern SCM scm_port_mode SCM_P ((SCM port));
213 extern SCM scm_close_port SCM_P ((SCM port));
214 extern SCM scm_close_all_ports_except SCM_P ((SCM ports));
215 extern SCM scm_input_port_p SCM_P ((SCM x));
216 extern SCM scm_output_port_p SCM_P ((SCM x));
217 extern SCM scm_eof_object_p SCM_P ((SCM x));
218 extern SCM scm_force_output SCM_P ((SCM port));
219 extern SCM scm_flush_all_ports SCM_P ((void));
220 extern SCM scm_read_char SCM_P ((SCM port));
221 extern SCM scm_peek_char SCM_P ((SCM port));
222 extern SCM scm_unread_char SCM_P ((SCM cobj, SCM port));
223 extern SCM scm_unread_string SCM_P ((SCM str, SCM port));
224 extern char *scm_generic_fgets SCM_P ((SCM port, int *len));
225 extern SCM scm_port_line SCM_P ((SCM port));
226 extern SCM scm_set_port_line_x SCM_P ((SCM port, SCM line));
227 extern SCM scm_port_column SCM_P ((SCM port));
228 extern SCM scm_set_port_column_x SCM_P ((SCM port, SCM line));
229 extern SCM scm_port_filename SCM_P ((SCM port));
230 extern SCM scm_set_port_filename_x SCM_P ((SCM port, SCM filename));
231 extern void scm_prinport SCM_P ((SCM exp, SCM port, char *type));
232 extern void scm_ports_prehistory SCM_P ((void));
233 extern SCM scm_void_port SCM_P ((char * mode_str));
234 extern SCM scm_sys_make_void_port SCM_P ((SCM mode));
235 extern void scm_init_ports SCM_P ((void));
236
237 #ifdef GUILE_DEBUG
238 extern SCM scm_pt_size SCM_P ((void));
239 extern SCM scm_pt_member SCM_P ((SCM member));
240 #endif /* GUILE_DEBUG */
241
242 #endif /* PORTSH */