summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo C2018-07-30 22:09:45 +0200
committerLeo C2018-07-30 22:09:45 +0200
commitc646b5a9585d4c83df048be9198b0662a5881dcb (patch)
tree970504012f6f1817cadc142ab8fc09a7dbfb5b59
parentd530fed0a0505db5d2c9bfe283409cedb9598c88 (diff)
downloadz180-stamp-c646b5a9585d4c83df048be9198b0662a5881dcb.zip
Revised help command. Options -a -f -k
-rw-r--r--avr/command.c278
-rw-r--r--avr/command_tbl.c13
2 files changed, 229 insertions, 62 deletions
diff --git a/avr/command.c b/avr/command.c
index bb179b8..b579a5e 100644
--- a/avr/command.c
+++ b/avr/command.c
@@ -21,8 +21,9 @@
#include "con-utils.h"
#include "env.h"
#include "debug.h"
+#include "getopt-min.h"
-#define DEBUG_CMD 1 /* set to 1 to debug */
+#define DEBUG_CMD 0 /* set to 1 to debug */
#define debug_cmd(fmt, args...) \
debug_cond(DEBUG_CMD, fmt, ##args)
@@ -30,7 +31,29 @@
jmp_buf cmd_jbuf;
+char *strcasestr_P2(const FLASH char *haystack, const char *needle)
+{
+ int nlen = strlen(needle);
+ int hlen = strlen_P(haystack) - nlen + 1;
+ int i;
+
+ for (i = 0; i < hlen; i++) {
+ int j;
+ for (j = 0; j < nlen; j++) {
+ unsigned char c1 = haystack[i+j];
+ unsigned char c2 = needle[j];
+ if (toupper(c1) != toupper(c2))
+ goto next;
+ }
+ return (char *) haystack + i;
+ next:
+ ;
+ }
+ return NULL;
+}
+
+#if 0
static int cmd_tbl_item_count(cmd_tbl_t *p)
{
int count = 0;
@@ -50,6 +73,7 @@ static int cmd_tbl_item_count(cmd_tbl_t *p)
}
return count;
}
+#endif
static cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t *cmdtp)
{
@@ -61,36 +85,46 @@ static cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t *cmdtp)
return p->subcmd;
}
-static void print_name_prefix(cmd_tbl_t *p)
+static cmd_tbl_t *get_cmd_tbl_parent(cmd_tbl_t *child)
{
- cmd_tbl_t *tbl_start = get_cmd_tbl_base(p);
- cmd_tbl_t *top;
-
- if (tbl_start == cmd_tbl)
- top = NULL;
- else {
- top = cmd_tbl;
- while (top->subcmd != tbl_start)
- ++top;
+ if ((child->flags & CTBL_SUBCMDAUTO) == 0) {
+
+ cmd_tbl_t *tbl_start = get_cmd_tbl_base(child);
+ if (tbl_start != cmd_tbl) {
+ cmd_tbl_t *top = cmd_tbl;
+ while (top->subcmd != tbl_start)
+ ++top;
+ return top;
+ }
}
+ return NULL;
+}
+
+static int print_name_prefix(cmd_tbl_t *p)
+{
+ cmd_tbl_t *top = get_cmd_tbl_parent(p);
- if (top && (p->flags & CTBL_SUBCMDAUTO) == 0) {
- printf_P(PSTR("%S "), top->name);
+ int width = 0;
+ if (top) {
+ width = printf_P(PSTR("%S "), top->name);
}
+
+ return width;
}
static void print_usage_line(cmd_tbl_t *p, int width)
{
width -= strlen_P(p->name);
+ width -= print_name_prefix(p);
if (width < 0)
width = 0;
- print_name_prefix(p);
my_puts_P(p->name);
print_blanks(width);
my_puts_P(PSTR(" - "));
puts_P(p->usage);
}
+#if 0
static int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
{
unsigned char c1, c2;
@@ -102,12 +136,58 @@ static int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
return c1 - c2;
}
+#endif
+#if 1
+static int cmpstring_PP(const void *p1, const void *p2)
+{
+ char s1[21] = {'\0'};
+ char s2[21] = {'\0'};
+ cmd_tbl_t *cp1 = *(cmd_tbl_t **) p1;
+ cmd_tbl_t *cp2 = *(cmd_tbl_t **) p2;
+ cmd_tbl_t *top1 = get_cmd_tbl_parent(*(cmd_tbl_t **) p1);
+ cmd_tbl_t *top2 = get_cmd_tbl_parent(*(cmd_tbl_t **) p2);
+
+ if (top1)
+ strlcpy_P(s1, top1->name, 21);
+ if (top2)
+ strlcpy_P(s2, top2->name, 21);
+
+ strlcat_P(s1, cp1->name, 21);
+ strlcat_P(s2, cp2->name, 21);
+
+ return strcmp(s1, s2);
+}
+#else
static int cmpstring_PP(const void *p1, const void *p2)
{
- return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name,
- (*(const FLASH cmd_tbl_t **) p2)->name);
+ cmd_tbl_t *cp1 = *(cmd_tbl_t **) p1;
+ cmd_tbl_t *cp2 = *(cmd_tbl_t **) p2;
+ cmd_tbl_t *top1 = get_cmd_tbl_parent(*(cmd_tbl_t **) p1);
+ cmd_tbl_t *top2 = get_cmd_tbl_parent(*(cmd_tbl_t **) p2);
+ int res;
+
+ if (top1 && top2) {
+ res = strcmp_PP(top1->name, top2->name);
+ if (res == 0)
+ res = strcmp_PP(cp1->name, cp2->name);
+ return res;
+ }
+ if (top1) {
+ res = strcmp_PP(top1->name, cp2->name);
+ if (res == 0)
+ res = strcmp_PP(cp1->name, cp2->name);
+ return res;
+ }
+ if (top2) {
+ res = strcmp_PP(cp1->name, top2->name);
+ if (res == 0)
+ res = strcmp_PP(cp1->name, cp2->name);
+ return res;
+ }
+ return strcmp_PP(cp1->name, cp2->name);
}
+#endif
/****************************************************************************/
@@ -164,15 +244,64 @@ static cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char * const argv[])
{
cmd_tbl_t *tbl_start = get_cmd_tbl_base(cmdtp);
- command_ret_t rc = CMD_RET_SUCCESS;
+ command_ret_t res = CMD_RET_SUCCESS;
+ uint_fast8_t options = 0;
+#define OPT_DBG_CMDS 0x01
+#define OPT_ALL 0x02
+#define OPT_NAME 0x04
+#define OPT_USAGE 0x08
+#define OPT_LONG 0x10
+
+ /* reset getopt() */
+ optind = 0;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("afk"))) != -1) {
+ switch (opt) {
+ case 'a':
+ options |= OPT_ALL;
+ break;
+ case 'f':
+ options |= OPT_NAME | OPT_ALL;
+ break;
+ case 'k':
+ options |= OPT_USAGE | OPT_ALL;
+ break;
+ default: /* '?' */
+ return CMD_RET_USAGE;
+ }
+ }
- char *optenv = getenv_str(PSTR("cmd"));
- bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+ /* remaining arguments */
+ argc -= optind;
+ argv += optind;
- if (argc == 1) { /*show list of commands */
- int cmd_items = cmd_tbl_item_count(tbl_start);
- cmd_tbl_t **cmd_list = (cmd_tbl_t **) malloc(cmd_items * sizeof(cmd_tbl_t *));
- uint_fast8_t maxlen_cmd = 0;
+ if ((options & (OPT_USAGE|OPT_NAME)) == (OPT_USAGE|OPT_NAME)) {
+ puts_P(PSTR("Inkompatible options: -f -k\n"
+ "Try 'help help'"));
+ return CMD_RET_USAGE;
+ }
+ if (options & OPT_USAGE)
+ options |= OPT_NAME;
+
+ if ((options & (OPT_NAME | OPT_USAGE)) == 0 && argc > 0)
+ options |= OPT_LONG;
+
+ char *optenv = getenv_str(PSTR("cmd"));
+ if (optenv && strstr_P(optenv, PSTR("debug")) != NULL)
+ options |= OPT_DBG_CMDS;
+
+ uint_fast8_t maxlen_cmd;
+ int cmd_items;
+ cmd_tbl_t **cmd_list = NULL;
+ for (uint_fast8_t pass = 0; pass < 2; ++pass) {
+ maxlen_cmd = 0;
+ if (pass == 0)
+ cmd_items = 0;
+ else {
+ cmd_list = (cmd_tbl_t **) malloc(cmd_items * sizeof(cmd_tbl_t *));
+ /* TODO: Check error */
+ }
/* Make array of commands */
cmd_tbl_t *tp = tbl_start;
@@ -181,60 +310,93 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char
if (tp->subcmd) {
cmd_tbl_t *sub = tp->subcmd;
while (sub->name != NULL) {
- if (sub->flags & CTBL_SUBCMDAUTO) {
- uint_fast8_t len = strlen_P(sub->name);
- if (len > maxlen_cmd)
- maxlen_cmd = len;
- cmd_list[i++] = sub;
+ if (options & OPT_ALL || sub->flags & CTBL_SUBCMDAUTO) {
+ if (pass == 0)
+ ++cmd_items;
+ else {
+ cmd_list[i++] = sub;
+ uint_fast8_t len = strlen_P(sub->name);
+ if ((sub->flags & CTBL_SUBCMDAUTO) == 0) {
+ len += strlen_P(tp->name) + 1;
+ }
+ if (len > maxlen_cmd) {
+ maxlen_cmd = len;
+ }
+//debug_cmd("### i:%3d, maxlen:%3d, tp: '%S', sub: '%S'\n", i, maxlen_cmd, tp->name, sub->name);
+ }
}
sub++;
}
}
if ((tp->flags & CTBL_SUBCMDAUTO) == 0) {
- uint_fast8_t len = strlen_P(tp->name);
- if (len > maxlen_cmd)
- maxlen_cmd = len;
+ if (pass == 0)
+ ++cmd_items;
+ else {
cmd_list[i++] = tp;
+ uint_fast8_t len = strlen_P(tp->name);
+ cmd_tbl_t *top = get_cmd_tbl_parent(tp);
+ if (top)
+ len += strlen_P(top->name) + 1;
+ if (len > maxlen_cmd)
+ maxlen_cmd = len;
+ }
}
tp++;
}
- /* Sort command list */
- qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
+//debug_cmd("### pass: %d, cmd_items: %d, i: %d, maxlen_cmd: %d\n", pass, cmd_items, i, maxlen_cmd);
+ }
- /* print short help (usage) */
- for (int i = 0; i < cmd_items; i++) {
- if ((cmd_list[i]->flags & CTBL_DBG) && !opt_debug)
- continue;
- if (cmd_list[i]->usage == NULL)
- continue;
+ /* Sort command list */
+ qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
- print_usage_line(cmd_list[i], maxlen_cmd);
+ if ((options & OPT_LONG) == 0) {
+ /* print short help (usage) */
+ for (uint_fast8_t cmdi = 0; cmdi < cmd_items; cmdi++) {
/* allow user abort */
if (ctrlc ()) {
- rc = CMD_RET_FAILURE;
+ res = CMD_RET_FAILURE;
break;
}
- }
- free(cmd_list);
- return rc;
- }
+ if ((cmd_list[cmdi]->flags & CTBL_DBG) && !(options & OPT_DBG_CMDS))
+ continue;
+ if (cmd_list[cmdi]->usage == NULL)
+ continue;
- /*
- * command help (long version)
- */
- for (uint8_t i = 1; i < argc; ++i) {
- if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL) {
- cmd_usage(cmdtp);
- } else {
- printf_P(PSTR("Unknown command '%s' - try 'help'"
- " without arguments.\n\n"), argv[i]
- );
- return CMD_RET_FAILURE;
+ if (argc == 0)
+ print_usage_line(cmd_list[cmdi], maxlen_cmd);
+ else {
+ for (uint_fast8_t argi = 0; argi < argc; argi++) {
+ if (((options & OPT_NAME) &&
+ strcasestr_P2(cmd_list[cmdi]->name, argv[argi])) ||
+ ((options & OPT_USAGE) &&
+ strcasestr_P2(cmd_list[cmdi]->usage, argv[argi]))) {
+ print_usage_line(cmd_list[cmdi], maxlen_cmd);
+ }
+ }
+ }
+ }
+ } else {
+ /* command help (long version) */
+ for (uint_fast8_t argi = 0; argi < argc; ++argi) {
+ uint_fast8_t got = 0;
+
+ for (uint_fast8_t cmdi = 0; cmdi < cmd_items; cmdi++) {
+ if (strcmp_P(argv[argi], cmd_list[cmdi]->name) == 0) {
+ cmd_usage(cmd_list[cmdi]);
+ got = 1;
+ }
+ }
+ if (!got) {
+ printf_P(PSTR("Unknown command '%s' - try 'help help'\n"),
+ argv[argi]);
+ res = CMD_RET_FAILURE;
+ break;
+ }
}
}
-
- return CMD_RET_SUCCESS;
+ free(cmd_list);
+ return res;
}
diff --git a/avr/command_tbl.c b/avr/command_tbl.c
index ea41fc7..1226180 100644
--- a/avr/command_tbl.c
+++ b/avr/command_tbl.c
@@ -352,10 +352,15 @@ CMD_TBL_ITEM(
CMD_TBL_ITEM(
help, CONFIG_SYS_MAXARGS, 0, do_help,
"print command description/usage",
- "\n"
- " - print brief description of all commands\n"
- "help command ...\n"
- " - print detailed usage of 'command'"
+ "[-a]\n"
+ " print brief description of all commands\n"
+ " -a print description of subcommands too\n"
+ "help -fk keyword ...\n"
+ " print brief description of commands matching keyword\n"
+ " -f search keyword in command name\n"
+ " -k search keyword in command name and description\n"
+ "help [-a] command ...\n"
+ " print detailed usage of 'command'"
),
/* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */