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