]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/command.c
Find subcommands without prefix
[z180-stamp.git] / avr / command.c
1 /*
2 * (C) Copyright 2014, 2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * (C) Copyright 2000-2009
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10 /*
11 * Command Processor Table
12 */
13
14 #include "command.h"
15 #include "common.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <setjmp.h>
20
21 #include "config.h"
22 #include "print-utils.h"
23 #include "con-utils.h"
24 #include "env.h"
25 #include "debug.h"
26
27
28 jmp_buf cmd_jbuf;
29
30
31 static void print_usage_line(const FLASH char *name, int width,
32 const FLASH char *usage)
33 {
34 width -= strlen_P(name);
35 if (width < 0)
36 width = 0;
37 my_puts_P(name);
38 print_blanks(width);
39 my_puts_P(PSTR(" - "));
40 my_puts_P(usage);
41 my_puts_P(PSTR("\n"));
42 }
43
44 int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
45 {
46 unsigned char c1, c2;
47
48 while ((c1 = *(const FLASH unsigned char *)s1++)
49 == (c2 = *(const FLASH unsigned char *)s2++))
50 if (c1 == 0)
51 return 0;
52
53 return c1 - c2;
54 }
55
56 int cmpstring_PP(const void *p1, const void *p2)
57 {
58 return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name,
59 (*(const FLASH cmd_tbl_t **) p2)->name);
60 }
61
62 /****************************************************************************/
63
64 int cmd_tbl_item_count(cmd_tbl_t *p)
65 {
66 int count = 0;
67
68 while (p->name != NULL) {
69 p++; count++;
70 }
71 return count;
72 }
73
74
75 cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t *cmdtp)
76 {
77 cmd_tbl_t *p = cmdtp;
78
79 while (p->name != NULL)
80 ++p;
81
82 return p->subcmd;
83 }
84
85 /*
86 * find command table entry for a command
87 */
88 cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
89 {
90 cmd_tbl_t *cmdtp;
91 cmd_tbl_t *cmdtp_temp = table; /*Init value */
92 int table_len = cmd_tbl_item_count(table);
93 size_t len;
94 uint_fast8_t n_found = 0;
95
96 char *optenv = getenv_str(PSTR("cmd"));
97 bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
98
99 if (!cmd)
100 return NULL;
101
102 len = strlen(cmd);
103
104 for (cmdtp = table;
105 cmdtp != table + table_len;
106 cmdtp++) {
107 if (strncmp_P(cmd, cmdtp->name, len) == 0 &&
108 (opt_debug || cmdtp->name[0] != '!')) {
109 if (len == strlen_P(cmdtp->name))
110 return cmdtp; /* full match */
111
112 cmdtp_temp = cmdtp; /* abbreviated command ? */
113 n_found++;
114 }
115 }
116 if (n_found == 1) /* exactly one match */
117 return cmdtp_temp;
118
119 return NULL; /* not found or ambiguous command */
120 }
121
122 cmd_tbl_t *find_cmd_sub(const char *cmd, cmd_tbl_t *table)
123 {
124 cmd_tbl_t *entry = NULL;
125
126 for (cmd_tbl_t *tp = get_cmd_tbl_base(table); tp->name && entry == NULL; tp++)
127 if (tp->subcmd && tp->flags & CTBL_SUBCMDAUTO)
128 entry = find_cmd(cmd, tp->subcmd);
129
130 return entry;
131 }
132
133 /*
134 * Use puts() instead of printf() to avoid printf buffer overflow
135 * for long help messages
136 */
137
138 command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
139 {
140 (void) flag;
141
142 cmd_tbl_t *tbl_start = get_cmd_tbl_base(cmdtp);
143
144 char *optenv = getenv_str(PSTR("cmd"));
145 bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
146
147 if (argc == 1) { /*show list of commands */
148 int cmd_items = cmd_tbl_item_count(tbl_start);
149 cmd_tbl_t *cmd_array[cmd_items];
150 cmd_tbl_t *tp = tbl_start;
151 uint_fast8_t max_len = 0;
152
153 /* Make array of commands from .uboot_cmd section */
154 for (int i = 0; i < cmd_items; i++) {
155 cmd_array[i] = tp++;
156 uint_fast8_t l = strlen_P(cmd_array[i]->name);
157 if (l > max_len)
158 max_len = l;
159 }
160
161 /* Sort command list */
162 qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
163
164 /* print short help (usage) */
165 for (int i = 0; i < cmd_items; i++) {
166 if (opt_debug || cmd_array[i]->name[0] != '!') {
167 const FLASH char *usage = cmd_array[i]->usage;
168
169 /* allow user abort */
170 if (ctrlc ())
171 return CMD_RET_FAILURE;
172 if (usage == NULL)
173 continue;
174 #if defined(GCC_BUG_61443) || 1
175 print_usage_line(cmd_array[i]->name, max_len, usage);
176 #else
177 printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
178 cmd_array[i]->name, usage);
179 #endif
180 }
181 }
182 return CMD_RET_SUCCESS;
183 }
184
185 /*
186 * command help (long version)
187 */
188 for (uint8_t i = 1; i < argc; ++i) {
189 if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL &&
190 (opt_debug || cmdtp->name[0] != '!')) {
191 cmd_usage(cmdtp);
192 } else {
193 printf_P(PSTR("Unknown command '%s' - try 'help'"
194 " without arguments.\n\n"), argv[i]
195 );
196 return CMD_RET_FAILURE;
197 }
198 }
199
200 return CMD_RET_SUCCESS;
201 }
202
203
204 command_ret_t cmd_usage(const FLASH cmd_tbl_t *cmdtp)
205 {
206 // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
207 print_usage_line(cmdtp->name, 0, cmdtp->usage);
208 #if 0
209 my_puts_P(cmdtp->name);
210 print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp->name));
211 my_puts_P(PSTR(" - "));
212 my_puts_P(cmdtp->usage);
213 my_puts_P(PSTR("\n\n"));
214 #endif
215 #ifdef CONFIG_SYS_LONGHELP
216 // printf("Usage:\n%s ", cmdtp->name);
217 my_puts_P(PSTR("Usage:\n"));
218 my_puts_P(cmdtp->name);
219 my_puts_P(PSTR(" "));
220
221 if (!cmdtp->help) {
222 my_puts_P(PSTR(" - No additional help available.\n"));
223 return CMD_RET_FAILURE;
224 }
225
226 my_puts_P(cmdtp->help);
227 my_puts_P(PSTR("\n"));
228 #endif /* CONFIG_SYS_LONGHELP */
229 return CMD_RET_FAILURE;
230 }
231
232 #ifdef CONFIG_AUTO_COMPLETE
233
234 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
235 {
236 static char tmp_buf[CONFIG_SYS_CBSIZE];
237 int space;
238
239 space = last_char == '\0' || isblank(last_char);
240
241 if (space && argc == 1)
242 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
243
244 if (!space && argc == 2)
245 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
246
247 return 0;
248 }
249
250 /*************************************************************************************/
251
252 /* TODO: cmdtp points to FLASH */
253
254 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
255 {
256 cmd_tbl_t *cmdtp = cmd_tbl;
257 // const int count = ARRAY_SIZE(cmd_tbl);
258 // const cmd_tbl_t *cmdend = cmdtp + count;
259 // const char *p;
260 int len, clen;
261 int n_found = 0;
262 const char *cmd;
263
264 /* sanity? */
265 if (maxv < 2)
266 return -2;
267
268 cmdv[0] = NULL;
269
270 if (argc == 0) {
271 /* output full list of commands */
272 for (; cmdtp->name[0] != '\0'; cmdtp++) {
273 if (n_found >= maxv - 2) {
274 cmdv[n_found++] = "...";
275 break;
276 }
277 cmdv[n_found++] = cmdtp->name;
278 }
279 cmdv[n_found] = NULL;
280 return n_found;
281 }
282
283 /* more than one arg or one but the start of the next */
284 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
285 cmdtp = find_cmd(argv[0], cmd_tbl);
286 if (cmdtp == NULL || cmdtp->complete == NULL) {
287 cmdv[0] = NULL;
288 return 0;
289 }
290 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
291 }
292
293 cmd = argv[0];
294
295 len = strlen(cmd);
296
297 /* return the partial matches */
298 for (; cmdtp->name[0] != '\0'; cmdtp++) {
299
300 clen = strlen(cmdtp->name);
301 if (clen < len)
302 continue;
303
304 if (memcmp(cmd, cmdtp->name, len) != 0)
305 continue;
306
307 /* too many! */
308 if (n_found >= maxv - 2) {
309 cmdv[n_found++] = "...";
310 break;
311 }
312
313 cmdv[n_found++] = cmdtp->name;
314 }
315
316 cmdv[n_found] = NULL;
317 return n_found;
318 }
319
320 static int make_argv(char *s, int argvsz, char *argv[])
321 {
322 int argc = 0;
323
324 /* split into argv */
325 while (argc < argvsz - 1) {
326
327 /* skip any white space */
328 while (isblank(*s))
329 ++s;
330
331 if (*s == '\0') /* end of s, no more args */
332 break;
333
334 argv[argc++] = s; /* begin of argument string */
335
336 /* find end of string */
337 while (*s && !isblank(*s))
338 ++s;
339
340 if (*s == '\0') /* end of s, no more args */
341 break;
342
343 *s++ = '\0'; /* terminate current arg */
344 }
345 argv[argc] = NULL;
346
347 return argc;
348 }
349
350 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
351 {
352 int ll = leader != NULL ? strlen(leader) : 0;
353 int sl = sep != NULL ? strlen(sep) : 0;
354 int len, i;
355
356 if (banner) {
357 my_puts_P(PSTR("\n"));
358 my_puts(banner);
359 }
360
361 i = linemax; /* force leader and newline */
362 while (*argv != NULL) {
363 len = strlen(*argv) + sl;
364 if (i + len >= linemax) {
365 my_puts_P(PSTR("\n"));
366 if (leader)
367 my_puts(leader);
368 i = ll - sl;
369 } else if (sep)
370 my_puts(sep);
371 my_puts(*argv++);
372 i += len;
373 }
374 my_puts_P(PSTR("\n"));
375 }
376
377 static int find_common_prefix(char * const argv[])
378 {
379 int i, len;
380 char *anchor, *s, *t;
381
382 if (*argv == NULL)
383 return 0;
384
385 /* begin with max */
386 anchor = *argv++;
387 len = strlen(anchor);
388 while ((t = *argv++) != NULL) {
389 s = anchor;
390 for (i = 0; i < len; i++, t++, s++) {
391 if (*t != *s)
392 break;
393 }
394 len = s - anchor;
395 }
396 return len;
397 }
398
399 static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
400
401
402 int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp)
403 {
404 int n = *np, col = *colp;
405 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
406 char *cmdv[20];
407 char *s, *t;
408 const char *sep;
409 int i, j, k, len, seplen, argc;
410 int cnt;
411 char last_char;
412
413 if (strcmp_PP(prompt, CONFIG_SYS_PROMPT) != 0)
414 return 0; /* not in normal console */
415
416 cnt = strlen(buf);
417 if (cnt >= 1)
418 last_char = buf[cnt - 1];
419 else
420 last_char = '\0';
421
422 /* copy to secondary buffer which will be affected */
423 strcpy(tmp_buf, buf);
424
425 /* separate into argv */
426 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
427
428 /* do the completion and return the possible completions */
429 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
430
431 /* no match; bell and out */
432 if (i == 0) {
433 if (argc > 1) /* allow tab for non command */
434 return 0;
435 putchar('\a');
436 return 1;
437 }
438
439 s = NULL;
440 len = 0;
441 sep = NULL;
442 seplen = 0;
443 if (i == 1) { /* one match; perfect */
444 k = strlen(argv[argc - 1]);
445 s = cmdv[0] + k;
446 len = strlen(s);
447 sep = " ";
448 seplen = 1;
449 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
450 k = strlen(argv[argc - 1]);
451 j -= k;
452 if (j > 0) {
453 s = cmdv[0] + k;
454 len = j;
455 }
456 }
457
458 if (s != NULL) {
459 k = len + seplen;
460 /* make sure it fits */
461 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
462 putchar('\a');
463 return 1;
464 }
465
466 t = buf + cnt;
467 for (i = 0; i < len; i++)
468 *t++ = *s++;
469 if (sep != NULL)
470 for (i = 0; i < seplen; i++)
471 *t++ = sep[i];
472 *t = '\0';
473 n += k;
474 col += k;
475 my_puts(t - k);
476 if (sep == NULL)
477 putchar('\a');
478 *np = n;
479 *colp = col;
480 } else {
481 print_argv(NULL, " ", " ", 78, cmdv);
482
483 my_puts_P(prompt);
484 my_puts(buf);
485 }
486 return 1;
487 }
488
489 #endif /* CONFIG_AUTO_COMPLETE */
490
491
492
493 /**
494 * Call a command function. This should be the only route in U-Boot to call
495 * a command, so that we can track whether we are waiting for input or
496 * executing a command.
497 *
498 * @param cmdtp Pointer to the command to execute
499 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
500 * @param argc Number of arguments (arg 0 must be the command text)
501 * @param argv Arguments
502 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
503 */
504 command_ret_t cmd_call(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
505 {
506 command_ret_t result;
507
508 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
509 // if (result != CMD_RET_SUCCESS)
510 // debug("Command failed, result=%d\n", result);
511 return result;
512 }
513
514 #pragma GCC diagnostic ignored "-Wclobbered"
515
516 command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
517 uint_fast8_t *repeatable)
518 {
519 command_ret_t rc = CMD_RET_SUCCESS;
520 cmd_tbl_t *cmdtp;
521
522 /* Look up command in command table */
523 cmdtp = find_cmd(argv[0], cmd_tbl);
524 if (cmdtp != NULL) {
525 /* Check if this command has subcommands */
526 if (cmdtp->subcmd && argc > 1) {
527
528 /* Look up subcommand in subcommand table */
529 cmd_tbl_t *cmdtpsub = find_cmd(argv[1], cmdtp->subcmd);
530 if (cmdtpsub == NULL) {
531 printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv[0], argv[1], argv[0]);
532 return CMD_RET_FAILURE;
533 }
534 cmdtp = cmdtpsub;
535 --argc;
536 ++argv;
537 }
538 } else {
539 /* Search subcommands */
540 cmdtp = find_cmd_sub(argv[0], cmd_tbl);
541 }
542
543 if (cmdtp == NULL) {
544 printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
545 return CMD_RET_FAILURE;
546 }
547
548 /* found - check max args */
549 if (argc > cmdtp->maxargs)
550 rc = CMD_RET_USAGE;
551
552 #if defined(CONFIG_CMD_BOOTD)
553 /* avoid "bootd" recursion */
554 else if (cmdtp->cmd == do_bootd) {
555 if (flag & CMD_FLAG_BOOTD) {
556 my_puts_P(PSTR("'bootd' recursion detected\n"));
557 rc = CMD_RET_FAILURE;
558 } else {
559 flag |= CMD_FLAG_BOOTD;
560 }
561 }
562 #endif
563
564 if (setjmp(cmd_jbuf) != 0)
565 return CMD_RET_FAILURE;
566
567 /* If OK so far, then do the command */
568 if (!rc) {
569 rc = cmd_call(cmdtp, flag, argc, argv);
570 *repeatable &= (cmdtp->flags & CTBL_REPEAT) != 0;
571 }
572 if (rc == CMD_RET_USAGE)
573 rc = cmd_usage(cmdtp);
574 return rc;
575 }
576
577 int cmd_process_error(cmd_tbl_t *cmdtp, int err)
578 {
579 char buf[strlen_P(cmdtp->name) + 1];
580 strcpy_P(buf, cmdtp->name);
581
582 if (err) {
583 printf_P(PSTR("Command '%s' failed: Error %d\n"), buf, err);
584 return 1;
585 }
586
587 return 0;
588 }