* validate.h, deprecated.h (SCM_VALIDATE_INUM, SCM_VALIDATE_INUM_COPY,
[bpt/guile.git] / libguile / ports.c
CommitLineData
185e369a 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
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
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
d68fee48
JB
20/* Headers. */
21
bd515f37
RB
22#if HAVE_CONFIG_H
23# include <config.h>
24#endif
25
0f2d19dd 26#include <stdio.h>
e6e2e95a
MD
27#include <errno.h>
28
a0599745 29#include "libguile/_scm.h"
f0942910 30#include "libguile/eval.h"
a0599745
MD
31#include "libguile/objects.h"
32#include "libguile/smob.h"
33#include "libguile/chars.h"
185e369a 34#include "libguile/dynwind.h"
0f2d19dd 35
a0599745
MD
36#include "libguile/keywords.h"
37#include "libguile/root.h"
38#include "libguile/strings.h"
b42170a4 39#include "libguile/mallocs.h"
a0599745
MD
40#include "libguile/validate.h"
41#include "libguile/ports.h"
0f2d19dd 42
bd9e24b3
GH
43#ifdef HAVE_STRING_H
44#include <string.h>
45#endif
46
0f2d19dd 47#ifdef HAVE_MALLOC_H
95b88819 48#include <malloc.h>
0f2d19dd
JB
49#endif
50
ec65f5da
MV
51#ifdef HAVE_IO_H
52#include <io.h>
53#endif
54
0f2d19dd
JB
55#ifdef HAVE_UNISTD_H
56#include <unistd.h>
57#endif
58
95b88819
GH
59#ifdef HAVE_SYS_IOCTL_H
60#include <sys/ioctl.h>
61#endif
d68fee48 62
82893676
MG
63#ifdef __MINGW32__
64#include <fcntl.h>
65#define ftruncate(fd, size) chsize (fd, size)
66#endif
67
0f2d19dd 68\f
d68fee48 69/* The port kind table --- a dynamically resized array of port types. */
0f2d19dd
JB
70
71
72/* scm_ptobs scm_numptob
73 * implement a dynamicly resized array of ptob records.
74 * Indexes into this table are used when generating type
75 * tags for smobjects (if you know a tag you can get an index and conversely).
76 */
92c2555f 77scm_t_ptob_descriptor *scm_ptobs;
c014a02e 78long scm_numptob;
0f2d19dd 79
ee149d03 80/* GC marker for a port with stream of SCM type. */
0f2d19dd 81SCM
a284e297 82scm_markstream (SCM ptr)
0f2d19dd
JB
83{
84 int openp;
f9a64404 85 openp = SCM_CELL_WORD_0 (ptr) & SCM_OPN;
0f2d19dd 86 if (openp)
74a16888 87 return SCM_PACK (SCM_STREAM (ptr));
0f2d19dd
JB
88 else
89 return SCM_BOOL_F;
90}
91
f12733c9 92/*
f12733c9 93 * We choose to use an interface similar to the smob interface with
affc96b5 94 * fill_input and write as standard fields, passed to the port
f12733c9
MD
95 * type constructor, and optional fields set by setters.
96 */
97
70df8af6 98static void
e81d98ec 99flush_port_default (SCM port SCM_UNUSED)
70df8af6
GH
100{
101}
102
103static void
e81d98ec 104end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED)
70df8af6
GH
105{
106}
0f2d19dd 107
4c9419ac
MV
108static size_t
109scm_port_free0 (SCM port)
110{
111 return 0;
112}
113
92c2555f 114scm_t_bits
f12733c9 115scm_make_port_type (char *name,
affc96b5 116 int (*fill_input) (SCM port),
8aa011a1 117 void (*write) (SCM port, const void *data, size_t size))
0f2d19dd
JB
118{
119 char *tmp;
120 if (255 <= scm_numptob)
121 goto ptoberr;
f12733c9
MD
122 SCM_DEFER_INTS;
123 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs,
124 (1 + scm_numptob)
92c2555f 125 * sizeof (scm_t_ptob_descriptor)));
0f2d19dd
JB
126 if (tmp)
127 {
92c2555f 128 scm_ptobs = (scm_t_ptob_descriptor *) tmp;
affc96b5 129
f12733c9
MD
130 scm_ptobs[scm_numptob].name = name;
131 scm_ptobs[scm_numptob].mark = 0;
4c9419ac 132 scm_ptobs[scm_numptob].free = scm_port_free0;
f12733c9
MD
133 scm_ptobs[scm_numptob].print = scm_port_print;
134 scm_ptobs[scm_numptob].equalp = 0;
affc96b5
GH
135 scm_ptobs[scm_numptob].close = 0;
136
137 scm_ptobs[scm_numptob].write = write;
70df8af6 138 scm_ptobs[scm_numptob].flush = flush_port_default;
affc96b5 139
70df8af6 140 scm_ptobs[scm_numptob].end_input = end_input_default;
affc96b5
GH
141 scm_ptobs[scm_numptob].fill_input = fill_input;
142 scm_ptobs[scm_numptob].input_waiting = 0;
143
f12733c9 144 scm_ptobs[scm_numptob].seek = 0;
affc96b5
GH
145 scm_ptobs[scm_numptob].truncate = 0;
146
0f2d19dd
JB
147 scm_numptob++;
148 }
f12733c9 149 SCM_ALLOW_INTS;
0f2d19dd 150 if (!tmp)
2500356c
DH
151 {
152 ptoberr:
153 scm_memory_error ("scm_make_port_type");
154 }
f12733c9
MD
155 /* Make a class object if Goops is present */
156 if (scm_port_class)
157 scm_make_port_classes (scm_numptob - 1, SCM_PTOBNAME (scm_numptob - 1));
0f2d19dd
JB
158 return scm_tc7_port + (scm_numptob - 1) * 256;
159}
160
f12733c9 161void
6c747373 162scm_set_port_mark (long tc, SCM (*mark) (SCM))
f12733c9
MD
163{
164 scm_ptobs[SCM_TC2PTOBNUM (tc)].mark = mark;
165}
166
167void
1be6b49c 168scm_set_port_free (long tc, size_t (*free) (SCM))
f12733c9
MD
169{
170 scm_ptobs[SCM_TC2PTOBNUM (tc)].free = free;
171}
172
173void
6c747373 174scm_set_port_print (long tc, int (*print) (SCM exp, SCM port,
f12733c9
MD
175 scm_print_state *pstate))
176{
177 scm_ptobs[SCM_TC2PTOBNUM (tc)].print = print;
178}
179
180void
6c747373 181scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM))
f12733c9
MD
182{
183 scm_ptobs[SCM_TC2PTOBNUM (tc)].equalp = equalp;
184}
185
31703ab8 186void
affc96b5 187scm_set_port_flush (long tc, void (*flush) (SCM port))
31703ab8 188{
affc96b5 189 scm_ptobs[SCM_TC2PTOBNUM (tc)].flush = flush;
31703ab8
GH
190}
191
f12733c9 192void
affc96b5 193scm_set_port_end_input (long tc, void (*end_input) (SCM port, int offset))
f12733c9 194{
affc96b5 195 scm_ptobs[SCM_TC2PTOBNUM (tc)].end_input = end_input;
f12733c9
MD
196}
197
198void
6c747373 199scm_set_port_close (long tc, int (*close) (SCM))
f12733c9 200{
affc96b5 201 scm_ptobs[SCM_TC2PTOBNUM (tc)].close = close;
f12733c9
MD
202}
203
204void
6c747373 205scm_set_port_seek (long tc, off_t (*seek) (SCM port,
f12733c9
MD
206 off_t OFFSET,
207 int WHENCE))
208{
209 scm_ptobs[SCM_TC2PTOBNUM (tc)].seek = seek;
210}
211
212void
6c747373 213scm_set_port_truncate (long tc, void (*truncate) (SCM port, off_t length))
f12733c9 214{
affc96b5 215 scm_ptobs[SCM_TC2PTOBNUM (tc)].truncate = truncate;
f12733c9
MD
216}
217
218void
affc96b5 219scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM))
f12733c9 220{
affc96b5 221 scm_ptobs[SCM_TC2PTOBNUM (tc)].input_waiting = input_waiting;
f12733c9
MD
222}
223
0f2d19dd 224\f
0f2d19dd 225
3b3b36dd 226SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
1e6808ea
MG
227 (SCM port),
228 "Return @code{#t} if a character is ready on input @var{port}\n"
229 "and return @code{#f} otherwise. If @code{char-ready?} returns\n"
230 "@code{#t} then the next @code{read-char} operation on\n"
231 "@var{port} is guaranteed not to hang. If @var{port} is a file\n"
232 "port at end of file then @code{char-ready?} returns @code{#t}.\n"
c2dfff19
KR
233 "\n"
234 "@code{char-ready?} exists to make it possible for a\n"
1e6808ea
MG
235 "program to accept characters from interactive ports without\n"
236 "getting stuck waiting for input. Any input editors associated\n"
237 "with such ports must make sure that characters whose existence\n"
238 "has been asserted by @code{char-ready?} cannot be rubbed out.\n"
239 "If @code{char-ready?} were to return @code{#f} at end of file,\n"
240 "a port at end of file would be indistinguishable from an\n"
c2dfff19 241 "interactive port that has no ready characters.")
1bbd0b84 242#define FUNC_NAME s_scm_char_ready_p
0f2d19dd 243{
92c2555f 244 scm_t_port *pt;
6c951427 245
0f2d19dd
JB
246 if (SCM_UNBNDP (port))
247 port = scm_cur_inp;
248 else
34d19ef6 249 SCM_VALIDATE_OPINPORT (1, port);
d68fee48 250
ae4c4016
JB
251 pt = SCM_PTAB_ENTRY (port);
252
6c951427
GH
253 /* if the current read buffer is filled, or the
254 last pushed-back char has been read and the saved buffer is
255 filled, result is true. */
256 if (pt->read_pos < pt->read_end
257 || (pt->read_buf == pt->putback_buf
258 && pt->saved_read_pos < pt->saved_read_end))
0f2d19dd 259 return SCM_BOOL_T;
ee149d03
JB
260 else
261 {
92c2555f 262 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
ee149d03 263
affc96b5 264 if (ptob->input_waiting)
7888309b 265 return scm_from_bool(ptob->input_waiting (port));
ee149d03 266 else
6c951427 267 return SCM_BOOL_T;
ee149d03 268 }
0f2d19dd 269}
1bbd0b84 270#undef FUNC_NAME
0f2d19dd 271
c2da2648
GH
272/* move up to read_len chars from port's putback and/or read buffers
273 into memory starting at dest. returns the number of chars moved. */
274size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
275{
92c2555f 276 scm_t_port *pt = SCM_PTAB_ENTRY (port);
c2da2648
GH
277 size_t chars_read = 0;
278 size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
279
280 if (from_buf > 0)
281 {
282 memcpy (dest, pt->read_pos, from_buf);
283 pt->read_pos += from_buf;
284 chars_read += from_buf;
285 read_len -= from_buf;
286 dest += from_buf;
287 }
288
289 /* if putback was active, try the real input buffer too. */
290 if (pt->read_buf == pt->putback_buf)
291 {
292 from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len);
293 if (from_buf > 0)
294 {
295 memcpy (dest, pt->saved_read_pos, from_buf);
296 pt->saved_read_pos += from_buf;
297 chars_read += from_buf;
298 }
299 }
300 return chars_read;
301}
302
6c951427 303/* Clear a port's read buffers, returning the contents. */
a1ec6916 304SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
1bbd0b84 305 (SCM port),
4a151b3d
GH
306 "This procedure clears a port's input buffers, similar\n"
307 "to the way that force-output clears the output buffer. The\n"
308 "contents of the buffers are returned as a single string, e.g.,\n"
309 "\n"
310 "@lisp\n"
311 "(define p (open-input-file ...))\n"
312 "(drain-input p) => empty string, nothing buffered yet.\n"
313 "(unread-char (read-char p) p)\n"
314 "(drain-input p) => initial chars from p, up to the buffer size.\n"
315 "@end lisp\n\n"
316 "Draining the buffers may be useful for cleanly finishing\n"
317 "buffered I/O so that the file descriptor can be used directly\n"
318 "for further input.")
1bbd0b84 319#define FUNC_NAME s_scm_drain_input
ee149d03 320{
840ae05d 321 SCM result;
28a6e1b0 322 scm_t_port *pt;
c014a02e 323 long count;
ee149d03 324
34d19ef6 325 SCM_VALIDATE_OPINPORT (1, port);
28a6e1b0 326 pt = SCM_PTAB_ENTRY (port);
840ae05d 327
6c951427
GH
328 count = pt->read_end - pt->read_pos;
329 if (pt->read_buf == pt->putback_buf)
330 count += pt->saved_read_end - pt->saved_read_pos;
840ae05d 331
be54b15d 332 result = scm_allocate_string (count);
c2da2648 333 scm_take_from_input_buffers (port, SCM_STRING_CHARS (result), count);
6c951427 334
840ae05d 335 return result;
ee149d03 336}
1bbd0b84 337#undef FUNC_NAME
0f2d19dd
JB
338
339\f
d68fee48 340/* Standard ports --- current input, output, error, and more(!). */
0f2d19dd 341
3b3b36dd 342SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0,
e1546b65
MG
343 (),
344 "Return the current input port. This is the default port used\n"
345 "by many input procedures. Initially, @code{current-input-port}\n"
346 "returns the @dfn{standard input} in Unix and C terminology.")
1bbd0b84 347#define FUNC_NAME s_scm_current_input_port
0f2d19dd
JB
348{
349 return scm_cur_inp;
350}
1bbd0b84 351#undef FUNC_NAME
0f2d19dd 352
3b3b36dd 353SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0,
e1546b65
MG
354 (),
355 "Return the current output port. This is the default port used\n"
9401323e 356 "by many output procedures. Initially,\n"
e1546b65
MG
357 "@code{current-output-port} returns the @dfn{standard output} in\n"
358 "Unix and C terminology.")
1bbd0b84 359#define FUNC_NAME s_scm_current_output_port
0f2d19dd
JB
360{
361 return scm_cur_outp;
362}
1bbd0b84 363#undef FUNC_NAME
0f2d19dd 364
3b3b36dd 365SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0,
1bbd0b84 366 (),
b380b885
MD
367 "Return the port to which errors and warnings should be sent (the\n"
368 "@dfn{standard error} in Unix and C terminology).")
1bbd0b84 369#define FUNC_NAME s_scm_current_error_port
0f2d19dd
JB
370{
371 return scm_cur_errp;
372}
1bbd0b84 373#undef FUNC_NAME
0f2d19dd 374
3b3b36dd 375SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0,
e1546b65 376 (),
b450f070 377 "Return the current-load-port.\n"
e1546b65 378 "The load port is used internally by @code{primitive-load}.")
1bbd0b84 379#define FUNC_NAME s_scm_current_load_port
31614d8e
MD
380{
381 return scm_cur_loadp;
382}
1bbd0b84 383#undef FUNC_NAME
31614d8e 384
3b3b36dd 385SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0,
1bbd0b84 386 (SCM port),
8f85c0c6
NJ
387 "@deffnx {Scheme Procedure} set-current-output-port port\n"
388 "@deffnx {Scheme Procedure} set-current-error-port port\n"
b380b885
MD
389 "Change the ports returned by @code{current-input-port},\n"
390 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
391 "so that they use the supplied @var{port} for input or output.")
1bbd0b84 392#define FUNC_NAME s_scm_set_current_input_port
0f2d19dd
JB
393{
394 SCM oinp = scm_cur_inp;
34d19ef6 395 SCM_VALIDATE_OPINPORT (1, port);
0f2d19dd
JB
396 scm_cur_inp = port;
397 return oinp;
398}
1bbd0b84 399#undef FUNC_NAME
0f2d19dd
JB
400
401
3b3b36dd 402SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0,
e1546b65
MG
403 (SCM port),
404 "Set the current default output port to @var{port}.")
1bbd0b84 405#define FUNC_NAME s_scm_set_current_output_port
0f2d19dd
JB
406{
407 SCM ooutp = scm_cur_outp;
78446828 408 port = SCM_COERCE_OUTPORT (port);
34d19ef6 409 SCM_VALIDATE_OPOUTPORT (1, port);
0f2d19dd
JB
410 scm_cur_outp = port;
411 return ooutp;
412}
1bbd0b84 413#undef FUNC_NAME
0f2d19dd
JB
414
415
3b3b36dd 416SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
e1546b65
MG
417 (SCM port),
418 "Set the current default error port to @var{port}.")
1bbd0b84 419#define FUNC_NAME s_scm_set_current_error_port
0f2d19dd
JB
420{
421 SCM oerrp = scm_cur_errp;
78446828 422 port = SCM_COERCE_OUTPORT (port);
34d19ef6 423 SCM_VALIDATE_OPOUTPORT (1, port);
0f2d19dd
JB
424 scm_cur_errp = port;
425 return oerrp;
426}
1bbd0b84 427#undef FUNC_NAME
0f2d19dd 428
185e369a
MV
429typedef struct {
430 SCM value;
431 SCM (*getter) (void);
432 SCM (*setter) (SCM);
433} swap_data;
434
435static void
b42170a4 436swap_port (SCM scm_data)
185e369a 437{
b42170a4 438 swap_data *d = (swap_data *)SCM_MALLOCDATA (scm_data);
185e369a
MV
439 SCM t;
440
441 t = d->getter ();
442 d->setter (d->value);
443 d->value = t;
444}
445
446static void
07add345 447scm_frame_current_foo_port (SCM port,
185e369a
MV
448 SCM (*getter) (void), SCM (*setter) (SCM))
449{
b42170a4
MV
450 SCM scm_data = scm_malloc_obj (sizeof (swap_data));
451 swap_data *data = (swap_data *)SCM_MALLOCDATA (scm_data);
452 data->value = port;
453 data->getter = getter;
454 data->setter = setter;
185e369a 455
16c5cac2
MV
456 scm_frame_rewind_handler_with_scm (swap_port, scm_data,
457 SCM_F_WIND_EXPLICITLY);
458 scm_frame_unwind_handler_with_scm (swap_port, scm_data,
459 SCM_F_WIND_EXPLICITLY);
185e369a
MV
460}
461
462void
07add345 463scm_frame_current_input_port (SCM port)
185e369a 464{
07add345
MV
465 scm_frame_current_foo_port (port,
466 scm_current_input_port,
467 scm_set_current_input_port);
185e369a
MV
468}
469
470void
07add345 471scm_frame_current_output_port (SCM port)
185e369a 472{
07add345
MV
473 scm_frame_current_foo_port (port,
474 scm_current_output_port,
475 scm_set_current_output_port);
185e369a
MV
476}
477
478void
07add345 479scm_frame_current_error_port (SCM port)
185e369a 480{
07add345
MV
481 scm_frame_current_foo_port (port,
482 scm_current_error_port,
483 scm_set_current_error_port);
185e369a
MV
484}
485
0f2d19dd 486\f
840ae05d 487/* The port table --- an array of pointers to ports. */
0f2d19dd 488
67329a9e 489scm_t_port **scm_i_port_table;
0f2d19dd 490
67329a9e
HWN
491long scm_i_port_table_size = 0; /* Number of ports in scm_i_port_table. */
492long scm_i_port_table_room = 20; /* Size of the array. */
0f2d19dd 493
b9ad392e
MD
494SCM_GLOBAL_MUTEX (scm_i_port_table_mutex);
495
496/* This function is not and should not be thread safe. */
1cc91f1b 497
da220f27
HWN
498SCM
499scm_new_port_table_entry (scm_t_bits tag)
402788a9 500#define FUNC_NAME "scm_new_port_table_entry"
0f2d19dd 501{
85835e59
HWN
502 /*
503 We initialize the cell to empty, this is in case scm_gc_calloc
504 triggers GC ; we don't want the GC to scan a half-finished Z.
505 */
506
67329a9e 507 SCM z = scm_cons (SCM_EOL, SCM_EOL);
39e8f371 508 scm_t_port *entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port");
67329a9e 509 if (scm_i_port_table_size == scm_i_port_table_room)
0f2d19dd 510 {
4c9419ac 511 /* initial malloc is in gc.c. this doesn't use scm_gc_malloc etc.,
c6c79933 512 since it can never be freed during gc. */
67329a9e 513 void *newt = scm_realloc ((char *) scm_i_port_table,
4c9419ac 514 (size_t) (sizeof (scm_t_port *)
67329a9e
HWN
515 * scm_i_port_table_room * 2));
516 scm_i_port_table = (scm_t_port **) newt;
517 scm_i_port_table_room *= 2;
0f2d19dd 518 }
840ae05d 519
67329a9e 520 entry->entry = scm_i_port_table_size;
5f16b897 521
840ae05d 522 entry->file_name = SCM_BOOL_F;
61e452ba 523 entry->rw_active = SCM_PORT_NEITHER;
5f16b897 524
67329a9e
HWN
525 scm_i_port_table[scm_i_port_table_size] = entry;
526 scm_i_port_table_size++;
840ae05d 527
da220f27
HWN
528 entry->port = z;
529 SCM_SET_CELL_TYPE(z, tag);
530 SCM_SETPTAB_ENTRY(z, entry);
531
532 return z;
0f2d19dd 533}
c6c79933 534#undef FUNC_NAME
0f2d19dd 535
67329a9e
HWN
536#if SCM_ENABLE_DEPRECATED==1
537SCM_API scm_t_port *
538scm_add_to_port_table (SCM port)
539{
540 SCM z = scm_new_port_table_entry (scm_tc7_port);
541 scm_t_port * pt = SCM_PTAB_ENTRY(z);
542
543 pt->port = port;
544 SCM_SETCAR(z, SCM_EOL);
545 SCM_SETCDR(z, SCM_EOL);
85835e59 546 SCM_SETPTAB_ENTRY (port, pt);
67329a9e
HWN
547 return pt;
548}
549#endif
550
551
6c951427 552/* Remove a port from the table and destroy it. */
b9ad392e
MD
553
554/* This function is not and should not be thread safe. */
555
0f2d19dd 556void
a284e297 557scm_remove_from_port_table (SCM port)
db4b4ca6 558#define FUNC_NAME "scm_remove_from_port_table"
0f2d19dd 559{
92c2555f 560 scm_t_port *p = SCM_PTAB_ENTRY (port);
c014a02e 561 long i = p->entry;
6c951427 562
67329a9e 563 if (i >= scm_i_port_table_size)
1afff620 564 SCM_MISC_ERROR ("Port not in table: ~S", scm_list_1 (port));
6c951427 565 if (p->putback_buf)
4c9419ac
MV
566 scm_gc_free (p->putback_buf, p->putback_buf_size, "putback buffer");
567 scm_gc_free (p, sizeof (scm_t_port), "port");
ee1e7e13
MD
568 /* Since we have just freed slot i we can shrink the table by moving
569 the last entry to that slot... */
67329a9e 570 if (i < scm_i_port_table_size - 1)
0f2d19dd 571 {
67329a9e
HWN
572 scm_i_port_table[i] = scm_i_port_table[scm_i_port_table_size - 1];
573 scm_i_port_table[i]->entry = i;
0f2d19dd 574 }
0f2d19dd 575 SCM_SETPTAB_ENTRY (port, 0);
67329a9e 576 scm_i_port_table_size--;
0f2d19dd 577}
db4b4ca6
DH
578#undef FUNC_NAME
579
0f2d19dd 580
fea6b4ea 581#ifdef GUILE_DEBUG
b450f070 582/* Functions for debugging. */
1cc91f1b 583
3b3b36dd 584SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
b450f070 585 (),
1e6808ea 586 "Return the number of ports in the port table. @code{pt-size}\n"
5352393c 587 "is only included in @code{--enable-guile-debug} builds.")
1bbd0b84 588#define FUNC_NAME s_scm_pt_size
0f2d19dd 589{
93ccaef0 590 return SCM_I_MAKINUM (scm_i_port_table_size);
0f2d19dd 591}
1bbd0b84 592#undef FUNC_NAME
0f2d19dd 593
3b3b36dd 594SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
b450f070 595 (SCM index),
1e6808ea 596 "Return the port at @var{index} in the port table.\n"
5352393c
MG
597 "@code{pt-member} is only included in\n"
598 "@code{--enable-guile-debug} builds.")
1bbd0b84 599#define FUNC_NAME s_scm_pt_member
0f2d19dd 600{
a55c2b68
MV
601 size_t i = scm_to_size_t (index);
602 if (i >= scm_i_port_table_size)
0f2d19dd
JB
603 return SCM_BOOL_F;
604 else
67329a9e 605 return scm_i_port_table[i]->port;
0f2d19dd 606}
1bbd0b84 607#undef FUNC_NAME
0f2d19dd
JB
608#endif
609
70df8af6 610void
92c2555f 611scm_port_non_buffer (scm_t_port *pt)
70df8af6
GH
612{
613 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
614 pt->write_buf = pt->write_pos = &pt->shortbuf;
615 pt->read_buf_size = pt->write_buf_size = 1;
616 pt->write_end = pt->write_buf + pt->write_buf_size;
617}
0f2d19dd 618
d68fee48
JB
619\f
620/* Revealed counts --- an oddity inherited from SCSH. */
621
8b13c6b3
GH
622/* Find a port in the table and return its revealed count.
623 Also used by the garbage collector.
0f2d19dd 624 */
1cc91f1b 625
0f2d19dd 626int
a284e297 627scm_revealed_count (SCM port)
0f2d19dd
JB
628{
629 return SCM_REVEALED(port);
630}
631
632
633
634/* Return the revealed count for a port. */
635
3b3b36dd 636SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
1bbd0b84 637 (SCM port),
1e6808ea 638 "Return the revealed count for @var{port}.")
1bbd0b84 639#define FUNC_NAME s_scm_port_revealed
0f2d19dd 640{
78446828 641 port = SCM_COERCE_OUTPORT (port);
34d19ef6 642 SCM_VALIDATE_OPENPORT (1, port);
93ccaef0 643 return SCM_I_MAKINUM (scm_revealed_count (port));
0f2d19dd 644}
1bbd0b84 645#undef FUNC_NAME
0f2d19dd
JB
646
647/* Set the revealed count for a port. */
3b3b36dd 648SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
1bbd0b84 649 (SCM port, SCM rcount),
b450f070 650 "Sets the revealed count for a port to a given value.\n"
b380b885 651 "The return value is unspecified.")
1bbd0b84 652#define FUNC_NAME s_scm_set_port_revealed_x
0f2d19dd 653{
78446828 654 port = SCM_COERCE_OUTPORT (port);
34d19ef6 655 SCM_VALIDATE_OPENPORT (1, port);
a55c2b68 656 SCM_REVEALED (port) = scm_to_int (rcount);
8b13c6b3 657 return SCM_UNSPECIFIED;
0f2d19dd 658}
1bbd0b84 659#undef FUNC_NAME
0f2d19dd 660
d68fee48
JB
661
662\f
663/* Retrieving a port's mode. */
664
eadd48de
GH
665/* Return the flags that characterize a port based on the mode
666 * string used to open a file for that port.
667 *
668 * See PORT FLAGS in scm.h
669 */
670
671long
a284e297 672scm_mode_bits (char *modes)
eadd48de
GH
673{
674 return (SCM_OPN
675 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
676 | ( strchr (modes, 'w')
677 || strchr (modes, 'a')
678 || strchr (modes, '+') ? SCM_WRTNG : 0)
ee149d03
JB
679 | (strchr (modes, '0') ? SCM_BUF0 : 0)
680 | (strchr (modes, 'l') ? SCM_BUFLINE : 0));
eadd48de
GH
681}
682
683
684/* Return the mode flags from an open port.
685 * Some modes such as "append" are only used when opening
686 * a file and are not returned here. */
687
3b3b36dd 688SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
1bbd0b84 689 (SCM port),
1e6808ea
MG
690 "Return the port modes associated with the open port @var{port}.\n"
691 "These will not necessarily be identical to the modes used when\n"
692 "the port was opened, since modes such as \"append\" which are\n"
693 "used only during port creation are not retained.")
1bbd0b84 694#define FUNC_NAME s_scm_port_mode
eadd48de 695{
26a3038d 696 char modes[4];
eadd48de 697 modes[0] = '\0';
78446828
MV
698
699 port = SCM_COERCE_OUTPORT (port);
34d19ef6 700 SCM_VALIDATE_OPPORT (1, port);
f9a64404
DH
701 if (SCM_CELL_WORD_0 (port) & SCM_RDNG) {
702 if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
eadd48de
GH
703 strcpy (modes, "r+");
704 else
705 strcpy (modes, "r");
706 }
f9a64404 707 else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
eadd48de 708 strcpy (modes, "w");
f9a64404 709 if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
eadd48de 710 strcat (modes, "0");
36284627 711 return scm_mem2string (modes, strlen (modes));
eadd48de 712}
1bbd0b84 713#undef FUNC_NAME
eadd48de
GH
714
715
d68fee48
JB
716\f
717/* Closing ports. */
718
0f2d19dd
JB
719/* scm_close_port
720 * Call the close operation on a port object.
eadd48de 721 * see also scm_close.
0f2d19dd 722 */
3b3b36dd 723SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
1bbd0b84 724 (SCM port),
1e6808ea
MG
725 "Close the specified port object. Return @code{#t} if it\n"
726 "successfully closes a port or @code{#f} if it was already\n"
727 "closed. An exception may be raised if an error occurs, for\n"
728 "example when flushing buffered output. See also @ref{Ports and\n"
729 "File Descriptors, close}, for a procedure which can close file\n"
730 "descriptors.")
1bbd0b84 731#define FUNC_NAME s_scm_close_port
0f2d19dd 732{
1be6b49c 733 size_t i;
eadd48de
GH
734 int rv;
735
78446828
MV
736 port = SCM_COERCE_OUTPORT (port);
737
7a754ca6 738 SCM_VALIDATE_PORT (1, port);
0f2d19dd 739 if (SCM_CLOSEDP (port))
eadd48de 740 return SCM_BOOL_F;
0f2d19dd 741 i = SCM_PTOBNUM (port);
affc96b5
GH
742 if (scm_ptobs[i].close)
743 rv = (scm_ptobs[i].close) (port);
eadd48de
GH
744 else
745 rv = 0;
b9ad392e 746 scm_mutex_lock (&scm_i_port_table_mutex);
0f2d19dd 747 scm_remove_from_port_table (port);
b9ad392e 748 scm_mutex_unlock (&scm_i_port_table_mutex);
22a52da1 749 SCM_CLR_PORT_OPEN_FLAG (port);
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
c536b4b3
MV
785void
786scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data)
c2ca4493 787{
c014a02e 788 long i;
fdfe6305
MV
789 SCM ports;
790
fdfe6305
MV
791 /* Even without pre-emptive multithreading, running arbitrary code
792 while scanning the port table is unsafe because the port table
793 can change arbitrarily (from a GC, for example). So we build a
794 list in advance while blocking the GC. -mvo */
795
b9ad392e 796 scm_mutex_lock (&scm_i_port_table_mutex);
fdfe6305
MV
797 scm_block_gc++;
798 ports = SCM_EOL;
67329a9e
HWN
799 for (i = 0; i < scm_i_port_table_size; i++)
800 ports = scm_cons (scm_i_port_table[i]->port, ports);
fdfe6305 801 scm_block_gc--;
b9ad392e 802 scm_mutex_unlock (&scm_i_port_table_mutex);
fdfe6305
MV
803
804 while (ports != SCM_EOL)
c2ca4493 805 {
c536b4b3 806 proc (data, SCM_CAR (ports));
fdfe6305 807 ports = SCM_CDR (ports);
c2ca4493 808 }
c536b4b3 809}
fdfe6305 810
c536b4b3
MV
811SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
812 (SCM proc),
813 "Apply @var{proc} to each port in the Guile port table\n"
814 "in turn. The return value is unspecified. More specifically,\n"
815 "@var{proc} is applied exactly once to every port that exists\n"
816 "in the system at the time @var{port-for-each} is invoked.\n"
817 "Changes to the port table while @var{port-for-each} is running\n"
818 "have no effect as far as @var{port-for-each} is concerned.")
819#define FUNC_NAME s_scm_port_for_each
820{
821 SCM_VALIDATE_PROC (1, proc);
822
823 scm_c_port_for_each ((void (*)(void*,SCM))scm_call_1, proc);
c2ca4493
GH
824 return SCM_UNSPECIFIED;
825}
826#undef FUNC_NAME
827
c536b4b3 828
d68fee48
JB
829\f
830/* Utter miscellany. Gosh, we should clean this up some time. */
831
3b3b36dd 832SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
1bbd0b84 833 (SCM x),
1e6808ea
MG
834 "Return @code{#t} if @var{x} is an input port, otherwise return\n"
835 "@code{#f}. Any object satisfying this predicate also satisfies\n"
836 "@code{port?}.")
1bbd0b84 837#define FUNC_NAME s_scm_input_port_p
0f2d19dd 838{
7888309b 839 return scm_from_bool (SCM_INPUT_PORT_P (x));
0f2d19dd 840}
1bbd0b84 841#undef FUNC_NAME
0f2d19dd 842
3b3b36dd 843SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
1bbd0b84 844 (SCM x),
1e6808ea
MG
845 "Return @code{#t} if @var{x} is an output port, otherwise return\n"
846 "@code{#f}. Any object satisfying this predicate also satisfies\n"
847 "@code{port?}.")
1bbd0b84 848#define FUNC_NAME s_scm_output_port_p
0f2d19dd 849{
82893676 850 x = SCM_COERCE_OUTPORT (x);
7888309b 851 return scm_from_bool (SCM_OUTPUT_PORT_P (x));
0f2d19dd 852}
1bbd0b84 853#undef FUNC_NAME
0f2d19dd 854
eb5c0a2a
GH
855SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
856 (SCM x),
1e6808ea 857 "Return a boolean indicating whether @var{x} is a port.\n"
5352393c
MG
858 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
859 "@var{x}))}.")
eb5c0a2a
GH
860#define FUNC_NAME s_scm_port_p
861{
7888309b 862 return scm_from_bool (SCM_PORTP (x));
eb5c0a2a
GH
863}
864#undef FUNC_NAME
865
3b3b36dd 866SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
1bbd0b84 867 (SCM port),
1e6808ea
MG
868 "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
869 "open.")
1bbd0b84 870#define FUNC_NAME s_scm_port_closed_p
60d0643d 871{
34d19ef6 872 SCM_VALIDATE_PORT (1, port);
7888309b 873 return scm_from_bool (!SCM_OPPORTP (port));
60d0643d 874}
1bbd0b84 875#undef FUNC_NAME
0f2d19dd 876
3b3b36dd 877SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
1bbd0b84 878 (SCM x),
1e6808ea
MG
879 "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
880 "return @code{#f}.")
1bbd0b84 881#define FUNC_NAME s_scm_eof_object_p
0f2d19dd 882{
7888309b 883 return scm_from_bool(SCM_EOF_OBJECT_P (x));
0f2d19dd 884}
1bbd0b84 885#undef FUNC_NAME
0f2d19dd 886
3b3b36dd 887SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
1bbd0b84 888 (SCM port),
b380b885 889 "Flush the specified output port, or the current output port if @var{port}\n"
9401323e 890 "is omitted. The current output buffer contents are passed to the\n"
b380b885
MD
891 "underlying port implementation (e.g., in the case of fports, the\n"
892 "data will be written to the file and the output buffer will be cleared.)\n"
893 "It has no effect on an unbuffered port.\n\n"
894 "The return value is unspecified.")
1bbd0b84 895#define FUNC_NAME s_scm_force_output
0f2d19dd
JB
896{
897 if (SCM_UNBNDP (port))
3e877d15 898 port = scm_cur_outp;
0f2d19dd 899 else
78446828
MV
900 {
901 port = SCM_COERCE_OUTPORT (port);
34d19ef6 902 SCM_VALIDATE_OPOUTPORT (1, port);
78446828 903 }
affc96b5 904 scm_flush (port);
ee149d03 905 return SCM_UNSPECIFIED;
0f2d19dd 906}
1bbd0b84 907#undef FUNC_NAME
0f2d19dd 908
a1ec6916 909SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
1bbd0b84 910 (),
b380b885
MD
911 "Equivalent to calling @code{force-output} on\n"
912 "all open output ports. The return value is unspecified.")
1bbd0b84 913#define FUNC_NAME s_scm_flush_all_ports
89ea5b7c 914{
1be6b49c 915 size_t i;
89ea5b7c 916
b9ad392e 917 scm_mutex_lock (&scm_i_port_table_mutex);
67329a9e 918 for (i = 0; i < scm_i_port_table_size; i++)
89ea5b7c 919 {
67329a9e
HWN
920 if (SCM_OPOUTPORTP (scm_i_port_table[i]->port))
921 scm_flush (scm_i_port_table[i]->port);
89ea5b7c 922 }
b9ad392e 923 scm_mutex_unlock (&scm_i_port_table_mutex);
89ea5b7c
GH
924 return SCM_UNSPECIFIED;
925}
1bbd0b84 926#undef FUNC_NAME
0f2d19dd 927
3b3b36dd 928SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
1bbd0b84 929 (SCM port),
1e6808ea
MG
930 "Return the next character available from @var{port}, updating\n"
931 "@var{port} to point to the following character. If no more\n"
932 "characters are available, the end-of-file object is returned.")
1bbd0b84 933#define FUNC_NAME s_scm_read_char
0f2d19dd
JB
934{
935 int c;
936 if (SCM_UNBNDP (port))
334341aa 937 port = scm_cur_inp;
34d19ef6 938 SCM_VALIDATE_OPINPORT (1, port);
b7f3516f 939 c = scm_getc (port);
0f2d19dd
JB
940 if (EOF == c)
941 return SCM_EOF_VAL;
7866a09b 942 return SCM_MAKE_CHAR (c);
0f2d19dd 943}
1bbd0b84 944#undef FUNC_NAME
0f2d19dd 945
5c070ca7 946/* this should only be called when the read buffer is empty. it
affc96b5 947 tries to refill the read buffer. it returns the first char from
5c070ca7 948 the port, which is either EOF or *(pt->read_pos). */
6c951427 949int
affc96b5 950scm_fill_input (SCM port)
6c951427 951{
92c2555f 952 scm_t_port *pt = SCM_PTAB_ENTRY (port);
283a1a0e 953
6c951427
GH
954 if (pt->read_buf == pt->putback_buf)
955 {
956 /* finished reading put-back chars. */
957 pt->read_buf = pt->saved_read_buf;
958 pt->read_pos = pt->saved_read_pos;
959 pt->read_end = pt->saved_read_end;
960 pt->read_buf_size = pt->saved_read_buf_size;
961 if (pt->read_pos < pt->read_end)
5c070ca7 962 return *(pt->read_pos);
6c951427 963 }
affc96b5 964 return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port);
6c951427
GH
965}
966
ee149d03 967int
a284e297 968scm_getc (SCM port)
0f2d19dd
JB
969{
970 int c;
92c2555f 971 scm_t_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 972
840ae05d 973 if (pt->rw_active == SCM_PORT_WRITE)
aab1caad
RB
974 /* may be marginally faster than calling scm_flush. */
975 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
6c951427 976
5c070ca7
GH
977 if (pt->rw_random)
978 pt->rw_active = SCM_PORT_READ;
979
980 if (pt->read_pos >= pt->read_end)
ee149d03 981 {
affc96b5 982 if (scm_fill_input (port) == EOF)
5c070ca7 983 return EOF;
ee149d03
JB
984 }
985
5c070ca7 986 c = *(pt->read_pos++);
840ae05d 987
aab1caad 988 switch (c)
ee149d03 989 {
aab1caad
RB
990 case '\n':
991 SCM_INCLINE (port);
992 break;
993 case '\t':
994 SCM_TABCOL (port);
995 break;
996 default:
997 SCM_INCCOL (port);
998 break;
ee149d03 999 }
aab1caad 1000
ee149d03 1001 return c;
0f2d19dd
JB
1002}
1003
ee149d03 1004void
a284e297 1005scm_putc (char c, SCM port)
ee149d03 1006{
265e6a4d 1007 scm_lfwrite (&c, 1, port);
ee149d03 1008}
3cb988bd 1009
ee149d03 1010void
70d63753 1011scm_puts (const char *s, SCM port)
3cb988bd 1012{
265e6a4d 1013 scm_lfwrite (s, strlen (s), port);
ee149d03 1014}
3cb988bd 1015
6fe692e9
MD
1016/* scm_lfwrite
1017 *
53802ea8
MV
1018 * This function differs from scm_c_write; it updates port line and
1019 * column. */
6fe692e9 1020
ee149d03 1021void
1be6b49c 1022scm_lfwrite (const char *ptr, size_t size, SCM port)
ee149d03 1023{
92c2555f
MV
1024 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1025 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
3e2043c4 1026
840ae05d 1027 if (pt->rw_active == SCM_PORT_READ)
affc96b5 1028 scm_end_input (port);
283a1a0e 1029
31703ab8 1030 ptob->write (port, ptr, size);
840ae05d 1031
53802ea8
MV
1032 for (; size; ptr++, size--) {
1033 if (*ptr == '\n') {
1034 SCM_INCLINE(port);
1035 }
1036 else if (*ptr == '\t') {
1037 SCM_TABCOL(port);
1038 }
1039 else {
1040 SCM_INCCOL(port);
1041 }
1042 }
1043
840ae05d
JB
1044 if (pt->rw_random)
1045 pt->rw_active = SCM_PORT_WRITE;
ee149d03 1046}
3cb988bd 1047
6fe692e9
MD
1048/* scm_c_read
1049 *
1050 * Used by an application to read arbitrary number of bytes from an
1051 * SCM port. Same semantics as libc read, except that scm_c_read only
1052 * returns less than SIZE bytes if at end-of-file.
1053 *
1054 * Warning: Doesn't update port line and column counts! */
1055
1be6b49c
ML
1056size_t
1057scm_c_read (SCM port, void *buffer, size_t size)
6fe692e9 1058{
92c2555f 1059 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1be6b49c 1060 size_t n_read = 0, n_available;
6fe692e9
MD
1061
1062 if (pt->rw_active == SCM_PORT_WRITE)
1063 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
1064
1065 if (pt->rw_random)
1066 pt->rw_active = SCM_PORT_READ;
1067
1068 if (SCM_READ_BUFFER_EMPTY_P (pt))
1069 {
1070 if (scm_fill_input (port) == EOF)
1071 return 0;
1072 }
1073
1074 n_available = pt->read_end - pt->read_pos;
1075
1076 while (n_available < size)
1077 {
1078 memcpy (buffer, pt->read_pos, n_available);
910d1e40 1079 buffer = (char *) buffer + n_available;
6fe692e9
MD
1080 pt->read_pos += n_available;
1081 n_read += n_available;
1082
1083 if (SCM_READ_BUFFER_EMPTY_P (pt))
1084 {
1085 if (scm_fill_input (port) == EOF)
1086 return n_read;
1087 }
1088
1089 size -= n_available;
1090 n_available = pt->read_end - pt->read_pos;
1091 }
1092
1093 memcpy (buffer, pt->read_pos, size);
1094 pt->read_pos += size;
1095
1096 return n_read + size;
1097}
1098
1099/* scm_c_write
1100 *
1101 * Used by an application to write arbitrary number of bytes to an SCM
1102 * port. Similar semantics as libc write. However, unlike libc
1103 * write, scm_c_write writes the requested number of bytes and has no
1104 * return value.
1105 *
1106 * Warning: Doesn't update port line and column counts!
1107 */
1108
1109void
1be6b49c 1110scm_c_write (SCM port, const void *ptr, size_t size)
6fe692e9 1111{
92c2555f
MV
1112 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1113 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
6fe692e9
MD
1114
1115 if (pt->rw_active == SCM_PORT_READ)
1116 scm_end_input (port);
1117
1118 ptob->write (port, ptr, size);
1119
1120 if (pt->rw_random)
1121 pt->rw_active = SCM_PORT_WRITE;
1122}
3cb988bd 1123
ee149d03 1124void
a284e297 1125scm_flush (SCM port)
ee149d03 1126{
c014a02e 1127 long i = SCM_PTOBNUM (port);
affc96b5 1128 (scm_ptobs[i].flush) (port);
ee149d03
JB
1129}
1130
283a1a0e 1131void
a284e297 1132scm_end_input (SCM port)
283a1a0e 1133{
c014a02e 1134 long offset;
92c2555f 1135 scm_t_port *pt = SCM_PTAB_ENTRY (port);
283a1a0e
GH
1136
1137 if (pt->read_buf == pt->putback_buf)
1138 {
1139 offset = pt->read_end - pt->read_pos;
1140 pt->read_buf = pt->saved_read_buf;
1141 pt->read_pos = pt->saved_read_pos;
1142 pt->read_end = pt->saved_read_end;
1143 pt->read_buf_size = pt->saved_read_buf_size;
1144 }
1145 else
1146 offset = 0;
1147
affc96b5 1148 scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset);
283a1a0e
GH
1149}
1150
ee149d03
JB
1151\f
1152
1153
1154void
a284e297 1155scm_ungetc (int c, SCM port)
c6c79933 1156#define FUNC_NAME "scm_ungetc"
ee149d03 1157{
92c2555f 1158 scm_t_port *pt = SCM_PTAB_ENTRY (port);
840ae05d 1159
6c951427
GH
1160 if (pt->read_buf == pt->putback_buf)
1161 /* already using the put-back buffer. */
1162 {
1163 /* enlarge putback_buf if necessary. */
1164 if (pt->read_end == pt->read_buf + pt->read_buf_size
1165 && pt->read_buf == pt->read_pos)
1166 {
1be6b49c 1167 size_t new_size = pt->read_buf_size * 2;
c6c79933 1168 unsigned char *tmp = (unsigned char *)
4c9419ac
MV
1169 scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size,
1170 "putback buffer");
6c951427 1171
6c951427
GH
1172 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
1173 pt->read_end = pt->read_buf + pt->read_buf_size;
1174 pt->read_buf_size = pt->putback_buf_size = new_size;
1175 }
1176
1177 /* shift any existing bytes to buffer + 1. */
1178 if (pt->read_pos == pt->read_end)
1179 pt->read_end = pt->read_buf + 1;
1180 else if (pt->read_pos != pt->read_buf + 1)
1181 {
1182 int count = pt->read_end - pt->read_pos;
1183
1184 memmove (pt->read_buf + 1, pt->read_pos, count);
1185 pt->read_end = pt->read_buf + 1 + count;
1186 }
1187
1188 pt->read_pos = pt->read_buf;
1189 }
1190 else
1191 /* switch to the put-back buffer. */
1192 {
1193 if (pt->putback_buf == NULL)
1194 {
c357d546 1195 pt->putback_buf
4c9419ac
MV
1196 = (unsigned char *) scm_gc_malloc (SCM_INITIAL_PUTBACK_BUF_SIZE,
1197 "putback buffer");
6c951427
GH
1198 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
1199 }
1200
1201 pt->saved_read_buf = pt->read_buf;
1202 pt->saved_read_pos = pt->read_pos;
1203 pt->saved_read_end = pt->read_end;
1204 pt->saved_read_buf_size = pt->read_buf_size;
1205
1206 pt->read_pos = pt->read_buf = pt->putback_buf;
1207 pt->read_end = pt->read_buf + 1;
1208 pt->read_buf_size = pt->putback_buf_size;
1209 }
1210
1211 *pt->read_buf = c;
ee149d03 1212
840ae05d
JB
1213 if (pt->rw_random)
1214 pt->rw_active = SCM_PORT_READ;
1215
ee149d03
JB
1216 if (c == '\n')
1217 {
1218 /* What should col be in this case?
1219 * We'll leave it at -1.
1220 */
1221 SCM_LINUM (port) -= 1;
1222 }
1223 else
1224 SCM_COL(port) -= 1;
1225}
c6c79933 1226#undef FUNC_NAME
ee149d03
JB
1227
1228
1229void
70d63753 1230scm_ungets (const char *s, int n, SCM port)
ee149d03
JB
1231{
1232 /* This is simple minded and inefficient, but unreading strings is
1233 * probably not a common operation, and remember that line and
1234 * column numbers have to be handled...
1235 *
1236 * Please feel free to write an optimized version!
1237 */
1238 while (n--)
1239 scm_ungetc (s[n], port);
1240}
1241
1242
3b3b36dd 1243SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
1bbd0b84 1244 (SCM port),
1e6808ea
MG
1245 "Return the next character available from @var{port},\n"
1246 "@emph{without} updating @var{port} to point to the following\n"
1247 "character. If no more characters are available, the\n"
c2dfff19
KR
1248 "end-of-file object is returned.\n"
1249 "\n"
1250 "The value returned by\n"
1e6808ea
MG
1251 "a call to @code{peek-char} is the same as the value that would\n"
1252 "have been returned by a call to @code{read-char} on the same\n"
1253 "port. The only difference is that the very next call to\n"
1254 "@code{read-char} or @code{peek-char} on that @var{port} will\n"
1255 "return the value returned by the preceding call to\n"
1256 "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
1257 "an interactive port will hang waiting for input whenever a call\n"
c2dfff19 1258 "to @code{read-char} would have hung.")
1bbd0b84 1259#define FUNC_NAME s_scm_peek_char
ee149d03 1260{
1a973c42 1261 int c, column;
ee149d03
JB
1262 if (SCM_UNBNDP (port))
1263 port = scm_cur_inp;
1264 else
34d19ef6 1265 SCM_VALIDATE_OPINPORT (1, port);
1a973c42 1266 column = SCM_COL(port);
ee149d03
JB
1267 c = scm_getc (port);
1268 if (EOF == c)
1269 return SCM_EOF_VAL;
1270 scm_ungetc (c, port);
1a973c42 1271 SCM_COL(port) = column;
7866a09b 1272 return SCM_MAKE_CHAR (c);
3cb988bd 1273}
1bbd0b84 1274#undef FUNC_NAME
3cb988bd 1275
1be4270a 1276SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
1bbd0b84 1277 (SCM cobj, SCM port),
b380b885
MD
1278 "Place @var{char} in @var{port} so that it will be read by the\n"
1279 "next read operation. If called multiple times, the unread characters\n"
1280 "will be read again in last-in first-out order. If @var{port} is\n"
1281 "not supplied, the current input port is used.")
1bbd0b84 1282#define FUNC_NAME s_scm_unread_char
0f2d19dd
JB
1283{
1284 int c;
1285
34d19ef6 1286 SCM_VALIDATE_CHAR (1, cobj);
0f2d19dd
JB
1287 if (SCM_UNBNDP (port))
1288 port = scm_cur_inp;
1289 else
34d19ef6 1290 SCM_VALIDATE_OPINPORT (2, port);
0f2d19dd 1291
7866a09b 1292 c = SCM_CHAR (cobj);
0f2d19dd 1293
b7f3516f 1294 scm_ungetc (c, port);
0f2d19dd
JB
1295 return cobj;
1296}
1bbd0b84 1297#undef FUNC_NAME
0f2d19dd 1298
a1ec6916 1299SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
1bbd0b84 1300 (SCM str, SCM port),
b380b885
MD
1301 "Place the string @var{str} in @var{port} so that its characters will be\n"
1302 "read in subsequent read operations. If called multiple times, the\n"
1303 "unread characters will be read again in last-in first-out order. If\n"
1304 "@var{port} is not supplied, the current-input-port is used.")
1bbd0b84 1305#define FUNC_NAME s_scm_unread_string
ee1e7e13 1306{
34d19ef6 1307 SCM_VALIDATE_STRING (1, str);
ee1e7e13
MD
1308 if (SCM_UNBNDP (port))
1309 port = scm_cur_inp;
1310 else
34d19ef6 1311 SCM_VALIDATE_OPINPORT (2, port);
ee1e7e13 1312
34f0f2b8 1313 scm_ungets (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port);
ee1e7e13
MD
1314
1315 return str;
1316}
1bbd0b84 1317#undef FUNC_NAME
ee1e7e13 1318
a1ec6916 1319SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
1e6808ea
MG
1320 (SCM fd_port, SCM offset, SCM whence),
1321 "Sets the current position of @var{fd/port} to the integer\n"
1322 "@var{offset}, which is interpreted according to the value of\n"
1323 "@var{whence}.\n"
1324 "\n"
1325 "One of the following variables should be supplied for\n"
1326 "@var{whence}:\n"
b380b885
MD
1327 "@defvar SEEK_SET\n"
1328 "Seek from the beginning of the file.\n"
1329 "@end defvar\n"
1330 "@defvar SEEK_CUR\n"
1331 "Seek from the current position.\n"
1332 "@end defvar\n"
1333 "@defvar SEEK_END\n"
1334 "Seek from the end of the file.\n"
1e6808ea
MG
1335 "@end defvar\n"
1336 "If @var{fd/port} is a file descriptor, the underlying system\n"
1337 "call is @code{lseek}. @var{port} may be a string port.\n"
1338 "\n"
1339 "The value returned is the new position in the file. This means\n"
1340 "that the current position of a port can be obtained using:\n"
1341 "@lisp\n"
b380b885 1342 "(seek port 0 SEEK_CUR)\n"
1e6808ea 1343 "@end lisp")
1bbd0b84 1344#define FUNC_NAME s_scm_seek
840ae05d
JB
1345{
1346 off_t off;
1347 off_t rv;
1348 int how;
1349
1e6808ea 1350 fd_port = SCM_COERCE_OUTPORT (fd_port);
840ae05d 1351
a55c2b68
MV
1352 if (sizeof (off_t) == sizeof (scm_t_intmax))
1353 off = scm_to_intmax (offset);
1354 else
1355 off = scm_to_long (offset);
1356 how = scm_to_int (whence);
1357
840ae05d 1358 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
1bbd0b84 1359 SCM_OUT_OF_RANGE (3, whence);
1e6808ea 1360 if (SCM_OPPORTP (fd_port))
840ae05d 1361 {
92c2555f 1362 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (fd_port);
840ae05d
JB
1363
1364 if (!ptob->seek)
1bbd0b84 1365 SCM_MISC_ERROR ("port is not seekable",
1e6808ea 1366 scm_cons (fd_port, SCM_EOL));
840ae05d 1367 else
1e6808ea 1368 rv = ptob->seek (fd_port, off, how);
840ae05d
JB
1369 }
1370 else /* file descriptor?. */
1371 {
a55c2b68 1372 rv = lseek (scm_to_int (fd_port), off, how);
840ae05d 1373 if (rv == -1)
1bbd0b84 1374 SCM_SYSERROR;
840ae05d 1375 }
a55c2b68 1376 return scm_from_intmax (rv);
840ae05d 1377}
1bbd0b84 1378#undef FUNC_NAME
840ae05d 1379
82893676
MG
1380#ifdef __MINGW32__
1381/* Define this function since it is not supported under Windows. */
1382static int truncate (char *file, int length)
1383{
1384 int ret = -1, fdes;
1385 if ((fdes = open (file, O_BINARY | O_WRONLY)) != -1)
1386 {
1387 ret = chsize (fdes, length);
1388 close (fdes);
1389 }
1390 return ret;
1391}
1392#endif /* __MINGW32__ */
1393
a1ec6916 1394SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
1bbd0b84 1395 (SCM object, SCM length),
1e6808ea
MG
1396 "Truncates the object referred to by @var{object} to at most\n"
1397 "@var{length} bytes. @var{object} can be a string containing a\n"
1398 "file name or an integer file descriptor or a port.\n"
1399 "@var{length} may be omitted if @var{object} is not a file name,\n"
1400 "in which case the truncation occurs at the current port.\n"
1401 "position. The return value is unspecified.")
1bbd0b84 1402#define FUNC_NAME s_scm_truncate_file
840ae05d 1403{
69bc9ff3
GH
1404 int rv;
1405 off_t c_length;
1406
1407 /* object can be a port, fdes or filename. */
840ae05d 1408
840ae05d
JB
1409 if (SCM_UNBNDP (length))
1410 {
69bc9ff3 1411 /* must supply length if object is a filename. */
a6d9e5ab 1412 if (SCM_STRINGP (object))
34d19ef6 1413 SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL);
69bc9ff3 1414
93ccaef0 1415 length = scm_seek (object, SCM_INUM0, SCM_I_MAKINUM (SEEK_CUR));
840ae05d 1416 }
34d19ef6 1417 c_length = SCM_NUM2LONG (2, length);
69bc9ff3 1418 if (c_length < 0)
1bbd0b84 1419 SCM_MISC_ERROR ("negative offset", SCM_EOL);
3fe6190f 1420
69bc9ff3
GH
1421 object = SCM_COERCE_OUTPORT (object);
1422 if (SCM_INUMP (object))
1423 {
1424 SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length));
1425 }
0c95b57d 1426 else if (SCM_OPOUTPORTP (object))
69bc9ff3 1427 {
92c2555f
MV
1428 scm_t_port *pt = SCM_PTAB_ENTRY (object);
1429 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
69bc9ff3 1430
affc96b5 1431 if (!ptob->truncate)
1bbd0b84 1432 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
69bc9ff3 1433 if (pt->rw_active == SCM_PORT_READ)
affc96b5 1434 scm_end_input (object);
69bc9ff3 1435 else if (pt->rw_active == SCM_PORT_WRITE)
affc96b5 1436 ptob->flush (object);
69bc9ff3 1437
affc96b5 1438 ptob->truncate (object, c_length);
69bc9ff3
GH
1439 rv = 0;
1440 }
1441 else
1442 {
a6d9e5ab 1443 SCM_VALIDATE_STRING (1, object);
a6d9e5ab 1444 SCM_SYSCALL (rv = truncate (SCM_STRING_CHARS (object), c_length));
69bc9ff3
GH
1445 }
1446 if (rv == -1)
1bbd0b84 1447 SCM_SYSERROR;
840ae05d
JB
1448 return SCM_UNSPECIFIED;
1449}
1bbd0b84 1450#undef FUNC_NAME
840ae05d 1451
a1ec6916 1452SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
1bbd0b84 1453 (SCM port),
a150979d
KR
1454 "Return the current line number for @var{port}.\n"
1455 "\n"
1456 "The first line of a file is 0. But you might want to add 1\n"
1457 "when printing line numbers, since starting from 1 is\n"
1458 "traditional in error messages, and likely to be more natural to\n"
1459 "non-programmers.")
1bbd0b84 1460#define FUNC_NAME s_scm_port_line
0f2d19dd 1461{
78446828 1462 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1463 SCM_VALIDATE_OPENPORT (1, port);
93ccaef0 1464 return SCM_I_MAKINUM (SCM_LINUM (port));
0f2d19dd 1465}
1bbd0b84 1466#undef FUNC_NAME
0f2d19dd 1467
a1ec6916 1468SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
1bbd0b84 1469 (SCM port, SCM line),
a150979d
KR
1470 "Set the current line number for @var{port} to @var{line}. The\n"
1471 "first line of a file is 0.")
1bbd0b84 1472#define FUNC_NAME s_scm_set_port_line_x
d043d8c2 1473{
360fc44c 1474 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1475 SCM_VALIDATE_OPENPORT (1, port);
a55c2b68 1476 SCM_PTAB_ENTRY (port)->line_number = scm_to_int (line);
564478fd 1477 return SCM_UNSPECIFIED;
d043d8c2 1478}
1bbd0b84 1479#undef FUNC_NAME
d043d8c2 1480
a1ec6916 1481SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
1bbd0b84 1482 (SCM port),
a150979d
KR
1483 "Return the current column number of @var{port}.\n"
1484 "If the number is\n"
b380b885
MD
1485 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1486 "- i.e. the first character of the first line is line 0, column 0.\n"
1487 "(However, when you display a file position, for example in an error\n"
650a1cf9 1488 "message, we recommend you add 1 to get 1-origin integers. This is\n"
b380b885
MD
1489 "because lines and column numbers traditionally start with 1, and that is\n"
1490 "what non-programmers will find most natural.)")
1bbd0b84 1491#define FUNC_NAME s_scm_port_column
0f2d19dd 1492{
78446828 1493 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1494 SCM_VALIDATE_OPENPORT (1, port);
93ccaef0 1495 return SCM_I_MAKINUM (SCM_COL (port));
0f2d19dd 1496}
1bbd0b84 1497#undef FUNC_NAME
0f2d19dd 1498
a1ec6916 1499SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
1bbd0b84 1500 (SCM port, SCM column),
a150979d
KR
1501 "Set the current column of @var{port}. Before reading the first\n"
1502 "character on a line the column should be 0.")
1bbd0b84 1503#define FUNC_NAME s_scm_set_port_column_x
d043d8c2 1504{
360fc44c 1505 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1506 SCM_VALIDATE_OPENPORT (1, port);
a55c2b68 1507 SCM_PTAB_ENTRY (port)->column_number = scm_to_int (column);
564478fd 1508 return SCM_UNSPECIFIED;
d043d8c2 1509}
1bbd0b84 1510#undef FUNC_NAME
d043d8c2 1511
a1ec6916 1512SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
1bbd0b84 1513 (SCM port),
b380b885 1514 "Return the filename associated with @var{port}. This function returns\n"
2a2a730b 1515 "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
a3c8b9fc 1516 "when called on the current input, output and error ports respectively.")
1bbd0b84 1517#define FUNC_NAME s_scm_port_filename
0f2d19dd 1518{
78446828 1519 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1520 SCM_VALIDATE_OPENPORT (1, port);
b24b5e13 1521 return SCM_FILENAME (port);
0f2d19dd 1522}
1bbd0b84 1523#undef FUNC_NAME
0f2d19dd 1524
a1ec6916 1525SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
1bbd0b84 1526 (SCM port, SCM filename),
b380b885
MD
1527 "Change the filename associated with @var{port}, using the current input\n"
1528 "port if none is specified. Note that this does not change the port's\n"
1529 "source of data, but only the value that is returned by\n"
1530 "@code{port-filename} and reported in diagnostic output.")
1bbd0b84 1531#define FUNC_NAME s_scm_set_port_filename_x
d14af9f2 1532{
360fc44c 1533 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1534 SCM_VALIDATE_OPENPORT (1, port);
360fc44c 1535 /* We allow the user to set the filename to whatever he likes. */
b24b5e13
DH
1536 SCM_SET_FILENAME (port, filename);
1537 return SCM_UNSPECIFIED;
d14af9f2 1538}
1bbd0b84 1539#undef FUNC_NAME
d14af9f2 1540
0f2d19dd
JB
1541#ifndef ttyname
1542extern char * ttyname();
1543#endif
1544
f12733c9
MD
1545void
1546scm_print_port_mode (SCM exp, SCM port)
1547{
1548 scm_puts (SCM_CLOSEDP (exp)
1549 ? "closed: "
f9a64404
DH
1550 : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
1551 ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
f12733c9
MD
1552 ? "input-output: "
1553 : "input: ")
f9a64404 1554 : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
f12733c9
MD
1555 ? "output: "
1556 : "bogus: ")),
1557 port);
1558}
1cc91f1b 1559
f12733c9 1560int
e81d98ec 1561scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
0f2d19dd 1562{
f12733c9
MD
1563 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
1564 if (!type)
1565 type = "port";
b7f3516f 1566 scm_puts ("#<", port);
f12733c9 1567 scm_print_port_mode (exp, port);
b7f3516f
TT
1568 scm_puts (type, port);
1569 scm_putc (' ', port);
12a8b769 1570 scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
b7f3516f 1571 scm_putc ('>', port);
f12733c9 1572 return 1;
0f2d19dd
JB
1573}
1574
0f2d19dd
JB
1575void
1576scm_ports_prehistory ()
0f2d19dd
JB
1577{
1578 scm_numptob = 0;
67329a9e 1579 scm_ptobs = (scm_t_ptob_descriptor *) scm_malloc (sizeof (scm_t_ptob_descriptor));
0f2d19dd 1580}
0f2d19dd
JB
1581
1582\f
ee149d03 1583
d68fee48 1584/* Void ports. */
0f2d19dd 1585
92c2555f 1586scm_t_bits scm_tc16_void_port = 0;
0f2d19dd 1587
e81d98ec 1588static int fill_input_void_port (SCM port SCM_UNUSED)
283a1a0e 1589{
70df8af6 1590 return EOF;
283a1a0e
GH
1591}
1592
31703ab8 1593static void
e81d98ec
DH
1594write_void_port (SCM port SCM_UNUSED,
1595 const void *data SCM_UNUSED,
1596 size_t size SCM_UNUSED)
31703ab8
GH
1597{
1598}
1599
0f2d19dd 1600SCM
a284e297 1601scm_void_port (char *mode_str)
0f2d19dd 1602{
b9ad392e 1603 scm_mutex_lock (&scm_i_port_table_mutex);
402788a9
HWN
1604 {
1605 int mode_bits = scm_mode_bits (mode_str);
da220f27
HWN
1606 SCM answer = scm_new_port_table_entry (scm_tc16_void_port);
1607 scm_t_port * pt = SCM_PTAB_ENTRY(answer);
1608
402788a9 1609 scm_port_non_buffer (pt);
402788a9
HWN
1610
1611 SCM_SETSTREAM (answer, 0);
1612 SCM_SET_CELL_TYPE (answer, scm_tc16_void_port | mode_bits);
b9ad392e 1613 scm_mutex_unlock (&scm_i_port_table_mutex);
402788a9
HWN
1614 return answer;
1615 }
0f2d19dd
JB
1616}
1617
a1ec6916 1618SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
1bbd0b84 1619 (SCM mode),
70df8af6 1620 "Create and return a new void port. A void port acts like\n"
bb2c02f2 1621 "@file{/dev/null}. The @var{mode} argument\n"
70df8af6 1622 "specifies the input/output modes for this port: see the\n"
b380b885 1623 "documentation for @code{open-file} in @ref{File Ports}.")
1bbd0b84 1624#define FUNC_NAME s_scm_sys_make_void_port
0f2d19dd 1625{
a6d9e5ab 1626 SCM_VALIDATE_STRING (1, mode);
a6d9e5ab 1627 return scm_void_port (SCM_STRING_CHARS (mode));
0f2d19dd 1628}
1bbd0b84 1629#undef FUNC_NAME
0f2d19dd 1630
0f2d19dd 1631\f
89545eba 1632/* Initialization. */
1cc91f1b 1633
0f2d19dd
JB
1634void
1635scm_init_ports ()
0f2d19dd 1636{
840ae05d 1637 /* lseek() symbols. */
93ccaef0
MV
1638 scm_c_define ("SEEK_SET", SCM_I_MAKINUM (SEEK_SET));
1639 scm_c_define ("SEEK_CUR", SCM_I_MAKINUM (SEEK_CUR));
1640 scm_c_define ("SEEK_END", SCM_I_MAKINUM (SEEK_END));
840ae05d 1641
70df8af6
GH
1642 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
1643 write_void_port);
a0599745 1644#include "libguile/ports.x"
0f2d19dd 1645}
89e00824
ML
1646
1647/*
1648 Local Variables:
1649 c-file-style: "gnu"
1650 End:
1651*/