Browse Source

The succesfull implementation of command alias.

git-svn-id: https://klish.googlecode.com/svn/trunk@320 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
bfd4028980
5 changed files with 32 additions and 18 deletions
  1. 3 1
      clish/command.h
  2. 24 15
      clish/command/command.c
  3. 1 0
      clish/command/private.h
  4. 2 0
      clish/nspace/nspace.c
  5. 2 2
      clish/variable/variable_expand.c

+ 3 - 1
clish/command.h

@@ -28,7 +28,6 @@ typedef enum {
 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);
-clish_command_t *clish_command_new_link_from_alias(const clish_command_t * alias);
 clish_command_t * clish_command_alias_to_link(clish_command_t * instance);
 
 int clish_command_bt_compare(const void *clientnode, const void *clientkey);
@@ -106,6 +105,7 @@ clish_view_restore_t clish_command__get_restore(const clish_command_t * instance
 bool_t clish_command__get_unique(const clish_command_t * instance);
 void clish_command__set_unique(clish_command_t * instance, bool_t unique);
 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);
 void clish_command__set_cfg_depth(clish_command_t * instance, const char * cfg_depth);
 unsigned clish_command__get_cfg_depth(const clish_command_t * instance,
 	const char *viewid, clish_pargv_t * pargv);
@@ -119,5 +119,7 @@ const char * clish_command__get_alias(const clish_command_t * instance);
 void clish_command__set_alias_view(clish_command_t * instance,
 	clish_view_t * alias_view);
 clish_view_t * clish_command__get_alias_view(const clish_command_t * instance);
+void clish_command__set_dynamic(clish_command_t * instance, bool_t dynamic);
+bool_t clish_command__get_dynamic(const clish_command_t * instance);
 
 #endif				/* _clish_command_h */

+ 24 - 15
clish/command/command.c

@@ -39,6 +39,7 @@ clish_command_init(clish_command_t * this, const char *name, const char *text)
 	this->args = NULL;
 	this->pview = NULL;
 	this->lock = BOOL_TRUE;
+	this->dynamic = BOOL_FALSE;
 
 	/* ACTION params */
 	this->builtin = NULL;
@@ -162,21 +163,6 @@ clish_command_t *clish_command_new_link(const char *name,
 	return this;
 }
 
-/*--------------------------------------------------------- */
-clish_command_t *clish_command_new_link_from_alias(const clish_command_t * alias)
-{
-	clish_command_t * ref;
-
-	if (!alias->alias)
-		return NULL;
-	assert(alias->alias_view);
-	ref = clish_view_find_command(alias->alias_view, alias->alias, BOOL_FALSE);
-	if (!ref)
-		return NULL;
-
-	return clish_command_new_link(alias->name, alias->text, ref);
-}
-
 /*--------------------------------------------------------- */
 clish_command_t * clish_command_alias_to_link(clish_command_t * this)
 {
@@ -314,6 +300,8 @@ clish_command_diff(const clish_command_t * cmd1, const clish_command_t * cmd2)
  *--------------------------------------------------------- */
 const char *clish_command__get_name(const clish_command_t * this)
 {
+	if (!this)
+		return NULL;
 	return this->name;
 }
 
@@ -705,4 +693,25 @@ clish_view_t * clish_command__get_alias_view(const clish_command_t * this)
 	return this->alias_view;
 }
 
+/*--------------------------------------------------------- */
+void clish_command__set_dynamic(clish_command_t * this, bool_t dynamic)
+{
+	this->dynamic = dynamic;
+}
+
+/*--------------------------------------------------------- */
+bool_t clish_command__get_dynamic(const clish_command_t * this)
+{
+	return this->dynamic;
+}
+
+/*--------------------------------------------------------- */
+const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
+{
+	if (!this->dynamic)
+		return this;
+	if (this->link)
+		return clish_command__get_cmd(this->link);
+	return NULL;
+}
 

+ 1 - 0
clish/command/private.h

@@ -22,6 +22,7 @@ struct clish_command_s {
 	char *alias;
 	clish_view_t *pview;
 	bool_t lock;
+	bool_t dynamic; /* Is command dynamically created */
 
 	/* ACTION params: */
 	char *builtin;

+ 2 - 0
clish/nspace/nspace.c

@@ -100,6 +100,8 @@ static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
 	cmd = clish_command_new_link(name, help, ref);
 	free(name);
 	assert(cmd);
+	/* The command was created dynamically */
+	clish_command__set_dynamic(cmd, BOOL_TRUE);
 
 	/* Delete proxy commands with another prefixes */
 	tmp = lub_bintree_findfirst(&this->tree);

+ 2 - 2
clish/variable/variable_expand.c

@@ -65,7 +65,7 @@ static char *find_context_var(const context_t * this, const char *name)
 		result = lub_string_dup(clish_command__get_name(this->cmd));
 	} else if (!lub_string_nocasecmp(name, "__cmd")) {
 		result = lub_string_dup(clish_command__get_name(
-			clish_command__get_orig(this->cmd)));
+			clish_command__get_cmd(this->cmd)));
 	} else if (!lub_string_nocasecmp(name, "__orig_cmd")) {
 		result = lub_string_dup(clish_command__get_name(
 			clish_command__get_orig(this->cmd)));
@@ -80,7 +80,7 @@ static char *find_context_var(const context_t * this, const char *name)
 		int pnum = 0;
 		pnum = lub_argv_wordcount(clish_command__get_name(this->cmd)) -
 			lub_argv_wordcount(clish_command__get_name(
-			clish_command__get_orig(this->cmd)));
+			clish_command__get_cmd(this->cmd)));
 		idx = atoi(name + strlen("__prefix"));
 		if (idx < pnum) {
 			lub_argv_t *argv = lub_argv_new(