소스 검색

Unique help entries

Serj Kalichev 1 년 전
부모
커밋
c3a3e6959b
3개의 변경된 파일18개의 추가작업 그리고 17개의 파일을 삭제
  1. 6 3
      bin/klish/interactive.c
  2. 4 9
      klish/ktp/help.c
  3. 8 5
      klish/ktp/ktpd_session.c

+ 6 - 3
bin/klish/interactive.c

@@ -657,8 +657,8 @@ bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
 
 	process_prompt_param(ctx->tinyrl, msg);
 
-	help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
-		help_compare, help_kcompare, help_free);
+	help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
+		help_compare, NULL, help_free);
 
 	// Wait for PREFIX - LINE pairs
 	iter = faux_msg_init_param_iter(msg);
@@ -685,7 +685,10 @@ bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
 		line_str = faux_str_dupn(param_data, param_len);
 
 		help = help_new(prefix_str, line_str);
-		faux_list_add(help_list, help);
+		if (!faux_list_add(help_list, help)) {
+			help_free(help);
+			continue;
+		}
 		if (prefix_len > max_prefix_len)
 			max_prefix_len = prefix_len;
 	}

+ 4 - 9
klish/ktp/help.c

@@ -11,17 +11,12 @@ int help_compare(const void *first, const void *second)
 {
 	const help_t *f = (const help_t *)first;
 	const help_t *s = (const help_t *)second;
+	int r = 0;
 
-	return strcmp(f->prefix, s->prefix);
-}
-
-
-int help_kcompare(const void *key, const void *list_item)
-{
-	const char *f = (const char *)key;
-	const help_t *s = (const help_t *)list_item;
+	if ((r = strcmp(f->prefix, s->prefix)) != 0)
+		return r;
 
-	return strcmp(f, s->prefix);
+	return strcmp(f->line, s->line);
 }
 
 

+ 8 - 5
klish/ktp/ktpd_session.c

@@ -698,8 +698,8 @@ static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
 		faux_list_t *help_list = NULL;
 		help_t *help_struct = NULL;
 
-		help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
-			help_compare, help_kcompare, help_free);
+		help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
+			help_compare, NULL, help_free);
 		while ((candidate = kpargv_completions_each(&citer))) {
 			const kentry_t *help = NULL;
 			const kentry_t *ptype = NULL;
@@ -742,8 +742,10 @@ static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
 							break;
 						}
 						help_struct = help_new(prefix_str, line_str);
-						faux_list_add(help_list, help_struct);
-						help_added = BOOL_TRUE;
+						if (faux_list_add(help_list, help_struct))
+							help_added = BOOL_TRUE;
+						else
+							help_free(help_struct);
 					} while (line_str);
 					faux_str_free(out);
 				}
@@ -780,7 +782,8 @@ static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
 				help_struct = help_new(
 					faux_str_dup(prefix_str),
 					faux_str_dup(line_str));
-				faux_list_add(help_list, help_struct);
+				if (!faux_list_add(help_list, help_struct))
+					help_free(help_struct);
 			}
 		}