]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/command.c
New debug command: dump heap
[z180-stamp.git] / avr / command.c
index bb887941e462eccc0552c7a1485cf0c928f813ed..aa784cbca637246810ecbf9eb056830f29cc5b12 100644 (file)
@@ -1,21 +1,31 @@
+/*
+ * (C) Copyright 2014, 2016 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * (C) Copyright 2000-2009
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
 /*
  *  Command Processor Table
  */
 
+#include "command.h"
 #include "common.h"
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
-#include <stdio.h>
+#include <setjmp.h>
 
 #include "config.h"
 #include "print-utils.h"
 #include "con-utils.h"
-#ifdef CONFIG_AUTO_COMPLETE
 #include "env.h"
-#endif
 #include "debug.h"
-#include "command.h"
+
+
+jmp_buf cmd_jbuf;
 
 
 static void print_usage_line(const FLASH char *name, int width,
@@ -35,7 +45,7 @@ int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
 {
        unsigned char c1, c2;
 
-       while ((c1 = *(const FLASH unsigned char *)s1++) 
+       while ((c1 = *(const FLASH unsigned char *)s1++)
                        == (c2 = *(const FLASH unsigned char *)s2++))
                if (c1 == 0)
                        return 0;
@@ -43,15 +53,16 @@ int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
        return c1 - c2;
 }
 
-int cmpstringp(const void *p1, const void *p2)
+int cmpstring_PP(const void *p1, const void *p2)
 {
-       return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name, 
+       return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name,
                         (*(const FLASH cmd_tbl_t **) p2)->name);
 }
 
-int cmd_tbl_item_count(void)
+/****************************************************************************/
+
+int cmd_tbl_item_count(cmd_tbl_t *p)
 {
-       cmd_tbl_t * p = cmd_tbl;
        int count = 0;
 
        while (p->name != NULL) {
@@ -60,79 +71,31 @@ int cmd_tbl_item_count(void)
        return count;
 }
 
-/*
- * Use puts() instead of printf() to avoid printf buffer overflow
- * for long help messages
- */
 
-command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
-               int flag, int argc, char * const argv[])
+cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t  *cmdtp)
 {
-       uint_fast8_t i, max_len = 0;
-       command_ret_t rcode = CMD_RET_SUCCESS;
+       cmd_tbl_t *p = cmdtp;
 
-       (void) flag;
-
-       if (argc == 1) {        /*show list of commands */
-               cmd_tbl_t *cmd_array[cmd_items];
-               int i;
-
-               /* Make array of commands from .uboot_cmd section */
-               cmdtp = cmd_start;
-               for (i = 0; i < cmd_items; i++) {
-                       cmd_array[i] = cmdtp++;
-                       uint_fast8_t l = strlen_P(cmd_array[i]->name);
-                       if (l > max_len)
-                               max_len = l;
-               }
+       while (p->name != NULL)
+               ++p;
 
-               /* Sort command list */
-               qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstringp);
-
-               /* print short help (usage) */
-               for (i = 0; i < cmd_items; i++) {
-                       const FLASH char *usage = cmd_array[i]->usage;
-
-                       /* allow user abort */
-                       if (ctrlc ())
-                               return CMD_RET_FAILURE;
-                       if (usage == NULL)
-                               continue;
-#ifdef GCC_BUG_61443 
-                       print_usage_line(cmd_array[i]->name, max_len, usage);
-#else
-                       printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"), 
-                                       cmd_array[i]->name, usage);
-#endif
-               }
-               return CMD_RET_SUCCESS;
-       }
-       /*
-        * command help (long version)
-        */
-       for (i = 1; i < argc; ++i) {
-               if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
-                       rcode = cmd_usage(cmdtp);
-               } else {
-                       printf_P(PSTR("Unknown command '%s' - try 'help'"
-                               " without arguments.\n\n"), argv[i]
-                               );
-                       rcode = CMD_RET_FAILURE;
-               }
-       }
-       return rcode;
+       return p->subcmd;
 }
 
-/***************************************************************************
+/*
  * find command table entry for a command
  */
-cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
+cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
 {
        cmd_tbl_t *cmdtp;
        cmd_tbl_t *cmdtp_temp = table;  /*Init value */
+       int table_len = cmd_tbl_item_count(table);
        size_t len;
        uint_fast8_t n_found = 0;
 
+       char *optenv = getenv_str(PSTR("cmd"));
+       bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
        if (!cmd)
                return NULL;
 
@@ -141,25 +104,100 @@ cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
        for (cmdtp = table;
             cmdtp != table + table_len;
             cmdtp++) {
-               if (strncmp_P (cmd, cmdtp->name, len) == 0) {
-                       if (len == strlen (cmdtp->name))
+               if (strncmp_P(cmd, cmdtp->name, len) == 0 &&
+                                       (opt_debug || cmdtp->name[0] != '!')) {
+                       if (len == strlen_P(cmdtp->name))
                                return cmdtp;   /* full match */
 
                        cmdtp_temp = cmdtp;     /* abbreviated command ? */
                        n_found++;
                }
        }
-       if (n_found == 1) {                     /* exactly one match */
+       if (n_found == 1)                       /* exactly one match */
                return cmdtp_temp;
-       }
 
        return NULL;    /* not found or ambiguous command */
 }
 
+cmd_tbl_t *find_cmd_sub(const char *cmd, cmd_tbl_t *table)
+{
+       cmd_tbl_t *entry = NULL;
+
+       for (cmd_tbl_t *tp = get_cmd_tbl_base(table); tp->name && entry == NULL; tp++)
+               if (tp->subcmd && tp->flags & CTBL_SUBCMDAUTO)
+                       entry = find_cmd(cmd, tp->subcmd);
 
-cmd_tbl_t *find_cmd (const char *cmd)
+       return entry;
+}
+
+/*
+ * Use puts() instead of printf() to avoid printf buffer overflow
+ * for long help messages
+ */
+
+command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
 {
-       return find_cmd_tbl(cmd, cmd_tbl, cmd_tbl_item_count());
+       (void) flag;
+
+       cmd_tbl_t *tbl_start = get_cmd_tbl_base(cmdtp);
+
+       char *optenv = getenv_str(PSTR("cmd"));
+       bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
+       if (argc == 1) {        /*show list of commands */
+               int cmd_items = cmd_tbl_item_count(tbl_start);
+               cmd_tbl_t *cmd_array[cmd_items];
+               cmd_tbl_t *tp = tbl_start;
+               uint_fast8_t max_len = 0;
+
+               /* Make array of commands from .uboot_cmd section */
+               for (int i = 0; i < cmd_items; i++) {
+                       cmd_array[i] = tp++;
+                       uint_fast8_t l = strlen_P(cmd_array[i]->name);
+                       if (l > max_len)
+                               max_len = l;
+               }
+
+               /* Sort command list */
+               qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
+
+               /* print short help (usage) */
+               for (int i = 0; i < cmd_items; i++) {
+                       if (opt_debug || cmd_array[i]->name[0] != '!') {
+                               const FLASH char *usage = cmd_array[i]->usage;
+
+                               /* allow user abort */
+                               if (ctrlc ())
+                                       return CMD_RET_FAILURE;
+                               if (usage == NULL)
+                                       continue;
+#if defined(GCC_BUG_61443) || 1
+                               print_usage_line(cmd_array[i]->name, max_len, usage);
+#else
+                               printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"),
+                                               cmd_array[i]->name, usage);
+#endif
+                       }
+               }
+               return CMD_RET_SUCCESS;
+       }
+
+       /*
+        * command help (long version)
+        */
+       for (uint8_t i = 1; i < argc; ++i) {
+               if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL &&
+                                       (opt_debug || cmdtp->name[0] != '!')) {
+                       cmd_usage(cmdtp);
+               } else {
+                       printf_P(PSTR("Unknown command '%s' - try 'help'"
+                               " without arguments.\n\n"), argv[i]
+                               );
+                       return CMD_RET_FAILURE;
+               }
+       }
+
+       return CMD_RET_SUCCESS;
 }
 
 
@@ -244,7 +282,7 @@ static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv
 
        /* more than one arg or one but the start of the next */
        if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
-               cmdtp = find_cmd(argv[0]);
+               cmdtp = find_cmd(argv[0], cmd_tbl);
                if (cmdtp == NULL || cmdtp->complete == NULL) {
                        cmdv[0] = NULL;
                        return 0;
@@ -463,32 +501,49 @@ int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *c
  * @param argv         Arguments
  * @return 0 if command succeeded, else non-zero (CMD_RET_...)
  */
-command_ret_t cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t cmd_call(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
 {
        command_ret_t result;
 
        result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
-       if (result != CMD_RET_SUCCESS)
-               debug("Command failed, result=%d\n", result);
+//     if (result != CMD_RET_SUCCESS)
+//             debug("Command failed, result=%d\n", result);
        return result;
 }
 
-command_ret_t cmd_process(int flag, int argc, char * const argv[],
+#pragma GCC diagnostic ignored "-Wclobbered"
+
+command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
                               uint_fast8_t *repeatable)
 {
        command_ret_t rc = CMD_RET_SUCCESS;
        cmd_tbl_t *cmdtp;
 
        /* Look up command in command table */
-       cmdtp = find_cmd(argv[0]);
+       cmdtp = find_cmd(argv[0], cmd_tbl);
+       if (cmdtp != NULL) {
+               /* Check if this command has subcommands */
+               if (cmdtp->subcmd && argc > 1) {
+
+                       /* Look up subcommand in subcommand table */
+                       cmd_tbl_t *cmdtpsub = find_cmd(argv[1], cmdtp->subcmd);
+                       if (cmdtpsub == NULL) {
+                               printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv[0], argv[1], argv[0]);
+                               return CMD_RET_FAILURE;
+                       }
+                       cmdtp = cmdtpsub;
+                       --argc;
+                       ++argv;
+               }
+       } else {
+               /* Search subcommands */
+               cmdtp = find_cmd_sub(argv[0], cmd_tbl);
+       }
+
        if (cmdtp == NULL) {
                printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
                return CMD_RET_FAILURE;
        }
-       if (!cmdtp->cmd) {
-               debug("### Command '%s' found, but ->cmd == NULL \n", argv[0]);
-               return CMD_RET_FAILURE;
-       }
 
        /* found - check max args */
        if (argc > cmdtp->maxargs)
@@ -506,10 +561,13 @@ command_ret_t cmd_process(int flag, int argc, char * const argv[],
        }
 #endif
 
+       if (setjmp(cmd_jbuf) != 0)
+               return CMD_RET_FAILURE;
+
        /* If OK so far, then do the command */
        if (!rc) {
                rc = cmd_call(cmdtp, flag, argc, argv);
-               *repeatable &= cmdtp->repeatable;
+               *repeatable &= (cmdtp->flags & CTBL_REPEAT) != 0;
        }
        if (rc == CMD_RET_USAGE)
                rc = cmd_usage(cmdtp);