]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
ad39006716453104ee353d82185cd9ad7ed9b277
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
);
182 my_puts_P(PSTR(" - No additional help available.\n"));
186 my_puts_P(cmdtp
->help
);
187 my_puts_P(PSTR("\n"));
188 #endif /* CONFIG_SYS_LONGHELP */
192 #ifdef CONFIG_AUTO_COMPLETE
194 int var_complete(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
196 static char tmp_buf
[512];
199 space
= last_char
== '\0' || isblank(last_char
);
201 if (space
&& argc
== 1)
202 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
204 if (!space
&& argc
== 2)
205 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
210 /*************************************************************************************/
212 static int complete_cmdv(int argc
, char * const argv
[], char last_char
, int maxv
, char *cmdv
[])
214 cmd_tbl_t
*cmdtp
= cmd_tbl
;
215 // const int count = ARRAY_SIZE(cmd_tbl);
216 // const cmd_tbl_t *cmdend = cmdtp + count;
229 /* output full list of commands */
230 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
231 if (n_found
>= maxv
- 2) {
232 cmdv
[n_found
++] = "...";
235 cmdv
[n_found
++] = cmdtp
->name
;
237 cmdv
[n_found
] = NULL
;
241 /* more than one arg or one but the start of the next */
242 if (argc
> 1 || (last_char
== '\0' || isblank(last_char
))) {
243 cmdtp
= find_cmd(argv
[0]);
244 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
248 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
255 /* return the partial matches */
256 for (; cmdtp
->name
[0] != '\0'; cmdtp
++) {
258 clen
= strlen(cmdtp
->name
);
262 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
266 if (n_found
>= maxv
- 2) {
267 cmdv
[n_found
++] = "...";
271 cmdv
[n_found
++] = cmdtp
->name
;
274 cmdv
[n_found
] = NULL
;
278 static int make_argv(char *s
, int argvsz
, char *argv
[])
282 /* split into argv */
283 while (argc
< argvsz
- 1) {
285 /* skip any white space */
289 if (*s
== '\0') /* end of s, no more args */
292 argv
[argc
++] = s
; /* begin of argument string */
294 /* find end of string */
295 while (*s
&& !isblank(*s
))
298 if (*s
== '\0') /* end of s, no more args */
301 *s
++ = '\0'; /* terminate current arg */
308 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char * const argv
[])
310 int ll
= leader
!= NULL
? strlen(leader
) : 0;
311 int sl
= sep
!= NULL
? strlen(sep
) : 0;
315 my_puts_P(PSTR("\n"));
319 i
= linemax
; /* force leader and newline */
320 while (*argv
!= NULL
) {
321 len
= strlen(*argv
) + sl
;
322 if (i
+ len
>= linemax
) {
323 my_puts_P(PSTR("\n"));
332 my_puts_P(PSTR("\n"));
335 static int find_common_prefix(char * const argv
[])
338 char *anchor
, *s
, *t
;
345 len
= strlen(anchor
);
346 while ((t
= *argv
++) != NULL
) {
348 for (i
= 0; i
< len
; i
++, t
++, s
++) {
357 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
359 int cmd_auto_complete(const FLASH
char *const prompt
, char *buf
, int *np
, int *colp
)
361 int n
= *np
, col
= *colp
;
362 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
366 int i
, j
, k
, len
, seplen
, argc
;
370 if (strcmp_PP(prompt
, CONFIG_SYS_PROMPT
) != 0)
371 return 0; /* not in normal console */
375 last_char
= buf
[cnt
- 1];
379 /* copy to secondary buffer which will be affected */
380 strcpy(tmp_buf
, buf
);
382 /* separate into argv */
383 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
385 /* do the completion and return the possible completions */
386 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
388 /* no match; bell and out */
390 if (argc
> 1) /* allow tab for non command */
400 if (i
== 1) { /* one match; perfect */
401 k
= strlen(argv
[argc
- 1]);
406 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
407 k
= strlen(argv
[argc
- 1]);
417 /* make sure it fits */
418 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
424 for (i
= 0; i
< len
; i
++)
427 for (i
= 0; i
< seplen
; i
++)
438 print_argv(NULL
, " ", " ", 78, cmdv
);
446 #endif /* CONFIG_AUTO_COMPLETE */
451 * Call a command function. This should be the only route in U-Boot to call
452 * a command, so that we can track whether we are waiting for input or
453 * executing a command.
455 * @param cmdtp Pointer to the command to execute
456 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
457 * @param argc Number of arguments (arg 0 must be the command text)
458 * @param argv Arguments
459 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
461 static int cmd_call(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
465 result
= (cmdtp
->cmd
)(cmdtp
, flag
, argc
, argv
);
467 debug("Command failed, result=%d\n", result
);
471 enum command_ret_t
cmd_process(int flag
, int argc
, char * const argv
[],
472 uint_fast8_t *repeatable
)
474 enum command_ret_t rc
= CMD_RET_SUCCESS
;
477 /* Look up command in command table */
478 cmdtp
= find_cmd(argv
[0]);
480 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv
[0]);
484 debug("### Command '%s' found, but ->cmd == NULL \n", argv
[0]);
488 /* found - check max args */
489 if (argc
> cmdtp
->maxargs
)
492 #if defined(CONFIG_CMD_BOOTD)
493 /* avoid "bootd" recursion */
494 else if (cmdtp
->cmd
== do_bootd
) {
495 if (flag
& CMD_FLAG_BOOTD
) {
496 my_puts_P(PSTR("'bootd' recursion detected\n"));
497 rc
= CMD_RET_FAILURE
;
499 flag
|= CMD_FLAG_BOOTD
;
504 /* If OK so far, then do the command */
506 rc
= cmd_call(cmdtp
, flag
, argc
, argv
);
507 *repeatable
&= cmdtp
->repeatable
;
509 if (rc
== CMD_RET_USAGE
)
510 rc
= cmd_usage(cmdtp
);
514 int cmd_process_error(cmd_tbl_t
*cmdtp
, int err
)
516 char buf
[strlen_P(cmdtp
->name
) + 1];
517 strcpy_P(buf
, cmdtp
->name
);
520 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf
, err
);