qemu-option.c 33 KB

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