]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
07fff78fbf24dce4cf1498fb5e2843561a8bee29
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 int cmd_tbl_item_count(cmd_tbl_t
*p
)
66 while (p
->name
!= NULL
) {
73 * Use puts() instead of printf() to avoid printf buffer overflow
74 * for long help messages
77 command_ret_t
_do_help(cmd_tbl_t
*tbl_start
, cmd_tbl_t
* cmdtp
,
78 uint_fast8_t flag
, int argc
, char * const argv
[])
83 char *optenv
= getenv_str(PSTR("cmd"));
84 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
86 if (argc
== 1) { /*show list of commands */
87 int cmd_items
= cmd_tbl_item_count(tbl_start
);
88 cmd_tbl_t
*cmd_array
[cmd_items
];
89 cmd_tbl_t
*tp
= tbl_start
;
90 uint_fast8_t max_len
= 0;
92 /* Make array of commands from .uboot_cmd section */
93 for (int i
= 0; i
< cmd_items
; i
++) {
95 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
100 /* Sort command list */
101 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
103 /* print short help (usage) */
104 for (int i
= 0; i
< cmd_items
; i
++) {
105 if (opt_debug
|| cmd_array
[i
]->name
[0] != '!') {
106 const FLASH
char *usage
= cmd_array
[i
]->usage
;
108 /* allow user abort */
110 return CMD_RET_FAILURE
;
113 #if defined(GCC_BUG_61443) || 1
114 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
116 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
117 cmd_array
[i
]->name
, usage
);
121 return CMD_RET_SUCCESS
;
125 * command help (long version)
127 for (uint8_t i
= 1; i
< argc
; ++i
) {
128 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
)) != NULL
&&
129 (opt_debug
|| cmdtp
->name
[0] != '!')) {
132 printf_P(PSTR("Unknown command '%s' - try 'help'"
133 " without arguments.\n\n"), argv
[i
]
135 return CMD_RET_FAILURE
;
139 return CMD_RET_SUCCESS
;
142 /***************************************************************************
143 * find command table entry for a command
145 cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
148 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
149 int table_len
= cmd_tbl_item_count(table
);
151 uint_fast8_t n_found
= 0;
153 char *optenv
= getenv_str(PSTR("cmd"));
154 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
162 cmdtp
!= table
+ table_len
;
164 if (strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
165 (opt_debug
|| cmdtp
->name
[0] != '!')) {
166 if (len
== strlen_P(cmdtp
->name
))
167 return cmdtp
; /* full match */
169 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
173 if (n_found
== 1) /* exactly one match */
176 return NULL
; /* not found or ambiguous command */
181 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
183 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
184 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
186 my_puts_P(cmdtp
->name
);
187 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
188 my_puts_P(PSTR(" - "));
189 my_puts_P(cmdtp
->usage
);
190 my_puts_P(PSTR("\n\n"));
192 #ifdef CONFIG_SYS_LONGHELP
193 // printf("Usage:\n%s ", cmdtp->name);
194 my_puts_P(PSTR("Usage:\n"));
195 my_puts_P(cmdtp
->name
);
196 my_puts_P(PSTR(" "));
199 my_puts_P(PSTR(" - No additional help available.\n"));
200 return CMD_RET_FAILURE
;
203 my_puts_P(cmdtp
->help
);
204 my_puts_P(PSTR("\n"));
205 #endif /* CONFIG_SYS_LONGHELP */
206 return CMD_RET_FAILURE
;
209 #ifdef CONFIG_AUTO_COMPLETE
211 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
213 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
216 space
= last_char
== '\0' || isblank(last_char
);
218 if (space
&& argc
== 1)
219 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
221 if (!space
&& argc
== 2)
222 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
227 /*************************************************************************************/
229 /* TODO: cmdtp points to FLASH */
231 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
233 cmd_tbl_t
*cmdtp
= cmd_tbl
;
234 // const int count = ARRAY_SIZE(cmd_tbl);
235 // const cmd_tbl_t *cmdend = cmdtp + count;
248 /* output full list of commands */
249 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
250 if (n_found
>= maxv
- 2) {
251 cmdv
[n_found
++] = "...";
254 cmdv
[n_found
++] = cmdtp
->name
;
256 cmdv
[n_found
] = NULL
;
260 /* more than one arg or one but the start of the next */
261 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
262 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
263 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
267 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
274 /* return the partial matches */
275 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
277 clen
= strlen(cmdtp
->name
);
281 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
285 if (n_found
>= maxv
- 2) {
286 cmdv
[n_found
++] = "...";
290 cmdv
[n_found
++] = cmdtp
->name
;
293 cmdv
[n_found
] = NULL
;
297 static int make_argv(char *s
, int argvsz
, char *argv
[])
301 /* split into argv */
302 while (argc
< argvsz
- 1) {
304 /* skip any white space */
308 if (*s
== '\0') /* end of s, no more args */
311 argv
[argc
++] = s
; /* begin of argument string */
313 /* find end of string */
314 while (*s
&& !isblank(*s
))
317 if (*s
== '\0') /* end of s, no more args */
320 *s
++ = '\0'; /* terminate current arg */
327 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
329 int ll
= leader
!= NULL
? strlen(leader
) : 0;
330 int sl
= sep
!= NULL
? strlen(sep
) : 0;
334 my_puts_P(PSTR("\n"));
338 i
= linemax
; /* force leader and newline */
339 while (*argv
!= NULL
) {
340 len
= strlen(*argv
) + sl
;
341 if (i
+ len
>= linemax
) {
342 my_puts_P(PSTR("\n"));
351 my_puts_P(PSTR("\n"));
354 static int find_common_prefix(char * const argv
[])
357 char *anchor
, *s
, *t
;
364 len
= strlen(anchor
);
365 while ((t
= *argv
++) != NULL
) {
367 for (i
= 0; i
< len
; i
++, t
++, s
++) {
376 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
379 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
381 int n
= *np
, col
= *colp
;
382 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
386 int i
, j
, k
, len
, seplen
, argc
;
390 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
391 return 0; /* not in normal console */
395 last_char
= buf
[cnt
- 1];
399 /* copy to secondary buffer which will be affected */
400 strcpy(tmp_buf
, buf
);
402 /* separate into argv */
403 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
405 /* do the completion and return the possible completions */
406 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
408 /* no match; bell and out */
410 if (argc
> 1) /* allow tab for non command */
420 if (i
== 1) { /* one match; perfect */
421 k
= strlen(argv
[argc
- 1]);
426 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
427 k
= strlen(argv
[argc
- 1]);
437 /* make sure it fits */
438 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
444 for (i
= 0; i
< len
; i
++)
447 for (i
= 0; i
< seplen
; i
++)
458 print_argv(NULL
, " ", " ", 78, cmdv
);
466 #endif /* CONFIG_AUTO_COMPLETE */
471 * Call a command function. This should be the only route in U-Boot to call
472 * a command, so that we can track whether we are waiting for input or
473 * executing a command.
475 * @param cmdtp Pointer to the command to execute
476 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
477 * @param argc Number of arguments (arg 0 must be the command text)
478 * @param argv Arguments
479 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
481 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
483 command_ret_t result
;
485 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
486 // if (result != CMD_RET_SUCCESS)
487 // debug("Command failed, result=%d\n", result);
491 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
492 uint_fast8_t *repeatable
)
494 command_ret_t rc
= CMD_RET_SUCCESS
;
497 /* Look up command in command table */
498 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
500 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
501 return CMD_RET_FAILURE
;
504 /* Check for subcommand */
505 if (cmdtp
->subcmd
&& argc
> 1) {
507 /* Look up subcommand in subcommand table */
508 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
509 if (cmdtpsub
== NULL
) {
510 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
511 return CMD_RET_FAILURE
;
518 /* found - check max args */
519 if (argc
> cmdtp
->maxargs
)
522 #if defined(CONFIG_CMD_BOOTD)
523 /* avoid "bootd" recursion */
524 else if (cmdtp
->cmd
== do_bootd
) {
525 if (flag
& CMD_FLAG_BOOTD
) {
526 my_puts_P(PSTR("'bootd' recursion detected\n"));
527 rc
= CMD_RET_FAILURE
;
529 flag
|= CMD_FLAG_BOOTD
;
534 if (setjmp(cmd_jbuf
) != 0)
535 return CMD_RET_FAILURE
;
537 /* If OK so far, then do the command */
539 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
540 *repeatable
&= (cmdtp
->flags
& CTBL_REPEAT
) != 0;
542 if (rc
== CMD_RET_USAGE
)
543 rc
= cmd_usage(cmdtp
);
547 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
549 char buf
[strlen_P(cmdtp
->name
) + 1];
550 strcpy_P(buf
, cmdtp
->name
);
553 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);