Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / locale / nl_langinfo.c
1 /*-
2  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libc/locale/nl_langinfo.c,v 1.13.2.3 2002/03/12 17:23:24 phantom Exp $
27  */
28
29 #include <locale.h>
30 #include <langinfo.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "../stdtime/timelocal.h"
36 #include "lnumeric.h"
37 #include "lmonetary.h"
38 #include "lmessages.h"
39
40 #define TRANSITION_PERIOD_HACK
41
42 #define _REL(BASE) ((int)item-BASE)
43
44 char *
45 nl_langinfo(nl_item item) {
46
47    char *ret, *s, *cs;
48    static char *csym = NULL;
49 #ifdef TRANSITION_PERIOD_HACK
50    static char *cset = NULL;
51 #endif /* TRANSITION_PERIOD_HACK */
52
53    switch (item) {
54         case CODESET:
55                 ret = "";
56                 if ((s = setlocale(LC_CTYPE, NULL)) != NULL) {
57                         if ((cs = strchr(s, '.')) != NULL) {
58                                 ret = cs + 1;
59 #ifdef TRANSITION_PERIOD_HACK
60                                 if (strncmp(ret, "ISO_", 4) == 0) {
61                                         int slen = strlen(ret);
62
63                                         if ((cset = reallocf(cset, slen)) != NULL) {
64                                                 strcpy(cset, "ISO");
65                                                 strcat(cset, ret + 4);
66                                                 ret = cset;
67                                         } else
68                                                 ret = "";
69                                 } else if (strcmp(ret, "EUC") == 0) {
70                                         if (strncmp(s, "ja_JP", 5) == 0)
71                                                 ret = "eucJP";
72                                         else if (strncmp(s, "ko_KR", 5) == 0)
73                                                 ret = "eucKR";
74                                         else if (strncmp(s, "zh_CN", 5) == 0)
75                                                 ret = "eucCN";
76                                 } else if (strcmp(ret, "ASCII") == 0)
77                                         ret = "US-ASCII";
78 #endif /* TRANSITION_PERIOD_HACK */
79                         } else if (strcmp(s, "C") == 0 ||
80                                    strcmp(s, "POSIX") == 0
81 #ifdef TRANSITION_PERIOD_HACK
82                                    || strstr(s, "ASCII") != NULL
83 #endif /* TRANSITION_PERIOD_HACK */
84                                   )
85                                 ret = "US-ASCII";
86                 }
87                 break;
88         case D_T_FMT:
89                 ret = (char *) __get_current_time_locale()->c_fmt;
90                 break;
91         case D_FMT:
92                 ret = (char *) __get_current_time_locale()->x_fmt;
93                 break;
94         case T_FMT:
95                 ret = (char *) __get_current_time_locale()->X_fmt;
96                 break;
97         case T_FMT_AMPM:
98                 ret = (char *) __get_current_time_locale()->ampm_fmt;
99                 break;
100         case AM_STR:
101                 ret = (char *) __get_current_time_locale()->am;
102                 break;
103         case PM_STR:
104                 ret = (char *) __get_current_time_locale()->pm;
105                 break;
106         case DAY_1: case DAY_2: case DAY_3:
107         case DAY_4: case DAY_5: case DAY_6: case DAY_7:
108                 ret = (char*) __get_current_time_locale()->weekday[_REL(DAY_1)];
109                 break;
110         case ABDAY_1: case ABDAY_2: case ABDAY_3:
111         case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
112                 ret = (char*) __get_current_time_locale()->wday[_REL(ABDAY_1)];
113                 break;
114         case MON_1: case MON_2: case MON_3: case MON_4:
115         case MON_5: case MON_6: case MON_7: case MON_8:
116         case MON_9: case MON_10: case MON_11: case MON_12:
117                 ret = (char*) __get_current_time_locale()->month[_REL(MON_1)];
118                 break;
119         case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
120         case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
121         case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
122                 ret = (char*) __get_current_time_locale()->mon[_REL(ABMON_1)];
123                 break;
124         case ERA:
125                 /* XXX: need to be implemented  */
126                 ret = "";
127                 break;
128         case ERA_D_FMT:
129                 /* XXX: need to be implemented  */
130                 ret = "";
131                 break;
132         case ERA_D_T_FMT:
133                 /* XXX: need to be implemented  */
134                 ret = "";
135                 break;
136         case ERA_T_FMT:
137                 /* XXX: need to be implemented  */
138                 ret = "";
139                 break;
140         case ALT_DIGITS:
141                 /* XXX: need to be implemented  */
142                 ret = "";
143                 break;
144         case RADIXCHAR:
145                 ret = (char*) __get_current_numeric_locale()->decimal_point;
146                 break;
147         case THOUSEP:
148                 ret = (char*) __get_current_numeric_locale()->thousands_sep;
149                 break;
150         case YESEXPR:
151                 ret = (char*) __get_current_messages_locale()->yesexpr;
152                 break;
153         case NOEXPR:
154                 ret = (char*) __get_current_messages_locale()->noexpr;
155                 break;
156         /*
157          * All items marked with LEGACY are available, but not recomended
158          * by SUSv2 to be used in portable applications since they're subject
159          * to remove in future specification editions
160          */
161         case YESSTR:            /* LEGACY  */
162                 ret = (char*) __get_current_messages_locale()->yesstr;
163                 break;
164         case NOSTR:             /* LEGACY  */
165                 ret = (char*) __get_current_messages_locale()->nostr;
166                 break;
167         case CRNCYSTR:
168                 ret = "";
169                 cs = (char*) __get_current_monetary_locale()->currency_symbol;
170                 if (*cs != '\0') {
171                         char pos = localeconv()->p_cs_precedes;
172
173                         if (pos == localeconv()->n_cs_precedes) {
174                                 char psn = '\0';
175
176                                 if (pos == CHAR_MAX) {
177                                         if (strcmp(cs, __get_current_monetary_locale()->mon_decimal_point) == 0)
178                                                 psn = '.';
179                                 } else
180                                         psn = pos ? '-' : '+';
181                                 if (psn != '\0') {
182                                         int clen = strlen(cs);
183
184                                         if ((csym = reallocf(csym, clen + 2)) != NULL) {
185                                                 *csym = psn;
186                                                 strcpy(csym + 1, cs);
187                                                 ret = csym;
188                                         }
189                                 }
190                         }
191                 }
192                 break;
193         case D_MD_ORDER:        /* local extension */
194                 ret = (char *) __get_current_time_locale()->md_order;
195                 break;
196         default:
197                 ret = "";
198    }
199    return (ret);
200 }