Browse Source

Completion field for PTYPE

Serj Kalichev 4 years ago
parent
commit
22f3fc6ef3
6 changed files with 31 additions and 1 deletions
  1. 6 0
      clish.xsd
  2. 13 1
      clish/param/param.c
  3. 2 0
      clish/ptype.h
  4. 1 0
      clish/ptype/private.h
  5. 4 0
      clish/ptype/ptype.c
  6. 5 0
      clish/shell/shell_xml.c

+ 6 - 0
clish.xsd

@@ -109,6 +109,11 @@
 *
 *	tolower - before validation convert to lowercase.
 *
+* completion - An optional field to define PTYPE completion. You can consider
+*	it as a default completion for the PARAM that uses this PTYPE. If PARAM
+*	has its own completion then PARAM's completion will be used. Else the
+*	PTYPE's completion will be used for PARAM.
+*
 ********************************************************
 -->
 	<xs:simpleType name="ptype_method_e">
@@ -139,6 +144,7 @@
 		<xs:attribute name="pattern" type="xs:string"/>
 		<xs:attribute name="method" type="ptype_method_e" use="optional" default="regexp"/>
 		<xs:attribute name="preprocess" type="ptype_preprocess_e" use="optional" default="none"/>
+		<xs:attribute name="completion" type="xs:string" use="optional"/>
 	</xs:complexType>
 
 <!--

+ 13 - 1
clish/param/param.c

@@ -146,7 +146,6 @@ CLISH_GET_STR(param, defval);
 CLISH_SET_STR_ONCE(param, test);
 CLISH_GET_STR(param, test);
 CLISH_SET_STR_ONCE(param, completion);
-CLISH_GET_STR(param, completion);
 CLISH_SET(param, clish_param_mode_e, mode);
 CLISH_GET(param, clish_param_mode_e, mode);
 CLISH_GET(param, clish_paramv_t *, paramv);
@@ -187,3 +186,16 @@ _CLISH_GET(param, unsigned int, param_count)
 	assert(inst);
 	return clish_paramv__get_count(inst->paramv);
 }
+
+/*--------------------------------------------------------- */
+_CLISH_GET_STR(param, completion)
+{
+	assert(inst);
+	if (inst->completion)
+		return inst->completion;
+	// If param has no 'completion' field then get completion from ptype
+	if (inst->ptype)
+		return clish_ptype__get_completion(inst->ptype);
+
+	return NULL;
+}

+ 2 - 0
clish/ptype.h

@@ -87,6 +87,8 @@ void clish_ptype_dump(clish_ptype_t * instance);
 _CLISH_GET_STR(ptype, name);
 _CLISH_SET_STR_ONCE(ptype, text);
 _CLISH_GET_STR(ptype, text);
+_CLISH_SET_STR_ONCE(ptype, completion);
+_CLISH_GET_STR(ptype, completion);
 _CLISH_SET_ONCE(ptype, clish_ptype_preprocess_e, preprocess);
 _CLISH_GET_STR(ptype, range);
 _CLISH_GET(ptype, clish_action_t *, action);

+ 1 - 0
clish/ptype/private.h

@@ -29,6 +29,7 @@ struct clish_ptype_s {
 	char *text;
 	char *pattern;
 	char *range;
+	char *completion; // Default completion for PARAMs of this PTYPE
 	clish_ptype_method_e method;
 	clish_ptype_preprocess_e preprocess;
 	unsigned int last_name; /* Index used for auto-completion */

+ 4 - 0
clish/ptype/ptype.c

@@ -31,6 +31,7 @@ static void clish_ptype_init(clish_ptype_t * this,
 	assert(name);
 	this->name = lub_string_dup(name);
 	this->text = NULL;
+	this->completion = NULL;
 	this->pattern = NULL;
 	this->preprocess = preprocess;
 	this->range = NULL;
@@ -79,6 +80,7 @@ static void clish_ptype_fini(clish_ptype_t * this)
 	lub_string_free(this->text);
 	lub_string_free(this->pattern);
 	lub_string_free(this->range);
+	lub_string_free(this->completion);
 	clish_action_delete(this->action);
 }
 
@@ -457,6 +459,8 @@ char *clish_ptype_translate(clish_ptype_t * this, const char *text)
 CLISH_GET_STR(ptype, name);
 CLISH_SET_STR_ONCE(ptype, text);
 CLISH_GET_STR(ptype, text);
+CLISH_SET_STR_ONCE(ptype, completion);
+CLISH_GET_STR(ptype, completion);
 CLISH_SET_ONCE(ptype, clish_ptype_preprocess_e, preprocess);
 CLISH_GET_STR(ptype, range);
 CLISH_GET(ptype, clish_action_t *, action);

+ 5 - 0
clish/shell/shell_xml.c

@@ -382,6 +382,7 @@ static int process_ptype(clish_shell_t *shell, clish_xmlnode_t *element,
 	char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
 	char *method_name = clish_xmlnode_fetch_attr(element, "method");
 	char *preprocess_name =	clish_xmlnode_fetch_attr(element, "preprocess");
+	char *completion = clish_xmlnode_fetch_attr(element, "completion");
 
 	/* Check syntax */
 	if (!name) {
@@ -403,6 +404,9 @@ static int process_ptype(clish_shell_t *shell, clish_xmlnode_t *element,
 	ptype = clish_shell_find_create_ptype(shell,
 		name, help, pattern, method, preprocess);
 
+	if (completion)
+		clish_ptype__set_completion(ptype, completion);
+
 	res = process_children(shell, element, ptype);
 error:
 	clish_xml_release(name);
@@ -410,6 +414,7 @@ error:
 	clish_xml_release(pattern);
 	clish_xml_release(method_name);
 	clish_xml_release(preprocess_name);
+	clish_xml_release(completion);
 
 	parent = parent; /* Happy compiler */