SetLocale.c revision 0f8248bf
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 70#if defined(__APPLE__) || defined(__CYGWIN__) 71char * 72_Xsetlocale( 73 int category, 74 _Xconst char *name 75) 76{ 77 return setlocale(category, name); 78} 79#endif /* __APPLE__ || __CYGWIN__ */ 80 81/* 82 * _XlcMapOSLocaleName is an implementation dependent routine that derives 83 * the LC_CTYPE locale name as used in the sample implementation from that 84 * returned by setlocale. 85 * 86 * Should match the code in Xt ExtractLocaleName. 87 * 88 * This function name is a bit of a misnomer. Even the siname parameter 89 * name is a misnomer. On most modern operating systems this function is 90 * a no-op, simply returning the osname; but on older operating systems 91 * like Ultrix, or HPUX 9.x and earlier, when you set LANG=german.88591 92 * then the string returned by setlocale(LC_ALL, "") will look something 93 * like: "german.88591 german.88591 ... german.88591". Then this function 94 * will pick out the LC_CTYPE component and return a pointer to that. 95 */ 96 97char * 98_XlcMapOSLocaleName( 99 char *osname, 100 char *siname) 101{ 102#if defined(hpux) || defined(CSRG_BASED) || defined(sun) || defined(SVR4) || defined(sgi) || defined(__osf__) || defined(AIXV3) || defined(ultrix) || defined(WIN32) || defined(__UNIXOS2__) || defined(linux) 103# ifdef hpux 104# ifndef _LastCategory 105 /* HPUX 9 and earlier */ 106# define SKIPCOUNT 2 107# define STARTCHAR ':' 108# define ENDCHAR ';' 109# else 110 /* HPUX 10 */ 111# define ENDCHAR ' ' 112# endif 113# else 114# ifdef ultrix 115# define SKIPCOUNT 2 116# define STARTCHAR '\001' 117# define ENDCHAR '\001' 118# else 119# if defined(WIN32) || defined(__UNIXOS2__) 120# define SKIPCOUNT 1 121# define STARTCHAR '=' 122# define ENDCHAR ';' 123# define WHITEFILL 124# else 125# if defined(__osf__) || (defined(AIXV3) && !defined(AIXV4)) 126# define STARTCHAR ' ' 127# define ENDCHAR ' ' 128# else 129# if defined(linux) 130# define STARTSTR "LC_CTYPE=" 131# define ENDCHAR ';' 132# else 133# if !defined(sun) || defined(SVR4) 134# define STARTCHAR '/' 135# define ENDCHAR '/' 136# endif 137# endif 138# endif 139# endif 140# endif 141# endif 142 143 char *start; 144 char *end; 145 int len; 146# ifdef SKIPCOUNT 147 int n; 148# endif 149 150 start = osname; 151# ifdef SKIPCOUNT 152 for (n = SKIPCOUNT; 153 --n >= 0 && start && (start = strchr (start, STARTCHAR)); 154 start++) 155 ; 156 if (!start) 157 start = osname; 158# endif 159# ifdef STARTCHAR 160 if (start && (start = strchr (start, STARTCHAR))) 161# elif defined (STARTSTR) 162 if (start && (start = strstr (start,STARTSTR))) 163# endif 164 { 165# ifdef STARTCHAR 166 start++; 167# elif defined (STARTSTR) 168 start += strlen(STARTSTR); 169# endif 170 if ((end = strchr (start, ENDCHAR))) { 171 len = end - start; 172 if (len >= MAXLOCALE) 173 len = MAXLOCALE - 1; 174 strncpy(siname, start, len); 175 *(siname + len) = '\0'; 176# ifdef WHITEFILL 177 for (start = siname; start = strchr(start, ' '); ) 178 *start++ = '-'; 179# endif 180 return siname; 181 } else /* if no ENDCHAR is found we are at the end of the line */ 182 return start; 183 } 184# ifdef WHITEFILL 185 if (strchr(osname, ' ')) { 186 len = strlen(osname); 187 if (len >= MAXLOCALE - 1) 188 len = MAXLOCALE - 1; 189 strncpy(siname, osname, len); 190 *(siname + len) = '\0'; 191 for (start = siname; start = strchr(start, ' '); ) 192 *start++ = '-'; 193 return siname; 194 } 195# endif 196# undef STARTCHAR 197# undef ENDCHAR 198# undef WHITEFILL 199#endif 200 return osname; 201} 202 203