Received: from eggs.gnu.org ([2001:4830:134:3::10]:39457) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAikx-00004t-Ul for qemu-devel@nongnu.org; Thu, 02 Jul 2015 13:58:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZAikw-0005Ge-AR for qemu-devel@nongnu.org; Thu, 02 Jul 2015 13:58:39 -0400 From: Markus Armbruster References: <086dea7d41d459f7182908663df451f897e299e8.1435064848.git.DirtY.iCE.hu@gmail.com> Date: Thu, 02 Jul 2015 19:58:28 +0200 In-Reply-To: <086dea7d41d459f7182908663df451f897e299e8.1435064848.git.DirtY.iCE.hu@gmail.com> (=?utf-8?B?IkvFkXbDoWfDsywgWm9sdMOhbiIncw==?= message of "Tue, 23 Jun 2015 15:33:01 +0200") Message-ID: <87bnfuqw57.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 5/5] opts: produce valid command line in qemu_opts_print List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?B?S8WRdsOhZ8OzLCBab2x0w6Fu?= Cc: Kevin Wolf , Stefan Hajnoczi , qemu-devel@nongnu.org, "open list:Block layer core" "K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n" writes: > This will let us print options in a format that the user would actually > write it on the command line (foo=3Dbar,baz=3Dasd,etc=3Ddef), without > prepending a spurious comma at the beginning of the list, or quoting > values unnecessarily. This patch provides the following changes: > * write and id=3D, if the option has an id > * do not print separator before the first element > * do not quote string arguments which only contains letters or numbers > * properly escape commas (,) for QEMU, apostrophe (') for shell > > Signed-off-by: K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n > > --- > > Chages from v1 -audiodev patch: > * print id=3D > * proper value escaping (apostrophe and comma) > * renamed d_sep -> separator > > > block.c | 2 +- > util/qemu-option.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- > 2 files changed, 45 insertions(+), 4 deletions(-) > > diff --git a/block.c b/block.c > index e2e33fd..a18af00 100644 > --- a/block.c > +++ b/block.c > @@ -3825,7 +3825,7 @@ void bdrv_img_create(const char *filename, const ch= ar *fmt, > } >=20=20 > if (!quiet) { > - printf("Formatting '%s', fmt=3D%s", filename, fmt); > + printf("Formatting '%s', fmt=3D%s ", filename, fmt); > qemu_opts_print(opts, " "); > puts(""); > } > diff --git a/util/qemu-option.c b/util/qemu-option.c > index efe9d27..db60ac9 100644 > --- a/util/qemu-option.c > +++ b/util/qemu-option.c > @@ -730,14 +730,53 @@ void qemu_opts_del(QemuOpts *opts) > g_free(opts); > } >=20=20 > -void qemu_opts_print(QemuOpts *opts, const char *sep) > +/* print value properly escaping it for the shell (at least for bash) */ > +static void escaped_print(const char *value) > +{ > + const char *ptr; > + bool need_quote =3D false; > + > + for (ptr =3D value; *ptr; ++ptr) { > + if (!qemu_isalnum(*ptr)) { > + need_quote =3D true; > + break; > + } > + } Rather eager to quote. Shell wants you to quote | & ; < > ( ) $ ` \ " ' always, and * ? [ # =CB=9C =3D % in some contexts. In particular, there is no need to quote the common characters '.', '-' and '_'. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#ta= g_18_02 > + > + if (need_quote) { > + putchar('\''); > + for (ptr =3D value; *ptr; ++ptr) { > + if (*ptr =3D=3D '\'') { > + printf("'\\''"); Won't achieve the stated purpose "for shell", because backslash escapes do not work within single quotes. You could try double quote, but things become even more complicated then. > + } else if (*ptr =3D=3D ',') { > + printf(",,"); > + } else { > + putchar(*ptr); > + } If we do backslash escapes, should we escape non-printing characters? > + } > + putchar('\''); > + } else { > + printf("%s", value); > + } > +} > + > +void qemu_opts_print(QemuOpts *opts, const char *separator) > { > QemuOpt *opt; > QemuOptDesc *desc =3D opts->list->desc; > + const char *sep =3D ""; > + > + if (opts->id) { > + printf("id=3D"); > + escaped_print(opts->id); Since opts->id has passed id_wellformed(), it won't need quoting. Your escaped_print() may quote it anyway, though. > + sep =3D separator; > + } >=20=20 > if (desc[0].name =3D=3D NULL) { > QTAILQ_FOREACH(opt, &opts->head, next) { > - printf("%s%s=3D\"%s\"", sep, opt->name, opt->str); > + printf("%s%s=3D", sep, opt->name); > + escaped_print(opt->str); > + sep =3D separator; > } > return; > } > @@ -750,13 +789,15 @@ void qemu_opts_print(QemuOpts *opts, const char *se= p) > continue; > } > if (desc->type =3D=3D QEMU_OPT_STRING) { > - printf("%s%s=3D'%s'", sep, desc->name, value); > + printf("%s%s=3D", sep, desc->name); > + escaped_print(value); > } else if ((desc->type =3D=3D QEMU_OPT_SIZE || > desc->type =3D=3D QEMU_OPT_NUMBER) && opt) { > printf("%s%s=3D%" PRId64, sep, desc->name, opt->value.uint); > } else { > printf("%s%s=3D%s", sep, desc->name, value); > } > + sep =3D separator; > } > } Things I like about the patch: * Making parameter sep a true separator * Printing opts->id * Escaping comma I'm not sure the "quote for shell" feature is worth the bother. If we want it, then "foo=3D,msg=3D\"hello\",gnu=3Don" would perhaps be nicer than foo=3D"",msg=3D"\"hello\"",gnu=3Don but could be even more complicated to do. If you want to pursue quoting for shell, I recommend to split this patch into a part I like and a part we want to discuss :)