* nscd/nscd_helper.c (get_mapping): Use MSG_CMSG_CLOEXEC for
[glibc.git] / locale / programs / ld-numeric.c
blobb1a89e262b027b8212fc70de7680d9aa5d129da6
1 /* Copyright (C) 1995-2002,2005,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <langinfo.h>
24 #include <string.h>
25 #include <sys/uio.h>
27 #include <assert.h>
29 #include "localedef.h"
30 #include "linereader.h"
31 #include "localeinfo.h"
32 #include "locfile.h"
35 /* The real definition of the struct for the LC_NUMERIC locale. */
36 struct locale_numeric_t
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;
47 static void
48 numeric_startup (struct linereader *lr, struct localedef_t *locale,
49 int ignore_content)
51 if (!ignore_content)
53 locale->categories[LC_NUMERIC].numeric =
54 (struct locale_numeric_t *) xcalloc (1,
55 sizeof (struct locale_numeric_t));
58 if (lr != NULL)
60 lr->translate_strings = 1;
61 lr->return_widestr = 0;
66 void
67 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
69 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
70 int nothing = 0;
72 /* Now resolve copying and also handle completely missing definitions. */
73 if (numeric == NULL)
75 /* First see whether we were supposed to copy. If yes, find the
76 actual definition. */
77 if (locale->copy_name[LC_NUMERIC] != NULL)
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;
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);
89 numeric = locale->categories[LC_NUMERIC].numeric
90 = from->categories[LC_NUMERIC].numeric;
93 /* If there is still no definition issue an warning and create an
94 empty one. */
95 if (numeric == NULL)
97 if (! be_quiet)
98 WITH_CUR_LOCALE (error (0, 0, _("\
99 No definition for %s category found"), "LC_NUMERIC"));
100 numeric_startup (NULL, locale, 0);
101 numeric = locale->categories[LC_NUMERIC].numeric;
102 nothing = 1;
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)
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 = ".";
116 else if (numeric->decimal_point[0] == '\0' && ! be_quiet && ! nothing)
118 WITH_CUR_LOCALE (error (0, 0, _("\
119 %s: value for field `%s' must not be an empty string"),
120 "LC_NUMERIC", "decimal_point"));
122 if (numeric->decimal_point_wc == L'\0')
123 numeric->decimal_point_wc = L'.';
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"));
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, "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 = 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)
325 grouping[act++] = '\177';
326 else if (now->val.num == 0)
328 /* A value of 0 disables grouping from here on but
329 we must not store a NUL character since this
330 terminates the string. Use something different
331 which must not be used otherwise. */
332 grouping[act++] = '\377';
334 else if (now->val.num > 126)
335 lr_error (ldfile, _("\
336 %s: values for field `%s' must be smaller than 127"),
337 "LC_NUMERIC", "grouping");
338 else
339 grouping[act++] = now->val.num;
341 /* Next must be semicolon. */
342 now = lr_token (ldfile, charmap, result, NULL, verbose);
343 if (now->tok != tok_semicolon)
344 break;
346 now = lr_token (ldfile, charmap, result, NULL, verbose);
348 while (now->tok == tok_minus1 || now->tok == tok_number);
350 if (now->tok != tok_eol)
351 goto err_label;
353 grouping[act++] = '\0';
355 numeric->grouping = xrealloc (grouping, act);
356 numeric->grouping_len = act;
358 break;
360 case tok_end:
361 /* Next we assume `LC_NUMERIC'. */
362 now = lr_token (ldfile, charmap, result, NULL, verbose);
363 if (now->tok == tok_eof)
364 break;
365 if (now->tok == tok_eol)
366 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
367 else if (now->tok != tok_lc_numeric)
368 lr_error (ldfile, _("\
369 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
370 lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
371 return;
373 default:
374 err_label:
375 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
378 /* Prepare for the next round. */
379 now = lr_token (ldfile, charmap, result, NULL, verbose);
380 nowtok = now->tok;
383 /* When we come here we reached the end of the file. */
384 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");