Update.
[glibc.git] / locale / programs / ld-numeric.c
blobc0be8271330f44ffdf016ddc72bf3b750f31cab7
1 /* Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <langinfo.h>
25 #include <string.h>
26 #include <sys/uio.h>
28 #include <assert.h>
30 #include "linereader.h"
31 #include "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
36 /* The real definition of the struct for the LC_NUMERIC locale. */
37 struct locale_numeric_t
39 const char *decimal_point;
40 const char *thousands_sep;
41 char *grouping;
42 size_t grouping_len;
43 uint32_t decimal_point_wc;
44 uint32_t thousands_sep_wc;
48 static void
49 numeric_startup (struct linereader *lr, struct localedef_t *locale,
50 int ignore_content)
52 if (!ignore_content)
54 locale->categories[LC_NUMERIC].numeric =
55 (struct locale_numeric_t *) xcalloc (1,
56 sizeof (struct locale_numeric_t));
59 if (lr != NULL)
61 lr->translate_strings = 1;
62 lr->return_widestr = 0;
67 void
68 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
70 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
71 int nothing = 0;
73 /* Now resolve copying and also handle completely missing definitions. */
74 if (numeric == NULL)
76 /* First see whether we were supposed to copy. If yes, find the
77 actual definition. */
78 if (locale->copy_name[LC_NUMERIC] != NULL)
80 /* Find the copying locale. This has to happen transitively since
81 the locale we are copying from might also copying another one. */
82 struct localedef_t *from = locale;
85 from = find_locale (LC_NUMERIC, from->copy_name[LC_NUMERIC],
86 from->repertoire_name, charmap);
87 while (from->categories[LC_NUMERIC].numeric == NULL
88 && from->copy_name[LC_NUMERIC] != NULL);
90 numeric = locale->categories[LC_NUMERIC].numeric
91 = from->categories[LC_NUMERIC].numeric;
94 /* If there is still no definition issue an warning and create an
95 empty one. */
96 if (numeric == NULL)
98 if (! be_quiet)
99 error (0, 0, _("No definition for %s category found"),
100 "LC_NUMERIC");
101 numeric_startup (NULL, locale, 0);
102 numeric = locale->categories[LC_NUMERIC].numeric;
103 nothing = 1;
107 /* The decimal point must not be empty. This is not said explicitly
108 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
109 != "". */
110 if (numeric->decimal_point == NULL)
112 if (! be_quiet && ! nothing)
113 error (0, 0, _("%s: field `%s' not defined"),
114 "LC_NUMERIC", "decimal_point");
115 numeric->decimal_point = ".";
117 else if (numeric->decimal_point[0] == '\0' && ! be_quiet && ! nothing)
119 error (0, 0, _("\
120 %s: value for field `%s' must not be the empty string"),
121 "LC_NUMERIC", "decimal_point");
123 if (numeric->decimal_point_wc == L'\0')
124 numeric->decimal_point_wc = L'.';
126 if (numeric->grouping_len == 0 && ! be_quiet && ! nothing)
127 error (0, 0, _("%s: field `%s' not defined"), "LC_NUMERIC", "grouping");
131 void
132 numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
133 const char *output_path)
135 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
136 struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
137 struct locale_file data;
138 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
139 size_t cnt = 0;
141 data.magic = LIMAGIC (LC_NUMERIC);
142 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC);
143 iov[cnt].iov_base = (void *) &data;
144 iov[cnt].iov_len = sizeof (data);
145 ++cnt;
147 iov[cnt].iov_base = (void *) idx;
148 iov[cnt].iov_len = sizeof (idx);
149 ++cnt;
151 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
152 iov[cnt].iov_base = (void *) (numeric->decimal_point ?: "");
153 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
154 ++cnt;
156 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
157 iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: "");
158 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
159 ++cnt;
161 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
162 iov[cnt].iov_base = numeric->grouping;
163 iov[cnt].iov_len = numeric->grouping_len;
164 ++cnt;
166 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
168 /* Align following data */
169 iov[cnt].iov_base = (void *) "\0\0";
170 iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
171 idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
172 ++cnt;
174 iov[cnt].iov_base = (void *) &numeric->decimal_point_wc;
175 iov[cnt].iov_len = sizeof (uint32_t);
176 ++cnt;
178 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
179 iov[cnt].iov_base = (void *) &numeric->thousands_sep_wc;
180 iov[cnt].iov_len = sizeof (uint32_t);
181 ++cnt;
183 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
184 iov[cnt].iov_base = (void *) charmap->code_set_name;
185 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
187 assert (cnt + 1 == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
189 write_locale_data (output_path, "LC_NUMERIC",
190 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov);
194 /* The parser for the LC_NUMERIC section of the locale definition. */
195 void
196 numeric_read (struct linereader *ldfile, struct localedef_t *result,
197 const struct charmap_t *charmap, const char *repertoire_name,
198 int ignore_content)
200 struct repertoire_t *repertoire = NULL;
201 struct locale_numeric_t *numeric;
202 struct token *now;
203 enum token_t nowtok;
205 /* Get the repertoire we have to use. */
206 if (repertoire_name != NULL)
207 repertoire = repertoire_read (repertoire_name);
209 /* The rest of the line containing `LC_NUMERIC' must be free. */
210 lr_ignore_rest (ldfile, 1);
215 now = lr_token (ldfile, charmap, result, NULL, verbose);
216 nowtok = now->tok;
218 while (nowtok == tok_eol);
220 /* If we see `copy' now we are almost done. */
221 if (nowtok == tok_copy)
223 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
224 LC_NUMERIC, "LC_NUMERIC", ignore_content);
225 return;
228 /* Prepare the data structures. */
229 numeric_startup (ldfile, result, ignore_content);
230 numeric = result->categories[LC_NUMERIC].numeric;
232 while (1)
234 /* Of course we don't proceed beyond the end of file. */
235 if (nowtok == tok_eof)
236 break;
238 /* Ingore empty lines. */
239 if (nowtok == tok_eol)
241 now = lr_token (ldfile, charmap, result, NULL, verbose);
242 nowtok = now->tok;
243 continue;
246 switch (nowtok)
248 #define STR_ELEM(cat) \
249 case tok_##cat: \
250 /* Ignore the rest of the line if we don't need the input of \
251 this line. */ \
252 if (ignore_content) \
254 lr_ignore_rest (ldfile, 0); \
255 break; \
258 ldfile->return_widestr = 1; \
259 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
260 if (now->tok != tok_string) \
261 goto err_label; \
262 if (numeric->cat != NULL) \
263 lr_error (ldfile, _("\
264 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
265 else if (!ignore_content && now->val.str.startmb == NULL) \
267 lr_error (ldfile, _("\
268 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
269 numeric->cat = ""; \
270 numeric->cat##_wc = L'\0'; \
272 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
274 lr_error (ldfile, _("\
275 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
277 else if (!ignore_content) \
279 numeric->cat = now->val.str.startmb; \
281 if (now->val.str.startwc != NULL) \
282 numeric->cat##_wc = *now->val.str.startwc; \
284 ldfile->return_widestr = 0; \
285 break
287 STR_ELEM (decimal_point);
288 STR_ELEM (thousands_sep);
290 case tok_grouping:
291 /* Ignore the rest of the line if we don't need the input of
292 this line. */
293 if (ignore_content)
295 lr_ignore_rest (ldfile, 0);
296 break;
299 now = lr_token (ldfile, charmap, result, NULL, verbose);
300 if (now->tok != tok_minus1 && now->tok != tok_number)
301 goto err_label;
302 else
304 size_t act = 0;
305 size_t max = 10;
306 char *grouping = ignore_content ? NULL : xmalloc (max);
310 if (act + 1 >= max)
312 max *= 2;
313 grouping = xrealloc (grouping, max);
316 if (act > 0 && grouping[act - 1] == '\177')
318 lr_error (ldfile, _("\
319 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
320 lr_ignore_rest (ldfile, 0);
321 break;
324 if (now->tok == tok_minus1)
326 if (!ignore_content)
327 grouping[act++] = '\177';
329 else if (now->val.num == 0)
331 /* A value of 0 disables grouping from here on but
332 we must not store a NUL character since this
333 terminates the string. Use something different
334 which must not be used otherwise. */
335 if (!ignore_content)
336 grouping[act++] = '\377';
338 else if (now->val.num > 126)
339 lr_error (ldfile, _("\
340 %s: values for field `%s' must be smaller than 127"),
341 "LC_NUMERIC", "grouping");
342 else if (!ignore_content)
343 grouping[act++] = now->val.num;
345 /* Next must be semicolon. */
346 now = lr_token (ldfile, charmap, result, NULL, verbose);
347 if (now->tok != tok_semicolon)
348 break;
350 now = lr_token (ldfile, charmap, result, NULL, verbose);
352 while (now->tok == tok_minus1 || now->tok == tok_number);
354 if (now->tok != tok_eol)
355 goto err_label;
357 if (!ignore_content)
359 grouping[act++] = '\0';
361 numeric->grouping = xrealloc (grouping, act);
362 numeric->grouping_len = act;
365 break;
367 case tok_end:
368 /* Next we assume `LC_NUMERIC'. */
369 now = lr_token (ldfile, charmap, result, NULL, verbose);
370 if (now->tok == tok_eof)
371 break;
372 if (now->tok == tok_eol)
373 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
374 else if (now->tok != tok_lc_numeric)
375 lr_error (ldfile, _("\
376 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
377 lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
378 return;
380 default:
381 err_label:
382 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
385 /* Prepare for the next round. */
386 now = lr_token (ldfile, charmap, result, NULL, verbose);
387 nowtok = now->tok;
390 /* When we come here we reached the end of the file. */
391 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");