qmp-registry.c 2.0 KB

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