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