diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-07-10 19:58:55 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-07-10 20:39:13 +0300 |
commit | 218818d2366c1ed01b4ded1b12faf299eaf93859 (patch) | |
tree | 3bc649b77cdef2dddf47e057c1cb1a474a1114d6 /src | |
parent | e906e318c552d89fdd7d56045cc0fe15a5853547 (diff) | |
download | idest-218818d2366c1ed01b4ded1b12faf299eaf93859.tar.gz idest-218818d2366c1ed01b4ded1b12faf299eaf93859.tar.bz2 |
Search for Guile files in the %load-path. Implement startup files.
* NEWS: Document Guile startup files.
* src/Makefile.am (AM_CPPFLAGS): Define GUILE_SITE and PKG_SITE.
* src/guile.c (guile_load): Second argument controls whether to
search for the file in the %load-path.
(load_path_prepend,try_file)
(load_startup_file): New functions.
(guile_init): Load startup files.
Unless guile_script contains directory separators, look it up
in the %load-path.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/guile.c | 85 |
2 files changed, 85 insertions, 4 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 1d51cfa..ed125b6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -39,3 +39,7 @@ DISTCLEANFILES= sitedir = @GUILE_SITE@/$(PACKAGE) site_DATA= include ../gint/gint.mk + +AM_CPPFLAGS=\ + -DGUILE_SITE=\"@GUILE_SITE@\"\ + -DPKG_SITE=\"$(sitedir)\"
\ No newline at end of file diff --git a/src/guile.c b/src/guile.c index cd95024..4e05886 100644 --- a/src/guile.c +++ b/src/guile.c @@ -95,8 +95,18 @@ load_handler(void *data) return SCM_UNDEFINED; } +static SCM +load_handler_path(void *data) +{ + struct load_closure *lp = data; + + scm_set_program_arguments(lp->argc, lp->argv, lp->filename); + scm_primitive_load_path(scm_from_locale_string(lp->filename)); + return SCM_UNDEFINED; +} + static int -guile_load(char *filename) +guile_load(char *filename, int use_path) { struct load_closure lc; char *argv[2]; @@ -105,7 +115,8 @@ guile_load(char *filename) lc.argc = 1; lc.argv = argv; lc.filename = filename; - return guile_safe_exec(load_handler, &lc, NULL); + return guile_safe_exec(use_path ? load_handler_path : load_handler, + &lc, NULL); } static SCM @@ -329,6 +340,71 @@ guile_list(const char *file, struct id3_tag *tag) return 1; } +static void +load_path_prepend(const char *dir) +{ + SCM scm, path_scm; + SCM *pscm; + + path_scm = SCM_VARIABLE_REF(scm_c_lookup("%load-path")); + for (scm = path_scm; !scm_is_null(scm); scm = SCM_CDR(scm)) { + SCM val = SCM_CAR(scm); + if (scm_is_string(val)) { + char *s = scm_to_locale_string(val); + int rc = strcmp(s, dir); + free(s); + if (rc == 0) + return; + } + } + + pscm = SCM_VARIABLE_LOC(scm_c_lookup("%load-path")); + *pscm = scm_cons(scm_from_locale_string(dir), path_scm); +} + +static int +try_file(const char *dir, const char *file_name) +{ + char *ptr; + size_t len = strlen(dir); + if (len > 0 && dir[len - 1] == '/') + len--; + ptr = xmalloc(len + 1 + strlen(file_name) + 1); + memcpy(ptr, dir, len); + ptr[len++] = '/'; + strcpy(ptr + len, file_name); + + if (access(ptr, R_OK) == 0) { + if (guile_load(ptr, 0)) + error(1, 0, "cannot load startup script %s", ptr); + return 0; + } + free(ptr); + return 1; +} + +static void +load_startup_file() +{ + const char init_name[] = ".idest.scm"; + const char *dir; + + load_path_prepend(GUILE_SITE); + load_path_prepend(PKG_SITE); + load_path_prepend("."); + + if (access(init_name, R_OK) == 0) { + if (guile_load((char*)init_name, 0) == 0) + return; + error(1, 0, "cannot load startup script %s", init_name); + } + + dir = getenv("HOME"); + if (dir && try_file(dir, init_name) == 0) + return; + try_file(PKG_SITE, "idest.scm"); +} + void guile_init() { @@ -350,8 +426,9 @@ guile_init() #endif } - if (guile_load(guile_script)) - error(1, 0, "cannot load init script %s", guile_script); + load_startup_file(); + + guile_load(guile_script, !strchr(guile_script, '/')); proc = SCM_VARIABLE_REF(sym_idest_main); if (proc == SCM_EOL) { |