1/* Copyright (C) 1998-2016 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. */
37struct 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
57static 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
75static void
76identification_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
97void
98identification_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, _("\
134No 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 if (num != LC_ALL && identification->category[num] == NULL)
168 {
169 if (verbose && ! nothing)
170 WITH_CUR_LOCALE (error (0, 0, _("\
171%s: no identification for category `%s'"),
172 "LC_IDENTIFICATION", category_name[num]));
173 identification->category[num] = "";
174 }
175}
176
177
178void
179identification_output (struct localedef_t *locale,
180 const struct charmap_t *charmap,
181 const char *output_path)
182{
183 struct locale_identification_t *identification
184 = locale->categories[LC_IDENTIFICATION].identification;
185 struct locale_file file;
186 size_t num;
187
188 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION));
189 add_locale_string (&file, identification->title);
190 add_locale_string (&file, identification->source);
191 add_locale_string (&file, identification->address);
192 add_locale_string (&file, identification->contact);
193 add_locale_string (&file, identification->email);
194 add_locale_string (&file, identification->tel);
195 add_locale_string (&file, identification->fax);
196 add_locale_string (&file, identification->language);
197 add_locale_string (&file, identification->territory);
198 add_locale_string (&file, identification->audience);
199 add_locale_string (&file, identification->application);
200 add_locale_string (&file, identification->abbreviation);
201 add_locale_string (&file, identification->revision);
202 add_locale_string (&file, identification->date);
203 start_locale_structure (&file);
204 for (num = 0; num < __LC_LAST; ++num)
205 if (num != LC_ALL)
206 add_locale_string (&file, identification->category[num]);
207 end_locale_structure (&file);
208 add_locale_string (&file, charmap->code_set_name);
209 write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION",
210 &file);
211}
212
213
214/* The parser for the LC_IDENTIFICATION section of the locale definition. */
215void
216identification_read (struct linereader *ldfile, struct localedef_t *result,
217 const struct charmap_t *charmap, const char *repertoire_name,
218 int ignore_content)
219{
220 struct locale_identification_t *identification;
221 struct token *now;
222 struct token *arg;
223 struct token *cattok;
224 int category;
225 enum token_t nowtok;
226
227 /* The rest of the line containing `LC_IDENTIFICATION' must be free. */
228 lr_ignore_rest (ldfile, 1);
229
230 do
231 {
232 now = lr_token (ldfile, charmap, result, NULL, verbose);
233 nowtok = now->tok;
234 }
235 while (nowtok == tok_eol);
236
237 /* If we see `copy' now we are almost done. */
238 if (nowtok == tok_copy)
239 {
240 handle_copy (ldfile, charmap, repertoire_name, result,
241 tok_lc_identification, LC_IDENTIFICATION,
242 "LC_IDENTIFICATION", ignore_content);
243 return;
244 }
245
246 /* Prepare the data structures. */
247 identification_startup (ldfile, result, ignore_content);
248 identification = result->categories[LC_IDENTIFICATION].identification;
249
250 while (1)
251 {
252 /* Of course we don't proceed beyond the end of file. */
253 if (nowtok == tok_eof)
254 break;
255
256 /* Ignore empty lines. */
257 if (nowtok == tok_eol)
258 {
259 now = lr_token (ldfile, charmap, result, NULL, verbose);
260 nowtok = now->tok;
261 continue;
262 }
263
264 switch (nowtok)
265 {
266#define STR_ELEM(cat) \
267 case tok_##cat: \
268 /* Ignore the rest of the line if we don't need the input of \
269 this line. */ \
270 if (ignore_content) \
271 { \
272 lr_ignore_rest (ldfile, 0); \
273 break; \
274 } \
275 \
276 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
277 if (arg->tok != tok_string) \
278 goto err_label; \
279 if (identification->cat != NULL) \
280 lr_error (ldfile, _("\
281%s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \
282 else if (!ignore_content && arg->val.str.startmb == NULL) \
283 { \
284 lr_error (ldfile, _("\
285%s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \
286 identification->cat = ""; \
287 } \
288 else if (!ignore_content) \
289 identification->cat = arg->val.str.startmb; \
290 break
291
292 STR_ELEM (title);
293 STR_ELEM (source);
294 STR_ELEM (address);
295 STR_ELEM (contact);
296 STR_ELEM (email);
297 STR_ELEM (tel);
298 STR_ELEM (fax);
299 STR_ELEM (language);
300 STR_ELEM (territory);
301 STR_ELEM (audience);
302 STR_ELEM (application);
303 STR_ELEM (abbreviation);
304 STR_ELEM (revision);
305 STR_ELEM (date);
306
307 case tok_category:
308 /* Ignore the rest of the line if we don't need the input of
309 this line. */
310 if (ignore_content)
311 {
312 lr_ignore_rest (ldfile, 0);
313 break;
314 }
315
316 /* We expect two operands. */
317 arg = lr_token (ldfile, charmap, result, NULL, verbose);
318 if (arg->tok != tok_string && arg->tok != tok_ident)
319 goto err_label;
320 /* Next is a semicolon. */
321 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
322 if (cattok->tok != tok_semicolon)
323 goto err_label;
324 /* Now a LC_xxx identifier. */
325 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
326 switch (cattok->tok)
327 {
328#define CATEGORY(lname, uname) \
329 case tok_lc_##lname: \
330 category = LC_##uname; \
331 break
332
333 CATEGORY (identification, IDENTIFICATION);
334 CATEGORY (ctype, CTYPE);
335 CATEGORY (collate, COLLATE);
336 CATEGORY (time, TIME);
337 CATEGORY (numeric, NUMERIC);
338 CATEGORY (monetary, MONETARY);
339 CATEGORY (messages, MESSAGES);
340 CATEGORY (paper, PAPER);
341 CATEGORY (name, NAME);
342 CATEGORY (address, ADDRESS);
343 CATEGORY (telephone, TELEPHONE);
344 CATEGORY (measurement, MEASUREMENT);
345
346 default:
347 goto err_label;
348 }
349 if (identification->category[category] != NULL)
350 {
351 lr_error (ldfile, _("\
352%s: duplicate category version definition"), "LC_IDENTIFICATION");
353 free (arg->val.str.startmb);
354 }
355 else
356 identification->category[category] = arg->val.str.startmb;
357 break;
358
359 case tok_end:
360 /* Next we assume `LC_IDENTIFICATION'. */
361 arg = lr_token (ldfile, charmap, result, NULL, verbose);
362 if (arg->tok == tok_eof)
363 break;
364 if (arg->tok == tok_eol)
365 lr_error (ldfile, _("%s: incomplete `END' line"),
366 "LC_IDENTIFICATION");
367 else if (arg->tok != tok_lc_identification)
368 lr_error (ldfile, _("\
369%1$s: definition does not end with `END %1$s'"), "LC_IDENTIFICATION");
370 lr_ignore_rest (ldfile, arg->tok == tok_lc_identification);
371 return;
372
373 default:
374 err_label:
375 SYNTAX_ERROR (_("%s: syntax error"), "LC_IDENTIFICATION");
376 }
377
378 /* Prepare for the next round. */
379 now = lr_token (ldfile, charmap, result, NULL, verbose);
380 nowtok = now->tok;
381 }
382
383 /* When we come here we reached the end of the file. */
384 lr_error (ldfile, _("%s: premature end of file"), "LC_IDENTIFICATION");
385}
386