]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
2 * (C) Copyright 2014, 2016, 2018 Leo C. <erbl259-lmu@yahoo.de>
4 * (C) Copyright 2000-2009
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0
11 * Command Processor Table
20 #include "print-utils.h"
21 #include "con-utils.h"
24 #include "getopt-min.h"
26 #define DEBUG_CMD 0 /* set to 1 to debug */
28 #define debug_cmd(fmt, args...) \
29 debug_cond(DEBUG_CMD, fmt, ##args)
34 char *strcasestr_P2(const FLASH
char *haystack
, const char *needle
)
36 int nlen
= strlen(needle
);
37 int hlen
= strlen_P(haystack
) - nlen
+ 1;
40 for (i
= 0; i
< hlen
; i
++) {
42 for (j
= 0; j
< nlen
; j
++) {
43 unsigned char c1
= haystack
[i
+j
];
44 unsigned char c2
= needle
[j
];
45 if (toupper(c1
) != toupper(c2
))
48 return (char *) haystack
+ i
;
57 static int cmd_tbl_item_count(cmd_tbl_t
*p
)
61 while (p
->name
!= NULL
) {
63 cmd_tbl_t
*sub
= p
->subcmd
;
64 while (sub
->name
!= NULL
) {
65 if (sub
->flags
& CTBL_SUBCMDAUTO
)
70 if ((p
->flags
& CTBL_SUBCMDAUTO
) == 0)
78 static cmd_tbl_t
*get_cmd_tbl_base(cmd_tbl_t
*cmdtp
)
82 while (p
->name
!= NULL
)
88 static cmd_tbl_t
*get_cmd_tbl_parent(cmd_tbl_t
*child
)
90 if ((child
->flags
& CTBL_SUBCMDAUTO
) == 0) {
92 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(child
);
93 if (tbl_start
!= cmd_tbl
) {
94 cmd_tbl_t
*top
= cmd_tbl
;
95 while (top
->subcmd
!= tbl_start
)
103 static int print_nameprefix(cmd_tbl_t
*p
)
105 cmd_tbl_t
*top
= get_cmd_tbl_parent(p
);
109 width
= printf_P(PSTR("%S "), top
->name
);
115 static int print_prefixed_name(cmd_tbl_t
*p
)
119 len
= print_nameprefix(p
);
120 len
+= strlen_P(p
->name
);
126 static void print_usage_line(cmd_tbl_t
*p
, int width
)
128 width
-= print_prefixed_name(p
);
132 my_puts_P(PSTR(" - "));
137 static int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
139 unsigned char c1
, c2
;
141 while ((c1
= *(const FLASH
unsigned char *)s1
++)
142 == (c2
= *(const FLASH
unsigned char *)s2
++))
151 static int cmpstring_PP(const void *p1
, const void *p2
)
153 char s1
[21] = {'\0'};
154 char s2
[21] = {'\0'};
155 cmd_tbl_t
*cp1
= *(cmd_tbl_t
**) p1
;
156 cmd_tbl_t
*cp2
= *(cmd_tbl_t
**) p2
;
157 cmd_tbl_t
*top1
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p1
);
158 cmd_tbl_t
*top2
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p2
);
161 strlcpy_P(s1
, top1
->name
, 21);
163 strlcpy_P(s2
, top2
->name
, 21);
165 strlcat_P(s1
, cp1
->name
, 21);
166 strlcat_P(s2
, cp2
->name
, 21);
168 return strcmp(s1
, s2
);
171 static int cmpstring_PP(const void *p1
, const void *p2
)
173 cmd_tbl_t
*cp1
= *(cmd_tbl_t
**) p1
;
174 cmd_tbl_t
*cp2
= *(cmd_tbl_t
**) p2
;
175 cmd_tbl_t
*top1
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p1
);
176 cmd_tbl_t
*top2
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p2
);
180 res
= strcmp_PP(top1
->name
, top2
->name
);
182 res
= strcmp_PP(cp1
->name
, cp2
->name
);
186 res
= strcmp_PP(top1
->name
, cp2
->name
);
188 res
= strcmp_PP(cp1
->name
, cp2
->name
);
192 res
= strcmp_PP(cp1
->name
, top2
->name
);
194 res
= strcmp_PP(cp1
->name
, cp2
->name
);
197 return strcmp_PP(cp1
->name
, cp2
->name
);
201 /****************************************************************************/
204 * find command table entry for a command
207 static cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
212 char *optenv
= getenv_str(PSTR("cmd"));
213 uint8_t opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
215 cmd_tbl_t
*cmdtp_ret
= NULL
;
216 uint_fast8_t n_found
= 0;
217 uint_fast8_t len
= strlen(cmd
);
219 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
221 for (cmd_tbl_t
*sub
= cmdtp
->subcmd
; sub
->name
!= NULL
; sub
++) {
222 if (sub
->flags
& CTBL_SUBCMDAUTO
&&
223 strncmp_P(cmd
, sub
->name
, len
) == 0 &&
224 (opt_debug
|| !(sub
->flags
& CTBL_DBG
))) {
225 if (len
== strlen_P(sub
->name
))
226 return sub
; /* full match */
227 cmdtp_ret
= sub
; /* abbreviated command ? */
232 if ((cmdtp
->flags
& CTBL_SUBCMDAUTO
) == 0 &&
233 strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
234 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
235 if (len
== strlen_P(cmdtp
->name
))
236 return cmdtp
; /* full match */
237 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
243 return cmdtp_ret
; /* exactly one match */
245 return NULL
; /* not found or ambiguous command */
249 * Use puts() instead of printf() to avoid printf buffer overflow
250 * for long help messages
253 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
255 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
256 command_ret_t res
= CMD_RET_SUCCESS
;
257 uint_fast8_t options
= 0;
258 #define OPT_DBG_CMDS 0x01
260 #define OPT_NAME 0x04
261 #define OPT_USAGE 0x08
262 #define OPT_LONG 0x10
268 while ((opt
= getopt(argc
, argv
, PSTR("afk"))) != -1) {
274 options
|= OPT_NAME
| OPT_ALL
;
277 options
|= OPT_USAGE
| OPT_ALL
;
280 return CMD_RET_USAGE
;
284 /* remaining arguments */
288 if ((options
& (OPT_USAGE
|OPT_NAME
)) == (OPT_USAGE
|OPT_NAME
)) {
289 puts_P(PSTR("Inkompatible options: -f -k\n"
291 return CMD_RET_USAGE
;
293 if (options
& OPT_USAGE
)
296 if ((options
& (OPT_NAME
| OPT_USAGE
)) == 0 && argc
> 0)
299 char *optenv
= getenv_str(PSTR("cmd"));
300 if (optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
)
301 options
|= OPT_DBG_CMDS
;
303 uint_fast8_t maxlen_cmd
;
305 cmd_tbl_t
**cmd_list
= NULL
;
306 for (uint_fast8_t pass
= 0; pass
< 2; ++pass
) {
311 cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
312 /* TODO: Check error */
315 /* Make array of commands */
316 cmd_tbl_t
*tp
= tbl_start
;
318 while (tp
->name
!= NULL
) {
320 cmd_tbl_t
*sub
= tp
->subcmd
;
321 while (sub
->name
!= NULL
) {
322 if (options
& OPT_ALL
|| sub
->flags
& CTBL_SUBCMDAUTO
) {
327 uint_fast8_t len
= strlen_P(sub
->name
);
328 if ((sub
->flags
& CTBL_SUBCMDAUTO
) == 0) {
329 len
+= strlen_P(tp
->name
) + 1;
331 if (len
> maxlen_cmd
) {
334 //debug_cmd("### i:%3d, maxlen:%3d, tp: '%S', sub: '%S'\n", i, maxlen_cmd, tp->name, sub->name);
340 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
345 uint_fast8_t len
= strlen_P(tp
->name
);
346 cmd_tbl_t
*top
= get_cmd_tbl_parent(tp
);
348 len
+= strlen_P(top
->name
) + 1;
349 if (len
> maxlen_cmd
)
355 //debug_cmd("### pass: %d, cmd_items: %d, i: %d, maxlen_cmd: %d\n", pass, cmd_items, i, maxlen_cmd);
358 /* Sort command list */
359 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
361 if ((options
& OPT_LONG
) == 0) {
362 /* print short help (usage) */
363 for (uint_fast8_t cmdi
= 0; cmdi
< cmd_items
; cmdi
++) {
365 /* allow user abort */
367 res
= CMD_RET_FAILURE
;
370 if ((cmd_list
[cmdi
]->flags
& CTBL_DBG
) && !(options
& OPT_DBG_CMDS
))
372 if (cmd_list
[cmdi
]->usage
== NULL
)
376 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
378 for (uint_fast8_t argi
= 0; argi
< argc
; argi
++) {
379 if (((options
& OPT_NAME
) &&
380 strcasestr_P2(cmd_list
[cmdi
]->name
, argv
[argi
])) ||
381 ((options
& OPT_USAGE
) &&
382 strcasestr_P2(cmd_list
[cmdi
]->usage
, argv
[argi
]))) {
383 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
389 /* command help (long version) */
390 for (uint_fast8_t argi
= 0; argi
< argc
; ++argi
) {
391 uint_fast8_t got
= 0;
392 cmd_tbl_t
*tp
= find_cmd(argv
[argi
], tbl_start
);
397 if (options
& OPT_ALL
) {
398 for (cmd_tbl_t
*sub
= tbl_start
; sub
->name
!= NULL
; sub
++) {
400 tp
= find_cmd(argv
[argi
], sub
->subcmd
);
409 printf_P(PSTR("Unknown command '%s' - try 'help help'\n"),
411 res
= CMD_RET_FAILURE
;
421 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
423 print_usage_line(cmdtp
, 0);
424 #ifdef CONFIG_SYS_LONGHELP
425 my_puts_P(PSTR("Usage:\n"));
426 print_prefixed_name(cmdtp
);
427 my_puts_P(PSTR(" "));
429 if (cmdtp
->help
&& *cmdtp
->help
!= '\0')
432 my_puts_P(PSTR(" - No additional help available.\n"));
433 #endif /* CONFIG_SYS_LONGHELP */
434 return CMD_RET_FAILURE
;
437 #ifdef CONFIG_AUTO_COMPLETE
439 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
441 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
444 space
= last_char
== '\0' || isblank(last_char
);
446 if (space
&& argc
== 1)
447 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
449 if (!space
&& argc
== 2)
450 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
455 /*************************************************************************************/
457 /* TODO: cmdtp points to FLASH */
459 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
461 cmd_tbl_t
*cmdtp
= cmd_tbl
;
462 // const int count = ARRAY_SIZE(cmd_tbl);
463 // const cmd_tbl_t *cmdend = cmdtp + count;
476 /* output full list of commands */
477 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
478 if (n_found
>= maxv
- 2) {
479 cmdv
[n_found
++] = "...";
482 cmdv
[n_found
++] = cmdtp
->name
;
484 cmdv
[n_found
] = NULL
;
488 /* more than one arg or one but the start of the next */
489 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
490 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
491 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
495 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
502 /* return the partial matches */
503 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
505 clen
= strlen(cmdtp
->name
);
509 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
513 if (n_found
>= maxv
- 2) {
514 cmdv
[n_found
++] = "...";
518 cmdv
[n_found
++] = cmdtp
->name
;
521 cmdv
[n_found
] = NULL
;
525 static int make_argv(char *s
, int argvsz
, char *argv
[])
529 /* split into argv */
530 while (argc
< argvsz
- 1) {
532 /* skip any white space */
536 if (*s
== '\0') /* end of s, no more args */
539 argv
[argc
++] = s
; /* begin of argument string */
541 /* find end of string */
542 while (*s
&& !isblank(*s
))
545 if (*s
== '\0') /* end of s, no more args */
548 *s
++ = '\0'; /* terminate current arg */
555 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
557 int ll
= leader
!= NULL
? strlen(leader
) : 0;
558 int sl
= sep
!= NULL
? strlen(sep
) : 0;
562 my_puts_P(PSTR("\n"));
566 i
= linemax
; /* force leader and newline */
567 while (*argv
!= NULL
) {
568 len
= strlen(*argv
) + sl
;
569 if (i
+ len
>= linemax
) {
570 my_puts_P(PSTR("\n"));
579 my_puts_P(PSTR("\n"));
582 static int find_common_prefix(char * const argv
[])
585 char *anchor
, *s
, *t
;
592 len
= strlen(anchor
);
593 while ((t
= *argv
++) != NULL
) {
595 for (i
= 0; i
< len
; i
++, t
++, s
++) {
604 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
607 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
609 int n
= *np
, col
= *colp
;
610 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
614 int i
, j
, k
, len
, seplen
, argc
;
618 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
619 return 0; /* not in normal console */
623 last_char
= buf
[cnt
- 1];
627 /* copy to secondary buffer which will be affected */
628 strcpy(tmp_buf
, buf
);
630 /* separate into argv */
631 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
633 /* do the completion and return the possible completions */
634 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
636 /* no match; bell and out */
638 if (argc
> 1) /* allow tab for non command */
648 if (i
== 1) { /* one match; perfect */
649 k
= strlen(argv
[argc
- 1]);
654 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
655 k
= strlen(argv
[argc
- 1]);
665 /* make sure it fits */
666 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
672 for (i
= 0; i
< len
; i
++)
675 for (i
= 0; i
< seplen
; i
++)
686 print_argv(NULL
, " ", " ", 78, cmdv
);
694 #endif /* CONFIG_AUTO_COMPLETE */
697 static cmd_tbl_t
*cmd_invocation_ptr
;
700 * Call a command function. This should be the only route in U-Boot to call
701 * a command, so that we can track whether we are waiting for input or
702 * executing a command.
704 * @param cmdtp Pointer to the command to execute
705 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
706 * @param argc Number of arguments (arg 0 must be the command text)
707 * @param argv Arguments
708 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
710 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
712 command_ret_t result
;
714 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
715 // if (result != CMD_RET_SUCCESS)
716 // debug("Command failed, result=%d\n", result);
720 #pragma GCC diagnostic ignored "-Wclobbered"
722 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
723 uint_fast8_t *repeatable
)
725 command_ret_t rc
= CMD_RET_SUCCESS
;
728 /* Look up command in command table */
729 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
731 /* Check if this command has subcommands */
732 if (cmdtp
->subcmd
&& argc
> 1) {
734 /* Look up subcommand in subcommand table */
735 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
736 if (cmdtpsub
== NULL
) {
737 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
738 return CMD_RET_FAILURE
;
747 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
748 return CMD_RET_FAILURE
;
751 /* found - check max args */
752 if (argc
> cmdtp
->maxargs
)
755 #if defined(CONFIG_CMD_BOOTD)
756 /* avoid "bootd" recursion */
757 else if (cmdtp
->cmd
== do_bootd
) {
758 if (flag
& CMD_FLAG_BOOTD
) {
759 my_puts_P(PSTR("'bootd' recursion detected\n"));
760 rc
= CMD_RET_FAILURE
;
762 flag
|= CMD_FLAG_BOOTD
;
767 if (setjmp(cmd_jbuf
) != 0)
768 return CMD_RET_FAILURE
;
770 /* If OK so far, then do the command */
772 cmd_invocation_ptr
= cmdtp
;
773 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
774 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
776 if (rc
== CMD_RET_USAGE
)
777 rc
= cmd_usage(cmdtp
);
781 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
783 char buf
[strlen_P(cmdtp
->name
) + 1];
784 strcpy_P(buf
, cmdtp
->name
);
787 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);
795 void cmd_error(const char *fmt
, ...)
799 print_prefixed_name(cmd_invocation_ptr
);
800 my_puts_P(PSTR(": "));
801 vfprintf_P(stdout
, fmt
, ap
);
805 //command_ret = CMD_RET_FAILURE;