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