(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / locale / programs / ld-identification.c
blobae5ea6fb16e2367139de8f08b73b31537e655f8d
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 "localedef.h"
33 #include "localeinfo.h"
34 #include "locfile.h"
37 /* The real definition of the struct for the LC_IDENTIFICATION locale. */
38 struct locale_identification_t
40 const char *title;
41 const char *source;
42 const char *address;
43 const char *contact;
44 const char *email;
45 const char *tel;
46 const char *fax;
47 const char *language;
48 const char *territory;
49 const char *audience;
50 const char *application;
51 const char *abbreviation;
52 const char *revision;
53 const char *date;
54 const char *category[__LC_LAST];
58 static const char *category_name[__LC_LAST] =
60 [LC_CTYPE] = "LC_CTYPE",
61 [LC_NUMERIC] = "LC_NUMERIC",
62 [LC_TIME] = "LC_TIME",
63 [LC_COLLATE] = "LC_COLLATE",
64 [LC_MONETARY] = "LC_MONETARY",
65 [LC_MESSAGES] = "LC_MESSAGES",
66 [LC_ALL] = "LC_ALL",
67 [LC_PAPER] = "LC_PAPER",
68 [LC_NAME] = "LC_NAME",
69 [LC_ADDRESS] = "LC_ADDRESS",
70 [LC_TELEPHONE] = "LC_TELEPHONE",
71 [LC_MEASUREMENT] = "LC_MEASUREMENT",
72 [LC_IDENTIFICATION] = "LC_IDENTIFICATION"
76 static void
77 identification_startup (struct linereader *lr, struct localedef_t *locale,
78 int ignore_content)
80 if (!ignore_content)
82 locale->categories[LC_IDENTIFICATION].identification =
83 (struct locale_identification_t *)
84 xcalloc (1, sizeof (struct locale_identification_t));
86 locale->categories[LC_IDENTIFICATION].identification->category[LC_ALL] =
87 "";
90 if (lr != NULL)
92 lr->translate_strings = 1;
93 lr->return_widestr = 0;
98 void
99 identification_finish (struct localedef_t *locale,
100 const struct charmap_t *charmap)
102 struct locale_identification_t *identification
103 = locale->categories[LC_IDENTIFICATION].identification;
104 int nothing = 0;
105 size_t num;
107 /* Now resolve copying and also handle completely missing definitions. */
108 if (identification == NULL)
110 /* First see whether we were supposed to copy. If yes, find the
111 actual definition. */
112 if (locale->copy_name[LC_IDENTIFICATION] != NULL)
114 /* Find the copying locale. This has to happen transitively since
115 the locale we are copying from might also copying another one. */
116 struct localedef_t *from = locale;
119 from = find_locale (LC_IDENTIFICATION,
120 from->copy_name[LC_IDENTIFICATION],
121 from->repertoire_name, charmap);
122 while (from->categories[LC_IDENTIFICATION].identification == NULL
123 && from->copy_name[LC_IDENTIFICATION] != NULL);
125 identification = locale->categories[LC_IDENTIFICATION].identification
126 = from->categories[LC_IDENTIFICATION].identification;
129 /* If there is still no definition issue an warning and create an
130 empty one. */
131 if (identification == NULL)
133 if (! be_quiet)
134 WITH_CUR_LOCALE (error (0, 0, _("\
135 No definition for %s category found"), "LC_IDENTIFICATION"));
136 identification_startup (NULL, locale, 0);
137 identification
138 = locale->categories[LC_IDENTIFICATION].identification;
139 nothing = 1;
143 #define TEST_ELEM(cat) \
144 if (identification->cat == NULL) \
146 if (verbose && ! nothing) \
147 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
148 "LC_IDENTIFICATION", #cat)); \
149 identification->cat = ""; \
152 TEST_ELEM (title);
153 TEST_ELEM (source);
154 TEST_ELEM (address);
155 TEST_ELEM (contact);
156 TEST_ELEM (email);
157 TEST_ELEM (tel);
158 TEST_ELEM (fax);
159 TEST_ELEM (language);
160 TEST_ELEM (territory);
161 TEST_ELEM (audience);
162 TEST_ELEM (application);
163 TEST_ELEM (abbreviation);
164 TEST_ELEM (revision);
165 TEST_ELEM (date);
167 for (num = 0; num < __LC_LAST; ++num)
168 if (num != LC_ALL && identification->category[num] == NULL)
170 if (verbose && ! nothing)
171 WITH_CUR_LOCALE (error (0, 0, _("\
172 %s: no identification for category `%s'"),
173 "LC_IDENTIFICATION", category_name[num]));
174 identification->category[num] = "";
179 void
180 identification_output (struct localedef_t *locale,
181 const struct charmap_t *charmap,
182 const char *output_path)
184 struct locale_identification_t *identification
185 = locale->categories[LC_IDENTIFICATION].identification;
186 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)
187 + (__LC_LAST - 2)];
188 struct locale_file data;
189 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)];
190 size_t cnt = 0;
191 size_t num;
192 size_t last_idx;
194 data.magic = LIMAGIC (LC_IDENTIFICATION);
195 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION);
196 iov[cnt].iov_base = (void *) &data;
197 iov[cnt].iov_len = sizeof (data);
198 ++cnt;
200 iov[cnt].iov_base = (void *) idx;
201 iov[cnt].iov_len = sizeof (idx);
202 ++cnt;
204 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
205 iov[cnt].iov_base = (void *) identification->title;
206 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
207 ++cnt;
209 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
210 iov[cnt].iov_base = (void *) identification->source;
211 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
212 ++cnt;
214 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
215 iov[cnt].iov_base = (void *) identification->address;
216 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
217 ++cnt;
219 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
220 iov[cnt].iov_base = (void *) identification->contact;
221 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
222 ++cnt;
224 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
225 iov[cnt].iov_base = (void *) identification->email;
226 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
227 ++cnt;
229 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
230 iov[cnt].iov_base = (void *) identification->tel;
231 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
232 ++cnt;
234 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
235 iov[cnt].iov_base = (void *) identification->fax;
236 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
237 ++cnt;
239 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
240 iov[cnt].iov_base = (void *) identification->language;
241 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
242 ++cnt;
244 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
245 iov[cnt].iov_base = (void *) identification->territory;
246 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
247 ++cnt;
249 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
250 iov[cnt].iov_base = (void *) identification->audience;
251 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
252 ++cnt;
254 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
255 iov[cnt].iov_base = (void *) identification->application;
256 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
257 ++cnt;
259 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
260 iov[cnt].iov_base = (void *) identification->abbreviation;
261 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
262 ++cnt;
264 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
265 iov[cnt].iov_base = (void *) identification->revision;
266 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
267 ++cnt;
269 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
270 iov[cnt].iov_base = (void *) identification->date;
271 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
272 ++cnt;
274 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
275 last_idx = cnt - 1;
276 idx[last_idx] = idx[cnt - 2];
277 for (num = 0; num < __LC_LAST; ++num)
278 if (num != LC_ALL)
280 iov[cnt].iov_base = (void *) identification->category[num];
281 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
282 idx[last_idx] += iov[cnt].iov_len;
283 ++cnt;
286 assert (last_idx == _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION) - 1);
287 iov[cnt].iov_base = (void *) charmap->code_set_name;
288 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
289 ++cnt;
291 assert (cnt == (2 + _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)
292 + (__LC_LAST - 2)));
294 write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION", cnt,
295 iov);
299 /* The parser for the LC_IDENTIFICATION section of the locale definition. */
300 void
301 identification_read (struct linereader *ldfile, struct localedef_t *result,
302 const struct charmap_t *charmap, const char *repertoire_name,
303 int ignore_content)
305 struct locale_identification_t *identification;
306 struct token *now;
307 struct token *arg;
308 struct token *cattok;
309 int category;
310 enum token_t nowtok;
312 /* The rest of the line containing `LC_IDENTIFICATION' must be free. */
313 lr_ignore_rest (ldfile, 1);
317 now = lr_token (ldfile, charmap, result, NULL, verbose);
318 nowtok = now->tok;
320 while (nowtok == tok_eol);
322 /* If we see `copy' now we are almost done. */
323 if (nowtok == tok_copy)
325 handle_copy (ldfile, charmap, repertoire_name, result,
326 tok_lc_identification, LC_IDENTIFICATION,
327 "LC_IDENTIFICATION", ignore_content);
328 return;
331 /* Prepare the data structures. */
332 identification_startup (ldfile, result, ignore_content);
333 identification = result->categories[LC_IDENTIFICATION].identification;
335 while (1)
337 /* Of course we don't proceed beyond the end of file. */
338 if (nowtok == tok_eof)
339 break;
341 /* Ignore empty lines. */
342 if (nowtok == tok_eol)
344 now = lr_token (ldfile, charmap, result, NULL, verbose);
345 nowtok = now->tok;
346 continue;
349 switch (nowtok)
351 #define STR_ELEM(cat) \
352 case tok_##cat: \
353 /* Ignore the rest of the line if we don't need the input of \
354 this line. */ \
355 if (ignore_content) \
357 lr_ignore_rest (ldfile, 0); \
358 break; \
361 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
362 if (arg->tok != tok_string) \
363 goto err_label; \
364 if (identification->cat != NULL) \
365 lr_error (ldfile, _("\
366 %s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \
367 else if (!ignore_content && arg->val.str.startmb == NULL) \
369 lr_error (ldfile, _("\
370 %s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \
371 identification->cat = ""; \
373 else if (!ignore_content) \
374 identification->cat = arg->val.str.startmb; \
375 break
377 STR_ELEM (title);
378 STR_ELEM (source);
379 STR_ELEM (address);
380 STR_ELEM (contact);
381 STR_ELEM (email);
382 STR_ELEM (tel);
383 STR_ELEM (fax);
384 STR_ELEM (language);
385 STR_ELEM (territory);
386 STR_ELEM (audience);
387 STR_ELEM (application);
388 STR_ELEM (abbreviation);
389 STR_ELEM (revision);
390 STR_ELEM (date);
392 case tok_category:
393 /* Ignore the rest of the line if we don't need the input of
394 this line. */
395 if (ignore_content)
397 lr_ignore_rest (ldfile, 0);
398 break;
401 /* We expect two operands. */
402 arg = lr_token (ldfile, charmap, result, NULL, verbose);
403 if (arg->tok != tok_string && arg->tok != tok_ident)
404 goto err_label;
405 /* Next is a semicolon. */
406 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
407 if (cattok->tok != tok_semicolon)
408 goto err_label;
409 /* Now a LC_xxx identifier. */
410 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
411 switch (cattok->tok)
413 #define CATEGORY(lname, uname) \
414 case tok_lc_##lname: \
415 category = LC_##uname; \
416 break
418 CATEGORY (identification, IDENTIFICATION);
419 CATEGORY (ctype, CTYPE);
420 CATEGORY (collate, COLLATE);
421 CATEGORY (time, TIME);
422 CATEGORY (numeric, NUMERIC);
423 CATEGORY (monetary, MONETARY);
424 CATEGORY (messages, MESSAGES);
425 CATEGORY (paper, PAPER);
426 CATEGORY (name, NAME);
427 CATEGORY (address, ADDRESS);
428 CATEGORY (telephone, TELEPHONE);
429 CATEGORY (measurement, MEASUREMENT);
431 default:
432 goto err_label;
434 if (identification->category[category] != NULL)
436 lr_error (ldfile, _("\
437 %s: duplicate category version definition"), "LC_IDENTIFICATION");
438 free (arg->val.str.startmb);
440 else
441 identification->category[category] = arg->val.str.startmb;
442 break;
444 case tok_end:
445 /* Next we assume `LC_IDENTIFICATION'. */
446 arg = lr_token (ldfile, charmap, result, NULL, verbose);
447 if (arg->tok == tok_eof)
448 break;
449 if (arg->tok == tok_eol)
450 lr_error (ldfile, _("%s: incomplete `END' line"),
451 "LC_IDENTIFICATION");
452 else if (arg->tok != tok_lc_identification)
453 lr_error (ldfile, _("\
454 %1$s: definition does not end with `END %1$s'"), "LC_IDENTIFICATION");
455 lr_ignore_rest (ldfile, arg->tok == tok_lc_identification);
456 return;
458 default:
459 err_label:
460 SYNTAX_ERROR (_("%s: syntax error"), "LC_IDENTIFICATION");
463 /* Prepare for the next round. */
464 now = lr_token (ldfile, charmap, result, NULL, verbose);
465 nowtok = now->tok;
468 /* When we come here we reached the end of the file. */
469 lr_error (ldfile, _("%s: premature end of file"), "LC_IDENTIFICATION");