]>
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 void print_usage_line(const FLASH
char *name
, int width
,
35 const FLASH
char *usage
)
37 width
-= strlen_P(name
);
42 my_puts_P(PSTR(" - "));
44 my_puts_P(PSTR("\n"));
47 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
51 while ((c1
= *(const FLASH
unsigned char *)s1
++)
52 == (c2
= *(const FLASH
unsigned char *)s2
++))
59 int cmpstring_PP(const void *p1
, const void *p2
)
61 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
62 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
65 /****************************************************************************/
67 int cmd_tbl_item_count(cmd_tbl_t
*p
)
71 while (p
->name
!= NULL
) {
73 cmd_tbl_t
*sub
= p
->subcmd
;
74 while (sub
->name
!= NULL
) {
75 if (sub
->flags
& CTBL_SUBCMDAUTO
)
80 if ((p
->flags
& CTBL_SUBCMDAUTO
) == 0)
88 cmd_tbl_t
*get_cmd_tbl_base(cmd_tbl_t
*cmdtp
)
92 while (p
->name
!= NULL
)
99 * find command table entry for a command
107 cmd_tbl_t
*_find_cmd (const char *cmd
, cmd_tbl_t
*table
, find_cmd_para_t
*para
)
109 cmd_tbl_t
*cmdtp_ret
= NULL
;
110 uint_fast8_t n_found
= 0, sub_found
= 0;
112 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
113 if (strncmp_P(cmd
, cmdtp
->name
, para
->len
) == 0 &&
114 (para
->opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
115 if (para
->len
== strlen_P(cmdtp
->name
))
116 return cmdtp
; /* full match */
118 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
120 } else if (cmdtp
->subcmd
) {
121 cmd_tbl_t
*sub
= _find_cmd(cmd
, cmdtp
->subcmd
, para
);
122 if (sub
&& sub
->flags
& CTBL_SUBCMDAUTO
) {
128 if (n_found
== 1) { /* exactly one match */
134 return NULL
; /* not found or ambiguous command */
138 cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
, uint_fast8_t *cmdlevel
)
140 find_cmd_para_t para
;
145 char *optenv
= getenv_str(PSTR("cmd"));
147 para
.opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
148 para
.len
= strlen(cmd
);
150 cmd_tbl_t
*cmdtp
= _find_cmd(cmd
, table
, ¶
);
153 *cmdlevel
= para
.level
;
157 cmd_tbl_t
*find_cmd_sub(const char *cmd
, cmd_tbl_t
*table
)
159 cmd_tbl_t
*entry
= NULL
;
161 for (cmd_tbl_t
*tp
= get_cmd_tbl_base(table
); tp
->name
&& entry
== NULL
; tp
++)
162 if (tp
->subcmd
&& tp
->flags
& CTBL_SUBCMDAUTO
)
163 entry
= find_cmd(cmd
, tp
->subcmd
, NULL
);
169 * Use puts() instead of printf() to avoid printf buffer overflow
170 * for long help messages
173 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
175 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
177 char *optenv
= getenv_str(PSTR("cmd"));
178 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
180 if (argc
== 1) { /*show list of commands */
181 int cmd_items
= cmd_tbl_item_count(tbl_start
);
182 cmd_tbl_t
**cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
183 uint_fast8_t maxlen_cmd
= 0;
185 /* Make array of commands */
186 cmd_tbl_t
*tp
= tbl_start
;
188 while (tp
->name
!= NULL
) {
190 cmd_tbl_t
*sub
= tp
->subcmd
;
191 while (sub
->name
!= NULL
) {
192 if (sub
->flags
& CTBL_SUBCMDAUTO
) {
193 uint_fast8_t len
= strlen_P(sub
->name
);
194 if (len
> maxlen_cmd
)
201 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
202 uint_fast8_t len
= strlen_P(tp
->name
);
203 if (len
> maxlen_cmd
)
209 /* Sort command list */
210 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
212 /* print short help (usage) */
213 for (int i
= 0; i
< cmd_items
; i
++) {
214 if (opt_debug
|| !(cmd_list
[i
]->flags
& CTBL_DBG
)) {
215 const FLASH
char *usage
= cmd_list
[i
]->usage
;
217 /* allow user abort */
220 return CMD_RET_FAILURE
;
224 #if defined(GCC_BUG_61443) || 1
225 print_usage_line(cmd_list
[i
]->name
, maxlen_cmd
, usage
);
227 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
228 cmd_list
[i
]->name
, usage
);
233 return CMD_RET_SUCCESS
;
237 * command help (long version)
239 for (uint8_t i
= 1; i
< argc
; ++i
) {
240 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
, NULL
)) != NULL
&&
241 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
244 printf_P(PSTR("Unknown command '%s' - try 'help'"
245 " without arguments.\n\n"), argv
[i
]
247 return CMD_RET_FAILURE
;
251 return CMD_RET_SUCCESS
;
255 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
257 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
258 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
260 my_puts_P(cmdtp
->name
);
261 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
262 my_puts_P(PSTR(" - "));
263 my_puts_P(cmdtp
->usage
);
264 my_puts_P(PSTR("\n\n"));
266 #ifdef CONFIG_SYS_LONGHELP
267 // printf("Usage:\n%s ", cmdtp->name);
268 my_puts_P(PSTR("Usage:\n"));
269 my_puts_P(cmdtp
->name
);
270 my_puts_P(PSTR(" "));
273 my_puts_P(PSTR(" - No additional help available.\n"));
274 return CMD_RET_FAILURE
;
277 my_puts_P(cmdtp
->help
);
278 my_puts_P(PSTR("\n"));
279 #endif /* CONFIG_SYS_LONGHELP */
280 return CMD_RET_FAILURE
;
283 #ifdef CONFIG_AUTO_COMPLETE
285 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
287 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
290 space
= last_char
== '\0' || isblank(last_char
);
292 if (space
&& argc
== 1)
293 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
295 if (!space
&& argc
== 2)
296 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
301 /*************************************************************************************/
303 /* TODO: cmdtp points to FLASH */
305 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
307 cmd_tbl_t
*cmdtp
= cmd_tbl
;
308 // const int count = ARRAY_SIZE(cmd_tbl);
309 // const cmd_tbl_t *cmdend = cmdtp + count;
322 /* output full list of commands */
323 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
324 if (n_found
>= maxv
- 2) {
325 cmdv
[n_found
++] = "...";
328 cmdv
[n_found
++] = cmdtp
->name
;
330 cmdv
[n_found
] = NULL
;
334 /* more than one arg or one but the start of the next */
335 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
336 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
337 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
341 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
348 /* return the partial matches */
349 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
351 clen
= strlen(cmdtp
->name
);
355 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
359 if (n_found
>= maxv
- 2) {
360 cmdv
[n_found
++] = "...";
364 cmdv
[n_found
++] = cmdtp
->name
;
367 cmdv
[n_found
] = NULL
;
371 static int make_argv(char *s
, int argvsz
, char *argv
[])
375 /* split into argv */
376 while (argc
< argvsz
- 1) {
378 /* skip any white space */
382 if (*s
== '\0') /* end of s, no more args */
385 argv
[argc
++] = s
; /* begin of argument string */
387 /* find end of string */
388 while (*s
&& !isblank(*s
))
391 if (*s
== '\0') /* end of s, no more args */
394 *s
++ = '\0'; /* terminate current arg */
401 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
403 int ll
= leader
!= NULL
? strlen(leader
) : 0;
404 int sl
= sep
!= NULL
? strlen(sep
) : 0;
408 my_puts_P(PSTR("\n"));
412 i
= linemax
; /* force leader and newline */
413 while (*argv
!= NULL
) {
414 len
= strlen(*argv
) + sl
;
415 if (i
+ len
>= linemax
) {
416 my_puts_P(PSTR("\n"));
425 my_puts_P(PSTR("\n"));
428 static int find_common_prefix(char * const argv
[])
431 char *anchor
, *s
, *t
;
438 len
= strlen(anchor
);
439 while ((t
= *argv
++) != NULL
) {
441 for (i
= 0; i
< len
; i
++, t
++, s
++) {
450 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
453 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
455 int n
= *np
, col
= *colp
;
456 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
460 int i
, j
, k
, len
, seplen
, argc
;
464 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
465 return 0; /* not in normal console */
469 last_char
= buf
[cnt
- 1];
473 /* copy to secondary buffer which will be affected */
474 strcpy(tmp_buf
, buf
);
476 /* separate into argv */
477 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
479 /* do the completion and return the possible completions */
480 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
482 /* no match; bell and out */
484 if (argc
> 1) /* allow tab for non command */
494 if (i
== 1) { /* one match; perfect */
495 k
= strlen(argv
[argc
- 1]);
500 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
501 k
= strlen(argv
[argc
- 1]);
511 /* make sure it fits */
512 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
518 for (i
= 0; i
< len
; i
++)
521 for (i
= 0; i
< seplen
; i
++)
532 print_argv(NULL
, " ", " ", 78, cmdv
);
540 #endif /* CONFIG_AUTO_COMPLETE */
545 * Call a command function. This should be the only route in U-Boot to call
546 * a command, so that we can track whether we are waiting for input or
547 * executing a command.
549 * @param cmdtp Pointer to the command to execute
550 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
551 * @param argc Number of arguments (arg 0 must be the command text)
552 * @param argv Arguments
553 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
555 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
557 command_ret_t result
;
559 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
560 // if (result != CMD_RET_SUCCESS)
561 // debug("Command failed, result=%d\n", result);
565 #pragma GCC diagnostic ignored "-Wclobbered"
567 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
568 uint_fast8_t *repeatable
)
570 command_ret_t rc
= CMD_RET_SUCCESS
;
572 uint_fast8_t cmdlevel
;
574 /* Look up command in command table */
575 cmdtp
= find_cmd(argv
[0], cmd_tbl
, &cmdlevel
);
577 /* Check if this command has subcommands */
578 if (cmdtp
->subcmd
&& argc
> 1) {
580 /* Look up subcommand in subcommand table */
581 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
, &cmdlevel
);
582 if (cmdtpsub
== NULL
) {
583 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
584 return CMD_RET_FAILURE
;
593 /* Search subcommands */
594 cmdtp
= find_cmd_sub(argv
[0], cmd_tbl
, &cmdlevel
);
599 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
600 return CMD_RET_FAILURE
;
603 /* found - check max args */
604 if (argc
> cmdtp
->maxargs
)
607 #if defined(CONFIG_CMD_BOOTD)
608 /* avoid "bootd" recursion */
609 else if (cmdtp
->cmd
== do_bootd
) {
610 if (flag
& CMD_FLAG_BOOTD
) {
611 my_puts_P(PSTR("'bootd' recursion detected\n"));
612 rc
= CMD_RET_FAILURE
;
614 flag
|= CMD_FLAG_BOOTD
;
619 if (setjmp(cmd_jbuf
) != 0)
620 return CMD_RET_FAILURE
;
622 /* If OK so far, then do the command */
624 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
625 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
627 if (rc
== CMD_RET_USAGE
)
628 rc
= cmd_usage(cmdtp
);
632 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
634 char buf
[strlen_P(cmdtp
->name
) + 1];
635 strcpy_P(buf
, cmdtp
->name
);
638 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);