1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004,
2 * 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
32 #include <fcntl.h> /* for chsize on mingw */
37 #include <striconveh.h>
41 #include "libguile/_scm.h"
42 #include "libguile/async.h"
43 #include "libguile/deprecation.h"
44 #include "libguile/eval.h"
45 #include "libguile/fports.h" /* direct access for seek and truncate */
46 #include "libguile/goops.h"
47 #include "libguile/smob.h"
48 #include "libguile/chars.h"
49 #include "libguile/dynwind.h"
51 #include "libguile/keywords.h"
52 #include "libguile/hashtab.h"
53 #include "libguile/root.h"
54 #include "libguile/strings.h"
55 #include "libguile/mallocs.h"
56 #include "libguile/validate.h"
57 #include "libguile/ports.h"
58 #include "libguile/ports-internal.h"
59 #include "libguile/vectors.h"
60 #include "libguile/weak-set.h"
61 #include "libguile/fluids.h"
62 #include "libguile/eq.h"
63 #include "libguile/alist.h"
77 #ifdef HAVE_SYS_IOCTL_H
78 #include <sys/ioctl.h>
81 /* Mingw (version 3.4.5, circa 2006) has ftruncate as an alias for chsize
82 already, but have this code here in case that wasn't so in past versions,
83 or perhaps to help other minimal DOS environments.
85 gnulib ftruncate.c has code using fcntl F_CHSIZE and F_FREESP, which
86 might be possibilities if we've got other systems without ftruncate. */
88 #if defined HAVE_CHSIZE && ! defined HAVE_FTRUNCATE
89 #define ftruncate(fd, size) chsize (fd, size)
91 #define HAVE_FTRUNCATE 1
95 /* Port encodings are case-insensitive ASCII strings. */
97 ascii_toupper (char c
)
99 return (c
< 'a' || c
> 'z') ? c
: ('A' + (c
- 'a'));
102 /* It is only necessary to use this function on encodings that come from
103 the user and have not been canonicalized yet. Encodings that are set
104 on ports or in the default encoding fluid are in upper-case, and can
105 be compared with strcmp. */
107 encoding_matches (const char *enc
, const char *upper
)
113 if (ascii_toupper (*enc
++) != *upper
++)
120 canonicalize_encoding (const char *enc
)
128 ret
= scm_gc_strdup (enc
, "port");
130 for (i
= 0; ret
[i
]; i
++)
133 /* Restrict to ASCII. */
134 scm_misc_error (NULL
, "invalid character encoding ~s",
135 scm_list_1 (scm_from_latin1_string (enc
)));
137 ret
[i
] = ascii_toupper (ret
[i
]);
145 /* The port kind table --- a dynamically resized array of port types. */
148 /* scm_ptobs scm_numptob
149 * implement a dynamically resized array of ptob records.
150 * Indexes into this table are used when generating type
151 * tags for smobjects (if you know a tag you can get an index and conversely).
153 static scm_t_ptob_descriptor
**scm_ptobs
= NULL
;
154 static long scm_numptob
= 0; /* Number of port types. */
155 static long scm_ptobs_size
= 0; /* Number of slots in the port type
157 static scm_i_pthread_mutex_t scm_ptobs_lock
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
160 scm_c_num_port_types (void)
164 scm_i_pthread_mutex_lock (&scm_ptobs_lock
);
166 scm_i_pthread_mutex_unlock (&scm_ptobs_lock
);
171 scm_t_ptob_descriptor
*
172 scm_c_port_type_ref (long ptobnum
)
174 scm_t_ptob_descriptor
*ret
= NULL
;
176 scm_i_pthread_mutex_lock (&scm_ptobs_lock
);
178 if (0 <= ptobnum
&& ptobnum
< scm_numptob
)
179 ret
= scm_ptobs
[ptobnum
];
181 scm_i_pthread_mutex_unlock (&scm_ptobs_lock
);
184 scm_out_of_range ("scm_c_port_type_ref", scm_from_long (ptobnum
));
190 scm_c_port_type_add_x (scm_t_ptob_descriptor
*desc
)
194 scm_i_pthread_mutex_lock (&scm_ptobs_lock
);
196 if (scm_numptob
+ 1 < SCM_I_MAX_PORT_TYPE_COUNT
)
198 if (scm_numptob
== scm_ptobs_size
)
200 unsigned long old_size
= scm_ptobs_size
;
201 scm_t_ptob_descriptor
**old_ptobs
= scm_ptobs
;
203 /* Currently there are only 9 predefined port types, so one
204 resize will cover it. */
205 scm_ptobs_size
= old_size
+ 10;
207 if (scm_ptobs_size
>= SCM_I_MAX_PORT_TYPE_COUNT
)
208 scm_ptobs_size
= SCM_I_MAX_PORT_TYPE_COUNT
;
210 scm_ptobs
= scm_gc_malloc (sizeof (*scm_ptobs
) * scm_ptobs_size
,
213 memcpy (scm_ptobs
, old_ptobs
, sizeof (*scm_ptobs
) * scm_numptob
);
217 scm_ptobs
[ret
] = desc
;
220 scm_i_pthread_mutex_unlock (&scm_ptobs_lock
);
223 scm_out_of_range ("scm_c_port_type_add_x", scm_from_long (scm_numptob
));
229 * We choose to use an interface similar to the smob interface with
230 * fill_input and write as standard fields, passed to the port
231 * type constructor, and optional fields set by setters.
235 flush_port_default (SCM port SCM_UNUSED
)
240 end_input_default (SCM port SCM_UNUSED
, int offset SCM_UNUSED
)
245 scm_make_port_type (char *name
,
246 int (*fill_input
) (SCM port
),
247 void (*write
) (SCM port
, const void *data
, size_t size
))
249 scm_t_ptob_descriptor
*desc
;
252 desc
= scm_gc_malloc_pointerless (sizeof (*desc
), "port-type");
253 memset (desc
, 0, sizeof (*desc
));
256 desc
->print
= scm_port_print
;
258 desc
->flush
= flush_port_default
;
259 desc
->end_input
= end_input_default
;
260 desc
->fill_input
= fill_input
;
262 ptobnum
= scm_c_port_type_add_x (desc
);
264 /* Make a class object if GOOPS is present. */
265 if (SCM_UNPACK (scm_port_class
[0]) != 0)
266 scm_make_port_classes (ptobnum
, name
);
268 return scm_tc7_port
+ ptobnum
* 256;
272 scm_set_port_mark (scm_t_bits tc
, SCM (*mark
) (SCM
))
274 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->mark
= mark
;
278 scm_set_port_free (scm_t_bits tc
, size_t (*free
) (SCM
))
280 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->free
= free
;
284 scm_set_port_print (scm_t_bits tc
, int (*print
) (SCM exp
, SCM port
,
285 scm_print_state
*pstate
))
287 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->print
= print
;
291 scm_set_port_equalp (scm_t_bits tc
, SCM (*equalp
) (SCM
, SCM
))
293 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->equalp
= equalp
;
297 scm_set_port_close (scm_t_bits tc
, int (*close
) (SCM
))
299 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->close
= close
;
303 scm_set_port_flush (scm_t_bits tc
, void (*flush
) (SCM port
))
305 scm_t_ptob_descriptor
*ptob
= scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
));
307 ptob
->flags
|= SCM_PORT_TYPE_HAS_FLUSH
;
311 scm_set_port_end_input (scm_t_bits tc
, void (*end_input
) (SCM port
, int offset
))
313 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->end_input
= end_input
;
317 scm_set_port_seek (scm_t_bits tc
, scm_t_off (*seek
) (SCM
, scm_t_off
, int))
319 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->seek
= seek
;
323 scm_set_port_truncate (scm_t_bits tc
, void (*truncate
) (SCM
, scm_t_off
))
325 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->truncate
= truncate
;
329 scm_set_port_input_waiting (scm_t_bits tc
, int (*input_waiting
) (SCM
))
331 scm_c_port_type_ref (SCM_TC2PTOBNUM (tc
))->input_waiting
= input_waiting
;
335 scm_i_set_pending_eof (SCM port
)
337 SCM_PORT_GET_INTERNAL (port
)->pending_eof
= 1;
341 scm_i_clear_pending_eof (SCM port
)
343 SCM_PORT_GET_INTERNAL (port
)->pending_eof
= 0;
346 SCM_DEFINE (scm_i_port_property
, "%port-property", 2, 0, 0,
348 "Return the property of @var{port} associated with @var{key}.")
349 #define FUNC_NAME s_scm_i_port_property
351 SCM_VALIDATE_OPPORT (1, port
);
352 return scm_assq_ref (SCM_PORT_GET_INTERNAL (port
)->alist
, key
);
356 SCM_DEFINE (scm_i_set_port_property_x
, "%set-port-property!", 3, 0, 0,
357 (SCM port
, SCM key
, SCM value
),
358 "Set the property of @var{port} associated with @var{key} to @var{value}.")
359 #define FUNC_NAME s_scm_i_set_port_property_x
361 scm_t_port_internal
*pti
;
363 SCM_VALIDATE_OPPORT (1, port
);
364 pti
= SCM_PORT_GET_INTERNAL (port
);
365 pti
->alist
= scm_assq_set_x (pti
->alist
, key
, value
);
366 return SCM_UNSPECIFIED
;
372 /* Standard ports --- current input, output, error, and more(!). */
374 static SCM cur_inport_fluid
= SCM_BOOL_F
;
375 static SCM cur_outport_fluid
= SCM_BOOL_F
;
376 static SCM cur_errport_fluid
= SCM_BOOL_F
;
377 static SCM cur_loadport_fluid
= SCM_BOOL_F
;
379 SCM_DEFINE (scm_current_input_port
, "current-input-port", 0, 0, 0,
381 "Return the current input port. This is the default port used\n"
382 "by many input procedures. Initially, @code{current-input-port}\n"
383 "returns the @dfn{standard input} in Unix and C terminology.")
384 #define FUNC_NAME s_scm_current_input_port
386 if (scm_is_true (cur_inport_fluid
))
387 return scm_fluid_ref (cur_inport_fluid
);
393 SCM_DEFINE (scm_current_output_port
, "current-output-port", 0, 0, 0,
395 "Return the current output port. This is the default port used\n"
396 "by many output procedures. Initially,\n"
397 "@code{current-output-port} returns the @dfn{standard output} in\n"
398 "Unix and C terminology.")
399 #define FUNC_NAME s_scm_current_output_port
401 if (scm_is_true (cur_outport_fluid
))
402 return scm_fluid_ref (cur_outport_fluid
);
408 SCM_DEFINE (scm_current_error_port
, "current-error-port", 0, 0, 0,
410 "Return the port to which errors and warnings should be sent (the\n"
411 "@dfn{standard error} in Unix and C terminology).")
412 #define FUNC_NAME s_scm_current_error_port
414 if (scm_is_true (cur_errport_fluid
))
415 return scm_fluid_ref (cur_errport_fluid
);
422 scm_current_warning_port (void)
424 static SCM cwp_var
= SCM_UNDEFINED
;
425 static scm_i_pthread_mutex_t cwp_var_mutex
426 = SCM_I_PTHREAD_MUTEX_INITIALIZER
;
428 scm_i_scm_pthread_mutex_lock (&cwp_var_mutex
);
429 if (SCM_UNBNDP (cwp_var
))
430 cwp_var
= scm_c_private_variable ("guile", "current-warning-port");
431 scm_i_pthread_mutex_unlock (&cwp_var_mutex
);
433 return scm_call_0 (scm_variable_ref (cwp_var
));
436 SCM_DEFINE (scm_current_load_port
, "current-load-port", 0, 0, 0,
438 "Return the current-load-port.\n"
439 "The load port is used internally by @code{primitive-load}.")
440 #define FUNC_NAME s_scm_current_load_port
442 return scm_fluid_ref (cur_loadport_fluid
);
446 SCM_DEFINE (scm_set_current_input_port
, "set-current-input-port", 1, 0, 0,
448 "@deffnx {Scheme Procedure} set-current-output-port port\n"
449 "@deffnx {Scheme Procedure} set-current-error-port port\n"
450 "Change the ports returned by @code{current-input-port},\n"
451 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
452 "so that they use the supplied @var{port} for input or output.")
453 #define FUNC_NAME s_scm_set_current_input_port
455 SCM oinp
= scm_fluid_ref (cur_inport_fluid
);
456 SCM_VALIDATE_OPINPORT (1, port
);
457 scm_fluid_set_x (cur_inport_fluid
, port
);
463 SCM_DEFINE (scm_set_current_output_port
, "set-current-output-port", 1, 0, 0,
465 "Set the current default output port to @var{port}.")
466 #define FUNC_NAME s_scm_set_current_output_port
468 SCM ooutp
= scm_fluid_ref (cur_outport_fluid
);
469 port
= SCM_COERCE_OUTPORT (port
);
470 SCM_VALIDATE_OPOUTPORT (1, port
);
471 scm_fluid_set_x (cur_outport_fluid
, port
);
477 SCM_DEFINE (scm_set_current_error_port
, "set-current-error-port", 1, 0, 0,
479 "Set the current default error port to @var{port}.")
480 #define FUNC_NAME s_scm_set_current_error_port
482 SCM oerrp
= scm_fluid_ref (cur_errport_fluid
);
483 port
= SCM_COERCE_OUTPORT (port
);
484 SCM_VALIDATE_OPOUTPORT (1, port
);
485 scm_fluid_set_x (cur_errport_fluid
, port
);
492 scm_set_current_warning_port (SCM port
)
494 static SCM cwp_var
= SCM_BOOL_F
;
496 if (scm_is_false (cwp_var
))
497 cwp_var
= scm_c_private_lookup ("guile", "current-warning-port");
499 return scm_call_1 (scm_variable_ref (cwp_var
), port
);
504 scm_dynwind_current_input_port (SCM port
)
505 #define FUNC_NAME NULL
507 SCM_VALIDATE_OPINPORT (1, port
);
508 scm_dynwind_fluid (cur_inport_fluid
, port
);
513 scm_dynwind_current_output_port (SCM port
)
514 #define FUNC_NAME NULL
516 port
= SCM_COERCE_OUTPORT (port
);
517 SCM_VALIDATE_OPOUTPORT (1, port
);
518 scm_dynwind_fluid (cur_outport_fluid
, port
);
523 scm_dynwind_current_error_port (SCM port
)
524 #define FUNC_NAME NULL
526 port
= SCM_COERCE_OUTPORT (port
);
527 SCM_VALIDATE_OPOUTPORT (1, port
);
528 scm_dynwind_fluid (cur_errport_fluid
, port
);
533 scm_i_dynwind_current_load_port (SCM port
)
535 scm_dynwind_fluid (cur_loadport_fluid
, port
);
541 /* Retrieving a port's mode. */
543 /* Return the flags that characterize a port based on the mode
544 * string used to open a file for that port.
546 * See PORT FLAGS in scm.h
550 scm_i_mode_bits_n (SCM modes
)
553 | (scm_i_string_contains_char (modes
, 'r')
554 || scm_i_string_contains_char (modes
, '+') ? SCM_RDNG
: 0)
555 | (scm_i_string_contains_char (modes
, 'w')
556 || scm_i_string_contains_char (modes
, 'a')
557 || scm_i_string_contains_char (modes
, '+') ? SCM_WRTNG
: 0)
558 | (scm_i_string_contains_char (modes
, '0') ? SCM_BUF0
: 0)
559 | (scm_i_string_contains_char (modes
, 'l') ? SCM_BUFLINE
: 0));
563 scm_mode_bits (char *modes
)
565 /* Valid characters are rw+a0l. So, use latin1. */
566 return scm_i_mode_bits (scm_from_latin1_string (modes
));
570 scm_i_mode_bits (SCM modes
)
574 if (!scm_is_string (modes
))
575 scm_wrong_type_arg_msg (NULL
, 0, modes
, "string");
577 bits
= scm_i_mode_bits_n (modes
);
578 scm_remember_upto_here_1 (modes
);
582 /* Return the mode flags from an open port.
583 * Some modes such as "append" are only used when opening
584 * a file and are not returned here. */
586 SCM_DEFINE (scm_port_mode
, "port-mode", 1, 0, 0,
588 "Return the port modes associated with the open port @var{port}.\n"
589 "These will not necessarily be identical to the modes used when\n"
590 "the port was opened, since modes such as \"append\" which are\n"
591 "used only during port creation are not retained.")
592 #define FUNC_NAME s_scm_port_mode
597 port
= SCM_COERCE_OUTPORT (port
);
598 SCM_VALIDATE_OPPORT (1, port
);
599 if (SCM_CELL_WORD_0 (port
) & SCM_RDNG
) {
600 if (SCM_CELL_WORD_0 (port
) & SCM_WRTNG
)
601 strcpy (modes
, "r+");
605 else if (SCM_CELL_WORD_0 (port
) & SCM_WRTNG
)
607 if (SCM_CELL_WORD_0 (port
) & SCM_BUF0
)
610 return scm_from_latin1_string (modes
);
617 /* The port table --- a weak set of all ports.
619 We need a global registry of ports to flush them all at exit, and to
620 get all the ports matching a file descriptor. */
621 SCM scm_i_port_weak_set
;
626 /* Port finalization. */
630 scm_t_ptob_descriptor
*ptob
;
635 do_free (void *body_data
)
637 struct do_free_data
*data
= body_data
;
639 /* `close' is for explicit `close-port' by user. `free' is for this
640 purpose: ports collected by the GC. */
641 data
->ptob
->free (data
->port
);
646 /* Finalize the object (a port) pointed to by PTR. */
648 finalize_port (void *ptr
, void *data
)
650 SCM port
= SCM_PACK_POINTER (ptr
);
652 if (!SCM_PORTP (port
))
655 if (SCM_OPENP (port
))
657 struct do_free_data data
;
659 SCM_CLR_PORT_OPEN_FLAG (port
);
661 data
.ptob
= SCM_PORT_DESCRIPTOR (port
);
664 scm_internal_catch (SCM_BOOL_T
, do_free
, &data
,
665 scm_handle_by_message_noexit
, NULL
);
667 scm_gc_ports_collected
++;
675 scm_c_make_port_with_encoding (scm_t_bits tag
, unsigned long mode_bits
,
676 const char *encoding
,
677 scm_t_string_failed_conversion_handler handler
,
682 scm_t_port_internal
*pti
;
683 scm_t_ptob_descriptor
*ptob
;
685 entry
= scm_gc_typed_calloc (scm_t_port
);
686 pti
= scm_gc_typed_calloc (scm_t_port_internal
);
687 ptob
= scm_c_port_type_ref (SCM_TC2PTOBNUM (tag
));
689 ret
= scm_words (tag
| mode_bits
, 3);
690 SCM_SET_CELL_WORD_1 (ret
, (scm_t_bits
) entry
);
691 SCM_SET_CELL_WORD_2 (ret
, (scm_t_bits
) ptob
);
693 entry
->lock
= scm_gc_malloc_pointerless (sizeof (*entry
->lock
), "port lock");
694 scm_i_pthread_mutex_init (entry
->lock
, scm_i_pthread_mutexattr_recursive
);
696 entry
->internal
= pti
;
697 entry
->file_name
= SCM_BOOL_F
;
698 entry
->rw_active
= SCM_PORT_NEITHER
;
700 entry
->stream
= stream
;
702 if (encoding_matches (encoding
, "UTF-8"))
704 pti
->encoding_mode
= SCM_PORT_ENCODING_MODE_UTF8
;
705 entry
->encoding
= "UTF-8";
707 else if (encoding_matches (encoding
, "ISO-8859-1"))
709 pti
->encoding_mode
= SCM_PORT_ENCODING_MODE_LATIN1
;
710 entry
->encoding
= "ISO-8859-1";
714 pti
->encoding_mode
= SCM_PORT_ENCODING_MODE_ICONV
;
715 entry
->encoding
= canonicalize_encoding (encoding
);
718 entry
->ilseq_handler
= handler
;
719 pti
->iconv_descriptors
= NULL
;
721 pti
->at_stream_start_for_bom_read
= 1;
722 pti
->at_stream_start_for_bom_write
= 1;
724 pti
->pending_eof
= 0;
725 pti
->alist
= SCM_EOL
;
727 if (SCM_PORT_DESCRIPTOR (ret
)->free
)
728 scm_i_set_finalizer (SCM2PTR (ret
), finalize_port
, NULL
);
730 if (SCM_PORT_DESCRIPTOR (ret
)->flags
& SCM_PORT_TYPE_HAS_FLUSH
)
731 scm_weak_set_add_x (scm_i_port_weak_set
, ret
);
737 scm_c_make_port (scm_t_bits tag
, unsigned long mode_bits
, scm_t_bits stream
)
739 return scm_c_make_port_with_encoding (tag
, mode_bits
,
740 scm_i_default_port_encoding (),
741 scm_i_default_port_conversion_handler (),
746 scm_new_port_table_entry (scm_t_bits tag
)
748 return scm_c_make_port (tag
, 0, 0);
755 SCM_DEFINE (scm_port_p
, "port?", 1, 0, 0,
757 "Return a boolean indicating whether @var{x} is a port.\n"
758 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
760 #define FUNC_NAME s_scm_port_p
762 return scm_from_bool (SCM_PORTP (x
));
766 SCM_DEFINE (scm_input_port_p
, "input-port?", 1, 0, 0,
768 "Return @code{#t} if @var{x} is an input port, otherwise return\n"
769 "@code{#f}. Any object satisfying this predicate also satisfies\n"
771 #define FUNC_NAME s_scm_input_port_p
773 return scm_from_bool (SCM_INPUT_PORT_P (x
));
777 SCM_DEFINE (scm_output_port_p
, "output-port?", 1, 0, 0,
779 "Return @code{#t} if @var{x} is an output port, otherwise return\n"
780 "@code{#f}. Any object satisfying this predicate also satisfies\n"
782 #define FUNC_NAME s_scm_output_port_p
784 x
= SCM_COERCE_OUTPORT (x
);
785 return scm_from_bool (SCM_OUTPUT_PORT_P (x
));
789 SCM_DEFINE (scm_port_closed_p
, "port-closed?", 1, 0, 0,
791 "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
793 #define FUNC_NAME s_scm_port_closed_p
795 SCM_VALIDATE_PORT (1, port
);
796 return scm_from_bool (!SCM_OPPORTP (port
));
800 SCM_DEFINE (scm_eof_object_p
, "eof-object?", 1, 0, 0,
802 "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
804 #define FUNC_NAME s_scm_eof_object_p
806 return scm_from_bool (SCM_EOF_OBJECT_P (x
));
815 static void close_iconv_descriptors (scm_t_iconv_descriptors
*id
);
818 * Call the close operation on a port object.
819 * see also scm_close.
821 SCM_DEFINE (scm_close_port
, "close-port", 1, 0, 0,
823 "Close the specified port object. Return @code{#t} if it\n"
824 "successfully closes a port or @code{#f} if it was already\n"
825 "closed. An exception may be raised if an error occurs, for\n"
826 "example when flushing buffered output. See also @ref{Ports and\n"
827 "File Descriptors, close}, for a procedure which can close file\n"
829 #define FUNC_NAME s_scm_close_port
831 scm_t_port_internal
*pti
;
834 port
= SCM_COERCE_OUTPORT (port
);
836 SCM_VALIDATE_PORT (1, port
);
837 if (SCM_CLOSEDP (port
))
840 pti
= SCM_PORT_GET_INTERNAL (port
);
841 SCM_CLR_PORT_OPEN_FLAG (port
);
843 if (SCM_PORT_DESCRIPTOR (port
)->flags
& SCM_PORT_TYPE_HAS_FLUSH
)
844 scm_weak_set_remove_x (scm_i_port_weak_set
, port
);
846 if (SCM_PORT_DESCRIPTOR (port
)->close
)
847 /* Note! This may throw an exception. Anything after this point
848 should be resilient to non-local exits. */
849 rv
= SCM_PORT_DESCRIPTOR (port
)->close (port
);
853 if (pti
->iconv_descriptors
)
855 /* If we don't get here, the iconv_descriptors finalizer will
857 close_iconv_descriptors (pti
->iconv_descriptors
);
858 pti
->iconv_descriptors
= NULL
;
861 return scm_from_bool (rv
>= 0);
865 SCM_DEFINE (scm_close_input_port
, "close-input-port", 1, 0, 0,
867 "Close the specified input port object. The routine has no effect if\n"
868 "the file has already been closed. An exception may be raised if an\n"
869 "error occurs. The value returned is unspecified.\n\n"
870 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
871 "which can close file descriptors.")
872 #define FUNC_NAME s_scm_close_input_port
874 SCM_VALIDATE_INPUT_PORT (1, port
);
875 scm_close_port (port
);
876 return SCM_UNSPECIFIED
;
880 SCM_DEFINE (scm_close_output_port
, "close-output-port", 1, 0, 0,
882 "Close the specified output port object. The routine has no effect if\n"
883 "the file has already been closed. An exception may be raised if an\n"
884 "error occurs. The value returned is unspecified.\n\n"
885 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
886 "which can close file descriptors.")
887 #define FUNC_NAME s_scm_close_output_port
889 port
= SCM_COERCE_OUTPORT (port
);
890 SCM_VALIDATE_OUTPUT_PORT (1, port
);
891 scm_close_port (port
);
892 return SCM_UNSPECIFIED
;
899 /* Encoding characters to byte streams, and decoding byte streams to
902 /* A fluid specifying the default encoding for newly created ports. If it is
903 a string, that is the encoding. If it is #f, it is in the "native"
904 (Latin-1) encoding. */
905 SCM_VARIABLE (default_port_encoding_var
, "%default-port-encoding");
907 static int scm_port_encoding_init
= 0;
909 /* Use ENCODING as the default encoding for future ports. */
911 scm_i_set_default_port_encoding (const char *encoding
)
913 if (!scm_port_encoding_init
914 || !scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var
)))
915 scm_misc_error (NULL
, "tried to set port encoding fluid before it is initialized",
918 if (encoding_matches (encoding
, "ISO-8859-1"))
919 scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var
), SCM_BOOL_F
);
921 scm_fluid_set_x (SCM_VARIABLE_REF (default_port_encoding_var
),
922 scm_from_latin1_string (canonicalize_encoding (encoding
)));
925 /* Return the name of the default encoding for newly created ports. */
927 scm_i_default_port_encoding (void)
929 if (!scm_port_encoding_init
)
931 else if (!scm_is_fluid (SCM_VARIABLE_REF (default_port_encoding_var
)))
937 encoding
= scm_fluid_ref (SCM_VARIABLE_REF (default_port_encoding_var
));
938 if (!scm_is_string (encoding
))
941 return scm_i_string_chars (encoding
);
945 /* A fluid specifying the default conversion handler for newly created
946 ports. Its value should be one of the symbols below. */
947 SCM_VARIABLE (default_conversion_strategy_var
,
948 "%default-port-conversion-strategy");
950 /* Whether the above fluid is initialized. */
951 static int scm_conversion_strategy_init
= 0;
953 /* The possible conversion strategies. */
954 SCM_SYMBOL (sym_error
, "error");
955 SCM_SYMBOL (sym_substitute
, "substitute");
956 SCM_SYMBOL (sym_escape
, "escape");
958 /* Return the default failed encoding conversion policy for new created
960 scm_t_string_failed_conversion_handler
961 scm_i_default_port_conversion_handler (void)
963 scm_t_string_failed_conversion_handler handler
;
965 if (!scm_conversion_strategy_init
966 || !scm_is_fluid (SCM_VARIABLE_REF (default_conversion_strategy_var
)))
967 handler
= SCM_FAILED_CONVERSION_QUESTION_MARK
;
972 fluid
= SCM_VARIABLE_REF (default_conversion_strategy_var
);
973 value
= scm_fluid_ref (fluid
);
975 if (scm_is_eq (sym_substitute
, value
))
976 handler
= SCM_FAILED_CONVERSION_QUESTION_MARK
;
977 else if (scm_is_eq (sym_escape
, value
))
978 handler
= SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
;
980 /* Default to 'error also when the fluid's value is not one of
981 the valid symbols. */
982 handler
= SCM_FAILED_CONVERSION_ERROR
;
988 /* Use HANDLER as the default conversion strategy for future ports. */
990 scm_i_set_default_port_conversion_handler (scm_t_string_failed_conversion_handler
995 if (!scm_conversion_strategy_init
996 || !scm_is_fluid (SCM_VARIABLE_REF (default_conversion_strategy_var
)))
997 scm_misc_error (NULL
, "tried to set conversion strategy fluid before it is initialized",
1002 case SCM_FAILED_CONVERSION_ERROR
:
1003 strategy
= sym_error
;
1006 case SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
:
1007 strategy
= sym_escape
;
1010 case SCM_FAILED_CONVERSION_QUESTION_MARK
:
1011 strategy
= sym_substitute
;
1018 scm_fluid_set_x (SCM_VARIABLE_REF (default_conversion_strategy_var
),
1023 scm_i_unget_bytes_unlocked (const unsigned char *buf
, size_t len
, SCM port
);
1025 /* If the next LEN bytes from PORT are equal to those in BYTES, then
1026 return 1, else return 0. Leave the port position unchanged. */
1028 looking_at_bytes (SCM port
, const unsigned char *bytes
, int len
)
1030 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
1033 while (i
< len
&& scm_peek_byte_or_eof_unlocked (port
) == bytes
[i
])
1038 scm_i_unget_bytes_unlocked (bytes
, i
, port
);
1042 static const unsigned char scm_utf8_bom
[3] = {0xEF, 0xBB, 0xBF};
1043 static const unsigned char scm_utf16be_bom
[2] = {0xFE, 0xFF};
1044 static const unsigned char scm_utf16le_bom
[2] = {0xFF, 0xFE};
1045 static const unsigned char scm_utf32be_bom
[4] = {0x00, 0x00, 0xFE, 0xFF};
1046 static const unsigned char scm_utf32le_bom
[4] = {0xFF, 0xFE, 0x00, 0x00};
1048 /* Decide what byte order to use for a UTF-16 port. Return "UTF-16BE"
1049 or "UTF-16LE". MODE must be either SCM_PORT_READ or SCM_PORT_WRITE,
1050 and specifies which operation is about to be done. The MODE
1051 determines how we will decide the byte order. We deliberately avoid
1052 reading from the port unless the user is about to do so. If the user
1053 is about to read, then we look for a BOM, and if present, we use it
1054 to determine the byte order. Otherwise we choose big endian, as
1055 recommended by the Unicode Standard. Note that the BOM (if any) is
1056 not consumed here. */
1058 decide_utf16_encoding (SCM port
, scm_t_port_rw_active mode
)
1060 if (mode
== SCM_PORT_READ
1061 && SCM_PORT_GET_INTERNAL (port
)->at_stream_start_for_bom_read
1062 && looking_at_bytes (port
, scm_utf16le_bom
, sizeof scm_utf16le_bom
))
1068 /* Decide what byte order to use for a UTF-32 port. Return "UTF-32BE"
1069 or "UTF-32LE". See the comment above 'decide_utf16_encoding' for
1072 decide_utf32_encoding (SCM port
, scm_t_port_rw_active mode
)
1074 if (mode
== SCM_PORT_READ
1075 && SCM_PORT_GET_INTERNAL (port
)->at_stream_start_for_bom_read
1076 && looking_at_bytes (port
, scm_utf32le_bom
, sizeof scm_utf32le_bom
))
1083 finalize_iconv_descriptors (void *ptr
, void *data
)
1085 close_iconv_descriptors (ptr
);
1088 static scm_t_iconv_descriptors
*
1089 open_iconv_descriptors (const char *encoding
, int reading
, int writing
)
1091 scm_t_iconv_descriptors
*id
;
1092 iconv_t input_cd
, output_cd
;
1095 input_cd
= (iconv_t
) -1;
1096 output_cd
= (iconv_t
) -1;
1098 for (i
= 0; encoding
[i
]; i
++)
1099 if (encoding
[i
] > 127)
1100 goto invalid_encoding
;
1104 /* Open an input iconv conversion descriptor, from ENCODING
1105 to UTF-8. We choose UTF-8, not UTF-32, because iconv
1106 implementations can typically convert from anything to
1107 UTF-8, but not to UTF-32 (see
1108 <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00007.html>). */
1110 /* Assume opening an iconv descriptor causes about 16 KB of
1112 scm_gc_register_allocation (16 * 1024);
1114 input_cd
= iconv_open ("UTF-8", encoding
);
1115 if (input_cd
== (iconv_t
) -1)
1116 goto invalid_encoding
;
1121 /* Assume opening an iconv descriptor causes about 16 KB of
1123 scm_gc_register_allocation (16 * 1024);
1125 output_cd
= iconv_open (encoding
, "UTF-8");
1126 if (output_cd
== (iconv_t
) -1)
1128 if (input_cd
!= (iconv_t
) -1)
1129 iconv_close (input_cd
);
1130 goto invalid_encoding
;
1134 id
= scm_gc_malloc_pointerless (sizeof (*id
), "iconv descriptors");
1135 id
->input_cd
= input_cd
;
1136 id
->output_cd
= output_cd
;
1138 /* Register a finalizer to close the descriptors. */
1139 scm_i_set_finalizer (id
, finalize_iconv_descriptors
, NULL
);
1146 err
= scm_from_latin1_string (encoding
);
1147 scm_misc_error ("open_iconv_descriptors",
1148 "invalid or unknown character encoding ~s",
1154 close_iconv_descriptors (scm_t_iconv_descriptors
*id
)
1156 if (id
->input_cd
!= (iconv_t
) -1)
1157 iconv_close (id
->input_cd
);
1158 if (id
->output_cd
!= (iconv_t
) -1)
1159 iconv_close (id
->output_cd
);
1160 id
->input_cd
= (void *) -1;
1161 id
->output_cd
= (void *) -1;
1164 scm_t_iconv_descriptors
*
1165 scm_i_port_iconv_descriptors (SCM port
, scm_t_port_rw_active mode
)
1167 scm_t_port_internal
*pti
= SCM_PORT_GET_INTERNAL (port
);
1169 assert (pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_ICONV
);
1171 if (!pti
->iconv_descriptors
)
1173 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
1174 const char *precise_encoding
;
1177 pt
->encoding
= "ISO-8859-1";
1179 /* If the specified encoding is UTF-16 or UTF-32, then make
1180 that more precise by deciding what byte order to use. */
1181 if (strcmp (pt
->encoding
, "UTF-16") == 0)
1182 precise_encoding
= decide_utf16_encoding (port
, mode
);
1183 else if (strcmp (pt
->encoding
, "UTF-32") == 0)
1184 precise_encoding
= decide_utf32_encoding (port
, mode
);
1186 precise_encoding
= pt
->encoding
;
1188 pti
->iconv_descriptors
=
1189 open_iconv_descriptors (precise_encoding
,
1190 SCM_INPUT_PORT_P (port
),
1191 SCM_OUTPUT_PORT_P (port
));
1194 return pti
->iconv_descriptors
;
1197 /* The name of the encoding is itself encoded in ASCII. */
1199 scm_i_set_port_encoding_x (SCM port
, const char *encoding
)
1202 scm_t_port_internal
*pti
;
1203 scm_t_iconv_descriptors
*prev
;
1205 /* Set the character encoding for this port. */
1206 pt
= SCM_PTAB_ENTRY (port
);
1207 pti
= SCM_PORT_GET_INTERNAL (port
);
1208 prev
= pti
->iconv_descriptors
;
1210 /* In order to handle cases where the encoding changes mid-stream
1211 (e.g. within an HTTP stream, or within a file that is composed of
1212 segments with different encodings), we consider this to be "stream
1213 start" for purposes of BOM handling, regardless of our actual file
1215 pti
->at_stream_start_for_bom_read
= 1;
1216 pti
->at_stream_start_for_bom_write
= 1;
1218 if (encoding_matches (encoding
, "UTF-8"))
1220 pt
->encoding
= "UTF-8";
1221 pti
->encoding_mode
= SCM_PORT_ENCODING_MODE_UTF8
;
1223 else if (encoding_matches (encoding
, "ISO-8859-1"))
1225 pt
->encoding
= "ISO-8859-1";
1226 pti
->encoding_mode
= SCM_PORT_ENCODING_MODE_LATIN1
;
1230 pt
->encoding
= canonicalize_encoding (encoding
);
1231 pti
->encoding_mode
= SCM_PORT_ENCODING_MODE_ICONV
;
1234 pti
->iconv_descriptors
= NULL
;
1236 close_iconv_descriptors (prev
);
1239 SCM_DEFINE (scm_port_encoding
, "port-encoding", 1, 0, 0,
1241 "Returns, as a string, the character encoding that @var{port}\n"
1242 "uses to interpret its input and output.\n")
1243 #define FUNC_NAME s_scm_port_encoding
1245 SCM_VALIDATE_PORT (1, port
);
1247 return scm_from_latin1_string (SCM_PTAB_ENTRY (port
)->encoding
);
1251 SCM_DEFINE (scm_set_port_encoding_x
, "set-port-encoding!", 2, 0, 0,
1252 (SCM port
, SCM enc
),
1253 "Sets the character encoding that will be used to interpret all\n"
1254 "port I/O. New ports are created with the encoding\n"
1255 "appropriate for the current locale if @code{setlocale} has \n"
1256 "been called or ISO-8859-1 otherwise\n"
1257 "and this procedure can be used to modify that encoding.\n")
1258 #define FUNC_NAME s_scm_set_port_encoding_x
1262 SCM_VALIDATE_PORT (1, port
);
1263 SCM_VALIDATE_STRING (2, enc
);
1265 enc_str
= scm_to_latin1_string (enc
);
1266 scm_i_set_port_encoding_x (port
, enc_str
);
1269 return SCM_UNSPECIFIED
;
1273 SCM_DEFINE (scm_port_conversion_strategy
, "port-conversion-strategy",
1274 1, 0, 0, (SCM port
),
1275 "Returns the behavior of the port when handling a character that\n"
1276 "is not representable in the port's current encoding.\n"
1277 "It returns the symbol @code{error} if unrepresentable characters\n"
1278 "should cause exceptions, @code{substitute} if the port should\n"
1279 "try to replace unrepresentable characters with question marks or\n"
1280 "approximate characters, or @code{escape} if unrepresentable\n"
1281 "characters should be converted to string escapes.\n"
1283 "If @var{port} is @code{#f}, then the current default behavior\n"
1284 "will be returned. New ports will have this default behavior\n"
1285 "when they are created.\n")
1286 #define FUNC_NAME s_scm_port_conversion_strategy
1288 scm_t_string_failed_conversion_handler h
;
1290 SCM_VALIDATE_OPPORT (1, port
);
1292 if (scm_is_false (port
))
1293 h
= scm_i_default_port_conversion_handler ();
1298 SCM_VALIDATE_OPPORT (1, port
);
1299 pt
= SCM_PTAB_ENTRY (port
);
1301 h
= pt
->ilseq_handler
;
1304 if (h
== SCM_FAILED_CONVERSION_ERROR
)
1305 return scm_from_latin1_symbol ("error");
1306 else if (h
== SCM_FAILED_CONVERSION_QUESTION_MARK
)
1307 return scm_from_latin1_symbol ("substitute");
1308 else if (h
== SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
)
1309 return scm_from_latin1_symbol ("escape");
1313 /* Never gets here. */
1314 return SCM_UNDEFINED
;
1318 SCM_DEFINE (scm_set_port_conversion_strategy_x
, "set-port-conversion-strategy!",
1320 (SCM port
, SCM sym
),
1321 "Sets the behavior of the interpreter when outputting a character\n"
1322 "that is not representable in the port's current encoding.\n"
1323 "@var{sym} can be either @code{'error}, @code{'substitute}, or\n"
1324 "@code{'escape}. If it is @code{'error}, an error will be thrown\n"
1325 "when an unconvertible character is encountered. If it is\n"
1326 "@code{'substitute}, then unconvertible characters will \n"
1327 "be replaced with approximate characters, or with question marks\n"
1328 "if no approximately correct character is available.\n"
1329 "If it is @code{'escape},\n"
1330 "it will appear as a hex escape when output.\n"
1332 "If @var{port} is an open port, the conversion error behavior\n"
1333 "is set for that port. If it is @code{#f}, it is set as the\n"
1334 "default behavior for any future ports that get created in\n"
1336 #define FUNC_NAME s_scm_set_port_conversion_strategy_x
1338 scm_t_string_failed_conversion_handler handler
;
1340 if (scm_is_eq (sym
, sym_error
))
1341 handler
= SCM_FAILED_CONVERSION_ERROR
;
1342 else if (scm_is_eq (sym
, sym_substitute
))
1343 handler
= SCM_FAILED_CONVERSION_QUESTION_MARK
;
1344 else if (scm_is_eq (sym
, sym_escape
))
1345 handler
= SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
;
1347 SCM_MISC_ERROR ("unknown conversion strategy ~s", scm_list_1 (sym
));
1349 if (scm_is_false (port
))
1350 scm_i_set_default_port_conversion_handler (handler
);
1353 SCM_VALIDATE_OPPORT (1, port
);
1354 SCM_PTAB_ENTRY (port
)->ilseq_handler
= handler
;
1357 return SCM_UNSPECIFIED
;
1364 /* The port lock. */
1367 lock_port (void *mutex
)
1369 scm_i_pthread_mutex_lock ((scm_i_pthread_mutex_t
*) mutex
);
1373 unlock_port (void *mutex
)
1375 scm_i_pthread_mutex_unlock ((scm_i_pthread_mutex_t
*) mutex
);
1379 scm_dynwind_lock_port (SCM port
)
1380 #define FUNC_NAME "dynwind-lock-port"
1382 scm_i_pthread_mutex_t
*lock
;
1383 SCM_VALIDATE_OPPORT (SCM_ARG1
, port
);
1384 scm_c_lock_port (port
, &lock
);
1387 scm_dynwind_unwind_handler (unlock_port
, lock
, SCM_F_WIND_EXPLICITLY
);
1388 scm_dynwind_rewind_handler (lock_port
, lock
, 0);
1399 scm_get_byte_or_eof (SCM port
)
1401 scm_i_pthread_mutex_t
*lock
;
1404 scm_c_lock_port (port
, &lock
);
1405 ret
= scm_get_byte_or_eof_unlocked (port
);
1407 scm_i_pthread_mutex_unlock (lock
);
1413 scm_peek_byte_or_eof (SCM port
)
1415 scm_i_pthread_mutex_t
*lock
;
1418 scm_c_lock_port (port
, &lock
);
1419 ret
= scm_peek_byte_or_eof_unlocked (port
);
1421 scm_i_pthread_mutex_unlock (lock
);
1428 * Used by an application to read arbitrary number of bytes from an
1429 * SCM port. Same semantics as libc read, except that scm_c_read only
1430 * returns less than SIZE bytes if at end-of-file.
1432 * Warning: Doesn't update port line and column counts! */
1434 /* This structure, and the following swap_buffer function, are used
1435 for temporarily swapping a port's own read buffer, and the buffer
1436 that the caller of scm_c_read provides. */
1437 struct port_and_swap_buffer
1440 unsigned char *buffer
;
1445 swap_buffer (void *data
)
1447 struct port_and_swap_buffer
*psb
= (struct port_and_swap_buffer
*) data
;
1448 unsigned char *old_buf
= psb
->pt
->read_buf
;
1449 size_t old_size
= psb
->pt
->read_buf_size
;
1451 /* Make the port use (buffer, size) from the struct. */
1452 psb
->pt
->read_pos
= psb
->pt
->read_buf
= psb
->pt
->read_end
= psb
->buffer
;
1453 psb
->pt
->read_buf_size
= psb
->size
;
1455 /* Save the port's old (buffer, size) in the struct. */
1456 psb
->buffer
= old_buf
;
1457 psb
->size
= old_size
;
1460 static int scm_i_fill_input_unlocked (SCM port
);
1463 scm_c_read_unlocked (SCM port
, void *buffer
, size_t size
)
1464 #define FUNC_NAME "scm_c_read"
1467 scm_t_port_internal
*pti
;
1468 size_t n_read
= 0, n_available
;
1469 struct port_and_swap_buffer psb
;
1471 SCM_VALIDATE_OPINPORT (1, port
);
1473 pt
= SCM_PTAB_ENTRY (port
);
1474 pti
= SCM_PORT_GET_INTERNAL (port
);
1475 if (pt
->rw_active
== SCM_PORT_WRITE
)
1476 SCM_PORT_DESCRIPTOR (port
)->flush (port
);
1479 pt
->rw_active
= SCM_PORT_READ
;
1481 /* Take bytes first from the port's read buffer. */
1482 if (pt
->read_pos
< pt
->read_end
)
1484 n_available
= min (size
, pt
->read_end
- pt
->read_pos
);
1485 memcpy (buffer
, pt
->read_pos
, n_available
);
1486 buffer
= (char *) buffer
+ n_available
;
1487 pt
->read_pos
+= n_available
;
1488 n_read
+= n_available
;
1489 size
-= n_available
;
1492 /* Avoid the scm_dynwind_* costs if we now have enough data. */
1496 /* Now we will call scm_i_fill_input_unlocked repeatedly until we have
1497 read the requested number of bytes. (Note that a single
1498 scm_i_fill_input_unlocked call does not guarantee to fill the whole
1499 of the port's read buffer.) */
1500 if (pt
->read_buf_size
<= 1
1501 && pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_LATIN1
)
1503 /* The port that we are reading from is unbuffered - i.e. does not
1504 have its own persistent buffer - but we have a buffer, provided
1505 by our caller, that is the right size for the data that is
1506 wanted. For the following scm_i_fill_input_unlocked calls,
1507 therefore, we use the buffer in hand as the port's read buffer.
1509 We need to make sure that the port's normal (1 byte) buffer is
1510 reinstated in case one of the scm_i_fill_input_unlocked ()
1511 calls throws an exception; we use the scm_dynwind_* API to
1514 A consequence of this optimization is that the fill_input
1515 functions can't unget characters. That'll push data to the
1516 pushback buffer instead of this psb buffer. */
1518 unsigned char *pback
= pt
->putback_buf
;
1521 psb
.buffer
= buffer
;
1523 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE
);
1524 scm_dynwind_rewind_handler (swap_buffer
, &psb
, SCM_F_WIND_EXPLICITLY
);
1525 scm_dynwind_unwind_handler (swap_buffer
, &psb
, SCM_F_WIND_EXPLICITLY
);
1527 /* Call scm_i_fill_input_unlocked until we have all the bytes that
1528 we need, or we hit EOF. */
1529 while (pt
->read_buf_size
&& (scm_i_fill_input_unlocked (port
) != EOF
))
1531 pt
->read_buf_size
-= (pt
->read_end
- pt
->read_pos
);
1532 pt
->read_pos
= pt
->read_buf
= pt
->read_end
;
1535 if (pback
!= pt
->putback_buf
1536 || pt
->read_buf
- (unsigned char *) buffer
< 0)
1537 scm_misc_error (FUNC_NAME
,
1538 "scm_c_read must not call a fill function that pushes "
1539 "back characters onto an unbuffered port", SCM_EOL
);
1541 n_read
+= pt
->read_buf
- (unsigned char *) buffer
;
1543 /* Reinstate the port's normal buffer. */
1548 /* The port has its own buffer. It is important that we use it,
1549 even if it happens to be smaller than our caller's buffer, so
1550 that a custom port implementation's entry points (in
1551 particular, fill_input) can rely on the buffer always being
1552 the same as they first set up. */
1553 while (size
&& (scm_i_fill_input_unlocked (port
) != EOF
))
1555 n_available
= min (size
, pt
->read_end
- pt
->read_pos
);
1556 memcpy (buffer
, pt
->read_pos
, n_available
);
1557 buffer
= (char *) buffer
+ n_available
;
1558 pt
->read_pos
+= n_available
;
1559 n_read
+= n_available
;
1560 size
-= n_available
;
1569 scm_c_read (SCM port
, void *buffer
, size_t size
)
1571 scm_i_pthread_mutex_t
*lock
;
1574 scm_c_lock_port (port
, &lock
);
1575 ret
= scm_c_read_unlocked (port
, buffer
, size
);
1577 scm_i_pthread_mutex_unlock (lock
);
1583 /* Update the line and column number of PORT after consumption of C. */
1585 update_port_lf (scm_t_wchar c
, SCM port
)
1610 #define SCM_MBCHAR_BUF_SIZE (4)
1612 /* Convert the SIZE-byte UTF-8 sequence in UTF8_BUF to a codepoint.
1613 UTF8_BUF is assumed to contain a valid UTF-8 sequence. */
1615 utf8_to_codepoint (const scm_t_uint8
*utf8_buf
, size_t size
)
1617 scm_t_wchar codepoint
;
1619 if (utf8_buf
[0] <= 0x7f)
1622 codepoint
= utf8_buf
[0];
1624 else if ((utf8_buf
[0] & 0xe0) == 0xc0)
1627 codepoint
= ((scm_t_wchar
) utf8_buf
[0] & 0x1f) << 6UL
1628 | (utf8_buf
[1] & 0x3f);
1630 else if ((utf8_buf
[0] & 0xf0) == 0xe0)
1633 codepoint
= ((scm_t_wchar
) utf8_buf
[0] & 0x0f) << 12UL
1634 | ((scm_t_wchar
) utf8_buf
[1] & 0x3f) << 6UL
1635 | (utf8_buf
[2] & 0x3f);
1640 codepoint
= ((scm_t_wchar
) utf8_buf
[0] & 0x07) << 18UL
1641 | ((scm_t_wchar
) utf8_buf
[1] & 0x3f) << 12UL
1642 | ((scm_t_wchar
) utf8_buf
[2] & 0x3f) << 6UL
1643 | (utf8_buf
[3] & 0x3f);
1649 /* Read a UTF-8 sequence from PORT. On success, return 0 and set
1650 *CODEPOINT to the codepoint that was read, fill BUF with its UTF-8
1651 representation, and set *LEN to the length in bytes. Return
1652 `EILSEQ' on error. */
1654 get_utf8_codepoint (SCM port
, scm_t_wchar
*codepoint
,
1655 scm_t_uint8 buf
[SCM_MBCHAR_BUF_SIZE
], size_t *len
)
1657 #define ASSERT_NOT_EOF(b) \
1658 if (SCM_UNLIKELY ((b) == EOF)) \
1660 #define CONSUME_PEEKED_BYTE() \
1667 pt
= SCM_PTAB_ENTRY (port
);
1669 byte
= scm_get_byte_or_eof_unlocked (port
);
1676 buf
[0] = (scm_t_uint8
) byte
;
1681 *codepoint
= buf
[0];
1682 else if (buf
[0] >= 0xc2 && buf
[0] <= 0xdf)
1685 byte
= scm_peek_byte_or_eof_unlocked (port
);
1686 ASSERT_NOT_EOF (byte
);
1688 if (SCM_UNLIKELY ((byte
& 0xc0) != 0x80))
1691 CONSUME_PEEKED_BYTE ();
1692 buf
[1] = (scm_t_uint8
) byte
;
1695 *codepoint
= ((scm_t_wchar
) buf
[0] & 0x1f) << 6UL
1698 else if ((buf
[0] & 0xf0) == 0xe0)
1701 byte
= scm_peek_byte_or_eof_unlocked (port
);
1702 ASSERT_NOT_EOF (byte
);
1704 if (SCM_UNLIKELY ((byte
& 0xc0) != 0x80
1705 || (buf
[0] == 0xe0 && byte
< 0xa0)
1706 || (buf
[0] == 0xed && byte
> 0x9f)))
1709 CONSUME_PEEKED_BYTE ();
1710 buf
[1] = (scm_t_uint8
) byte
;
1713 byte
= scm_peek_byte_or_eof_unlocked (port
);
1714 ASSERT_NOT_EOF (byte
);
1716 if (SCM_UNLIKELY ((byte
& 0xc0) != 0x80))
1719 CONSUME_PEEKED_BYTE ();
1720 buf
[2] = (scm_t_uint8
) byte
;
1723 *codepoint
= ((scm_t_wchar
) buf
[0] & 0x0f) << 12UL
1724 | ((scm_t_wchar
) buf
[1] & 0x3f) << 6UL
1727 else if (buf
[0] >= 0xf0 && buf
[0] <= 0xf4)
1730 byte
= scm_peek_byte_or_eof_unlocked (port
);
1731 ASSERT_NOT_EOF (byte
);
1733 if (SCM_UNLIKELY (((byte
& 0xc0) != 0x80)
1734 || (buf
[0] == 0xf0 && byte
< 0x90)
1735 || (buf
[0] == 0xf4 && byte
> 0x8f)))
1738 CONSUME_PEEKED_BYTE ();
1739 buf
[1] = (scm_t_uint8
) byte
;
1742 byte
= scm_peek_byte_or_eof_unlocked (port
);
1743 ASSERT_NOT_EOF (byte
);
1745 if (SCM_UNLIKELY ((byte
& 0xc0) != 0x80))
1748 CONSUME_PEEKED_BYTE ();
1749 buf
[2] = (scm_t_uint8
) byte
;
1752 byte
= scm_peek_byte_or_eof_unlocked (port
);
1753 ASSERT_NOT_EOF (byte
);
1755 if (SCM_UNLIKELY ((byte
& 0xc0) != 0x80))
1758 CONSUME_PEEKED_BYTE ();
1759 buf
[3] = (scm_t_uint8
) byte
;
1762 *codepoint
= ((scm_t_wchar
) buf
[0] & 0x07) << 18UL
1763 | ((scm_t_wchar
) buf
[1] & 0x3f) << 12UL
1764 | ((scm_t_wchar
) buf
[2] & 0x3f) << 6UL
1773 /* Here we could choose the consume the faulty byte when it's not a
1774 valid starting byte, but it's not a requirement. What Section 3.9
1775 of Unicode 6.0.0 mandates, though, is to not consume a byte that
1776 would otherwise be a valid starting byte. */
1780 #undef CONSUME_PEEKED_BYTE
1781 #undef ASSERT_NOT_EOF
1784 /* Read an ISO-8859-1 codepoint (a byte) from PORT. On success, return
1785 0 and set *CODEPOINT to the codepoint that was read, fill BUF with
1786 its UTF-8 representation, and set *LEN to the length in bytes.
1787 Return `EILSEQ' on error. */
1789 get_latin1_codepoint (SCM port
, scm_t_wchar
*codepoint
,
1790 char buf
[SCM_MBCHAR_BUF_SIZE
], size_t *len
)
1792 *codepoint
= scm_get_byte_or_eof_unlocked (port
);
1794 if (*codepoint
== EOF
)
1799 buf
[0] = *codepoint
;
1804 /* Likewise, read a byte sequence from PORT, passing it through its
1805 input conversion descriptor. */
1807 get_iconv_codepoint (SCM port
, scm_t_wchar
*codepoint
,
1808 char buf
[SCM_MBCHAR_BUF_SIZE
], size_t *len
)
1810 scm_t_iconv_descriptors
*id
;
1811 scm_t_uint8 utf8_buf
[SCM_MBCHAR_BUF_SIZE
];
1812 size_t input_size
= 0;
1814 id
= scm_i_port_iconv_descriptors (port
, SCM_PORT_READ
);
1819 char *input
, *output
;
1820 size_t input_left
, output_left
, done
;
1822 byte_read
= scm_get_byte_or_eof_unlocked (port
);
1823 if (SCM_UNLIKELY (byte_read
== EOF
))
1825 if (SCM_LIKELY (input_size
== 0))
1827 *codepoint
= (scm_t_wchar
) EOF
;
1833 /* EOF found in the middle of a multibyte character. */
1834 scm_i_set_pending_eof (port
);
1839 buf
[input_size
++] = byte_read
;
1842 input_left
= input_size
;
1843 output
= (char *) utf8_buf
;
1844 output_left
= sizeof (utf8_buf
);
1846 done
= iconv (id
->input_cd
, &input
, &input_left
, &output
, &output_left
);
1848 if (done
== (size_t) -1)
1851 if (SCM_LIKELY (err
== EINVAL
))
1852 /* The input byte sequence did not form a complete
1853 character. Read another byte and try again. */
1860 size_t output_size
= sizeof (utf8_buf
) - output_left
;
1861 if (SCM_LIKELY (output_size
> 0))
1863 /* iconv generated output. Convert the UTF8_BUF sequence
1864 to a Unicode code point. */
1865 *codepoint
= utf8_to_codepoint (utf8_buf
, output_size
);
1871 /* iconv consumed some bytes without producing any output.
1872 Most likely this means that a Unicode byte-order mark
1873 (BOM) was consumed, which should not be included in the
1874 returned buf. Shift any remaining bytes to the beginning
1875 of buf, and continue the loop. */
1876 memmove (buf
, input
, input_left
);
1877 input_size
= input_left
;
1884 /* Read a codepoint from PORT and return it in *CODEPOINT. Fill BUF
1885 with the byte representation of the codepoint in PORT's encoding, and
1886 set *LEN to the length in bytes of that representation. Return 0 on
1887 success and an errno value on error. */
1888 static SCM_C_INLINE
int
1889 get_codepoint (SCM port
, scm_t_wchar
*codepoint
,
1890 char buf
[SCM_MBCHAR_BUF_SIZE
], size_t *len
)
1893 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
1894 scm_t_port_internal
*pti
= SCM_PORT_GET_INTERNAL (port
);
1896 if (pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_UTF8
)
1897 err
= get_utf8_codepoint (port
, codepoint
, (scm_t_uint8
*) buf
, len
);
1898 else if (pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_LATIN1
)
1899 err
= get_latin1_codepoint (port
, codepoint
, buf
, len
);
1901 err
= get_iconv_codepoint (port
, codepoint
, buf
, len
);
1903 if (SCM_LIKELY (err
== 0))
1905 if (SCM_UNLIKELY (pti
->at_stream_start_for_bom_read
))
1907 /* Record that we're no longer at stream start. */
1908 pti
->at_stream_start_for_bom_read
= 0;
1910 pti
->at_stream_start_for_bom_write
= 0;
1912 /* If we just read a BOM in an encoding that recognizes them,
1913 then silently consume it and read another code point. */
1915 (*codepoint
== SCM_UNICODE_BOM
1916 && (pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_UTF8
1917 || strcmp (pt
->encoding
, "UTF-16") == 0
1918 || strcmp (pt
->encoding
, "UTF-32") == 0)))
1919 return get_codepoint (port
, codepoint
, buf
, len
);
1921 update_port_lf (*codepoint
, port
);
1923 else if (pt
->ilseq_handler
== SCM_ICONVEH_QUESTION_MARK
)
1927 update_port_lf (*codepoint
, port
);
1933 /* Read a codepoint from PORT and return it. */
1935 scm_getc_unlocked (SCM port
)
1936 #define FUNC_NAME "scm_getc"
1940 scm_t_wchar codepoint
;
1941 char buf
[SCM_MBCHAR_BUF_SIZE
];
1943 err
= get_codepoint (port
, &codepoint
, buf
, &len
);
1944 if (SCM_UNLIKELY (err
!= 0))
1945 /* At this point PORT should point past the invalid encoding, as per
1946 R6RS-lib Section 8.2.4. */
1947 scm_decoding_error (FUNC_NAME
, err
, "input decoding error", port
);
1956 scm_i_pthread_mutex_t
*lock
;
1959 scm_c_lock_port (port
, &lock
);
1960 ret
= scm_getc_unlocked (port
);
1962 scm_i_pthread_mutex_unlock (lock
);
1968 SCM_DEFINE (scm_read_char
, "read-char", 0, 1, 0,
1970 "Return the next character available from @var{port}, updating\n"
1971 "@var{port} to point to the following character. If no more\n"
1972 "characters are available, the end-of-file object is returned.\n"
1974 "When @var{port}'s data cannot be decoded according to its\n"
1975 "character encoding, a @code{decoding-error} is raised and\n"
1976 "@var{port} points past the erroneous byte sequence.\n")
1977 #define FUNC_NAME s_scm_read_char
1980 if (SCM_UNBNDP (port
))
1981 port
= scm_current_input_port ();
1982 SCM_VALIDATE_OPINPORT (1, port
);
1983 c
= scm_getc_unlocked (port
);
1986 return SCM_MAKE_CHAR (c
);
1998 scm_i_unget_bytes_unlocked (const unsigned char *buf
, size_t len
, SCM port
)
1999 #define FUNC_NAME "scm_unget_bytes"
2001 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2002 size_t old_len
, new_len
;
2004 scm_i_clear_pending_eof (port
);
2006 if (pt
->read_buf
!= pt
->putback_buf
)
2007 /* switch to the put-back buffer. */
2009 if (pt
->putback_buf
== NULL
)
2011 pt
->putback_buf_size
= (len
> SCM_INITIAL_PUTBACK_BUF_SIZE
2012 ? len
: SCM_INITIAL_PUTBACK_BUF_SIZE
);
2014 = (unsigned char *) scm_gc_malloc_pointerless
2015 (pt
->putback_buf_size
, "putback buffer");
2018 pt
->saved_read_buf
= pt
->read_buf
;
2019 pt
->saved_read_pos
= pt
->read_pos
;
2020 pt
->saved_read_end
= pt
->read_end
;
2021 pt
->saved_read_buf_size
= pt
->read_buf_size
;
2023 /* Put read_pos at the end of the buffer, so that ungets will not
2024 have to shift the buffer contents each time. */
2025 pt
->read_buf
= pt
->putback_buf
;
2026 pt
->read_pos
= pt
->read_end
= pt
->putback_buf
+ pt
->putback_buf_size
;
2027 pt
->read_buf_size
= pt
->putback_buf_size
;
2030 old_len
= pt
->read_end
- pt
->read_pos
;
2031 new_len
= old_len
+ len
;
2033 if (new_len
> pt
->read_buf_size
)
2034 /* The putback buffer needs to be enlarged. */
2036 size_t new_buf_size
;
2037 unsigned char *new_buf
, *new_end
, *new_pos
;
2039 new_buf_size
= pt
->read_buf_size
* 2;
2040 if (new_buf_size
< new_len
)
2041 new_buf_size
= new_len
;
2043 new_buf
= (unsigned char *)
2044 scm_gc_malloc_pointerless (new_buf_size
, "putback buffer");
2046 /* Put the bytes at the end of the buffer, so that future
2047 ungets won't need to shift the buffer. */
2048 new_end
= new_buf
+ new_buf_size
;
2049 new_pos
= new_end
- old_len
;
2050 memcpy (new_pos
, pt
->read_pos
, old_len
);
2052 pt
->read_buf
= pt
->putback_buf
= new_buf
;
2053 pt
->read_pos
= new_pos
;
2054 pt
->read_end
= new_end
;
2055 pt
->read_buf_size
= pt
->putback_buf_size
= new_buf_size
;
2057 else if (pt
->read_buf
+ len
< pt
->read_pos
)
2058 /* If needed, shift the existing buffer contents up.
2059 This should not happen unless some external code
2060 manipulates the putback buffer pointers. */
2062 unsigned char *new_end
= pt
->read_buf
+ pt
->read_buf_size
;
2063 unsigned char *new_pos
= new_end
- old_len
;
2065 memmove (new_pos
, pt
->read_pos
, old_len
);
2066 pt
->read_pos
= new_pos
;
2067 pt
->read_end
= new_end
;
2070 /* Move read_pos back and copy the bytes there. */
2071 pt
->read_pos
-= len
;
2072 memcpy (pt
->read_buf
+ (pt
->read_pos
- pt
->read_buf
), buf
, len
);
2074 if (pt
->rw_active
== SCM_PORT_WRITE
)
2078 pt
->rw_active
= SCM_PORT_READ
;
2083 scm_unget_bytes_unlocked (const unsigned char *buf
, size_t len
, SCM port
)
2085 scm_i_unget_bytes_unlocked (buf
, len
, port
);
2089 scm_unget_byte_unlocked (int c
, SCM port
)
2091 unsigned char byte
= c
;
2092 scm_i_unget_bytes_unlocked (&byte
, 1, port
);
2096 scm_unget_bytes (const unsigned char *buf
, size_t len
, SCM port
)
2098 scm_i_pthread_mutex_t
*lock
;
2099 scm_c_lock_port (port
, &lock
);
2100 scm_i_unget_bytes_unlocked (buf
, len
, port
);
2102 scm_i_pthread_mutex_unlock (lock
);
2106 scm_unget_byte (int c
, SCM port
)
2108 unsigned char byte
= c
;
2109 scm_i_pthread_mutex_t
*lock
;
2110 scm_c_lock_port (port
, &lock
);
2111 scm_i_unget_bytes_unlocked (&byte
, 1, port
);
2113 scm_i_pthread_mutex_unlock (lock
);
2117 scm_ungetc_unlocked (scm_t_wchar c
, SCM port
)
2118 #define FUNC_NAME "scm_ungetc"
2120 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2121 scm_t_port_internal
*pti
= SCM_PORT_GET_INTERNAL (port
);
2123 char result_buf
[10];
2126 len
= sizeof (result_buf
);
2128 if (pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_UTF8
)
2132 result_buf
[0] = (char) c
;
2133 result
= result_buf
;
2138 (char *) u32_to_u8 ((uint32_t *) &c
, 1, (uint8_t *) result_buf
, &len
);
2140 else if (pti
->encoding_mode
== SCM_PORT_ENCODING_MODE_LATIN1
&& c
<= 0xff)
2142 result_buf
[0] = (char) c
;
2143 result
= result_buf
;
2147 result
= u32_conv_to_encoding (pt
->encoding
,
2148 (enum iconv_ilseq_handler
) pt
->ilseq_handler
,
2149 (uint32_t *) &c
, 1, NULL
,
2152 if (SCM_UNLIKELY (result
== NULL
|| len
== 0))
2153 scm_encoding_error (FUNC_NAME
, errno
,
2154 "conversion to port encoding failed",
2155 SCM_BOOL_F
, SCM_MAKE_CHAR (c
));
2157 scm_i_unget_bytes_unlocked ((unsigned char *) result
, len
, port
);
2159 if (SCM_UNLIKELY (result
!= result_buf
))
2164 /* What should col be in this case?
2165 * We'll leave it at -1.
2167 SCM_LINUM (port
) -= 1;
2175 scm_ungetc (scm_t_wchar c
, SCM port
)
2177 scm_i_pthread_mutex_t
*lock
;
2178 scm_c_lock_port (port
, &lock
);
2179 scm_ungetc_unlocked (c
, port
);
2181 scm_i_pthread_mutex_unlock (lock
);
2186 scm_ungets_unlocked (const char *s
, int n
, SCM port
)
2188 /* This is simple minded and inefficient, but unreading strings is
2189 * probably not a common operation, and remember that line and
2190 * column numbers have to be handled...
2192 * Please feel free to write an optimized version!
2195 scm_ungetc_unlocked (s
[n
], port
);
2199 scm_ungets (const char *s
, int n
, SCM port
)
2201 scm_i_pthread_mutex_t
*lock
;
2202 scm_c_lock_port (port
, &lock
);
2203 scm_ungets_unlocked (s
, n
, port
);
2205 scm_i_pthread_mutex_unlock (lock
);
2209 SCM_DEFINE (scm_peek_char
, "peek-char", 0, 1, 0,
2211 "Return the next character available from @var{port},\n"
2212 "@emph{without} updating @var{port} to point to the following\n"
2213 "character. If no more characters are available, the\n"
2214 "end-of-file object is returned.\n"
2216 "The value returned by\n"
2217 "a call to @code{peek-char} is the same as the value that would\n"
2218 "have been returned by a call to @code{read-char} on the same\n"
2219 "port. The only difference is that the very next call to\n"
2220 "@code{read-char} or @code{peek-char} on that @var{port} will\n"
2221 "return the value returned by the preceding call to\n"
2222 "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
2223 "an interactive port will hang waiting for input whenever a call\n"
2224 "to @code{read-char} would have hung.\n"
2226 "As for @code{read-char}, a @code{decoding-error} may be raised\n"
2227 "if such a situation occurs. However, unlike with @code{read-char},\n"
2228 "@var{port} still points at the beginning of the erroneous byte\n"
2229 "sequence when the error is raised.\n")
2230 #define FUNC_NAME s_scm_peek_char
2235 char bytes
[SCM_MBCHAR_BUF_SIZE
];
2239 if (SCM_UNBNDP (port
))
2240 port
= scm_current_input_port ();
2241 SCM_VALIDATE_OPINPORT (1, port
);
2243 column
= SCM_COL (port
);
2244 line
= SCM_LINUM (port
);
2246 err
= get_codepoint (port
, &c
, bytes
, &len
);
2248 scm_i_unget_bytes_unlocked ((unsigned char *) bytes
, len
, port
);
2250 SCM_COL (port
) = column
;
2251 SCM_LINUM (port
) = line
;
2253 if (SCM_UNLIKELY (err
!= 0))
2255 scm_decoding_error (FUNC_NAME
, err
, "input decoding error", port
);
2257 /* Shouldn't happen since `catch' always aborts to prompt. */
2258 result
= SCM_BOOL_F
;
2262 scm_i_set_pending_eof (port
);
2263 result
= SCM_EOF_VAL
;
2266 result
= SCM_MAKE_CHAR (c
);
2272 SCM_DEFINE (scm_unread_char
, "unread-char", 1, 1, 0,
2273 (SCM cobj
, SCM port
),
2274 "Place character @var{cobj} in @var{port} so that it will be\n"
2275 "read by the next read operation. If called multiple times, the\n"
2276 "unread characters will be read again in last-in first-out\n"
2277 "order. If @var{port} is not supplied, the current input port\n"
2279 #define FUNC_NAME s_scm_unread_char
2283 SCM_VALIDATE_CHAR (1, cobj
);
2284 if (SCM_UNBNDP (port
))
2285 port
= scm_current_input_port ();
2286 SCM_VALIDATE_OPINPORT (2, port
);
2288 c
= SCM_CHAR (cobj
);
2290 scm_ungetc_unlocked (c
, port
);
2295 SCM_DEFINE (scm_unread_string
, "unread-string", 2, 0, 0,
2296 (SCM str
, SCM port
),
2297 "Place the string @var{str} in @var{port} so that its characters will be\n"
2298 "read in subsequent read operations. If called multiple times, the\n"
2299 "unread characters will be read again in last-in first-out order. If\n"
2300 "@var{port} is not supplied, the current-input-port is used.")
2301 #define FUNC_NAME s_scm_unread_string
2304 SCM_VALIDATE_STRING (1, str
);
2305 if (SCM_UNBNDP (port
))
2306 port
= scm_current_input_port ();
2307 SCM_VALIDATE_OPINPORT (2, port
);
2309 n
= scm_i_string_length (str
);
2312 scm_ungetc_unlocked (scm_i_string_ref (str
, n
), port
);
2321 /* Manipulating the buffers. */
2323 /* This routine does not take any locks, as it is usually called as part
2324 of a port implementation. */
2326 scm_port_non_buffer (scm_t_port
*pt
)
2328 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
2329 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
2330 pt
->read_buf_size
= pt
->write_buf_size
= 1;
2331 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
2334 /* this should only be called when the read buffer is empty. it
2335 tries to refill the read buffer. it returns the first char from
2336 the port, which is either EOF or *(pt->read_pos). */
2338 scm_i_fill_input_unlocked (SCM port
)
2340 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2341 scm_t_port_internal
*pti
= SCM_PORT_GET_INTERNAL (port
);
2343 assert (pt
->read_pos
== pt
->read_end
);
2345 if (pti
->pending_eof
)
2347 pti
->pending_eof
= 0;
2351 if (pt
->read_buf
== pt
->putback_buf
)
2353 /* finished reading put-back chars. */
2354 pt
->read_buf
= pt
->saved_read_buf
;
2355 pt
->read_pos
= pt
->saved_read_pos
;
2356 pt
->read_end
= pt
->saved_read_end
;
2357 pt
->read_buf_size
= pt
->saved_read_buf_size
;
2358 if (pt
->read_pos
< pt
->read_end
)
2359 return *(pt
->read_pos
);
2361 return SCM_PORT_DESCRIPTOR (port
)->fill_input (port
);
2365 scm_fill_input (SCM port
)
2367 scm_i_pthread_mutex_t
*lock
;
2370 scm_c_lock_port (port
, &lock
);
2371 ret
= scm_fill_input_unlocked (port
);
2373 scm_i_pthread_mutex_unlock (lock
);
2379 /* Slow-path fallback for 'scm_get_byte_or_eof_unlocked' */
2381 scm_slow_get_byte_or_eof_unlocked (SCM port
)
2383 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2385 if (pt
->rw_active
== SCM_PORT_WRITE
)
2386 scm_flush_unlocked (port
);
2389 pt
->rw_active
= SCM_PORT_READ
;
2391 if (pt
->read_pos
>= pt
->read_end
)
2393 if (SCM_UNLIKELY (scm_i_fill_input_unlocked (port
) == EOF
))
2397 return *pt
->read_pos
++;
2400 /* Slow-path fallback for 'scm_peek_byte_or_eof_unlocked' */
2402 scm_slow_peek_byte_or_eof_unlocked (SCM port
)
2404 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2406 if (pt
->rw_active
== SCM_PORT_WRITE
)
2407 scm_flush_unlocked (port
);
2410 pt
->rw_active
= SCM_PORT_READ
;
2412 if (pt
->read_pos
>= pt
->read_end
)
2414 if (SCM_UNLIKELY (scm_i_fill_input_unlocked (port
) == EOF
))
2416 scm_i_set_pending_eof (port
);
2421 return *pt
->read_pos
;
2424 /* Move up to READ_LEN bytes from PORT's putback and/or read buffers
2425 into memory starting at DEST. Return the number of bytes moved.
2426 PORT's line/column numbers are left unchanged. */
2428 scm_take_from_input_buffers (SCM port
, char *dest
, size_t read_len
)
2430 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2431 size_t bytes_read
= 0;
2432 size_t from_buf
= min (pt
->read_end
- pt
->read_pos
, read_len
);
2436 memcpy (dest
, pt
->read_pos
, from_buf
);
2437 pt
->read_pos
+= from_buf
;
2438 bytes_read
+= from_buf
;
2439 read_len
-= from_buf
;
2443 /* if putback was active, try the real input buffer too. */
2444 if (pt
->read_buf
== pt
->putback_buf
)
2446 from_buf
= min (pt
->saved_read_end
- pt
->saved_read_pos
, read_len
);
2449 memcpy (dest
, pt
->saved_read_pos
, from_buf
);
2450 pt
->saved_read_pos
+= from_buf
;
2451 bytes_read
+= from_buf
;
2458 /* Clear a port's read buffers, returning the contents. */
2459 SCM_DEFINE (scm_drain_input
, "drain-input", 1, 0, 0,
2461 "This procedure clears a port's input buffers, similar\n"
2462 "to the way that force-output clears the output buffer. The\n"
2463 "contents of the buffers are returned as a single string, e.g.,\n"
2466 "(define p (open-input-file ...))\n"
2467 "(drain-input p) => empty string, nothing buffered yet.\n"
2468 "(unread-char (read-char p) p)\n"
2469 "(drain-input p) => initial chars from p, up to the buffer size.\n"
2471 "Draining the buffers may be useful for cleanly finishing\n"
2472 "buffered I/O so that the file descriptor can be used directly\n"
2473 "for further input.")
2474 #define FUNC_NAME s_scm_drain_input
2481 SCM_VALIDATE_OPINPORT (1, port
);
2482 pt
= SCM_PTAB_ENTRY (port
);
2484 count
= pt
->read_end
- pt
->read_pos
;
2485 if (pt
->read_buf
== pt
->putback_buf
)
2486 count
+= pt
->saved_read_end
- pt
->saved_read_pos
;
2490 result
= scm_i_make_string (count
, &data
, 0);
2491 scm_take_from_input_buffers (port
, data
, count
);
2494 result
= scm_nullstr
;
2501 scm_end_input_unlocked (SCM port
)
2504 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2506 scm_i_clear_pending_eof (port
);
2507 if (pt
->read_buf
== pt
->putback_buf
)
2509 offset
= pt
->read_end
- pt
->read_pos
;
2510 pt
->read_buf
= pt
->saved_read_buf
;
2511 pt
->read_pos
= pt
->saved_read_pos
;
2512 pt
->read_end
= pt
->saved_read_end
;
2513 pt
->read_buf_size
= pt
->saved_read_buf_size
;
2518 SCM_PORT_DESCRIPTOR (port
)->end_input (port
, offset
);
2522 scm_end_input (SCM port
)
2524 scm_i_pthread_mutex_t
*lock
;
2525 scm_c_lock_port (port
, &lock
);
2526 scm_end_input_unlocked (port
);
2528 scm_i_pthread_mutex_unlock (lock
);
2532 SCM_DEFINE (scm_force_output
, "force-output", 0, 1, 0,
2534 "Flush the specified output port, or the current output port if @var{port}\n"
2535 "is omitted. The current output buffer contents are passed to the\n"
2536 "underlying port implementation (e.g., in the case of fports, the\n"
2537 "data will be written to the file and the output buffer will be cleared.)\n"
2538 "It has no effect on an unbuffered port.\n\n"
2539 "The return value is unspecified.")
2540 #define FUNC_NAME s_scm_force_output
2542 if (SCM_UNBNDP (port
))
2543 port
= scm_current_output_port ();
2546 port
= SCM_COERCE_OUTPORT (port
);
2547 SCM_VALIDATE_OPOUTPORT (1, port
);
2549 scm_flush_unlocked (port
);
2550 return SCM_UNSPECIFIED
;
2555 scm_flush_unlocked (SCM port
)
2557 SCM_PORT_DESCRIPTOR (port
)->flush (port
);
2561 scm_flush (SCM port
)
2563 scm_i_pthread_mutex_t
*lock
;
2564 scm_c_lock_port (port
, &lock
);
2565 scm_flush_unlocked (port
);
2567 scm_i_pthread_mutex_unlock (lock
);
2572 scm_fill_input_unlocked (SCM port
)
2574 return scm_i_fill_input_unlocked (port
);
2583 scm_putc (char c
, SCM port
)
2585 scm_i_pthread_mutex_t
*lock
;
2586 scm_c_lock_port (port
, &lock
);
2587 scm_putc_unlocked (c
, port
);
2589 scm_i_pthread_mutex_unlock (lock
);
2594 scm_puts (const char *s
, SCM port
)
2596 scm_i_pthread_mutex_t
*lock
;
2597 scm_c_lock_port (port
, &lock
);
2598 scm_puts_unlocked (s
, port
);
2600 scm_i_pthread_mutex_unlock (lock
);
2606 * Used by an application to write arbitrary number of bytes to an SCM
2607 * port. Similar semantics as libc write. However, unlike libc
2608 * write, scm_c_write writes the requested number of bytes and has no
2611 * Warning: Doesn't update port line and column counts!
2614 scm_c_write_unlocked (SCM port
, const void *ptr
, size_t size
)
2615 #define FUNC_NAME "scm_c_write"
2618 scm_t_ptob_descriptor
*ptob
;
2620 SCM_VALIDATE_OPOUTPORT (1, port
);
2622 pt
= SCM_PTAB_ENTRY (port
);
2623 ptob
= SCM_PORT_DESCRIPTOR (port
);
2625 if (pt
->rw_active
== SCM_PORT_READ
)
2626 scm_end_input_unlocked (port
);
2628 ptob
->write (port
, ptr
, size
);
2631 pt
->rw_active
= SCM_PORT_WRITE
;
2636 scm_c_write (SCM port
, const void *ptr
, size_t size
)
2638 scm_i_pthread_mutex_t
*lock
;
2639 scm_c_lock_port (port
, &lock
);
2640 scm_c_write_unlocked (port
, ptr
, size
);
2642 scm_i_pthread_mutex_unlock (lock
);
2648 * This function differs from scm_c_write; it updates port line and
2651 scm_lfwrite_unlocked (const char *ptr
, size_t size
, SCM port
)
2653 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2654 scm_t_ptob_descriptor
*ptob
= SCM_PORT_DESCRIPTOR (port
);
2656 if (pt
->rw_active
== SCM_PORT_READ
)
2657 scm_end_input_unlocked (port
);
2659 ptob
->write (port
, ptr
, size
);
2661 for (; size
; ptr
++, size
--)
2662 update_port_lf ((scm_t_wchar
) (unsigned char) *ptr
, port
);
2665 pt
->rw_active
= SCM_PORT_WRITE
;
2669 scm_lfwrite (const char *ptr
, size_t size
, SCM port
)
2671 scm_i_pthread_mutex_t
*lock
;
2672 scm_c_lock_port (port
, &lock
);
2673 scm_lfwrite_unlocked (ptr
, size
, port
);
2675 scm_i_pthread_mutex_unlock (lock
);
2679 /* Write STR to PORT from START inclusive to END exclusive. */
2681 scm_lfwrite_substr (SCM str
, size_t start
, size_t end
, SCM port
)
2683 scm_t_port
*pt
= SCM_PTAB_ENTRY (port
);
2685 if (pt
->rw_active
== SCM_PORT_READ
)
2686 scm_end_input_unlocked (port
);
2688 if (end
== (size_t) -1)
2689 end
= scm_i_string_length (str
);
2691 scm_i_display_substring (str
, start
, end
, port
);
2694 pt
->rw_active
= SCM_PORT_WRITE
;
2700 /* Querying and setting positions, and character availability. */
2702 SCM_DEFINE (scm_char_ready_p
, "char-ready?", 0, 1, 0,
2704 "Return @code{#t} if a character is ready on input @var{port}\n"
2705 "and return @code{#f} otherwise. If @code{char-ready?} returns\n"
2706 "@code{#t} then the next @code{read-char} operation on\n"
2707 "@var{port} is guaranteed not to hang. If @var{port} is a file\n"
2708 "port at end of file then @code{char-ready?} returns @code{#t}.\n"
2710 "@code{char-ready?} exists to make it possible for a\n"
2711 "program to accept characters from interactive ports without\n"
2712 "getting stuck waiting for input. Any input editors associated\n"
2713 "with such ports must make sure that characters whose existence\n"
2714 "has been asserted by @code{char-ready?} cannot be rubbed out.\n"
2715 "If @code{char-ready?} were to return @code{#f} at end of file,\n"
2716 "a port at end of file would be indistinguishable from an\n"
2717 "interactive port that has no ready characters.")
2718 #define FUNC_NAME s_scm_char_ready_p
2722 if (SCM_UNBNDP (port
))
2723 port
= scm_current_input_port ();
2724 /* It's possible to close the current input port, so validate even in
2726 SCM_VALIDATE_OPINPORT (1, port
);
2728 pt
= SCM_PTAB_ENTRY (port
);
2730 /* if the current read buffer is filled, or the
2731 last pushed-back char has been read and the saved buffer is
2732 filled, result is true. */
2733 if (pt
->read_pos
< pt
->read_end
2734 || (pt
->read_buf
== pt
->putback_buf
2735 && pt
->saved_read_pos
< pt
->saved_read_end
))
2739 scm_t_ptob_descriptor
*ptob
= SCM_PORT_DESCRIPTOR (port
);
2741 if (ptob
->input_waiting
)
2742 return scm_from_bool(ptob
->input_waiting (port
));
2749 SCM_DEFINE (scm_seek
, "seek", 3, 0, 0,
2750 (SCM fd_port
, SCM offset
, SCM whence
),
2751 "Sets the current position of @var{fd_port} to the integer\n"
2752 "@var{offset}, which is interpreted according to the value of\n"
2755 "One of the following variables should be supplied for\n"
2757 "@defvar SEEK_SET\n"
2758 "Seek from the beginning of the file.\n"
2760 "@defvar SEEK_CUR\n"
2761 "Seek from the current position.\n"
2763 "@defvar SEEK_END\n"
2764 "Seek from the end of the file.\n"
2766 "If @var{fd_port} is a file descriptor, the underlying system\n"
2767 "call is @code{lseek}. @var{port} may be a string port.\n"
2769 "The value returned is the new position in the file. This means\n"
2770 "that the current position of a port can be obtained using:\n"
2772 "(seek port 0 SEEK_CUR)\n"
2774 #define FUNC_NAME s_scm_seek
2778 fd_port
= SCM_COERCE_OUTPORT (fd_port
);
2780 how
= scm_to_int (whence
);
2781 if (how
!= SEEK_SET
&& how
!= SEEK_CUR
&& how
!= SEEK_END
)
2782 SCM_OUT_OF_RANGE (3, whence
);
2784 if (SCM_OPPORTP (fd_port
))
2786 scm_t_port_internal
*pti
= SCM_PORT_GET_INTERNAL (fd_port
);
2787 scm_t_ptob_descriptor
*ptob
= SCM_PORT_DESCRIPTOR (fd_port
);
2788 off_t_or_off64_t off
= scm_to_off_t_or_off64_t (offset
);
2789 off_t_or_off64_t rv
;
2792 SCM_MISC_ERROR ("port is not seekable",
2793 scm_cons (fd_port
, SCM_EOL
));
2795 rv
= ptob
->seek (fd_port
, off
, how
);
2797 /* Set stream-start flags according to new position. */
2798 pti
->at_stream_start_for_bom_read
= (rv
== 0);
2799 pti
->at_stream_start_for_bom_write
= (rv
== 0);
2801 scm_i_clear_pending_eof (fd_port
);
2803 return scm_from_off_t_or_off64_t (rv
);
2805 else /* file descriptor?. */
2807 off_t_or_off64_t off
= scm_to_off_t_or_off64_t (offset
);
2808 off_t_or_off64_t rv
;
2809 rv
= lseek_or_lseek64 (scm_to_int (fd_port
), off
, how
);
2812 return scm_from_off_t_or_off64_t (rv
);
2821 /* Mingw has ftruncate(), perhaps implemented above using chsize, but
2822 doesn't have the filename version truncate(), hence this code. */
2823 #if HAVE_FTRUNCATE && ! HAVE_TRUNCATE
2825 truncate (const char *file
, off_t length
)
2829 fdes
= open (file
, O_BINARY
| O_WRONLY
);
2833 ret
= ftruncate (fdes
, length
);
2836 int save_errno
= errno
;
2842 return close (fdes
);
2844 #endif /* HAVE_FTRUNCATE && ! HAVE_TRUNCATE */
2846 SCM_DEFINE (scm_truncate_file
, "truncate-file", 1, 1, 0,
2847 (SCM object
, SCM length
),
2848 "Truncate file @var{object} to @var{length} bytes. @var{object}\n"
2849 "can be a filename string, a port object, or an integer file\n"
2851 "The return value is unspecified.\n"
2853 "For a port or file descriptor @var{length} can be omitted, in\n"
2854 "which case the file is truncated at the current position (per\n"
2855 "@code{ftell} above).\n"
2857 "On most systems a file can be extended by giving a length\n"
2858 "greater than the current size, but this is not mandatory in the\n"
2860 #define FUNC_NAME s_scm_truncate_file
2864 /* "object" can be a port, fdes or filename.
2866 Negative "length" makes no sense, but it's left to truncate() or
2867 ftruncate() to give back an error for that (normally EINVAL).
2870 if (SCM_UNBNDP (length
))
2872 /* must supply length if object is a filename. */
2873 if (scm_is_string (object
))
2874 SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL
);
2876 length
= scm_seek (object
, SCM_INUM0
, scm_from_int (SEEK_CUR
));
2879 object
= SCM_COERCE_OUTPORT (object
);
2880 if (scm_is_integer (object
))
2882 off_t_or_off64_t c_length
= scm_to_off_t_or_off64_t (length
);
2883 SCM_SYSCALL (rv
= ftruncate_or_ftruncate64 (scm_to_int (object
),
2886 else if (SCM_OPOUTPORTP (object
))
2888 off_t_or_off64_t c_length
= scm_to_off_t_or_off64_t (length
);
2889 scm_t_port
*pt
= SCM_PTAB_ENTRY (object
);
2890 scm_t_ptob_descriptor
*ptob
= SCM_PORT_DESCRIPTOR (object
);
2892 if (!ptob
->truncate
)
2893 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL
);
2895 scm_i_clear_pending_eof (object
);
2896 if (pt
->rw_active
== SCM_PORT_READ
)
2897 scm_end_input_unlocked (object
);
2898 else if (pt
->rw_active
== SCM_PORT_WRITE
)
2899 ptob
->flush (object
);
2901 ptob
->truncate (object
, c_length
);
2906 off_t_or_off64_t c_length
= scm_to_off_t_or_off64_t (length
);
2907 char *str
= scm_to_locale_string (object
);
2909 SCM_SYSCALL (rv
= truncate_or_truncate64 (str
, c_length
));
2916 return SCM_UNSPECIFIED
;
2920 SCM_DEFINE (scm_port_line
, "port-line", 1, 0, 0,
2922 "Return the current line number for @var{port}.\n"
2924 "The first line of a file is 0. But you might want to add 1\n"
2925 "when printing line numbers, since starting from 1 is\n"
2926 "traditional in error messages, and likely to be more natural to\n"
2928 #define FUNC_NAME s_scm_port_line
2930 port
= SCM_COERCE_OUTPORT (port
);
2931 SCM_VALIDATE_OPENPORT (1, port
);
2932 return scm_from_long (SCM_LINUM (port
));
2936 SCM_DEFINE (scm_set_port_line_x
, "set-port-line!", 2, 0, 0,
2937 (SCM port
, SCM line
),
2938 "Set the current line number for @var{port} to @var{line}. The\n"
2939 "first line of a file is 0.")
2940 #define FUNC_NAME s_scm_set_port_line_x
2942 port
= SCM_COERCE_OUTPORT (port
);
2943 SCM_VALIDATE_OPENPORT (1, port
);
2944 SCM_PTAB_ENTRY (port
)->line_number
= scm_to_long (line
);
2945 return SCM_UNSPECIFIED
;
2949 SCM_DEFINE (scm_port_column
, "port-column", 1, 0, 0,
2951 "Return the current column number of @var{port}.\n"
2952 "If the number is\n"
2953 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
2954 "- i.e. the first character of the first line is line 0, column 0.\n"
2955 "(However, when you display a file position, for example in an error\n"
2956 "message, we recommend you add 1 to get 1-origin integers. This is\n"
2957 "because lines and column numbers traditionally start with 1, and that is\n"
2958 "what non-programmers will find most natural.)")
2959 #define FUNC_NAME s_scm_port_column
2961 port
= SCM_COERCE_OUTPORT (port
);
2962 SCM_VALIDATE_OPENPORT (1, port
);
2963 return scm_from_int (SCM_COL (port
));
2967 SCM_DEFINE (scm_set_port_column_x
, "set-port-column!", 2, 0, 0,
2968 (SCM port
, SCM column
),
2969 "Set the current column of @var{port}. Before reading the first\n"
2970 "character on a line the column should be 0.")
2971 #define FUNC_NAME s_scm_set_port_column_x
2973 port
= SCM_COERCE_OUTPORT (port
);
2974 SCM_VALIDATE_OPENPORT (1, port
);
2975 SCM_PTAB_ENTRY (port
)->column_number
= scm_to_int (column
);
2976 return SCM_UNSPECIFIED
;
2980 SCM_DEFINE (scm_port_filename
, "port-filename", 1, 0, 0,
2982 "Return the filename associated with @var{port}, or @code{#f}\n"
2983 "if no filename is associated with the port.")
2984 #define FUNC_NAME s_scm_port_filename
2986 port
= SCM_COERCE_OUTPORT (port
);
2987 SCM_VALIDATE_OPENPORT (1, port
);
2988 return SCM_FILENAME (port
);
2992 SCM_DEFINE (scm_set_port_filename_x
, "set-port-filename!", 2, 0, 0,
2993 (SCM port
, SCM filename
),
2994 "Change the filename associated with @var{port}, using the current input\n"
2995 "port if none is specified. Note that this does not change the port's\n"
2996 "source of data, but only the value that is returned by\n"
2997 "@code{port-filename} and reported in diagnostic output.")
2998 #define FUNC_NAME s_scm_set_port_filename_x
3000 port
= SCM_COERCE_OUTPORT (port
);
3001 SCM_VALIDATE_OPENPORT (1, port
);
3002 /* We allow the user to set the filename to whatever he likes. */
3003 SCM_SET_FILENAME (port
, filename
);
3004 return SCM_UNSPECIFIED
;
3011 /* Implementation helpers for port printing functions. */
3014 scm_print_port_mode (SCM exp
, SCM port
)
3016 scm_puts_unlocked (SCM_CLOSEDP (exp
)
3018 : (SCM_RDNG
& SCM_CELL_WORD_0 (exp
)
3019 ? (SCM_WRTNG
& SCM_CELL_WORD_0 (exp
)
3022 : (SCM_WRTNG
& SCM_CELL_WORD_0 (exp
)
3029 scm_port_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
3031 char *type
= SCM_PTOBNAME (SCM_PTOBNUM (exp
));
3034 scm_puts_unlocked ("#<", port
);
3035 scm_print_port_mode (exp
, port
);
3036 scm_puts_unlocked (type
, port
);
3037 scm_putc_unlocked (' ', port
);
3038 scm_uintprint (SCM_CELL_WORD_1 (exp
), 16, port
);
3039 scm_putc_unlocked ('>', port
);
3046 /* Iterating over all ports. */
3048 struct for_each_data
3050 void (*proc
) (void *data
, SCM p
);
3055 for_each_trampoline (void *data
, SCM port
, SCM result
)
3057 struct for_each_data
*d
= data
;
3059 d
->proc (d
->data
, port
);
3065 scm_c_port_for_each (void (*proc
)(void *data
, SCM p
), void *data
)
3067 struct for_each_data d
;
3072 scm_c_weak_set_fold (for_each_trampoline
, &d
, SCM_EOL
,
3073 scm_i_port_weak_set
);
3077 scm_for_each_trampoline (void *data
, SCM port
)
3079 scm_call_1 (SCM_PACK_POINTER (data
), port
);
3082 SCM_DEFINE (scm_port_for_each
, "port-for-each", 1, 0, 0,
3084 "Apply @var{proc} to each port in the Guile port table\n"
3085 "in turn. The return value is unspecified. More specifically,\n"
3086 "@var{proc} is applied exactly once to every port that exists\n"
3087 "in the system at the time @code{port-for-each} is invoked.\n"
3088 "Changes to the port table while @code{port-for-each} is running\n"
3089 "have no effect as far as @code{port-for-each} is concerned.")
3090 #define FUNC_NAME s_scm_port_for_each
3092 SCM_VALIDATE_PROC (1, proc
);
3094 scm_c_port_for_each (scm_for_each_trampoline
, SCM_UNPACK_POINTER (proc
));
3096 return SCM_UNSPECIFIED
;
3101 flush_output_port (void *closure
, SCM port
)
3103 if (SCM_OPOUTPORTP (port
))
3104 scm_flush_unlocked (port
);
3107 SCM_DEFINE (scm_flush_all_ports
, "flush-all-ports", 0, 0, 0,
3109 "Equivalent to calling @code{force-output} on\n"
3110 "all open output ports. The return value is unspecified.")
3111 #define FUNC_NAME s_scm_flush_all_ports
3113 scm_c_port_for_each (&flush_output_port
, NULL
);
3114 return SCM_UNSPECIFIED
;
3123 scm_t_bits scm_tc16_void_port
= 0;
3125 static int fill_input_void_port (SCM port SCM_UNUSED
)
3131 write_void_port (SCM port SCM_UNUSED
,
3132 const void *data SCM_UNUSED
,
3133 size_t size SCM_UNUSED
)
3138 scm_i_void_port (long mode_bits
)
3142 ret
= scm_c_make_port (scm_tc16_void_port
, mode_bits
, 0);
3144 scm_port_non_buffer (SCM_PTAB_ENTRY (ret
));
3150 scm_void_port (char *mode_str
)
3152 return scm_i_void_port (scm_mode_bits (mode_str
));
3155 SCM_DEFINE (scm_sys_make_void_port
, "%make-void-port", 1, 0, 0,
3157 "Create and return a new void port. A void port acts like\n"
3158 "@file{/dev/null}. The @var{mode} argument\n"
3159 "specifies the input/output modes for this port: see the\n"
3160 "documentation for @code{open-file} in @ref{File Ports}.")
3161 #define FUNC_NAME s_scm_sys_make_void_port
3163 return scm_i_void_port (scm_i_mode_bits (mode
));
3170 /* Initialization. */
3175 /* lseek() symbols. */
3176 scm_c_define ("SEEK_SET", scm_from_int (SEEK_SET
));
3177 scm_c_define ("SEEK_CUR", scm_from_int (SEEK_CUR
));
3178 scm_c_define ("SEEK_END", scm_from_int (SEEK_END
));
3180 scm_tc16_void_port
= scm_make_port_type ("void", fill_input_void_port
,
3183 cur_inport_fluid
= scm_make_fluid ();
3184 cur_outport_fluid
= scm_make_fluid ();
3185 cur_errport_fluid
= scm_make_fluid ();
3186 cur_loadport_fluid
= scm_make_fluid ();
3188 scm_i_port_weak_set
= scm_c_make_weak_set (31);
3190 #include "libguile/ports.x"
3192 /* Use Latin-1 as the default port encoding. */
3193 SCM_VARIABLE_SET (default_port_encoding_var
,
3194 scm_make_fluid_with_default (SCM_BOOL_F
));
3195 scm_port_encoding_init
= 1;
3197 SCM_VARIABLE_SET (default_conversion_strategy_var
,
3198 scm_make_fluid_with_default (sym_substitute
));
3199 scm_conversion_strategy_init
= 1;
3201 /* These bindings are used when boot-9 turns `current-input-port' et
3202 al into parameters. They are then removed from the guile module. */
3203 scm_c_define ("%current-input-port-fluid", cur_inport_fluid
);
3204 scm_c_define ("%current-output-port-fluid", cur_outport_fluid
);
3205 scm_c_define ("%current-error-port-fluid", cur_errport_fluid
);