Fix:maptool:Another name for faroe islands
[navit-package] / navit / plugin.c
index 8650a61..991eb50 100644 (file)
 #include <glib.h>
 #include "config.h"
 #ifdef USE_PLUGINS
+#ifdef HAVE_GMODULE
 #include <gmodule.h>
+#else
+#include <dlfcn.h>
+#endif
 #endif
 #include "plugin.h"
 #include "file.h"
 #include "item.h"
 #include "debug.h"
 
+#ifdef USE_PLUGINS
+#ifndef HAVE_GMODULE
+typedef void * GModule;
+#define G_MODULE_BIND_LOCAL 1
+#define G_MODULE_BIND_LAZY 2
+static int
+g_module_supported(void)
+{
+       return 1;
+}
+
+static void *
+g_module_open(char *name, int flags)
+{
+       return dlopen(name,
+               (flags & G_MODULE_BIND_LAZY ? RTLD_LAZY : RTLD_NOW) |
+               (flags & G_MODULE_BIND_LOCAL ? RTLD_LOCAL : RTLD_GLOBAL));
+}
+
+static char *
+g_module_error(void)
+{
+       return dlerror();
+}
+
+static int
+g_module_symbol(GModule *handle, char *symbol, gpointer *addr)
+{
+       *addr=dlsym(handle, symbol);
+       return (*addr != NULL);
+}
+
+static void
+g_module_close(GModule *handle)
+{
+       dlclose(handle);
+}
+
+#endif
+#endif
+
 struct plugin {
        int active;
        int lazy;
@@ -46,13 +91,13 @@ struct plugins {
        GList *list;
 } *pls;
 
-struct plugin *
-plugin_new(char *plugin)
+static struct plugin *
+plugin_new_from_path(char *plugin)
 {
 #ifdef USE_PLUGINS
        struct plugin *ret;
        if (! g_module_supported()) {
-               return NULL;
+              return NULL;
        }
        ret=g_new0(struct plugin, 1);
        ret->name=g_strdup(plugin);
@@ -161,9 +206,9 @@ plugins_new(void)
        return ret;
 }
 
-void
-plugins_add_path(struct plugins *pls, struct attr **attrs) {
-#ifdef USE_PLUGINS 
+struct plugin *
+plugin_new(struct attr *parent, struct attr **attrs) {
+#ifdef USE_PLUGINS
        struct attr *path_attr, *attr;
        struct file_wordexp *we;
        int active=1; // default active
@@ -171,11 +216,15 @@ plugins_add_path(struct plugins *pls, struct attr **attrs) {
        int i, count;
        char **array;
        char *name;
-       struct plugin *pl;
+       struct plugin *pl=NULL;
+       struct plugins *pls=NULL;
+
+       if (parent)
+               pls=parent->u.plugins;
 
        if (! (path_attr=attr_search(attrs, NULL, attr_path))) {
                dbg(0,"missing path\n");
-               return;
+               return NULL;
        }
        if ( (attr=attr_search(attrs, NULL, attr_active))) {
                active=attr->u.num;
@@ -191,25 +240,40 @@ plugins_add_path(struct plugins *pls, struct attr **attrs) {
        we=file_wordexp_new(path_attr->u.str);
        count=file_wordexp_get_count(we);
        array=file_wordexp_get_array(we);       
-       for (i = 0 ; i < count ; i++) {
-               name=array[i];
-               if (! (pl=g_hash_table_lookup(pls->hash, name))) {
-                       pl=plugin_new(name);
-                       if (! pl) {
-                               dbg(0,"failed to create plugin '%s'\n", name);
-                               continue;
+       dbg(2,"expanded to %d words\n",count);
+       if (count != 1 || file_exists(array[0])) {
+               for (i = 0 ; i < count ; i++) {
+                       name=array[i];
+                       dbg(2,"name[%d]='%s'\n", i, name);
+                       if (! (pls && (pl=g_hash_table_lookup(pls->hash, name)))) {
+                               pl=plugin_new_from_path(name);
+                               if (! pl) {
+                                       dbg(0,"failed to create plugin '%s'\n", name);
+                                       continue;
+                               }
+                               if (pls) {
+                                       g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
+                                       pls->list=g_list_append(pls->list, pl);
+                               }
+                       } else {
+                               if (pls) {
+                                       pls->list=g_list_remove(pls->list, pl);
+                                       pls->list=g_list_append(pls->list, pl);
+                               }
+                       }
+                       plugin_set_active(pl, active);
+                       plugin_set_lazy(pl, lazy);
+                       plugin_set_ondemand(pl, ondemand);
+                       if (!pls && active) {
+                               if (!plugin_load(pl)) 
+                                       plugin_set_active(pl, 0);
+                               else
+                                       plugin_call_init(pl);
                        }
-                       g_hash_table_insert(pls->hash, plugin_get_name(pl), pl);
-                       pls->list=g_list_append(pls->list, pl);
-               } else {
-                       pls->list=g_list_remove(pls->list, pl);
-                       pls->list=g_list_append(pls->list, pl);
                }
-               plugin_set_active(pl, active);
-               plugin_set_lazy(pl, lazy);
-               plugin_set_ondemand(pl, ondemand);
+               file_wordexp_destroy(we);
        }
-       file_wordexp_destroy(we);
+       return pl;
 #endif
 }
 
@@ -260,8 +324,6 @@ plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
        struct name_val *nv;
        struct plugin *pl;
        char *mod_name, *filename=NULL, *corename=NULL;
-       if (!pls)
-               return NULL;
        l=plugin_types[type];
        while (l) {
                nv=l->data;
@@ -269,9 +331,9 @@ plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
                        return nv->val;
                l=g_list_next(l);
        }
+       if (!pls)
+               return NULL;
        lpls=pls->list;
-       if(!g_ascii_strcasecmp(type_name, "map"))
-               type_name="data";
        filename=g_strjoin("", "lib", type_name, "_", name, NULL);
        corename=g_strjoin("", "lib", type_name, "_", "core", NULL);
        while (lpls) {
@@ -280,8 +342,9 @@ plugin_get_type(enum plugin_type type, const char *type_name, const char *name)
                        mod_name++;
                else
                        mod_name=pl->name;
-               if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(filename))) {
-                       dbg(0, "Loading module \"%s\"\n",pl->name) ;
+               dbg(2,"compare '%s' with '%s'\n", mod_name, filename);
+               if (!g_ascii_strncasecmp(mod_name, filename, strlen(filename)) || !g_ascii_strncasecmp(mod_name, corename, strlen(corename))) {
+                       dbg(1, "Loading module \"%s\"\n",pl->name) ;
                        if (plugin_get_active(pl)) 
                                if (!plugin_load(pl)) 
                                        plugin_set_active(pl, 0);