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