From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
qemu-devel@nongnu.org, Michael Roth <mdroth@linux.vnet.ibm.com>,
Markus Armbruster <armbru@redhat.com>,
Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH v4 02/14] qapi: Move to_c_string() to common.py
Date: Mon, 24 Jun 2019 19:39:22 +0200 [thread overview]
Message-ID: <20190624173935.25747-3-mreitz@redhat.com> (raw)
In-Reply-To: <20190624173935.25747-1-mreitz@redhat.com>
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 <mreitz@redhat.com>
---
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)
+# Translates a string to a valid C constant
+def to_c_string(string):
+ result = '"'
+
+ python2 = isinstance(string, bytes)
+ if not python2:
+ # Will return integers when iterated over
+ string = string.encode()
+
+ for c in string:
+ value = ord(c) if python2 else c
+ if value < 0x20 or value > 0x7e:
+ result += '\\%03o' % value
+ else:
+ c = chr(value)
+ if c == '"':
+ result += '\\"'
+ elif c == '\\':
+ result += '\\\\'
+ else:
+ result += c
+
+ result += '"'
+ 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=0, suppress_first_indent=False):
return ret
-def to_c_string(string):
- return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
-
-
class QAPISchemaGenIntrospectVisitor(QAPISchemaMonolithicCVisitor):
def __init__(self, prefix, unmask):
--
2.21.0
next prev parent reply other threads:[~2019-06-24 17:42 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-24 17:39 [Qemu-devel] [PATCH v4 00/14] block: Try to create well-typed json:{} filenames Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 01/14] qapi: Parse numeric values Max Reitz
2019-11-14 9:15 ` Markus Armbruster
2019-11-14 9:50 ` Max Reitz
2019-11-14 12:01 ` Markus Armbruster
2019-06-24 17:39 ` Max Reitz [this message]
2019-11-14 9:20 ` [Qemu-devel] [PATCH v4 02/14] qapi: Move to_c_string() to common.py Markus Armbruster
2019-11-14 9:58 ` Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 03/14] qapi: Introduce default values for struct members Max Reitz
2019-11-14 15:53 ` Markus Armbruster
2019-11-21 15:07 ` Markus Armbruster
2019-11-21 15:24 ` Eric Blake
2019-11-21 19:46 ` Markus Armbruster
2019-11-21 19:56 ` Eric Blake
2019-11-22 7:29 ` Markus Armbruster
2019-11-22 10:25 ` Kevin Wolf
2019-11-22 14:40 ` Markus Armbruster
2019-11-22 16:12 ` Kevin Wolf
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 04/14] qapi: Allow optional discriminators Max Reitz
2019-11-21 15:13 ` Markus Armbruster
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 05/14] qapi: Document default values for struct members Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 06/14] test-qapi: Print struct members' default values Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 07/14] tests: Test QAPI default values for struct members Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 08/14] tests: Add QAPI optional discriminator tests Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 09/14] qapi: Formalize qcow2 encryption probing Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 10/14] qapi: Formalize qcow " Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 11/14] block: Try to create well typed json:{} filenames Max Reitz
2019-06-24 20:06 ` Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 12/14] iotests: Test internal option typing Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 13/14] iotests: qcow2's encrypt.format is now optional Max Reitz
2019-06-24 17:39 ` [Qemu-devel] [PATCH v4 14/14] block: Make use of QAPI defaults Max Reitz
2019-06-24 18:35 ` [Qemu-devel] [PATCH v4 00/14] block: Try to create well-typed json:{} filenames no-reply
2019-06-24 19:04 ` Max Reitz
2019-06-24 19:06 ` Max Reitz
2019-06-24 19:00 ` no-reply
2019-06-24 20:06 ` Max Reitz
2019-08-19 16:49 ` Max Reitz
2019-09-13 11:49 ` Max Reitz
2019-11-06 13:01 ` Max Reitz
2019-11-14 8:54 ` Markus Armbruster
2019-11-21 15:17 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190624173935.25747-3-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=armbru@redhat.com \
--cc=kwolf@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).