Commit | Line | Data |
---|---|---|
f4bc4e59 LC |
1 | /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, |
2 | * 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. | |
3 | * | |
73be1d9e | 4 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
5 | * modify it under the terms of the GNU Lesser General Public License |
6 | * as published by the Free Software Foundation; either version 3 of | |
7 | * the License, or (at your option) any later version. | |
0f2d19dd | 8 | * |
53befeb7 NJ |
9 | * This library is distributed in the hope that it will be useful, but |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
0f2d19dd | 13 | * |
73be1d9e MV |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | * 02110-1301 USA | |
73be1d9e | 18 | */ |
1bbd0b84 | 19 | |
1bbd0b84 | 20 | |
0f2d19dd | 21 | \f |
d68fee48 JB |
22 | /* Headers. */ |
23 | ||
2b829bbb KR |
24 | #define _LARGEFILE64_SOURCE /* ask for stat64 etc */ |
25 | ||
dbb605f5 | 26 | #ifdef HAVE_CONFIG_H |
bd515f37 RB |
27 | # include <config.h> |
28 | #endif | |
29 | ||
0f2d19dd | 30 | #include <stdio.h> |
e6e2e95a | 31 | #include <errno.h> |
8ab3d8a0 | 32 | #include <fcntl.h> /* for chsize on mingw */ |
b5cb4464 | 33 | #include <assert.h> |
f4bc4e59 | 34 | #include <iconv.h> |
889975e5 MG |
35 | #include <uniconv.h> |
36 | #include <unistr.h> | |
37 | #include <striconveh.h> | |
e6e2e95a | 38 | |
fca43887 LC |
39 | #include <assert.h> |
40 | ||
a0599745 | 41 | #include "libguile/_scm.h" |
4e047c3e | 42 | #include "libguile/async.h" |
8269ba5b | 43 | #include "libguile/deprecation.h" |
f0942910 | 44 | #include "libguile/eval.h" |
8ab3d8a0 | 45 | #include "libguile/fports.h" /* direct access for seek and truncate */ |
9511876f | 46 | #include "libguile/goops.h" |
a0599745 MD |
47 | #include "libguile/smob.h" |
48 | #include "libguile/chars.h" | |
185e369a | 49 | #include "libguile/dynwind.h" |
0f2d19dd | 50 | |
a0599745 | 51 | #include "libguile/keywords.h" |
5dbc6c06 | 52 | #include "libguile/hashtab.h" |
a0599745 MD |
53 | #include "libguile/root.h" |
54 | #include "libguile/strings.h" | |
b42170a4 | 55 | #include "libguile/mallocs.h" |
a0599745 MD |
56 | #include "libguile/validate.h" |
57 | #include "libguile/ports.h" | |
3a5fb14d | 58 | #include "libguile/vectors.h" |
2721f918 | 59 | #include "libguile/weak-set.h" |
9de87eea | 60 | #include "libguile/fluids.h" |
889975e5 | 61 | #include "libguile/eq.h" |
0f2d19dd | 62 | |
bd9e24b3 GH |
63 | #ifdef HAVE_STRING_H |
64 | #include <string.h> | |
65 | #endif | |
66 | ||
ec65f5da MV |
67 | #ifdef HAVE_IO_H |
68 | #include <io.h> | |
69 | #endif | |
70 | ||
0f2d19dd JB |
71 | #ifdef HAVE_UNISTD_H |
72 | #include <unistd.h> | |
73 | #endif | |
74 | ||
95b88819 GH |
75 | #ifdef HAVE_SYS_IOCTL_H |
76 | #include <sys/ioctl.h> | |
77 | #endif | |
d68fee48 | 78 | |
8ab3d8a0 KR |
79 | /* Mingw (version 3.4.5, circa 2006) has ftruncate as an alias for chsize |
80 | already, but have this code here in case that wasn't so in past versions, | |
81 | or perhaps to help other minimal DOS environments. | |
82 | ||
83 | gnulib ftruncate.c has code using fcntl F_CHSIZE and F_FREESP, which | |
84 | might be possibilities if we've got other systems without ftruncate. */ | |
85 | ||
56a3dcd4 | 86 | #if defined HAVE_CHSIZE && ! defined HAVE_FTRUNCATE |
82893676 | 87 | #define ftruncate(fd, size) chsize (fd, size) |
8ab3d8a0 KR |
88 | #undef HAVE_FTRUNCATE |
89 | #define HAVE_FTRUNCATE 1 | |
82893676 MG |
90 | #endif |
91 | ||
0f2d19dd | 92 | \f |
d68fee48 | 93 | /* The port kind table --- a dynamically resized array of port types. */ |
0f2d19dd JB |
94 | |
95 | ||
96 | /* scm_ptobs scm_numptob | |
5dbc6c06 | 97 | * implement a dynamically resized array of ptob records. |
0f2d19dd JB |
98 | * Indexes into this table are used when generating type |
99 | * tags for smobjects (if you know a tag you can get an index and conversely). | |
100 | */ | |
62bd5d66 AW |
101 | static scm_t_ptob_descriptor **scm_ptobs = NULL; |
102 | static long scm_numptob = 0; /* Number of port types. */ | |
103 | static long scm_ptobs_size = 0; /* Number of slots in the port type | |
104 | table. */ | |
105 | static scm_i_pthread_mutex_t scm_ptobs_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER; | |
106 | ||
107 | long | |
108 | scm_c_num_port_types (void) | |
109 | { | |
110 | long ret; | |
111 | ||
112 | scm_i_pthread_mutex_lock (&scm_ptobs_lock); | |
113 | ret = scm_numptob; | |
114 | scm_i_pthread_mutex_unlock (&scm_ptobs_lock); | |
115 | ||
116 | return ret; | |
117 | } | |
118 | ||
119 | scm_t_ptob_descriptor* | |
120 | scm_c_port_type_ref (long ptobnum) | |
121 | { | |
122 | scm_t_ptob_descriptor *ret = NULL; | |
123 | ||
124 | scm_i_pthread_mutex_lock (&scm_ptobs_lock); | |
125 | ||
126 | if (0 <= ptobnum && ptobnum < scm_numptob) | |
127 | ret = scm_ptobs[ptobnum]; | |
128 | ||
129 | scm_i_pthread_mutex_unlock (&scm_ptobs_lock); | |
130 | ||
131 | if (!ret) | |
132 | scm_out_of_range ("scm_c_port_type_ref", scm_from_long (ptobnum)); | |
133 | ||
134 | return ret; | |
135 | } | |
136 | ||
137 | long | |
138 | scm_c_port_type_add_x (scm_t_ptob_descriptor *desc) | |
139 | { | |
140 | long ret = -1; | |
141 | ||
142 | scm_i_pthread_mutex_lock (&scm_ptobs_lock); | |
143 | ||
144 | if (scm_numptob + 1 < SCM_I_MAX_PORT_TYPE_COUNT) | |
145 | { | |
146 | if (scm_numptob == scm_ptobs_size) | |
147 | { | |
148 | unsigned long old_size = scm_ptobs_size; | |
149 | scm_t_ptob_descriptor **old_ptobs = scm_ptobs; | |
150 | ||
151 | /* Currently there are only 9 predefined port types, so one | |
152 | resize will cover it. */ | |
153 | scm_ptobs_size = old_size + 10; | |
154 | ||
155 | if (scm_ptobs_size >= SCM_I_MAX_PORT_TYPE_COUNT) | |
156 | scm_ptobs_size = SCM_I_MAX_PORT_TYPE_COUNT; | |
157 | ||
158 | scm_ptobs = scm_gc_malloc (sizeof (*scm_ptobs) * scm_ptobs_size, | |
159 | "scm_ptobs"); | |
160 | ||
161 | memcpy (scm_ptobs, old_ptobs, sizeof (*scm_ptobs) * scm_numptob); | |
162 | } | |
163 | ||
164 | ret = scm_numptob++; | |
165 | scm_ptobs[ret] = desc; | |
166 | } | |
167 | ||
168 | scm_i_pthread_mutex_unlock (&scm_ptobs_lock); | |
169 | ||
170 | if (ret < 0) | |
171 | scm_out_of_range ("scm_c_port_type_add_x", scm_from_long (scm_numptob)); | |
172 | ||
173 | return ret; | |
174 | } | |
0f2d19dd | 175 | |
f12733c9 | 176 | /* |
f12733c9 | 177 | * We choose to use an interface similar to the smob interface with |
affc96b5 | 178 | * fill_input and write as standard fields, passed to the port |
f12733c9 MD |
179 | * type constructor, and optional fields set by setters. |
180 | */ | |
181 | ||
70df8af6 | 182 | static void |
e81d98ec | 183 | flush_port_default (SCM port SCM_UNUSED) |
70df8af6 GH |
184 | { |
185 | } | |
186 | ||
187 | static void | |
e81d98ec | 188 | end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED) |
70df8af6 GH |
189 | { |
190 | } | |
0f2d19dd | 191 | |
92c2555f | 192 | scm_t_bits |
f12733c9 | 193 | scm_make_port_type (char *name, |
affc96b5 | 194 | int (*fill_input) (SCM port), |
8aa011a1 | 195 | void (*write) (SCM port, const void *data, size_t size)) |
0f2d19dd | 196 | { |
62bd5d66 AW |
197 | scm_t_ptob_descriptor *desc; |
198 | long ptobnum; | |
affc96b5 | 199 | |
62bd5d66 AW |
200 | desc = scm_gc_malloc_pointerless (sizeof (*desc), "port-type"); |
201 | memset (desc, 0, sizeof (*desc)); | |
affc96b5 | 202 | |
62bd5d66 AW |
203 | desc->name = name; |
204 | desc->print = scm_port_print; | |
205 | desc->write = write; | |
206 | desc->flush = flush_port_default; | |
207 | desc->end_input = end_input_default; | |
208 | desc->fill_input = fill_input; | |
affc96b5 | 209 | |
62bd5d66 | 210 | ptobnum = scm_c_port_type_add_x (desc); |
affc96b5 | 211 | |
62bd5d66 | 212 | /* Make a class object if GOOPS is present. */ |
63385df2 | 213 | if (SCM_UNPACK (scm_port_class[0]) != 0) |
62bd5d66 AW |
214 | scm_make_port_classes (ptobnum, name); |
215 | ||
216 | return scm_tc7_port + ptobnum * 256; | |
0f2d19dd JB |
217 | } |
218 | ||
f12733c9 | 219 | void |
23f2b9a3 | 220 | scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM)) |
f12733c9 | 221 | { |
62bd5d66 | 222 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->mark = mark; |
f12733c9 MD |
223 | } |
224 | ||
225 | void | |
23f2b9a3 | 226 | scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM)) |
f12733c9 | 227 | { |
62bd5d66 | 228 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->free = free; |
f12733c9 MD |
229 | } |
230 | ||
231 | void | |
23f2b9a3 | 232 | scm_set_port_print (scm_t_bits tc, int (*print) (SCM exp, SCM port, |
19b8d12b | 233 | scm_print_state *pstate)) |
f12733c9 | 234 | { |
62bd5d66 | 235 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->print = print; |
f12733c9 MD |
236 | } |
237 | ||
238 | void | |
23f2b9a3 | 239 | scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM)) |
f12733c9 | 240 | { |
62bd5d66 | 241 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->equalp = equalp; |
f12733c9 MD |
242 | } |
243 | ||
31703ab8 | 244 | void |
19b8d12b | 245 | scm_set_port_close (scm_t_bits tc, int (*close) (SCM)) |
31703ab8 | 246 | { |
19b8d12b | 247 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->close = close; |
31703ab8 GH |
248 | } |
249 | ||
f12733c9 | 250 | void |
19b8d12b | 251 | scm_set_port_flush (scm_t_bits tc, void (*flush) (SCM port)) |
f12733c9 | 252 | { |
19b8d12b | 253 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->flush = flush; |
f12733c9 MD |
254 | } |
255 | ||
256 | void | |
19b8d12b | 257 | scm_set_port_end_input (scm_t_bits tc, void (*end_input) (SCM port, int offset)) |
f12733c9 | 258 | { |
19b8d12b | 259 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->end_input = end_input; |
f12733c9 MD |
260 | } |
261 | ||
262 | void | |
19b8d12b | 263 | scm_set_port_seek (scm_t_bits tc, scm_t_off (*seek) (SCM, scm_t_off, int)) |
f12733c9 | 264 | { |
62bd5d66 | 265 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->seek = seek; |
f12733c9 MD |
266 | } |
267 | ||
268 | void | |
f1ce9199 | 269 | scm_set_port_truncate (scm_t_bits tc, void (*truncate) (SCM, scm_t_off)) |
f12733c9 | 270 | { |
62bd5d66 | 271 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->truncate = truncate; |
f12733c9 MD |
272 | } |
273 | ||
274 | void | |
23f2b9a3 | 275 | scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM)) |
f12733c9 | 276 | { |
62bd5d66 | 277 | scm_c_port_type_ref (SCM_TC2PTOBNUM (tc))->input_waiting = input_waiting; |
f12733c9 MD |
278 | } |
279 | ||
0f2d19dd | 280 | \f |
0f2d19dd | 281 | |
d68fee48 | 282 | /* Standard ports --- current input, output, error, and more(!). */ |
0f2d19dd | 283 | |
34297700 AW |
284 | static SCM cur_inport_fluid = SCM_BOOL_F; |
285 | static SCM cur_outport_fluid = SCM_BOOL_F; | |
286 | static SCM cur_errport_fluid = SCM_BOOL_F; | |
287 | static SCM cur_loadport_fluid = SCM_BOOL_F; | |
9de87eea | 288 | |
3b3b36dd | 289 | SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0, |
e1546b65 MG |
290 | (), |
291 | "Return the current input port. This is the default port used\n" | |
292 | "by many input procedures. Initially, @code{current-input-port}\n" | |
293 | "returns the @dfn{standard input} in Unix and C terminology.") | |
1bbd0b84 | 294 | #define FUNC_NAME s_scm_current_input_port |
0f2d19dd | 295 | { |
34297700 | 296 | if (scm_is_true (cur_inport_fluid)) |
889975e5 MG |
297 | return scm_fluid_ref (cur_inport_fluid); |
298 | else | |
299 | return SCM_BOOL_F; | |
0f2d19dd | 300 | } |
1bbd0b84 | 301 | #undef FUNC_NAME |
0f2d19dd | 302 | |
3b3b36dd | 303 | SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0, |
e1546b65 MG |
304 | (), |
305 | "Return the current output port. This is the default port used\n" | |
9401323e | 306 | "by many output procedures. Initially,\n" |
e1546b65 MG |
307 | "@code{current-output-port} returns the @dfn{standard output} in\n" |
308 | "Unix and C terminology.") | |
1bbd0b84 | 309 | #define FUNC_NAME s_scm_current_output_port |
0f2d19dd | 310 | { |
34297700 | 311 | if (scm_is_true (cur_outport_fluid)) |
889975e5 MG |
312 | return scm_fluid_ref (cur_outport_fluid); |
313 | else | |
314 | return SCM_BOOL_F; | |
0f2d19dd | 315 | } |
1bbd0b84 | 316 | #undef FUNC_NAME |
0f2d19dd | 317 | |
3b3b36dd | 318 | SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0, |
1bbd0b84 | 319 | (), |
b380b885 MD |
320 | "Return the port to which errors and warnings should be sent (the\n" |
321 | "@dfn{standard error} in Unix and C terminology).") | |
1bbd0b84 | 322 | #define FUNC_NAME s_scm_current_error_port |
0f2d19dd | 323 | { |
34297700 | 324 | if (scm_is_true (cur_errport_fluid)) |
889975e5 MG |
325 | return scm_fluid_ref (cur_errport_fluid); |
326 | else | |
327 | return SCM_BOOL_F; | |
0f2d19dd | 328 | } |
1bbd0b84 | 329 | #undef FUNC_NAME |
0f2d19dd | 330 | |
3b3b36dd | 331 | SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0, |
e1546b65 | 332 | (), |
b450f070 | 333 | "Return the current-load-port.\n" |
e1546b65 | 334 | "The load port is used internally by @code{primitive-load}.") |
1bbd0b84 | 335 | #define FUNC_NAME s_scm_current_load_port |
31614d8e | 336 | { |
9de87eea | 337 | return scm_fluid_ref (cur_loadport_fluid); |
31614d8e | 338 | } |
1bbd0b84 | 339 | #undef FUNC_NAME |
31614d8e | 340 | |
3b3b36dd | 341 | SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0, |
1bbd0b84 | 342 | (SCM port), |
8f85c0c6 NJ |
343 | "@deffnx {Scheme Procedure} set-current-output-port port\n" |
344 | "@deffnx {Scheme Procedure} set-current-error-port port\n" | |
b380b885 MD |
345 | "Change the ports returned by @code{current-input-port},\n" |
346 | "@code{current-output-port} and @code{current-error-port}, respectively,\n" | |
347 | "so that they use the supplied @var{port} for input or output.") | |
1bbd0b84 | 348 | #define FUNC_NAME s_scm_set_current_input_port |
0f2d19dd | 349 | { |
9de87eea | 350 | SCM oinp = scm_fluid_ref (cur_inport_fluid); |
34d19ef6 | 351 | SCM_VALIDATE_OPINPORT (1, port); |
9de87eea | 352 | scm_fluid_set_x (cur_inport_fluid, port); |
0f2d19dd JB |
353 | return oinp; |
354 | } | |
1bbd0b84 | 355 | #undef FUNC_NAME |
0f2d19dd JB |
356 | |
357 | ||
3b3b36dd | 358 | SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0, |
e1546b65 MG |
359 | (SCM port), |
360 | "Set the current default output port to @var{port}.") | |
1bbd0b84 | 361 | #define FUNC_NAME s_scm_set_current_output_port |
0f2d19dd | 362 | { |
9de87eea | 363 | SCM ooutp = scm_fluid_ref (cur_outport_fluid); |
78446828 | 364 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 365 | SCM_VALIDATE_OPOUTPORT (1, port); |
9de87eea | 366 | scm_fluid_set_x (cur_outport_fluid, port); |
0f2d19dd JB |
367 | return ooutp; |
368 | } | |
1bbd0b84 | 369 | #undef FUNC_NAME |
0f2d19dd JB |
370 | |
371 | ||
3b3b36dd | 372 | SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0, |
e1546b65 MG |
373 | (SCM port), |
374 | "Set the current default error port to @var{port}.") | |
1bbd0b84 | 375 | #define FUNC_NAME s_scm_set_current_error_port |
0f2d19dd | 376 | { |
9de87eea | 377 | SCM oerrp = scm_fluid_ref (cur_errport_fluid); |
78446828 | 378 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 379 | SCM_VALIDATE_OPOUTPORT (1, port); |
9de87eea | 380 | scm_fluid_set_x (cur_errport_fluid, port); |
0f2d19dd JB |
381 | return oerrp; |
382 | } | |
1bbd0b84 | 383 | #undef FUNC_NAME |
0f2d19dd | 384 | |
185e369a | 385 | void |
661ae7ab | 386 | scm_dynwind_current_input_port (SCM port) |
9de87eea | 387 | #define FUNC_NAME NULL |
185e369a | 388 | { |
9de87eea | 389 | SCM_VALIDATE_OPINPORT (1, port); |
661ae7ab | 390 | scm_dynwind_fluid (cur_inport_fluid, port); |
185e369a | 391 | } |
9de87eea | 392 | #undef FUNC_NAME |
185e369a MV |
393 | |
394 | void | |
661ae7ab | 395 | scm_dynwind_current_output_port (SCM port) |
9de87eea | 396 | #define FUNC_NAME NULL |
185e369a | 397 | { |
9de87eea MV |
398 | port = SCM_COERCE_OUTPORT (port); |
399 | SCM_VALIDATE_OPOUTPORT (1, port); | |
661ae7ab | 400 | scm_dynwind_fluid (cur_outport_fluid, port); |
185e369a | 401 | } |
9de87eea | 402 | #undef FUNC_NAME |
185e369a MV |
403 | |
404 | void | |
661ae7ab | 405 | scm_dynwind_current_error_port (SCM port) |
9de87eea MV |
406 | #define FUNC_NAME NULL |
407 | { | |
408 | port = SCM_COERCE_OUTPORT (port); | |
409 | SCM_VALIDATE_OPOUTPORT (1, port); | |
661ae7ab | 410 | scm_dynwind_fluid (cur_errport_fluid, port); |
9de87eea MV |
411 | } |
412 | #undef FUNC_NAME | |
413 | ||
414 | void | |
661ae7ab | 415 | scm_i_dynwind_current_load_port (SCM port) |
185e369a | 416 | { |
661ae7ab | 417 | scm_dynwind_fluid (cur_loadport_fluid, port); |
185e369a MV |
418 | } |
419 | ||
19b8d12b | 420 | |
0f2d19dd | 421 | \f |
0f2d19dd | 422 | |
19b8d12b AW |
423 | /* Retrieving a port's mode. */ |
424 | ||
425 | /* Return the flags that characterize a port based on the mode | |
426 | * string used to open a file for that port. | |
427 | * | |
428 | * See PORT FLAGS in scm.h | |
5dbc6c06 | 429 | */ |
19b8d12b AW |
430 | |
431 | static long | |
432 | scm_i_mode_bits_n (SCM modes) | |
433 | { | |
434 | return (SCM_OPN | |
435 | | (scm_i_string_contains_char (modes, 'r') | |
436 | || scm_i_string_contains_char (modes, '+') ? SCM_RDNG : 0) | |
437 | | (scm_i_string_contains_char (modes, 'w') | |
438 | || scm_i_string_contains_char (modes, 'a') | |
439 | || scm_i_string_contains_char (modes, '+') ? SCM_WRTNG : 0) | |
440 | | (scm_i_string_contains_char (modes, '0') ? SCM_BUF0 : 0) | |
441 | | (scm_i_string_contains_char (modes, 'l') ? SCM_BUFLINE : 0)); | |
442 | } | |
443 | ||
444 | long | |
445 | scm_mode_bits (char *modes) | |
446 | { | |
447 | return scm_i_mode_bits (scm_from_locale_string (modes)); | |
448 | } | |
449 | ||
450 | long | |
451 | scm_i_mode_bits (SCM modes) | |
452 | { | |
453 | long bits; | |
454 | ||
455 | if (!scm_is_string (modes)) | |
456 | scm_wrong_type_arg_msg (NULL, 0, modes, "string"); | |
457 | ||
458 | bits = scm_i_mode_bits_n (modes); | |
459 | scm_remember_upto_here_1 (modes); | |
460 | return bits; | |
461 | } | |
462 | ||
463 | /* Return the mode flags from an open port. | |
464 | * Some modes such as "append" are only used when opening | |
465 | * a file and are not returned here. */ | |
466 | ||
467 | SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0, | |
468 | (SCM port), | |
469 | "Return the port modes associated with the open port @var{port}.\n" | |
470 | "These will not necessarily be identical to the modes used when\n" | |
471 | "the port was opened, since modes such as \"append\" which are\n" | |
472 | "used only during port creation are not retained.") | |
473 | #define FUNC_NAME s_scm_port_mode | |
474 | { | |
475 | char modes[4]; | |
476 | modes[0] = '\0'; | |
477 | ||
478 | port = SCM_COERCE_OUTPORT (port); | |
479 | SCM_VALIDATE_OPPORT (1, port); | |
480 | if (SCM_CELL_WORD_0 (port) & SCM_RDNG) { | |
481 | if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) | |
482 | strcpy (modes, "r+"); | |
483 | else | |
484 | strcpy (modes, "r"); | |
485 | } | |
486 | else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) | |
487 | strcpy (modes, "w"); | |
488 | if (SCM_CELL_WORD_0 (port) & SCM_BUF0) | |
489 | strcat (modes, "0"); | |
490 | ||
491 | return scm_from_latin1_string (modes); | |
492 | } | |
493 | #undef FUNC_NAME | |
494 | ||
495 | ||
496 | \f | |
497 | ||
498 | /* The port table --- a weak set of all ports. | |
499 | ||
500 | We need a global registry of ports to flush them all at exit, and to | |
501 | get all the ports matching a file descriptor. */ | |
2721f918 | 502 | SCM scm_i_port_weak_set; |
b9ad392e | 503 | |
19b8d12b | 504 | |
651a0735 | 505 | \f |
651a0735 | 506 | |
19b8d12b | 507 | /* Port finalization. */ |
1cc91f1b | 508 | |
651a0735 LC |
509 | static void finalize_port (GC_PTR, GC_PTR); |
510 | ||
f4bc4e59 | 511 | /* Register a finalizer for PORT. */ |
651a0735 LC |
512 | static SCM_C_INLINE_KEYWORD void |
513 | register_finalizer_for_port (SCM port) | |
514 | { | |
f4bc4e59 LC |
515 | GC_finalization_proc prev_finalizer; |
516 | GC_PTR prev_finalization_data; | |
651a0735 | 517 | |
f4bc4e59 LC |
518 | /* Register a finalizer for PORT so that its iconv CDs get freed and |
519 | optionally its type's `free' function gets called. */ | |
47ed8656 AW |
520 | GC_REGISTER_FINALIZER_NO_ORDER (SCM_HEAP_OBJECT_BASE (port), |
521 | finalize_port, 0, | |
f4bc4e59 LC |
522 | &prev_finalizer, |
523 | &prev_finalization_data); | |
651a0735 LC |
524 | } |
525 | ||
526 | /* Finalize the object (a port) pointed to by PTR. */ | |
527 | static void | |
528 | finalize_port (GC_PTR ptr, GC_PTR data) | |
529 | { | |
21041372 | 530 | SCM port = SCM_PACK_POINTER (ptr); |
651a0735 LC |
531 | |
532 | if (!SCM_PORTP (port)) | |
533 | abort (); | |
534 | ||
535 | if (SCM_OPENP (port)) | |
536 | { | |
537 | if (SCM_REVEALED (port) > 0) | |
538 | /* Keep "revealed" ports alive and re-register a finalizer. */ | |
539 | register_finalizer_for_port (port); | |
540 | else | |
541 | { | |
62bd5d66 | 542 | scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port); |
f4bc4e59 LC |
543 | scm_t_port *entry; |
544 | ||
62bd5d66 AW |
545 | if (ptob->free) |
546 | /* Yes, I really do mean `free' rather than `close'. `close' | |
651a0735 | 547 | is for explicit `close-port' by user. */ |
62bd5d66 | 548 | ptob->free (port); |
651a0735 | 549 | |
f4bc4e59 LC |
550 | entry = SCM_PTAB_ENTRY (port); |
551 | ||
552 | if (entry->input_cd != (iconv_t) -1) | |
553 | iconv_close (entry->input_cd); | |
554 | if (entry->output_cd != (iconv_t) -1) | |
555 | iconv_close (entry->output_cd); | |
556 | ||
651a0735 LC |
557 | SCM_SETSTREAM (port, 0); |
558 | SCM_CLR_PORT_OPEN_FLAG (port); | |
651a0735 LC |
559 | |
560 | scm_gc_ports_collected++; | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | ||
651a0735 LC |
566 | \f |
567 | ||
da220f27 | 568 | SCM |
2721f918 AW |
569 | scm_c_make_port_with_encoding (scm_t_bits tag, unsigned long mode_bits, |
570 | const char *encoding, | |
571 | scm_t_string_failed_conversion_handler handler, | |
572 | scm_t_bits stream) | |
0f2d19dd | 573 | { |
2721f918 AW |
574 | SCM ret; |
575 | scm_t_port *entry; | |
62bd5d66 | 576 | scm_t_ptob_descriptor *ptob; |
2721f918 AW |
577 | |
578 | entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port"); | |
62bd5d66 AW |
579 | ptob = scm_c_port_type_ref (SCM_TC2PTOBNUM (tag)); |
580 | ||
581 | ret = scm_words (tag | mode_bits, 3); | |
582 | SCM_SET_CELL_WORD_1 (ret, (scm_t_bits) entry); | |
583 | SCM_SET_CELL_WORD_2 (ret, (scm_t_bits) ptob); | |
5f16b897 | 584 | |
30b126d2 AW |
585 | #if SCM_USE_PTHREAD_THREADS |
586 | scm_i_pthread_mutex_init (&entry->lock, scm_i_pthread_mutexattr_recursive); | |
587 | #endif | |
588 | ||
840ae05d | 589 | entry->file_name = SCM_BOOL_F; |
61e452ba | 590 | entry->rw_active = SCM_PORT_NEITHER; |
2721f918 AW |
591 | entry->port = ret; |
592 | entry->stream = stream; | |
593 | entry->encoding = encoding ? scm_gc_strdup (encoding, "port") : NULL; | |
f4bc4e59 LC |
594 | /* The conversion descriptors will be opened lazily. */ |
595 | entry->input_cd = (iconv_t) -1; | |
596 | entry->output_cd = (iconv_t) -1; | |
2721f918 | 597 | entry->ilseq_handler = handler; |
f4bc4e59 | 598 | |
2721f918 | 599 | scm_weak_set_add_x (scm_i_port_weak_set, ret); |
651a0735 LC |
600 | |
601 | /* For each new port, register a finalizer so that it port type's free | |
602 | function can be invoked eventually. */ | |
2721f918 | 603 | register_finalizer_for_port (ret); |
651a0735 | 604 | |
2721f918 AW |
605 | return ret; |
606 | } | |
607 | ||
608 | SCM | |
609 | scm_c_make_port (scm_t_bits tag, unsigned long mode_bits, scm_t_bits stream) | |
610 | { | |
611 | return scm_c_make_port_with_encoding (tag, mode_bits, | |
612 | scm_i_default_port_encoding (), | |
613 | scm_i_get_conversion_strategy (SCM_BOOL_F), | |
614 | stream); | |
615 | } | |
616 | ||
617 | SCM | |
618 | scm_new_port_table_entry (scm_t_bits tag) | |
619 | { | |
620 | return scm_c_make_port (tag, 0, 0); | |
0f2d19dd JB |
621 | } |
622 | ||
6c951427 | 623 | /* Remove a port from the table and destroy it. */ |
b9ad392e | 624 | |
8269ba5b | 625 | static void |
5dbc6c06 HWN |
626 | scm_i_remove_port (SCM port) |
627 | #define FUNC_NAME "scm_remove_port" | |
0f2d19dd | 628 | { |
8269ba5b | 629 | scm_t_port *p; |
682d78d0 | 630 | |
8269ba5b AW |
631 | p = SCM_PTAB_ENTRY (port); |
632 | scm_port_non_buffer (p); | |
2721f918 AW |
633 | SCM_SETPTAB_ENTRY (port, 0); |
634 | scm_weak_set_remove_x (scm_i_port_weak_set, port); | |
635 | ||
682d78d0 LC |
636 | p->putback_buf = NULL; |
637 | p->putback_buf_size = 0; | |
3e05fc04 AW |
638 | |
639 | if (p->input_cd != (iconv_t) -1) | |
640 | { | |
641 | iconv_close (p->input_cd); | |
642 | p->input_cd = (iconv_t) -1; | |
643 | } | |
644 | ||
645 | if (p->output_cd != (iconv_t) -1) | |
646 | { | |
647 | iconv_close (p->output_cd); | |
648 | p->output_cd = (iconv_t) -1; | |
649 | } | |
0f2d19dd | 650 | } |
db4b4ca6 DH |
651 | #undef FUNC_NAME |
652 | ||
0f2d19dd | 653 | |
d68fee48 | 654 | \f |
d68fee48 | 655 | |
19b8d12b | 656 | /* Predicates. */ |
1cc91f1b | 657 | |
19b8d12b AW |
658 | SCM_DEFINE (scm_port_p, "port?", 1, 0, 0, |
659 | (SCM x), | |
660 | "Return a boolean indicating whether @var{x} is a port.\n" | |
661 | "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n" | |
662 | "@var{x}))}.") | |
663 | #define FUNC_NAME s_scm_port_p | |
0f2d19dd | 664 | { |
19b8d12b | 665 | return scm_from_bool (SCM_PORTP (x)); |
0f2d19dd | 666 | } |
19b8d12b | 667 | #undef FUNC_NAME |
0f2d19dd | 668 | |
19b8d12b AW |
669 | SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0, |
670 | (SCM x), | |
671 | "Return @code{#t} if @var{x} is an input port, otherwise return\n" | |
672 | "@code{#f}. Any object satisfying this predicate also satisfies\n" | |
673 | "@code{port?}.") | |
674 | #define FUNC_NAME s_scm_input_port_p | |
0f2d19dd | 675 | { |
19b8d12b | 676 | return scm_from_bool (SCM_INPUT_PORT_P (x)); |
0f2d19dd | 677 | } |
1bbd0b84 | 678 | #undef FUNC_NAME |
0f2d19dd | 679 | |
19b8d12b AW |
680 | SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0, |
681 | (SCM x), | |
682 | "Return @code{#t} if @var{x} is an output port, otherwise return\n" | |
683 | "@code{#f}. Any object satisfying this predicate also satisfies\n" | |
684 | "@code{port?}.") | |
685 | #define FUNC_NAME s_scm_output_port_p | |
0f2d19dd | 686 | { |
19b8d12b AW |
687 | x = SCM_COERCE_OUTPORT (x); |
688 | return scm_from_bool (SCM_OUTPUT_PORT_P (x)); | |
0f2d19dd | 689 | } |
1bbd0b84 | 690 | #undef FUNC_NAME |
0f2d19dd | 691 | |
19b8d12b AW |
692 | SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0, |
693 | (SCM port), | |
694 | "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n" | |
695 | "open.") | |
696 | #define FUNC_NAME s_scm_port_closed_p | |
d617ee18 | 697 | { |
19b8d12b AW |
698 | SCM_VALIDATE_PORT (1, port); |
699 | return scm_from_bool (!SCM_OPPORTP (port)); | |
d617ee18 | 700 | } |
19b8d12b | 701 | #undef FUNC_NAME |
eadd48de | 702 | |
19b8d12b AW |
703 | SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0, |
704 | (SCM x), | |
705 | "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n" | |
706 | "return @code{#f}.") | |
707 | #define FUNC_NAME s_scm_eof_object_p | |
eadd48de | 708 | { |
19b8d12b | 709 | return scm_from_bool (SCM_EOF_OBJECT_P (x)); |
eadd48de | 710 | } |
1bbd0b84 | 711 | #undef FUNC_NAME |
eadd48de GH |
712 | |
713 | ||
d68fee48 | 714 | \f |
19b8d12b | 715 | |
d68fee48 JB |
716 | /* Closing ports. */ |
717 | ||
0f2d19dd JB |
718 | /* scm_close_port |
719 | * Call the close operation on a port object. | |
eadd48de | 720 | * see also scm_close. |
0f2d19dd | 721 | */ |
3b3b36dd | 722 | SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0, |
1bbd0b84 | 723 | (SCM port), |
1e6808ea MG |
724 | "Close the specified port object. Return @code{#t} if it\n" |
725 | "successfully closes a port or @code{#f} if it was already\n" | |
726 | "closed. An exception may be raised if an error occurs, for\n" | |
727 | "example when flushing buffered output. See also @ref{Ports and\n" | |
728 | "File Descriptors, close}, for a procedure which can close file\n" | |
729 | "descriptors.") | |
1bbd0b84 | 730 | #define FUNC_NAME s_scm_close_port |
0f2d19dd | 731 | { |
eadd48de GH |
732 | int rv; |
733 | ||
78446828 MV |
734 | port = SCM_COERCE_OUTPORT (port); |
735 | ||
7a754ca6 | 736 | SCM_VALIDATE_PORT (1, port); |
0f2d19dd | 737 | if (SCM_CLOSEDP (port)) |
eadd48de | 738 | return SCM_BOOL_F; |
62bd5d66 AW |
739 | if (SCM_PORT_DESCRIPTOR (port)->close) |
740 | rv = SCM_PORT_DESCRIPTOR (port)->close (port); | |
eadd48de GH |
741 | else |
742 | rv = 0; | |
5dbc6c06 | 743 | scm_i_remove_port (port); |
22a52da1 | 744 | SCM_CLR_PORT_OPEN_FLAG (port); |
7888309b | 745 | return scm_from_bool (rv >= 0); |
7a754ca6 MD |
746 | } |
747 | #undef FUNC_NAME | |
748 | ||
749 | SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0, | |
750 | (SCM port), | |
751 | "Close the specified input port object. The routine has no effect if\n" | |
752 | "the file has already been closed. An exception may be raised if an\n" | |
753 | "error occurs. The value returned is unspecified.\n\n" | |
754 | "See also @ref{Ports and File Descriptors, close}, for a procedure\n" | |
755 | "which can close file descriptors.") | |
756 | #define FUNC_NAME s_scm_close_input_port | |
757 | { | |
758 | SCM_VALIDATE_INPUT_PORT (1, port); | |
759 | scm_close_port (port); | |
760 | return SCM_UNSPECIFIED; | |
761 | } | |
762 | #undef FUNC_NAME | |
763 | ||
764 | SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0, | |
765 | (SCM port), | |
766 | "Close the specified output port object. The routine has no effect if\n" | |
767 | "the file has already been closed. An exception may be raised if an\n" | |
768 | "error occurs. The value returned is unspecified.\n\n" | |
769 | "See also @ref{Ports and File Descriptors, close}, for a procedure\n" | |
770 | "which can close file descriptors.") | |
771 | #define FUNC_NAME s_scm_close_output_port | |
772 | { | |
773 | port = SCM_COERCE_OUTPORT (port); | |
774 | SCM_VALIDATE_OUTPUT_PORT (1, port); | |
775 | scm_close_port (port); | |
776 | return SCM_UNSPECIFIED; | |
0f2d19dd | 777 | } |
1bbd0b84 | 778 | #undef FUNC_NAME |
0f2d19dd | 779 | |
2721f918 | 780 | |
19b8d12b | 781 | \f |
2721f918 | 782 | |
19b8d12b AW |
783 | /* Encoding characters to byte streams, and decoding byte streams to |
784 | characters. */ | |
5dbc6c06 | 785 | |
19b8d12b AW |
786 | /* A fluid specifying the default encoding for newly created ports. If it is |
787 | a string, that is the encoding. If it is #f, it is in the "native" | |
788 | (Latin-1) encoding. */ | |
789 | SCM_VARIABLE (default_port_encoding_var, "%default-port-encoding"); | |
790 | ||
791 | static int scm_port_encoding_init = 0; | |
792 | ||
793 | /* Use ENCODING as the default encoding for future ports. */ | |
c536b4b3 | 794 | void |
19b8d12b | 795 | scm_i_set_default_port_encoding (const char *encoding) |
c2ca4493 | 796 | { |
19b8d12b AW |
797 | if (!scm_port_encoding_init |
798 | || !scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var))) | |
799 | scm_misc_error (NULL, "tried to set port encoding fluid before it is initialized", | |
800 | SCM_EOL); | |
fdfe6305 | 801 | |
19b8d12b AW |
802 | if (encoding == NULL |
803 | || !strcmp (encoding, "ASCII") | |
804 | || !strcmp (encoding, "ANSI_X3.4-1968") | |
805 | || !strcmp (encoding, "ISO-8859-1")) | |
806 | scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F); | |
807 | else | |
808 | scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), | |
809 | scm_from_locale_string (encoding)); | |
2721f918 | 810 | } |
3a5fb14d | 811 | |
19b8d12b AW |
812 | /* Return the name of the default encoding for newly created ports; a |
813 | return value of NULL means "ISO-8859-1". */ | |
814 | const char * | |
815 | scm_i_default_port_encoding (void) | |
2721f918 | 816 | { |
19b8d12b AW |
817 | if (!scm_port_encoding_init) |
818 | return NULL; | |
819 | else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var))) | |
820 | return NULL; | |
821 | else | |
822 | { | |
823 | SCM encoding; | |
824 | ||
825 | encoding = scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var)); | |
826 | if (!scm_is_string (encoding)) | |
827 | return NULL; | |
828 | else | |
829 | return scm_i_string_chars (encoding); | |
830 | } | |
c536b4b3 | 831 | } |
fdfe6305 | 832 | |
19b8d12b AW |
833 | void |
834 | scm_i_set_port_encoding_x (SCM port, const char *encoding) | |
c536b4b3 | 835 | { |
19b8d12b AW |
836 | scm_t_port *pt; |
837 | iconv_t new_input_cd, new_output_cd; | |
c536b4b3 | 838 | |
19b8d12b AW |
839 | new_input_cd = (iconv_t) -1; |
840 | new_output_cd = (iconv_t) -1; | |
c2ca4493 | 841 | |
19b8d12b AW |
842 | /* Set the character encoding for this port. */ |
843 | pt = SCM_PTAB_ENTRY (port); | |
c536b4b3 | 844 | |
19b8d12b AW |
845 | if (encoding == NULL) |
846 | encoding = "ISO-8859-1"; | |
d68fee48 | 847 | |
19b8d12b AW |
848 | if (pt->encoding != encoding) |
849 | pt->encoding = scm_gc_strdup (encoding, "port"); | |
0f2d19dd | 850 | |
19b8d12b AW |
851 | /* If ENCODING is UTF-8, then no conversion descriptor is opened |
852 | because we do I/O ourselves. This saves 100+ KiB for each | |
853 | descriptor. */ | |
854 | if (strcmp (encoding, "UTF-8")) | |
855 | { | |
856 | if (SCM_CELL_WORD_0 (port) & SCM_RDNG) | |
857 | { | |
858 | /* Open an input iconv conversion descriptor, from ENCODING | |
859 | to UTF-8. We choose UTF-8, not UTF-32, because iconv | |
860 | implementations can typically convert from anything to | |
861 | UTF-8, but not to UTF-32 (see | |
862 | <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00007.html>). */ | |
863 | new_input_cd = iconv_open ("UTF-8", encoding); | |
864 | if (new_input_cd == (iconv_t) -1) | |
865 | goto invalid_encoding; | |
866 | } | |
0f2d19dd | 867 | |
19b8d12b AW |
868 | if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) |
869 | { | |
870 | new_output_cd = iconv_open (encoding, "UTF-8"); | |
871 | if (new_output_cd == (iconv_t) -1) | |
872 | { | |
873 | if (new_input_cd != (iconv_t) -1) | |
874 | iconv_close (new_input_cd); | |
875 | goto invalid_encoding; | |
876 | } | |
877 | } | |
878 | } | |
eb5c0a2a | 879 | |
19b8d12b AW |
880 | if (pt->input_cd != (iconv_t) -1) |
881 | iconv_close (pt->input_cd); | |
882 | if (pt->output_cd != (iconv_t) -1) | |
883 | iconv_close (pt->output_cd); | |
0f2d19dd | 884 | |
19b8d12b AW |
885 | pt->input_cd = new_input_cd; |
886 | pt->output_cd = new_output_cd; | |
887 | ||
888 | return; | |
889 | ||
890 | invalid_encoding: | |
891 | { | |
892 | SCM err; | |
893 | err = scm_from_locale_string (encoding); | |
894 | scm_misc_error ("scm_i_set_port_encoding_x", | |
895 | "invalid or unknown character encoding ~s", | |
896 | scm_list_1 (err)); | |
897 | } | |
0f2d19dd JB |
898 | } |
899 | ||
19b8d12b AW |
900 | SCM_DEFINE (scm_port_encoding, "port-encoding", 1, 0, 0, |
901 | (SCM port), | |
902 | "Returns, as a string, the character encoding that @var{port}\n" | |
903 | "uses to interpret its input and output.\n") | |
904 | #define FUNC_NAME s_scm_port_encoding | |
0f2d19dd | 905 | { |
19b8d12b AW |
906 | scm_t_port *pt; |
907 | const char *enc; | |
908 | ||
909 | SCM_VALIDATE_PORT (1, port); | |
910 | ||
911 | pt = SCM_PTAB_ENTRY (port); | |
912 | enc = pt->encoding; | |
913 | if (enc) | |
914 | return scm_from_locale_string (pt->encoding); | |
0f2d19dd | 915 | else |
19b8d12b | 916 | return SCM_BOOL_F; |
0f2d19dd | 917 | } |
1bbd0b84 | 918 | #undef FUNC_NAME |
0f2d19dd | 919 | |
19b8d12b AW |
920 | SCM_DEFINE (scm_set_port_encoding_x, "set-port-encoding!", 2, 0, 0, |
921 | (SCM port, SCM enc), | |
922 | "Sets the character encoding that will be used to interpret all\n" | |
923 | "port I/O. New ports are created with the encoding\n" | |
924 | "appropriate for the current locale if @code{setlocale} has \n" | |
925 | "been called or ISO-8859-1 otherwise\n" | |
926 | "and this procedure can be used to modify that encoding.\n") | |
927 | #define FUNC_NAME s_scm_set_port_encoding_x | |
5dbc6c06 | 928 | { |
19b8d12b | 929 | char *enc_str; |
5dbc6c06 | 930 | |
19b8d12b AW |
931 | SCM_VALIDATE_PORT (1, port); |
932 | SCM_VALIDATE_STRING (2, enc); | |
0f2d19dd | 933 | |
19b8d12b AW |
934 | enc_str = scm_to_locale_string (enc); |
935 | scm_i_set_port_encoding_x (port, enc_str); | |
936 | free (enc_str); | |
937 | ||
938 | return SCM_UNSPECIFIED; | |
0f2d19dd | 939 | } |
1bbd0b84 | 940 | #undef FUNC_NAME |
0f2d19dd | 941 | |
f4bc4e59 | 942 | |
19b8d12b AW |
943 | /* This determines how conversions handle unconvertible characters. */ |
944 | SCM_GLOBAL_VARIABLE (scm_conversion_strategy, "%port-conversion-strategy"); | |
945 | static int scm_conversion_strategy_init = 0; | |
889975e5 | 946 | |
19b8d12b AW |
947 | scm_t_string_failed_conversion_handler |
948 | scm_i_get_conversion_strategy (SCM port) | |
f4bc4e59 | 949 | { |
19b8d12b AW |
950 | SCM encoding; |
951 | ||
952 | if (scm_is_false (port)) | |
f4bc4e59 | 953 | { |
19b8d12b AW |
954 | if (!scm_conversion_strategy_init |
955 | || !scm_is_fluid (SCM_VARIABLE_REF (scm_conversion_strategy))) | |
956 | return SCM_FAILED_CONVERSION_QUESTION_MARK; | |
957 | else | |
958 | { | |
959 | encoding = scm_fluid_ref (SCM_VARIABLE_REF (scm_conversion_strategy)); | |
960 | if (scm_is_false (encoding)) | |
961 | return SCM_FAILED_CONVERSION_QUESTION_MARK; | |
962 | else | |
963 | return (scm_t_string_failed_conversion_handler) scm_to_int (encoding); | |
964 | } | |
f4bc4e59 | 965 | } |
19b8d12b | 966 | else |
f4bc4e59 | 967 | { |
19b8d12b AW |
968 | scm_t_port *pt; |
969 | pt = SCM_PTAB_ENTRY (port); | |
970 | return pt->ilseq_handler; | |
f4bc4e59 | 971 | } |
19b8d12b AW |
972 | |
973 | } | |
974 | ||
975 | void | |
976 | scm_i_set_conversion_strategy_x (SCM port, | |
977 | scm_t_string_failed_conversion_handler handler) | |
978 | { | |
979 | SCM strategy; | |
980 | scm_t_port *pt; | |
981 | ||
982 | strategy = scm_from_int ((int) handler); | |
983 | ||
984 | if (scm_is_false (port)) | |
f4bc4e59 | 985 | { |
19b8d12b AW |
986 | /* Set the default encoding for future ports. */ |
987 | if (!scm_conversion_strategy_init | |
988 | || !scm_is_fluid (SCM_VARIABLE_REF (scm_conversion_strategy))) | |
989 | scm_misc_error (NULL, "tried to set conversion strategy fluid before it is initialized", | |
990 | SCM_EOL); | |
991 | scm_fluid_set_x (SCM_VARIABLE_REF (scm_conversion_strategy), strategy); | |
f4bc4e59 LC |
992 | } |
993 | else | |
994 | { | |
19b8d12b AW |
995 | /* Set the character encoding for this port. */ |
996 | pt = SCM_PTAB_ENTRY (port); | |
997 | pt->ilseq_handler = handler; | |
f4bc4e59 | 998 | } |
f4bc4e59 LC |
999 | } |
1000 | ||
19b8d12b AW |
1001 | SCM_DEFINE (scm_port_conversion_strategy, "port-conversion-strategy", |
1002 | 1, 0, 0, (SCM port), | |
1003 | "Returns the behavior of the port when handling a character that\n" | |
1004 | "is not representable in the port's current encoding.\n" | |
1005 | "It returns the symbol @code{error} if unrepresentable characters\n" | |
1006 | "should cause exceptions, @code{substitute} if the port should\n" | |
1007 | "try to replace unrepresentable characters with question marks or\n" | |
1008 | "approximate characters, or @code{escape} if unrepresentable\n" | |
1009 | "characters should be converted to string escapes.\n" | |
1010 | "\n" | |
1011 | "If @var{port} is @code{#f}, then the current default behavior\n" | |
1012 | "will be returned. New ports will have this default behavior\n" | |
1013 | "when they are created.\n") | |
1014 | #define FUNC_NAME s_scm_port_conversion_strategy | |
889975e5 | 1015 | { |
19b8d12b | 1016 | scm_t_string_failed_conversion_handler h; |
7b292a9d | 1017 | |
19b8d12b | 1018 | SCM_VALIDATE_OPPORT (1, port); |
7b292a9d | 1019 | |
19b8d12b | 1020 | if (!scm_is_false (port)) |
7b292a9d | 1021 | { |
19b8d12b | 1022 | SCM_VALIDATE_OPPORT (1, port); |
7b292a9d LC |
1023 | } |
1024 | ||
19b8d12b AW |
1025 | h = scm_i_get_conversion_strategy (port); |
1026 | if (h == SCM_FAILED_CONVERSION_ERROR) | |
1027 | return scm_from_latin1_symbol ("error"); | |
1028 | else if (h == SCM_FAILED_CONVERSION_QUESTION_MARK) | |
1029 | return scm_from_latin1_symbol ("substitute"); | |
1030 | else if (h == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE) | |
1031 | return scm_from_latin1_symbol ("escape"); | |
1032 | else | |
1033 | abort (); | |
7b292a9d | 1034 | |
19b8d12b AW |
1035 | /* Never gets here. */ |
1036 | return SCM_UNDEFINED; | |
1037 | } | |
1038 | #undef FUNC_NAME | |
7b292a9d | 1039 | |
19b8d12b AW |
1040 | SCM_DEFINE (scm_set_port_conversion_strategy_x, "set-port-conversion-strategy!", |
1041 | 2, 0, 0, | |
1042 | (SCM port, SCM sym), | |
1043 | "Sets the behavior of the interpreter when outputting a character\n" | |
1044 | "that is not representable in the port's current encoding.\n" | |
1045 | "@var{sym} can be either @code{'error}, @code{'substitute}, or\n" | |
1046 | "@code{'escape}. If it is @code{'error}, an error will be thrown\n" | |
1047 | "when an unconvertible character is encountered. If it is\n" | |
1048 | "@code{'substitute}, then unconvertible characters will \n" | |
1049 | "be replaced with approximate characters, or with question marks\n" | |
1050 | "if no approximately correct character is available.\n" | |
1051 | "If it is @code{'escape},\n" | |
1052 | "it will appear as a hex escape when output.\n" | |
1053 | "\n" | |
1054 | "If @var{port} is an open port, the conversion error behavior\n" | |
1055 | "is set for that port. If it is @code{#f}, it is set as the\n" | |
1056 | "default behavior for any future ports that get created in\n" | |
1057 | "this thread.\n") | |
1058 | #define FUNC_NAME s_scm_set_port_conversion_strategy_x | |
1059 | { | |
1060 | SCM err; | |
1061 | SCM qm; | |
1062 | SCM esc; | |
7be1705d | 1063 | |
19b8d12b | 1064 | if (!scm_is_false (port)) |
7b292a9d | 1065 | { |
19b8d12b AW |
1066 | SCM_VALIDATE_OPPORT (1, port); |
1067 | } | |
7b292a9d | 1068 | |
19b8d12b AW |
1069 | err = scm_from_latin1_symbol ("error"); |
1070 | if (scm_is_true (scm_eqv_p (sym, err))) | |
1071 | { | |
1072 | scm_i_set_conversion_strategy_x (port, SCM_FAILED_CONVERSION_ERROR); | |
1073 | return SCM_UNSPECIFIED; | |
1074 | } | |
7b292a9d | 1075 | |
19b8d12b AW |
1076 | qm = scm_from_latin1_symbol ("substitute"); |
1077 | if (scm_is_true (scm_eqv_p (sym, qm))) | |
1078 | { | |
1079 | scm_i_set_conversion_strategy_x (port, | |
1080 | SCM_FAILED_CONVERSION_QUESTION_MARK); | |
1081 | return SCM_UNSPECIFIED; | |
1082 | } | |
7b292a9d | 1083 | |
19b8d12b AW |
1084 | esc = scm_from_latin1_symbol ("escape"); |
1085 | if (scm_is_true (scm_eqv_p (sym, esc))) | |
1086 | { | |
1087 | scm_i_set_conversion_strategy_x (port, | |
1088 | SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE); | |
1089 | return SCM_UNSPECIFIED; | |
1090 | } | |
7b292a9d | 1091 | |
19b8d12b | 1092 | SCM_MISC_ERROR ("unknown conversion behavior ~s", scm_list_1 (sym)); |
7b292a9d | 1093 | |
19b8d12b AW |
1094 | return SCM_UNSPECIFIED; |
1095 | } | |
1096 | #undef FUNC_NAME | |
7be1705d | 1097 | |
7b292a9d | 1098 | |
19b8d12b | 1099 | \f |
7b292a9d | 1100 | |
19b8d12b | 1101 | /* Revealed counts --- an oddity inherited from SCSH. */ |
7be1705d | 1102 | |
19b8d12b AW |
1103 | /* Find a port in the table and return its revealed count. |
1104 | Also used by the garbage collector. | |
1105 | */ | |
1106 | int | |
1107 | scm_revealed_count (SCM port) | |
1108 | { | |
b262d306 AW |
1109 | int ret; |
1110 | ||
1111 | scm_c_lock_port (port); | |
1112 | ret = SCM_REVEALED (port); | |
1113 | scm_c_unlock_port (port); | |
1114 | ||
1115 | return ret; | |
19b8d12b | 1116 | } |
7b292a9d | 1117 | |
19b8d12b AW |
1118 | SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0, |
1119 | (SCM port), | |
1120 | "Return the revealed count for @var{port}.") | |
1121 | #define FUNC_NAME s_scm_port_revealed | |
1122 | { | |
1123 | port = SCM_COERCE_OUTPORT (port); | |
1124 | SCM_VALIDATE_OPENPORT (1, port); | |
1125 | return scm_from_int (scm_revealed_count (port)); | |
1126 | } | |
1127 | #undef FUNC_NAME | |
7be1705d | 1128 | |
19b8d12b AW |
1129 | /* Set the revealed count for a port. */ |
1130 | SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0, | |
1131 | (SCM port, SCM rcount), | |
1132 | "Sets the revealed count for a port to a given value.\n" | |
1133 | "The return value is unspecified.") | |
1134 | #define FUNC_NAME s_scm_set_port_revealed_x | |
1135 | { | |
b262d306 AW |
1136 | int r; |
1137 | port = SCM_COERCE_OUTPORT (port); | |
1138 | SCM_VALIDATE_OPENPORT (1, port); | |
1139 | r = scm_to_int (rcount); | |
1140 | scm_c_lock_port (port); | |
1141 | SCM_REVEALED (port) = r; | |
1142 | scm_c_unlock_port (port); | |
1143 | return SCM_UNSPECIFIED; | |
1144 | } | |
1145 | #undef FUNC_NAME | |
1146 | ||
1147 | /* Set the revealed count for a port. */ | |
1148 | SCM_DEFINE (scm_adjust_port_revealed_x, "adjust-port-revealed!", 2, 0, 0, | |
1149 | (SCM port, SCM addend), | |
1150 | "Add @var{addend} to the revealed count of @var{port}.\n" | |
1151 | "The return value is unspecified.") | |
1152 | #define FUNC_NAME s_scm_set_port_revealed_x | |
1153 | { | |
1154 | int a; | |
19b8d12b AW |
1155 | port = SCM_COERCE_OUTPORT (port); |
1156 | SCM_VALIDATE_OPENPORT (1, port); | |
b262d306 AW |
1157 | a = scm_to_int (addend); |
1158 | scm_c_lock_port (port); | |
1159 | SCM_REVEALED (port) += a; | |
1160 | scm_c_unlock_port (port); | |
19b8d12b AW |
1161 | return SCM_UNSPECIFIED; |
1162 | } | |
1163 | #undef FUNC_NAME | |
7b292a9d | 1164 | |
7b292a9d | 1165 | |
19b8d12b | 1166 | \f |
7b292a9d | 1167 | |
19b8d12b | 1168 | /* Input. */ |
7be1705d | 1169 | |
19b8d12b AW |
1170 | /* scm_c_read |
1171 | * | |
1172 | * Used by an application to read arbitrary number of bytes from an | |
1173 | * SCM port. Same semantics as libc read, except that scm_c_read only | |
1174 | * returns less than SIZE bytes if at end-of-file. | |
1175 | * | |
1176 | * Warning: Doesn't update port line and column counts! */ | |
7b292a9d | 1177 | |
19b8d12b AW |
1178 | /* This structure, and the following swap_buffer function, are used |
1179 | for temporarily swapping a port's own read buffer, and the buffer | |
1180 | that the caller of scm_c_read provides. */ | |
1181 | struct port_and_swap_buffer | |
1182 | { | |
1183 | scm_t_port *pt; | |
1184 | unsigned char *buffer; | |
1185 | size_t size; | |
1186 | }; | |
7b292a9d | 1187 | |
19b8d12b AW |
1188 | static void |
1189 | swap_buffer (void *data) | |
1190 | { | |
1191 | struct port_and_swap_buffer *psb = (struct port_and_swap_buffer *) data; | |
1192 | unsigned char *old_buf = psb->pt->read_buf; | |
1193 | size_t old_size = psb->pt->read_buf_size; | |
7be1705d | 1194 | |
19b8d12b AW |
1195 | /* Make the port use (buffer, size) from the struct. */ |
1196 | psb->pt->read_pos = psb->pt->read_buf = psb->pt->read_end = psb->buffer; | |
1197 | psb->pt->read_buf_size = psb->size; | |
7b292a9d | 1198 | |
19b8d12b AW |
1199 | /* Save the port's old (buffer, size) in the struct. */ |
1200 | psb->buffer = old_buf; | |
1201 | psb->size = old_size; | |
7b292a9d LC |
1202 | } |
1203 | ||
19b8d12b AW |
1204 | size_t |
1205 | scm_c_read (SCM port, void *buffer, size_t size) | |
1206 | #define FUNC_NAME "scm_c_read" | |
7b292a9d LC |
1207 | { |
1208 | scm_t_port *pt; | |
19b8d12b AW |
1209 | size_t n_read = 0, n_available; |
1210 | struct port_and_swap_buffer psb; | |
889975e5 | 1211 | |
19b8d12b | 1212 | SCM_VALIDATE_OPINPORT (1, port); |
889975e5 | 1213 | |
19b8d12b AW |
1214 | pt = SCM_PTAB_ENTRY (port); |
1215 | if (pt->rw_active == SCM_PORT_WRITE) | |
1216 | SCM_PORT_DESCRIPTOR (port)->flush (port); | |
889975e5 | 1217 | |
19b8d12b AW |
1218 | if (pt->rw_random) |
1219 | pt->rw_active = SCM_PORT_READ; | |
889975e5 | 1220 | |
19b8d12b AW |
1221 | /* Take bytes first from the port's read buffer. */ |
1222 | if (pt->read_pos < pt->read_end) | |
1223 | { | |
1224 | n_available = min (size, pt->read_end - pt->read_pos); | |
1225 | memcpy (buffer, pt->read_pos, n_available); | |
1226 | buffer = (char *) buffer + n_available; | |
1227 | pt->read_pos += n_available; | |
1228 | n_read += n_available; | |
1229 | size -= n_available; | |
1230 | } | |
889975e5 | 1231 | |
19b8d12b AW |
1232 | /* Avoid the scm_dynwind_* costs if we now have enough data. */ |
1233 | if (size == 0) | |
1234 | return n_read; | |
b5cb4464 NJ |
1235 | |
1236 | /* Now we will call scm_fill_input repeatedly until we have read the | |
1237 | requested number of bytes. (Note that a single scm_fill_input | |
1238 | call does not guarantee to fill the whole of the port's read | |
6d227556 | 1239 | buffer.) */ |
75192345 | 1240 | if (pt->read_buf_size <= 1 && pt->encoding == NULL) |
b5cb4464 | 1241 | { |
6d227556 NJ |
1242 | /* The port that we are reading from is unbuffered - i.e. does |
1243 | not have its own persistent buffer - but we have a buffer, | |
1244 | provided by our caller, that is the right size for the data | |
1245 | that is wanted. For the following scm_fill_input calls, | |
1246 | therefore, we use the buffer in hand as the port's read | |
1247 | buffer. | |
1248 | ||
1249 | We need to make sure that the port's normal (1 byte) buffer | |
1250 | is reinstated in case one of the scm_fill_input () calls | |
1251 | throws an exception; we use the scm_dynwind_* API to achieve | |
75192345 MG |
1252 | that. |
1253 | ||
1254 | A consequence of this optimization is that the fill_input | |
1255 | functions can't unget characters. That'll push data to the | |
1256 | pushback buffer instead of this psb buffer. */ | |
1257 | #if SCM_DEBUG == 1 | |
1258 | unsigned char *pback = pt->putback_buf; | |
1259 | #endif | |
6d227556 NJ |
1260 | psb.pt = pt; |
1261 | psb.buffer = buffer; | |
1262 | psb.size = size; | |
1263 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); | |
1264 | scm_dynwind_rewind_handler (swap_buffer, &psb, SCM_F_WIND_EXPLICITLY); | |
1265 | scm_dynwind_unwind_handler (swap_buffer, &psb, SCM_F_WIND_EXPLICITLY); | |
1266 | ||
1267 | /* Call scm_fill_input until we have all the bytes that we need, | |
1268 | or we hit EOF. */ | |
1269 | while (pt->read_buf_size && (scm_fill_input (port) != EOF)) | |
1270 | { | |
1271 | pt->read_buf_size -= (pt->read_end - pt->read_pos); | |
1272 | pt->read_pos = pt->read_buf = pt->read_end; | |
1273 | } | |
75192345 MG |
1274 | #if SCM_DEBUG == 1 |
1275 | if (pback != pt->putback_buf | |
1276 | || pt->read_buf - (unsigned char *) buffer < 0) | |
1277 | scm_misc_error (FUNC_NAME, | |
1278 | "scm_c_read must not call a fill function that pushes " | |
1279 | "back characters onto an unbuffered port", SCM_EOL); | |
1280 | #endif | |
6d227556 | 1281 | n_read += pt->read_buf - (unsigned char *) buffer; |
75192345 | 1282 | |
6d227556 NJ |
1283 | /* Reinstate the port's normal buffer. */ |
1284 | scm_dynwind_end (); | |
1285 | } | |
1286 | else | |
1287 | { | |
1288 | /* The port has its own buffer. It is important that we use it, | |
1289 | even if it happens to be smaller than our caller's buffer, so | |
1290 | that a custom port implementation's entry points (in | |
1291 | particular, fill_input) can rely on the buffer always being | |
1292 | the same as they first set up. */ | |
1293 | while (size && (scm_fill_input (port) != EOF)) | |
1294 | { | |
1295 | n_available = min (size, pt->read_end - pt->read_pos); | |
1296 | memcpy (buffer, pt->read_pos, n_available); | |
1297 | buffer = (char *) buffer + n_available; | |
1298 | pt->read_pos += n_available; | |
1299 | n_read += n_available; | |
1300 | size -= n_available; | |
1301 | } | |
1302 | } | |
6fe692e9 | 1303 | |
b5cb4464 | 1304 | return n_read; |
6fe692e9 | 1305 | } |
693758d5 | 1306 | #undef FUNC_NAME |
6fe692e9 | 1307 | |
19b8d12b AW |
1308 | /* Update the line and column number of PORT after consumption of C. */ |
1309 | static inline void | |
1310 | update_port_lf (scm_t_wchar c, SCM port) | |
6fe692e9 | 1311 | { |
19b8d12b AW |
1312 | switch (c) |
1313 | { | |
1314 | case '\a': | |
1315 | case EOF: | |
1316 | break; | |
1317 | case '\b': | |
1318 | SCM_DECCOL (port); | |
1319 | break; | |
1320 | case '\n': | |
1321 | SCM_INCLINE (port); | |
1322 | break; | |
1323 | case '\r': | |
1324 | SCM_ZEROCOL (port); | |
1325 | break; | |
1326 | case '\t': | |
1327 | SCM_TABCOL (port); | |
1328 | break; | |
1329 | default: | |
1330 | SCM_INCCOL (port); | |
1331 | break; | |
1332 | } | |
1333 | } | |
6fe692e9 | 1334 | |
19b8d12b | 1335 | #define SCM_MBCHAR_BUF_SIZE (4) |
6fe692e9 | 1336 | |
19b8d12b AW |
1337 | /* Convert the SIZE-byte UTF-8 sequence in UTF8_BUF to a codepoint. |
1338 | UTF8_BUF is assumed to contain a valid UTF-8 sequence. */ | |
1339 | static scm_t_wchar | |
1340 | utf8_to_codepoint (const scm_t_uint8 *utf8_buf, size_t size) | |
283a1a0e | 1341 | { |
19b8d12b | 1342 | scm_t_wchar codepoint; |
283a1a0e | 1343 | |
19b8d12b | 1344 | if (utf8_buf[0] <= 0x7f) |
283a1a0e | 1345 | { |
19b8d12b AW |
1346 | assert (size == 1); |
1347 | codepoint = utf8_buf[0]; | |
1348 | } | |
1349 | else if ((utf8_buf[0] & 0xe0) == 0xc0) | |
1350 | { | |
1351 | assert (size == 2); | |
1352 | codepoint = ((scm_t_wchar) utf8_buf[0] & 0x1f) << 6UL | |
1353 | | (utf8_buf[1] & 0x3f); | |
1354 | } | |
1355 | else if ((utf8_buf[0] & 0xf0) == 0xe0) | |
1356 | { | |
1357 | assert (size == 3); | |
1358 | codepoint = ((scm_t_wchar) utf8_buf[0] & 0x0f) << 12UL | |
1359 | | ((scm_t_wchar) utf8_buf[1] & 0x3f) << 6UL | |
1360 | | (utf8_buf[2] & 0x3f); | |
283a1a0e GH |
1361 | } |
1362 | else | |
19b8d12b AW |
1363 | { |
1364 | assert (size == 4); | |
1365 | codepoint = ((scm_t_wchar) utf8_buf[0] & 0x07) << 18UL | |
1366 | | ((scm_t_wchar) utf8_buf[1] & 0x3f) << 12UL | |
1367 | | ((scm_t_wchar) utf8_buf[2] & 0x3f) << 6UL | |
1368 | | (utf8_buf[3] & 0x3f); | |
1369 | } | |
283a1a0e | 1370 | |
19b8d12b | 1371 | return codepoint; |
283a1a0e GH |
1372 | } |
1373 | ||
19b8d12b AW |
1374 | /* Read a UTF-8 sequence from PORT. On success, return 0 and set |
1375 | *CODEPOINT to the codepoint that was read, fill BUF with its UTF-8 | |
1376 | representation, and set *LEN to the length in bytes. Return | |
1377 | `EILSEQ' on error. */ | |
1378 | static int | |
1379 | get_utf8_codepoint (SCM port, scm_t_wchar *codepoint, | |
1380 | scm_t_uint8 buf[SCM_MBCHAR_BUF_SIZE], size_t *len) | |
1381 | { | |
1382 | #define ASSERT_NOT_EOF(b) \ | |
1383 | if (SCM_UNLIKELY ((b) == EOF)) \ | |
1384 | goto invalid_seq | |
1385 | #define CONSUME_PEEKED_BYTE() \ | |
1386 | pt->read_pos++ | |
ee149d03 | 1387 | |
19b8d12b AW |
1388 | int byte; |
1389 | scm_t_port *pt; | |
ee149d03 | 1390 | |
19b8d12b AW |
1391 | *len = 0; |
1392 | pt = SCM_PTAB_ENTRY (port); | |
840ae05d | 1393 | |
19b8d12b AW |
1394 | byte = scm_get_byte_or_eof (port); |
1395 | if (byte == EOF) | |
6c951427 | 1396 | { |
19b8d12b AW |
1397 | *codepoint = EOF; |
1398 | return 0; | |
1399 | } | |
6c951427 | 1400 | |
19b8d12b AW |
1401 | buf[0] = (scm_t_uint8) byte; |
1402 | *len = 1; | |
6c951427 | 1403 | |
19b8d12b AW |
1404 | if (buf[0] <= 0x7f) |
1405 | /* 1-byte form. */ | |
1406 | *codepoint = buf[0]; | |
1407 | else if (buf[0] >= 0xc2 && buf[0] <= 0xdf) | |
1408 | { | |
1409 | /* 2-byte form. */ | |
1410 | byte = scm_peek_byte_or_eof (port); | |
1411 | ASSERT_NOT_EOF (byte); | |
6c951427 | 1412 | |
19b8d12b AW |
1413 | if (SCM_UNLIKELY ((byte & 0xc0) != 0x80)) |
1414 | goto invalid_seq; | |
6c951427 | 1415 | |
19b8d12b AW |
1416 | CONSUME_PEEKED_BYTE (); |
1417 | buf[1] = (scm_t_uint8) byte; | |
1418 | *len = 2; | |
1419 | ||
1420 | *codepoint = ((scm_t_wchar) buf[0] & 0x1f) << 6UL | |
1421 | | (buf[1] & 0x3f); | |
6c951427 | 1422 | } |
19b8d12b | 1423 | else if ((buf[0] & 0xf0) == 0xe0) |
6c951427 | 1424 | { |
19b8d12b AW |
1425 | /* 3-byte form. */ |
1426 | byte = scm_peek_byte_or_eof (port); | |
1427 | ASSERT_NOT_EOF (byte); | |
6c951427 | 1428 | |
19b8d12b AW |
1429 | if (SCM_UNLIKELY ((byte & 0xc0) != 0x80 |
1430 | || (buf[0] == 0xe0 && byte < 0xa0) | |
1431 | || (buf[0] == 0xed && byte > 0x9f))) | |
1432 | goto invalid_seq; | |
6c951427 | 1433 | |
19b8d12b AW |
1434 | CONSUME_PEEKED_BYTE (); |
1435 | buf[1] = (scm_t_uint8) byte; | |
1436 | *len = 2; | |
1437 | ||
1438 | byte = scm_peek_byte_or_eof (port); | |
1439 | ASSERT_NOT_EOF (byte); | |
1440 | ||
1441 | if (SCM_UNLIKELY ((byte & 0xc0) != 0x80)) | |
1442 | goto invalid_seq; | |
1443 | ||
1444 | CONSUME_PEEKED_BYTE (); | |
1445 | buf[2] = (scm_t_uint8) byte; | |
1446 | *len = 3; | |
1447 | ||
1448 | *codepoint = ((scm_t_wchar) buf[0] & 0x0f) << 12UL | |
1449 | | ((scm_t_wchar) buf[1] & 0x3f) << 6UL | |
1450 | | (buf[2] & 0x3f); | |
6c951427 | 1451 | } |
19b8d12b AW |
1452 | else if (buf[0] >= 0xf0 && buf[0] <= 0xf4) |
1453 | { | |
1454 | /* 4-byte form. */ | |
1455 | byte = scm_peek_byte_or_eof (port); | |
1456 | ASSERT_NOT_EOF (byte); | |
6c951427 | 1457 | |
19b8d12b AW |
1458 | if (SCM_UNLIKELY (((byte & 0xc0) != 0x80) |
1459 | || (buf[0] == 0xf0 && byte < 0x90) | |
1460 | || (buf[0] == 0xf4 && byte > 0x8f))) | |
1461 | goto invalid_seq; | |
ee149d03 | 1462 | |
19b8d12b AW |
1463 | CONSUME_PEEKED_BYTE (); |
1464 | buf[1] = (scm_t_uint8) byte; | |
1465 | *len = 2; | |
889975e5 | 1466 | |
19b8d12b AW |
1467 | byte = scm_peek_byte_or_eof (port); |
1468 | ASSERT_NOT_EOF (byte); | |
889975e5 | 1469 | |
19b8d12b AW |
1470 | if (SCM_UNLIKELY ((byte & 0xc0) != 0x80)) |
1471 | goto invalid_seq; | |
63479112 | 1472 | |
19b8d12b AW |
1473 | CONSUME_PEEKED_BYTE (); |
1474 | buf[2] = (scm_t_uint8) byte; | |
1475 | *len = 3; | |
63479112 | 1476 | |
19b8d12b AW |
1477 | byte = scm_peek_byte_or_eof (port); |
1478 | ASSERT_NOT_EOF (byte); | |
63479112 | 1479 | |
19b8d12b AW |
1480 | if (SCM_UNLIKELY ((byte & 0xc0) != 0x80)) |
1481 | goto invalid_seq; | |
63479112 | 1482 | |
19b8d12b AW |
1483 | CONSUME_PEEKED_BYTE (); |
1484 | buf[3] = (scm_t_uint8) byte; | |
1485 | *len = 4; | |
840ae05d | 1486 | |
19b8d12b AW |
1487 | *codepoint = ((scm_t_wchar) buf[0] & 0x07) << 18UL |
1488 | | ((scm_t_wchar) buf[1] & 0x3f) << 12UL | |
1489 | | ((scm_t_wchar) buf[2] & 0x3f) << 6UL | |
1490 | | (buf[3] & 0x3f); | |
ee149d03 JB |
1491 | } |
1492 | else | |
19b8d12b | 1493 | goto invalid_seq; |
ee149d03 | 1494 | |
19b8d12b | 1495 | return 0; |
ee149d03 | 1496 | |
19b8d12b AW |
1497 | invalid_seq: |
1498 | /* Here we could choose the consume the faulty byte when it's not a | |
1499 | valid starting byte, but it's not a requirement. What Section 3.9 | |
1500 | of Unicode 6.0.0 mandates, though, is to not consume a byte that | |
1501 | would otherwise be a valid starting byte. */ | |
ee149d03 | 1502 | |
19b8d12b | 1503 | return EILSEQ; |
ee149d03 | 1504 | |
19b8d12b AW |
1505 | #undef CONSUME_PEEKED_BYTE |
1506 | #undef ASSERT_NOT_EOF | |
1507 | } | |
1508 | ||
1509 | /* Likewise, read a byte sequence from PORT, passing it through its | |
1510 | input conversion descriptor. */ | |
1511 | static int | |
1512 | get_iconv_codepoint (SCM port, scm_t_wchar *codepoint, | |
1513 | char buf[SCM_MBCHAR_BUF_SIZE], size_t *len) | |
1514 | { | |
1515 | scm_t_port *pt; | |
1516 | int err, byte_read; | |
1517 | size_t bytes_consumed, output_size; | |
1518 | char *output; | |
1519 | scm_t_uint8 utf8_buf[SCM_MBCHAR_BUF_SIZE]; | |
1520 | ||
1521 | pt = SCM_PTAB_ENTRY (port); | |
1522 | ||
1523 | for (output_size = 0, output = (char *) utf8_buf, | |
1524 | bytes_consumed = 0, err = 0; | |
1525 | err == 0 && output_size == 0 | |
1526 | && (bytes_consumed == 0 || byte_read != EOF); | |
1527 | bytes_consumed++) | |
1528 | { | |
1529 | char *input; | |
1530 | size_t input_left, output_left, done; | |
1531 | ||
1532 | byte_read = scm_get_byte_or_eof (port); | |
1533 | if (byte_read == EOF) | |
1534 | { | |
1535 | if (bytes_consumed == 0) | |
1536 | { | |
1537 | *codepoint = (scm_t_wchar) EOF; | |
1538 | *len = 0; | |
1539 | return 0; | |
1540 | } | |
1541 | else | |
1542 | continue; | |
1543 | } | |
1544 | ||
1545 | buf[bytes_consumed] = byte_read; | |
1546 | ||
1547 | input = buf; | |
1548 | input_left = bytes_consumed + 1; | |
1549 | output_left = sizeof (utf8_buf); | |
1550 | ||
1551 | done = iconv (pt->input_cd, &input, &input_left, | |
1552 | &output, &output_left); | |
1553 | if (done == (size_t) -1) | |
1554 | { | |
1555 | err = errno; | |
1556 | if (err == EINVAL) | |
1557 | /* Missing input: keep trying. */ | |
1558 | err = 0; | |
1559 | } | |
1560 | else | |
1561 | output_size = sizeof (utf8_buf) - output_left; | |
1562 | } | |
1563 | ||
1564 | if (SCM_UNLIKELY (output_size == 0)) | |
1565 | /* An unterminated sequence. */ | |
1566 | err = EILSEQ; | |
1567 | else if (SCM_LIKELY (err == 0)) | |
1568 | { | |
1569 | /* Convert the UTF8_BUF sequence to a Unicode code point. */ | |
1570 | *codepoint = utf8_to_codepoint (utf8_buf, output_size); | |
1571 | *len = bytes_consumed; | |
1572 | } | |
1573 | ||
1574 | return err; | |
1575 | } | |
1576 | ||
1577 | /* Read a codepoint from PORT and return it in *CODEPOINT. Fill BUF | |
1578 | with the byte representation of the codepoint in PORT's encoding, and | |
1579 | set *LEN to the length in bytes of that representation. Return 0 on | |
1580 | success and an errno value on error. */ | |
1581 | static int | |
1582 | get_codepoint (SCM port, scm_t_wchar *codepoint, | |
1583 | char buf[SCM_MBCHAR_BUF_SIZE], size_t *len) | |
1584 | { | |
1585 | int err; | |
1586 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
1587 | ||
1588 | if (pt->input_cd == (iconv_t) -1) | |
1589 | /* Initialize the conversion descriptors, if needed. */ | |
1590 | scm_i_set_port_encoding_x (port, pt->encoding); | |
1591 | ||
1592 | /* FIXME: In 2.1, add a flag to determine whether a port is UTF-8. */ | |
1593 | if (pt->input_cd == (iconv_t) -1) | |
1594 | err = get_utf8_codepoint (port, codepoint, (scm_t_uint8 *) buf, len); | |
1595 | else | |
1596 | err = get_iconv_codepoint (port, codepoint, buf, len); | |
1597 | ||
1598 | if (SCM_LIKELY (err == 0)) | |
1599 | update_port_lf (*codepoint, port); | |
1600 | else if (pt->ilseq_handler == SCM_ICONVEH_QUESTION_MARK) | |
1601 | { | |
1602 | *codepoint = '?'; | |
1603 | err = 0; | |
1604 | update_port_lf (*codepoint, port); | |
1605 | } | |
1606 | ||
1607 | return err; | |
1608 | } | |
1609 | ||
1610 | /* Read a codepoint from PORT and return it. */ | |
1611 | scm_t_wchar | |
1612 | scm_getc (SCM port) | |
1613 | #define FUNC_NAME "scm_getc" | |
1614 | { | |
1615 | int err; | |
1616 | size_t len; | |
1617 | scm_t_wchar codepoint; | |
1618 | char buf[SCM_MBCHAR_BUF_SIZE]; | |
1619 | ||
1620 | err = get_codepoint (port, &codepoint, buf, &len); | |
1621 | if (SCM_UNLIKELY (err != 0)) | |
1622 | /* At this point PORT should point past the invalid encoding, as per | |
1623 | R6RS-lib Section 8.2.4. */ | |
1624 | scm_decoding_error (FUNC_NAME, err, "input decoding error", port); | |
1625 | ||
1626 | return codepoint; | |
1627 | } | |
1628 | #undef FUNC_NAME | |
1629 | ||
1630 | SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0, | |
1631 | (SCM port), | |
1632 | "Return the next character available from @var{port}, updating\n" | |
1633 | "@var{port} to point to the following character. If no more\n" | |
1634 | "characters are available, the end-of-file object is returned.\n" | |
1635 | "\n" | |
1636 | "When @var{port}'s data cannot be decoded according to its\n" | |
1637 | "character encoding, a @code{decoding-error} is raised and\n" | |
1638 | "@var{port} points past the erroneous byte sequence.\n") | |
1639 | #define FUNC_NAME s_scm_read_char | |
1640 | { | |
1641 | scm_t_wchar c; | |
1642 | if (SCM_UNBNDP (port)) | |
1643 | port = scm_current_input_port (); | |
1644 | SCM_VALIDATE_OPINPORT (1, port); | |
1645 | c = scm_getc (port); | |
1646 | if (EOF == c) | |
1647 | return SCM_EOF_VAL; | |
1648 | return SCM_MAKE_CHAR (c); | |
1649 | } | |
1650 | #undef FUNC_NAME | |
1651 | ||
1652 | ||
1653 | \f | |
1654 | ||
1655 | /* Pushback. */ | |
1656 | ||
1657 | void | |
1658 | scm_unget_byte (int c, SCM port) | |
1659 | #define FUNC_NAME "scm_unget_byte" | |
1660 | { | |
1661 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
1662 | ||
1663 | if (pt->read_buf == pt->putback_buf) | |
1664 | /* already using the put-back buffer. */ | |
1665 | { | |
1666 | /* enlarge putback_buf if necessary. */ | |
1667 | if (pt->read_end == pt->read_buf + pt->read_buf_size | |
1668 | && pt->read_buf == pt->read_pos) | |
1669 | { | |
1670 | size_t new_size = pt->read_buf_size * 2; | |
1671 | unsigned char *tmp = (unsigned char *) | |
1672 | scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size, | |
1673 | "putback buffer"); | |
1674 | ||
1675 | pt->read_pos = pt->read_buf = pt->putback_buf = tmp; | |
1676 | pt->read_end = pt->read_buf + pt->read_buf_size; | |
1677 | pt->read_buf_size = pt->putback_buf_size = new_size; | |
1678 | } | |
1679 | ||
1680 | /* shift any existing bytes to buffer + 1. */ | |
1681 | if (pt->read_pos == pt->read_end) | |
1682 | pt->read_end = pt->read_buf + 1; | |
1683 | else if (pt->read_pos != pt->read_buf + 1) | |
1684 | { | |
1685 | int count = pt->read_end - pt->read_pos; | |
1686 | ||
1687 | memmove (pt->read_buf + 1, pt->read_pos, count); | |
1688 | pt->read_end = pt->read_buf + 1 + count; | |
1689 | } | |
1690 | ||
1691 | pt->read_pos = pt->read_buf; | |
1692 | } | |
1693 | else | |
1694 | /* switch to the put-back buffer. */ | |
1695 | { | |
1696 | if (pt->putback_buf == NULL) | |
1697 | { | |
1698 | pt->putback_buf | |
1699 | = (unsigned char *) scm_gc_malloc_pointerless | |
1700 | (SCM_INITIAL_PUTBACK_BUF_SIZE, "putback buffer"); | |
1701 | pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE; | |
1702 | } | |
1703 | ||
1704 | pt->saved_read_buf = pt->read_buf; | |
1705 | pt->saved_read_pos = pt->read_pos; | |
1706 | pt->saved_read_end = pt->read_end; | |
1707 | pt->saved_read_buf_size = pt->read_buf_size; | |
1708 | ||
1709 | pt->read_pos = pt->read_buf = pt->putback_buf; | |
1710 | pt->read_end = pt->read_buf + 1; | |
1711 | pt->read_buf_size = pt->putback_buf_size; | |
1712 | } | |
1713 | ||
1714 | *pt->read_buf = c; | |
1715 | ||
1716 | if (pt->rw_random) | |
1717 | pt->rw_active = SCM_PORT_READ; | |
1718 | } | |
1719 | #undef FUNC_NAME | |
1720 | ||
1721 | void | |
1722 | scm_ungetc (scm_t_wchar c, SCM port) | |
1723 | #define FUNC_NAME "scm_ungetc" | |
1724 | { | |
1725 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
1726 | char *result; | |
1727 | char result_buf[10]; | |
1728 | const char *encoding; | |
1729 | size_t len; | |
1730 | int i; | |
1731 | ||
1732 | if (pt->encoding != NULL) | |
1733 | encoding = pt->encoding; | |
1734 | else | |
1735 | encoding = "ISO-8859-1"; | |
1736 | ||
1737 | len = sizeof (result_buf); | |
1738 | result = u32_conv_to_encoding (encoding, | |
1739 | (enum iconv_ilseq_handler) pt->ilseq_handler, | |
1740 | (uint32_t *) &c, 1, NULL, | |
1741 | result_buf, &len); | |
1742 | ||
1743 | if (SCM_UNLIKELY (result == NULL || len == 0)) | |
1744 | scm_encoding_error (FUNC_NAME, errno, | |
1745 | "conversion to port encoding failed", | |
1746 | SCM_BOOL_F, SCM_MAKE_CHAR (c)); | |
1747 | ||
1748 | for (i = len - 1; i >= 0; i--) | |
1749 | scm_unget_byte (result[i], port); | |
1750 | ||
1751 | if (SCM_UNLIKELY (result != result_buf)) | |
1752 | free (result); | |
1753 | ||
1754 | if (c == '\n') | |
1755 | { | |
1756 | /* What should col be in this case? | |
1757 | * We'll leave it at -1. | |
1758 | */ | |
1759 | SCM_LINUM (port) -= 1; | |
1760 | } | |
1761 | else | |
1762 | SCM_COL(port) -= 1; | |
1763 | } | |
1764 | #undef FUNC_NAME | |
1765 | ||
1766 | ||
1767 | void | |
1768 | scm_ungets (const char *s, int n, SCM port) | |
1769 | { | |
1770 | /* This is simple minded and inefficient, but unreading strings is | |
1771 | * probably not a common operation, and remember that line and | |
1772 | * column numbers have to be handled... | |
1773 | * | |
1774 | * Please feel free to write an optimized version! | |
1775 | */ | |
1776 | while (n--) | |
1777 | scm_ungetc (s[n], port); | |
1778 | } | |
1779 | ||
1780 | ||
1781 | SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0, | |
1bbd0b84 | 1782 | (SCM port), |
1e6808ea MG |
1783 | "Return the next character available from @var{port},\n" |
1784 | "@emph{without} updating @var{port} to point to the following\n" | |
1785 | "character. If no more characters are available, the\n" | |
c2dfff19 KR |
1786 | "end-of-file object is returned.\n" |
1787 | "\n" | |
1788 | "The value returned by\n" | |
1e6808ea MG |
1789 | "a call to @code{peek-char} is the same as the value that would\n" |
1790 | "have been returned by a call to @code{read-char} on the same\n" | |
1791 | "port. The only difference is that the very next call to\n" | |
1792 | "@code{read-char} or @code{peek-char} on that @var{port} will\n" | |
1793 | "return the value returned by the preceding call to\n" | |
1794 | "@code{peek-char}. In particular, a call to @code{peek-char} on\n" | |
1795 | "an interactive port will hang waiting for input whenever a call\n" | |
c62da8f8 LC |
1796 | "to @code{read-char} would have hung.\n" |
1797 | "\n" | |
1798 | "As for @code{read-char}, a @code{decoding-error} may be raised\n" | |
1799 | "if such a situation occurs. However, unlike with @code{read-char},\n" | |
1800 | "@var{port} still points at the beginning of the erroneous byte\n" | |
1801 | "sequence when the error is raised.\n") | |
1bbd0b84 | 1802 | #define FUNC_NAME s_scm_peek_char |
ee149d03 | 1803 | { |
c62da8f8 | 1804 | int err; |
fd5eec2b LC |
1805 | SCM result; |
1806 | scm_t_wchar c; | |
1807 | char bytes[SCM_MBCHAR_BUF_SIZE]; | |
c62da8f8 | 1808 | long column, line, i; |
fd5eec2b LC |
1809 | size_t len; |
1810 | ||
ee149d03 | 1811 | if (SCM_UNBNDP (port)) |
9de87eea | 1812 | port = scm_current_input_port (); |
b2456dd4 | 1813 | SCM_VALIDATE_OPINPORT (1, port); |
fd5eec2b LC |
1814 | |
1815 | column = SCM_COL (port); | |
1816 | line = SCM_LINUM (port); | |
1817 | ||
c62da8f8 | 1818 | err = get_codepoint (port, &c, bytes, &len); |
fd5eec2b | 1819 | |
c62da8f8 LC |
1820 | for (i = len - 1; i >= 0; i--) |
1821 | scm_unget_byte (bytes[i], port); | |
fd5eec2b | 1822 | |
c62da8f8 LC |
1823 | SCM_COL (port) = column; |
1824 | SCM_LINUM (port) = line; | |
fd5eec2b | 1825 | |
c62da8f8 LC |
1826 | if (SCM_UNLIKELY (err != 0)) |
1827 | { | |
1828 | scm_decoding_error (FUNC_NAME, err, "input decoding error", port); | |
1829 | ||
1830 | /* Shouldn't happen since `catch' always aborts to prompt. */ | |
1831 | result = SCM_BOOL_F; | |
fd5eec2b | 1832 | } |
c62da8f8 LC |
1833 | else if (c == EOF) |
1834 | result = SCM_EOF_VAL; | |
1835 | else | |
1836 | result = SCM_MAKE_CHAR (c); | |
fd5eec2b LC |
1837 | |
1838 | return result; | |
3cb988bd | 1839 | } |
1bbd0b84 | 1840 | #undef FUNC_NAME |
3cb988bd | 1841 | |
1be4270a | 1842 | SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0, |
1bbd0b84 | 1843 | (SCM cobj, SCM port), |
b380b885 MD |
1844 | "Place @var{char} in @var{port} so that it will be read by the\n" |
1845 | "next read operation. If called multiple times, the unread characters\n" | |
1846 | "will be read again in last-in first-out order. If @var{port} is\n" | |
1847 | "not supplied, the current input port is used.") | |
1bbd0b84 | 1848 | #define FUNC_NAME s_scm_unread_char |
0f2d19dd JB |
1849 | { |
1850 | int c; | |
1851 | ||
34d19ef6 | 1852 | SCM_VALIDATE_CHAR (1, cobj); |
0f2d19dd | 1853 | if (SCM_UNBNDP (port)) |
9de87eea | 1854 | port = scm_current_input_port (); |
b2456dd4 | 1855 | SCM_VALIDATE_OPINPORT (2, port); |
0f2d19dd | 1856 | |
7866a09b | 1857 | c = SCM_CHAR (cobj); |
0f2d19dd | 1858 | |
b7f3516f | 1859 | scm_ungetc (c, port); |
0f2d19dd JB |
1860 | return cobj; |
1861 | } | |
1bbd0b84 | 1862 | #undef FUNC_NAME |
0f2d19dd | 1863 | |
a1ec6916 | 1864 | SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0, |
1bbd0b84 | 1865 | (SCM str, SCM port), |
b380b885 MD |
1866 | "Place the string @var{str} in @var{port} so that its characters will be\n" |
1867 | "read in subsequent read operations. If called multiple times, the\n" | |
1868 | "unread characters will be read again in last-in first-out order. If\n" | |
1869 | "@var{port} is not supplied, the current-input-port is used.") | |
1bbd0b84 | 1870 | #define FUNC_NAME s_scm_unread_string |
ee1e7e13 | 1871 | { |
889975e5 | 1872 | int n; |
34d19ef6 | 1873 | SCM_VALIDATE_STRING (1, str); |
ee1e7e13 | 1874 | if (SCM_UNBNDP (port)) |
9de87eea | 1875 | port = scm_current_input_port (); |
b2456dd4 | 1876 | SCM_VALIDATE_OPINPORT (2, port); |
ee1e7e13 | 1877 | |
889975e5 MG |
1878 | n = scm_i_string_length (str); |
1879 | ||
1880 | while (n--) | |
1881 | scm_ungetc (scm_i_string_ref (str, n), port); | |
ee1e7e13 MD |
1882 | |
1883 | return str; | |
1884 | } | |
1bbd0b84 | 1885 | #undef FUNC_NAME |
ee1e7e13 | 1886 | |
840ae05d | 1887 | |
19b8d12b | 1888 | \f |
23f2b9a3 | 1889 | |
19b8d12b | 1890 | /* Manipulating the buffers. */ |
840ae05d | 1891 | |
19b8d12b AW |
1892 | void |
1893 | scm_port_non_buffer (scm_t_port *pt) | |
1894 | { | |
1895 | pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf; | |
1896 | pt->write_buf = pt->write_pos = &pt->shortbuf; | |
1897 | pt->read_buf_size = pt->write_buf_size = 1; | |
1898 | pt->write_end = pt->write_buf + pt->write_buf_size; | |
840ae05d | 1899 | } |
8ab3d8a0 | 1900 | |
19b8d12b AW |
1901 | /* this should only be called when the read buffer is empty. it |
1902 | tries to refill the read buffer. it returns the first char from | |
1903 | the port, which is either EOF or *(pt->read_pos). */ | |
1904 | int | |
1905 | scm_fill_input (SCM port) | |
82893676 | 1906 | { |
19b8d12b | 1907 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
8ab3d8a0 | 1908 | |
19b8d12b | 1909 | assert (pt->read_pos == pt->read_end); |
8ab3d8a0 | 1910 | |
19b8d12b | 1911 | if (pt->read_buf == pt->putback_buf) |
82893676 | 1912 | { |
19b8d12b AW |
1913 | /* finished reading put-back chars. */ |
1914 | pt->read_buf = pt->saved_read_buf; | |
1915 | pt->read_pos = pt->saved_read_pos; | |
1916 | pt->read_end = pt->saved_read_end; | |
1917 | pt->read_buf_size = pt->saved_read_buf_size; | |
1918 | if (pt->read_pos < pt->read_end) | |
1919 | return *(pt->read_pos); | |
82893676 | 1920 | } |
19b8d12b | 1921 | return SCM_PORT_DESCRIPTOR (port)->fill_input (port); |
82893676 | 1922 | } |
82893676 | 1923 | |
19b8d12b AW |
1924 | /* move up to read_len chars from port's putback and/or read buffers |
1925 | into memory starting at dest. returns the number of chars moved. */ | |
1926 | size_t | |
1927 | scm_take_from_input_buffers (SCM port, char *dest, size_t read_len) | |
840ae05d | 1928 | { |
19b8d12b AW |
1929 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
1930 | size_t chars_read = 0; | |
1931 | size_t from_buf = min (pt->read_end - pt->read_pos, read_len); | |
840ae05d | 1932 | |
19b8d12b | 1933 | if (from_buf > 0) |
840ae05d | 1934 | { |
19b8d12b AW |
1935 | memcpy (dest, pt->read_pos, from_buf); |
1936 | pt->read_pos += from_buf; | |
1937 | chars_read += from_buf; | |
1938 | read_len -= from_buf; | |
1939 | dest += from_buf; | |
840ae05d | 1940 | } |
3fe6190f | 1941 | |
19b8d12b AW |
1942 | /* if putback was active, try the real input buffer too. */ |
1943 | if (pt->read_buf == pt->putback_buf) | |
69bc9ff3 | 1944 | { |
19b8d12b AW |
1945 | from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len); |
1946 | if (from_buf > 0) | |
1947 | { | |
1948 | memcpy (dest, pt->saved_read_pos, from_buf); | |
1949 | pt->saved_read_pos += from_buf; | |
1950 | chars_read += from_buf; | |
1951 | } | |
69bc9ff3 | 1952 | } |
19b8d12b | 1953 | return chars_read; |
840ae05d JB |
1954 | } |
1955 | ||
19b8d12b AW |
1956 | /* Clear a port's read buffers, returning the contents. */ |
1957 | SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0, | |
1bbd0b84 | 1958 | (SCM port), |
19b8d12b AW |
1959 | "This procedure clears a port's input buffers, similar\n" |
1960 | "to the way that force-output clears the output buffer. The\n" | |
1961 | "contents of the buffers are returned as a single string, e.g.,\n" | |
a150979d | 1962 | "\n" |
19b8d12b AW |
1963 | "@lisp\n" |
1964 | "(define p (open-input-file ...))\n" | |
1965 | "(drain-input p) => empty string, nothing buffered yet.\n" | |
1966 | "(unread-char (read-char p) p)\n" | |
1967 | "(drain-input p) => initial chars from p, up to the buffer size.\n" | |
1968 | "@end lisp\n\n" | |
1969 | "Draining the buffers may be useful for cleanly finishing\n" | |
1970 | "buffered I/O so that the file descriptor can be used directly\n" | |
1971 | "for further input.") | |
1972 | #define FUNC_NAME s_scm_drain_input | |
0f2d19dd | 1973 | { |
19b8d12b AW |
1974 | SCM result; |
1975 | char *data; | |
1976 | scm_t_port *pt; | |
1977 | long count; | |
0f2d19dd | 1978 | |
19b8d12b AW |
1979 | SCM_VALIDATE_OPINPORT (1, port); |
1980 | pt = SCM_PTAB_ENTRY (port); | |
1981 | ||
1982 | count = pt->read_end - pt->read_pos; | |
1983 | if (pt->read_buf == pt->putback_buf) | |
1984 | count += pt->saved_read_end - pt->saved_read_pos; | |
1985 | ||
1986 | if (count) | |
1987 | { | |
1988 | result = scm_i_make_string (count, &data, 0); | |
1989 | scm_take_from_input_buffers (port, data, count); | |
1990 | } | |
1991 | else | |
1992 | result = scm_nullstr; | |
1993 | ||
1994 | return result; | |
d043d8c2 | 1995 | } |
1bbd0b84 | 1996 | #undef FUNC_NAME |
d043d8c2 | 1997 | |
19b8d12b AW |
1998 | void |
1999 | scm_end_input (SCM port) | |
0f2d19dd | 2000 | { |
19b8d12b AW |
2001 | long offset; |
2002 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
2003 | ||
2004 | if (pt->read_buf == pt->putback_buf) | |
2005 | { | |
2006 | offset = pt->read_end - pt->read_pos; | |
2007 | pt->read_buf = pt->saved_read_buf; | |
2008 | pt->read_pos = pt->saved_read_pos; | |
2009 | pt->read_end = pt->saved_read_end; | |
2010 | pt->read_buf_size = pt->saved_read_buf_size; | |
2011 | } | |
2012 | else | |
2013 | offset = 0; | |
2014 | ||
2015 | SCM_PORT_DESCRIPTOR (port)->end_input (port, offset); | |
0f2d19dd JB |
2016 | } |
2017 | ||
19b8d12b AW |
2018 | SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0, |
2019 | (SCM port), | |
2020 | "Flush the specified output port, or the current output port if @var{port}\n" | |
2021 | "is omitted. The current output buffer contents are passed to the\n" | |
2022 | "underlying port implementation (e.g., in the case of fports, the\n" | |
2023 | "data will be written to the file and the output buffer will be cleared.)\n" | |
2024 | "It has no effect on an unbuffered port.\n\n" | |
2025 | "The return value is unspecified.") | |
2026 | #define FUNC_NAME s_scm_force_output | |
d043d8c2 | 2027 | { |
19b8d12b AW |
2028 | if (SCM_UNBNDP (port)) |
2029 | port = scm_current_output_port (); | |
2030 | else | |
2031 | { | |
2032 | port = SCM_COERCE_OUTPORT (port); | |
2033 | SCM_VALIDATE_OPOUTPORT (1, port); | |
2034 | } | |
2035 | scm_flush (port); | |
564478fd | 2036 | return SCM_UNSPECIFIED; |
d043d8c2 | 2037 | } |
1bbd0b84 | 2038 | #undef FUNC_NAME |
d043d8c2 | 2039 | |
19b8d12b AW |
2040 | void |
2041 | scm_flush (SCM port) | |
0f2d19dd | 2042 | { |
19b8d12b | 2043 | SCM_PORT_DESCRIPTOR (port)->flush (port); |
0f2d19dd JB |
2044 | } |
2045 | ||
d14af9f2 | 2046 | |
19b8d12b | 2047 | \f |
d6a6989e | 2048 | |
19b8d12b | 2049 | /* Output. */ |
889975e5 | 2050 | |
19b8d12b AW |
2051 | /* scm_c_write |
2052 | * | |
2053 | * Used by an application to write arbitrary number of bytes to an SCM | |
2054 | * port. Similar semantics as libc write. However, unlike libc | |
2055 | * write, scm_c_write writes the requested number of bytes and has no | |
2056 | * return value. | |
2057 | * | |
2058 | * Warning: Doesn't update port line and column counts! | |
2059 | */ | |
9d9c66ba | 2060 | void |
19b8d12b AW |
2061 | scm_c_write (SCM port, const void *ptr, size_t size) |
2062 | #define FUNC_NAME "scm_c_write" | |
9d9c66ba | 2063 | { |
19b8d12b AW |
2064 | scm_t_port *pt; |
2065 | scm_t_ptob_descriptor *ptob; | |
9d9c66ba | 2066 | |
19b8d12b | 2067 | SCM_VALIDATE_OPOUTPORT (1, port); |
9d9c66ba | 2068 | |
19b8d12b AW |
2069 | pt = SCM_PTAB_ENTRY (port); |
2070 | ptob = SCM_PORT_DESCRIPTOR (port); | |
9d9c66ba | 2071 | |
19b8d12b AW |
2072 | if (pt->rw_active == SCM_PORT_READ) |
2073 | scm_end_input (port); | |
2074 | ||
2075 | ptob->write (port, ptr, size); | |
2076 | ||
2077 | if (pt->rw_random) | |
2078 | pt->rw_active = SCM_PORT_WRITE; | |
889975e5 | 2079 | } |
19b8d12b | 2080 | #undef FUNC_NAME |
889975e5 | 2081 | |
19b8d12b AW |
2082 | /* scm_lfwrite |
2083 | * | |
2084 | * This function differs from scm_c_write; it updates port line and | |
2085 | * column. */ | |
889975e5 | 2086 | void |
19b8d12b | 2087 | scm_lfwrite (const char *ptr, size_t size, SCM port) |
889975e5 | 2088 | { |
19b8d12b AW |
2089 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
2090 | scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port); | |
f4bc4e59 | 2091 | |
19b8d12b AW |
2092 | if (pt->rw_active == SCM_PORT_READ) |
2093 | scm_end_input (port); | |
f4bc4e59 | 2094 | |
19b8d12b | 2095 | ptob->write (port, ptr, size); |
f4bc4e59 | 2096 | |
19b8d12b AW |
2097 | for (; size; ptr++, size--) |
2098 | update_port_lf ((scm_t_wchar) (unsigned char) *ptr, port); | |
d9544bf0 | 2099 | |
19b8d12b AW |
2100 | if (pt->rw_random) |
2101 | pt->rw_active = SCM_PORT_WRITE; | |
2102 | } | |
f4bc4e59 | 2103 | |
19b8d12b AW |
2104 | /* Write STR to PORT from START inclusive to END exclusive. */ |
2105 | void | |
2106 | scm_lfwrite_substr (SCM str, size_t start, size_t end, SCM port) | |
2107 | { | |
2108 | scm_t_port *pt = SCM_PTAB_ENTRY (port); | |
f4bc4e59 | 2109 | |
19b8d12b AW |
2110 | if (pt->rw_active == SCM_PORT_READ) |
2111 | scm_end_input (port); | |
f4bc4e59 | 2112 | |
19b8d12b AW |
2113 | if (end == (size_t) -1) |
2114 | end = scm_i_string_length (str); | |
f4bc4e59 | 2115 | |
19b8d12b | 2116 | scm_display (scm_c_substring (str, start, end), port); |
f4bc4e59 | 2117 | |
19b8d12b AW |
2118 | if (pt->rw_random) |
2119 | pt->rw_active = SCM_PORT_WRITE; | |
889975e5 MG |
2120 | } |
2121 | ||
19b8d12b AW |
2122 | |
2123 | \f | |
2124 | ||
2125 | /* Querying and setting positions, and character availability. */ | |
2126 | ||
2127 | SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0, | |
889975e5 | 2128 | (SCM port), |
19b8d12b AW |
2129 | "Return @code{#t} if a character is ready on input @var{port}\n" |
2130 | "and return @code{#f} otherwise. If @code{char-ready?} returns\n" | |
2131 | "@code{#t} then the next @code{read-char} operation on\n" | |
2132 | "@var{port} is guaranteed not to hang. If @var{port} is a file\n" | |
2133 | "port at end of file then @code{char-ready?} returns @code{#t}.\n" | |
2134 | "\n" | |
2135 | "@code{char-ready?} exists to make it possible for a\n" | |
2136 | "program to accept characters from interactive ports without\n" | |
2137 | "getting stuck waiting for input. Any input editors associated\n" | |
2138 | "with such ports must make sure that characters whose existence\n" | |
2139 | "has been asserted by @code{char-ready?} cannot be rubbed out.\n" | |
2140 | "If @code{char-ready?} were to return @code{#f} at end of file,\n" | |
2141 | "a port at end of file would be indistinguishable from an\n" | |
2142 | "interactive port that has no ready characters.") | |
2143 | #define FUNC_NAME s_scm_char_ready_p | |
889975e5 MG |
2144 | { |
2145 | scm_t_port *pt; | |
889975e5 | 2146 | |
19b8d12b AW |
2147 | if (SCM_UNBNDP (port)) |
2148 | port = scm_current_input_port (); | |
2149 | /* It's possible to close the current input port, so validate even in | |
2150 | this case. */ | |
2151 | SCM_VALIDATE_OPINPORT (1, port); | |
889975e5 MG |
2152 | |
2153 | pt = SCM_PTAB_ENTRY (port); | |
19b8d12b AW |
2154 | |
2155 | /* if the current read buffer is filled, or the | |
2156 | last pushed-back char has been read and the saved buffer is | |
2157 | filled, result is true. */ | |
2158 | if (pt->read_pos < pt->read_end | |
2159 | || (pt->read_buf == pt->putback_buf | |
2160 | && pt->saved_read_pos < pt->saved_read_end)) | |
2161 | return SCM_BOOL_T; | |
889975e5 | 2162 | else |
19b8d12b AW |
2163 | { |
2164 | scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (port); | |
2165 | ||
2166 | if (ptob->input_waiting) | |
2167 | return scm_from_bool(ptob->input_waiting (port)); | |
2168 | else | |
2169 | return SCM_BOOL_T; | |
2170 | } | |
889975e5 MG |
2171 | } |
2172 | #undef FUNC_NAME | |
d6a6989e | 2173 | |
19b8d12b AW |
2174 | SCM_DEFINE (scm_seek, "seek", 3, 0, 0, |
2175 | (SCM fd_port, SCM offset, SCM whence), | |
2176 | "Sets the current position of @var{fd/port} to the integer\n" | |
2177 | "@var{offset}, which is interpreted according to the value of\n" | |
2178 | "@var{whence}.\n" | |
2179 | "\n" | |
2180 | "One of the following variables should be supplied for\n" | |
2181 | "@var{whence}:\n" | |
2182 | "@defvar SEEK_SET\n" | |
2183 | "Seek from the beginning of the file.\n" | |
2184 | "@end defvar\n" | |
2185 | "@defvar SEEK_CUR\n" | |
2186 | "Seek from the current position.\n" | |
2187 | "@end defvar\n" | |
2188 | "@defvar SEEK_END\n" | |
2189 | "Seek from the end of the file.\n" | |
2190 | "@end defvar\n" | |
2191 | "If @var{fd/port} is a file descriptor, the underlying system\n" | |
2192 | "call is @code{lseek}. @var{port} may be a string port.\n" | |
2193 | "\n" | |
2194 | "The value returned is the new position in the file. This means\n" | |
2195 | "that the current position of a port can be obtained using:\n" | |
2196 | "@lisp\n" | |
2197 | "(seek port 0 SEEK_CUR)\n" | |
2198 | "@end lisp") | |
2199 | #define FUNC_NAME s_scm_seek | |
889975e5 | 2200 | { |
19b8d12b | 2201 | int how; |
889975e5 | 2202 | |
19b8d12b | 2203 | fd_port = SCM_COERCE_OUTPORT (fd_port); |
889975e5 | 2204 | |
19b8d12b AW |
2205 | how = scm_to_int (whence); |
2206 | if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END) | |
2207 | SCM_OUT_OF_RANGE (3, whence); | |
da288f50 | 2208 | |
19b8d12b AW |
2209 | if (SCM_OPPORTP (fd_port)) |
2210 | { | |
2211 | scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (fd_port); | |
2212 | off_t_or_off64_t off = scm_to_off_t_or_off64_t (offset); | |
2213 | off_t_or_off64_t rv; | |
2214 | ||
2215 | if (!ptob->seek) | |
2216 | SCM_MISC_ERROR ("port is not seekable", | |
2217 | scm_cons (fd_port, SCM_EOL)); | |
2218 | else | |
2219 | rv = ptob->seek (fd_port, off, how); | |
2220 | return scm_from_off_t_or_off64_t (rv); | |
2221 | } | |
2222 | else /* file descriptor?. */ | |
2223 | { | |
2224 | off_t_or_off64_t off = scm_to_off_t_or_off64_t (offset); | |
2225 | off_t_or_off64_t rv; | |
2226 | rv = lseek_or_lseek64 (scm_to_int (fd_port), off, how); | |
2227 | if (rv == -1) | |
2228 | SCM_SYSERROR; | |
2229 | return scm_from_off_t_or_off64_t (rv); | |
2230 | } | |
889975e5 MG |
2231 | } |
2232 | #undef FUNC_NAME | |
2233 | ||
19b8d12b AW |
2234 | #ifndef O_BINARY |
2235 | #define O_BINARY 0 | |
2236 | #endif | |
889975e5 | 2237 | |
19b8d12b AW |
2238 | /* Mingw has ftruncate(), perhaps implemented above using chsize, but |
2239 | doesn't have the filename version truncate(), hence this code. */ | |
2240 | #if HAVE_FTRUNCATE && ! HAVE_TRUNCATE | |
2241 | static int | |
2242 | truncate (const char *file, off_t length) | |
889975e5 | 2243 | { |
19b8d12b AW |
2244 | int ret, fdes; |
2245 | ||
2246 | fdes = open (file, O_BINARY | O_WRONLY); | |
2247 | if (fdes == -1) | |
2248 | return -1; | |
2249 | ||
2250 | ret = ftruncate (fdes, length); | |
2251 | if (ret == -1) | |
889975e5 | 2252 | { |
19b8d12b AW |
2253 | int save_errno = errno; |
2254 | close (fdes); | |
2255 | errno = save_errno; | |
2256 | return -1; | |
889975e5 | 2257 | } |
19b8d12b AW |
2258 | |
2259 | return close (fdes); | |
889975e5 | 2260 | } |
19b8d12b | 2261 | #endif /* HAVE_FTRUNCATE && ! HAVE_TRUNCATE */ |
889975e5 | 2262 | |
19b8d12b AW |
2263 | SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0, |
2264 | (SCM object, SCM length), | |
2265 | "Truncate @var{file} to @var{length} bytes. @var{file} can be a\n" | |
2266 | "filename string, a port object, or an integer file descriptor.\n" | |
2267 | "The return value is unspecified.\n" | |
2268 | "\n" | |
2269 | "For a port or file descriptor @var{length} can be omitted, in\n" | |
2270 | "which case the file is truncated at the current position (per\n" | |
2271 | "@code{ftell} above).\n" | |
2272 | "\n" | |
2273 | "On most systems a file can be extended by giving a length\n" | |
2274 | "greater than the current size, but this is not mandatory in the\n" | |
2275 | "POSIX standard.") | |
2276 | #define FUNC_NAME s_scm_truncate_file | |
889975e5 | 2277 | { |
19b8d12b AW |
2278 | int rv; |
2279 | ||
2280 | /* "object" can be a port, fdes or filename. | |
2281 | ||
2282 | Negative "length" makes no sense, but it's left to truncate() or | |
2283 | ftruncate() to give back an error for that (normally EINVAL). | |
2284 | */ | |
2285 | ||
2286 | if (SCM_UNBNDP (length)) | |
889975e5 | 2287 | { |
19b8d12b AW |
2288 | /* must supply length if object is a filename. */ |
2289 | if (scm_is_string (object)) | |
2290 | SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL); | |
2291 | ||
2292 | length = scm_seek (object, SCM_INUM0, scm_from_int (SEEK_CUR)); | |
2293 | } | |
2294 | ||
2295 | object = SCM_COERCE_OUTPORT (object); | |
2296 | if (scm_is_integer (object)) | |
2297 | { | |
2298 | off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length); | |
2299 | SCM_SYSCALL (rv = ftruncate_or_ftruncate64 (scm_to_int (object), | |
2300 | c_length)); | |
2301 | } | |
2302 | else if (SCM_OPOUTPORTP (object)) | |
2303 | { | |
2304 | off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length); | |
2305 | scm_t_port *pt = SCM_PTAB_ENTRY (object); | |
2306 | scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (object); | |
2307 | ||
2308 | if (!ptob->truncate) | |
2309 | SCM_MISC_ERROR ("port is not truncatable", SCM_EOL); | |
2310 | if (pt->rw_active == SCM_PORT_READ) | |
2311 | scm_end_input (object); | |
2312 | else if (pt->rw_active == SCM_PORT_WRITE) | |
2313 | ptob->flush (object); | |
2314 | ||
2315 | ptob->truncate (object, c_length); | |
2316 | rv = 0; | |
889975e5 MG |
2317 | } |
2318 | else | |
2319 | { | |
19b8d12b AW |
2320 | off_t_or_off64_t c_length = scm_to_off_t_or_off64_t (length); |
2321 | char *str = scm_to_locale_string (object); | |
2322 | int eno; | |
2323 | SCM_SYSCALL (rv = truncate_or_truncate64 (str, c_length)); | |
2324 | eno = errno; | |
2325 | free (str); | |
2326 | errno = eno; | |
889975e5 | 2327 | } |
19b8d12b AW |
2328 | if (rv == -1) |
2329 | SCM_SYSERROR; | |
2330 | return SCM_UNSPECIFIED; | |
889975e5 | 2331 | } |
19b8d12b | 2332 | #undef FUNC_NAME |
889975e5 | 2333 | |
19b8d12b AW |
2334 | SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0, |
2335 | (SCM port), | |
2336 | "Return the current line number for @var{port}.\n" | |
889975e5 | 2337 | "\n" |
19b8d12b AW |
2338 | "The first line of a file is 0. But you might want to add 1\n" |
2339 | "when printing line numbers, since starting from 1 is\n" | |
2340 | "traditional in error messages, and likely to be more natural to\n" | |
2341 | "non-programmers.") | |
2342 | #define FUNC_NAME s_scm_port_line | |
889975e5 | 2343 | { |
19b8d12b AW |
2344 | port = SCM_COERCE_OUTPORT (port); |
2345 | SCM_VALIDATE_OPENPORT (1, port); | |
2346 | return scm_from_long (SCM_LINUM (port)); | |
889975e5 MG |
2347 | } |
2348 | #undef FUNC_NAME | |
2349 | ||
19b8d12b AW |
2350 | SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0, |
2351 | (SCM port, SCM line), | |
2352 | "Set the current line number for @var{port} to @var{line}. The\n" | |
2353 | "first line of a file is 0.") | |
2354 | #define FUNC_NAME s_scm_set_port_line_x | |
889975e5 | 2355 | { |
19b8d12b AW |
2356 | port = SCM_COERCE_OUTPORT (port); |
2357 | SCM_VALIDATE_OPENPORT (1, port); | |
2358 | SCM_PTAB_ENTRY (port)->line_number = scm_to_long (line); | |
2359 | return SCM_UNSPECIFIED; | |
2360 | } | |
2361 | #undef FUNC_NAME | |
889975e5 | 2362 | |
19b8d12b AW |
2363 | SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0, |
2364 | (SCM port), | |
2365 | "Return the current column number of @var{port}.\n" | |
2366 | "If the number is\n" | |
2367 | "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n" | |
2368 | "- i.e. the first character of the first line is line 0, column 0.\n" | |
2369 | "(However, when you display a file position, for example in an error\n" | |
2370 | "message, we recommend you add 1 to get 1-origin integers. This is\n" | |
2371 | "because lines and column numbers traditionally start with 1, and that is\n" | |
2372 | "what non-programmers will find most natural.)") | |
2373 | #define FUNC_NAME s_scm_port_column | |
2374 | { | |
2375 | port = SCM_COERCE_OUTPORT (port); | |
2376 | SCM_VALIDATE_OPENPORT (1, port); | |
2377 | return scm_from_int (SCM_COL (port)); | |
2378 | } | |
2379 | #undef FUNC_NAME | |
889975e5 | 2380 | |
19b8d12b AW |
2381 | SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0, |
2382 | (SCM port, SCM column), | |
2383 | "Set the current column of @var{port}. Before reading the first\n" | |
2384 | "character on a line the column should be 0.") | |
2385 | #define FUNC_NAME s_scm_set_port_column_x | |
2386 | { | |
2387 | port = SCM_COERCE_OUTPORT (port); | |
2388 | SCM_VALIDATE_OPENPORT (1, port); | |
2389 | SCM_PTAB_ENTRY (port)->column_number = scm_to_int (column); | |
2390 | return SCM_UNSPECIFIED; | |
2391 | } | |
2392 | #undef FUNC_NAME | |
889975e5 | 2393 | |
19b8d12b AW |
2394 | SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0, |
2395 | (SCM port), | |
2396 | "Return the filename associated with @var{port}, or @code{#f}\n" | |
2397 | "if no filename is associated with the port.") | |
2398 | #define FUNC_NAME s_scm_port_filename | |
2399 | { | |
2400 | port = SCM_COERCE_OUTPORT (port); | |
2401 | SCM_VALIDATE_OPENPORT (1, port); | |
2402 | return SCM_FILENAME (port); | |
2403 | } | |
2404 | #undef FUNC_NAME | |
889975e5 | 2405 | |
19b8d12b AW |
2406 | SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0, |
2407 | (SCM port, SCM filename), | |
2408 | "Change the filename associated with @var{port}, using the current input\n" | |
2409 | "port if none is specified. Note that this does not change the port's\n" | |
2410 | "source of data, but only the value that is returned by\n" | |
2411 | "@code{port-filename} and reported in diagnostic output.") | |
2412 | #define FUNC_NAME s_scm_set_port_filename_x | |
2413 | { | |
2414 | port = SCM_COERCE_OUTPORT (port); | |
2415 | SCM_VALIDATE_OPENPORT (1, port); | |
2416 | /* We allow the user to set the filename to whatever he likes. */ | |
2417 | SCM_SET_FILENAME (port, filename); | |
889975e5 MG |
2418 | return SCM_UNSPECIFIED; |
2419 | } | |
2420 | #undef FUNC_NAME | |
2421 | ||
2422 | ||
19b8d12b AW |
2423 | \f |
2424 | ||
2425 | /* Implementation helpers for port printing functions. */ | |
889975e5 | 2426 | |
f12733c9 MD |
2427 | void |
2428 | scm_print_port_mode (SCM exp, SCM port) | |
2429 | { | |
2430 | scm_puts (SCM_CLOSEDP (exp) | |
2431 | ? "closed: " | |
f9a64404 DH |
2432 | : (SCM_RDNG & SCM_CELL_WORD_0 (exp) |
2433 | ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp) | |
f12733c9 MD |
2434 | ? "input-output: " |
2435 | : "input: ") | |
f9a64404 | 2436 | : (SCM_WRTNG & SCM_CELL_WORD_0 (exp) |
f12733c9 MD |
2437 | ? "output: " |
2438 | : "bogus: ")), | |
2439 | port); | |
2440 | } | |
1cc91f1b | 2441 | |
f12733c9 | 2442 | int |
e81d98ec | 2443 | scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 2444 | { |
f12733c9 MD |
2445 | char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp)); |
2446 | if (!type) | |
2447 | type = "port"; | |
b7f3516f | 2448 | scm_puts ("#<", port); |
f12733c9 | 2449 | scm_print_port_mode (exp, port); |
b7f3516f TT |
2450 | scm_puts (type, port); |
2451 | scm_putc (' ', port); | |
0345e278 | 2452 | scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port); |
b7f3516f | 2453 | scm_putc ('>', port); |
f12733c9 | 2454 | return 1; |
0f2d19dd JB |
2455 | } |
2456 | ||
19b8d12b AW |
2457 | |
2458 | \f | |
2459 | ||
2460 | /* Iterating over all ports. */ | |
2461 | ||
2462 | struct for_each_data | |
2463 | { | |
2464 | void (*proc) (void *data, SCM p); | |
2465 | void *data; | |
2466 | }; | |
2467 | ||
2468 | static SCM | |
2469 | for_each_trampoline (void *data, SCM port, SCM result) | |
2470 | { | |
2471 | struct for_each_data *d = data; | |
2472 | ||
2473 | d->proc (d->data, port); | |
2474 | ||
2475 | return result; | |
2476 | } | |
2477 | ||
2478 | void | |
2479 | scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data) | |
2480 | { | |
2481 | struct for_each_data d; | |
2482 | ||
2483 | d.proc = proc; | |
2484 | d.data = data; | |
2485 | ||
2486 | scm_c_weak_set_fold (for_each_trampoline, &d, SCM_EOL, | |
2487 | scm_i_port_weak_set); | |
2488 | } | |
2489 | ||
2490 | static void | |
2491 | scm_for_each_trampoline (void *data, SCM port) | |
2492 | { | |
2493 | scm_call_1 (SCM_PACK_POINTER (data), port); | |
2494 | } | |
2495 | ||
2496 | SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0, | |
2497 | (SCM proc), | |
2498 | "Apply @var{proc} to each port in the Guile port table\n" | |
2499 | "in turn. The return value is unspecified. More specifically,\n" | |
2500 | "@var{proc} is applied exactly once to every port that exists\n" | |
2501 | "in the system at the time @var{port-for-each} is invoked.\n" | |
2502 | "Changes to the port table while @var{port-for-each} is running\n" | |
2503 | "have no effect as far as @var{port-for-each} is concerned.") | |
2504 | #define FUNC_NAME s_scm_port_for_each | |
2505 | { | |
2506 | SCM_VALIDATE_PROC (1, proc); | |
2507 | ||
2508 | scm_c_port_for_each (scm_for_each_trampoline, SCM_UNPACK_POINTER (proc)); | |
2509 | ||
2510 | return SCM_UNSPECIFIED; | |
2511 | } | |
2512 | #undef FUNC_NAME | |
2513 | ||
2514 | static void | |
2515 | flush_output_port (void *closure, SCM port) | |
2516 | { | |
2517 | if (SCM_OPOUTPORTP (port)) | |
2518 | scm_flush (port); | |
2519 | } | |
2520 | ||
2521 | SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0, | |
2522 | (), | |
2523 | "Equivalent to calling @code{force-output} on\n" | |
2524 | "all open output ports. The return value is unspecified.") | |
2525 | #define FUNC_NAME s_scm_flush_all_ports | |
2526 | { | |
2527 | scm_c_port_for_each (&flush_output_port, NULL); | |
2528 | return SCM_UNSPECIFIED; | |
2529 | } | |
2530 | #undef FUNC_NAME | |
2531 | ||
2532 | ||
0f2d19dd | 2533 | \f |
ee149d03 | 2534 | |
d68fee48 | 2535 | /* Void ports. */ |
0f2d19dd | 2536 | |
92c2555f | 2537 | scm_t_bits scm_tc16_void_port = 0; |
0f2d19dd | 2538 | |
e81d98ec | 2539 | static int fill_input_void_port (SCM port SCM_UNUSED) |
283a1a0e | 2540 | { |
70df8af6 | 2541 | return EOF; |
283a1a0e GH |
2542 | } |
2543 | ||
31703ab8 | 2544 | static void |
e81d98ec DH |
2545 | write_void_port (SCM port SCM_UNUSED, |
2546 | const void *data SCM_UNUSED, | |
2547 | size_t size SCM_UNUSED) | |
31703ab8 GH |
2548 | { |
2549 | } | |
2550 | ||
d617ee18 MV |
2551 | static SCM |
2552 | scm_i_void_port (long mode_bits) | |
0f2d19dd | 2553 | { |
2721f918 AW |
2554 | SCM ret; |
2555 | ||
2556 | ret = scm_c_make_port (scm_tc16_void_port, mode_bits, 0); | |
da220f27 | 2557 | |
2721f918 | 2558 | scm_port_non_buffer (SCM_PTAB_ENTRY (ret)); |
402788a9 | 2559 | |
2721f918 | 2560 | return ret; |
0f2d19dd JB |
2561 | } |
2562 | ||
d617ee18 MV |
2563 | SCM |
2564 | scm_void_port (char *mode_str) | |
2565 | { | |
2566 | return scm_i_void_port (scm_mode_bits (mode_str)); | |
2567 | } | |
2568 | ||
a1ec6916 | 2569 | SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0, |
1bbd0b84 | 2570 | (SCM mode), |
70df8af6 | 2571 | "Create and return a new void port. A void port acts like\n" |
bb2c02f2 | 2572 | "@file{/dev/null}. The @var{mode} argument\n" |
70df8af6 | 2573 | "specifies the input/output modes for this port: see the\n" |
b380b885 | 2574 | "documentation for @code{open-file} in @ref{File Ports}.") |
1bbd0b84 | 2575 | #define FUNC_NAME s_scm_sys_make_void_port |
0f2d19dd | 2576 | { |
d617ee18 | 2577 | return scm_i_void_port (scm_i_mode_bits (mode)); |
0f2d19dd | 2578 | } |
1bbd0b84 | 2579 | #undef FUNC_NAME |
0f2d19dd | 2580 | |
19b8d12b | 2581 | |
0f2d19dd | 2582 | \f |
19b8d12b | 2583 | |
89545eba | 2584 | /* Initialization. */ |
1cc91f1b | 2585 | |
0f2d19dd JB |
2586 | void |
2587 | scm_init_ports () | |
0f2d19dd | 2588 | { |
840ae05d | 2589 | /* lseek() symbols. */ |
e11e83f3 MV |
2590 | scm_c_define ("SEEK_SET", scm_from_int (SEEK_SET)); |
2591 | scm_c_define ("SEEK_CUR", scm_from_int (SEEK_CUR)); | |
2592 | scm_c_define ("SEEK_END", scm_from_int (SEEK_END)); | |
840ae05d | 2593 | |
70df8af6 GH |
2594 | scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port, |
2595 | write_void_port); | |
9de87eea | 2596 | |
f39448c5 AW |
2597 | cur_inport_fluid = scm_make_fluid (); |
2598 | cur_outport_fluid = scm_make_fluid (); | |
2599 | cur_errport_fluid = scm_make_fluid (); | |
2600 | cur_loadport_fluid = scm_make_fluid (); | |
9de87eea | 2601 | |
2721f918 | 2602 | scm_i_port_weak_set = scm_c_make_weak_set (31); |
d6a6989e | 2603 | |
a0599745 | 2604 | #include "libguile/ports.x" |
889975e5 | 2605 | |
d6a6989e LC |
2606 | /* Use Latin-1 as the default port encoding. */ |
2607 | SCM_VARIABLE_SET (default_port_encoding_var, scm_make_fluid ()); | |
2608 | scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var), SCM_BOOL_F); | |
889975e5 | 2609 | scm_port_encoding_init = 1; |
d6a6989e | 2610 | |
889975e5 MG |
2611 | SCM_VARIABLE_SET (scm_conversion_strategy, scm_make_fluid ()); |
2612 | scm_fluid_set_x (SCM_VARIABLE_REF (scm_conversion_strategy), | |
2613 | scm_from_int ((int) SCM_FAILED_CONVERSION_QUESTION_MARK)); | |
2614 | scm_conversion_strategy_init = 1; | |
2615 | ||
0f2d19dd | 2616 | } |
89e00824 ML |
2617 | |
2618 | /* | |
2619 | Local Variables: | |
2620 | c-file-style: "gnu" | |
2621 | End: | |
2622 | */ |