summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo C2018-04-08 10:16:20 +0200
committerLeo C2018-07-21 13:14:21 +0200
commit8da60ec50c642816ed55a2004e99afea1cc25147 (patch)
tree6186e2904be868febe8015406785f992ef2029d1
parentfcf1d5b30bd3b341fb7596aef395f347c6dc97b4 (diff)
downloadz180-stamp-8da60ec50c642816ed55a2004e99afea1cc25147.zip
Call subcommands from main command processor
-rw-r--r--avr/cmd_fat.c11
-rw-r--r--avr/cmd_help.c3
-rw-r--r--avr/cmd_sd.c11
-rw-r--r--avr/command.c58
-rw-r--r--avr/command_tbl.c23
-rw-r--r--include/command.h35
6 files changed, 80 insertions, 61 deletions
diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c
index 2257ddc..0fc6f83 100644
--- a/avr/cmd_fat.c
+++ b/avr/cmd_fat.c
@@ -539,7 +539,7 @@ command_ret_t do_rw(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
static
command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
-cmd_tbl_t cmd_fat_sub[] = {
+cmd_tbl_t cmd_tbl_fat[] = {
CMD_TBL_ITEM(
stat, 2, CTBL_REPEAT, do_stat,
"Show logical drive status",
@@ -594,16 +594,19 @@ CMD_TBL_ITEM(
#ifdef CONFIG_SYS_LONGHELP
FSTR(""),
#endif /* CONFIG_SYS_LONGHELP */
+ NULL,
#ifdef CONFIG_AUTO_COMPLETE
- 0,
+ NULL,
#endif
},
+/* Mark end of table */
+{ 0 },
};
static
command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
- return _do_help(cmd_fat_sub, ARRAY_SIZE(cmd_fat_sub), cmdtp, flag, argc, argv);
+ return _do_help(cmd_tbl_fat, cmdtp, flag, argc, argv);
}
@@ -618,7 +621,7 @@ command_ret_t do_fat(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
argc--;
argv++;
- cp = find_cmd_tbl(argv[0], cmd_fat_sub, ARRAY_SIZE(cmd_fat_sub));
+ cp = find_cmd(argv[0], cmd_tbl_fat);
if (cp)
return cp->cmd(cmdtp, flag, argc, argv);
diff --git a/avr/cmd_help.c b/avr/cmd_help.c
index 309d9e6..4a52f5e 100644
--- a/avr/cmd_help.c
+++ b/avr/cmd_help.c
@@ -12,6 +12,5 @@
command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
- const int len = cmd_tbl_item_count();
- return _do_help(cmd_tbl, len, cmdtp, flag, argc, argv);
+ return _do_help(cmd_tbl, cmdtp, flag, argc, argv);
}
diff --git a/avr/cmd_sd.c b/avr/cmd_sd.c
index 9d6e5ca..98955bd 100644
--- a/avr/cmd_sd.c
+++ b/avr/cmd_sd.c
@@ -319,7 +319,7 @@ command_ret_t do_ioctl_sync(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char
static
command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
-cmd_tbl_t cmd_sd_sub[] = {
+cmd_tbl_t cmd_tbl_sd[] = {
CMD_TBL_ITEM(
status, 2, CTBL_REPEAT, do_status,
"Socket staus",
@@ -371,16 +371,19 @@ CMD_TBL_ITEM(
#ifdef CONFIG_SYS_LONGHELP
FSTR(""),
#endif /* CONFIG_SYS_LONGHELP */
+ NULL,
#ifdef CONFIG_AUTO_COMPLETE
- 0,
+ NULL,
#endif
},
+/* Mark end of table */
+{ 0 },
};
static
command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
- return _do_help(cmd_sd_sub, ARRAY_SIZE(cmd_sd_sub), cmdtp, flag, argc, argv);
+ return _do_help(cmd_tbl_sd, cmdtp, flag, argc, argv);
}
@@ -395,7 +398,7 @@ command_ret_t do_sd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
argc--;
argv++;
- cp = find_cmd_tbl(argv[0], cmd_sd_sub, ARRAY_SIZE(cmd_sd_sub));
+ cp = find_cmd(argv[0], cmd_tbl_sd);
if (cp)
return cp->cmd(cmdtp, flag, argc, argv);
diff --git a/avr/command.c b/avr/command.c
index 78b5cc4..07fff78 100644
--- a/avr/command.c
+++ b/avr/command.c
@@ -59,9 +59,8 @@ int cmpstring_PP(const void *p1, const void *p2)
(*(const FLASH cmd_tbl_t **) p2)->name);
}
-int cmd_tbl_item_count(void)
+int cmd_tbl_item_count(cmd_tbl_t *p)
{
- cmd_tbl_t * p = cmd_tbl;
int count = 0;
while (p->name != NULL) {
@@ -75,11 +74,9 @@ int cmd_tbl_item_count(void)
* for long help messages
*/
-command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
+command_ret_t _do_help(cmd_tbl_t *tbl_start, cmd_tbl_t * cmdtp,
uint_fast8_t flag, int argc, char * const argv[])
{
- uint_fast8_t i, max_len = 0;
- command_ret_t rcode = CMD_RET_SUCCESS;
(void) flag;
@@ -87,13 +84,14 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
if (argc == 1) { /*show list of commands */
+ int cmd_items = cmd_tbl_item_count(tbl_start);
cmd_tbl_t *cmd_array[cmd_items];
- int i;
+ cmd_tbl_t *tp = tbl_start;
+ uint_fast8_t max_len = 0;
/* Make array of commands from .uboot_cmd section */
- cmdtp = cmd_start;
- for (i = 0; i < cmd_items; i++) {
- cmd_array[i] = cmdtp++;
+ for (int i = 0; i < cmd_items; i++) {
+ cmd_array[i] = tp++;
uint_fast8_t l = strlen_P(cmd_array[i]->name);
if (l > max_len)
max_len = l;
@@ -103,7 +101,7 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
/* print short help (usage) */
- for (i = 0; i < cmd_items; i++) {
+ for (int i = 0; i < cmd_items; i++) {
if (opt_debug || cmd_array[i]->name[0] != '!') {
const FLASH char *usage = cmd_array[i]->usage;
@@ -112,7 +110,7 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
return CMD_RET_FAILURE;
if (usage == NULL)
continue;
-#ifdef GCC_BUG_61443
+#if defined(GCC_BUG_61443) || 1
print_usage_line(cmd_array[i]->name, max_len, usage);
#else
printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
@@ -122,30 +120,33 @@ command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
}
return CMD_RET_SUCCESS;
}
+
/*
* command help (long version)
*/
- for (i = 1; i < argc; ++i) {
- if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL &&
+ for (uint8_t i = 1; i < argc; ++i) {
+ if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL &&
(opt_debug || cmdtp->name[0] != '!')) {
cmd_usage(cmdtp);
} else {
printf_P(PSTR("Unknown command '%s' - try 'help'"
" without arguments.\n\n"), argv[i]
);
- rcode = CMD_RET_FAILURE;
+ return CMD_RET_FAILURE;
}
}
- return rcode;
+
+ return CMD_RET_SUCCESS;
}
/***************************************************************************
* find command table entry for a command
*/
-cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
+cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
{
cmd_tbl_t *cmdtp;
cmd_tbl_t *cmdtp_temp = table; /*Init value */
+ int table_len = cmd_tbl_item_count(table);
size_t len;
uint_fast8_t n_found = 0;
@@ -176,11 +177,6 @@ cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
}
-cmd_tbl_t *find_cmd (const char *cmd)
-{
- return find_cmd_tbl(cmd, cmd_tbl, cmd_tbl_item_count());
-}
-
command_ret_t cmd_usage(const FLASH cmd_tbl_t *cmdtp)
{
@@ -263,7 +259,7 @@ static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv
/* more than one arg or one but the start of the next */
if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
- cmdtp = find_cmd(argv[0]);
+ cmdtp = find_cmd(argv[0], cmd_tbl);
if (cmdtp == NULL || cmdtp->complete == NULL) {
cmdv[0] = NULL;
return 0;
@@ -499,14 +495,24 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
cmd_tbl_t *cmdtp;
/* Look up command in command table */
- cmdtp = find_cmd(argv[0]);
+ cmdtp = find_cmd(argv[0], cmd_tbl);
if (cmdtp == NULL) {
printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
return CMD_RET_FAILURE;
}
- if (!cmdtp->cmd) {
- debug("### Command '%s' found, but ->cmd == NULL \n", argv[0]);
- return CMD_RET_FAILURE;
+
+ /* Check for subcommand */
+ if (cmdtp->subcmd && argc > 1) {
+
+ /* Look up subcommand in subcommand table */
+ cmd_tbl_t *cmdtpsub = find_cmd(argv[1], cmdtp->subcmd);
+ if (cmdtpsub == NULL) {
+ printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv[0], argv[1], argv[0]);
+ return CMD_RET_FAILURE;
+ }
+ cmdtp = cmdtpsub;
+ --argc;
+ ++argv;
}
/* found - check max args */
diff --git a/avr/command_tbl.c b/avr/command_tbl.c
index ba92933..3f74be8 100644
--- a/avr/command_tbl.c
+++ b/avr/command_tbl.c
@@ -39,12 +39,8 @@ extern command_ret_t do_source(cmd_tbl_t *, uint_fast8_t, int, char * const []);
extern command_ret_t do_attach(cmd_tbl_t *, uint_fast8_t, int, char * const []);
extern command_ret_t do_pr_free_avr(cmd_tbl_t *, uint_fast8_t, int, char * const []);
-#ifdef CONFIG_SYS_LONGHELP
-const FLASH char sd_help_text[] =
- "bla \t- do bla\n"
- ;
-#endif /* CONFIG_SYS_LONGHELP */
-
+extern cmd_tbl_t cmd_tbl_fat[];
+extern cmd_tbl_t cmd_tbl_sd[];
cmd_tbl_t cmd_tbl[] = {
@@ -343,19 +339,21 @@ CMD_TBL_ITEM(
),
#endif /* CONFIG_MX_CYCLIC */
-CMD_TBL_ITEM(
+CMD_TBL_ITEM_TOP(
sd, CONFIG_SYS_MAXARGS, 1, do_sd,
"SD/MMC card handling commands",
"<subcommand> args ...\n"
"sd help\n"
- " - print help on subcommands"
+ " - print help on subcommands",
+ cmd_tbl_sd
),
-CMD_TBL_ITEM(
+CMD_TBL_ITEM_TOP(
fat, CONFIG_SYS_MAXARGS, 1, do_fat,
"fat filesystem commands",
"<subcommand> args ...\n"
"fat help\n"
- " - print help on subcommands"
+ " - print help on subcommands",
+ cmd_tbl_fat
),
CMD_TBL_ITEM(
@@ -379,7 +377,7 @@ CMD_TBL_ITEM(
" -a Detach all.\n"
"\n"
"attach\n"
- " Without arguments, list current assignments\n"
+ " Without arguments, list current assignments"
),
CMD_TBL_ITEM(
detach, 2, CTBL_REPEAT, do_attach,
@@ -403,8 +401,9 @@ CMD_TBL_ITEM(
#ifdef CONFIG_SYS_LONGHELP
FSTR(""),
#endif /* CONFIG_SYS_LONGHELP */
+ NULL,
#ifdef CONFIG_AUTO_COMPLETE
- 0,
+ NULL,
#endif
},
/* Mark end of table */
diff --git a/include/command.h b/include/command.h
index 75b5a0d..5e55e47 100644
--- a/include/command.h
+++ b/include/command.h
@@ -44,6 +44,7 @@ typedef enum {
* Monitor Command Table
*/
+typedef const FLASH struct cmd_tbl_s cmd_tbl_t;
struct cmd_tbl_s {
const FLASH char *name; /* Command Name */
uint8_t maxargs; /* maximum number of arguments */
@@ -54,14 +55,14 @@ struct cmd_tbl_s {
#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[]);
#endif
};
-typedef const FLASH struct cmd_tbl_s cmd_tbl_t;
-
/**
* Process a command with arguments. We look up the command and execute it
* if valid. Otherwise we print a usage message.
@@ -79,11 +80,10 @@ cmd_process(uint_fast8_t flag, int argc, char * const argv[], uint_fast8_t *repe
/* command.c */
-command_ret_t _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
-cmd_tbl_t *find_cmd(const char *cmd);
-cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
+command_ret_t _do_help (cmd_tbl_t *cmd_start, cmd_tbl_t * cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
+cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table);
-int cmd_tbl_item_count(void);
+int cmd_tbl_item_count(cmd_tbl_t *p);
command_ret_t cmd_usage(cmd_tbl_t *cmdtp);
#ifdef CONFIG_AUTO_COMPLETE
@@ -151,14 +151,23 @@ extern command_ret_t do_reset(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, cha
#endif
-#define CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
- _usage, _help, _comp) \
- { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
- _CMD_HELP(FSTR(_help)) _CMD_COMPLETE(_comp) }
+#define CMD_TBL_ITEM_FULL(_name, _maxargs, _rep, _cmd, \
+ _usage, _help, _subtbl, _comp) \
+ { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
+ _CMD_HELP(FSTR(_help)) _subtbl, _CMD_COMPLETE(_comp) }
+
+#define CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
+ _usage, _help, _comp) \
+ { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
+ _CMD_HELP(FSTR(_help)) NULL, _CMD_COMPLETE(_comp) }
+
+#define CMD_TBL_ITEM(_name, _maxargs, _rep, _cmd, _usage, _help) \
+ CMD_TBL_ITEM_FULL(_name, _maxargs, _rep, _cmd, \
+ _usage, _help, NULL, NULL)
-#define CMD_TBL_ITEM(_name, _maxargs, _rep, _cmd, _usage, _help) \
- CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
- _usage, _help, NULL)
+#define CMD_TBL_ITEM_TOP(_name, _maxargs, _rep, _cmd, _usage, _help, _subtbl) \
+ CMD_TBL_ITEM_FULL(_name, _maxargs, _rep, _cmd, \
+ _usage, _help, _subtbl, NULL)
typedef command_ret_t (*do_cmd_t)(cmd_tbl_t *, uint_fast8_t, int, char * const []);