1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <glib.h>
16#include <string.h>
17#include "qapi/qmp/dispatch.h"
18
19static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
20 QTAILQ_HEAD_INITIALIZER(qmp_commands);
21
22void qmp_register_command(const char *name, QmpCommandFunc *fn,
23 QmpCommandOptions options)
24{
25 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
26
27 cmd->name = name;
28 cmd->type = QCT_NORMAL;
29 cmd->fn = fn;
30 cmd->enabled = true;
31 cmd->options = options;
32 QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
33}
34
35QmpCommand *qmp_find_command(const char *name)
36{
37 QmpCommand *cmd;
38
39 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
40 if (strcmp(cmd->name, name) == 0) {
41 return cmd;
42 }
43 }
44 return NULL;
45}
46
47static void qmp_toggle_command(const char *name, bool enabled)
48{
49 QmpCommand *cmd;
50
51 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
52 if (strcmp(cmd->name, name) == 0) {
53 cmd->enabled = enabled;
54 return;
55 }
56 }
57}
58
59void qmp_disable_command(const char *name)
60{
61 qmp_toggle_command(name, false);
62}
63
64void qmp_enable_command(const char *name)
65{
66 qmp_toggle_command(name, true);
67}
68
69bool qmp_command_is_enabled(const char *name)
70{
71 QmpCommand *cmd;
72
73 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
74 if (strcmp(cmd->name, name) == 0) {
75 return cmd->enabled;
76 }
77 }
78
79 return false;
80}
81
82char **qmp_get_command_list(void)
83{
84 QmpCommand *cmd;
85 int count = 1;
86 char **list_head, **list;
87
88 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
89 count++;
90 }
91
92 list_head = list = g_malloc0(count * sizeof(char *));
93
94 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
95 *list = g_strdup(cmd->name);
96 list++;
97 }
98
99 return list_head;
100}
101