Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
First version of pkg_plugins_hook() for hooking into the library
Marin Atanasov Nikolov committed 13 years ago
commit 7821fdb28d956b700e332fcacc307759b8fa27b8
parent 4346466
2 files changed +46 -0
modified libpkg/pkg.h
@@ -291,6 +291,14 @@ typedef enum _pkg_plugins_key {
} pkg_plugins_key;

/**
+
 * Keys for hooking into the library
+
 */
+
typedef enum _pkg_plugins_hook_t {
+
	PKG_PLUGINS_HOOK_PRE_INSTALL = 1,
+
	PKG_PLUGINS_HOOK_POST_INSTALL,
+
} pkg_plugins_hook_t;
+

+
/**
 * Error type used everywhere by libpkg.
 */
typedef enum {
@@ -899,6 +907,13 @@ bool pkg_plugins_is_loaded(struct pkg_plugins *p);
const char *pkg_plugins_get(struct pkg_plugins *p, pkg_plugins_key key);

/**
+
 * This is where plugins hook into the library using pkg_plugins_hook()
+
 * @todo: Document
+
 */
+
typedef int(*pkg_plugins_callback)(void *data);
+
int pkg_plugins_hook(const char *pluginname, pkg_plugins_hook_t hook, pkg_plugins_callback callback);
+

+
/**
 * Get the value of a configuration key
 */
int pkg_config_string(pkg_config_key key, const char **value);
modified libpkg/plugins.c
@@ -57,6 +57,8 @@ struct _pkg_plugins_kv {
struct pkg_plugins {
	struct _pkg_plugins_kv fields[N(pkg_plugins_kv)];	/* plugin configuration fields */
	void *lh;						/* library handle */
+
	pkg_plugins_hook_t hook;
+
	pkg_plugins_callback callback;
	STAILQ_ENTRY(pkg_plugins) next;
};

@@ -311,6 +313,35 @@ pkg_plugins_unload(struct pkg_plugins *p)
	return (rc);
}

+
int
+
pkg_plugins_hook(const char *pluginname, pkg_plugins_hook_t hook, pkg_plugins_callback callback)
+
{
+
	struct pkg_plugins *p = NULL;
+
	const char *pname = NULL;
+
	bool plugin_found = false;
+
	
+
	assert(pluginname != NULL);
+

+
	/* locate the plugin */
+
	while (pkg_plugins_list(&p) != EPKG_END) {
+
		pname = pkg_plugins_get(p, PKG_PLUGINS_NAME);
+
		if ((strcmp(pname, pluginname)) == 0) {
+
			p->hook |= hook;
+
			p->callback = callback;
+
			plugin_found = true;
+
		}
+
	}
+

+
	if (plugin_found == false) {
+
		pkg_emit_error("Plugin name '%s' was not found in the registry, cannot hook",
+
			       pluginname);
+
		return (EPKG_FATAL);
+
	}
+

+
	return (EPKG_OK);
+
}
+

+

const char *
pkg_plugins_get(struct pkg_plugins *p, pkg_plugins_key key)
{