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