Commit dc660d2c authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Register plugin correctly and handle dlhandle correctly

parent d1cef30e
......@@ -33,41 +33,72 @@ zathura_document_plugins_load(void)
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
/* check if entry is a file */
if (entry->d_type == 0x8) {
/* get full path */
char* path = string_concat(PLUGIN_DIR, "/", entry->d_name, NULL);
if (entry->d_type != 0x8) {
continue;
}
if (path == NULL) {
continue;
}
void* handle = NULL;
zathura_document_plugin_t* plugin = NULL;
char* path = NULL;
/* load plugin */
void* handle = dlopen(path, RTLD_NOW);
free(path);
/* get full path */
path = string_concat(PLUGIN_DIR, "/", entry->d_name, NULL);
if (handle == NULL) {
fprintf(stderr, "error: could not load plugin (%s)\n", dlerror());
continue;
}
if (path == NULL) {
goto error_continue;
}
/* resolve symbol */
zathura_plugin_register_service_t register_plugin;
*(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
/* load plugin */
handle = dlopen(path, RTLD_NOW);
if (register_plugin == NULL) {
fprintf(stderr, "error: could not find '%s' function in the plugin\n", PLUGIN_REGISTER_FUNCTION);
dlclose(handle);
continue;
}
if (handle == NULL) {
fprintf(stderr, "error: could not load plugin (%s)\n", dlerror());
goto error_free;
}
bool r = register_plugin();
/* resolve symbol */
zathura_plugin_register_service_t register_plugin;
*(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
if (r == false) {
fprintf(stderr, "error: could not register plugin\n");
}
if (register_plugin == NULL) {
fprintf(stderr, "error: could not find '%s' function in the plugin\n", PLUGIN_REGISTER_FUNCTION);
goto error_free;
}
plugin = malloc(sizeof(zathura_document_plugin_t));
if (plugin == NULL) {
goto error_free;
}
plugin->file_extension = NULL;
plugin->open_function = NULL;
register_plugin(plugin);
bool r = zathura_document_plugin_register(plugin, handle);
/*dlclose(handle);*/ // FIXME
if (r == false) {
fprintf(stderr, "error: could not register plugin (%s)\n", path);
goto error_free;
}
free(path);
continue;
error_free:
free(path);
free(plugin);
if (handle) {
dlclose(handle);
}
error_continue:
continue;
}
if (closedir(dir) == -1) {
......@@ -91,9 +122,10 @@ zathura_document_plugins_free(void)
}
bool
zathura_document_plugin_register(char* file_extension, zathura_document_open_t open_function)
zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, void* handle)
{
if( (file_extension == NULL) || (open_function == NULL) ) {
if( (new_plugin == NULL) || (new_plugin->file_extension == NULL) || (new_plugin->open_function == NULL)
|| (handle == NULL) ) {
fprintf(stderr, "plugin: could not register\n");
return false;
}
......@@ -101,8 +133,8 @@ zathura_document_plugin_register(char* file_extension, zathura_document_open_t o
/* search existing plugins */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
if (!strcmp(plugin->file_extension, file_extension)) {
fprintf(stderr, "plugin: already registered for filetype %s\n", file_extension);
if (!strcmp(plugin->file_extension, new_plugin->file_extension)) {
fprintf(stderr, "plugin: already registered for filetype %s\n", plugin->file_extension);
return false;
}
......@@ -114,15 +146,8 @@ zathura_document_plugin_register(char* file_extension, zathura_document_open_t o
}
/* create new plugin */
zathura_document_plugin_t* new_plugin = malloc(sizeof(zathura_document_plugin_t));
if (new_plugin == NULL) {
return false;
}
new_plugin->file_extension = strdup(file_extension);
new_plugin->open_function = open_function;
new_plugin->next = NULL;
new_plugin->handle = handle;
new_plugin->next = NULL;
/* append to list */
if (plugin == NULL) {
......
......@@ -20,10 +20,11 @@ typedef struct zathura_document_plugin_s
{
char* file_extension;
zathura_document_open_t open_function;
void* handle;
struct zathura_document_plugin_s *next;
} zathura_document_plugin_t;
typedef bool (*zathura_plugin_register_service_t)(void);
typedef void (*zathura_plugin_register_service_t)(zathura_document_plugin_t*);
struct zathura_list_s
{
......@@ -121,7 +122,7 @@ struct zathura_document_s
void zathura_document_plugins_load(void);
void zathura_document_plugins_free(void);
bool zathura_document_plugin_register(char* file_extension, zathura_document_open_t open_function);
bool zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, void* handle);
zathura_document_t* zathura_document_open(const char* path, const char* password);
bool zathura_document_free(zathura_document_t* document);
......
......@@ -5,13 +5,11 @@
#include "djvu.h"
#include "../../zathura.h"
bool
plugin_register(void)
void
plugin_register(zathura_document_plugin_t* plugin)
{
zathura_document_plugin_register("djvu", djvu_document_open);
zathura_document_plugin_register("DJVU", djvu_document_open);
return true;
plugin->file_extension = "djvu";
plugin->open_function = djvu_document_open;
}
bool
......
......@@ -6,13 +6,11 @@
#include "pdf.h"
#include "../../zathura.h"
bool
plugin_register(void)
void
plugin_register(zathura_document_plugin_t* plugin)
{
zathura_document_plugin_register("pdf", pdf_document_open);
zathura_document_plugin_register("PDF", pdf_document_open);
return true;
plugin->file_extension = "pdf";
plugin->open_function = pdf_document_open;
}
bool
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment