f103ed23c5224d4ce279783fa0cf34f0aeaba9af
[openocd.git] / src / hello.c
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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <helper/log.h>
24
25 COMMAND_HANDLER(handle_foo_command)
26 {
27 if (CMD_ARGC < 1 || CMD_ARGC > 2)
28 return ERROR_COMMAND_SYNTAX_ERROR;
29
30 uint32_t address;
31 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
32
33 const char *msg = "<unchanged>";
34 if (CMD_ARGC == 2) {
35 bool enable;
36 COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
37 msg = enable ? "enable" : "disable";
38 }
39
40 LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
41 return ERROR_OK;
42 }
43
44 static bool foo_flag;
45
46 COMMAND_HANDLER(handle_flag_command)
47 {
48 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
49 &foo_flag, "foo flag");
50 }
51
52 static const struct command_registration foo_command_handlers[] = {
53 {
54 .name = "bar",
55 .handler = &handle_foo_command,
56 .mode = COMMAND_ANY,
57 .usage = "address ['enable'|'disable']",
58 .help = "an example command",
59 },
60 {
61 .name = "baz",
62 .handler = &handle_foo_command,
63 .mode = COMMAND_ANY,
64 .usage = "address ['enable'|'disable']",
65 .help = "a sample command",
66 },
67 {
68 .name = "flag",
69 .handler = &handle_flag_command,
70 .mode = COMMAND_ANY,
71 .usage = "[on|off]",
72 .help = "set a flag",
73 },
74 COMMAND_REGISTRATION_DONE
75 };
76
77 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
78 {
79 if (CMD_ARGC > 1)
80 return ERROR_COMMAND_SYNTAX_ERROR;
81 if (1 == CMD_ARGC) {
82 *sep = " ";
83 *name = CMD_ARGV[0];
84 } else
85 *sep = *name = "";
86
87 return ERROR_OK;
88 }
89 COMMAND_HANDLER(handle_hello_command)
90 {
91 const char *sep, *name;
92 int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
93 if (ERROR_OK == retval)
94 command_print(CMD_CTX, "Greetings%s%s!", sep, name);
95 return retval;
96 }
97
98 const struct command_registration hello_command_handlers[] = {
99 {
100 .name = "hello",
101 .handler = handle_hello_command,
102 .mode = COMMAND_ANY,
103 .help = "prints a warm welcome",
104 .usage = "[name]",
105 },
106 {
107 .name = "foo",
108 .mode = COMMAND_ANY,
109 .help = "example command handler skeleton",
110
111 .chain = foo_command_handlers,
112 },
113 COMMAND_REGISTRATION_DONE
114 };