* libguile/ports.c (scm_i_port_alist, scm_i_set_port_alist_x): Removed.
(scm_i_port_property, scm_i_set_port_property_x): New procedures,
available from Scheme as '%port-property' and '%set-port-property!'.
* libguile/ports.h (scm_i_port_alist, scm_i_set_port_alist_x): Removed.
(scm_i_port_property, scm_i_set_port_property_x): New prototypes.
* libguile/read.c (set_port_read_option, init_read_options): Adapt to
use scm_i_port_property and scm_i_set_port_property_x.
* libguile/ports.c (scm_i_unget_bytes): New static function.
(scm_unget_bytes): New API function.
(scm_unget_byte): Rewrite to simply call 'scm_i_unget_bytes'.
(scm_ungetc, scm_peek_char, looking_at_bytes): Use 'scm_i_unget_bytes'.
* libguile/ports.h: Add prototype for 'scm_unget_bytes'.
* libguile/fports.c (scm_setvbuf): Use 'scm_unget_bytes'.
* libguile/r6rs-ports.c (scm_unget_bytevector): New procedure.
* module/ice-9/binary-ports.scm (unget-bytevector): New export.
* doc/ref/api-io.texi (R6RS Binary Input): Add documentation.
(R6RS I/O Ports): Update brief description of (ice-9 binary-ports) to
reflect the new reality: it is no longer a subset of (rnrs io ports).
* test-suite/tests/ports.test ("unget-bytevector"): Add test.
* libguile/ports.c (scm_i_get_byte_or_eof, scm_i_peek_byte_or_eof):
Rename to 'scm_slow_get_byte_or_eof' and 'scm_slow_peek_byte_or_eof',
respectively.
* libguile/ports.h (scm_i_get_byte_or_eof, scm_i_peek_byte_or_eof):
Rename to 'scm_slow_get_byte_or_eof' and 'scm_slow_peek_byte_or_eof',
respectively, and mark them as SCM_API.
* libguile/inline.h (scm_get_byte_or_eof, scm_peek_byte_or_eof): Adjust
to use the new names.
Suggested by Andy Wingo.
* libguile/inline.h (scm_get_byte_or_eof, scm_peek_byte_or_eof): Keep
only the fast path here, with fallback to 'scm_i_get_byte_or_eof' and
'scm_i_peek_byte_or_eof'.
* libguile/ports.c (scm_i_get_byte_or_eof, scm_i_peek_byte_or_eof):
New internal functions.
* libguile/ports.h (scm_i_get_byte_or_eof, scm_i_peek_byte_or_eof): Add
prototypes.
* libguile/ports-internal.h (struct scm_port_internal): Add 'alist'
member.
* libguile/ports.c (scm_i_port_alist, scm_i_set_port_alist_x): New
internal functions.
(scm_i_port_weak_hash): Update comment: the hash table is no longer
used to store the port's alist.
(scm_new_port_table_entry): Initialize 'alist'. Store SCM_BOOL_F in
the port weak hash, not SCM_EOL.
* libguile/ports.h (scm_i_port_alist, scm_i_set_port_alist_x): Add
protoypes.
* libguile/read.c (set_port_read_option, init_read_options): Access the
port's alist via 'scm_i_port_alist' and 'scm_i_set_port_alist_x'.
* libguile/ports-internal.h: New file.
* libguile/Makefile.am (noinst_HEADERS): Add ports-internal.h.
* libguile/ports.h (scm_t_port): Add a comment mentioning that the
'input_cd' and 'output_cd' fields of the public structure are no
longer what they seem to be.
* libguile/ports.c: Include ports-internal.h.
(finalize_port, scm_i_remove_port, get_iconv_codepoint, get_codepoint,
scm_i_set_port_encoding_x): Access 'input_cd' and 'output_cd' via the
new internal port structure.
(scm_new_port_table_entry): Allocate and initialize the internal port
structure.
* libguile/print.c: Include ports-internal.h.
(display_string_using_iconv, display_string): Access 'input_cd' and
'output_cd' via 'internal' pointer.
* libguile/ports.h:
* libguile/ports.c (scm_consume_byte_order_mark): New procedure.
* libguile/fports.c (scm_open_file): Call consume-byte-order-mark if we
are opening a file in "r" mode.
* libguile/read.c (scm_i_scan_for_encoding): Don't do anything about
byte-order marks.
* libguile/load.c (scm_primitive_load): Add a note about the duplicate
encoding scan.
* test-suite/tests/filesys.test: Add tests for UTF-8, UTF-16BE, and
UTF-16LE BOM handling.
Moved scm_i_struct_hash from struct.c to hash.c and made it static.
The port's alist is now a field of 'scm_t_port'.
Conflicts:
libguile/arrays.c
libguile/hash.c
libguile/ports.c
libguile/print.h
libguile/read.c
Fixes <http://bugs.gnu.org/11468>.
* libguile/ports.c (scm_conversion_strategy): Remove.
(default_conversion_strategy_var, sym_error, sym_substitute,
sym_escape): New variables.
(scm_i_get_conversion_strategy, scm_i_set_conversion_strategy_x):
Remove.
(scm_i_default_port_conversion_handler,
scm_i_set_default_port_conversion_handler): New functions.
(scm_port_conversion_strategy): Use
`scm_i_default_port_conversion_handler' when PORT is #f.
(scm_set_port_conversion_strategy_x): Use SYM_ERROR, SYM_SUBSTITUTE,
and SYM_ESCAPE. Use `scm_i_set_default_port_conversion_handler' when
PORT is #f.
(scm_init_ports): Initialize DEFAULT_CONVERSION_STRATEGY_VAR.
* libguile/ports.h: Update declarations accordingly.
* libguile/foreign.c: Change
`scm_i_get_conversion_strategy (SCM_BOOL_F)' to
`scm_i_default_port_conversion_handler ()'.
* libguile/strings.c: Likewise.
* test-suite/tests/ports.test ("%default-port-conversion-strategy"): New
test prefix.
* test-suite/tests/foreign.test ("pointer<->string")["%default-port-conversion-strategy
is error", "%default-port-conversion-strategy is soft"]: New tests.
* test-suite/test-suite/lib.scm (exception:encoding-error): Allow the
regexp to match `scm_to_stringn' error messages.
* doc/ref/api-io.texi (Ports): Document `%default-port-conversion-strategy'.
* libguile/fports.c (scm_revealed_count, scm_port_revealed)
(scm_set_port_revealed_x, scm_adjust_port_revealed_x): Move these APIs
here, and only operate on fports. To keep revealed ports alive, now
we will just keep them in a data structure that the GC knows about --
a static list.
* libguile/fports.h: Add revealed count to scm_t_fport, and move decls
of revealed-count functions here.
* libguile/ports.h:
* libguile/ports.c: Adapt to change. Remove SCM_REVEALED and
SCM_SETREVEALED; since they only apply to fports now, keeping them
around would be inviting type errors.
(finalize_port): We don't need to worry about resuscitating ports
here.
* libguile/init.c: Use the scm_set_port_revealed_x function to set the
revealed counts on stream ports.
* libguile/ports.h (scm_t_port_type_flags, scm_t_ptob_descriptor): Add
flags to ptob descriptors.
* libguile/ports.c (scm_set_port_flush): Set the SCM_PORT_TYPE_HAS_FLUSH
flag here.
(scm_c_make_port_with_encoding): Only add ports to the table if
SCM_PORT_TYPE_HAS_FLUSH is set. Only add finalizers to ports if there
is a free function.
(scm_close_port): Inline scm_i_remove_port here. Only remove from the
weak set if SCM_PORT_TYPE_HAS_FLUSH is set.
(scm_set_port_revealed_x): Add a comment.
* libguile/ports.h (struct scm_t_port): Add a flag for the port encoding
mode: UTF8 or iconv. The iconv descriptors are now in a separate
structure so that we can avoid attaching finalizers to the ports
themselves, in some cases.
* libguile/ports.c (scm_c_make_port_with_encoding): Init the encoding
mode.
(scm_i_remove_port): Adapt to call close_iconv_descriptors.
(finalize_iconv_descriptors, open_iconv_descriptors):
(close_iconv_descriptors): New infrastructure to manage iconv
descriptors.
(scm_i_port_iconv_descriptors): New internal helper.
(scm_i_set_port_encoding_x): Use open_iconv_descriptors, if needed.
(get_iconv_codepoint): Use pt->iconv_descriptors.
(get_codepoint): Check the port encoding mode flags.
* libguile/print.c (display_string_using_iconv): Use
scm_i_port_iconv_descriptors.
(display_string): Use pt->encoding_mode flag.
* libguile/ports.h (struct scm_t_port): Make the lock into a pointer
field instead of an inline field. It should be possible to make
unlocked ports by having a NULL lock field.
(scm_c_lock_port, scm_c_try_lock_port): Return the mutex if the port
was actually locked.
(scm_c_unlock_port): Remove.
* libguile/ports.c (scm_c_make_port_with_encoding): For now, leave
`lock' set to 0.
Change scm_c_lock_port callers to pay attention to the new API.
* libguile/print.c (scm_write, scm_display): Fix call to
dynwind-lock-port for ports-with-print-states.
* libguile/ports.h:
* libguile/ports.c (scm_current_warning_port)
(scm_set_current_warning_port): New functions, wrapping the Scheme
parameter.
* module/ice-9/boot-9.scm (current-warning-port): New parameter,
defining a port for warnings.
* libguile/ports.h (scm_c_lock_port, scm_c_try_lock_port)
(scm_c_unlock_port): Disable port locking until I have time to sort
out the bugs that are coming up.
* libguile/ports.c (scm_putc, scm_puts):
* libguile/ports.h (scm_putc_unlocked, scm_puts_unlocked): Separate into
_unlocked and locked variants. Change all callers to use the
_unlocked versions.
* libguile/ports.c (scm_c_read_unlocked, scm_c_read, scm_getc_unlocked)
(scm_getc): Split getc and read operations into locked and unlocked
variants. Change most uses to use the _unlocked version.
* libguile/ports.h (scm_get_byte_or_eof_unlocked):
(scm_peek_byte_or_eof_unlocked): Rename, adding _unlocked.
* libguile/ports.c (scm_get_byte_or_eof, scm_peek_byte_or_eof): Add
locking implementations. Adapt callers to use _unlocked variants;
they will do locking.
* libguile/read.c (read_token, scm_read_semicolon_comment)
(scm_read_shebang): Use unlocked variants. We will add locking
later.
* libguile/ports.h:
* libguile/ports.c (scm_revealed_count, scm_set_port_revealed_x): Make
threadsafe.
(scm_adjust_port_revealed_x): New function, to adjust a port's
revealed count in a threadsafe way.
* libguile/ports.h: Slight reorder.
* libguile/ports.c: Reorder ports implementation to match the header
file. This will make it easier to add locking and _unlocked
variants.
* libguile/ports.h (SCM_PORT_DESCRIPTOR): New macro, to get at a port
descriptor in the third word of a port instead of looking it up in a
table.
(scm_c_port_type_ref, scm_c_port_type_add_x): New API for working with
numbered ptob descriptors.
(SCM_PTOBNAME): Implement in terms of scm_c_port_type_ref.
(scm_get_byte_or_eof, scm_peek_byte_or_eof): Use SCM_PORT_DESCRIPTOR.
* libguile/ports.c (scm_c_num_port_types, scm_c_port_type_ref)
(scm_c_port_type_add_x, scm_make_port_type): Protect scm_ptobs access
with a mutex. Have it be an array of pointers instead of an array of
structures. Adapt users to the new APIs.
(scm_c_make_port_with_encoding): Allocate ports with three words. The
third word is the ptob descriptor.
* libguile/backtrace.c:
* libguile/goops.c:
* libguile/ioext.c:
* libguile/print.c: Adapt to use scm_c_port_type_ref and
SCM_PORT_DESCRIPTOR.
* libguile/ports.h (scm_grow_port_cbuf, scm_pt_size, scm_pt_member):
Remove declarations of unimplemented functions. Move a couple of
other definitions around.
* libguile/ports.h (scm_c_lock_port, scm_c_try_lock_port)
(scm_c_unlock_port): New inline functions.
(scm_t_port): Add a lock field, if threads are enabled. This is a
first step towards threadsafe ports.
* libguile/ports.c (scm_c_make_port_with_encoding): Init the port's
lock.
* libguile/inline.c: Residualize the inline functions from ports.h.
* libguile/ports.c (scm_c_make_port_with_encoding, scm_c_make_port): New
functions, to replace scm_new_port_table_entry. Use a weak set
instead of a weak table.
(scm_i_remove_port):
(scm_c_port_for_each, scm_port_for_each): Adapt to use weak set.
(scm_i_void_port): Use scm_c_make_port.
(scm_init_ports): Make a weak set.
* libguile/fports.c:
* libguile/ioext.c:
* libguile/r6rs-ports.c:
* libguile/strports.c:
* libguile/vports.c: Adapt to use the new scm_c_make_port API.
* libguile/async.c:
* libguile/async.h:
* libguile/debug.h:
* libguile/deprecated.c:
* libguile/deprecated.h:
* libguile/evalext.h:
* libguile/gc-malloc.c:
* libguile/gc.h:
* libguile/gen-scmconfig.c:
* libguile/numbers.c:
* libguile/ports.c:
* libguile/ports.h:
* libguile/procprop.c:
* libguile/procprop.h:
* libguile/read.c:
* libguile/socket.c:
* libguile/srfi-4.h:
* libguile/strings.c:
* libguile/strings.h:
* libguile/tags.h:
* module/ice-9/boot-9.scm:
* module/ice-9/deprecated.scm: Remove all deprecated code. CPP defines
that were not previously issuing warnings were changed so that their
expansions would indicate the replacement forms to use,
e.g. scm_sizet__GONE__REPLACE_WITH__size_t.
The two exceptions were SCM_LISTN, which did not produce warnings
before, and the string-filter argument order stuff.
Drops the initial dirty memory usage of Guile down to 2.8 MB on my
machine, from 4.4 MB.
* libguile/ports.h (scm_i_remove_port): Remove declaration, as it was
SCM_INTERNAL.
* libguile/ports.c (scm_add_to_port_table): Issue a deprecation
warning if this function is called. Remove needless SCM_API
declaration, it was already declared as such in ports.h. Safely
access the port table.
(scm_i_remove_port): Remove bogus comment about lack of need for
threadsafety. Take the port table mutex.
(scm_close_port): No need to take port table mutex around calling
scm_i_remove_port.
* libguile/ports.c (scm_i_set_default_port_encoding,
scm_i_default_port_encoding): New function. Replace
`scm_i_set_port_encoding_x' and `scm_i_get_port_encoding' with
PORT == SCM_BOOL_F.
(scm_i_set_port_encoding_x): Assume PORT is a port.
(scm_i_get_port_encoding): Remove.
(scm_port_encoding): Adjust accordingly.
(scm_new_port_table_entry): Use `scm_i_default_port_encoding'.
* libguile/ports.h (scm_i_get_port_encoding): Remove declarations.
(scm_i_default_port_encoding, scm_i_set_default_port_encoding): New
declarations.
* libguile/posix.c (setlocale): Use `scm_i_set_default_port_encoding'.
Thanks to Bruno Haible for his suggestions. See
<http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00007.html>,
for details.
* libguile/ports.c (register_finalizer_for_port): Always register a
finalizer for PORT.
(finalize_port): Close ENTRY->input_cd and ENTRY->output_cd.
(scm_new_port_table_entry): Initialize the `input_cd' and `output_cd'
fields.
(utf8_to_codepoint): New function.
(get_codepoint): Rewrite to use `iconv' instead of libunistring.
(scm_i_set_port_encoding_x): Initialize the `input_cd' and `output_cd'
fields.
(update_port_lf): Move upward. Use `switch' instead of `if's.
* libguile/ports.h (scm_t_port)[input_cd, output_cd]: New fields.
* libguile/print.c (codepoint_to_utf8, display_string): New functions.
(display_character): Use `display_string'.
(write_combining_character): Likewise.
(iprin1): Use `display_string' instead of `scm_lfwrite_str', and
`display_character' instead of `scm_putc'.
(write_character): Likewise.
(write_character_escaped): New function.
* test-suite/tests/encoding-escapes.test ("display output
escapes")["Rashomon"]: Use lower-case escapes.
["fake escape"]: New test.