qmp-registry.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <glib.h>
  15. #include <string.h>
  16. #include "qapi/qmp/dispatch.h"
  17. static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
  18. QTAILQ_HEAD_INITIALIZER(qmp_commands);
  19. void qmp_register_command(const char *name, QmpCommandFunc *fn,
  20. QmpCommandOptions options)
  21. {
  22. QmpCommand *cmd = g_malloc0(sizeof(*cmd));
  23. cmd->name = name;
  24. cmd->type = QCT_NORMAL;
  25. cmd->fn = fn;
  26. cmd->enabled = true;
  27. cmd->options = options;
  28. QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
  29. }
  30. QmpCommand *qmp_find_command(const char *name)
  31. {
  32. QmpCommand *cmd;
  33. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  34. if (strcmp(cmd->name, name) == 0) {
  35. return cmd;
  36. }
  37. }
  38. return NULL;
  39. }
  40. static void qmp_toggle_command(const char *name, bool enabled)
  41. {
  42. QmpCommand *cmd;
  43. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  44. if (strcmp(cmd->name, name) == 0) {
  45. cmd->enabled = enabled;
  46. return;
  47. }
  48. }
  49. }
  50. void qmp_disable_command(const char *name)
  51. {
  52. qmp_toggle_command(name, false);
  53. }
  54. void qmp_enable_command(const char *name)
  55. {
  56. qmp_toggle_command(name, true);
  57. }
  58. bool qmp_command_is_enabled(const char *name)
  59. {
  60. QmpCommand *cmd;
  61. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  62. if (strcmp(cmd->name, name) == 0) {
  63. return cmd->enabled;
  64. }
  65. }
  66. return false;
  67. }
  68. char **qmp_get_command_list(void)
  69. {
  70. QmpCommand *cmd;
  71. int count = 1;
  72. char **list_head, **list;
  73. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  74. count++;
  75. }
  76. list_head = list = g_malloc0(count * sizeof(char *));
  77. QTAILQ_FOREACH(cmd, &qmp_commands, node) {
  78. *list = g_strdup(cmd->name);
  79. list++;
  80. }
  81. return list_head;
  82. }