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