]>
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 int cmd_tbl_item_count(void)
64 cmd_tbl_t
* p
= cmd_tbl
;
67 while (p
->name
!= NULL
) {
74 * Use puts() instead of printf() to avoid printf buffer overflow
75 * for long help messages
78 command_ret_t
_do_help(cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
,
79 int flag
, int argc
, char * const argv
[])
81 uint_fast8_t i
, max_len
= 0;
82 command_ret_t rcode
= CMD_RET_SUCCESS
;
86 char *optenv
= getenv_str(PSTR("cmd"));
87 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
89 if (argc
== 1) { /*show list of commands */
90 cmd_tbl_t
*cmd_array
[cmd_items
];
93 /* Make array of commands from .uboot_cmd section */
95 for (i
= 0; i
< cmd_items
; i
++) {
96 cmd_array
[i
] = cmdtp
++;
97 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
102 /* Sort command list */
103 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstring_PP
);
105 /* print short help (usage) */
106 for (i
= 0; i
< cmd_items
; i
++) {
107 if (opt_debug
|| cmd_array
[i
]->name
[0] != '!') {
108 const FLASH
char *usage
= cmd_array
[i
]->usage
;
110 /* allow user abort */
112 return CMD_RET_FAILURE
;
116 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
118 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
119 cmd_array
[i
]->name
, usage
);
123 return CMD_RET_SUCCESS
;
126 * command help (long version)
128 for (i
= 1; i
< argc
; ++i
) {
129 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
&&
130 (opt_debug
|| cmdtp
->name
[0] != '!')) {
133 printf_P(PSTR("Unknown command '%s' - try 'help'"
134 " without arguments.\n\n"), argv
[i
]
136 rcode
= CMD_RET_FAILURE
;
142 /***************************************************************************
143 * find command table entry for a command
145 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
148 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
150 uint_fast8_t n_found
= 0;
152 char *optenv
= getenv_str(PSTR("cmd"));
153 bool opt_debug
= optenv
&& strstr_P(optenv
, PSTR("debug")) != NULL
;
161 cmdtp
!= table
+ table_len
;
163 if (strncmp_P(cmd
, cmdtp
->name
, len
) == 0 &&
164 (opt_debug
|| cmdtp
->name
[0] != '!')) {
165 if (len
== strlen_P(cmdtp
->name
))
166 return cmdtp
; /* full match */
168 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
172 if (n_found
== 1) /* exactly one match */
175 return NULL
; /* not found or ambiguous command */
179 cmd_tbl_t
*find_cmd (const char *cmd
)
181 return find_cmd_tbl(cmd
, cmd_tbl
, cmd_tbl_item_count());
185 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
187 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
188 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
190 my_puts_P(cmdtp
->name
);
191 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
192 my_puts_P(PSTR(" - "));
193 my_puts_P(cmdtp
->usage
);
194 my_puts_P(PSTR("\n\n"));
196 #ifdef CONFIG_SYS_LONGHELP
197 // printf("Usage:\n%s ", cmdtp->name);
198 my_puts_P(PSTR("Usage:\n"));
199 my_puts_P(cmdtp
->name
);
200 my_puts_P(PSTR(" "));
203 my_puts_P(PSTR(" - No additional help available.\n"));
204 return CMD_RET_FAILURE
;
207 my_puts_P(cmdtp
->help
);
208 my_puts_P(PSTR("\n"));
209 #endif /* CONFIG_SYS_LONGHELP */
210 return CMD_RET_FAILURE
;
213 #ifdef CONFIG_AUTO_COMPLETE
215 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
217 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
220 space
= last_char
== '\0' || isblank(last_char
);
222 if (space
&& argc
== 1)
223 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
225 if (!space
&& argc
== 2)
226 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
231 /*************************************************************************************/
233 /* TODO: cmdtp points to FLASH */
235 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
237 cmd_tbl_t
*cmdtp
= cmd_tbl
;
238 // const int count = ARRAY_SIZE(cmd_tbl);
239 // const cmd_tbl_t *cmdend = cmdtp + count;
252 /* output full list of commands */
253 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
254 if (n_found
>= maxv
- 2) {
255 cmdv
[n_found
++] = "...";
258 cmdv
[n_found
++] = cmdtp
->name
;
260 cmdv
[n_found
] = NULL
;
264 /* more than one arg or one but the start of the next */
265 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
266 cmdtp
= find_cmd(argv
[0]);
267 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
271 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
278 /* return the partial matches */
279 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
281 clen
= strlen(cmdtp
->name
);
285 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
289 if (n_found
>= maxv
- 2) {
290 cmdv
[n_found
++] = "...";
294 cmdv
[n_found
++] = cmdtp
->name
;
297 cmdv
[n_found
] = NULL
;
301 static int make_argv(char *s
, int argvsz
, char *argv
[])
305 /* split into argv */
306 while (argc
< argvsz
- 1) {
308 /* skip any white space */
312 if (*s
== '\0') /* end of s, no more args */
315 argv
[argc
++] = s
; /* begin of argument string */
317 /* find end of string */
318 while (*s
&& !isblank(*s
))
321 if (*s
== '\0') /* end of s, no more args */
324 *s
++ = '\0'; /* terminate current arg */
331 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
333 int ll
= leader
!= NULL
? strlen(leader
) : 0;
334 int sl
= sep
!= NULL
? strlen(sep
) : 0;
338 my_puts_P(PSTR("\n"));
342 i
= linemax
; /* force leader and newline */
343 while (*argv
!= NULL
) {
344 len
= strlen(*argv
) + sl
;
345 if (i
+ len
>= linemax
) {
346 my_puts_P(PSTR("\n"));
355 my_puts_P(PSTR("\n"));
358 static int find_common_prefix(char * const argv
[])
361 char *anchor
, *s
, *t
;
368 len
= strlen(anchor
);
369 while ((t
= *argv
++) != NULL
) {
371 for (i
= 0; i
< len
; i
++, t
++, s
++) {
380 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
383 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
385 int n
= *np
, col
= *colp
;
386 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
390 int i
, j
, k
, len
, seplen
, argc
;
394 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
395 return 0; /* not in normal console */
399 last_char
= buf
[cnt
- 1];
403 /* copy to secondary buffer which will be affected */
404 strcpy(tmp_buf
, buf
);
406 /* separate into argv */
407 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
409 /* do the completion and return the possible completions */
410 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
412 /* no match; bell and out */
414 if (argc
> 1) /* allow tab for non command */
424 if (i
== 1) { /* one match; perfect */
425 k
= strlen(argv
[argc
- 1]);
430 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
431 k
= strlen(argv
[argc
- 1]);
441 /* make sure it fits */
442 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
448 for (i
= 0; i
< len
; i
++)
451 for (i
= 0; i
< seplen
; i
++)
462 print_argv(NULL
, " ", " ", 78, cmdv
);
470 #endif /* CONFIG_AUTO_COMPLETE */
475 * Call a command function. This should be the only route in U-Boot to call
476 * a command, so that we can track whether we are waiting for input or
477 * executing a command.
479 * @param cmdtp Pointer to the command to execute
480 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
481 * @param argc Number of arguments (arg 0 must be the command text)
482 * @param argv Arguments
483 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
485 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
487 command_ret_t result
;
489 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
490 // if (result != CMD_RET_SUCCESS)
491 // debug("Command failed, result=%d\n", result);
495 command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
496 uint_fast8_t *repeatable
)
498 command_ret_t rc
= CMD_RET_SUCCESS
;
501 /* Look up command in command table */
502 cmdtp
= find_cmd(argv
[0]);
504 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
505 return CMD_RET_FAILURE
;
508 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
509 return CMD_RET_FAILURE
;
512 /* found - check max args */
513 if (argc
> cmdtp
->maxargs
)
516 #if defined(CONFIG_CMD_BOOTD)
517 /* avoid "bootd" recursion */
518 else if (cmdtp
->cmd
== do_bootd
) {
519 if (flag
& CMD_FLAG_BOOTD
) {
520 my_puts_P(PSTR("'bootd' recursion detected\n"));
521 rc
= CMD_RET_FAILURE
;
523 flag
|= CMD_FLAG_BOOTD
;
528 if (setjmp(cmd_jbuf
) != 0)
529 return CMD_RET_FAILURE
;
531 /* If OK so far, then do the command */
533 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
534 *repeatable
&= cmdtp
->repeatable
;
536 if (rc
== CMD_RET_USAGE
)
537 rc
= cmd_usage(cmdtp
);
541 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
543 char buf
[strlen_P(cmdtp
->name
) + 1];
544 strcpy_P(buf
, cmdtp
->name
);
547 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);