Browse Source

Use lub_list_t for COMMANDs

Serj Kalichev 6 years ago
parent
commit
f4a3751afa
7 changed files with 58 additions and 54 deletions
  1. 3 0
      clish/command.h
  2. 7 0
      clish/command/command.c
  3. 21 25
      clish/nspace/nspace.c
  4. 2 5
      clish/nspace/private.h
  5. 18 13
      clish/shell/shell_startup.c
  6. 1 1
      clish/view.h
  7. 6 10
      clish/view/view.c

+ 3 - 0
clish/command.h

@@ -73,4 +73,7 @@ clish_view_restore_e clish_command__get_restore(const clish_command_t * instance
 const clish_command_t * clish_command__get_orig(const clish_command_t * instance);
 const clish_command_t * clish_command__get_cmd(const clish_command_t * instance);
 
+// Find functions
+int clish_command_fn_find_by_name(const void *key, const void *data);
+
 #endif				/* _clish_command_h */

+ 7 - 0
clish/command/command.c

@@ -281,3 +281,10 @@ const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
 		return clish_command__get_cmd(this->link);
 	return NULL;
 }
+
+/*--------------------------------------------------------- */
+int clish_command_fn_find_by_name(const void *key, const void *data) {
+	const char *name = (const char *)key;
+	const clish_command_t *d = (const clish_command_t *)data;
+	return lub_string_nocasecmp(name, clish_command__get_name(d));
+}

+ 21 - 25
clish/nspace/nspace.c

@@ -30,29 +30,19 @@ static void clish_nspace_init(clish_nspace_t *this,  const char *view_name)
 	this->prefix_cmd = NULL;
 	this->access = NULL;
 
-	/* initialise the tree of commands links for this nspace */
-	lub_bintree_init(&this->tree,
-		clish_command_bt_offset(),
-		clish_command_bt_compare, clish_command_bt_getkey);
+	this->cmds = lub_list_new(clish_command_compare, clish_command_delete);
 }
 
 /*--------------------------------------------------------- */
 static void clish_nspace_fini(clish_nspace_t *this)
 {
-	clish_command_t *cmd;
-
-	/* deallocate the memory for this instance */
+	/* Deallocate the memory for this instance */
 	if (this->prefix) {
 		free(this->prefix);
 		regfree(&this->prefix_regex);
 	}
-	/* delete each command link held by this nspace */
-	while ((cmd = lub_bintree_findfirst(&this->tree))) {
-		/* remove the command from the tree */
-		lub_bintree_remove(&this->tree, cmd);
-		/* release the instance */
-		clish_command_delete(cmd);
-	}
+	/* Delete COMMANDs */
+	lub_list_free_all(this->cmds);
 	/* Delete prefix pseudo-command */
 	if (this->prefix_cmd) {
 		clish_command_delete(this->prefix_cmd);
@@ -83,6 +73,7 @@ static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
 	const char *help = NULL;
 	clish_command_t *tmp = NULL;
 	const char *str = NULL;
+	lub_list_node_t *iter = NULL;
 
 	assert(prefix);
 	if (!ref) {
@@ -99,7 +90,7 @@ static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
 	}
 
 	/* The command is cached already */
-	if ((cmd = lub_bintree_find(&this->tree, name))) {
+	if ((cmd = lub_list_find(this->cmds, clish_command_fn_find_by_name, name))) {
 		free(name);
 		return cmd;
 	}
@@ -110,18 +101,24 @@ static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
 	clish_command__set_dynamic(cmd, BOOL_TRUE);
 
 	/* Delete proxy commands with another prefixes */
-	tmp = lub_bintree_findfirst(&this->tree);
+	iter = lub_list__get_head(this->cmds);
+	if (iter)
+		tmp = lub_list_node__get_data(iter);
 	if (tmp)
 		str = clish_command__get_name(tmp);
 	if (str && (lub_string_nocasestr(str, prefix) != str)) {
 		do {
-			lub_bintree_remove(&this->tree, tmp);
 			clish_command_delete(tmp);
-		} while ((tmp = lub_bintree_findfirst(&this->tree)));
+			lub_list_del(this->cmds, iter);
+			lub_list_node_free(iter);
+			iter = lub_list__get_head(this->cmds);
+			if (iter)
+				tmp = lub_list_node__get_data(iter);
+		} while (iter);
 	}
 
 	/* Insert command link into the tree */
-	if (-1 == lub_bintree_insert(&this->tree, cmd)) {
+	if (!lub_list_add(this->cmds, cmd)) {
 		clish_command_delete(cmd);
 		cmd = NULL;
 	}
@@ -262,16 +259,15 @@ const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
 /*--------------------------------------------------------- */
 void clish_nspace_clean_proxy(clish_nspace_t * this)
 {
-	clish_command_t *cmd = NULL;
+	lub_list_node_t *iter = NULL;
 
 	/* Recursive proxy clean */
 	clish_view_clean_proxy(this->view);
 	/* Delete each command proxy held by this nspace */
-	while ((cmd = lub_bintree_findfirst(&this->tree))) {
-		/* remove the command from the tree */
-		lub_bintree_remove(&this->tree, cmd);
-		/* release the instance */
-		clish_command_delete(cmd);
+	while ((iter = lub_list__get_tail(this->cmds))) {
+		clish_command_delete(lub_list_node__get_data(iter));
+		lub_list_del(this->cmds, iter);
+		lub_list_node_free(iter);
 	}
 }
 

+ 2 - 5
clish/nspace/private.h

@@ -5,11 +5,8 @@
 
 #include "clish/nspace.h"
 
-/*---------------------------------------------------------
- * PRIVATE TYPES
- *--------------------------------------------------------- */
 struct clish_nspace_s {
-	lub_bintree_t tree;	/* Tree of command links */
+	lub_list_t *cmds;	/* 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 */
@@ -19,5 +16,5 @@ struct clish_nspace_s {
 	bool_t completion;
 	bool_t context_help;
 	bool_t inherit;
-	clish_command_t * prefix_cmd;
+	clish_command_t *prefix_cmd;
 };

+ 18 - 13
clish/shell/shell_startup.c

@@ -142,10 +142,8 @@ int clish_shell_prepare(clish_shell_t *this)
 	clish_command_t *cmd;
 	clish_view_t *view;
 	clish_nspace_t *nspace;
-	lub_list_t *view_tree, *nspace_tree;
-	lub_list_node_t *nspace_iter, *view_iter;
-	lub_bintree_t *cmd_tree;
-	lub_bintree_iterator_t cmd_iter;
+	lub_list_t *view_tree, *nspace_tree, *cmd_tree;
+	lub_list_node_t *nspace_iter, *view_iter, *cmd_iter;
 	clish_hook_access_fn_t *access_fn = NULL;
 	clish_paramv_t *paramv;
 	int i = 0;
@@ -191,7 +189,7 @@ int clish_shell_prepare(clish_shell_t *this)
 	/* Iterate the VIEWs */
 	view_tree = this->view_tree;
 	view_iter = lub_list_iterator_init(view_tree);
-	while(view_iter) {
+	while (view_iter) {
 		lub_list_node_t *old_view_iter;
 		view = (clish_view_t *)lub_list_node__get_data(view_iter);
 		old_view_iter = view_iter;
@@ -212,7 +210,7 @@ int clish_shell_prepare(clish_shell_t *this)
 		/* Iterate the NAMESPACEs */
 		nspace_tree = clish_view__get_nspaces(view);
 		nspace_iter = lub_list__get_head(nspace_tree);
-		while(nspace_iter) {
+		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);
@@ -253,11 +251,15 @@ int clish_shell_prepare(clish_shell_t *this)
 
 		/* Iterate the COMMANDs */
 		cmd_tree = clish_view__get_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)) {
-			int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
+		cmd_iter = lub_list_iterator_init(cmd_tree);
+		while (cmd_iter) {
+			int cmd_is_alias;
 			clish_param_t *args = NULL;
+			lub_list_node_t *old_cmd_iter;
+			cmd = (clish_command_t *)lub_list_node__get_data(cmd_iter);
+			old_cmd_iter = cmd_iter;
+			cmd_iter = lub_list_node__get_next(cmd_iter);
+			cmd_is_alias = clish_command__get_alias(cmd) ? 1 : 0;
 
 			/* Check access rights for the COMMAND */
 			if (access_fn && clish_command__get_access(cmd) &&
@@ -266,7 +268,8 @@ int clish_shell_prepare(clish_shell_t *this)
 				fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
 					clish_command__get_name(cmd), clish_view__get_name(view));
 #endif
-				lub_bintree_remove(cmd_tree, cmd);
+				lub_list_del(cmd_tree, old_cmd_iter);
+				lub_list_node_free(old_cmd_iter);
 				clish_command_delete(cmd);
 				continue;
 			}
@@ -289,7 +292,8 @@ int clish_shell_prepare(clish_shell_t *this)
 					fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
 						clish_command__get_name(cmd), clish_view__get_name(view));
 #endif
-					lub_bintree_remove(cmd_tree, cmd);
+					lub_list_del(cmd_tree, old_cmd_iter);
+					lub_list_node_free(old_cmd_iter);
 					clish_command_delete(cmd);
 					continue;
 					/*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
@@ -313,7 +317,8 @@ int clish_shell_prepare(clish_shell_t *this)
 					fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
 						clish_command__get_name(cmd), clish_view__get_name(view));
 #endif
-					lub_bintree_remove(cmd_tree, cmd);
+					lub_list_del(cmd_tree, old_cmd_iter);
+					lub_list_node_free(old_cmd_iter);
 					clish_command_delete(cmd);
 					continue;
 				}

+ 1 - 1
clish/view.h

@@ -60,7 +60,7 @@ _CLISH_GET(view, unsigned int, depth);
 _CLISH_SET(view, clish_view_restore_e, restore);
 _CLISH_GET(view, clish_view_restore_e, restore);
 
-lub_bintree_t * clish_view__get_tree(clish_view_t *instance);
+lub_list_t * clish_view__get_tree(clish_view_t *instance);
 
 #endif				/* _clish_view_h */
 /** @} clish_view */

+ 6 - 10
clish/view/view.c

@@ -167,13 +167,6 @@ clish_command_t *clish_view_resolve_command(clish_view_t *this,
 	return result;
 }
 
-/*--------------------------------------------------------- */
-static int cmd_by_name(const void *key, const void *data) {
-	const char *name = (const char *)key;
-	const clish_command_t *d = (const clish_command_t *)data;
-	return lub_string_nocasecmp(name, clish_command__get_name(d));
-}
-
 /*--------------------------------------------------------- */
 clish_command_t *clish_view_find_command(clish_view_t *this,
 	const char *name, bool_t inherit)
@@ -181,7 +174,7 @@ clish_command_t *clish_view_find_command(clish_view_t *this,
 	clish_command_t *result = NULL;
 
 	/* Search the current view */
-	result = lub_list_find(this->cmds, cmd_by_name, name);
+	result = lub_list_find(this->cmds, clish_command_fn_find_by_name, name);
 
 	if (inherit) {
 		lub_list_node_t *iter;
@@ -210,6 +203,7 @@ clish_command_t *clish_view_find_command(clish_view_t *this,
 static const clish_command_t *find_next_completion(clish_view_t * this,
 		const char *iter_cmd, const char *line)
 {
+#if 0 // WORK
 	clish_command_t *cmd;
 	const char *name = "";
 	lub_argv_t *largv;
@@ -238,6 +232,8 @@ static const clish_command_t *find_next_completion(clish_view_t * this,
 	lub_argv_delete(largv);
 
 	return cmd;
+#endif
+return NULL;
 }
 
 /*--------------------------------------------------------- */
@@ -314,8 +310,8 @@ CLISH_SET(view, clish_view_restore_e, restore);
 CLISH_GET(view, clish_view_restore_e, restore);
 
 /*-------------------------------------------------------- */
-lub_bintree_t * clish_view__get_tree(clish_view_t *inst)
+lub_list_t * clish_view__get_tree(clish_view_t *inst)
 {
 	assert(inst);
-	return &inst->tree;
+	return inst->cmds;
 }