From a93b404161dc42f8dee805c8f95bc4974aded9cb Mon Sep 17 00:00:00 2001
From: Zachary T Welch <zw@superlucidity.net>
Date: Sat, 21 Nov 2009 19:27:20 -0800
Subject: [PATCH] improve command handling examples

Removes hello and foo commands from top-level registration.  Instead,
the dummy interface driver and faux flash driver have been augmented
to register these commands as sub-commands.
---
 src/flash/faux.c | 12 ++++++++++++
 src/hello.c      |  2 +-
 src/hello.h      | 35 +++++++++++++++++++++++++++++++++++
 src/jtag/dummy.c | 14 ++++++++++++++
 src/openocd.c    |  4 ----
 5 files changed, 62 insertions(+), 5 deletions(-)
 create mode 100644 src/hello.h

diff --git a/src/flash/faux.c b/src/flash/faux.c
index adfc7bd3e..caec2c791 100644
--- a/src/flash/faux.c
+++ b/src/flash/faux.c
@@ -23,6 +23,7 @@
 
 #include "flash.h"
 #include "image.h"
+#include "../hello.h"
 
 
 struct faux_flash_bank
@@ -123,8 +124,19 @@ static int faux_probe(struct flash_bank *bank)
 	return ERROR_OK;
 }
 
+static const struct command_registration faux_command_handlers[] = {
+	{
+		.name = "faux",
+		.mode = COMMAND_ANY,
+		.help = "faux flash command group",
+		.chain = hello_command_handlers,
+	},
+	COMMAND_REGISTRATION_DONE
+};
+
 struct flash_driver faux_flash = {
 		.name = "faux",
+		.commands = faux_command_handlers,
 		.flash_bank_command = &faux_flash_bank_command,
 		.erase = &faux_erase,
 		.protect = &faux_protect,
diff --git a/src/hello.c b/src/hello.c
index 9a1bf9252..2e5c9289c 100644
--- a/src/hello.c
+++ b/src/hello.c
@@ -101,7 +101,7 @@ COMMAND_HANDLER(handle_hello_command)
 	return retval;
 }
 
-static const struct command_registration hello_command_handlers[] = {
+const struct command_registration hello_command_handlers[] = {
 	{
 		.name = "hello",
 		.handler = &handle_hello_command,
diff --git a/src/hello.h b/src/hello.h
new file mode 100644
index 000000000..fc674ad6e
--- /dev/null
+++ b/src/hello.h
@@ -0,0 +1,35 @@
+/***************************************************************************
+ *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#ifndef OPENOCD_HELLO_H
+#define OPENOCD_HELLO_H
+
+struct command_context;
+struct command_registration;
+
+/// Register the hello commands in the specified command_context
+int hello_register_commands(struct command_context *cmd_ctx);
+
+/**
+ * Export the registration for the hello command group, so it can be
+ * embedded in example drivers.
+ */
+extern const struct command_registration hello_command_handlers[];
+
+#endif // OPENOCD_HELLO_H
diff --git a/src/jtag/dummy.c b/src/jtag/dummy.c
index 11b6f71ae..c2beb0920 100644
--- a/src/jtag/dummy.c
+++ b/src/jtag/dummy.c
@@ -23,6 +23,7 @@
 
 #include "interface.h"
 #include "bitbang.h"
+#include "../hello.h"
 
 
 /* my private tap controller state, which tracks state for calling code */
@@ -146,12 +147,25 @@ static int dummy_quit(void)
 	return ERROR_OK;
 }
 
+static const struct command_registration dummy_command_handlers[] = {
+	{
+		.name = "dummy",
+		.mode = COMMAND_ANY,
+		.help = "dummy interface driver commands",
+
+		.chain = hello_command_handlers,
+	},
+	COMMAND_REGISTRATION_DONE,
+};
+
 /* The dummy driver is used to easily check the code path
  * where the target is unresponsive.
  */
 struct jtag_interface dummy_interface = {
 		.name = "dummy",
 
+		.commands = dummy_command_handlers,
+
 		.execute_queue = &bitbang_execute_queue,
 
 		.speed = &dummy_speed,
diff --git a/src/openocd.c b/src/openocd.c
index e38c84ecc..1f29acdb5 100644
--- a/src/openocd.c
+++ b/src/openocd.c
@@ -179,9 +179,6 @@ static const struct command_registration openocd_command_handlers[] = {
 
 struct command_context *global_cmd_ctx;
 
-/// src/hello.c gives a simple example for writing new command modules
-int hello_register_commands(struct command_context *cmd_ctx);
-
 /* NB! this fn can be invoked outside this file for non PC hosted builds */
 struct command_context *setup_command_handler(void)
 {
@@ -191,7 +188,6 @@ struct command_context *setup_command_handler(void)
 
 	register_commands(cmd_ctx, NULL, openocd_command_handlers);
 	/* register subsystem commands */
-	hello_register_commands(cmd_ctx);
 	server_register_commands(cmd_ctx);
 	telnet_register_commands(cmd_ctx);
 	gdb_register_commands(cmd_ctx);
-- 
GitLab