mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-12 06:41:13 +02:00
(scm_internal_parse_path): Removed.
(scm_parse_path): Use scm_string_split to do the work. (scm_init_load_path): Use scm_parse_path instead of scm_internal_parse_path. (scm_search_path): Rewritten string handling part of the code in terms of scm_to_locale_stringbuf and so that it is thread safe.
This commit is contained in:
parent
c829a4274f
commit
7d04d68bf7
2 changed files with 151 additions and 128 deletions
278
libguile/load.c
278
libguile/load.c
|
@ -23,6 +23,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "libguile/_scm.h"
|
#include "libguile/_scm.h"
|
||||||
#include "libguile/libpath.h"
|
#include "libguile/libpath.h"
|
||||||
|
@ -36,6 +37,8 @@
|
||||||
#include "libguile/strings.h"
|
#include "libguile/strings.h"
|
||||||
#include "libguile/modules.h"
|
#include "libguile/modules.h"
|
||||||
#include "libguile/lang.h"
|
#include "libguile/lang.h"
|
||||||
|
#include "libguile/chars.h"
|
||||||
|
#include "libguile/strop.h"
|
||||||
|
|
||||||
#include "libguile/validate.h"
|
#include "libguile/validate.h"
|
||||||
#include "libguile/load.h"
|
#include "libguile/load.h"
|
||||||
|
@ -172,37 +175,6 @@ static SCM *scm_loc_load_path;
|
||||||
static SCM *scm_loc_load_extensions;
|
static SCM *scm_loc_load_extensions;
|
||||||
|
|
||||||
|
|
||||||
/* Parse the null-terminated string PATH as if it were a standard path
|
|
||||||
environment variable (i.e. a colon-separated list of strings), and
|
|
||||||
prepend the elements to TAIL. */
|
|
||||||
SCM
|
|
||||||
scm_internal_parse_path (char *path, SCM tail)
|
|
||||||
{
|
|
||||||
if (path && path[0] != '\0')
|
|
||||||
{
|
|
||||||
char *scan, *elt_end;
|
|
||||||
|
|
||||||
/* Scan backwards from the end of the string, to help
|
|
||||||
construct the list in the right order. */
|
|
||||||
scan = elt_end = path + strlen (path);
|
|
||||||
do {
|
|
||||||
/* Scan back to the beginning of the current element. */
|
|
||||||
do scan--;
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
while (scan >= path && *scan != ';');
|
|
||||||
#else
|
|
||||||
while (scan >= path && *scan != ':');
|
|
||||||
#endif
|
|
||||||
tail = scm_cons (scm_mem2string (scan + 1, elt_end - (scan + 1)),
|
|
||||||
tail);
|
|
||||||
elt_end = scan;
|
|
||||||
} while (scan >= path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tail;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
|
SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
|
||||||
(SCM path, SCM tail),
|
(SCM path, SCM tail),
|
||||||
"Parse @var{path}, which is expected to be a colon-separated\n"
|
"Parse @var{path}, which is expected to be a colon-separated\n"
|
||||||
|
@ -211,14 +183,17 @@ SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
|
||||||
"is returned.")
|
"is returned.")
|
||||||
#define FUNC_NAME s_scm_parse_path
|
#define FUNC_NAME s_scm_parse_path
|
||||||
{
|
{
|
||||||
SCM_ASSERT (scm_is_false (path) || (SCM_STRINGP (path)),
|
#ifdef __MINGW32__
|
||||||
path,
|
SCM sep = SCM_MAKE_CHAR (';');
|
||||||
SCM_ARG1, FUNC_NAME);
|
#else
|
||||||
|
SCM sep = SCM_MAKE_CHAR (':');
|
||||||
|
#endif
|
||||||
|
|
||||||
if (SCM_UNBNDP (tail))
|
if (SCM_UNBNDP (tail))
|
||||||
tail = SCM_EOL;
|
tail = SCM_EOL;
|
||||||
return (scm_is_false (path)
|
return (scm_is_false (path)
|
||||||
? tail
|
? tail
|
||||||
: scm_internal_parse_path (SCM_STRING_CHARS (path), tail));
|
: scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
|
@ -237,13 +212,86 @@ scm_init_load_path ()
|
||||||
scm_makfrom0str (SCM_PKGDATA_DIR));
|
scm_makfrom0str (SCM_PKGDATA_DIR));
|
||||||
#endif /* SCM_LIBRARY_DIR */
|
#endif /* SCM_LIBRARY_DIR */
|
||||||
|
|
||||||
path = scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path);
|
path = scm_parse_path (scm_from_locale_string (getenv ("GUILE_LOAD_PATH")),
|
||||||
|
path);
|
||||||
|
|
||||||
*scm_loc_load_path = path;
|
*scm_loc_load_path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
SCM scm_listofnullstr;
|
SCM scm_listofnullstr;
|
||||||
|
|
||||||
|
/* Utility functions for assembling C strings in a buffer.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct stringbuf {
|
||||||
|
char *buf, *ptr;
|
||||||
|
size_t buf_len;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
stringbuf_free (void *data)
|
||||||
|
{
|
||||||
|
struct stringbuf *buf = (struct stringbuf *)data;
|
||||||
|
free (buf->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
stringbuf_grow (struct stringbuf *buf)
|
||||||
|
{
|
||||||
|
size_t ptroff = buf->ptr - buf->buf;
|
||||||
|
buf->buf_len *= 2;
|
||||||
|
// fprintf (stderr, "growing to %u\n", buf->buf_len);
|
||||||
|
buf->buf = scm_realloc (buf->buf, buf->buf_len);
|
||||||
|
buf->ptr = buf->buf + ptroff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
|
||||||
|
{
|
||||||
|
size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
|
||||||
|
size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
|
||||||
|
if (len > max_len)
|
||||||
|
{
|
||||||
|
/* buffer is too small, double its size and try again.
|
||||||
|
*/
|
||||||
|
stringbuf_grow (buf);
|
||||||
|
stringbuf_cat_locale_string (buf, str);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* string fits, terminate it and check for embedded '\0'.
|
||||||
|
*/
|
||||||
|
buf->ptr[len] = '\0';
|
||||||
|
if (strlen (buf->ptr) != len)
|
||||||
|
scm_misc_error (NULL,
|
||||||
|
"string contains #\\nul character: ~S",
|
||||||
|
scm_list_1 (str));
|
||||||
|
buf->ptr += len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
stringbuf_cat (struct stringbuf *buf, char *str)
|
||||||
|
{
|
||||||
|
size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
|
||||||
|
size_t len = strlen (str);
|
||||||
|
if (len > max_len)
|
||||||
|
{
|
||||||
|
/* buffer is too small, double its size and try again.
|
||||||
|
*/
|
||||||
|
stringbuf_grow (buf);
|
||||||
|
stringbuf_cat (buf, str);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* string fits, copy it into buffer.
|
||||||
|
*/
|
||||||
|
strcpy (buf->ptr, str);
|
||||||
|
buf->ptr += len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Search PATH for a directory containing a file named FILENAME.
|
/* Search PATH for a directory containing a file named FILENAME.
|
||||||
The file must be readable, and not a directory.
|
The file must be readable, and not a directory.
|
||||||
If we find one, return its full filename; otherwise, return #f.
|
If we find one, return its full filename; otherwise, return #f.
|
||||||
|
@ -261,20 +309,19 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
|
||||||
"concatenated with each @var{extension}.")
|
"concatenated with each @var{extension}.")
|
||||||
#define FUNC_NAME s_scm_search_path
|
#define FUNC_NAME s_scm_search_path
|
||||||
{
|
{
|
||||||
|
struct stringbuf buf;
|
||||||
char *filename_chars;
|
char *filename_chars;
|
||||||
int filename_len;
|
size_t filename_len;
|
||||||
size_t max_path_len; /* maximum length of any PATH element */
|
SCM result = SCM_BOOL_F;
|
||||||
size_t max_ext_len; /* maximum length of any EXTENSIONS element */
|
|
||||||
|
|
||||||
SCM_VALIDATE_LIST (1, path);
|
|
||||||
SCM_VALIDATE_STRING (2, filename);
|
|
||||||
if (SCM_UNBNDP (extensions))
|
if (SCM_UNBNDP (extensions))
|
||||||
extensions = SCM_EOL;
|
extensions = SCM_EOL;
|
||||||
else
|
|
||||||
SCM_VALIDATE_LIST (3, extensions);
|
|
||||||
|
|
||||||
filename_chars = SCM_STRING_CHARS (filename);
|
scm_frame_begin (0);
|
||||||
filename_len = SCM_STRING_LENGTH (filename);
|
|
||||||
|
filename_chars = scm_to_locale_string (filename);
|
||||||
|
filename_len = strlen (filename_chars);
|
||||||
|
scm_frame_free (filename_chars);
|
||||||
|
|
||||||
/* If FILENAME is absolute, return it unchanged. */
|
/* If FILENAME is absolute, return it unchanged. */
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
|
@ -287,22 +334,10 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
|
||||||
#else
|
#else
|
||||||
if (filename_len >= 1 && filename_chars[0] == '/')
|
if (filename_len >= 1 && filename_chars[0] == '/')
|
||||||
#endif
|
#endif
|
||||||
return filename;
|
{
|
||||||
|
scm_frame_end ();
|
||||||
/* Find the length of the longest element of path. */
|
return filename;
|
||||||
{
|
}
|
||||||
SCM walk;
|
|
||||||
|
|
||||||
max_path_len = 0;
|
|
||||||
for (walk = path; !SCM_NULL_OR_NIL_P (walk); walk = SCM_CDR (walk))
|
|
||||||
{
|
|
||||||
SCM elt = SCM_CAR (walk);
|
|
||||||
SCM_ASSERT_TYPE (SCM_STRINGP (elt), path, 1, FUNC_NAME,
|
|
||||||
"list of strings");
|
|
||||||
if (SCM_STRING_LENGTH (elt) > max_path_len)
|
|
||||||
max_path_len = SCM_STRING_LENGTH (elt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
|
/* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
|
||||||
{
|
{
|
||||||
|
@ -330,81 +365,70 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find the length of the longest element of the load extensions
|
/* This simplifies the loop below a bit.
|
||||||
list. */
|
*/
|
||||||
{ /* scope */
|
if (SCM_NULLP (extensions))
|
||||||
SCM walk;
|
extensions = scm_listofnullstr;
|
||||||
|
|
||||||
max_ext_len = 0;
|
buf.buf_len = 512;
|
||||||
for (walk = extensions; !SCM_NULL_OR_NIL_P (walk); walk = SCM_CDR (walk))
|
buf.buf = scm_malloc (buf.buf_len);
|
||||||
{
|
scm_frame_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
|
||||||
SCM elt = SCM_CAR (walk);
|
|
||||||
SCM_ASSERT_TYPE (SCM_STRINGP (elt), elt, 3, FUNC_NAME,
|
|
||||||
"list of strings");
|
|
||||||
if (SCM_STRING_LENGTH (elt) > max_ext_len)
|
|
||||||
max_ext_len = SCM_STRING_LENGTH (elt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SCM_DEFER_INTS;
|
/* Try every path element.
|
||||||
|
*/
|
||||||
|
for (; SCM_CONSP (path); path = SCM_CDR (path))
|
||||||
|
{
|
||||||
|
SCM dir = SCM_CAR (path);
|
||||||
|
SCM exts;
|
||||||
|
size_t sans_ext_len;
|
||||||
|
|
||||||
{ /* scope */
|
buf.ptr = buf.buf;
|
||||||
SCM result = SCM_BOOL_F;
|
stringbuf_cat_locale_string (&buf, dir);
|
||||||
size_t buf_size = max_path_len + 1 + filename_len + max_ext_len + 1;
|
|
||||||
char *buf = scm_malloc (buf_size);
|
/* Concatenate the path name and the filename. */
|
||||||
|
|
||||||
/* This simplifies the loop below a bit. */
|
|
||||||
if (SCM_NULL_OR_NIL_P (extensions))
|
|
||||||
extensions = scm_listofnullstr;
|
|
||||||
|
|
||||||
/* Try every path element. At this point, we know the path is a
|
|
||||||
proper list of strings. */
|
|
||||||
for (; !SCM_NULL_OR_NIL_P (path); path = SCM_CDR (path))
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
SCM dir = SCM_CAR (path);
|
|
||||||
SCM exts;
|
|
||||||
|
|
||||||
/* Concatenate the path name and the filename. */
|
|
||||||
len = SCM_STRING_LENGTH (dir);
|
|
||||||
memcpy (buf, SCM_STRING_CHARS (dir), len);
|
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
if (len >= 1 && buf[len - 1] != '/' && buf[len - 1] != '\\')
|
if (buf.ptr > buf.buf && buf.ptr[-1] != '/' && buf.ptr[-1] != '\\')
|
||||||
#else
|
#else
|
||||||
if (len >= 1 && buf[len - 1] != '/')
|
if (buf.ptr > buf.buf >= 1 && buf.ptr[-1] != '/')
|
||||||
#endif
|
#endif
|
||||||
buf[len++] = '/';
|
stringbuf_cat (&buf, "/");
|
||||||
memcpy (buf + len, filename_chars, filename_len);
|
|
||||||
len += filename_len;
|
|
||||||
|
|
||||||
/* Try every extension. At this point, we know the extension
|
stringbuf_cat (&buf, filename_chars);
|
||||||
list is a proper, nonempty list of strings. */
|
sans_ext_len = buf.ptr - buf.buf;
|
||||||
for (exts = extensions; !SCM_NULL_OR_NIL_P (exts); exts = SCM_CDR (exts))
|
|
||||||
{
|
|
||||||
SCM ext = SCM_CAR (exts);
|
|
||||||
size_t ext_len = SCM_STRING_LENGTH (ext);
|
|
||||||
struct stat mode;
|
|
||||||
|
|
||||||
/* Concatenate the extension. */
|
/* Try every extension. */
|
||||||
memcpy (buf + len, SCM_STRING_CHARS (ext), ext_len);
|
for (exts = extensions; SCM_CONSP (exts); exts = SCM_CDR (exts))
|
||||||
buf[len + ext_len] = '\0';
|
{
|
||||||
|
SCM ext = SCM_CAR (exts);
|
||||||
/* If the file exists at all, we should return it. If the
|
struct stat mode;
|
||||||
file is inaccessible, then that's an error. */
|
|
||||||
if (stat (buf, &mode) == 0
|
buf.ptr = buf.buf + sans_ext_len;
|
||||||
&& ! (mode.st_mode & S_IFDIR))
|
stringbuf_cat_locale_string (&buf, ext);
|
||||||
{
|
|
||||||
result = scm_mem2string (buf, len + ext_len);
|
/* If the file exists at all, we should return it. If the
|
||||||
goto end;
|
file is inaccessible, then that's an error. */
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
end:
|
// fprintf (stderr, "trying: %s\n", buf.buf);
|
||||||
free (buf);
|
|
||||||
SCM_ALLOW_INTS;
|
if (stat (buf.buf, &mode) == 0
|
||||||
return result;
|
&& ! (mode.st_mode & S_IFDIR))
|
||||||
}
|
{
|
||||||
|
result = scm_from_locale_string (buf.buf);
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!SCM_NULL_OR_NIL_P (exts))
|
||||||
|
scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!SCM_NULL_OR_NIL_P (path))
|
||||||
|
scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
|
||||||
|
|
||||||
|
end:
|
||||||
|
scm_frame_end ();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
#include "libguile/__scm.h"
|
#include "libguile/__scm.h"
|
||||||
|
|
||||||
|
|
||||||
SCM_API SCM scm_internal_parse_path (char *path, SCM tail);
|
|
||||||
SCM_API SCM scm_parse_path (SCM path, SCM tail);
|
SCM_API SCM scm_parse_path (SCM path, SCM tail);
|
||||||
SCM_API void scm_init_load_path (void);
|
SCM_API void scm_init_load_path (void);
|
||||||
SCM_API SCM scm_primitive_load (SCM filename);
|
SCM_API SCM scm_primitive_load (SCM filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue