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/eval.h"
51 #include "libguile/objects.h"
52 #include "libguile/smob.h"
53 #include "libguile/chars.h"
55 #include "libguile/keywords.h"
56 #include "libguile/root.h"
57 #include "libguile/strings.h"
59 #include "libguile/validate.h"
60 #include "libguile/ports.h"
74 #ifdef HAVE_SYS_IOCTL_H
75 #include <sys/ioctl.h>
79 /* The port kind table --- a dynamically resized array of port types. */
82 /* scm_ptobs scm_numptob
83 * implement a dynamicly resized array of ptob records.
84 * Indexes into this table are used when generating type
85 * tags for smobjects (if you know a tag you can get an index and conversely).
87 scm_ptob_descriptor
*scm_ptobs
;
90 /* GC marker for a port with stream of SCM type. */
92 scm_markstream (SCM ptr
)
95 openp
= SCM_CELL_WORD_0 (ptr
) & SCM_OPN
;
97 return SCM_PACK (SCM_STREAM (ptr
));
103 * We choose to use an interface similar to the smob interface with
104 * fill_input and write as standard fields, passed to the port
105 * type constructor, and optional fields set by setters.
109 flush_port_default (SCM port
)
114 end_input_default (SCM port
, int offset
)
119 scm_make_port_type (char *name
,
120 int (*fill_input
) (SCM port
),
121 void (*write
) (SCM port
, const void *data
, size_t size
))
124 if (255 <= scm_numptob
)
127 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
,
129 * sizeof (scm_ptob_descriptor
)));
132 scm_ptobs
= (scm_ptob_descriptor
*) tmp
;
134 scm_ptobs
[scm_numptob
].name
= name
;
135 scm_ptobs
[scm_numptob
].mark
= 0;
136 scm_ptobs
[scm_numptob
].free
= scm_free0
;
137 scm_ptobs
[scm_numptob
].print
= scm_port_print
;
138 scm_ptobs
[scm_numptob
].equalp
= 0;
139 scm_ptobs
[scm_numptob
].close
= 0;
141 scm_ptobs
[scm_numptob
].write
= write
;
142 scm_ptobs
[scm_numptob
].flush
= flush_port_default
;
144 scm_ptobs
[scm_numptob
].end_input
= end_input_default
;
145 scm_ptobs
[scm_numptob
].fill_input
= fill_input
;
146 scm_ptobs
[scm_numptob
].input_waiting
= 0;
148 scm_ptobs
[scm_numptob
].seek
= 0;
149 scm_ptobs
[scm_numptob
].truncate
= 0;
157 scm_memory_error ("scm_make_port_type");
159 /* Make a class object if Goops is present */
161 scm_make_port_classes (scm_numptob
- 1, SCM_PTOBNAME (scm_numptob
- 1));
162 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
166 scm_set_port_mark (long tc
, SCM (*mark
) (SCM
))
168 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].mark
= mark
;
172 scm_set_port_free (long tc
, scm_sizet (*free
) (SCM
))
174 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].free
= free
;
178 scm_set_port_print (long tc
, int (*print
) (SCM exp
, SCM port
,
179 scm_print_state
*pstate
))
181 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].print
= print
;
185 scm_set_port_equalp (long tc
, SCM (*equalp
) (SCM
, SCM
))
187 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].equalp
= equalp
;
191 scm_set_port_flush (long tc
, void (*flush
) (SCM port
))
193 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].flush
= flush
;
197 scm_set_port_end_input (long tc
, void (*end_input
) (SCM port
, int offset
))
199 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].end_input
= end_input
;
203 scm_set_port_close (long tc
, int (*close
) (SCM
))
205 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].close
= close
;
209 scm_set_port_seek (long tc
, off_t (*seek
) (SCM port
,
213 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].seek
= seek
;
217 scm_set_port_truncate (long tc
, void (*truncate
) (SCM port
, off_t length
))
219 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].truncate
= truncate
;
223 scm_set_port_input_waiting (long tc
, int (*input_waiting
) (SCM
))
225 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].input_waiting
= input_waiting
;
230 SCM_DEFINE (scm_char_ready_p
, "char-ready?", 0, 1, 0,
232 "Returns @code{#t} if a character is ready on input @var{port} and\n"
233 "returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}\n"
234 "then the next @code{read-char} operation on @var{port} is\n"
235 "guaranteed not to hang. If @var{port} is a file port at end of\n"
236 "file then @code{char-ready?} returns @code{#t}.\n"
237 "@footnote{@code{char-ready?} exists to make it possible for a\n"
238 "program to accept characters from interactive ports without getting\n"
239 "stuck waiting for input. Any input editors associated with such ports\n"
240 "must make sure that characters whose existence has been asserted by\n"
241 "@code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to\n"
242 "return @code{#f} at end of file, a port at end of file would be\n"
243 "indistinguishable from an interactive port that has no ready\n"
245 #define FUNC_NAME s_scm_char_ready_p
249 if (SCM_UNBNDP (port
))
252 SCM_VALIDATE_OPINPORT (1,port
);
254 pt
= SCM_PTAB_ENTRY (port
);
256 /* if the current read buffer is filled, or the
257 last pushed-back char has been read and the saved buffer is
258 filled, result is true. */
259 if (pt
->read_pos
< pt
->read_end
260 || (pt
->read_buf
== pt
->putback_buf
261 && pt
->saved_read_pos
< pt
->saved_read_end
))
265 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
267 if (ptob
->input_waiting
)
268 return SCM_BOOL(ptob
->input_waiting (port
));
275 /* move up to read_len chars from port's putback and/or read buffers
276 into memory starting at dest. returns the number of chars moved. */
277 size_t scm_take_from_input_buffers (SCM port
, char *dest
, size_t read_len
)
279 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
280 size_t chars_read
= 0;
281 size_t from_buf
= min (pt
->read_end
- pt
->read_pos
, read_len
);
285 memcpy (dest
, pt
->read_pos
, from_buf
);
286 pt
->read_pos
+= from_buf
;
287 chars_read
+= from_buf
;
288 read_len
-= from_buf
;
292 /* if putback was active, try the real input buffer too. */
293 if (pt
->read_buf
== pt
->putback_buf
)
295 from_buf
= min (pt
->saved_read_end
- pt
->saved_read_pos
, read_len
);
298 memcpy (dest
, pt
->saved_read_pos
, from_buf
);
299 pt
->saved_read_pos
+= from_buf
;
300 chars_read
+= from_buf
;
306 /* Clear a port's read buffers, returning the contents. */
307 SCM_DEFINE (scm_drain_input
, "drain-input", 1, 0, 0,
309 "Drains @var{PORT}'s read buffers (including any pushed-back characters)\n"
310 "and returns the contents as a single string.")
311 #define FUNC_NAME s_scm_drain_input
314 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
317 SCM_VALIDATE_OPINPORT (1,port
);
319 count
= pt
->read_end
- pt
->read_pos
;
320 if (pt
->read_buf
== pt
->putback_buf
)
321 count
+= pt
->saved_read_end
- pt
->saved_read_pos
;
323 result
= scm_makstr (count
, 0);
324 scm_take_from_input_buffers (port
, SCM_STRING_CHARS (result
), count
);
331 /* Standard ports --- current input, output, error, and more(!). */
333 SCM_DEFINE (scm_current_input_port
, "current-input-port", 0, 0, 0,
335 "Returns the current input port. This is the default port used by many\n"
336 "input procedures. Initially, @code{current-input-port} returns the\n"
337 "value of @code{???}.")
338 #define FUNC_NAME s_scm_current_input_port
344 SCM_DEFINE (scm_current_output_port
, "current-output-port", 0, 0, 0,
346 "Returns the current output port. This is the default port used by many\n"
347 "output procedures. Initially, @code{current-output-port} returns the\n"
348 "value of @code{???}.")
349 #define FUNC_NAME s_scm_current_output_port
355 SCM_DEFINE (scm_current_error_port
, "current-error-port", 0, 0, 0,
357 "Return the port to which errors and warnings should be sent (the\n"
358 "@dfn{standard error} in Unix and C terminology).")
359 #define FUNC_NAME s_scm_current_error_port
365 SCM_DEFINE (scm_current_load_port
, "current-load-port", 0, 0, 0,
367 "Return the current-load-port.\n"
368 "The load port is used internally by `primitive-load'.")
369 #define FUNC_NAME s_scm_current_load_port
371 return scm_cur_loadp
;
375 SCM_DEFINE (scm_set_current_input_port
, "set-current-input-port", 1, 0, 0,
377 "@deffnx primitive set-current-output-port port\n"
378 "@deffnx primitive set-current-error-port port\n"
379 "Change the ports returned by @code{current-input-port},\n"
380 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
381 "so that they use the supplied @var{port} for input or output.")
382 #define FUNC_NAME s_scm_set_current_input_port
384 SCM oinp
= scm_cur_inp
;
385 SCM_VALIDATE_OPINPORT (1,port
);
392 SCM_DEFINE (scm_set_current_output_port
, "set-current-output-port", 1, 0, 0,
394 "Set the current default output port to PORT.")
395 #define FUNC_NAME s_scm_set_current_output_port
397 SCM ooutp
= scm_cur_outp
;
398 port
= SCM_COERCE_OUTPORT (port
);
399 SCM_VALIDATE_OPOUTPORT (1,port
);
406 SCM_DEFINE (scm_set_current_error_port
, "set-current-error-port", 1, 0, 0,
408 "Set the current default error port to PORT.")
409 #define FUNC_NAME s_scm_set_current_error_port
411 SCM oerrp
= scm_cur_errp
;
412 port
= SCM_COERCE_OUTPORT (port
);
413 SCM_VALIDATE_OPOUTPORT (1,port
);
420 /* The port table --- an array of pointers to ports. */
422 scm_port
**scm_port_table
;
424 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
425 int scm_port_table_room
= 20; /* Size of the array. */
427 /* Add a port to the table. */
430 scm_add_to_port_table (SCM port
)
434 if (scm_port_table_size
== scm_port_table_room
)
436 void *newt
= realloc ((char *) scm_port_table
,
437 (scm_sizet
) (sizeof (scm_port
*)
438 * scm_port_table_room
* 2));
440 scm_memory_error ("scm_add_to_port_table");
441 scm_port_table
= (scm_port
**) newt
;
442 scm_port_table_room
*= 2;
444 entry
= (scm_port
*) malloc (sizeof (scm_port
));
446 scm_memory_error ("scm_add_to_port_table");
449 entry
->entry
= scm_port_table_size
;
452 entry
->file_name
= SCM_BOOL_F
;
453 entry
->line_number
= 0;
454 entry
->column_number
= 0;
455 entry
->putback_buf
= 0;
456 entry
->putback_buf_size
= 0;
457 entry
->rw_active
= SCM_PORT_NEITHER
;
458 entry
->rw_random
= 0;
460 scm_port_table
[scm_port_table_size
] = entry
;
461 scm_port_table_size
++;
466 /* Remove a port from the table and destroy it. */
469 scm_remove_from_port_table (SCM port
)
471 scm_port
*p
= SCM_PTAB_ENTRY (port
);
474 if (i
>= scm_port_table_size
)
475 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
477 free (p
->putback_buf
);
479 /* Since we have just freed slot i we can shrink the table by moving
480 the last entry to that slot... */
481 if (i
< scm_port_table_size
- 1)
483 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
484 scm_port_table
[i
]->entry
= i
;
486 SCM_SETPTAB_ENTRY (port
, 0);
487 scm_port_table_size
--;
491 /* Functions for debugging. */
493 SCM_DEFINE (scm_pt_size
, "pt-size", 0, 0, 0,
495 "Returns the number of ports in the port table.\n"
496 "`pt-size' is only included in GUILE_DEBUG builds.")
497 #define FUNC_NAME s_scm_pt_size
499 return SCM_MAKINUM (scm_port_table_size
);
503 SCM_DEFINE (scm_pt_member
, "pt-member", 1, 0, 0,
505 "Returns the port at INDEX in the port table.\n"
506 "`pt-member' is only included in GUILE_DEBUG builds.")
507 #define FUNC_NAME s_scm_pt_member
510 SCM_VALIDATE_INUM_COPY (1,index
,i
);
511 if (i
< 0 || i
>= scm_port_table_size
)
514 return scm_port_table
[i
]->port
;
520 scm_port_non_buffer (scm_port
*pt
)
522 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
523 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
524 pt
->read_buf_size
= pt
->write_buf_size
= 1;
525 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
529 /* Revealed counts --- an oddity inherited from SCSH. */
531 /* Find a port in the table and return its revealed count.
532 Also used by the garbage collector.
536 scm_revealed_count (SCM port
)
538 return SCM_REVEALED(port
);
543 /* Return the revealed count for a port. */
545 SCM_DEFINE (scm_port_revealed
, "port-revealed", 1, 0, 0,
547 "Returns the revealed count for @var{port}.")
548 #define FUNC_NAME s_scm_port_revealed
550 port
= SCM_COERCE_OUTPORT (port
);
551 SCM_VALIDATE_PORT (1,port
);
552 return SCM_MAKINUM (scm_revealed_count (port
));
556 /* Set the revealed count for a port. */
557 SCM_DEFINE (scm_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0,
558 (SCM port
, SCM rcount
),
559 "Sets the revealed count for a port to a given value.\n"
560 "The return value is unspecified.")
561 #define FUNC_NAME s_scm_set_port_revealed_x
563 port
= SCM_COERCE_OUTPORT (port
);
564 SCM_VALIDATE_PORT (1,port
);
565 SCM_VALIDATE_INUM (2,rcount
);
566 SCM_REVEALED (port
) = SCM_INUM (rcount
);
567 return SCM_UNSPECIFIED
;
573 /* Retrieving a port's mode. */
575 /* Return the flags that characterize a port based on the mode
576 * string used to open a file for that port.
578 * See PORT FLAGS in scm.h
582 scm_mode_bits (char *modes
)
585 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
586 | ( strchr (modes
, 'w')
587 || strchr (modes
, 'a')
588 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
589 | (strchr (modes
, '0') ? SCM_BUF0
: 0)
590 | (strchr (modes
, 'l') ? SCM_BUFLINE
: 0));
594 /* Return the mode flags from an open port.
595 * Some modes such as "append" are only used when opening
596 * a file and are not returned here. */
598 SCM_DEFINE (scm_port_mode
, "port-mode", 1, 0, 0,
600 "Returns the port modes associated with the open port @var{port}. These\n"
601 "will not necessarily be identical to the modes used when the port was\n"
602 "opened, since modes such as \"append\" which are used only during\n"
603 "port creation are not retained.")
604 #define FUNC_NAME s_scm_port_mode
609 port
= SCM_COERCE_OUTPORT (port
);
610 SCM_VALIDATE_OPPORT (1,port
);
611 if (SCM_CELL_WORD_0 (port
) & SCM_RDNG
) {
612 if (SCM_CELL_WORD_0 (port
) & SCM_WRTNG
)
613 strcpy (modes
, "r+");
617 else if (SCM_CELL_WORD_0 (port
) & SCM_WRTNG
)
619 if (SCM_CELL_WORD_0 (port
) & SCM_BUF0
)
621 return scm_makfromstr (modes
, strlen (modes
), 0);
630 * Call the close operation on a port object.
631 * see also scm_close.
633 SCM_DEFINE (scm_close_port
, "close-port", 1, 0, 0,
635 "Close the specified port object. Returns @code{#t} if it successfully\n"
636 "closes a port or @code{#f} if it was already\n"
637 "closed. An exception may be raised if an error occurs, for example\n"
638 "when flushing buffered output.\n"
639 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
640 "which can close file descriptors.")
641 #define FUNC_NAME s_scm_close_port
646 port
= SCM_COERCE_OUTPORT (port
);
648 SCM_VALIDATE_PORT (1, port
);
649 if (SCM_CLOSEDP (port
))
651 i
= SCM_PTOBNUM (port
);
652 if (scm_ptobs
[i
].close
)
653 rv
= (scm_ptobs
[i
].close
) (port
);
656 scm_remove_from_port_table (port
);
657 SCM_SETAND_CAR (port
, ~SCM_OPN
);
658 return SCM_NEGATE_BOOL (rv
< 0);
662 SCM_DEFINE (scm_close_input_port
, "close-input-port", 1, 0, 0,
664 "Close the specified input port object. The routine has no effect if\n"
665 "the file has already been closed. An exception may be raised if an\n"
666 "error occurs. The value returned is unspecified.\n\n"
667 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
668 "which can close file descriptors.")
669 #define FUNC_NAME s_scm_close_input_port
671 SCM_VALIDATE_INPUT_PORT (1, port
);
672 scm_close_port (port
);
673 return SCM_UNSPECIFIED
;
677 SCM_DEFINE (scm_close_output_port
, "close-output-port", 1, 0, 0,
679 "Close the specified output port object. The routine has no effect if\n"
680 "the file has already been closed. An exception may be raised if an\n"
681 "error occurs. The value returned is unspecified.\n\n"
682 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
683 "which can close file descriptors.")
684 #define FUNC_NAME s_scm_close_output_port
686 port
= SCM_COERCE_OUTPORT (port
);
687 SCM_VALIDATE_OUTPUT_PORT (1, port
);
688 scm_close_port (port
);
689 return SCM_UNSPECIFIED
;
693 SCM_DEFINE (scm_port_for_each
, "port-for-each", 1, 0, 0,
695 "Apply @var{proc} to each port in the Guile port table\n"
696 "in turn. The return value is unspecified.")
697 #define FUNC_NAME s_scm_port_for_each
702 SCM_VALIDATE_PROC (1, proc
);
704 /* when pre-emptive multithreading is supported, access to the port
705 table will need to be controlled by a mutex. */
707 /* Even without pre-emptive multithreading, running arbitrary code
708 while scanning the port table is unsafe because the port table
709 can change arbitrarily (from a GC, for example). So we build a
710 list in advance while blocking the GC. -mvo */
715 for (i
= 0; i
< scm_port_table_size
; i
++)
716 ports
= scm_cons (scm_port_table
[i
]->port
, ports
);
720 while (ports
!= SCM_EOL
)
722 scm_apply (proc
, scm_cons (SCM_CAR (ports
), SCM_EOL
), SCM_EOL
);
723 ports
= SCM_CDR (ports
);
726 return SCM_UNSPECIFIED
;
730 #if (SCM_DEBUG_DEPRECATED == 0)
732 SCM_DEFINE (scm_close_all_ports_except
, "close-all-ports-except", 0, 0, 1,
734 "[DEPRECATED] Close all open file ports used by the interpreter\n"
735 "except for those supplied as arguments. This procedure\n"
736 "was intended to be used before an exec call to close file descriptors\n"
737 "which are not needed in the new process. However it has the\n"
738 "undesirable side-effect of flushing buffes, so it's deprecated.\n"
739 "Use port-for-each instead.")
740 #define FUNC_NAME s_scm_close_all_ports_except
743 SCM_VALIDATE_REST_ARGUMENT (ports
);
744 while (i
< scm_port_table_size
)
746 SCM thisport
= scm_port_table
[i
]->port
;
748 SCM ports_ptr
= ports
;
750 while (SCM_NNULLP (ports_ptr
))
752 SCM port
= SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr
));
754 SCM_VALIDATE_OPPORT (SCM_ARG1
,port
);
755 if (SCM_EQ_P (port
, thisport
))
757 ports_ptr
= SCM_CDR (ports_ptr
);
762 /* i is not to be incremented here. */
763 scm_close_port (thisport
);
765 return SCM_UNSPECIFIED
;
772 /* Utter miscellany. Gosh, we should clean this up some time. */
774 SCM_DEFINE (scm_input_port_p
, "input-port?", 1, 0, 0,
776 "Returns @code{#t} if @var{x} is an input port, otherwise returns\n"
777 "@code{#f}. Any object satisfying this predicate also satisfies\n"
779 #define FUNC_NAME s_scm_input_port_p
783 return SCM_BOOL(SCM_INPUT_PORT_P (x
));
787 SCM_DEFINE (scm_output_port_p
, "output-port?", 1, 0, 0,
789 "Returns @code{#t} if @var{x} is an output port, otherwise returns\n"
790 "@code{#f}. Any object satisfying this predicate also satisfies\n"
792 #define FUNC_NAME s_scm_output_port_p
796 if (SCM_PORT_WITH_PS_P (x
))
797 x
= SCM_PORT_WITH_PS_PORT (x
);
798 return SCM_BOOL(SCM_OUTPUT_PORT_P (x
));
802 SCM_DEFINE (scm_port_p
, "port?", 1, 0, 0,
804 "Returns a boolean indicating whether @var{x} is a port.\n"
805 "Equivalent to @code{(or (input-port? X) (output-port? X))}.")
806 #define FUNC_NAME s_scm_port_p
808 return SCM_BOOL (SCM_PORTP (x
));
812 SCM_DEFINE (scm_port_closed_p
, "port-closed?", 1, 0, 0,
814 "Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.")
815 #define FUNC_NAME s_scm_port_closed_p
817 SCM_VALIDATE_PORT (1,port
);
818 return SCM_NEGATE_BOOL(SCM_OPPORTP (port
));
822 SCM_DEFINE (scm_eof_object_p
, "eof-object?", 1, 0, 0,
824 "Returns @code{#t} if @var{x} is an end-of-file object; otherwise\n"
825 "returns @code{#f}.")
826 #define FUNC_NAME s_scm_eof_object_p
828 return SCM_BOOL(SCM_EOF_OBJECT_P (x
));
832 SCM_DEFINE (scm_force_output
, "force-output", 0, 1, 0,
834 "Flush the specified output port, or the current output port if @var{port}\n"
835 "is omitted. The current output buffer contents are passed to the \n"
836 "underlying port implementation (e.g., in the case of fports, the\n"
837 "data will be written to the file and the output buffer will be cleared.)\n"
838 "It has no effect on an unbuffered port.\n\n"
839 "The return value is unspecified.")
840 #define FUNC_NAME s_scm_force_output
842 if (SCM_UNBNDP (port
))
846 port
= SCM_COERCE_OUTPORT (port
);
847 SCM_VALIDATE_OPOUTPORT (1,port
);
850 return SCM_UNSPECIFIED
;
854 SCM_DEFINE (scm_flush_all_ports
, "flush-all-ports", 0, 0, 0,
856 "Equivalent to calling @code{force-output} on\n"
857 "all open output ports. The return value is unspecified.")
858 #define FUNC_NAME s_scm_flush_all_ports
862 for (i
= 0; i
< scm_port_table_size
; i
++)
864 if (SCM_OPOUTPORTP (scm_port_table
[i
]->port
))
865 scm_flush (scm_port_table
[i
]->port
);
867 return SCM_UNSPECIFIED
;
871 SCM_DEFINE (scm_read_char
, "read-char", 0, 1, 0,
873 "Returns the next character available from @var{port}, updating\n"
874 "@var{port} to point to the following character. If no more\n"
875 "characters are available, an end-of-file object is returned.")
876 #define FUNC_NAME s_scm_read_char
879 if (SCM_UNBNDP (port
))
881 SCM_VALIDATE_OPINPORT (1,port
);
885 return SCM_MAKE_CHAR (c
);
889 /* this should only be called when the read buffer is empty. it
890 tries to refill the read buffer. it returns the first char from
891 the port, which is either EOF or *(pt->read_pos). */
893 scm_fill_input (SCM port
)
895 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
897 if (pt
->read_buf
== pt
->putback_buf
)
899 /* finished reading put-back chars. */
900 pt
->read_buf
= pt
->saved_read_buf
;
901 pt
->read_pos
= pt
->saved_read_pos
;
902 pt
->read_end
= pt
->saved_read_end
;
903 pt
->read_buf_size
= pt
->saved_read_buf_size
;
904 if (pt
->read_pos
< pt
->read_end
)
905 return *(pt
->read_pos
);
907 return scm_ptobs
[SCM_PTOBNUM (port
)].fill_input (port
);
914 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
916 if (pt
->rw_active
== SCM_PORT_WRITE
)
918 /* may be marginally faster than calling scm_flush. */
919 scm_ptobs
[SCM_PTOBNUM (port
)].flush (port
);
923 pt
->rw_active
= SCM_PORT_READ
;
925 if (pt
->read_pos
>= pt
->read_end
)
927 if (scm_fill_input (port
) == EOF
)
931 c
= *(pt
->read_pos
++);
950 scm_putc (char c
, SCM port
)
952 scm_lfwrite (&c
, 1, port
);
956 scm_puts (const char *s
, SCM port
)
958 scm_lfwrite (s
, strlen (s
), port
);
962 scm_lfwrite (const char *ptr
, scm_sizet size
, SCM port
)
964 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
965 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
967 if (pt
->rw_active
== SCM_PORT_READ
)
968 scm_end_input (port
);
970 ptob
->write (port
, ptr
, size
);
973 pt
->rw_active
= SCM_PORT_WRITE
;
980 scm_sizet i
= SCM_PTOBNUM (port
);
981 (scm_ptobs
[i
].flush
) (port
);
985 scm_end_input (SCM port
)
988 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
990 if (pt
->read_buf
== pt
->putback_buf
)
992 offset
= pt
->read_end
- pt
->read_pos
;
993 pt
->read_buf
= pt
->saved_read_buf
;
994 pt
->read_pos
= pt
->saved_read_pos
;
995 pt
->read_end
= pt
->saved_read_end
;
996 pt
->read_buf_size
= pt
->saved_read_buf_size
;
1001 scm_ptobs
[SCM_PTOBNUM (port
)].end_input (port
, offset
);
1008 scm_ungetc (int c
, SCM port
)
1010 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
1012 if (pt
->read_buf
== pt
->putback_buf
)
1013 /* already using the put-back buffer. */
1015 /* enlarge putback_buf if necessary. */
1016 if (pt
->read_end
== pt
->read_buf
+ pt
->read_buf_size
1017 && pt
->read_buf
== pt
->read_pos
)
1019 int new_size
= pt
->read_buf_size
* 2;
1020 unsigned char *tmp
=
1021 (unsigned char *) realloc (pt
->putback_buf
, new_size
);
1024 scm_memory_error ("scm_ungetc");
1025 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
= tmp
;
1026 pt
->read_end
= pt
->read_buf
+ pt
->read_buf_size
;
1027 pt
->read_buf_size
= pt
->putback_buf_size
= new_size
;
1030 /* shift any existing bytes to buffer + 1. */
1031 if (pt
->read_pos
== pt
->read_end
)
1032 pt
->read_end
= pt
->read_buf
+ 1;
1033 else if (pt
->read_pos
!= pt
->read_buf
+ 1)
1035 int count
= pt
->read_end
- pt
->read_pos
;
1037 memmove (pt
->read_buf
+ 1, pt
->read_pos
, count
);
1038 pt
->read_end
= pt
->read_buf
+ 1 + count
;
1041 pt
->read_pos
= pt
->read_buf
;
1044 /* switch to the put-back buffer. */
1046 if (pt
->putback_buf
== NULL
)
1049 = (unsigned char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE
);
1050 if (pt
->putback_buf
== NULL
)
1051 scm_memory_error ("scm_ungetc");
1052 pt
->putback_buf_size
= SCM_INITIAL_PUTBACK_BUF_SIZE
;
1055 pt
->saved_read_buf
= pt
->read_buf
;
1056 pt
->saved_read_pos
= pt
->read_pos
;
1057 pt
->saved_read_end
= pt
->read_end
;
1058 pt
->saved_read_buf_size
= pt
->read_buf_size
;
1060 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
;
1061 pt
->read_end
= pt
->read_buf
+ 1;
1062 pt
->read_buf_size
= pt
->putback_buf_size
;
1068 pt
->rw_active
= SCM_PORT_READ
;
1072 /* What should col be in this case?
1073 * We'll leave it at -1.
1075 SCM_LINUM (port
) -= 1;
1083 scm_ungets (const char *s
, int n
, SCM port
)
1085 /* This is simple minded and inefficient, but unreading strings is
1086 * probably not a common operation, and remember that line and
1087 * column numbers have to be handled...
1089 * Please feel free to write an optimized version!
1092 scm_ungetc (s
[n
], port
);
1096 SCM_DEFINE (scm_peek_char
, "peek-char", 0, 1, 0,
1098 "Returns the next character available from @var{port},\n"
1099 "@emph{without} updating @var{port} to point to the following\n"
1100 "character. If no more characters are available, an end-of-file object\n"
1101 "is returned.@footnote{The value returned by a call to @code{peek-char}\n"
1102 "is the same as the value that would have been returned by a call to\n"
1103 "@code{read-char} on the same port. The only difference is that the very\n"
1104 "next call to @code{read-char} or @code{peek-char} on that\n"
1105 "@var{port} will return the value returned by the preceding call to\n"
1106 "@code{peek-char}. In particular, a call to @code{peek-char} on an\n"
1107 "interactive port will hang waiting for input whenever a call to\n"
1108 "@code{read-char} would have hung.}")
1109 #define FUNC_NAME s_scm_peek_char
1112 if (SCM_UNBNDP (port
))
1115 SCM_VALIDATE_OPINPORT (1,port
);
1116 c
= scm_getc (port
);
1119 scm_ungetc (c
, port
);
1120 return SCM_MAKE_CHAR (c
);
1124 SCM_DEFINE (scm_unread_char
, "unread-char", 1, 1, 0,
1125 (SCM cobj
, SCM port
),
1126 "Place @var{char} in @var{port} so that it will be read by the\n"
1127 "next read operation. If called multiple times, the unread characters\n"
1128 "will be read again in last-in first-out order. If @var{port} is\n"
1129 "not supplied, the current input port is used.")
1130 #define FUNC_NAME s_scm_unread_char
1134 SCM_VALIDATE_CHAR (1,cobj
);
1135 if (SCM_UNBNDP (port
))
1138 SCM_VALIDATE_OPINPORT (2,port
);
1140 c
= SCM_CHAR (cobj
);
1142 scm_ungetc (c
, port
);
1147 SCM_DEFINE (scm_unread_string
, "unread-string", 2, 0, 0,
1148 (SCM str
, SCM port
),
1149 "Place the string @var{str} in @var{port} so that its characters will be\n"
1150 "read in subsequent read operations. If called multiple times, the\n"
1151 "unread characters will be read again in last-in first-out order. If\n"
1152 "@var{port} is not supplied, the current-input-port is used.")
1153 #define FUNC_NAME s_scm_unread_string
1155 SCM_VALIDATE_STRING (1,str
);
1156 if (SCM_UNBNDP (port
))
1159 SCM_VALIDATE_OPINPORT (2,port
);
1161 scm_ungets (SCM_STRING_CHARS (str
), SCM_STRING_LENGTH (str
), port
);
1167 SCM_DEFINE (scm_seek
, "seek", 3, 0, 0,
1168 (SCM object
, SCM offset
, SCM whence
),
1169 "Sets the current position of @var{fd/port} to the integer @var{offset},\n"
1170 "which is interpreted according to the value of @var{whence}.\n\n"
1171 "One of the following variables should be supplied\n"
1172 "for @var{whence}:\n"
1173 "@defvar SEEK_SET\n"
1174 "Seek from the beginning of the file.\n"
1176 "@defvar SEEK_CUR\n"
1177 "Seek from the current position.\n"
1179 "@defvar SEEK_END\n"
1180 "Seek from the end of the file.\n"
1182 "If @var{fd/port} is a file descriptor, the underlying system call is\n"
1183 "@code{lseek}. @var{port} may be a string port.\n\n"
1184 "The value returned is the new position in the file. This means that\n"
1185 "the current position of a port can be obtained using:\n"
1187 "(seek port 0 SEEK_CUR)\n"
1189 #define FUNC_NAME s_scm_seek
1195 object
= SCM_COERCE_OUTPORT (object
);
1197 off
= SCM_NUM2LONG (2,offset
);
1198 SCM_VALIDATE_INUM_COPY (3,whence
,how
);
1199 if (how
!= SEEK_SET
&& how
!= SEEK_CUR
&& how
!= SEEK_END
)
1200 SCM_OUT_OF_RANGE (3, whence
);
1201 if (SCM_OPPORTP (object
))
1203 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1206 SCM_MISC_ERROR ("port is not seekable",
1207 scm_cons (object
, SCM_EOL
));
1209 rv
= ptob
->seek (object
, off
, how
);
1211 else /* file descriptor?. */
1213 SCM_VALIDATE_INUM (1,object
);
1214 rv
= lseek (SCM_INUM (object
), off
, how
);
1218 return scm_long2num (rv
);
1222 SCM_DEFINE (scm_truncate_file
, "truncate-file", 1, 1, 0,
1223 (SCM object
, SCM length
),
1224 "Truncates the object referred to by @var{obj} to at most @var{size} bytes.\n"
1225 "@var{obj} can be a string containing a file name or an integer file\n"
1226 "descriptor or a port. @var{size} may be omitted if @var{obj} is not\n"
1227 "a file name, in which case the truncation occurs at the current port.\n"
1229 "The return value is unspecified.")
1230 #define FUNC_NAME s_scm_truncate_file
1235 /* object can be a port, fdes or filename. */
1237 if (SCM_UNBNDP (length
))
1239 /* must supply length if object is a filename. */
1240 if (SCM_STRINGP (object
))
1241 SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL
);
1243 length
= scm_seek (object
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
1245 c_length
= SCM_NUM2LONG (2,length
);
1247 SCM_MISC_ERROR ("negative offset", SCM_EOL
);
1249 object
= SCM_COERCE_OUTPORT (object
);
1250 if (SCM_INUMP (object
))
1252 SCM_SYSCALL (rv
= ftruncate (SCM_INUM (object
), c_length
));
1254 else if (SCM_OPOUTPORTP (object
))
1256 scm_port
*pt
= SCM_PTAB_ENTRY (object
);
1257 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1259 if (!ptob
->truncate
)
1260 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL
);
1261 if (pt
->rw_active
== SCM_PORT_READ
)
1262 scm_end_input (object
);
1263 else if (pt
->rw_active
== SCM_PORT_WRITE
)
1264 ptob
->flush (object
);
1266 ptob
->truncate (object
, c_length
);
1271 SCM_VALIDATE_STRING (1, object
);
1272 SCM_STRING_COERCE_0TERMINATION_X (object
);
1273 SCM_SYSCALL (rv
= truncate (SCM_STRING_CHARS (object
), c_length
));
1277 return SCM_UNSPECIFIED
;
1281 SCM_DEFINE (scm_port_line
, "port-line", 1, 0, 0,
1283 "Return the current line number for PORT.")
1284 #define FUNC_NAME s_scm_port_line
1286 port
= SCM_COERCE_OUTPORT (port
);
1287 SCM_VALIDATE_OPENPORT (1,port
);
1288 return SCM_MAKINUM (SCM_LINUM (port
));
1292 SCM_DEFINE (scm_set_port_line_x
, "set-port-line!", 2, 0, 0,
1293 (SCM port
, SCM line
),
1294 "Set the current line number for PORT to LINE.")
1295 #define FUNC_NAME s_scm_set_port_line_x
1297 port
= SCM_COERCE_OUTPORT (port
);
1298 SCM_VALIDATE_OPENPORT (1,port
);
1299 SCM_VALIDATE_INUM (2,line
);
1300 SCM_PTAB_ENTRY (port
)->line_number
= SCM_INUM (line
);
1301 return SCM_UNSPECIFIED
;
1305 SCM_DEFINE (scm_port_column
, "port-column", 1, 0, 0,
1307 "@deffnx primitive port-line [input-port]\n"
1308 "Return the current column number or line number of @var{input-port},\n"
1309 "using the current input port if none is specified. If the number is\n"
1310 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1311 "- i.e. the first character of the first line is line 0, column 0.\n"
1312 "(However, when you display a file position, for example in an error\n"
1313 "message, we recommand you add 1 to get 1-origin integers. This is\n"
1314 "because lines and column numbers traditionally start with 1, and that is\n"
1315 "what non-programmers will find most natural.)")
1316 #define FUNC_NAME s_scm_port_column
1318 port
= SCM_COERCE_OUTPORT (port
);
1319 SCM_VALIDATE_OPENPORT (1,port
);
1320 return SCM_MAKINUM (SCM_COL (port
));
1324 SCM_DEFINE (scm_set_port_column_x
, "set-port-column!", 2, 0, 0,
1325 (SCM port
, SCM column
),
1326 "@deffnx primitive set-port-line! port line\n"
1327 "Set the current column or line number of @var{port}, using the\n"
1328 "current input port if none is specified.")
1329 #define FUNC_NAME s_scm_set_port_column_x
1331 port
= SCM_COERCE_OUTPORT (port
);
1332 SCM_VALIDATE_OPENPORT (1,port
);
1333 SCM_VALIDATE_INUM (2,column
);
1334 SCM_PTAB_ENTRY (port
)->column_number
= SCM_INUM (column
);
1335 return SCM_UNSPECIFIED
;
1339 SCM_DEFINE (scm_port_filename
, "port-filename", 1, 0, 0,
1341 "Return the filename associated with @var{port}. This function returns\n"
1342 "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
1343 "when called on the current input, output and error ports respectively.")
1344 #define FUNC_NAME s_scm_port_filename
1346 port
= SCM_COERCE_OUTPORT (port
);
1347 SCM_VALIDATE_OPENPORT (1,port
);
1348 return SCM_FILENAME (port
);
1352 SCM_DEFINE (scm_set_port_filename_x
, "set-port-filename!", 2, 0, 0,
1353 (SCM port
, SCM filename
),
1354 "Change the filename associated with @var{port}, using the current input\n"
1355 "port if none is specified. Note that this does not change the port's\n"
1356 "source of data, but only the value that is returned by\n"
1357 "@code{port-filename} and reported in diagnostic output.")
1358 #define FUNC_NAME s_scm_set_port_filename_x
1360 port
= SCM_COERCE_OUTPORT (port
);
1361 SCM_VALIDATE_OPENPORT (1,port
);
1362 /* We allow the user to set the filename to whatever he likes. */
1363 SCM_SET_FILENAME (port
, filename
);
1364 return SCM_UNSPECIFIED
;
1369 extern char * ttyname();
1373 scm_print_port_mode (SCM exp
, SCM port
)
1375 scm_puts (SCM_CLOSEDP (exp
)
1377 : (SCM_RDNG
& SCM_CELL_WORD_0 (exp
)
1378 ? (SCM_WRTNG
& SCM_CELL_WORD_0 (exp
)
1381 : (SCM_WRTNG
& SCM_CELL_WORD_0 (exp
)
1388 scm_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
1390 char *type
= SCM_PTOBNAME (SCM_PTOBNUM (exp
));
1393 scm_puts ("#<", port
);
1394 scm_print_port_mode (exp
, port
);
1395 scm_puts (type
, port
);
1396 scm_putc (' ', port
);
1397 scm_intprint (SCM_CELL_WORD_1 (exp
), 16, port
);
1398 scm_putc ('>', port
);
1403 scm_ports_prehistory ()
1406 scm_ptobs
= (scm_ptob_descriptor
*) malloc (sizeof (scm_ptob_descriptor
));
1413 scm_bits_t scm_tc16_void_port
= 0;
1415 static int fill_input_void_port (SCM port
)
1421 write_void_port (SCM port
, const void *data
, size_t size
)
1426 scm_void_port (char *mode_str
)
1432 SCM_NEWCELL (answer
);
1434 mode_bits
= scm_mode_bits (mode_str
);
1435 pt
= scm_add_to_port_table (answer
);
1436 scm_port_non_buffer (pt
);
1437 SCM_SETPTAB_ENTRY (answer
, pt
);
1438 SCM_SETSTREAM (answer
, 0);
1439 SCM_SET_CELL_TYPE (answer
, scm_tc16_void_port
| mode_bits
);
1444 SCM_DEFINE (scm_sys_make_void_port
, "%make-void-port", 1, 0, 0,
1446 "Create and return a new void port. A void port acts like\n"
1447 "/dev/null. The @var{mode} argument\n"
1448 "specifies the input/output modes for this port: see the\n"
1449 "documentation for @code{open-file} in @ref{File Ports}.")
1450 #define FUNC_NAME s_scm_sys_make_void_port
1452 SCM_VALIDATE_STRING (1, mode
);
1453 SCM_STRING_COERCE_0TERMINATION_X (mode
);
1454 return scm_void_port (SCM_STRING_CHARS (mode
));
1459 /* Initialization. */
1464 /* lseek() symbols. */
1465 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET
));
1466 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR
));
1467 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END
));
1469 scm_tc16_void_port
= scm_make_port_type ("void", fill_input_void_port
,
1471 #ifndef SCM_MAGIC_SNARFER
1472 #include "libguile/ports.x"