qmp-input-visitor.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Input Visitor
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "qmp-input-visitor.h"
  14. #include "qemu-queue.h"
  15. #include "qemu-common.h"
  16. #include "qemu-objects.h"
  17. #include "qerror.h"
  18. #define QIV_STACK_SIZE 1024
  19. typedef struct StackObject
  20. {
  21. const QObject *obj;
  22. const QListEntry *entry;
  23. } StackObject;
  24. struct QmpInputVisitor
  25. {
  26. Visitor visitor;
  27. QObject *obj;
  28. StackObject stack[QIV_STACK_SIZE];
  29. int nb_stack;
  30. };
  31. static QmpInputVisitor *to_qiv(Visitor *v)
  32. {
  33. return container_of(v, QmpInputVisitor, visitor);
  34. }
  35. static const QObject *qmp_input_get_object(QmpInputVisitor *qiv,
  36. const char *name)
  37. {
  38. const QObject *qobj;
  39. if (qiv->nb_stack == 0) {
  40. qobj = qiv->obj;
  41. } else {
  42. qobj = qiv->stack[qiv->nb_stack - 1].obj;
  43. }
  44. if (name && qobject_type(qobj) == QTYPE_QDICT) {
  45. return qdict_get(qobject_to_qdict(qobj), name);
  46. } else if (qiv->nb_stack > 0 && qobject_type(qobj) == QTYPE_QLIST) {
  47. return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry);
  48. }
  49. return qobj;
  50. }
  51. static void qmp_input_push(QmpInputVisitor *qiv, const QObject *obj, Error **errp)
  52. {
  53. qiv->stack[qiv->nb_stack].obj = obj;
  54. if (qobject_type(obj) == QTYPE_QLIST) {
  55. qiv->stack[qiv->nb_stack].entry = qlist_first(qobject_to_qlist(obj));
  56. }
  57. qiv->nb_stack++;
  58. if (qiv->nb_stack >= QIV_STACK_SIZE) {
  59. error_set(errp, QERR_BUFFER_OVERRUN);
  60. return;
  61. }
  62. }
  63. static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
  64. {
  65. qiv->nb_stack--;
  66. if (qiv->nb_stack < 0) {
  67. error_set(errp, QERR_BUFFER_OVERRUN);
  68. return;
  69. }
  70. }
  71. static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,
  72. const char *name, size_t size, Error **errp)
  73. {
  74. QmpInputVisitor *qiv = to_qiv(v);
  75. const QObject *qobj = qmp_input_get_object(qiv, name);
  76. if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
  77. error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
  78. "QDict");
  79. return;
  80. }
  81. qmp_input_push(qiv, qobj, errp);
  82. if (error_is_set(errp)) {
  83. return;
  84. }
  85. if (obj) {
  86. *obj = qemu_mallocz(size);
  87. }
  88. }
  89. static void qmp_input_end_struct(Visitor *v, Error **errp)
  90. {
  91. QmpInputVisitor *qiv = to_qiv(v);
  92. qmp_input_pop(qiv, errp);
  93. }
  94. static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
  95. {
  96. QmpInputVisitor *qiv = to_qiv(v);
  97. const QObject *qobj = qmp_input_get_object(qiv, name);
  98. if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
  99. error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
  100. "list");
  101. return;
  102. }
  103. qmp_input_push(qiv, qobj, errp);
  104. }
  105. static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
  106. Error **errp)
  107. {
  108. QmpInputVisitor *qiv = to_qiv(v);
  109. GenericList *entry;
  110. StackObject *so = &qiv->stack[qiv->nb_stack - 1];
  111. if (so->entry == NULL) {
  112. return NULL;
  113. }
  114. entry = qemu_mallocz(sizeof(*entry));
  115. if (*list) {
  116. so->entry = qlist_next(so->entry);
  117. if (so->entry == NULL) {
  118. qemu_free(entry);
  119. return NULL;
  120. }
  121. (*list)->next = entry;
  122. }
  123. *list = entry;
  124. return entry;
  125. }
  126. static void qmp_input_end_list(Visitor *v, Error **errp)
  127. {
  128. QmpInputVisitor *qiv = to_qiv(v);
  129. qmp_input_pop(qiv, errp);
  130. }
  131. static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name,
  132. Error **errp)
  133. {
  134. QmpInputVisitor *qiv = to_qiv(v);
  135. const QObject *qobj = qmp_input_get_object(qiv, name);
  136. if (!qobj || qobject_type(qobj) != QTYPE_QINT) {
  137. error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
  138. "integer");
  139. return;
  140. }
  141. *obj = qint_get_int(qobject_to_qint(qobj));
  142. }
  143. static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name,
  144. Error **errp)
  145. {
  146. QmpInputVisitor *qiv = to_qiv(v);
  147. const QObject *qobj = qmp_input_get_object(qiv, name);
  148. if (!qobj || qobject_type(qobj) != QTYPE_QBOOL) {
  149. error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
  150. "boolean");
  151. return;
  152. }
  153. *obj = qbool_get_int(qobject_to_qbool(qobj));
  154. }
  155. static void qmp_input_type_str(Visitor *v, char **obj, const char *name,
  156. Error **errp)
  157. {
  158. QmpInputVisitor *qiv = to_qiv(v);
  159. const QObject *qobj = qmp_input_get_object(qiv, name);
  160. if (!qobj || qobject_type(qobj) != QTYPE_QSTRING) {
  161. error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
  162. "string");
  163. return;
  164. }
  165. *obj = qemu_strdup(qstring_get_str(qobject_to_qstring(qobj)));
  166. }
  167. static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
  168. Error **errp)
  169. {
  170. QmpInputVisitor *qiv = to_qiv(v);
  171. const QObject *qobj = qmp_input_get_object(qiv, name);
  172. if (!qobj || qobject_type(qobj) != QTYPE_QFLOAT) {
  173. error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
  174. "double");
  175. return;
  176. }
  177. *obj = qfloat_get_double(qobject_to_qfloat(qobj));
  178. }
  179. static void qmp_input_type_enum(Visitor *v, int *obj, const char *strings[],
  180. const char *kind, const char *name,
  181. Error **errp)
  182. {
  183. int64_t value = 0;
  184. char *enum_str;
  185. assert(strings);
  186. qmp_input_type_str(v, &enum_str, name, errp);
  187. if (error_is_set(errp)) {
  188. return;
  189. }
  190. while (strings[value] != NULL) {
  191. if (strcmp(strings[value], enum_str) == 0) {
  192. break;
  193. }
  194. value++;
  195. }
  196. if (strings[value] == NULL) {
  197. error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
  198. return;
  199. }
  200. *obj = value;
  201. }
  202. static void qmp_input_start_optional(Visitor *v, bool *present,
  203. const char *name, Error **errp)
  204. {
  205. QmpInputVisitor *qiv = to_qiv(v);
  206. const QObject *qobj = qmp_input_get_object(qiv, name);
  207. if (!qobj) {
  208. *present = false;
  209. return;
  210. }
  211. *present = true;
  212. }
  213. static void qmp_input_end_optional(Visitor *v, Error **errp)
  214. {
  215. }
  216. Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
  217. {
  218. return &v->visitor;
  219. }
  220. void qmp_input_visitor_cleanup(QmpInputVisitor *v)
  221. {
  222. qobject_decref(v->obj);
  223. qemu_free(v);
  224. }
  225. QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
  226. {
  227. QmpInputVisitor *v;
  228. v = qemu_mallocz(sizeof(*v));
  229. v->visitor.start_struct = qmp_input_start_struct;
  230. v->visitor.end_struct = qmp_input_end_struct;
  231. v->visitor.start_list = qmp_input_start_list;
  232. v->visitor.next_list = qmp_input_next_list;
  233. v->visitor.end_list = qmp_input_end_list;
  234. v->visitor.type_enum = qmp_input_type_enum;
  235. v->visitor.type_int = qmp_input_type_int;
  236. v->visitor.type_bool = qmp_input_type_bool;
  237. v->visitor.type_str = qmp_input_type_str;
  238. v->visitor.type_number = qmp_input_type_number;
  239. v->visitor.start_optional = qmp_input_start_optional;
  240. v->visitor.end_optional = qmp_input_end_optional;
  241. v->obj = obj;
  242. qobject_incref(v->obj);
  243. return v;
  244. }