From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bosCh-0004ez-Vq for qemu-devel@nongnu.org; Tue, 27 Sep 2016 09:13:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bosCg-0003ic-9C for qemu-devel@nongnu.org; Tue, 27 Sep 2016 09:13:47 -0400 From: "Daniel P. Berrange" Date: Tue, 27 Sep 2016 14:13:04 +0100 Message-Id: <1474982001-20878-3-git-send-email-berrange@redhat.com> In-Reply-To: <1474982001-20878-1-git-send-email-berrange@redhat.com> References: <1474982001-20878-1-git-send-email-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v14 02/19] option: make parse_option_bool/number non-static List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= , "Daniel P. Berrange" The opts-visitor.c opts_type_bool() method has code for parsing a string to set a bool value, as does the qemu-option.c parse_option_bool() method, except it handles fewer cases. To enable consistency across the codebase, extend parse_option_bool() to handle "yes", "no", "y" and "n", and make it non-static. Convert the opts visitor to call this method directly. Also make parse_option_number() non-static to allow for similar reuse later. Reviewed-by: Kevin Wolf Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster Signed-off-by: Daniel P. Berrange --- include/qemu/option.h | 4 ++++ qapi/opts-visitor.c | 19 +------------------ tests/qemu-iotests/051.out | 6 +++--- tests/qemu-iotests/051.pc.out | 6 +++--- tests/qemu-iotests/137.out | 4 ++-- util/qemu-option.c | 27 ++++++++++++++++----------- 6 files changed, 29 insertions(+), 37 deletions(-) diff --git a/include/qemu/option.h b/include/qemu/option.h index 1f9e3f9..2a5266f 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -37,6 +37,10 @@ int get_param_value(char *buf, int buf_size, const char *tag, const char *str); =20 =20 +void parse_option_bool(const char *name, const char *value, bool *ret, + Error **errp); +void parse_option_number(const char *name, const char *value, + uint64_t *ret, Error **errp); void parse_option_size(const char *name, const char *value, uint64_t *ret, Error **errp); bool has_help_option(const char *param); diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c index 1048bbc..084f7cc 100644 --- a/qapi/opts-visitor.c +++ b/qapi/opts-visitor.c @@ -334,7 +334,6 @@ opts_type_str(Visitor *v, const char *name, char **ob= j, Error **errp) } =20 =20 -/* mimics qemu-option.c::parse_option_bool() */ static void opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) { @@ -346,23 +345,7 @@ opts_type_bool(Visitor *v, const char *name, bool *o= bj, Error **errp) return; } =20 - if (opt->str) { - if (strcmp(opt->str, "on") =3D=3D 0 || - strcmp(opt->str, "yes") =3D=3D 0 || - strcmp(opt->str, "y") =3D=3D 0) { - *obj =3D true; - } else if (strcmp(opt->str, "off") =3D=3D 0 || - strcmp(opt->str, "no") =3D=3D 0 || - strcmp(opt->str, "n") =3D=3D 0) { - *obj =3D false; - } else { - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, - "on|yes|y|off|no|n"); - return; - } - } else { - *obj =3D true; - } + parse_option_bool(opt->name, opt->str, obj, errp); =20 processed(ov, name); } diff --git a/tests/qemu-iotests/051.out b/tests/qemu-iotests/051.out index 408d613..ffdc0b9 100644 --- a/tests/qemu-iotests/051.out +++ b/tests/qemu-iotests/051.out @@ -86,13 +86,13 @@ QEMU X.Y.Z monitor - type 'help' for more information (qemu) q=1B[K=1B[Dqu=1B[K=1B[D=1B[Dqui=1B[K=1B[D=1B[D=1B[Dquit=1B[K =20 Testing: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D -QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= : Parameter 'lazy-refcounts' expects 'on' or 'off' +QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= : Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or 'n' =20 Testing: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= 42 -QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= 42: Parameter 'lazy-refcounts' expects 'on' or 'off' +QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= 42: Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or '= n' =20 Testing: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= foo -QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= foo: Parameter 'lazy-refcounts' expects 'on' or 'off' +QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= foo: Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or = 'n' =20 =20 =3D=3D=3D With version 2 images enabling lazy refcounts must fail =3D=3D= =3D diff --git a/tests/qemu-iotests/051.pc.out b/tests/qemu-iotests/051.pc.ou= t index ec6d222..6abf1ab 100644 --- a/tests/qemu-iotests/051.pc.out +++ b/tests/qemu-iotests/051.pc.out @@ -86,13 +86,13 @@ QEMU X.Y.Z monitor - type 'help' for more information (qemu) q=1B[K=1B[Dqu=1B[K=1B[D=1B[Dqui=1B[K=1B[D=1B[D=1B[Dquit=1B[K =20 Testing: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D -QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= : Parameter 'lazy-refcounts' expects 'on' or 'off' +QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= : Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or 'n' =20 Testing: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= 42 -QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= 42: Parameter 'lazy-refcounts' expects 'on' or 'off' +QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= 42: Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or '= n' =20 Testing: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= foo -QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= foo: Parameter 'lazy-refcounts' expects 'on' or 'off' +QEMU_PROG: -drive file=3DTEST_DIR/t.qcow2,format=3Dqcow2,lazy-refcounts=3D= foo: Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or = 'n' =20 =20 =3D=3D=3D With version 2 images enabling lazy refcounts must fail =3D=3D= =3D diff --git a/tests/qemu-iotests/137.out b/tests/qemu-iotests/137.out index c0e7534..61c1f90 100644 --- a/tests/qemu-iotests/137.out +++ b/tests/qemu-iotests/137.out @@ -15,7 +15,7 @@ read 33554432/33554432 bytes at offset 0 =20 =3D=3D=3D Try setting some invalid values =3D=3D=3D =20 -Parameter 'lazy-refcounts' expects 'on' or 'off' +Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or 'n' cache-size, l2-cache-size and refcount-cache-size may not be set the sam= e time l2-cache-size may not exceed cache-size refcount-cache-size may not exceed cache-size @@ -40,7 +40,7 @@ incompatible_features 0x0 Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D67108864 wrote 65536/65536 bytes at offset 0 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) -Parameter 'lazy-refcounts' expects 'on' or 'off' +Parameter 'lazy-refcounts' expects 'on', 'yes', 'y', 'off', 'no' or 'n' qcow2: Marking image as corrupt: Preventing invalid write on metadata (o= verlaps with qcow2_header); further corruption events will be suppressed write failed: Input/output error *** done diff --git a/util/qemu-option.c b/util/qemu-option.c index 3467dc2..41b356c 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -125,25 +125,30 @@ int get_param_value(char *buf, int buf_size, return get_next_param_value(buf, buf_size, tag, &str); } =20 -static void parse_option_bool(const char *name, const char *value, bool = *ret, - Error **errp) +void parse_option_bool(const char *name, const char *value, bool *ret, + Error **errp) { if (value !=3D NULL) { - if (!strcmp(value, "on")) { - *ret =3D 1; - } else if (!strcmp(value, "off")) { - *ret =3D 0; + if (strcmp(value, "on") =3D=3D 0 || + strcmp(value, "yes") =3D=3D 0 || + strcmp(value, "y") =3D=3D 0) { + *ret =3D true; + } else if (strcmp(value, "off") =3D=3D 0 || + strcmp(value, "no") =3D=3D 0 || + strcmp(value, "n") =3D=3D 0) { + *ret =3D false; } else { - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, - name, "'on' or 'off'"); + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, + "'on', 'yes', 'y', 'off', 'no' or 'n'"); + return; } } else { - *ret =3D 1; + *ret =3D true; } } =20 -static void parse_option_number(const char *name, const char *value, - uint64_t *ret, Error **errp) +void parse_option_number(const char *name, const char *value, + uint64_t *ret, Error **errp) { char *postfix; uint64_t number; --=20 2.7.4