Browse Source

Fix help generation

Serj Kalichev 1 year ago
parent
commit
c3f55cc5c9
3 changed files with 39 additions and 8 deletions
  1. 9 6
      examples/simple/example.xml
  2. 2 0
      klish/ktp/help.c
  3. 28 2
      klish/ktp/ktpd_session.c

+ 9 - 6
examples/simple/example.xml

@@ -10,11 +10,20 @@
 	<ENTRY name="completion" purpose="completion">
 		<ACTION sym="completion_COMMAND"/>
 	</ENTRY>
+	<ENTRY name="help" purpose="help">
+		<ACTION sym="completion_COMMAND"/>
+	</ENTRY>
 	<ACTION sym="COMMAND"/>
 </ENTRY>
 
 <ENTRY name="main" mode="switch" container="true">
 
+<ENTRY name="exit" help="Exit view">
+	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+	<ACTION sym="nav">pop</ACTION>
+	<ACTION sym="print">Exiting klish session</ACTION>
+</ENTRY>
+
 <ENTRY name="cmd" help="Clear settings" mode="sequence">
 	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
 	<ENTRY name="first" help="Clear settings">
@@ -33,12 +42,6 @@
 	<ACTION sym="print">test2</ACTION>
 </ENTRY>
 
-<ENTRY name="exit" help="Exit view">
-	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
-	<ACTION sym="nav">pop</ACTION>
-	<ACTION sym="print">Exiting klish session</ACTION>
-</ENTRY>
-
 </ENTRY>
 
 </KLISH>

+ 2 - 0
klish/ktp/help.c

@@ -43,4 +43,6 @@ void help_free(void *ptr)
 
 	faux_free(help->prefix);
 	faux_free(help->line);
+
+	faux_free(help);
 }

+ 28 - 2
klish/ktp/ktpd_session.c

@@ -518,12 +518,25 @@ static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
 				kpargv_set_candidate_parg(pargv, parg);
 				ksession_exec_locally(ktpd->session,
 					help, pargv, &rc, &prefix_str);
+				// Remove \n
+				if (prefix_str) {
+					char *eol = NULL;
+					eol = faux_str_chars(prefix_str, "\n\r");
+					if (eol)
+						*eol = '\0';
+				}
 				kparg_free(parg);
 			}
 			if (!prefix_str) {
+				const char *str = NULL;
 				// If help is not defined use name of PTYPE.
 				// It can be informative enough.
-				prefix_str = faux_str_dup(kentry_name(ptype));
+				str = kentry_help(ptype);
+				if (!str)
+					str = kentry_value(ptype);
+				if (!str)
+					str = kentry_name(ptype);
+				prefix_str = faux_str_dup(str);
 			}
 
 			// Get completion entry from candidate entry for 'line'
@@ -536,12 +549,25 @@ static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
 				kpargv_set_candidate_parg(pargv, parg);
 				ksession_exec_locally(ktpd->session,
 					help, pargv, &rc, &line_str);
+				// Remove \n
+				if (line_str) {
+					char *eol = NULL;
+					eol = faux_str_chars(line_str, "\n\r");
+					if (eol)
+						*eol = '\0';
+				}
 				kparg_free(parg);
 			}
 			if (!line_str) {
+				const char *str = NULL;
 				// If help is not defined use name of ENTRY.
 				// It can be informative enough.
-				line_str = faux_str_dup(kentry_name(candidate));
+				str = kentry_help(candidate);
+				if (!str)
+					str = kentry_value(candidate);
+				if (!str)
+					str = kentry_name(candidate);
+				line_str = faux_str_dup(str);
 			}
 
 			help_struct = help_new(prefix_str, line_str);