From 46f0e7ccdfb34f49b0cc1de7d00c1429a00f1ff3 Mon Sep 17 00:00:00 2001 From: Brooks Davis Date: Wed, 8 Jun 2016 17:35:13 +0000 Subject: [PATCH] Vendor import of NetBSD's (un)vis(3) at 2016-06-08 --- unvis.c | 10 +++- vis.3 | 49 +++++++++++++++---- vis.c | 145 +++++++++++++++++++++++++++++++++++++++++--------------- vis.h | 6 ++- 4 files changed, 159 insertions(+), 51 deletions(-) diff --git a/unvis.c b/unvis.c index cf0273dfc8c4..30fad8154d75 100644 --- a/unvis.c +++ b/unvis.c @@ -1,4 +1,4 @@ -/* $NetBSD: unvis.c,v 1.41 2012/12/15 04:29:53 matt Exp $ */ +/* $NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: unvis.c,v 1.41 2012/12/15 04:29:53 matt Exp $"); +__RCSID("$NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -313,6 +313,12 @@ unvis(char *cp, int c, int *astate, int flag) */ *astate = SS(0, S_GROUND); return UNVIS_NOCHAR; + default: + if (isgraph(c)) { + *cp = c; + *astate = SS(0, S_GROUND); + return UNVIS_VALID; + } } goto bad; diff --git a/vis.3 b/vis.3 index 166316d1f9ae..bde9c00f08e4 100644 --- a/vis.3 +++ b/vis.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: vis.3,v 1.39 2013/02/20 20:05:26 christos Exp $ +.\" $NetBSD: vis.3,v 1.45 2016/06/08 15:00:04 wiz Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,13 +29,14 @@ .\" .\" @(#)vis.3 8.1 (Berkeley) 6/9/93 .\" -.Dd February 19, 2013 +.Dd January 14, 2015 .Dt VIS 3 .Os .Sh NAME .Nm vis , .Nm nvis , .Nm strvis , +.Nm stravis , .Nm strnvis , .Nm strvisx , .Nm strnvisx , @@ -59,6 +60,8 @@ .Ft int .Fn strvis "char *dst" "const char *src" "int flag" .Ft int +.Fn stravis "char **dst" "const char *src" "int flag" +.Ft int .Fn strnvis "char *dst" "size_t dlen" "const char *src" "int flag" .Ft int .Fn strvisx "char *dst" "const char *src" "size_t len" "int flag" @@ -111,6 +114,7 @@ encoding format (explained below). .Pp The .Fn strvis , +.Fn stravis , .Fn strnvis , .Fn strvisx , and @@ -157,6 +161,9 @@ forms return the number of characters in (not including the trailing .Dv NUL ) . The +.Fn stravis +function allocates space dynamically to hold the string. +The .Dq Nm n versions of the functions also take an additional argument .Fa dlen @@ -239,11 +246,31 @@ alter this: Also encode the magic characters .Ql ( * , .Ql \&? , -.Ql \&[ +.Ql \&[ , and .Ql # ) recognized by .Xr glob 3 . +.It Dv VIS_SHELL +Also encode the meta characters used by shells (in addition to the glob +characters): +.Ql ( ' , +.Ql ` , +.Ql \&" , +.Ql \&; , +.Ql & , +.Ql < , +.Ql > , +.Ql \&( , +.Ql \&) , +.Ql \&| , +.Ql \&] , +.Ql \e , +.Ql $ , +.Ql \&! , +.Ql \&^ , +and +.Ql ~ ) . .It Dv VIS_SP Also encode space. .It Dv VIS_TAB @@ -252,11 +279,10 @@ Also encode tab. Also encode newline. .It Dv VIS_WHITE Synonym for -.Dv VIS_SP -\&| -.Dv VIS_TAB -\&| -.Dv VIS_NL . +.Dv VIS_SP | VIS_TAB | VIS_NL . +.It Dv VIS_META +Synonym for +.Dv VIS_WHITE | VIS_GLOB | VIS_SHELL . .It Dv VIS_SAFE Only encode .Dq unsafe @@ -417,6 +443,11 @@ The encoding conversion is influenced by the setting of the environment variable which defines the set of characters that can be copied without encoding. .Pp +If +.Dv VIS_NOLOCALE +is set, processing is done assuming the C locale and overriding +any other environment settings. +.Pp When 8-bit data is present in the input, .Ev LC_CTYPE must be set to the correct locale or to the C locale. @@ -510,7 +541,7 @@ appeared in .Nx 6.0 and .Fx 9.2 . -Myltibyte character support was added in +Multibyte character support was added in .Nx 7.0 and .Fx 9.2 . diff --git a/vis.c b/vis.c index 4863bbea8596..b4a5f3291daa 100644 --- a/vis.c +++ b/vis.c @@ -1,4 +1,4 @@ -/* $NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $ */ +/* $NetBSD: vis.c,v 1.71 2016/01/14 20:41:23 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -57,7 +57,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $"); +__RCSID("$NetBSD: vis.c,v 1.71 2016/01/14 20:41:23 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #ifdef __FBSDID __FBSDID("$FreeBSD$"); @@ -97,6 +97,30 @@ static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *); #undef BELL #define BELL L'\a' + +#if defined(LC_C_LOCALE) +#define iscgraph(c) isgraph_l(c, LC_C_LOCALE) +#else +/* Keep it simple for now, no locale stuff */ +#define iscgraph(c) isgraph(c) +#ifdef notyet +#include +static int +iscgraph(int c) { + int rv; + char *ol; + + ol = setlocale(LC_CTYPE, "C"); + rv = isgraph(c); + if (ol) + setlocale(LC_CTYPE, ol); + return rv; +} +#endif +#endif + +#define ISGRAPH(flags, c) \ + (((flags) & VIS_NOLOCALE) ? iscgraph(c) : iswgraph(c)) #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7') #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n') @@ -104,7 +128,10 @@ static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *); #define xtoa(c) L"0123456789abcdef"[c] #define XTOA(c) L"0123456789ABCDEF"[c] -#define MAXEXTRAS 10 +#define MAXEXTRAS 30 + +static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~"; +static const wchar_t char_glob[] = L"*?[#"; #if !HAVE_NBTOOL_CONFIG_H #ifndef __NetBSD__ @@ -213,8 +240,23 @@ do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra) *dst++ = L'0'; } return dst; + /* We cannot encode these characters in VIS_CSTYLE + * because they special meaning */ + case L'n': + case L'r': + case L'b': + case L'a': + case L'v': + case L't': + case L'f': + case L's': + case L'0': + case L'M': + case L'^': + case L'$': /* vis(1) -l */ + break; default: - if (iswgraph(c)) { + if (ISGRAPH(flags, c) && !iswoctal(c)) { *dst++ = L'\\'; *dst++ = c; return dst; @@ -266,7 +308,7 @@ do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra) uint64_t bmsk, wmsk; iswextra = wcschr(extra, c) != NULL; - if (!iswextra && (iswgraph(c) || iswwhite(c) || + if (!iswextra && (ISGRAPH(flags, c) || iswwhite(c) || ((flags & VIS_SAFE) && iswsafe(c)))) { *dst++ = c; return dst; @@ -310,25 +352,27 @@ makeextralist(int flags, const char *src) { wchar_t *dst, *d; size_t len; + const wchar_t *s; len = strlen(src); if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL) return NULL; - if (mbstowcs(dst, src, len) == (size_t)-1) { + if ((flags & VIS_NOLOCALE) || mbstowcs(dst, src, len) == (size_t)-1) { size_t i; for (i = 0; i < len; i++) - dst[i] = (wint_t)(u_char)src[i]; + dst[i] = (wchar_t)(u_char)src[i]; d = dst + len; } else d = dst + wcslen(dst); - if (flags & VIS_GLOB) { - *d++ = L'*'; - *d++ = L'?'; - *d++ = L'['; - *d++ = L'#'; - } + if (flags & VIS_GLOB) + for (s = char_glob; *s; *d++ = *s++) + continue; + + if (flags & VIS_SHELL) + for (s = char_shell; *s; *d++ = *s++) + continue; if (flags & VIS_SP) *d++ = L' '; if (flags & VIS_TAB) *d++ = L'\t'; @@ -345,7 +389,7 @@ makeextralist(int flags, const char *src) * All user-visible functions call this one. */ static int -istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength, +istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength, int flags, const char *mbextra, int *cerr_ptr) { wchar_t *dst, *src, *pdst, *psrc, *start, *extra; @@ -353,10 +397,11 @@ istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength, uint64_t bmsk, wmsk; wint_t c; visfun_t f; - int clen = 0, cerr = 0, error = -1, i, shft; + int clen = 0, cerr, error = -1, i, shft; + char *mbdst, *mdst; ssize_t mbslength, maxolen; - _DIAGASSERT(mbdst != NULL); + _DIAGASSERT(mbdstp != NULL); _DIAGASSERT(mbsrc != NULL || mblength == 0); _DIAGASSERT(mbextra != NULL); @@ -375,16 +420,28 @@ istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength, /* Allocate space for the wide char strings */ psrc = pdst = extra = NULL; + mdst = NULL; if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL) return -1; if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL) goto out; + if (*mbdstp == NULL) { + if ((mdst = calloc((4 * mblength) + 1, sizeof(*mdst))) == NULL) + goto out; + *mbdstp = mdst; + } + + mbdst = *mbdstp; dst = pdst; src = psrc; - /* Use caller's multibyte conversion error flag. */ - if (cerr_ptr) - cerr = *cerr_ptr; + if (flags & VIS_NOLOCALE) { + /* Do one byte at a time conversion */ + cerr = 1; + } else { + /* Use caller's multibyte conversion error flag. */ + cerr = cerr_ptr ? *cerr_ptr : 0; + } /* * Input loop. @@ -439,7 +496,7 @@ istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength, errno = ENOSPC; goto out; } - *mbdst = '\0'; /* can't create extra, return "" */ + *mbdst = '\0'; /* can't create extra, return "" */ error = 0; goto out; } @@ -511,9 +568,11 @@ istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength, /* Terminate the output string. */ *mbdst = '\0'; - /* Pass conversion error flag out. */ - if (cerr_ptr) - *cerr_ptr = cerr; + if (flags & VIS_NOLOCALE) { + /* Pass conversion error flag out. */ + if (cerr_ptr) + *cerr_ptr = cerr; + } free(extra); free(pdst); @@ -524,14 +583,15 @@ istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength, free(extra); free(pdst); free(psrc); + free(mdst); return error; } static int -istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc, +istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc, int flags, const char *mbextra, int *cerr_ptr) { - return istrsenvisx(mbdst, dlen, mbsrc, + return istrsenvisx(mbdstp, dlen, mbsrc, mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr); } @@ -554,7 +614,7 @@ svis(char *mbdst, int c, int flags, int nextc, const char *mbextra) cc[0] = c; cc[1] = nextc; - ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL); + ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, mbextra, NULL); if (ret < 0) return NULL; return mbdst + ret; @@ -569,7 +629,7 @@ snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra cc[0] = c; cc[1] = nextc; - ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL); + ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, mbextra, NULL); if (ret < 0) return NULL; return mbdst + ret; @@ -578,33 +638,33 @@ snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra int strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra) { - return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL); + return istrsenvisxl(&mbdst, NULL, mbsrc, flags, mbextra, NULL); } int strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra) { - return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL); + return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, mbextra, NULL); } int strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra) { - return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL); + return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, mbextra, NULL); } int strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, const char *mbextra) { - return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL); + return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, NULL); } int strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, const char *mbextra, int *cerr_ptr) { - return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr); + return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr); } #endif @@ -621,7 +681,7 @@ vis(char *mbdst, int c, int flags, int nextc) cc[0] = c; cc[1] = nextc; - ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL); + ret = istrsenvisx(&mbdst, NULL, cc, 1, flags, "", NULL); if (ret < 0) return NULL; return mbdst + ret; @@ -636,7 +696,7 @@ nvis(char *mbdst, size_t dlen, int c, int flags, int nextc) cc[0] = c; cc[1] = nextc; - ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL); + ret = istrsenvisx(&mbdst, &dlen, cc, 1, flags, "", NULL); if (ret < 0) return NULL; return mbdst + ret; @@ -653,13 +713,20 @@ nvis(char *mbdst, size_t dlen, int c, int flags, int nextc) int strvis(char *mbdst, const char *mbsrc, int flags) { - return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL); + return istrsenvisxl(&mbdst, NULL, mbsrc, flags, "", NULL); } int strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags) { - return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL); + return istrsenvisxl(&mbdst, &dlen, mbsrc, flags, "", NULL); +} + +int +stravis(char **mbdstp, const char *mbsrc, int flags) +{ + *mbdstp = NULL; + return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL); } /* @@ -676,19 +743,19 @@ strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags) int strvisx(char *mbdst, const char *mbsrc, size_t len, int flags) { - return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL); + return istrsenvisx(&mbdst, NULL, mbsrc, len, flags, "", NULL); } int strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags) { - return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL); + return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", NULL); } int strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags, int *cerr_ptr) { - return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr); + return istrsenvisx(&mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr); } #endif diff --git a/vis.h b/vis.h index b4c6b5e1c6de..501ad182eeb3 100644 --- a/vis.h +++ b/vis.h @@ -1,4 +1,4 @@ -/* $NetBSD: vis.h,v 1.21 2013/02/20 17:01:15 christos Exp $ */ +/* $NetBSD: vis.h,v 1.24 2016/01/14 20:42:14 christos Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -63,6 +63,9 @@ #define VIS_NOESCAPE 0x0400 /* don't decode `\' */ #define _VIS_END 0x0800 /* for unvis */ #define VIS_GLOB 0x1000 /* encode glob(3) magic characters */ +#define VIS_SHELL 0x2000 /* encode shell special characters [not glob] */ +#define VIS_META (VIS_WHITE | VIS_GLOB | VIS_SHELL) +#define VIS_NOLOCALE 0x4000 /* encode using the C locale */ /* * unvis return codes @@ -88,6 +91,7 @@ char *svis(char *, int, int, int, const char *); char *snvis(char *, size_t, int, int, int, const char *); int strvis(char *, const char *, int); +int stravis(char **, const char *, int); int strnvis(char *, size_t, const char *, int); int strsvis(char *, const char *, int, const char *);