qmp-registry.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. QmpCommand *qmp_find_command(QmpCommandList *cmds, const char *name)
  27. {
  28. QmpCommand *cmd;
  29. QTAILQ_FOREACH(cmd, cmds, node) {
  30. if (strcmp(cmd->name, name) == 0) {
  31. return cmd;
  32. }
  33. }
  34. return NULL;
  35. }
  36. static void qmp_toggle_command(QmpCommandList *cmds, const char *name,
  37. bool enabled)
  38. {
  39. QmpCommand *cmd;
  40. QTAILQ_FOREACH(cmd, cmds, node) {
  41. if (strcmp(cmd->name, name) == 0) {
  42. cmd->enabled = enabled;
  43. return;
  44. }
  45. }
  46. }
  47. void qmp_disable_command(QmpCommandList *cmds, const char *name)
  48. {
  49. qmp_toggle_command(cmds, name, false);
  50. }
  51. void qmp_enable_command(QmpCommandList *cmds, const char *name)
  52. {
  53. qmp_toggle_command(cmds, name, true);
  54. }
  55. bool qmp_command_is_enabled(const QmpCommand *cmd)
  56. {
  57. return cmd->enabled;
  58. }
  59. const char *qmp_command_name(const QmpCommand *cmd)
  60. {
  61. return cmd->name;
  62. }
  63. bool qmp_has_success_response(const QmpCommand *cmd)
  64. {
  65. return !(cmd->options & QCO_NO_SUCCESS_RESP);
  66. }
  67. void qmp_for_each_command(QmpCommandList *cmds, qmp_cmd_callback_fn fn,
  68. void *opaque)
  69. {
  70. QmpCommand *cmd;
  71. QTAILQ_FOREACH(cmd, cmds, node) {
  72. fn(cmd, opaque);
  73. }
  74. }