|
@@ -309,9 +309,10 @@ static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
|
|
|
iter = faux_list_head(help_list);
|
|
|
while ((node = faux_list_each_node(&iter))) {
|
|
|
help_t *help = (help_t *)faux_list_data(node);
|
|
|
- tinyrl_printf(tinyrl, "%s%*s\n",
|
|
|
+ tinyrl_printf(tinyrl, "%s%*s%s\n",
|
|
|
help->prefix,
|
|
|
(max + 1 - strlen(help->prefix)),
|
|
|
+ " ",
|
|
|
help->line);
|
|
|
}
|
|
|
}
|
|
@@ -320,82 +321,58 @@ static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
|
|
|
bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
|
|
|
{
|
|
|
ctx_t *ctx = (ctx_t *)udata;
|
|
|
+ faux_list_t *help_list = NULL;
|
|
|
faux_list_node_t *iter = NULL;
|
|
|
uint32_t param_len = 0;
|
|
|
char *param_data = NULL;
|
|
|
uint16_t param_type = 0;
|
|
|
- char *prefix = NULL;
|
|
|
- faux_list_t *completions = NULL;
|
|
|
- size_t completions_num = 0;
|
|
|
- size_t max_compl_len = 0;
|
|
|
+ size_t max_prefix_len = 0;
|
|
|
|
|
|
tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
|
|
|
|
|
|
- prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
|
|
|
-
|
|
|
- completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
|
|
|
- NULL, NULL, (void (*)(void *))faux_str_free);
|
|
|
+ help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
|
|
|
+ help_compare, help_kcompare, help_free);
|
|
|
|
|
|
+ // Wait for PREFIX - LINE pairs
|
|
|
iter = faux_msg_init_param_iter(msg);
|
|
|
- while (faux_msg_get_param_each(&iter, ¶m_type, (void **)¶m_data, ¶m_len)) {
|
|
|
- char *compl = NULL;
|
|
|
- if (KTP_PARAM_LINE != param_type)
|
|
|
+ while (faux_msg_get_param_each(&iter, ¶m_type, (void **)¶m_data,
|
|
|
+ ¶m_len)) {
|
|
|
+ char *prefix_str = NULL;
|
|
|
+ char *line_str = NULL;
|
|
|
+ help_t *help = NULL;
|
|
|
+ size_t prefix_len = 0;
|
|
|
+
|
|
|
+ // Get PREFIX
|
|
|
+ if (KTP_PARAM_PREFIX != param_type)
|
|
|
continue;
|
|
|
- compl = faux_str_dupn(param_data, param_len);
|
|
|
- faux_list_add(completions, compl);
|
|
|
- if (param_len > max_compl_len)
|
|
|
- max_compl_len = param_len;
|
|
|
- }
|
|
|
-
|
|
|
- completions_num = faux_list_len(completions);
|
|
|
-
|
|
|
- // Single possible completion
|
|
|
- if (1 == completions_num) {
|
|
|
- char *compl = (char *)faux_list_data(faux_list_head(completions));
|
|
|
- tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
|
|
|
- tinyrl_redisplay(ctx->tinyrl);
|
|
|
-
|
|
|
- // Multi possible completions
|
|
|
- } else if (completions_num > 1) {
|
|
|
- faux_list_node_t *eq_iter = NULL;
|
|
|
- size_t eq_part = 0;
|
|
|
- char *str = NULL;
|
|
|
- char *compl = NULL;
|
|
|
-
|
|
|
- // Try to find equal part for all possible completions
|
|
|
- eq_iter = faux_list_head(completions);
|
|
|
- str = (char *)faux_list_data(eq_iter);
|
|
|
- eq_part = strlen(str);
|
|
|
- eq_iter = faux_list_next_node(eq_iter);
|
|
|
-
|
|
|
- while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
|
|
|
- size_t cur_eq = 0;
|
|
|
- cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
|
|
|
- if (cur_eq < eq_part)
|
|
|
- eq_part = cur_eq;
|
|
|
+ prefix_str = faux_str_dupn(param_data, param_len);
|
|
|
+ prefix_len = param_len;
|
|
|
+
|
|
|
+ // Get LINE
|
|
|
+ if (!faux_msg_get_param_each(&iter, ¶m_type,
|
|
|
+ (void **)¶m_data, ¶m_len) ||
|
|
|
+ (KTP_PARAM_LINE != param_type)) {
|
|
|
+ faux_str_free(prefix_str);
|
|
|
+ break;
|
|
|
}
|
|
|
+ line_str = faux_str_dupn(param_data, param_len);
|
|
|
|
|
|
- // The equal part was found
|
|
|
- if (eq_part > 0) {
|
|
|
- tinyrl_line_insert(ctx->tinyrl, str, eq_part);
|
|
|
- tinyrl_redisplay(ctx->tinyrl);
|
|
|
+ help = help_new(prefix_str, line_str);
|
|
|
+ faux_list_add(help_list, help);
|
|
|
+ if (prefix_len > max_prefix_len)
|
|
|
+ max_prefix_len = prefix_len;
|
|
|
+ }
|
|
|
|
|
|
- // There is no equal part for all completions
|
|
|
- } else {
|
|
|
- tinyrl_multi_crlf(ctx->tinyrl);
|
|
|
- tinyrl_reset_line_state(ctx->tinyrl);
|
|
|
- display_completions(ctx->tinyrl, completions,
|
|
|
- prefix, max_compl_len);
|
|
|
- tinyrl_redisplay(ctx->tinyrl);
|
|
|
- }
|
|
|
+ if (faux_list_len(help_list) > 0) {
|
|
|
+ tinyrl_multi_crlf(ctx->tinyrl);
|
|
|
+ tinyrl_reset_line_state(ctx->tinyrl);
|
|
|
+ display_help(ctx->tinyrl, help_list, max_prefix_len);
|
|
|
+ tinyrl_redisplay(ctx->tinyrl);
|
|
|
}
|
|
|
|
|
|
- faux_list_free(completions);
|
|
|
- faux_str_free(prefix);
|
|
|
+ faux_list_free(help_list);
|
|
|
|
|
|
- // Happy compiler
|
|
|
- ktp = ktp;
|
|
|
- msg = msg;
|
|
|
+ ktp = ktp; // happy compiler
|
|
|
|
|
|
return BOOL_TRUE;
|
|
|
}
|