OpenOCD
hello.c
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16  ***************************************************************************/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 #include <helper/log.h>
22 
23 COMMAND_HANDLER(handle_foo_command)
24 {
25  if (CMD_ARGC < 1 || CMD_ARGC > 2)
27 
28  uint32_t address;
29  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
30 
31  const char *msg = "<unchanged>";
32  if (CMD_ARGC == 2) {
33  bool enable;
34  COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
35  msg = enable ? "enable" : "disable";
36  }
37 
38  LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
39  return ERROR_OK;
40 }
41 
42 static bool foo_flag;
43 
44 COMMAND_HANDLER(handle_flag_command)
45 {
46  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
47  &foo_flag, "foo flag");
48 }
49 
50 static const struct command_registration foo_command_handlers[] = {
51  {
52  .name = "bar",
53  .handler = &handle_foo_command,
54  .mode = COMMAND_ANY,
55  .usage = "address ['enable'|'disable']",
56  .help = "an example command",
57  },
58  {
59  .name = "baz",
60  .handler = &handle_foo_command,
61  .mode = COMMAND_ANY,
62  .usage = "address ['enable'|'disable']",
63  .help = "a sample command",
64  },
65  {
66  .name = "flag",
67  .handler = &handle_flag_command,
68  .mode = COMMAND_ANY,
69  .usage = "[on|off]",
70  .help = "set a flag",
71  },
73 };
74 
75 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
76 {
77  if (CMD_ARGC > 1)
79  if (1 == CMD_ARGC) {
80  *sep = " ";
81  *name = CMD_ARGV[0];
82  } else
83  *sep = *name = "";
84 
85  return ERROR_OK;
86 }
87 COMMAND_HANDLER(handle_hello_command)
88 {
89  const char *sep, *name;
90  int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
91  if (ERROR_OK == retval)
92  command_print(CMD_CTX, "Greetings%s%s!", sep, name);
93  return retval;
94 }
95 
97  {
98  .name = "hello",
99  .handler = handle_hello_command,
100  .mode = COMMAND_ANY,
101  .help = "prints a warm welcome",
102  .usage = "[name]",
103  },
104  {
105  .name = "foo",
106  .mode = COMMAND_ANY,
107  .help = "example command handler skeleton",
108 
109  .chain = foo_command_handlers,
110  },
112 };
COMMAND_HANDLER(handle_foo_command)
Definition: hello.c:23
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:131
const char * name
Definition: armv4_5.c:78
#define LOG_INFO(expr...)
Definition: log.h:123
#define CALL_COMMAND_HANDLER(name, extra...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:93
static const struct command_registration foo_command_handlers[]
Definition: hello.c:50
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:126
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:121
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:378
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:222
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:338
static bool foo_flag
Definition: hello.c:42
const char * name
Definition: command.h:203
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:116
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:418
void command_print(struct command_context *context, const char *format,...)
Definition: command.c:528
#define ERROR_OK
Definition: log.h:144
static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
Definition: hello.c:75
const struct command_registration hello_command_handlers[]
Export the registration for the hello command group, so it can be embedded in example drivers...
Definition: hello.c:96