commit-hurd
[Top][All Lists]
Advanced

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

[hurd] 85/98: utils/rpctrace: escape non-printable characters in strings


From: Samuel Thibault
Subject: [hurd] 85/98: utils/rpctrace: escape non-printable characters in strings
Date: Tue, 14 Jan 2014 02:00:04 +0000

This is an automated email from the git hooks/post-receive script.

sthibault pushed a commit to branch upstream
in repository hurd.

commit 84932431cf4fbd494b4597105faed26ed2ac4efe
Author: Justus Winter <address@hidden>
Date:   Fri Dec 13 12:57:55 2013 +0100

    utils/rpctrace: escape non-printable characters in strings
    
    * utils/rpctrace.c (escape_sequences): New char array mapping
    characters to their escape sequence.
    (print_data): Escape non-printable characters when printing strings.
---
 utils/rpctrace.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 2 deletions(-)

diff --git a/utils/rpctrace.c b/utils/rpctrace.c
index b39b2e3..0f68f44 100644
--- a/utils/rpctrace.c
+++ b/utils/rpctrace.c
@@ -1528,6 +1528,20 @@ print_reply_header (struct send_once_info *info, 
mig_reply_header_t *reply,
     }
 }
 
+static char escape_sequences[0x100] =
+  {
+    ['\0'] = '0',
+    ['\a'] = 'a',
+    ['\b'] = 'b',
+    ['\f'] = 'f',
+    ['\n'] = 'n',
+    ['\r'] = 'r',
+    ['\t'] = 't',
+    ['\v'] = 'v',
+    ['\\'] = '\\',
+    ['\''] = '\'',
+    ['"'] = '"',
+  };
 
 static void
 print_data (mach_msg_type_name_t type,
@@ -1555,8 +1569,38 @@ print_data (mach_msg_type_name_t type,
     case MACH_MSG_TYPE_CHAR:
       if (nelt > strsize)
        nelt = strsize;
-      fprintf (ostream, "\"%.*s\"",
-              (int) (nelt * eltsize), (const char *) data);
+      fprintf (ostream, "\"");
+      /* Scan data for non-printable characters.  p always points to
+        the first character that has not yet been printed.  */
+      const char *p, *q;
+      p = q = (const char *) data;
+      while (*q && q - (const char *) data < (int) (nelt * eltsize))
+       {
+         if (isgraph (*q) || *q == ' ')
+           {
+             q += 1;
+             continue;
+           }
+
+         /* We encountered a non-printable character.  Print anything
+            that has not been printed so far.  */
+         if (p < q)
+           fprintf (ostream, "%.*s", q - p, p);
+
+         char c = escape_sequences[*((const unsigned char *) q)];
+         if (c)
+           fprintf (ostream, "\\%c", c);
+         else
+           fprintf (ostream, "\\x%02x", *((const unsigned char *) q));
+
+         q += 1;
+         p = q;
+       }
+
+      /* Print anything that has not been printed so far.  */
+      if (p < q)
+       fprintf (ostream, "%.*s", q - p, p);
+      fprintf (ostream, "\"");
       return;
 
 #if 0

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-hurd/hurd.git



reply via email to

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