string-output-visitor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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-common.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. static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
  187. uint64_t div, val;
  188. char *out;
  189. int i;
  190. if (!sov->human) {
  191. out = g_strdup_printf("%"PRIu64, *obj);
  192. string_output_set(sov, out);
  193. return;
  194. }
  195. val = *obj;
  196. /* The exponent (returned in i) minus one gives us
  197. * floor(log2(val * 1024 / 1000). The correction makes us
  198. * switch to the higher power when the integer part is >= 1000.
  199. */
  200. frexp(val / (1000.0 / 1024.0), &i);
  201. i = (i - 1) / 10;
  202. assert(i < ARRAY_SIZE(suffixes));
  203. div = 1ULL << (i * 10);
  204. out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
  205. (double)val/div, suffixes[i], i ? "iB" : "");
  206. string_output_set(sov, out);
  207. }
  208. static void print_type_bool(Visitor *v, const char *name, bool *obj,
  209. Error **errp)
  210. {
  211. StringOutputVisitor *sov = to_sov(v);
  212. string_output_set(sov, g_strdup(*obj ? "true" : "false"));
  213. }
  214. static void print_type_str(Visitor *v, const char *name, char **obj,
  215. Error **errp)
  216. {
  217. StringOutputVisitor *sov = to_sov(v);
  218. char *out;
  219. if (sov->human) {
  220. out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
  221. } else {
  222. out = g_strdup(*obj ? *obj : "");
  223. }
  224. string_output_set(sov, out);
  225. }
  226. static void print_type_number(Visitor *v, const char *name, double *obj,
  227. Error **errp)
  228. {
  229. StringOutputVisitor *sov = to_sov(v);
  230. string_output_set(sov, g_strdup_printf("%f", *obj));
  231. }
  232. static void
  233. start_list(Visitor *v, const char *name, GenericList **list, size_t size,
  234. Error **errp)
  235. {
  236. StringOutputVisitor *sov = to_sov(v);
  237. /* we can't traverse a list in a list */
  238. assert(sov->list_mode == LM_NONE);
  239. /* We don't support visits without a list */
  240. assert(list);
  241. sov->list = list;
  242. /* List handling is only needed if there are at least two elements */
  243. if (*list && (*list)->next) {
  244. sov->list_mode = LM_STARTED;
  245. }
  246. }
  247. static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
  248. {
  249. StringOutputVisitor *sov = to_sov(v);
  250. GenericList *ret = tail->next;
  251. if (ret && !ret->next) {
  252. sov->list_mode = LM_END;
  253. }
  254. return ret;
  255. }
  256. static void end_list(Visitor *v, void **obj)
  257. {
  258. StringOutputVisitor *sov = to_sov(v);
  259. assert(sov->list == obj);
  260. assert(sov->list_mode == LM_STARTED ||
  261. sov->list_mode == LM_END ||
  262. sov->list_mode == LM_NONE ||
  263. sov->list_mode == LM_IN_PROGRESS);
  264. sov->list_mode = LM_NONE;
  265. }
  266. static void string_output_complete(Visitor *v, void *opaque)
  267. {
  268. StringOutputVisitor *sov = to_sov(v);
  269. assert(opaque == sov->result);
  270. *sov->result = g_string_free(sov->string, false);
  271. sov->string = NULL;
  272. }
  273. static void free_range(void *range, void *dummy)
  274. {
  275. g_free(range);
  276. }
  277. static void string_output_free(Visitor *v)
  278. {
  279. StringOutputVisitor *sov = to_sov(v);
  280. if (sov->string) {
  281. g_string_free(sov->string, true);
  282. }
  283. g_list_foreach(sov->ranges, free_range, NULL);
  284. g_list_free(sov->ranges);
  285. g_free(sov);
  286. }
  287. Visitor *string_output_visitor_new(bool human, char **result)
  288. {
  289. StringOutputVisitor *v;
  290. v = g_malloc0(sizeof(*v));
  291. v->string = g_string_new(NULL);
  292. v->human = human;
  293. v->result = result;
  294. *result = NULL;
  295. v->visitor.type = VISITOR_OUTPUT;
  296. v->visitor.type_int64 = print_type_int64;
  297. v->visitor.type_uint64 = print_type_uint64;
  298. v->visitor.type_size = print_type_size;
  299. v->visitor.type_bool = print_type_bool;
  300. v->visitor.type_str = print_type_str;
  301. v->visitor.type_number = print_type_number;
  302. v->visitor.start_list = start_list;
  303. v->visitor.next_list = next_list;
  304. v->visitor.end_list = end_list;
  305. v->visitor.complete = string_output_complete;
  306. v->visitor.free = string_output_free;
  307. return &v->visitor;
  308. }