You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/12/07 01:04:08 UTC

incubator-mynewt-core git commit: MYNEWT-494 assert no duplicate shell commands

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 40f9ad1e5 -> 524a6ea72


MYNEWT-494 assert no duplicate shell commands

This commit adds a new syscfg var: SHELL_DEBUG.  If this setting is
enabled, the shell package asserts that no two commands with the same
name get registered.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/524a6ea7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/524a6ea7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/524a6ea7

Branch: refs/heads/develop
Commit: 524a6ea7206687b8e3ccacfa894483b6a76990b7
Parents: 40f9ad1
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Dec 6 17:01:07 2016 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Dec 6 17:01:07 2016 -0800

----------------------------------------------------------------------
 sys/shell/src/shell.c | 73 +++++++++++++++++++++++++++++++---------------
 sys/shell/syscfg.yml  |  3 ++
 2 files changed, 53 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/524a6ea7/sys/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/sys/shell/src/shell.c b/sys/shell/src/shell.c
index 166eda6..f11086b 100644
--- a/sys/shell/src/shell.c
+++ b/sys/shell/src/shell.c
@@ -23,6 +23,7 @@
 
 #include "sysinit/sysinit.h"
 #include "syscfg/syscfg.h"
+#include "defs/error.h"
 #include "console/console.h"
 #include "console/prompt.h"
 #include "console/ticks.h"
@@ -146,11 +147,49 @@ err:
     return (rc);
 }
 
+static int
+shell_cmd_find(char *cmd_name, struct shell_cmd **out_cmd)
+{
+    struct shell_cmd *sc;
+    int rc;
+
+    rc = shell_cmd_list_lock();
+    if (rc != 0) {
+        return rc;
+    }
+
+    STAILQ_FOREACH(sc, &g_shell_cmd_list, sc_next) {
+        if (!strcmp(sc->sc_cmd, cmd_name)) {
+            break;
+        }
+    }
+
+    rc = shell_cmd_list_unlock();
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (out_cmd != NULL) {
+        *out_cmd = sc;
+    }
+
+    if (sc == NULL) {
+        return SYS_ENOENT;
+    }
+
+    return 0;
+}
+
 int
 shell_cmd_register(struct shell_cmd *sc)
 {
     int rc;
 
+#if MYNEWT_VAL(SHELL_DEBUG)
+    /* Ensure command not already registered. */
+    assert(shell_cmd_find(sc->sc_cmd, NULL) == SYS_ENOENT);
+#endif
+
     /* Add the command that is being registered. */
     rc = shell_cmd_list_lock();
     if (rc != 0) {
@@ -175,31 +214,19 @@ shell_cmd(char *cmd, char **argv, int argc)
     struct shell_cmd *sc;
     int rc;
 
-    rc = shell_cmd_list_lock();
-    if (rc != 0) {
-        goto err;
-    }
-
-    STAILQ_FOREACH(sc, &g_shell_cmd_list, sc_next) {
-        if (!strcmp(sc->sc_cmd, cmd)) {
-            break;
-        }
-    }
-
-    rc = shell_cmd_list_unlock();
-    if (rc != 0) {
-        goto err;
-    }
-
-    if (sc) {
+    rc = shell_cmd_find(cmd, &sc);
+    switch (rc) {
+    case 0:
         sc->sc_cmd_func(argc, argv);
-    } else {
+        return 0;
+
+    case SYS_ENOENT:
         console_printf("Unknown command %s\n", cmd);
-    }
+        return 0;
 
-    return (0);
-err:
-    return (rc);
+    default:
+        return rc;
+    }
 }
 
 static int
@@ -563,7 +590,7 @@ shell_init(void)
     rc = shell_cmd_register(&g_shell_help_cmd);
     SYSINIT_PANIC_ASSERT(rc == 0);
 
-   rc = shell_cmd_register(&g_shell_prompt_cmd);
+    rc = shell_cmd_register(&g_shell_prompt_cmd);
     SYSINIT_PANIC_ASSERT(rc == 0);
     
     rc = shell_cmd_register(&g_shell_ticks_cmd);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/524a6ea7/sys/shell/syscfg.yml
----------------------------------------------------------------------
diff --git a/sys/shell/syscfg.yml b/sys/shell/syscfg.yml
index e3539b8..c495c58 100644
--- a/sys/shell/syscfg.yml
+++ b/sys/shell/syscfg.yml
@@ -25,3 +25,6 @@ syscfg.defs:
     SHELL_MAX_INPUT_LEN:
         description: 'TBD'
         value: 256
+    SHELL_DEBUG:
+        description: Enables additional error checking in the shell package.
+        value: 1