xref: /qemu/qapi/qmp-registry.c (revision f22d85e9)
143c20a43SMichael Roth /*
243c20a43SMichael Roth  * Core Definitions for QAPI/QMP Dispatch
343c20a43SMichael Roth  *
443c20a43SMichael Roth  * Copyright IBM, Corp. 2011
543c20a43SMichael Roth  *
643c20a43SMichael Roth  * Authors:
743c20a43SMichael Roth  *  Anthony Liguori   <aliguori@us.ibm.com>
843c20a43SMichael Roth  *  Michael Roth      <mdroth@us.ibm.com>
943c20a43SMichael Roth  *
1043c20a43SMichael Roth  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
1143c20a43SMichael Roth  * See the COPYING.LIB file in the top-level directory.
1243c20a43SMichael Roth  *
1343c20a43SMichael Roth  */
1443c20a43SMichael Roth 
1543c20a43SMichael Roth #include "qapi/qmp-core.h"
1643c20a43SMichael Roth 
17abd6cf6dSMichael Roth static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
1843c20a43SMichael Roth     QTAILQ_HEAD_INITIALIZER(qmp_commands);
1943c20a43SMichael Roth 
2043c20a43SMichael Roth void qmp_register_command(const char *name, QmpCommandFunc *fn)
2143c20a43SMichael Roth {
227267c094SAnthony Liguori     QmpCommand *cmd = g_malloc0(sizeof(*cmd));
2343c20a43SMichael Roth 
2443c20a43SMichael Roth     cmd->name = name;
2543c20a43SMichael Roth     cmd->type = QCT_NORMAL;
2643c20a43SMichael Roth     cmd->fn = fn;
27abd6cf6dSMichael Roth     cmd->enabled = true;
2843c20a43SMichael Roth     QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
2943c20a43SMichael Roth }
3043c20a43SMichael Roth 
3143c20a43SMichael Roth QmpCommand *qmp_find_command(const char *name)
3243c20a43SMichael Roth {
33abd6cf6dSMichael Roth     QmpCommand *cmd;
3443c20a43SMichael Roth 
35abd6cf6dSMichael Roth     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
36abd6cf6dSMichael Roth         if (strcmp(cmd->name, name) == 0) {
37abd6cf6dSMichael Roth             return cmd;
3843c20a43SMichael Roth         }
3943c20a43SMichael Roth     }
4043c20a43SMichael Roth     return NULL;
4143c20a43SMichael Roth }
42abd6cf6dSMichael Roth 
43*f22d85e9SMichael Roth static void qmp_toggle_command(const char *name, bool enabled)
44abd6cf6dSMichael Roth {
45abd6cf6dSMichael Roth     QmpCommand *cmd;
46abd6cf6dSMichael Roth 
47abd6cf6dSMichael Roth     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
48abd6cf6dSMichael Roth         if (strcmp(cmd->name, name) == 0) {
49*f22d85e9SMichael Roth             cmd->enabled = enabled;
50abd6cf6dSMichael Roth             return;
51abd6cf6dSMichael Roth         }
52abd6cf6dSMichael Roth     }
53abd6cf6dSMichael Roth }
54abd6cf6dSMichael Roth 
55*f22d85e9SMichael Roth void qmp_disable_command(const char *name)
56*f22d85e9SMichael Roth {
57*f22d85e9SMichael Roth     qmp_toggle_command(name, false);
58*f22d85e9SMichael Roth }
59*f22d85e9SMichael Roth 
60*f22d85e9SMichael Roth void qmp_enable_command(const char *name)
61*f22d85e9SMichael Roth {
62*f22d85e9SMichael Roth     qmp_toggle_command(name, true);
63*f22d85e9SMichael Roth }
64*f22d85e9SMichael Roth 
65bf95c0d5SMichael Roth bool qmp_command_is_enabled(const char *name)
66bf95c0d5SMichael Roth {
67bf95c0d5SMichael Roth     QmpCommand *cmd;
68bf95c0d5SMichael Roth 
69bf95c0d5SMichael Roth     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
70bf95c0d5SMichael Roth         if (strcmp(cmd->name, name) == 0) {
71bf95c0d5SMichael Roth             return cmd->enabled;
72bf95c0d5SMichael Roth         }
73bf95c0d5SMichael Roth     }
74bf95c0d5SMichael Roth 
75bf95c0d5SMichael Roth     return false;
76bf95c0d5SMichael Roth }
77bf95c0d5SMichael Roth 
78abd6cf6dSMichael Roth char **qmp_get_command_list(void)
79abd6cf6dSMichael Roth {
80abd6cf6dSMichael Roth     QmpCommand *cmd;
81abd6cf6dSMichael Roth     int count = 1;
82abd6cf6dSMichael Roth     char **list_head, **list;
83abd6cf6dSMichael Roth 
84abd6cf6dSMichael Roth     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
85abd6cf6dSMichael Roth         count++;
86abd6cf6dSMichael Roth     }
87abd6cf6dSMichael Roth 
88abd6cf6dSMichael Roth     list_head = list = g_malloc0(count * sizeof(char *));
89abd6cf6dSMichael Roth 
90abd6cf6dSMichael Roth     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
91abd6cf6dSMichael Roth         *list = strdup(cmd->name);
92abd6cf6dSMichael Roth         list++;
93abd6cf6dSMichael Roth     }
94abd6cf6dSMichael Roth 
95abd6cf6dSMichael Roth     return list_head;
96abd6cf6dSMichael Roth }
97