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