Browse Source

UNFINISHED: Work on command_t

Serj Kalichev 6 years ago
parent
commit
ea84acb2be
6 changed files with 56 additions and 93 deletions
  1. 5 9
      clish/command.h
  2. 9 31
      clish/command/command.c
  3. 5 6
      clish/command/private.h
  4. 6 13
      clish/shell/shell_xml.c
  5. 2 2
      clish/view/private.h
  6. 29 32
      clish/view/view.c

+ 5 - 9
clish/command.h

@@ -7,7 +7,6 @@
 
 typedef struct clish_command_s clish_command_t;
 
-#include "lub/bintree.h"
 #include "lub/argv.h"
 #include "clish/types.h"
 #include "clish/macros.h"
@@ -19,18 +18,15 @@ typedef struct clish_command_s clish_command_t;
 
 clish_command_t *clish_command_new(const char *name, const char *help);
 clish_command_t *clish_command_new_link(const char *name,
-	const char *help, const clish_command_t * ref);
+	const char *help, const clish_command_t *ref);
 clish_command_t * clish_command_alias_to_link(clish_command_t *instance, clish_command_t *ref);
 
-int clish_command_bt_compare(const void *clientnode, const void *clientkey);
-void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key);
-size_t clish_command_bt_offset(void);
-clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
+int clish_command_compare(const void *clientnode, const void *clientkey);
+clish_command_t *clish_command_choose_longest(clish_command_t *cmd1,
 	clish_command_t * cmd2);
-int
-clish_command_diff(const clish_command_t * cmd1, const clish_command_t * cmd2);
+int clish_command_diff(const clish_command_t *cmd1, const clish_command_t *cmd2);
 
-void clish_command_delete(clish_command_t *instance);
+void clish_command_delete(void *instance);
 void clish_command_insert_param(clish_command_t *instance,
 	clish_param_t *param);
 int clish_command_help(const clish_command_t *instance);

+ 9 - 31
clish/command/command.c

@@ -14,17 +14,13 @@
 #include <string.h>
 #include <stdio.h>
 
-static void
-clish_command_init(clish_command_t *this, const char *name, const char *text)
+static void clish_command_init(clish_command_t *this,
+	const char *name, const char *text)
 {
-	/* initialise the node part */
 	this->name = lub_string_dup(name);
 	this->text = lub_string_dup(text);
 
-	/* Be a good binary tree citizen */
-	lub_bintree_node_init(&this->bt_node);
-
-	/* set up defaults */
+	/* Defaults */
 	this->link = NULL;
 	this->alias = NULL;
 	this->alias_view = NULL;
@@ -71,27 +67,11 @@ static void clish_command_fini(clish_command_t * this)
 }
 
 /*--------------------------------------------------------- */
-size_t clish_command_bt_offset(void)
-{
-	return offsetof(clish_command_t, bt_node);
-}
-
-/*--------------------------------------------------------- */
-int clish_command_bt_compare(const void *clientnode, const void *clientkey)
+int clish_command_compare(const void *first, const void *second)
 {
-	const clish_command_t *this = clientnode;
-	const char *key = clientkey;
-
-	return lub_string_nocasecmp(this->name, key);
-}
-
-/*--------------------------------------------------------- */
-void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
-{
-	const clish_command_t *this = clientnode;
-
-	/* fill out the opaque key */
-	strcpy((char *)key, this->name);
+	const clish_command_t *f = (const clish_command_t *)first;
+	const clish_command_t *s = (const clish_command_t *)second;
+	return lub_string_nocasecmp(f->name, s->name);
 }
 
 /*--------------------------------------------------------- */
@@ -121,8 +101,6 @@ clish_command_t *clish_command_new_link(const char *name,
 	this->name = lub_string_dup(name);
 	/* Initialise the help (other than original help) */
 	this->text = lub_string_dup(help);
-	/* Be a good binary tree citizen */
-	lub_bintree_node_init(&this->bt_node);
 	/* It a link to command so set the link flag */
 	this->link = ref;
 
@@ -140,7 +118,6 @@ clish_command_t * clish_command_alias_to_link(clish_command_t *this, clish_comma
 		return NULL;
 	memcpy(&tmp, this, sizeof(tmp));
 	*this = *ref;
-	memcpy(&this->bt_node, &tmp.bt_node, sizeof(tmp.bt_node));
 	this->name = lub_string_dup(tmp.name); /* Save an original name */
 	this->text = lub_string_dup(tmp.text); /* Save an original help */
 	this->link = ref;
@@ -151,8 +128,9 @@ clish_command_t * clish_command_alias_to_link(clish_command_t *this, clish_comma
 }
 
 /*--------------------------------------------------------- */
-void clish_command_delete(clish_command_t * this)
+void clish_command_delete(void *data)
 {
+	clish_command_t *this = (clish_command_t *)data;
 	clish_command_fini(this);
 	free(this);
 }

+ 5 - 6
clish/command/private.h

@@ -5,22 +5,21 @@
 #include "clish/command.h"
 
 struct clish_command_s {
-	lub_bintree_node_t bt_node;
 	char *name;
 	char *text;
-	clish_paramv_t *paramv;
-	clish_action_t *action;
-	clish_config_t *config;
 	char *viewname;
 	char *viewid;
 	char *detail;
 	char *escape_chars;
 	char *regex_chars;
 	char *access;
-	clish_param_t *args;
-	const struct clish_command_s *link;
 	char *alias_view;
 	char *alias;
+	clish_paramv_t *paramv;
+	clish_action_t *action;
+	clish_config_t *config;
+	clish_param_t *args;
+	const struct clish_command_s *link;
 	clish_view_t *pview;
 	bool_t dynamic; /* Is command dynamically created */
 	bool_t internal; /* Is command internal? Like the "startup" */

+ 6 - 13
clish/shell/shell_xml.c

@@ -464,7 +464,6 @@ static int process_command(clish_shell_t *shell, clish_xmlnode_t *element,
 {
 	clish_view_t *v = (clish_view_t *) parent;
 	clish_command_t *cmd = NULL;
-	clish_command_t *old;
 	int res = -1;
 
 	char *access = clish_xmlnode_fetch_attr(element, "access");
@@ -492,17 +491,13 @@ static int process_command(clish_shell_t *shell, clish_xmlnode_t *element,
 		goto error;
 	}
 
-	/* check this command doesn't already exist */
-	old = clish_view_find_command(v, name, BOOL_FALSE);
-	if (old) {
+	/* Create a command with unique name */
+	cmd = clish_view_new_command(v, name, help);
+	if (!cmd) {
 		fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate COMMAND name=\"%s\".\n", name);
 		goto error;
 	}
 
-	/* create a command */
-	cmd = clish_view_new_command(v, name, help);
-	clish_command__set_pview(cmd, v);
-
 	/* Reference 'ref' field */
 	if (ref) {
 		char *saveptr = NULL;
@@ -597,7 +592,6 @@ error:
 static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
 	void *parent)
 {
-	clish_view_t *v = (clish_view_t *) parent;
 	clish_command_t *cmd = NULL;
 	int res = -1;
 
@@ -626,7 +620,7 @@ static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
 	}
 
 	/* create a command with NULL help */
-	cmd = clish_view_new_command(v, "startup", NULL);
+	cmd = clish_command_new("startup", NULL);
 	clish_command__set_internal(cmd, BOOL_TRUE);
 
 	/* reference the next view */
@@ -1156,7 +1150,6 @@ error:
 static int process_wdog(clish_shell_t *shell,
 	clish_xmlnode_t *element, void *parent)
 {
-	clish_view_t *v = (clish_view_t *)parent;
 	clish_command_t *cmd = NULL;
 	int res = -1;
 
@@ -1166,8 +1159,8 @@ static int process_wdog(clish_shell_t *shell,
 		goto error;
 	}
 
-	/* Create a command with NULL help */
-	cmd = clish_view_new_command(v, "watchdog", NULL);
+	/* Create a command for WDOG */
+	cmd = clish_command_new("watchdog", NULL);
 #ifdef LEGACY
 	// Legacy watchdog has lockless ACTION
 	clish_action__set_lock(clish_command__get_action(cmd), BOOL_FALSE);

+ 2 - 2
clish/view/private.h

@@ -2,15 +2,15 @@
  * view.h
  */
 #include "clish/view.h"
-#include "lub/bintree.h"
 #include "lub/list.h"
 #include "clish/hotkey.h"
 
 struct clish_view_s {
-	lub_bintree_t tree;
 	char *name;
 	char *prompt;
 	char *access;
+	lub_list_t *cmds;
+	lub_list_t *vars;
 	lub_list_t *nspaces;
 	clish_hotkeyv_t *hotkeys;
 	unsigned int depth;

+ 29 - 32
clish/view/view.c

@@ -25,19 +25,19 @@ int clish_view_compare(const void *first, const void *second)
 /*-------------------------------------------------------- */
 static void clish_view_init(clish_view_t * this, const char *name)
 {
-	/* set up defaults */
 	this->name = lub_string_dup(name);
 	this->prompt = NULL;
 	this->depth = 0;
 	this->restore = CLISH_RESTORE_NONE;
 	this->access = NULL;
 
-	/* initialise the tree of commands for this view */
-	lub_bintree_init(&this->tree,
-		clish_command_bt_offset(),
-		clish_command_bt_compare, clish_command_bt_getkey);
+	/* Init COMMAND list */
+	this->cmds = lub_list_new(clish_command_compare, clish_command_delete);
 
-	/* Initialise the list of namespaces.
+	/* Init VAR list */
+	this->vars = lub_list_new(clish_command_compare, clish_command_delete);
+
+	/* Initialise the list of NAMESPACEs.
 	 * It's important to add new items to the
 	 * tail of list.
 	 */
@@ -50,23 +50,18 @@ static void clish_view_init(clish_view_t * this, const char *name)
 /*--------------------------------------------------------- */
 static void clish_view_fini(clish_view_t * this)
 {
-	clish_command_t *cmd;
+	/* Free COMMAND list */
+	lub_list_free_all(this->cmds);
 
-	/* delete each command held by this view */
-	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);
-	}
+	/* Free VAR list */
+	lub_list_free_all(this->vars);
 
-	/* Free namespaces list */
+	/* Free NAMESPACE list */
 	lub_list_free_all(this->nspaces);
 
 	/* Free hotkey structures */
 	clish_hotkeyv_delete(this->hotkeys);
 
-	/* free our memory */
 	lub_string_free(this->name);
 	lub_string_free(this->prompt);
 	lub_string_free(this->access);
@@ -91,21 +86,16 @@ void clish_view_delete(void *data)
 }
 
 /*--------------------------------------------------------- */
-clish_command_t *clish_view_new_command(clish_view_t * this,
+/* Create new command and add it to list of commands */
+clish_command_t *clish_view_new_command(clish_view_t *this,
 	const char *name, const char *help)
 {
-	/* allocate the memory for a new parameter definition */
 	clish_command_t *cmd = clish_command_new(name, help);
 	assert(cmd);
-
-	/* if this is a command other than the startup command... */
-	if (NULL != help) {
-		/* ...insert it into the binary tree for this view */
-		if (-1 == lub_bintree_insert(&this->tree, cmd)) {
-			/* inserting a duplicate command is bad */
-			clish_command_delete(cmd);
-			cmd = NULL;
-		}
+	clish_command__set_pview(cmd, this);
+	if (!lub_list_add_uniq(this->cmds, cmd)) {
+		clish_command_delete(cmd);
+		cmd = NULL;
 	}
 	return cmd;
 }
@@ -119,13 +109,13 @@ clish_command_t *clish_view_new_command(clish_view_t * this,
  * this - the view instance upon which to operate
  * line - the command line to analyse 
  */
-clish_command_t *clish_view_resolve_prefix(clish_view_t * this,
+clish_command_t *clish_view_resolve_prefix(clish_view_t *this,
 	const char *line, bool_t inherit)
 {
 	clish_command_t *result = NULL, *cmd;
 	char *buffer = NULL;
 	lub_argv_t *argv;
-	unsigned i;
+	unsigned int i;
 
 	/* create a vector of arguments */
 	argv = lub_argv_new(line, 0);
@@ -178,13 +168,20 @@ clish_command_t *clish_view_resolve_command(clish_view_t *this,
 }
 
 /*--------------------------------------------------------- */
-clish_command_t *clish_view_find_command(clish_view_t * this,
+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)
 {
 	clish_command_t *result = NULL;
 
 	/* Search the current view */
-	result = lub_bintree_find(&this->tree, name);
+	result = lub_list_find(this->cmds, cmd_by_name, name);
 
 	if (inherit) {
 		lub_list_node_t *iter;
@@ -192,7 +189,7 @@ clish_command_t *clish_view_find_command(clish_view_t * this,
 		clish_nspace_t *nspace;
 
 		/* Iterate elements. It's important to iterate
-		 * items starting from tail bacause the next
+		 * items starting from tail because the next
 		 * NAMESPACE has higher priority than previous one
 		 * in a case then the both NAMESPACEs have the
 		 * commands with the same name.