1 | /* Copyright (C) 1998-2017 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. |
4 | |
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. |
9 | |
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. |
14 | |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
17 | |
18 | #ifdef HAVE_CONFIG_H |
19 | # include <config.h> |
20 | #endif |
21 | |
22 | #include <error.h> |
23 | #include <langinfo.h> |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | #include <stdint.h> |
27 | #include <sys/uio.h> |
28 | |
29 | #include <assert.h> |
30 | |
31 | #include "localedef.h" |
32 | #include "localeinfo.h" |
33 | #include "locfile.h" |
34 | |
35 | |
36 | /* The real definition of the struct for the LC_IDENTIFICATION locale. */ |
37 | struct locale_identification_t |
38 | { |
39 | const char *title; |
40 | const char *source; |
41 | const char *address; |
42 | const char *contact; |
43 | const char *email; |
44 | const char *tel; |
45 | const char *fax; |
46 | const char *language; |
47 | const char *territory; |
48 | const char *audience; |
49 | const char *application; |
50 | const char *abbreviation; |
51 | const char *revision; |
52 | const char *date; |
53 | const char *category[__LC_LAST]; |
54 | }; |
55 | |
56 | |
57 | static const char *category_name[__LC_LAST] = |
58 | { |
59 | [LC_CTYPE] = "LC_CTYPE" , |
60 | [LC_NUMERIC] = "LC_NUMERIC" , |
61 | [LC_TIME] = "LC_TIME" , |
62 | [LC_COLLATE] = "LC_COLLATE" , |
63 | [LC_MONETARY] = "LC_MONETARY" , |
64 | [LC_MESSAGES] = "LC_MESSAGES" , |
65 | [LC_ALL] = "LC_ALL" , |
66 | [LC_PAPER] = "LC_PAPER" , |
67 | [LC_NAME] = "LC_NAME" , |
68 | [LC_ADDRESS] = "LC_ADDRESS" , |
69 | [LC_TELEPHONE] = "LC_TELEPHONE" , |
70 | [LC_MEASUREMENT] = "LC_MEASUREMENT" , |
71 | [LC_IDENTIFICATION] = "LC_IDENTIFICATION" |
72 | }; |
73 | |
74 | |
75 | static void |
76 | identification_startup (struct linereader *lr, struct localedef_t *locale, |
77 | int ignore_content) |
78 | { |
79 | if (!ignore_content) |
80 | { |
81 | locale->categories[LC_IDENTIFICATION].identification = |
82 | (struct locale_identification_t *) |
83 | xcalloc (1, sizeof (struct locale_identification_t)); |
84 | |
85 | locale->categories[LC_IDENTIFICATION].identification->category[LC_ALL] = |
86 | "" ; |
87 | } |
88 | |
89 | if (lr != NULL) |
90 | { |
91 | lr->translate_strings = 1; |
92 | lr->return_widestr = 0; |
93 | } |
94 | } |
95 | |
96 | |
97 | void |
98 | identification_finish (struct localedef_t *locale, |
99 | const struct charmap_t *charmap) |
100 | { |
101 | struct locale_identification_t *identification |
102 | = locale->categories[LC_IDENTIFICATION].identification; |
103 | int nothing = 0; |
104 | size_t num; |
105 | |
106 | /* Now resolve copying and also handle completely missing definitions. */ |
107 | if (identification == NULL) |
108 | { |
109 | /* First see whether we were supposed to copy. If yes, find the |
110 | actual definition. */ |
111 | if (locale->copy_name[LC_IDENTIFICATION] != NULL) |
112 | { |
113 | /* Find the copying locale. This has to happen transitively since |
114 | the locale we are copying from might also copying another one. */ |
115 | struct localedef_t *from = locale; |
116 | |
117 | do |
118 | from = find_locale (LC_IDENTIFICATION, |
119 | from->copy_name[LC_IDENTIFICATION], |
120 | from->repertoire_name, charmap); |
121 | while (from->categories[LC_IDENTIFICATION].identification == NULL |
122 | && from->copy_name[LC_IDENTIFICATION] != NULL); |
123 | |
124 | identification = locale->categories[LC_IDENTIFICATION].identification |
125 | = from->categories[LC_IDENTIFICATION].identification; |
126 | } |
127 | |
128 | /* If there is still no definition issue an warning and create an |
129 | empty one. */ |
130 | if (identification == NULL) |
131 | { |
132 | if (! be_quiet) |
133 | WITH_CUR_LOCALE (error (0, 0, _("\ |
134 | No definition for %s category found" ), "LC_IDENTIFICATION" )); |
135 | identification_startup (NULL, locale, 0); |
136 | identification |
137 | = locale->categories[LC_IDENTIFICATION].identification; |
138 | nothing = 1; |
139 | } |
140 | } |
141 | |
142 | #define TEST_ELEM(cat) \ |
143 | if (identification->cat == NULL) \ |
144 | { \ |
145 | if (verbose && ! nothing) \ |
146 | WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \ |
147 | "LC_IDENTIFICATION", #cat)); \ |
148 | identification->cat = ""; \ |
149 | } |
150 | |
151 | TEST_ELEM (title); |
152 | TEST_ELEM (source); |
153 | TEST_ELEM (address); |
154 | TEST_ELEM (contact); |
155 | TEST_ELEM (email); |
156 | TEST_ELEM (tel); |
157 | TEST_ELEM (fax); |
158 | TEST_ELEM (language); |
159 | TEST_ELEM (territory); |
160 | TEST_ELEM (audience); |
161 | TEST_ELEM (application); |
162 | TEST_ELEM (abbreviation); |
163 | TEST_ELEM (revision); |
164 | TEST_ELEM (date); |
165 | |
166 | for (num = 0; num < __LC_LAST; ++num) |
167 | { |
168 | /* We don't accept/parse this category, so skip it early. */ |
169 | if (num == LC_ALL) |
170 | continue; |
171 | |
172 | if (identification->category[num] == NULL) |
173 | { |
174 | if (verbose && ! nothing) |
175 | WITH_CUR_LOCALE (error (0, 0, _("\ |
176 | %s: no identification for category `%s'" ), |
177 | "LC_IDENTIFICATION" , category_name[num])); |
178 | identification->category[num] = "" ; |
179 | } |
180 | else |
181 | { |
182 | /* Only list the standards we care about. This is based on the |
183 | ISO 30112 WD10 [2014] standard which supersedes all previous |
184 | revisions of the ISO 14652 standard. */ |
185 | static const char * const standards[] = |
186 | { |
187 | "posix:1993" , |
188 | "i18n:2004" , |
189 | "i18n:2012" , |
190 | }; |
191 | size_t i; |
192 | bool matched = false; |
193 | |
194 | for (i = 0; i < sizeof (standards) / sizeof (standards[0]); ++i) |
195 | if (strcmp (identification->category[num], standards[i]) == 0) |
196 | matched = true; |
197 | |
198 | if (matched != true) |
199 | WITH_CUR_LOCALE (error (0, 0, _("\ |
200 | %s: unknown standard `%s' for category `%s'" ), |
201 | "LC_IDENTIFICATION" , |
202 | identification->category[num], |
203 | category_name[num])); |
204 | } |
205 | } |
206 | } |
207 | |
208 | |
209 | void |
210 | identification_output (struct localedef_t *locale, |
211 | const struct charmap_t *charmap, |
212 | const char *output_path) |
213 | { |
214 | struct locale_identification_t *identification |
215 | = locale->categories[LC_IDENTIFICATION].identification; |
216 | struct locale_file file; |
217 | size_t num; |
218 | |
219 | init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)); |
220 | add_locale_string (&file, identification->title); |
221 | add_locale_string (&file, identification->source); |
222 | add_locale_string (&file, identification->address); |
223 | add_locale_string (&file, identification->contact); |
224 | add_locale_string (&file, identification->email); |
225 | add_locale_string (&file, identification->tel); |
226 | add_locale_string (&file, identification->fax); |
227 | add_locale_string (&file, identification->language); |
228 | add_locale_string (&file, identification->territory); |
229 | add_locale_string (&file, identification->audience); |
230 | add_locale_string (&file, identification->application); |
231 | add_locale_string (&file, identification->abbreviation); |
232 | add_locale_string (&file, identification->revision); |
233 | add_locale_string (&file, identification->date); |
234 | start_locale_structure (&file); |
235 | for (num = 0; num < __LC_LAST; ++num) |
236 | if (num != LC_ALL) |
237 | add_locale_string (&file, identification->category[num]); |
238 | end_locale_structure (&file); |
239 | add_locale_string (&file, charmap->code_set_name); |
240 | write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION" , |
241 | &file); |
242 | } |
243 | |
244 | |
245 | /* The parser for the LC_IDENTIFICATION section of the locale definition. */ |
246 | void |
247 | identification_read (struct linereader *ldfile, struct localedef_t *result, |
248 | const struct charmap_t *charmap, const char *repertoire_name, |
249 | int ignore_content) |
250 | { |
251 | struct locale_identification_t *identification; |
252 | struct token *now; |
253 | struct token *arg; |
254 | struct token *cattok; |
255 | int category; |
256 | enum token_t nowtok; |
257 | |
258 | /* The rest of the line containing `LC_IDENTIFICATION' must be free. */ |
259 | lr_ignore_rest (ldfile, 1); |
260 | |
261 | do |
262 | { |
263 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
264 | nowtok = now->tok; |
265 | } |
266 | while (nowtok == tok_eol); |
267 | |
268 | /* If we see `copy' now we are almost done. */ |
269 | if (nowtok == tok_copy) |
270 | { |
271 | handle_copy (ldfile, charmap, repertoire_name, result, |
272 | tok_lc_identification, LC_IDENTIFICATION, |
273 | "LC_IDENTIFICATION" , ignore_content); |
274 | return; |
275 | } |
276 | |
277 | /* Prepare the data structures. */ |
278 | identification_startup (ldfile, result, ignore_content); |
279 | identification = result->categories[LC_IDENTIFICATION].identification; |
280 | |
281 | while (1) |
282 | { |
283 | /* Of course we don't proceed beyond the end of file. */ |
284 | if (nowtok == tok_eof) |
285 | break; |
286 | |
287 | /* Ignore empty lines. */ |
288 | if (nowtok == tok_eol) |
289 | { |
290 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
291 | nowtok = now->tok; |
292 | continue; |
293 | } |
294 | |
295 | switch (nowtok) |
296 | { |
297 | #define STR_ELEM(cat) \ |
298 | case tok_##cat: \ |
299 | /* Ignore the rest of the line if we don't need the input of \ |
300 | this line. */ \ |
301 | if (ignore_content) \ |
302 | { \ |
303 | lr_ignore_rest (ldfile, 0); \ |
304 | break; \ |
305 | } \ |
306 | \ |
307 | arg = lr_token (ldfile, charmap, result, NULL, verbose); \ |
308 | if (arg->tok != tok_string) \ |
309 | goto err_label; \ |
310 | if (identification->cat != NULL) \ |
311 | lr_error (ldfile, _("\ |
312 | %s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \ |
313 | else if (!ignore_content && arg->val.str.startmb == NULL) \ |
314 | { \ |
315 | lr_error (ldfile, _("\ |
316 | %s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \ |
317 | identification->cat = ""; \ |
318 | } \ |
319 | else if (!ignore_content) \ |
320 | identification->cat = arg->val.str.startmb; \ |
321 | break |
322 | |
323 | STR_ELEM (title); |
324 | STR_ELEM (source); |
325 | STR_ELEM (address); |
326 | STR_ELEM (contact); |
327 | STR_ELEM (email); |
328 | STR_ELEM (tel); |
329 | STR_ELEM (fax); |
330 | STR_ELEM (language); |
331 | STR_ELEM (territory); |
332 | STR_ELEM (audience); |
333 | STR_ELEM (application); |
334 | STR_ELEM (abbreviation); |
335 | STR_ELEM (revision); |
336 | STR_ELEM (date); |
337 | |
338 | case tok_category: |
339 | /* Ignore the rest of the line if we don't need the input of |
340 | this line. */ |
341 | if (ignore_content) |
342 | { |
343 | lr_ignore_rest (ldfile, 0); |
344 | break; |
345 | } |
346 | |
347 | /* We expect two operands. */ |
348 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
349 | if (arg->tok != tok_string && arg->tok != tok_ident) |
350 | goto err_label; |
351 | /* Next is a semicolon. */ |
352 | cattok = lr_token (ldfile, charmap, result, NULL, verbose); |
353 | if (cattok->tok != tok_semicolon) |
354 | goto err_label; |
355 | /* Now a LC_xxx identifier. */ |
356 | cattok = lr_token (ldfile, charmap, result, NULL, verbose); |
357 | switch (cattok->tok) |
358 | { |
359 | #define CATEGORY(lname, uname) \ |
360 | case tok_lc_##lname: \ |
361 | category = LC_##uname; \ |
362 | break |
363 | |
364 | CATEGORY (identification, IDENTIFICATION); |
365 | CATEGORY (ctype, CTYPE); |
366 | CATEGORY (collate, COLLATE); |
367 | CATEGORY (time, TIME); |
368 | CATEGORY (numeric, NUMERIC); |
369 | CATEGORY (monetary, MONETARY); |
370 | CATEGORY (messages, MESSAGES); |
371 | CATEGORY (paper, PAPER); |
372 | CATEGORY (name, NAME); |
373 | CATEGORY (address, ADDRESS); |
374 | CATEGORY (telephone, TELEPHONE); |
375 | CATEGORY (measurement, MEASUREMENT); |
376 | |
377 | default: |
378 | goto err_label; |
379 | } |
380 | if (identification->category[category] != NULL) |
381 | { |
382 | lr_error (ldfile, _("\ |
383 | %s: duplicate category version definition" ), "LC_IDENTIFICATION" ); |
384 | free (arg->val.str.startmb); |
385 | } |
386 | else |
387 | identification->category[category] = arg->val.str.startmb; |
388 | break; |
389 | |
390 | case tok_end: |
391 | /* Next we assume `LC_IDENTIFICATION'. */ |
392 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
393 | if (arg->tok == tok_eof) |
394 | break; |
395 | if (arg->tok == tok_eol) |
396 | lr_error (ldfile, _("%s: incomplete `END' line" ), |
397 | "LC_IDENTIFICATION" ); |
398 | else if (arg->tok != tok_lc_identification) |
399 | lr_error (ldfile, _("\ |
400 | %1$s: definition does not end with `END %1$s'" ), "LC_IDENTIFICATION" ); |
401 | lr_ignore_rest (ldfile, arg->tok == tok_lc_identification); |
402 | return; |
403 | |
404 | default: |
405 | err_label: |
406 | SYNTAX_ERROR (_("%s: syntax error" ), "LC_IDENTIFICATION" ); |
407 | } |
408 | |
409 | /* Prepare for the next round. */ |
410 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
411 | nowtok = now->tok; |
412 | } |
413 | |
414 | /* When we come here we reached the end of the file. */ |
415 | lr_error (ldfile, _("%s: premature end of file" ), "LC_IDENTIFICATION" ); |
416 | } |
417 | |