#include "command.h"
#include "common.h"
-#include <ctype.h>
#include <setjmp.h>
#include "config.h"
#include "env.h"
#include "debug.h"
#include "getopt-min.h"
+#include "strerror.h"
#define DEBUG_CMD 0 /* set to 1 to debug */
return NULL;
}
-static int print_name_prefix(cmd_tbl_t *p)
+static int print_nameprefix(cmd_tbl_t *p)
{
cmd_tbl_t *top = get_cmd_tbl_parent(p);
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 -= strlen_P(p->name);
- width -= print_name_prefix(p);
+ width -= print_prefixed_name(p);
if (width < 0)
width = 0;
- my_puts_P(p->name);
print_blanks(width);
my_puts_P(PSTR(" - "));
puts_P(p->usage);
#define OPT_USAGE 0x08
#define OPT_LONG 0x10
- /* reset getopt() */
- optind = 0;
-
int opt;
while ((opt = getopt(argc, argv, PSTR("afk"))) != -1) {
switch (opt) {
/* 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;
+ 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) {
print_usage_line(cmdtp, 0);
#ifdef CONFIG_SYS_LONGHELP
my_puts_P(PSTR("Usage:\n"));
- print_name_prefix(cmdtp);
- my_puts_P(cmdtp->name);
+ print_prefixed_name(cmdtp);
my_puts_P(PSTR(" "));
if (cmdtp->help && *cmdtp->help != '\0')
#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
* @param argv Arguments
* @return 0 if command succeeded, else non-zero (CMD_RET_...)
*/
-command_ret_t cmd_call(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+static command_ret_t cmd_call(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
command_ret_t result;
/* 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;
}
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);
+ }
+}