]>
Commit | Line | Data |
---|---|---|
43c20a43 MR |
1 | /* |
2 | * Core Definitions for QAPI/QMP Dispatch | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * Michael Roth <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
11 | * See the COPYING.LIB file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
ccff63ca EH |
15 | #include <glib.h> |
16 | #include <string.h> | |
7b1b5d19 | 17 | #include "qapi/qmp/dispatch.h" |
43c20a43 | 18 | |
abd6cf6d | 19 | static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands = |
43c20a43 MR |
20 | QTAILQ_HEAD_INITIALIZER(qmp_commands); |
21 | ||
d34b867d LC |
22 | void qmp_register_command(const char *name, QmpCommandFunc *fn, |
23 | QmpCommandOptions options) | |
43c20a43 | 24 | { |
7267c094 | 25 | QmpCommand *cmd = g_malloc0(sizeof(*cmd)); |
43c20a43 MR |
26 | |
27 | cmd->name = name; | |
28 | cmd->type = QCT_NORMAL; | |
29 | cmd->fn = fn; | |
abd6cf6d | 30 | cmd->enabled = true; |
d34b867d | 31 | cmd->options = options; |
43c20a43 MR |
32 | QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node); |
33 | } | |
34 | ||
35 | QmpCommand *qmp_find_command(const char *name) | |
36 | { | |
abd6cf6d | 37 | QmpCommand *cmd; |
43c20a43 | 38 | |
abd6cf6d MR |
39 | QTAILQ_FOREACH(cmd, &qmp_commands, node) { |
40 | if (strcmp(cmd->name, name) == 0) { | |
41 | return cmd; | |
43c20a43 MR |
42 | } |
43 | } | |
44 | return NULL; | |
45 | } | |
abd6cf6d | 46 | |
f22d85e9 | 47 | static void qmp_toggle_command(const char *name, bool enabled) |
abd6cf6d MR |
48 | { |
49 | QmpCommand *cmd; | |
50 | ||
51 | QTAILQ_FOREACH(cmd, &qmp_commands, node) { | |
52 | if (strcmp(cmd->name, name) == 0) { | |
f22d85e9 | 53 | cmd->enabled = enabled; |
abd6cf6d MR |
54 | return; |
55 | } | |
56 | } | |
57 | } | |
58 | ||
f22d85e9 MR |
59 | void qmp_disable_command(const char *name) |
60 | { | |
61 | qmp_toggle_command(name, false); | |
62 | } | |
63 | ||
64 | void qmp_enable_command(const char *name) | |
65 | { | |
66 | qmp_toggle_command(name, true); | |
67 | } | |
68 | ||
bf95c0d5 MR |
69 | bool 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 | ||
abd6cf6d MR |
82 | char **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) { | |
13b10e05 | 95 | *list = g_strdup(cmd->name); |
abd6cf6d MR |
96 | list++; |
97 | } | |
98 | ||
99 | return list_head; | |
100 | } |