xref: /openbmc/qemu/qapi/qmp-registry.c (revision 64547a3b)
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 const QmpCommand *qmp_find_command(const QmpCommandList *cmds, const char *name)
31 {
32     QmpCommand *cmd;
33 
34     QTAILQ_FOREACH(cmd, cmds, node) {
35         if (strcmp(cmd->name, name) == 0) {
36             return cmd;
37         }
38     }
39     return NULL;
40 }
41 
42 static void qmp_toggle_command(QmpCommandList *cmds, const char *name,
43                                bool enabled)
44 {
45     QmpCommand *cmd;
46 
47     QTAILQ_FOREACH(cmd, cmds, node) {
48         if (strcmp(cmd->name, name) == 0) {
49             cmd->enabled = enabled;
50             return;
51         }
52     }
53 }
54 
55 void qmp_disable_command(QmpCommandList *cmds, const char *name)
56 {
57     qmp_toggle_command(cmds, name, false);
58 }
59 
60 void qmp_enable_command(QmpCommandList *cmds, const char *name)
61 {
62     qmp_toggle_command(cmds, name, true);
63 }
64 
65 bool qmp_command_is_enabled(const QmpCommand *cmd)
66 {
67     return cmd->enabled;
68 }
69 
70 const char *qmp_command_name(const QmpCommand *cmd)
71 {
72     return cmd->name;
73 }
74 
75 bool qmp_has_success_response(const QmpCommand *cmd)
76 {
77     return !(cmd->options & QCO_NO_SUCCESS_RESP);
78 }
79 
80 void qmp_for_each_command(const QmpCommandList *cmds, qmp_cmd_callback_fn fn,
81                           void *opaque)
82 {
83     const QmpCommand *cmd;
84 
85     QTAILQ_FOREACH(cmd, cmds, node) {
86         fn(cmd, opaque);
87     }
88 }
89