xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/locale.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*    locale.c
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4*0Sstevel@tonic-gate  *    2000, 2001, 2002, 2003, by Larry Wall and others
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  *    You may distribute under the terms of either the GNU General Public
7*0Sstevel@tonic-gate  *    License or the Artistic License, as specified in the README file.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  */
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate /*
12*0Sstevel@tonic-gate  * A Elbereth Gilthoniel,
13*0Sstevel@tonic-gate  * silivren penna m�riel
14*0Sstevel@tonic-gate  * o menel aglar elenath!
15*0Sstevel@tonic-gate  * Na-chaered palan-d�riel
16*0Sstevel@tonic-gate  * o galadhremmin ennorath,
17*0Sstevel@tonic-gate  * Fanuilos, le linnathon
18*0Sstevel@tonic-gate  * nef aear, si nef aearon!
19*0Sstevel@tonic-gate  */
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include "EXTERN.h"
22*0Sstevel@tonic-gate #define PERL_IN_LOCALE_C
23*0Sstevel@tonic-gate #include "perl.h"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #ifdef I_LOCALE
26*0Sstevel@tonic-gate #  include <locale.h>
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #ifdef I_LANGINFO
30*0Sstevel@tonic-gate #   include <langinfo.h>
31*0Sstevel@tonic-gate #endif
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "reentr.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * Standardize the locale name from a string returned by 'setlocale'.
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * The standard return value of setlocale() is either
39*0Sstevel@tonic-gate  * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
40*0Sstevel@tonic-gate  * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
41*0Sstevel@tonic-gate  *     (the space-separated values represent the various sublocales,
42*0Sstevel@tonic-gate  *      in some unspecificed order)
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
45*0Sstevel@tonic-gate  * which is harmful for further use of the string in setlocale().
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate STATIC char *
S_stdize_locale(pTHX_ char * locs)49*0Sstevel@tonic-gate S_stdize_locale(pTHX_ char *locs)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate     char *s;
52*0Sstevel@tonic-gate     bool okay = TRUE;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate     if ((s = strchr(locs, '='))) {
55*0Sstevel@tonic-gate 	char *t;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	okay = FALSE;
58*0Sstevel@tonic-gate 	if ((t = strchr(s, '.'))) {
59*0Sstevel@tonic-gate 	    char *u;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	    if ((u = strchr(t, '\n'))) {
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 		if (u[1] == 0) {
64*0Sstevel@tonic-gate 		    STRLEN len = u - s;
65*0Sstevel@tonic-gate 		    Move(s + 1, locs, len, char);
66*0Sstevel@tonic-gate 		    locs[len] = 0;
67*0Sstevel@tonic-gate 		    okay = TRUE;
68*0Sstevel@tonic-gate 		}
69*0Sstevel@tonic-gate 	    }
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate     }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate     if (!okay)
74*0Sstevel@tonic-gate 	Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate     return locs;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate void
Perl_set_numeric_radix(pTHX)80*0Sstevel@tonic-gate Perl_set_numeric_radix(pTHX)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
83*0Sstevel@tonic-gate # ifdef HAS_LOCALECONV
84*0Sstevel@tonic-gate     struct lconv* lc;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate     lc = localeconv();
87*0Sstevel@tonic-gate     if (lc && lc->decimal_point) {
88*0Sstevel@tonic-gate 	if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
89*0Sstevel@tonic-gate 	    SvREFCNT_dec(PL_numeric_radix_sv);
90*0Sstevel@tonic-gate 	    PL_numeric_radix_sv = Nullsv;
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 	else {
93*0Sstevel@tonic-gate 	    if (PL_numeric_radix_sv)
94*0Sstevel@tonic-gate 		sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
95*0Sstevel@tonic-gate 	    else
96*0Sstevel@tonic-gate 		PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate     }
99*0Sstevel@tonic-gate     else
100*0Sstevel@tonic-gate 	PL_numeric_radix_sv = Nullsv;
101*0Sstevel@tonic-gate # endif /* HAS_LOCALECONV */
102*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * Set up for a new numeric locale.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate void
Perl_new_numeric(pTHX_ char * newnum)109*0Sstevel@tonic-gate Perl_new_numeric(pTHX_ char *newnum)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate     if (! newnum) {
114*0Sstevel@tonic-gate 	if (PL_numeric_name) {
115*0Sstevel@tonic-gate 	    Safefree(PL_numeric_name);
116*0Sstevel@tonic-gate 	    PL_numeric_name = NULL;
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 	PL_numeric_standard = TRUE;
119*0Sstevel@tonic-gate 	PL_numeric_local = TRUE;
120*0Sstevel@tonic-gate 	return;
121*0Sstevel@tonic-gate     }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate     if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
124*0Sstevel@tonic-gate 	Safefree(PL_numeric_name);
125*0Sstevel@tonic-gate 	PL_numeric_name = stdize_locale(savepv(newnum));
126*0Sstevel@tonic-gate 	PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
127*0Sstevel@tonic-gate 	PL_numeric_local = TRUE;
128*0Sstevel@tonic-gate 	set_numeric_radix();
129*0Sstevel@tonic-gate     }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate void
Perl_set_numeric_standard(pTHX)135*0Sstevel@tonic-gate Perl_set_numeric_standard(pTHX)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate     if (! PL_numeric_standard) {
140*0Sstevel@tonic-gate 	setlocale(LC_NUMERIC, "C");
141*0Sstevel@tonic-gate 	PL_numeric_standard = TRUE;
142*0Sstevel@tonic-gate 	PL_numeric_local = FALSE;
143*0Sstevel@tonic-gate 	set_numeric_radix();
144*0Sstevel@tonic-gate     }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate void
Perl_set_numeric_local(pTHX)150*0Sstevel@tonic-gate Perl_set_numeric_local(pTHX)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate     if (! PL_numeric_local) {
155*0Sstevel@tonic-gate 	setlocale(LC_NUMERIC, PL_numeric_name);
156*0Sstevel@tonic-gate 	PL_numeric_standard = FALSE;
157*0Sstevel@tonic-gate 	PL_numeric_local = TRUE;
158*0Sstevel@tonic-gate 	set_numeric_radix();
159*0Sstevel@tonic-gate     }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  * Set up for a new ctype locale.
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate void
Perl_new_ctype(pTHX_ char * newctype)168*0Sstevel@tonic-gate Perl_new_ctype(pTHX_ char *newctype)
169*0Sstevel@tonic-gate {
170*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate     int i;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate     for (i = 0; i < 256; i++) {
175*0Sstevel@tonic-gate 	if (isUPPER_LC(i))
176*0Sstevel@tonic-gate 	    PL_fold_locale[i] = toLOWER_LC(i);
177*0Sstevel@tonic-gate 	else if (isLOWER_LC(i))
178*0Sstevel@tonic-gate 	    PL_fold_locale[i] = toUPPER_LC(i);
179*0Sstevel@tonic-gate 	else
180*0Sstevel@tonic-gate 	    PL_fold_locale[i] = i;
181*0Sstevel@tonic-gate     }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate  * Set up for a new collation locale.
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate void
Perl_new_collate(pTHX_ char * newcoll)190*0Sstevel@tonic-gate Perl_new_collate(pTHX_ char *newcoll)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate     if (! newcoll) {
195*0Sstevel@tonic-gate 	if (PL_collation_name) {
196*0Sstevel@tonic-gate 	    ++PL_collation_ix;
197*0Sstevel@tonic-gate 	    Safefree(PL_collation_name);
198*0Sstevel@tonic-gate 	    PL_collation_name = NULL;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 	PL_collation_standard = TRUE;
201*0Sstevel@tonic-gate 	PL_collxfrm_base = 0;
202*0Sstevel@tonic-gate 	PL_collxfrm_mult = 2;
203*0Sstevel@tonic-gate 	return;
204*0Sstevel@tonic-gate     }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate     if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
207*0Sstevel@tonic-gate 	++PL_collation_ix;
208*0Sstevel@tonic-gate 	Safefree(PL_collation_name);
209*0Sstevel@tonic-gate 	PL_collation_name = stdize_locale(savepv(newcoll));
210*0Sstevel@tonic-gate 	PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	{
213*0Sstevel@tonic-gate 	  /*  2: at most so many chars ('a', 'b'). */
214*0Sstevel@tonic-gate 	  /* 50: surely no system expands a char more. */
215*0Sstevel@tonic-gate #define XFRMBUFSIZE  (2 * 50)
216*0Sstevel@tonic-gate 	  char xbuf[XFRMBUFSIZE];
217*0Sstevel@tonic-gate 	  Size_t fa = strxfrm(xbuf, "a",  XFRMBUFSIZE);
218*0Sstevel@tonic-gate 	  Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
219*0Sstevel@tonic-gate 	  SSize_t mult = fb - fa;
220*0Sstevel@tonic-gate 	  if (mult < 1)
221*0Sstevel@tonic-gate 	      Perl_croak(aTHX_ "strxfrm() gets absurd");
222*0Sstevel@tonic-gate 	  PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
223*0Sstevel@tonic-gate 	  PL_collxfrm_mult = mult;
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate     }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate /*
231*0Sstevel@tonic-gate  * Initialize locale awareness.
232*0Sstevel@tonic-gate  */
233*0Sstevel@tonic-gate int
Perl_init_i18nl10n(pTHX_ int printwarn)234*0Sstevel@tonic-gate Perl_init_i18nl10n(pTHX_ int printwarn)
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate     int ok = 1;
237*0Sstevel@tonic-gate     /* returns
238*0Sstevel@tonic-gate      *    1 = set ok or not applicable,
239*0Sstevel@tonic-gate      *    0 = fallback to C locale,
240*0Sstevel@tonic-gate      *   -1 = fallback to C locale failed
241*0Sstevel@tonic-gate      */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate #if defined(USE_LOCALE)
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
246*0Sstevel@tonic-gate     char *curctype   = NULL;
247*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
248*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
249*0Sstevel@tonic-gate     char *curcoll    = NULL;
250*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
251*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
252*0Sstevel@tonic-gate     char *curnum     = NULL;
253*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
254*0Sstevel@tonic-gate #ifdef __GLIBC__
255*0Sstevel@tonic-gate     char *language   = PerlEnv_getenv("LANGUAGE");
256*0Sstevel@tonic-gate #endif
257*0Sstevel@tonic-gate     char *lc_all     = PerlEnv_getenv("LC_ALL");
258*0Sstevel@tonic-gate     char *lang       = PerlEnv_getenv("LANG");
259*0Sstevel@tonic-gate     bool setlocale_failure = FALSE;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate #ifdef LOCALE_ENVIRON_REQUIRED
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate     /*
264*0Sstevel@tonic-gate      * Ultrix setlocale(..., "") fails if there are no environment
265*0Sstevel@tonic-gate      * variables from which to get a locale name.
266*0Sstevel@tonic-gate      */
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate     bool done = FALSE;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate #ifdef LC_ALL
271*0Sstevel@tonic-gate     if (lang) {
272*0Sstevel@tonic-gate 	if (setlocale(LC_ALL, ""))
273*0Sstevel@tonic-gate 	    done = TRUE;
274*0Sstevel@tonic-gate 	else
275*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
276*0Sstevel@tonic-gate     }
277*0Sstevel@tonic-gate     if (!setlocale_failure) {
278*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
279*0Sstevel@tonic-gate 	if (! (curctype =
280*0Sstevel@tonic-gate 	       setlocale(LC_CTYPE,
281*0Sstevel@tonic-gate 			 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
282*0Sstevel@tonic-gate 				    ? "" : Nullch)))
283*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
284*0Sstevel@tonic-gate 	else
285*0Sstevel@tonic-gate 	    curctype = savepv(curctype);
286*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
287*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
288*0Sstevel@tonic-gate 	if (! (curcoll =
289*0Sstevel@tonic-gate 	       setlocale(LC_COLLATE,
290*0Sstevel@tonic-gate 			 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
291*0Sstevel@tonic-gate 				   ? "" : Nullch)))
292*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
293*0Sstevel@tonic-gate 	else
294*0Sstevel@tonic-gate 	    curcoll = savepv(curcoll);
295*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
296*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
297*0Sstevel@tonic-gate 	if (! (curnum =
298*0Sstevel@tonic-gate 	       setlocale(LC_NUMERIC,
299*0Sstevel@tonic-gate 			 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
300*0Sstevel@tonic-gate 				  ? "" : Nullch)))
301*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
302*0Sstevel@tonic-gate 	else
303*0Sstevel@tonic-gate 	    curnum = savepv(curnum);
304*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
305*0Sstevel@tonic-gate     }
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate #endif /* LC_ALL */
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate #endif /* !LOCALE_ENVIRON_REQUIRED */
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate #ifdef LC_ALL
312*0Sstevel@tonic-gate     if (! setlocale(LC_ALL, ""))
313*0Sstevel@tonic-gate 	setlocale_failure = TRUE;
314*0Sstevel@tonic-gate #endif /* LC_ALL */
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate     if (!setlocale_failure) {
317*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
318*0Sstevel@tonic-gate 	if (! (curctype = setlocale(LC_CTYPE, "")))
319*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
320*0Sstevel@tonic-gate 	else
321*0Sstevel@tonic-gate 	    curctype = savepv(curctype);
322*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
323*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
324*0Sstevel@tonic-gate 	if (! (curcoll = setlocale(LC_COLLATE, "")))
325*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
326*0Sstevel@tonic-gate 	else
327*0Sstevel@tonic-gate 	    curcoll = savepv(curcoll);
328*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
329*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
330*0Sstevel@tonic-gate 	if (! (curnum = setlocale(LC_NUMERIC, "")))
331*0Sstevel@tonic-gate 	    setlocale_failure = TRUE;
332*0Sstevel@tonic-gate 	else
333*0Sstevel@tonic-gate 	    curnum = savepv(curnum);
334*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
335*0Sstevel@tonic-gate     }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate     if (setlocale_failure) {
338*0Sstevel@tonic-gate 	char *p;
339*0Sstevel@tonic-gate 	bool locwarn = (printwarn > 1 ||
340*0Sstevel@tonic-gate 			(printwarn &&
341*0Sstevel@tonic-gate 			 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if (locwarn) {
344*0Sstevel@tonic-gate #ifdef LC_ALL
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
347*0Sstevel@tonic-gate 	       "perl: warning: Setting locale failed.\n");
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate #else /* !LC_ALL */
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
352*0Sstevel@tonic-gate 	       "perl: warning: Setting locale failed for the categories:\n\t");
353*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
354*0Sstevel@tonic-gate 	    if (! curctype)
355*0Sstevel@tonic-gate 		PerlIO_printf(Perl_error_log, "LC_CTYPE ");
356*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
357*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
358*0Sstevel@tonic-gate 	    if (! curcoll)
359*0Sstevel@tonic-gate 		PerlIO_printf(Perl_error_log, "LC_COLLATE ");
360*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
361*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
362*0Sstevel@tonic-gate 	    if (! curnum)
363*0Sstevel@tonic-gate 		PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
364*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
365*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log, "\n");
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate #endif /* LC_ALL */
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
370*0Sstevel@tonic-gate 		"perl: warning: Please check that your locale settings:\n");
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate #ifdef __GLIBC__
373*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
374*0Sstevel@tonic-gate 			  "\tLANGUAGE = %c%s%c,\n",
375*0Sstevel@tonic-gate 			  language ? '"' : '(',
376*0Sstevel@tonic-gate 			  language ? language : "unset",
377*0Sstevel@tonic-gate 			  language ? '"' : ')');
378*0Sstevel@tonic-gate #endif
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
381*0Sstevel@tonic-gate 			  "\tLC_ALL = %c%s%c,\n",
382*0Sstevel@tonic-gate 			  lc_all ? '"' : '(',
383*0Sstevel@tonic-gate 			  lc_all ? lc_all : "unset",
384*0Sstevel@tonic-gate 			  lc_all ? '"' : ')');
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate #if defined(USE_ENVIRON_ARRAY)
387*0Sstevel@tonic-gate 	    {
388*0Sstevel@tonic-gate 	      char **e;
389*0Sstevel@tonic-gate 	      for (e = environ; *e; e++) {
390*0Sstevel@tonic-gate 		  if (strnEQ(*e, "LC_", 3)
391*0Sstevel@tonic-gate 			&& strnNE(*e, "LC_ALL=", 7)
392*0Sstevel@tonic-gate 			&& (p = strchr(*e, '=')))
393*0Sstevel@tonic-gate 		      PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
394*0Sstevel@tonic-gate 				    (int)(p - *e), *e, p + 1);
395*0Sstevel@tonic-gate 	      }
396*0Sstevel@tonic-gate 	    }
397*0Sstevel@tonic-gate #else
398*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
399*0Sstevel@tonic-gate 			  "\t(possibly more locale environment variables)\n");
400*0Sstevel@tonic-gate #endif
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
403*0Sstevel@tonic-gate 			  "\tLANG = %c%s%c\n",
404*0Sstevel@tonic-gate 			  lang ? '"' : '(',
405*0Sstevel@tonic-gate 			  lang ? lang : "unset",
406*0Sstevel@tonic-gate 			  lang ? '"' : ')');
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	    PerlIO_printf(Perl_error_log,
409*0Sstevel@tonic-gate 			  "    are supported and installed on your system.\n");
410*0Sstevel@tonic-gate 	}
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate #ifdef LC_ALL
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	if (setlocale(LC_ALL, "C")) {
415*0Sstevel@tonic-gate 	    if (locwarn)
416*0Sstevel@tonic-gate 		PerlIO_printf(Perl_error_log,
417*0Sstevel@tonic-gate       "perl: warning: Falling back to the standard locale (\"C\").\n");
418*0Sstevel@tonic-gate 	    ok = 0;
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 	else {
421*0Sstevel@tonic-gate 	    if (locwarn)
422*0Sstevel@tonic-gate 		PerlIO_printf(Perl_error_log,
423*0Sstevel@tonic-gate       "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
424*0Sstevel@tonic-gate 	    ok = -1;
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate #else /* ! LC_ALL */
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	if (0
430*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
431*0Sstevel@tonic-gate 	    || !(curctype || setlocale(LC_CTYPE, "C"))
432*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
433*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
434*0Sstevel@tonic-gate 	    || !(curcoll || setlocale(LC_COLLATE, "C"))
435*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
436*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
437*0Sstevel@tonic-gate 	    || !(curnum || setlocale(LC_NUMERIC, "C"))
438*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
439*0Sstevel@tonic-gate 	    )
440*0Sstevel@tonic-gate 	{
441*0Sstevel@tonic-gate 	    if (locwarn)
442*0Sstevel@tonic-gate 		PerlIO_printf(Perl_error_log,
443*0Sstevel@tonic-gate       "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
444*0Sstevel@tonic-gate 	    ok = -1;
445*0Sstevel@tonic-gate 	}
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate #endif /* ! LC_ALL */
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
450*0Sstevel@tonic-gate 	curctype = savepv(setlocale(LC_CTYPE, Nullch));
451*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
452*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
453*0Sstevel@tonic-gate 	curcoll = savepv(setlocale(LC_COLLATE, Nullch));
454*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
455*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
456*0Sstevel@tonic-gate 	curnum = savepv(setlocale(LC_NUMERIC, Nullch));
457*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
458*0Sstevel@tonic-gate     }
459*0Sstevel@tonic-gate     else {
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
462*0Sstevel@tonic-gate     new_ctype(curctype);
463*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
466*0Sstevel@tonic-gate     new_collate(curcoll);
467*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
470*0Sstevel@tonic-gate     new_numeric(curnum);
471*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate     }
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate #endif /* USE_LOCALE */
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate #ifdef USE_PERLIO
478*0Sstevel@tonic-gate     {
479*0Sstevel@tonic-gate       /* Set PL_utf8locale to TRUE if using PerlIO _and_
480*0Sstevel@tonic-gate 	 any of the following are true:
481*0Sstevel@tonic-gate 	 - nl_langinfo(CODESET) contains /^utf-?8/i
482*0Sstevel@tonic-gate 	 - $ENV{LC_ALL}   contains /^utf-?8/i
483*0Sstevel@tonic-gate 	 - $ENV{LC_CTYPE} contains /^utf-?8/i
484*0Sstevel@tonic-gate 	 - $ENV{LANG}     contains /^utf-?8/i
485*0Sstevel@tonic-gate 	 The LC_ALL, LC_CTYPE, LANG obey the usual override
486*0Sstevel@tonic-gate 	 hierarchy of locale environment variables.  (LANGUAGE
487*0Sstevel@tonic-gate 	 affects only LC_MESSAGES only under glibc.) (If present,
488*0Sstevel@tonic-gate 	 it overrides LC_MESSAGES for GNU gettext, and it also
489*0Sstevel@tonic-gate 	 can have more than one locale, separated by spaces,
490*0Sstevel@tonic-gate 	 in case you need to know.)
491*0Sstevel@tonic-gate 	 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
492*0Sstevel@tonic-gate          are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
493*0Sstevel@tonic-gate 	 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
494*0Sstevel@tonic-gate       */
495*0Sstevel@tonic-gate 	 bool utf8locale = FALSE;
496*0Sstevel@tonic-gate 	 char *codeset = NULL;
497*0Sstevel@tonic-gate #if defined(HAS_NL_LANGINFO) && defined(CODESET)
498*0Sstevel@tonic-gate 	 codeset = nl_langinfo(CODESET);
499*0Sstevel@tonic-gate #endif
500*0Sstevel@tonic-gate 	 if (codeset)
501*0Sstevel@tonic-gate 	      utf8locale = (ibcmp(codeset,  "UTF-8", 5) == 0 ||
502*0Sstevel@tonic-gate  			    ibcmp(codeset,  "UTF8",  4) == 0);
503*0Sstevel@tonic-gate #if defined(USE_LOCALE)
504*0Sstevel@tonic-gate 	 else { /* nl_langinfo(CODESET) is supposed to correctly
505*0Sstevel@tonic-gate 		 * interpret the locale environment variables,
506*0Sstevel@tonic-gate 		 * but just in case it fails, let's do this manually. */
507*0Sstevel@tonic-gate 	      if (lang)
508*0Sstevel@tonic-gate 		   utf8locale = (ibcmp(lang,     "UTF-8", 5) == 0 ||
509*0Sstevel@tonic-gate 			         ibcmp(lang,     "UTF8",  4) == 0);
510*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
511*0Sstevel@tonic-gate 	      if (curctype)
512*0Sstevel@tonic-gate 		   utf8locale = (ibcmp(curctype,     "UTF-8", 5) == 0 ||
513*0Sstevel@tonic-gate 			         ibcmp(curctype,     "UTF8",  4) == 0);
514*0Sstevel@tonic-gate #endif
515*0Sstevel@tonic-gate 	      if (lc_all)
516*0Sstevel@tonic-gate 		   utf8locale = (ibcmp(lc_all,   "UTF-8", 5) == 0 ||
517*0Sstevel@tonic-gate 			         ibcmp(lc_all,   "UTF8",  4) == 0);
518*0Sstevel@tonic-gate 	 }
519*0Sstevel@tonic-gate #endif /* USE_LOCALE */
520*0Sstevel@tonic-gate 	 if (utf8locale)
521*0Sstevel@tonic-gate 	      PL_utf8locale = TRUE;
522*0Sstevel@tonic-gate     }
523*0Sstevel@tonic-gate     /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
524*0Sstevel@tonic-gate        This is an alternative to using the -C command line switch
525*0Sstevel@tonic-gate        (the -C if present will override this). */
526*0Sstevel@tonic-gate     {
527*0Sstevel@tonic-gate 	 char *p = PerlEnv_getenv("PERL_UNICODE");
528*0Sstevel@tonic-gate 	 PL_unicode = p ? parse_unicode_opts(&p) : 0;
529*0Sstevel@tonic-gate     }
530*0Sstevel@tonic-gate #endif
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate #ifdef USE_LOCALE_CTYPE
533*0Sstevel@tonic-gate     if (curctype != NULL)
534*0Sstevel@tonic-gate 	Safefree(curctype);
535*0Sstevel@tonic-gate #endif /* USE_LOCALE_CTYPE */
536*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
537*0Sstevel@tonic-gate     if (curcoll != NULL)
538*0Sstevel@tonic-gate 	Safefree(curcoll);
539*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
540*0Sstevel@tonic-gate #ifdef USE_LOCALE_NUMERIC
541*0Sstevel@tonic-gate     if (curnum != NULL)
542*0Sstevel@tonic-gate 	Safefree(curnum);
543*0Sstevel@tonic-gate #endif /* USE_LOCALE_NUMERIC */
544*0Sstevel@tonic-gate     return ok;
545*0Sstevel@tonic-gate }
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate /* Backwards compatibility. */
548*0Sstevel@tonic-gate int
Perl_init_i18nl14n(pTHX_ int printwarn)549*0Sstevel@tonic-gate Perl_init_i18nl14n(pTHX_ int printwarn)
550*0Sstevel@tonic-gate {
551*0Sstevel@tonic-gate     return init_i18nl10n(printwarn);
552*0Sstevel@tonic-gate }
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate #ifdef USE_LOCALE_COLLATE
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate /*
557*0Sstevel@tonic-gate  * mem_collxfrm() is a bit like strxfrm() but with two important
558*0Sstevel@tonic-gate  * differences. First, it handles embedded NULs. Second, it allocates
559*0Sstevel@tonic-gate  * a bit more memory than needed for the transformed data itself.
560*0Sstevel@tonic-gate  * The real transformed data begins at offset sizeof(collationix).
561*0Sstevel@tonic-gate  * Please see sv_collxfrm() to see how this is used.
562*0Sstevel@tonic-gate  */
563*0Sstevel@tonic-gate char *
Perl_mem_collxfrm(pTHX_ const char * s,STRLEN len,STRLEN * xlen)564*0Sstevel@tonic-gate Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
565*0Sstevel@tonic-gate {
566*0Sstevel@tonic-gate     char *xbuf;
567*0Sstevel@tonic-gate     STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate     /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
570*0Sstevel@tonic-gate     /* the +1 is for the terminating NUL. */
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate     xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
573*0Sstevel@tonic-gate     New(171, xbuf, xAlloc, char);
574*0Sstevel@tonic-gate     if (! xbuf)
575*0Sstevel@tonic-gate 	goto bad;
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate     *(U32*)xbuf = PL_collation_ix;
578*0Sstevel@tonic-gate     xout = sizeof(PL_collation_ix);
579*0Sstevel@tonic-gate     for (xin = 0; xin < len; ) {
580*0Sstevel@tonic-gate 	SSize_t xused;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	for (;;) {
583*0Sstevel@tonic-gate 	    xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
584*0Sstevel@tonic-gate 	    if (xused == -1)
585*0Sstevel@tonic-gate 		goto bad;
586*0Sstevel@tonic-gate 	    if ((STRLEN)xused < xAlloc - xout)
587*0Sstevel@tonic-gate 		break;
588*0Sstevel@tonic-gate 	    xAlloc = (2 * xAlloc) + 1;
589*0Sstevel@tonic-gate 	    Renew(xbuf, xAlloc, char);
590*0Sstevel@tonic-gate 	    if (! xbuf)
591*0Sstevel@tonic-gate 		goto bad;
592*0Sstevel@tonic-gate 	}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	xin += strlen(s + xin) + 1;
595*0Sstevel@tonic-gate 	xout += xused;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	/* Embedded NULs are understood but silently skipped
598*0Sstevel@tonic-gate 	 * because they make no sense in locale collation. */
599*0Sstevel@tonic-gate     }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate     xbuf[xout] = '\0';
602*0Sstevel@tonic-gate     *xlen = xout - sizeof(PL_collation_ix);
603*0Sstevel@tonic-gate     return xbuf;
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate   bad:
606*0Sstevel@tonic-gate     Safefree(xbuf);
607*0Sstevel@tonic-gate     *xlen = 0;
608*0Sstevel@tonic-gate     return NULL;
609*0Sstevel@tonic-gate }
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate #endif /* USE_LOCALE_COLLATE */
612*0Sstevel@tonic-gate 
613