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