xref: /openssh-portable/utf8.c (revision 31909696)
1*31909696Sdjm@openbsd.org /* $OpenBSD: utf8.c,v 1.11 2020/05/01 06:28:52 djm Exp $ */
20e059cdfSschwarze@openbsd.org /*
30e059cdfSschwarze@openbsd.org  * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
40e059cdfSschwarze@openbsd.org  *
50e059cdfSschwarze@openbsd.org  * Permission to use, copy, modify, and distribute this software for any
60e059cdfSschwarze@openbsd.org  * purpose with or without fee is hereby granted, provided that the above
70e059cdfSschwarze@openbsd.org  * copyright notice and this permission notice appear in all copies.
80e059cdfSschwarze@openbsd.org  *
90e059cdfSschwarze@openbsd.org  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
100e059cdfSschwarze@openbsd.org  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
110e059cdfSschwarze@openbsd.org  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
120e059cdfSschwarze@openbsd.org  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
130e059cdfSschwarze@openbsd.org  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
140e059cdfSschwarze@openbsd.org  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
150e059cdfSschwarze@openbsd.org  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160e059cdfSschwarze@openbsd.org  */
170e059cdfSschwarze@openbsd.org 
180e059cdfSschwarze@openbsd.org /*
190e059cdfSschwarze@openbsd.org  * Utility functions for multibyte-character handling,
200e059cdfSschwarze@openbsd.org  * in particular to sanitize untrusted strings for terminal output.
210e059cdfSschwarze@openbsd.org  */
220e059cdfSschwarze@openbsd.org 
23df820722SDarren Tucker #include "includes.h"
24df820722SDarren Tucker 
250e059cdfSschwarze@openbsd.org #include <sys/types.h>
26f3f2cc83SDarren Tucker #ifdef HAVE_LANGINFO_H
270e059cdfSschwarze@openbsd.org # include <langinfo.h>
28f3f2cc83SDarren Tucker #endif
290e059cdfSschwarze@openbsd.org #include <limits.h>
30dda78a03SDamien Miller #include <locale.h>
310e059cdfSschwarze@openbsd.org #include <stdarg.h>
320e059cdfSschwarze@openbsd.org #include <stdio.h>
330e059cdfSschwarze@openbsd.org #include <stdlib.h>
340e059cdfSschwarze@openbsd.org #include <string.h>
35df820722SDarren Tucker #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
360e059cdfSschwarze@openbsd.org # include <vis.h>
37df820722SDarren Tucker #endif
38f3f2cc83SDarren Tucker #ifdef HAVE_WCHAR_H
390e059cdfSschwarze@openbsd.org # include <wchar.h>
40f3f2cc83SDarren Tucker #endif
410e059cdfSschwarze@openbsd.org 
420e059cdfSschwarze@openbsd.org #include "utf8.h"
430e059cdfSschwarze@openbsd.org 
440e059cdfSschwarze@openbsd.org static int	 dangerous_locale(void);
45cd9e1eabSschwarze@openbsd.org static int	 grow_dst(char **, size_t *, size_t, char **, size_t);
460e059cdfSschwarze@openbsd.org 
470e059cdfSschwarze@openbsd.org 
480e059cdfSschwarze@openbsd.org /*
490e059cdfSschwarze@openbsd.org  * For US-ASCII and UTF-8 encodings, we can safely recover from
500e059cdfSschwarze@openbsd.org  * encoding errors and from non-printable characters.  For any
510e059cdfSschwarze@openbsd.org  * other encodings, err to the side of caution and abort parsing:
520e059cdfSschwarze@openbsd.org  * For state-dependent encodings, recovery is impossible.
530e059cdfSschwarze@openbsd.org  * For arbitrary encodings, replacement of non-printable
540e059cdfSschwarze@openbsd.org  * characters would be non-trivial and too fragile.
55b8ae02a2Sschwarze@openbsd.org  * The comments indicate what nl_langinfo(CODESET)
56b8ae02a2Sschwarze@openbsd.org  * returns for US-ASCII on various operating systems.
570e059cdfSschwarze@openbsd.org  */
580e059cdfSschwarze@openbsd.org 
590e059cdfSschwarze@openbsd.org static int
dangerous_locale(void)600e059cdfSschwarze@openbsd.org dangerous_locale(void) {
610e059cdfSschwarze@openbsd.org 	char	*loc;
620e059cdfSschwarze@openbsd.org 
630e059cdfSschwarze@openbsd.org 	loc = nl_langinfo(CODESET);
64b8ae02a2Sschwarze@openbsd.org 	return strcmp(loc, "UTF-8") != 0 &&
65b8ae02a2Sschwarze@openbsd.org 	    strcmp(loc, "US-ASCII") != 0 &&		/* OpenBSD */
66b8ae02a2Sschwarze@openbsd.org 	    strcmp(loc, "ANSI_X3.4-1968") != 0 &&	/* Linux */
67b8ae02a2Sschwarze@openbsd.org 	    strcmp(loc, "ISO8859-1") != 0 &&		/* AIX */
68b8ae02a2Sschwarze@openbsd.org 	    strcmp(loc, "646") != 0 &&			/* Solaris, NetBSD */
69b8ae02a2Sschwarze@openbsd.org 	    strcmp(loc, "") != 0;			/* Solaris 6 */
700e059cdfSschwarze@openbsd.org }
710e059cdfSschwarze@openbsd.org 
72cd9e1eabSschwarze@openbsd.org static int
grow_dst(char ** dst,size_t * sz,size_t maxsz,char ** dp,size_t need)73cd9e1eabSschwarze@openbsd.org grow_dst(char **dst, size_t *sz, size_t maxsz, char **dp, size_t need)
74cd9e1eabSschwarze@openbsd.org {
75cd9e1eabSschwarze@openbsd.org 	char	*tp;
76cd9e1eabSschwarze@openbsd.org 	size_t	 tsz;
77cd9e1eabSschwarze@openbsd.org 
78cd9e1eabSschwarze@openbsd.org 	if (*dp + need < *dst + *sz)
79cd9e1eabSschwarze@openbsd.org 		return 0;
80cd9e1eabSschwarze@openbsd.org 	tsz = *sz + 128;
81cd9e1eabSschwarze@openbsd.org 	if (tsz > maxsz)
82cd9e1eabSschwarze@openbsd.org 		tsz = maxsz;
839e509d4eSderaadt@openbsd.org 	if ((tp = recallocarray(*dst, *sz, tsz, 1)) == NULL)
84cd9e1eabSschwarze@openbsd.org 		return -1;
85cd9e1eabSschwarze@openbsd.org 	*dp = tp + (*dp - *dst);
86cd9e1eabSschwarze@openbsd.org 	*dst = tp;
87cd9e1eabSschwarze@openbsd.org 	*sz = tsz;
88cd9e1eabSschwarze@openbsd.org 	return 0;
89cd9e1eabSschwarze@openbsd.org }
90cd9e1eabSschwarze@openbsd.org 
910e059cdfSschwarze@openbsd.org /*
920e059cdfSschwarze@openbsd.org  * The following two functions limit the number of bytes written,
930e059cdfSschwarze@openbsd.org  * including the terminating '\0', to sz.  Unless wp is NULL,
940e059cdfSschwarze@openbsd.org  * they limit the number of display columns occupied to *wp.
950e059cdfSschwarze@openbsd.org  * Whichever is reached first terminates the output string.
960e059cdfSschwarze@openbsd.org  * To stay close to the standard interfaces, they return the number of
970e059cdfSschwarze@openbsd.org  * non-NUL bytes that would have been written if both were unlimited.
980e059cdfSschwarze@openbsd.org  * If wp is NULL, newline, carriage return, and tab are allowed;
990e059cdfSschwarze@openbsd.org  * otherwise, the actual number of columns occupied by what was
1000e059cdfSschwarze@openbsd.org  * written is returned in *wp.
1010e059cdfSschwarze@openbsd.org  */
1020e059cdfSschwarze@openbsd.org 
103*31909696Sdjm@openbsd.org int
vasnmprintf(char ** str,size_t maxsz,int * wp,const char * fmt,va_list ap)1040e059cdfSschwarze@openbsd.org vasnmprintf(char **str, size_t maxsz, int *wp, const char *fmt, va_list ap)
1050e059cdfSschwarze@openbsd.org {
1060e059cdfSschwarze@openbsd.org 	char	*src;	/* Source string returned from vasprintf. */
1070e059cdfSschwarze@openbsd.org 	char	*sp;	/* Pointer into src. */
1080e059cdfSschwarze@openbsd.org 	char	*dst;	/* Destination string to be returned. */
1090e059cdfSschwarze@openbsd.org 	char	*dp;	/* Pointer into dst. */
1100e059cdfSschwarze@openbsd.org 	char	*tp;	/* Temporary pointer for dst. */
1110e059cdfSschwarze@openbsd.org 	size_t	 sz;	/* Number of bytes allocated for dst. */
1120e059cdfSschwarze@openbsd.org 	wchar_t	 wc;	/* Wide character at sp. */
1130e059cdfSschwarze@openbsd.org 	int	 len;	/* Number of bytes in the character at sp. */
1140e059cdfSschwarze@openbsd.org 	int	 ret;	/* Number of bytes needed to format src. */
1150e059cdfSschwarze@openbsd.org 	int	 width;	/* Display width of the character wc. */
1160e059cdfSschwarze@openbsd.org 	int	 total_width, max_width, print;
1170e059cdfSschwarze@openbsd.org 
118ac284a35Sschwarze@openbsd.org 	src = NULL;
119ac284a35Sschwarze@openbsd.org 	if ((ret = vasprintf(&src, fmt, ap)) <= 0)
1200e059cdfSschwarze@openbsd.org 		goto fail;
1210e059cdfSschwarze@openbsd.org 
122cd9e1eabSschwarze@openbsd.org 	sz = strlen(src) + 1;
123ac284a35Sschwarze@openbsd.org 	if ((dst = malloc(sz)) == NULL) {
124ac284a35Sschwarze@openbsd.org 		free(src);
1253ec5fa4bSjsg@openbsd.org 		ret = -1;
1260e059cdfSschwarze@openbsd.org 		goto fail;
127ac284a35Sschwarze@openbsd.org 	}
1280e059cdfSschwarze@openbsd.org 
1290e059cdfSschwarze@openbsd.org 	if (maxsz > INT_MAX)
1300e059cdfSschwarze@openbsd.org 		maxsz = INT_MAX;
1310e059cdfSschwarze@openbsd.org 
1320e059cdfSschwarze@openbsd.org 	sp = src;
1330e059cdfSschwarze@openbsd.org 	dp = dst;
1340e059cdfSschwarze@openbsd.org 	ret = 0;
1350e059cdfSschwarze@openbsd.org 	print = 1;
1360e059cdfSschwarze@openbsd.org 	total_width = 0;
1370e059cdfSschwarze@openbsd.org 	max_width = wp == NULL ? INT_MAX : *wp;
1380e059cdfSschwarze@openbsd.org 	while (*sp != '\0') {
1390e059cdfSschwarze@openbsd.org 		if ((len = mbtowc(&wc, sp, MB_CUR_MAX)) == -1) {
1400e059cdfSschwarze@openbsd.org 			(void)mbtowc(NULL, NULL, MB_CUR_MAX);
1410e059cdfSschwarze@openbsd.org 			if (dangerous_locale()) {
1420e059cdfSschwarze@openbsd.org 				ret = -1;
1430e059cdfSschwarze@openbsd.org 				break;
1440e059cdfSschwarze@openbsd.org 			}
1450e059cdfSschwarze@openbsd.org 			len = 1;
1460e059cdfSschwarze@openbsd.org 			width = -1;
1470e059cdfSschwarze@openbsd.org 		} else if (wp == NULL &&
1480e059cdfSschwarze@openbsd.org 		    (wc == L'\n' || wc == L'\r' || wc == L'\t')) {
1490e059cdfSschwarze@openbsd.org 			/*
1500e059cdfSschwarze@openbsd.org 			 * Don't use width uninitialized; the actual
1510e059cdfSschwarze@openbsd.org 			 * value doesn't matter because total_width
1520e059cdfSschwarze@openbsd.org 			 * is only returned for wp != NULL.
1530e059cdfSschwarze@openbsd.org 			 */
1540e059cdfSschwarze@openbsd.org 			width = 0;
1550e059cdfSschwarze@openbsd.org 		} else if ((width = wcwidth(wc)) == -1 &&
1560e059cdfSschwarze@openbsd.org 		    dangerous_locale()) {
1570e059cdfSschwarze@openbsd.org 			ret = -1;
1580e059cdfSschwarze@openbsd.org 			break;
1590e059cdfSschwarze@openbsd.org 		}
1600e059cdfSschwarze@openbsd.org 
1610e059cdfSschwarze@openbsd.org 		/* Valid, printable character. */
1620e059cdfSschwarze@openbsd.org 
1630e059cdfSschwarze@openbsd.org 		if (width >= 0) {
1640e059cdfSschwarze@openbsd.org 			if (print && (dp - dst >= (int)maxsz - len ||
1650e059cdfSschwarze@openbsd.org 			    total_width > max_width - width))
1660e059cdfSschwarze@openbsd.org 				print = 0;
1670e059cdfSschwarze@openbsd.org 			if (print) {
168cd9e1eabSschwarze@openbsd.org 				if (grow_dst(&dst, &sz, maxsz,
169cd9e1eabSschwarze@openbsd.org 				    &dp, len) == -1) {
170cd9e1eabSschwarze@openbsd.org 					ret = -1;
171cd9e1eabSschwarze@openbsd.org 					break;
172cd9e1eabSschwarze@openbsd.org 				}
1730e059cdfSschwarze@openbsd.org 				total_width += width;
1740e059cdfSschwarze@openbsd.org 				memcpy(dp, sp, len);
1750e059cdfSschwarze@openbsd.org 				dp += len;
1760e059cdfSschwarze@openbsd.org 			}
1770e059cdfSschwarze@openbsd.org 			sp += len;
1780e059cdfSschwarze@openbsd.org 			if (ret >= 0)
1790e059cdfSschwarze@openbsd.org 				ret += len;
1800e059cdfSschwarze@openbsd.org 			continue;
1810e059cdfSschwarze@openbsd.org 		}
1820e059cdfSschwarze@openbsd.org 
1830e059cdfSschwarze@openbsd.org 		/* Escaping required. */
1840e059cdfSschwarze@openbsd.org 
1850e059cdfSschwarze@openbsd.org 		while (len > 0) {
1860e059cdfSschwarze@openbsd.org 			if (print && (dp - dst >= (int)maxsz - 4 ||
1870e059cdfSschwarze@openbsd.org 			    total_width > max_width - 4))
1880e059cdfSschwarze@openbsd.org 				print = 0;
1890e059cdfSschwarze@openbsd.org 			if (print) {
190cd9e1eabSschwarze@openbsd.org 				if (grow_dst(&dst, &sz, maxsz,
191cd9e1eabSschwarze@openbsd.org 				    &dp, 4) == -1) {
1920e059cdfSschwarze@openbsd.org 					ret = -1;
1930e059cdfSschwarze@openbsd.org 					break;
1940e059cdfSschwarze@openbsd.org 				}
1950e059cdfSschwarze@openbsd.org 				tp = vis(dp, *sp, VIS_OCTAL | VIS_ALL, 0);
1960e059cdfSschwarze@openbsd.org 				width = tp - dp;
1970e059cdfSschwarze@openbsd.org 				total_width += width;
1980e059cdfSschwarze@openbsd.org 				dp = tp;
1990e059cdfSschwarze@openbsd.org 			} else
2000e059cdfSschwarze@openbsd.org 				width = 4;
2010e059cdfSschwarze@openbsd.org 			len--;
2020e059cdfSschwarze@openbsd.org 			sp++;
2030e059cdfSschwarze@openbsd.org 			if (ret >= 0)
2040e059cdfSschwarze@openbsd.org 				ret += width;
2050e059cdfSschwarze@openbsd.org 		}
2060e059cdfSschwarze@openbsd.org 		if (len > 0)
2070e059cdfSschwarze@openbsd.org 			break;
2080e059cdfSschwarze@openbsd.org 	}
2090e059cdfSschwarze@openbsd.org 	free(src);
2100e059cdfSschwarze@openbsd.org 	*dp = '\0';
2110e059cdfSschwarze@openbsd.org 	*str = dst;
2120e059cdfSschwarze@openbsd.org 	if (wp != NULL)
2130e059cdfSschwarze@openbsd.org 		*wp = total_width;
2140e059cdfSschwarze@openbsd.org 
2150e059cdfSschwarze@openbsd.org 	/*
2160e059cdfSschwarze@openbsd.org 	 * If the string was truncated by the width limit but
2170e059cdfSschwarze@openbsd.org 	 * would have fit into the size limit, the only sane way
2180e059cdfSschwarze@openbsd.org 	 * to report the problem is using the return value, such
2190e059cdfSschwarze@openbsd.org 	 * that the usual idiom "if (ret < 0 || ret >= sz) error"
2200e059cdfSschwarze@openbsd.org 	 * works as expected.
2210e059cdfSschwarze@openbsd.org 	 */
2220e059cdfSschwarze@openbsd.org 
2230e059cdfSschwarze@openbsd.org 	if (ret < (int)maxsz && !print)
2240e059cdfSschwarze@openbsd.org 		ret = -1;
2250e059cdfSschwarze@openbsd.org 	return ret;
2260e059cdfSschwarze@openbsd.org 
2270e059cdfSschwarze@openbsd.org fail:
2280e059cdfSschwarze@openbsd.org 	if (wp != NULL)
2290e059cdfSschwarze@openbsd.org 		*wp = 0;
230ac284a35Sschwarze@openbsd.org 	if (ret == 0) {
231ac284a35Sschwarze@openbsd.org 		*str = src;
232ac284a35Sschwarze@openbsd.org 		return 0;
233ac284a35Sschwarze@openbsd.org 	} else {
234ac284a35Sschwarze@openbsd.org 		*str = NULL;
2350e059cdfSschwarze@openbsd.org 		return -1;
2360e059cdfSschwarze@openbsd.org 	}
237ac284a35Sschwarze@openbsd.org }
2380e059cdfSschwarze@openbsd.org 
2390e059cdfSschwarze@openbsd.org int
snmprintf(char * str,size_t sz,int * wp,const char * fmt,...)2400e059cdfSschwarze@openbsd.org snmprintf(char *str, size_t sz, int *wp, const char *fmt, ...)
2410e059cdfSschwarze@openbsd.org {
2420e059cdfSschwarze@openbsd.org 	va_list	 ap;
2436fb6f186Smarkus@openbsd.org 	char	*cp = NULL;
2440e059cdfSschwarze@openbsd.org 	int	 ret;
2450e059cdfSschwarze@openbsd.org 
2460e059cdfSschwarze@openbsd.org 	va_start(ap, fmt);
2470e059cdfSschwarze@openbsd.org 	ret = vasnmprintf(&cp, sz, wp, fmt, ap);
2480e059cdfSschwarze@openbsd.org 	va_end(ap);
249ac284a35Sschwarze@openbsd.org 	if (cp != NULL) {
2500e059cdfSschwarze@openbsd.org 		(void)strlcpy(str, cp, sz);
2510e059cdfSschwarze@openbsd.org 		free(cp);
252ac284a35Sschwarze@openbsd.org 	} else
253ac284a35Sschwarze@openbsd.org 		*str = '\0';
2540e059cdfSschwarze@openbsd.org 	return ret;
2550e059cdfSschwarze@openbsd.org }
2560e059cdfSschwarze@openbsd.org 
257af628b8aSdjm@openbsd.org int
asmprintf(char ** outp,size_t sz,int * wp,const char * fmt,...)258af628b8aSdjm@openbsd.org asmprintf(char **outp, size_t sz, int *wp, const char *fmt, ...)
259af628b8aSdjm@openbsd.org {
260af628b8aSdjm@openbsd.org 	va_list	 ap;
261af628b8aSdjm@openbsd.org 	int	 ret;
262af628b8aSdjm@openbsd.org 
263af628b8aSdjm@openbsd.org 	*outp = NULL;
264af628b8aSdjm@openbsd.org 	va_start(ap, fmt);
265af628b8aSdjm@openbsd.org 	ret = vasnmprintf(outp, sz, wp, fmt, ap);
266af628b8aSdjm@openbsd.org 	va_end(ap);
267af628b8aSdjm@openbsd.org 
268af628b8aSdjm@openbsd.org 	return ret;
269af628b8aSdjm@openbsd.org }
270af628b8aSdjm@openbsd.org 
2710e059cdfSschwarze@openbsd.org /*
2720e059cdfSschwarze@openbsd.org  * To stay close to the standard interfaces, the following functions
2730e059cdfSschwarze@openbsd.org  * return the number of non-NUL bytes written.
2740e059cdfSschwarze@openbsd.org  */
2750e059cdfSschwarze@openbsd.org 
2760e059cdfSschwarze@openbsd.org int
vfmprintf(FILE * stream,const char * fmt,va_list ap)2770e059cdfSschwarze@openbsd.org vfmprintf(FILE *stream, const char *fmt, va_list ap)
2780e059cdfSschwarze@openbsd.org {
2796fb6f186Smarkus@openbsd.org 	char	*str = NULL;
2800e059cdfSschwarze@openbsd.org 	int	 ret;
2810e059cdfSschwarze@openbsd.org 
2826fb6f186Smarkus@openbsd.org 	if ((ret = vasnmprintf(&str, INT_MAX, NULL, fmt, ap)) < 0) {
2836fb6f186Smarkus@openbsd.org 		free(str);
2840e059cdfSschwarze@openbsd.org 		return -1;
2856fb6f186Smarkus@openbsd.org 	}
2860e059cdfSschwarze@openbsd.org 	if (fputs(str, stream) == EOF)
2870e059cdfSschwarze@openbsd.org 		ret = -1;
2880e059cdfSschwarze@openbsd.org 	free(str);
2890e059cdfSschwarze@openbsd.org 	return ret;
2900e059cdfSschwarze@openbsd.org }
2910e059cdfSschwarze@openbsd.org 
2920e059cdfSschwarze@openbsd.org int
fmprintf(FILE * stream,const char * fmt,...)2930e059cdfSschwarze@openbsd.org fmprintf(FILE *stream, const char *fmt, ...)
2940e059cdfSschwarze@openbsd.org {
2950e059cdfSschwarze@openbsd.org 	va_list	 ap;
2960e059cdfSschwarze@openbsd.org 	int	 ret;
2970e059cdfSschwarze@openbsd.org 
2980e059cdfSschwarze@openbsd.org 	va_start(ap, fmt);
2990e059cdfSschwarze@openbsd.org 	ret = vfmprintf(stream, fmt, ap);
3000e059cdfSschwarze@openbsd.org 	va_end(ap);
3010e059cdfSschwarze@openbsd.org 	return ret;
3020e059cdfSschwarze@openbsd.org }
3030e059cdfSschwarze@openbsd.org 
3040e059cdfSschwarze@openbsd.org int
mprintf(const char * fmt,...)3050e059cdfSschwarze@openbsd.org mprintf(const char *fmt, ...)
3060e059cdfSschwarze@openbsd.org {
3070e059cdfSschwarze@openbsd.org 	va_list	 ap;
3080e059cdfSschwarze@openbsd.org 	int	 ret;
3090e059cdfSschwarze@openbsd.org 
3100e059cdfSschwarze@openbsd.org 	va_start(ap, fmt);
3110e059cdfSschwarze@openbsd.org 	ret = vfmprintf(stdout, fmt, ap);
3120e059cdfSschwarze@openbsd.org 	va_end(ap);
3130e059cdfSschwarze@openbsd.org 	return ret;
3140e059cdfSschwarze@openbsd.org }
315dda78a03SDamien Miller 
316dda78a03SDamien Miller /*
317dda78a03SDamien Miller  * Set up libc for multibyte output in the user's chosen locale.
318dda78a03SDamien Miller  *
319dda78a03SDamien Miller  * XXX: we are known to have problems with Turkish (i/I confusion) so we
320dda78a03SDamien Miller  *      deliberately fall back to the C locale for now. Longer term we should
321dda78a03SDamien Miller  *      always prefer to select C.[encoding] if possible, but there's no
322dda78a03SDamien Miller  *      standardisation in locales between systems, so we'll need to survey
323dda78a03SDamien Miller  *      what's out there first.
324dda78a03SDamien Miller  */
325dda78a03SDamien Miller void
msetlocale(void)326dda78a03SDamien Miller msetlocale(void)
327dda78a03SDamien Miller {
328dda78a03SDamien Miller 	const char *vars[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
329dda78a03SDamien Miller 	char *cp;
330dda78a03SDamien Miller 	int i;
331dda78a03SDamien Miller 
332dda78a03SDamien Miller 	/*
333dda78a03SDamien Miller 	 * We can't yet cope with dotless/dotted I in Turkish locales,
334dda78a03SDamien Miller 	 * so fall back to the C locale for these.
335dda78a03SDamien Miller 	 */
336dda78a03SDamien Miller 	for (i = 0; vars[i] != NULL; i++) {
337dda78a03SDamien Miller 		if ((cp = getenv(vars[i])) == NULL)
338dda78a03SDamien Miller 			continue;
339dda78a03SDamien Miller 		if (strncasecmp(cp, "TR", 2) != 0)
340dda78a03SDamien Miller 			break;
341dda78a03SDamien Miller 		/*
342dda78a03SDamien Miller 		 * If we're in a UTF-8 locale then prefer to use
343dda78a03SDamien Miller 		 * the C.UTF-8 locale (or equivalent) if it exists.
344dda78a03SDamien Miller 		 */
345dda78a03SDamien Miller 		if ((strcasestr(cp, "UTF-8") != NULL ||
346dda78a03SDamien Miller 		    strcasestr(cp, "UTF8") != NULL) &&
347dda78a03SDamien Miller 		    (setlocale(LC_CTYPE, "C.UTF-8") != NULL ||
348dda78a03SDamien Miller 		    setlocale(LC_CTYPE, "POSIX.UTF-8") != NULL))
349dda78a03SDamien Miller 			return;
350dda78a03SDamien Miller 		setlocale(LC_CTYPE, "C");
351dda78a03SDamien Miller 		return;
352dda78a03SDamien Miller 	}
353dda78a03SDamien Miller 	/* We can handle this locale */
354dda78a03SDamien Miller 	setlocale(LC_CTYPE, "");
355dda78a03SDamien Miller }
356