]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/command.c
Print prefix of subcommands
[z180-stamp.git] / avr / command.c
index 89e88695c2289a5684cfc23f2faae05b3a3a80bb..bb179b800d4b8d88e24533bca0aa7e8ec9abea8d 100644 (file)
+/*
+ * (C) Copyright 2014, 2016, 2018 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 <string.h>
 #include <ctype.h>
-#include <stdlib.h>
-#include <stdio.h>
+#include <setjmp.h>
 
 #include "config.h"
-#include "debug.h"
+#include "print-utils.h"
 #include "con-utils.h"
 #include "env.h"
-#include "timer.h"
-#include "command.h"
+#include "debug.h"
+
+#define DEBUG_CMD      1       /* set to 1 to debug */
+
+#define debug_cmd(fmt, args...)                                                              \
+       debug_cond(DEBUG_CMD, fmt, ##args)
 
 
-static void print_blanks(int_fast8_t count)
+jmp_buf cmd_jbuf;
+
+
+static int cmd_tbl_item_count(cmd_tbl_t *p)
 {
-       while(count--)
-               putchar(' ');
+       int count = 0;
+
+       while (p->name != NULL) {
+               if (p->subcmd) {
+                       cmd_tbl_t *sub = p->subcmd;
+                       while (sub->name != NULL) {
+                               if (sub->flags & CTBL_SUBCMDAUTO)
+                                       count++;
+                               sub++;
+                       }
+               }
+               if ((p->flags & CTBL_SUBCMDAUTO) == 0)
+                       count++;
+               p++;
+       }
+       return count;
 }
 
-static void print_usage_line(const FLASH char *name, const FLASH char *usage)
+static cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t  *cmdtp)
 {
-       my_puts_P(name);
-       print_blanks(CONFIG_SYS_MAXARGS - strlen_P(name));
+       cmd_tbl_t *p = cmdtp;
+
+       while (p->name != NULL)
+               ++p;
+
+       return p->subcmd;
+}
+
+static void print_name_prefix(cmd_tbl_t *p)
+{
+       cmd_tbl_t *tbl_start = get_cmd_tbl_base(p);
+       cmd_tbl_t *top;
+
+       if (tbl_start == cmd_tbl)
+               top = NULL;
+       else {
+               top = cmd_tbl;
+               while (top->subcmd != tbl_start)
+                       ++top;
+       }
+
+       if (top && (p->flags & CTBL_SUBCMDAUTO) == 0) {
+               printf_P(PSTR("%S "), top->name);
+       }
+}
+
+static void print_usage_line(cmd_tbl_t *p, int width)
+{
+       width -= strlen_P(p->name);
+       if (width < 0)
+               width = 0;
+       print_name_prefix(p);
+       my_puts_P(p->name);
+       print_blanks(width);
        my_puts_P(PSTR(" - "));
-       my_puts_P(usage);
-       my_puts_P(PSTR("\n"));
+       puts_P(p->usage);
 }
 
-int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
+static 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;
@@ -45,21 +103,57 @@ int strcmp_PP(const FLASH char *s1, const FLASH char *s2)
        return c1 - c2;
 }
 
-int cmpstringp(const void *p1, const void *p2)
+static 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)
+/****************************************************************************/
+
+/*
+ * find command table entry for a command
+ */
+
+static cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
 {
-       cmd_tbl_t * p = cmd_tbl;
-       int count = 0;
+       if (!cmd)
+               return NULL;
 
-       while (p->name != NULL) {
-               p++; count++;
+       char *optenv = getenv_str(PSTR("cmd"));
+       uint8_t opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
+       cmd_tbl_t *cmdtp_ret = NULL;
+       uint_fast8_t n_found = 0;
+       uint_fast8_t len = strlen(cmd);
+
+       for (cmd_tbl_t *cmdtp = table; cmdtp->name != NULL; cmdtp++) {
+               if (cmdtp->subcmd) {
+                       for (cmd_tbl_t *sub = cmdtp->subcmd; sub->name != NULL; sub++) {
+                               if (sub->flags & CTBL_SUBCMDAUTO &&
+                                                       strncmp_P(cmd, sub->name, len) == 0 &&
+                                                       (opt_debug || !(sub->flags & CTBL_DBG))) {
+                                       if (len == strlen_P(sub->name))
+                                               return sub;                             /* full match */
+                                       cmdtp_ret = sub;                        /* abbreviated command ? */
+                                       ++n_found;
+                               }
+                       }
+               }
+               if ((cmdtp->flags & CTBL_SUBCMDAUTO) == 0 &&
+                                       strncmp_P(cmd, cmdtp->name, len) == 0 &&
+                                       (opt_debug || !(cmdtp->flags & CTBL_DBG))) {
+                       if (len == strlen_P(cmdtp->name))
+                               return cmdtp;                                   /* full match */
+                       cmdtp_ret = cmdtp;                                      /* abbreviated command ? */
+                       ++n_found;
+               }
        }
-       return count;
+
+       if (n_found == 1)
+               return cmdtp_ret;                       /* exactly one match */
+
+       return NULL;    /* not found or ambiguous command */
 }
 
 /*
@@ -67,134 +161,105 @@ int cmd_tbl_item_count(void)
  * for long help messages
  */
 
-int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp,
-               int flag, int argc, char * const argv[])
+command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
-       uint_fast8_t i;
-       int rcode = 0;
+       cmd_tbl_t *tbl_start = get_cmd_tbl_base(cmdtp);
+       command_ret_t rc = CMD_RET_SUCCESS;
 
-       (void) flag;
+       char *optenv = getenv_str(PSTR("cmd"));
+       bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
 
        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++;
+               int cmd_items = cmd_tbl_item_count(tbl_start);
+               cmd_tbl_t **cmd_list = (cmd_tbl_t **) malloc(cmd_items * sizeof(cmd_tbl_t *));
+               uint_fast8_t maxlen_cmd = 0;
+
+               /* Make array of commands */
+               cmd_tbl_t *tp = tbl_start;
+               int i = 0;
+               while (tp->name != NULL) {
+                       if (tp->subcmd) {
+                               cmd_tbl_t *sub = tp->subcmd;
+                               while (sub->name != NULL) {
+                                       if (sub->flags & CTBL_SUBCMDAUTO) {
+                                               uint_fast8_t len = strlen_P(sub->name);
+                                               if (len > maxlen_cmd)
+                                                       maxlen_cmd = len;
+                                               cmd_list[i++] = sub;
+                                       }
+                                       sub++;
+                               }
+                       }
+                       if ((tp->flags & CTBL_SUBCMDAUTO) == 0) {
+                               uint_fast8_t len = strlen_P(tp->name);
+                               if (len > maxlen_cmd)
+                                       maxlen_cmd = len;
+                                       cmd_list[i++] = tp;
+                       }
+                       tp++;
                }
-
                /* Sort command list */
-               qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstringp);
+               qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
 
                /* print short help (usage) */
-               for (i = 0; i < cmd_items; i++) {
-                       const FLASH char *usage = cmd_array[i]->usage;
+               for (int i = 0; i < cmd_items; i++) {
+                       if ((cmd_list[i]->flags & CTBL_DBG) && !opt_debug)
+                               continue;
+                       if (cmd_list[i]->usage == NULL)
+                               continue;
+
+                       print_usage_line(cmd_list[i], maxlen_cmd);
 
                        /* allow user abort */
-                       if (ctrlc ())
-                               return 1;
-                       if (usage == NULL)
-                               continue;
-#ifdef GCC_BUG_61443 
-                       print_usage_line(cmd_array[i]->name, usage);
-#else
-                       printf_P(PSTR("%-" stringify(CONFIG_SYS_MAXARGS) "S - %S\n"), 
-                                       cmd_array[i]->name, usage);
-#endif
+                       if (ctrlc ()) {
+                               rc = CMD_RET_FAILURE;
+                               break;
+                       }
                }
-               return 0;
+               free(cmd_list);
+               return rc;
        }
+
        /*
         * 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);
+       for (uint8_t i = 1; i < argc; ++i) {
+               if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL) {
+                       cmd_usage(cmdtp);
                } else {
                        printf_P(PSTR("Unknown command '%s' - try 'help'"
                                " without arguments.\n\n"), argv[i]
                                );
-                       rcode = 1;
-               }
-       }
-       return rcode;
-}
-
-/***************************************************************************
- * 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 *cmdtp;
-       cmd_tbl_t *cmdtp_temp = table;  /*Init value */
-       size_t len;
-       uint_fast8_t n_found = 0;
-
-       if (!cmd)
-               return NULL;
-
-       len = strlen(cmd);
-
-       for (cmdtp = table;
-            cmdtp != table + table_len;
-            cmdtp++) {
-               if (strncmp_P (cmd, cmdtp->name, len) == 0) {
-                       if (len == strlen (cmdtp->name))
-                               return cmdtp;   /* full match */
-
-                       cmdtp_temp = cmdtp;     /* abbreviated command ? */
-                       n_found++;
+                       return CMD_RET_FAILURE;
                }
        }
-       if (n_found == 1) {                     /* exactly one match */
-               return cmdtp_temp;
-       }
 
-       return NULL;    /* not found or ambiguous command */
+       return CMD_RET_SUCCESS;
 }
 
 
-cmd_tbl_t *find_cmd (const char *cmd)
+command_ret_t cmd_usage(cmd_tbl_t *cmdtp)
 {
-       return find_cmd_tbl(cmd, cmd_tbl, cmd_tbl_item_count());
-}
-
-
-int cmd_usage(const FLASH cmd_tbl_t *cmdtp)
-{
-//     printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
-       print_usage_line(cmdtp->name, cmdtp->usage);
-#if 0
-       my_puts_P(cmdtp->name);
-       print_blanks(CONFIG_SYS_MAXARGS - strlen_P(cmdtp->name));
-       my_puts_P(PSTR(" - "));
-       my_puts_P(cmdtp->usage);
-       my_puts_P(PSTR("\n\n"));
-#endif
+       print_usage_line(cmdtp, 0);
 #ifdef CONFIG_SYS_LONGHELP
-//     printf("Usage:\n%s ", cmdtp->name);
        my_puts_P(PSTR("Usage:\n"));
+       print_name_prefix(cmdtp);
        my_puts_P(cmdtp->name);
-       putchar(' ');
+       my_puts_P(PSTR(" "));
 
-       if (!cmdtp->help) {
+       if (cmdtp->help && *cmdtp->help != '\0')
+               puts_P(cmdtp->help);
+       else
                my_puts_P(PSTR(" - No additional help available.\n"));
-               return 1;
-       }
-
-       my_puts_P(cmdtp->help);
-       my_puts_P(PSTR("\n"));
 #endif /* CONFIG_SYS_LONGHELP */
-       return 1;
+       return CMD_RET_FAILURE;
 }
 
 #ifdef CONFIG_AUTO_COMPLETE
 
 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
 {
-       static char tmp_buf[512];
+       static char tmp_buf[CONFIG_SYS_CBSIZE];
        int space;
 
        space = last_char == '\0' || isblank(last_char);
@@ -210,6 +275,8 @@ int var_complete(int argc, char * const argv[], char last_char, int maxv, char *
 
 /*************************************************************************************/
 
+/* TODO: cmdtp points to FLASH */
+
 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
 {
        cmd_tbl_t *cmdtp = cmd_tbl;
@@ -241,7 +308,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;
@@ -357,6 +424,7 @@ static int find_common_prefix(char * const argv[])
 
 static char tmp_buf[CONFIG_SYS_CBSIZE];        /* copy of console I/O buffer   */
 
+
 int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp)
 {
        int n = *np, col = *colp;
@@ -459,31 +527,45 @@ 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_...)
  */
-static int 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[])
 {
-       int result;
+       command_ret_t result;
 
        result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
-       if (result)
-               debug("Command failed, result=%d\n", result);
+//     if (result != CMD_RET_SUCCESS)
+//             debug("Command failed, result=%d\n", result);
        return result;
 }
 
-enum 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)
 {
-       enum command_ret_t rc = CMD_RET_SUCCESS;
+       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;
+               }
+       }
+
        if (cmdtp == NULL) {
                printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
-               return 1;
-       }
-       if (!cmdtp->cmd) {
-               debug("### Command '%s' found, but ->cmd == NULL \n", argv[0]);
-               return 1;
+               return CMD_RET_FAILURE;
        }
 
        /* found - check max args */
@@ -502,10 +584,13 @@ enum 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_RPT) != 0;
        }
        if (rc == CMD_RET_USAGE)
                rc = cmd_usage(cmdtp);