Browse Source

Functions to work with unresolved symbopls

Serj Kalichev 11 years ago
parent
commit
a1485aef48
6 changed files with 80 additions and 31 deletions
  1. 1 3
      bin/clish.c
  2. 2 3
      clish/plugin.h
  3. 7 1
      clish/plugin/plugin.c
  4. 8 0
      clish/shell.h
  5. 59 23
      clish/shell/shell_plugin.c
  6. 3 1
      clish/shell/shell_xml.c

+ 1 - 3
bin/clish.c

@@ -277,10 +277,8 @@ int main(int argc, char **argv)
 	/* Load plugins */
 	if (clish_shell_load_plugins(shell) < 0)
 		goto end;
-	if (clish_shell_link_plugins(shell) < 0) {
-		fprintf(stderr, "Error: Undefined reference while plugin linking.\n");
+	if (clish_shell_link_plugins(shell) < 0)
 		goto end;
-	}
 
 	/* Set source of command stream: files or interactive tty */
 	if(optind < argc) {

+ 2 - 3
clish/plugin.h

@@ -4,8 +4,6 @@
 #ifndef _clish_plugin_h
 #define _clish_plugin_h
 
-#include "clish/shell.h"
-
 /* Symbol types */
 
 typedef struct clish_sym_s clish_sym_t;
@@ -13,7 +11,7 @@ typedef struct clish_plugin_s clish_plugin_t;
 
 /* Plugin types */
 
-typedef int clish_plugin_fn_t(clish_context_t *context, char **out);
+typedef int clish_plugin_fn_t(void *context, char **out);
 typedef int clish_plugin_init_t(clish_plugin_t *plugin);
 
 /* Name of init function within plugin */
@@ -26,6 +24,7 @@ clish_sym_t *clish_sym_new(const char *name, clish_plugin_fn_t *func);
 void clish_sym_free(clish_sym_t *instance);
 void clish_sym__set_func(clish_sym_t *instance, clish_plugin_fn_t *func);
 clish_plugin_fn_t *clish_sym__get_func(clish_sym_t *instance);
+char *clish_sym__get_name(clish_sym_t *instance);
 
 /* Plugin */
 

+ 7 - 1
clish/plugin/plugin.c

@@ -57,6 +57,12 @@ clish_plugin_fn_t *clish_sym__get_func(clish_sym_t *this)
 	return this->func;
 }
 
+/*--------------------------------------------------------- */
+char *clish_sym__get_name(clish_sym_t *this)
+{
+	return this->name;
+}
+
 /**********************************************************
  * PLUGIN functions                                       *
  **********************************************************/
@@ -136,7 +142,7 @@ clish_plugin_fn_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name)
 		iter; iter = lub_list_node__get_next(iter)) {
 		int res;
 		sym = (clish_sym_t *)lub_list_node__get_data(iter);
-		res = strcmp(sym->name, name);
+		res = strcmp(clish_sym__get_name(sym), name);
 		if (!res)
 			return clish_sym__get_func(sym);
 		if (res > 0) /* No chances to find name */

+ 8 - 0
clish/shell.h

@@ -23,6 +23,7 @@
 #include "clish/view.h"
 #include "clish/ptype.h"
 #include "clish/var.h"
+#include "clish/plugin.h"
 #include "konf/net.h"
 
 #define CLISH_LOCK_PATH "/tmp/clish.lock"
@@ -360,6 +361,13 @@ struct passwd *clish_shell__get_user(clish_shell_t *instance);
 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_add_sym(clish_shell_t *instance,
+	clish_plugin_fn_t *func, const char *name);
+clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *instance,
+	const char *name);
+
 _END_C_DECL
 
 #endif				/* _clish_shell_h */

+ 59 - 23
clish/shell/shell_plugin.c

@@ -105,37 +105,73 @@ end:
 	return func;
 }
 
-/*----------------------------------------------------------------------- */
-static int plugins_link_view(clish_shell_t *this, clish_view_t *view)
+/*--------------------------------------------------------- */
+clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name)
 {
-	clish_command_t *c;
-	lub_bintree_iterator_t iter;
-	lub_bintree_t *tree;
-
-	tree = clish_view__cmd_tree(view);
-	/* Iterate the tree of commands */
-	c = lub_bintree_findfirst(tree);
-	for (lub_bintree_iterator_init(&iter, tree, c);
-		c; c = lub_bintree_iterator_next(&iter)) {
-		plugins_find_sym(this, "jjj");
-		printf("command: %s\n", clish_command__get_name(c));
+	lub_list_node_t *iter;
+	clish_sym_t *sym;
+
+	/* Iterate elements */
+	for(iter = lub_list__get_head(this->syms);
+		iter; iter = lub_list_node__get_next(iter)) {
+		int res;
+		sym = (clish_sym_t *)lub_list_node__get_data(iter);
+		res = strcmp(clish_sym__get_name(sym), name);
+		if (!res)
+			return sym;
+		if (res > 0) /* No chances to find name */
+			break;
 	}
 
-	return 0;
+	return NULL;
+}
+
+/*----------------------------------------------------------------------- */
+clish_sym_t *clish_shell_add_sym(clish_shell_t *this,
+	clish_plugin_fn_t *func, const char *name)
+{
+	clish_sym_t *sym = NULL;
+
+	if (!name)
+		return NULL;
+	if ((sym = clish_shell_find_sym(this, name)))
+		return sym;
+	if (!(sym = clish_sym_new(name, func)))
+		return NULL;
+	lub_list_add(this->syms, sym);
+
+	return sym;
+}
+
+/*----------------------------------------------------------------------- */
+clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *this,
+	const char *name)
+{
+	return clish_shell_add_sym(this, NULL, name);
 }
 
 /*----------------------------------------------------------------------- */
 int clish_shell_link_plugins(clish_shell_t *this)
 {
-	clish_view_t *v;
-	lub_bintree_iterator_t iter;
-
-	v = lub_bintree_findfirst(&this->view_tree);
-	/* Iterate the tree of views */
-	for (lub_bintree_iterator_init(&iter, &this->view_tree, v);
-		v; v = lub_bintree_iterator_next(&iter)) {
-		if (plugins_link_view(this, v))
-			return -1;
+	clish_sym_t *sym;
+	lub_list_node_t *iter;
+	char *sym_name = NULL;
+	clish_plugin_fn_t *fn = NULL;
+
+	/* Iterate elements */
+	for(iter = lub_list__get_head(this->syms);
+		iter; iter = lub_list_node__get_next(iter)) {
+//		int res;
+		sym = (clish_sym_t *)lub_list_node__get_data(iter);
+		sym_name = clish_sym__get_name(sym);
+		fn = plugins_find_sym(this, sym_name);
+		if (!fn) {
+			fprintf(stderr, "Error: Can't resolve symbol %s.\n",
+				sym_name);
+//			return 0;
+		}
+//		fprintf(stderr, "sym: %s\n", sym_name);
+
 	}
 
 	return 0;

+ 3 - 1
clish/shell/shell_xml.c

@@ -701,8 +701,10 @@ process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	if (text)
 		free(text);
 
-	if (builtin)
+	if (builtin) {
 		clish_action__set_builtin(action, builtin);
+		clish_shell_add_unresolved_sym(shell, builtin);
+	}
 	if (shebang)
 		clish_action__set_shebang(action, shebang);