From 772ada399dad74cae4f7761e6a34904ff951712c Mon Sep 17 00:00:00 2001 From: sivaramaaa Date: Sun, 10 Jun 2018 00:37:13 +0530 Subject: [PATCH] Add autocompletion for pf.fmt.ele --- libr/core/core.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/libr/core/core.c b/libr/core/core.c index b2b26fc679..29b94180ae 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -885,6 +885,33 @@ out: free (input); } +//TODO: make it recursive to handle nested struct +static int autocomplete_pfele (RCore *core, char *key, char *pfx, int idx, char *ptr) { + int i, ret = 0; + int len = strlen (ptr); + char* fmt = sdb_get (core->print->formats, key, NULL); + if (fmt) { + int nargs = r_str_word_set0_stack (fmt); + if (nargs > 1) { + for (i = 1; i < nargs; i++) { + const char *arg = r_str_word_get0 (fmt, i); + char *p = strchr (arg, '('); + char *p2 = strchr (arg, ')'); + // remove '(' and ')' from fmt + if (p && p2) { + arg = p + 1; + *p2 = '\0'; + } + if (!len || !strncmp (ptr, arg, len)) { + tmp_argv[ret++] = r_str_newf ("pf%s.%s.%s", pfx, key, arg); + } + } + } + } + free (fmt); + return ret; +} + #define ADDARG(x) if (!strncmp (line->buffer.data+chr, x, strlen (line->buffer.data+chr))) { tmp_argv[j++] = x; } static int autocomplete(RLine *line) { @@ -1017,8 +1044,15 @@ static int autocomplete(RLine *line) { int j = 0; ls_foreach (sls, iter, kv) { int len = strlen (line->buffer.data + chr); - if (!len || !strncmp (line->buffer.data + chr, kv->key, len)) { - tmp_argv[j++] = r_str_newf ("pf%s.%s", pfx, kv->key); + int minlen = R_MIN (len, strlen (kv->key)); + if (!len || !strncmp (line->buffer.data + chr, kv->key, minlen)) { + char *p = strchr (line->buffer.data + chr, '.'); + if (p) { + j += autocomplete_pfele (core, kv->key, pfx, j, p + 1); + break; + } else { + tmp_argv[j++] = r_str_newf ("pf%s.%s", pfx, kv->key); + } } } if (j > 0) tmp_argv_heap = true; @@ -1086,6 +1120,7 @@ static int autocomplete(RLine *line) { line->completion.argc = i; line->completion.argv = tmp_argv; } else if (!strncmp (line->buffer.data, "ts ", 3) + || !strncmp (line->buffer.data, "tl ", 3) || !strncmp (line->buffer.data, "tss ", 4) || !strncmp (line->buffer.data, "ts* ", 4)) { int i = 0;