("scm_new_port_table_entry"): return a boxed SCM in
[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
f5fd8aa2 449scm_t_port **scm_port_table;
0f2d19dd 450
f5fd8aa2
MV
451long scm_port_table_size = 0; /* Number of ports in scm_port_table. */
452long scm_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{
da220f27 459 SCM z = scm_cell (SCM_EOL, SCM_EOL);
39e8f371 460 scm_t_port *entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port");
f5fd8aa2 461 if (scm_port_table_size == scm_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. */
4c9419ac
MV
465 void *newt = scm_realloc ((char *) scm_port_table,
466 (size_t) (sizeof (scm_t_port *)
467 * scm_port_table_room * 2));
f5fd8aa2
MV
468 scm_port_table = (scm_t_port **) newt;
469 scm_port_table_room *= 2;
0f2d19dd 470 }
840ae05d 471
f5fd8aa2 472 entry->entry = scm_port_table_size;
5f16b897 473
840ae05d 474 entry->file_name = SCM_BOOL_F;
61e452ba 475 entry->rw_active = SCM_PORT_NEITHER;
5f16b897 476
f5fd8aa2
MV
477 scm_port_table[scm_port_table_size] = entry;
478 scm_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
6c951427 488/* Remove a port from the table and destroy it. */
0f2d19dd 489void
a284e297 490scm_remove_from_port_table (SCM port)
db4b4ca6 491#define FUNC_NAME "scm_remove_from_port_table"
0f2d19dd 492{
92c2555f 493 scm_t_port *p = SCM_PTAB_ENTRY (port);
c014a02e 494 long i = p->entry;
6c951427 495
f5fd8aa2 496 if (i >= scm_port_table_size)
1afff620 497 SCM_MISC_ERROR ("Port not in table: ~S", scm_list_1 (port));
6c951427 498 if (p->putback_buf)
4c9419ac
MV
499 scm_gc_free (p->putback_buf, p->putback_buf_size, "putback buffer");
500 scm_gc_free (p, sizeof (scm_t_port), "port");
ee1e7e13
MD
501 /* Since we have just freed slot i we can shrink the table by moving
502 the last entry to that slot... */
f5fd8aa2 503 if (i < scm_port_table_size - 1)
0f2d19dd 504 {
f5fd8aa2
MV
505 scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
506 scm_port_table[i]->entry = i;
0f2d19dd 507 }
0f2d19dd 508 SCM_SETPTAB_ENTRY (port, 0);
f5fd8aa2 509 scm_port_table_size--;
0f2d19dd 510}
db4b4ca6
DH
511#undef FUNC_NAME
512
0f2d19dd 513
fea6b4ea 514#ifdef GUILE_DEBUG
b450f070 515/* Functions for debugging. */
1cc91f1b 516
3b3b36dd 517SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
b450f070 518 (),
1e6808ea 519 "Return the number of ports in the port table. @code{pt-size}\n"
5352393c 520 "is only included in @code{--enable-guile-debug} builds.")
1bbd0b84 521#define FUNC_NAME s_scm_pt_size
0f2d19dd 522{
f5fd8aa2 523 return SCM_MAKINUM (scm_port_table_size);
0f2d19dd 524}
1bbd0b84 525#undef FUNC_NAME
0f2d19dd 526
3b3b36dd 527SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
b450f070 528 (SCM index),
1e6808ea 529 "Return the port at @var{index} in the port table.\n"
5352393c
MG
530 "@code{pt-member} is only included in\n"
531 "@code{--enable-guile-debug} builds.")
1bbd0b84 532#define FUNC_NAME s_scm_pt_member
0f2d19dd 533{
c014a02e 534 long i;
34d19ef6 535 SCM_VALIDATE_INUM_COPY (1, index, i);
f5fd8aa2 536 if (i < 0 || i >= scm_port_table_size)
0f2d19dd
JB
537 return SCM_BOOL_F;
538 else
f5fd8aa2 539 return scm_port_table[i]->port;
0f2d19dd 540}
1bbd0b84 541#undef FUNC_NAME
0f2d19dd
JB
542#endif
543
70df8af6 544void
92c2555f 545scm_port_non_buffer (scm_t_port *pt)
70df8af6
GH
546{
547 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
548 pt->write_buf = pt->write_pos = &pt->shortbuf;
549 pt->read_buf_size = pt->write_buf_size = 1;
550 pt->write_end = pt->write_buf + pt->write_buf_size;
551}
0f2d19dd 552
d68fee48
JB
553\f
554/* Revealed counts --- an oddity inherited from SCSH. */
555
8b13c6b3
GH
556/* Find a port in the table and return its revealed count.
557 Also used by the garbage collector.
0f2d19dd 558 */
1cc91f1b 559
0f2d19dd 560int
a284e297 561scm_revealed_count (SCM port)
0f2d19dd
JB
562{
563 return SCM_REVEALED(port);
564}
565
566
567
568/* Return the revealed count for a port. */
569
3b3b36dd 570SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
1bbd0b84 571 (SCM port),
1e6808ea 572 "Return the revealed count for @var{port}.")
1bbd0b84 573#define FUNC_NAME s_scm_port_revealed
0f2d19dd 574{
78446828 575 port = SCM_COERCE_OUTPORT (port);
34d19ef6 576 SCM_VALIDATE_OPENPORT (1, port);
8b13c6b3 577 return SCM_MAKINUM (scm_revealed_count (port));
0f2d19dd 578}
1bbd0b84 579#undef FUNC_NAME
0f2d19dd
JB
580
581/* Set the revealed count for a port. */
3b3b36dd 582SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
1bbd0b84 583 (SCM port, SCM rcount),
b450f070 584 "Sets the revealed count for a port to a given value.\n"
b380b885 585 "The return value is unspecified.")
1bbd0b84 586#define FUNC_NAME s_scm_set_port_revealed_x
0f2d19dd 587{
78446828 588 port = SCM_COERCE_OUTPORT (port);
34d19ef6
HWN
589 SCM_VALIDATE_OPENPORT (1, port);
590 SCM_VALIDATE_INUM (2, rcount);
0f2d19dd 591 SCM_REVEALED (port) = SCM_INUM (rcount);
8b13c6b3 592 return SCM_UNSPECIFIED;
0f2d19dd 593}
1bbd0b84 594#undef FUNC_NAME
0f2d19dd 595
d68fee48
JB
596
597\f
598/* Retrieving a port's mode. */
599
eadd48de
GH
600/* Return the flags that characterize a port based on the mode
601 * string used to open a file for that port.
602 *
603 * See PORT FLAGS in scm.h
604 */
605
606long
a284e297 607scm_mode_bits (char *modes)
eadd48de
GH
608{
609 return (SCM_OPN
610 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
611 | ( strchr (modes, 'w')
612 || strchr (modes, 'a')
613 || strchr (modes, '+') ? SCM_WRTNG : 0)
ee149d03
JB
614 | (strchr (modes, '0') ? SCM_BUF0 : 0)
615 | (strchr (modes, 'l') ? SCM_BUFLINE : 0));
eadd48de
GH
616}
617
618
619/* Return the mode flags from an open port.
620 * Some modes such as "append" are only used when opening
621 * a file and are not returned here. */
622
3b3b36dd 623SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
1bbd0b84 624 (SCM port),
1e6808ea
MG
625 "Return the port modes associated with the open port @var{port}.\n"
626 "These will not necessarily be identical to the modes used when\n"
627 "the port was opened, since modes such as \"append\" which are\n"
628 "used only during port creation are not retained.")
1bbd0b84 629#define FUNC_NAME s_scm_port_mode
eadd48de 630{
26a3038d 631 char modes[4];
eadd48de 632 modes[0] = '\0';
78446828
MV
633
634 port = SCM_COERCE_OUTPORT (port);
34d19ef6 635 SCM_VALIDATE_OPPORT (1, port);
f9a64404
DH
636 if (SCM_CELL_WORD_0 (port) & SCM_RDNG) {
637 if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
eadd48de
GH
638 strcpy (modes, "r+");
639 else
640 strcpy (modes, "r");
641 }
f9a64404 642 else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
eadd48de 643 strcpy (modes, "w");
f9a64404 644 if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
eadd48de 645 strcat (modes, "0");
36284627 646 return scm_mem2string (modes, strlen (modes));
eadd48de 647}
1bbd0b84 648#undef FUNC_NAME
eadd48de
GH
649
650
d68fee48
JB
651\f
652/* Closing ports. */
653
0f2d19dd
JB
654/* scm_close_port
655 * Call the close operation on a port object.
eadd48de 656 * see also scm_close.
0f2d19dd 657 */
3b3b36dd 658SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
1bbd0b84 659 (SCM port),
1e6808ea
MG
660 "Close the specified port object. Return @code{#t} if it\n"
661 "successfully closes a port or @code{#f} if it was already\n"
662 "closed. An exception may be raised if an error occurs, for\n"
663 "example when flushing buffered output. See also @ref{Ports and\n"
664 "File Descriptors, close}, for a procedure which can close file\n"
665 "descriptors.")
1bbd0b84 666#define FUNC_NAME s_scm_close_port
0f2d19dd 667{
1be6b49c 668 size_t i;
eadd48de
GH
669 int rv;
670
78446828
MV
671 port = SCM_COERCE_OUTPORT (port);
672
7a754ca6 673 SCM_VALIDATE_PORT (1, port);
0f2d19dd 674 if (SCM_CLOSEDP (port))
eadd48de 675 return SCM_BOOL_F;
0f2d19dd 676 i = SCM_PTOBNUM (port);
affc96b5
GH
677 if (scm_ptobs[i].close)
678 rv = (scm_ptobs[i].close) (port);
eadd48de
GH
679 else
680 rv = 0;
0f2d19dd 681 scm_remove_from_port_table (port);
22a52da1 682 SCM_CLR_PORT_OPEN_FLAG (port);
36284627 683 return SCM_BOOL (rv >= 0);
7a754ca6
MD
684}
685#undef FUNC_NAME
686
687SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0,
688 (SCM port),
689 "Close the specified input port object. The routine has no effect if\n"
690 "the file has already been closed. An exception may be raised if an\n"
691 "error occurs. The value returned is unspecified.\n\n"
692 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
693 "which can close file descriptors.")
694#define FUNC_NAME s_scm_close_input_port
695{
696 SCM_VALIDATE_INPUT_PORT (1, port);
697 scm_close_port (port);
698 return SCM_UNSPECIFIED;
699}
700#undef FUNC_NAME
701
702SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0,
703 (SCM port),
704 "Close the specified output port object. The routine has no effect if\n"
705 "the file has already been closed. An exception may be raised if an\n"
706 "error occurs. The value returned is unspecified.\n\n"
707 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
708 "which can close file descriptors.")
709#define FUNC_NAME s_scm_close_output_port
710{
711 port = SCM_COERCE_OUTPORT (port);
712 SCM_VALIDATE_OUTPUT_PORT (1, port);
713 scm_close_port (port);
714 return SCM_UNSPECIFIED;
0f2d19dd 715}
1bbd0b84 716#undef FUNC_NAME
0f2d19dd 717
c2ca4493
GH
718SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
719 (SCM proc),
720 "Apply @var{proc} to each port in the Guile port table\n"
2f1bbcfd
MV
721 "in turn. The return value is unspecified. More specifically,\n"
722 "@var{proc} is applied exactly once to every port that exists\n"
723 "in the system at the time @var{port-for-each} is invoked.\n"
724 "Changes to the port table while @var{port-for-each} is running\n"
9401323e 725 "have no effect as far as @var{port-for-each} is concerned.")
c2ca4493
GH
726#define FUNC_NAME s_scm_port_for_each
727{
c014a02e 728 long i;
fdfe6305
MV
729 SCM ports;
730
c2ca4493
GH
731 SCM_VALIDATE_PROC (1, proc);
732
733 /* when pre-emptive multithreading is supported, access to the port
734 table will need to be controlled by a mutex. */
fdfe6305
MV
735
736 /* Even without pre-emptive multithreading, running arbitrary code
737 while scanning the port table is unsafe because the port table
738 can change arbitrarily (from a GC, for example). So we build a
739 list in advance while blocking the GC. -mvo */
740
c2ca4493 741 SCM_DEFER_INTS;
fdfe6305
MV
742 scm_block_gc++;
743 ports = SCM_EOL;
f5fd8aa2
MV
744 for (i = 0; i < scm_port_table_size; i++)
745 ports = scm_cons (scm_port_table[i]->port, ports);
fdfe6305
MV
746 scm_block_gc--;
747 SCM_ALLOW_INTS;
748
749 while (ports != SCM_EOL)
c2ca4493 750 {
fdc28395 751 scm_call_1 (proc, SCM_CAR (ports));
fdfe6305 752 ports = SCM_CDR (ports);
c2ca4493 753 }
fdfe6305 754
c2ca4493
GH
755 return SCM_UNSPECIFIED;
756}
757#undef FUNC_NAME
758
d68fee48
JB
759\f
760/* Utter miscellany. Gosh, we should clean this up some time. */
761
3b3b36dd 762SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
1bbd0b84 763 (SCM x),
1e6808ea
MG
764 "Return @code{#t} if @var{x} is an input port, otherwise return\n"
765 "@code{#f}. Any object satisfying this predicate also satisfies\n"
766 "@code{port?}.")
1bbd0b84 767#define FUNC_NAME s_scm_input_port_p
0f2d19dd 768{
36284627 769 return SCM_BOOL (SCM_INPUT_PORT_P (x));
0f2d19dd 770}
1bbd0b84 771#undef FUNC_NAME
0f2d19dd 772
3b3b36dd 773SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
1bbd0b84 774 (SCM x),
1e6808ea
MG
775 "Return @code{#t} if @var{x} is an output port, otherwise return\n"
776 "@code{#f}. Any object satisfying this predicate also satisfies\n"
777 "@code{port?}.")
1bbd0b84 778#define FUNC_NAME s_scm_output_port_p
0f2d19dd 779{
82893676 780 x = SCM_COERCE_OUTPORT (x);
36284627 781 return SCM_BOOL (SCM_OUTPUT_PORT_P (x));
0f2d19dd 782}
1bbd0b84 783#undef FUNC_NAME
0f2d19dd 784
eb5c0a2a
GH
785SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
786 (SCM x),
1e6808ea 787 "Return a boolean indicating whether @var{x} is a port.\n"
5352393c
MG
788 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
789 "@var{x}))}.")
eb5c0a2a
GH
790#define FUNC_NAME s_scm_port_p
791{
792 return SCM_BOOL (SCM_PORTP (x));
793}
794#undef FUNC_NAME
795
3b3b36dd 796SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
1bbd0b84 797 (SCM port),
1e6808ea
MG
798 "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
799 "open.")
1bbd0b84 800#define FUNC_NAME s_scm_port_closed_p
60d0643d 801{
34d19ef6 802 SCM_VALIDATE_PORT (1, port);
36284627 803 return SCM_BOOL (!SCM_OPPORTP (port));
60d0643d 804}
1bbd0b84 805#undef FUNC_NAME
0f2d19dd 806
3b3b36dd 807SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
1bbd0b84 808 (SCM x),
1e6808ea
MG
809 "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
810 "return @code{#f}.")
1bbd0b84 811#define FUNC_NAME s_scm_eof_object_p
0f2d19dd 812{
1bbd0b84 813 return SCM_BOOL(SCM_EOF_OBJECT_P (x));
0f2d19dd 814}
1bbd0b84 815#undef FUNC_NAME
0f2d19dd 816
3b3b36dd 817SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
1bbd0b84 818 (SCM port),
b380b885 819 "Flush the specified output port, or the current output port if @var{port}\n"
9401323e 820 "is omitted. The current output buffer contents are passed to the\n"
b380b885
MD
821 "underlying port implementation (e.g., in the case of fports, the\n"
822 "data will be written to the file and the output buffer will be cleared.)\n"
823 "It has no effect on an unbuffered port.\n\n"
824 "The return value is unspecified.")
1bbd0b84 825#define FUNC_NAME s_scm_force_output
0f2d19dd
JB
826{
827 if (SCM_UNBNDP (port))
3e877d15 828 port = scm_cur_outp;
0f2d19dd 829 else
78446828
MV
830 {
831 port = SCM_COERCE_OUTPORT (port);
34d19ef6 832 SCM_VALIDATE_OPOUTPORT (1, port);
78446828 833 }
affc96b5 834 scm_flush (port);
ee149d03 835 return SCM_UNSPECIFIED;
0f2d19dd 836}
1bbd0b84 837#undef FUNC_NAME
0f2d19dd 838
a1ec6916 839SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
1bbd0b84 840 (),
b380b885
MD
841 "Equivalent to calling @code{force-output} on\n"
842 "all open output ports. The return value is unspecified.")
1bbd0b84 843#define FUNC_NAME s_scm_flush_all_ports
89ea5b7c 844{
1be6b49c 845 size_t i;
89ea5b7c 846
f5fd8aa2 847 for (i = 0; i < scm_port_table_size; i++)
89ea5b7c 848 {
f5fd8aa2
MV
849 if (SCM_OPOUTPORTP (scm_port_table[i]->port))
850 scm_flush (scm_port_table[i]->port);
89ea5b7c
GH
851 }
852 return SCM_UNSPECIFIED;
853}
1bbd0b84 854#undef FUNC_NAME
0f2d19dd 855
3b3b36dd 856SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
1bbd0b84 857 (SCM port),
1e6808ea
MG
858 "Return the next character available from @var{port}, updating\n"
859 "@var{port} to point to the following character. If no more\n"
860 "characters are available, the end-of-file object is returned.")
1bbd0b84 861#define FUNC_NAME s_scm_read_char
0f2d19dd
JB
862{
863 int c;
864 if (SCM_UNBNDP (port))
334341aa 865 port = scm_cur_inp;
34d19ef6 866 SCM_VALIDATE_OPINPORT (1, port);
b7f3516f 867 c = scm_getc (port);
0f2d19dd
JB
868 if (EOF == c)
869 return SCM_EOF_VAL;
7866a09b 870 return SCM_MAKE_CHAR (c);
0f2d19dd 871}
1bbd0b84 872#undef FUNC_NAME
0f2d19dd 873
5c070ca7 874/* this should only be called when the read buffer is empty. it
affc96b5 875 tries to refill the read buffer. it returns the first char from
5c070ca7 876 the port, which is either EOF or *(pt->read_pos). */
6c951427 877int
affc96b5 878scm_fill_input (SCM port)
6c951427 879{
92c2555f 880 scm_t_port *pt = SCM_PTAB_ENTRY (port);
283a1a0e 881
6c951427
GH
882 if (pt->read_buf == pt->putback_buf)
883 {
884 /* finished reading put-back chars. */
885 pt->read_buf = pt->saved_read_buf;
886 pt->read_pos = pt->saved_read_pos;
887 pt->read_end = pt->saved_read_end;
888 pt->read_buf_size = pt->saved_read_buf_size;
889 if (pt->read_pos < pt->read_end)
5c070ca7 890 return *(pt->read_pos);
6c951427 891 }
affc96b5 892 return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port);
6c951427
GH
893}
894
ee149d03 895int
a284e297 896scm_getc (SCM port)
0f2d19dd
JB
897{
898 int c;
92c2555f 899 scm_t_port *pt = SCM_PTAB_ENTRY (port);
ee149d03 900
840ae05d
JB
901 if (pt->rw_active == SCM_PORT_WRITE)
902 {
affc96b5
GH
903 /* may be marginally faster than calling scm_flush. */
904 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
840ae05d 905 }
6c951427 906
5c070ca7
GH
907 if (pt->rw_random)
908 pt->rw_active = SCM_PORT_READ;
909
910 if (pt->read_pos >= pt->read_end)
ee149d03 911 {
affc96b5 912 if (scm_fill_input (port) == EOF)
5c070ca7 913 return EOF;
ee149d03
JB
914 }
915
5c070ca7 916 c = *(pt->read_pos++);
840ae05d 917
ee149d03
JB
918 if (c == '\n')
919 {
920 SCM_INCLINE (port);
921 }
922 else if (c == '\t')
923 {
924 SCM_TABCOL (port);
925 }
926 else
927 {
928 SCM_INCCOL (port);
929 }
930
931 return c;
0f2d19dd
JB
932}
933
ee149d03 934void
a284e297 935scm_putc (char c, SCM port)
ee149d03 936{
265e6a4d 937 scm_lfwrite (&c, 1, port);
ee149d03 938}
3cb988bd 939
ee149d03 940void
70d63753 941scm_puts (const char *s, SCM port)
3cb988bd 942{
265e6a4d 943 scm_lfwrite (s, strlen (s), port);
ee149d03 944}
3cb988bd 945
6fe692e9
MD
946/* scm_lfwrite
947 *
53802ea8
MV
948 * This function differs from scm_c_write; it updates port line and
949 * column. */
6fe692e9 950
ee149d03 951void
1be6b49c 952scm_lfwrite (const char *ptr, size_t size, SCM port)
ee149d03 953{
92c2555f
MV
954 scm_t_port *pt = SCM_PTAB_ENTRY (port);
955 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
3e2043c4 956
840ae05d 957 if (pt->rw_active == SCM_PORT_READ)
affc96b5 958 scm_end_input (port);
283a1a0e 959
31703ab8 960 ptob->write (port, ptr, size);
840ae05d 961
53802ea8
MV
962 for (; size; ptr++, size--) {
963 if (*ptr == '\n') {
964 SCM_INCLINE(port);
965 }
966 else if (*ptr == '\t') {
967 SCM_TABCOL(port);
968 }
969 else {
970 SCM_INCCOL(port);
971 }
972 }
973
840ae05d
JB
974 if (pt->rw_random)
975 pt->rw_active = SCM_PORT_WRITE;
ee149d03 976}
3cb988bd 977
6fe692e9
MD
978/* scm_c_read
979 *
980 * Used by an application to read arbitrary number of bytes from an
981 * SCM port. Same semantics as libc read, except that scm_c_read only
982 * returns less than SIZE bytes if at end-of-file.
983 *
984 * Warning: Doesn't update port line and column counts! */
985
1be6b49c
ML
986size_t
987scm_c_read (SCM port, void *buffer, size_t size)
6fe692e9 988{
92c2555f 989 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1be6b49c 990 size_t n_read = 0, n_available;
6fe692e9
MD
991
992 if (pt->rw_active == SCM_PORT_WRITE)
993 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
994
995 if (pt->rw_random)
996 pt->rw_active = SCM_PORT_READ;
997
998 if (SCM_READ_BUFFER_EMPTY_P (pt))
999 {
1000 if (scm_fill_input (port) == EOF)
1001 return 0;
1002 }
1003
1004 n_available = pt->read_end - pt->read_pos;
1005
1006 while (n_available < size)
1007 {
1008 memcpy (buffer, pt->read_pos, n_available);
910d1e40 1009 buffer = (char *) buffer + n_available;
6fe692e9
MD
1010 pt->read_pos += n_available;
1011 n_read += n_available;
1012
1013 if (SCM_READ_BUFFER_EMPTY_P (pt))
1014 {
1015 if (scm_fill_input (port) == EOF)
1016 return n_read;
1017 }
1018
1019 size -= n_available;
1020 n_available = pt->read_end - pt->read_pos;
1021 }
1022
1023 memcpy (buffer, pt->read_pos, size);
1024 pt->read_pos += size;
1025
1026 return n_read + size;
1027}
1028
1029/* scm_c_write
1030 *
1031 * Used by an application to write arbitrary number of bytes to an SCM
1032 * port. Similar semantics as libc write. However, unlike libc
1033 * write, scm_c_write writes the requested number of bytes and has no
1034 * return value.
1035 *
1036 * Warning: Doesn't update port line and column counts!
1037 */
1038
1039void
1be6b49c 1040scm_c_write (SCM port, const void *ptr, size_t size)
6fe692e9 1041{
92c2555f
MV
1042 scm_t_port *pt = SCM_PTAB_ENTRY (port);
1043 scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
6fe692e9
MD
1044
1045 if (pt->rw_active == SCM_PORT_READ)
1046 scm_end_input (port);
1047
1048 ptob->write (port, ptr, size);
1049
1050 if (pt->rw_random)
1051 pt->rw_active = SCM_PORT_WRITE;
1052}
3cb988bd 1053
ee149d03 1054void
a284e297 1055scm_flush (SCM port)
ee149d03 1056{
c014a02e 1057 long i = SCM_PTOBNUM (port);
affc96b5 1058 (scm_ptobs[i].flush) (port);
ee149d03
JB
1059}
1060
283a1a0e 1061void
a284e297 1062scm_end_input (SCM port)
283a1a0e 1063{
c014a02e 1064 long offset;
92c2555f 1065 scm_t_port *pt = SCM_PTAB_ENTRY (port);
283a1a0e
GH
1066
1067 if (pt->read_buf == pt->putback_buf)
1068 {
1069 offset = pt->read_end - pt->read_pos;
1070 pt->read_buf = pt->saved_read_buf;
1071 pt->read_pos = pt->saved_read_pos;
1072 pt->read_end = pt->saved_read_end;
1073 pt->read_buf_size = pt->saved_read_buf_size;
1074 }
1075 else
1076 offset = 0;
1077
affc96b5 1078 scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset);
283a1a0e
GH
1079}
1080
ee149d03
JB
1081\f
1082
1083
1084void
a284e297 1085scm_ungetc (int c, SCM port)
c6c79933 1086#define FUNC_NAME "scm_ungetc"
ee149d03 1087{
92c2555f 1088 scm_t_port *pt = SCM_PTAB_ENTRY (port);
840ae05d 1089
6c951427
GH
1090 if (pt->read_buf == pt->putback_buf)
1091 /* already using the put-back buffer. */
1092 {
1093 /* enlarge putback_buf if necessary. */
1094 if (pt->read_end == pt->read_buf + pt->read_buf_size
1095 && pt->read_buf == pt->read_pos)
1096 {
1be6b49c 1097 size_t new_size = pt->read_buf_size * 2;
c6c79933 1098 unsigned char *tmp = (unsigned char *)
4c9419ac
MV
1099 scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size,
1100 "putback buffer");
6c951427 1101
6c951427
GH
1102 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
1103 pt->read_end = pt->read_buf + pt->read_buf_size;
1104 pt->read_buf_size = pt->putback_buf_size = new_size;
1105 }
1106
1107 /* shift any existing bytes to buffer + 1. */
1108 if (pt->read_pos == pt->read_end)
1109 pt->read_end = pt->read_buf + 1;
1110 else if (pt->read_pos != pt->read_buf + 1)
1111 {
1112 int count = pt->read_end - pt->read_pos;
1113
1114 memmove (pt->read_buf + 1, pt->read_pos, count);
1115 pt->read_end = pt->read_buf + 1 + count;
1116 }
1117
1118 pt->read_pos = pt->read_buf;
1119 }
1120 else
1121 /* switch to the put-back buffer. */
1122 {
1123 if (pt->putback_buf == NULL)
1124 {
c357d546 1125 pt->putback_buf
4c9419ac
MV
1126 = (unsigned char *) scm_gc_malloc (SCM_INITIAL_PUTBACK_BUF_SIZE,
1127 "putback buffer");
6c951427
GH
1128 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
1129 }
1130
1131 pt->saved_read_buf = pt->read_buf;
1132 pt->saved_read_pos = pt->read_pos;
1133 pt->saved_read_end = pt->read_end;
1134 pt->saved_read_buf_size = pt->read_buf_size;
1135
1136 pt->read_pos = pt->read_buf = pt->putback_buf;
1137 pt->read_end = pt->read_buf + 1;
1138 pt->read_buf_size = pt->putback_buf_size;
1139 }
1140
1141 *pt->read_buf = c;
ee149d03 1142
840ae05d
JB
1143 if (pt->rw_random)
1144 pt->rw_active = SCM_PORT_READ;
1145
ee149d03
JB
1146 if (c == '\n')
1147 {
1148 /* What should col be in this case?
1149 * We'll leave it at -1.
1150 */
1151 SCM_LINUM (port) -= 1;
1152 }
1153 else
1154 SCM_COL(port) -= 1;
1155}
c6c79933 1156#undef FUNC_NAME
ee149d03
JB
1157
1158
1159void
70d63753 1160scm_ungets (const char *s, int n, SCM port)
ee149d03
JB
1161{
1162 /* This is simple minded and inefficient, but unreading strings is
1163 * probably not a common operation, and remember that line and
1164 * column numbers have to be handled...
1165 *
1166 * Please feel free to write an optimized version!
1167 */
1168 while (n--)
1169 scm_ungetc (s[n], port);
1170}
1171
1172
3b3b36dd 1173SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
1bbd0b84 1174 (SCM port),
1e6808ea
MG
1175 "Return the next character available from @var{port},\n"
1176 "@emph{without} updating @var{port} to point to the following\n"
1177 "character. If no more characters are available, the\n"
1178 "end-of-file object is returned.@footnote{The value returned by\n"
1179 "a call to @code{peek-char} is the same as the value that would\n"
1180 "have been returned by a call to @code{read-char} on the same\n"
1181 "port. The only difference is that the very next call to\n"
1182 "@code{read-char} or @code{peek-char} on that @var{port} will\n"
1183 "return the value returned by the preceding call to\n"
1184 "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
1185 "an interactive port will hang waiting for input whenever a call\n"
1186 "to @code{read-char} would have hung.}")
1bbd0b84 1187#define FUNC_NAME s_scm_peek_char
ee149d03
JB
1188{
1189 int c;
1190 if (SCM_UNBNDP (port))
1191 port = scm_cur_inp;
1192 else
34d19ef6 1193 SCM_VALIDATE_OPINPORT (1, port);
ee149d03
JB
1194 c = scm_getc (port);
1195 if (EOF == c)
1196 return SCM_EOF_VAL;
1197 scm_ungetc (c, port);
7866a09b 1198 return SCM_MAKE_CHAR (c);
3cb988bd 1199}
1bbd0b84 1200#undef FUNC_NAME
3cb988bd 1201
1be4270a 1202SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
1bbd0b84 1203 (SCM cobj, SCM port),
b380b885
MD
1204 "Place @var{char} in @var{port} so that it will be read by the\n"
1205 "next read operation. If called multiple times, the unread characters\n"
1206 "will be read again in last-in first-out order. If @var{port} is\n"
1207 "not supplied, the current input port is used.")
1bbd0b84 1208#define FUNC_NAME s_scm_unread_char
0f2d19dd
JB
1209{
1210 int c;
1211
34d19ef6 1212 SCM_VALIDATE_CHAR (1, cobj);
0f2d19dd
JB
1213 if (SCM_UNBNDP (port))
1214 port = scm_cur_inp;
1215 else
34d19ef6 1216 SCM_VALIDATE_OPINPORT (2, port);
0f2d19dd 1217
7866a09b 1218 c = SCM_CHAR (cobj);
0f2d19dd 1219
b7f3516f 1220 scm_ungetc (c, port);
0f2d19dd
JB
1221 return cobj;
1222}
1bbd0b84 1223#undef FUNC_NAME
0f2d19dd 1224
a1ec6916 1225SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
1bbd0b84 1226 (SCM str, SCM port),
b380b885
MD
1227 "Place the string @var{str} in @var{port} so that its characters will be\n"
1228 "read in subsequent read operations. If called multiple times, the\n"
1229 "unread characters will be read again in last-in first-out order. If\n"
1230 "@var{port} is not supplied, the current-input-port is used.")
1bbd0b84 1231#define FUNC_NAME s_scm_unread_string
ee1e7e13 1232{
34d19ef6 1233 SCM_VALIDATE_STRING (1, str);
ee1e7e13
MD
1234 if (SCM_UNBNDP (port))
1235 port = scm_cur_inp;
1236 else
34d19ef6 1237 SCM_VALIDATE_OPINPORT (2, port);
ee1e7e13 1238
34f0f2b8 1239 scm_ungets (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port);
ee1e7e13
MD
1240
1241 return str;
1242}
1bbd0b84 1243#undef FUNC_NAME
ee1e7e13 1244
a1ec6916 1245SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
1e6808ea
MG
1246 (SCM fd_port, SCM offset, SCM whence),
1247 "Sets the current position of @var{fd/port} to the integer\n"
1248 "@var{offset}, which is interpreted according to the value of\n"
1249 "@var{whence}.\n"
1250 "\n"
1251 "One of the following variables should be supplied for\n"
1252 "@var{whence}:\n"
b380b885
MD
1253 "@defvar SEEK_SET\n"
1254 "Seek from the beginning of the file.\n"
1255 "@end defvar\n"
1256 "@defvar SEEK_CUR\n"
1257 "Seek from the current position.\n"
1258 "@end defvar\n"
1259 "@defvar SEEK_END\n"
1260 "Seek from the end of the file.\n"
1e6808ea
MG
1261 "@end defvar\n"
1262 "If @var{fd/port} is a file descriptor, the underlying system\n"
1263 "call is @code{lseek}. @var{port} may be a string port.\n"
1264 "\n"
1265 "The value returned is the new position in the file. This means\n"
1266 "that the current position of a port can be obtained using:\n"
1267 "@lisp\n"
b380b885 1268 "(seek port 0 SEEK_CUR)\n"
1e6808ea 1269 "@end lisp")
1bbd0b84 1270#define FUNC_NAME s_scm_seek
840ae05d
JB
1271{
1272 off_t off;
1273 off_t rv;
1274 int how;
1275
1e6808ea 1276 fd_port = SCM_COERCE_OUTPORT (fd_port);
840ae05d 1277
6fe692e9
MD
1278 off = SCM_NUM2LONG (2, offset);
1279 SCM_VALIDATE_INUM_COPY (3, whence, how);
840ae05d 1280 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
1bbd0b84 1281 SCM_OUT_OF_RANGE (3, whence);
1e6808ea 1282 if (SCM_OPPORTP (fd_port))
840ae05d 1283 {
92c2555f 1284 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (fd_port);
840ae05d
JB
1285
1286 if (!ptob->seek)
1bbd0b84 1287 SCM_MISC_ERROR ("port is not seekable",
1e6808ea 1288 scm_cons (fd_port, SCM_EOL));
840ae05d 1289 else
1e6808ea 1290 rv = ptob->seek (fd_port, off, how);
840ae05d
JB
1291 }
1292 else /* file descriptor?. */
1293 {
34d19ef6 1294 SCM_VALIDATE_INUM (1, fd_port);
1e6808ea 1295 rv = lseek (SCM_INUM (fd_port), off, how);
840ae05d 1296 if (rv == -1)
1bbd0b84 1297 SCM_SYSERROR;
840ae05d
JB
1298 }
1299 return scm_long2num (rv);
1300}
1bbd0b84 1301#undef FUNC_NAME
840ae05d 1302
82893676
MG
1303#ifdef __MINGW32__
1304/* Define this function since it is not supported under Windows. */
1305static int truncate (char *file, int length)
1306{
1307 int ret = -1, fdes;
1308 if ((fdes = open (file, O_BINARY | O_WRONLY)) != -1)
1309 {
1310 ret = chsize (fdes, length);
1311 close (fdes);
1312 }
1313 return ret;
1314}
1315#endif /* __MINGW32__ */
1316
a1ec6916 1317SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
1bbd0b84 1318 (SCM object, SCM length),
1e6808ea
MG
1319 "Truncates the object referred to by @var{object} to at most\n"
1320 "@var{length} bytes. @var{object} can be a string containing a\n"
1321 "file name or an integer file descriptor or a port.\n"
1322 "@var{length} may be omitted if @var{object} is not a file name,\n"
1323 "in which case the truncation occurs at the current port.\n"
1324 "position. The return value is unspecified.")
1bbd0b84 1325#define FUNC_NAME s_scm_truncate_file
840ae05d 1326{
69bc9ff3
GH
1327 int rv;
1328 off_t c_length;
1329
1330 /* object can be a port, fdes or filename. */
840ae05d 1331
840ae05d
JB
1332 if (SCM_UNBNDP (length))
1333 {
69bc9ff3 1334 /* must supply length if object is a filename. */
a6d9e5ab 1335 if (SCM_STRINGP (object))
34d19ef6 1336 SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL);
69bc9ff3 1337
c94577b4 1338 length = scm_seek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR));
840ae05d 1339 }
34d19ef6 1340 c_length = SCM_NUM2LONG (2, length);
69bc9ff3 1341 if (c_length < 0)
1bbd0b84 1342 SCM_MISC_ERROR ("negative offset", SCM_EOL);
3fe6190f 1343
69bc9ff3
GH
1344 object = SCM_COERCE_OUTPORT (object);
1345 if (SCM_INUMP (object))
1346 {
1347 SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length));
1348 }
0c95b57d 1349 else if (SCM_OPOUTPORTP (object))
69bc9ff3 1350 {
92c2555f
MV
1351 scm_t_port *pt = SCM_PTAB_ENTRY (object);
1352 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
69bc9ff3 1353
affc96b5 1354 if (!ptob->truncate)
1bbd0b84 1355 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
69bc9ff3 1356 if (pt->rw_active == SCM_PORT_READ)
affc96b5 1357 scm_end_input (object);
69bc9ff3 1358 else if (pt->rw_active == SCM_PORT_WRITE)
affc96b5 1359 ptob->flush (object);
69bc9ff3 1360
affc96b5 1361 ptob->truncate (object, c_length);
69bc9ff3
GH
1362 rv = 0;
1363 }
1364 else
1365 {
a6d9e5ab 1366 SCM_VALIDATE_STRING (1, object);
a6d9e5ab 1367 SCM_SYSCALL (rv = truncate (SCM_STRING_CHARS (object), c_length));
69bc9ff3
GH
1368 }
1369 if (rv == -1)
1bbd0b84 1370 SCM_SYSERROR;
840ae05d
JB
1371 return SCM_UNSPECIFIED;
1372}
1bbd0b84 1373#undef FUNC_NAME
840ae05d 1374
a1ec6916 1375SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
1bbd0b84 1376 (SCM port),
e1546b65 1377 "Return the current line number for @var{port}.")
1bbd0b84 1378#define FUNC_NAME s_scm_port_line
0f2d19dd 1379{
78446828 1380 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1381 SCM_VALIDATE_OPENPORT (1, port);
360fc44c 1382 return SCM_MAKINUM (SCM_LINUM (port));
0f2d19dd 1383}
1bbd0b84 1384#undef FUNC_NAME
0f2d19dd 1385
a1ec6916 1386SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
1bbd0b84 1387 (SCM port, SCM line),
e1546b65 1388 "Set the current line number for @var{port} to @var{line}.")
1bbd0b84 1389#define FUNC_NAME s_scm_set_port_line_x
d043d8c2 1390{
360fc44c 1391 port = SCM_COERCE_OUTPORT (port);
34d19ef6
HWN
1392 SCM_VALIDATE_OPENPORT (1, port);
1393 SCM_VALIDATE_INUM (2, line);
564478fd
GB
1394 SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
1395 return SCM_UNSPECIFIED;
d043d8c2 1396}
1bbd0b84 1397#undef FUNC_NAME
d043d8c2 1398
a1ec6916 1399SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
1bbd0b84 1400 (SCM port),
8f85c0c6 1401 "@deffnx {Scheme Procedure} port-line port\n"
650a1cf9 1402 "Return the current column number or line number of @var{port},\n"
b380b885
MD
1403 "using the current input port if none is specified. If the number is\n"
1404 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1405 "- i.e. the first character of the first line is line 0, column 0.\n"
1406 "(However, when you display a file position, for example in an error\n"
650a1cf9 1407 "message, we recommend you add 1 to get 1-origin integers. This is\n"
b380b885
MD
1408 "because lines and column numbers traditionally start with 1, and that is\n"
1409 "what non-programmers will find most natural.)")
1bbd0b84 1410#define FUNC_NAME s_scm_port_column
0f2d19dd 1411{
78446828 1412 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1413 SCM_VALIDATE_OPENPORT (1, port);
360fc44c 1414 return SCM_MAKINUM (SCM_COL (port));
0f2d19dd 1415}
1bbd0b84 1416#undef FUNC_NAME
0f2d19dd 1417
a1ec6916 1418SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
1bbd0b84 1419 (SCM port, SCM column),
8f85c0c6 1420 "@deffnx {Scheme Procedure} set-port-line! port line\n"
92ccc1f1 1421 "Set the current column or line number of @var{port}, using the\n"
b380b885 1422 "current input port if none is specified.")
1bbd0b84 1423#define FUNC_NAME s_scm_set_port_column_x
d043d8c2 1424{
360fc44c 1425 port = SCM_COERCE_OUTPORT (port);
34d19ef6
HWN
1426 SCM_VALIDATE_OPENPORT (1, port);
1427 SCM_VALIDATE_INUM (2, column);
564478fd
GB
1428 SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
1429 return SCM_UNSPECIFIED;
d043d8c2 1430}
1bbd0b84 1431#undef FUNC_NAME
d043d8c2 1432
a1ec6916 1433SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
1bbd0b84 1434 (SCM port),
b380b885 1435 "Return the filename associated with @var{port}. This function returns\n"
2a2a730b 1436 "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
a3c8b9fc 1437 "when called on the current input, output and error ports respectively.")
1bbd0b84 1438#define FUNC_NAME s_scm_port_filename
0f2d19dd 1439{
78446828 1440 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1441 SCM_VALIDATE_OPENPORT (1, port);
b24b5e13 1442 return SCM_FILENAME (port);
0f2d19dd 1443}
1bbd0b84 1444#undef FUNC_NAME
0f2d19dd 1445
a1ec6916 1446SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
1bbd0b84 1447 (SCM port, SCM filename),
b380b885
MD
1448 "Change the filename associated with @var{port}, using the current input\n"
1449 "port if none is specified. Note that this does not change the port's\n"
1450 "source of data, but only the value that is returned by\n"
1451 "@code{port-filename} and reported in diagnostic output.")
1bbd0b84 1452#define FUNC_NAME s_scm_set_port_filename_x
d14af9f2 1453{
360fc44c 1454 port = SCM_COERCE_OUTPORT (port);
34d19ef6 1455 SCM_VALIDATE_OPENPORT (1, port);
360fc44c 1456 /* We allow the user to set the filename to whatever he likes. */
b24b5e13
DH
1457 SCM_SET_FILENAME (port, filename);
1458 return SCM_UNSPECIFIED;
d14af9f2 1459}
1bbd0b84 1460#undef FUNC_NAME
d14af9f2 1461
0f2d19dd
JB
1462#ifndef ttyname
1463extern char * ttyname();
1464#endif
1465
f12733c9
MD
1466void
1467scm_print_port_mode (SCM exp, SCM port)
1468{
1469 scm_puts (SCM_CLOSEDP (exp)
1470 ? "closed: "
f9a64404
DH
1471 : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
1472 ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
f12733c9
MD
1473 ? "input-output: "
1474 : "input: ")
f9a64404 1475 : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
f12733c9
MD
1476 ? "output: "
1477 : "bogus: ")),
1478 port);
1479}
1cc91f1b 1480
f12733c9 1481int
e81d98ec 1482scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
0f2d19dd 1483{
f12733c9
MD
1484 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
1485 if (!type)
1486 type = "port";
b7f3516f 1487 scm_puts ("#<", port);
f12733c9 1488 scm_print_port_mode (exp, port);
b7f3516f
TT
1489 scm_puts (type, port);
1490 scm_putc (' ', port);
12a8b769 1491 scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
b7f3516f 1492 scm_putc ('>', port);
f12733c9 1493 return 1;
0f2d19dd
JB
1494}
1495
0f2d19dd
JB
1496void
1497scm_ports_prehistory ()
0f2d19dd
JB
1498{
1499 scm_numptob = 0;
92c2555f 1500 scm_ptobs = (scm_t_ptob_descriptor *) malloc (sizeof (scm_t_ptob_descriptor));
0f2d19dd 1501}
0f2d19dd
JB
1502
1503\f
ee149d03 1504
d68fee48 1505/* Void ports. */
0f2d19dd 1506
92c2555f 1507scm_t_bits scm_tc16_void_port = 0;
0f2d19dd 1508
e81d98ec 1509static int fill_input_void_port (SCM port SCM_UNUSED)
283a1a0e 1510{
70df8af6 1511 return EOF;
283a1a0e
GH
1512}
1513
31703ab8 1514static void
e81d98ec
DH
1515write_void_port (SCM port SCM_UNUSED,
1516 const void *data SCM_UNUSED,
1517 size_t size SCM_UNUSED)
31703ab8
GH
1518{
1519}
1520
0f2d19dd 1521SCM
a284e297 1522scm_void_port (char *mode_str)
0f2d19dd 1523{
0f2d19dd 1524 SCM_DEFER_INTS;
402788a9
HWN
1525 {
1526 int mode_bits = scm_mode_bits (mode_str);
da220f27
HWN
1527 SCM answer = scm_new_port_table_entry (scm_tc16_void_port);
1528 scm_t_port * pt = SCM_PTAB_ENTRY(answer);
1529
402788a9 1530 scm_port_non_buffer (pt);
402788a9
HWN
1531
1532 SCM_SETSTREAM (answer, 0);
1533 SCM_SET_CELL_TYPE (answer, scm_tc16_void_port | mode_bits);
1534 SCM_ALLOW_INTS;
1535 return answer;
1536 }
0f2d19dd
JB
1537}
1538
a1ec6916 1539SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
1bbd0b84 1540 (SCM mode),
70df8af6 1541 "Create and return a new void port. A void port acts like\n"
bb2c02f2 1542 "@file{/dev/null}. The @var{mode} argument\n"
70df8af6 1543 "specifies the input/output modes for this port: see the\n"
b380b885 1544 "documentation for @code{open-file} in @ref{File Ports}.")
1bbd0b84 1545#define FUNC_NAME s_scm_sys_make_void_port
0f2d19dd 1546{
a6d9e5ab 1547 SCM_VALIDATE_STRING (1, mode);
a6d9e5ab 1548 return scm_void_port (SCM_STRING_CHARS (mode));
0f2d19dd 1549}
1bbd0b84 1550#undef FUNC_NAME
0f2d19dd 1551
0f2d19dd 1552\f
89545eba 1553/* Initialization. */
1cc91f1b 1554
0f2d19dd
JB
1555void
1556scm_init_ports ()
0f2d19dd 1557{
840ae05d 1558 /* lseek() symbols. */
86d31dfe
MV
1559 scm_c_define ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
1560 scm_c_define ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
1561 scm_c_define ("SEEK_END", SCM_MAKINUM (SEEK_END));
840ae05d 1562
70df8af6
GH
1563 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
1564 write_void_port);
a0599745 1565#include "libguile/ports.x"
0f2d19dd 1566}
89e00824
ML
1567
1568/*
1569 Local Variables:
1570 c-file-style: "gnu"
1571 End:
1572*/