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