1 /* Copyright (C) 1995-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #include <langinfo.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <sys/uio.h>
25
26 #include <assert.h>
27
28 #include "localedef.h"
29 #include "linereader.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
32
33
34 /* The real definition of the struct for the LC_NUMERIC locale. */
35 struct locale_numeric_t
36 {
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;
43 };
44
45
46 static void
numeric_startup(struct linereader * lr,struct localedef_t * locale,int ignore_content)47 numeric_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
49 {
50 if (!ignore_content)
51 {
52 locale->categories[LC_NUMERIC].numeric =
53 (struct locale_numeric_t *) xcalloc (1,
54 sizeof (struct locale_numeric_t));
55 }
56
57 if (lr != NULL)
58 {
59 lr->translate_strings = 1;
60 lr->return_widestr = 0;
61 }
62 }
63
64
65 void
numeric_finish(struct localedef_t * locale,const struct charmap_t * charmap)66 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
67 {
68 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
69 int nothing = 0;
70
71 /* Now resolve copying and also handle completely missing definitions. */
72 if (numeric == NULL)
73 {
74 /* First see whether we were supposed to copy. If yes, find the
75 actual definition. */
76 if (locale->copy_name[LC_NUMERIC] != NULL)
77 {
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;
81
82 do
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);
87
88 numeric = locale->categories[LC_NUMERIC].numeric
89 = from->categories[LC_NUMERIC].numeric;
90 }
91
92 /* If there is still no definition issue an warning and create an
93 empty one. */
94 if (numeric == NULL)
95 {
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;
101 }
102 }
103
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)
108 {
109 if (! nothing)
110 record_error (0, 0, _("%s: field `%s' not defined"),
111 "LC_NUMERIC", "decimal_point");
112 numeric->decimal_point = ".";
113 }
114 else if (numeric->decimal_point[0] == '\0' && ! nothing)
115 {
116 record_error (0, 0, _("\
117 %s: value for field `%s' must not be an empty string"),
118 "LC_NUMERIC", "decimal_point");
119 }
120 if (numeric->decimal_point_wc == L'\0')
121 numeric->decimal_point_wc = L'.';
122
123 if (numeric->grouping_len == 0 && ! nothing)
124 record_error (0, 0, _("%s: field `%s' not defined"),
125 "LC_NUMERIC", "grouping");
126 }
127
128
129 void
numeric_output(struct localedef_t * locale,const struct charmap_t * charmap,const char * output_path)130 numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
131 const char *output_path)
132 {
133 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
134 struct locale_file file;
135
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);
144 }
145
146
147 /* The parser for the LC_NUMERIC section of the locale definition. */
148 void
numeric_read(struct linereader * ldfile,struct localedef_t * result,const struct charmap_t * charmap,const char * repertoire_name,int ignore_content)149 numeric_read (struct linereader *ldfile, struct localedef_t *result,
150 const struct charmap_t *charmap, const char *repertoire_name,
151 int ignore_content)
152 {
153 struct repertoire_t *repertoire = NULL;
154 struct locale_numeric_t *numeric;
155 struct token *now;
156 enum token_t nowtok;
157
158 /* Get the repertoire we have to use. */
159 if (repertoire_name != NULL)
160 repertoire = repertoire_read (repertoire_name);
161
162 /* The rest of the line containing `LC_NUMERIC' must be free. */
163 lr_ignore_rest (ldfile, 1);
164
165
166 do
167 {
168 now = lr_token (ldfile, charmap, result, NULL, verbose);
169 nowtok = now->tok;
170 }
171 while (nowtok == tok_eol);
172
173 /* If we see `copy' now we are almost done. */
174 if (nowtok == tok_copy)
175 {
176 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
177 LC_NUMERIC, "LC_NUMERIC", ignore_content);
178 return;
179 }
180
181 /* Prepare the data structures. */
182 numeric_startup (ldfile, result, ignore_content);
183 numeric = result->categories[LC_NUMERIC].numeric;
184
185 while (1)
186 {
187 /* Of course we don't proceed beyond the end of file. */
188 if (nowtok == tok_eof)
189 break;
190
191 /* Ingore empty lines. */
192 if (nowtok == tok_eol)
193 {
194 now = lr_token (ldfile, charmap, result, NULL, verbose);
195 nowtok = now->tok;
196 continue;
197 }
198
199 switch (nowtok)
200 {
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) \
206 { \
207 lr_ignore_rest (ldfile, 0); \
208 break; \
209 } \
210 \
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) \
219 { \
220 lr_error (ldfile, _("\
221 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
222 numeric->cat = ""; \
223 numeric->cat##_wc = L'\0'; \
224 } \
225 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
226 { \
227 lr_error (ldfile, _("\
228 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
229 } \
230 else if (!ignore_content) \
231 { \
232 numeric->cat = now->val.str.startmb; \
233 \
234 if (now->val.str.startwc != NULL) \
235 numeric->cat##_wc = *now->val.str.startwc; \
236 } \
237 ldfile->return_widestr = 0; \
238 break
239
240 STR_ELEM (decimal_point);
241 STR_ELEM (thousands_sep);
242
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)
247 {
248 lr_ignore_rest (ldfile, 0);
249 break;
250 }
251
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
256 {
257 size_t act = 0;
258 size_t max = 10;
259 char *grouping = xmalloc (max);
260
261 do
262 {
263 if (act + 1 >= max)
264 {
265 max *= 2;
266 grouping = xrealloc (grouping, max);
267 }
268
269 if (act > 0 && grouping[act - 1] == '\177')
270 {
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;
275 }
276
277 if (now->tok == tok_minus1)
278 grouping[act++] = '\177';
279 else if (now->val.num == 0)
280 {
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';
286 }
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;
293
294 /* Next must be semicolon. */
295 now = lr_token (ldfile, charmap, result, NULL, verbose);
296 if (now->tok != tok_semicolon)
297 break;
298
299 now = lr_token (ldfile, charmap, result, NULL, verbose);
300 }
301 while (now->tok == tok_minus1 || now->tok == tok_number);
302
303 if (now->tok != tok_eol)
304 goto err_label;
305
306 /* A single -1 means no grouping. */
307 if (act == 1 && grouping[0] == '\177')
308 act--;
309 grouping[act++] = '\0';
310
311 numeric->grouping = xrealloc (grouping, act);
312 numeric->grouping_len = act;
313 }
314 break;
315
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;
328
329 default:
330 err_label:
331 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
332 }
333
334 /* Prepare for the next round. */
335 now = lr_token (ldfile, charmap, result, NULL, verbose);
336 nowtok = now->tok;
337 }
338
339 /* When we come here we reached the end of the file. */
340 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");
341 }
342