]>
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_array
[cmd_items
];
170 cmd_tbl_t
*tp
= tbl_start
;
171 uint_fast8_t max_len
= 0;
173 /* Make array of commands from .uboot_cmd section */
174 for (int i
= 0; i
< cmd_items
; i
++) {
176 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
181 /* Sort command list */
182 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
184 /* print short help (usage) */
185 for (int i
= 0; i
< cmd_items
; i
++) {
186 if (opt_debug
|| !(cmd_array
[i
]->flags
& CTBL_DBG
)) {
187 const FLASH
char *usage
= cmd_array
[i
]->usage
;
189 /* allow user abort */
191 return CMD_RET_FAILURE
;
194 #if defined(GCC_BUG_61443) || 1
195 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
197 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
198 cmd_array
[i
]->name
, usage
);
202 return CMD_RET_SUCCESS
;
206 * command help (long version)
208 for (uint8_t i
= 1; i
< argc
; ++i
) {
209 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
, NULL
)) != NULL
&&
210 (opt_debug
|| cmdtp
->name
[0] != '!')) {
213 printf_P(PSTR("Unknown command '%s' - try 'help'"
214 " without arguments.\n\n"), argv
[i
]
216 return CMD_RET_FAILURE
;
220 return CMD_RET_SUCCESS
;
224 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
226 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
227 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
229 my_puts_P(cmdtp
->name
);
230 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
231 my_puts_P(PSTR(" - "));
232 my_puts_P(cmdtp
->usage
);
233 my_puts_P(PSTR("\n\n"));
235 #ifdef CONFIG_SYS_LONGHELP
236 // printf("Usage:\n%s ", cmdtp->name);
237 my_puts_P(PSTR("Usage:\n"));
238 my_puts_P(cmdtp
->name
);
239 my_puts_P(PSTR(" "));
242 my_puts_P(PSTR(" - No additional help available.\n"));
243 return CMD_RET_FAILURE
;
246 my_puts_P(cmdtp
->help
);
247 my_puts_P(PSTR("\n"));
248 #endif /* CONFIG_SYS_LONGHELP */
249 return CMD_RET_FAILURE
;
252 #ifdef CONFIG_AUTO_COMPLETE
254 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
256 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
259 space
= last_char
== '\0' || isblank(last_char
);
261 if (space
&& argc
== 1)
262 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
264 if (!space
&& argc
== 2)
265 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
270 /*************************************************************************************/
272 /* TODO: cmdtp points to FLASH */
274 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
276 cmd_tbl_t
*cmdtp
= cmd_tbl
;
277 // const int count = ARRAY_SIZE(cmd_tbl);
278 // const cmd_tbl_t *cmdend = cmdtp + count;
291 /* output full list of commands */
292 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
293 if (n_found
>= maxv
- 2) {
294 cmdv
[n_found
++] = "...";
297 cmdv
[n_found
++] = cmdtp
->name
;
299 cmdv
[n_found
] = NULL
;
303 /* more than one arg or one but the start of the next */
304 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
305 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
306 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
310 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
317 /* return the partial matches */
318 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
320 clen
= strlen(cmdtp
->name
);
324 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
328 if (n_found
>= maxv
- 2) {
329 cmdv
[n_found
++] = "...";
333 cmdv
[n_found
++] = cmdtp
->name
;
336 cmdv
[n_found
] = NULL
;
340 static int make_argv(char *s
, int argvsz
, char *argv
[])
344 /* split into argv */
345 while (argc
< argvsz
- 1) {
347 /* skip any white space */
351 if (*s
== '\0') /* end of s, no more args */
354 argv
[argc
++] = s
; /* begin of argument string */
356 /* find end of string */
357 while (*s
&& !isblank(*s
))
360 if (*s
== '\0') /* end of s, no more args */
363 *s
++ = '\0'; /* terminate current arg */
370 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
372 int ll
= leader
!= NULL
? strlen(leader
) : 0;
373 int sl
= sep
!= NULL
? strlen(sep
) : 0;
377 my_puts_P(PSTR("\n"));
381 i
= linemax
; /* force leader and newline */
382 while (*argv
!= NULL
) {
383 len
= strlen(*argv
) + sl
;
384 if (i
+ len
>= linemax
) {
385 my_puts_P(PSTR("\n"));
394 my_puts_P(PSTR("\n"));
397 static int find_common_prefix(char * const argv
[])
400 char *anchor
, *s
, *t
;
407 len
= strlen(anchor
);
408 while ((t
= *argv
++) != NULL
) {
410 for (i
= 0; i
< len
; i
++, t
++, s
++) {
419 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
422 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
424 int n
= *np
, col
= *colp
;
425 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
429 int i
, j
, k
, len
, seplen
, argc
;
433 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
434 return 0; /* not in normal console */
438 last_char
= buf
[cnt
- 1];
442 /* copy to secondary buffer which will be affected */
443 strcpy(tmp_buf
, buf
);
445 /* separate into argv */
446 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
448 /* do the completion and return the possible completions */
449 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
451 /* no match; bell and out */
453 if (argc
> 1) /* allow tab for non command */
463 if (i
== 1) { /* one match; perfect */
464 k
= strlen(argv
[argc
- 1]);
469 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
470 k
= strlen(argv
[argc
- 1]);
480 /* make sure it fits */
481 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
487 for (i
= 0; i
< len
; i
++)
490 for (i
= 0; i
< seplen
; i
++)
501 print_argv(NULL
, " ", " ", 78, cmdv
);
509 #endif /* CONFIG_AUTO_COMPLETE */
514 * Call a command function. This should be the only route in U-Boot to call
515 * a command, so that we can track whether we are waiting for input or
516 * executing a command.
518 * @param cmdtp Pointer to the command to execute
519 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
520 * @param argc Number of arguments (arg 0 must be the command text)
521 * @param argv Arguments
522 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
524 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
526 command_ret_t result
;
528 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
529 // if (result != CMD_RET_SUCCESS)
530 // debug("Command failed, result=%d\n", result);
534 #pragma GCC diagnostic ignored "-Wclobbered"
536 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
537 uint_fast8_t *repeatable
)
539 command_ret_t rc
= CMD_RET_SUCCESS
;
541 uint_fast8_t cmdlevel
;
543 /* Look up command in command table */
544 cmdtp
= find_cmd(argv
[0], cmd_tbl
, &cmdlevel
);
546 /* Check if this command has subcommands */
547 if (cmdtp
->subcmd
&& argc
> 1) {
549 /* Look up subcommand in subcommand table */
550 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
, &cmdlevel
);
551 if (cmdtpsub
== NULL
) {
552 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
553 return CMD_RET_FAILURE
;
562 /* Search subcommands */
563 cmdtp
= find_cmd_sub(argv
[0], cmd_tbl
, &cmdlevel
);
568 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
569 return CMD_RET_FAILURE
;
572 /* found - check max args */
573 if (argc
> cmdtp
->maxargs
)
576 #if defined(CONFIG_CMD_BOOTD)
577 /* avoid "bootd" recursion */
578 else if (cmdtp
->cmd
== do_bootd
) {
579 if (flag
& CMD_FLAG_BOOTD
) {
580 my_puts_P(PSTR("'bootd' recursion detected\n"));
581 rc
= CMD_RET_FAILURE
;
583 flag
|= CMD_FLAG_BOOTD
;
588 if (setjmp(cmd_jbuf
) != 0)
589 return CMD_RET_FAILURE
;
591 /* If OK so far, then do the command */
593 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
594 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
596 if (rc
== CMD_RET_USAGE
)
597 rc
= cmd_usage(cmdtp
);
601 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
603 char buf
[strlen_P(cmdtp
->name
) + 1];
604 strcpy_P(buf
, cmdtp
->name
);
607 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);