summaryrefslogtreecommitdiffstats
path: root/compat/vis.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2015-09-01 21:08:19 +0100
committerNicholas Marriott <nicholas.marriott@gmail.com>2015-09-01 21:08:19 +0100
commit2ebef95994953e49ae84862a65f9ee649a72e200 (patch)
treeee07ead31815e2861f7fc78ed0325e19c8d9e7a8 /compat/vis.c
parent2c6ea705fd05966c00534623a9b8fa3431df98f5 (diff)
Sync up vis.* for stravis().
Diffstat (limited to 'compat/vis.c')
-rw-r--r--compat/vis.c56
1 files changed, 39 insertions, 17 deletions
diff --git a/compat/vis.c b/compat/vis.c
index 97eb5271..82b42be9 100644
--- a/compat/vis.c
+++ b/compat/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.24 2015/07/20 01:52:28 millert Exp $ */
/*-
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -29,14 +29,17 @@
*/
#include <sys/types.h>
-#include <limits.h>
+#include <errno.h>
#include <ctype.h>
+#include <limits.h>
#include <string.h>
+#include <stdlib.h>
#include "tmux.h"
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
-#define isvisible(c) \
+#define isvisible(c,flag) \
+ (((c) == '\\' || (flag & VIS_ALL) == 0) && \
(((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
(((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
(flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \
@@ -45,7 +48,7 @@
((flag & VIS_NL) == 0 && (c) == '\n') || \
((flag & VIS_SAFE) && ((c) == '\b' || \
(c) == '\007' || (c) == '\r' || \
- isgraph((u_char)(c)))))
+ isgraph((u_char)(c))))))
/*
* vis - visually encode characters
@@ -53,10 +56,11 @@
char *
vis(char *dst, int c, int flag, int nextc)
{
- if (isvisible(c)) {
- *dst++ = c;
- if (c == '\\' && (flag & VIS_NOSLASH) == 0)
+ if (isvisible(c, flag)) {
+ if ((c == '"' && (flag & VIS_DQ) != 0) ||
+ (c == '\\' && (flag & VIS_NOSLASH) == 0))
*dst++ = '\\';
+ *dst++ = c;
*dst = '\0';
return (dst);
}
@@ -136,10 +140,10 @@ done:
/*
* strvis, strnvis, strvisx - visually encode characters from src into dst
- *
+ *
* Dst must be 4 times the size of src to account for possible
* expansion. The length of dst, not including the trailing NULL,
- * is returned.
+ * is returned.
*
* Strnvis will write no more than siz-1 bytes (and will NULL terminate).
* The number of bytes needed to fully encode the string is returned.
@@ -168,19 +172,18 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
i = 0;
for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
- if (isvisible(c)) {
- i = 1;
- *dst++ = c;
- if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
+ if (isvisible(c, flag)) {
+ if ((c == '"' && (flag & VIS_DQ) != 0) ||
+ (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
/* need space for the extra '\\' */
- if (dst < end)
- *dst++ = '\\';
- else {
- dst--;
+ if (dst + 1 >= end) {
i = 2;
break;
}
+ *dst++ = '\\';
}
+ i = 1;
+ *dst++ = c;
src++;
} else {
i = vis(tbuf, c, flag, *++src) - tbuf;
@@ -204,6 +207,25 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
}
int
+stravis(char **outp, const char *src, int flag)
+{
+ char *buf;
+ int len, serrno;
+
+ buf = calloc(4, strlen(src) + 1);
+ if (buf == NULL)
+ return -1;
+ len = strvis(buf, src, flag);
+ serrno = errno;
+ *outp = realloc(buf, len + 1);
+ if (*outp == NULL) {
+ *outp = buf;
+ errno = serrno;
+ }
+ return (len);
+}
+
+int
strvisx(char *dst, const char *src, size_t len, int flag)
{
char c;