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