2
0

string-output-visitor.c 10 KB

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