2
0

qemu-option.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. /*
  2. * Commandline option parsing functions
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "qemu/error-report.h"
  28. #include "qapi/qmp/qbool.h"
  29. #include "qapi/qmp/qdict.h"
  30. #include "qapi/qmp/qnum.h"
  31. #include "qapi/qmp/qstring.h"
  32. #include "qapi/qmp/qerror.h"
  33. #include "qemu/option_int.h"
  34. #include "qemu/cutils.h"
  35. #include "qemu/id.h"
  36. #include "qemu/help_option.h"
  37. /*
  38. * Extracts the name of an option from the parameter string (@p points at the
  39. * first byte of the option name)
  40. *
  41. * The option name is @len characters long and is copied into @option. The
  42. * caller is responsible for free'ing @option when no longer required.
  43. *
  44. * The return value is the position of the delimiter/zero byte after the option
  45. * name in @p.
  46. */
  47. static const char *get_opt_name(const char *p, char **option, size_t len)
  48. {
  49. *option = g_strndup(p, len);
  50. return p + len;
  51. }
  52. /*
  53. * Extracts the value of an option from the parameter string p (p points at the
  54. * first byte of the option value)
  55. *
  56. * This function is comparable to get_opt_name with the difference that the
  57. * delimiter is fixed to be comma which starts a new option. To specify an
  58. * option value that contains commas, double each comma.
  59. */
  60. const char *get_opt_value(const char *p, char **value)
  61. {
  62. size_t capacity = 0, length;
  63. const char *offset;
  64. *value = NULL;
  65. while (1) {
  66. offset = qemu_strchrnul(p, ',');
  67. length = offset - p;
  68. if (*offset != '\0' && *(offset + 1) == ',') {
  69. length++;
  70. }
  71. *value = g_renew(char, *value, capacity + length + 1);
  72. strncpy(*value + capacity, p, length);
  73. (*value)[capacity + length] = '\0';
  74. capacity += length;
  75. if (*offset == '\0' ||
  76. *(offset + 1) != ',') {
  77. break;
  78. }
  79. p += (offset - p) + 2;
  80. }
  81. return offset;
  82. }
  83. static bool parse_option_number(const char *name, const char *value,
  84. uint64_t *ret, Error **errp)
  85. {
  86. uint64_t number;
  87. int err;
  88. err = qemu_strtou64(value, NULL, 0, &number);
  89. if (err == -ERANGE) {
  90. error_setg(errp, "Value '%s' is too large for parameter '%s'",
  91. value, name);
  92. return false;
  93. }
  94. if (err) {
  95. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
  96. return false;
  97. }
  98. *ret = number;
  99. return true;
  100. }
  101. static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
  102. const char *name)
  103. {
  104. int i;
  105. for (i = 0; desc[i].name != NULL; i++) {
  106. if (strcmp(desc[i].name, name) == 0) {
  107. return &desc[i];
  108. }
  109. }
  110. return NULL;
  111. }
  112. static const char *find_default_by_name(QemuOpts *opts, const char *name)
  113. {
  114. const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
  115. return desc ? desc->def_value_str : NULL;
  116. }
  117. bool parse_option_size(const char *name, const char *value,
  118. uint64_t *ret, Error **errp)
  119. {
  120. uint64_t size;
  121. int err;
  122. err = qemu_strtosz(value, NULL, &size);
  123. if (err == -ERANGE) {
  124. error_setg(errp, "Value '%s' is out of range for parameter '%s'",
  125. value, name);
  126. return false;
  127. }
  128. if (err) {
  129. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
  130. "a non-negative number below 2^64");
  131. error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
  132. " kilo-, mega-, giga-, tera-, peta-\n"
  133. "and exabytes, respectively.\n");
  134. return false;
  135. }
  136. *ret = size;
  137. return true;
  138. }
  139. static const char *opt_type_to_string(enum QemuOptType type)
  140. {
  141. switch (type) {
  142. case QEMU_OPT_STRING:
  143. return "str";
  144. case QEMU_OPT_BOOL:
  145. return "bool (on/off)";
  146. case QEMU_OPT_NUMBER:
  147. return "num";
  148. case QEMU_OPT_SIZE:
  149. return "size";
  150. }
  151. g_assert_not_reached();
  152. }
  153. /**
  154. * Print the list of options available in the given list. If
  155. * @print_caption is true, a caption (including the list name, if it
  156. * exists) is printed. The options itself will be indented, so
  157. * @print_caption should only be set to false if the caller prints its
  158. * own custom caption (so that the indentation makes sense).
  159. */
  160. void qemu_opts_print_help(QemuOptsList *list, bool print_caption)
  161. {
  162. QemuOptDesc *desc;
  163. int i;
  164. GPtrArray *array = g_ptr_array_new();
  165. assert(list);
  166. desc = list->desc;
  167. while (desc && desc->name) {
  168. GString *str = g_string_new(NULL);
  169. g_string_append_printf(str, " %s=<%s>", desc->name,
  170. opt_type_to_string(desc->type));
  171. if (desc->help) {
  172. if (str->len < 24) {
  173. g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
  174. }
  175. g_string_append_printf(str, " - %s", desc->help);
  176. }
  177. g_ptr_array_add(array, g_string_free(str, false));
  178. desc++;
  179. }
  180. g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
  181. if (print_caption && array->len > 0) {
  182. if (list->name) {
  183. printf("%s options:\n", list->name);
  184. } else {
  185. printf("Options:\n");
  186. }
  187. } else if (array->len == 0) {
  188. if (list->name) {
  189. printf("There are no options for %s.\n", list->name);
  190. } else {
  191. printf("No options available.\n");
  192. }
  193. }
  194. for (i = 0; i < array->len; i++) {
  195. printf("%s\n", (char *)array->pdata[i]);
  196. }
  197. g_ptr_array_set_free_func(array, g_free);
  198. g_ptr_array_free(array, true);
  199. }
  200. /* ------------------------------------------------------------------ */
  201. QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
  202. {
  203. QemuOpt *opt;
  204. QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
  205. if (strcmp(opt->name, name) != 0)
  206. continue;
  207. return opt;
  208. }
  209. return NULL;
  210. }
  211. static void qemu_opt_del(QemuOpt *opt)
  212. {
  213. QTAILQ_REMOVE(&opt->opts->head, opt, next);
  214. g_free(opt->name);
  215. g_free(opt->str);
  216. g_free(opt);
  217. }
  218. /* qemu_opt_set allows many settings for the same option.
  219. * This function deletes all settings for an option.
  220. */
  221. static void qemu_opt_del_all(QemuOpts *opts, const char *name)
  222. {
  223. QemuOpt *opt, *next_opt;
  224. QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
  225. if (!strcmp(opt->name, name)) {
  226. qemu_opt_del(opt);
  227. }
  228. }
  229. }
  230. const char *qemu_opt_get(QemuOpts *opts, const char *name)
  231. {
  232. QemuOpt *opt;
  233. if (opts == NULL) {
  234. return NULL;
  235. }
  236. opt = qemu_opt_find(opts, name);
  237. if (!opt) {
  238. return find_default_by_name(opts, name);
  239. }
  240. return opt->str;
  241. }
  242. void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
  243. {
  244. iter->opts = opts;
  245. iter->opt = QTAILQ_FIRST(&opts->head);
  246. iter->name = name;
  247. }
  248. const char *qemu_opt_iter_next(QemuOptsIter *iter)
  249. {
  250. QemuOpt *ret = iter->opt;
  251. if (iter->name) {
  252. while (ret && !g_str_equal(iter->name, ret->name)) {
  253. ret = QTAILQ_NEXT(ret, next);
  254. }
  255. }
  256. iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
  257. return ret ? ret->str : NULL;
  258. }
  259. /* Get a known option (or its default) and remove it from the list
  260. * all in one action. Return a malloced string of the option value.
  261. * Result must be freed by caller with g_free().
  262. */
  263. char *qemu_opt_get_del(QemuOpts *opts, const char *name)
  264. {
  265. QemuOpt *opt;
  266. char *str;
  267. if (opts == NULL) {
  268. return NULL;
  269. }
  270. opt = qemu_opt_find(opts, name);
  271. if (!opt) {
  272. return g_strdup(find_default_by_name(opts, name));
  273. }
  274. str = opt->str;
  275. opt->str = NULL;
  276. qemu_opt_del_all(opts, name);
  277. return str;
  278. }
  279. bool qemu_opt_has_help_opt(QemuOpts *opts)
  280. {
  281. QemuOpt *opt;
  282. QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
  283. if (is_help_option(opt->name)) {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
  290. bool defval, bool del)
  291. {
  292. QemuOpt *opt;
  293. const char *def_val;
  294. bool ret = defval;
  295. if (opts == NULL) {
  296. return ret;
  297. }
  298. opt = qemu_opt_find(opts, name);
  299. if (opt == NULL) {
  300. def_val = find_default_by_name(opts, name);
  301. if (def_val) {
  302. qapi_bool_parse(name, def_val, &ret, &error_abort);
  303. }
  304. return ret;
  305. }
  306. assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
  307. ret = opt->value.boolean;
  308. if (del) {
  309. qemu_opt_del_all(opts, name);
  310. }
  311. return ret;
  312. }
  313. bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
  314. {
  315. return qemu_opt_get_bool_helper(opts, name, defval, false);
  316. }
  317. bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
  318. {
  319. return qemu_opt_get_bool_helper(opts, name, defval, true);
  320. }
  321. static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
  322. uint64_t defval, bool del)
  323. {
  324. QemuOpt *opt;
  325. const char *def_val;
  326. uint64_t ret = defval;
  327. if (opts == NULL) {
  328. return ret;
  329. }
  330. opt = qemu_opt_find(opts, name);
  331. if (opt == NULL) {
  332. def_val = find_default_by_name(opts, name);
  333. if (def_val) {
  334. parse_option_number(name, def_val, &ret, &error_abort);
  335. }
  336. return ret;
  337. }
  338. assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
  339. ret = opt->value.uint;
  340. if (del) {
  341. qemu_opt_del_all(opts, name);
  342. }
  343. return ret;
  344. }
  345. uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
  346. {
  347. return qemu_opt_get_number_helper(opts, name, defval, false);
  348. }
  349. uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
  350. uint64_t defval)
  351. {
  352. return qemu_opt_get_number_helper(opts, name, defval, true);
  353. }
  354. static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
  355. uint64_t defval, bool del)
  356. {
  357. QemuOpt *opt;
  358. const char *def_val;
  359. uint64_t ret = defval;
  360. if (opts == NULL) {
  361. return ret;
  362. }
  363. opt = qemu_opt_find(opts, name);
  364. if (opt == NULL) {
  365. def_val = find_default_by_name(opts, name);
  366. if (def_val) {
  367. parse_option_size(name, def_val, &ret, &error_abort);
  368. }
  369. return ret;
  370. }
  371. assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
  372. ret = opt->value.uint;
  373. if (del) {
  374. qemu_opt_del_all(opts, name);
  375. }
  376. return ret;
  377. }
  378. uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
  379. {
  380. return qemu_opt_get_size_helper(opts, name, defval, false);
  381. }
  382. uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
  383. uint64_t defval)
  384. {
  385. return qemu_opt_get_size_helper(opts, name, defval, true);
  386. }
  387. static bool qemu_opt_parse(QemuOpt *opt, Error **errp)
  388. {
  389. if (opt->desc == NULL)
  390. return true;
  391. switch (opt->desc->type) {
  392. case QEMU_OPT_STRING:
  393. /* nothing */
  394. return true;
  395. case QEMU_OPT_BOOL:
  396. return qapi_bool_parse(opt->name, opt->str, &opt->value.boolean, errp);
  397. case QEMU_OPT_NUMBER:
  398. return parse_option_number(opt->name, opt->str, &opt->value.uint,
  399. errp);
  400. case QEMU_OPT_SIZE:
  401. return parse_option_size(opt->name, opt->str, &opt->value.uint,
  402. errp);
  403. default:
  404. abort();
  405. }
  406. }
  407. static bool opts_accepts_any(const QemuOptsList *list)
  408. {
  409. return list->desc[0].name == NULL;
  410. }
  411. int qemu_opt_unset(QemuOpts *opts, const char *name)
  412. {
  413. QemuOpt *opt = qemu_opt_find(opts, name);
  414. assert(opts_accepts_any(opts->list));
  415. if (opt == NULL) {
  416. return -1;
  417. } else {
  418. qemu_opt_del(opt);
  419. return 0;
  420. }
  421. }
  422. static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value)
  423. {
  424. QemuOpt *opt = g_malloc0(sizeof(*opt));
  425. opt->name = g_strdup(name);
  426. opt->str = value;
  427. opt->opts = opts;
  428. QTAILQ_INSERT_TAIL(&opts->head, opt, next);
  429. return opt;
  430. }
  431. static bool opt_validate(QemuOpt *opt, Error **errp)
  432. {
  433. const QemuOptDesc *desc;
  434. const QemuOptsList *list = opt->opts->list;
  435. desc = find_desc_by_name(list->desc, opt->name);
  436. if (!desc && !opts_accepts_any(list)) {
  437. error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
  438. return false;
  439. }
  440. opt->desc = desc;
  441. if (!qemu_opt_parse(opt, errp)) {
  442. return false;
  443. }
  444. return true;
  445. }
  446. bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
  447. Error **errp)
  448. {
  449. QemuOpt *opt = opt_create(opts, name, g_strdup(value));
  450. if (!opt_validate(opt, errp)) {
  451. qemu_opt_del(opt);
  452. return false;
  453. }
  454. return true;
  455. }
  456. bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
  457. Error **errp)
  458. {
  459. QemuOpt *opt;
  460. const QemuOptDesc *desc;
  461. const QemuOptsList *list = opts->list;
  462. desc = find_desc_by_name(list->desc, name);
  463. if (!desc && !opts_accepts_any(list)) {
  464. error_setg(errp, QERR_INVALID_PARAMETER, name);
  465. return false;
  466. }
  467. opt = g_malloc0(sizeof(*opt));
  468. opt->name = g_strdup(name);
  469. opt->opts = opts;
  470. opt->desc = desc;
  471. opt->value.boolean = !!val;
  472. opt->str = g_strdup(val ? "on" : "off");
  473. QTAILQ_INSERT_TAIL(&opts->head, opt, next);
  474. return true;
  475. }
  476. bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
  477. Error **errp)
  478. {
  479. QemuOpt *opt;
  480. const QemuOptDesc *desc;
  481. const QemuOptsList *list = opts->list;
  482. desc = find_desc_by_name(list->desc, name);
  483. if (!desc && !opts_accepts_any(list)) {
  484. error_setg(errp, QERR_INVALID_PARAMETER, name);
  485. return false;
  486. }
  487. opt = g_malloc0(sizeof(*opt));
  488. opt->name = g_strdup(name);
  489. opt->opts = opts;
  490. opt->desc = desc;
  491. opt->value.uint = val;
  492. opt->str = g_strdup_printf("%" PRId64, val);
  493. QTAILQ_INSERT_TAIL(&opts->head, opt, next);
  494. return true;
  495. }
  496. /**
  497. * For each member of @opts, call @func(@opaque, name, value, @errp).
  498. * @func() may store an Error through @errp, but must return non-zero then.
  499. * When @func() returns non-zero, break the loop and return that value.
  500. * Return zero when the loop completes.
  501. */
  502. int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
  503. Error **errp)
  504. {
  505. QemuOpt *opt;
  506. int rc;
  507. QTAILQ_FOREACH(opt, &opts->head, next) {
  508. rc = func(opaque, opt->name, opt->str, errp);
  509. if (rc) {
  510. return rc;
  511. }
  512. assert(!errp || !*errp);
  513. }
  514. return 0;
  515. }
  516. QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
  517. {
  518. QemuOpts *opts;
  519. QTAILQ_FOREACH(opts, &list->head, next) {
  520. if (!opts->id && !id) {
  521. return opts;
  522. }
  523. if (opts->id && id && !strcmp(opts->id, id)) {
  524. return opts;
  525. }
  526. }
  527. return NULL;
  528. }
  529. QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
  530. int fail_if_exists, Error **errp)
  531. {
  532. QemuOpts *opts = NULL;
  533. if (list->merge_lists) {
  534. if (id) {
  535. error_setg(errp, QERR_INVALID_PARAMETER, "id");
  536. return NULL;
  537. }
  538. opts = qemu_opts_find(list, NULL);
  539. if (opts) {
  540. return opts;
  541. }
  542. } else if (id) {
  543. assert(fail_if_exists);
  544. if (!id_wellformed(id)) {
  545. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
  546. "an identifier");
  547. error_append_hint(errp, "Identifiers consist of letters, digits, "
  548. "'-', '.', '_', starting with a letter.\n");
  549. return NULL;
  550. }
  551. opts = qemu_opts_find(list, id);
  552. if (opts != NULL) {
  553. error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
  554. return NULL;
  555. }
  556. }
  557. opts = g_malloc0(sizeof(*opts));
  558. opts->id = g_strdup(id);
  559. opts->list = list;
  560. loc_save(&opts->loc);
  561. QTAILQ_INIT(&opts->head);
  562. QTAILQ_INSERT_TAIL(&list->head, opts, next);
  563. return opts;
  564. }
  565. void qemu_opts_reset(QemuOptsList *list)
  566. {
  567. QemuOpts *opts, *next_opts;
  568. QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
  569. qemu_opts_del(opts);
  570. }
  571. }
  572. void qemu_opts_loc_restore(QemuOpts *opts)
  573. {
  574. loc_restore(&opts->loc);
  575. }
  576. const char *qemu_opts_id(QemuOpts *opts)
  577. {
  578. return opts->id;
  579. }
  580. /* The id string will be g_free()d by qemu_opts_del */
  581. void qemu_opts_set_id(QemuOpts *opts, char *id)
  582. {
  583. opts->id = id;
  584. }
  585. void qemu_opts_del(QemuOpts *opts)
  586. {
  587. QemuOpt *opt;
  588. if (opts == NULL) {
  589. return;
  590. }
  591. for (;;) {
  592. opt = QTAILQ_FIRST(&opts->head);
  593. if (opt == NULL)
  594. break;
  595. qemu_opt_del(opt);
  596. }
  597. QTAILQ_REMOVE(&opts->list->head, opts, next);
  598. g_free(opts->id);
  599. g_free(opts);
  600. }
  601. /* print value, escaping any commas in value */
  602. static void escaped_print(const char *value)
  603. {
  604. const char *ptr;
  605. for (ptr = value; *ptr; ++ptr) {
  606. if (*ptr == ',') {
  607. putchar(',');
  608. }
  609. putchar(*ptr);
  610. }
  611. }
  612. void qemu_opts_print(QemuOpts *opts, const char *separator)
  613. {
  614. QemuOpt *opt;
  615. QemuOptDesc *desc = opts->list->desc;
  616. const char *sep = "";
  617. if (opts->id) {
  618. printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
  619. sep = separator;
  620. }
  621. if (desc[0].name == NULL) {
  622. QTAILQ_FOREACH(opt, &opts->head, next) {
  623. printf("%s%s=", sep, opt->name);
  624. escaped_print(opt->str);
  625. sep = separator;
  626. }
  627. return;
  628. }
  629. for (; desc && desc->name; desc++) {
  630. const char *value;
  631. opt = qemu_opt_find(opts, desc->name);
  632. value = opt ? opt->str : desc->def_value_str;
  633. if (!value) {
  634. continue;
  635. }
  636. if (desc->type == QEMU_OPT_STRING) {
  637. printf("%s%s=", sep, desc->name);
  638. escaped_print(value);
  639. } else if ((desc->type == QEMU_OPT_SIZE ||
  640. desc->type == QEMU_OPT_NUMBER) && opt) {
  641. printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
  642. } else {
  643. printf("%s%s=%s", sep, desc->name, value);
  644. }
  645. sep = separator;
  646. }
  647. }
  648. static const char *get_opt_name_value(const char *params,
  649. const char *firstname,
  650. bool warn_on_flag,
  651. bool *help_wanted,
  652. char **name, char **value)
  653. {
  654. const char *p;
  655. const char *prefix = "";
  656. size_t len;
  657. bool is_help = false;
  658. len = strcspn(params, "=,");
  659. if (params[len] != '=') {
  660. /* found "foo,more" */
  661. if (firstname) {
  662. /* implicitly named first option */
  663. *name = g_strdup(firstname);
  664. p = get_opt_value(params, value);
  665. } else {
  666. /* option without value, must be a flag */
  667. p = get_opt_name(params, name, len);
  668. if (strncmp(*name, "no", 2) == 0) {
  669. memmove(*name, *name + 2, strlen(*name + 2) + 1);
  670. *value = g_strdup("off");
  671. prefix = "no";
  672. } else {
  673. *value = g_strdup("on");
  674. is_help = is_help_option(*name);
  675. }
  676. if (!is_help && warn_on_flag) {
  677. warn_report("short-form boolean option '%s%s' deprecated", prefix, *name);
  678. if (g_str_equal(*name, "delay")) {
  679. error_printf("Please use nodelay=%s instead\n", prefix[0] ? "on" : "off");
  680. } else {
  681. error_printf("Please use %s=%s instead\n", *name, *value);
  682. }
  683. }
  684. }
  685. } else {
  686. /* found "foo=bar,more" */
  687. p = get_opt_name(params, name, len);
  688. assert(*p == '=');
  689. p++;
  690. p = get_opt_value(p, value);
  691. }
  692. assert(!*p || *p == ',');
  693. if (help_wanted && is_help) {
  694. *help_wanted = true;
  695. }
  696. if (*p == ',') {
  697. p++;
  698. }
  699. return p;
  700. }
  701. static bool opts_do_parse(QemuOpts *opts, const char *params,
  702. const char *firstname,
  703. bool warn_on_flag, bool *help_wanted, Error **errp)
  704. {
  705. char *option, *value;
  706. const char *p;
  707. QemuOpt *opt;
  708. for (p = params; *p;) {
  709. p = get_opt_name_value(p, firstname, warn_on_flag, help_wanted, &option, &value);
  710. if (help_wanted && *help_wanted) {
  711. g_free(option);
  712. g_free(value);
  713. return false;
  714. }
  715. firstname = NULL;
  716. if (!strcmp(option, "id")) {
  717. g_free(option);
  718. g_free(value);
  719. continue;
  720. }
  721. opt = opt_create(opts, option, value);
  722. g_free(option);
  723. if (!opt_validate(opt, errp)) {
  724. qemu_opt_del(opt);
  725. return false;
  726. }
  727. }
  728. return true;
  729. }
  730. static char *opts_parse_id(const char *params)
  731. {
  732. const char *p;
  733. char *name, *value;
  734. for (p = params; *p;) {
  735. p = get_opt_name_value(p, NULL, false, NULL, &name, &value);
  736. if (!strcmp(name, "id")) {
  737. g_free(name);
  738. return value;
  739. }
  740. g_free(name);
  741. g_free(value);
  742. }
  743. return NULL;
  744. }
  745. bool has_help_option(const char *params)
  746. {
  747. const char *p;
  748. char *name, *value;
  749. bool ret = false;
  750. for (p = params; *p;) {
  751. p = get_opt_name_value(p, NULL, false, &ret, &name, &value);
  752. g_free(name);
  753. g_free(value);
  754. if (ret) {
  755. return true;
  756. }
  757. }
  758. return false;
  759. }
  760. /**
  761. * Store options parsed from @params into @opts.
  762. * If @firstname is non-null, the first key=value in @params may omit
  763. * key=, and is treated as if key was @firstname.
  764. * On error, store an error object through @errp if non-null.
  765. */
  766. bool qemu_opts_do_parse(QemuOpts *opts, const char *params,
  767. const char *firstname, Error **errp)
  768. {
  769. return opts_do_parse(opts, params, firstname, false, NULL, errp);
  770. }
  771. static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
  772. bool permit_abbrev,
  773. bool warn_on_flag, bool *help_wanted, Error **errp)
  774. {
  775. const char *firstname;
  776. char *id = opts_parse_id(params);
  777. QemuOpts *opts;
  778. assert(!permit_abbrev || list->implied_opt_name);
  779. firstname = permit_abbrev ? list->implied_opt_name : NULL;
  780. opts = qemu_opts_create(list, id, !list->merge_lists, errp);
  781. g_free(id);
  782. if (opts == NULL) {
  783. return NULL;
  784. }
  785. if (!opts_do_parse(opts, params, firstname,
  786. warn_on_flag, help_wanted, errp)) {
  787. qemu_opts_del(opts);
  788. return NULL;
  789. }
  790. return opts;
  791. }
  792. /**
  793. * Create a QemuOpts in @list and with options parsed from @params.
  794. * If @permit_abbrev, the first key=value in @params may omit key=,
  795. * and is treated as if key was @list->implied_opt_name.
  796. * On error, store an error object through @errp if non-null.
  797. * Return the new QemuOpts on success, null pointer on error.
  798. */
  799. QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
  800. bool permit_abbrev, Error **errp)
  801. {
  802. return opts_parse(list, params, permit_abbrev, false, NULL, errp);
  803. }
  804. /**
  805. * Create a QemuOpts in @list and with options parsed from @params.
  806. * If @permit_abbrev, the first key=value in @params may omit key=,
  807. * and is treated as if key was @list->implied_opt_name.
  808. * Report errors with error_report_err(). This is inappropriate in
  809. * QMP context. Do not use this function there!
  810. * Return the new QemuOpts on success, null pointer on error.
  811. */
  812. QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
  813. bool permit_abbrev)
  814. {
  815. Error *err = NULL;
  816. QemuOpts *opts;
  817. bool help_wanted = false;
  818. opts = opts_parse(list, params, permit_abbrev, true,
  819. opts_accepts_any(list) ? NULL : &help_wanted,
  820. &err);
  821. if (!opts) {
  822. assert(!!err + !!help_wanted == 1);
  823. if (help_wanted) {
  824. qemu_opts_print_help(list, true);
  825. } else {
  826. error_report_err(err);
  827. }
  828. }
  829. return opts;
  830. }
  831. static bool qemu_opts_from_qdict_entry(QemuOpts *opts,
  832. const QDictEntry *entry,
  833. Error **errp)
  834. {
  835. const char *key = qdict_entry_key(entry);
  836. QObject *obj = qdict_entry_value(entry);
  837. char buf[32];
  838. g_autofree char *tmp = NULL;
  839. const char *value;
  840. if (!strcmp(key, "id")) {
  841. return true;
  842. }
  843. switch (qobject_type(obj)) {
  844. case QTYPE_QSTRING:
  845. value = qstring_get_str(qobject_to(QString, obj));
  846. break;
  847. case QTYPE_QNUM:
  848. tmp = qnum_to_string(qobject_to(QNum, obj));
  849. value = tmp;
  850. break;
  851. case QTYPE_QBOOL:
  852. pstrcpy(buf, sizeof(buf),
  853. qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
  854. value = buf;
  855. break;
  856. default:
  857. return true;
  858. }
  859. return qemu_opt_set(opts, key, value, errp);
  860. }
  861. /*
  862. * Create QemuOpts from a QDict.
  863. * Use value of key "id" as ID if it exists and is a QString. Only
  864. * QStrings, QNums and QBools are copied. Entries with other types
  865. * are silently ignored.
  866. */
  867. QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
  868. Error **errp)
  869. {
  870. QemuOpts *opts;
  871. const QDictEntry *entry;
  872. opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
  873. if (!opts) {
  874. return NULL;
  875. }
  876. for (entry = qdict_first(qdict);
  877. entry;
  878. entry = qdict_next(qdict, entry)) {
  879. if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
  880. qemu_opts_del(opts);
  881. return NULL;
  882. }
  883. }
  884. return opts;
  885. }
  886. /*
  887. * Adds all QDict entries to the QemuOpts that can be added and removes them
  888. * from the QDict. When this function returns, the QDict contains only those
  889. * entries that couldn't be added to the QemuOpts.
  890. */
  891. bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
  892. {
  893. const QDictEntry *entry, *next;
  894. entry = qdict_first(qdict);
  895. while (entry != NULL) {
  896. next = qdict_next(qdict, entry);
  897. if (opts_accepts_any(opts->list) ||
  898. find_desc_by_name(opts->list->desc, entry->key)) {
  899. if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
  900. return false;
  901. }
  902. qdict_del(qdict, entry->key);
  903. }
  904. entry = next;
  905. }
  906. return true;
  907. }
  908. /*
  909. * Convert from QemuOpts to QDict. The QDict values are of type QString.
  910. *
  911. * If @list is given, only add those options to the QDict that are contained in
  912. * the list. If @del is true, any options added to the QDict are removed from
  913. * the QemuOpts, otherwise they remain there.
  914. *
  915. * If two options in @opts have the same name, they are processed in order
  916. * so that the last one wins (consistent with the reverse iteration in
  917. * qemu_opt_find()), but all of them are deleted if @del is true.
  918. *
  919. * TODO We'll want to use types appropriate for opt->desc->type, but
  920. * this is enough for now.
  921. */
  922. QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
  923. QemuOptsList *list, bool del)
  924. {
  925. QemuOpt *opt, *next;
  926. if (!qdict) {
  927. qdict = qdict_new();
  928. }
  929. if (opts->id) {
  930. qdict_put_str(qdict, "id", opts->id);
  931. }
  932. QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
  933. if (list) {
  934. QemuOptDesc *desc;
  935. bool found = false;
  936. for (desc = list->desc; desc->name; desc++) {
  937. if (!strcmp(desc->name, opt->name)) {
  938. found = true;
  939. break;
  940. }
  941. }
  942. if (!found) {
  943. continue;
  944. }
  945. }
  946. qdict_put_str(qdict, opt->name, opt->str);
  947. if (del) {
  948. qemu_opt_del(opt);
  949. }
  950. }
  951. return qdict;
  952. }
  953. /* Copy all options in a QemuOpts to the given QDict. See
  954. * qemu_opts_to_qdict_filtered() for details. */
  955. QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
  956. {
  957. return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
  958. }
  959. /* Validate parsed opts against descriptions where no
  960. * descriptions were provided in the QemuOptsList.
  961. */
  962. bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
  963. {
  964. QemuOpt *opt;
  965. assert(opts_accepts_any(opts->list));
  966. QTAILQ_FOREACH(opt, &opts->head, next) {
  967. opt->desc = find_desc_by_name(desc, opt->name);
  968. if (!opt->desc) {
  969. error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
  970. return false;
  971. }
  972. if (!qemu_opt_parse(opt, errp)) {
  973. return false;
  974. }
  975. }
  976. return true;
  977. }
  978. /**
  979. * For each member of @list, call @func(@opaque, member, @errp).
  980. * Call it with the current location temporarily set to the member's.
  981. * @func() may store an Error through @errp, but must return non-zero then.
  982. * When @func() returns non-zero, break the loop and return that value.
  983. * Return zero when the loop completes.
  984. */
  985. int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
  986. void *opaque, Error **errp)
  987. {
  988. Location loc;
  989. QemuOpts *opts, *next;
  990. int rc = 0;
  991. loc_push_none(&loc);
  992. QTAILQ_FOREACH_SAFE(opts, &list->head, next, next) {
  993. loc_restore(&opts->loc);
  994. rc = func(opaque, opts, errp);
  995. if (rc) {
  996. break;
  997. }
  998. assert(!errp || !*errp);
  999. }
  1000. loc_pop(&loc);
  1001. return rc;
  1002. }
  1003. static size_t count_opts_list(QemuOptsList *list)
  1004. {
  1005. QemuOptDesc *desc = NULL;
  1006. size_t num_opts = 0;
  1007. if (!list) {
  1008. return 0;
  1009. }
  1010. desc = list->desc;
  1011. while (desc && desc->name) {
  1012. num_opts++;
  1013. desc++;
  1014. }
  1015. return num_opts;
  1016. }
  1017. void qemu_opts_free(QemuOptsList *list)
  1018. {
  1019. g_free(list);
  1020. }
  1021. /* Realloc dst option list and append options from an option list (list)
  1022. * to it. dst could be NULL or a malloced list.
  1023. * The lifetime of dst must be shorter than the input list because the
  1024. * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
  1025. */
  1026. QemuOptsList *qemu_opts_append(QemuOptsList *dst,
  1027. QemuOptsList *list)
  1028. {
  1029. size_t num_opts, num_dst_opts;
  1030. QemuOptDesc *desc;
  1031. bool need_init = false;
  1032. bool need_head_update;
  1033. if (!list) {
  1034. return dst;
  1035. }
  1036. /* If dst is NULL, after realloc, some area of dst should be initialized
  1037. * before adding options to it.
  1038. */
  1039. if (!dst) {
  1040. need_init = true;
  1041. need_head_update = true;
  1042. } else {
  1043. /* Moreover, even if dst is not NULL, the realloc may move it to a
  1044. * different address in which case we may get a stale tail pointer
  1045. * in dst->head. */
  1046. need_head_update = QTAILQ_EMPTY(&dst->head);
  1047. }
  1048. num_opts = count_opts_list(dst);
  1049. num_dst_opts = num_opts;
  1050. num_opts += count_opts_list(list);
  1051. dst = g_realloc(dst, sizeof(QemuOptsList) +
  1052. (num_opts + 1) * sizeof(QemuOptDesc));
  1053. if (need_init) {
  1054. dst->name = NULL;
  1055. dst->implied_opt_name = NULL;
  1056. dst->merge_lists = false;
  1057. }
  1058. if (need_head_update) {
  1059. QTAILQ_INIT(&dst->head);
  1060. }
  1061. dst->desc[num_dst_opts].name = NULL;
  1062. /* append list->desc to dst->desc */
  1063. if (list) {
  1064. desc = list->desc;
  1065. while (desc && desc->name) {
  1066. if (find_desc_by_name(dst->desc, desc->name) == NULL) {
  1067. dst->desc[num_dst_opts++] = *desc;
  1068. dst->desc[num_dst_opts].name = NULL;
  1069. }
  1070. desc++;
  1071. }
  1072. }
  1073. return dst;
  1074. }