Browse Source

Add plugin symbol type

Serj Kalichev 11 years ago
parent
commit
e10081436e

+ 18 - 7
clish/plugin.h

@@ -6,7 +6,16 @@
 
 #include "lub/types.h"
 
-/* Symbol types */
+/* Symbol */
+
+/* Symbol types. Functions with different definition. */
+#define CLISH_SYM_TYPE_NONE	0 /* None */
+#define CLISH_SYM_TYPE_FN	1 /* Common builtin symbol */
+#define CLISH_SYM_TYPE_INIT	2
+#define CLISH_SYM_TYPE_FINI	3
+#define CLISH_SYM_TYPE_ACCESS	4
+#define CLISH_SYM_TYPE_CONFIG	5
+#define CLISH_SYM_TYPE_LOG	6
 
 typedef struct clish_sym_s clish_sym_t;
 typedef struct clish_plugin_s clish_plugin_t;
@@ -18,10 +27,8 @@ typedef struct clish_plugin_s clish_plugin_t;
 #define CLISH_PLUGIN_INIT_NAME "clish_plugin_init"
 #define CLISH_PLUGIN_INIT_FUNC(name) int name(clish_plugin_t *plugin)
 #define CLISH_PLUGIN_INIT CLISH_PLUGIN_INIT_FUNC(CLISH_PLUGIN_INIT_FNAME)
-
 #define CLISH_PLUGIN_SYM(name) int name(void *clish_context, const char *script, char **out)
-
-#define CLISH_DEFAULT_SYM "clish_script@clish"
+#define CLISH_DEFAULT_SYM "clish_script@clish" /* Builtin symbol to use by default */
 
 typedef CLISH_PLUGIN_SYM(clish_plugin_fn_t);
 typedef CLISH_PLUGIN_INIT_FUNC(clish_plugin_init_t);
@@ -29,9 +36,9 @@ typedef CLISH_PLUGIN_INIT_FUNC(clish_plugin_init_t);
 /* Symbol */
 
 int clish_sym_compare(const void *first, const void *second);
-clish_sym_t *clish_sym_new(const char *name, clish_plugin_fn_t *func);
+clish_sym_t *clish_sym_new(const char *name, void *func, int type);
 void clish_sym_free(clish_sym_t *instance);
-void clish_sym__set_func(clish_sym_t *instance, clish_plugin_fn_t *func);
+void clish_sym__set_func(clish_sym_t *instance, void *func);
 clish_plugin_fn_t *clish_sym__get_func(clish_sym_t *instance);
 void clish_sym__set_name(clish_sym_t *instance, const char *name);
 char *clish_sym__get_name(clish_sym_t *instance);
@@ -39,6 +46,8 @@ void clish_sym__set_permanent(clish_sym_t *instance, bool_t permanent);
 bool_t clish_sym__get_permanent(clish_sym_t *instance);
 void clish_sym__set_plugin(clish_sym_t *instance, clish_plugin_t *plugin);
 clish_plugin_t *clish_sym__get_plugin(clish_sym_t *instance);
+void clish_sym__set_type(clish_sym_t *instance, int type);
+int clish_sym__get_type(clish_sym_t *instance);
 int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src);
 
 /* Plugin */
@@ -47,7 +56,9 @@ clish_plugin_t *clish_plugin_new(const char *name, const char *file);
 void clish_plugin_free(clish_plugin_t *instance);
 void *clish_plugin_load(clish_plugin_t *instance);
 clish_sym_t *clish_plugin_get_sym(clish_plugin_t *instance,
-	const char *name);
+	const char *name, int type);
+clish_sym_t *clish_plugin_add_generic(clish_plugin_t *instance,
+	void *func, const char *name, int type);
 clish_sym_t *clish_plugin_add_sym(clish_plugin_t *instance,
 	clish_plugin_fn_t *func, const char *name);
 clish_sym_t *clish_plugin_add_psym(clish_plugin_t *instance,

+ 28 - 7
clish/plugin/plugin.c

@@ -25,13 +25,14 @@ int clish_sym_compare(const void *first, const void *second)
 }
 
 /*--------------------------------------------------------- */
-clish_sym_t *clish_sym_new(const char *name, clish_plugin_fn_t *func)
+clish_sym_t *clish_sym_new(const char *name, void *func, int type)
 {
 	clish_sym_t *this;
 
 	this = malloc(sizeof(*this));
 	this->name = lub_string_dup(name);
 	this->func = func;
+	this->type = type;
 	this->permanent = BOOL_FALSE;
 
 	return this;
@@ -47,7 +48,7 @@ void clish_sym_free(clish_sym_t *this)
 }
 
 /*--------------------------------------------------------- */
-void clish_sym__set_func(clish_sym_t *this, clish_plugin_fn_t *func)
+void clish_sym__set_func(clish_sym_t *this, void *func)
 {
 	this->func = func;
 }
@@ -95,6 +96,18 @@ clish_plugin_t *clish_sym__get_plugin(clish_sym_t *this)
 	return this->plugin;
 }
 
+/*--------------------------------------------------------- */
+void clish_sym__set_type(clish_sym_t *this, int type)
+{
+	this->type = type;
+}
+
+/*--------------------------------------------------------- */
+int clish_sym__get_type(clish_sym_t *this)
+{
+	return this->type;
+}
+
 /*--------------------------------------------------------- */
 int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
 {
@@ -162,15 +175,15 @@ void clish_plugin_free(clish_plugin_t *this)
 }
 
 /*--------------------------------------------------------- */
-clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
-	clish_plugin_fn_t *func, const char *name)
+clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
+	void *func, const char *name, int type)
 {
 	clish_sym_t *sym;
 
 	if (!name || !func)
 		return NULL;
 
-	if (!(sym = clish_sym_new(name, func)))
+	if (!(sym = clish_sym_new(name, func, type)))
 		return NULL;
 	clish_sym__set_plugin(sym, this);
 	lub_list_add(this->syms, sym);
@@ -178,6 +191,14 @@ clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
 	return sym;
 }
 
+/*--------------------------------------------------------- */
+clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
+	clish_plugin_fn_t *func, const char *name)
+{
+	return clish_plugin_add_generic(this, func,
+		name, CLISH_SYM_TYPE_FN);
+}
+
 /*--------------------------------------------------------- */
 /* Add permanent symbol (can't be turned off by dry-run) */
 clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
@@ -193,7 +214,7 @@ clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
 }
 
 /*--------------------------------------------------------- */
-clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name)
+clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
 {
 	lub_list_node_t *iter;
 	clish_sym_t *sym;
@@ -204,7 +225,7 @@ clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name)
 		int res;
 		sym = (clish_sym_t *)lub_list_node__get_data(iter);
 		res = strcmp(clish_sym__get_name(sym), name);
-		if (!res)
+		if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
 			return sym;
 		if (res > 0) /* No chances to find name */
 			break;

+ 29 - 0
clish/plugin/plugin_dump.c

@@ -9,11 +9,40 @@
 /*--------------------------------------------------------- */
 void clish_sym_dump(const clish_sym_t *this)
 {
+	char *type = NULL;
+
 	lub_dump_printf("sym(%p)\n", this);
 
 	lub_dump_indent();
 	lub_dump_printf("name      : %s\n", this->name);
 	lub_dump_printf("func      : %p\n", this->func);
+	switch (this->type) {
+	case CLISH_SYM_TYPE_NONE:
+		type = "none";
+		break;
+	case CLISH_SYM_TYPE_FN:
+		type = "fn";
+		break;
+	case CLISH_SYM_TYPE_INIT:
+		type = "init";
+		break;
+	case CLISH_SYM_TYPE_FINI:
+		type = "fini";
+		break;
+	case CLISH_SYM_TYPE_ACCESS:
+		type = "access";
+		break;
+	case CLISH_SYM_TYPE_CONFIG:
+		type = "config";
+		break;
+	case CLISH_SYM_TYPE_LOG:
+		type = "log";
+		break;
+	default:
+		type = "unknown";
+		break;
+	}
+	lub_dump_printf("type      : %s\n", type);
 	lub_dump_printf("permanent : %s\n",
 		this->permanent ? "true" : "false");
 	lub_dump_printf("plugin    : %p\n", this->plugin);

+ 3 - 2
clish/plugin/private.h

@@ -11,8 +11,9 @@
 
 struct clish_sym_s {
 	char *name; /* Symbol name */
-	clish_plugin_fn_t *func; /* Function address */
-	bool_t permanent; /* If permanent the dry-run can't switch off it */
+	void *func; /* Function address */
+	int type; /* Function type */
+	bool_t permanent; /* If permanent the dry-run can't switch it off */
 	clish_plugin_t *plugin; /* Parent plugin */
 };
 

+ 4 - 3
clish/shell.h

@@ -303,11 +303,12 @@ int clish_shell_load_plugins(clish_shell_t *instance);
 int clish_shell_link_plugins(clish_shell_t *instance);
 
 /* Unresolved symbols functions */
-clish_sym_t *clish_shell_find_sym(clish_shell_t *instance, const char *name);
+clish_sym_t *clish_shell_find_sym(clish_shell_t *instance,
+	const char *name, int type);
 clish_sym_t *clish_shell_add_sym(clish_shell_t *instance,
-	clish_plugin_fn_t *func, const char *name);
+	clish_plugin_fn_t *func, const char *name, int type);
 clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *instance,
-	const char *name);
+	const char *name, int type);
 
 _END_C_DECL
 

+ 2 - 1
clish/shell/shell_new.c

@@ -48,7 +48,8 @@ static void clish_shell_init(clish_shell_t * this,
 	/* Initialise the list of unresolved (yet) symbols */
 	this->syms = lub_list_new(clish_sym_compare);
 	/* Add default sym and save it to shell structure */
-	sym = clish_shell_add_unresolved_sym(this, CLISH_DEFAULT_SYM);
+	sym = clish_shell_add_unresolved_sym(this,
+		CLISH_DEFAULT_SYM, CLISH_SYM_TYPE_FN);
 	this->default_sym = sym;
 
 	assert(NULL != hooks);

+ 13 - 11
clish/shell/shell_plugin.c

@@ -47,7 +47,7 @@ int clish_shell_load_plugins(clish_shell_t *this)
  * mysym@plugin1
  * The symbols with suffix will be resolved using specified plugin only.
  */
-static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name)
+static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name, int type)
 {
 	lub_list_node_t *iter;
 	clish_plugin_t *plugin;
@@ -77,7 +77,7 @@ static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name)
 			plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
 			if (strcmp(clish_plugin__get_name(plugin), plugin_name))
 				continue;
-			if ((sym = clish_plugin_get_sym(plugin, cmdn)))
+			if ((sym = clish_plugin_get_sym(plugin, cmdn, type)))
 				break;
 		}
 	} else {
@@ -85,7 +85,7 @@ static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name)
 		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 ((sym = clish_plugin_get_sym(plugin, cmdn)))
+			if ((sym = clish_plugin_get_sym(plugin, cmdn, type)))
 				break;
 		}
 	}
@@ -96,7 +96,7 @@ static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name)
 
 /*--------------------------------------------------------- */
 /* Find symbol by name in the list of unresolved symbols */
-clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name)
+clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name, int type)
 {
 	lub_list_node_t *iter;
 	clish_sym_t *sym;
@@ -107,7 +107,7 @@ clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name)
 		int res;
 		sym = (clish_sym_t *)lub_list_node__get_data(iter);
 		res = strcmp(clish_sym__get_name(sym), name);
-		if (!res)
+		if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
 			return sym;
 		if (res > 0) /* No chance to find name */
 			break;
@@ -119,15 +119,15 @@ clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name)
 /*----------------------------------------------------------------------- */
 /* Add symbol to the table of unresolved symbols */
 clish_sym_t *clish_shell_add_sym(clish_shell_t *this,
-	clish_plugin_fn_t *func, const char *name)
+	clish_plugin_fn_t *func, const char *name, int type)
 {
 	clish_sym_t *sym = NULL;
 
 	if (!name)
 		return NULL;
-	if ((sym = clish_shell_find_sym(this, name)))
+	if ((sym = clish_shell_find_sym(this, name, type)))
 		return sym;
-	if (!(sym = clish_sym_new(name, func)))
+	if (!(sym = clish_sym_new(name, func, type)))
 		return NULL;
 	lub_list_add(this->syms, sym);
 
@@ -136,9 +136,9 @@ clish_sym_t *clish_shell_add_sym(clish_shell_t *this,
 
 /*----------------------------------------------------------------------- */
 clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *this,
-	const char *name)
+	const char *name, int type)
 {
-	return clish_shell_add_sym(this, NULL, name);
+	return clish_shell_add_sym(this, NULL, name, type);
 }
 
 /*----------------------------------------------------------------------- */
@@ -148,13 +148,15 @@ int clish_shell_link_plugins(clish_shell_t *this)
 	clish_sym_t *sym, *plugin_sym;
 	lub_list_node_t *iter;
 	char *sym_name = NULL;
+	int sym_type;
 
 	/* Iterate elements */
 	for(iter = lub_list__get_head(this->syms);
 		iter; iter = lub_list_node__get_next(iter)) {
 		sym = (clish_sym_t *)lub_list_node__get_data(iter);
 		sym_name = clish_sym__get_name(sym);
-		plugin_sym = plugins_find_sym(this, sym_name);
+		sym_type = clish_sym__get_type(sym);
+		plugin_sym = plugins_find_sym(this, sym_name, sym_type);
 		if (!plugin_sym) {
 			fprintf(stderr, "Error: Can't resolve symbol %s.\n",
 				sym_name);

+ 3 - 2
clish/shell/shell_xml.c

@@ -810,7 +810,7 @@ error:
 
 /* ------------------------------------------------------ */
 static int
-process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
+process_action(clish_shell_t *shell, clish_xmlnode_t *element, void *parent)
 {
 	clish_action_t *action = NULL;
 	char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
@@ -838,7 +838,8 @@ process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 		free(text);
 
 	if (builtin)
-		sym = clish_shell_add_unresolved_sym(shell, builtin);
+		sym = clish_shell_add_unresolved_sym(shell, builtin,
+			CLISH_SYM_TYPE_FN);
 	else
 		sym = shell->default_sym;
 	clish_action__set_builtin(action, sym);