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