Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Allow plugins to register commands for execution, e.g. 'pkg <plugin-command>'
Marin Atanasov Nikolov committed 13 years ago
commit b74cd85dc857a66068c1ebd82d153f64f8d4697b
parent 64f7ed6
2 files changed +56 -0
modified libpkg/pkg.h
@@ -921,6 +921,14 @@ int pkg_plugins_hook(const char *pluginname, pkg_plugins_hook_t hook, pkg_plugin
int pkg_plugins_hook_run(pkg_plugins_hook_t hook, void *data, struct pkgdb *db);

/**
+
 * These functions are used by plugins for registering new commands provided by plugins
+
 * @todo: Document
+
 */
+
typedef int(*pkg_plugins_cmd_callback)(int argc, char **argv);
+
int pkg_plugins_register_cmd(const char *pluginname, pkg_plugins_cmd_callback callback);
+
int pkg_plugins_cmd_run(const char *cmd, int argc, char **argv);
+
			     
+
/**
 * Get the value of a configuration key
 */
int pkg_config_string(pkg_config_key key, const char **value);
modified libpkg/plugins.c
@@ -63,6 +63,7 @@ struct plugins_hook {
struct pkg_plugins {
	struct _pkg_plugins_kv fields[N(pkg_plugins_kv)];	/* plugin configuration fields */
	void *lh;						/* library handle */
+
	pkg_plugins_cmd_callback exec_cmd;			/* exec callback for plugins providing commands */
	STAILQ_HEAD(phooks, plugins_hook) phooks;		/* plugin hooks */
	STAILQ_ENTRY(pkg_plugins) next;
};
@@ -425,6 +426,34 @@ pkg_plugins_hook(const char *pluginname, pkg_plugins_hook_t hook, pkg_plugins_ca
}

int
+
pkg_plugins_register_cmd(const char *pluginname, pkg_plugins_cmd_callback callback)
+
{
+
	struct pkg_plugins *p = NULL;
+
	const char *pname = NULL;
+
	bool plugin_found = false;
+
	
+
	assert(pluginname != NULL);
+
	assert(callback != 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->exec_cmd = 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);
+
}
+

+
int
pkg_plugins_hook_run(pkg_plugins_hook_t hook, void *data, struct pkgdb *db)
{
	struct pkg_plugins *p = NULL;
@@ -436,6 +465,25 @@ pkg_plugins_hook_run(pkg_plugins_hook_t hook, void *data, struct pkgdb *db)
	return (EPKG_OK);
}

+
int
+
pkg_plugins_cmd_run(const char *cmd, int argc, char **argv)
+
{
+
	struct pkg_plugins *p = NULL;
+
	bool cmd_found = false;
+

+
	while (pkg_plugins_list(&p) != EPKG_END)
+
		if ((pkg_plugins_is_loaded(p)) &&
+
		    (strcmp(cmd, pkg_plugins_get(p, PKG_PLUGINS_NAME)) == 0)) {
+
			cmd_found = true;
+
			p->exec_cmd(argc, argv);
+
		}
+

+
	if (cmd_found == false)
+
		return (EPKG_FATAL);
+
	else
+
		return (EPKG_OK);
+
}
+

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