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