Browse Source

The plugin has alias and plugin defined name

Serj Kalichev 11 years ago
parent
commit
cc9e5dc358

+ 5 - 1
clish/plugin.h

@@ -84,7 +84,7 @@ int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src);
 
 /* Plugin */
 
-clish_plugin_t *clish_plugin_new(const char *name, const char *file);
+clish_plugin_t *clish_plugin_new(const char *file, const char *alias);
 void clish_plugin_free(clish_plugin_t *instance, void *userdata);
 int clish_plugin_load(clish_plugin_t *instance, void *userdata);
 clish_sym_t *clish_plugin_get_sym(clish_plugin_t *instance,
@@ -100,7 +100,11 @@ clish_sym_t *clish_plugin_add_hook(clish_plugin_t *instance,
 clish_sym_t *clish_plugin_add_phook(clish_plugin_t *instance,
 	void *func, const char *name, int type);
 void clish_plugin_dump(const clish_plugin_t *instance);
+void clish_plugin__set_name(clish_plugin_t *instance, const char *name);
 char *clish_plugin__get_name(const clish_plugin_t *instance);
+void clish_plugin__set_alias(clish_plugin_t *instance, const char *alias);
+char *clish_plugin__get_alias(const clish_plugin_t *instance);
+char *clish_plugin__get_pubname(const clish_plugin_t *instance);
 char *clish_plugin__get_file(const clish_plugin_t *instance);
 
 #endif				/* _clish_plugin_h */

+ 30 - 9
clish/plugin/plugin.c

@@ -127,20 +127,15 @@ int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
  **********************************************************/
 
 /*--------------------------------------------------------- */
-clish_plugin_t *clish_plugin_new(const char *name, const char *file)
+clish_plugin_t *clish_plugin_new(const char *file, const char *alias)
 {
 	clish_plugin_t *this;
 
 	this = malloc(sizeof(*this));
 
-	if (file)
-		this->file = lub_string_dup(file);
-	else
-		this->file = NULL; /* For main program binary */
-	if (name)
-		this->name = lub_string_dup(name);
-	else
-		this->name = NULL;
+	this->file = lub_string_dup(file);
+	this->name = NULL;
+	this->alias = lub_string_dup(alias);
 	this->dlhan = NULL;
 	/* Initialise the list of symbols */
 	this->syms = lub_list_new(clish_sym_compare);
@@ -287,12 +282,38 @@ int clish_plugin_load(clish_plugin_t *this, void *userdata)
 	return res;
 }
 
+/*--------------------------------------------------------- */
+void clish_plugin__set_name(clish_plugin_t *this, const char *name)
+{
+	lub_string_free(this->name);
+	this->name = lub_string_dup(name);
+}
+
 /*--------------------------------------------------------- */
 char *clish_plugin__get_name(const clish_plugin_t *this)
 {
 	return this->name;
 }
 
+/*--------------------------------------------------------- */
+void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
+{
+	lub_string_free(this->alias);
+	this->alias = lub_string_dup(alias);
+}
+
+/*--------------------------------------------------------- */
+char *clish_plugin__get_alias(const clish_plugin_t *this)
+{
+	return this->alias;
+}
+
+/*--------------------------------------------------------- */
+char *clish_plugin__get_pubname(const clish_plugin_t *this)
+{
+	return (this->alias ? this->alias : this->name);
+}
+
 /*--------------------------------------------------------- */
 char *clish_plugin__get_file(const clish_plugin_t *this)
 {

+ 2 - 1
clish/plugin/private.h

@@ -19,7 +19,8 @@ struct clish_sym_s {
 
 struct clish_plugin_s {
 	char *file; /* Plugin file name. Must be unique. */
-	char *name; /* Local plugin name. Can be used in builtin ref. */
+	char *name; /* Plugin name defined by plugin itself. */
+	char *alias; /* User defined plugin name. Can be used in builtin ref. */
 	lub_list_t *syms; /* List of plugin symbols */
 	void *dlhan; /* Handler of dlopen() */
 	clish_plugin_init_t *init; /* Init function (constructor) != NULL */

+ 1 - 1
clish/shell/shell_new.c

@@ -40,7 +40,7 @@ static void clish_shell_init(clish_shell_t * this,
 	/* Initialize plugin list */
 	this->plugins = lub_list_new(NULL);
 	/* Create internal plugin "clish" */
-	plugin = clish_plugin_new("clish", NULL);
+	plugin = clish_plugin_new(NULL, "clish");
 	lub_list_add(this->plugins, plugin);
 
 	/* Initialise the list of unresolved (yet) symbols */

+ 1 - 1
clish/shell/shell_plugin.c

@@ -73,7 +73,7 @@ static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name, int
 		for(iter = lub_list__get_head(this->plugins);
 			iter; iter = lub_list_node__get_next(iter)) {
 			plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
-			if (strcmp(clish_plugin__get_name(plugin), plugin_name))
+			if (strcmp(clish_plugin__get_pubname(plugin), plugin_name))
 				continue;
 			if ((sym = clish_plugin_get_sym(plugin, cmdn, type)))
 				break;

+ 1 - 1
clish/shell/shell_xml.c

@@ -1176,7 +1176,7 @@ process_plugin(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
 		goto error;
 	}
 
-	plugin = clish_plugin_new(name, file);
+	plugin = clish_plugin_new(file, name); /* Really the name is alias */
 	lub_list_add(shell->plugins, plugin);
 
 	res = 0;

+ 2 - 0
examples/explugin/anplug.c

@@ -11,6 +11,8 @@ CLISH_PLUGIN_SYM(anplug_fn)
 CLISH_PLUGIN_INIT
 {
 	printf("anplug: INIT shell = %p\n", clish_shell);
+	/* Set a name of plugin to use in sym@plugin */
+	clish_plugin__set_name(plugin, "another_plug");
 	clish_plugin_add_sym(plugin, anplug_fn, "an_fn");
 
 	return 0;