Browse Source

Resolve NAMESPACES while clish_shell_prepare()

Serj Kalichev 9 years ago
parent
commit
4c860a741f

+ 7 - 5
clish/nspace.h

@@ -34,7 +34,7 @@ typedef enum {
 /*-----------------
  * meta functions
  *----------------- */
-clish_nspace_t *clish_nspace_new(clish_view_t * view);
+clish_nspace_t *clish_nspace_new(const char *view_name);
 /*-----------------
  * methods
  *----------------- */
@@ -50,16 +50,18 @@ void clish_nspace_clean_proxy(clish_nspace_t * instance);
 /*-----------------
  * attributes
  *----------------- */
+void clish_nspace__set_view(clish_nspace_t *instance, clish_view_t *view);
+clish_view_t *clish_nspace__get_view(const clish_nspace_t * instance);
+void clish_nspace__set_view_name(clish_nspace_t *instance, const char *view_name);
+const char * clish_nspace__get_view_name(const clish_nspace_t *instance);
 const char *clish_nspace__get_prefix(const clish_nspace_t * instance);
 const regex_t *clish_nspace__get_prefix_regex(const clish_nspace_t * instance);
 bool_t clish_nspace__get_help(const clish_nspace_t * instance);
 bool_t clish_nspace__get_completion(const clish_nspace_t * instance);
 bool_t clish_nspace__get_context_help(const clish_nspace_t * instance);
 bool_t clish_nspace__get_inherit(const clish_nspace_t * instance);
-bool_t
-clish_nspace__get_visibility(const clish_nspace_t * instance,
-			     clish_nspace_visibility_t field);
-clish_view_t *clish_nspace__get_view(const clish_nspace_t * instance);
+bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
+	clish_nspace_visibility_t field);
 void clish_nspace__set_prefix(clish_nspace_t * instance, const char *prefix);
 void clish_nspace__set_help(clish_nspace_t * instance, bool_t help);
 void clish_nspace__set_completion(clish_nspace_t * instance, bool_t help);

+ 31 - 8
clish/nspace/nspace.c

@@ -17,11 +17,13 @@
 /*---------------------------------------------------------
  * PRIVATE METHODS
  *--------------------------------------------------------- */
-static void clish_nspace_init(clish_nspace_t * this, clish_view_t * view)
+static void clish_nspace_init(clish_nspace_t *this,  const char *view_name)
 {
-	this->view = view;
+	this->view_name = NULL;
+	clish_nspace__set_view_name(this, view_name);
 
-	/* set up defaults */
+	/* Set up defaults */
+	this->view = NULL;
 	this->prefix = NULL;
 	this->help = BOOL_FALSE;
 	this->completion = BOOL_TRUE;
@@ -37,7 +39,7 @@ static void clish_nspace_init(clish_nspace_t * this, clish_view_t * view)
 }
 
 /*--------------------------------------------------------- */
-static void clish_nspace_fini(clish_nspace_t * this)
+static void clish_nspace_fini(clish_nspace_t *this)
 {
 	clish_command_t *cmd;
 
@@ -59,6 +61,7 @@ static void clish_nspace_fini(clish_nspace_t * this)
 		this->prefix_cmd = NULL;
 	}
 	lub_string_free(this->access);
+	lub_string_free(this->view_name);
 }
 
 /*--------------------------------------------------------- */
@@ -131,19 +134,19 @@ static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
 /*---------------------------------------------------------
  * PUBLIC META FUNCTIONS
  *--------------------------------------------------------- */
-clish_nspace_t *clish_nspace_new(clish_view_t * view)
+clish_nspace_t *clish_nspace_new(const char *view_name)
 {
 	clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
 
 	if (this)
-		clish_nspace_init(this, view);
+		clish_nspace_init(this, view_name);
 	return this;
 }
 
 /*---------------------------------------------------------
  * PUBLIC METHODS
  *--------------------------------------------------------- */
-void clish_nspace_delete(clish_nspace_t * this)
+void clish_nspace_delete(clish_nspace_t *this)
 {
 	clish_nspace_fini(this);
 	free(this);
@@ -280,11 +283,31 @@ void clish_nspace_clean_proxy(clish_nspace_t * this)
 /*---------------------------------------------------------
  * PUBLIC ATTRIBUTES
  *--------------------------------------------------------- */
-clish_view_t *clish_nspace__get_view(const clish_nspace_t * this)
+clish_view_t *clish_nspace__get_view(const clish_nspace_t *this)
 {
 	return this->view;
 }
 
+/*--------------------------------------------------------- */
+void clish_nspace__set_view(clish_nspace_t *this, clish_view_t *view)
+{
+	this->view = view;
+}
+
+/*--------------------------------------------------------- */
+void clish_nspace__set_view_name(clish_nspace_t *this, const char *view_name)
+{
+	if (this->view_name)
+		lub_string_free(this->view_name);
+	this->view_name = lub_string_dup(view_name);
+}
+
+/*--------------------------------------------------------- */
+const char * clish_nspace__get_view_name(const clish_nspace_t *this)
+{
+	return this->view_name;
+}
+
 /*--------------------------------------------------------- */
 void clish_nspace__set_prefix(clish_nspace_t * this, const char *prefix)
 {

+ 1 - 0
clish/nspace/nspace_dump.c

@@ -14,6 +14,7 @@ void clish_nspace_dump(const clish_nspace_t * this)
 	lub_dump_indent();
 	lub_dump_printf("view         : %s\n",
 		clish_view__get_name(this->view));
+	lub_dump_printf("view_name    : %s\n", LUB_DUMP_STR(this->view_name));
 	lub_dump_printf("prefix       : %s\n", LUB_DUMP_STR(this->prefix));
 	lub_dump_printf("access       : %s\n", LUB_DUMP_STR(this->access));
 	lub_dump_printf("help         : %s\n", LUB_DUMP_BOOL(this->help));

+ 1 - 0
clish/nspace/private.h

@@ -11,6 +11,7 @@
 struct clish_nspace_s {
 	lub_bintree_t tree;	/* Tree of command links */
 	clish_view_t *view;	/* The view to import commands from */
+	char *view_name;	/* The text name of view to import command from */
 	char *prefix;		/* if non NULL the prefix for imported commands */
 	char *access;
 	regex_t prefix_regex;

+ 19 - 0
clish/shell/shell_startup.c

@@ -71,6 +71,13 @@ const char * clish_shell__get_default_shebang(const clish_shell_t *this)
 }
 
 /*-------------------------------------------------------- */
+/* Don't forget:
+ *    Global view
+ *    startup command
+ *    hooks
+ *    remove unresolved syms
+ */
+
 int clish_shell_prepare(clish_shell_t *this)
 {
 	clish_command_t *cmd;
@@ -96,15 +103,27 @@ int clish_shell_prepare(clish_shell_t *this)
 			clish_view_delete(view);
 			continue;
 		}
+
 		/* Iterate the NAMESPACEs */
 		nspace_tree = clish_view__get_nspace_tree(view);
 		nspace_iter = lub_list__get_head(nspace_tree);
 		while(nspace_iter) {
+			clish_view_t *ref_view;
 			lub_list_node_t *old_nspace_iter;
 			nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
 			old_nspace_iter = nspace_iter;
 			nspace_iter = lub_list_node__get_next(nspace_iter);
 			/* Resolve NAMESPACEs and remove unresolved ones */
+			ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
+			if (!ref_view) {
+				fprintf(stderr, "Warning: Remove unresolved NAMESPACE %s from %s VIEW\n",
+					clish_nspace__get_view_name(nspace), clish_view__get_name(view));
+				lub_list_del(nspace_tree, old_nspace_iter);
+				lub_list_node_free(old_nspace_iter);
+				clish_nspace_delete(nspace);
+				continue;
+			}
+			clish_nspace__set_view(nspace, ref_view);
 			/* Check access rights for the NAMESPACE */
 			if (access_fn && clish_nspace__get_access(nspace) &&
 				(access_fn(this, clish_nspace__get_access(nspace)) < 0)) {

+ 3 - 4
clish/shell/shell_xml.c

@@ -920,7 +920,7 @@ static int process_detail(clish_shell_t *shell, clish_xmlnode_t *element,
 static int process_namespace(clish_shell_t *shell, clish_xmlnode_t *element,
 	void *parent)
 {
-	clish_view_t *v = (clish_view_t *) parent;
+	clish_view_t *v = (clish_view_t *)parent;
 	clish_nspace_t *nspace = NULL;
 	int res = -1;
 
@@ -939,14 +939,13 @@ static int process_namespace(clish_shell_t *shell, clish_xmlnode_t *element,
 		goto error;
 	}
 
-	clish_view_t *ref_view = clish_shell_find_create_view(shell,
-		view, NULL);
+	clish_view_t *ref_view = clish_shell_find_view(shell, view);
 
 	/* Don't include itself without prefix */
 	if ((ref_view == v) && !prefix)
 		goto process_namespace_end;
 
-	nspace = clish_nspace_new(ref_view);
+	nspace = clish_nspace_new(view);
 	clish_view_insert_nspace(v, nspace);
 
 	if (prefix) {