]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-numeric.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / locale / programs / ld-numeric.c
1 /* Copyright (C) 1995-1999, 2000, 2001, 2002 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 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.
9
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.
14
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. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <langinfo.h>
25 #include <string.h>
26 #include <sys/uio.h>
27
28 #include <assert.h>
29
30 #include "localedef.h"
31 #include "linereader.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
34
35
36 /* The real definition of the struct for the LC_NUMERIC locale. */
37 struct locale_numeric_t
38 {
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;
45 };
46
47
48 static void
49 numeric_startup (struct linereader *lr, struct localedef_t *locale,
50 int ignore_content)
51 {
52 if (!ignore_content)
53 {
54 locale->categories[LC_NUMERIC].numeric =
55 (struct locale_numeric_t *) xcalloc (1,
56 sizeof (struct locale_numeric_t));
57 }
58
59 if (lr != NULL)
60 {
61 lr->translate_strings = 1;
62 lr->return_widestr = 0;
63 }
64 }
65
66
67 void
68 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
69 {
70 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
71 int nothing = 0;
72
73 /* Now resolve copying and also handle completely missing definitions. */
74 if (numeric == NULL)
75 {
76 /* First see whether we were supposed to copy. If yes, find the
77 actual definition. */
78 if (locale->copy_name[LC_NUMERIC] != NULL)
79 {
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;
83
84 do
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);
89
90 numeric = locale->categories[LC_NUMERIC].numeric
91 = from->categories[LC_NUMERIC].numeric;
92 }
93
94 /* If there is still no definition issue an warning and create an
95 empty one. */
96 if (numeric == NULL)
97 {
98 if (! be_quiet)
99 WITH_CUR_LOCALE (error (0, 0, _("\
100 No definition for %s category found"), "LC_NUMERIC"));
101 numeric_startup (NULL, locale, 0);
102 numeric = locale->categories[LC_NUMERIC].numeric;
103 nothing = 1;
104 }
105 }
106
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)
111 {
112 if (! be_quiet && ! nothing)
113 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
114 "LC_NUMERIC", "decimal_point"));
115 numeric->decimal_point = ".";
116 }
117 else if (numeric->decimal_point[0] == '\0' && ! be_quiet && ! nothing)
118 {
119 WITH_CUR_LOCALE (error (0, 0, _("\
120 %s: value for field `%s' must not be the empty string"),
121 "LC_NUMERIC", "decimal_point"));
122 }
123 if (numeric->decimal_point_wc == L'\0')
124 numeric->decimal_point_wc = L'.';
125
126 if (numeric->grouping_len == 0 && ! be_quiet && ! nothing)
127 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
128 "LC_NUMERIC", "grouping"));
129 }
130
131
132 void
133 numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
134 const char *output_path)
135 {
136 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
137 struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
138 struct locale_file data;
139 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
140 size_t cnt = 0;
141
142 data.magic = LIMAGIC (LC_NUMERIC);
143 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC);
144 iov[cnt].iov_base = (void *) &data;
145 iov[cnt].iov_len = sizeof (data);
146 ++cnt;
147
148 iov[cnt].iov_base = (void *) idx;
149 iov[cnt].iov_len = sizeof (idx);
150 ++cnt;
151
152 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
153 iov[cnt].iov_base = (void *) (numeric->decimal_point ?: "");
154 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
155 ++cnt;
156
157 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
158 iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: "");
159 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
160 ++cnt;
161
162 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
163 iov[cnt].iov_base = numeric->grouping;
164 iov[cnt].iov_len = numeric->grouping_len;
165 ++cnt;
166
167 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
168
169 /* Align following data */
170 iov[cnt].iov_base = (void *) "\0\0";
171 iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
172 idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
173 ++cnt;
174
175 iov[cnt].iov_base = (void *) &numeric->decimal_point_wc;
176 iov[cnt].iov_len = sizeof (uint32_t);
177 ++cnt;
178
179 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
180 iov[cnt].iov_base = (void *) &numeric->thousands_sep_wc;
181 iov[cnt].iov_len = sizeof (uint32_t);
182 ++cnt;
183
184 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
185 iov[cnt].iov_base = (void *) charmap->code_set_name;
186 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
187
188 assert (cnt + 1 == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
189
190 write_locale_data (output_path, LC_NUMERIC, "LC_NUMERIC",
191 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov);
192 }
193
194
195 /* The parser for the LC_NUMERIC section of the locale definition. */
196 void
197 numeric_read (struct linereader *ldfile, struct localedef_t *result,
198 const struct charmap_t *charmap, const char *repertoire_name,
199 int ignore_content)
200 {
201 struct repertoire_t *repertoire = NULL;
202 struct locale_numeric_t *numeric;
203 struct token *now;
204 enum token_t nowtok;
205
206 /* Get the repertoire we have to use. */
207 if (repertoire_name != NULL)
208 repertoire = repertoire_read (repertoire_name);
209
210 /* The rest of the line containing `LC_NUMERIC' must be free. */
211 lr_ignore_rest (ldfile, 1);
212
213
214 do
215 {
216 now = lr_token (ldfile, charmap, result, NULL, verbose);
217 nowtok = now->tok;
218 }
219 while (nowtok == tok_eol);
220
221 /* If we see `copy' now we are almost done. */
222 if (nowtok == tok_copy)
223 {
224 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
225 LC_NUMERIC, "LC_NUMERIC", ignore_content);
226 return;
227 }
228
229 /* Prepare the data structures. */
230 numeric_startup (ldfile, result, ignore_content);
231 numeric = result->categories[LC_NUMERIC].numeric;
232
233 while (1)
234 {
235 /* Of course we don't proceed beyond the end of file. */
236 if (nowtok == tok_eof)
237 break;
238
239 /* Ingore empty lines. */
240 if (nowtok == tok_eol)
241 {
242 now = lr_token (ldfile, charmap, result, NULL, verbose);
243 nowtok = now->tok;
244 continue;
245 }
246
247 switch (nowtok)
248 {
249 #define STR_ELEM(cat) \
250 case tok_##cat: \
251 /* Ignore the rest of the line if we don't need the input of \
252 this line. */ \
253 if (ignore_content) \
254 { \
255 lr_ignore_rest (ldfile, 0); \
256 break; \
257 } \
258 \
259 ldfile->return_widestr = 1; \
260 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
261 if (now->tok != tok_string) \
262 goto err_label; \
263 if (numeric->cat != NULL) \
264 lr_error (ldfile, _("\
265 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
266 else if (!ignore_content && now->val.str.startmb == NULL) \
267 { \
268 lr_error (ldfile, _("\
269 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
270 numeric->cat = ""; \
271 numeric->cat##_wc = L'\0'; \
272 } \
273 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
274 { \
275 lr_error (ldfile, _("\
276 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
277 } \
278 else if (!ignore_content) \
279 { \
280 numeric->cat = now->val.str.startmb; \
281 \
282 if (now->val.str.startwc != NULL) \
283 numeric->cat##_wc = *now->val.str.startwc; \
284 } \
285 ldfile->return_widestr = 0; \
286 break
287
288 STR_ELEM (decimal_point);
289 STR_ELEM (thousands_sep);
290
291 case tok_grouping:
292 /* Ignore the rest of the line if we don't need the input of
293 this line. */
294 if (ignore_content)
295 {
296 lr_ignore_rest (ldfile, 0);
297 break;
298 }
299
300 now = lr_token (ldfile, charmap, result, NULL, verbose);
301 if (now->tok != tok_minus1 && now->tok != tok_number)
302 goto err_label;
303 else
304 {
305 size_t act = 0;
306 size_t max = 10;
307 char *grouping = ignore_content ? NULL : xmalloc (max);
308
309 do
310 {
311 if (act + 1 >= max)
312 {
313 max *= 2;
314 grouping = xrealloc (grouping, max);
315 }
316
317 if (act > 0 && grouping[act - 1] == '\177')
318 {
319 lr_error (ldfile, _("\
320 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
321 lr_ignore_rest (ldfile, 0);
322 break;
323 }
324
325 if (now->tok == tok_minus1)
326 {
327 if (!ignore_content)
328 grouping[act++] = '\177';
329 }
330 else if (now->val.num == 0)
331 {
332 /* A value of 0 disables grouping from here on but
333 we must not store a NUL character since this
334 terminates the string. Use something different
335 which must not be used otherwise. */
336 if (!ignore_content)
337 grouping[act++] = '\377';
338 }
339 else if (now->val.num > 126)
340 lr_error (ldfile, _("\
341 %s: values for field `%s' must be smaller than 127"),
342 "LC_NUMERIC", "grouping");
343 else if (!ignore_content)
344 grouping[act++] = now->val.num;
345
346 /* Next must be semicolon. */
347 now = lr_token (ldfile, charmap, result, NULL, verbose);
348 if (now->tok != tok_semicolon)
349 break;
350
351 now = lr_token (ldfile, charmap, result, NULL, verbose);
352 }
353 while (now->tok == tok_minus1 || now->tok == tok_number);
354
355 if (now->tok != tok_eol)
356 goto err_label;
357
358 if (!ignore_content)
359 {
360 grouping[act++] = '\0';
361
362 numeric->grouping = xrealloc (grouping, act);
363 numeric->grouping_len = act;
364 }
365 }
366 break;
367
368 case tok_end:
369 /* Next we assume `LC_NUMERIC'. */
370 now = lr_token (ldfile, charmap, result, NULL, verbose);
371 if (now->tok == tok_eof)
372 break;
373 if (now->tok == tok_eol)
374 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
375 else if (now->tok != tok_lc_numeric)
376 lr_error (ldfile, _("\
377 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
378 lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
379 return;
380
381 default:
382 err_label:
383 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
384 }
385
386 /* Prepare for the next round. */
387 now = lr_token (ldfile, charmap, result, NULL, verbose);
388 nowtok = now->tok;
389 }
390
391 /* When we come here we reached the end of the file. */
392 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");
393 }