root / qemu-option.c @ cf62adfa
History | View | Annotate | Download (27.2 kB)
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 |
|
26 |
#include <stdio.h> |
27 |
#include <string.h> |
28 |
|
29 |
#include "qemu-common.h" |
30 |
#include "qemu-error.h" |
31 |
#include "qemu-objects.h" |
32 |
#include "qemu-option.h" |
33 |
#include "error.h" |
34 |
#include "qerror.h" |
35 |
|
36 |
/*
|
37 |
* Extracts the name of an option from the parameter string (p points at the
|
38 |
* first byte of the option name)
|
39 |
*
|
40 |
* The option name is delimited by delim (usually , or =) or the string end
|
41 |
* and is copied into buf. If the option name is longer than buf_size, it is
|
42 |
* truncated. buf is always zero terminated.
|
43 |
*
|
44 |
* The return value is the position of the delimiter/zero byte after the option
|
45 |
* name in p.
|
46 |
*/
|
47 |
const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) |
48 |
{ |
49 |
char *q;
|
50 |
|
51 |
q = buf; |
52 |
while (*p != '\0' && *p != delim) { |
53 |
if (q && (q - buf) < buf_size - 1) |
54 |
*q++ = *p; |
55 |
p++; |
56 |
} |
57 |
if (q)
|
58 |
*q = '\0';
|
59 |
|
60 |
return p;
|
61 |
} |
62 |
|
63 |
/*
|
64 |
* Extracts the value of an option from the parameter string p (p points at the
|
65 |
* first byte of the option value)
|
66 |
*
|
67 |
* This function is comparable to get_opt_name with the difference that the
|
68 |
* delimiter is fixed to be comma which starts a new option. To specify an
|
69 |
* option value that contains commas, double each comma.
|
70 |
*/
|
71 |
const char *get_opt_value(char *buf, int buf_size, const char *p) |
72 |
{ |
73 |
char *q;
|
74 |
|
75 |
q = buf; |
76 |
while (*p != '\0') { |
77 |
if (*p == ',') { |
78 |
if (*(p + 1) != ',') |
79 |
break;
|
80 |
p++; |
81 |
} |
82 |
if (q && (q - buf) < buf_size - 1) |
83 |
*q++ = *p; |
84 |
p++; |
85 |
} |
86 |
if (q)
|
87 |
*q = '\0';
|
88 |
|
89 |
return p;
|
90 |
} |
91 |
|
92 |
int get_next_param_value(char *buf, int buf_size, |
93 |
const char *tag, const char **pstr) |
94 |
{ |
95 |
const char *p; |
96 |
char option[128]; |
97 |
|
98 |
p = *pstr; |
99 |
for(;;) {
|
100 |
p = get_opt_name(option, sizeof(option), p, '='); |
101 |
if (*p != '=') |
102 |
break;
|
103 |
p++; |
104 |
if (!strcmp(tag, option)) {
|
105 |
*pstr = get_opt_value(buf, buf_size, p); |
106 |
if (**pstr == ',') { |
107 |
(*pstr)++; |
108 |
} |
109 |
return strlen(buf);
|
110 |
} else {
|
111 |
p = get_opt_value(NULL, 0, p); |
112 |
} |
113 |
if (*p != ',') |
114 |
break;
|
115 |
p++; |
116 |
} |
117 |
return 0; |
118 |
} |
119 |
|
120 |
int get_param_value(char *buf, int buf_size, |
121 |
const char *tag, const char *str) |
122 |
{ |
123 |
return get_next_param_value(buf, buf_size, tag, &str);
|
124 |
} |
125 |
|
126 |
int check_params(char *buf, int buf_size, |
127 |
const char * const *params, const char *str) |
128 |
{ |
129 |
const char *p; |
130 |
int i;
|
131 |
|
132 |
p = str; |
133 |
while (*p != '\0') { |
134 |
p = get_opt_name(buf, buf_size, p, '=');
|
135 |
if (*p != '=') { |
136 |
return -1; |
137 |
} |
138 |
p++; |
139 |
for (i = 0; params[i] != NULL; i++) { |
140 |
if (!strcmp(params[i], buf)) {
|
141 |
break;
|
142 |
} |
143 |
} |
144 |
if (params[i] == NULL) { |
145 |
return -1; |
146 |
} |
147 |
p = get_opt_value(NULL, 0, p); |
148 |
if (*p != ',') { |
149 |
break;
|
150 |
} |
151 |
p++; |
152 |
} |
153 |
return 0; |
154 |
} |
155 |
|
156 |
/*
|
157 |
* Searches an option list for an option with the given name
|
158 |
*/
|
159 |
QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, |
160 |
const char *name) |
161 |
{ |
162 |
while (list && list->name) {
|
163 |
if (!strcmp(list->name, name)) {
|
164 |
return list;
|
165 |
} |
166 |
list++; |
167 |
} |
168 |
|
169 |
return NULL; |
170 |
} |
171 |
|
172 |
static void parse_option_bool(const char *name, const char *value, bool *ret, |
173 |
Error **errp) |
174 |
{ |
175 |
if (value != NULL) { |
176 |
if (!strcmp(value, "on")) { |
177 |
*ret = 1;
|
178 |
} else if (!strcmp(value, "off")) { |
179 |
*ret = 0;
|
180 |
} else {
|
181 |
error_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
|
182 |
} |
183 |
} else {
|
184 |
*ret = 1;
|
185 |
} |
186 |
} |
187 |
|
188 |
static void parse_option_number(const char *name, const char *value, |
189 |
uint64_t *ret, Error **errp) |
190 |
{ |
191 |
char *postfix;
|
192 |
uint64_t number; |
193 |
|
194 |
if (value != NULL) { |
195 |
number = strtoull(value, &postfix, 0);
|
196 |
if (*postfix != '\0') { |
197 |
error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
|
198 |
return;
|
199 |
} |
200 |
*ret = number; |
201 |
} else {
|
202 |
error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
|
203 |
} |
204 |
} |
205 |
|
206 |
static int parse_option_size(const char *name, const char *value, uint64_t *ret) |
207 |
{ |
208 |
char *postfix;
|
209 |
double sizef;
|
210 |
|
211 |
if (value != NULL) { |
212 |
sizef = strtod(value, &postfix); |
213 |
switch (*postfix) {
|
214 |
case 'T': |
215 |
sizef *= 1024;
|
216 |
/* fall through */
|
217 |
case 'G': |
218 |
sizef *= 1024;
|
219 |
/* fall through */
|
220 |
case 'M': |
221 |
sizef *= 1024;
|
222 |
/* fall through */
|
223 |
case 'K': |
224 |
case 'k': |
225 |
sizef *= 1024;
|
226 |
/* fall through */
|
227 |
case 'b': |
228 |
case '\0': |
229 |
*ret = (uint64_t) sizef; |
230 |
break;
|
231 |
default:
|
232 |
qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
|
233 |
error_printf_unless_qmp("You may use k, M, G or T suffixes for "
|
234 |
"kilobytes, megabytes, gigabytes and terabytes.\n");
|
235 |
return -1; |
236 |
} |
237 |
} else {
|
238 |
qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
|
239 |
return -1; |
240 |
} |
241 |
return 0; |
242 |
} |
243 |
|
244 |
/*
|
245 |
* Sets the value of a parameter in a given option list. The parsing of the
|
246 |
* value depends on the type of option:
|
247 |
*
|
248 |
* OPT_FLAG (uses value.n):
|
249 |
* If no value is given, the flag is set to 1.
|
250 |
* Otherwise the value must be "on" (set to 1) or "off" (set to 0)
|
251 |
*
|
252 |
* OPT_STRING (uses value.s):
|
253 |
* value is strdup()ed and assigned as option value
|
254 |
*
|
255 |
* OPT_SIZE (uses value.n):
|
256 |
* The value is converted to an integer. Suffixes for kilobytes etc. are
|
257 |
* allowed (powers of 1024).
|
258 |
*
|
259 |
* Returns 0 on succes, -1 in error cases
|
260 |
*/
|
261 |
int set_option_parameter(QEMUOptionParameter *list, const char *name, |
262 |
const char *value) |
263 |
{ |
264 |
bool flag;
|
265 |
Error *local_err = NULL;
|
266 |
|
267 |
// Find a matching parameter
|
268 |
list = get_option_parameter(list, name); |
269 |
if (list == NULL) { |
270 |
fprintf(stderr, "Unknown option '%s'\n", name);
|
271 |
return -1; |
272 |
} |
273 |
|
274 |
// Process parameter
|
275 |
switch (list->type) {
|
276 |
case OPT_FLAG:
|
277 |
parse_option_bool(name, value, &flag, &local_err); |
278 |
if (!error_is_set(&local_err)) {
|
279 |
list->value.n = flag; |
280 |
} |
281 |
break;
|
282 |
|
283 |
case OPT_STRING:
|
284 |
if (value != NULL) { |
285 |
list->value.s = g_strdup(value); |
286 |
} else {
|
287 |
fprintf(stderr, "Option '%s' needs a parameter\n", name);
|
288 |
return -1; |
289 |
} |
290 |
break;
|
291 |
|
292 |
case OPT_SIZE:
|
293 |
if (parse_option_size(name, value, &list->value.n) == -1) |
294 |
return -1; |
295 |
break;
|
296 |
|
297 |
default:
|
298 |
fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
|
299 |
return -1; |
300 |
} |
301 |
|
302 |
if (error_is_set(&local_err)) {
|
303 |
qerror_report_err(local_err); |
304 |
error_free(local_err); |
305 |
return -1; |
306 |
} |
307 |
|
308 |
return 0; |
309 |
} |
310 |
|
311 |
/*
|
312 |
* Sets the given parameter to an integer instead of a string.
|
313 |
* This function cannot be used to set string options.
|
314 |
*
|
315 |
* Returns 0 on success, -1 in error cases
|
316 |
*/
|
317 |
int set_option_parameter_int(QEMUOptionParameter *list, const char *name, |
318 |
uint64_t value) |
319 |
{ |
320 |
// Find a matching parameter
|
321 |
list = get_option_parameter(list, name); |
322 |
if (list == NULL) { |
323 |
fprintf(stderr, "Unknown option '%s'\n", name);
|
324 |
return -1; |
325 |
} |
326 |
|
327 |
// Process parameter
|
328 |
switch (list->type) {
|
329 |
case OPT_FLAG:
|
330 |
case OPT_NUMBER:
|
331 |
case OPT_SIZE:
|
332 |
list->value.n = value; |
333 |
break;
|
334 |
|
335 |
default:
|
336 |
return -1; |
337 |
} |
338 |
|
339 |
return 0; |
340 |
} |
341 |
|
342 |
/*
|
343 |
* Frees a option list. If it contains strings, the strings are freed as well.
|
344 |
*/
|
345 |
void free_option_parameters(QEMUOptionParameter *list)
|
346 |
{ |
347 |
QEMUOptionParameter *cur = list; |
348 |
|
349 |
while (cur && cur->name) {
|
350 |
if (cur->type == OPT_STRING) {
|
351 |
g_free(cur->value.s); |
352 |
} |
353 |
cur++; |
354 |
} |
355 |
|
356 |
g_free(list); |
357 |
} |
358 |
|
359 |
/*
|
360 |
* Count valid options in list
|
361 |
*/
|
362 |
static size_t count_option_parameters(QEMUOptionParameter *list)
|
363 |
{ |
364 |
size_t num_options = 0;
|
365 |
|
366 |
while (list && list->name) {
|
367 |
num_options++; |
368 |
list++; |
369 |
} |
370 |
|
371 |
return num_options;
|
372 |
} |
373 |
|
374 |
/*
|
375 |
* Append an option list (list) to an option list (dest).
|
376 |
*
|
377 |
* If dest is NULL, a new copy of list is created.
|
378 |
*
|
379 |
* Returns a pointer to the first element of dest (or the newly allocated copy)
|
380 |
*/
|
381 |
QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, |
382 |
QEMUOptionParameter *list) |
383 |
{ |
384 |
size_t num_options, num_dest_options; |
385 |
|
386 |
num_options = count_option_parameters(dest); |
387 |
num_dest_options = num_options; |
388 |
|
389 |
num_options += count_option_parameters(list); |
390 |
|
391 |
dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter)); |
392 |
dest[num_dest_options].name = NULL;
|
393 |
|
394 |
while (list && list->name) {
|
395 |
if (get_option_parameter(dest, list->name) == NULL) { |
396 |
dest[num_dest_options++] = *list; |
397 |
dest[num_dest_options].name = NULL;
|
398 |
} |
399 |
list++; |
400 |
} |
401 |
|
402 |
return dest;
|
403 |
} |
404 |
|
405 |
/*
|
406 |
* Parses a parameter string (param) into an option list (dest).
|
407 |
*
|
408 |
* list is the template option list. If dest is NULL, a new copy of list is
|
409 |
* created. If list is NULL, this function fails.
|
410 |
*
|
411 |
* A parameter string consists of one or more parameters, separated by commas.
|
412 |
* Each parameter consists of its name and possibly of a value. In the latter
|
413 |
* case, the value is delimited by an = character. To specify a value which
|
414 |
* contains commas, double each comma so it won't be recognized as the end of
|
415 |
* the parameter.
|
416 |
*
|
417 |
* For more details of the parsing see above.
|
418 |
*
|
419 |
* Returns a pointer to the first element of dest (or the newly allocated copy)
|
420 |
* or NULL in error cases
|
421 |
*/
|
422 |
QEMUOptionParameter *parse_option_parameters(const char *param, |
423 |
QEMUOptionParameter *list, QEMUOptionParameter *dest) |
424 |
{ |
425 |
QEMUOptionParameter *allocated = NULL;
|
426 |
char name[256]; |
427 |
char value[256]; |
428 |
char *param_delim, *value_delim;
|
429 |
char next_delim;
|
430 |
|
431 |
if (list == NULL) { |
432 |
return NULL; |
433 |
} |
434 |
|
435 |
if (dest == NULL) { |
436 |
dest = allocated = append_option_parameters(NULL, list);
|
437 |
} |
438 |
|
439 |
while (*param) {
|
440 |
|
441 |
// Find parameter name and value in the string
|
442 |
param_delim = strchr(param, ',');
|
443 |
value_delim = strchr(param, '=');
|
444 |
|
445 |
if (value_delim && (value_delim < param_delim || !param_delim)) {
|
446 |
next_delim = '=';
|
447 |
} else {
|
448 |
next_delim = ',';
|
449 |
value_delim = NULL;
|
450 |
} |
451 |
|
452 |
param = get_opt_name(name, sizeof(name), param, next_delim);
|
453 |
if (value_delim) {
|
454 |
param = get_opt_value(value, sizeof(value), param + 1); |
455 |
} |
456 |
if (*param != '\0') { |
457 |
param++; |
458 |
} |
459 |
|
460 |
// Set the parameter
|
461 |
if (set_option_parameter(dest, name, value_delim ? value : NULL)) { |
462 |
goto fail;
|
463 |
} |
464 |
} |
465 |
|
466 |
return dest;
|
467 |
|
468 |
fail:
|
469 |
// Only free the list if it was newly allocated
|
470 |
free_option_parameters(allocated); |
471 |
return NULL; |
472 |
} |
473 |
|
474 |
/*
|
475 |
* Prints all options of a list that have a value to stdout
|
476 |
*/
|
477 |
void print_option_parameters(QEMUOptionParameter *list)
|
478 |
{ |
479 |
while (list && list->name) {
|
480 |
switch (list->type) {
|
481 |
case OPT_STRING:
|
482 |
if (list->value.s != NULL) { |
483 |
printf("%s='%s' ", list->name, list->value.s);
|
484 |
} |
485 |
break;
|
486 |
case OPT_FLAG:
|
487 |
printf("%s=%s ", list->name, list->value.n ? "on" : "off"); |
488 |
break;
|
489 |
case OPT_SIZE:
|
490 |
case OPT_NUMBER:
|
491 |
printf("%s=%" PRId64 " ", list->name, list->value.n); |
492 |
break;
|
493 |
default:
|
494 |
printf("%s=(unknown type) ", list->name);
|
495 |
break;
|
496 |
} |
497 |
list++; |
498 |
} |
499 |
} |
500 |
|
501 |
/*
|
502 |
* Prints an overview of all available options
|
503 |
*/
|
504 |
void print_option_help(QEMUOptionParameter *list)
|
505 |
{ |
506 |
printf("Supported options:\n");
|
507 |
while (list && list->name) {
|
508 |
printf("%-16s %s\n", list->name,
|
509 |
list->help ? list->help : "No description available");
|
510 |
list++; |
511 |
} |
512 |
} |
513 |
|
514 |
/* ------------------------------------------------------------------ */
|
515 |
|
516 |
struct QemuOpt {
|
517 |
const char *name; |
518 |
const char *str; |
519 |
|
520 |
const QemuOptDesc *desc;
|
521 |
union {
|
522 |
bool boolean;
|
523 |
uint64_t uint; |
524 |
} value; |
525 |
|
526 |
QemuOpts *opts; |
527 |
QTAILQ_ENTRY(QemuOpt) next; |
528 |
}; |
529 |
|
530 |
struct QemuOpts {
|
531 |
char *id;
|
532 |
QemuOptsList *list; |
533 |
Location loc; |
534 |
QTAILQ_HEAD(QemuOptHead, QemuOpt) head; |
535 |
QTAILQ_ENTRY(QemuOpts) next; |
536 |
}; |
537 |
|
538 |
static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
539 |
{ |
540 |
QemuOpt *opt; |
541 |
|
542 |
QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { |
543 |
if (strcmp(opt->name, name) != 0) |
544 |
continue;
|
545 |
return opt;
|
546 |
} |
547 |
return NULL; |
548 |
} |
549 |
|
550 |
const char *qemu_opt_get(QemuOpts *opts, const char *name) |
551 |
{ |
552 |
QemuOpt *opt = qemu_opt_find(opts, name); |
553 |
return opt ? opt->str : NULL; |
554 |
} |
555 |
|
556 |
bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
557 |
{ |
558 |
QemuOpt *opt = qemu_opt_find(opts, name); |
559 |
|
560 |
if (opt == NULL) |
561 |
return defval;
|
562 |
assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
563 |
return opt->value.boolean;
|
564 |
} |
565 |
|
566 |
uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
567 |
{ |
568 |
QemuOpt *opt = qemu_opt_find(opts, name); |
569 |
|
570 |
if (opt == NULL) |
571 |
return defval;
|
572 |
assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
573 |
return opt->value.uint;
|
574 |
} |
575 |
|
576 |
uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
577 |
{ |
578 |
QemuOpt *opt = qemu_opt_find(opts, name); |
579 |
|
580 |
if (opt == NULL) |
581 |
return defval;
|
582 |
assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
583 |
return opt->value.uint;
|
584 |
} |
585 |
|
586 |
static int qemu_opt_parse(QemuOpt *opt) |
587 |
{ |
588 |
Error *local_err = NULL;
|
589 |
|
590 |
if (opt->desc == NULL) |
591 |
return 0; |
592 |
|
593 |
switch (opt->desc->type) {
|
594 |
case QEMU_OPT_STRING:
|
595 |
/* nothing */
|
596 |
return 0; |
597 |
case QEMU_OPT_BOOL:
|
598 |
parse_option_bool(opt->name, opt->str, &opt->value.boolean, &local_err); |
599 |
break;
|
600 |
case QEMU_OPT_NUMBER:
|
601 |
parse_option_number(opt->name, opt->str, &opt->value.uint, |
602 |
&local_err); |
603 |
break;
|
604 |
case QEMU_OPT_SIZE:
|
605 |
return parse_option_size(opt->name, opt->str, &opt->value.uint);
|
606 |
default:
|
607 |
abort(); |
608 |
} |
609 |
|
610 |
if (error_is_set(&local_err)) {
|
611 |
qerror_report_err(local_err); |
612 |
error_free(local_err); |
613 |
return -1; |
614 |
} |
615 |
|
616 |
return 0; |
617 |
} |
618 |
|
619 |
static void qemu_opt_del(QemuOpt *opt) |
620 |
{ |
621 |
QTAILQ_REMOVE(&opt->opts->head, opt, next); |
622 |
g_free((/* !const */ char*)opt->name); |
623 |
g_free((/* !const */ char*)opt->str); |
624 |
g_free(opt); |
625 |
} |
626 |
|
627 |
static int opt_set(QemuOpts *opts, const char *name, const char *value, |
628 |
bool prepend)
|
629 |
{ |
630 |
QemuOpt *opt; |
631 |
const QemuOptDesc *desc = opts->list->desc;
|
632 |
int i;
|
633 |
|
634 |
for (i = 0; desc[i].name != NULL; i++) { |
635 |
if (strcmp(desc[i].name, name) == 0) { |
636 |
break;
|
637 |
} |
638 |
} |
639 |
if (desc[i].name == NULL) { |
640 |
if (i == 0) { |
641 |
/* empty list -> allow any */;
|
642 |
} else {
|
643 |
qerror_report(QERR_INVALID_PARAMETER, name); |
644 |
return -1; |
645 |
} |
646 |
} |
647 |
|
648 |
opt = g_malloc0(sizeof(*opt));
|
649 |
opt->name = g_strdup(name); |
650 |
opt->opts = opts; |
651 |
if (prepend) {
|
652 |
QTAILQ_INSERT_HEAD(&opts->head, opt, next); |
653 |
} else {
|
654 |
QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
655 |
} |
656 |
if (desc[i].name != NULL) { |
657 |
opt->desc = desc+i; |
658 |
} |
659 |
if (value) {
|
660 |
opt->str = g_strdup(value); |
661 |
} |
662 |
if (qemu_opt_parse(opt) < 0) { |
663 |
qemu_opt_del(opt); |
664 |
return -1; |
665 |
} |
666 |
return 0; |
667 |
} |
668 |
|
669 |
int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
670 |
{ |
671 |
return opt_set(opts, name, value, false); |
672 |
} |
673 |
|
674 |
int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) |
675 |
{ |
676 |
QemuOpt *opt; |
677 |
const QemuOptDesc *desc = opts->list->desc;
|
678 |
int i;
|
679 |
|
680 |
for (i = 0; desc[i].name != NULL; i++) { |
681 |
if (strcmp(desc[i].name, name) == 0) { |
682 |
break;
|
683 |
} |
684 |
} |
685 |
if (desc[i].name == NULL) { |
686 |
if (i == 0) { |
687 |
/* empty list -> allow any */;
|
688 |
} else {
|
689 |
qerror_report(QERR_INVALID_PARAMETER, name); |
690 |
return -1; |
691 |
} |
692 |
} |
693 |
|
694 |
opt = g_malloc0(sizeof(*opt));
|
695 |
opt->name = g_strdup(name); |
696 |
opt->opts = opts; |
697 |
QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
698 |
if (desc[i].name != NULL) { |
699 |
opt->desc = desc+i; |
700 |
} |
701 |
opt->value.boolean = !!val; |
702 |
return 0; |
703 |
} |
704 |
|
705 |
int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
706 |
int abort_on_failure)
|
707 |
{ |
708 |
QemuOpt *opt; |
709 |
int rc = 0; |
710 |
|
711 |
QTAILQ_FOREACH(opt, &opts->head, next) { |
712 |
rc = func(opt->name, opt->str, opaque); |
713 |
if (abort_on_failure && rc != 0) |
714 |
break;
|
715 |
} |
716 |
return rc;
|
717 |
} |
718 |
|
719 |
QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
720 |
{ |
721 |
QemuOpts *opts; |
722 |
|
723 |
QTAILQ_FOREACH(opts, &list->head, next) { |
724 |
if (!opts->id) {
|
725 |
if (!id) {
|
726 |
return opts;
|
727 |
} |
728 |
continue;
|
729 |
} |
730 |
if (strcmp(opts->id, id) != 0) { |
731 |
continue;
|
732 |
} |
733 |
return opts;
|
734 |
} |
735 |
return NULL; |
736 |
} |
737 |
|
738 |
static int id_wellformed(const char *id) |
739 |
{ |
740 |
int i;
|
741 |
|
742 |
if (!qemu_isalpha(id[0])) { |
743 |
return 0; |
744 |
} |
745 |
for (i = 1; id[i]; i++) { |
746 |
if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { |
747 |
return 0; |
748 |
} |
749 |
} |
750 |
return 1; |
751 |
} |
752 |
|
753 |
QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, |
754 |
int fail_if_exists, Error **errp)
|
755 |
{ |
756 |
QemuOpts *opts = NULL;
|
757 |
|
758 |
if (id) {
|
759 |
if (!id_wellformed(id)) {
|
760 |
error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); |
761 |
error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
|
762 |
return NULL; |
763 |
} |
764 |
opts = qemu_opts_find(list, id); |
765 |
if (opts != NULL) { |
766 |
if (fail_if_exists && !list->merge_lists) {
|
767 |
error_set(errp, QERR_DUPLICATE_ID, id, list->name); |
768 |
return NULL; |
769 |
} else {
|
770 |
return opts;
|
771 |
} |
772 |
} |
773 |
} else if (list->merge_lists) { |
774 |
opts = qemu_opts_find(list, NULL);
|
775 |
if (opts) {
|
776 |
return opts;
|
777 |
} |
778 |
} |
779 |
opts = g_malloc0(sizeof(*opts));
|
780 |
if (id) {
|
781 |
opts->id = g_strdup(id); |
782 |
} |
783 |
opts->list = list; |
784 |
loc_save(&opts->loc); |
785 |
QTAILQ_INIT(&opts->head); |
786 |
QTAILQ_INSERT_TAIL(&list->head, opts, next); |
787 |
return opts;
|
788 |
} |
789 |
|
790 |
void qemu_opts_reset(QemuOptsList *list)
|
791 |
{ |
792 |
QemuOpts *opts, *next_opts; |
793 |
|
794 |
QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { |
795 |
qemu_opts_del(opts); |
796 |
} |
797 |
} |
798 |
|
799 |
void qemu_opts_loc_restore(QemuOpts *opts)
|
800 |
{ |
801 |
loc_restore(&opts->loc); |
802 |
} |
803 |
|
804 |
int qemu_opts_set(QemuOptsList *list, const char *id, |
805 |
const char *name, const char *value) |
806 |
{ |
807 |
QemuOpts *opts; |
808 |
Error *local_err = NULL;
|
809 |
|
810 |
opts = qemu_opts_create(list, id, 1, &local_err);
|
811 |
if (error_is_set(&local_err)) {
|
812 |
qerror_report_err(local_err); |
813 |
error_free(local_err); |
814 |
return -1; |
815 |
} |
816 |
return qemu_opt_set(opts, name, value);
|
817 |
} |
818 |
|
819 |
const char *qemu_opts_id(QemuOpts *opts) |
820 |
{ |
821 |
return opts->id;
|
822 |
} |
823 |
|
824 |
void qemu_opts_del(QemuOpts *opts)
|
825 |
{ |
826 |
QemuOpt *opt; |
827 |
|
828 |
for (;;) {
|
829 |
opt = QTAILQ_FIRST(&opts->head); |
830 |
if (opt == NULL) |
831 |
break;
|
832 |
qemu_opt_del(opt); |
833 |
} |
834 |
QTAILQ_REMOVE(&opts->list->head, opts, next); |
835 |
g_free(opts->id); |
836 |
g_free(opts); |
837 |
} |
838 |
|
839 |
int qemu_opts_print(QemuOpts *opts, void *dummy) |
840 |
{ |
841 |
QemuOpt *opt; |
842 |
|
843 |
fprintf(stderr, "%s: %s:", opts->list->name,
|
844 |
opts->id ? opts->id : "<noid>");
|
845 |
QTAILQ_FOREACH(opt, &opts->head, next) { |
846 |
fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
|
847 |
} |
848 |
fprintf(stderr, "\n");
|
849 |
return 0; |
850 |
} |
851 |
|
852 |
static int opts_do_parse(QemuOpts *opts, const char *params, |
853 |
const char *firstname, bool prepend) |
854 |
{ |
855 |
char option[128], value[1024]; |
856 |
const char *p,*pe,*pc; |
857 |
|
858 |
for (p = params; *p != '\0'; p++) { |
859 |
pe = strchr(p, '=');
|
860 |
pc = strchr(p, ',');
|
861 |
if (!pe || (pc && pc < pe)) {
|
862 |
/* found "foo,more" */
|
863 |
if (p == params && firstname) {
|
864 |
/* implicitly named first option */
|
865 |
pstrcpy(option, sizeof(option), firstname);
|
866 |
p = get_opt_value(value, sizeof(value), p);
|
867 |
} else {
|
868 |
/* option without value, probably a flag */
|
869 |
p = get_opt_name(option, sizeof(option), p, ','); |
870 |
if (strncmp(option, "no", 2) == 0) { |
871 |
memmove(option, option+2, strlen(option+2)+1); |
872 |
pstrcpy(value, sizeof(value), "off"); |
873 |
} else {
|
874 |
pstrcpy(value, sizeof(value), "on"); |
875 |
} |
876 |
} |
877 |
} else {
|
878 |
/* found "foo=bar,more" */
|
879 |
p = get_opt_name(option, sizeof(option), p, '='); |
880 |
if (*p != '=') { |
881 |
break;
|
882 |
} |
883 |
p++; |
884 |
p = get_opt_value(value, sizeof(value), p);
|
885 |
} |
886 |
if (strcmp(option, "id") != 0) { |
887 |
/* store and parse */
|
888 |
if (opt_set(opts, option, value, prepend) == -1) { |
889 |
return -1; |
890 |
} |
891 |
} |
892 |
if (*p != ',') { |
893 |
break;
|
894 |
} |
895 |
} |
896 |
return 0; |
897 |
} |
898 |
|
899 |
int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
900 |
{ |
901 |
return opts_do_parse(opts, params, firstname, false); |
902 |
} |
903 |
|
904 |
static QemuOpts *opts_parse(QemuOptsList *list, const char *params, |
905 |
int permit_abbrev, bool defaults) |
906 |
{ |
907 |
const char *firstname; |
908 |
char value[1024], *id = NULL; |
909 |
const char *p; |
910 |
QemuOpts *opts; |
911 |
Error *local_err = NULL;
|
912 |
|
913 |
assert(!permit_abbrev || list->implied_opt_name); |
914 |
firstname = permit_abbrev ? list->implied_opt_name : NULL;
|
915 |
|
916 |
if (strncmp(params, "id=", 3) == 0) { |
917 |
get_opt_value(value, sizeof(value), params+3); |
918 |
id = value; |
919 |
} else if ((p = strstr(params, ",id=")) != NULL) { |
920 |
get_opt_value(value, sizeof(value), p+4); |
921 |
id = value; |
922 |
} |
923 |
if (defaults) {
|
924 |
if (!id && !QTAILQ_EMPTY(&list->head)) {
|
925 |
opts = qemu_opts_find(list, NULL);
|
926 |
} else {
|
927 |
opts = qemu_opts_create(list, id, 0, &local_err);
|
928 |
} |
929 |
} else {
|
930 |
opts = qemu_opts_create(list, id, 1, &local_err);
|
931 |
} |
932 |
if (opts == NULL) { |
933 |
if (error_is_set(&local_err)) {
|
934 |
qerror_report_err(local_err); |
935 |
error_free(local_err); |
936 |
} |
937 |
return NULL; |
938 |
} |
939 |
|
940 |
if (opts_do_parse(opts, params, firstname, defaults) != 0) { |
941 |
qemu_opts_del(opts); |
942 |
return NULL; |
943 |
} |
944 |
|
945 |
return opts;
|
946 |
} |
947 |
|
948 |
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
949 |
int permit_abbrev)
|
950 |
{ |
951 |
return opts_parse(list, params, permit_abbrev, false); |
952 |
} |
953 |
|
954 |
void qemu_opts_set_defaults(QemuOptsList *list, const char *params, |
955 |
int permit_abbrev)
|
956 |
{ |
957 |
QemuOpts *opts; |
958 |
|
959 |
opts = opts_parse(list, params, permit_abbrev, true);
|
960 |
assert(opts); |
961 |
} |
962 |
|
963 |
static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) |
964 |
{ |
965 |
char buf[32]; |
966 |
const char *value; |
967 |
int n;
|
968 |
|
969 |
if (!strcmp(key, "id")) { |
970 |
return;
|
971 |
} |
972 |
|
973 |
switch (qobject_type(obj)) {
|
974 |
case QTYPE_QSTRING:
|
975 |
value = qstring_get_str(qobject_to_qstring(obj)); |
976 |
break;
|
977 |
case QTYPE_QINT:
|
978 |
n = snprintf(buf, sizeof(buf), "%" PRId64, |
979 |
qint_get_int(qobject_to_qint(obj))); |
980 |
assert(n < sizeof(buf));
|
981 |
value = buf; |
982 |
break;
|
983 |
case QTYPE_QFLOAT:
|
984 |
n = snprintf(buf, sizeof(buf), "%.17g", |
985 |
qfloat_get_double(qobject_to_qfloat(obj))); |
986 |
assert(n < sizeof(buf));
|
987 |
value = buf; |
988 |
break;
|
989 |
case QTYPE_QBOOL:
|
990 |
pstrcpy(buf, sizeof(buf),
|
991 |
qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off"); |
992 |
value = buf; |
993 |
break;
|
994 |
default:
|
995 |
return;
|
996 |
} |
997 |
qemu_opt_set(opaque, key, value); |
998 |
} |
999 |
|
1000 |
/*
|
1001 |
* Create QemuOpts from a QDict.
|
1002 |
* Use value of key "id" as ID if it exists and is a QString.
|
1003 |
* Only QStrings, QInts, QFloats and QBools are copied. Entries with
|
1004 |
* other types are silently ignored.
|
1005 |
*/
|
1006 |
QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
|
1007 |
{ |
1008 |
QemuOpts *opts; |
1009 |
Error *local_err = NULL;
|
1010 |
|
1011 |
opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, |
1012 |
&local_err); |
1013 |
if (error_is_set(&local_err)) {
|
1014 |
qerror_report_err(local_err); |
1015 |
error_free(local_err); |
1016 |
return NULL; |
1017 |
} |
1018 |
|
1019 |
assert(opts != NULL);
|
1020 |
qdict_iter(qdict, qemu_opts_from_qdict_1, opts); |
1021 |
return opts;
|
1022 |
} |
1023 |
|
1024 |
/*
|
1025 |
* Convert from QemuOpts to QDict.
|
1026 |
* The QDict values are of type QString.
|
1027 |
* TODO We'll want to use types appropriate for opt->desc->type, but
|
1028 |
* this is enough for now.
|
1029 |
*/
|
1030 |
QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) |
1031 |
{ |
1032 |
QemuOpt *opt; |
1033 |
QObject *val; |
1034 |
|
1035 |
if (!qdict) {
|
1036 |
qdict = qdict_new(); |
1037 |
} |
1038 |
if (opts->id) {
|
1039 |
qdict_put(qdict, "id", qstring_from_str(opts->id));
|
1040 |
} |
1041 |
QTAILQ_FOREACH(opt, &opts->head, next) { |
1042 |
val = QOBJECT(qstring_from_str(opt->str)); |
1043 |
qdict_put_obj(qdict, opt->name, val); |
1044 |
} |
1045 |
return qdict;
|
1046 |
} |
1047 |
|
1048 |
/* Validate parsed opts against descriptions where no
|
1049 |
* descriptions were provided in the QemuOptsList.
|
1050 |
*/
|
1051 |
int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc) |
1052 |
{ |
1053 |
QemuOpt *opt; |
1054 |
|
1055 |
assert(opts->list->desc[0].name == NULL); |
1056 |
|
1057 |
QTAILQ_FOREACH(opt, &opts->head, next) { |
1058 |
int i;
|
1059 |
|
1060 |
for (i = 0; desc[i].name != NULL; i++) { |
1061 |
if (strcmp(desc[i].name, opt->name) == 0) { |
1062 |
break;
|
1063 |
} |
1064 |
} |
1065 |
if (desc[i].name == NULL) { |
1066 |
qerror_report(QERR_INVALID_PARAMETER, opt->name); |
1067 |
return -1; |
1068 |
} |
1069 |
|
1070 |
opt->desc = &desc[i]; |
1071 |
|
1072 |
if (qemu_opt_parse(opt) < 0) { |
1073 |
return -1; |
1074 |
} |
1075 |
} |
1076 |
|
1077 |
return 0; |
1078 |
} |
1079 |
|
1080 |
int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
1081 |
int abort_on_failure)
|
1082 |
{ |
1083 |
Location loc; |
1084 |
QemuOpts *opts; |
1085 |
int rc = 0; |
1086 |
|
1087 |
loc_push_none(&loc); |
1088 |
QTAILQ_FOREACH(opts, &list->head, next) { |
1089 |
loc_restore(&opts->loc); |
1090 |
rc |= func(opts, opaque); |
1091 |
if (abort_on_failure && rc != 0) |
1092 |
break;
|
1093 |
} |
1094 |
loc_pop(&loc); |
1095 |
return rc;
|
1096 |
} |