]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
bb887941e462eccc0552c7a1485cf0c928f813ed
2 * Command Processor Table
12 #include "print-utils.h"
13 #include "con-utils.h"
14 #ifdef CONFIG_AUTO_COMPLETE
21 static void print_usage_line(const FLASH
char *name
, int width
,
22 const FLASH
char *usage
)
24 width
-= strlen_P(name
);
29 my_puts_P(PSTR(" - "));
31 my_puts_P(PSTR("\n"));
34 int strcmp_PP(const FLASH
char *s1
, const FLASH
char *s2
)
38 while ((c1
= *(const FLASH
unsigned char *)s1
++)
39 == (c2
= *(const FLASH
unsigned char *)s2
++))
46 int cmpstringp(const void *p1
, const void *p2
)
48 return strcmp_PP((*(const FLASH cmd_tbl_t
**) p1
)->name
,
49 (*(const FLASH cmd_tbl_t
**) p2
)->name
);
52 int cmd_tbl_item_count(void)
54 cmd_tbl_t
* p
= cmd_tbl
;
57 while (p
->name
!= NULL
) {
64 * Use puts() instead of printf() to avoid printf buffer overflow
65 * for long help messages
68 command_ret_t
_do_help(cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
,
69 int flag
, int argc
, char * const argv
[])
71 uint_fast8_t i
, max_len
= 0;
72 command_ret_t rcode
= CMD_RET_SUCCESS
;
76 if (argc
== 1) { /*show list of commands */
77 cmd_tbl_t
*cmd_array
[cmd_items
];
80 /* Make array of commands from .uboot_cmd section */
82 for (i
= 0; i
< cmd_items
; i
++) {
83 cmd_array
[i
] = cmdtp
++;
84 uint_fast8_t l
= strlen_P(cmd_array
[i
]->name
);
89 /* Sort command list */
90 qsort(cmd_array
, cmd_items
, sizeof (cmd_tbl_t
*), cmpstringp
);
92 /* print short help (usage) */
93 for (i
= 0; i
< cmd_items
; i
++) {
94 const FLASH
char *usage
= cmd_array
[i
]->usage
;
96 /* allow user abort */
98 return CMD_RET_FAILURE
;
102 print_usage_line(cmd_array
[i
]->name
, max_len
, usage
);
104 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
105 cmd_array
[i
]->name
, usage
);
108 return CMD_RET_SUCCESS
;
111 * command help (long version)
113 for (i
= 1; i
< argc
; ++i
) {
114 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
115 rcode
= cmd_usage(cmdtp
);
117 printf_P(PSTR("Unknown command '%s' - try 'help'"
118 " without arguments.\n\n"), argv
[i
]
120 rcode
= CMD_RET_FAILURE
;
126 /***************************************************************************
127 * find command table entry for a command
129 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
132 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
134 uint_fast8_t n_found
= 0;
142 cmdtp
!= table
+ table_len
;
144 if (strncmp_P (cmd
, cmdtp
->name
, len
) == 0) {
145 if (len
== strlen (cmdtp
->name
))
146 return cmdtp
; /* full match */
148 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
152 if (n_found
== 1) { /* exactly one match */
156 return NULL
; /* not found or ambiguous command */
160 cmd_tbl_t
*find_cmd (const char *cmd
)
162 return find_cmd_tbl(cmd
, cmd_tbl
, cmd_tbl_item_count());
166 command_ret_t
cmd_usage(const FLASH cmd_tbl_t
*cmdtp
)
168 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
169 print_usage_line(cmdtp
->name
, 0, cmdtp
->usage
);
171 my_puts_P(cmdtp
->name
);
172 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp
->name
));
173 my_puts_P(PSTR(" - "));
174 my_puts_P(cmdtp
->usage
);
175 my_puts_P(PSTR("\n\n"));
177 #ifdef CONFIG_SYS_LONGHELP
178 // printf("Usage:\n%s ", cmdtp->name);
179 my_puts_P(PSTR("Usage:\n"));
180 my_puts_P(cmdtp
->name
);
181 my_puts_P(PSTR(" "));
184 my_puts_P(PSTR(" - No additional help available.\n"));
185 return CMD_RET_FAILURE
;
188 my_puts_P(cmdtp
->help
);
189 my_puts_P(PSTR("\n"));
190 #endif /* CONFIG_SYS_LONGHELP */
191 return CMD_RET_FAILURE
;
194 #ifdef CONFIG_AUTO_COMPLETE
196 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
198 static char tmp_buf
[CONFIG_SYS_CBSIZE
];
201 space
= last_char
== '\0' || isblank(last_char
);
203 if (space
&& argc
== 1)
204 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
206 if (!space
&& argc
== 2)
207 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
212 /*************************************************************************************/
214 /* TODO: cmdtp points to FLASH */
216 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
218 cmd_tbl_t
*cmdtp
= cmd_tbl
;
219 // const int count = ARRAY_SIZE(cmd_tbl);
220 // const cmd_tbl_t *cmdend = cmdtp + count;
233 /* output full list of commands */
234 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
235 if (n_found
>= maxv
- 2) {
236 cmdv
[n_found
++] = "...";
239 cmdv
[n_found
++] = cmdtp
->name
;
241 cmdv
[n_found
] = NULL
;
245 /* more than one arg or one but the start of the next */
246 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
247 cmdtp
= find_cmd(argv
[0]);
248 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
252 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
259 /* return the partial matches */
260 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
262 clen
= strlen(cmdtp
->name
);
266 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
270 if (n_found
>= maxv
- 2) {
271 cmdv
[n_found
++] = "...";
275 cmdv
[n_found
++] = cmdtp
->name
;
278 cmdv
[n_found
] = NULL
;
282 static int make_argv(char *s
, int argvsz
, char *argv
[])
286 /* split into argv */
287 while (argc
< argvsz
- 1) {
289 /* skip any white space */
293 if (*s
== '\0') /* end of s, no more args */
296 argv
[argc
++] = s
; /* begin of argument string */
298 /* find end of string */
299 while (*s
&& !isblank(*s
))
302 if (*s
== '\0') /* end of s, no more args */
305 *s
++ = '\0'; /* terminate current arg */
312 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
314 int ll
= leader
!= NULL
? strlen(leader
) : 0;
315 int sl
= sep
!= NULL
? strlen(sep
) : 0;
319 my_puts_P(PSTR("\n"));
323 i
= linemax
; /* force leader and newline */
324 while (*argv
!= NULL
) {
325 len
= strlen(*argv
) + sl
;
326 if (i
+ len
>= linemax
) {
327 my_puts_P(PSTR("\n"));
336 my_puts_P(PSTR("\n"));
339 static int find_common_prefix(char * const argv
[])
342 char *anchor
, *s
, *t
;
349 len
= strlen(anchor
);
350 while ((t
= *argv
++) != NULL
) {
352 for (i
= 0; i
< len
; i
++, t
++, s
++) {
361 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
364 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
366 int n
= *np
, col
= *colp
;
367 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
371 int i
, j
, k
, len
, seplen
, argc
;
375 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
376 return 0; /* not in normal console */
380 last_char
= buf
[cnt
- 1];
384 /* copy to secondary buffer which will be affected */
385 strcpy(tmp_buf
, buf
);
387 /* separate into argv */
388 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
390 /* do the completion and return the possible completions */
391 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
393 /* no match; bell and out */
395 if (argc
> 1) /* allow tab for non command */
405 if (i
== 1) { /* one match; perfect */
406 k
= strlen(argv
[argc
- 1]);
411 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
412 k
= strlen(argv
[argc
- 1]);
422 /* make sure it fits */
423 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
429 for (i
= 0; i
< len
; i
++)
432 for (i
= 0; i
< seplen
; i
++)
443 print_argv(NULL
, " ", " ", 78, cmdv
);
451 #endif /* CONFIG_AUTO_COMPLETE */
456 * Call a command function. This should be the only route in U-Boot to call
457 * a command, so that we can track whether we are waiting for input or
458 * executing a command.
460 * @param cmdtp Pointer to the command to execute
461 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
462 * @param argc Number of arguments (arg 0 must be the command text)
463 * @param argv Arguments
464 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
466 command_ret_t
cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
468 command_ret_t result
;
470 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
471 if (result
!= CMD_RET_SUCCESS
)
472 debug("Command failed, result=%d\n", result
);
476 command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
477 uint_fast8_t *repeatable
)
479 command_ret_t rc
= CMD_RET_SUCCESS
;
482 /* Look up command in command table */
483 cmdtp
= find_cmd(argv
[0]);
485 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
486 return CMD_RET_FAILURE
;
489 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
490 return CMD_RET_FAILURE
;
493 /* found - check max args */
494 if (argc
> cmdtp
->maxargs
)
497 #if defined(CONFIG_CMD_BOOTD)
498 /* avoid "bootd" recursion */
499 else if (cmdtp
->cmd
== do_bootd
) {
500 if (flag
& CMD_FLAG_BOOTD
) {
501 my_puts_P(PSTR("'bootd' recursion detected\n"));
502 rc
= CMD_RET_FAILURE
;
504 flag
|= CMD_FLAG_BOOTD
;
509 /* If OK so far, then do the command */
511 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
512 *repeatable
&= cmdtp
->repeatable
;
514 if (rc
== CMD_RET_USAGE
)
515 rc
= cmd_usage(cmdtp
);
519 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
521 char buf
[strlen_P(cmdtp
->name
) + 1];
522 strcpy_P(buf
, cmdtp
->name
);
525 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);