1 /* Copyright (C) 1995,1996,1997,1998,1999, 2000 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
49 #include "libguile/_scm.h"
50 #include "libguile/objects.h"
51 #include "libguile/smob.h"
52 #include "libguile/chars.h"
54 #include "libguile/keywords.h"
55 #include "libguile/root.h"
56 #include "libguile/strings.h"
58 #include "libguile/validate.h"
59 #include "libguile/ports.h"
73 #ifdef HAVE_SYS_IOCTL_H
74 #include <sys/ioctl.h>
78 /* The port kind table --- a dynamically resized array of port types. */
81 /* scm_ptobs scm_numptob
82 * implement a dynamicly resized array of ptob records.
83 * Indexes into this table are used when generating type
84 * tags for smobjects (if you know a tag you can get an index and conversely).
86 scm_ptob_descriptor
*scm_ptobs
;
89 /* GC marker for a port with stream of SCM type. */
91 scm_markstream (SCM ptr
)
94 openp
= SCM_UNPACK_CAR (ptr
) & SCM_OPN
;
96 return SCM_PACK (SCM_STREAM (ptr
));
102 * We choose to use an interface similar to the smob interface with
103 * fill_input and write as standard fields, passed to the port
104 * type constructor, and optional fields set by setters.
108 flush_port_default (SCM port
)
113 end_input_default (SCM port
, int offset
)
118 scm_make_port_type (char *name
,
119 int (*fill_input
) (SCM port
),
120 void (*write
) (SCM port
, const void *data
, size_t size
))
123 if (255 <= scm_numptob
)
126 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
,
128 * sizeof (scm_ptob_descriptor
)));
131 scm_ptobs
= (scm_ptob_descriptor
*) tmp
;
133 scm_ptobs
[scm_numptob
].name
= name
;
134 scm_ptobs
[scm_numptob
].mark
= 0;
135 scm_ptobs
[scm_numptob
].free
= scm_free0
;
136 scm_ptobs
[scm_numptob
].print
= scm_port_print
;
137 scm_ptobs
[scm_numptob
].equalp
= 0;
138 scm_ptobs
[scm_numptob
].close
= 0;
140 scm_ptobs
[scm_numptob
].write
= write
;
141 scm_ptobs
[scm_numptob
].flush
= flush_port_default
;
143 scm_ptobs
[scm_numptob
].end_input
= end_input_default
;
144 scm_ptobs
[scm_numptob
].fill_input
= fill_input
;
145 scm_ptobs
[scm_numptob
].input_waiting
= 0;
147 scm_ptobs
[scm_numptob
].seek
= 0;
148 scm_ptobs
[scm_numptob
].truncate
= 0;
154 ptoberr
:scm_wta (SCM_MAKINUM ((long) scm_numptob
),
155 (char *) SCM_NALLOC
, "scm_make_port_type");
156 /* Make a class object if Goops is present */
158 scm_make_port_classes (scm_numptob
- 1, SCM_PTOBNAME (scm_numptob
- 1));
159 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
163 scm_set_port_mark (long tc
, SCM (*mark
) (SCM
))
165 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].mark
= mark
;
169 scm_set_port_free (long tc
, scm_sizet (*free
) (SCM
))
171 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].free
= free
;
175 scm_set_port_print (long tc
, int (*print
) (SCM exp
, SCM port
,
176 scm_print_state
*pstate
))
178 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].print
= print
;
182 scm_set_port_equalp (long tc
, SCM (*equalp
) (SCM
, SCM
))
184 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].equalp
= equalp
;
188 scm_set_port_flush (long tc
, void (*flush
) (SCM port
))
190 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].flush
= flush
;
194 scm_set_port_end_input (long tc
, void (*end_input
) (SCM port
, int offset
))
196 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].end_input
= end_input
;
200 scm_set_port_close (long tc
, int (*close
) (SCM
))
202 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].close
= close
;
206 scm_set_port_seek (long tc
, off_t (*seek
) (SCM port
,
210 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].seek
= seek
;
214 scm_set_port_truncate (long tc
, void (*truncate
) (SCM port
, off_t length
))
216 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].truncate
= truncate
;
220 scm_set_port_input_waiting (long tc
, int (*input_waiting
) (SCM
))
222 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].input_waiting
= input_waiting
;
227 SCM_DEFINE (scm_char_ready_p
, "char-ready?", 0, 1, 0,
229 "Returns @code{#t} if a character is ready on input @var{port} and\n"
230 "returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}\n"
231 "then the next @code{read-char} operation on @var{port} is\n"
232 "guaranteed not to hang. If @var{port} is a file port at end of\n"
233 "file then @code{char-ready?} returns @code{#t}.\n"
234 "@footnote{@code{char-ready?} exists to make it possible for a\n"
235 "program to accept characters from interactive ports without getting\n"
236 "stuck waiting for input. Any input editors associated with such ports\n"
237 "must make sure that characters whose existence has been asserted by\n"
238 "@code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to\n"
239 "return @code{#f} at end of file, a port at end of file would be\n"
240 "indistinguishable from an interactive port that has no ready\n"
242 #define FUNC_NAME s_scm_char_ready_p
246 if (SCM_UNBNDP (port
))
249 SCM_VALIDATE_OPINPORT (1,port
);
251 pt
= SCM_PTAB_ENTRY (port
);
253 /* if the current read buffer is filled, or the
254 last pushed-back char has been read and the saved buffer is
255 filled, result is true. */
256 if (pt
->read_pos
< pt
->read_end
257 || (pt
->read_buf
== pt
->putback_buf
258 && pt
->saved_read_pos
< pt
->saved_read_end
))
262 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
264 if (ptob
->input_waiting
)
265 return SCM_BOOL(ptob
->input_waiting (port
));
272 /* Clear a port's read buffers, returning the contents. */
273 SCM_DEFINE (scm_drain_input
, "drain-input", 1, 0, 0,
275 "Drains @var{PORT}'s read buffers (including any pushed-back characters)\n"
276 "and returns the contents as a single string.")
277 #define FUNC_NAME s_scm_drain_input
280 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
284 SCM_VALIDATE_OPINPORT (1,port
);
286 count
= pt
->read_end
- pt
->read_pos
;
287 if (pt
->read_buf
== pt
->putback_buf
)
288 count
+= pt
->saved_read_end
- pt
->saved_read_pos
;
290 result
= scm_makstr (count
, 0);
291 dst
= SCM_CHARS (result
);
293 while (pt
->read_pos
< pt
->read_end
)
294 *dst
++ = *(pt
->read_pos
++);
296 if (pt
->read_buf
== pt
->putback_buf
)
298 while (pt
->saved_read_pos
< pt
->saved_read_end
)
299 *dst
++ = *(pt
->saved_read_pos
++);
307 /* Standard ports --- current input, output, error, and more(!). */
309 SCM_DEFINE (scm_current_input_port
, "current-input-port", 0, 0, 0,
311 "Returns the current input port. This is the default port used by many\n"
312 "input procedures. Initially, @code{current-input-port} returns the\n"
313 "value of @code{???}.")
314 #define FUNC_NAME s_scm_current_input_port
320 SCM_DEFINE (scm_current_output_port
, "current-output-port", 0, 0, 0,
322 "Returns the current output port. This is the default port used by many\n"
323 "output procedures. Initially, @code{current-output-port} returns the\n"
324 "value of @code{???}.")
325 #define FUNC_NAME s_scm_current_output_port
331 SCM_DEFINE (scm_current_error_port
, "current-error-port", 0, 0, 0,
333 "Return the port to which errors and warnings should be sent (the\n"
334 "@dfn{standard error} in Unix and C terminology).")
335 #define FUNC_NAME s_scm_current_error_port
341 SCM_DEFINE (scm_current_load_port
, "current-load-port", 0, 0, 0,
343 "Return the current-load-port.\n"
344 "The load port is used internally by `primitive-load'.")
345 #define FUNC_NAME s_scm_current_load_port
347 return scm_cur_loadp
;
351 SCM_DEFINE (scm_set_current_input_port
, "set-current-input-port", 1, 0, 0,
353 "@deffnx primitive set-current-output-port port\n"
354 "@deffnx primitive set-current-error-port port\n"
355 "Change the ports returned by @code{current-input-port},\n"
356 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
357 "so that they use the supplied @var{port} for input or output.")
358 #define FUNC_NAME s_scm_set_current_input_port
360 SCM oinp
= scm_cur_inp
;
361 SCM_VALIDATE_OPINPORT (1,port
);
368 SCM_DEFINE (scm_set_current_output_port
, "set-current-output-port", 1, 0, 0,
370 "Set the current default output port to PORT.")
371 #define FUNC_NAME s_scm_set_current_output_port
373 SCM ooutp
= scm_cur_outp
;
374 port
= SCM_COERCE_OUTPORT (port
);
375 SCM_VALIDATE_OPOUTPORT (1,port
);
382 SCM_DEFINE (scm_set_current_error_port
, "set-current-error-port", 1, 0, 0,
384 "Set the current default error port to PORT.")
385 #define FUNC_NAME s_scm_set_current_error_port
387 SCM oerrp
= scm_cur_errp
;
388 port
= SCM_COERCE_OUTPORT (port
);
389 SCM_VALIDATE_OPOUTPORT (1,port
);
396 /* The port table --- an array of pointers to ports. */
398 scm_port
**scm_port_table
;
400 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
401 int scm_port_table_room
= 20; /* Size of the array. */
403 /* Add a port to the table. */
406 scm_add_to_port_table (SCM port
)
410 if (scm_port_table_size
== scm_port_table_room
)
412 void *newt
= realloc ((char *) scm_port_table
,
413 (scm_sizet
) (sizeof (scm_port
*)
414 * scm_port_table_room
* 2));
416 scm_memory_error ("scm_add_to_port_table");
417 scm_port_table
= (scm_port
**) newt
;
418 scm_port_table_room
*= 2;
420 entry
= (scm_port
*) malloc (sizeof (scm_port
));
422 scm_memory_error ("scm_add_to_port_table");
425 entry
->entry
= scm_port_table_size
;
428 entry
->file_name
= SCM_BOOL_F
;
429 entry
->line_number
= 0;
430 entry
->column_number
= 0;
431 entry
->putback_buf
= 0;
432 entry
->putback_buf_size
= 0;
433 entry
->rw_active
= SCM_PORT_NEITHER
;
434 entry
->rw_random
= 0;
436 scm_port_table
[scm_port_table_size
] = entry
;
437 scm_port_table_size
++;
442 /* Remove a port from the table and destroy it. */
445 scm_remove_from_port_table (SCM port
)
447 scm_port
*p
= SCM_PTAB_ENTRY (port
);
450 if (i
>= scm_port_table_size
)
451 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
453 free (p
->putback_buf
);
455 /* Since we have just freed slot i we can shrink the table by moving
456 the last entry to that slot... */
457 if (i
< scm_port_table_size
- 1)
459 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
460 scm_port_table
[i
]->entry
= i
;
462 SCM_SETPTAB_ENTRY (port
, 0);
463 scm_port_table_size
--;
467 /* Functions for debugging. */
469 SCM_DEFINE (scm_pt_size
, "pt-size", 0, 0, 0,
471 "Returns the number of ports in the port table.\n"
472 "`pt-size' is only included in GUILE_DEBUG builds.")
473 #define FUNC_NAME s_scm_pt_size
475 return SCM_MAKINUM (scm_port_table_size
);
479 SCM_DEFINE (scm_pt_member
, "pt-member", 1, 0, 0,
481 "Returns the port at INDEX in the port table.\n"
482 "`pt-member' is only included in GUILE_DEBUG builds.")
483 #define FUNC_NAME s_scm_pt_member
486 SCM_VALIDATE_INUM_COPY (1,index
,i
);
487 if (i
< 0 || i
>= scm_port_table_size
)
490 return scm_port_table
[i
]->port
;
496 scm_port_non_buffer (scm_port
*pt
)
498 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
499 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
500 pt
->read_buf_size
= pt
->write_buf_size
= 1;
501 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
505 /* Revealed counts --- an oddity inherited from SCSH. */
507 /* Find a port in the table and return its revealed count.
508 Also used by the garbage collector.
512 scm_revealed_count (SCM port
)
514 return SCM_REVEALED(port
);
519 /* Return the revealed count for a port. */
521 SCM_DEFINE (scm_port_revealed
, "port-revealed", 1, 0, 0,
523 "Returns the revealed count for @var{port}.")
524 #define FUNC_NAME s_scm_port_revealed
526 port
= SCM_COERCE_OUTPORT (port
);
527 SCM_VALIDATE_PORT (1,port
);
528 return SCM_MAKINUM (scm_revealed_count (port
));
532 /* Set the revealed count for a port. */
533 SCM_DEFINE (scm_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0,
534 (SCM port
, SCM rcount
),
535 "Sets the revealed count for a port to a given value.\n"
536 "The return value is unspecified.")
537 #define FUNC_NAME s_scm_set_port_revealed_x
539 port
= SCM_COERCE_OUTPORT (port
);
540 SCM_VALIDATE_PORT (1,port
);
541 SCM_VALIDATE_INUM (2,rcount
);
542 SCM_REVEALED (port
) = SCM_INUM (rcount
);
543 return SCM_UNSPECIFIED
;
549 /* Retrieving a port's mode. */
551 /* Return the flags that characterize a port based on the mode
552 * string used to open a file for that port.
554 * See PORT FLAGS in scm.h
558 scm_mode_bits (char *modes
)
561 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
562 | ( strchr (modes
, 'w')
563 || strchr (modes
, 'a')
564 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
565 | (strchr (modes
, '0') ? SCM_BUF0
: 0)
566 | (strchr (modes
, 'l') ? SCM_BUFLINE
: 0));
570 /* Return the mode flags from an open port.
571 * Some modes such as "append" are only used when opening
572 * a file and are not returned here. */
574 SCM_DEFINE (scm_port_mode
, "port-mode", 1, 0, 0,
576 "Returns the port modes associated with the open port @var{port}. These\n"
577 "will not necessarily be identical to the modes used when the port was\n"
578 "opened, since modes such as \"append\" which are used only during\n"
579 "port creation are not retained.")
580 #define FUNC_NAME s_scm_port_mode
585 port
= SCM_COERCE_OUTPORT (port
);
586 SCM_VALIDATE_OPPORT (1,port
);
587 if (SCM_UNPACK_CAR (port
) & SCM_RDNG
) {
588 if (SCM_UNPACK_CAR (port
) & SCM_WRTNG
)
589 strcpy (modes
, "r+");
593 else if (SCM_UNPACK_CAR (port
) & SCM_WRTNG
)
595 if (SCM_UNPACK_CAR (port
) & SCM_BUF0
)
597 return scm_makfromstr (modes
, strlen (modes
), 0);
606 * Call the close operation on a port object.
607 * see also scm_close.
609 SCM_DEFINE (scm_close_port
, "close-port", 1, 0, 0,
611 "Close the specified port object. Returns @code{#t} if it successfully\n"
612 "closes a port or @code{#f} if it was already\n"
613 "closed. An exception may be raised if an error occurs, for example\n"
614 "when flushing buffered output.\n"
615 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
616 "which can close file descriptors.")
617 #define FUNC_NAME s_scm_close_port
622 port
= SCM_COERCE_OUTPORT (port
);
624 SCM_VALIDATE_PORT (1, port
);
625 if (SCM_CLOSEDP (port
))
627 i
= SCM_PTOBNUM (port
);
628 if (scm_ptobs
[i
].close
)
629 rv
= (scm_ptobs
[i
].close
) (port
);
632 scm_remove_from_port_table (port
);
633 SCM_SETAND_CAR (port
, ~SCM_OPN
);
634 return SCM_NEGATE_BOOL (rv
< 0);
638 SCM_DEFINE (scm_close_input_port
, "close-input-port", 1, 0, 0,
640 "Close the specified input port object. The routine has no effect if\n"
641 "the file has already been closed. An exception may be raised if an\n"
642 "error occurs. The value returned is unspecified.\n\n"
643 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
644 "which can close file descriptors.")
645 #define FUNC_NAME s_scm_close_input_port
647 SCM_VALIDATE_INPUT_PORT (1, port
);
648 scm_close_port (port
);
649 return SCM_UNSPECIFIED
;
653 SCM_DEFINE (scm_close_output_port
, "close-output-port", 1, 0, 0,
655 "Close the specified output port object. The routine has no effect if\n"
656 "the file has already been closed. An exception may be raised if an\n"
657 "error occurs. The value returned is unspecified.\n\n"
658 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
659 "which can close file descriptors.")
660 #define FUNC_NAME s_scm_close_output_port
662 port
= SCM_COERCE_OUTPORT (port
);
663 SCM_VALIDATE_OUTPUT_PORT (1, port
);
664 scm_close_port (port
);
665 return SCM_UNSPECIFIED
;
669 SCM_DEFINE (scm_close_all_ports_except
, "close-all-ports-except", 0, 0, 1,
671 "Close all open file ports used by the interpreter\n"
672 "except for those supplied as arguments. This procedure\n"
673 "is intended to be used before an exec call to close file descriptors\n"
674 "which are not needed in the new process.")
675 #define FUNC_NAME s_scm_close_all_ports_except
678 SCM_VALIDATE_CONS (1,ports
);
679 while (i
< scm_port_table_size
)
681 SCM thisport
= scm_port_table
[i
]->port
;
683 SCM ports_ptr
= ports
;
685 while (SCM_NNULLP (ports_ptr
))
687 SCM port
= SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr
));
689 SCM_VALIDATE_OPPORT (SCM_ARG1
,port
);
690 if (SCM_EQ_P (port
, thisport
))
692 ports_ptr
= SCM_CDR (ports_ptr
);
697 /* i is not to be incremented here. */
698 scm_close_port (thisport
);
700 return SCM_UNSPECIFIED
;
706 /* Utter miscellany. Gosh, we should clean this up some time. */
708 SCM_DEFINE (scm_input_port_p
, "input-port?", 1, 0, 0,
710 "Returns @code{#t} if @var{x} is an input port, otherwise returns\n"
711 "@code{#f}. Any object satisfying this predicate also satisfies\n"
713 #define FUNC_NAME s_scm_input_port_p
717 return SCM_BOOL(SCM_INPORTP (x
));
721 SCM_DEFINE (scm_output_port_p
, "output-port?", 1, 0, 0,
723 "Returns @code{#t} if @var{x} is an output port, otherwise returns\n"
724 "@code{#f}. Any object satisfying this predicate also satisfies\n"
726 #define FUNC_NAME s_scm_output_port_p
730 if (SCM_PORT_WITH_PS_P (x
))
731 x
= SCM_PORT_WITH_PS_PORT (x
);
732 return SCM_BOOL(SCM_OUTPORTP (x
));
736 SCM_DEFINE (scm_port_closed_p
, "port-closed?", 1, 0, 0,
738 "Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.")
739 #define FUNC_NAME s_scm_port_closed_p
741 SCM_VALIDATE_PORT (1,port
);
742 return SCM_NEGATE_BOOL(SCM_OPPORTP (port
));
746 SCM_DEFINE (scm_eof_object_p
, "eof-object?", 1, 0, 0,
748 "Returns @code{#t} if @var{x} is an end-of-file object; otherwise\n"
749 "returns @code{#f}.")
750 #define FUNC_NAME s_scm_eof_object_p
752 return SCM_BOOL(SCM_EOF_OBJECT_P (x
));
756 SCM_DEFINE (scm_force_output
, "force-output", 0, 1, 0,
758 "Flush the specified output port, or the current output port if @var{port}\n"
759 "is omitted. The current output buffer contents are passed to the \n"
760 "underlying port implementation (e.g., in the case of fports, the\n"
761 "data will be written to the file and the output buffer will be cleared.)\n"
762 "It has no effect on an unbuffered port.\n\n"
763 "The return value is unspecified.")
764 #define FUNC_NAME s_scm_force_output
766 if (SCM_UNBNDP (port
))
770 port
= SCM_COERCE_OUTPORT (port
);
771 SCM_VALIDATE_OPOUTPORT (1,port
);
774 return SCM_UNSPECIFIED
;
778 SCM_DEFINE (scm_flush_all_ports
, "flush-all-ports", 0, 0, 0,
780 "Equivalent to calling @code{force-output} on\n"
781 "all open output ports. The return value is unspecified.")
782 #define FUNC_NAME s_scm_flush_all_ports
786 for (i
= 0; i
< scm_port_table_size
; i
++)
788 if (SCM_OPOUTPORTP (scm_port_table
[i
]->port
))
789 scm_flush (scm_port_table
[i
]->port
);
791 return SCM_UNSPECIFIED
;
795 SCM_DEFINE (scm_read_char
, "read-char", 0, 1, 0,
797 "Returns the next character available from @var{port}, updating\n"
798 "@var{port} to point to the following character. If no more\n"
799 "characters are available, an end-of-file object is returned.")
800 #define FUNC_NAME s_scm_read_char
803 if (SCM_UNBNDP (port
))
805 SCM_VALIDATE_OPINPORT (1,port
);
809 return SCM_MAKE_CHAR (c
);
813 /* this should only be called when the read buffer is empty. it
814 tries to refill the read buffer. it returns the first char from
815 the port, which is either EOF or *(pt->read_pos). */
817 scm_fill_input (SCM port
)
819 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
821 if (pt
->read_buf
== pt
->putback_buf
)
823 /* finished reading put-back chars. */
824 pt
->read_buf
= pt
->saved_read_buf
;
825 pt
->read_pos
= pt
->saved_read_pos
;
826 pt
->read_end
= pt
->saved_read_end
;
827 pt
->read_buf_size
= pt
->saved_read_buf_size
;
828 if (pt
->read_pos
< pt
->read_end
)
829 return *(pt
->read_pos
);
831 return scm_ptobs
[SCM_PTOBNUM (port
)].fill_input (port
);
838 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
840 if (pt
->rw_active
== SCM_PORT_WRITE
)
842 /* may be marginally faster than calling scm_flush. */
843 scm_ptobs
[SCM_PTOBNUM (port
)].flush (port
);
847 pt
->rw_active
= SCM_PORT_READ
;
849 if (pt
->read_pos
>= pt
->read_end
)
851 if (scm_fill_input (port
) == EOF
)
855 c
= *(pt
->read_pos
++);
874 scm_putc (char c
, SCM port
)
876 scm_lfwrite (&c
, 1, port
);
880 scm_puts (const char *s
, SCM port
)
882 scm_lfwrite (s
, strlen (s
), port
);
886 scm_lfwrite (const char *ptr
, scm_sizet size
, SCM port
)
888 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
889 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
891 if (pt
->rw_active
== SCM_PORT_READ
)
892 scm_end_input (port
);
894 ptob
->write (port
, ptr
, size
);
897 pt
->rw_active
= SCM_PORT_WRITE
;
904 scm_sizet i
= SCM_PTOBNUM (port
);
905 (scm_ptobs
[i
].flush
) (port
);
909 scm_end_input (SCM port
)
912 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
914 if (pt
->read_buf
== pt
->putback_buf
)
916 offset
= pt
->read_end
- pt
->read_pos
;
917 pt
->read_buf
= pt
->saved_read_buf
;
918 pt
->read_pos
= pt
->saved_read_pos
;
919 pt
->read_end
= pt
->saved_read_end
;
920 pt
->read_buf_size
= pt
->saved_read_buf_size
;
925 scm_ptobs
[SCM_PTOBNUM (port
)].end_input (port
, offset
);
932 scm_ungetc (int c
, SCM port
)
934 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
936 if (pt
->read_buf
== pt
->putback_buf
)
937 /* already using the put-back buffer. */
939 /* enlarge putback_buf if necessary. */
940 if (pt
->read_end
== pt
->read_buf
+ pt
->read_buf_size
941 && pt
->read_buf
== pt
->read_pos
)
943 int new_size
= pt
->read_buf_size
* 2;
945 (unsigned char *) realloc (pt
->putback_buf
, new_size
);
948 scm_memory_error ("scm_ungetc");
949 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
= tmp
;
950 pt
->read_end
= pt
->read_buf
+ pt
->read_buf_size
;
951 pt
->read_buf_size
= pt
->putback_buf_size
= new_size
;
954 /* shift any existing bytes to buffer + 1. */
955 if (pt
->read_pos
== pt
->read_end
)
956 pt
->read_end
= pt
->read_buf
+ 1;
957 else if (pt
->read_pos
!= pt
->read_buf
+ 1)
959 int count
= pt
->read_end
- pt
->read_pos
;
961 memmove (pt
->read_buf
+ 1, pt
->read_pos
, count
);
962 pt
->read_end
= pt
->read_buf
+ 1 + count
;
965 pt
->read_pos
= pt
->read_buf
;
968 /* switch to the put-back buffer. */
970 if (pt
->putback_buf
== NULL
)
972 pt
->putback_buf
= (char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE
);
973 if (pt
->putback_buf
== NULL
)
974 scm_memory_error ("scm_ungetc");
975 pt
->putback_buf_size
= SCM_INITIAL_PUTBACK_BUF_SIZE
;
978 pt
->saved_read_buf
= pt
->read_buf
;
979 pt
->saved_read_pos
= pt
->read_pos
;
980 pt
->saved_read_end
= pt
->read_end
;
981 pt
->saved_read_buf_size
= pt
->read_buf_size
;
983 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
;
984 pt
->read_end
= pt
->read_buf
+ 1;
985 pt
->read_buf_size
= pt
->putback_buf_size
;
991 pt
->rw_active
= SCM_PORT_READ
;
995 /* What should col be in this case?
996 * We'll leave it at -1.
998 SCM_LINUM (port
) -= 1;
1006 scm_ungets (const char *s
, int n
, SCM port
)
1008 /* This is simple minded and inefficient, but unreading strings is
1009 * probably not a common operation, and remember that line and
1010 * column numbers have to be handled...
1012 * Please feel free to write an optimized version!
1015 scm_ungetc (s
[n
], port
);
1019 SCM_DEFINE (scm_peek_char
, "peek-char", 0, 1, 0,
1021 "Returns the next character available from @var{port},\n"
1022 "@emph{without} updating @var{port} to point to the following\n"
1023 "character. If no more characters are available, an end-of-file object\n"
1024 "is returned.@footnote{The value returned by a call to @code{peek-char}\n"
1025 "is the same as the value that would have been returned by a call to\n"
1026 "@code{read-char} on the same port. The only difference is that the very\n"
1027 "next call to @code{read-char} or @code{peek-char} on that\n"
1028 "@var{port} will return the value returned by the preceding call to\n"
1029 "@code{peek-char}. In particular, a call to @code{peek-char} on an\n"
1030 "interactive port will hang waiting for input whenever a call to\n"
1031 "@code{read-char} would have hung.}")
1032 #define FUNC_NAME s_scm_peek_char
1035 if (SCM_UNBNDP (port
))
1038 SCM_VALIDATE_OPINPORT (1,port
);
1039 c
= scm_getc (port
);
1042 scm_ungetc (c
, port
);
1043 return SCM_MAKE_CHAR (c
);
1047 SCM_DEFINE (scm_unread_char
, "unread-char", 2, 0, 0,
1048 (SCM cobj
, SCM port
),
1049 "Place @var{char} in @var{port} so that it will be read by the\n"
1050 "next read operation. If called multiple times, the unread characters\n"
1051 "will be read again in last-in first-out order. If @var{port} is\n"
1052 "not supplied, the current input port is used.")
1053 #define FUNC_NAME s_scm_unread_char
1057 SCM_VALIDATE_CHAR (1,cobj
);
1058 if (SCM_UNBNDP (port
))
1061 SCM_VALIDATE_OPINPORT (2,port
);
1063 c
= SCM_CHAR (cobj
);
1065 scm_ungetc (c
, port
);
1070 SCM_DEFINE (scm_unread_string
, "unread-string", 2, 0, 0,
1071 (SCM str
, SCM port
),
1072 "Place the string @var{str} in @var{port} so that its characters will be\n"
1073 "read in subsequent read operations. If called multiple times, the\n"
1074 "unread characters will be read again in last-in first-out order. If\n"
1075 "@var{port} is not supplied, the current-input-port is used.")
1076 #define FUNC_NAME s_scm_unread_string
1078 SCM_VALIDATE_STRING (1,str
);
1079 if (SCM_UNBNDP (port
))
1082 SCM_VALIDATE_OPINPORT (2,port
);
1084 scm_ungets (SCM_ROUCHARS (str
), SCM_LENGTH (str
), port
);
1090 SCM_DEFINE (scm_seek
, "seek", 3, 0, 0,
1091 (SCM object
, SCM offset
, SCM whence
),
1092 "Sets the current position of @var{fd/port} to the integer @var{offset},\n"
1093 "which is interpreted according to the value of @var{whence}.\n\n"
1094 "One of the following variables should be supplied\n"
1095 "for @var{whence}:\n"
1096 "@defvar SEEK_SET\n"
1097 "Seek from the beginning of the file.\n"
1099 "@defvar SEEK_CUR\n"
1100 "Seek from the current position.\n"
1102 "@defvar SEEK_END\n"
1103 "Seek from the end of the file.\n"
1105 "If @var{fd/port} is a file descriptor, the underlying system call is\n"
1106 "@code{lseek}. @var{port} may be a string port.\n\n"
1107 "The value returned is the new position in the file. This means that\n"
1108 "the current position of a port can be obtained using:\n"
1110 "(seek port 0 SEEK_CUR)\n"
1112 #define FUNC_NAME s_scm_seek
1118 object
= SCM_COERCE_OUTPORT (object
);
1120 off
= SCM_NUM2LONG (2,offset
);
1121 SCM_VALIDATE_INUM_COPY (3,whence
,how
);
1122 if (how
!= SEEK_SET
&& how
!= SEEK_CUR
&& how
!= SEEK_END
)
1123 SCM_OUT_OF_RANGE (3, whence
);
1124 if (SCM_OPPORTP (object
))
1126 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1129 SCM_MISC_ERROR ("port is not seekable",
1130 scm_cons (object
, SCM_EOL
));
1132 rv
= ptob
->seek (object
, off
, how
);
1134 else /* file descriptor?. */
1136 SCM_VALIDATE_INUM (1,object
);
1137 rv
= lseek (SCM_INUM (object
), off
, how
);
1141 return scm_long2num (rv
);
1145 SCM_DEFINE (scm_truncate_file
, "truncate-file", 1, 1, 0,
1146 (SCM object
, SCM length
),
1147 "Truncates the object referred to by @var{obj} to at most @var{size} bytes.\n"
1148 "@var{obj} can be a string containing a file name or an integer file\n"
1149 "descriptor or a port. @var{size} may be omitted if @var{obj} is not\n"
1150 "a file name, in which case the truncation occurs at the current port.\n"
1152 "The return value is unspecified.")
1153 #define FUNC_NAME s_scm_truncate_file
1158 /* object can be a port, fdes or filename. */
1160 if (SCM_UNBNDP (length
))
1162 /* must supply length if object is a filename. */
1163 if (SCM_ROSTRINGP (object
))
1164 SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL
);
1166 length
= scm_seek (object
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
1168 c_length
= SCM_NUM2LONG (2,length
);
1170 SCM_MISC_ERROR ("negative offset", SCM_EOL
);
1172 object
= SCM_COERCE_OUTPORT (object
);
1173 if (SCM_INUMP (object
))
1175 SCM_SYSCALL (rv
= ftruncate (SCM_INUM (object
), c_length
));
1177 else if (SCM_OPOUTPORTP (object
))
1179 scm_port
*pt
= SCM_PTAB_ENTRY (object
);
1180 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1182 if (!ptob
->truncate
)
1183 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL
);
1184 if (pt
->rw_active
== SCM_PORT_READ
)
1185 scm_end_input (object
);
1186 else if (pt
->rw_active
== SCM_PORT_WRITE
)
1187 ptob
->flush (object
);
1189 ptob
->truncate (object
, c_length
);
1194 SCM_VALIDATE_ROSTRING (1,object
);
1195 SCM_COERCE_SUBSTR (object
);
1196 SCM_SYSCALL (rv
= truncate (SCM_ROCHARS (object
), c_length
));
1200 return SCM_UNSPECIFIED
;
1204 SCM_DEFINE (scm_port_line
, "port-line", 1, 0, 0,
1206 "Return the current line number for PORT.")
1207 #define FUNC_NAME s_scm_port_line
1209 port
= SCM_COERCE_OUTPORT (port
);
1210 SCM_VALIDATE_OPENPORT (1,port
);
1211 return SCM_MAKINUM (SCM_LINUM (port
));
1215 SCM_DEFINE (scm_set_port_line_x
, "set-port-line!", 2, 0, 0,
1216 (SCM port
, SCM line
),
1217 "Set the current line number for PORT to LINE.")
1218 #define FUNC_NAME s_scm_set_port_line_x
1220 port
= SCM_COERCE_OUTPORT (port
);
1221 SCM_VALIDATE_OPENPORT (1,port
);
1222 SCM_VALIDATE_INUM (2,line
);
1223 SCM_PTAB_ENTRY (port
)->line_number
= SCM_INUM (line
);
1224 return SCM_UNSPECIFIED
;
1228 SCM_DEFINE (scm_port_column
, "port-column", 1, 0, 0,
1230 "@deffnx primitive port-line [input-port]\n"
1231 "Return the current column number or line number of @var{input-port},\n"
1232 "using the current input port if none is specified. If the number is\n"
1233 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1234 "- i.e. the first character of the first line is line 0, column 0.\n"
1235 "(However, when you display a file position, for example in an error\n"
1236 "message, we recommand you add 1 to get 1-origin integers. This is\n"
1237 "because lines and column numbers traditionally start with 1, and that is\n"
1238 "what non-programmers will find most natural.)")
1239 #define FUNC_NAME s_scm_port_column
1241 port
= SCM_COERCE_OUTPORT (port
);
1242 SCM_VALIDATE_OPENPORT (1,port
);
1243 return SCM_MAKINUM (SCM_COL (port
));
1247 SCM_DEFINE (scm_set_port_column_x
, "set-port-column!", 2, 0, 0,
1248 (SCM port
, SCM column
),
1249 "@deffnx primitive set-port-column! [input-port] column\n"
1250 "Set the current column or line number of @var{input-port}, using the\n"
1251 "current input port if none is specified.")
1252 #define FUNC_NAME s_scm_set_port_column_x
1254 port
= SCM_COERCE_OUTPORT (port
);
1255 SCM_VALIDATE_OPENPORT (1,port
);
1256 SCM_VALIDATE_INUM (2,column
);
1257 SCM_PTAB_ENTRY (port
)->column_number
= SCM_INUM (column
);
1258 return SCM_UNSPECIFIED
;
1262 SCM_DEFINE (scm_port_filename
, "port-filename", 1, 0, 0,
1264 "Return the filename associated with @var{port}. This function returns\n"
1265 "the strings \"standard input\", \"standard output\" and \"standard error\""
1266 "when called on the current input, output and error ports respectively.")
1267 #define FUNC_NAME s_scm_port_filename
1269 port
= SCM_COERCE_OUTPORT (port
);
1270 SCM_VALIDATE_OPENPORT (1,port
);
1271 return SCM_PTAB_ENTRY (port
)->file_name
;
1275 SCM_DEFINE (scm_set_port_filename_x
, "set-port-filename!", 2, 0, 0,
1276 (SCM port
, SCM filename
),
1277 "Change the filename associated with @var{port}, using the current input\n"
1278 "port if none is specified. Note that this does not change the port's\n"
1279 "source of data, but only the value that is returned by\n"
1280 "@code{port-filename} and reported in diagnostic output.")
1281 #define FUNC_NAME s_scm_set_port_filename_x
1283 port
= SCM_COERCE_OUTPORT (port
);
1284 SCM_VALIDATE_OPENPORT (1,port
);
1285 /* We allow the user to set the filename to whatever he likes. */
1286 return SCM_PTAB_ENTRY (port
)->file_name
= filename
;
1291 extern char * ttyname();
1295 scm_print_port_mode (SCM exp
, SCM port
)
1297 scm_puts (SCM_CLOSEDP (exp
)
1299 : (SCM_RDNG
& SCM_UNPACK_CAR (exp
)
1300 ? (SCM_WRTNG
& SCM_UNPACK_CAR (exp
)
1303 : (SCM_WRTNG
& SCM_UNPACK_CAR (exp
)
1310 scm_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
1312 char *type
= SCM_PTOBNAME (SCM_PTOBNUM (exp
));
1315 scm_puts ("#<", port
);
1316 scm_print_port_mode (exp
, port
);
1317 scm_puts (type
, port
);
1318 scm_putc (' ', port
);
1319 scm_intprint (SCM_CELL_WORD_1 (exp
), 16, port
);
1320 scm_putc ('>', port
);
1324 extern void scm_make_fptob ();
1325 extern void scm_make_stptob ();
1326 extern void scm_make_sfptob ();
1329 scm_ports_prehistory ()
1332 scm_ptobs
= (scm_ptob_descriptor
*) malloc (sizeof (scm_ptob_descriptor
));
1334 /* WARNING: These scm_newptob calls must be done in this order.
1335 * They must agree with the port declarations in tags.h.
1337 /* scm_tc16_fport = */ scm_make_fptob ();
1338 /* scm_tc16_pipe was here */ scm_make_fptob (); /* dummy. */
1339 /* scm_tc16_strport = */ scm_make_stptob ();
1340 /* scm_tc16_sfport = */ scm_make_sfptob ();
1347 long scm_tc16_void_port
= 0;
1349 static int fill_input_void_port (SCM port
)
1355 write_void_port (SCM port
, const void *data
, size_t size
)
1360 scm_void_port (char *mode_str
)
1366 SCM_NEWCELL (answer
);
1368 mode_bits
= scm_mode_bits (mode_str
);
1369 pt
= scm_add_to_port_table (answer
);
1370 scm_port_non_buffer (pt
);
1371 SCM_SETPTAB_ENTRY (answer
, pt
);
1372 SCM_SETSTREAM (answer
, 0);
1373 SCM_SET_CELL_TYPE (answer
, scm_tc16_void_port
| mode_bits
);
1378 SCM_DEFINE (scm_sys_make_void_port
, "%make-void-port", 1, 0, 0,
1380 "Create and return a new void port. A void port acts like\n"
1381 "/dev/null. The @var{mode} argument\n"
1382 "specifies the input/output modes for this port: see the\n"
1383 "documentation for @code{open-file} in @ref{File Ports}.")
1384 #define FUNC_NAME s_scm_sys_make_void_port
1386 SCM_VALIDATE_ROSTRING (1,mode
);
1387 SCM_COERCE_SUBSTR (mode
);
1388 return scm_void_port (SCM_ROCHARS (mode
));
1393 /* Initialization. */
1398 /* lseek() symbols. */
1399 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET
));
1400 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR
));
1401 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END
));
1403 scm_tc16_void_port
= scm_make_port_type ("void", fill_input_void_port
,
1405 #include "libguile/ports.x"