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 #include "hello.h"
12 
13 COMMAND_HANDLER(handle_foo_command)
14 {
15  if (CMD_ARGC < 1 || CMD_ARGC > 2)
17 
18  uint32_t address;
19  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
20 
21  const char *msg = "<unchanged>";
22  if (CMD_ARGC == 2) {
23  bool enable;
24  COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
25  msg = enable ? "enable" : "disable";
26  }
27 
28  LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
29  return ERROR_OK;
30 }
31 
32 static bool foo_flag;
33 
34 COMMAND_HANDLER(handle_flag_command)
35 {
36  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
37  &foo_flag, "foo flag");
38 }
39 
40 static const struct command_registration foo_command_handlers[] = {
41  {
42  .name = "bar",
43  .handler = &handle_foo_command,
44  .mode = COMMAND_ANY,
45  .usage = "address ['enable'|'disable']",
46  .help = "an example command",
47  },
48  {
49  .name = "baz",
50  .handler = &handle_foo_command,
51  .mode = COMMAND_ANY,
52  .usage = "address ['enable'|'disable']",
53  .help = "a sample command",
54  },
55  {
56  .name = "flag",
57  .handler = &handle_flag_command,
58  .mode = COMMAND_ANY,
59  .usage = "[on|off]",
60  .help = "set a flag",
61  },
63 };
64 
65 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
66 {
67  if (CMD_ARGC > 1)
69  if (1 == CMD_ARGC) {
70  *sep = " ";
71  *name = CMD_ARGV[0];
72  } else
73  *sep = *name = "";
74 
75  return ERROR_OK;
76 }
77 COMMAND_HANDLER(handle_hello_command)
78 {
79  const char *sep, *name;
80  int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
81  if (retval == ERROR_OK)
82  command_print(CMD, "Greetings%s%s!", sep, name);
83  return retval;
84 }
85 
87  {
88  .name = "hello",
89  .handler = handle_hello_command,
90  .mode = COMMAND_ANY,
91  .help = "prints a warm welcome",
92  .usage = "[name]",
93  },
94  {
95  .name = "foo",
96  .mode = COMMAND_ANY,
97  .help = "example command handler skeleton",
98  .chain = foo_command_handlers,
99  .usage = "",
100  },
102 };
const char * name
Definition: armv4_5.c:76
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:166
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:533
#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:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42
COMMAND_HANDLER(handle_foo_command)
Definition: hello.c:13
static const struct command_registration foo_command_handlers[]
Definition: hello.c:40
static bool foo_flag
Definition: hello.c:32
static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
Definition: hello.c:65
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:86
#define LOG_INFO(expr ...)
Definition: log.h:126
#define ERROR_OK
Definition: log.h:164
const char * name
Definition: command.h:235
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:241