From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 04/19] Add support for a option parameter as an enum
Date: Mon, 7 Jun 2010 15:42:17 +0100 [thread overview]
Message-ID: <1275921752-29420-5-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1275921752-29420-1-git-send-email-berrange@redhat.com>
This adds a new option parameter QEMU_OPT_ENUM. The user
provides the value in its string representation. The parser
validates this and converts it to integer representation
for internal use. If the user supplies an invalid value
it will report the precise allowed list of values.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
qemu-option.c | 31 +++++++++++++++++++++++++++++++
qemu-option.h | 10 ++++++++++
2 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/qemu-option.c b/qemu-option.c
index acd74f9..bb9cc43 100644
--- a/qemu-option.c
+++ b/qemu-option.c
@@ -238,6 +238,24 @@ static int parse_option_size(const char *name, const char *value, uint64_t *ret)
return 0;
}
+static int parse_option_enum(const char *name, const char *value, int *ret, const QemuOptDesc *desc)
+{
+ if (!value) {
+ qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a enumerated string");
+ return -1;
+ }
+
+ *ret = (desc->validate.optEnum.from_string)(value);
+ if (*ret < 0) {
+ char *valid = (desc->validate.optEnum.to_string_list)();
+ qerror_report(QERR_INVALID_PARAMETER_VALUE, name, valid);
+ qemu_free(valid);
+ return -1;
+ }
+ return 0;
+}
+
+
/*
* Sets the value of a parameter in a given option list. The parsing of the
* value depends on the type of option:
@@ -516,6 +534,7 @@ struct QemuOpt {
union {
int boolean;
uint64_t uint;
+ int enumVal;
} value;
QemuOpts *opts;
@@ -578,6 +597,16 @@ uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
return opt->value.uint;
}
+int qemu_opt_get_enum(QemuOpts *opts, const char *name, int defval)
+{
+ QemuOpt *opt = qemu_opt_find(opts, name);
+
+ if (opt == NULL)
+ return defval;
+ assert(opt->desc && opt->desc->type == QEMU_OPT_ENUM);
+ return opt->value.enumVal;
+}
+
static int qemu_opt_parse(QemuOpt *opt)
{
if (opt->desc == NULL)
@@ -592,6 +621,8 @@ static int qemu_opt_parse(QemuOpt *opt)
return parse_option_number(opt->name, opt->str, &opt->value.uint);
case QEMU_OPT_SIZE:
return parse_option_size(opt->name, opt->str, &opt->value.uint);
+ case QEMU_OPT_ENUM:
+ return parse_option_enum(opt->name, opt->str, &opt->value.enumVal, opt->desc);
default:
abort();
}
diff --git a/qemu-option.h b/qemu-option.h
index 4823219..3540a9b 100644
--- a/qemu-option.h
+++ b/qemu-option.h
@@ -89,12 +89,21 @@ enum QemuOptType {
QEMU_OPT_BOOL, /* on/off */
QEMU_OPT_NUMBER, /* simple number */
QEMU_OPT_SIZE, /* size, accepts (K)ilo, (M)ega, (G)iga, (T)era postfix */
+ QEMU_OPT_ENUM, /* int, from user string validated against an enum */
};
typedef struct QemuOptDesc {
const char *name;
enum QemuOptType type;
const char *help;
+ union {
+ struct {
+ int (*from_string)(const char *);
+ const char *(*to_string)(int);
+ char *(*to_string_list)(void);
+ int last;
+ } optEnum;
+ } validate;
} QemuOptDesc;
struct QemuOptsList {
@@ -108,6 +117,7 @@ const char *qemu_opt_get(QemuOpts *opts, const char *name);
int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval);
uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval);
uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval);
+int qemu_opt_get_enum(QemuOpts *opts, const char *name, int defval);
int qemu_opt_set(QemuOpts *opts, const char *name, const char *value);
typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaque);
int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
--
1.6.6.1
next prev parent reply other threads:[~2010-06-07 14:43 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-07 14:42 [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 01/19] Add support for JSON pretty printing Daniel P. Berrange
2010-06-09 19:51 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 02/19] Add support for compile time assertions Daniel P. Berrange
2010-06-07 15:35 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 03/19] Add enum handlers for easy & efficient string <-> int conversion Daniel P. Berrange
2010-06-09 19:52 ` Luiz Capitulino
2010-06-26 6:52 ` Markus Armbruster
2010-06-07 14:42 ` Daniel P. Berrange [this message]
2010-06-26 6:59 ` [Qemu-devel] [PATCH 04/19] Add support for a option parameter as an enum Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 05/19] Ensure that QEMU exits if drive_add parsing fails Daniel P. Berrange
2010-06-26 7:04 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 06/19] Convert drive options to use enumeration data type Daniel P. Berrange
2010-06-09 19:52 ` Luiz Capitulino
2010-06-26 7:07 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 07/19] Convert netdev client types to use an enumeration Daniel P. Berrange
2010-06-07 15:09 ` Anthony Liguori
2010-06-07 15:13 ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 08/19] Convert RTC to use enumerations for configuration parameters Daniel P. Berrange
2010-06-09 19:54 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 09/19] Change 'query-version' to output broken down version string Daniel P. Berrange
2010-06-07 15:11 ` Anthony Liguori
2010-06-09 20:04 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 10/19] Add a query-machines command to QMP Daniel P. Berrange
2010-06-07 15:13 ` Anthony Liguori
2010-06-07 16:44 ` Daniel P. Berrange
2010-06-07 17:07 ` Anthony Liguori
2010-06-07 17:14 ` Daniel P. Berrange
2010-06-09 20:06 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 11/19] Add a query-devices " Daniel P. Berrange
2010-06-07 15:14 ` Anthony Liguori
2010-06-07 16:03 ` Daniel P. Berrange
2010-06-26 7:12 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 12/19] Add a query-cputypes " Daniel P. Berrange
2010-06-26 7:18 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 13/19] Add a query-target " Daniel P. Berrange
2010-06-07 15:43 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 14/19] Add a query-argv " Daniel P. Berrange
2010-06-07 15:01 ` Anthony Liguori
2010-06-10 15:34 ` [Qemu-devel] " Paolo Bonzini
2010-06-26 7:30 ` [Qemu-devel] " Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 15/19] Expand query-argv to include help string Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 16/19] Add a query-netdev command to QMP Daniel P. Berrange
2010-06-07 15:15 ` Anthony Liguori
2010-06-26 7:32 ` Markus Armbruster
2010-06-07 19:13 ` Miguel Di Ciurcio Filho
2010-06-08 8:38 ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 17/19] Add a query-config " Daniel P. Berrange
2010-06-26 7:34 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 18/19] Add option to turn on JSON pretty printing in monitor Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 19/19] Add a -capabilities argument to allow easy query for static QEMU info Daniel P. Berrange
2010-06-07 16:04 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:09 ` Daniel P. Berrange
2010-06-07 16:04 ` [Qemu-devel] " Anthony Liguori
2010-06-07 14:58 ` [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Anthony Liguori
2010-06-07 15:10 ` Daniel P. Berrange
2010-06-07 15:18 ` Anthony Liguori
2010-06-07 16:02 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:03 ` Anthony Liguori
2010-06-07 16:07 ` [Qemu-devel] " Anthony Liguori
2010-06-09 20:25 ` Luiz Capitulino
2010-06-26 6:45 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1275921752-29420-5-git-send-email-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).