db6774eaa350f74a14331194a1e5f54e9ed68ff9
[fw/openocd] / src / hello.c
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)
15                 return ERROR_COMMAND_SYNTAX_ERROR;
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         },
61         COMMAND_REGISTRATION_DONE
62 };
63
64 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
65 {
66         if (CMD_ARGC > 1)
67                 return ERROR_COMMAND_SYNTAX_ERROR;
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
85 const struct command_registration hello_command_handlers[] = {
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         },
100         COMMAND_REGISTRATION_DONE
101 };