]>
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
102 cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
107 char *optenv
= getenv_str(PSTR("cmd"));
108 uint8_t opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
110 cmd_tbl_t
*cmdtp_ret
= NULL
;
111 uint_fast8_t n_found
= 0;
112 uint_fast8_t len
= strlen(cmd
);
114 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
116 for (cmd_tbl_t
*sub
= cmdtp
->subcmd
; sub
->name
!= NULL
; sub
++) {
117 if (sub
->flags
& CTBL_SUBCMDAUTO
&&
118 strncmp_P(cmd
, sub
->name
, len
) == 0 &&
119 (opt_debug
|| !(sub
->flags
& CTBL_DBG
))) {
120 if (len
== strlen_P(sub
->name
))
121 return sub
; /* full match */
122 cmdtp_ret
= sub
; /* abbreviated command ? */
127 if ((cmdtp
->flags
& CTBL_SUBCMDAUTO
) == 0 &&
128 strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
129 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
130 if (len
== strlen_P(cmdtp
->name
))
131 return cmdtp
; /* full match */
132 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
138 return cmdtp_ret
; /* exactly one match */
140 return NULL
; /* not found or ambiguous command */
144 * Use puts() instead of printf() to avoid printf buffer overflow
145 * for long help messages
148 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
150 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
152 char *optenv
= getenv_str(PSTR("cmd"));
153 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
155 if (argc
== 1) { /*show list of commands */
156 int cmd_items
= cmd_tbl_item_count(tbl_start
);
157 cmd_tbl_t
**cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
158 uint_fast8_t maxlen_cmd
= 0;
160 /* Make array of commands */
161 cmd_tbl_t
*tp
= tbl_start
;
163 while (tp
->name
!= NULL
) {
165 cmd_tbl_t
*sub
= tp
->subcmd
;
166 while (sub
->name
!= NULL
) {
167 if (sub
->flags
& CTBL_SUBCMDAUTO
) {
168 uint_fast8_t len
= strlen_P(sub
->name
);
169 if (len
> maxlen_cmd
)
176 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
177 uint_fast8_t len
= strlen_P(tp
->name
);
178 if (len
> maxlen_cmd
)
184 /* Sort command list */
185 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
187 /* print short help (usage) */
188 for (int i
= 0; i
< cmd_items
; i
++) {
189 if (opt_debug
|| !(cmd_list
[i
]->flags
& CTBL_DBG
)) {
190 const FLASH
char *usage
= cmd_list
[i
]->usage
;
192 /* allow user abort */
195 return CMD_RET_FAILURE
;
199 #if defined(GCC_BUG_61443) || 1
200 print_usage_line(cmd_list
[i
]->name
, maxlen_cmd
, usage
);
202 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
203 cmd_list
[i
]->name
, usage
);
208 return CMD_RET_SUCCESS
;
212 * command help (long version)
214 for (uint8_t i
= 1; i
< argc
; ++i
) {
215 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
)) != NULL
) {
218 printf_P(PSTR("Unknown command '%s' - try 'help'"
219 " without arguments.\n\n"), argv
[i
]
221 return CMD_RET_FAILURE
;
225 return CMD_RET_SUCCESS
;
229 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
231 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
232 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
234 my_puts_P(cmdtp
->name
);
235 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
236 my_puts_P(PSTR(" - "));
237 my_puts_P(cmdtp
->usage
);
238 my_puts_P(PSTR("\n\n"));
240 #ifdef CONFIG_SYS_LONGHELP
241 // printf("Usage:\n%s ", cmdtp->name);
242 my_puts_P(PSTR("Usage:\n"));
243 my_puts_P(cmdtp
->name
);
244 my_puts_P(PSTR(" "));
247 my_puts_P(PSTR(" - No additional help available.\n"));
248 return CMD_RET_FAILURE
;
251 my_puts_P(cmdtp
->help
);
252 my_puts_P(PSTR("\n"));
253 #endif /* CONFIG_SYS_LONGHELP */
254 return CMD_RET_FAILURE
;
257 #ifdef CONFIG_AUTO_COMPLETE
259 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
261 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
264 space
= last_char
== '\0' || isblank(last_char
);
266 if (space
&& argc
== 1)
267 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
269 if (!space
&& argc
== 2)
270 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
275 /*************************************************************************************/
277 /* TODO: cmdtp points to FLASH */
279 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
281 cmd_tbl_t
*cmdtp
= cmd_tbl
;
282 // const int count = ARRAY_SIZE(cmd_tbl);
283 // const cmd_tbl_t *cmdend = cmdtp + count;
296 /* output full list of commands */
297 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
298 if (n_found
>= maxv
- 2) {
299 cmdv
[n_found
++] = "...";
302 cmdv
[n_found
++] = cmdtp
->name
;
304 cmdv
[n_found
] = NULL
;
308 /* more than one arg or one but the start of the next */
309 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
310 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
311 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
315 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
322 /* return the partial matches */
323 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
325 clen
= strlen(cmdtp
->name
);
329 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
333 if (n_found
>= maxv
- 2) {
334 cmdv
[n_found
++] = "...";
338 cmdv
[n_found
++] = cmdtp
->name
;
341 cmdv
[n_found
] = NULL
;
345 static int make_argv(char *s
, int argvsz
, char *argv
[])
349 /* split into argv */
350 while (argc
< argvsz
- 1) {
352 /* skip any white space */
356 if (*s
== '\0') /* end of s, no more args */
359 argv
[argc
++] = s
; /* begin of argument string */
361 /* find end of string */
362 while (*s
&& !isblank(*s
))
365 if (*s
== '\0') /* end of s, no more args */
368 *s
++ = '\0'; /* terminate current arg */
375 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
377 int ll
= leader
!= NULL
? strlen(leader
) : 0;
378 int sl
= sep
!= NULL
? strlen(sep
) : 0;
382 my_puts_P(PSTR("\n"));
386 i
= linemax
; /* force leader and newline */
387 while (*argv
!= NULL
) {
388 len
= strlen(*argv
) + sl
;
389 if (i
+ len
>= linemax
) {
390 my_puts_P(PSTR("\n"));
399 my_puts_P(PSTR("\n"));
402 static int find_common_prefix(char * const argv
[])
405 char *anchor
, *s
, *t
;
412 len
= strlen(anchor
);
413 while ((t
= *argv
++) != NULL
) {
415 for (i
= 0; i
< len
; i
++, t
++, s
++) {
424 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
427 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
429 int n
= *np
, col
= *colp
;
430 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
434 int i
, j
, k
, len
, seplen
, argc
;
438 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
439 return 0; /* not in normal console */
443 last_char
= buf
[cnt
- 1];
447 /* copy to secondary buffer which will be affected */
448 strcpy(tmp_buf
, buf
);
450 /* separate into argv */
451 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
453 /* do the completion and return the possible completions */
454 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
456 /* no match; bell and out */
458 if (argc
> 1) /* allow tab for non command */
468 if (i
== 1) { /* one match; perfect */
469 k
= strlen(argv
[argc
- 1]);
474 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
475 k
= strlen(argv
[argc
- 1]);
485 /* make sure it fits */
486 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
492 for (i
= 0; i
< len
; i
++)
495 for (i
= 0; i
< seplen
; i
++)
506 print_argv(NULL
, " ", " ", 78, cmdv
);
514 #endif /* CONFIG_AUTO_COMPLETE */
519 * Call a command function. This should be the only route in U-Boot to call
520 * a command, so that we can track whether we are waiting for input or
521 * executing a command.
523 * @param cmdtp Pointer to the command to execute
524 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
525 * @param argc Number of arguments (arg 0 must be the command text)
526 * @param argv Arguments
527 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
529 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
531 command_ret_t result
;
533 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
534 // if (result != CMD_RET_SUCCESS)
535 // debug("Command failed, result=%d\n", result);
539 #pragma GCC diagnostic ignored "-Wclobbered"
541 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
542 uint_fast8_t *repeatable
)
544 command_ret_t rc
= CMD_RET_SUCCESS
;
547 /* Look up command in command table */
548 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
550 /* Check if this command has subcommands */
551 if (cmdtp
->subcmd
&& argc
> 1) {
553 /* Look up subcommand in subcommand table */
554 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
555 if (cmdtpsub
== NULL
) {
556 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
557 return CMD_RET_FAILURE
;
566 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
567 return CMD_RET_FAILURE
;
570 /* found - check max args */
571 if (argc
> cmdtp
->maxargs
)
574 #if defined(CONFIG_CMD_BOOTD)
575 /* avoid "bootd" recursion */
576 else if (cmdtp
->cmd
== do_bootd
) {
577 if (flag
& CMD_FLAG_BOOTD
) {
578 my_puts_P(PSTR("'bootd' recursion detected\n"));
579 rc
= CMD_RET_FAILURE
;
581 flag
|= CMD_FLAG_BOOTD
;
586 if (setjmp(cmd_jbuf
) != 0)
587 return CMD_RET_FAILURE
;
589 /* If OK so far, then do the command */
591 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
592 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
594 if (rc
== CMD_RET_USAGE
)
595 rc
= cmd_usage(cmdtp
);
599 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
601 char buf
[strlen_P(cmdtp
->name
) + 1];
602 strcpy_P(buf
, cmdtp
->name
);
605 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);