]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
2 * Command Processor Table
10 #include <avr/pgmspace.h>
13 #include "print-utils.h"
14 #include "con-utils.h"
15 #ifdef CONFIG_AUTO_COMPLETE
22 static void print_usage_line(const FLASH
char *name
, int width
,
23 const FLASH
char *usage
)
25 width
-= strlen_P(name
);
30 my_puts_P(PSTR(" - "));
32 my_puts_P(PSTR("\n"));
35 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
39 while ((c1
= *(const FLASH
unsigned char *)s1
++)
40 == (c2
= *(const FLASH
unsigned char *)s2
++))
47 int cmpstringp(const void *p1
, const void *p2
)
49 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
50 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
53 int cmd_tbl_item_count(void)
55 cmd_tbl_t
* p
= cmd_tbl
;
58 while (p
->name
!= NULL
) {
65 * Use puts() instead of printf() to avoid printf buffer overflow
66 * for long help messages
69 command_ret_t
_do_help(cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
,
70 int flag
, int argc
, char * const argv
[])
72 uint_fast8_t i
, max_len
= 0;
73 command_ret_t rcode
= CMD_RET_SUCCESS
;
77 if (argc
== 1) { /*show list of commands */
78 cmd_tbl_t
*cmd_array
[cmd_items
];
81 /* Make array of commands from .uboot_cmd section */
83 for (i
= 0; i
< cmd_items
; i
++) {
84 cmd_array
[i
] = cmdtp
++;
85 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
90 /* Sort command list */
91 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstringp
);
93 /* print short help (usage) */
94 for (i
= 0; i
< cmd_items
; i
++) {
95 const FLASH
char *usage
= cmd_array
[i
]->usage
;
97 /* allow user abort */
99 return CMD_RET_FAILURE
;
103 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
105 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
106 cmd_array
[i
]->name
, usage
);
109 return CMD_RET_SUCCESS
;
112 * command help (long version)
114 for (i
= 1; i
< argc
; ++i
) {
115 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
116 rcode
= cmd_usage(cmdtp
);
118 printf_P(PSTR("Unknown command '%s' - try 'help'"
119 " without arguments.\n\n"), argv
[i
]
121 rcode
= CMD_RET_FAILURE
;
127 /***************************************************************************
128 * find command table entry for a command
130 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
133 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
135 uint_fast8_t n_found
= 0;
143 cmdtp
!= table
+ table_len
;
145 if (strncmp_P (cmd
, cmdtp
->name
, len
) == 0) {
146 if (len
== strlen (cmdtp
->name
))
147 return cmdtp
; /* full match */
149 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
153 if (n_found
== 1) { /* exactly one match */
157 return NULL
; /* not found or ambiguous command */
161 cmd_tbl_t
*find_cmd (const char *cmd
)
163 return find_cmd_tbl(cmd
, cmd_tbl
, cmd_tbl_item_count());
167 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
169 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
170 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
172 my_puts_P(cmdtp
->name
);
173 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
174 my_puts_P(PSTR(" - "));
175 my_puts_P(cmdtp
->usage
);
176 my_puts_P(PSTR("\n\n"));
178 #ifdef CONFIG_SYS_LONGHELP
179 // printf("Usage:\n%s ", cmdtp->name);
180 my_puts_P(PSTR("Usage:\n"));
181 my_puts_P(cmdtp
->name
);
182 my_puts_P(PSTR(" "));
185 my_puts_P(PSTR(" - No additional help available.\n"));
186 return CMD_RET_FAILURE
;
189 my_puts_P(cmdtp
->help
);
190 my_puts_P(PSTR("\n"));
191 #endif /* CONFIG_SYS_LONGHELP */
192 return CMD_RET_FAILURE
;
195 #ifdef CONFIG_AUTO_COMPLETE
197 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
199 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
202 space
= last_char
== '\0' || isblank(last_char
);
204 if (space
&& argc
== 1)
205 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
207 if (!space
&& argc
== 2)
208 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
213 /*************************************************************************************/
215 /* TODO: cmdtp points to FLASH */
217 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
219 cmd_tbl_t
*cmdtp
= cmd_tbl
;
220 // const int count = ARRAY_SIZE(cmd_tbl);
221 // const cmd_tbl_t *cmdend = cmdtp + count;
234 /* output full list of commands */
235 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
236 if (n_found
>= maxv
- 2) {
237 cmdv
[n_found
++] = "...";
240 cmdv
[n_found
++] = cmdtp
->name
;
242 cmdv
[n_found
] = NULL
;
246 /* more than one arg or one but the start of the next */
247 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
248 cmdtp
= find_cmd(argv
[0]);
249 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
253 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
260 /* return the partial matches */
261 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
263 clen
= strlen(cmdtp
->name
);
267 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
271 if (n_found
>= maxv
- 2) {
272 cmdv
[n_found
++] = "...";
276 cmdv
[n_found
++] = cmdtp
->name
;
279 cmdv
[n_found
] = NULL
;
283 static int make_argv(char *s
, int argvsz
, char *argv
[])
287 /* split into argv */
288 while (argc
< argvsz
- 1) {
290 /* skip any white space */
294 if (*s
== '\0') /* end of s, no more args */
297 argv
[argc
++] = s
; /* begin of argument string */
299 /* find end of string */
300 while (*s
&& !isblank(*s
))
303 if (*s
== '\0') /* end of s, no more args */
306 *s
++ = '\0'; /* terminate current arg */
313 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
315 int ll
= leader
!= NULL
? strlen(leader
) : 0;
316 int sl
= sep
!= NULL
? strlen(sep
) : 0;
320 my_puts_P(PSTR("\n"));
324 i
= linemax
; /* force leader and newline */
325 while (*argv
!= NULL
) {
326 len
= strlen(*argv
) + sl
;
327 if (i
+ len
>= linemax
) {
328 my_puts_P(PSTR("\n"));
337 my_puts_P(PSTR("\n"));
340 static int find_common_prefix(char * const argv
[])
343 char *anchor
, *s
, *t
;
350 len
= strlen(anchor
);
351 while ((t
= *argv
++) != NULL
) {
353 for (i
= 0; i
< len
; i
++, t
++, s
++) {
362 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
365 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
367 int n
= *np
, col
= *colp
;
368 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
372 int i
, j
, k
, len
, seplen
, argc
;
376 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
377 return 0; /* not in normal console */
381 last_char
= buf
[cnt
- 1];
385 /* copy to secondary buffer which will be affected */
386 strcpy(tmp_buf
, buf
);
388 /* separate into argv */
389 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
391 /* do the completion and return the possible completions */
392 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
394 /* no match; bell and out */
396 if (argc
> 1) /* allow tab for non command */
406 if (i
== 1) { /* one match; perfect */
407 k
= strlen(argv
[argc
- 1]);
412 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
413 k
= strlen(argv
[argc
- 1]);
423 /* make sure it fits */
424 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
430 for (i
= 0; i
< len
; i
++)
433 for (i
= 0; i
< seplen
; i
++)
444 print_argv(NULL
, " ", " ", 78, cmdv
);
452 #endif /* CONFIG_AUTO_COMPLETE */
457 * Call a command function. This should be the only route in U-Boot to call
458 * a command, so that we can track whether we are waiting for input or
459 * executing a command.
461 * @param cmdtp Pointer to the command to execute
462 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
463 * @param argc Number of arguments (arg 0 must be the command text)
464 * @param argv Arguments
465 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
467 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
469 command_ret_t result
;
471 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
472 if (result
!= CMD_RET_SUCCESS
)
473 debug("Command failed, result=%d\n", result
);
477 command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
478 uint_fast8_t *repeatable
)
480 command_ret_t rc
= CMD_RET_SUCCESS
;
483 /* Look up command in command table */
484 cmdtp
= find_cmd(argv
[0]);
486 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
487 return CMD_RET_FAILURE
;
490 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
491 return CMD_RET_FAILURE
;
494 /* found - check max args */
495 if (argc
> cmdtp
->maxargs
)
498 #if defined(CONFIG_CMD_BOOTD)
499 /* avoid "bootd" recursion */
500 else if (cmdtp
->cmd
== do_bootd
) {
501 if (flag
& CMD_FLAG_BOOTD
) {
502 my_puts_P(PSTR("'bootd' recursion detected\n"));
503 rc
= CMD_RET_FAILURE
;
505 flag
|= CMD_FLAG_BOOTD
;
510 /* If OK so far, then do the command */
512 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
513 *repeatable
&= cmdtp
->repeatable
;
515 if (rc
== CMD_RET_USAGE
)
516 rc
= cmd_usage(cmdtp
);
520 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
522 char buf
[strlen_P(cmdtp
->name
) + 1];
523 strcpy_P(buf
, cmdtp
->name
);
526 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);