]>
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"
25 #define DEBUG_CMD 1 /* set to 1 to debug */
27 #define debug_cmd(fmt, args...) \
28 debug_cond(DEBUG_CMD, fmt, ##args)
34 static int cmd_tbl_item_count(cmd_tbl_t
*p
)
38 while (p
->name
!= NULL
) {
40 cmd_tbl_t
*sub
= p
->subcmd
;
41 while (sub
->name
!= NULL
) {
42 if (sub
->flags
& CTBL_SUBCMDAUTO
)
47 if ((p
->flags
& CTBL_SUBCMDAUTO
) == 0)
54 static cmd_tbl_t
*get_cmd_tbl_base(cmd_tbl_t
*cmdtp
)
58 while (p
->name
!= NULL
)
64 static void print_name_prefix(cmd_tbl_t
*p
)
66 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(p
);
69 if (tbl_start
== cmd_tbl
)
73 while (top
->subcmd
!= tbl_start
)
77 if (top
&& (p
->flags
& CTBL_SUBCMDAUTO
) == 0) {
78 printf_P(PSTR("%S "), top
->name
);
82 static void print_usage_line(cmd_tbl_t
*p
, int width
)
84 width
-= strlen_P(p
->name
);
90 my_puts_P(PSTR(" - "));
94 static int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
98 while ((c1
= *(const FLASH
unsigned char *)s1
++)
99 == (c2
= *(const FLASH
unsigned char *)s2
++))
106 static int cmpstring_PP(const void *p1
, const void *p2
)
108 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
109 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
112 /****************************************************************************/
115 * find command table entry for a command
118 static cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
123 char *optenv
= getenv_str(PSTR("cmd"));
124 uint8_t opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
126 cmd_tbl_t
*cmdtp_ret
= NULL
;
127 uint_fast8_t n_found
= 0;
128 uint_fast8_t len
= strlen(cmd
);
130 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
132 for (cmd_tbl_t
*sub
= cmdtp
->subcmd
; sub
->name
!= NULL
; sub
++) {
133 if (sub
->flags
& CTBL_SUBCMDAUTO
&&
134 strncmp_P(cmd
, sub
->name
, len
) == 0 &&
135 (opt_debug
|| !(sub
->flags
& CTBL_DBG
))) {
136 if (len
== strlen_P(sub
->name
))
137 return sub
; /* full match */
138 cmdtp_ret
= sub
; /* abbreviated command ? */
143 if ((cmdtp
->flags
& CTBL_SUBCMDAUTO
) == 0 &&
144 strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
145 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
146 if (len
== strlen_P(cmdtp
->name
))
147 return cmdtp
; /* full match */
148 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
154 return cmdtp_ret
; /* exactly one match */
156 return NULL
; /* not found or ambiguous command */
160 * Use puts() instead of printf() to avoid printf buffer overflow
161 * for long help messages
164 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
166 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
167 command_ret_t rc
= CMD_RET_SUCCESS
;
169 char *optenv
= getenv_str(PSTR("cmd"));
170 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
172 if (argc
== 1) { /*show list of commands */
173 int cmd_items
= cmd_tbl_item_count(tbl_start
);
174 cmd_tbl_t
**cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
175 uint_fast8_t maxlen_cmd
= 0;
177 /* Make array of commands */
178 cmd_tbl_t
*tp
= tbl_start
;
180 while (tp
->name
!= NULL
) {
182 cmd_tbl_t
*sub
= tp
->subcmd
;
183 while (sub
->name
!= NULL
) {
184 if (sub
->flags
& CTBL_SUBCMDAUTO
) {
185 uint_fast8_t len
= strlen_P(sub
->name
);
186 if (len
> maxlen_cmd
)
193 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
194 uint_fast8_t len
= strlen_P(tp
->name
);
195 if (len
> maxlen_cmd
)
201 /* Sort command list */
202 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
204 /* print short help (usage) */
205 for (int i
= 0; i
< cmd_items
; i
++) {
206 if ((cmd_list
[i
]->flags
& CTBL_DBG
) && !opt_debug
)
208 if (cmd_list
[i
]->usage
== NULL
)
211 print_usage_line(cmd_list
[i
], maxlen_cmd
);
213 /* allow user abort */
215 rc
= CMD_RET_FAILURE
;
224 * command help (long version)
226 for (uint8_t i
= 1; i
< argc
; ++i
) {
227 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
)) != NULL
) {
230 printf_P(PSTR("Unknown command '%s' - try 'help'"
231 " without arguments.\n\n"), argv
[i
]
233 return CMD_RET_FAILURE
;
237 return CMD_RET_SUCCESS
;
241 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
243 print_usage_line(cmdtp
, 0);
244 #ifdef CONFIG_SYS_LONGHELP
245 my_puts_P(PSTR("Usage:\n"));
246 print_name_prefix(cmdtp
);
247 my_puts_P(cmdtp
->name
);
248 my_puts_P(PSTR(" "));
250 if (cmdtp
->help
&& *cmdtp
->help
!= '\0')
253 my_puts_P(PSTR(" - No additional help available.\n"));
254 #endif /* CONFIG_SYS_LONGHELP */
255 return CMD_RET_FAILURE
;
258 #ifdef CONFIG_AUTO_COMPLETE
260 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
262 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
265 space
= last_char
== '\0' || isblank(last_char
);
267 if (space
&& argc
== 1)
268 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
270 if (!space
&& argc
== 2)
271 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
276 /*************************************************************************************/
278 /* TODO: cmdtp points to FLASH */
280 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
282 cmd_tbl_t
*cmdtp
= cmd_tbl
;
283 // const int count = ARRAY_SIZE(cmd_tbl);
284 // const cmd_tbl_t *cmdend = cmdtp + count;
297 /* output full list of commands */
298 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
299 if (n_found
>= maxv
- 2) {
300 cmdv
[n_found
++] = "...";
303 cmdv
[n_found
++] = cmdtp
->name
;
305 cmdv
[n_found
] = NULL
;
309 /* more than one arg or one but the start of the next */
310 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
311 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
312 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
316 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
323 /* return the partial matches */
324 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
326 clen
= strlen(cmdtp
->name
);
330 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
334 if (n_found
>= maxv
- 2) {
335 cmdv
[n_found
++] = "...";
339 cmdv
[n_found
++] = cmdtp
->name
;
342 cmdv
[n_found
] = NULL
;
346 static int make_argv(char *s
, int argvsz
, char *argv
[])
350 /* split into argv */
351 while (argc
< argvsz
- 1) {
353 /* skip any white space */
357 if (*s
== '\0') /* end of s, no more args */
360 argv
[argc
++] = s
; /* begin of argument string */
362 /* find end of string */
363 while (*s
&& !isblank(*s
))
366 if (*s
== '\0') /* end of s, no more args */
369 *s
++ = '\0'; /* terminate current arg */
376 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
378 int ll
= leader
!= NULL
? strlen(leader
) : 0;
379 int sl
= sep
!= NULL
? strlen(sep
) : 0;
383 my_puts_P(PSTR("\n"));
387 i
= linemax
; /* force leader and newline */
388 while (*argv
!= NULL
) {
389 len
= strlen(*argv
) + sl
;
390 if (i
+ len
>= linemax
) {
391 my_puts_P(PSTR("\n"));
400 my_puts_P(PSTR("\n"));
403 static int find_common_prefix(char * const argv
[])
406 char *anchor
, *s
, *t
;
413 len
= strlen(anchor
);
414 while ((t
= *argv
++) != NULL
) {
416 for (i
= 0; i
< len
; i
++, t
++, s
++) {
425 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
428 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
430 int n
= *np
, col
= *colp
;
431 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
435 int i
, j
, k
, len
, seplen
, argc
;
439 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
440 return 0; /* not in normal console */
444 last_char
= buf
[cnt
- 1];
448 /* copy to secondary buffer which will be affected */
449 strcpy(tmp_buf
, buf
);
451 /* separate into argv */
452 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
454 /* do the completion and return the possible completions */
455 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
457 /* no match; bell and out */
459 if (argc
> 1) /* allow tab for non command */
469 if (i
== 1) { /* one match; perfect */
470 k
= strlen(argv
[argc
- 1]);
475 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
476 k
= strlen(argv
[argc
- 1]);
486 /* make sure it fits */
487 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
493 for (i
= 0; i
< len
; i
++)
496 for (i
= 0; i
< seplen
; i
++)
507 print_argv(NULL
, " ", " ", 78, cmdv
);
515 #endif /* CONFIG_AUTO_COMPLETE */
520 * Call a command function. This should be the only route in U-Boot to call
521 * a command, so that we can track whether we are waiting for input or
522 * executing a command.
524 * @param cmdtp Pointer to the command to execute
525 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
526 * @param argc Number of arguments (arg 0 must be the command text)
527 * @param argv Arguments
528 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
530 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
532 command_ret_t result
;
534 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
535 // if (result != CMD_RET_SUCCESS)
536 // debug("Command failed, result=%d\n", result);
540 #pragma GCC diagnostic ignored "-Wclobbered"
542 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
543 uint_fast8_t *repeatable
)
545 command_ret_t rc
= CMD_RET_SUCCESS
;
548 /* Look up command in command table */
549 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
551 /* Check if this command has subcommands */
552 if (cmdtp
->subcmd
&& argc
> 1) {
554 /* Look up subcommand in subcommand table */
555 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
556 if (cmdtpsub
== NULL
) {
557 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
558 return CMD_RET_FAILURE
;
567 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
568 return CMD_RET_FAILURE
;
571 /* found - check max args */
572 if (argc
> cmdtp
->maxargs
)
575 #if defined(CONFIG_CMD_BOOTD)
576 /* avoid "bootd" recursion */
577 else if (cmdtp
->cmd
== do_bootd
) {
578 if (flag
& CMD_FLAG_BOOTD
) {
579 my_puts_P(PSTR("'bootd' recursion detected\n"));
580 rc
= CMD_RET_FAILURE
;
582 flag
|= CMD_FLAG_BOOTD
;
587 if (setjmp(cmd_jbuf
) != 0)
588 return CMD_RET_FAILURE
;
590 /* If OK so far, then do the command */
592 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
593 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
595 if (rc
== CMD_RET_USAGE
)
596 rc
= cmd_usage(cmdtp
);
600 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
602 char buf
[strlen_P(cmdtp
->name
) + 1];
603 strcpy_P(buf
, cmdtp
->name
);
606 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);