Browse Source

plugin-klish: help function for COMMAND ptype

Serj Kalichev 1 year ago
parent
commit
80d41e5aa4
4 changed files with 52 additions and 5 deletions
  1. 9 5
      examples/test/ptypes.xml
  2. 2 0
      plugins/klish/plugin_init.c
  3. 5 0
      plugins/klish/private.h
  4. 36 0
      plugins/klish/ptypes.c

+ 9 - 5
examples/test/ptypes.xml

@@ -5,14 +5,18 @@
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xsi:schemaLocation="http://clish.sourceforge.net/XMLSchema http://clish.sourceforge.net/XMLSchema/clish.xsd">
 
-<!-- PTYPEs -->
 
 <ENTRY name="COMMAND" purpose="ptype">
-	<ACTION sym="COMMAND" exec_on="always"/>
+	<ENTRY name="completion" purpose="completion">
+		<ACTION sym="completion_COMMAND"/>
+	</ENTRY>
+	<ENTRY name="help" purpose="help">
+		<ACTION sym="help_COMMAND"/>
+	</ENTRY>
+	<ACTION sym="COMMAND"/>
 </ENTRY>
 
-<ENTRY name="SUBCOMMAND" purpose="ptype">
-	<ACTION sym="COMMAND" exec_on="always"/>
-</ENTRY>
+<ENTRY name="SUBCOMMAND" purpose="ptype" ref="/COMMAND"/>
+
 
 </KLISH>

+ 2 - 0
plugins/klish/plugin_init.c

@@ -46,6 +46,8 @@ int kplugin_klish_init(kcontext_t *context)
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("completion_COMMAND", klish_completion_COMMAND,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("help_COMMAND", klish_help_COMMAND,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("COMMAND_CASE", klish_ptype_COMMAND_CASE,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("INT", klish_ptype_INT,

+ 5 - 0
plugins/klish/private.h

@@ -23,9 +23,14 @@ int klish_nav(kcontext_t *context);
 // PTYPEs
 int klish_ptype_COMMAND(kcontext_t *context);
 int klish_completion_COMMAND(kcontext_t *context);
+int klish_help_COMMAND(kcontext_t *context);
+
 int klish_ptype_COMMAND_CASE(kcontext_t *context);
+
 int klish_ptype_INT(kcontext_t *context);
+
 int klish_ptype_UINT(kcontext_t *context);
+
 int klish_ptype_STRING(kcontext_t *context);
 
 

+ 36 - 0
plugins/klish/ptypes.c

@@ -67,6 +67,42 @@ int klish_completion_COMMAND(kcontext_t *context)
 }
 
 
+/** @brief HELP: Consider ENTRY's name (or "value" field) as a command
+ *
+ * This help function has main ENTRY that is a child of COMMAND ptype
+ * ENTRY. The PTYPE entry has specific ENTRY (with name and possible value)
+ * as a parent.
+ *
+ * command (COMMON ENTRY) with name or value
+ *     ptype (PTYPE ENTRY)
+ *         help (HELP ENTRY) - start point
+ */
+int klish_help_COMMAND(kcontext_t *context)
+{
+	const kentry_t *entry = NULL;
+	const char *command_name = NULL;
+	const char *help_text = NULL;
+
+	entry = kcontext_candidate_entry(context);
+
+	command_name = kentry_value(entry);
+	if (!command_name)
+		command_name = kentry_name(entry);
+	assert(command_name);
+
+	help_text = kentry_help(entry);
+	if (!help_text)
+		help_text = kentry_value(entry);
+	if (!help_text)
+		help_text = kentry_name(entry);
+	assert(help_text);
+
+	printf("%s\n%s\n", command_name, help_text);
+
+	return 0;
+}
+
+
 /** @brief PTYPE: ENTRY's name (or "value" field) as a case sensitive command
  */
 int klish_ptype_COMMAND_CASE(kcontext_t *context)