Coverage Report

Created: 2026-11-20 08:13

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