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