Fix exit condition.
[glibc/pb-stable.git] / locale / programs / ld-identification.c
blobae6a6068883dcacfc8b94964a1a2948dcd0e82d9
1 /* Copyright (C) 1998,1999,2000,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <error.h>
24 #include <langinfo.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localedef.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,
99 const struct charmap_t *charmap)
101 struct locale_identification_t *identification
102 = locale->categories[LC_IDENTIFICATION].identification;
103 int nothing = 0;
104 size_t num;
106 /* Now resolve copying and also handle completely missing definitions. */
107 if (identification == NULL)
109 /* First see whether we were supposed to copy. If yes, find the
110 actual definition. */
111 if (locale->copy_name[LC_IDENTIFICATION] != NULL)
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;
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);
124 identification = locale->categories[LC_IDENTIFICATION].identification
125 = from->categories[LC_IDENTIFICATION].identification;
128 /* If there is still no definition issue an warning and create an
129 empty one. */
130 if (identification == NULL)
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;
142 #define TEST_ELEM(cat) \
143 if (identification->cat == NULL) \
145 if (verbose && ! nothing) \
146 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
147 "LC_IDENTIFICATION", #cat)); \
148 identification->cat = ""; \
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);
166 for (num = 0; num < __LC_LAST; ++num)
167 if (num != LC_ALL && identification->category[num] == NULL)
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] = "";
178 void
179 identification_output (struct localedef_t *locale,
180 const struct charmap_t *charmap,
181 const char *output_path)
183 struct locale_identification_t *identification
184 = locale->categories[LC_IDENTIFICATION].identification;
185 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)
186 + (__LC_LAST - 2)];
187 struct locale_file data;
188 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)];
189 size_t cnt = 0;
190 size_t num;
191 size_t last_idx;
193 data.magic = LIMAGIC (LC_IDENTIFICATION);
194 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION);
195 iov[cnt].iov_base = (void *) &data;
196 iov[cnt].iov_len = sizeof (data);
197 ++cnt;
199 iov[cnt].iov_base = (void *) idx;
200 iov[cnt].iov_len = sizeof (idx);
201 ++cnt;
203 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
204 iov[cnt].iov_base = (void *) identification->title;
205 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
206 ++cnt;
208 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
209 iov[cnt].iov_base = (void *) identification->source;
210 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
211 ++cnt;
213 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
214 iov[cnt].iov_base = (void *) identification->address;
215 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
216 ++cnt;
218 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
219 iov[cnt].iov_base = (void *) identification->contact;
220 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
221 ++cnt;
223 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
224 iov[cnt].iov_base = (void *) identification->email;
225 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
226 ++cnt;
228 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
229 iov[cnt].iov_base = (void *) identification->tel;
230 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
231 ++cnt;
233 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
234 iov[cnt].iov_base = (void *) identification->fax;
235 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
236 ++cnt;
238 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
239 iov[cnt].iov_base = (void *) identification->language;
240 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
241 ++cnt;
243 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
244 iov[cnt].iov_base = (void *) identification->territory;
245 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
246 ++cnt;
248 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
249 iov[cnt].iov_base = (void *) identification->audience;
250 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
251 ++cnt;
253 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
254 iov[cnt].iov_base = (void *) identification->application;
255 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
256 ++cnt;
258 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
259 iov[cnt].iov_base = (void *) identification->abbreviation;
260 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
261 ++cnt;
263 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
264 iov[cnt].iov_base = (void *) identification->revision;
265 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
266 ++cnt;
268 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
269 iov[cnt].iov_base = (void *) identification->date;
270 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
271 ++cnt;
273 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
274 last_idx = cnt - 1;
275 idx[last_idx] = idx[cnt - 2];
276 for (num = 0; num < __LC_LAST; ++num)
277 if (num != LC_ALL)
279 iov[cnt].iov_base = (void *) identification->category[num];
280 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
281 idx[last_idx] += iov[cnt].iov_len;
282 ++cnt;
285 assert (last_idx == _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION) - 1);
286 iov[cnt].iov_base = (void *) charmap->code_set_name;
287 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
288 ++cnt;
290 assert (cnt == (2 + _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)
291 + (__LC_LAST - 2)));
293 write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION", cnt,
294 iov);
298 /* The parser for the LC_IDENTIFICATION section of the locale definition. */
299 void
300 identification_read (struct linereader *ldfile, struct localedef_t *result,
301 const struct charmap_t *charmap, const char *repertoire_name,
302 int ignore_content)
304 struct locale_identification_t *identification;
305 struct token *now;
306 struct token *arg;
307 struct token *cattok;
308 int category;
309 enum token_t nowtok;
311 /* The rest of the line containing `LC_IDENTIFICATION' must be free. */
312 lr_ignore_rest (ldfile, 1);
316 now = lr_token (ldfile, charmap, result, NULL, verbose);
317 nowtok = now->tok;
319 while (nowtok == tok_eol);
321 /* If we see `copy' now we are almost done. */
322 if (nowtok == tok_copy)
324 handle_copy (ldfile, charmap, repertoire_name, result,
325 tok_lc_identification, LC_IDENTIFICATION,
326 "LC_IDENTIFICATION", ignore_content);
327 return;
330 /* Prepare the data structures. */
331 identification_startup (ldfile, result, ignore_content);
332 identification = result->categories[LC_IDENTIFICATION].identification;
334 while (1)
336 /* Of course we don't proceed beyond the end of file. */
337 if (nowtok == tok_eof)
338 break;
340 /* Ignore empty lines. */
341 if (nowtok == tok_eol)
343 now = lr_token (ldfile, charmap, result, NULL, verbose);
344 nowtok = now->tok;
345 continue;
348 switch (nowtok)
350 #define STR_ELEM(cat) \
351 case tok_##cat: \
352 /* Ignore the rest of the line if we don't need the input of \
353 this line. */ \
354 if (ignore_content) \
356 lr_ignore_rest (ldfile, 0); \
357 break; \
360 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
361 if (arg->tok != tok_string) \
362 goto err_label; \
363 if (identification->cat != NULL) \
364 lr_error (ldfile, _("\
365 %s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \
366 else if (!ignore_content && arg->val.str.startmb == NULL) \
368 lr_error (ldfile, _("\
369 %s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \
370 identification->cat = ""; \
372 else if (!ignore_content) \
373 identification->cat = arg->val.str.startmb; \
374 break
376 STR_ELEM (title);
377 STR_ELEM (source);
378 STR_ELEM (address);
379 STR_ELEM (contact);
380 STR_ELEM (email);
381 STR_ELEM (tel);
382 STR_ELEM (fax);
383 STR_ELEM (language);
384 STR_ELEM (territory);
385 STR_ELEM (audience);
386 STR_ELEM (application);
387 STR_ELEM (abbreviation);
388 STR_ELEM (revision);
389 STR_ELEM (date);
391 case tok_category:
392 /* Ignore the rest of the line if we don't need the input of
393 this line. */
394 if (ignore_content)
396 lr_ignore_rest (ldfile, 0);
397 break;
400 /* We expect two operands. */
401 arg = lr_token (ldfile, charmap, result, NULL, verbose);
402 if (arg->tok != tok_string && arg->tok != tok_ident)
403 goto err_label;
404 /* Next is a semicolon. */
405 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
406 if (cattok->tok != tok_semicolon)
407 goto err_label;
408 /* Now a LC_xxx identifier. */
409 cattok = lr_token (ldfile, charmap, result, NULL, verbose);
410 switch (cattok->tok)
412 #define CATEGORY(lname, uname) \
413 case tok_lc_##lname: \
414 category = LC_##uname; \
415 break
417 CATEGORY (identification, IDENTIFICATION);
418 CATEGORY (ctype, CTYPE);
419 CATEGORY (collate, COLLATE);
420 CATEGORY (time, TIME);
421 CATEGORY (numeric, NUMERIC);
422 CATEGORY (monetary, MONETARY);
423 CATEGORY (messages, MESSAGES);
424 CATEGORY (paper, PAPER);
425 CATEGORY (name, NAME);
426 CATEGORY (address, ADDRESS);
427 CATEGORY (telephone, TELEPHONE);
428 CATEGORY (measurement, MEASUREMENT);
430 default:
431 goto err_label;
433 if (identification->category[category] != NULL)
435 lr_error (ldfile, _("\
436 %s: duplicate category version definition"), "LC_IDENTIFICATION");
437 free (arg->val.str.startmb);
439 else
440 identification->category[category] = arg->val.str.startmb;
441 break;
443 case tok_end:
444 /* Next we assume `LC_IDENTIFICATION'. */
445 arg = lr_token (ldfile, charmap, result, NULL, verbose);
446 if (arg->tok == tok_eof)
447 break;
448 if (arg->tok == tok_eol)
449 lr_error (ldfile, _("%s: incomplete `END' line"),
450 "LC_IDENTIFICATION");
451 else if (arg->tok != tok_lc_identification)
452 lr_error (ldfile, _("\
453 %1$s: definition does not end with `END %1$s'"), "LC_IDENTIFICATION");
454 lr_ignore_rest (ldfile, arg->tok == tok_lc_identification);
455 return;
457 default:
458 err_label:
459 SYNTAX_ERROR (_("%s: syntax error"), "LC_IDENTIFICATION");
462 /* Prepare for the next round. */
463 now = lr_token (ldfile, charmap, result, NULL, verbose);
464 nowtok = now->tok;
467 /* When we come here we reached the end of the file. */
468 lr_error (ldfile, _("%s: premature end of file"), "LC_IDENTIFICATION");