Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Make the plugin independant from the frontend, while here segfault when a file with now extension is found search for a plugin conf
Baptiste Daroussin committed 13 years ago
commit 7e3f8df7f5955a61e9d8b2eecb9f79415347b3d2
parent d54a25a
5 files changed +62 -19
modified libpkg/pkg.h
@@ -912,6 +912,7 @@ int pkg_plugins_display_loaded(void);
bool pkg_plugins_is_enabled(struct pkg_plugins *p);
bool pkg_plugins_is_loaded(struct pkg_plugins *p);
const char *pkg_plugins_get(struct pkg_plugins *p, pkg_plugins_key key);
+
void *pkg_plugins_func(struct pkg_plugins *p, const char *func);

/**
 * This is where plugins hook into the library using pkg_plugins_hook()
modified libpkg/plugins.c
@@ -83,6 +83,12 @@ static int pkg_plugins_hook_register(struct pkg_plugins *p, pkg_plugins_hook_t h
static int pkg_plugins_hook_exec(struct pkg_plugins *p, pkg_plugins_hook_t hook, void *data, struct pkgdb *db);
static int pkg_plugins_hook_list(struct pkg_plugins *p, struct plugins_hook **h);

+
void *
+
pkg_plugins_func(struct pkg_plugins *p, const char *func)
+
{
+
	return (dlsym(p->lh, func));
+
}
+

static int
pkg_plugins_discover(void)
{
@@ -113,7 +119,7 @@ pkg_plugins_discover(void)

		/* parse only .conf files */
		ext = strrchr(ftsent->fts_name, '.');
-
		if ((strcmp(ext, ".conf")) == 0)
+
		if (ext && (strcmp(ext, ".conf")) == 0)
			pkg_plugins_parse_conf(ftsent->fts_path);
	}

modified pkg/main.c
@@ -30,6 +30,7 @@
#include <sys/param.h>
#include <sys/jail.h>
#include <sys/stat.h>
+
#include <sys/queue.h>

#include <assert.h>
#include <err.h>
@@ -91,10 +92,20 @@ static struct commands {

const unsigned int cmd_len = (sizeof(cmd)/sizeof(cmd[0]));

+
static STAILQ_HEAD(, plugcmd) plugins = STAILQ_HEAD_INITIALIZER(plugins);
+
struct plugcmd {
+
	const char *name;
+
	const char *desc;
+
	int (*exec)(int argc, char **argv);
+
	STAILQ_ENTRY(plugcmd) next;
+
};
+

+
typedef int (register_cmd)(const char **name, const char **desc, int (**exec)(int argc, char **argv));
+

static void
usage(void)
{
-
	struct pkg_plugins *p = NULL;
+
	struct plugcmd *c;
	bool plugins_enabled = false;
	
	fprintf(stderr, "usage: pkg [-v] [-d] [-j <jail name or id>|-c <chroot path>] <command> [<args>]\n\n");
@@ -118,12 +129,9 @@ usage(void)
			errx(EX_SOFTWARE, "Plugins cannot be loaded");
		
		printf("\nCommands provided by plugins:\n");
-
		
-
		while (pkg_plugins_list(&p) != EPKG_END)
-
			if (pkg_plugins_provides_cmd(p))
-
				printf("\t%-15s%s\n",
-
				       pkg_plugins_get(p, PKG_PLUGINS_NAME),
-
				       pkg_plugins_get(p, PKG_PLUGINS_DESC));
+

+
		STAILQ_FOREACH(c, &plugins, next)
+
			fprintf(stderr, "\t%-15s%s\n", c->name, c->desc);
	}
	
	fprintf(stderr, "\nFor more information on the different commands"
@@ -195,7 +203,9 @@ main(int argc, char **argv)
	const char *buf = NULL;
	bool b, plugins_enabled = false, plugins_summary = false;
	struct pkg_config_kv *kv = NULL;
-
	
+
	struct plugcmd *c;
+
	struct pkg_plugins *p = NULL;
+

	/* Set stdout unbuffered */
        setvbuf(stdout, NULL, _IONBF, 0);

@@ -268,6 +278,16 @@ main(int argc, char **argv)
		if (pkg_plugins_init() != EPKG_OK)
			errx(EX_SOFTWARE, "Plugins cannot be loaded");

+
		/* load commands plugins */
+
		while (pkg_plugins_list(&p) != EPKG_END) {
+
			register_cmd *reg = pkg_plugins_func(p, "pkg_register_cmd");
+
			if (reg != NULL) {
+
				c = malloc(sizeof(struct plugcmd));
+
				reg(&c->name, &c->desc, &c->exec);
+
				STAILQ_INSERT_TAIL(&plugins, c, next);
+
			}
+
		}
+

		pkg_config_bool(PKG_CONFIG_PLUGINS_SUMMARY, &plugins_summary);
		if (plugins_summary)
			pkg_plugins_display_loaded();
@@ -337,8 +357,15 @@ main(int argc, char **argv)

	if (command == NULL) {
		/* Check if a plugin provides the requested command */
-
		if (plugins_enabled)
-
			ret = pkg_plugins_cmd_run(argv[0], argc, argv);
+
		if (plugins_enabled) {
+
			STAILQ_FOREACH(c, &plugins, next) {
+
				ret = EPKG_FATAL;
+
				if (strcmp(c->name, argv[0]) == 0) {
+
					ret = c->exec(argc, argv);
+
					break;
+
				}
+
			}
+
		}
		
		pkg_shutdown();
		pkg_plugins_shutdown();
modified plugins/pkg-plugin-mystats-command/mystats.c
@@ -39,17 +39,14 @@
#define PLUGIN_STATS_LOCAL 	(1<<0)
#define PLUGIN_STATS_REMOTE 	(1<<1)

+
static char myname[] = "mystats";
+
static char mydesc[] = "A plugin for displaying package statistics";
+

static int plugin_mystats_usage(void);

int
pkg_plugins_init_mystats(void)
{
-

-
	if (pkg_plugins_register_cmd(PLUGIN_NAME, &plugin_mystats_callback) != EPKG_OK) {
-
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
-
		return (EPKG_FATAL);
-
	}
-

	return (EPKG_OK);
}

@@ -66,9 +63,10 @@ plugin_mystats_usage(void)
{
	fprintf(stderr, "usage: pkg mystats [-lr]\n\n");
	fprintf(stderr, "A plugin for displaying package statistics\n");
+
	return (EPKG_OK);
}

-
int
+
static int
plugin_mystats_callback(int argc, char **argv)
{
	struct pkgdb *db = NULL;
@@ -125,3 +123,14 @@ plugin_mystats_callback(int argc, char **argv)

	return (EPKG_OK);
}
+

+
int
+
pkg_register_cmd(const char **name, const char **desc, int (**exec)(int argc, char **argv))
+
{
+
	*name = myname;
+
	*desc = mydesc;
+
	*exec = plugin_mystats_callback;
+

+
	return (EPKG_OK);
+
}
+

modified plugins/pkg-plugin-mystats-command/mystats.h
@@ -28,7 +28,7 @@
#define _PKG_PLUGINS_MYSTATS_H

/* callback functions */
-
int plugin_mystats_callback(int argc, char **argv);
+
int pkg_register_cmd(const char **name, const char **desc, int (**exec)(int argc, char **argv));

/* plugin init and shutdown functions */
int pkg_plugins_init_mystats(void);