From patchwork Thu Jun 18 16:43:48 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: 486410 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 D28B214027C for ; Fri, 19 Jun 2015 02:44:57 +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=YM6w1o53; dkim-atps=neutral Received: from localhost ([::1]:53905 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5cvv-0000he-RF for incoming@patchwork.ozlabs.org; Thu, 18 Jun 2015 12:44:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54847) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5cv4-0007V3-Fs for qemu-devel@nongnu.org; Thu, 18 Jun 2015 12:44:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z5cv3-0000tq-4z for qemu-devel@nongnu.org; Thu, 18 Jun 2015 12:44:02 -0400 Received: from mail-wg0-x231.google.com ([2a00:1450:400c:c00::231]:36146) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5cv2-0000tf-Tj; Thu, 18 Jun 2015 12:44:01 -0400 Received: by wgzl5 with SMTP id l5so68831102wgz.3; Thu, 18 Jun 2015 09:44:00 -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=Qu0vkfSiTMKqVMQ5HPvl6yR5zimjHCkrAWKlxI2SmBE=; b=YM6w1o53InCjhiS3hagnVOyqRNJliGhSkQja1Hi0gdXW42OveZvfxuWkl5IfMDo0kD 5JquZJ3ME55lSOm1UcLWCrxydiwAW/N9cervcv98hIpVUP1Y9lrh1bPTtA5PTgQ9+0Ul sytSI6MOseUpIAeqDu5525UJ8gIfFpf60VAy7BRPIezSW6Hddy1mN1UFTb14D7S8PfeV 6UKKClt+9tujb4CBY/O336YFNRGwM7SD69pjMIGcddK80r9WrIUau0iYpfGupm5VM61T M5zlgvP457T77tPV8egMoDTJWfeYnxffUn3IbZ5QZtzXj5Kyx+gnjJIB2WWGyGduvkMd U8xg== X-Received: by 10.180.37.229 with SMTP id b5mr66320482wik.16.1434645840159; Thu, 18 Jun 2015 09:44:00 -0700 (PDT) Received: from nullptr.home.dirty-ice.org (178-164-167-192.pool.digikabel.hu. [178.164.167.192]) by mx.google.com with ESMTPSA id fx7sm13006423wjb.10.2015.06.18.09.43.59 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Jun 2015 09:43:59 -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: Thu, 18 Jun 2015 18:43:48 +0200 Message-Id: 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:c00::231 Cc: Kevin Wolf , Gerd Hoffmann , "open list:Block layer core" Subject: [Qemu-devel] [PATCH v3 6/8] 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 v2: * 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 dd4f58d..c5d456c 100644 --- a/block.c +++ b/block.c @@ -3823,7 +3823,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 840f5f7..911f02d 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -728,14 +728,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; } @@ -748,13 +787,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; } }