Sync up vis.* for stravis().

This commit is contained in:
Nicholas Marriott 2015-09-01 21:08:19 +01:00
parent 2c6ea705fd
commit 2ebef95994
3 changed files with 55 additions and 25 deletions

View File

@ -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 * Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -29,14 +29,17 @@
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <limits.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "tmux.h" #include "tmux.h"
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') #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)) && \ (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
(((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
(flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \ (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \
@ -45,7 +48,7 @@
((flag & VIS_NL) == 0 && (c) == '\n') || \ ((flag & VIS_NL) == 0 && (c) == '\n') || \
((flag & VIS_SAFE) && ((c) == '\b' || \ ((flag & VIS_SAFE) && ((c) == '\b' || \
(c) == '\007' || (c) == '\r' || \ (c) == '\007' || (c) == '\r' || \
isgraph((u_char)(c))))) isgraph((u_char)(c))))))
/* /*
* vis - visually encode characters * vis - visually encode characters
@ -53,10 +56,11 @@
char * char *
vis(char *dst, int c, int flag, int nextc) vis(char *dst, int c, int flag, int nextc)
{ {
if (isvisible(c)) { if (isvisible(c, flag)) {
*dst++ = c; if ((c == '"' && (flag & VIS_DQ) != 0) ||
if (c == '\\' && (flag & VIS_NOSLASH) == 0) (c == '\\' && (flag & VIS_NOSLASH) == 0))
*dst++ = '\\'; *dst++ = '\\';
*dst++ = c;
*dst = '\0'; *dst = '\0';
return (dst); return (dst);
} }
@ -136,10 +140,10 @@ done:
/* /*
* strvis, strnvis, strvisx - visually encode characters from src into dst * strvis, strnvis, strvisx - visually encode characters from src into dst
* *
* Dst must be 4 times the size of src to account for possible * Dst must be 4 times the size of src to account for possible
* expansion. The length of dst, not including the trailing NULL, * 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). * 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. * 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; i = 0;
for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
if (isvisible(c)) { if (isvisible(c, flag)) {
i = 1; if ((c == '"' && (flag & VIS_DQ) != 0) ||
*dst++ = c; (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
/* need space for the extra '\\' */ /* need space for the extra '\\' */
if (dst < end) if (dst + 1 >= end) {
*dst++ = '\\';
else {
dst--;
i = 2; i = 2;
break; break;
} }
*dst++ = '\\';
} }
i = 1;
*dst++ = c;
src++; src++;
} else { } else {
i = vis(tbuf, c, flag, *++src) - tbuf; i = vis(tbuf, c, flag, *++src) - tbuf;
@ -203,6 +206,25 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
return (dst - start); return (dst - start);
} }
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 int
strvisx(char *dst, const char *src, size_t len, int flag) strvisx(char *dst, const char *src, size_t len, int flag)
{ {

View File

@ -1,4 +1,4 @@
/* $OpenBSD: vis.h,v 1.11 2005/08/09 19:38:31 millert Exp $ */ /* $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $ */
/* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ /* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */
/*- /*-
@ -50,6 +50,8 @@
#define VIS_NL 0x10 /* also encode newline */ #define VIS_NL 0x10 /* also encode newline */
#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) #define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
#define VIS_SAFE 0x20 /* only encode "unsafe" characters */ #define VIS_SAFE 0x20 /* only encode "unsafe" characters */
#define VIS_DQ 0x200 /* backslash-escape double quotes */
#define VIS_ALL 0x400 /* encode all characters */
/* /*
* other * other
@ -71,12 +73,18 @@
*/ */
#define UNVIS_END 1 /* no more characters */ #define UNVIS_END 1 /* no more characters */
#include <sys/cdefs.h>
__BEGIN_DECLS
char *vis(char *, int, int, int); char *vis(char *, int, int, int);
int strvis(char *, const char *, int); int strvis(char *, const char *, int);
int stravis(char **, const char *, int);
int strnvis(char *, const char *, size_t, int); int strnvis(char *, const char *, size_t, int);
int strvisx(char *, const char *, size_t, int); int strvisx(char *, const char *, size_t, int);
int strunvis(char *, const char *); int strunvis(char *, const char *);
int unvis(char *, char, int *, int); int unvis(char *, char, int *, int);
ssize_t strnunvis(char *, const char *, size_t); ssize_t strnunvis(char *, const char *, size_t);
__END_DECLS
#endif /* !_VIS_H_ */ #endif /* !_VIS_H_ */

View File

@ -326,22 +326,22 @@ if test "x$found_strtonum" = xyes; then
fi fi
AM_CONDITIONAL(NO_STRTONUM, [test "x$found_strtonum" = xno]) AM_CONDITIONAL(NO_STRTONUM, [test "x$found_strtonum" = xno])
# Look for strnvis, compat/{vis,unvis}.c used if missing. # Look for stravis, compat/{vis,unvis}.c used if missing.
AC_CHECK_FUNC(strnvis, found_strnvis=yes, found_strnvis=no) AC_CHECK_FUNC(stravis, found_stravis=yes, found_stravis=no)
if test "x$found_strnvis" = xyes; then if test "x$found_stravis" = xyes; then
AC_MSG_CHECKING(if strnvis is broken) AC_MSG_CHECKING(if strnvis is broken)
AC_EGREP_HEADER([strnvis\(char \*, const char \*, size_t, int\)], AC_EGREP_HEADER([strnvis\(char \*, const char \*, size_t, int\)],
vis.h, vis.h,
AC_MSG_RESULT(no), AC_MSG_RESULT(no),
[found_strnvis=no]) [found_stravis=no])
if test "x$found_strnvis" = xno; then if test "x$found_stravis" = xno; then
AC_MSG_RESULT(yes) AC_MSG_RESULT(yes)
fi fi
fi fi
if test "x$found_strnvis" = xyes; then if test "x$found_stravis" = xyes; then
AC_DEFINE(HAVE_VIS) AC_DEFINE(HAVE_VIS)
fi fi
AM_CONDITIONAL(NO_VIS, [test "x$found_strnvis" = xno]) AM_CONDITIONAL(NO_VIS, [test "x$found_stravis" = xno])
# Look for cfmakeraw, compat/cfmakeraw.c used if missing. # Look for cfmakeraw, compat/cfmakeraw.c used if missing.
AC_CHECK_FUNC(cfmakeraw, found_cfmakeraw=yes, found_cfmakeraw=no) AC_CHECK_FUNC(cfmakeraw, found_cfmakeraw=yes, found_cfmakeraw=no)