summaryrefslogtreecommitdiff
path: root/avr/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/command.c')
-rw-r--r--avr/command.c812
1 files changed, 812 insertions, 0 deletions
diff --git a/avr/command.c b/avr/command.c
new file mode 100644
index 0000000..8a4ca02
--- /dev/null
+++ b/avr/command.c
@@ -0,0 +1,812 @@
+/*
+ * (C) Copyright 2014, 2016, 2018 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * (C) Copyright 2000-2009
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+/*
+ * Command Processor Table
+ */
+
+#include "command.h"
+#include "common.h"
+#include <setjmp.h>
+
+#include "config.h"
+#include "print-utils.h"
+#include "con-utils.h"
+#include "env.h"
+#include "debug.h"
+#include "getopt-min.h"
+#include "strerror.h"
+
+#define DEBUG_CMD 0 /* set to 1 to debug */
+
+#define debug_cmd(fmt, args...) \
+ debug_cond(DEBUG_CMD, fmt, ##args)
+
+
+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;
+
+ while (p->name != NULL) {
+ if (p->subcmd) {
+ cmd_tbl_t *sub = p->subcmd;
+ while (sub->name != NULL) {
+ if (sub->flags & CTBL_SUBCMDAUTO)
+ count++;
+ sub++;
+ }
+ }
+ if ((p->flags & CTBL_SUBCMDAUTO) == 0)
+ count++;
+ p++;
+ }
+ return count;
+}
+#endif
+
+static cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t *cmdtp)
+{
+ cmd_tbl_t *p = cmdtp;
+
+ while (p->name != NULL)
+ ++p;
+
+ return p->subcmd;
+}
+
+static cmd_tbl_t *get_cmd_tbl_parent(cmd_tbl_t *child)
+{
+ 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_nameprefix(cmd_tbl_t *p)
+{
+ cmd_tbl_t *top = get_cmd_tbl_parent(p);
+
+ int width = 0;
+ if (top) {
+ width = printf_P(PSTR("%S "), top->name);
+ }
+
+ return width;
+}
+
+static int print_prefixed_name(cmd_tbl_t *p)
+{
+ int len;
+
+ len = print_nameprefix(p);
+ len += strlen_P(p->name);
+ my_puts_P(p->name);
+
+ return len;
+}
+
+static void print_usage_line(cmd_tbl_t *p, int width)
+{
+ width -= print_prefixed_name(p);
+ if (width < 0)
+ width = 0;
+ 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;
+
+ while ((c1 = *(const FLASH unsigned char *)s1++)
+ == (c2 = *(const FLASH unsigned char *)s2++))
+ if (c1 == 0)
+ return 0;
+
+ 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)
+{
+ 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
+
+/****************************************************************************/
+
+/*
+ * find command table entry for a command
+ */
+
+static cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
+{
+ if (!cmd)
+ return NULL;
+
+ char *optenv = getenv_str(PSTR("cmd"));
+ uint8_t opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
+ cmd_tbl_t *cmdtp_ret = NULL;
+ uint_fast8_t n_found = 0;
+ uint_fast8_t len = strlen(cmd);
+
+ for (cmd_tbl_t *cmdtp = table; cmdtp->name != NULL; cmdtp++) {
+ if (cmdtp->subcmd) {
+ for (cmd_tbl_t *sub = cmdtp->subcmd; sub->name != NULL; sub++) {
+ if (sub->flags & CTBL_SUBCMDAUTO &&
+ strncmp_P(cmd, sub->name, len) == 0 &&
+ (opt_debug || !(sub->flags & CTBL_DBG))) {
+ if (len == strlen_P(sub->name))
+ return sub; /* full match */
+ cmdtp_ret = sub; /* abbreviated command ? */
+ ++n_found;
+ }
+ }
+ }
+ if ((cmdtp->flags & CTBL_SUBCMDAUTO) == 0 &&
+ strncmp_P(cmd, cmdtp->name, len) == 0 &&
+ (opt_debug || !(cmdtp->flags & CTBL_DBG))) {
+ if (len == strlen_P(cmdtp->name))
+ return cmdtp; /* full match */
+ cmdtp_ret = cmdtp; /* abbreviated command ? */
+ ++n_found;
+ }
+ }
+
+ if (n_found == 1)
+ return cmdtp_ret; /* exactly one match */
+
+ return NULL; /* not found or ambiguous command */
+}
+
+/*
+ * Use puts() instead of printf() to avoid printf buffer overflow
+ * for long help messages
+ */
+
+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 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
+
+ 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;
+ }
+ }
+
+ /* remaining arguments */
+ argc -= optind;
+ argv += optind;
+
+ 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;
+ int i = 0;
+ while (tp->name != NULL) {
+ if (tp->subcmd) {
+ cmd_tbl_t *sub = tp->subcmd;
+ while (sub->name != NULL) {
+ 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) {
+ 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++;
+ }
+//debug_cmd("### pass: %d, cmd_items: %d, i: %d, maxlen_cmd: %d\n", pass, cmd_items, i, maxlen_cmd);
+ }
+
+ /* Sort command list */
+ qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
+
+ if ((options & OPT_LONG) == 0) {
+ /* print short help (usage) */
+ for (uint_fast8_t cmdi = 0; cmdi < cmd_items; cmdi++) {
+
+ /* allow user abort */
+ if (ctrlc ()) {
+ res = CMD_RET_FAILURE;
+ break;
+ }
+ if ((cmd_list[cmdi]->flags & CTBL_DBG) && !(options & OPT_DBG_CMDS))
+ continue;
+ if (cmd_list[cmdi]->usage == NULL)
+ continue;
+
+ 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;
+ cmd_tbl_t *tp = find_cmd(argv[argi], tbl_start);
+ if (tp) {
+ cmd_usage(tp);
+ got = 1;
+ }
+ if (options & OPT_ALL) {
+ for (cmd_tbl_t *sub = tbl_start; sub->name != NULL; sub++) {
+ if (sub->subcmd) {
+ tp = find_cmd(argv[argi], sub->subcmd);
+ if (tp) {
+ cmd_usage(tp);
+ got = 1;
+ }
+ }
+ }
+ }
+ if (!got) {
+ printf_P(PSTR("Unknown command '%s' - try 'help help'\n"),
+ argv[argi]);
+ res = CMD_RET_FAILURE;
+ break;
+ }
+ }
+ }
+ free(cmd_list);
+ return res;
+}
+
+
+command_ret_t cmd_usage(cmd_tbl_t *cmdtp)
+{
+ print_usage_line(cmdtp, 0);
+#ifdef CONFIG_SYS_LONGHELP
+ my_puts_P(PSTR("Usage:\n"));
+ print_prefixed_name(cmdtp);
+ my_puts_P(PSTR(" "));
+
+ if (cmdtp->help && *cmdtp->help != '\0')
+ puts_P(cmdtp->help);
+ else
+ my_puts_P(PSTR(" - No additional help available.\n"));
+#endif /* CONFIG_SYS_LONGHELP */
+ return CMD_RET_FAILURE;
+}
+
+#ifdef CONFIG_AUTO_COMPLETE
+
+int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
+{
+ static char tmp_buf[CONFIG_SYS_CBSIZE];
+ int space;
+
+ space = last_char == '\0' || isblank(last_char);
+
+ if (space && argc == 1)
+ return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
+
+ if (!space && argc == 2)
+ return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
+
+ return 0;
+}
+
+/*************************************************************************************/
+
+/* TODO: cmdtp points to FLASH */
+
+static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
+{
+ cmd_tbl_t *cmdtp = cmd_tbl;
+// const int count = ARRAY_SIZE(cmd_tbl);
+// const cmd_tbl_t *cmdend = cmdtp + count;
+// const char *p;
+ int len, clen;
+ int n_found = 0;
+ const char *cmd;
+
+ /* sanity? */
+ if (maxv < 2)
+ return -2;
+
+ cmdv[0] = NULL;
+
+ if (argc == 0) {
+ /* output full list of commands */
+ for (; cmdtp->name[0] != '\0'; cmdtp++) {
+ if (n_found >= maxv - 2) {
+ cmdv[n_found++] = "...";
+ break;
+ }
+ cmdv[n_found++] = cmdtp->name;
+ }
+ cmdv[n_found] = NULL;
+ return n_found;
+ }
+
+ /* 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], cmd_tbl);
+ if (cmdtp == NULL || cmdtp->complete == NULL) {
+ cmdv[0] = NULL;
+ return 0;
+ }
+ return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
+ }
+
+ cmd = argv[0];
+
+ len = strlen(cmd);
+
+ /* return the partial matches */
+ for (; cmdtp->name[0] != '\0'; cmdtp++) {
+
+ clen = strlen(cmdtp->name);
+ if (clen < len)
+ continue;
+
+ if (memcmp(cmd, cmdtp->name, len) != 0)
+ continue;
+
+ /* too many! */
+ if (n_found >= maxv - 2) {
+ cmdv[n_found++] = "...";
+ break;
+ }
+
+ cmdv[n_found++] = cmdtp->name;
+ }
+
+ cmdv[n_found] = NULL;
+ return n_found;
+}
+
+static int make_argv(char *s, int argvsz, char *argv[])
+{
+ int argc = 0;
+
+ /* split into argv */
+ while (argc < argvsz - 1) {
+
+ /* skip any white space */
+ while (isblank(*s))
+ ++s;
+
+ if (*s == '\0') /* end of s, no more args */
+ break;
+
+ argv[argc++] = s; /* begin of argument string */
+
+ /* find end of string */
+ while (*s && !isblank(*s))
+ ++s;
+
+ if (*s == '\0') /* end of s, no more args */
+ break;
+
+ *s++ = '\0'; /* terminate current arg */
+ }
+ argv[argc] = NULL;
+
+ return argc;
+}
+
+static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
+{
+ int ll = leader != NULL ? strlen(leader) : 0;
+ int sl = sep != NULL ? strlen(sep) : 0;
+ int len, i;
+
+ if (banner) {
+ my_puts_P(PSTR("\n"));
+ my_puts(banner);
+ }
+
+ i = linemax; /* force leader and newline */
+ while (*argv != NULL) {
+ len = strlen(*argv) + sl;
+ if (i + len >= linemax) {
+ my_puts_P(PSTR("\n"));
+ if (leader)
+ my_puts(leader);
+ i = ll - sl;
+ } else if (sep)
+ my_puts(sep);
+ my_puts(*argv++);
+ i += len;
+ }
+ my_puts_P(PSTR("\n"));
+}
+
+static int find_common_prefix(char * const argv[])
+{
+ int i, len;
+ char *anchor, *s, *t;
+
+ if (*argv == NULL)
+ return 0;
+
+ /* begin with max */
+ anchor = *argv++;
+ len = strlen(anchor);
+ while ((t = *argv++) != NULL) {
+ s = anchor;
+ for (i = 0; i < len; i++, t++, s++) {
+ if (*t != *s)
+ break;
+ }
+ len = s - anchor;
+ }
+ return len;
+}
+
+static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
+
+
+int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp)
+{
+ int n = *np, col = *colp;
+ char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
+ char *cmdv[20];
+ char *s, *t;
+ const char *sep;
+ int i, j, k, len, seplen, argc;
+ int cnt;
+ char last_char;
+
+ if (strcmp_PP(prompt, CONFIG_SYS_PROMPT) != 0)
+ return 0; /* not in normal console */
+
+ cnt = strlen(buf);
+ if (cnt >= 1)
+ last_char = buf[cnt - 1];
+ else
+ last_char = '\0';
+
+ /* copy to secondary buffer which will be affected */
+ strcpy(tmp_buf, buf);
+
+ /* separate into argv */
+ argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
+
+ /* do the completion and return the possible completions */
+ i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
+
+ /* no match; bell and out */
+ if (i == 0) {
+ if (argc > 1) /* allow tab for non command */
+ return 0;
+ putchar('\a');
+ return 1;
+ }
+
+ s = NULL;
+ len = 0;
+ sep = NULL;
+ seplen = 0;
+ if (i == 1) { /* one match; perfect */
+ k = strlen(argv[argc - 1]);
+ s = cmdv[0] + k;
+ len = strlen(s);
+ sep = " ";
+ seplen = 1;
+ } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
+ k = strlen(argv[argc - 1]);
+ j -= k;
+ if (j > 0) {
+ s = cmdv[0] + k;
+ len = j;
+ }
+ }
+
+ if (s != NULL) {
+ k = len + seplen;
+ /* make sure it fits */
+ if (n + k >= CONFIG_SYS_CBSIZE - 2) {
+ putchar('\a');
+ return 1;
+ }
+
+ t = buf + cnt;
+ for (i = 0; i < len; i++)
+ *t++ = *s++;
+ if (sep != NULL)
+ for (i = 0; i < seplen; i++)
+ *t++ = sep[i];
+ *t = '\0';
+ n += k;
+ col += k;
+ my_puts(t - k);
+ if (sep == NULL)
+ putchar('\a');
+ *np = n;
+ *colp = col;
+ } else {
+ print_argv(NULL, " ", " ", 78, cmdv);
+
+ my_puts_P(prompt);
+ my_puts(buf);
+ }
+ return 1;
+}
+
+#endif /* CONFIG_AUTO_COMPLETE */
+
+
+static cmd_tbl_t *cmd_invocation_ptr;
+
+/**
+ * Call a command function. This should be the only route in U-Boot to call
+ * a command, so that we can track whether we are waiting for input or
+ * executing a command.
+ *
+ * @param cmdtp Pointer to the command to execute
+ * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
+ * @param argc Number of arguments (arg 0 must be the command text)
+ * @param argv Arguments
+ * @return 0 if command succeeded, else non-zero (CMD_RET_...)
+ */
+static command_ret_t cmd_call(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+{
+ command_ret_t result;
+
+ result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
+// if (result != CMD_RET_SUCCESS)
+// debug("Command failed, result=%d\n", result);
+ return result;
+}
+
+#pragma GCC diagnostic ignored "-Wclobbered"
+
+command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
+ uint_fast8_t *repeatable)
+{
+ command_ret_t rc = CMD_RET_SUCCESS;
+ cmd_tbl_t *cmdtp;
+
+ /* Look up command in command table */
+ cmdtp = find_cmd(argv[0], cmd_tbl);
+ if (cmdtp != NULL) {
+ /* Check if this command has subcommands */
+ 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;
+ }
+ }
+
+ if (cmdtp == NULL) {
+ printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
+ return CMD_RET_FAILURE;
+ }
+
+ /* found - check max args */
+ if (argc > cmdtp->maxargs)
+ rc = CMD_RET_USAGE;
+
+#if defined(CONFIG_CMD_BOOTD)
+ /* avoid "bootd" recursion */
+ else if (cmdtp->cmd == do_bootd) {
+ if (flag & CMD_FLAG_BOOTD) {
+ my_puts_P(PSTR("'bootd' recursion detected\n"));
+ rc = CMD_RET_FAILURE;
+ } else {
+ flag |= CMD_FLAG_BOOTD;
+ }
+ }
+#endif
+
+ if (setjmp(cmd_jbuf) != 0)
+ return CMD_RET_FAILURE;
+
+ /* If OK so far, then do the command */
+ if (!rc) {
+ optind = 0; /* reset getopt() */
+ cmd_invocation_ptr = cmdtp;
+ rc = cmd_call(cmdtp, flag, argc, argv);
+ *repeatable &= (cmdtp->flags & CTBL_RPT) != 0;
+ }
+ if (rc == CMD_RET_USAGE)
+ rc = cmd_usage(cmdtp);
+ return rc;
+}
+
+int cmd_process_error(cmd_tbl_t *cmdtp, int err)
+{
+ char buf[strlen_P(cmdtp->name) + 1];
+ strcpy_P(buf, cmdtp->name);
+
+ if (err) {
+ printf_P(PSTR("Command '%s' failed: Error %d\n"), buf, err);
+ return 1;
+ }
+
+ return 0;
+}
+
+void cmd_error(int status, int errnum, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ print_prefixed_name(cmd_invocation_ptr);
+ if (fmt != NULL) {
+ my_puts_P(PSTR(": "));
+ vfprintf_P(stdout, fmt, ap);
+ }
+ va_end(ap);
+
+ if (errnum != 0)
+ printf_P(PSTR(": %S"), my_strerror_P(errnum));
+
+ putchar('\n');
+ _delay_ms(20);
+
+ if (status != 0) {
+ longjmp(cmd_jbuf, 1);
+ }
+}