Re-factor plugin loading

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2018-02-08 21:14:08 +01:00
parent b1d9862b0e
commit 821dc79117

View file

@ -93,45 +93,28 @@ check_suffix(const char* path)
return false; return false;
} }
void static void
zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager) load_plugin(zathura_plugin_manager_t* plugin_manager, const char* plugindir, const char* name)
{ {
if (plugin_manager == NULL || plugin_manager->path == NULL) {
return;
}
GIRARA_LIST_FOREACH_BODY_WITH_ITER(plugin_manager->path, char*, iter, plugindir,
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
girara_list_iterator_next(iter);
continue;
}
char* name = NULL;
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
char* path = g_build_filename(plugindir, name, NULL); char* path = g_build_filename(plugindir, name, NULL);
if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) { if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) {
girara_debug("%s is not a regular file. Skipping.", path); girara_debug("%s is not a regular file. Skipping.", path);
g_free(path); g_free(path);
continue; return;
} }
if (check_suffix(path) == false) { if (check_suffix(path) == false) {
girara_debug("%s is not a plugin file. Skipping.", path); girara_debug("%s is not a plugin file. Skipping.", path);
g_free(path); g_free(path);
continue; return;
} }
zathura_plugin_t* plugin = NULL;
/* load plugin */ /* load plugin */
GModule* handle = g_module_open(path, G_MODULE_BIND_LOCAL); GModule* handle = g_module_open(path, G_MODULE_BIND_LOCAL);
if (handle == NULL) { if (handle == NULL) {
girara_error("could not load plugin %s (%s)", path, g_module_error()); girara_error("could not load plugin %s (%s)", path, g_module_error());
g_free(path); g_free(path);
continue; return;
} }
/* resolve symbols and check API and ABI version*/ /* resolve symbols and check API and ABI version*/
@ -141,33 +124,31 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
girara_error("could not find '%s' in plugin %s - is not a plugin or needs to be rebuilt", G_STRINGIFY(ZATHURA_PLUGIN_DEFINITION_SYMBOL), path); girara_error("could not find '%s' in plugin %s - is not a plugin or needs to be rebuilt", G_STRINGIFY(ZATHURA_PLUGIN_DEFINITION_SYMBOL), path);
g_free(path); g_free(path);
g_module_close(handle); g_module_close(handle);
continue; return;
} }
/* check name */ /* check name */
if (plugin_definition->name == NULL) { if (plugin_definition->name == NULL) {
girara_error("plugin has no name"); girara_error("plugin has no name");
g_free(path); g_free(path);
g_free(plugin);
g_module_close(handle); g_module_close(handle);
continue; return;
} }
/* check mime type */ /* check mime type */
if (plugin_definition->mime_types == NULL || plugin_definition->mime_types_size == 0) { if (plugin_definition->mime_types == NULL || plugin_definition->mime_types_size == 0) {
girara_error("plugin has no mime_types"); girara_error("plugin has no mime_types");
g_free(path); g_free(path);
g_free(plugin);
g_module_close(handle); g_module_close(handle);
continue; return;
} }
plugin = g_try_malloc0(sizeof(zathura_plugin_t)); zathura_plugin_t* plugin = g_try_malloc0(sizeof(zathura_plugin_t));
if (plugin == NULL) { if (plugin == NULL) {
girara_error("Failed to allocate memory for plugin."); girara_error("Failed to allocate memory for plugin.");
g_free(path); g_free(path);
g_module_close(handle); g_module_close(handle);
continue; return;
} }
plugin->definition = plugin_definition; plugin->definition = plugin_definition;
@ -192,6 +173,27 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
plugin_definition->version.rev); plugin_definition->version.rev);
} }
} }
void
zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
{
if (plugin_manager == NULL || plugin_manager->path == NULL) {
return;
}
GIRARA_LIST_FOREACH_BODY_WITH_ITER(plugin_manager->path, char*, iter, plugindir,
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
girara_list_iterator_next(iter);
continue;
}
char* name = NULL;
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
load_plugin(plugin_manager, plugindir, name);
}
g_dir_close(dir); g_dir_close(dir);
); );
} }