Browse Source

Resolve aliases while schema parsing

Serj Kalichev 9 years ago
parent
commit
511ed94e6e
4 changed files with 30 additions and 5 deletions
  1. 1 1
      clish/command/command.c
  2. 22 0
      clish/shell/shell_xml.c
  3. 1 0
      clish/view.h
  4. 6 4
      clish/view/view.c

+ 1 - 1
clish/command/command.c

@@ -144,7 +144,7 @@ clish_command_t * clish_command_alias_to_link(clish_command_t * this)
 	assert(this->alias_view);
 	ref = clish_view_find_command(this->alias_view, this->alias, BOOL_FALSE);
 	if (!ref)
-		return this;
+		return NULL;
 	memcpy(&tmp, this, sizeof(tmp));
 	*this = *ref;
 	memcpy(&this->bt_node, &tmp.bt_node, sizeof(tmp.bt_node));

+ 22 - 0
clish/shell/shell_xml.c

@@ -93,6 +93,10 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
 	char *saveptr = NULL;
 	int res = 0;
 	int i = 0;
+	clish_command_t *cmd;
+	clish_view_t *view;
+	lub_bintree_t *view_tree, *cmd_tree;
+	lub_bintree_iterator_t cmd_iter, view_iter;
 
 	/* use the default path */
 	if (!path)
@@ -173,6 +177,24 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
 			lub_list_add(this->syms, this->hooks[i]);
 	}
 
+	/* Resolve COMMAND aliases */
+	view_tree = &this->view_tree;
+	view = lub_bintree_findfirst(view_tree);
+	for (lub_bintree_iterator_init(&view_iter, view_tree, view);
+		view; view = lub_bintree_iterator_next(&view_iter)) {
+		/* Iterate the tree of commands */
+		cmd_tree = clish_view__get_command_tree(view);
+		cmd = lub_bintree_findfirst(cmd_tree);
+		for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
+			cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
+			if (!clish_command_alias_to_link(cmd)) {
+				fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
+					clish_command__get_name(cmd));
+				res = -1;
+			}
+		}
+	}
+
 #ifdef DEBUG
 	clish_shell_dump(this);
 #endif

+ 1 - 0
clish/view.h

@@ -42,6 +42,7 @@ size_t clish_view_bt_offset(void);
 /*-----------------
  * methods
  *----------------- */
+lub_bintree_t * clish_view__get_command_tree(clish_view_t *instance);
 void clish_view_delete(clish_view_t * instance);
 clish_command_t *clish_view_new_command(clish_view_t * instance,
 	const char *name, const char *text);

+ 6 - 4
clish/view/view.c

@@ -218,8 +218,6 @@ clish_command_t *clish_view_find_command(clish_view_t * this,
 
 	/* Search the current view */
 	result = lub_bintree_find(&this->tree, name);
-	/* Make command link from command alias */
-	result = clish_command_alias_to_link(result);
 
 	if (inherit) {
 		lub_list_node_t *iter;
@@ -259,8 +257,6 @@ static const clish_command_t *find_next_completion(clish_view_t * this,
 	if (iter_cmd)
 		name = iter_cmd;
 	while ((cmd = lub_bintree_findnext(&this->tree, name))) {
-		/* Make command link from command alias */
-		cmd = clish_command_alias_to_link(cmd);
 		name = clish_command__get_name(cmd);
 		if (words == lub_string_wordcount(name)) {
 			/* only bother with commands of which this line is a prefix */
@@ -328,6 +324,12 @@ void clish_view_clean_proxy(clish_view_t * this)
 /*---------------------------------------------------------
  * PUBLIC ATTRIBUTES
  *--------------------------------------------------------- */
+lub_bintree_t * clish_view__get_command_tree(clish_view_t *this)
+{
+	return &this->tree;
+}
+
+/*--------------------------------------------------------- */
 const char *clish_view__get_name(const clish_view_t * this)
 {
 	return this->name;