Now all places using QEMUOptionParameter could use QemuOpts too, remove QEMUOptionParameter related code.
Signed-off-by: Dong Xu Wang <wdon...@linux.vnet.ibm.com> Signed-off-by: Chunyan Liu <cy...@suse.com> --- block.c | 147 ++--------------------- block/cow.c | 2 +- block/qcow.c | 2 +- block/qcow2.c | 2 +- block/qed.c | 2 +- block/raw_bsd.c | 2 +- block/vhdx.c | 2 +- block/vmdk.c | 4 +- block/vvfat.c | 2 +- include/block/block.h | 4 +- include/block/block_int.h | 3 - include/qemu/option.h | 35 ------ qemu-img.c | 93 ++------------- util/qemu-option.c | 294 --------------------------------------------- 14 files changed, 30 insertions(+), 564 deletions(-) diff --git a/block.c b/block.c index 8c490c6..b33d095 100644 --- a/block.c +++ b/block.c @@ -394,7 +394,6 @@ BlockDriver *bdrv_find_whitelisted_format(const char *format_name, typedef struct CreateCo { BlockDriver *drv; char *filename; - QEMUOptionParameter *options; QemuOpts *opts; int ret; Error *err; @@ -403,15 +402,13 @@ typedef struct CreateCo { static void coroutine_fn bdrv_create_co_entry(void *opaque) { Error *local_err = NULL; - int ret; + int ret = -1; CreateCo *cco = opaque; assert(cco->drv); if (cco->drv->bdrv_create2) ret = cco->drv->bdrv_create2(cco->filename, cco->opts, &local_err); - else - ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err); if (error_is_set(&local_err)) { error_propagate(&cco->err, local_err); } @@ -419,7 +416,7 @@ static void coroutine_fn bdrv_create_co_entry(void *opaque) } int bdrv_create(BlockDriver *drv, const char* filename, - QEMUOptionParameter *options, QemuOpts *opts, Error **errp) + QemuOpts *opts, Error **errp) { int ret; @@ -427,13 +424,12 @@ int bdrv_create(BlockDriver *drv, const char* filename, CreateCo cco = { .drv = drv, .filename = g_strdup(filename), - .options = options, .opts = opts, .ret = NOT_DONE, .err = NULL, }; - if (!drv->bdrv_create && !drv->bdrv_create2) { + if (!drv->bdrv_create2) { error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); ret = -ENOTSUP; goto out; @@ -464,7 +460,7 @@ out: return ret; } -int bdrv_create_file(const char* filename, QEMUOptionParameter *options, +int bdrv_create_file(const char* filename, QemuOpts *opts, Error **errp) { BlockDriver *drv; @@ -477,7 +473,7 @@ int bdrv_create_file(const char* filename, QEMUOptionParameter *options, return -ENOENT; } - ret = bdrv_create(drv, filename, options, opts, &local_err); + ret = bdrv_create(drv, filename, opts, &local_err); if (error_is_set(&local_err)) { error_propagate(errp, local_err); } @@ -1058,7 +1054,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, BlockDriverState *bs1; int64_t total_size; BlockDriver *bdrv_qcow2; - QEMUOptionParameter *create_options = NULL; QemuOpts *opts = NULL; QDict *snapshot_options; @@ -1089,14 +1084,9 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, if (bdrv_qcow2->bdrv_create2) { opts = qemu_opts_create(bdrv_qcow2->create_opts, NULL, 0, &error_abort); qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size); - } else { - create_options = - parse_option_parameters("", bdrv_qcow2->create_options, NULL); - set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); } - ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, opts, &local_err); - free_option_parameters(create_options); + ret = bdrv_create(bdrv_qcow2, tmp_filename, opts, &local_err); qemu_opts_del(opts); if (ret < 0) { error_setg_errno(errp, -ret, "Could not create temporary overlay " @@ -4722,7 +4712,6 @@ void bdrv_img_create(const char *filename, const char *fmt, char *options, uint64_t img_size, int flags, Error **errp, bool quiet) { - QEMUOptionParameter *param = NULL, *create_options = NULL; QemuOptsList *create_opts = NULL; QemuOpts *opts = NULL; BlockDriver *drv, *proto_drv; @@ -4843,7 +4832,7 @@ void bdrv_img_create(const char *filename, const char *fmt, puts(""); } - ret = bdrv_create(drv, filename, NULL, opts, &local_err); + ret = bdrv_create(drv, filename, opts, &local_err); if (ret == -EFBIG) { /* This is generally a better message than whatever the driver would * deliver (especially because of the cluster_size_hint), since that @@ -4859,128 +4848,10 @@ void bdrv_img_create(const char *filename, const char *fmt, } } else { - QEMUOptionParameter *backing_fmt, *backing_file, *size; - - create_options = append_option_parameters(create_options, - drv->create_options); - create_options = append_option_parameters(create_options, - proto_drv->create_options); - - /* Create parameter list with default values */ - param = parse_option_parameters("", create_options, param); - - set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); - - /* Parse -o options */ - if (options) { - param = parse_option_parameters(options, create_options, param); - if (param == NULL) { - error_setg(errp, "Invalid options for file format '%s'.", fmt); - goto out; - } - } - - if (base_filename) { - if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, - base_filename)) { - error_setg(errp, "Backing file not supported for file format '%s'", - fmt); - goto out; - } - } - - if (base_fmt) { - if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { - error_setg(errp, "Backing file format not supported for file " - "format '%s'", fmt); - goto out; - } - } - - backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); - if (backing_file && backing_file->value.s) { - if (!strcmp(filename, backing_file->value.s)) { - error_setg(errp, "Error: Trying to create an image with the " - "same filename as the backing file"); - goto out; - } - } - - backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); - if (backing_fmt && backing_fmt->value.s) { - backing_drv = bdrv_find_format(backing_fmt->value.s); - if (!backing_drv) { - error_setg(errp, "Unknown backing file format '%s'", - backing_fmt->value.s); - goto out; - } - } - - // The size for the image must always be specified, with one exception: - // If we are using a backing file, we can obtain the size from there - size = get_option_parameter(param, BLOCK_OPT_SIZE); - if (size && size->value.n == -1) { - if (backing_file && backing_file->value.s) { - BlockDriverState *bs; - uint64_t size; - char buf[32]; - int back_flags; - - /* backing files always opened read-only */ - back_flags = - flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); - - bs = bdrv_new(""); - - ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, - backing_drv, &local_err); - if (ret < 0) { - error_setg_errno(errp, -ret, "Could not open '%s': %s", - backing_file->value.s, - error_get_pretty(local_err)); - error_free(local_err); - local_err = NULL; - bdrv_unref(bs); - goto out; - } - bdrv_get_geometry(bs, &size); - size *= 512; - - snprintf(buf, sizeof(buf), "%" PRId64, size); - set_option_parameter(param, BLOCK_OPT_SIZE, buf); - - bdrv_unref(bs); - } else { - error_setg(errp, "Image creation needs a size parameter"); - goto out; - } - } - - if (!quiet) { - printf("Formatting '%s', fmt=%s ", filename, fmt); - print_option_parameters(param); - puts(""); - } - - ret = bdrv_create(drv, filename, param, NULL, &local_err); - if (ret == -EFBIG) { - /* This is generally a better message than whatever the driver would - * deliver (especially because of the cluster_size_hint), since that - * is most probably not much different from "image too large". */ - const char *cluster_size_hint = ""; - if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { - cluster_size_hint = " (try using a larger cluster size)"; - } - error_setg(errp, "The image size is too large for file format '%s'" - "%s", fmt, cluster_size_hint); - error_free(local_err); - local_err = NULL; - } - } + error_setg(errp, "Unsupported"); + } out: - free_option_parameters(create_options); - free_option_parameters(param); qemu_opts_del(opts); qemu_opts_free(create_opts); if (error_is_set(&local_err)) { diff --git a/block/cow.c b/block/cow.c index 6688a87..61f2bab 100644 --- a/block/cow.c +++ b/block/cow.c @@ -338,7 +338,7 @@ static int cow_create(const char *filename, QemuOpts *opts, image_sectors = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512; image_filename = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); - ret = bdrv_create_file(filename, NULL, opts, &local_err); + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { qerror_report_err(local_err); error_free(local_err); diff --git a/block/qcow.c b/block/qcow.c index ac0ddff..4f7907b 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -679,7 +679,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, flags |= BLOCK_FLAG_ENCRYPT; } - ret = bdrv_create_file(filename, NULL, opts, &local_err); + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { qerror_report_err(local_err); error_free(local_err); diff --git a/block/qcow2.c b/block/qcow2.c index 580bc93..ed14195 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1477,7 +1477,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, Error *local_err = NULL; int ret; - ret = bdrv_create_file(filename, NULL, opts, &local_err); + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { error_propagate(errp, local_err); return ret; diff --git a/block/qed.c b/block/qed.c index 9f819a8..e858dc5 100644 --- a/block/qed.c +++ b/block/qed.c @@ -556,7 +556,7 @@ static int qed_create(const char *filename, uint32_t cluster_size, int ret = 0; BlockDriverState *bs = NULL; - ret = bdrv_create_file(filename, NULL, NULL, &local_err); + ret = bdrv_create_file(filename, NULL, &local_err); if (ret < 0) { qerror_report_err(local_err); error_free(local_err); diff --git a/block/raw_bsd.c b/block/raw_bsd.c index 4ae12dd..d9dd324 100644 --- a/block/raw_bsd.c +++ b/block/raw_bsd.c @@ -143,7 +143,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error *local_err = NULL; int ret; - ret = bdrv_create_file(filename, NULL, opts, &local_err); + ret = bdrv_create_file(filename, opts, &local_err); if (error_is_set(&local_err)) { error_propagate(errp, local_err); } diff --git a/block/vhdx.c b/block/vhdx.c index 8021e8e..057c409 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1782,7 +1782,7 @@ static int vhdx_create(const char *filename, QemuOpts *opts, block_size = block_size > VHDX_BLOCK_SIZE_MAX ? VHDX_BLOCK_SIZE_MAX : block_size; - ret = bdrv_create_file(filename, NULL, opts, &local_err); + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { error_propagate(errp, local_err); goto exit; diff --git a/block/vmdk.c b/block/vmdk.c index c3d4328..9403d5d 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1463,7 +1463,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize, uint32_t *gd_buf = NULL; int gd_buf_size; - ret = bdrv_create_file(filename, NULL, NULL, &local_err); + ret = bdrv_create_file(filename, NULL, &local_err); if (ret < 0) { error_propagate(errp, local_err); goto exit; @@ -1796,7 +1796,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, if (!split && !flat) { desc_offset = 0x200; } else { - ret = bdrv_create_file(filename, NULL, opts, &local_err); + ret = bdrv_create_file(filename, opts, &local_err); if (ret < 0) { error_setg_errno(errp, -ret, "Could not create image file"); goto exit; diff --git a/block/vvfat.c b/block/vvfat.c index 81733bc..b43b4b1 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -2929,7 +2929,7 @@ static int enable_write_target(BDRVVVFATState *s) qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512); qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:"); - ret = bdrv_create(bdrv_qcow, s->qcow_filename, NULL, opts, &local_err); + ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, &local_err); if (ret < 0) { qerror_report_err(local_err); error_free(local_err); diff --git a/include/block/block.h b/include/block/block.h index e23a7da..feb44d2 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -174,8 +174,8 @@ BlockDriver *bdrv_find_format(const char *format_name); BlockDriver *bdrv_find_whitelisted_format(const char *format_name, bool readonly); int bdrv_create(BlockDriver *drv, const char* filename, - QEMUOptionParameter *options, QemuOpts *opts, Error **errp); -int bdrv_create_file(const char* filename, QEMUOptionParameter *options, + QemuOpts *opts, Error **errp); +int bdrv_create_file(const char* filename, QemuOpts *opts, Error **errp); BlockDriverState *bdrv_new(const char *device_name); void bdrv_make_anon(BlockDriverState *bs); diff --git a/include/block/block_int.h b/include/block/block_int.h index f5bdd08..b9ddbaa 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -103,8 +103,6 @@ struct BlockDriver { const uint8_t *buf, int nb_sectors); void (*bdrv_close)(BlockDriverState *bs); void (*bdrv_rebind)(BlockDriverState *bs); - int (*bdrv_create)(const char *filename, QEMUOptionParameter *options, - Error **errp); int (*bdrv_create2)(const char *filename, QemuOpts *opts, Error **errp); int (*bdrv_set_key)(BlockDriverState *bs, const char *key); @@ -205,7 +203,6 @@ struct BlockDriver { BlockDriverCompletionFunc *cb, void *opaque); /* List of options for creating images, terminated by name == NULL */ - QEMUOptionParameter *create_options; QemuOptsList *create_opts; /* diff --git a/include/qemu/option.h b/include/qemu/option.h index 8d77e2e..6051452 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -38,18 +38,6 @@ enum QEMUOptionParType { OPT_STRING, }; -typedef struct QEMUOptionParameter { - const char *name; - enum QEMUOptionParType type; - union { - uint64_t n; - char* s; - } value; - const char *help; - bool assigned; -} QEMUOptionParameter; - - const char *get_opt_name(char *buf, int buf_size, const char *p, char delim); const char *get_opt_value(char *buf, int buf_size, const char *p); int get_next_param_value(char *buf, int buf_size, @@ -57,29 +45,6 @@ int get_next_param_value(char *buf, int buf_size, int get_param_value(char *buf, int buf_size, const char *tag, const char *str); - -/* - * The following functions take a parameter list as input. This is a pointer to - * the first element of a QEMUOptionParameter array which is terminated by an - * entry with entry->name == NULL. - */ - -QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, - const char *name); -int set_option_parameter(QEMUOptionParameter *list, const char *name, - const char *value); -int set_option_parameter_int(QEMUOptionParameter *list, const char *name, - uint64_t value); -QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, - QEMUOptionParameter *list); -QEMUOptionParameter *parse_option_parameters(const char *param, - QEMUOptionParameter *list, QEMUOptionParameter *dest); -void parse_option_size(const char *name, const char *value, - uint64_t *ret, Error **errp); -void free_option_parameters(QEMUOptionParameter *list); -void print_option_parameters(QEMUOptionParameter *list); -void print_option_help(QEMUOptionParameter *list); - /* ------------------------------------------------------------------ */ typedef struct QemuOpt QemuOpt; diff --git a/qemu-img.c b/qemu-img.c index 863bd03..920e4bd 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -241,7 +241,6 @@ static int read_password(char *buf, int buf_size) static int print_block_option_help(const char *filename, const char *fmt) { BlockDriver *drv, *proto_drv; - QEMUOptionParameter *create_options = NULL; QemuOptsList *create_opts = NULL; /* Find driver and parse its options */ @@ -257,18 +256,10 @@ static int print_block_option_help(const char *filename, const char *fmt) return 1; } - if (drv->bdrv_create2) { - create_opts = qemu_opts_append(create_opts, drv->create_opts); - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); - qemu_opts_print_help(create_opts); - } else { - create_options = append_option_parameters(create_options, - drv->create_options); - create_options = append_option_parameters(create_options, - proto_drv->create_options); - print_option_help(create_options); - } - free_option_parameters(create_options); + create_opts = qemu_opts_append(create_opts, drv->create_opts); + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); + qemu_opts_print_help(create_opts); + qemu_opts_free(create_opts); return 0; } @@ -324,22 +315,19 @@ fail: return NULL; } -static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, +static int add_old_style_options(const char *fmt, QemuOpts *opts, const char *base_filename, const char *base_fmt) { if (base_filename) { - if ((opts && qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename)) || - (list && set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename))) { - error_report("Backing file not supported for file format '%s'", + if (opts && qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename)) { error_report("Backing file not supported for file format '%s'", fmt); return -1; } } if (base_fmt) { - if ((opts && qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt)) || - (list && set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt))) { + if (opts && qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt)) { error_report("Backing file format not supported for file " "format '%s'", fmt); return -1; @@ -1152,7 +1140,6 @@ static int img_convert(int argc, char **argv) size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE; const uint8_t *buf1; BlockDriverInfo bdi; - QEMUOptionParameter *param = NULL, *create_options = NULL; QemuOpts *opts = NULL; QemuOptsList *create_opts = NULL; char *options = NULL; @@ -1337,7 +1324,7 @@ static int img_convert(int argc, char **argv) } qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512); - ret = add_old_style_options(out_fmt, NULL, opts, out_baseimg, NULL); + ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); if (ret < 0) { goto out; } @@ -1379,70 +1366,12 @@ static int img_convert(int argc, char **argv) } } else { - QEMUOptionParameter *out_baseimg_param; - - create_options = append_option_parameters(create_options, - drv->create_options); - create_options = append_option_parameters(create_options, - proto_drv->create_options); - - if (options) { - param = parse_option_parameters(options, create_options, param); - if (param == NULL) { - error_report("Invalid options for file format '%s'.", out_fmt); - ret = -1; - goto out; - } - } else { - param = parse_option_parameters("", create_options, param); - } - - set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512); - ret = add_old_style_options(out_fmt, param, NULL, out_baseimg, NULL); - if (ret < 0) { - goto out; - } - - /* Get backing file name if -o backing_file was used */ - out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); - if (out_baseimg_param) { - out_baseimg = out_baseimg_param->value.s; - } - - /* Check if compression is supported */ - if (compress) { - QEMUOptionParameter *encryption = - get_option_parameter(param, BLOCK_OPT_ENCRYPT); - QEMUOptionParameter *preallocation = - get_option_parameter(param, BLOCK_OPT_PREALLOC); - - if (!drv->bdrv_write_compressed) { - error_report("Compression not supported for this file format"); - ret = -1; - goto out; - } - - if (encryption && encryption->value.n) { - error_report("Compression and encryption not supported at " - "the same time"); - ret = -1; - goto out; - } - - if (preallocation && preallocation->value.s - && strcmp(preallocation->value.s, "off")) - { - error_report("Compression and preallocation not supported at " - "the same time"); - ret = -1; - goto out; - } - } + error_report("Not supported"); } if (!skip_create) { /* Create the new image */ - ret = bdrv_create(drv, out_filename, param, opts, &local_err); + ret = bdrv_create(drv, out_filename, opts, &local_err); if (ret < 0) { error_report("%s: error while converting %s: %s", out_filename, out_fmt, error_get_pretty(local_err)); @@ -1706,8 +1635,6 @@ out: qemu_progress_print(100, 0); } qemu_progress_end(); - free_option_parameters(create_options); - free_option_parameters(param); qemu_opts_free(create_opts); qemu_opts_del(opts); qemu_vfree(buf); diff --git a/util/qemu-option.c b/util/qemu-option.c index 6bd5154..11a8a6b 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -126,22 +126,6 @@ int get_param_value(char *buf, int buf_size, return get_next_param_value(buf, buf_size, tag, &str); } -/* - * Searches an option list for an option with the given name - */ -QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, - const char *name) -{ - while (list && list->name) { - if (!strcmp(list->name, name)) { - return list; - } - list++; - } - - return NULL; -} - static void parse_option_bool(const char *name, const char *value, bool *ret, Error **errp) { @@ -215,170 +199,6 @@ void parse_option_size(const char *name, const char *value, } } -/* - * Sets the value of a parameter in a given option list. The parsing of the - * value depends on the type of option: - * - * OPT_FLAG (uses value.n): - * If no value is given, the flag is set to 1. - * Otherwise the value must be "on" (set to 1) or "off" (set to 0) - * - * OPT_STRING (uses value.s): - * value is strdup()ed and assigned as option value - * - * OPT_SIZE (uses value.n): - * The value is converted to an integer. Suffixes for kilobytes etc. are - * allowed (powers of 1024). - * - * Returns 0 on succes, -1 in error cases - */ -int set_option_parameter(QEMUOptionParameter *list, const char *name, - const char *value) -{ - bool flag; - Error *local_err = NULL; - - // Find a matching parameter - list = get_option_parameter(list, name); - if (list == NULL) { - fprintf(stderr, "Unknown option '%s'\n", name); - return -1; - } - - // Process parameter - switch (list->type) { - case OPT_FLAG: - parse_option_bool(name, value, &flag, &local_err); - if (!error_is_set(&local_err)) { - list->value.n = flag; - } - break; - - case OPT_STRING: - if (value != NULL) { - list->value.s = g_strdup(value); - } else { - fprintf(stderr, "Option '%s' needs a parameter\n", name); - return -1; - } - break; - - case OPT_SIZE: - parse_option_size(name, value, &list->value.n, &local_err); - break; - - default: - fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name); - return -1; - } - - if (error_is_set(&local_err)) { - qerror_report_err(local_err); - error_free(local_err); - return -1; - } - - list->assigned = true; - - return 0; -} - -/* - * Sets the given parameter to an integer instead of a string. - * This function cannot be used to set string options. - * - * Returns 0 on success, -1 in error cases - */ -int set_option_parameter_int(QEMUOptionParameter *list, const char *name, - uint64_t value) -{ - // Find a matching parameter - list = get_option_parameter(list, name); - if (list == NULL) { - fprintf(stderr, "Unknown option '%s'\n", name); - return -1; - } - - // Process parameter - switch (list->type) { - case OPT_FLAG: - case OPT_NUMBER: - case OPT_SIZE: - list->value.n = value; - break; - - default: - return -1; - } - - list->assigned = true; - - return 0; -} - -/* - * Frees a option list. If it contains strings, the strings are freed as well. - */ -void free_option_parameters(QEMUOptionParameter *list) -{ - QEMUOptionParameter *cur = list; - - while (cur && cur->name) { - if (cur->type == OPT_STRING) { - g_free(cur->value.s); - } - cur++; - } - - g_free(list); -} - -/* - * Count valid options in list - */ -static size_t count_option_parameters(QEMUOptionParameter *list) -{ - size_t num_options = 0; - - while (list && list->name) { - num_options++; - list++; - } - - return num_options; -} - -/* - * Append an option list (list) to an option list (dest). - * - * If dest is NULL, a new copy of list is created. - * - * Returns a pointer to the first element of dest (or the newly allocated copy) - */ -QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, - QEMUOptionParameter *list) -{ - size_t num_options, num_dest_options; - - num_options = count_option_parameters(dest); - num_dest_options = num_options; - - num_options += count_option_parameters(list); - - dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter)); - dest[num_dest_options].name = NULL; - - while (list && list->name) { - if (get_option_parameter(dest, list->name) == NULL) { - dest[num_dest_options++] = *list; - dest[num_dest_options].name = NULL; - } - list++; - } - - return dest; -} - static size_t count_opts_list(QemuOptsList *list) { QemuOptDesc *desc = NULL; @@ -445,120 +265,6 @@ QemuOptsList *qemu_opts_append(QemuOptsList *dst, return tmp; } -/* - * Parses a parameter string (param) into an option list (dest). - * - * list is the template option list. If dest is NULL, a new copy of list is - * created. If list is NULL, this function fails. - * - * A parameter string consists of one or more parameters, separated by commas. - * Each parameter consists of its name and possibly of a value. In the latter - * case, the value is delimited by an = character. To specify a value which - * contains commas, double each comma so it won't be recognized as the end of - * the parameter. - * - * For more details of the parsing see above. - * - * Returns a pointer to the first element of dest (or the newly allocated copy) - * or NULL in error cases - */ -QEMUOptionParameter *parse_option_parameters(const char *param, - QEMUOptionParameter *list, QEMUOptionParameter *dest) -{ - QEMUOptionParameter *allocated = NULL; - char name[256]; - char value[256]; - char *param_delim, *value_delim; - char next_delim; - int i; - - if (list == NULL) { - return NULL; - } - - if (dest == NULL) { - dest = allocated = append_option_parameters(NULL, list); - } - - for (i = 0; dest[i].name; i++) { - dest[i].assigned = false; - } - - while (*param) { - - // Find parameter name and value in the string - param_delim = strchr(param, ','); - value_delim = strchr(param, '='); - - if (value_delim && (value_delim < param_delim || !param_delim)) { - next_delim = '='; - } else { - next_delim = ','; - value_delim = NULL; - } - - param = get_opt_name(name, sizeof(name), param, next_delim); - if (value_delim) { - param = get_opt_value(value, sizeof(value), param + 1); - } - if (*param != '\0') { - param++; - } - - // Set the parameter - if (set_option_parameter(dest, name, value_delim ? value : NULL)) { - goto fail; - } - } - - return dest; - -fail: - // Only free the list if it was newly allocated - free_option_parameters(allocated); - return NULL; -} - -/* - * Prints all options of a list that have a value to stdout - */ -void print_option_parameters(QEMUOptionParameter *list) -{ - while (list && list->name) { - switch (list->type) { - case OPT_STRING: - if (list->value.s != NULL) { - printf("%s='%s' ", list->name, list->value.s); - } - break; - case OPT_FLAG: - printf("%s=%s ", list->name, list->value.n ? "on" : "off"); - break; - case OPT_SIZE: - case OPT_NUMBER: - printf("%s=%" PRId64 " ", list->name, list->value.n); - break; - default: - printf("%s=(unknown type) ", list->name); - break; - } - list++; - } -} - -/* - * Prints an overview of all available options - */ -void print_option_help(QEMUOptionParameter *list) -{ - printf("Supported options:\n"); - while (list && list->name) { - printf("%-16s %s\n", list->name, - list->help ? list->help : "No description available"); - list++; - } -} - /* ------------------------------------------------------------------ */ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) -- 1.6.0.2