]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
2 * (C) Copyright 2014 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"
22 #ifdef CONFIG_AUTO_COMPLETE
29 static void print_usage_line(const FLASH
char *name
, int width
,
30 const FLASH
char *usage
)
32 width
-= strlen_P(name
);
37 my_puts_P(PSTR(" - "));
39 my_puts_P(PSTR("\n"));
42 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
46 while ((c1
= *(const FLASH
unsigned char *)s1
++)
47 == (c2
= *(const FLASH
unsigned char *)s2
++))
54 int cmpstring_PP(const void *p1
, const void *p2
)
56 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
57 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
60 int cmd_tbl_item_count(void)
62 cmd_tbl_t
* p
= cmd_tbl
;
65 while (p
->name
!= NULL
) {
72 * Use puts() instead of printf() to avoid printf buffer overflow
73 * for long help messages
76 command_ret_t
_do_help(cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
,
77 int flag
, int argc
, char * const argv
[])
79 uint_fast8_t i
, max_len
= 0;
80 command_ret_t rcode
= CMD_RET_SUCCESS
;
84 if (argc
== 1) { /*show list of commands */
85 cmd_tbl_t
*cmd_array
[cmd_items
];
88 /* Make array of commands from .uboot_cmd section */
90 for (i
= 0; i
< cmd_items
; i
++) {
91 cmd_array
[i
] = cmdtp
++;
92 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
97 /* Sort command list */
98 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
100 /* print short help (usage) */
101 for (i
= 0; i
< cmd_items
; i
++) {
102 const FLASH
char *usage
= cmd_array
[i
]->usage
;
104 /* allow user abort */
106 return CMD_RET_FAILURE
;
110 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
112 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
113 cmd_array
[i
]->name
, usage
);
116 return CMD_RET_SUCCESS
;
119 * command help (long version)
121 for (i
= 1; i
< argc
; ++i
) {
122 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
123 rcode
= cmd_usage(cmdtp
);
125 printf_P(PSTR("Unknown command '%s' - try 'help'"
126 " without arguments.\n\n"), argv
[i
]
128 rcode
= CMD_RET_FAILURE
;
134 /***************************************************************************
135 * find command table entry for a command
137 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
140 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
142 uint_fast8_t n_found
= 0;
150 cmdtp
!= table
+ table_len
;
152 if (strncmp_P (cmd
, cmdtp
->name
, len
) == 0) {
153 if (len
== strlen (cmdtp
->name
))
154 return cmdtp
; /* full match */
156 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
160 if (n_found
== 1) { /* exactly one match */
164 return NULL
; /* not found or ambiguous command */
168 cmd_tbl_t
*find_cmd (const char *cmd
)
170 return find_cmd_tbl(cmd
, cmd_tbl
, cmd_tbl_item_count());
174 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
176 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
177 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
179 my_puts_P(cmdtp
->name
);
180 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
181 my_puts_P(PSTR(" - "));
182 my_puts_P(cmdtp
->usage
);
183 my_puts_P(PSTR("\n\n"));
185 #ifdef CONFIG_SYS_LONGHELP
186 // printf("Usage:\n%s ", cmdtp->name);
187 my_puts_P(PSTR("Usage:\n"));
188 my_puts_P(cmdtp
->name
);
189 my_puts_P(PSTR(" "));
192 my_puts_P(PSTR(" - No additional help available.\n"));
193 return CMD_RET_FAILURE
;
196 my_puts_P(cmdtp
->help
);
197 my_puts_P(PSTR("\n"));
198 #endif /* CONFIG_SYS_LONGHELP */
199 return CMD_RET_FAILURE
;
202 #ifdef CONFIG_AUTO_COMPLETE
204 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
206 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
209 space
= last_char
== '\0' || isblank(last_char
);
211 if (space
&& argc
== 1)
212 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
214 if (!space
&& argc
== 2)
215 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
220 /*************************************************************************************/
222 /* TODO: cmdtp points to FLASH */
224 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
226 cmd_tbl_t
*cmdtp
= cmd_tbl
;
227 // const int count = ARRAY_SIZE(cmd_tbl);
228 // const cmd_tbl_t *cmdend = cmdtp + count;
241 /* output full list of commands */
242 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
243 if (n_found
>= maxv
- 2) {
244 cmdv
[n_found
++] = "...";
247 cmdv
[n_found
++] = cmdtp
->name
;
249 cmdv
[n_found
] = NULL
;
253 /* more than one arg or one but the start of the next */
254 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
255 cmdtp
= find_cmd(argv
[0]);
256 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
260 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
267 /* return the partial matches */
268 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
270 clen
= strlen(cmdtp
->name
);
274 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
278 if (n_found
>= maxv
- 2) {
279 cmdv
[n_found
++] = "...";
283 cmdv
[n_found
++] = cmdtp
->name
;
286 cmdv
[n_found
] = NULL
;
290 static int make_argv(char *s
, int argvsz
, char *argv
[])
294 /* split into argv */
295 while (argc
< argvsz
- 1) {
297 /* skip any white space */
301 if (*s
== '\0') /* end of s, no more args */
304 argv
[argc
++] = s
; /* begin of argument string */
306 /* find end of string */
307 while (*s
&& !isblank(*s
))
310 if (*s
== '\0') /* end of s, no more args */
313 *s
++ = '\0'; /* terminate current arg */
320 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
322 int ll
= leader
!= NULL
? strlen(leader
) : 0;
323 int sl
= sep
!= NULL
? strlen(sep
) : 0;
327 my_puts_P(PSTR("\n"));
331 i
= linemax
; /* force leader and newline */
332 while (*argv
!= NULL
) {
333 len
= strlen(*argv
) + sl
;
334 if (i
+ len
>= linemax
) {
335 my_puts_P(PSTR("\n"));
344 my_puts_P(PSTR("\n"));
347 static int find_common_prefix(char * const argv
[])
350 char *anchor
, *s
, *t
;
357 len
= strlen(anchor
);
358 while ((t
= *argv
++) != NULL
) {
360 for (i
= 0; i
< len
; i
++, t
++, s
++) {
369 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
372 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
374 int n
= *np
, col
= *colp
;
375 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
379 int i
, j
, k
, len
, seplen
, argc
;
383 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
384 return 0; /* not in normal console */
388 last_char
= buf
[cnt
- 1];
392 /* copy to secondary buffer which will be affected */
393 strcpy(tmp_buf
, buf
);
395 /* separate into argv */
396 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
398 /* do the completion and return the possible completions */
399 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
401 /* no match; bell and out */
403 if (argc
> 1) /* allow tab for non command */
413 if (i
== 1) { /* one match; perfect */
414 k
= strlen(argv
[argc
- 1]);
419 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
420 k
= strlen(argv
[argc
- 1]);
430 /* make sure it fits */
431 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
437 for (i
= 0; i
< len
; i
++)
440 for (i
= 0; i
< seplen
; i
++)
451 print_argv(NULL
, " ", " ", 78, cmdv
);
459 #endif /* CONFIG_AUTO_COMPLETE */
464 * Call a command function. This should be the only route in U-Boot to call
465 * a command, so that we can track whether we are waiting for input or
466 * executing a command.
468 * @param cmdtp Pointer to the command to execute
469 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
470 * @param argc Number of arguments (arg 0 must be the command text)
471 * @param argv Arguments
472 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
474 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
476 command_ret_t result
;
478 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
479 if (result
!= CMD_RET_SUCCESS
)
480 debug("Command failed, result=%d\n", result
);
484 command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
485 uint_fast8_t *repeatable
)
487 command_ret_t rc
= CMD_RET_SUCCESS
;
490 /* Look up command in command table */
491 cmdtp
= find_cmd(argv
[0]);
493 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
494 return CMD_RET_FAILURE
;
497 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
498 return CMD_RET_FAILURE
;
501 /* found - check max args */
502 if (argc
> cmdtp
->maxargs
)
505 #if defined(CONFIG_CMD_BOOTD)
506 /* avoid "bootd" recursion */
507 else if (cmdtp
->cmd
== do_bootd
) {
508 if (flag
& CMD_FLAG_BOOTD
) {
509 my_puts_P(PSTR("'bootd' recursion detected\n"));
510 rc
= CMD_RET_FAILURE
;
512 flag
|= CMD_FLAG_BOOTD
;
517 /* If OK so far, then do the command */
519 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
520 *repeatable
&= cmdtp
->repeatable
;
522 if (rc
== CMD_RET_USAGE
)
523 rc
= cmd_usage(cmdtp
);
527 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
529 char buf
[strlen_P(cmdtp
->name
) + 1];
530 strcpy_P(buf
, cmdtp
->name
);
533 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);