From nobody Mon Feb 9 14:37:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1561398161; cv=none; d=zoho.com; s=zohoarc; b=Pl8OjG3Xh1cfVqGzPSn4SgKwJMg5PUou7pYeslixpa1Ymy/5E+H/Yrqx2n2LKHY8pUerPpmC9iDyR+03atiUs7sEVfZrmesXDPUrDtqO8ZyvK+kdheuAI2bp229ajGCLiXg8MS+FN6vUaNu781L1FdUPdxzQ8uOlLOZbF5elYiI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561398161; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=HyYwWfM7v41K20Klynjo5p52bRcLGrbOFWbIJzTUH8g=; b=J/hDf6058MrbFH2ZRn+35DijMNx/55SKJCsudyD9nHOmSkBWZMISP6U9XHfQKOS5NtwF+9N2WcOp1gYgoz3vyChD3gtJ/2ZSpqp3CwwerbBBAYWXRq24GFgL3Dyr/9L3oy3tzzceEyNZRm9ev7WwifQHMMiWmzE5unNej3fFNR4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1561398161638718.7082215448862; Mon, 24 Jun 2019 10:42:41 -0700 (PDT) Received: from localhost ([::1]:53456 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hfSzE-0003AB-0n for importer@patchew.org; Mon, 24 Jun 2019 13:42:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48766) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hfSwj-00018b-AT for qemu-devel@nongnu.org; Mon, 24 Jun 2019 13:40:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hfSwd-0004jo-La for qemu-devel@nongnu.org; Mon, 24 Jun 2019 13:39:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36692) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hfSwX-0004BT-Vk; Mon, 24 Jun 2019 13:39:50 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 43F2E30832E4; Mon, 24 Jun 2019 17:39:42 +0000 (UTC) Received: from localhost (ovpn-204-152.brq.redhat.com [10.40.204.152]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D38E660BE2; Mon, 24 Jun 2019 17:39:41 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 24 Jun 2019 19:39:22 +0200 Message-Id: <20190624173935.25747-3-mreitz@redhat.com> In-Reply-To: <20190624173935.25747-1-mreitz@redhat.com> References: <20190624173935.25747-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Mon, 24 Jun 2019 17:39:42 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 02/14] qapi: Move to_c_string() to common.py X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Michael Roth , Markus Armbruster , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" This function will be useful for code generation once we allow default values, so move it to the other "C helper functions". In the process, rewrite it so it supports all nonprintable and non-ASCII characters. Signed-off-by: Max Reitz --- scripts/qapi/common.py | 26 ++++++++++++++++++++++++++ scripts/qapi/introspect.py | 4 ---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 3396ea4a09..c6754a5856 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -2208,6 +2208,32 @@ def c_fname(filename): return re.sub(r'[^A-Za-z0-9_]', '_', filename) =20 =20 +# Translates a string to a valid C constant +def to_c_string(string): + result =3D '"' + + python2 =3D isinstance(string, bytes) + if not python2: + # Will return integers when iterated over + string =3D string.encode() + + for c in string: + value =3D ord(c) if python2 else c + if value < 0x20 or value > 0x7e: + result +=3D '\\%03o' % value + else: + c =3D chr(value) + if c =3D=3D '"': + result +=3D '\\"' + elif c =3D=3D '\\': + result +=3D '\\\\' + else: + result +=3D c + + result +=3D '"' + return result + + def guardstart(name): return mcgen(''' #ifndef %(name)s diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py index 6a61dd831f..572e0b8331 100644 --- a/scripts/qapi/introspect.py +++ b/scripts/qapi/introspect.py @@ -66,10 +66,6 @@ def to_qlit(obj, level=3D0, suppress_first_indent=3DFals= e): return ret =20 =20 -def to_c_string(string): - return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"' - - class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor): =20 def __init__(self, prefix, unmask): --=20 2.21.0