mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-02 13:00:26 +02:00
Have `scm_scan_for_encoding ()' use GC-managed memory.
* libguile/read.c (scm_scan_for_encoding): Rename to ... (scm_i_scan_for_encoding): ... this; update callers. Use `scm_gc_strndup ()' instead of `scm_malloc ()'. * libguile/read.h: Update accordingly. * libguile/load.c (scm_primitive_load): Don't call free(3) on the value returned by `scm_i_scan_for_encoding ()'.
This commit is contained in:
parent
d6a6989e08
commit
f8a1c9a859
3 changed files with 19 additions and 24 deletions
|
@ -98,15 +98,14 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
|
||||||
SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
|
SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
|
||||||
scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
|
scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
|
||||||
scm_i_dynwind_current_load_port (port);
|
scm_i_dynwind_current_load_port (port);
|
||||||
encoding = scm_scan_for_encoding (port);
|
|
||||||
|
encoding = scm_i_scan_for_encoding (port);
|
||||||
if (encoding)
|
if (encoding)
|
||||||
{
|
scm_i_set_port_encoding_x (port, encoding);
|
||||||
scm_i_set_port_encoding_x (port, encoding);
|
|
||||||
free (encoding);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
/* The file has no encoding declaraed. We'll presume Latin-1. */
|
/* The file has no encoding declared. We'll presume Latin-1. */
|
||||||
scm_i_set_port_encoding_x (port, NULL);
|
scm_i_set_port_encoding_x (port, NULL);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
SCM reader, form;
|
SCM reader, form;
|
||||||
|
|
|
@ -1460,10 +1460,11 @@ scm_get_hash_procedure (int c)
|
||||||
|
|
||||||
#define SCM_ENCODING_SEARCH_SIZE (500)
|
#define SCM_ENCODING_SEARCH_SIZE (500)
|
||||||
|
|
||||||
/* Search the first few hundred characters of a file for
|
/* Search the first few hundred characters of a file for an Emacs-like coding
|
||||||
an emacs-like coding declaration. */
|
declaration. Returns either NULL or a string whose storage has been
|
||||||
|
allocated with `scm_gc_malloc ()'. */
|
||||||
char *
|
char *
|
||||||
scm_scan_for_encoding (SCM port)
|
scm_i_scan_for_encoding (SCM port)
|
||||||
{
|
{
|
||||||
char header[SCM_ENCODING_SEARCH_SIZE+1];
|
char header[SCM_ENCODING_SEARCH_SIZE+1];
|
||||||
size_t bytes_read;
|
size_t bytes_read;
|
||||||
|
@ -1512,9 +1513,7 @@ scm_scan_for_encoding (SCM port)
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
encoding = scm_malloc (i+1);
|
encoding = scm_gc_strndup (pos, i + 1, "encoding");
|
||||||
memcpy (encoding, pos, i);
|
|
||||||
encoding[i] ='\0';
|
|
||||||
for (i = 0; i < strlen (encoding); i++)
|
for (i = 0; i < strlen (encoding); i++)
|
||||||
encoding[i] = toupper ((int) encoding[i]);
|
encoding[i] = toupper ((int) encoding[i]);
|
||||||
|
|
||||||
|
@ -1540,16 +1539,14 @@ scm_scan_for_encoding (SCM port)
|
||||||
i ++;
|
i ++;
|
||||||
}
|
}
|
||||||
if (!in_comment)
|
if (!in_comment)
|
||||||
{
|
/* This wasn't in a comment */
|
||||||
/* This wasn't in a comment */
|
return NULL;
|
||||||
free (encoding);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (utf8_bom && strcmp(encoding, "UTF-8"))
|
if (utf8_bom && strcmp(encoding, "UTF-8"))
|
||||||
scm_misc_error (NULL,
|
scm_misc_error (NULL,
|
||||||
"the port input declares the encoding ~s but is encoded as UTF-8",
|
"the port input declares the encoding ~s but is encoded as UTF-8",
|
||||||
scm_list_1 (scm_from_locale_string (encoding)));
|
scm_list_1 (scm_from_locale_string (encoding)));
|
||||||
|
|
||||||
return encoding;
|
return encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1566,17 +1563,16 @@ SCM_DEFINE (scm_file_encoding, "file-encoding", 1, 0, 0,
|
||||||
{
|
{
|
||||||
char *enc;
|
char *enc;
|
||||||
SCM s_enc;
|
SCM s_enc;
|
||||||
|
|
||||||
enc = scm_scan_for_encoding (port);
|
enc = scm_i_scan_for_encoding (port);
|
||||||
if (enc == NULL)
|
if (enc == NULL)
|
||||||
return SCM_BOOL_F;
|
return SCM_BOOL_F;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
s_enc = scm_from_locale_string (enc);
|
s_enc = scm_from_locale_string (enc);
|
||||||
free (enc);
|
|
||||||
return s_enc;
|
return s_enc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return SCM_BOOL_F;
|
return SCM_BOOL_F;
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#ifndef SCM_READ_H
|
#ifndef SCM_READ_H
|
||||||
#define SCM_READ_H
|
#define SCM_READ_H
|
||||||
|
|
||||||
/* Copyright (C) 1995,1996,2000, 2006, 2008 Free Software Foundation, Inc.
|
/* Copyright (C) 1995,1996,2000, 2006, 2008, 2009 Free Software Foundation, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public License
|
* modify it under the terms of the GNU Lesser General Public License
|
||||||
|
@ -56,7 +56,7 @@ SCM_API SCM scm_read_options (SCM setting);
|
||||||
SCM_API SCM scm_read (SCM port);
|
SCM_API SCM scm_read (SCM port);
|
||||||
SCM_API size_t scm_read_token (int ic, SCM * tok_buf, SCM port, int weird);
|
SCM_API size_t scm_read_token (int ic, SCM * tok_buf, SCM port, int weird);
|
||||||
SCM_API SCM scm_read_hash_extend (SCM chr, SCM proc);
|
SCM_API SCM scm_read_hash_extend (SCM chr, SCM proc);
|
||||||
SCM_INTERNAL char *scm_scan_for_encoding (SCM port);
|
SCM_INTERNAL char *scm_i_scan_for_encoding (SCM port);
|
||||||
SCM_API SCM scm_file_encoding (SCM port);
|
SCM_API SCM scm_file_encoding (SCM port);
|
||||||
|
|
||||||
SCM_INTERNAL void scm_i_input_error (const char *func, SCM port,
|
SCM_INTERNAL void scm_i_input_error (const char *func, SCM port,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue