]>
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_name_prefix(cmd_tbl_t
*p
)
105 cmd_tbl_t
*top
= get_cmd_tbl_parent(p
);
109 width
= printf_P(PSTR("%S "), top
->name
);
115 static void print_usage_line(cmd_tbl_t
*p
, int width
)
117 width
-= strlen_P(p
->name
);
118 width
-= print_name_prefix(p
);
123 my_puts_P(PSTR(" - "));
128 static int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
130 unsigned char c1
, c2
;
132 while ((c1
= *(const FLASH
unsigned char *)s1
++)
133 == (c2
= *(const FLASH
unsigned char *)s2
++))
142 static int cmpstring_PP(const void *p1
, const void *p2
)
144 char s1
[21] = {'\0'};
145 char s2
[21] = {'\0'};
146 cmd_tbl_t
*cp1
= *(cmd_tbl_t
**) p1
;
147 cmd_tbl_t
*cp2
= *(cmd_tbl_t
**) p2
;
148 cmd_tbl_t
*top1
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p1
);
149 cmd_tbl_t
*top2
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p2
);
152 strlcpy_P(s1
, top1
->name
, 21);
154 strlcpy_P(s2
, top2
->name
, 21);
156 strlcat_P(s1
, cp1
->name
, 21);
157 strlcat_P(s2
, cp2
->name
, 21);
159 return strcmp(s1
, s2
);
162 static int cmpstring_PP(const void *p1
, const void *p2
)
164 cmd_tbl_t
*cp1
= *(cmd_tbl_t
**) p1
;
165 cmd_tbl_t
*cp2
= *(cmd_tbl_t
**) p2
;
166 cmd_tbl_t
*top1
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p1
);
167 cmd_tbl_t
*top2
= get_cmd_tbl_parent(*(cmd_tbl_t
**) p2
);
171 res
= strcmp_PP(top1
->name
, top2
->name
);
173 res
= strcmp_PP(cp1
->name
, cp2
->name
);
177 res
= strcmp_PP(top1
->name
, cp2
->name
);
179 res
= strcmp_PP(cp1
->name
, cp2
->name
);
183 res
= strcmp_PP(cp1
->name
, top2
->name
);
185 res
= strcmp_PP(cp1
->name
, cp2
->name
);
188 return strcmp_PP(cp1
->name
, cp2
->name
);
192 /****************************************************************************/
195 * find command table entry for a command
198 static cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
203 char *optenv
= getenv_str(PSTR("cmd"));
204 uint8_t opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
206 cmd_tbl_t
*cmdtp_ret
= NULL
;
207 uint_fast8_t n_found
= 0;
208 uint_fast8_t len
= strlen(cmd
);
210 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
212 for (cmd_tbl_t
*sub
= cmdtp
->subcmd
; sub
->name
!= NULL
; sub
++) {
213 if (sub
->flags
& CTBL_SUBCMDAUTO
&&
214 strncmp_P(cmd
, sub
->name
, len
) == 0 &&
215 (opt_debug
|| !(sub
->flags
& CTBL_DBG
))) {
216 if (len
== strlen_P(sub
->name
))
217 return sub
; /* full match */
218 cmdtp_ret
= sub
; /* abbreviated command ? */
223 if ((cmdtp
->flags
& CTBL_SUBCMDAUTO
) == 0 &&
224 strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
225 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
226 if (len
== strlen_P(cmdtp
->name
))
227 return cmdtp
; /* full match */
228 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
234 return cmdtp_ret
; /* exactly one match */
236 return NULL
; /* not found or ambiguous command */
240 * Use puts() instead of printf() to avoid printf buffer overflow
241 * for long help messages
244 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
246 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
247 command_ret_t res
= CMD_RET_SUCCESS
;
248 uint_fast8_t options
= 0;
249 #define OPT_DBG_CMDS 0x01
251 #define OPT_NAME 0x04
252 #define OPT_USAGE 0x08
253 #define OPT_LONG 0x10
259 while ((opt
= getopt(argc
, argv
, PSTR("afk"))) != -1) {
265 options
|= OPT_NAME
| OPT_ALL
;
268 options
|= OPT_USAGE
| OPT_ALL
;
271 return CMD_RET_USAGE
;
275 /* remaining arguments */
279 if ((options
& (OPT_USAGE
|OPT_NAME
)) == (OPT_USAGE
|OPT_NAME
)) {
280 puts_P(PSTR("Inkompatible options: -f -k\n"
282 return CMD_RET_USAGE
;
284 if (options
& OPT_USAGE
)
287 if ((options
& (OPT_NAME
| OPT_USAGE
)) == 0 && argc
> 0)
290 char *optenv
= getenv_str(PSTR("cmd"));
291 if (optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
)
292 options
|= OPT_DBG_CMDS
;
294 uint_fast8_t maxlen_cmd
;
296 cmd_tbl_t
**cmd_list
= NULL
;
297 for (uint_fast8_t pass
= 0; pass
< 2; ++pass
) {
302 cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
303 /* TODO: Check error */
306 /* Make array of commands */
307 cmd_tbl_t
*tp
= tbl_start
;
309 while (tp
->name
!= NULL
) {
311 cmd_tbl_t
*sub
= tp
->subcmd
;
312 while (sub
->name
!= NULL
) {
313 if (options
& OPT_ALL
|| sub
->flags
& CTBL_SUBCMDAUTO
) {
318 uint_fast8_t len
= strlen_P(sub
->name
);
319 if ((sub
->flags
& CTBL_SUBCMDAUTO
) == 0) {
320 len
+= strlen_P(tp
->name
) + 1;
322 if (len
> maxlen_cmd
) {
325 //debug_cmd("### i:%3d, maxlen:%3d, tp: '%S', sub: '%S'\n", i, maxlen_cmd, tp->name, sub->name);
331 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
336 uint_fast8_t len
= strlen_P(tp
->name
);
337 cmd_tbl_t
*top
= get_cmd_tbl_parent(tp
);
339 len
+= strlen_P(top
->name
) + 1;
340 if (len
> maxlen_cmd
)
346 //debug_cmd("### pass: %d, cmd_items: %d, i: %d, maxlen_cmd: %d\n", pass, cmd_items, i, maxlen_cmd);
349 /* Sort command list */
350 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
352 if ((options
& OPT_LONG
) == 0) {
353 /* print short help (usage) */
354 for (uint_fast8_t cmdi
= 0; cmdi
< cmd_items
; cmdi
++) {
356 /* allow user abort */
358 res
= CMD_RET_FAILURE
;
361 if ((cmd_list
[cmdi
]->flags
& CTBL_DBG
) && !(options
& OPT_DBG_CMDS
))
363 if (cmd_list
[cmdi
]->usage
== NULL
)
367 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
369 for (uint_fast8_t argi
= 0; argi
< argc
; argi
++) {
370 if (((options
& OPT_NAME
) &&
371 strcasestr_P2(cmd_list
[cmdi
]->name
, argv
[argi
])) ||
372 ((options
& OPT_USAGE
) &&
373 strcasestr_P2(cmd_list
[cmdi
]->usage
, argv
[argi
]))) {
374 print_usage_line(cmd_list
[cmdi
], maxlen_cmd
);
380 /* command help (long version) */
381 for (uint_fast8_t argi
= 0; argi
< argc
; ++argi
) {
382 uint_fast8_t got
= 0;
383 cmd_tbl_t
*tp
= find_cmd(argv
[argi
], tbl_start
);
388 if (options
& OPT_ALL
) {
389 for (cmd_tbl_t
*sub
= tbl_start
; sub
->name
!= NULL
; sub
++) {
391 tp
= find_cmd(argv
[argi
], sub
->subcmd
);
400 printf_P(PSTR("Unknown command '%s' - try 'help help'\n"),
402 res
= CMD_RET_FAILURE
;
412 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
414 print_usage_line(cmdtp
, 0);
415 #ifdef CONFIG_SYS_LONGHELP
416 my_puts_P(PSTR("Usage:\n"));
417 print_name_prefix(cmdtp
);
418 my_puts_P(cmdtp
->name
);
419 my_puts_P(PSTR(" "));
421 if (cmdtp
->help
&& *cmdtp
->help
!= '\0')
424 my_puts_P(PSTR(" - No additional help available.\n"));
425 #endif /* CONFIG_SYS_LONGHELP */
426 return CMD_RET_FAILURE
;
429 #ifdef CONFIG_AUTO_COMPLETE
431 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
433 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
436 space
= last_char
== '\0' || isblank(last_char
);
438 if (space
&& argc
== 1)
439 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
441 if (!space
&& argc
== 2)
442 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
447 /*************************************************************************************/
449 /* TODO: cmdtp points to FLASH */
451 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
453 cmd_tbl_t
*cmdtp
= cmd_tbl
;
454 // const int count = ARRAY_SIZE(cmd_tbl);
455 // const cmd_tbl_t *cmdend = cmdtp + count;
468 /* output full list of commands */
469 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
470 if (n_found
>= maxv
- 2) {
471 cmdv
[n_found
++] = "...";
474 cmdv
[n_found
++] = cmdtp
->name
;
476 cmdv
[n_found
] = NULL
;
480 /* more than one arg or one but the start of the next */
481 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
482 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
483 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
487 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
494 /* return the partial matches */
495 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
497 clen
= strlen(cmdtp
->name
);
501 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
505 if (n_found
>= maxv
- 2) {
506 cmdv
[n_found
++] = "...";
510 cmdv
[n_found
++] = cmdtp
->name
;
513 cmdv
[n_found
] = NULL
;
517 static int make_argv(char *s
, int argvsz
, char *argv
[])
521 /* split into argv */
522 while (argc
< argvsz
- 1) {
524 /* skip any white space */
528 if (*s
== '\0') /* end of s, no more args */
531 argv
[argc
++] = s
; /* begin of argument string */
533 /* find end of string */
534 while (*s
&& !isblank(*s
))
537 if (*s
== '\0') /* end of s, no more args */
540 *s
++ = '\0'; /* terminate current arg */
547 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
549 int ll
= leader
!= NULL
? strlen(leader
) : 0;
550 int sl
= sep
!= NULL
? strlen(sep
) : 0;
554 my_puts_P(PSTR("\n"));
558 i
= linemax
; /* force leader and newline */
559 while (*argv
!= NULL
) {
560 len
= strlen(*argv
) + sl
;
561 if (i
+ len
>= linemax
) {
562 my_puts_P(PSTR("\n"));
571 my_puts_P(PSTR("\n"));
574 static int find_common_prefix(char * const argv
[])
577 char *anchor
, *s
, *t
;
584 len
= strlen(anchor
);
585 while ((t
= *argv
++) != NULL
) {
587 for (i
= 0; i
< len
; i
++, t
++, s
++) {
596 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
599 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
601 int n
= *np
, col
= *colp
;
602 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
606 int i
, j
, k
, len
, seplen
, argc
;
610 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
611 return 0; /* not in normal console */
615 last_char
= buf
[cnt
- 1];
619 /* copy to secondary buffer which will be affected */
620 strcpy(tmp_buf
, buf
);
622 /* separate into argv */
623 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
625 /* do the completion and return the possible completions */
626 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
628 /* no match; bell and out */
630 if (argc
> 1) /* allow tab for non command */
640 if (i
== 1) { /* one match; perfect */
641 k
= strlen(argv
[argc
- 1]);
646 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
647 k
= strlen(argv
[argc
- 1]);
657 /* make sure it fits */
658 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
664 for (i
= 0; i
< len
; i
++)
667 for (i
= 0; i
< seplen
; i
++)
678 print_argv(NULL
, " ", " ", 78, cmdv
);
686 #endif /* CONFIG_AUTO_COMPLETE */
691 * Call a command function. This should be the only route in U-Boot to call
692 * a command, so that we can track whether we are waiting for input or
693 * executing a command.
695 * @param cmdtp Pointer to the command to execute
696 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
697 * @param argc Number of arguments (arg 0 must be the command text)
698 * @param argv Arguments
699 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
701 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
703 command_ret_t result
;
705 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
706 // if (result != CMD_RET_SUCCESS)
707 // debug("Command failed, result=%d\n", result);
711 #pragma GCC diagnostic ignored "-Wclobbered"
713 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
714 uint_fast8_t *repeatable
)
716 command_ret_t rc
= CMD_RET_SUCCESS
;
719 /* Look up command in command table */
720 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
722 /* Check if this command has subcommands */
723 if (cmdtp
->subcmd
&& argc
> 1) {
725 /* Look up subcommand in subcommand table */
726 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
727 if (cmdtpsub
== NULL
) {
728 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
729 return CMD_RET_FAILURE
;
738 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
739 return CMD_RET_FAILURE
;
742 /* found - check max args */
743 if (argc
> cmdtp
->maxargs
)
746 #if defined(CONFIG_CMD_BOOTD)
747 /* avoid "bootd" recursion */
748 else if (cmdtp
->cmd
== do_bootd
) {
749 if (flag
& CMD_FLAG_BOOTD
) {
750 my_puts_P(PSTR("'bootd' recursion detected\n"));
751 rc
= CMD_RET_FAILURE
;
753 flag
|= CMD_FLAG_BOOTD
;
758 if (setjmp(cmd_jbuf
) != 0)
759 return CMD_RET_FAILURE
;
761 /* If OK so far, then do the command */
763 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
764 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
766 if (rc
== CMD_RET_USAGE
)
767 rc
= cmd_usage(cmdtp
);
771 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
773 char buf
[strlen_P(cmdtp
->name
) + 1];
774 strcpy_P(buf
, cmdtp
->name
);
777 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);