xref: /openbmc/qemu/qapi/qmp-registry.c (revision d6032e06)
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 <glib.h>
16 #include <string.h>
17 #include "qapi/qmp/dispatch.h"
18 
19 static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
20     QTAILQ_HEAD_INITIALIZER(qmp_commands);
21 
22 void qmp_register_command(const char *name, QmpCommandFunc *fn,
23                           QmpCommandOptions options)
24 {
25     QmpCommand *cmd = g_malloc0(sizeof(*cmd));
26 
27     cmd->name = name;
28     cmd->type = QCT_NORMAL;
29     cmd->fn = fn;
30     cmd->enabled = true;
31     cmd->options = options;
32     QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
33 }
34 
35 QmpCommand *qmp_find_command(const char *name)
36 {
37     QmpCommand *cmd;
38 
39     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
40         if (strcmp(cmd->name, name) == 0) {
41             return cmd;
42         }
43     }
44     return NULL;
45 }
46 
47 static void qmp_toggle_command(const char *name, bool enabled)
48 {
49     QmpCommand *cmd;
50 
51     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
52         if (strcmp(cmd->name, name) == 0) {
53             cmd->enabled = enabled;
54             return;
55         }
56     }
57 }
58 
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 
69 bool qmp_command_is_enabled(const QmpCommand *cmd)
70 {
71     return cmd->enabled;
72 }
73 
74 const char *qmp_command_name(const QmpCommand *cmd)
75 {
76     return cmd->name;
77 }
78 
79 bool qmp_has_success_response(const QmpCommand *cmd)
80 {
81     return !(cmd->options & QCO_NO_SUCCESS_RESP);
82 }
83 
84 void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
85 {
86     QmpCommand *cmd;
87 
88     QTAILQ_FOREACH(cmd, &qmp_commands, node) {
89         fn(cmd, opaque);
90     }
91 }
92