Only include strings.h where it is actually needed.
[bpt/guile.git] / libguile / ports.c
1 /* Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
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
45 \f
46 /* Headers. */
47
48 #include <stdio.h>
49 #include "_scm.h"
50 #include "objects.h"
51 #include "smob.h"
52 #include "chars.h"
53
54 #include "keywords.h"
55 #include "strings.h"
56
57 #include "validate.h"
58 #include "ports.h"
59
60 #ifdef HAVE_STRING_H
61 #include <string.h>
62 #endif
63
64 #ifdef HAVE_MALLOC_H
65 #include <malloc.h>
66 #endif
67
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifdef HAVE_SYS_IOCTL_H
73 #include <sys/ioctl.h>
74 #endif
75
76 \f
77 /* The port kind table --- a dynamically resized array of port types. */
78
79
80 /* scm_ptobs scm_numptob
81 * implement a dynamicly resized array of ptob records.
82 * Indexes into this table are used when generating type
83 * tags for smobjects (if you know a tag you can get an index and conversely).
84 */
85 scm_ptob_descriptor *scm_ptobs;
86 int scm_numptob;
87
88 /* GC marker for a port with stream of SCM type. */
89 SCM
90 scm_markstream (SCM ptr)
91 {
92 int openp;
93 openp = SCM_UNPACK_CAR (ptr) & SCM_OPN;
94 if (openp)
95 return SCM_STREAM (ptr);
96 else
97 return SCM_BOOL_F;
98 }
99
100 /*
101 * We choose to use an interface similar to the smob interface with
102 * fill_input and write as standard fields, passed to the port
103 * type constructor, and optional fields set by setters.
104 */
105
106 static void
107 flush_port_default (SCM port)
108 {
109 }
110
111 static void
112 end_input_default (SCM port, int offset)
113 {
114 }
115
116 long
117 scm_make_port_type (char *name,
118 int (*fill_input) (SCM port),
119 void (*write) (SCM port, const void *data, size_t size))
120 {
121 char *tmp;
122 if (255 <= scm_numptob)
123 goto ptoberr;
124 SCM_DEFER_INTS;
125 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs,
126 (1 + scm_numptob)
127 * sizeof (scm_ptob_descriptor)));
128 if (tmp)
129 {
130 scm_ptobs = (scm_ptob_descriptor *) tmp;
131
132 scm_ptobs[scm_numptob].name = name;
133 scm_ptobs[scm_numptob].mark = 0;
134 scm_ptobs[scm_numptob].free = scm_free0;
135 scm_ptobs[scm_numptob].print = scm_port_print;
136 scm_ptobs[scm_numptob].equalp = 0;
137 scm_ptobs[scm_numptob].close = 0;
138
139 scm_ptobs[scm_numptob].write = write;
140 scm_ptobs[scm_numptob].flush = flush_port_default;
141
142 scm_ptobs[scm_numptob].end_input = end_input_default;
143 scm_ptobs[scm_numptob].fill_input = fill_input;
144 scm_ptobs[scm_numptob].input_waiting = 0;
145
146 scm_ptobs[scm_numptob].seek = 0;
147 scm_ptobs[scm_numptob].truncate = 0;
148
149 scm_numptob++;
150 }
151 SCM_ALLOW_INTS;
152 if (!tmp)
153 ptoberr:scm_wta (SCM_MAKINUM ((long) scm_numptob),
154 (char *) SCM_NALLOC, "scm_make_port_type");
155 /* Make a class object if Goops is present */
156 if (scm_port_class)
157 scm_make_port_classes (scm_numptob - 1, SCM_PTOBNAME (scm_numptob - 1));
158 return scm_tc7_port + (scm_numptob - 1) * 256;
159 }
160
161 void
162 scm_set_port_mark (long tc, SCM (*mark) (SCM))
163 {
164 scm_ptobs[SCM_TC2PTOBNUM (tc)].mark = mark;
165 }
166
167 void
168 scm_set_port_free (long tc, scm_sizet (*free) (SCM))
169 {
170 scm_ptobs[SCM_TC2PTOBNUM (tc)].free = free;
171 }
172
173 void
174 scm_set_port_print (long tc, int (*print) (SCM exp, SCM port,
175 scm_print_state *pstate))
176 {
177 scm_ptobs[SCM_TC2PTOBNUM (tc)].print = print;
178 }
179
180 void
181 scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM))
182 {
183 scm_ptobs[SCM_TC2PTOBNUM (tc)].equalp = equalp;
184 }
185
186 void
187 scm_set_port_flush (long tc, void (*flush) (SCM port))
188 {
189 scm_ptobs[SCM_TC2PTOBNUM (tc)].flush = flush;
190 }
191
192 void
193 scm_set_port_end_input (long tc, void (*end_input) (SCM port, int offset))
194 {
195 scm_ptobs[SCM_TC2PTOBNUM (tc)].end_input = end_input;
196 }
197
198 void
199 scm_set_port_close (long tc, int (*close) (SCM))
200 {
201 scm_ptobs[SCM_TC2PTOBNUM (tc)].close = close;
202 }
203
204 void
205 scm_set_port_seek (long tc, off_t (*seek) (SCM port,
206 off_t OFFSET,
207 int WHENCE))
208 {
209 scm_ptobs[SCM_TC2PTOBNUM (tc)].seek = seek;
210 }
211
212 void
213 scm_set_port_truncate (long tc, void (*truncate) (SCM port, off_t length))
214 {
215 scm_ptobs[SCM_TC2PTOBNUM (tc)].truncate = truncate;
216 }
217
218 void
219 scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM))
220 {
221 scm_ptobs[SCM_TC2PTOBNUM (tc)].input_waiting = input_waiting;
222 }
223
224 \f
225
226 SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
227 (SCM port),
228 "Returns @code{#t} if a character is ready on input @var{port} and\n"
229 "returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}\n"
230 "then the next @code{read-char} operation on @var{port} is\n"
231 "guaranteed not to hang. If @var{port} is a file port at end of\n"
232 "file then @code{char-ready?} returns @code{#t}.\n"
233 "@footnote{@code{char-ready?} exists to make it possible for a\n"
234 "program to accept characters from interactive ports without getting\n"
235 "stuck waiting for input. Any input editors associated with such ports\n"
236 "must make sure that characters whose existence has been asserted by\n"
237 "@code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to\n"
238 "return @code{#f} at end of file, a port at end of file would be\n"
239 "indistinguishable from an interactive port that has no ready\n"
240 "characters.}")
241 #define FUNC_NAME s_scm_char_ready_p
242 {
243 scm_port *pt;
244
245 if (SCM_UNBNDP (port))
246 port = scm_cur_inp;
247 else
248 SCM_VALIDATE_OPINPORT (1,port);
249
250 pt = SCM_PTAB_ENTRY (port);
251
252 /* if the current read buffer is filled, or the
253 last pushed-back char has been read and the saved buffer is
254 filled, result is true. */
255 if (pt->read_pos < pt->read_end
256 || (pt->read_buf == pt->putback_buf
257 && pt->saved_read_pos < pt->saved_read_end))
258 return SCM_BOOL_T;
259 else
260 {
261 scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
262
263 if (ptob->input_waiting)
264 return SCM_BOOL(ptob->input_waiting (port));
265 else
266 return SCM_BOOL_T;
267 }
268 }
269 #undef FUNC_NAME
270
271 /* Clear a port's read buffers, returning the contents. */
272 SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
273 (SCM port),
274 "Drains @var{PORT}'s read buffers (including any pushed-back characters)\n"
275 "and returns the contents as a single string.")
276 #define FUNC_NAME s_scm_drain_input
277 {
278 SCM result;
279 scm_port *pt = SCM_PTAB_ENTRY (port);
280 int count;
281 char *dst;
282
283 SCM_VALIDATE_OPINPORT (1,port);
284
285 count = pt->read_end - pt->read_pos;
286 if (pt->read_buf == pt->putback_buf)
287 count += pt->saved_read_end - pt->saved_read_pos;
288
289 result = scm_makstr (count, 0);
290 dst = SCM_CHARS (result);
291
292 while (pt->read_pos < pt->read_end)
293 *dst++ = *(pt->read_pos++);
294
295 if (pt->read_buf == pt->putback_buf)
296 {
297 while (pt->saved_read_pos < pt->saved_read_end)
298 *dst++ = *(pt->saved_read_pos++);
299 }
300
301 return result;
302 }
303 #undef FUNC_NAME
304
305 \f
306 /* Standard ports --- current input, output, error, and more(!). */
307
308 SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0,
309 (),
310 "Returns the current input port. This is the default port used by many\n"
311 "input procedures. Initially, @code{current-input-port} returns the\n"
312 "value of @code{???}.")
313 #define FUNC_NAME s_scm_current_input_port
314 {
315 return scm_cur_inp;
316 }
317 #undef FUNC_NAME
318
319 SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0,
320 (),
321 "Returns the current output port. This is the default port used by many\n"
322 "output procedures. Initially, @code{current-output-port} returns the\n"
323 "value of @code{???}.")
324 #define FUNC_NAME s_scm_current_output_port
325 {
326 return scm_cur_outp;
327 }
328 #undef FUNC_NAME
329
330 SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0,
331 (),
332 "Return the port to which errors and warnings should be sent (the\n"
333 "@dfn{standard error} in Unix and C terminology).")
334 #define FUNC_NAME s_scm_current_error_port
335 {
336 return scm_cur_errp;
337 }
338 #undef FUNC_NAME
339
340 SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0,
341 (),
342 "Return the current-load-port.\n"
343 "The load port is used internally by `primitive-load'.")
344 #define FUNC_NAME s_scm_current_load_port
345 {
346 return scm_cur_loadp;
347 }
348 #undef FUNC_NAME
349
350 SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0,
351 (SCM port),
352 "@deffnx primitive set-current-output-port port\n"
353 "@deffnx primitive set-current-error-port port\n"
354 "Change the ports returned by @code{current-input-port},\n"
355 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
356 "so that they use the supplied @var{port} for input or output.")
357 #define FUNC_NAME s_scm_set_current_input_port
358 {
359 SCM oinp = scm_cur_inp;
360 SCM_VALIDATE_OPINPORT (1,port);
361 scm_cur_inp = port;
362 return oinp;
363 }
364 #undef FUNC_NAME
365
366
367 SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0,
368 (SCM port),
369 "Set the current default output port to PORT.")
370 #define FUNC_NAME s_scm_set_current_output_port
371 {
372 SCM ooutp = scm_cur_outp;
373 port = SCM_COERCE_OUTPORT (port);
374 SCM_VALIDATE_OPOUTPORT (1,port);
375 scm_cur_outp = port;
376 return ooutp;
377 }
378 #undef FUNC_NAME
379
380
381 SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
382 (SCM port),
383 "Set the current default error port to PORT.")
384 #define FUNC_NAME s_scm_set_current_error_port
385 {
386 SCM oerrp = scm_cur_errp;
387 port = SCM_COERCE_OUTPORT (port);
388 SCM_VALIDATE_OPOUTPORT (1,port);
389 scm_cur_errp = port;
390 return oerrp;
391 }
392 #undef FUNC_NAME
393
394 \f
395 /* The port table --- an array of pointers to ports. */
396
397 scm_port **scm_port_table;
398
399 int scm_port_table_size = 0; /* Number of ports in scm_port_table. */
400 int scm_port_table_room = 20; /* Size of the array. */
401
402 /* Add a port to the table. */
403
404 scm_port *
405 scm_add_to_port_table (SCM port)
406 {
407 scm_port *entry;
408
409 if (scm_port_table_size == scm_port_table_room)
410 {
411 void *newt = realloc ((char *) scm_port_table,
412 (scm_sizet) (sizeof (scm_port *)
413 * scm_port_table_room * 2));
414 if (newt == NULL)
415 scm_memory_error ("scm_add_to_port_table");
416 scm_port_table = (scm_port **) newt;
417 scm_port_table_room *= 2;
418 }
419 entry = (scm_port *) malloc (sizeof (scm_port));
420 if (entry == NULL)
421 scm_memory_error ("scm_add_to_port_table");
422
423 entry->port = port;
424 entry->entry = scm_port_table_size;
425 entry->revealed = 0;
426 entry->stream = 0;
427 entry->file_name = SCM_BOOL_F;
428 entry->line_number = 0;
429 entry->column_number = 0;
430 entry->putback_buf = 0;
431 entry->putback_buf_size = 0;
432 entry->rw_active = SCM_PORT_NEITHER;
433 entry->rw_random = 0;
434
435 scm_port_table[scm_port_table_size] = entry;
436 scm_port_table_size++;
437
438 return entry;
439 }
440
441 /* Remove a port from the table and destroy it. */
442
443 void
444 scm_remove_from_port_table (SCM port)
445 {
446 scm_port *p = SCM_PTAB_ENTRY (port);
447 int i = p->entry;
448
449 if (i >= scm_port_table_size)
450 scm_wta (port, "Port not in table", "scm_remove_from_port_table");
451 if (p->putback_buf)
452 free (p->putback_buf);
453 free (p);
454 /* Since we have just freed slot i we can shrink the table by moving
455 the last entry to that slot... */
456 if (i < scm_port_table_size - 1)
457 {
458 scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
459 scm_port_table[i]->entry = i;
460 }
461 SCM_SETPTAB_ENTRY (port, 0);
462 scm_port_table_size--;
463 }
464
465 #ifdef GUILE_DEBUG
466 /* Functions for debugging. */
467
468 SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
469 (),
470 "Returns the number of ports in the port table.\n"
471 "`pt-size' is only included in GUILE_DEBUG builds.")
472 #define FUNC_NAME s_scm_pt_size
473 {
474 return SCM_MAKINUM (scm_port_table_size);
475 }
476 #undef FUNC_NAME
477
478 SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
479 (SCM index),
480 "Returns the port at INDEX in the port table.\n"
481 "`pt-member' is only included in GUILE_DEBUG builds.")
482 #define FUNC_NAME s_scm_pt_member
483 {
484 int i;
485 SCM_VALIDATE_INUM_COPY (1,index,i);
486 if (i < 0 || i >= scm_port_table_size)
487 return SCM_BOOL_F;
488 else
489 return scm_port_table[i]->port;
490 }
491 #undef FUNC_NAME
492 #endif
493
494 void
495 scm_port_non_buffer (scm_port *pt)
496 {
497 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
498 pt->write_buf = pt->write_pos = &pt->shortbuf;
499 pt->read_buf_size = pt->write_buf_size = 1;
500 pt->write_end = pt->write_buf + pt->write_buf_size;
501 }
502
503 \f
504 /* Revealed counts --- an oddity inherited from SCSH. */
505
506 /* Find a port in the table and return its revealed count.
507 Also used by the garbage collector.
508 */
509
510 int
511 scm_revealed_count (SCM port)
512 {
513 return SCM_REVEALED(port);
514 }
515
516
517
518 /* Return the revealed count for a port. */
519
520 SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
521 (SCM port),
522 "Returns the revealed count for @var{port}.")
523 #define FUNC_NAME s_scm_port_revealed
524 {
525 port = SCM_COERCE_OUTPORT (port);
526 SCM_VALIDATE_PORT (1,port);
527 return SCM_MAKINUM (scm_revealed_count (port));
528 }
529 #undef FUNC_NAME
530
531 /* Set the revealed count for a port. */
532 SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
533 (SCM port, SCM rcount),
534 "Sets the revealed count for a port to a given value.\n"
535 "The return value is unspecified.")
536 #define FUNC_NAME s_scm_set_port_revealed_x
537 {
538 port = SCM_COERCE_OUTPORT (port);
539 SCM_VALIDATE_PORT (1,port);
540 SCM_VALIDATE_INUM (2,rcount);
541 SCM_REVEALED (port) = SCM_INUM (rcount);
542 return SCM_UNSPECIFIED;
543 }
544 #undef FUNC_NAME
545
546
547 \f
548 /* Retrieving a port's mode. */
549
550 /* Return the flags that characterize a port based on the mode
551 * string used to open a file for that port.
552 *
553 * See PORT FLAGS in scm.h
554 */
555
556 long
557 scm_mode_bits (char *modes)
558 {
559 return (SCM_OPN
560 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
561 | ( strchr (modes, 'w')
562 || strchr (modes, 'a')
563 || strchr (modes, '+') ? SCM_WRTNG : 0)
564 | (strchr (modes, '0') ? SCM_BUF0 : 0)
565 | (strchr (modes, 'l') ? SCM_BUFLINE : 0));
566 }
567
568
569 /* Return the mode flags from an open port.
570 * Some modes such as "append" are only used when opening
571 * a file and are not returned here. */
572
573 SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
574 (SCM port),
575 "Returns the port modes associated with the open port @var{port}. These\n"
576 "will not necessarily be identical to the modes used when the port was\n"
577 "opened, since modes such as \"append\" which are used only during\n"
578 "port creation are not retained.")
579 #define FUNC_NAME s_scm_port_mode
580 {
581 char modes[3];
582 modes[0] = '\0';
583
584 port = SCM_COERCE_OUTPORT (port);
585 SCM_VALIDATE_OPPORT (1,port);
586 if (SCM_UNPACK_CAR (port) & SCM_RDNG) {
587 if (SCM_UNPACK_CAR (port) & SCM_WRTNG)
588 strcpy (modes, "r+");
589 else
590 strcpy (modes, "r");
591 }
592 else if (SCM_UNPACK_CAR (port) & SCM_WRTNG)
593 strcpy (modes, "w");
594 if (SCM_UNPACK_CAR (port) & SCM_BUF0)
595 strcat (modes, "0");
596 return scm_makfromstr (modes, strlen (modes), 0);
597 }
598 #undef FUNC_NAME
599
600
601 \f
602 /* Closing ports. */
603
604 /* scm_close_port
605 * Call the close operation on a port object.
606 * see also scm_close.
607 */
608 SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
609 (SCM port),
610 "Close the specified port object. Returns @code{#t} if it successfully\n"
611 "closes a port or @code{#f} if it was already\n"
612 "closed. An exception may be raised if an error occurs, for example\n"
613 "when flushing buffered output.\n"
614 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
615 "which can close file descriptors.")
616 #define FUNC_NAME s_scm_close_port
617 {
618 scm_sizet i;
619 int rv;
620
621 port = SCM_COERCE_OUTPORT (port);
622
623 SCM_VALIDATE_PORT (1,port);
624 if (SCM_CLOSEDP (port))
625 return SCM_BOOL_F;
626 i = SCM_PTOBNUM (port);
627 if (scm_ptobs[i].close)
628 rv = (scm_ptobs[i].close) (port);
629 else
630 rv = 0;
631 scm_remove_from_port_table (port);
632 SCM_SETAND_CAR (port, ~SCM_OPN);
633 return SCM_NEGATE_BOOL(rv < 0);
634 }
635 #undef FUNC_NAME
636
637 SCM_DEFINE (scm_close_all_ports_except, "close-all-ports-except", 0, 0, 1,
638 (SCM ports),
639 "Close all open file ports used by the interpreter\n"
640 "except for those supplied as arguments. This procedure\n"
641 "is intended to be used before an exec call to close file descriptors\n"
642 "which are not needed in the new process.Close all open file ports used by the interpreter\n"
643 "except for those supplied as arguments. This procedure\n"
644 "is intended to be used before an exec call to close file descriptors\n"
645 "which are not needed in the new process.")
646 #define FUNC_NAME s_scm_close_all_ports_except
647 {
648 int i = 0;
649 SCM_VALIDATE_CONS (1,ports);
650 while (i < scm_port_table_size)
651 {
652 SCM thisport = scm_port_table[i]->port;
653 int found = 0;
654 SCM ports_ptr = ports;
655
656 while (SCM_NNULLP (ports_ptr))
657 {
658 SCM port = SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr));
659 if (i == 0)
660 SCM_VALIDATE_OPPORT (SCM_ARG1,port);
661 if (port == thisport)
662 found = 1;
663 ports_ptr = SCM_CDR (ports_ptr);
664 }
665 if (found)
666 i++;
667 else
668 /* i is not to be incremented here. */
669 scm_close_port (thisport);
670 }
671 return SCM_UNSPECIFIED;
672 }
673 #undef FUNC_NAME
674
675
676 \f
677 /* Utter miscellany. Gosh, we should clean this up some time. */
678
679 SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
680 (SCM x),
681 "Returns @code{#t} if @var{x} is an input port, otherwise returns\n"
682 "@code{#f}. Any object satisfying this predicate also satisfies\n"
683 "@code{port?}.")
684 #define FUNC_NAME s_scm_input_port_p
685 {
686 if (SCM_IMP (x))
687 return SCM_BOOL_F;
688 return SCM_BOOL(SCM_INPORTP (x));
689 }
690 #undef FUNC_NAME
691
692 SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
693 (SCM x),
694 "Returns @code{#t} if @var{x} is an output port, otherwise returns\n"
695 "@code{#f}. Any object satisfying this predicate also satisfies\n"
696 "@code{port?}.")
697 #define FUNC_NAME s_scm_output_port_p
698 {
699 if (SCM_IMP (x))
700 return SCM_BOOL_F;
701 if (SCM_PORT_WITH_PS_P (x))
702 x = SCM_PORT_WITH_PS_PORT (x);
703 return SCM_BOOL(SCM_OUTPORTP (x));
704 }
705 #undef FUNC_NAME
706
707 SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
708 (SCM port),
709 "Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.")
710 #define FUNC_NAME s_scm_port_closed_p
711 {
712 SCM_VALIDATE_PORT (1,port);
713 return SCM_NEGATE_BOOL(SCM_OPPORTP (port));
714 }
715 #undef FUNC_NAME
716
717 SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
718 (SCM x),
719 "Returns @code{#t} if @var{x} is an end-of-file object; otherwise\n"
720 "returns @code{#f}.")
721 #define FUNC_NAME s_scm_eof_object_p
722 {
723 return SCM_BOOL(SCM_EOF_OBJECT_P (x));
724 }
725 #undef FUNC_NAME
726
727 SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
728 (SCM port),
729 "Flush the specified output port, or the current output port if @var{port}\n"
730 "is omitted. The current output buffer contents are passed to the \n"
731 "underlying port implementation (e.g., in the case of fports, the\n"
732 "data will be written to the file and the output buffer will be cleared.)\n"
733 "It has no effect on an unbuffered port.\n\n"
734 "The return value is unspecified.")
735 #define FUNC_NAME s_scm_force_output
736 {
737 if (SCM_UNBNDP (port))
738 port = scm_cur_outp;
739 else
740 {
741 port = SCM_COERCE_OUTPORT (port);
742 SCM_VALIDATE_OPOUTPORT (1,port);
743 }
744 scm_flush (port);
745 return SCM_UNSPECIFIED;
746 }
747 #undef FUNC_NAME
748
749 SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
750 (),
751 "Equivalent to calling @code{force-output} on\n"
752 "all open output ports. The return value is unspecified.")
753 #define FUNC_NAME s_scm_flush_all_ports
754 {
755 int i;
756
757 for (i = 0; i < scm_port_table_size; i++)
758 {
759 if (SCM_OPOUTPORTP (scm_port_table[i]->port))
760 scm_flush (scm_port_table[i]->port);
761 }
762 return SCM_UNSPECIFIED;
763 }
764 #undef FUNC_NAME
765
766 SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
767 (SCM port),
768 "Returns the next character available from @var{port}, updating\n"
769 "@var{port} to point to the following character. If no more\n"
770 "characters are available, an end-of-file object is returned.")
771 #define FUNC_NAME s_scm_read_char
772 {
773 int c;
774 if (SCM_UNBNDP (port))
775 port = scm_cur_inp;
776 SCM_VALIDATE_OPINPORT (1,port);
777 c = scm_getc (port);
778 if (EOF == c)
779 return SCM_EOF_VAL;
780 return SCM_MAKE_CHAR (c);
781 }
782 #undef FUNC_NAME
783
784 /* this should only be called when the read buffer is empty. it
785 tries to refill the read buffer. it returns the first char from
786 the port, which is either EOF or *(pt->read_pos). */
787 int
788 scm_fill_input (SCM port)
789 {
790 scm_port *pt = SCM_PTAB_ENTRY (port);
791
792 if (pt->read_buf == pt->putback_buf)
793 {
794 /* finished reading put-back chars. */
795 pt->read_buf = pt->saved_read_buf;
796 pt->read_pos = pt->saved_read_pos;
797 pt->read_end = pt->saved_read_end;
798 pt->read_buf_size = pt->saved_read_buf_size;
799 if (pt->read_pos < pt->read_end)
800 return *(pt->read_pos);
801 }
802 return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port);
803 }
804
805 int
806 scm_getc (SCM port)
807 {
808 int c;
809 scm_port *pt = SCM_PTAB_ENTRY (port);
810
811 if (pt->rw_active == SCM_PORT_WRITE)
812 {
813 /* may be marginally faster than calling scm_flush. */
814 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
815 }
816
817 if (pt->rw_random)
818 pt->rw_active = SCM_PORT_READ;
819
820 if (pt->read_pos >= pt->read_end)
821 {
822 if (scm_fill_input (port) == EOF)
823 return EOF;
824 }
825
826 c = *(pt->read_pos++);
827
828 if (c == '\n')
829 {
830 SCM_INCLINE (port);
831 }
832 else if (c == '\t')
833 {
834 SCM_TABCOL (port);
835 }
836 else
837 {
838 SCM_INCCOL (port);
839 }
840
841 return c;
842 }
843
844 void
845 scm_putc (char c, SCM port)
846 {
847 scm_lfwrite (&c, 1, port);
848 }
849
850 void
851 scm_puts (const char *s, SCM port)
852 {
853 scm_lfwrite (s, strlen (s), port);
854 }
855
856 void
857 scm_lfwrite (const char *ptr, scm_sizet size, SCM port)
858 {
859 scm_port *pt = SCM_PTAB_ENTRY (port);
860 scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
861
862 if (pt->rw_active == SCM_PORT_READ)
863 scm_end_input (port);
864
865 ptob->write (port, ptr, size);
866
867 if (pt->rw_random)
868 pt->rw_active = SCM_PORT_WRITE;
869 }
870
871
872 void
873 scm_flush (SCM port)
874 {
875 scm_sizet i = SCM_PTOBNUM (port);
876 (scm_ptobs[i].flush) (port);
877 }
878
879 void
880 scm_end_input (SCM port)
881 {
882 int offset;
883 scm_port *pt = SCM_PTAB_ENTRY (port);
884
885 if (pt->read_buf == pt->putback_buf)
886 {
887 offset = pt->read_end - pt->read_pos;
888 pt->read_buf = pt->saved_read_buf;
889 pt->read_pos = pt->saved_read_pos;
890 pt->read_end = pt->saved_read_end;
891 pt->read_buf_size = pt->saved_read_buf_size;
892 }
893 else
894 offset = 0;
895
896 scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset);
897 }
898
899 \f
900
901
902 void
903 scm_ungetc (int c, SCM port)
904 {
905 scm_port *pt = SCM_PTAB_ENTRY (port);
906
907 if (pt->read_buf == pt->putback_buf)
908 /* already using the put-back buffer. */
909 {
910 /* enlarge putback_buf if necessary. */
911 if (pt->read_end == pt->read_buf + pt->read_buf_size
912 && pt->read_buf == pt->read_pos)
913 {
914 int new_size = pt->read_buf_size * 2;
915 unsigned char *tmp =
916 (unsigned char *) realloc (pt->putback_buf, new_size);
917
918 if (tmp == NULL)
919 scm_memory_error ("scm_ungetc");
920 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
921 pt->read_end = pt->read_buf + pt->read_buf_size;
922 pt->read_buf_size = pt->putback_buf_size = new_size;
923 }
924
925 /* shift any existing bytes to buffer + 1. */
926 if (pt->read_pos == pt->read_end)
927 pt->read_end = pt->read_buf + 1;
928 else if (pt->read_pos != pt->read_buf + 1)
929 {
930 int count = pt->read_end - pt->read_pos;
931
932 memmove (pt->read_buf + 1, pt->read_pos, count);
933 pt->read_end = pt->read_buf + 1 + count;
934 }
935
936 pt->read_pos = pt->read_buf;
937 }
938 else
939 /* switch to the put-back buffer. */
940 {
941 if (pt->putback_buf == NULL)
942 {
943 pt->putback_buf = (char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE);
944 if (pt->putback_buf == NULL)
945 scm_memory_error ("scm_ungetc");
946 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
947 }
948
949 pt->saved_read_buf = pt->read_buf;
950 pt->saved_read_pos = pt->read_pos;
951 pt->saved_read_end = pt->read_end;
952 pt->saved_read_buf_size = pt->read_buf_size;
953
954 pt->read_pos = pt->read_buf = pt->putback_buf;
955 pt->read_end = pt->read_buf + 1;
956 pt->read_buf_size = pt->putback_buf_size;
957 }
958
959 *pt->read_buf = c;
960
961 if (pt->rw_random)
962 pt->rw_active = SCM_PORT_READ;
963
964 if (c == '\n')
965 {
966 /* What should col be in this case?
967 * We'll leave it at -1.
968 */
969 SCM_LINUM (port) -= 1;
970 }
971 else
972 SCM_COL(port) -= 1;
973 }
974
975
976 void
977 scm_ungets (const char *s, int n, SCM port)
978 {
979 /* This is simple minded and inefficient, but unreading strings is
980 * probably not a common operation, and remember that line and
981 * column numbers have to be handled...
982 *
983 * Please feel free to write an optimized version!
984 */
985 while (n--)
986 scm_ungetc (s[n], port);
987 }
988
989
990 SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
991 (SCM port),
992 "Returns the next character available from @var{port},\n"
993 "@emph{without} updating @var{port} to point to the following\n"
994 "character. If no more characters are available, an end-of-file object\n"
995 "is returned.@footnote{The value returned by a call to @code{peek-char}\n"
996 "is the same as the value that would have been returned by a call to\n"
997 "@code{read-char} on the same port. The only difference is that the very\n"
998 "next call to @code{read-char} or @code{peek-char} on that\n"
999 "@var{port} will return the value returned by the preceding call to\n"
1000 "@code{peek-char}. In particular, a call to @code{peek-char} on an\n"
1001 "interactive port will hang waiting for input whenever a call to\n"
1002 "@code{read-char} would have hung.}")
1003 #define FUNC_NAME s_scm_peek_char
1004 {
1005 int c;
1006 if (SCM_UNBNDP (port))
1007 port = scm_cur_inp;
1008 else
1009 SCM_VALIDATE_OPINPORT (1,port);
1010 c = scm_getc (port);
1011 if (EOF == c)
1012 return SCM_EOF_VAL;
1013 scm_ungetc (c, port);
1014 return SCM_MAKE_CHAR (c);
1015 }
1016 #undef FUNC_NAME
1017
1018 SCM_DEFINE (scm_unread_char, "unread-char", 2, 0, 0,
1019 (SCM cobj, SCM port),
1020 "Place @var{char} in @var{port} so that it will be read by the\n"
1021 "next read operation. If called multiple times, the unread characters\n"
1022 "will be read again in last-in first-out order. If @var{port} is\n"
1023 "not supplied, the current input port is used.")
1024 #define FUNC_NAME s_scm_unread_char
1025 {
1026 int c;
1027
1028 SCM_VALIDATE_CHAR (1,cobj);
1029 if (SCM_UNBNDP (port))
1030 port = scm_cur_inp;
1031 else
1032 SCM_VALIDATE_OPINPORT (2,port);
1033
1034 c = SCM_CHAR (cobj);
1035
1036 scm_ungetc (c, port);
1037 return cobj;
1038 }
1039 #undef FUNC_NAME
1040
1041 SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
1042 (SCM str, SCM port),
1043 "Place the string @var{str} in @var{port} so that its characters will be\n"
1044 "read in subsequent read operations. If called multiple times, the\n"
1045 "unread characters will be read again in last-in first-out order. If\n"
1046 "@var{port} is not supplied, the current-input-port is used.")
1047 #define FUNC_NAME s_scm_unread_string
1048 {
1049 SCM_VALIDATE_STRING (1,str);
1050 if (SCM_UNBNDP (port))
1051 port = scm_cur_inp;
1052 else
1053 SCM_VALIDATE_OPINPORT (2,port);
1054
1055 scm_ungets (SCM_ROUCHARS (str), SCM_LENGTH (str), port);
1056
1057 return str;
1058 }
1059 #undef FUNC_NAME
1060
1061 SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
1062 (SCM object, SCM offset, SCM whence),
1063 "Sets the current position of @var{fd/port} to the integer @var{offset},\n"
1064 "which is interpreted according to the value of @var{whence}.\n\n"
1065 "One of the following variables should be supplied\n"
1066 "for @var{whence}:\n"
1067 "@defvar SEEK_SET\n"
1068 "Seek from the beginning of the file.\n"
1069 "@end defvar\n"
1070 "@defvar SEEK_CUR\n"
1071 "Seek from the current position.\n"
1072 "@end defvar\n"
1073 "@defvar SEEK_END\n"
1074 "Seek from the end of the file.\n"
1075 "@end defvar\n\n"
1076 "If @var{fd/port} is a file descriptor, the underlying system call is\n"
1077 "@code{lseek}. @var{port} may be a string port.\n\n"
1078 "The value returned is the new position in the file. This means that\n"
1079 "the current position of a port can be obtained using:\n"
1080 "@smalllisp\n"
1081 "(seek port 0 SEEK_CUR)\n"
1082 "@end smalllisp")
1083 #define FUNC_NAME s_scm_seek
1084 {
1085 off_t off;
1086 off_t rv;
1087 int how;
1088
1089 object = SCM_COERCE_OUTPORT (object);
1090
1091 off = SCM_NUM2LONG (2,offset);
1092 SCM_VALIDATE_INUM_COPY (3,whence,how);
1093 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
1094 SCM_OUT_OF_RANGE (3, whence);
1095 if (SCM_OPPORTP (object))
1096 {
1097 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
1098
1099 if (!ptob->seek)
1100 SCM_MISC_ERROR ("port is not seekable",
1101 scm_cons (object, SCM_EOL));
1102 else
1103 rv = ptob->seek (object, off, how);
1104 }
1105 else /* file descriptor?. */
1106 {
1107 SCM_VALIDATE_INUM (1,object);
1108 rv = lseek (SCM_INUM (object), off, how);
1109 if (rv == -1)
1110 SCM_SYSERROR;
1111 }
1112 return scm_long2num (rv);
1113 }
1114 #undef FUNC_NAME
1115
1116 SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
1117 (SCM object, SCM length),
1118 "Truncates the object referred to by @var{obj} to at most @var{size} bytes.\n"
1119 "@var{obj} can be a string containing a file name or an integer file\n"
1120 "descriptor or a port. @var{size} may be omitted if @var{obj} is not\n"
1121 "a file name, in which case the truncation occurs at the current port.\n"
1122 "position.\n\n"
1123 "The return value is unspecified.")
1124 #define FUNC_NAME s_scm_truncate_file
1125 {
1126 int rv;
1127 off_t c_length;
1128
1129 /* object can be a port, fdes or filename. */
1130
1131 if (SCM_UNBNDP (length))
1132 {
1133 /* must supply length if object is a filename. */
1134 if (SCM_ROSTRINGP (object))
1135 SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL);
1136
1137 length = scm_seek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR));
1138 }
1139 c_length = SCM_NUM2LONG (2,length);
1140 if (c_length < 0)
1141 SCM_MISC_ERROR ("negative offset", SCM_EOL);
1142
1143 object = SCM_COERCE_OUTPORT (object);
1144 if (SCM_INUMP (object))
1145 {
1146 SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length));
1147 }
1148 else if (SCM_OPOUTPORTP (object))
1149 {
1150 scm_port *pt = SCM_PTAB_ENTRY (object);
1151 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
1152
1153 if (!ptob->truncate)
1154 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
1155 if (pt->rw_active == SCM_PORT_READ)
1156 scm_end_input (object);
1157 else if (pt->rw_active == SCM_PORT_WRITE)
1158 ptob->flush (object);
1159
1160 ptob->truncate (object, c_length);
1161 rv = 0;
1162 }
1163 else
1164 {
1165 SCM_VALIDATE_ROSTRING (1,object);
1166 SCM_COERCE_SUBSTR (object);
1167 SCM_SYSCALL (rv = truncate (SCM_ROCHARS (object), c_length));
1168 }
1169 if (rv == -1)
1170 SCM_SYSERROR;
1171 return SCM_UNSPECIFIED;
1172 }
1173 #undef FUNC_NAME
1174
1175 SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
1176 (SCM port),
1177 "Return the current line number for PORT.")
1178 #define FUNC_NAME s_scm_port_line
1179 {
1180 port = SCM_COERCE_OUTPORT (port);
1181 SCM_VALIDATE_OPENPORT (1,port);
1182 return SCM_MAKINUM (SCM_LINUM (port));
1183 }
1184 #undef FUNC_NAME
1185
1186 SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
1187 (SCM port, SCM line),
1188 "Set the current line number for PORT to LINE.")
1189 #define FUNC_NAME s_scm_set_port_line_x
1190 {
1191 port = SCM_COERCE_OUTPORT (port);
1192 SCM_VALIDATE_OPENPORT (1,port);
1193 SCM_VALIDATE_INUM (2,line);
1194 SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
1195 return SCM_UNSPECIFIED;
1196 }
1197 #undef FUNC_NAME
1198
1199 SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
1200 (SCM port),
1201 "@deffnx primitive port-line [input-port]\n"
1202 "Return the current column number or line number of @var{input-port},\n"
1203 "using the current input port if none is specified. If the number is\n"
1204 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1205 "- i.e. the first character of the first line is line 0, column 0.\n"
1206 "(However, when you display a file position, for example in an error\n"
1207 "message, we recommand you add 1 to get 1-origin integers. This is\n"
1208 "because lines and column numbers traditionally start with 1, and that is\n"
1209 "what non-programmers will find most natural.)")
1210 #define FUNC_NAME s_scm_port_column
1211 {
1212 port = SCM_COERCE_OUTPORT (port);
1213 SCM_VALIDATE_OPENPORT (1,port);
1214 return SCM_MAKINUM (SCM_COL (port));
1215 }
1216 #undef FUNC_NAME
1217
1218 SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
1219 (SCM port, SCM column),
1220 "@deffnx primitive set-port-column! [input-port] column\n"
1221 "Set the current column or line number of @var{input-port}, using the\n"
1222 "current input port if none is specified.")
1223 #define FUNC_NAME s_scm_set_port_column_x
1224 {
1225 port = SCM_COERCE_OUTPORT (port);
1226 SCM_VALIDATE_OPENPORT (1,port);
1227 SCM_VALIDATE_INUM (2,column);
1228 SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
1229 return SCM_UNSPECIFIED;
1230 }
1231 #undef FUNC_NAME
1232
1233 SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
1234 (SCM port),
1235 "Return the filename associated with @var{port}. This function returns\n"
1236 "the strings \"standard input\", \"standard output\" and \"standard error\""
1237 "when called on the current input, output and error ports respectively.")
1238 #define FUNC_NAME s_scm_port_filename
1239 {
1240 port = SCM_COERCE_OUTPORT (port);
1241 SCM_VALIDATE_OPENPORT (1,port);
1242 return SCM_PTAB_ENTRY (port)->file_name;
1243 }
1244 #undef FUNC_NAME
1245
1246 SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
1247 (SCM port, SCM filename),
1248 "Change the filename associated with @var{port}, using the current input\n"
1249 "port if none is specified. Note that this does not change the port's\n"
1250 "source of data, but only the value that is returned by\n"
1251 "@code{port-filename} and reported in diagnostic output.")
1252 #define FUNC_NAME s_scm_set_port_filename_x
1253 {
1254 port = SCM_COERCE_OUTPORT (port);
1255 SCM_VALIDATE_OPENPORT (1,port);
1256 /* We allow the user to set the filename to whatever he likes. */
1257 return SCM_PTAB_ENTRY (port)->file_name = filename;
1258 }
1259 #undef FUNC_NAME
1260
1261 #ifndef ttyname
1262 extern char * ttyname();
1263 #endif
1264
1265 void
1266 scm_print_port_mode (SCM exp, SCM port)
1267 {
1268 scm_puts (SCM_CLOSEDP (exp)
1269 ? "closed: "
1270 : (SCM_RDNG & SCM_UNPACK_CAR (exp)
1271 ? (SCM_WRTNG & SCM_UNPACK_CAR (exp)
1272 ? "input-output: "
1273 : "input: ")
1274 : (SCM_WRTNG & SCM_UNPACK_CAR (exp)
1275 ? "output: "
1276 : "bogus: ")),
1277 port);
1278 }
1279
1280 int
1281 scm_port_print (SCM exp, SCM port, scm_print_state *pstate)
1282 {
1283 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
1284 if (!type)
1285 type = "port";
1286 scm_puts ("#<", port);
1287 scm_print_port_mode (exp, port);
1288 scm_puts (type, port);
1289 scm_putc (' ', port);
1290 scm_intprint ((int) SCM_CDR (exp), 16, port);
1291 scm_putc ('>', port);
1292 return 1;
1293 }
1294
1295 extern void scm_make_fptob ();
1296 extern void scm_make_stptob ();
1297 extern void scm_make_sfptob ();
1298
1299 void
1300 scm_ports_prehistory ()
1301 {
1302 scm_numptob = 0;
1303 scm_ptobs = (scm_ptob_descriptor *) malloc (sizeof (scm_ptob_descriptor));
1304
1305 /* WARNING: These scm_newptob calls must be done in this order.
1306 * They must agree with the port declarations in tags.h.
1307 */
1308 /* scm_tc16_fport = */ scm_make_fptob ();
1309 /* scm_tc16_pipe was here */ scm_make_fptob (); /* dummy. */
1310 /* scm_tc16_strport = */ scm_make_stptob ();
1311 /* scm_tc16_sfport = */ scm_make_sfptob ();
1312 }
1313
1314 \f
1315
1316 /* Void ports. */
1317
1318 long scm_tc16_void_port = 0;
1319
1320 static int fill_input_void_port (SCM port)
1321 {
1322 return EOF;
1323 }
1324
1325 static void
1326 write_void_port (SCM port, const void *data, size_t size)
1327 {
1328 }
1329
1330 SCM
1331 scm_void_port (char *mode_str)
1332 {
1333 int mode_bits;
1334 SCM answer;
1335 scm_port * pt;
1336
1337 SCM_NEWCELL (answer);
1338 SCM_DEFER_INTS;
1339 mode_bits = scm_mode_bits (mode_str);
1340 pt = scm_add_to_port_table (answer);
1341 scm_port_non_buffer (pt);
1342 SCM_SETPTAB_ENTRY (answer, pt);
1343 SCM_SETSTREAM (answer, 0);
1344 SCM_SETCAR (answer, scm_tc16_void_port | mode_bits);
1345 SCM_ALLOW_INTS;
1346 return answer;
1347 }
1348
1349 SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
1350 (SCM mode),
1351 "Create and return a new void port. A void port acts like\n"
1352 "/dev/null. The @var{mode} argument\n"
1353 "specifies the input/output modes for this port: see the\n"
1354 "documentation for @code{open-file} in @ref{File Ports}.")
1355 #define FUNC_NAME s_scm_sys_make_void_port
1356 {
1357 SCM_VALIDATE_ROSTRING (1,mode);
1358 SCM_COERCE_SUBSTR (mode);
1359 return scm_void_port (SCM_ROCHARS (mode));
1360 }
1361 #undef FUNC_NAME
1362
1363 \f
1364 /* Initialization. */
1365
1366 void
1367 scm_init_ports ()
1368 {
1369 /* lseek() symbols. */
1370 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
1371 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
1372 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END));
1373
1374 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
1375 write_void_port);
1376 #include "ports.x"
1377 }