]>
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
269 while ((opt
= getopt(argc
, argv
, PSTR("afk"))) != -1) {
275 options
|= OPT_NAME
| OPT_ALL
;
278 options
|= OPT_USAGE
| OPT_ALL
;
281 return CMD_RET_USAGE
;
285 /* remaining arguments */
289 if ((options
& (OPT_USAGE
|OPT_NAME
)) == (OPT_USAGE
|OPT_NAME
)) {
290 puts_P(PSTR("Inkompatible options: -f -k\n"
292 return CMD_RET_USAGE
;
294 if (options
& OPT_USAGE
)
297 if ((options
& (OPT_NAME
| OPT_USAGE
)) == 0 && argc
> 0)
300 char *optenv
= getenv_str(PSTR("cmd"));
301 if (optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
)
302 options
|= OPT_DBG_CMDS
;
304 uint_fast8_t maxlen_cmd
;
306 cmd_tbl_t
**cmd_list
= NULL
;
307 for (uint_fast8_t pass
= 0; pass
< 2; ++pass
) {
312 cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
313 /* TODO: Check error */
316 /* Make array of commands */
317 cmd_tbl_t
*tp
= tbl_start
;
319 while (tp
->name
!= NULL
) {
321 cmd_tbl_t
*sub
= tp
->subcmd
;
322 while (sub
->name
!= NULL
) {
323 if (options
& OPT_ALL
|| sub
->flags
& CTBL_SUBCMDAUTO
) {
328 uint_fast8_t len
= strlen_P(sub
->name
);
329 if ((sub
->flags
& CTBL_SUBCMDAUTO
) == 0) {
330 len
+= strlen_P(tp
->name
) + 1;
332 if (len
> maxlen_cmd
) {
335 //debug_cmd("### i:%3d, maxlen:%3d, tp: '%S', sub: '%S'\n", i, maxlen_cmd, tp->name, sub->name);
341 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
346 uint_fast8_t len
= strlen_P(tp
->name
);
347 cmd_tbl_t
*top
= get_cmd_tbl_parent(tp
);
349 len
+= strlen_P(top
->name
) + 1;
350 if (len
> maxlen_cmd
)
356 //debug_cmd("### pass: %d, cmd_items: %d, i: %d, maxlen_cmd: %d\n", pass, cmd_items, i, maxlen_cmd);
359 /* Sort command list */
360 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
362 if ((options
& OPT_LONG
) == 0) {
363 /* print short help (usage) */
364 for (uint_fast8_t cmdi
= 0; cmdi
< cmd_items
; cmdi
++) {
366 /* allow user abort */
368 res
= CMD_RET_FAILURE
;
371 if ((cmd_list
[cmdi
]->flags
& CTBL_DBG
) && !(options
& OPT_DBG_CMDS
))
373 if (cmd_list
[cmdi
]->usage
== NULL
)
377 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
379 for (uint_fast8_t argi
= 0; argi
< argc
; argi
++) {
380 if (((options
& OPT_NAME
) &&
381 strcasestr_P2(cmd_list
[cmdi
]->name
, argv
[argi
])) ||
382 ((options
& OPT_USAGE
) &&
383 strcasestr_P2(cmd_list
[cmdi
]->usage
, argv
[argi
]))) {
384 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
390 /* command help (long version) */
391 for (uint_fast8_t argi
= 0; argi
< argc
; ++argi
) {
392 uint_fast8_t got
= 0;
393 cmd_tbl_t
*tp
= find_cmd(argv
[argi
], tbl_start
);
398 if (options
& OPT_ALL
) {
399 for (cmd_tbl_t
*sub
= tbl_start
; sub
->name
!= NULL
; sub
++) {
401 tp
= find_cmd(argv
[argi
], sub
->subcmd
);
410 printf_P(PSTR("Unknown command '%s' - try 'help help'\n"),
412 res
= CMD_RET_FAILURE
;
422 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
424 print_usage_line(cmdtp
, 0);
425 #ifdef CONFIG_SYS_LONGHELP
426 my_puts_P(PSTR("Usage:\n"));
427 print_prefixed_name(cmdtp
);
428 my_puts_P(PSTR(" "));
430 if (cmdtp
->help
&& *cmdtp
->help
!= '\0')
433 my_puts_P(PSTR(" - No additional help available.\n"));
434 #endif /* CONFIG_SYS_LONGHELP */
435 return CMD_RET_FAILURE
;
438 #ifdef CONFIG_AUTO_COMPLETE
440 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
442 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
445 space
= last_char
== '\0' || isblank(last_char
);
447 if (space
&& argc
== 1)
448 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
450 if (!space
&& argc
== 2)
451 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
456 /*************************************************************************************/
458 /* TODO: cmdtp points to FLASH */
460 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
462 cmd_tbl_t
*cmdtp
= cmd_tbl
;
463 // const int count = ARRAY_SIZE(cmd_tbl);
464 // const cmd_tbl_t *cmdend = cmdtp + count;
477 /* output full list of commands */
478 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
479 if (n_found
>= maxv
- 2) {
480 cmdv
[n_found
++] = "...";
483 cmdv
[n_found
++] = cmdtp
->name
;
485 cmdv
[n_found
] = NULL
;
489 /* more than one arg or one but the start of the next */
490 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
491 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
492 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
496 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
503 /* return the partial matches */
504 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
506 clen
= strlen(cmdtp
->name
);
510 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
514 if (n_found
>= maxv
- 2) {
515 cmdv
[n_found
++] = "...";
519 cmdv
[n_found
++] = cmdtp
->name
;
522 cmdv
[n_found
] = NULL
;
526 static int make_argv(char *s
, int argvsz
, char *argv
[])
530 /* split into argv */
531 while (argc
< argvsz
- 1) {
533 /* skip any white space */
537 if (*s
== '\0') /* end of s, no more args */
540 argv
[argc
++] = s
; /* begin of argument string */
542 /* find end of string */
543 while (*s
&& !isblank(*s
))
546 if (*s
== '\0') /* end of s, no more args */
549 *s
++ = '\0'; /* terminate current arg */
556 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
558 int ll
= leader
!= NULL
? strlen(leader
) : 0;
559 int sl
= sep
!= NULL
? strlen(sep
) : 0;
563 my_puts_P(PSTR("\n"));
567 i
= linemax
; /* force leader and newline */
568 while (*argv
!= NULL
) {
569 len
= strlen(*argv
) + sl
;
570 if (i
+ len
>= linemax
) {
571 my_puts_P(PSTR("\n"));
580 my_puts_P(PSTR("\n"));
583 static int find_common_prefix(char * const argv
[])
586 char *anchor
, *s
, *t
;
593 len
= strlen(anchor
);
594 while ((t
= *argv
++) != NULL
) {
596 for (i
= 0; i
< len
; i
++, t
++, s
++) {
605 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
608 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
610 int n
= *np
, col
= *colp
;
611 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
615 int i
, j
, k
, len
, seplen
, argc
;
619 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
620 return 0; /* not in normal console */
624 last_char
= buf
[cnt
- 1];
628 /* copy to secondary buffer which will be affected */
629 strcpy(tmp_buf
, buf
);
631 /* separate into argv */
632 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
634 /* do the completion and return the possible completions */
635 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
637 /* no match; bell and out */
639 if (argc
> 1) /* allow tab for non command */
649 if (i
== 1) { /* one match; perfect */
650 k
= strlen(argv
[argc
- 1]);
655 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
656 k
= strlen(argv
[argc
- 1]);
666 /* make sure it fits */
667 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
673 for (i
= 0; i
< len
; i
++)
676 for (i
= 0; i
< seplen
; i
++)
687 print_argv(NULL
, " ", " ", 78, cmdv
);
695 #endif /* CONFIG_AUTO_COMPLETE */
698 static cmd_tbl_t
*cmd_invocation_ptr
;
701 * Call a command function. This should be the only route in U-Boot to call
702 * a command, so that we can track whether we are waiting for input or
703 * executing a command.
705 * @param cmdtp Pointer to the command to execute
706 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
707 * @param argc Number of arguments (arg 0 must be the command text)
708 * @param argv Arguments
709 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
711 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
713 command_ret_t result
;
715 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
716 // if (result != CMD_RET_SUCCESS)
717 // debug("Command failed, result=%d\n", result);
721 #pragma GCC diagnostic ignored "-Wclobbered"
723 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
724 uint_fast8_t *repeatable
)
726 command_ret_t rc
= CMD_RET_SUCCESS
;
729 /* Look up command in command table */
730 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
732 /* Check if this command has subcommands */
733 if (cmdtp
->subcmd
&& argc
> 1) {
735 /* Look up subcommand in subcommand table */
736 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
737 if (cmdtpsub
== NULL
) {
738 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
739 return CMD_RET_FAILURE
;
748 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
749 return CMD_RET_FAILURE
;
752 /* found - check max args */
753 if (argc
> cmdtp
->maxargs
)
756 #if defined(CONFIG_CMD_BOOTD)
757 /* avoid "bootd" recursion */
758 else if (cmdtp
->cmd
== do_bootd
) {
759 if (flag
& CMD_FLAG_BOOTD
) {
760 my_puts_P(PSTR("'bootd' recursion detected\n"));
761 rc
= CMD_RET_FAILURE
;
763 flag
|= CMD_FLAG_BOOTD
;
768 if (setjmp(cmd_jbuf
) != 0)
769 return CMD_RET_FAILURE
;
771 /* If OK so far, then do the command */
773 cmd_invocation_ptr
= cmdtp
;
774 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
775 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
777 if (rc
== CMD_RET_USAGE
)
778 rc
= cmd_usage(cmdtp
);
782 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
784 char buf
[strlen_P(cmdtp
->name
) + 1];
785 strcpy_P(buf
, cmdtp
->name
);
788 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);
795 void cmd_error(int status
, int errnum
, const char *fmt
, ...)
799 print_prefixed_name(cmd_invocation_ptr
);
801 my_puts_P(PSTR(": "));
802 vfprintf_P(stdout
, fmt
, ap
);
807 printf_P(PSTR(": %S"), my_strerror_P(errnum
));
813 longjmp(cmd_jbuf
, 1);