qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH for-1.4 v2 13/13] hmp: make memchar-read escape ASCI


From: Markus Armbruster
Subject: [Qemu-devel] [PATCH for-1.4 v2 13/13] hmp: make memchar-read escape ASCII control chars except \n and \t
Date: Wed, 6 Feb 2013 21:27:26 +0100

Signed-off-by: Markus Armbruster <address@hidden>
---
 hmp-commands.hx |  2 ++
 hmp.c           | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/hmp-commands.hx b/hmp-commands.hx
index 66ec716..4c100d7 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -869,6 +869,8 @@ STEXI
 @findex ringbuf_read
 Read and print up to @var{size} bytes from ring buffer character
 device @var{device}.
+Certain non-printable characters are printed \uXXXX, where XXXX is the
+character code in hexadecimal.  Character \ is printed \\.
 Bug: can screw up when the buffer contains invalid UTF-8 sequences,
 NUL characters, after the ring buffer lost data, and when reading
 stops because the size limit is reached.
diff --git a/hmp.c b/hmp.c
index cbd5727..420d48b 100644
--- a/hmp.c
+++ b/hmp.c
@@ -679,6 +679,7 @@ void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
     const char *chardev = qdict_get_str(qdict, "device");
     char *data;
     Error *errp = NULL;
+    int i;
 
     data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
     if (errp) {
@@ -687,7 +688,19 @@ void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
         return;
     }
 
-    monitor_printf(mon, "%s\n", data);
+    for (i = 0; data[i]; i++) {
+        unsigned char ch = data[i];
+
+        if (ch == '\\') {
+            monitor_printf(mon, "\\\\");
+        } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
+            monitor_printf(mon, "\\u%04X", ch);
+        } else {
+            monitor_printf(mon, "%c", ch);
+        }
+
+    }
+    monitor_printf(mon, "\n");
     g_free(data);
 }
 
-- 
1.7.11.7




reply via email to

[Prev in Thread] Current Thread [Next in Thread]