Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtudcfonted / libfal / _falSetLocale.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* SetLocale.c 1.1 - Fujitsu source for CDEnext    95/11/06 20:31:29    */ 
24 /* $XConsortium: _falSetLocale.c /main/1 1996/04/08 15:15:08 cde-fuj $ */
25
26 /*
27  * Copyright 1990, 1991 by OMRON Corporation, NTT Software Corporation,
28  *                      and Nippon Telegraph and Telephone Corporation
29  *
30  * Permission to use, copy, modify, distribute, and sell this software and its
31  * documentation for any purpose is hereby granted without fee, provided that
32  * the above copyright notice appear in all copies and that both that
33  * copyright notice and this permission notice appear in supporting
34  * documentation, and that the names of OMRON, NTT Software, and NTT
35  * not be used in advertising or publicity pertaining to distribution of the
36  * software without specific, written prior permission. OMRON, NTT Software,
37  * and NTT make no representations about the suitability of this
38  * software for any purpose.  It is provided "as is" without express or
39  * implied warranty.
40  *
41  * OMRON, NTT SOFTWARE, AND NTT, DISCLAIM ALL WARRANTIES WITH REGARD
42  * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
43  * AND FITNESS, IN NO EVENT SHALL OMRON, NTT SOFTWARE, OR NTT, BE
44  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
45  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48  *
49  *      Authors: Li Yuhong              OMRON Corporation
50  *               Tetsuya Kato           NTT Software Corporation
51  *               Hiroshi Kuribayashi    OMRON Corporation
52  *   
53  */
54 /*
55
56 Copyright (c) 1987  X Consortium
57
58 Permission is hereby granted, free of charge, to any person obtaining
59 a copy of this software and associated documentation files (the
60 "Software"), to deal in the Software without restriction, including
61 without limitation the rights to use, copy, modify, merge, publish,
62 distribute, sublicense, and/or sell copies of the Software, and to
63 permit persons to whom the Software is furnished to do so, subject to
64 the following conditions:
65
66 The above copyright notice and this permission notice shall be included
67 in all copies or substantial portions of the Software.
68
69 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
70 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
71 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
72 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
73 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
74 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
75 OTHER DEALINGS IN THE SOFTWARE.
76
77 Except as contained in this notice, the name of the X Consortium shall
78 not be used in advertising or otherwise to promote the sale, use or
79 other dealings in this Software without prior written authorization
80 from the X Consortium.
81
82 */
83
84 #include "_fallibint.h"
85 #include "_fallcint.h"
86 #include <X11/Xlocale.h>
87 #include <X11/Xos.h>
88
89 #ifdef X_LOCALE
90
91 /* alternative setlocale() for when the OS does not provide one */
92
93 #ifdef X_NOT_STDC_ENV
94 extern char *getenv();
95 #endif
96
97 #define MAXLOCALE       64      /* buffer size of locale name */
98
99 #if NeedFunctionPrototypes
100 char *
101 _falsetlocale(
102     int           category,
103     _Xconst char *name
104 )
105 #else
106 char *
107 _falsetlocale(category, name)
108     int         category;
109     char       *name;
110 #endif
111 {
112     static char *xsl_name;
113     char *old_name;
114     XrmMethods methods;
115     XPointer state;
116
117     if (category != LC_CTYPE && category != LC_ALL)
118         return NULL;
119     if (!name) {
120         if (xsl_name)
121             return xsl_name;
122         return "C";
123     }
124     if (!*name)
125         name = getenv("LC_CTYPE");
126     if (!name || !*name)
127         name = getenv("LANG");
128     if (!name || !*name)
129         name = "C";
130     old_name = xsl_name;
131     xsl_name = (char *)name;
132     methods = _falrmInitParseInfo(&state);
133     xsl_name = old_name;
134     if (!methods)
135         return NULL;
136     name = (*methods->lcname)(state);
137     xsl_name = Xmalloc(strlen(name) + 1);
138     if (!xsl_name) {
139         xsl_name = old_name;
140         (*methods->destroy)(state);
141         return NULL;
142     }
143     strcpy(xsl_name, name);
144     if (old_name)
145         Xfree(old_name);
146     (*methods->destroy)(state);
147     return xsl_name;
148 }
149
150 #else /* X_LOCALE */
151
152 /*
153  * _fallcMapOSLocaleName is an implementation dependent routine that derives
154  * the LC_CTYPE locale name as used in the sample implementation from that
155  * returned by setlocale.
156  * Should match the code in Xt ExtractLocaleName.
157  */
158
159 char *
160 _fallcMapOSLocaleName(osname, siname)
161     char *osname;
162     char *siname;
163 {
164 #if defined(hpux) || defined(CSRG_BASED) || defined(sun) || defined(SVR4) || defined(sgi) || defined(__osf__) || defined(AIXV3) || defined(ultrix) || defined(WIN32)
165 #ifdef hpux
166 #define SKIPCOUNT 2
167 #define STARTCHAR ':'
168 #define ENDCHAR ';'
169 #else
170 #ifdef ultrix
171 #define SKIPCOUNT 2
172 #define STARTCHAR '\001'
173 #define ENDCHAR '\001'
174 #else
175 #ifdef WIN32
176 #define SKIPCOUNT 1
177 #define STARTCHAR '='
178 #define ENDCHAR ';'
179 #define WHITEFILL
180 #else
181 #if defined(__osf__) || defined(AIXV3)
182 #define STARTCHAR ' '
183 #define ENDCHAR ' '
184 #else
185 #if !defined(sun) || defined(SVR4)
186 #define STARTCHAR '/'
187 #endif
188 #define ENDCHAR '/'
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         start++;
213 #endif
214         if (end = strchr (start, ENDCHAR)) {
215             len = end - start;
216             strncpy(siname, start, len);
217             *(siname + len) = '\0';
218 #ifdef WHITEFILL
219             for (start = siname; start = strchr(start, ' '); )
220                 *start++ = '-';
221 #endif
222             return siname;
223 #ifdef STARTCHAR
224         }
225 #endif
226     }
227 #ifdef WHITEFILL
228     if (strchr(osname, ' ')) {
229         strcpy(siname, osname);
230         for (start = siname; start = strchr(start, ' '); )
231             *start++ = '-';
232         return siname;
233     }
234 #endif
235 #undef STARTCHAR
236 #undef ENDCHAR
237 #undef WHITEFILL
238 #endif
239     return osname;
240 }
241
242 #endif  /* X_LOCALE */