From 04f849375f8277203eddbeac2bfbfbfc433bbacf Mon Sep 17 00:00:00 2001 From: Leo C Date: Sat, 28 Jul 2018 00:49:48 +0200 Subject: [PATCH] Enable execution of individual sub commands on top level --- avr/cmd_fat.c | 8 ++++---- avr/cmd_sd.c | 8 ++++---- avr/command.c | 42 +++++++++++++++++++++++++++++++----------- avr/command_tbl.c | 10 +++++----- include/command.h | 5 ++--- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c index 52ad95f..0c1d0e7 100644 --- a/avr/cmd_fat.c +++ b/avr/cmd_fat.c @@ -534,17 +534,17 @@ CMD_TBL_ITEM( "dev" ), CMD_TBL_ITEM( - pwd, 2, CTBL_RPT, do_pwd, + pwd, 2, CTBL_RPT|CTBL_SUBCMDAUTO, do_pwd, "Print name of current/working directory", "" ), CMD_TBL_ITEM( - cd, 2, 0, do_cd, + cd, 2, 0|CTBL_SUBCMDAUTO, do_cd, "Change the current/working directory.", "path" ), CMD_TBL_ITEM( - ls, 2, CTBL_RPT, do_ls, + ls, 2, CTBL_RPT|CTBL_SUBCMDAUTO, do_ls, "Directory listing", "path" ), @@ -578,7 +578,7 @@ CMD_TBL_ITEM( /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */ {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help, - FSTR("Alias for 'help'"), + NULL, #ifdef CONFIG_SYS_LONGHELP FSTR(""), #endif /* CONFIG_SYS_LONGHELP */ diff --git a/avr/cmd_sd.c b/avr/cmd_sd.c index 06c9338..1612a6f 100644 --- a/avr/cmd_sd.c +++ b/avr/cmd_sd.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014, 2018 Leo C. * * SPDX-License-Identifier: GPL-2.0 */ @@ -317,7 +317,7 @@ command_ret_t do_ioctl_sync(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char cmd_tbl_t cmd_tbl_sd[] = { CMD_TBL_ITEM( status, 2, CTBL_RPT, do_status, - "Socket staus", + "Socket status", "drive" ), CMD_TBL_ITEM( @@ -362,7 +362,7 @@ CMD_TBL_ITEM( /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */ {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help, - FSTR("Alias for 'help'"), + NULL, #ifdef CONFIG_SYS_LONGHELP FSTR(""), #endif /* CONFIG_SYS_LONGHELP */ @@ -376,7 +376,7 @@ CMD_TBL_END(cmd_tbl_sd) }; -command_ret_t do_sd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +command_ret_t do_sd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) { puts_P(PSTR("Huch?")); return CMD_RET_USAGE; diff --git a/avr/command.c b/avr/command.c index c8a73f0..c9137ea 100644 --- a/avr/command.c +++ b/avr/command.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014, 2016 Leo C. + * (C) Copyright 2014, 2016, 2018 Leo C. * * (C) Copyright 2000-2009 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. @@ -13,8 +13,6 @@ #include "command.h" #include "common.h" -#include -#include #include #include @@ -66,6 +64,14 @@ int cmd_tbl_item_count(cmd_tbl_t *p) int count = 0; while (p->name != NULL) { + if (p->subcmd && p->flags & CTBL_SUBCMDAUTO) { + cmd_tbl_t *sub = p->subcmd; + while (sub->name != NULL) { + if (sub->flags & CTBL_SUBCMDAUTO) + count++; + sub++; + } + } p++; count++; } return count; @@ -106,7 +112,7 @@ cmd_tbl_t *_find_cmd (const char *cmd, cmd_tbl_t *table, find_cmd_para_t *para) n_found++; } else if (cmdtp->subcmd && cmdtp->flags & CTBL_SUBCMDAUTO) { cmd_tbl_t *sub = _find_cmd(cmd, cmdtp->subcmd, para); - if (sub) { + if (sub && sub->flags & CTBL_SUBCMDAUTO) { cmdtp_ret = sub; ++n_found; } @@ -171,11 +177,25 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char /* Make array of commands */ cmd_tbl_t *tp = tbl_start; - for (int i = 0; i < cmd_items; i++) { - uint_fast8_t l = strlen_P(tp->name); - if (l > maxlen_cmd) - maxlen_cmd = l; - cmd_list[i] = tp++; + int i = 0; + while (tp->name != NULL) { + uint_fast8_t len; + if (tp->subcmd && tp->flags & CTBL_SUBCMDAUTO) { + cmd_tbl_t *sub = tp->subcmd; + while (sub->name != NULL) { + if (sub->flags & CTBL_SUBCMDAUTO) { + len = strlen_P(sub->name); + if (len > maxlen_cmd) + maxlen_cmd = len; + cmd_list[i++] = sub; + } + sub++; + } + } + len = strlen_P(tp->name); + if (len > maxlen_cmd) + maxlen_cmd = len; + cmd_list[i++] = tp++; } /* Sort command list */ qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP); @@ -209,7 +229,7 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char */ for (uint8_t i = 1; i < argc; ++i) { if ((cmdtp = find_cmd(argv[i], tbl_start, NULL)) != NULL && - (opt_debug || cmdtp->name[0] != '!')) { + (opt_debug || !(cmdtp->flags & CTBL_DBG))) { cmd_usage(cmdtp); } else { printf_P(PSTR("Unknown command '%s' - try 'help'" @@ -223,7 +243,7 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char } -command_ret_t cmd_usage(const FLASH cmd_tbl_t *cmdtp) +command_ret_t cmd_usage(cmd_tbl_t *cmdtp) { // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); print_usage_line(cmdtp->name, 0, cmdtp->usage); diff --git a/avr/command_tbl.c b/avr/command_tbl.c index 5e2d1d0..7a6b776 100644 --- a/avr/command_tbl.c +++ b/avr/command_tbl.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014-2016 Leo C. + * (C) Copyright 2014-2016, 2018 Leo C. * * SPDX-License-Identifier: GPL-2.0 */ @@ -271,12 +271,12 @@ CMD_TBL_ITEM( " -l write value as long (32 bit)" ), CMD_TBL_ITEM( - cp, 4, CTBL_RPT, do_mem_cp, + mcp, 4, CTBL_RPT, do_mem_cp, "memory copy", "source target count" ), CMD_TBL_ITEM( - cmp, 4, CTBL_RPT, do_mem_cmp, + mcmp, 4, CTBL_RPT, do_mem_cmp, "memory compare", "addr1 addr2 count" ), @@ -324,7 +324,7 @@ CMD_TBL_ITEM( #endif /* CONFIG_MX_CYCLIC */ CMD_TBL_ITEM_TOP( - sd, CONFIG_SYS_MAXARGS, CTBL_SUBCMDAUTO, do_sd, + sd, CONFIG_SYS_MAXARGS, 0, do_sd, "SD/MMC card handling commands", " args ...\n" "sd help\n" @@ -332,7 +332,7 @@ CMD_TBL_ITEM_TOP( cmd_tbl_sd ), CMD_TBL_ITEM_TOP( - fat, CONFIG_SYS_MAXARGS, CTBL_SUBCMD|CTBL_SUBCMDAUTO, do_fat, + fat, CONFIG_SYS_MAXARGS, CTBL_SUBCMDAUTO, do_fat, "fat filesystem commands", " args ...\n" "fat help\n" diff --git a/include/command.h b/include/command.h index d7eccc9..614d527 100644 --- a/include/command.h +++ b/include/command.h @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014-2016 Leo C. + * (C) Copyright 2014-2016, 2018 Leo C. * * (C) Copyright 2000-2009 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. @@ -50,13 +50,12 @@ struct cmd_tbl_s { uint8_t maxargs; /* maximum number of arguments */ uint8_t flags; /* autorepeat allowed? */ /* Implementation function */ - command_ret_t (*cmd)(const FLASH struct cmd_tbl_s *, uint_fast8_t, int, char * const []); + command_ret_t (*cmd)(cmd_tbl_t *, uint_fast8_t, int, char * const []); const FLASH char *usage; /* Usage message (short) */ #ifdef CONFIG_SYS_LONGHELP const FLASH char *help; /* Help message (long) */ #endif cmd_tbl_t *subcmd; -// const FLASH struct cmd_tbl_s *subcommands; #ifdef CONFIG_AUTO_COMPLETE /* do auto completion on the arguments */ int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]); -- 2.39.2