qmp-registry.c 2.1 KB

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