1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +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:
Ludovic Courtès 2009-11-14 16:27:28 +01:00
parent d6a6989e08
commit f8a1c9a859
3 changed files with 19 additions and 24 deletions

View file

@ -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;

View file

@ -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

View file

@ -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,