]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
b11297bb9c9d80578cdac0f327dc8478897a79f2
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(cmd_tbl_t
*p
, int width
)
36 width
-= strlen_P(p
->name
);
41 my_puts_P(PSTR(" - "));
45 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
49 while ((c1
= *(const FLASH
unsigned char *)s1
++)
50 == (c2
= *(const FLASH
unsigned char *)s2
++))
57 int cmpstring_PP(const void *p1
, const void *p2
)
59 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
60 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
63 /****************************************************************************/
65 int cmd_tbl_item_count(cmd_tbl_t
*p
)
69 while (p
->name
!= NULL
) {
71 cmd_tbl_t
*sub
= p
->subcmd
;
72 while (sub
->name
!= NULL
) {
73 if (sub
->flags
& CTBL_SUBCMDAUTO
)
78 if ((p
->flags
& CTBL_SUBCMDAUTO
) == 0)
86 cmd_tbl_t
*get_cmd_tbl_base(cmd_tbl_t
*cmdtp
)
90 while (p
->name
!= NULL
)
97 * find command table entry for a command
100 cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
105 char *optenv
= getenv_str(PSTR("cmd"));
106 uint8_t opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
108 cmd_tbl_t
*cmdtp_ret
= NULL
;
109 uint_fast8_t n_found
= 0;
110 uint_fast8_t len
= strlen(cmd
);
112 for (cmd_tbl_t
*cmdtp
= table
; cmdtp
->name
!= NULL
; cmdtp
++) {
114 for (cmd_tbl_t
*sub
= cmdtp
->subcmd
; sub
->name
!= NULL
; sub
++) {
115 if (sub
->flags
& CTBL_SUBCMDAUTO
&&
116 strncmp_P(cmd
, sub
->name
, len
) == 0 &&
117 (opt_debug
|| !(sub
->flags
& CTBL_DBG
))) {
118 if (len
== strlen_P(sub
->name
))
119 return sub
; /* full match */
120 cmdtp_ret
= sub
; /* abbreviated command ? */
125 if ((cmdtp
->flags
& CTBL_SUBCMDAUTO
) == 0 &&
126 strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
127 (opt_debug
|| !(cmdtp
->flags
& CTBL_DBG
))) {
128 if (len
== strlen_P(cmdtp
->name
))
129 return cmdtp
; /* full match */
130 cmdtp_ret
= cmdtp
; /* abbreviated command ? */
136 return cmdtp_ret
; /* exactly one match */
138 return NULL
; /* not found or ambiguous command */
142 * Use puts() instead of printf() to avoid printf buffer overflow
143 * for long help messages
146 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
148 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
149 command_ret_t rc
= CMD_RET_SUCCESS
;
151 char *optenv
= getenv_str(PSTR("cmd"));
152 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
154 if (argc
== 1) { /*show list of commands */
155 int cmd_items
= cmd_tbl_item_count(tbl_start
);
156 cmd_tbl_t
**cmd_list
= (cmd_tbl_t
**) malloc(cmd_items
* sizeof(cmd_tbl_t
*));
157 uint_fast8_t maxlen_cmd
= 0;
159 /* Make array of commands */
160 cmd_tbl_t
*tp
= tbl_start
;
162 while (tp
->name
!= NULL
) {
164 cmd_tbl_t
*sub
= tp
->subcmd
;
165 while (sub
->name
!= NULL
) {
166 if (sub
->flags
& CTBL_SUBCMDAUTO
) {
167 uint_fast8_t len
= strlen_P(sub
->name
);
168 if (len
> maxlen_cmd
)
175 if ((tp
->flags
& CTBL_SUBCMDAUTO
) == 0) {
176 uint_fast8_t len
= strlen_P(tp
->name
);
177 if (len
> maxlen_cmd
)
183 /* Sort command list */
184 qsort(cmd_list
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
186 /* print short help (usage) */
187 for (int i
= 0; i
< cmd_items
; i
++) {
188 if ((cmd_list
[i
]->flags
& CTBL_DBG
) && !opt_debug
)
190 if (cmd_list
[i
]->usage
== NULL
)
193 print_usage_line(cmd_list
[i
], maxlen_cmd
);
195 /* allow user abort */
197 rc
= CMD_RET_FAILURE
;
206 * command help (long version)
208 for (uint8_t i
= 1; i
< argc
; ++i
) {
209 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
)) != NULL
) {
212 printf_P(PSTR("Unknown command '%s' - try 'help'"
213 " without arguments.\n\n"), argv
[i
]
215 return CMD_RET_FAILURE
;
219 return CMD_RET_SUCCESS
;
223 command_ret_t
cmd_usage(cmd_tbl_t
*cmdtp
)
225 print_usage_line(cmdtp
, 0);
226 #ifdef CONFIG_SYS_LONGHELP
227 my_puts_P(PSTR("Usage:\n"));
228 my_puts_P(cmdtp
->name
);
229 my_puts_P(PSTR(" "));
231 if (cmdtp
->help
&& *cmdtp
->help
!= '\0')
234 my_puts_P(PSTR(" - No additional help available.\n"));
235 #endif /* CONFIG_SYS_LONGHELP */
236 return CMD_RET_FAILURE
;
239 #ifdef CONFIG_AUTO_COMPLETE
241 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
243 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
246 space
= last_char
== '\0' || isblank(last_char
);
248 if (space
&& argc
== 1)
249 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
251 if (!space
&& argc
== 2)
252 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
257 /*************************************************************************************/
259 /* TODO: cmdtp points to FLASH */
261 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
263 cmd_tbl_t
*cmdtp
= cmd_tbl
;
264 // const int count = ARRAY_SIZE(cmd_tbl);
265 // const cmd_tbl_t *cmdend = cmdtp + count;
278 /* output full list of commands */
279 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
280 if (n_found
>= maxv
- 2) {
281 cmdv
[n_found
++] = "...";
284 cmdv
[n_found
++] = cmdtp
->name
;
286 cmdv
[n_found
] = NULL
;
290 /* more than one arg or one but the start of the next */
291 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
292 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
293 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
297 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
304 /* return the partial matches */
305 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
307 clen
= strlen(cmdtp
->name
);
311 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
315 if (n_found
>= maxv
- 2) {
316 cmdv
[n_found
++] = "...";
320 cmdv
[n_found
++] = cmdtp
->name
;
323 cmdv
[n_found
] = NULL
;
327 static int make_argv(char *s
, int argvsz
, char *argv
[])
331 /* split into argv */
332 while (argc
< argvsz
- 1) {
334 /* skip any white space */
338 if (*s
== '\0') /* end of s, no more args */
341 argv
[argc
++] = s
; /* begin of argument string */
343 /* find end of string */
344 while (*s
&& !isblank(*s
))
347 if (*s
== '\0') /* end of s, no more args */
350 *s
++ = '\0'; /* terminate current arg */
357 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
359 int ll
= leader
!= NULL
? strlen(leader
) : 0;
360 int sl
= sep
!= NULL
? strlen(sep
) : 0;
364 my_puts_P(PSTR("\n"));
368 i
= linemax
; /* force leader and newline */
369 while (*argv
!= NULL
) {
370 len
= strlen(*argv
) + sl
;
371 if (i
+ len
>= linemax
) {
372 my_puts_P(PSTR("\n"));
381 my_puts_P(PSTR("\n"));
384 static int find_common_prefix(char * const argv
[])
387 char *anchor
, *s
, *t
;
394 len
= strlen(anchor
);
395 while ((t
= *argv
++) != NULL
) {
397 for (i
= 0; i
< len
; i
++, t
++, s
++) {
406 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
409 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
411 int n
= *np
, col
= *colp
;
412 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
416 int i
, j
, k
, len
, seplen
, argc
;
420 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
421 return 0; /* not in normal console */
425 last_char
= buf
[cnt
- 1];
429 /* copy to secondary buffer which will be affected */
430 strcpy(tmp_buf
, buf
);
432 /* separate into argv */
433 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
435 /* do the completion and return the possible completions */
436 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
438 /* no match; bell and out */
440 if (argc
> 1) /* allow tab for non command */
450 if (i
== 1) { /* one match; perfect */
451 k
= strlen(argv
[argc
- 1]);
456 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
457 k
= strlen(argv
[argc
- 1]);
467 /* make sure it fits */
468 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
474 for (i
= 0; i
< len
; i
++)
477 for (i
= 0; i
< seplen
; i
++)
488 print_argv(NULL
, " ", " ", 78, cmdv
);
496 #endif /* CONFIG_AUTO_COMPLETE */
501 * Call a command function. This should be the only route in U-Boot to call
502 * a command, so that we can track whether we are waiting for input or
503 * executing a command.
505 * @param cmdtp Pointer to the command to execute
506 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
507 * @param argc Number of arguments (arg 0 must be the command text)
508 * @param argv Arguments
509 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
511 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
513 command_ret_t result
;
515 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
516 // if (result != CMD_RET_SUCCESS)
517 // debug("Command failed, result=%d\n", result);
521 #pragma GCC diagnostic ignored "-Wclobbered"
523 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
524 uint_fast8_t *repeatable
)
526 command_ret_t rc
= CMD_RET_SUCCESS
;
529 /* Look up command in command table */
530 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
532 /* Check if this command has subcommands */
533 if (cmdtp
->subcmd
&& argc
> 1) {
535 /* Look up subcommand in subcommand table */
536 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
537 if (cmdtpsub
== NULL
) {
538 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
539 return CMD_RET_FAILURE
;
548 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
549 return CMD_RET_FAILURE
;
552 /* found - check max args */
553 if (argc
> cmdtp
->maxargs
)
556 #if defined(CONFIG_CMD_BOOTD)
557 /* avoid "bootd" recursion */
558 else if (cmdtp
->cmd
== do_bootd
) {
559 if (flag
& CMD_FLAG_BOOTD
) {
560 my_puts_P(PSTR("'bootd' recursion detected\n"));
561 rc
= CMD_RET_FAILURE
;
563 flag
|= CMD_FLAG_BOOTD
;
568 if (setjmp(cmd_jbuf
) != 0)
569 return CMD_RET_FAILURE
;
571 /* If OK so far, then do the command */
573 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
574 *repeatable
&= (cmdtp
->flags
& CTBL_RPT
) != 0;
576 if (rc
== CMD_RET_USAGE
)
577 rc
= cmd_usage(cmdtp
);
581 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
583 char buf
[strlen_P(cmdtp
->name
) + 1];
584 strcpy_P(buf
, cmdtp
->name
);
587 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);