OpenOCD
hello.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
5  ***************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 #include <helper/log.h>
11 
12 COMMAND_HANDLER(handle_foo_command)
13 {
14  if (CMD_ARGC < 1 || CMD_ARGC > 2)
16 
17  uint32_t address;
18  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
19 
20  const char *msg = "<unchanged>";
21  if (CMD_ARGC == 2) {
22  bool enable;
23  COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
24  msg = enable ? "enable" : "disable";
25  }
26 
27  LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
28  return ERROR_OK;
29 }
30 
31 static bool foo_flag;
32 
33 COMMAND_HANDLER(handle_flag_command)
34 {
35  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
36  &foo_flag, "foo flag");
37 }
38 
39 static const struct command_registration foo_command_handlers[] = {
40  {
41  .name = "bar",
42  .handler = &handle_foo_command,
43  .mode = COMMAND_ANY,
44  .usage = "address ['enable'|'disable']",
45  .help = "an example command",
46  },
47  {
48  .name = "baz",
49  .handler = &handle_foo_command,
50  .mode = COMMAND_ANY,
51  .usage = "address ['enable'|'disable']",
52  .help = "a sample command",
53  },
54  {
55  .name = "flag",
56  .handler = &handle_flag_command,
57  .mode = COMMAND_ANY,
58  .usage = "[on|off]",
59  .help = "set a flag",
60  },
62 };
63 
64 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
65 {
66  if (CMD_ARGC > 1)
68  if (1 == CMD_ARGC) {
69  *sep = " ";
70  *name = CMD_ARGV[0];
71  } else
72  *sep = *name = "";
73 
74  return ERROR_OK;
75 }
76 COMMAND_HANDLER(handle_hello_command)
77 {
78  const char *sep, *name;
79  int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
80  if (retval == ERROR_OK)
81  command_print(CMD, "Greetings%s%s!", sep, name);
82  return retval;
83 }
84 
86  {
87  .name = "hello",
88  .handler = handle_hello_command,
89  .mode = COMMAND_ANY,
90  .help = "prints a warm welcome",
91  .usage = "[name]",
92  },
93  {
94  .name = "foo",
95  .mode = COMMAND_ANY,
96  .help = "example command handler skeleton",
97  .chain = foo_command_handlers,
98  .usage = "",
99  },
101 };
const char * name
Definition: armv4_5.c:76
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:117
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:160
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:507
#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:425
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_ANY
Definition: command.h:42
COMMAND_HANDLER(handle_foo_command)
Definition: hello.c:12
static const struct command_registration foo_command_handlers[]
Definition: hello.c:39
static bool foo_flag
Definition: hello.c:31
static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
Definition: hello.c:64
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:85
#define LOG_INFO(expr ...)
Definition: log.h:117
#define ERROR_OK
Definition: log.h:155
const char * name
Definition: command.h:229
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:235