blob: 54146133772d9483d6d64d07136ca75211d17b70 [file] [log] [blame]
Michael Roth43c20a42011-07-19 14:50:36 -05001/*
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
Michael Rothabd6cf62011-12-06 22:03:42 -060017static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
Michael Roth43c20a42011-07-19 14:50:36 -050018 QTAILQ_HEAD_INITIALIZER(qmp_commands);
19
Luiz Capitulinod34b8672012-05-08 14:24:44 -030020void qmp_register_command(const char *name, QmpCommandFunc *fn,
21 QmpCommandOptions options)
Michael Roth43c20a42011-07-19 14:50:36 -050022{
Anthony Liguori7267c092011-08-20 22:09:37 -050023 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
Michael Roth43c20a42011-07-19 14:50:36 -050024
25 cmd->name = name;
26 cmd->type = QCT_NORMAL;
27 cmd->fn = fn;
Michael Rothabd6cf62011-12-06 22:03:42 -060028 cmd->enabled = true;
Luiz Capitulinod34b8672012-05-08 14:24:44 -030029 cmd->options = options;
Michael Roth43c20a42011-07-19 14:50:36 -050030 QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
31}
32
33QmpCommand *qmp_find_command(const char *name)
34{
Michael Rothabd6cf62011-12-06 22:03:42 -060035 QmpCommand *cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050036
Michael Rothabd6cf62011-12-06 22:03:42 -060037 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
38 if (strcmp(cmd->name, name) == 0) {
39 return cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050040 }
41 }
42 return NULL;
43}
Michael Rothabd6cf62011-12-06 22:03:42 -060044
Michael Rothf22d85e2012-04-17 19:01:45 -050045static void qmp_toggle_command(const char *name, bool enabled)
Michael Rothabd6cf62011-12-06 22:03:42 -060046{
47 QmpCommand *cmd;
48
49 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
50 if (strcmp(cmd->name, name) == 0) {
Michael Rothf22d85e2012-04-17 19:01:45 -050051 cmd->enabled = enabled;
Michael Rothabd6cf62011-12-06 22:03:42 -060052 return;
53 }
54 }
55}
56
Michael Rothf22d85e2012-04-17 19:01:45 -050057void qmp_disable_command(const char *name)
58{
59 qmp_toggle_command(name, false);
60}
61
62void qmp_enable_command(const char *name)
63{
64 qmp_toggle_command(name, true);
65}
66
Michael Rothbf95c0d2011-12-06 22:03:43 -060067bool qmp_command_is_enabled(const char *name)
68{
69 QmpCommand *cmd;
70
71 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
72 if (strcmp(cmd->name, name) == 0) {
73 return cmd->enabled;
74 }
75 }
76
77 return false;
78}
79
Michael Rothabd6cf62011-12-06 22:03:42 -060080char **qmp_get_command_list(void)
81{
82 QmpCommand *cmd;
83 int count = 1;
84 char **list_head, **list;
85
86 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
87 count++;
88 }
89
90 list_head = list = g_malloc0(count * sizeof(char *));
91
92 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
93 *list = strdup(cmd->name);
94 list++;
95 }
96
97 return list_head;
98}