]>
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"
27 #define DEBUG_CMD 0 /* set to 1 to debug */
29 #define debug_cmd(fmt, args...) \
30 debug_cond(DEBUG_CMD, fmt, ##args)
35 char *strcasestr_P2(const FLASH
char *haystack
, const char *needle
)
37 int nlen
= strlen(needle
);
38 int hlen
= strlen_P(haystack
) - nlen
+ 1;
41 for (i
= 0; i
< hlen
; i
++) {
43 for (j
= 0; j
< nlen
; j
++) {
44 unsigned char c1
= haystack
[i
+j
];
45 unsigned char c2
= needle
[j
];
46 if (toupper(c1
) != toupper(c2
))
49 return (char *) haystack
+ i
;
58 static int cmd_tbl_item_count(cmd_tbl_t
*p
)
62 while (p
->name
!= NULL
) {
64 cmd_tbl_t
*sub
= p
->subcmd
;
65 while (sub
->name
!= NULL
) {
66 if (sub
->flags
& CTBL_SUBCMDAUTO
)
71 if ((p
->flags
& CTBL_SUBCMDAUTO
) == 0)
79 static cmd_tbl_t
*get_cmd_tbl_base(cmd_tbl_t
*cmdtp
)
83 while (p
->name
!= NULL
)
89 static cmd_tbl_t
*get_cmd_tbl_parent(cmd_tbl_t
*child
)
91 if ((child
->flags
& CTBL_SUBCMDAUTO
) == 0) {
93 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(child
);
94 if (tbl_start
!= cmd_tbl
) {
95 cmd_tbl_t
*top
= cmd_tbl
;
96 while (top
->subcmd
!= tbl_start
)
104 static int print_nameprefix(cmd_tbl_t
*p
)
106 cmd_tbl_t
*top
= get_cmd_tbl_parent(p
);
110 width
= printf_P(PSTR("%S "), top
->name
);
116 static int print_prefixed_name(cmd_tbl_t
*p
)
120 len
= print_nameprefix(p
);
121 len
+= strlen_P(p
->name
);
127 static void print_usage_line(cmd_tbl_t
*p
, int width
)
129 width
-= print_prefixed_name(p
);
133 my_puts_P(PSTR(" - "));
138 static int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
140 unsigned char c1
, c2
;
142 while ((c1
= *(const FLASH
unsigned char *)s1
++)
143 == (c2
= *(const FLASH
unsigned char *)s2
++))
152 static int cmpstring_PP(const void *p1
, const void *p2
)
154 char s1
[21] = {'\0'};
155 char s2
[21] = {'\0'};
156 cmd_tbl_t
*cp1
= *(cmd_tbl_t
**) p1
;
157 cmd_tbl_t
*cp2
= *(cmd_tbl_t
**) p2
;
158 cmd_tbl_t
*top1
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p1
);
159 cmd_tbl_t
*top2
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p2
);
162 strlcpy_P(s1
, top1
->name
, 21);
164 strlcpy_P(s2
, top2
->name
, 21);
166 strlcat_P(s1
, cp1
->name
, 21);
167 strlcat_P(s2
, cp2
->name
, 21);
169 return strcmp(s1
, s2
);
172 static int cmpstring_PP(const void *p1
, const void *p2
)
174 cmd_tbl_t
*cp1
= *(cmd_tbl_t
**) p1
;
175 cmd_tbl_t
*cp2
= *(cmd_tbl_t
**) p2
;
176 cmd_tbl_t
*top1
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p1
);
177 cmd_tbl_t
*top2
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p2
);
181 res
= strcmp_PP(top1
->name
, top2
->name
);
183 res
= strcmp_PP(cp1
->name
, cp2
->name
);
187 res
= strcmp_PP(top1
->name
, cp2
->name
);
189 res
= strcmp_PP(cp1
->name
, cp2
->name
);
193 res
= strcmp_PP(cp1
->name
, top2
->name
);
195 res
= strcmp_PP(cp1
->name
, cp2
->name
);
198 return strcmp_PP(cp1
->name
, cp2
->name
);
202 /****************************************************************************/
205 * find command table entry for a command
208 static cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
213 char *optenv
= getenv_str(PSTR("cmd"));
214 uint8_t opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
216 cmd_tbl_t
*cmdtp_ret
= NULL
;
217 uint_fast8_t n_found
= 0;
218 uint_fast8_t len
= strlen(cmd
);
220 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
222 for (cmd_tbl_t
*sub
= cmdtp
->subcmd
; sub
->name
!= NULL
; sub
++) {
223 if (sub
->flags
& CTBL_SUBCMDAUTO
&&
224 strncmp_P(cmd
, sub
->name
, len
) == 0 &&
225 (opt_debug
|| !(sub
->flags
& CTBL_DBG
))) {
226 if (len
== strlen_P(sub
->name
))
227 return sub
; /* full match */
228 cmdtp_ret
= sub
; /* abbreviated command ? */
233 if ((cmdtp
->flags
& CTBL_SUBCMDAUTO
) == 0 &&
234 strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
235 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
236 if (len
== strlen_P(cmdtp
->name
))
237 return cmdtp
; /* full match */
238 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
244 return cmdtp_ret
; /* exactly one match */
246 return NULL
; /* not found or ambiguous command */
250 * Use puts() instead of printf() to avoid printf buffer overflow
251 * for long help messages
254 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
256 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
257 command_ret_t res
= CMD_RET_SUCCESS
;
258 uint_fast8_t options
= 0;
259 #define OPT_DBG_CMDS 0x01
261 #define OPT_NAME 0x04
262 #define OPT_USAGE 0x08
263 #define OPT_LONG 0x10
266 while ((opt
= getopt(argc
, argv
, PSTR("afk"))) != -1) {
272 options
|= OPT_NAME
| OPT_ALL
;
275 options
|= OPT_USAGE
| OPT_ALL
;
278 return CMD_RET_USAGE
;
282 /* remaining arguments */
286 if ((options
& (OPT_USAGE
|OPT_NAME
)) == (OPT_USAGE
|OPT_NAME
)) {
287 puts_P(PSTR("Inkompatible options: -f -k\n"
289 return CMD_RET_USAGE
;
291 if (options
& OPT_USAGE
)
294 if ((options
& (OPT_NAME
| OPT_USAGE
)) == 0 && argc
> 0)
297 char *optenv
= getenv_str(PSTR("cmd"));
298 if (optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
)
299 options
|= OPT_DBG_CMDS
;
301 uint_fast8_t maxlen_cmd
;
303 cmd_tbl_t
**cmd_list
= NULL
;
304 for (uint_fast8_t pass
= 0; pass
< 2; ++pass
) {
309 cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
310 /* TODO: Check error */
313 /* Make array of commands */
314 cmd_tbl_t
*tp
= tbl_start
;
316 while (tp
->name
!= NULL
) {
318 cmd_tbl_t
*sub
= tp
->subcmd
;
319 while (sub
->name
!= NULL
) {
320 if (options
& OPT_ALL
|| sub
->flags
& CTBL_SUBCMDAUTO
) {
325 uint_fast8_t len
= strlen_P(sub
->name
);
326 if ((sub
->flags
& CTBL_SUBCMDAUTO
) == 0) {
327 len
+= strlen_P(tp
->name
) + 1;
329 if (len
> maxlen_cmd
) {
332 //debug_cmd("### i:%3d, maxlen:%3d, tp: '%S', sub: '%S'\n", i, maxlen_cmd, tp->name, sub->name);
338 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
343 uint_fast8_t len
= strlen_P(tp
->name
);
344 cmd_tbl_t
*top
= get_cmd_tbl_parent(tp
);
346 len
+= strlen_P(top
->name
) + 1;
347 if (len
> maxlen_cmd
)
353 //debug_cmd("### pass: %d, cmd_items: %d, i: %d, maxlen_cmd: %d\n", pass, cmd_items, i, maxlen_cmd);
356 /* Sort command list */
357 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
359 if ((options
& OPT_LONG
) == 0) {
360 /* print short help (usage) */
361 for (uint_fast8_t cmdi
= 0; cmdi
< cmd_items
; cmdi
++) {
363 /* allow user abort */
365 res
= CMD_RET_FAILURE
;
368 if ((cmd_list
[cmdi
]->flags
& CTBL_DBG
) && !(options
& OPT_DBG_CMDS
))
370 if (cmd_list
[cmdi
]->usage
== NULL
)
374 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
376 for (uint_fast8_t argi
= 0; argi
< argc
; argi
++) {
377 if (((options
& OPT_NAME
) &&
378 strcasestr_P2(cmd_list
[cmdi
]->name
, argv
[argi
])) ||
379 ((options
& OPT_USAGE
) &&
380 strcasestr_P2(cmd_list
[cmdi
]->usage
, argv
[argi
]))) {
381 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
387 /* command help (long version) */
388 for (uint_fast8_t argi
= 0; argi
< argc
; ++argi
) {
389 uint_fast8_t got
= 0;
390 cmd_tbl_t
*tp
= find_cmd(argv
[argi
], tbl_start
);
395 if (options
& OPT_ALL
) {
396 for (cmd_tbl_t
*sub
= tbl_start
; sub
->name
!= NULL
; sub
++) {
398 tp
= find_cmd(argv
[argi
], sub
->subcmd
);
407 printf_P(PSTR("Unknown command '%s' - try 'help help'\n"),
409 res
= CMD_RET_FAILURE
;
419 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
421 print_usage_line(cmdtp
, 0);
422 #ifdef CONFIG_SYS_LONGHELP
423 my_puts_P(PSTR("Usage:\n"));
424 print_prefixed_name(cmdtp
);
425 my_puts_P(PSTR(" "));
427 if (cmdtp
->help
&& *cmdtp
->help
!= '\0')
430 my_puts_P(PSTR(" - No additional help available.\n"));
431 #endif /* CONFIG_SYS_LONGHELP */
432 return CMD_RET_FAILURE
;
435 #ifdef CONFIG_AUTO_COMPLETE
437 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
439 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
442 space
= last_char
== '\0' || isblank(last_char
);
444 if (space
&& argc
== 1)
445 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
447 if (!space
&& argc
== 2)
448 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
453 /*************************************************************************************/
455 /* TODO: cmdtp points to FLASH */
457 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
459 cmd_tbl_t
*cmdtp
= cmd_tbl
;
460 // const int count = ARRAY_SIZE(cmd_tbl);
461 // const cmd_tbl_t *cmdend = cmdtp + count;
474 /* output full list of commands */
475 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
476 if (n_found
>= maxv
- 2) {
477 cmdv
[n_found
++] = "...";
480 cmdv
[n_found
++] = cmdtp
->name
;
482 cmdv
[n_found
] = NULL
;
486 /* more than one arg or one but the start of the next */
487 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
488 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
489 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
493 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
500 /* return the partial matches */
501 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
503 clen
= strlen(cmdtp
->name
);
507 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
511 if (n_found
>= maxv
- 2) {
512 cmdv
[n_found
++] = "...";
516 cmdv
[n_found
++] = cmdtp
->name
;
519 cmdv
[n_found
] = NULL
;
523 static int make_argv(char *s
, int argvsz
, char *argv
[])
527 /* split into argv */
528 while (argc
< argvsz
- 1) {
530 /* skip any white space */
534 if (*s
== '\0') /* end of s, no more args */
537 argv
[argc
++] = s
; /* begin of argument string */
539 /* find end of string */
540 while (*s
&& !isblank(*s
))
543 if (*s
== '\0') /* end of s, no more args */
546 *s
++ = '\0'; /* terminate current arg */
553 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
555 int ll
= leader
!= NULL
? strlen(leader
) : 0;
556 int sl
= sep
!= NULL
? strlen(sep
) : 0;
560 my_puts_P(PSTR("\n"));
564 i
= linemax
; /* force leader and newline */
565 while (*argv
!= NULL
) {
566 len
= strlen(*argv
) + sl
;
567 if (i
+ len
>= linemax
) {
568 my_puts_P(PSTR("\n"));
577 my_puts_P(PSTR("\n"));
580 static int find_common_prefix(char * const argv
[])
583 char *anchor
, *s
, *t
;
590 len
= strlen(anchor
);
591 while ((t
= *argv
++) != NULL
) {
593 for (i
= 0; i
< len
; i
++, t
++, s
++) {
602 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
605 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
607 int n
= *np
, col
= *colp
;
608 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
612 int i
, j
, k
, len
, seplen
, argc
;
616 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
617 return 0; /* not in normal console */
621 last_char
= buf
[cnt
- 1];
625 /* copy to secondary buffer which will be affected */
626 strcpy(tmp_buf
, buf
);
628 /* separate into argv */
629 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
631 /* do the completion and return the possible completions */
632 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
634 /* no match; bell and out */
636 if (argc
> 1) /* allow tab for non command */
646 if (i
== 1) { /* one match; perfect */
647 k
= strlen(argv
[argc
- 1]);
652 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
653 k
= strlen(argv
[argc
- 1]);
663 /* make sure it fits */
664 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
670 for (i
= 0; i
< len
; i
++)
673 for (i
= 0; i
< seplen
; i
++)
684 print_argv(NULL
, " ", " ", 78, cmdv
);
692 #endif /* CONFIG_AUTO_COMPLETE */
695 static cmd_tbl_t
*cmd_invocation_ptr
;
698 * Call a command function. This should be the only route in U-Boot to call
699 * a command, so that we can track whether we are waiting for input or
700 * executing a command.
702 * @param cmdtp Pointer to the command to execute
703 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
704 * @param argc Number of arguments (arg 0 must be the command text)
705 * @param argv Arguments
706 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
708 static command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
710 command_ret_t result
;
712 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
713 // if (result != CMD_RET_SUCCESS)
714 // debug("Command failed, result=%d\n", result);
718 #pragma GCC diagnostic ignored "-Wclobbered"
720 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
721 uint_fast8_t *repeatable
)
723 command_ret_t rc
= CMD_RET_SUCCESS
;
726 /* Look up command in command table */
727 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
729 /* Check if this command has subcommands */
730 if (cmdtp
->subcmd
&& argc
> 1) {
732 /* Look up subcommand in subcommand table */
733 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
734 if (cmdtpsub
== NULL
) {
735 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
736 return CMD_RET_FAILURE
;
745 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
746 return CMD_RET_FAILURE
;
749 /* found - check max args */
750 if (argc
> cmdtp
->maxargs
)
753 #if defined(CONFIG_CMD_BOOTD)
754 /* avoid "bootd" recursion */
755 else if (cmdtp
->cmd
== do_bootd
) {
756 if (flag
& CMD_FLAG_BOOTD
) {
757 my_puts_P(PSTR("'bootd' recursion detected\n"));
758 rc
= CMD_RET_FAILURE
;
760 flag
|= CMD_FLAG_BOOTD
;
765 if (setjmp(cmd_jbuf
) != 0)
766 return CMD_RET_FAILURE
;
768 /* If OK so far, then do the command */
770 optind
= 0; /* reset getopt() */
771 cmd_invocation_ptr
= cmdtp
;
772 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
773 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
775 if (rc
== CMD_RET_USAGE
)
776 rc
= cmd_usage(cmdtp
);
780 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
782 char buf
[strlen_P(cmdtp
->name
) + 1];
783 strcpy_P(buf
, cmdtp
->name
);
786 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);
793 void cmd_error(int status
, int errnum
, const char *fmt
, ...)
797 print_prefixed_name(cmd_invocation_ptr
);
799 my_puts_P(PSTR(": "));
800 vfprintf_P(stdout
, fmt
, ap
);
805 printf_P(PSTR(": %S"), my_strerror_P(errnum
));
811 longjmp(cmd_jbuf
, 1);