Loading...
gen/FreeBSD/vis.c Libc-1725.40.4 Libc-1353.100.2
--- Libc/Libc-1725.40.4/gen/FreeBSD/vis.c
+++ Libc/Libc-1353.100.2/gen/FreeBSD/vis.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: vis.c,v 1.74 2017/11/27 16:37:21 christos Exp $	*/
+/*	$NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $	*/
 
 /*-
  * Copyright (c) 1989, 1993
@@ -57,7 +57,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: vis.c,v 1.74 2017/11/27 16:37:21 christos Exp $");
+__RCSID("$NetBSD: vis.c,v 1.62 2014/09/08 17:35:01 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 #ifdef __FBSDID
 __FBSDID("$FreeBSD$");
@@ -92,52 +92,14 @@
 
 #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 <locale.h>
-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')
-#ifdef __APPLE__
-/*
- * rdar://problem/108684957 - UTF-8, for instance, may define some whitespace
- * characters above the standard ASCII range, e.g., U+202F.  Based on the
- * description of VIS_CSTYLE, we should also be passing these through with their
- * default representation rather than encoding their UTF-16 equivalents.
- */
-#define iswwhite(flags, c)	(c == L' ' || c == L'\t' || c == L'\n' || \
-    (((flags) & VIS_NOLOCALE) == 0 && c > 0x7f && iswspace(c)))
-#else
 #define iswwhite(c)	(c == L' ' || c == L'\t' || c == L'\n')
-#endif
 #define iswsafe(c)	(c == L'\b' || c == BELL || c == L'\r')
 #define xtoa(c)		L"0123456789abcdef"[c]
 #define XTOA(c)		L"0123456789ABCDEF"[c]
 
-#define MAXEXTRAS	30
-
-static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
-static const wchar_t char_glob[] = L"*?[#";
+#define MAXEXTRAS	10
 
 _Static_assert(MB_LEN_MAX <= sizeof(uint64_t), "MB_LEN_MAX is less than 64-bits");
 
@@ -224,23 +186,8 @@
 				*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 (ISGRAPH(flags, c) && !iswoctal(c)) {
+			if (iswgraph(c)) {
 				*dst++ = L'\\';
 				*dst++ = c;
 				return dst;
@@ -292,7 +239,7 @@
 	uint64_t bmsk, wmsk;
 
 	iswextra = wcschr(extra, c) != NULL;
-	if (!iswextra && (ISGRAPH(flags, c) || iswwhite(flags, c) ||
+	if (!iswextra && (iswgraph(c) || iswwhite(c) ||
 	    ((flags & VIS_SAFE) && iswsafe(c)))) {
 		*dst++ = c;
 		return dst;
@@ -336,34 +283,29 @@
 {
 	wchar_t *dst, *d;
 	size_t len;
-	const wchar_t *s;
-	mbstate_t mbstate;
-
-	bzero(&mbstate, sizeof(mbstate));
+
 	len = strlen(src);
 	if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
 		return NULL;
 
-	if ((flags & VIS_NOLOCALE) || mbsrtowcs(dst, &src, len, &mbstate) == (size_t)-1) {
+	if (mbstowcs(dst, src, len) == (size_t)-1) {
 		size_t i;
 		for (i = 0; i < len; i++)
-			dst[i] = (wchar_t)(u_char)src[i];
+			dst[i] = (wint_t)(u_char)src[i];
 		d = dst + len;
 	} else
 		d = dst + wcslen(dst);
 
-	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_GLOB) {
+		*d++ = L'*';
+		*d++ = L'?';
+		*d++ = L'[';
+		*d++ = L'#';
+	}
 
 	if (flags & VIS_SP) *d++ = L' ';
 	if (flags & VIS_TAB) *d++ = L'\t';
 	if (flags & VIS_NL) *d++ = L'\n';
-	if (flags & VIS_DQ) *d++ = L'"';
 	if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
 	*d = L'\0';
 
@@ -376,32 +318,20 @@
  *	All user-visible functions call this one.
  */
 static int
-istrsenvisx(char **mbdstp, size_t *dlen, const char *mbsrc, size_t mblength,
+istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
     int flags, const char *mbextra, int *cerr_ptr)
 {
-	char mbbuf[MB_CUR_MAX];
 	wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
 	size_t len, olen;
 	uint64_t bmsk, wmsk;
 	wint_t c;
 	visfun_t f;
-	int clen = 0, cerr, error = -1, i, shft;
-	char *mbdst, *mbwrite, *mdst;
-	ssize_t mbslength;
-	size_t maxolen;
-	mbstate_t mbstate;
-
-	_DIAGASSERT(mbdstp != NULL);
+	int clen = 0, cerr = 0, error = -1, i, shft;
+	ssize_t mbslength, maxolen;
+
+	_DIAGASSERT(mbdst != NULL);
 	_DIAGASSERT(mbsrc != NULL || mblength == 0);
 	_DIAGASSERT(mbextra != NULL);
-
-	mbslength = (ssize_t)mblength;
-	/*
-	 * When inputing a single character, must also read in the
-	 * next character for nextc, the look-ahead character.
-	 */
-	if (mbslength == 1)
-		mbslength++;
 
 	/*
 	 * Input (mbsrc) is a char string considered to be multibyte
@@ -418,28 +348,16 @@
 
 	/* Allocate space for the wide char strings */
 	psrc = pdst = extra = NULL;
-	mdst = NULL;
-	if ((psrc = calloc(mbslength + 1, sizeof(*psrc))) == NULL)
+	if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
 		return -1;
-	if ((pdst = calloc((16 * mbslength) + 1, sizeof(*pdst))) == NULL)
+	if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
 		goto out;
-	if (*mbdstp == NULL) {
-		if ((mdst = calloc((16 * mbslength) + 1, sizeof(*mdst))) == NULL)
-			goto out;
-		*mbdstp = mdst;
-	}
-
-	mbdst = *mbdstp;
 	dst = pdst;
 	src = psrc;
 
-	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;
-	}
+	/* Use caller's multibyte conversion error flag. */
+	if (cerr_ptr)
+		cerr = *cerr_ptr;
 
 	/*
 	 * Input loop.
@@ -447,25 +365,29 @@
 	 * stop at NULs because we may be processing a block of data
 	 * that includes NULs.
 	 */
-	bzero(&mbstate, sizeof(mbstate));
+	mbslength = (ssize_t)mblength;
+	/*
+	 * When inputing a single character, must also read in the
+	 * next character for nextc, the look-ahead character.
+	 */
+	if (mbslength == 1)
+		mbslength++;
 	while (mbslength > 0) {
 		/* Convert one multibyte character to wchar_t. */
 		if (!cerr)
-			clen = mbrtowc(src, mbsrc, MIN(mbslength, MB_LEN_MAX),
-			    &mbstate);
+			clen = mbtowc(src, mbsrc, MB_LEN_MAX);
 		if (cerr || clen < 0) {
 			/* Conversion error, process as a byte instead. */
 			*src = (wint_t)(u_char)*mbsrc;
 			clen = 1;
 			cerr = 1;
 		}
-		if (clen == 0) {
+		if (clen == 0)
 			/*
 			 * NUL in input gives 0 return value. process
 			 * as single NUL byte and keep going.
 			 */
 			clen = 1;
-		}
 		/* Advance buffer character pointer. */
 		src++;
 		/* Advance input pointer by number of bytes read. */
@@ -475,7 +397,6 @@
 	}
 	len = src - psrc;
 	src = psrc;
-
 	/*
 	 * In the single character input case, we will have actually
 	 * processed two characters, c and nextc.  Reset len back to
@@ -491,7 +412,7 @@
 			errno = ENOSPC;
 			goto out;
 		}
-		*mbdst = '\0';	/* can't create extra, return "" */
+		*mbdst = '\0';		/* can't create extra, return "" */
 		error = 0;
 		goto out;
 	}
@@ -525,35 +446,9 @@
 	len = wcslen(start);
 	maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
 	olen = 0;
-	bzero(&mbstate, sizeof(mbstate));
 	for (dst = start; len > 0; len--) {
-		if (!cerr) {
-			/*
-			 * If we have at least MB_CUR_MAX bytes in the buffer,
-			 * we'll just do the conversion in-place into mbdst.  We
-			 * need to be a little more conservative when we get to
-			 * the end of the buffer, as we may not have MB_CUR_MAX
-			 * bytes but we may not need it.
-			 */
-			if (maxolen - olen > MB_CUR_MAX)
-				mbwrite = mbdst;
-			else
-				mbwrite = mbbuf;
-			clen = wcrtomb(mbwrite, *dst, &mbstate);
-			if (clen > 0 && mbwrite != mbdst) {
-				/*
-				 * Don't break past our output limit, noting
-				 * that maxolen includes the nul terminator so
-				 * we can't write past maxolen - 1 here.
-				 */
-				if (olen + clen >= maxolen) {
-					errno = ENOSPC;
-					goto out;
-				}
-
-				memcpy(mbdst, mbwrite, clen);
-			}
-		}
+		if (!cerr)
+			clen = wctomb(mbdst, *dst);
 		if (cerr || clen < 0) {
 			/*
 			 * Conversion error, process as a byte(s) instead.
@@ -568,27 +463,16 @@
 				shft = i * NBBY;
 				bmsk = (uint64_t)0xffLL << shft;
 				wmsk |= bmsk;
-				if ((*dst & wmsk) || i == 0) {
-					if (olen + clen + 1 >= maxolen) {
-						errno = ENOSPC;
-						goto out;
-					}
-
+				if ((*dst & wmsk) || i == 0)
 					mbdst[clen++] = (char)(
 					    (uint64_t)(*dst & bmsk) >>
 					    shft);
-				}
 			}
 			cerr = 1;
 		}
-
-		/*
-		 * We'll be dereferencing mbdst[clen] after this to write the
-		 * nul terminator; the above paths should have checked for a
-		 * possible overflow already.
-		 */
-		assert(olen + clen < maxolen);
-
+		/* If this character would exceed our output limit, stop. */
+		if (olen + clen > (size_t)maxolen)
+			break;
 		/* Advance output pointer by number of bytes written. */
 		mbdst += clen;
 		/* Advance buffer character pointer. */
@@ -600,11 +484,9 @@
 	/* Terminate the output string. */
 	*mbdst = '\0';
 
-	if (flags & VIS_NOLOCALE) {
-		/* Pass conversion error flag out. */
-		if (cerr_ptr)
-			*cerr_ptr = cerr;
-	}
+	/* Pass conversion error flag out. */
+	if (cerr_ptr)
+		*cerr_ptr = cerr;
 
 	free(extra);
 	free(pdst);
@@ -615,15 +497,14 @@
 	free(extra);
 	free(pdst);
 	free(psrc);
-	free(mdst);
 	return error;
 }
 
 static int
-istrsenvisxl(char **mbdstp, size_t *dlen, const char *mbsrc,
+istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
     int flags, const char *mbextra, int *cerr_ptr)
 {
-	return istrsenvisx(mbdstp, dlen, mbsrc,
+	return istrsenvisx(mbdst, dlen, mbsrc,
 	    mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
 }
 
@@ -646,7 +527,7 @@
 	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;
@@ -661,7 +542,7 @@
 	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;
@@ -670,33 +551,33 @@
 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
 
@@ -713,7 +594,7 @@
 	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;
@@ -728,7 +609,7 @@
 	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;
@@ -745,20 +626,13 @@
 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);
-}
-
-int
-stravis(char **mbdstp, const char *mbsrc, int flags)
-{
-	*mbdstp = NULL;
-	return istrsenvisxl(mbdstp, NULL, mbsrc, flags, "", NULL);
+	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
 }
 
 /*
@@ -775,19 +649,19 @@
 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