Browse Source

Use lub_list_t for VARs

Serj Kalichev 6 years ago
parent
commit
89d94f521a

+ 2 - 4
clish/shell/private.h

@@ -1,7 +1,6 @@
 /*
  * shell/private.h - private interface to the shell class
  */
-#include "lub/bintree.h"
 #include "lub/list.h"
 #include "tinyrl/tinyrl.h"
 #include "clish/shell.h"
@@ -30,7 +29,7 @@ struct clish_shell_file_s {
 typedef struct {
 	char *line;
 	clish_view_t *view;
-	lub_bintree_t viewid;
+	lub_list_t *viewid;
 	clish_pargv_t *pargv; /* Saved pargv structure */
 	char *cmd; /* Command name without prefix */
 	char *prefix; /* Prefix string if exists */
@@ -48,7 +47,6 @@ struct clish_context_s {
 struct clish_shell_s {
 	lub_list_t *view_tree; /* VIEW list */
 	lub_list_t *ptype_tree; /* PTYPE list */
-	lub_bintree_t var_tree; /* Tree of global variables */
 
 	/* Hooks */
 	clish_sym_t *hooks[CLISH_SYM_TYPE_MAX]; /* Callback hooks */
@@ -139,7 +137,7 @@ void clish_shell_param_generator(clish_shell_t * instance, lub_argv_t *matches,
 	const clish_command_t * cmd, const char *line, unsigned offset);
 char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
 	const char *line, unsigned start, unsigned end);
-void clish_shell__expand_viewid(const char *viewid, lub_bintree_t *tree,
+void clish_shell__expand_viewid(const char *viewid, lub_list_t *vars,
 	clish_context_t *context);
 void clish_shell__init_pwd(clish_shell_pwd_t *pwd);
 void clish_shell__fini_pwd(clish_shell_pwd_t *pwd);

+ 0 - 12
clish/shell/shell_new.c

@@ -29,11 +29,6 @@ static void clish_shell_init(clish_shell_t * this,
 	/* Init PTYPE list */
 	this->ptype_tree = lub_list_new(clish_ptype_compare, clish_ptype_free);
 
-	/* initialise the tree of vars */
-	lub_bintree_init(&this->var_tree,
-		clish_var_bt_offset(),
-		clish_var_bt_compare, clish_var_bt_getkey);
-
 	/* Initialize plugin list */
 	this->plugins = lub_list_new(NULL, clish_plugin_free);
 
@@ -97,7 +92,6 @@ static void clish_shell_init(clish_shell_t * this,
 /*--------------------------------------------------------- */
 static void clish_shell_fini(clish_shell_t *this)
 {
-	clish_var_t *var;
 	unsigned int i;
 
 	/* Free all loaded plugins */
@@ -109,12 +103,6 @@ static void clish_shell_fini(clish_shell_t *this)
 	/* Delete each PTYPE  */
 	lub_list_free_all(this->ptype_tree);
 
-	/* delete each VAR held  */
-	while ((var = lub_bintree_findfirst(&this->var_tree))) {
-		lub_bintree_remove(&this->var_tree, var);
-		clish_var_delete(var);
-	}
-
 	/* Free empty hooks */
 	for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
 		if (clish_sym__get_name(this->hooks[i]))

+ 7 - 12
clish/shell/shell_pwd.c

@@ -17,28 +17,23 @@ void clish_shell__init_pwd(clish_shell_pwd_t *pwd)
 	pwd->pargv = NULL;
 	pwd->cmd = NULL;
 	pwd->prefix = NULL;
-	/* initialise the tree of vars */
-	lub_bintree_init(&pwd->viewid,
-		clish_var_bt_offset(),
-		clish_var_bt_compare, clish_var_bt_getkey);
+
+	/* Init VARs */
+	pwd->viewid = lub_list_new(clish_var_compare, clish_var_delete);
 }
 
 /*--------------------------------------------------------- */
 void clish_shell__fini_pwd(clish_shell_pwd_t *pwd)
 {
-	clish_var_t *var;
-
 	lub_string_free(pwd->line);
 	lub_string_free(pwd->cmd);
 	if (pwd->prefix)
 		lub_string_free(pwd->prefix);
 	pwd->view = NULL;
 	clish_pargv_delete(pwd->pargv);
-	/* delete each VAR held  */
-	while ((var = lub_bintree_findfirst(&pwd->viewid))) {
-		lub_bintree_remove(&pwd->viewid, var);
-		clish_var_delete(var);
-	}
+
+	/* Free VARs  */
+	lub_list_free_all(pwd->viewid);
 }
 
 /*--------------------------------------------------------- */
@@ -88,7 +83,7 @@ void clish_shell__set_pwd(clish_shell_t *this,
 				newpwd->prefix = lub_string_dupn(full_cmd_name, len - 1);
 		}
 	}
-	clish_shell__expand_viewid(viewid, &newpwd->viewid, context);
+	clish_shell__expand_viewid(viewid, newpwd->viewid, context);
 	clish_shell__fini_pwd(this->pwdv[index]);
 	free(this->pwdv[index]);
 	this->pwdv[index] = newpwd;

+ 1 - 1
clish/shell/shell_startup.c

@@ -250,7 +250,7 @@ int clish_shell_prepare(clish_shell_t *this)
 		}
 
 		/* Iterate the COMMANDs */
-		cmd_tree = clish_view__get_tree(view);
+		cmd_tree = clish_view__get_cmds(view);
 		cmd_iter = lub_list_iterator_init(cmd_tree);
 		while (cmd_iter) {
 			int cmd_is_alias;

+ 6 - 6
clish/shell/shell_var.c

@@ -17,7 +17,7 @@
 /*
  * search the current viewid string for a variable
  */
-void clish_shell__expand_viewid(const char *viewid, lub_bintree_t *tree,
+void clish_shell__expand_viewid(const char *viewid, lub_list_t *vars,
 	clish_context_t *context)
 {
 	char *expanded;
@@ -40,7 +40,7 @@ void clish_shell__expand_viewid(const char *viewid, lub_bintree_t *tree,
 		value++;
 		/* Create var instance */
 		var = clish_var_new(q);
-		lub_bintree_insert(tree, var);
+		lub_list_add(vars, var);
 		clish_var__set_value(var, value);
 	}
 	lub_string_free(expanded);
@@ -148,9 +148,9 @@ static char *find_context_var(const char *name, clish_context_t *this)
 }
 
 /*--------------------------------------------------------- */
-static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *context)
+static char *find_var(const char *name, lub_list_t *vars, clish_context_t *context)
 {
-	clish_var_t *var = lub_bintree_find(tree, name);
+	clish_var_t *var = lub_list_find(vars, clish_var_fn_find_by_name, name);
 	const char *value;
 	bool_t dynamic;
 	char *res = NULL;
@@ -195,7 +195,7 @@ static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *co
 static char *find_global_var(const char *name, clish_context_t *context)
 {
 	clish_shell_t *shell = clish_context__get_shell(context);
-	return find_var(name, &shell->var_tree, context);
+	return find_var(name, clish_view__get_vars(shell->global), context);
 }
 
 /*--------------------------------------------------------- */
@@ -205,7 +205,7 @@ static char *find_viewid_var(const char *name, clish_context_t *context)
 	int depth = clish_shell__get_depth(shell);
 	if (depth < 0)
 		return NULL;
-	return find_var(name, &shell->pwdv[depth]->viewid, context);
+	return find_var(name, shell->pwdv[depth]->viewid, context);
 }
 
 static char * chardiff(const char *syms, const char *minus)

+ 4 - 2
clish/shell/shell_xml.c

@@ -1106,6 +1106,8 @@ static int process_config(clish_shell_t *shell, clish_xmlnode_t *element,
 static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
 	void *parent)
 {
+	clish_view_t *v = (clish_view_t *)parent;
+	lub_list_t *vars = clish_view__get_vars(v);
 	clish_var_t *var = NULL;
 	int res = -1;
 
@@ -1119,7 +1121,7 @@ static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
 		goto error;
 	}
 	/* Check if this var doesn't already exist */
-	var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
+	var = lub_list_find(vars, clish_var_fn_find_by_name, name);
 	if (var) {
 		fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
 		goto error;
@@ -1127,7 +1129,7 @@ static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
 
 	/* Create var instance */
 	var = clish_var_new(name);
-	lub_bintree_insert(&shell->var_tree, var);
+	lub_list_add(vars, var);
 
 	if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
 		clish_var__set_dynamic(var, BOOL_TRUE);

+ 4 - 1
clish/var.h

@@ -11,9 +11,12 @@ int clish_var_bt_compare(const void *clientnode, const void *clientkey);
 void clish_var_bt_getkey(const void *clientnode, lub_bintree_key_t * key);
 size_t clish_var_bt_offset(void);
 clish_var_t *clish_var_new(const char *name);
-void clish_var_delete(clish_var_t *instance);
+void clish_var_delete(void *instance);
 void clish_var_dump(const clish_var_t *instance);
 
+int clish_var_compare(const void *first, const void *second);
+int clish_var_fn_find_by_name(const void *key, const void *data);
+
 _CLISH_GET_STR(var, name);
 _CLISH_SET(var, bool_t, dynamic);
 _CLISH_GET(var, bool_t, dynamic);

+ 17 - 1
clish/var/var.c

@@ -67,8 +67,9 @@ clish_var_t *clish_var_new(const char *name)
 }
 
 /*--------------------------------------------------------- */
-void clish_var_delete(clish_var_t *this)
+void clish_var_delete(void *data)
 {
+	clish_var_t *this = (clish_var_t *)data;
 	clish_var_fini(this);
 	free(this);
 }
@@ -81,3 +82,18 @@ CLISH_GET_STR(var, value);
 CLISH_SET_STR(var, saved);
 CLISH_GET_STR(var, saved);
 CLISH_GET(var, clish_action_t *, action);
+
+/*--------------------------------------------------------- */
+int clish_var_compare(const void *first, const void *second)
+{
+	const clish_var_t *f = (const clish_var_t *)first;
+	const clish_var_t *s = (const clish_var_t *)second;
+	return lub_string_nocasecmp(f->name, s->name);
+}
+
+/*--------------------------------------------------------- */
+int clish_var_fn_find_by_name(const void *key, const void *data) {
+	const char *name = (const char *)key;
+	const clish_var_t *d = (const clish_var_t *)data;
+	return lub_string_nocasecmp(name, clish_var__get_name(d));
+}

+ 2 - 2
clish/view.h

@@ -50,6 +50,8 @@ int clish_view_insert_hotkey(const clish_view_t *instance, const char *key, cons
 const char *clish_view_find_hotkey(const clish_view_t *instance, int code);
 
 _CLISH_GET(view, lub_list_t *, nspaces);
+_CLISH_GET(view, lub_list_t *, cmds);
+_CLISH_GET(view, lub_list_t *, vars);
 _CLISH_GET_STR(view, name);
 _CLISH_SET_STR_ONCE(view, prompt);
 _CLISH_GET_STR(view, prompt);
@@ -60,7 +62,5 @@ _CLISH_GET(view, unsigned int, depth);
 _CLISH_SET(view, clish_view_restore_e, restore);
 _CLISH_GET(view, clish_view_restore_e, restore);
 
-lub_list_t * clish_view__get_tree(clish_view_t *instance);
-
 #endif				/* _clish_view_h */
 /** @} clish_view */

+ 3 - 8
clish/view/view.c

@@ -35,7 +35,7 @@ static void clish_view_init(clish_view_t * this, const char *name)
 	this->cmds = lub_list_new(clish_command_compare, clish_command_delete);
 
 	/* Init VAR list */
-	this->vars = lub_list_new(clish_command_compare, clish_command_delete);
+	this->vars = lub_list_new(clish_var_compare, clish_var_delete);
 
 	/* Initialise the list of NAMESPACEs.
 	 * It's important to add new items to the
@@ -299,6 +299,8 @@ const char *clish_view_find_hotkey(const clish_view_t *this, int code)
 }
 
 CLISH_GET(view, lub_list_t *, nspaces);
+CLISH_GET(view, lub_list_t *, cmds);
+CLISH_GET(view, lub_list_t *, vars);
 CLISH_GET_STR(view, name);
 CLISH_SET_STR_ONCE(view, prompt);
 CLISH_GET_STR(view, prompt);
@@ -308,10 +310,3 @@ CLISH_SET(view, unsigned int, depth);
 CLISH_GET(view, unsigned int, depth);
 CLISH_SET(view, clish_view_restore_e, restore);
 CLISH_GET(view, clish_view_restore_e, restore);
-
-/*-------------------------------------------------------- */
-lub_list_t * clish_view__get_tree(clish_view_t *inst)
-{
-	assert(inst);
-	return inst->cmds;
-}