SetLocale.c revision b4ee4795
1 2/* 3 * Copyright 1990, 1991 by OMRON Corporation, NTT Software Corporation, 4 * and Nippon Telegraph and Telephone Corporation 5 * 6 * Permission to use, copy, modify, distribute, and sell this software and its 7 * documentation for any purpose is hereby granted without fee, provided that 8 * the above copyright notice appear in all copies and that both that 9 * copyright notice and this permission notice appear in supporting 10 * documentation, and that the names of OMRON, NTT Software, and NTT 11 * not be used in advertising or publicity pertaining to distribution of the 12 * software without specific, written prior permission. OMRON, NTT Software, 13 * and NTT make no representations about the suitability of this 14 * software for any purpose. It is provided "as is" without express or 15 * implied warranty. 16 * 17 * OMRON, NTT SOFTWARE, AND NTT, DISCLAIM ALL WARRANTIES WITH REGARD 18 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 19 * AND FITNESS, IN NO EVENT SHALL OMRON, NTT SOFTWARE, OR NTT, BE 20 * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 24 * 25 * Authors: Li Yuhong OMRON Corporation 26 * Tetsuya Kato NTT Software Corporation 27 * Hiroshi Kuribayashi OMRON Corporation 28 * 29 */ 30/* 31 32Copyright 1987,1998 The Open Group 33 34Permission to use, copy, modify, distribute, and sell this software and its 35documentation for any purpose is hereby granted without fee, provided that 36the above copyright notice appear in all copies and that both that 37copyright notice and this permission notice appear in supporting 38documentation. 39 40The above copyright notice and this permission notice shall be included 41in all copies or substantial portions of the Software. 42 43THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 44OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 45MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 46IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 47OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 48ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 49OTHER DEALINGS IN THE SOFTWARE. 50 51Except as contained in this notice, the name of The Open Group shall 52not be used in advertising or otherwise to promote the sale, use or 53other dealings in this Software without prior written authorization 54from The Open Group. 55 56*/ 57 58#ifdef HAVE_CONFIG_H 59#include <config.h> 60#endif 61#include "Xlibint.h" 62#include "Xlcint.h" 63#include <X11/Xlocale.h> 64#include <X11/Xos.h> 65#include "XlcPubI.h" 66 67#define MAXLOCALE 64 /* buffer size of locale name */ 68 69#ifdef X_LOCALE 70 71/* alternative setlocale() for when the OS does not provide one */ 72 73char * 74_Xsetlocale( 75 int category, 76 _Xconst char *name 77) 78{ 79 static char *xsl_name; 80 char *old_name; 81 XrmMethods methods; 82 XPointer state; 83 84 if (category != LC_CTYPE && category != LC_ALL) 85 return NULL; 86 if (!name) { 87 if (xsl_name) 88 return xsl_name; 89 return "C"; 90 } 91 if (!*name) 92 name = getenv("LC_CTYPE"); 93 if (!name || !*name) 94 name = getenv("LANG"); 95 if (name && strlen(name) >= MAXLOCALE) 96 name = NULL; 97 if (!name || !*name || !_XOpenLC((char *) name)) 98 name = "C"; 99 old_name = xsl_name; 100 xsl_name = (char *)name; 101 methods = _XrmInitParseInfo(&state); 102 xsl_name = old_name; 103 if (!methods) 104 return NULL; 105 name = (*methods->lcname)(state); 106 xsl_name = Xmalloc(strlen(name) + 1); 107 if (!xsl_name) { 108 xsl_name = old_name; 109 (*methods->destroy)(state); 110 return NULL; 111 } 112 strcpy(xsl_name, name); 113 if (old_name) 114 Xfree(old_name); 115 (*methods->destroy)(state); 116 return xsl_name; 117} 118 119#else /* X_LOCALE */ 120 121#if defined(__APPLE__) || defined(__CYGWIN__) 122char * 123_Xsetlocale( 124 int category, 125 _Xconst char *name 126) 127{ 128 return setlocale(category, name); 129} 130#endif /* __APPLE__ || __CYGWIN__ */ 131 132/* 133 * _XlcMapOSLocaleName is an implementation dependent routine that derives 134 * the LC_CTYPE locale name as used in the sample implementation from that 135 * returned by setlocale. 136 * 137 * Should match the code in Xt ExtractLocaleName. 138 * 139 * This function name is a bit of a misnomer. Even the siname parameter 140 * name is a misnomer. On most modern operating systems this function is 141 * a no-op, simply returning the osname; but on older operating systems 142 * like Ultrix, or HPUX 9.x and earlier, when you set LANG=german.88591 143 * then the string returned by setlocale(LC_ALL, "") will look something 144 * like: "german.88591 german.88591 ... german.88591". Then this function 145 * will pick out the LC_CTYPE component and return a pointer to that. 146 */ 147 148char * 149_XlcMapOSLocaleName( 150 char *osname, 151 char *siname) 152{ 153#if defined(hpux) || defined(CSRG_BASED) || defined(sun) || defined(SVR4) || defined(sgi) || defined(__osf__) || defined(AIXV3) || defined(ultrix) || defined(WIN32) || defined(__UNIXOS2__) || defined(linux) 154# ifdef hpux 155# ifndef _LastCategory 156 /* HPUX 9 and earlier */ 157# define SKIPCOUNT 2 158# define STARTCHAR ':' 159# define ENDCHAR ';' 160# else 161 /* HPUX 10 */ 162# define ENDCHAR ' ' 163# endif 164# else 165# ifdef ultrix 166# define SKIPCOUNT 2 167# define STARTCHAR '\001' 168# define ENDCHAR '\001' 169# else 170# if defined(WIN32) || defined(__UNIXOS2__) 171# define SKIPCOUNT 1 172# define STARTCHAR '=' 173# define ENDCHAR ';' 174# define WHITEFILL 175# else 176# if defined(__osf__) || (defined(AIXV3) && !defined(AIXV4)) 177# define STARTCHAR ' ' 178# define ENDCHAR ' ' 179# else 180# if defined(linux) 181# define STARTSTR "LC_CTYPE=" 182# define ENDCHAR ';' 183# else 184# if !defined(sun) || defined(SVR4) 185# define STARTCHAR '/' 186# define ENDCHAR '/' 187# endif 188# endif 189# endif 190# endif 191# endif 192# endif 193 194 char *start; 195 char *end; 196 int len; 197# ifdef SKIPCOUNT 198 int n; 199# endif 200 201 start = osname; 202# ifdef SKIPCOUNT 203 for (n = SKIPCOUNT; 204 --n >= 0 && start && (start = strchr (start, STARTCHAR)); 205 start++) 206 ; 207 if (!start) 208 start = osname; 209# endif 210# ifdef STARTCHAR 211 if (start && (start = strchr (start, STARTCHAR))) 212# elif defined (STARTSTR) 213 if (start && (start = strstr (start,STARTSTR))) 214# endif 215 { 216# ifdef STARTCHAR 217 start++; 218# elif defined (STARTSTR) 219 start += strlen(STARTSTR); 220# endif 221 if ((end = strchr (start, ENDCHAR))) { 222 len = end - start; 223 if (len >= MAXLOCALE) 224 len = MAXLOCALE - 1; 225 strncpy(siname, start, len); 226 *(siname + len) = '\0'; 227# ifdef WHITEFILL 228 for (start = siname; start = strchr(start, ' '); ) 229 *start++ = '-'; 230# endif 231 return siname; 232 } else /* if no ENDCHAR is found we are at the end of the line */ 233 return start; 234 } 235# ifdef WHITEFILL 236 if (strchr(osname, ' ')) { 237 len = strlen(osname); 238 if (len >= MAXLOCALE - 1) 239 len = MAXLOCALE - 1; 240 strncpy(siname, osname, len); 241 *(siname + len) = '\0'; 242 for (start = siname; start = strchr(start, ' '); ) 243 *start++ = '-'; 244 return siname; 245 } 246# endif 247# undef STARTCHAR 248# undef ENDCHAR 249# undef WHITEFILL 250#endif 251 return osname; 252} 253 254#endif /* X_LOCALE */ 255