Update.
[glibc.git] / locale / programs / ld-identification.c
bloba64fe63d1be8bb41a28469e474539175cefc5220
1 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <error.h>
25 #include <langinfo.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/uio.h>
30 #include <assert.h>
32 #include "localeinfo.h"
33 #include "locfile.h"
36 /* The real definition of the struct for the LC_IDENTIFICATION locale. */
37 struct locale_identification_t
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];
57 static const char *category_name[__LC_LAST] =
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"
75 static void
76 identification_startup (struct linereader *lr, struct localedef_t *locale,
77 int ignore_content)
79 if (!ignore_content)
81 locale->categories[LC_IDENTIFICATION].identification =
82 (struct locale_identification_t *)
83 xcalloc (1, sizeof (struct locale_identification_t));
85 locale->categories[LC_IDENTIFICATION].identification->category[LC_ALL] =
86 "";
89 if (lr != NULL)
91 lr->translate_strings = 1;
92 lr->return_widestr = 0;
97 void
98 identification_finish (struct localedef_t *locale, struct charmap_t *charmap)
100 struct locale_identification_t *identification
101 = locale->categories[LC_IDENTIFICATION].identification;
102 int nothing = 0;
103 size_t num;
105 /* Now resolve copying and also handle completely missing definitions. */
106 if (identification == NULL)
108 /* First see whether we were supposed to copy. If yes, find the
109 actual definition. */
110 if (locale->copy_name[LC_IDENTIFICATION] != NULL)
112 /* Find the copying locale. This has to happen transitively since
113 the locale we are copying from might also copying another one. */
114 struct localedef_t *from = locale;
117 from = find_locale (LC_IDENTIFICATION,
118 from->copy_name[LC_IDENTIFICATION],
119 from->repertoire_name, charmap);
120 while (from->categories[LC_IDENTIFICATION].identification == NULL
121 && from->copy_name[LC_IDENTIFICATION] != NULL);
123 identification = locale->categories[LC_IDENTIFICATION].identification
124 = from->categories[LC_IDENTIFICATION].identification;
127 /* If there is still no definition issue an warning and create an
128 empty one. */
129 if (identification == NULL)
131 if (! be_quiet)
132 error (0, 0, _("No definition for %s category found"),
133 "LC_IDENTIFICATION");
134 identification_startup (NULL, locale, 0);
135 identification
136 = locale->categories[LC_IDENTIFICATION].identification;
137 nothing = 1;
141 #define TEST_ELEM(cat) \
142 if (identification->cat == NULL) \
144 if (verbose && ! nothing) \
145 error (0, 0, _("%s: field `%s' not defined"), \
146 "LC_IDENTIFICATION", #cat); \
147 identification->cat = ""; \
150 TEST_ELEM (title);
151 TEST_ELEM (source);
152 TEST_ELEM (address);
153 TEST_ELEM (contact);
154 TEST_ELEM (email);
155 TEST_ELEM (tel);
156 TEST_ELEM (fax);
157 TEST_ELEM (language);
158 TEST_ELEM (territory);
159 TEST_ELEM (audience);
160 TEST_ELEM (application);
161 TEST_ELEM (abbreviation);
162 TEST_ELEM (revision);
163 TEST_ELEM (date);
165 for (num = 0; num < __LC_LAST; ++num)
166 if (num != LC_ALL && identification->category[num] == NULL)
168 if (verbose && ! nothing)
169 error (0, 0, _("%s: no identification for category `%s'"),
170 "LC_IDENTIFICATION", category_name[num]);
171 identification->category[num] = "";
176 void
177 identification_output (struct localedef_t *locale, struct charmap_t *charmap,
178 const char *output_path)
180 struct locale_identification_t *identification
181 = locale->categories[LC_IDENTIFICATION].identification;
182 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)
183 + (__LC_LAST - 2)];
184 struct locale_file data;
185 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)];
186 size_t cnt = 0;
187 size_t num;
188 size_t last_idx;
190 data.magic = LIMAGIC (LC_IDENTIFICATION);
191 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION);
192 iov[cnt].iov_base = (void *) &data;
193 iov[cnt].iov_len = sizeof (data);
194 ++cnt;
196 iov[cnt].iov_base = (void *) idx;
197 iov[cnt].iov_len = sizeof (idx);
198 ++cnt;
200 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
201 iov[cnt].iov_base = (void *) identification->title;
202 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
203 ++cnt;
205 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
206 iov[cnt].iov_base = (void *) identification->source;
207 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
208 ++cnt;
210 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
211 iov[cnt].iov_base = (void *) identification->address;
212 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
213 ++cnt;
215 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
216 iov[cnt].iov_base = (void *) identification->contact;
217 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
218 ++cnt;
220 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
221 iov[cnt].iov_base = (void *) identification->email;
222 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
223 ++cnt;
225 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
226 iov[cnt].iov_base = (void *) identification->tel;
227 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
228 ++cnt;
230 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
231 iov[cnt].iov_base = (void *) identification->fax;
232 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
233 ++cnt;
235 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
236 iov[cnt].iov_base = (void *) identification->language;
237 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
238 ++cnt;
240 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
241 iov[cnt].iov_base = (void *) identification->territory;
242 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
243 ++cnt;
245 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
246 iov[cnt].iov_base = (void *) identification->audience;
247 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
248 ++cnt;
250 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
251 iov[cnt].iov_base = (void *) identification->application;
252 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
253 ++cnt;
255 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
256 iov[cnt].iov_base = (void *) identification->abbreviation;
257 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
258 ++cnt;
260 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
261 iov[cnt].iov_base = (void *) identification->revision;
262 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
263 ++cnt;
265 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
266 iov[cnt].iov_base = (void *) identification->date;
267 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
268 ++cnt;
270 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
271 last_idx = cnt - 1;
272 idx[last_idx] = idx[cnt - 2];
273 for (num = 0; num < __LC_LAST; ++num)
274 if (num != LC_ALL)
276 iov[cnt].iov_base = (void *) identification->category[num];
277 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
278 idx[last_idx] += iov[cnt].iov_len;
279 ++cnt;
282 assert (last_idx == _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION) - 1);
283 iov[cnt].iov_base = (void *) charmap->code_set_name;
284 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
285 ++cnt;
287 assert (cnt == (2 + _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)
288 + (__LC_LAST - 2)));
290 write_locale_data (output_path, "LC_IDENTIFICATION", cnt, iov);
294 /* The parser for the LC_IDENTIFICATION section of the locale definition. */
295 void
296 identification_read (struct linereader *ldfile, struct localedef_t *result,
297 struct charmap_t *charmap, const char *repertoire_name,
298 int ignore_content)
300 struct locale_identification_t *identification;
301 struct token *now;
302 struct token *arg;
303 struct token *cattok;
304 int category;
305 enum token_t nowtok;
307 /* The rest of the line containing `LC_IDENTIFICATION' must be free. */
308 lr_ignore_rest (ldfile, 1);
312 now = lr_token (ldfile, charmap, NULL, verbose);
313 nowtok = now->tok;
315 while (nowtok == tok_eol);
317 /* If we see `copy' now we are almost done. */
318 if (nowtok == tok_copy)
320 handle_copy (ldfile, charmap, repertoire_name, result,
321 tok_lc_identification, LC_IDENTIFICATION,
322 "LC_IDENTIFICATION", ignore_content);
323 return;
326 /* Prepare the data structures. */
327 identification_startup (ldfile, result, ignore_content);
328 identification = result->categories[LC_IDENTIFICATION].identification;
330 while (1)
332 /* Of course we don't proceed beyond the end of file. */
333 if (nowtok == tok_eof)
334 break;
336 /* Ignore empty lines. */
337 if (nowtok == tok_eol)
339 now = lr_token (ldfile, charmap, NULL, verbose);
340 nowtok = now->tok;
341 continue;
344 switch (nowtok)
346 #define STR_ELEM(cat) \
347 case tok_##cat: \
348 /* Ignore the rest of the line if we don't need the input of \
349 this line. */ \
350 if (ignore_content) \
352 lr_ignore_rest (ldfile, 0); \
353 break; \
356 arg = lr_token (ldfile, charmap, NULL, verbose); \
357 if (arg->tok != tok_string) \
358 goto err_label; \
359 if (identification->cat != NULL) \
360 lr_error (ldfile, _("\
361 %s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \
362 else if (!ignore_content && arg->val.str.startmb == NULL) \
364 lr_error (ldfile, _("\
365 %s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \
366 identification->cat = ""; \
368 else if (!ignore_content) \
369 identification->cat = arg->val.str.startmb; \
370 break
372 STR_ELEM (title);
373 STR_ELEM (source);
374 STR_ELEM (address);
375 STR_ELEM (contact);
376 STR_ELEM (email);
377 STR_ELEM (tel);
378 STR_ELEM (fax);
379 STR_ELEM (language);
380 STR_ELEM (territory);
381 STR_ELEM (audience);
382 STR_ELEM (application);
383 STR_ELEM (abbreviation);
384 STR_ELEM (revision);
385 STR_ELEM (date);
387 case tok_category:
388 /* Ignore the rest of the line if we don't need the input of
389 this line. */
390 if (ignore_content)
392 lr_ignore_rest (ldfile, 0);
393 break;
396 /* We expect two operands. */
397 arg = lr_token (ldfile, charmap, NULL, verbose);
398 if (arg->tok != tok_string && arg->tok != tok_ident)
399 goto err_label;
400 /* Next is a semicolon. */
401 cattok = lr_token (ldfile, charmap, NULL, verbose);
402 if (cattok->tok != tok_semicolon)
403 goto err_label;
404 /* Now a LC_xxx identifier. */
405 cattok = lr_token (ldfile, charmap, NULL, verbose);
406 switch (cattok->tok)
408 #define CATEGORY(lname, uname) \
409 case tok_lc_##lname: \
410 category = LC_##uname; \
411 break
413 CATEGORY (identification, IDENTIFICATION);
414 CATEGORY (ctype, CTYPE);
415 CATEGORY (collate, COLLATE);
416 CATEGORY (time, TIME);
417 CATEGORY (numeric, NUMERIC);
418 CATEGORY (monetary, MONETARY);
419 CATEGORY (messages, MESSAGES);
420 CATEGORY (paper, PAPER);
421 CATEGORY (name, NAME);
422 CATEGORY (address, ADDRESS);
423 CATEGORY (telephone, TELEPHONE);
424 CATEGORY (measurement, MEASUREMENT);
426 default:
427 goto err_label;
429 if (identification->category[category] != NULL)
431 lr_error (ldfile, _("\
432 %s: duplicate category version definition"), "LC_IDENTIFICATION");
433 free (arg->val.str.startmb);
435 else
436 identification->category[category] = arg->val.str.startmb;
437 break;
439 case tok_end:
440 /* Next we assume `LC_IDENTIFICATION'. */
441 arg = lr_token (ldfile, charmap, NULL, verbose);
442 if (arg->tok == tok_eof)
443 break;
444 if (arg->tok == tok_eol)
445 lr_error (ldfile, _("%s: incomplete `END' line"),
446 "LC_IDENTIFICATION");
447 else if (arg->tok != tok_lc_identification)
448 lr_error (ldfile, _("\
449 %1$s: definition does not end with `END %1$s'"), "LC_IDENTIFICATION");
450 lr_ignore_rest (ldfile, arg->tok == tok_lc_identification);
451 return;
453 default:
454 err_label:
455 SYNTAX_ERROR (_("%s: syntax error"), "LC_IDENTIFICATION");
458 /* Prepare for the next round. */
459 now = lr_token (ldfile, charmap, NULL, verbose);
460 nowtok = now->tok;
463 /* When we come here we reached the end of the file. */
464 lr_error (ldfile, _("%s: premature end of file"), "LC_IDENTIFICATION");