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