2
0

string-output-visitor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. unsigned int struct_nesting;
  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. switch (sov->list_mode) {
  69. case LM_STARTED:
  70. sov->list_mode = LM_IN_PROGRESS;
  71. /* fall through */
  72. case LM_NONE:
  73. if (sov->string) {
  74. g_string_free(sov->string, true);
  75. }
  76. sov->string = g_string_new(string);
  77. g_free(string);
  78. break;
  79. case LM_IN_PROGRESS:
  80. case LM_END:
  81. g_string_append(sov->string, ", ");
  82. g_string_append(sov->string, string);
  83. break;
  84. default:
  85. abort();
  86. }
  87. }
  88. static void string_output_append(StringOutputVisitor *sov, int64_t a)
  89. {
  90. Range *r = g_malloc0(sizeof(*r));
  91. range_set_bounds(r, a, a);
  92. sov->ranges = range_list_insert(sov->ranges, r);
  93. }
  94. static void string_output_append_range(StringOutputVisitor *sov,
  95. int64_t s, int64_t e)
  96. {
  97. Range *r = g_malloc0(sizeof(*r));
  98. range_set_bounds(r, s, e);
  99. sov->ranges = range_list_insert(sov->ranges, r);
  100. }
  101. static void format_string(StringOutputVisitor *sov, Range *r, bool next,
  102. bool human)
  103. {
  104. if (range_lob(r) != range_upb(r)) {
  105. if (human) {
  106. g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
  107. range_lob(r), range_upb(r));
  108. } else {
  109. g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
  110. range_lob(r), range_upb(r));
  111. }
  112. } else {
  113. if (human) {
  114. g_string_append_printf(sov->string, "0x%" PRIx64, range_lob(r));
  115. } else {
  116. g_string_append_printf(sov->string, "%" PRId64, range_lob(r));
  117. }
  118. }
  119. if (next) {
  120. g_string_append(sov->string, ",");
  121. }
  122. }
  123. static bool print_type_int64(Visitor *v, const char *name, int64_t *obj,
  124. Error **errp)
  125. {
  126. StringOutputVisitor *sov = to_sov(v);
  127. GList *l;
  128. if (sov->struct_nesting) {
  129. return true;
  130. }
  131. switch (sov->list_mode) {
  132. case LM_NONE:
  133. string_output_append(sov, *obj);
  134. break;
  135. case LM_STARTED:
  136. sov->range_start.s = *obj;
  137. sov->range_end.s = *obj;
  138. sov->list_mode = LM_IN_PROGRESS;
  139. return true;
  140. case LM_IN_PROGRESS:
  141. if (sov->range_end.s + 1 == *obj) {
  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. sov->range_start.s = *obj;
  152. sov->range_end.s = *obj;
  153. }
  154. return true;
  155. case LM_END:
  156. if (sov->range_end.s + 1 == *obj) {
  157. sov->range_end.s++;
  158. assert(sov->range_start.s < sov->range_end.s);
  159. string_output_append_range(sov, sov->range_start.s,
  160. sov->range_end.s);
  161. } else {
  162. if (sov->range_start.s == sov->range_end.s) {
  163. string_output_append(sov, sov->range_end.s);
  164. } else {
  165. assert(sov->range_start.s < sov->range_end.s);
  166. string_output_append_range(sov, sov->range_start.s,
  167. sov->range_end.s);
  168. }
  169. string_output_append(sov, *obj);
  170. }
  171. break;
  172. default:
  173. abort();
  174. }
  175. l = sov->ranges;
  176. while (l) {
  177. Range *r = l->data;
  178. format_string(sov, r, l->next != NULL, false);
  179. l = l->next;
  180. }
  181. if (sov->human) {
  182. l = sov->ranges;
  183. g_string_append(sov->string, " (");
  184. while (l) {
  185. Range *r = l->data;
  186. format_string(sov, r, l->next != NULL, true);
  187. l = l->next;
  188. }
  189. g_string_append(sov->string, ")");
  190. }
  191. return true;
  192. }
  193. static bool print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
  194. Error **errp)
  195. {
  196. /* FIXME: print_type_int64 mishandles values over INT64_MAX */
  197. int64_t i = *obj;
  198. return print_type_int64(v, name, &i, errp);
  199. }
  200. static bool print_type_size(Visitor *v, const char *name, uint64_t *obj,
  201. Error **errp)
  202. {
  203. StringOutputVisitor *sov = to_sov(v);
  204. uint64_t val;
  205. char *out, *psize;
  206. if (sov->struct_nesting) {
  207. return true;
  208. }
  209. if (!sov->human) {
  210. out = g_strdup_printf("%"PRIu64, *obj);
  211. string_output_set(sov, out);
  212. return true;
  213. }
  214. val = *obj;
  215. psize = size_to_str(val);
  216. out = g_strdup_printf("%"PRIu64" (%s)", val, psize);
  217. string_output_set(sov, out);
  218. g_free(psize);
  219. return true;
  220. }
  221. static bool print_type_bool(Visitor *v, const char *name, bool *obj,
  222. Error **errp)
  223. {
  224. StringOutputVisitor *sov = to_sov(v);
  225. if (sov->struct_nesting) {
  226. return true;
  227. }
  228. string_output_set(sov, g_strdup(*obj ? "true" : "false"));
  229. return true;
  230. }
  231. static bool print_type_str(Visitor *v, const char *name, char **obj,
  232. Error **errp)
  233. {
  234. StringOutputVisitor *sov = to_sov(v);
  235. char *out;
  236. if (sov->struct_nesting) {
  237. return true;
  238. }
  239. if (sov->human) {
  240. out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
  241. } else {
  242. out = g_strdup(*obj ? *obj : "");
  243. }
  244. string_output_set(sov, out);
  245. return true;
  246. }
  247. static bool print_type_number(Visitor *v, const char *name, double *obj,
  248. Error **errp)
  249. {
  250. StringOutputVisitor *sov = to_sov(v);
  251. if (sov->struct_nesting) {
  252. return true;
  253. }
  254. string_output_set(sov, g_strdup_printf("%.17g", *obj));
  255. return true;
  256. }
  257. static bool print_type_null(Visitor *v, const char *name, QNull **obj,
  258. Error **errp)
  259. {
  260. StringOutputVisitor *sov = to_sov(v);
  261. char *out;
  262. if (sov->struct_nesting) {
  263. return true;
  264. }
  265. if (sov->human) {
  266. out = g_strdup("<null>");
  267. } else {
  268. out = g_strdup("");
  269. }
  270. string_output_set(sov, out);
  271. return true;
  272. }
  273. static bool start_struct(Visitor *v, const char *name, void **obj,
  274. size_t size, Error **errp)
  275. {
  276. StringOutputVisitor *sov = to_sov(v);
  277. sov->struct_nesting++;
  278. return true;
  279. }
  280. static void end_struct(Visitor *v, void **obj)
  281. {
  282. StringOutputVisitor *sov = to_sov(v);
  283. if (--sov->struct_nesting) {
  284. return;
  285. }
  286. /* TODO actually print struct fields */
  287. string_output_set(sov, g_strdup("<omitted>"));
  288. }
  289. static bool
  290. start_list(Visitor *v, const char *name, GenericList **list, size_t size,
  291. Error **errp)
  292. {
  293. StringOutputVisitor *sov = to_sov(v);
  294. if (sov->struct_nesting) {
  295. return true;
  296. }
  297. /* we can't traverse a list in a list */
  298. assert(sov->list_mode == LM_NONE);
  299. /* We don't support visits without a list */
  300. assert(list);
  301. sov->list = list;
  302. /* List handling is only needed if there are at least two elements */
  303. if (*list && (*list)->next) {
  304. sov->list_mode = LM_STARTED;
  305. }
  306. return true;
  307. }
  308. static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
  309. {
  310. StringOutputVisitor *sov = to_sov(v);
  311. GenericList *ret = tail->next;
  312. if (sov->struct_nesting) {
  313. return ret;
  314. }
  315. if (ret && !ret->next) {
  316. sov->list_mode = LM_END;
  317. }
  318. return ret;
  319. }
  320. static void end_list(Visitor *v, void **obj)
  321. {
  322. StringOutputVisitor *sov = to_sov(v);
  323. if (sov->struct_nesting) {
  324. return;
  325. }
  326. assert(sov->list == obj);
  327. assert(sov->list_mode == LM_STARTED ||
  328. sov->list_mode == LM_END ||
  329. sov->list_mode == LM_NONE ||
  330. sov->list_mode == LM_IN_PROGRESS);
  331. sov->list_mode = LM_NONE;
  332. }
  333. static void string_output_complete(Visitor *v, void *opaque)
  334. {
  335. StringOutputVisitor *sov = to_sov(v);
  336. assert(opaque == sov->result);
  337. *sov->result = g_string_free(sov->string, false);
  338. sov->string = NULL;
  339. }
  340. static void free_range(void *range, void *dummy)
  341. {
  342. g_free(range);
  343. }
  344. static void string_output_free(Visitor *v)
  345. {
  346. StringOutputVisitor *sov = to_sov(v);
  347. if (sov->string) {
  348. g_string_free(sov->string, true);
  349. }
  350. g_list_foreach(sov->ranges, free_range, NULL);
  351. g_list_free(sov->ranges);
  352. g_free(sov);
  353. }
  354. Visitor *string_output_visitor_new(bool human, char **result)
  355. {
  356. StringOutputVisitor *v;
  357. v = g_malloc0(sizeof(*v));
  358. v->string = g_string_new(NULL);
  359. v->human = human;
  360. v->result = result;
  361. *result = NULL;
  362. v->visitor.type = VISITOR_OUTPUT;
  363. v->visitor.type_int64 = print_type_int64;
  364. v->visitor.type_uint64 = print_type_uint64;
  365. v->visitor.type_size = print_type_size;
  366. v->visitor.type_bool = print_type_bool;
  367. v->visitor.type_str = print_type_str;
  368. v->visitor.type_number = print_type_number;
  369. v->visitor.type_null = print_type_null;
  370. v->visitor.start_struct = start_struct;
  371. v->visitor.end_struct = end_struct;
  372. v->visitor.start_list = start_list;
  373. v->visitor.next_list = next_list;
  374. v->visitor.end_list = end_list;
  375. v->visitor.complete = string_output_complete;
  376. v->visitor.free = string_output_free;
  377. return &v->visitor;
  378. }