]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1995-2019 Free Software Foundation, Inc. |
2c6fe0bd | 2 | This file is part of the GNU C Library. |
4b10dd6c | 3 | Contributed by Ulrich Drepper <drepper@gnu.org>, 1995. |
19bc17a9 | 4 | |
43bc8ac6 | 5 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
6 | it under the terms of the GNU General Public License as published |
7 | by the Free Software Foundation; version 2 of the License, or | |
8 | (at your option) any later version. | |
19bc17a9 | 9 | |
43bc8ac6 | 10 | This program is distributed in the hope that it will be useful, |
2c6fe0bd | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. | |
19bc17a9 | 14 | |
43bc8ac6 | 15 | You should have received a copy of the GNU General Public License |
59ba27a6 | 16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
19bc17a9 RM |
17 | |
18 | #ifdef HAVE_CONFIG_H | |
19 | # include <config.h> | |
20 | #endif | |
21 | ||
22 | #include <langinfo.h> | |
23 | #include <string.h> | |
e054f494 | 24 | #include <stdint.h> |
4b10dd6c | 25 | #include <sys/uio.h> |
19bc17a9 | 26 | |
19bc17a9 RM |
27 | #include <assert.h> |
28 | ||
4b10dd6c | 29 | #include "localedef.h" |
f2b98f97 | 30 | #include "linereader.h" |
19bc17a9 | 31 | #include "localeinfo.h" |
4b10dd6c | 32 | #include "locfile.h" |
19bc17a9 RM |
33 | |
34 | ||
35 | /* The real definition of the struct for the LC_NUMERIC locale. */ | |
36 | struct locale_numeric_t | |
37 | { | |
38 | const char *decimal_point; | |
39 | const char *thousands_sep; | |
40 | char *grouping; | |
4b10dd6c | 41 | size_t grouping_len; |
4295702f UD |
42 | uint32_t decimal_point_wc; |
43 | uint32_t thousands_sep_wc; | |
19bc17a9 RM |
44 | }; |
45 | ||
46 | ||
4b10dd6c | 47 | static void |
19bc17a9 | 48 | numeric_startup (struct linereader *lr, struct localedef_t *locale, |
4b10dd6c | 49 | int ignore_content) |
19bc17a9 | 50 | { |
4b10dd6c UD |
51 | if (!ignore_content) |
52 | { | |
4295702f UD |
53 | locale->categories[LC_NUMERIC].numeric = |
54 | (struct locale_numeric_t *) xcalloc (1, | |
55 | sizeof (struct locale_numeric_t)); | |
4b10dd6c | 56 | } |
19bc17a9 | 57 | |
b9eb05d6 UD |
58 | if (lr != NULL) |
59 | { | |
60 | lr->translate_strings = 1; | |
0d54f746 | 61 | lr->return_widestr = 0; |
b9eb05d6 | 62 | } |
19bc17a9 RM |
63 | } |
64 | ||
65 | ||
66 | void | |
47e8b443 | 67 | numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap) |
19bc17a9 RM |
68 | { |
69 | struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric; | |
b9eb05d6 UD |
70 | int nothing = 0; |
71 | ||
72 | /* Now resolve copying and also handle completely missing definitions. */ | |
73 | if (numeric == NULL) | |
74 | { | |
75 | /* First see whether we were supposed to copy. If yes, find the | |
76 | actual definition. */ | |
77 | if (locale->copy_name[LC_NUMERIC] != NULL) | |
78 | { | |
79 | /* Find the copying locale. This has to happen transitively since | |
80 | the locale we are copying from might also copying another one. */ | |
81 | struct localedef_t *from = locale; | |
82 | ||
83 | do | |
84 | from = find_locale (LC_NUMERIC, from->copy_name[LC_NUMERIC], | |
85 | from->repertoire_name, charmap); | |
86 | while (from->categories[LC_NUMERIC].numeric == NULL | |
87 | && from->copy_name[LC_NUMERIC] != NULL); | |
88 | ||
89 | numeric = locale->categories[LC_NUMERIC].numeric | |
90 | = from->categories[LC_NUMERIC].numeric; | |
91 | } | |
92 | ||
93 | /* If there is still no definition issue an warning and create an | |
94 | empty one. */ | |
95 | if (numeric == NULL) | |
96 | { | |
f16491eb CD |
97 | record_warning (_("\ |
98 | No definition for %s category found"), "LC_NUMERIC"); | |
b9eb05d6 UD |
99 | numeric_startup (NULL, locale, 0); |
100 | numeric = locale->categories[LC_NUMERIC].numeric; | |
101 | nothing = 1; | |
102 | } | |
103 | } | |
19bc17a9 | 104 | |
19bc17a9 RM |
105 | /* The decimal point must not be empty. This is not said explicitly |
106 | in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be | |
107 | != "". */ | |
1ceb8afe UD |
108 | if (numeric->decimal_point == NULL) |
109 | { | |
f16491eb CD |
110 | if (! nothing) |
111 | record_error (0, 0, _("%s: field `%s' not defined"), | |
112 | "LC_NUMERIC", "decimal_point"); | |
1ceb8afe UD |
113 | numeric->decimal_point = "."; |
114 | } | |
f16491eb | 115 | else if (numeric->decimal_point[0] == '\0' && ! nothing) |
19bc17a9 | 116 | { |
f16491eb | 117 | record_error (0, 0, _("\ |
11bf311e | 118 | %s: value for field `%s' must not be an empty string"), |
f16491eb | 119 | "LC_NUMERIC", "decimal_point"); |
19bc17a9 | 120 | } |
6770573a UD |
121 | if (numeric->decimal_point_wc == L'\0') |
122 | numeric->decimal_point_wc = L'.'; | |
19bc17a9 | 123 | |
f16491eb CD |
124 | if (numeric->grouping_len == 0 && ! nothing) |
125 | record_error (0, 0, _("%s: field `%s' not defined"), | |
126 | "LC_NUMERIC", "grouping"); | |
19bc17a9 RM |
127 | } |
128 | ||
129 | ||
130 | void | |
47e8b443 | 131 | numeric_output (struct localedef_t *locale, const struct charmap_t *charmap, |
4b10dd6c | 132 | const char *output_path) |
19bc17a9 RM |
133 | { |
134 | struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric; | |
1ecbb381 RS |
135 | struct locale_file file; |
136 | ||
137 | init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)); | |
138 | add_locale_string (&file, numeric->decimal_point ?: ""); | |
139 | add_locale_string (&file, numeric->thousands_sep ?: ""); | |
140 | add_locale_raw_data (&file, numeric->grouping, numeric->grouping_len); | |
141 | add_locale_uint32 (&file, numeric->decimal_point_wc); | |
142 | add_locale_uint32 (&file, numeric->thousands_sep_wc); | |
143 | add_locale_string (&file, charmap->code_set_name); | |
144 | write_locale_data (output_path, LC_NUMERIC, "LC_NUMERIC", &file); | |
19bc17a9 RM |
145 | } |
146 | ||
147 | ||
4b10dd6c | 148 | /* The parser for the LC_NUMERIC section of the locale definition. */ |
19bc17a9 | 149 | void |
4b10dd6c | 150 | numeric_read (struct linereader *ldfile, struct localedef_t *result, |
47e8b443 | 151 | const struct charmap_t *charmap, const char *repertoire_name, |
4b10dd6c | 152 | int ignore_content) |
19bc17a9 | 153 | { |
0d54f746 | 154 | struct repertoire_t *repertoire = NULL; |
4b10dd6c UD |
155 | struct locale_numeric_t *numeric; |
156 | struct token *now; | |
157 | enum token_t nowtok; | |
19bc17a9 | 158 | |
0d54f746 UD |
159 | /* Get the repertoire we have to use. */ |
160 | if (repertoire_name != NULL) | |
161 | repertoire = repertoire_read (repertoire_name); | |
162 | ||
4b10dd6c UD |
163 | /* The rest of the line containing `LC_NUMERIC' must be free. */ |
164 | lr_ignore_rest (ldfile, 1); | |
165 | ||
166 | ||
167 | do | |
168 | { | |
47e8b443 | 169 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
170 | nowtok = now->tok; |
171 | } | |
172 | while (nowtok == tok_eol); | |
173 | ||
174 | /* If we see `copy' now we are almost done. */ | |
175 | if (nowtok == tok_copy) | |
176 | { | |
01ff9d0b | 177 | handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric, |
b9eb05d6 | 178 | LC_NUMERIC, "LC_NUMERIC", ignore_content); |
4b10dd6c UD |
179 | return; |
180 | } | |
181 | ||
182 | /* Prepare the data structures. */ | |
183 | numeric_startup (ldfile, result, ignore_content); | |
184 | numeric = result->categories[LC_NUMERIC].numeric; | |
185 | ||
186 | while (1) | |
19bc17a9 | 187 | { |
4b10dd6c UD |
188 | /* Of course we don't proceed beyond the end of file. */ |
189 | if (nowtok == tok_eof) | |
190 | break; | |
191 | ||
192 | /* Ingore empty lines. */ | |
193 | if (nowtok == tok_eol) | |
19bc17a9 | 194 | { |
47e8b443 | 195 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
196 | nowtok = now->tok; |
197 | continue; | |
19bc17a9 | 198 | } |
4b10dd6c UD |
199 | |
200 | switch (nowtok) | |
19bc17a9 | 201 | { |
4b10dd6c UD |
202 | #define STR_ELEM(cat) \ |
203 | case tok_##cat: \ | |
b9eb05d6 UD |
204 | /* Ignore the rest of the line if we don't need the input of \ |
205 | this line. */ \ | |
206 | if (ignore_content) \ | |
207 | { \ | |
208 | lr_ignore_rest (ldfile, 0); \ | |
209 | break; \ | |
210 | } \ | |
211 | \ | |
0d54f746 | 212 | ldfile->return_widestr = 1; \ |
47e8b443 | 213 | now = lr_token (ldfile, charmap, result, repertoire, verbose); \ |
4b10dd6c UD |
214 | if (now->tok != tok_string) \ |
215 | goto err_label; \ | |
216 | if (numeric->cat != NULL) \ | |
217 | lr_error (ldfile, _("\ | |
218 | %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \ | |
219 | else if (!ignore_content && now->val.str.startmb == NULL) \ | |
220 | { \ | |
221 | lr_error (ldfile, _("\ | |
222 | %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \ | |
223 | numeric->cat = ""; \ | |
4295702f UD |
224 | numeric->cat##_wc = L'\0'; \ |
225 | } \ | |
aec103d3 | 226 | else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \ |
4295702f UD |
227 | { \ |
228 | lr_error (ldfile, _("\ | |
229 | %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \ | |
4b10dd6c UD |
230 | } \ |
231 | else if (!ignore_content) \ | |
4295702f UD |
232 | { \ |
233 | numeric->cat = now->val.str.startmb; \ | |
234 | \ | |
235 | if (now->val.str.startwc != NULL) \ | |
236 | numeric->cat##_wc = *now->val.str.startwc; \ | |
237 | } \ | |
0d54f746 | 238 | ldfile->return_widestr = 0; \ |
4b10dd6c UD |
239 | break |
240 | ||
241 | STR_ELEM (decimal_point); | |
242 | STR_ELEM (thousands_sep); | |
243 | ||
244 | case tok_grouping: | |
b9eb05d6 UD |
245 | /* Ignore the rest of the line if we don't need the input of |
246 | this line. */ | |
247 | if (ignore_content) | |
248 | { | |
249 | lr_ignore_rest (ldfile, 0); | |
250 | break; | |
251 | } | |
252 | ||
47e8b443 | 253 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
254 | if (now->tok != tok_minus1 && now->tok != tok_number) |
255 | goto err_label; | |
256 | else | |
257 | { | |
258 | size_t act = 0; | |
259 | size_t max = 10; | |
cec40916 | 260 | char *grouping = xmalloc (max); |
4b10dd6c UD |
261 | |
262 | do | |
263 | { | |
264 | if (act + 1 >= max) | |
265 | { | |
266 | max *= 2; | |
267 | grouping = xrealloc (grouping, max); | |
268 | } | |
269 | ||
270 | if (act > 0 && grouping[act - 1] == '\177') | |
271 | { | |
272 | lr_error (ldfile, _("\ | |
273 | %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping"); | |
274 | lr_ignore_rest (ldfile, 0); | |
275 | break; | |
276 | } | |
277 | ||
278 | if (now->tok == tok_minus1) | |
cec40916 | 279 | grouping[act++] = '\177'; |
4b10dd6c UD |
280 | else if (now->val.num == 0) |
281 | { | |
282 | /* A value of 0 disables grouping from here on but | |
283 | we must not store a NUL character since this | |
284 | terminates the string. Use something different | |
285 | which must not be used otherwise. */ | |
cec40916 | 286 | grouping[act++] = '\377'; |
4b10dd6c UD |
287 | } |
288 | else if (now->val.num > 126) | |
289 | lr_error (ldfile, _("\ | |
290 | %s: values for field `%s' must be smaller than 127"), | |
291 | "LC_NUMERIC", "grouping"); | |
cec40916 | 292 | else |
4b10dd6c UD |
293 | grouping[act++] = now->val.num; |
294 | ||
295 | /* Next must be semicolon. */ | |
47e8b443 | 296 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
297 | if (now->tok != tok_semicolon) |
298 | break; | |
299 | ||
47e8b443 | 300 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
301 | } |
302 | while (now->tok == tok_minus1 || now->tok == tok_number); | |
303 | ||
304 | if (now->tok != tok_eol) | |
305 | goto err_label; | |
306 | ||
a7931fcf AS |
307 | /* A single -1 means no grouping. */ |
308 | if (act == 1 && grouping[0] == '\177') | |
309 | act--; | |
cec40916 | 310 | grouping[act++] = '\0'; |
4b10dd6c | 311 | |
cec40916 UD |
312 | numeric->grouping = xrealloc (grouping, act); |
313 | numeric->grouping_len = act; | |
4b10dd6c UD |
314 | } |
315 | break; | |
316 | ||
317 | case tok_end: | |
318 | /* Next we assume `LC_NUMERIC'. */ | |
47e8b443 | 319 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c UD |
320 | if (now->tok == tok_eof) |
321 | break; | |
322 | if (now->tok == tok_eol) | |
323 | lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC"); | |
324 | else if (now->tok != tok_lc_numeric) | |
325 | lr_error (ldfile, _("\ | |
326 | %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC"); | |
327 | lr_ignore_rest (ldfile, now->tok == tok_lc_numeric); | |
328 | return; | |
329 | ||
330 | default: | |
331 | err_label: | |
332 | SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC"); | |
19bc17a9 RM |
333 | } |
334 | ||
4b10dd6c | 335 | /* Prepare for the next round. */ |
47e8b443 | 336 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
4b10dd6c | 337 | nowtok = now->tok; |
19bc17a9 | 338 | } |
4b10dd6c UD |
339 | |
340 | /* When we come here we reached the end of the file. */ | |
341 | lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC"); | |
19bc17a9 | 342 | } |