From patchwork Tue Jun 23 13:33:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?UTF-8?B?Wm9sdMOhbiBLxZF2w6Fnw7M=?= X-Patchwork-Id: 487627 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8641A14010F for ; Tue, 23 Jun 2015 23:33:38 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=l2XoOXgX; dkim-atps=neutral Received: from localhost ([::1]:45353 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z7OKW-0000zr-Mp for incoming@patchwork.ozlabs.org; Tue, 23 Jun 2015 09:33:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33232) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z7OK2-00007Z-I2 for qemu-devel@nongnu.org; Tue, 23 Jun 2015 09:33:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z7OK1-0008LE-K7 for qemu-devel@nongnu.org; Tue, 23 Jun 2015 09:33:06 -0400 Received: from mail-wi0-x230.google.com ([2a00:1450:400c:c05::230]:33831) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z7OK1-0008L7-EB; Tue, 23 Jun 2015 09:33:05 -0400 Received: by wicnd19 with SMTP id nd19so106082799wic.1; Tue, 23 Jun 2015 06:33:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=aDTbcgiVoIjl7MG+iQj1y2wDoPXC9lWpsICZKvhEpJI=; b=l2XoOXgXy4faIqI7fiGIqrEAOgLxL8pSqwmw74JwcziAsHMW1hUmqbPse/OUQ1lSDS /Oec8uc7DzDvqCmPdyd/i8rIbKyXTr6CxROYPfGVETfA0cirntwggmfJETmBVF4AQdqY WCQv5Ga23j5Wq/pXcuzuGId2rUWfmbPjWDE+A+wnCCqBjXrukvmtPcLvLzW37HCCAXcG 77YBdqrYCFkZqhZHJmi0rSX6TTVizTjY/m6nY+Q1ulI+dPmSc01OVqdpXA+U3hVgjXTg hjamrgqp8EZsDk59ugJ8zC/4H2N3prfmNNNF1Y2x5rqru+iNU7bnq0mMeqCFlBQEn0DR zzaQ== X-Received: by 10.180.77.136 with SMTP id s8mr3523835wiw.7.1435066384893; Tue, 23 Jun 2015 06:33:04 -0700 (PDT) Received: from nullptr.home.dirty-ice.org (178-164-170-200.pool.digikabel.hu. [178.164.170.200]) by mx.google.com with ESMTPSA id fi6sm22381631wib.6.2015.06.23.06.33.03 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 23 Jun 2015 06:33:04 -0700 (PDT) From: "=?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?=" X-Google-Original-From: =?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?= To: qemu-devel@nongnu.org Date: Tue, 23 Jun 2015 15:33:01 +0200 Message-Id: <086dea7d41d459f7182908663df451f897e299e8.1435064848.git.DirtY.iCE.hu@gmail.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::230 Cc: Kevin Wolf , "open list:Block layer core" , Markus Armbruster , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 5/5] opts: produce valid command line in qemu_opts_print X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This will let us print options in a format that the user would actually write it on the command line (foo=bar,baz=asd,etc=def), without prepending a spurious comma at the beginning of the list, or quoting values unnecessarily. This patch provides the following changes: * write and id=, 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ővágó, Zoltán --- Chages from v1 -audiodev patch: * print id= * 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 char *fmt, } if (!quiet) { - printf("Formatting '%s', fmt=%s", filename, fmt); + printf("Formatting '%s', fmt=%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); } -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 = false; + + for (ptr = value; *ptr; ++ptr) { + if (!qemu_isalnum(*ptr)) { + need_quote = true; + break; + } + } + + if (need_quote) { + putchar('\''); + for (ptr = value; *ptr; ++ptr) { + if (*ptr == '\'') { + printf("'\\''"); + } else if (*ptr == ',') { + printf(",,"); + } else { + putchar(*ptr); + } + } + putchar('\''); + } else { + printf("%s", value); + } +} + +void qemu_opts_print(QemuOpts *opts, const char *separator) { QemuOpt *opt; QemuOptDesc *desc = opts->list->desc; + const char *sep = ""; + + if (opts->id) { + printf("id="); + escaped_print(opts->id); + sep = separator; + } if (desc[0].name == NULL) { QTAILQ_FOREACH(opt, &opts->head, next) { - printf("%s%s=\"%s\"", sep, opt->name, opt->str); + printf("%s%s=", sep, opt->name); + escaped_print(opt->str); + sep = separator; } return; } @@ -750,13 +789,15 @@ void qemu_opts_print(QemuOpts *opts, const char *sep) continue; } if (desc->type == QEMU_OPT_STRING) { - printf("%s%s='%s'", sep, desc->name, value); + printf("%s%s=", sep, desc->name); + escaped_print(value); } else if ((desc->type == QEMU_OPT_SIZE || desc->type == QEMU_OPT_NUMBER) && opt) { printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint); } else { printf("%s%s=%s", sep, desc->name, value); } + sep = separator; } }