powerpc64: Fix by using the configure value $libc_cv_cc_submachine [BZ #31629]
[glibc.git] / locale / programs / ld-numeric.c
blob11fdac06fa669b6ed000b05c13cd87eae6b2f3b6
1 /* Copyright (C) 1995-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <langinfo.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <sys/uio.h>
26 #include <assert.h>
28 #include "localedef.h"
29 #include "linereader.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
34 /* The real definition of the struct for the LC_NUMERIC locale. */
35 struct locale_numeric_t
37 const char *decimal_point;
38 const char *thousands_sep;
39 char *grouping;
40 size_t grouping_len;
41 uint32_t decimal_point_wc;
42 uint32_t thousands_sep_wc;
46 static void
47 numeric_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
50 if (!ignore_content)
52 locale->categories[LC_NUMERIC].numeric =
53 (struct locale_numeric_t *) xcalloc (1,
54 sizeof (struct locale_numeric_t));
57 if (lr != NULL)
59 lr->translate_strings = 1;
60 lr->return_widestr = 0;
65 void
66 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
68 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
69 int nothing = 0;
71 /* Now resolve copying and also handle completely missing definitions. */
72 if (numeric == NULL)
74 /* First see whether we were supposed to copy. If yes, find the
75 actual definition. */
76 if (locale->copy_name[LC_NUMERIC] != NULL)
78 /* Find the copying locale. This has to happen transitively since
79 the locale we are copying from might also copying another one. */
80 struct localedef_t *from = locale;
83 from = find_locale (LC_NUMERIC, from->copy_name[LC_NUMERIC],
84 from->repertoire_name, charmap);
85 while (from->categories[LC_NUMERIC].numeric == NULL
86 && from->copy_name[LC_NUMERIC] != NULL);
88 numeric = locale->categories[LC_NUMERIC].numeric
89 = from->categories[LC_NUMERIC].numeric;
92 /* If there is still no definition issue an warning and create an
93 empty one. */
94 if (numeric == NULL)
96 record_warning (_("\
97 No definition for %s category found"), "LC_NUMERIC");
98 numeric_startup (NULL, locale, 0);
99 numeric = locale->categories[LC_NUMERIC].numeric;
100 nothing = 1;
104 /* The decimal point must not be empty. This is not said explicitly
105 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
106 != "". */
107 if (numeric->decimal_point == NULL)
109 if (! nothing)
110 record_error (0, 0, _("%s: field `%s' not defined"),
111 "LC_NUMERIC", "decimal_point");
112 numeric->decimal_point = ".";
114 else if (numeric->decimal_point[0] == '\0' && ! nothing)
116 record_error (0, 0, _("\
117 %s: value for field `%s' must not be an empty string"),
118 "LC_NUMERIC", "decimal_point");
120 if (numeric->decimal_point_wc == L'\0')
121 numeric->decimal_point_wc = L'.';
123 if (numeric->grouping_len == 0 && ! nothing)
124 record_error (0, 0, _("%s: field `%s' not defined"),
125 "LC_NUMERIC", "grouping");
129 void
130 numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
131 const char *output_path)
133 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
134 struct locale_file file;
136 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
137 add_locale_string (&file, numeric->decimal_point ?: "");
138 add_locale_string (&file, numeric->thousands_sep ?: "");
139 add_locale_raw_data (&file, numeric->grouping, numeric->grouping_len);
140 add_locale_uint32 (&file, numeric->decimal_point_wc);
141 add_locale_uint32 (&file, numeric->thousands_sep_wc);
142 add_locale_string (&file, charmap->code_set_name);
143 write_locale_data (output_path, LC_NUMERIC, "LC_NUMERIC", &file);
147 /* The parser for the LC_NUMERIC section of the locale definition. */
148 void
149 numeric_read (struct linereader *ldfile, struct localedef_t *result,
150 const struct charmap_t *charmap, const char *repertoire_name,
151 int ignore_content)
153 struct repertoire_t *repertoire = NULL;
154 struct locale_numeric_t *numeric;
155 struct token *now;
156 enum token_t nowtok;
158 /* Get the repertoire we have to use. */
159 if (repertoire_name != NULL)
160 repertoire = repertoire_read (repertoire_name);
162 /* The rest of the line containing `LC_NUMERIC' must be free. */
163 lr_ignore_rest (ldfile, 1);
168 now = lr_token (ldfile, charmap, result, NULL, verbose);
169 nowtok = now->tok;
171 while (nowtok == tok_eol);
173 /* If we see `copy' now we are almost done. */
174 if (nowtok == tok_copy)
176 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
177 LC_NUMERIC, "LC_NUMERIC", ignore_content);
178 return;
181 /* Prepare the data structures. */
182 numeric_startup (ldfile, result, ignore_content);
183 numeric = result->categories[LC_NUMERIC].numeric;
185 while (1)
187 /* Of course we don't proceed beyond the end of file. */
188 if (nowtok == tok_eof)
189 break;
191 /* Ingore empty lines. */
192 if (nowtok == tok_eol)
194 now = lr_token (ldfile, charmap, result, NULL, verbose);
195 nowtok = now->tok;
196 continue;
199 switch (nowtok)
201 #define STR_ELEM(cat) \
202 case tok_##cat: \
203 /* Ignore the rest of the line if we don't need the input of \
204 this line. */ \
205 if (ignore_content) \
207 lr_ignore_rest (ldfile, 0); \
208 break; \
211 ldfile->return_widestr = 1; \
212 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
213 if (now->tok != tok_string) \
214 goto err_label; \
215 if (numeric->cat != NULL) \
216 lr_error (ldfile, _("\
217 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
218 else if (!ignore_content && now->val.str.startmb == NULL) \
220 lr_error (ldfile, _("\
221 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
222 numeric->cat = ""; \
223 numeric->cat##_wc = L'\0'; \
225 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
227 lr_error (ldfile, _("\
228 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
230 else if (!ignore_content) \
232 numeric->cat = now->val.str.startmb; \
234 if (now->val.str.startwc != NULL) \
235 numeric->cat##_wc = *now->val.str.startwc; \
237 ldfile->return_widestr = 0; \
238 break
240 STR_ELEM (decimal_point);
241 STR_ELEM (thousands_sep);
243 case tok_grouping:
244 /* Ignore the rest of the line if we don't need the input of
245 this line. */
246 if (ignore_content)
248 lr_ignore_rest (ldfile, 0);
249 break;
252 now = lr_token (ldfile, charmap, result, NULL, verbose);
253 if (now->tok != tok_minus1 && now->tok != tok_number)
254 goto err_label;
255 else
257 size_t act = 0;
258 size_t max = 10;
259 char *grouping = xmalloc (max);
263 if (act + 1 >= max)
265 max *= 2;
266 grouping = xrealloc (grouping, max);
269 if (act > 0 && grouping[act - 1] == '\177')
271 lr_error (ldfile, _("\
272 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
273 lr_ignore_rest (ldfile, 0);
274 break;
277 if (now->tok == tok_minus1)
278 grouping[act++] = '\177';
279 else if (now->val.num == 0)
281 /* A value of 0 disables grouping from here on but
282 we must not store a NUL character since this
283 terminates the string. Use something different
284 which must not be used otherwise. */
285 grouping[act++] = '\377';
287 else if (now->val.num > 126)
288 lr_error (ldfile, _("\
289 %s: values for field `%s' must be smaller than 127"),
290 "LC_NUMERIC", "grouping");
291 else
292 grouping[act++] = now->val.num;
294 /* Next must be semicolon. */
295 now = lr_token (ldfile, charmap, result, NULL, verbose);
296 if (now->tok != tok_semicolon)
297 break;
299 now = lr_token (ldfile, charmap, result, NULL, verbose);
301 while (now->tok == tok_minus1 || now->tok == tok_number);
303 if (now->tok != tok_eol)
304 goto err_label;
306 /* A single -1 means no grouping. */
307 if (act == 1 && grouping[0] == '\177')
308 act--;
309 grouping[act++] = '\0';
311 numeric->grouping = xrealloc (grouping, act);
312 numeric->grouping_len = act;
314 break;
316 case tok_end:
317 /* Next we assume `LC_NUMERIC'. */
318 now = lr_token (ldfile, charmap, result, NULL, verbose);
319 if (now->tok == tok_eof)
320 break;
321 if (now->tok == tok_eol)
322 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
323 else if (now->tok != tok_lc_numeric)
324 lr_error (ldfile, _("\
325 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
326 lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
327 return;
329 default:
330 err_label:
331 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
334 /* Prepare for the next round. */
335 now = lr_token (ldfile, charmap, result, NULL, verbose);
336 nowtok = now->tok;
339 /* When we come here we reached the end of the file. */
340 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");