Change return value on error.
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 {
29 return ERROR_COMMAND_SYNTAX_ERROR;
30 }
31
32 uint32_t address;
33 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
34
35 const char *msg = "<unchanged>";
36 if (CMD_ARGC == 2)
37 {
38 bool enable;
39 COMMAND_PARSE_ENABLE(CMD_ARGV[1], enable);
40 msg = enable ? "enable" : "disable";
41 }
42
43 LOG_INFO("%s: address=0x%8.8" PRIx32 " enabled=%s", CMD_NAME, address, msg);
44 return ERROR_OK;
45 }
46
47 static bool foo_flag;
48
49 COMMAND_HANDLER(handle_flag_command)
50 {
51 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
52 &foo_flag, "foo flag");
53 }
54
55 static const struct command_registration foo_command_handlers[] = {
56 {
57 .name = "bar",
58 .handler = &handle_foo_command,
59 .mode = COMMAND_ANY,
60 .usage = "address ['enable'|'disable']",
61 .help = "an example command",
62 },
63 {
64 .name = "baz",
65 .handler = &handle_foo_command,
66 .mode = COMMAND_ANY,
67 .usage = "address ['enable'|'disable']",
68 .help = "a sample command",
69 },
70 {
71 .name = "flag",
72 .handler = &handle_flag_command,
73 .mode = COMMAND_ANY,
74 .usage = "[on|off]",
75 .help = "set a flag",
76 },
77 COMMAND_REGISTRATION_DONE
78 };
79
80 static COMMAND_HELPER(handle_hello_args, const char **sep, const char **name)
81 {
82 if (CMD_ARGC > 1)
83 {
84 return ERROR_COMMAND_SYNTAX_ERROR;
85 }
86 if (1 == CMD_ARGC)
87 {
88 *sep = " ";
89 *name = CMD_ARGV[0];
90 }
91 else
92 *sep = *name = "";
93
94 return ERROR_OK;
95 }
96 COMMAND_HANDLER(handle_hello_command)
97 {
98 const char *sep, *name;
99 int retval = CALL_COMMAND_HANDLER(handle_hello_args, &sep, &name);
100 if (ERROR_OK == retval)
101 command_print(CMD_CTX, "Greetings%s%s!", sep, name);
102 return retval;
103 }
104
105 const struct command_registration hello_command_handlers[] = {
106 {
107 .name = "hello",
108 .handler = handle_hello_command,
109 .mode = COMMAND_ANY,
110 .help = "prints a warm welcome",
111 .usage = "[name]",
112 },
113 {
114 .name = "foo",
115 .mode = COMMAND_ANY,
116 .help = "example command handler skeleton",
117
118 .chain = foo_command_handlers,
119 },
120 COMMAND_REGISTRATION_DONE
121 };