]>
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
88 cmd_tbl_t
*find_cmd (const char *cmd
, cmd_tbl_t
*table
)
91 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
92 int table_len
= cmd_tbl_item_count(table
);
94 uint_fast8_t n_found
= 0;
96 char *optenv
= getenv_str(PSTR("cmd"));
97 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
105 cmdtp
!= table
+ table_len
;
107 if (strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
108 (opt_debug
|| cmdtp
->name
[0] != '!')) {
109 if (len
== strlen_P(cmdtp
->name
))
110 return cmdtp
; /* full match */
112 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
116 if (n_found
== 1) /* exactly one match */
119 return NULL
; /* not found or ambiguous command */
122 cmd_tbl_t
*find_cmd_sub(const char *cmd
, cmd_tbl_t
*table
)
124 cmd_tbl_t
*entry
= NULL
;
126 for (cmd_tbl_t
*tp
= get_cmd_tbl_base(table
); tp
->name
&& entry
== NULL
; tp
++)
127 if (tp
->subcmd
&& tp
->flags
& CTBL_SUBCMDAUTO
)
128 entry
= find_cmd(cmd
, tp
->subcmd
);
134 * Use puts() instead of printf() to avoid printf buffer overflow
135 * for long help messages
138 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
142 cmd_tbl_t
*tbl_start
= get_cmd_tbl_base(cmdtp
);
144 char *optenv
= getenv_str(PSTR("cmd"));
145 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
147 if (argc
== 1) { /*show list of commands */
148 int cmd_items
= cmd_tbl_item_count(tbl_start
);
149 cmd_tbl_t
*cmd_array
[cmd_items
];
150 cmd_tbl_t
*tp
= tbl_start
;
151 uint_fast8_t max_len
= 0;
153 /* Make array of commands from .uboot_cmd section */
154 for (int i
= 0; i
< cmd_items
; i
++) {
156 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
161 /* Sort command list */
162 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
164 /* print short help (usage) */
165 for (int i
= 0; i
< cmd_items
; i
++) {
166 if (opt_debug
|| cmd_array
[i
]->name
[0] != '!') {
167 const FLASH
char *usage
= cmd_array
[i
]->usage
;
169 /* allow user abort */
171 return CMD_RET_FAILURE
;
174 #if defined(GCC_BUG_61443) || 1
175 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
177 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
178 cmd_array
[i
]->name
, usage
);
182 return CMD_RET_SUCCESS
;
186 * command help (long version)
188 for (uint8_t i
= 1; i
< argc
; ++i
) {
189 if ((cmdtp
= find_cmd(argv
[i
], tbl_start
)) != NULL
&&
190 (opt_debug
|| cmdtp
->name
[0] != '!')) {
193 printf_P(PSTR("Unknown command '%s' - try 'help'"
194 " without arguments.\n\n"), argv
[i
]
196 return CMD_RET_FAILURE
;
200 return CMD_RET_SUCCESS
;
204 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
206 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
207 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
209 my_puts_P(cmdtp
->name
);
210 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
211 my_puts_P(PSTR(" - "));
212 my_puts_P(cmdtp
->usage
);
213 my_puts_P(PSTR("\n\n"));
215 #ifdef CONFIG_SYS_LONGHELP
216 // printf("Usage:\n%s ", cmdtp->name);
217 my_puts_P(PSTR("Usage:\n"));
218 my_puts_P(cmdtp
->name
);
219 my_puts_P(PSTR(" "));
222 my_puts_P(PSTR(" - No additional help available.\n"));
223 return CMD_RET_FAILURE
;
226 my_puts_P(cmdtp
->help
);
227 my_puts_P(PSTR("\n"));
228 #endif /* CONFIG_SYS_LONGHELP */
229 return CMD_RET_FAILURE
;
232 #ifdef CONFIG_AUTO_COMPLETE
234 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
236 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
239 space
= last_char
== '\0' || isblank(last_char
);
241 if (space
&& argc
== 1)
242 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
244 if (!space
&& argc
== 2)
245 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
250 /*************************************************************************************/
252 /* TODO: cmdtp points to FLASH */
254 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
256 cmd_tbl_t
*cmdtp
= cmd_tbl
;
257 // const int count = ARRAY_SIZE(cmd_tbl);
258 // const cmd_tbl_t *cmdend = cmdtp + count;
271 /* output full list of commands */
272 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
273 if (n_found
>= maxv
- 2) {
274 cmdv
[n_found
++] = "...";
277 cmdv
[n_found
++] = cmdtp
->name
;
279 cmdv
[n_found
] = NULL
;
283 /* more than one arg or one but the start of the next */
284 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
285 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
286 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
290 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
297 /* return the partial matches */
298 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
300 clen
= strlen(cmdtp
->name
);
304 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
308 if (n_found
>= maxv
- 2) {
309 cmdv
[n_found
++] = "...";
313 cmdv
[n_found
++] = cmdtp
->name
;
316 cmdv
[n_found
] = NULL
;
320 static int make_argv(char *s
, int argvsz
, char *argv
[])
324 /* split into argv */
325 while (argc
< argvsz
- 1) {
327 /* skip any white space */
331 if (*s
== '\0') /* end of s, no more args */
334 argv
[argc
++] = s
; /* begin of argument string */
336 /* find end of string */
337 while (*s
&& !isblank(*s
))
340 if (*s
== '\0') /* end of s, no more args */
343 *s
++ = '\0'; /* terminate current arg */
350 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
352 int ll
= leader
!= NULL
? strlen(leader
) : 0;
353 int sl
= sep
!= NULL
? strlen(sep
) : 0;
357 my_puts_P(PSTR("\n"));
361 i
= linemax
; /* force leader and newline */
362 while (*argv
!= NULL
) {
363 len
= strlen(*argv
) + sl
;
364 if (i
+ len
>= linemax
) {
365 my_puts_P(PSTR("\n"));
374 my_puts_P(PSTR("\n"));
377 static int find_common_prefix(char * const argv
[])
380 char *anchor
, *s
, *t
;
387 len
= strlen(anchor
);
388 while ((t
= *argv
++) != NULL
) {
390 for (i
= 0; i
< len
; i
++, t
++, s
++) {
399 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
402 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
404 int n
= *np
, col
= *colp
;
405 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
409 int i
, j
, k
, len
, seplen
, argc
;
413 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
414 return 0; /* not in normal console */
418 last_char
= buf
[cnt
- 1];
422 /* copy to secondary buffer which will be affected */
423 strcpy(tmp_buf
, buf
);
425 /* separate into argv */
426 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
428 /* do the completion and return the possible completions */
429 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
431 /* no match; bell and out */
433 if (argc
> 1) /* allow tab for non command */
443 if (i
== 1) { /* one match; perfect */
444 k
= strlen(argv
[argc
- 1]);
449 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
450 k
= strlen(argv
[argc
- 1]);
460 /* make sure it fits */
461 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
467 for (i
= 0; i
< len
; i
++)
470 for (i
= 0; i
< seplen
; i
++)
481 print_argv(NULL
, " ", " ", 78, cmdv
);
489 #endif /* CONFIG_AUTO_COMPLETE */
494 * Call a command function. This should be the only route in U-Boot to call
495 * a command, so that we can track whether we are waiting for input or
496 * executing a command.
498 * @param cmdtp Pointer to the command to execute
499 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
500 * @param argc Number of arguments (arg 0 must be the command text)
501 * @param argv Arguments
502 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
504 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
506 command_ret_t result
;
508 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
509 // if (result != CMD_RET_SUCCESS)
510 // debug("Command failed, result=%d\n", result);
514 #pragma GCC diagnostic ignored "-Wclobbered"
516 command_ret_t
cmd_process(uint_fast8_t flag
, int argc
, char * const argv
[],
517 uint_fast8_t *repeatable
)
519 command_ret_t rc
= CMD_RET_SUCCESS
;
522 /* Look up command in command table */
523 cmdtp
= find_cmd(argv
[0], cmd_tbl
);
525 /* Check if this command has subcommands */
526 if (cmdtp
->subcmd
&& argc
> 1) {
528 /* Look up subcommand in subcommand table */
529 cmd_tbl_t
*cmdtpsub
= find_cmd(argv
[1], cmdtp
->subcmd
);
530 if (cmdtpsub
== NULL
) {
531 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv
[0], argv
[1], argv
[0]);
532 return CMD_RET_FAILURE
;
539 /* Search subcommands */
540 cmdtp
= find_cmd_sub(argv
[0], cmd_tbl
);
544 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
545 return CMD_RET_FAILURE
;
548 /* found - check max args */
549 if (argc
> cmdtp
->maxargs
)
552 #if defined(CONFIG_CMD_BOOTD)
553 /* avoid "bootd" recursion */
554 else if (cmdtp
->cmd
== do_bootd
) {
555 if (flag
& CMD_FLAG_BOOTD
) {
556 my_puts_P(PSTR("'bootd' recursion detected\n"));
557 rc
= CMD_RET_FAILURE
;
559 flag
|= CMD_FLAG_BOOTD
;
564 if (setjmp(cmd_jbuf
) != 0)
565 return CMD_RET_FAILURE
;
567 /* If OK so far, then do the command */
569 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
570 *repeatable
&= (cmdtp
->flags
& CTBL_REPEAT
) != 0;
572 if (rc
== CMD_RET_USAGE
)
573 rc
= cmd_usage(cmdtp
);
577 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
579 char buf
[strlen_P(cmdtp
->name
) + 1];
580 strcpy_P(buf
, cmdtp
->name
);
583 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);