]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
89e88695c2289a5684cfc23f2faae05b3a3a80bb
3 * Command Processor Table
15 #include "con-utils.h"
21 static void print_blanks(int_fast8_t count
)
27 static void print_usage_line(const FLASH
char *name
, const FLASH
char *usage
)
30 print_blanks(CONFIG_SYS_MAXARGS
- strlen_P(name
));
31 my_puts_P(PSTR(" - "));
33 my_puts_P(PSTR("\n"));
36 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
40 while ((c1
= *(const FLASH
unsigned char *)s1
++)
41 == (c2
= *(const FLASH
unsigned char *)s2
++))
48 int cmpstringp(const void *p1
, const void *p2
)
50 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
51 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
54 int cmd_tbl_item_count(void)
56 cmd_tbl_t
* p
= cmd_tbl
;
59 while (p
->name
!= NULL
) {
66 * Use puts() instead of printf() to avoid printf buffer overflow
67 * for long help messages
70 int _do_help(cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
,
71 int flag
, int argc
, char * const argv
[])
78 if (argc
== 1) { /*show list of commands */
79 cmd_tbl_t
*cmd_array
[cmd_items
];
82 /* Make array of commands from .uboot_cmd section */
84 for (i
= 0; i
< cmd_items
; i
++) {
85 cmd_array
[i
] = cmdtp
++;
88 /* Sort command list */
89 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstringp
);
91 /* print short help (usage) */
92 for (i
= 0; i
< cmd_items
; i
++) {
93 const FLASH
char *usage
= cmd_array
[i
]->usage
;
95 /* allow user abort */
101 print_usage_line(cmd_array
[i
]->name
, usage
);
103 printf_P(PSTR("%-" stringify(CONFIG_SYS_MAXARGS
) "S - %S\n"),
104 cmd_array
[i
]->name
, usage
);
110 * command help (long version)
112 for (i
= 1; i
< argc
; ++i
) {
113 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
114 rcode
|= cmd_usage(cmdtp
);
116 printf_P(PSTR("Unknown command '%s' - try 'help'"
117 " without arguments.\n\n"), argv
[i
]
125 /***************************************************************************
126 * find command table entry for a command
128 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
131 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
133 uint_fast8_t n_found
= 0;
141 cmdtp
!= table
+ table_len
;
143 if (strncmp_P (cmd
, cmdtp
->name
, len
) == 0) {
144 if (len
== strlen (cmdtp
->name
))
145 return cmdtp
; /* full match */
147 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
151 if (n_found
== 1) { /* exactly one match */
155 return NULL
; /* not found or ambiguous command */
159 cmd_tbl_t
*find_cmd (const char *cmd
)
161 return find_cmd_tbl(cmd
, cmd_tbl
, cmd_tbl_item_count());
165 int cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
167 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
168 print_usage_line(cmdtp
->name
, cmdtp
->usage
);
170 my_puts_P(cmdtp
->name
);
171 print_blanks(CONFIG_SYS_MAXARGS
- strlen_P(cmdtp
->name
));
172 my_puts_P(PSTR(" - "));
173 my_puts_P(cmdtp
->usage
);
174 my_puts_P(PSTR("\n\n"));
176 #ifdef CONFIG_SYS_LONGHELP
177 // printf("Usage:\n%s ", cmdtp->name);
178 my_puts_P(PSTR("Usage:\n"));
179 my_puts_P(cmdtp
->name
);
183 my_puts_P(PSTR(" - No additional help available.\n"));
187 my_puts_P(cmdtp
->help
);
188 my_puts_P(PSTR("\n"));
189 #endif /* CONFIG_SYS_LONGHELP */
193 #ifdef CONFIG_AUTO_COMPLETE
195 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
197 static char tmp_buf
[512];
200 space
= last_char
== '\0' || isblank(last_char
);
202 if (space
&& argc
== 1)
203 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
205 if (!space
&& argc
== 2)
206 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
211 /*************************************************************************************/
213 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
215 cmd_tbl_t
*cmdtp
= cmd_tbl
;
216 // const int count = ARRAY_SIZE(cmd_tbl);
217 // const cmd_tbl_t *cmdend = cmdtp + count;
230 /* output full list of commands */
231 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
232 if (n_found
>= maxv
- 2) {
233 cmdv
[n_found
++] = "...";
236 cmdv
[n_found
++] = cmdtp
->name
;
238 cmdv
[n_found
] = NULL
;
242 /* more than one arg or one but the start of the next */
243 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
244 cmdtp
= find_cmd(argv
[0]);
245 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
249 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
256 /* return the partial matches */
257 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
259 clen
= strlen(cmdtp
->name
);
263 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
267 if (n_found
>= maxv
- 2) {
268 cmdv
[n_found
++] = "...";
272 cmdv
[n_found
++] = cmdtp
->name
;
275 cmdv
[n_found
] = NULL
;
279 static int make_argv(char *s
, int argvsz
, char *argv
[])
283 /* split into argv */
284 while (argc
< argvsz
- 1) {
286 /* skip any white space */
290 if (*s
== '\0') /* end of s, no more args */
293 argv
[argc
++] = s
; /* begin of argument string */
295 /* find end of string */
296 while (*s
&& !isblank(*s
))
299 if (*s
== '\0') /* end of s, no more args */
302 *s
++ = '\0'; /* terminate current arg */
309 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
311 int ll
= leader
!= NULL
? strlen(leader
) : 0;
312 int sl
= sep
!= NULL
? strlen(sep
) : 0;
316 my_puts_P(PSTR("\n"));
320 i
= linemax
; /* force leader and newline */
321 while (*argv
!= NULL
) {
322 len
= strlen(*argv
) + sl
;
323 if (i
+ len
>= linemax
) {
324 my_puts_P(PSTR("\n"));
333 my_puts_P(PSTR("\n"));
336 static int find_common_prefix(char * const argv
[])
339 char *anchor
, *s
, *t
;
346 len
= strlen(anchor
);
347 while ((t
= *argv
++) != NULL
) {
349 for (i
= 0; i
< len
; i
++, t
++, s
++) {
358 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
360 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
362 int n
= *np
, col
= *colp
;
363 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
367 int i
, j
, k
, len
, seplen
, argc
;
371 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
372 return 0; /* not in normal console */
376 last_char
= buf
[cnt
- 1];
380 /* copy to secondary buffer which will be affected */
381 strcpy(tmp_buf
, buf
);
383 /* separate into argv */
384 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
386 /* do the completion and return the possible completions */
387 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
389 /* no match; bell and out */
391 if (argc
> 1) /* allow tab for non command */
401 if (i
== 1) { /* one match; perfect */
402 k
= strlen(argv
[argc
- 1]);
407 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
408 k
= strlen(argv
[argc
- 1]);
418 /* make sure it fits */
419 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
425 for (i
= 0; i
< len
; i
++)
428 for (i
= 0; i
< seplen
; i
++)
439 print_argv(NULL
, " ", " ", 78, cmdv
);
447 #endif /* CONFIG_AUTO_COMPLETE */
452 * Call a command function. This should be the only route in U-Boot to call
453 * a command, so that we can track whether we are waiting for input or
454 * executing a command.
456 * @param cmdtp Pointer to the command to execute
457 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
458 * @param argc Number of arguments (arg 0 must be the command text)
459 * @param argv Arguments
460 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
462 static int cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
466 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
468 debug("Command failed, result=%d\n", result
);
472 enum command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
473 uint_fast8_t *repeatable
)
475 enum command_ret_t rc
= CMD_RET_SUCCESS
;
478 /* Look up command in command table */
479 cmdtp
= find_cmd(argv
[0]);
481 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
485 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
489 /* found - check max args */
490 if (argc
> cmdtp
->maxargs
)
493 #if defined(CONFIG_CMD_BOOTD)
494 /* avoid "bootd" recursion */
495 else if (cmdtp
->cmd
== do_bootd
) {
496 if (flag
& CMD_FLAG_BOOTD
) {
497 my_puts_P(PSTR("'bootd' recursion detected\n"));
498 rc
= CMD_RET_FAILURE
;
500 flag
|= CMD_FLAG_BOOTD
;
505 /* If OK so far, then do the command */
507 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
508 *repeatable
&= cmdtp
->repeatable
;
510 if (rc
== CMD_RET_USAGE
)
511 rc
= cmd_usage(cmdtp
);
515 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
517 char buf
[strlen_P(cmdtp
->name
) + 1];
518 strcpy_P(buf
, cmdtp
->name
);
521 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);