2
0

string-output-visitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 bool 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 true;
  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 true;
  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. return true;
  175. }
  176. static bool print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
  177. Error **errp)
  178. {
  179. /* FIXME: print_type_int64 mishandles values over INT64_MAX */
  180. int64_t i = *obj;
  181. return print_type_int64(v, name, &i, errp);
  182. }
  183. static bool print_type_size(Visitor *v, const char *name, uint64_t *obj,
  184. Error **errp)
  185. {
  186. StringOutputVisitor *sov = to_sov(v);
  187. uint64_t val;
  188. char *out, *psize;
  189. if (!sov->human) {
  190. out = g_strdup_printf("%"PRIu64, *obj);
  191. string_output_set(sov, out);
  192. return true;
  193. }
  194. val = *obj;
  195. psize = size_to_str(val);
  196. out = g_strdup_printf("%"PRIu64" (%s)", val, psize);
  197. string_output_set(sov, out);
  198. g_free(psize);
  199. return true;
  200. }
  201. static bool print_type_bool(Visitor *v, const char *name, bool *obj,
  202. Error **errp)
  203. {
  204. StringOutputVisitor *sov = to_sov(v);
  205. string_output_set(sov, g_strdup(*obj ? "true" : "false"));
  206. return true;
  207. }
  208. static bool print_type_str(Visitor *v, const char *name, char **obj,
  209. Error **errp)
  210. {
  211. StringOutputVisitor *sov = to_sov(v);
  212. char *out;
  213. if (sov->human) {
  214. out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
  215. } else {
  216. out = g_strdup(*obj ? *obj : "");
  217. }
  218. string_output_set(sov, out);
  219. return true;
  220. }
  221. static bool print_type_number(Visitor *v, const char *name, double *obj,
  222. Error **errp)
  223. {
  224. StringOutputVisitor *sov = to_sov(v);
  225. string_output_set(sov, g_strdup_printf("%f", *obj));
  226. return true;
  227. }
  228. static bool print_type_null(Visitor *v, const char *name, QNull **obj,
  229. Error **errp)
  230. {
  231. StringOutputVisitor *sov = to_sov(v);
  232. char *out;
  233. if (sov->human) {
  234. out = g_strdup("<null>");
  235. } else {
  236. out = g_strdup("");
  237. }
  238. string_output_set(sov, out);
  239. return true;
  240. }
  241. static bool
  242. start_list(Visitor *v, const char *name, GenericList **list, size_t size,
  243. Error **errp)
  244. {
  245. StringOutputVisitor *sov = to_sov(v);
  246. /* we can't traverse a list in a list */
  247. assert(sov->list_mode == LM_NONE);
  248. /* We don't support visits without a list */
  249. assert(list);
  250. sov->list = list;
  251. /* List handling is only needed if there are at least two elements */
  252. if (*list && (*list)->next) {
  253. sov->list_mode = LM_STARTED;
  254. }
  255. return true;
  256. }
  257. static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
  258. {
  259. StringOutputVisitor *sov = to_sov(v);
  260. GenericList *ret = tail->next;
  261. if (ret && !ret->next) {
  262. sov->list_mode = LM_END;
  263. }
  264. return ret;
  265. }
  266. static void end_list(Visitor *v, void **obj)
  267. {
  268. StringOutputVisitor *sov = to_sov(v);
  269. assert(sov->list == obj);
  270. assert(sov->list_mode == LM_STARTED ||
  271. sov->list_mode == LM_END ||
  272. sov->list_mode == LM_NONE ||
  273. sov->list_mode == LM_IN_PROGRESS);
  274. sov->list_mode = LM_NONE;
  275. }
  276. static void string_output_complete(Visitor *v, void *opaque)
  277. {
  278. StringOutputVisitor *sov = to_sov(v);
  279. assert(opaque == sov->result);
  280. *sov->result = g_string_free(sov->string, false);
  281. sov->string = NULL;
  282. }
  283. static void free_range(void *range, void *dummy)
  284. {
  285. g_free(range);
  286. }
  287. static void string_output_free(Visitor *v)
  288. {
  289. StringOutputVisitor *sov = to_sov(v);
  290. if (sov->string) {
  291. g_string_free(sov->string, true);
  292. }
  293. g_list_foreach(sov->ranges, free_range, NULL);
  294. g_list_free(sov->ranges);
  295. g_free(sov);
  296. }
  297. Visitor *string_output_visitor_new(bool human, char **result)
  298. {
  299. StringOutputVisitor *v;
  300. v = g_malloc0(sizeof(*v));
  301. v->string = g_string_new(NULL);
  302. v->human = human;
  303. v->result = result;
  304. *result = NULL;
  305. v->visitor.type = VISITOR_OUTPUT;
  306. v->visitor.type_int64 = print_type_int64;
  307. v->visitor.type_uint64 = print_type_uint64;
  308. v->visitor.type_size = print_type_size;
  309. v->visitor.type_bool = print_type_bool;
  310. v->visitor.type_str = print_type_str;
  311. v->visitor.type_number = print_type_number;
  312. v->visitor.type_null = print_type_null;
  313. v->visitor.start_list = start_list;
  314. v->visitor.next_list = next_list;
  315. v->visitor.end_list = end_list;
  316. v->visitor.complete = string_output_complete;
  317. v->visitor.free = string_output_free;
  318. return &v->visitor;
  319. }