From 456a3d1a2eb5fa3e0aa2e1d3c3ea69ed4854256e Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Mon, 19 Nov 2012 21:45:39 +0200 Subject: [PATCH] plugin_manager: Load parser plugins by name A parser plugin is a plugin that has name "%s_parser.so", where "%s" is the name of the parser. Implement a function that will try to find a parser plugin by a name. If we can't find such plugin from the global library path, try some other places as well, such as current working directory and executable path. In order to know the executable path, the plugin manager needs to be initialized with the executable path. We are now doing it in the beginning of the main function. Signed-off-by: Timo Kokkonen --- main.c | 3 +++ plugin_manager.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ plugin_manager.h | 2 ++ 3 files changed, 55 insertions(+) diff --git a/main.c b/main.c index 9f344e5..a1d74ca 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,8 @@ #include "config.h" #include "built_in_parsers.h" +#include "plugin_manager.h" + struct user_options { int max_jobs; char *config_file; @@ -67,6 +69,7 @@ int main(int argc, char *argv[]) if (read_args(argc, argv, &opts) < 0) return -1; + init_plugin_manager(argv[0]); register_built_in_parsers(); if (!opts.config_file) { diff --git a/plugin_manager.c b/plugin_manager.c index a645f4e..1c33951 100644 --- a/plugin_manager.c +++ b/plugin_manager.c @@ -1,9 +1,23 @@ +#include #include +#include +#include #include "plugin_manager.h" #include "plugin.h" #include "debug.h" +static char *exec_path; + +int init_plugin_manager(const char *_exec_path) +{ + char *str = strdup(_exec_path); + exec_path = strdup(dirname(str)); + + free(str); + return 0; +} + int load_plugin(const char *path) { struct plugin_info *info; @@ -37,3 +51,39 @@ out: return ret; } + +/* + * Try to load a parser plugin with file name matching some of the + * following candidates (%s being the requested name): + * + * "%s_parser.so" + * "./%s_parser.so" + * "/%s_parser.so" + */ +int load_parser_plugin(const char *name) +{ + char str[256], parser[] = "_parser.so"; + int ret; + + strncpy(str, name, sizeof(str)); + strncat(str, parser, sizeof(str) - 1); + ret = load_plugin(str); + if (!ret) + return 0; + + strncpy(str, "./", sizeof(str)); + strncat(str, name, sizeof(str) - 1); + strncat(str, parser, sizeof(str) - 1); + ret = load_plugin(str); + if (!ret) + return 0; + + if (!exec_path) + return 0; + + strncpy(str, exec_path, sizeof(str)); + strncat(str, "/", sizeof(str) - 1); + strncat(str, name, sizeof(str) - 1); + strncat(str, parser, sizeof(str) - 1); + return load_plugin(str); +} diff --git a/plugin_manager.h b/plugin_manager.h index 5d894b5..2410ef5 100644 --- a/plugin_manager.h +++ b/plugin_manager.h @@ -2,5 +2,7 @@ #define _PLUGIN_MANAGER_H int load_plugin(const char *path); +int load_parser_plugin(const char *name); +int init_plugin_manager(const char *exec_path); #endif -- 2.45.0