summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2009-11-14 16:27:28 +0100
committerLudovic Courtès <ludo@gnu.org>2009-11-14 16:59:25 +0100
commitf8a1c9a8594d5c64cd8b0dfada16ece6dcfe13f4 (patch)
treecb443399593c196d3baa0e0ba807cc2b3305dc98
parentd6a6989e08a84cbf3c6b11c199536f665cbb6b50 (diff)
downloadguile-f8a1c9a8594d5c64cd8b0dfada16ece6dcfe13f4.tar.gz
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 ()'.
-rw-r--r--libguile/load.c11
-rw-r--r--libguile/read.c28
-rw-r--r--libguile/read.h4
3 files changed, 19 insertions, 24 deletions
diff --git a/libguile/load.c b/libguile/load.c
index c6fbf5f1b..5c0c61e20 100644
--- a/libguile/load.c
+++ b/libguile/load.c
@@ -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_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
scm_i_dynwind_current_load_port (port);
- encoding = scm_scan_for_encoding (port);
+
+ encoding = scm_i_scan_for_encoding (port);
if (encoding)
- {
- scm_i_set_port_encoding_x (port, encoding);
- free (encoding);
- }
+ scm_i_set_port_encoding_x (port, encoding);
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);
+
while (1)
{
SCM reader, form;
diff --git a/libguile/read.c b/libguile/read.c
index fc56418df..e403cc302 100644
--- a/libguile/read.c
+++ b/libguile/read.c
@@ -1460,10 +1460,11 @@ scm_get_hash_procedure (int c)
#define SCM_ENCODING_SEARCH_SIZE (500)
-/* Search the first few hundred characters of a file for
- an emacs-like coding declaration. */
+/* Search the first few hundred characters of a file for an Emacs-like coding
+ declaration. Returns either NULL or a string whose storage has been
+ allocated with `scm_gc_malloc ()'. */
char *
-scm_scan_for_encoding (SCM port)
+scm_i_scan_for_encoding (SCM port)
{
char header[SCM_ENCODING_SEARCH_SIZE+1];
size_t bytes_read;
@@ -1512,9 +1513,7 @@ scm_scan_for_encoding (SCM port)
if (i == 0)
return NULL;
- encoding = scm_malloc (i+1);
- memcpy (encoding, pos, i);
- encoding[i] ='\0';
+ encoding = scm_gc_strndup (pos, i + 1, "encoding");
for (i = 0; i < strlen (encoding); i++)
encoding[i] = toupper ((int) encoding[i]);
@@ -1540,16 +1539,14 @@ scm_scan_for_encoding (SCM port)
i ++;
}
if (!in_comment)
- {
- /* This wasn't in a comment */
- free (encoding);
- return NULL;
- }
+ /* This wasn't in a comment */
+ return NULL;
+
if (utf8_bom && strcmp(encoding, "UTF-8"))
scm_misc_error (NULL,
"the port input declares the encoding ~s but is encoded as UTF-8",
scm_list_1 (scm_from_locale_string (encoding)));
-
+
return encoding;
}
@@ -1566,17 +1563,16 @@ SCM_DEFINE (scm_file_encoding, "file-encoding", 1, 0, 0,
{
char *enc;
SCM s_enc;
-
- enc = scm_scan_for_encoding (port);
+
+ enc = scm_i_scan_for_encoding (port);
if (enc == NULL)
return SCM_BOOL_F;
else
{
s_enc = scm_from_locale_string (enc);
- free (enc);
return s_enc;
}
-
+
return SCM_BOOL_F;
}
#undef FUNC_NAME
diff --git a/libguile/read.h b/libguile/read.h
index 7bc4a0ba4..4bd08fa44 100644
--- a/libguile/read.h
+++ b/libguile/read.h
@@ -3,7 +3,7 @@
#ifndef 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
* 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 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_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_INTERNAL void scm_i_input_error (const char *func, SCM port,