qmp-registry.c 2.1 KB

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