]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
2 * (C) Copyright 2014, 2016 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
22 #include "print-utils.h"
23 #include "con-utils.h"
31 static void print_usage_line(const FLASH
char *name
, int width
,
32 const FLASH
char *usage
)
34 width
-= strlen_P(name
);
39 my_puts_P(PSTR(" - "));
41 my_puts_P(PSTR("\n"));
44 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
48 while ((c1
= *(const FLASH
unsigned char *)s1
++)
49 == (c2
= *(const FLASH
unsigned char *)s2
++))
56 int cmpstring_PP(const void *p1
, const void *p2
)
58 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
59 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
62 /****************************************************************************/
64 int cmd_tbl_item_count(cmd_tbl_t
*p
)
68 while (p
->name
!= NULL
) {
75 cmd_tbl_t
*get_cmd_tbl_base(cmd_tbl_t
*cmdtp
)
79 while (p
->name
!= NULL
)
86 * find command table entry for a command
94 cmd_tbl_t
*_find_cmd (const char *cmd
, cmd_tbl_t
*table
, find_cmd_para_t
*para
)
96 cmd_tbl_t
*cmdtp_ret
= NULL
;
97 uint_fast8_t n_found
= 0, sub_found
= 0;
99 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
100 if (strncmp_P(cmd
, cmdtp
->name
, para
->len
) == 0 &&
101 (para
->opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
102 if (para
->len
== strlen_P(cmdtp
->name
))
103 return cmdtp
; /* full match */
105 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
107 } else if (cmdtp
->subcmd
&& cmdtp
->flags
& CTBL_SUBCMDAUTO
) {
108 cmd_tbl_t
*sub
= _find_cmd(cmd
, cmdtp
->subcmd
, para
);
115 if (n_found
== 1) { /* exactly one match */
121 return NULL
; /* not found or ambiguous command */
125 cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
, uint_fast8_t *cmdlevel
)
127 find_cmd_para_t para
;
132 char *optenv
= getenv_str(PSTR("cmd"));
134 para
.opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
135 para
.len
= strlen(cmd
);
137 cmd_tbl_t
*cmdtp
= _find_cmd(cmd
, table
, ¶
);
140 *cmdlevel
= para
.level
;
144 cmd_tbl_t
*find_cmd_sub(const char *cmd
, cmd_tbl_t
*table
)
146 cmd_tbl_t
*entry
= NULL
;
148 for (cmd_tbl_t
*tp
= get_cmd_tbl_base(table
); tp
->name
&& entry
== NULL
; tp
++)
149 if (tp
->subcmd
&& tp
->flags
& CTBL_SUBCMDAUTO
)
150 entry
= find_cmd(cmd
, tp
->subcmd
, NULL
);
156 * Use puts() instead of printf() to avoid printf buffer overflow
157 * for long help messages
160 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
162 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
164 char *optenv
= getenv_str(PSTR("cmd"));
165 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
167 if (argc
== 1) { /*show list of commands */
168 int cmd_items
= cmd_tbl_item_count(tbl_start
);
169 cmd_tbl_t
**cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
170 uint_fast8_t maxlen_cmd
= 0;
172 /* Make array of commands */
173 cmd_tbl_t
*tp
= tbl_start
;
174 for (int i
= 0; i
< cmd_items
; i
++) {
175 uint_fast8_t l
= strlen_P(tp
->name
);
180 /* Sort command list */
181 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
183 /* print short help (usage) */
184 for (int i
= 0; i
< cmd_items
; i
++) {
185 if (opt_debug
|| !(cmd_list
[i
]->flags
& CTBL_DBG
)) {
186 const FLASH
char *usage
= cmd_list
[i
]->usage
;
188 /* allow user abort */
191 return CMD_RET_FAILURE
;
195 #if defined(GCC_BUG_61443) || 1
196 print_usage_line(cmd_list
[i
]->name
, maxlen_cmd
, usage
);
198 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
199 cmd_list
[i
]->name
, usage
);
204 return CMD_RET_SUCCESS
;
208 * command help (long version)
210 for (uint8_t i
= 1; i
< argc
; ++i
) {
211 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
, NULL
)) != NULL
&&
212 (opt_debug
|| cmdtp
->name
[0] != '!')) {
215 printf_P(PSTR("Unknown command '%s' - try 'help'"
216 " without arguments.\n\n"), argv
[i
]
218 return CMD_RET_FAILURE
;
222 return CMD_RET_SUCCESS
;
226 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
228 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
229 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
231 my_puts_P(cmdtp
->name
);
232 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
233 my_puts_P(PSTR(" - "));
234 my_puts_P(cmdtp
->usage
);
235 my_puts_P(PSTR("\n\n"));
237 #ifdef CONFIG_SYS_LONGHELP
238 // printf("Usage:\n%s ", cmdtp->name);
239 my_puts_P(PSTR("Usage:\n"));
240 my_puts_P(cmdtp
->name
);
241 my_puts_P(PSTR(" "));
244 my_puts_P(PSTR(" - No additional help available.\n"));
245 return CMD_RET_FAILURE
;
248 my_puts_P(cmdtp
->help
);
249 my_puts_P(PSTR("\n"));
250 #endif /* CONFIG_SYS_LONGHELP */
251 return CMD_RET_FAILURE
;
254 #ifdef CONFIG_AUTO_COMPLETE
256 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
258 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
261 space
= last_char
== '\0' || isblank(last_char
);
263 if (space
&& argc
== 1)
264 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
266 if (!space
&& argc
== 2)
267 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
272 /*************************************************************************************/
274 /* TODO: cmdtp points to FLASH */
276 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
278 cmd_tbl_t
*cmdtp
= cmd_tbl
;
279 // const int count = ARRAY_SIZE(cmd_tbl);
280 // const cmd_tbl_t *cmdend = cmdtp + count;
293 /* output full list of commands */
294 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
295 if (n_found
>= maxv
- 2) {
296 cmdv
[n_found
++] = "...";
299 cmdv
[n_found
++] = cmdtp
->name
;
301 cmdv
[n_found
] = NULL
;
305 /* more than one arg or one but the start of the next */
306 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
307 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
308 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
312 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
319 /* return the partial matches */
320 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
322 clen
= strlen(cmdtp
->name
);
326 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
330 if (n_found
>= maxv
- 2) {
331 cmdv
[n_found
++] = "...";
335 cmdv
[n_found
++] = cmdtp
->name
;
338 cmdv
[n_found
] = NULL
;
342 static int make_argv(char *s
, int argvsz
, char *argv
[])
346 /* split into argv */
347 while (argc
< argvsz
- 1) {
349 /* skip any white space */
353 if (*s
== '\0') /* end of s, no more args */
356 argv
[argc
++] = s
; /* begin of argument string */
358 /* find end of string */
359 while (*s
&& !isblank(*s
))
362 if (*s
== '\0') /* end of s, no more args */
365 *s
++ = '\0'; /* terminate current arg */
372 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
374 int ll
= leader
!= NULL
? strlen(leader
) : 0;
375 int sl
= sep
!= NULL
? strlen(sep
) : 0;
379 my_puts_P(PSTR("\n"));
383 i
= linemax
; /* force leader and newline */
384 while (*argv
!= NULL
) {
385 len
= strlen(*argv
) + sl
;
386 if (i
+ len
>= linemax
) {
387 my_puts_P(PSTR("\n"));
396 my_puts_P(PSTR("\n"));
399 static int find_common_prefix(char * const argv
[])
402 char *anchor
, *s
, *t
;
409 len
= strlen(anchor
);
410 while ((t
= *argv
++) != NULL
) {
412 for (i
= 0; i
< len
; i
++, t
++, s
++) {
421 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
424 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
426 int n
= *np
, col
= *colp
;
427 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
431 int i
, j
, k
, len
, seplen
, argc
;
435 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
436 return 0; /* not in normal console */
440 last_char
= buf
[cnt
- 1];
444 /* copy to secondary buffer which will be affected */
445 strcpy(tmp_buf
, buf
);
447 /* separate into argv */
448 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
450 /* do the completion and return the possible completions */
451 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
453 /* no match; bell and out */
455 if (argc
> 1) /* allow tab for non command */
465 if (i
== 1) { /* one match; perfect */
466 k
= strlen(argv
[argc
- 1]);
471 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
472 k
= strlen(argv
[argc
- 1]);
482 /* make sure it fits */
483 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
489 for (i
= 0; i
< len
; i
++)
492 for (i
= 0; i
< seplen
; i
++)
503 print_argv(NULL
, " ", " ", 78, cmdv
);
511 #endif /* CONFIG_AUTO_COMPLETE */
516 * Call a command function. This should be the only route in U-Boot to call
517 * a command, so that we can track whether we are waiting for input or
518 * executing a command.
520 * @param cmdtp Pointer to the command to execute
521 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
522 * @param argc Number of arguments (arg 0 must be the command text)
523 * @param argv Arguments
524 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
526 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
528 command_ret_t result
;
530 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
531 // if (result != CMD_RET_SUCCESS)
532 // debug("Command failed, result=%d\n", result);
536 #pragma GCC diagnostic ignored "-Wclobbered"
538 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
539 uint_fast8_t *repeatable
)
541 command_ret_t rc
= CMD_RET_SUCCESS
;
543 uint_fast8_t cmdlevel
;
545 /* Look up command in command table */
546 cmdtp
= find_cmd(argv
[0], cmd_tbl
, &cmdlevel
);
548 /* Check if this command has subcommands */
549 if (cmdtp
->subcmd
&& argc
> 1) {
551 /* Look up subcommand in subcommand table */
552 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
, &cmdlevel
);
553 if (cmdtpsub
== NULL
) {
554 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
555 return CMD_RET_FAILURE
;
564 /* Search subcommands */
565 cmdtp
= find_cmd_sub(argv
[0], cmd_tbl
, &cmdlevel
);
570 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
571 return CMD_RET_FAILURE
;
574 /* found - check max args */
575 if (argc
> cmdtp
->maxargs
)
578 #if defined(CONFIG_CMD_BOOTD)
579 /* avoid "bootd" recursion */
580 else if (cmdtp
->cmd
== do_bootd
) {
581 if (flag
& CMD_FLAG_BOOTD
) {
582 my_puts_P(PSTR("'bootd' recursion detected\n"));
583 rc
= CMD_RET_FAILURE
;
585 flag
|= CMD_FLAG_BOOTD
;
590 if (setjmp(cmd_jbuf
) != 0)
591 return CMD_RET_FAILURE
;
593 /* If OK so far, then do the command */
595 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
596 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
598 if (rc
== CMD_RET_USAGE
)
599 rc
= cmd_usage(cmdtp
);
603 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
605 char buf
[strlen_P(cmdtp
->name
) + 1];
606 strcpy_P(buf
, cmdtp
->name
);
609 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);