xref: /qemu/qapi/qmp-registry.c (revision 287d55c6769c3a38e9083b103cb148fb38858b3a)
1 /*
2  * Core Definitions for QAPI/QMP Dispatch
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *  Michael Roth      <mdroth@us.ibm.com>
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 
15 #include "qapi/qmp-core.h"
16 
17 static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
18     QTAILQ_HEAD_INITIALIZER(qmp_commands);
19 
20 void qmp_register_command(const char *name, QmpCommandFunc *fn)
21 {
22     QmpCommand *cmd = g_malloc0(sizeof(*cmd));
23 
24     cmd->name = name;
25     cmd->type = QCT_NORMAL;
26     cmd->fn = fn;
27     cmd->enabled = true;
28     QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
29 }
30 
31 QmpCommand *qmp_find_command(const char *name)
32 {
33     QmpCommand *cmd;
34 
35     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
36         if (strcmp(cmd->name, name) == 0) {
37             return cmd;
38         }
39     }
40     return NULL;
41 }
42 
43 static void qmp_toggle_command(const char *name, bool enabled)
44 {
45     QmpCommand *cmd;
46 
47     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
48         if (strcmp(cmd->name, name) == 0) {
49             cmd->enabled = enabled;
50             return;
51         }
52     }
53 }
54 
55 void qmp_disable_command(const char *name)
56 {
57     qmp_toggle_command(name, false);
58 }
59 
60 void qmp_enable_command(const char *name)
61 {
62     qmp_toggle_command(name, true);
63 }
64 
65 bool qmp_command_is_enabled(const char *name)
66 {
67     QmpCommand *cmd;
68 
69     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
70         if (strcmp(cmd->name, name) == 0) {
71             return cmd->enabled;
72         }
73     }
74 
75     return false;
76 }
77 
78 char **qmp_get_command_list(void)
79 {
80     QmpCommand *cmd;
81     int count = 1;
82     char **list_head, **list;
83 
84     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
85         count++;
86     }
87 
88     list_head = list = g_malloc0(count * sizeof(char *));
89 
90     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
91         *list = strdup(cmd->name);
92         list++;
93     }
94 
95     return list_head;
96 }
97