string-output-visitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * String printing Visitor
  3. *
  4. * Copyright Red Hat, Inc. 2012-2016
  5. *
  6. * Author: Paolo Bonzini <pbonzini@redhat.com>
  7. *
  8. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  9. * See the COPYING.LIB file in the top-level directory.
  10. *
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/cutils.h"
  14. #include "qapi/string-output-visitor.h"
  15. #include "qapi/visitor-impl.h"
  16. #include "qemu/host-utils.h"
  17. #include <math.h>
  18. #include "qemu/range.h"
  19. enum ListMode {
  20. LM_NONE, /* not traversing a list of repeated options */
  21. LM_STARTED, /* next_list() ready to be called */
  22. LM_IN_PROGRESS, /* next_list() has been called.
  23. *
  24. * Generating the next list link will consume the most
  25. * recently parsed QemuOpt instance of the repeated
  26. * option.
  27. *
  28. * Parsing a value into the list link will examine the
  29. * next QemuOpt instance of the repeated option, and
  30. * possibly enter LM_SIGNED_INTERVAL or
  31. * LM_UNSIGNED_INTERVAL.
  32. */
  33. LM_SIGNED_INTERVAL, /* next_list() has been called.
  34. *
  35. * Generating the next list link will consume the most
  36. * recently stored element from the signed interval,
  37. * parsed from the most recent QemuOpt instance of the
  38. * repeated option. This may consume QemuOpt itself
  39. * and return to LM_IN_PROGRESS.
  40. *
  41. * Parsing a value into the list link will store the
  42. * next element of the signed interval.
  43. */
  44. LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */
  45. LM_END, /* next_list() called, about to see last element. */
  46. };
  47. typedef enum ListMode ListMode;
  48. struct StringOutputVisitor
  49. {
  50. Visitor visitor;
  51. bool human;
  52. GString *string;
  53. char **result;
  54. ListMode list_mode;
  55. union {
  56. int64_t s;
  57. uint64_t u;
  58. } range_start, range_end;
  59. GList *ranges;
  60. void *list; /* Only needed for sanity checking the caller */
  61. };
  62. static StringOutputVisitor *to_sov(Visitor *v)
  63. {
  64. return container_of(v, StringOutputVisitor, visitor);
  65. }
  66. static void string_output_set(StringOutputVisitor *sov, char *string)
  67. {
  68. if (sov->string) {
  69. g_string_free(sov->string, true);
  70. }
  71. sov->string = g_string_new(string);
  72. g_free(string);
  73. }
  74. static void string_output_append(StringOutputVisitor *sov, int64_t a)
  75. {
  76. Range *r = g_malloc0(sizeof(*r));
  77. range_set_bounds(r, a, a);
  78. sov->ranges = range_list_insert(sov->ranges, r);
  79. }
  80. static void string_output_append_range(StringOutputVisitor *sov,
  81. int64_t s, int64_t e)
  82. {
  83. Range *r = g_malloc0(sizeof(*r));
  84. range_set_bounds(r, s, e);
  85. sov->ranges = range_list_insert(sov->ranges, r);
  86. }
  87. static void format_string(StringOutputVisitor *sov, Range *r, bool next,
  88. bool human)
  89. {
  90. if (range_lob(r) != range_upb(r)) {
  91. if (human) {
  92. g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
  93. range_lob(r), range_upb(r));
  94. } else {
  95. g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
  96. range_lob(r), range_upb(r));
  97. }
  98. } else {
  99. if (human) {
  100. g_string_append_printf(sov->string, "0x%" PRIx64, range_lob(r));
  101. } else {
  102. g_string_append_printf(sov->string, "%" PRId64, range_lob(r));
  103. }
  104. }
  105. if (next) {
  106. g_string_append(sov->string, ",");
  107. }
  108. }
  109. static void print_type_int64(Visitor *v, const char *name, int64_t *obj,
  110. Error **errp)
  111. {
  112. StringOutputVisitor *sov = to_sov(v);
  113. GList *l;
  114. switch (sov->list_mode) {
  115. case LM_NONE:
  116. string_output_append(sov, *obj);
  117. break;
  118. case LM_STARTED:
  119. sov->range_start.s = *obj;
  120. sov->range_end.s = *obj;
  121. sov->list_mode = LM_IN_PROGRESS;
  122. return;
  123. case LM_IN_PROGRESS:
  124. if (sov->range_end.s + 1 == *obj) {
  125. sov->range_end.s++;
  126. } else {
  127. if (sov->range_start.s == sov->range_end.s) {
  128. string_output_append(sov, sov->range_end.s);
  129. } else {
  130. assert(sov->range_start.s < sov->range_end.s);
  131. string_output_append_range(sov, sov->range_start.s,
  132. sov->range_end.s);
  133. }
  134. sov->range_start.s = *obj;
  135. sov->range_end.s = *obj;
  136. }
  137. return;
  138. case LM_END:
  139. if (sov->range_end.s + 1 == *obj) {
  140. sov->range_end.s++;
  141. assert(sov->range_start.s < sov->range_end.s);
  142. string_output_append_range(sov, sov->range_start.s,
  143. sov->range_end.s);
  144. } else {
  145. if (sov->range_start.s == sov->range_end.s) {
  146. string_output_append(sov, sov->range_end.s);
  147. } else {
  148. assert(sov->range_start.s < sov->range_end.s);
  149. string_output_append_range(sov, sov->range_start.s,
  150. sov->range_end.s);
  151. }
  152. string_output_append(sov, *obj);
  153. }
  154. break;
  155. default:
  156. abort();
  157. }
  158. l = sov->ranges;
  159. while (l) {
  160. Range *r = l->data;
  161. format_string(sov, r, l->next != NULL, false);
  162. l = l->next;
  163. }
  164. if (sov->human) {
  165. l = sov->ranges;
  166. g_string_append(sov->string, " (");
  167. while (l) {
  168. Range *r = l->data;
  169. format_string(sov, r, l->next != NULL, true);
  170. l = l->next;
  171. }
  172. g_string_append(sov->string, ")");
  173. }
  174. }
  175. static void print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
  176. Error **errp)
  177. {
  178. /* FIXME: print_type_int64 mishandles values over INT64_MAX */
  179. int64_t i = *obj;
  180. print_type_int64(v, name, &i, errp);
  181. }
  182. static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
  183. Error **errp)
  184. {
  185. StringOutputVisitor *sov = to_sov(v);
  186. uint64_t val;
  187. char *out, *psize;
  188. if (!sov->human) {
  189. out = g_strdup_printf("%"PRIu64, *obj);
  190. string_output_set(sov, out);
  191. return;
  192. }
  193. val = *obj;
  194. psize = size_to_str(val);
  195. out = g_strdup_printf("%"PRIu64" (%s)", val, psize);
  196. string_output_set(sov, out);
  197. g_free(psize);
  198. }
  199. static void print_type_bool(Visitor *v, const char *name, bool *obj,
  200. Error **errp)
  201. {
  202. StringOutputVisitor *sov = to_sov(v);
  203. string_output_set(sov, g_strdup(*obj ? "true" : "false"));
  204. }
  205. static void print_type_str(Visitor *v, const char *name, char **obj,
  206. Error **errp)
  207. {
  208. StringOutputVisitor *sov = to_sov(v);
  209. char *out;
  210. if (sov->human) {
  211. out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
  212. } else {
  213. out = g_strdup(*obj ? *obj : "");
  214. }
  215. string_output_set(sov, out);
  216. }
  217. static void print_type_number(Visitor *v, const char *name, double *obj,
  218. Error **errp)
  219. {
  220. StringOutputVisitor *sov = to_sov(v);
  221. string_output_set(sov, g_strdup_printf("%f", *obj));
  222. }
  223. static void print_type_null(Visitor *v, const char *name, QNull **obj,
  224. Error **errp)
  225. {
  226. StringOutputVisitor *sov = to_sov(v);
  227. char *out;
  228. if (sov->human) {
  229. out = g_strdup("<null>");
  230. } else {
  231. out = g_strdup("");
  232. }
  233. string_output_set(sov, out);
  234. }
  235. static void
  236. start_list(Visitor *v, const char *name, GenericList **list, size_t size,
  237. Error **errp)
  238. {
  239. StringOutputVisitor *sov = to_sov(v);
  240. /* we can't traverse a list in a list */
  241. assert(sov->list_mode == LM_NONE);
  242. /* We don't support visits without a list */
  243. assert(list);
  244. sov->list = list;
  245. /* List handling is only needed if there are at least two elements */
  246. if (*list && (*list)->next) {
  247. sov->list_mode = LM_STARTED;
  248. }
  249. }
  250. static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
  251. {
  252. StringOutputVisitor *sov = to_sov(v);
  253. GenericList *ret = tail->next;
  254. if (ret && !ret->next) {
  255. sov->list_mode = LM_END;
  256. }
  257. return ret;
  258. }
  259. static void end_list(Visitor *v, void **obj)
  260. {
  261. StringOutputVisitor *sov = to_sov(v);
  262. assert(sov->list == obj);
  263. assert(sov->list_mode == LM_STARTED ||
  264. sov->list_mode == LM_END ||
  265. sov->list_mode == LM_NONE ||
  266. sov->list_mode == LM_IN_PROGRESS);
  267. sov->list_mode = LM_NONE;
  268. }
  269. static void string_output_complete(Visitor *v, void *opaque)
  270. {
  271. StringOutputVisitor *sov = to_sov(v);
  272. assert(opaque == sov->result);
  273. *sov->result = g_string_free(sov->string, false);
  274. sov->string = NULL;
  275. }
  276. static void free_range(void *range, void *dummy)
  277. {
  278. g_free(range);
  279. }
  280. static void string_output_free(Visitor *v)
  281. {
  282. StringOutputVisitor *sov = to_sov(v);
  283. if (sov->string) {
  284. g_string_free(sov->string, true);
  285. }
  286. g_list_foreach(sov->ranges, free_range, NULL);
  287. g_list_free(sov->ranges);
  288. g_free(sov);
  289. }
  290. Visitor *string_output_visitor_new(bool human, char **result)
  291. {
  292. StringOutputVisitor *v;
  293. v = g_malloc0(sizeof(*v));
  294. v->string = g_string_new(NULL);
  295. v->human = human;
  296. v->result = result;
  297. *result = NULL;
  298. v->visitor.type = VISITOR_OUTPUT;
  299. v->visitor.type_int64 = print_type_int64;
  300. v->visitor.type_uint64 = print_type_uint64;
  301. v->visitor.type_size = print_type_size;
  302. v->visitor.type_bool = print_type_bool;
  303. v->visitor.type_str = print_type_str;
  304. v->visitor.type_number = print_type_number;
  305. v->visitor.type_null = print_type_null;
  306. v->visitor.start_list = start_list;
  307. v->visitor.next_list = next_list;
  308. v->visitor.end_list = end_list;
  309. v->visitor.complete = string_output_complete;
  310. v->visitor.free = string_output_free;
  311. return &v->visitor;
  312. }