blob: 28bbbe849e2c872165f531a0353d73684143cca5 [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
Eduardo Habkostccff63c2012-10-23 21:35:44 -020015#include <glib.h>
16#include <string.h>
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/dispatch.h"
Michael Roth43c20a42011-07-19 14:50:36 -050018
Michael Rothabd6cf62011-12-06 22:03:42 -060019static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
Michael Roth43c20a42011-07-19 14:50:36 -050020 QTAILQ_HEAD_INITIALIZER(qmp_commands);
21
Luiz Capitulinod34b8672012-05-08 14:24:44 -030022void qmp_register_command(const char *name, QmpCommandFunc *fn,
23 QmpCommandOptions options)
Michael Roth43c20a42011-07-19 14:50:36 -050024{
Anthony Liguori7267c092011-08-20 22:09:37 -050025 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
Michael Roth43c20a42011-07-19 14:50:36 -050026
27 cmd->name = name;
28 cmd->type = QCT_NORMAL;
29 cmd->fn = fn;
Michael Rothabd6cf62011-12-06 22:03:42 -060030 cmd->enabled = true;
Luiz Capitulinod34b8672012-05-08 14:24:44 -030031 cmd->options = options;
Michael Roth43c20a42011-07-19 14:50:36 -050032 QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
33}
34
35QmpCommand *qmp_find_command(const char *name)
36{
Michael Rothabd6cf62011-12-06 22:03:42 -060037 QmpCommand *cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050038
Michael Rothabd6cf62011-12-06 22:03:42 -060039 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
40 if (strcmp(cmd->name, name) == 0) {
41 return cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050042 }
43 }
44 return NULL;
45}
Michael Rothabd6cf62011-12-06 22:03:42 -060046
Michael Rothf22d85e2012-04-17 19:01:45 -050047static void qmp_toggle_command(const char *name, bool enabled)
Michael Rothabd6cf62011-12-06 22:03:42 -060048{
49 QmpCommand *cmd;
50
51 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
52 if (strcmp(cmd->name, name) == 0) {
Michael Rothf22d85e2012-04-17 19:01:45 -050053 cmd->enabled = enabled;
Michael Rothabd6cf62011-12-06 22:03:42 -060054 return;
55 }
56 }
57}
58
Michael Rothf22d85e2012-04-17 19:01:45 -050059void 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
Michael Rothbf95c0d2011-12-06 22:03:43 -060069bool 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
Michael Rothabd6cf62011-12-06 22:03:42 -060082char **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) {
Markus Armbruster13b10e02013-01-22 11:08:05 +010095 *list = g_strdup(cmd->name);
Michael Rothabd6cf62011-12-06 22:03:42 -060096 list++;
97 }
98
99 return list_head;
100}