summaryrefslogtreecommitdiff
path: root/lib/libc/gen/vis.c
diff options
context:
space:
mode:
authorDamien Miller <djm@cvs.openbsd.org>2010-08-21 18:59:16 +0000
committerDamien Miller <djm@cvs.openbsd.org>2010-08-21 18:59:16 +0000
commit20b28aebdc9d72f6cff9b47e4a228afbd3d67834 (patch)
tree1f8d08fc90a6b3ce28d32ab07951169b175ea842 /lib/libc/gen/vis.c
parentdc723b55f0f06bf08b896ffd9eaec3d0472f9d46 (diff)
Two new flags: VIS_ALL - encode all characters, not just invisible ones
and VIS_HEX - use C89 \xff style hexadecimal encoding. Teach unvis(3) how to deal with the hex encoding. feedback and ok millert@ chl@
Diffstat (limited to 'lib/libc/gen/vis.c')
-rw-r--r--lib/libc/gen/vis.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/libc/gen/vis.c b/lib/libc/gen/vis.c
index 8e44ad733c7..51407e6a7da 100644
--- a/lib/libc/gen/vis.c
+++ b/lib/libc/gen/vis.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vis.c,v 1.19 2005/09/01 17:15:49 millert Exp $ */
+/* $OpenBSD: vis.c,v 1.20 2010/08/21 18:59:15 djm Exp $ */
/*-
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -46,13 +46,15 @@
(c) == '\007' || (c) == '\r' || \
isgraph((u_char)(c)))))
+static const char hexdigits[] = "0123456789abcdef";
+
/*
* vis - visually encode characters
*/
char *
vis(char *dst, int c, int flag, int nextc)
{
- if (isvisible(c)) {
+ if (isvisible(c) && (flag & VIS_ALL) == 0) {
*dst++ = c;
if (c == '\\' && (flag & VIS_NOSLASH) == 0)
*dst++ = '\\';
@@ -104,12 +106,19 @@ vis(char *dst, int c, int flag, int nextc)
goto done;
}
}
- if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
+ if (((c & 0177) == ' ') || (flag & (VIS_OCTAL|VIS_HEX)) ||
((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
- *dst++ = '\\';
- *dst++ = ((u_char)c >> 6 & 07) + '0';
- *dst++ = ((u_char)c >> 3 & 07) + '0';
- *dst++ = ((u_char)c & 07) + '0';
+ if ((flag & VIS_HEX) != 0) {
+ *dst++ = '\\';
+ *dst++ = 'x';
+ *dst++ = hexdigits[((u_char)c >> 4 & 0xf)];
+ *dst++ = hexdigits[((u_char)c & 0xf)];
+ } else {
+ *dst++ = '\\';
+ *dst++ = ((u_char)c >> 6 & 07) + '0';
+ *dst++ = ((u_char)c >> 3 & 07) + '0';
+ *dst++ = ((u_char)c & 07) + '0';
+ }
goto done;
}
if ((flag & VIS_NOSLASH) == 0)