]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
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
, int width
,
28 const FLASH
char *usage
)
30 width
-= strlen_P(name
);
35 my_puts_P(PSTR(" - "));
37 my_puts_P(PSTR("\n"));
40 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
44 while ((c1
= *(const FLASH
unsigned char *)s1
++)
45 == (c2
= *(const FLASH
unsigned char *)s2
++))
52 int cmpstringp(const void *p1
, const void *p2
)
54 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
55 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
58 int cmd_tbl_item_count(void)
60 cmd_tbl_t
* p
= cmd_tbl
;
63 while (p
->name
!= NULL
) {
70 * Use puts() instead of printf() to avoid printf buffer overflow
71 * for long help messages
74 command_ret_t
_do_help(cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
,
75 int flag
, int argc
, char * const argv
[])
77 uint_fast8_t i
, max_len
= 0;
78 command_ret_t rcode
= CMD_RET_SUCCESS
;
82 if (argc
== 1) { /*show list of commands */
83 cmd_tbl_t
*cmd_array
[cmd_items
];
86 /* Make array of commands from .uboot_cmd section */
88 for (i
= 0; i
< cmd_items
; i
++) {
89 cmd_array
[i
] = cmdtp
++;
90 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
95 /* Sort command list */
96 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstringp
);
98 /* print short help (usage) */
99 for (i
= 0; i
< cmd_items
; i
++) {
100 const FLASH
char *usage
= cmd_array
[i
]->usage
;
102 /* allow user abort */
104 return CMD_RET_FAILURE
;
108 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
110 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
111 cmd_array
[i
]->name
, usage
);
114 return CMD_RET_SUCCESS
;
117 * command help (long version)
119 for (i
= 1; i
< argc
; ++i
) {
120 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
121 rcode
= cmd_usage(cmdtp
);
123 printf_P(PSTR("Unknown command '%s' - try 'help'"
124 " without arguments.\n\n"), argv
[i
]
126 rcode
= CMD_RET_FAILURE
;
132 /***************************************************************************
133 * find command table entry for a command
135 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
138 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
140 uint_fast8_t n_found
= 0;
148 cmdtp
!= table
+ table_len
;
150 if (strncmp_P (cmd
, cmdtp
->name
, len
) == 0) {
151 if (len
== strlen (cmdtp
->name
))
152 return cmdtp
; /* full match */
154 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
158 if (n_found
== 1) { /* exactly one match */
162 return NULL
; /* not found or ambiguous command */
166 cmd_tbl_t
*find_cmd (const char *cmd
)
168 return find_cmd_tbl(cmd
, cmd_tbl
, cmd_tbl_item_count());
172 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
174 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
175 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
177 my_puts_P(cmdtp
->name
);
178 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
179 my_puts_P(PSTR(" - "));
180 my_puts_P(cmdtp
->usage
);
181 my_puts_P(PSTR("\n\n"));
183 #ifdef CONFIG_SYS_LONGHELP
184 // printf("Usage:\n%s ", cmdtp->name);
185 my_puts_P(PSTR("Usage:\n"));
186 my_puts_P(cmdtp
->name
);
187 my_puts_P(PSTR(" "));
190 my_puts_P(PSTR(" - No additional help available.\n"));
191 return CMD_RET_FAILURE
;
194 my_puts_P(cmdtp
->help
);
195 my_puts_P(PSTR("\n"));
196 #endif /* CONFIG_SYS_LONGHELP */
197 return CMD_RET_FAILURE
;
200 #ifdef CONFIG_AUTO_COMPLETE
202 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
204 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
207 space
= last_char
== '\0' || isblank(last_char
);
209 if (space
&& argc
== 1)
210 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
212 if (!space
&& argc
== 2)
213 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
218 /*************************************************************************************/
220 /* TODO: cmdtp points to FLASH */
222 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
224 cmd_tbl_t
*cmdtp
= cmd_tbl
;
225 // const int count = ARRAY_SIZE(cmd_tbl);
226 // const cmd_tbl_t *cmdend = cmdtp + count;
239 /* output full list of commands */
240 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
241 if (n_found
>= maxv
- 2) {
242 cmdv
[n_found
++] = "...";
245 cmdv
[n_found
++] = cmdtp
->name
;
247 cmdv
[n_found
] = NULL
;
251 /* more than one arg or one but the start of the next */
252 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
253 cmdtp
= find_cmd(argv
[0]);
254 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
258 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
265 /* return the partial matches */
266 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
268 clen
= strlen(cmdtp
->name
);
272 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
276 if (n_found
>= maxv
- 2) {
277 cmdv
[n_found
++] = "...";
281 cmdv
[n_found
++] = cmdtp
->name
;
284 cmdv
[n_found
] = NULL
;
288 static int make_argv(char *s
, int argvsz
, char *argv
[])
292 /* split into argv */
293 while (argc
< argvsz
- 1) {
295 /* skip any white space */
299 if (*s
== '\0') /* end of s, no more args */
302 argv
[argc
++] = s
; /* begin of argument string */
304 /* find end of string */
305 while (*s
&& !isblank(*s
))
308 if (*s
== '\0') /* end of s, no more args */
311 *s
++ = '\0'; /* terminate current arg */
318 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
320 int ll
= leader
!= NULL
? strlen(leader
) : 0;
321 int sl
= sep
!= NULL
? strlen(sep
) : 0;
325 my_puts_P(PSTR("\n"));
329 i
= linemax
; /* force leader and newline */
330 while (*argv
!= NULL
) {
331 len
= strlen(*argv
) + sl
;
332 if (i
+ len
>= linemax
) {
333 my_puts_P(PSTR("\n"));
342 my_puts_P(PSTR("\n"));
345 static int find_common_prefix(char * const argv
[])
348 char *anchor
, *s
, *t
;
355 len
= strlen(anchor
);
356 while ((t
= *argv
++) != NULL
) {
358 for (i
= 0; i
< len
; i
++, t
++, s
++) {
367 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
370 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
372 int n
= *np
, col
= *colp
;
373 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
377 int i
, j
, k
, len
, seplen
, argc
;
381 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
382 return 0; /* not in normal console */
386 last_char
= buf
[cnt
- 1];
390 /* copy to secondary buffer which will be affected */
391 strcpy(tmp_buf
, buf
);
393 /* separate into argv */
394 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
396 /* do the completion and return the possible completions */
397 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
399 /* no match; bell and out */
401 if (argc
> 1) /* allow tab for non command */
411 if (i
== 1) { /* one match; perfect */
412 k
= strlen(argv
[argc
- 1]);
417 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
418 k
= strlen(argv
[argc
- 1]);
428 /* make sure it fits */
429 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
435 for (i
= 0; i
< len
; i
++)
438 for (i
= 0; i
< seplen
; i
++)
449 print_argv(NULL
, " ", " ", 78, cmdv
);
457 #endif /* CONFIG_AUTO_COMPLETE */
462 * Call a command function. This should be the only route in U-Boot to call
463 * a command, so that we can track whether we are waiting for input or
464 * executing a command.
466 * @param cmdtp Pointer to the command to execute
467 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
468 * @param argc Number of arguments (arg 0 must be the command text)
469 * @param argv Arguments
470 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
472 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
474 command_ret_t result
;
476 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
477 if (result
!= CMD_RET_SUCCESS
)
478 debug("Command failed, result=%d\n", result
);
482 command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
483 uint_fast8_t *repeatable
)
485 command_ret_t rc
= CMD_RET_SUCCESS
;
488 /* Look up command in command table */
489 cmdtp
= find_cmd(argv
[0]);
491 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
492 return CMD_RET_FAILURE
;
495 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
496 return CMD_RET_FAILURE
;
499 /* found - check max args */
500 if (argc
> cmdtp
->maxargs
)
503 #if defined(CONFIG_CMD_BOOTD)
504 /* avoid "bootd" recursion */
505 else if (cmdtp
->cmd
== do_bootd
) {
506 if (flag
& CMD_FLAG_BOOTD
) {
507 my_puts_P(PSTR("'bootd' recursion detected\n"));
508 rc
= CMD_RET_FAILURE
;
510 flag
|= CMD_FLAG_BOOTD
;
515 /* If OK so far, then do the command */
517 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
518 *repeatable
&= cmdtp
->repeatable
;
520 if (rc
== CMD_RET_USAGE
)
521 rc
= cmd_usage(cmdtp
);
525 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
527 char buf
[strlen_P(cmdtp
->name
) + 1];
528 strcpy_P(buf
, cmdtp
->name
);
531 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);