Browse Source

Add 'completion' field to PARAM

git-svn-id: https://klish.googlecode.com/svn/trunk@437 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
0057cd09fb

+ 1 - 0
clish.xsd

@@ -312,6 +312,7 @@
         <xs:attribute name="value" type="xs:string" use="optional"/>
         <xs:attribute name="hidden" type="bool_t" use="optional" default="false"/>
         <xs:attribute name="test" type="xs:string" use="optional"/>
+        <xs:attribute name="completion" type="xs:string" use="optional"/>
     </xs:complexType>
     <!--
 ********************************************************

+ 2 - 0
clish/param.h

@@ -84,6 +84,8 @@ void clish_param__set_hidden(clish_param_t * instance, bool_t hidden);
 bool_t clish_param__get_hidden(const clish_param_t * instance);
 void clish_param__set_test(clish_param_t * instance, const char *test);
 char *clish_param__get_test(const clish_param_t *instance);
+void clish_param__set_completion(clish_param_t *instance, const char *completion);
+char *clish_param__get_completion(const clish_param_t *instance);
 
 /* paramv methods */
 clish_paramv_t *clish_paramv_new(void);

+ 15 - 5
clish/param/param.c

@@ -31,6 +31,7 @@ static void clish_param_init(clish_param_t *this, const char *name,
 	this->value = NULL;
 	this->hidden = BOOL_FALSE;
 	this->test = NULL;
+	this->completion = NULL;
 
 	this->paramv = clish_paramv_new();
 }
@@ -40,15 +41,11 @@ static void clish_param_fini(clish_param_t * this)
 {
 	/* deallocate the memory for this instance */
 	lub_string_free(this->defval);
-	this->defval = NULL;
 	lub_string_free(this->name);
-	this->name = NULL;
 	lub_string_free(this->text);
-	this->text = NULL;
 	lub_string_free(this->value);
-	this->value = NULL;
 	lub_string_free(this->test);
-	this->test = NULL;
+	lub_string_free(this->completion);
 
 	clish_paramv_delete(this->paramv);
 }
@@ -364,3 +361,16 @@ char *clish_param__get_test(const clish_param_t *this)
 {
 	return this->test;
 }
+
+/*--------------------------------------------------------- */
+void clish_param__set_completion(clish_param_t *this, const char *completion)
+{
+	assert(!this->completion);
+	this->completion = lub_string_dup(completion);
+}
+
+/*--------------------------------------------------------- */
+char *clish_param__get_completion(const clish_param_t *this)
+{
+	return this->completion;
+}

+ 11 - 10
clish/param/param_dump.c

@@ -12,11 +12,11 @@ void clish_param_dump(const clish_param_t * this)
 	lub_dump_printf("param(%p)\n", this);
 
 	lub_dump_indent();
-	lub_dump_printf("name     : %s\n", this->name);
-	lub_dump_printf("text     : %s\n", this->text);
-	lub_dump_printf("value    : %s\n", this->value);
-	lub_dump_printf("ptype    : %s\n", clish_ptype__get_name(this->ptype));
-	lub_dump_printf("default  : %s\n",
+	lub_dump_printf("name       : %s\n", this->name);
+	lub_dump_printf("text       : %s\n", this->text);
+	lub_dump_printf("value      : %s\n", this->value);
+	lub_dump_printf("ptype      : %s\n", clish_ptype__get_name(this->ptype));
+	lub_dump_printf("default    : %s\n",
 		this->defval ? this->defval : "(null)");
 	switch (this->mode) {
 	case CLISH_PARAM_COMMON:
@@ -32,13 +32,14 @@ void clish_param_dump(const clish_param_t * this)
 		mode = "Unknown";
 		break;
 	}
-	lub_dump_printf("mode     : %s\n", mode);
-	lub_dump_printf("paramc   : %d\n", clish_paramv__get_count(this->paramv));
-	lub_dump_printf("optional : %s\n",
+	lub_dump_printf("mode       : %s\n", mode);
+	lub_dump_printf("paramc     : %d\n", clish_paramv__get_count(this->paramv));
+	lub_dump_printf("optional   : %s\n",
 		this->optional ? "true" : "false");
-	lub_dump_printf("hidden   : %s\n",
+	lub_dump_printf("hidden     : %s\n",
 		this->hidden ? "true" : "false");
-	lub_dump_printf("test     : %s\n", this->test);
+	lub_dump_printf("test       : %s\n", this->test);
+	lub_dump_printf("completion : %s\n", this->completion);
 
 	/* Get each parameter to dump their details */
 	for (i = 0; i < clish_paramv__get_count(this->paramv); i++) {

+ 2 - 1
clish/param/private.h

@@ -22,5 +22,6 @@ struct clish_param_s {
 	clish_param_mode_e mode;
 	bool_t optional;
 	bool_t hidden;
-	char *test; /* the condition */
+	char *test; /* The condition to enable param */
+	char *completion; /* Possible completions */
 };

+ 14 - 1
clish/shell/shell_command.c

@@ -102,7 +102,6 @@ void clish_shell_param_generator(clish_shell_t *this, lub_argv_t *matches,
 		clish_shell_parse_pargv(pargv, cmd, &context,
 			clish_command__get_paramv(cmd),
 			argv, &idx, completion, index + idx);
-		clish_pargv_delete(pargv);
 		lub_argv_delete(argv);
 
 		while ((param = clish_pargv__get_param(completion,
@@ -121,11 +120,25 @@ void clish_shell_param_generator(clish_shell_t *this, lub_argv_t *matches,
 				if (result)
 					lub_argv_add(matches, result);
 			}
+			/* The 'completion' field of PARAM */
+			if (clish_param__get_completion(param)) {
+				char *str, *q;
+				char *saveptr;
+				str = clish_shell_expand(
+					clish_param__get_completion(param), &context);
+				for (q = strtok_r(str, " \n", &saveptr);
+					q; q = strtok_r(NULL, " \n", &saveptr)) {
+					if (q == strstr(q, text))
+						lub_argv_add(matches, q);
+				}
+				lub_string_free(str);
+			}
 			/* The common PARAM. Let ptype do the work */
 			if ((ptype = clish_param__get_ptype(param)))
 				clish_ptype_word_generator(ptype, matches, text);
 		}
 		clish_pargv_delete(completion);
+		clish_pargv_delete(pargv);
 	}
 
 	lub_string_free(text);

+ 6 - 4
clish/shell/shell_tinyxml.cpp

@@ -344,9 +344,9 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	clish_command_t *cmd = NULL;
 	clish_param_t *p_param = NULL;
 	if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
-		p_param = (clish_param_t *) parent;
+		p_param = (clish_param_t *)parent;
 	else
-		cmd = (clish_command_t *) parent;
+		cmd = (clish_command_t *)parent;
 
 	if (cmd || p_param) {
 		assert((!cmd) || (cmd != shell->startup));
@@ -360,6 +360,7 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		const char *value = element->Attribute("value");
 		const char *hidden = element->Attribute("hidden");
 		const char *test = element->Attribute("test");
+		const char *completion = element->Attribute("completion");
 		clish_param_t *param;
 		clish_ptype_t *tmp = NULL;
 
@@ -398,7 +399,7 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 			assert(tmp);
 			opt_param = clish_param_new(prefix, help, tmp);
 			clish_param__set_mode(opt_param,
-					      CLISH_PARAM_SUBCOMMAND);
+				CLISH_PARAM_SUBCOMMAND);
 			clish_param__set_optional(opt_param, BOOL_TRUE);
 
 			if (test)
@@ -451,6 +452,8 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 
 		if (test && !prefix)
 			clish_param__set_test(param, test);
+		if (completion)
+			clish_param__set_completion(param, completion);
 
 		if (cmd)
 			// add the parameter to the command
@@ -460,7 +463,6 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 			clish_param_insert_param(p_param, param);
 
 		process_children(shell, element, param);
-
 	}
 }
 

+ 1 - 1
clish/shell/shell_view.c

@@ -12,7 +12,7 @@ clish_view_t *clish_shell_find_create_view(clish_shell_t * this,
 {
 	clish_view_t *view = lub_bintree_find(&this->view_tree, name);
 
-	if (NULL == view) {
+	if (!view) {
 		/* create a view */
 		view = clish_view_new(name, prompt);
 		assert(view);