]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-time.c
Implement alternative month names (bug 10871).
[thirdparty/glibc.git] / locale / programs / ld-time.c
1 /* Copyright (C) 1995-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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 <byteswap.h>
23 #include <langinfo.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wchar.h>
27 #include <stdint.h>
28 #include <sys/uio.h>
29
30 #include <assert.h>
31
32 #include "localedef.h"
33 #include "linereader.h"
34 #include "localeinfo.h"
35 #include "locfile.h"
36
37
38 /* Entry describing an entry of the era specification. */
39 struct era_data
40 {
41 int32_t direction;
42 int32_t offset;
43 int32_t start_date[3];
44 int32_t stop_date[3];
45 const char *name;
46 const char *format;
47 uint32_t *wname;
48 uint32_t *wformat;
49 };
50
51
52 /* The real definition of the struct for the LC_TIME locale. */
53 struct locale_time_t
54 {
55 const char *abday[7];
56 const uint32_t *wabday[7];
57 int abday_defined;
58 const char *day[7];
59 const uint32_t *wday[7];
60 int day_defined;
61 const char *abmon[12];
62 const uint32_t *wabmon[12];
63 int abmon_defined;
64 const char *mon[12];
65 const uint32_t *wmon[12];
66 int mon_defined;
67 const char *am_pm[2];
68 const uint32_t *wam_pm[2];
69 int am_pm_defined;
70 const char *d_t_fmt;
71 const uint32_t *wd_t_fmt;
72 const char *d_fmt;
73 const uint32_t *wd_fmt;
74 const char *t_fmt;
75 const uint32_t *wt_fmt;
76 const char *t_fmt_ampm;
77 const uint32_t *wt_fmt_ampm;
78 const char **era;
79 const uint32_t **wera;
80 uint32_t num_era;
81 const char *era_year;
82 const uint32_t *wera_year;
83 const char *era_d_t_fmt;
84 const uint32_t *wera_d_t_fmt;
85 const char *era_t_fmt;
86 const uint32_t *wera_t_fmt;
87 const char *era_d_fmt;
88 const uint32_t *wera_d_fmt;
89 const char *alt_digits[100];
90 const uint32_t *walt_digits[100];
91 const char *date_fmt;
92 const uint32_t *wdate_fmt;
93 int alt_digits_defined;
94 const char *alt_mon[12];
95 const uint32_t *walt_mon[12];
96 int alt_mon_defined;
97 unsigned char week_ndays;
98 uint32_t week_1stday;
99 unsigned char week_1stweek;
100 unsigned char first_weekday;
101 unsigned char first_workday;
102 unsigned char cal_direction;
103 const char *timezone;
104 const uint32_t *wtimezone;
105
106 struct era_data *era_entries;
107 };
108
109
110 /* This constant is used to represent an empty wide character string. */
111 static const uint32_t empty_wstr[1] = { 0 };
112
113
114 static void
115 time_startup (struct linereader *lr, struct localedef_t *locale,
116 int ignore_content)
117 {
118 if (!ignore_content)
119 locale->categories[LC_TIME].time =
120 (struct locale_time_t *) xcalloc (1, sizeof (struct locale_time_t));
121
122 if (lr != NULL)
123 {
124 lr->translate_strings = 1;
125 lr->return_widestr = 1;
126 }
127 }
128
129
130 void
131 time_finish (struct localedef_t *locale, const struct charmap_t *charmap)
132 {
133 struct locale_time_t *time = locale->categories[LC_TIME].time;
134 int nothing = 0;
135
136 /* Now resolve copying and also handle completely missing definitions. */
137 if (time == NULL)
138 {
139 /* First see whether we were supposed to copy. If yes, find the
140 actual definition. */
141 if (locale->copy_name[LC_TIME] != NULL)
142 {
143 /* Find the copying locale. This has to happen transitively since
144 the locale we are copying from might also copying another one. */
145 struct localedef_t *from = locale;
146
147 do
148 from = find_locale (LC_TIME, from->copy_name[LC_TIME],
149 from->repertoire_name, charmap);
150 while (from->categories[LC_TIME].time == NULL
151 && from->copy_name[LC_TIME] != NULL);
152
153 time = locale->categories[LC_TIME].time
154 = from->categories[LC_TIME].time;
155 }
156
157 /* If there is still no definition issue an warning and create an
158 empty one. */
159 if (time == NULL)
160 {
161 record_warning (_("\
162 No definition for %s category found"), "LC_TIME");
163 time_startup (NULL, locale, 0);
164 time = locale->categories[LC_TIME].time;
165 nothing = 1;
166 }
167 }
168
169 #define noparen(arg1, argn...) arg1, ##argn
170 #define TESTARR_ELEM(cat, val) \
171 if (!time->cat##_defined) \
172 { \
173 const char *initval[] = { noparen val }; \
174 unsigned int i; \
175 \
176 if (! nothing) \
177 record_error (0, 0, _("%s: field `%s' not defined"), \
178 "LC_TIME", #cat); \
179 \
180 for (i = 0; i < sizeof (initval) / sizeof (initval[0]); ++i) \
181 time->cat[i] = initval[i]; \
182 }
183
184 TESTARR_ELEM (abday, ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ));
185 TESTARR_ELEM (day, ( "Sunday", "Monday", "Tuesday", "Wednesday",
186 "Thursday", "Friday", "Saturday" ));
187 TESTARR_ELEM (abmon, ( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
188 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ));
189 TESTARR_ELEM (mon, ( "January", "February", "March", "April",
190 "May", "June", "July", "August",
191 "September", "October", "November", "December" ));
192 TESTARR_ELEM (am_pm, ( "AM", "PM" ));
193
194 #define TEST_ELEM(cat, initval) \
195 if (time->cat == NULL) \
196 { \
197 if (! nothing) \
198 record_error (0, 0, _("%s: field `%s' not defined"), \
199 "LC_TIME", #cat); \
200 \
201 time->cat = initval; \
202 }
203
204 TEST_ELEM (d_t_fmt, "%a %b %e %H:%M:%S %Y");
205 TEST_ELEM (d_fmt, "%m/%d/%y");
206 TEST_ELEM (t_fmt, "%H:%M:%S");
207
208 /* According to C.Y.Alexis Cheng <alexis@vnet.ibm.com> the T_FMT_AMPM
209 field is optional. */
210 if (time->t_fmt_ampm == NULL)
211 {
212 if (time->am_pm[0][0] == '\0' && time->am_pm[1][0] == '\0')
213 {
214 /* No AM/PM strings defined, use the 24h format as default. */
215 time->t_fmt_ampm = time->t_fmt;
216 time->wt_fmt_ampm = time->wt_fmt;
217 }
218 else
219 {
220 time->t_fmt_ampm = "%I:%M:%S %p";
221 time->wt_fmt_ampm = (const uint32_t *) L"%I:%M:%S %p";
222 }
223 }
224
225 /* Now process the era entries. */
226 if (time->num_era != 0)
227 {
228 const int days_per_month[12] = { 31, 29, 31, 30, 31, 30,
229 31, 31, 30, 31 ,30, 31 };
230 size_t idx;
231 wchar_t *wstr;
232
233 time->era_entries =
234 (struct era_data *) xmalloc (time->num_era
235 * sizeof (struct era_data));
236
237 for (idx = 0; idx < time->num_era; ++idx)
238 {
239 size_t era_len = strlen (time->era[idx]);
240 char *str = xmalloc ((era_len + 1 + 3) & ~3);
241 char *endp;
242
243 memcpy (str, time->era[idx], era_len + 1);
244
245 /* First character must be + or - for the direction. */
246 if (*str != '+' && *str != '-')
247 {
248 record_error (0, 0, _("\
249 %s: direction flag in string %Zd in `era' field is not '+' nor '-'"),
250 "LC_TIME", idx + 1);
251 /* Default arbitrarily to '+'. */
252 time->era_entries[idx].direction = '+';
253 }
254 else
255 time->era_entries[idx].direction = *str;
256 if (*++str != ':')
257 {
258 record_error (0, 0, _("\
259 %s: direction flag in string %Zd in `era' field is not a single character"),
260 "LC_TIME", idx + 1);
261 (void) strsep (&str, ":");
262 }
263 else
264 ++str;
265
266 /* Now the offset year. */
267 time->era_entries[idx].offset = strtol (str, &endp, 10);
268 if (endp == str)
269 {
270 record_error (0, 0, _("\
271 %s: invalid number for offset in string %Zd in `era' field"),
272 "LC_TIME", idx + 1);
273 (void) strsep (&str, ":");
274 }
275 else if (*endp != ':')
276 {
277 record_error (0, 0, _("\
278 %s: garbage at end of offset value in string %Zd in `era' field"),
279 "LC_TIME", idx + 1);
280 (void) strsep (&str, ":");
281 }
282 else
283 str = endp + 1;
284
285 /* Next is the starting date in ISO format. */
286 if (strncmp (str, "-*", 2) == 0)
287 {
288 time->era_entries[idx].start_date[0] =
289 time->era_entries[idx].start_date[1] =
290 time->era_entries[idx].start_date[2] = 0x80000000;
291 if (str[2] != ':')
292 goto garbage_start_date;
293 str += 3;
294 }
295 else if (strncmp (str, "+*", 2) == 0)
296 {
297 time->era_entries[idx].start_date[0] =
298 time->era_entries[idx].start_date[1] =
299 time->era_entries[idx].start_date[2] = 0x7fffffff;
300 if (str[2] != ':')
301 goto garbage_start_date;
302 str += 3;
303 }
304 else
305 {
306 time->era_entries[idx].start_date[0] = strtol (str, &endp, 10);
307 if (endp == str || *endp != '/')
308 goto invalid_start_date;
309 else
310 str = endp + 1;
311 time->era_entries[idx].start_date[0] -= 1900;
312 /* year -1 represent 1 B.C. (not -1 A.D.) */
313 if (time->era_entries[idx].start_date[0] < -1900)
314 ++time->era_entries[idx].start_date[0];
315
316 time->era_entries[idx].start_date[1] = strtol (str, &endp, 10);
317 if (endp == str || *endp != '/')
318 goto invalid_start_date;
319 else
320 str = endp + 1;
321 time->era_entries[idx].start_date[1] -= 1;
322
323 time->era_entries[idx].start_date[2] = strtol (str, &endp, 10);
324 if (endp == str)
325 {
326 invalid_start_date:
327 record_error (0, 0, _("\
328 %s: invalid starting date in string %Zd in `era' field"),
329 "LC_TIME", idx + 1);
330 (void) strsep (&str, ":");
331 }
332 else if (*endp != ':')
333 {
334 garbage_start_date:
335 record_error (0, 0, _("\
336 %s: garbage at end of starting date in string %Zd in `era' field "),
337 "LC_TIME", idx + 1);
338 (void) strsep (&str, ":");
339 }
340 else
341 {
342 str = endp + 1;
343
344 /* Check for valid value. */
345 if ((time->era_entries[idx].start_date[1] < 0
346 || time->era_entries[idx].start_date[1] >= 12
347 || time->era_entries[idx].start_date[2] < 0
348 || (time->era_entries[idx].start_date[2]
349 > days_per_month[time->era_entries[idx].start_date[1]])
350 || (time->era_entries[idx].start_date[1] == 2
351 && time->era_entries[idx].start_date[2] == 29
352 && !__isleap (time->era_entries[idx].start_date[0]))))
353 record_error (0, 0, _("\
354 %s: starting date is invalid in string %Zd in `era' field"),
355 "LC_TIME", idx + 1);
356 }
357 }
358
359 /* Next is the stopping date in ISO format. */
360 if (strncmp (str, "-*", 2) == 0)
361 {
362 time->era_entries[idx].stop_date[0] =
363 time->era_entries[idx].stop_date[1] =
364 time->era_entries[idx].stop_date[2] = 0x80000000;
365 if (str[2] != ':')
366 goto garbage_stop_date;
367 str += 3;
368 }
369 else if (strncmp (str, "+*", 2) == 0)
370 {
371 time->era_entries[idx].stop_date[0] =
372 time->era_entries[idx].stop_date[1] =
373 time->era_entries[idx].stop_date[2] = 0x7fffffff;
374 if (str[2] != ':')
375 goto garbage_stop_date;
376 str += 3;
377 }
378 else
379 {
380 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
381 if (endp == str || *endp != '/')
382 goto invalid_stop_date;
383 else
384 str = endp + 1;
385 time->era_entries[idx].stop_date[0] -= 1900;
386 /* year -1 represent 1 B.C. (not -1 A.D.) */
387 if (time->era_entries[idx].stop_date[0] < -1900)
388 ++time->era_entries[idx].stop_date[0];
389
390 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
391 if (endp == str || *endp != '/')
392 goto invalid_stop_date;
393 else
394 str = endp + 1;
395 time->era_entries[idx].stop_date[1] -= 1;
396
397 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
398 if (endp == str)
399 {
400 invalid_stop_date:
401 record_error (0, 0, _("\
402 %s: invalid stopping date in string %Zd in `era' field"),
403 "LC_TIME", idx + 1);
404 (void) strsep (&str, ":");
405 }
406 else if (*endp != ':')
407 {
408 garbage_stop_date:
409 record_error (0, 0, _("\
410 %s: garbage at end of stopping date in string %Zd in `era' field"),
411 "LC_TIME", idx + 1);
412 (void) strsep (&str, ":");
413 }
414 else
415 {
416 str = endp + 1;
417
418 /* Check for valid value. */
419 if ((time->era_entries[idx].stop_date[1] < 0
420 || time->era_entries[idx].stop_date[1] >= 12
421 || time->era_entries[idx].stop_date[2] < 0
422 || (time->era_entries[idx].stop_date[2]
423 > days_per_month[time->era_entries[idx].stop_date[1]])
424 || (time->era_entries[idx].stop_date[1] == 2
425 && time->era_entries[idx].stop_date[2] == 29
426 && !__isleap (time->era_entries[idx].stop_date[0]))))
427 record_error (0, 0, _("\
428 %s: invalid stopping date in string %Zd in `era' field"),
429 "LC_TIME", idx + 1);
430 }
431 }
432
433 if (str == NULL || *str == '\0')
434 {
435 record_error (0, 0, _("\
436 %s: missing era name in string %Zd in `era' field"), "LC_TIME", idx + 1);
437 time->era_entries[idx].name =
438 time->era_entries[idx].format = "";
439 }
440 else
441 {
442 time->era_entries[idx].name = strsep (&str, ":");
443
444 if (str == NULL || *str == '\0')
445 {
446 record_error (0, 0, _("\
447 %s: missing era format in string %Zd in `era' field"),
448 "LC_TIME", idx + 1);
449 time->era_entries[idx].name =
450 time->era_entries[idx].format = "";
451 }
452 else
453 time->era_entries[idx].format = str;
454 }
455
456 /* Now generate the wide character name and format. */
457 wstr = wcschr ((wchar_t *) time->wera[idx], L':');/* end direction */
458 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end offset */
459 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end start */
460 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end end */
461 if (wstr != NULL)
462 {
463 time->era_entries[idx].wname = (uint32_t *) wstr + 1;
464 wstr = wcschr (wstr + 1, L':'); /* end name */
465 if (wstr != NULL)
466 {
467 *wstr = L'\0';
468 time->era_entries[idx].wformat = (uint32_t *) wstr + 1;
469 }
470 else
471 time->era_entries[idx].wname =
472 time->era_entries[idx].wformat = (uint32_t *) L"";
473 }
474 else
475 time->era_entries[idx].wname =
476 time->era_entries[idx].wformat = (uint32_t *) L"";
477 }
478 }
479
480 /* Set up defaults based on ISO 30112 WD10 [2014]. */
481 if (time->week_ndays == 0)
482 time->week_ndays = 7;
483
484 if (time->week_1stday == 0)
485 time->week_1stday = 19971130;
486
487 if (time->week_1stweek == 0)
488 time->week_1stweek = 7;
489
490 if (time->week_1stweek > time->week_ndays)
491 record_error (0, 0, _("\
492 %s: third operand for value of field `%s' must not be larger than %d"),
493 "LC_TIME", "week", 7);
494
495 if (time->first_weekday == '\0')
496 /* The definition does not specify this so the default is used. */
497 time->first_weekday = 1;
498 else if (time->first_weekday > time->week_ndays)
499 record_error (0, 0, _("\
500 %s: values for field `%s' must not be larger than %d"),
501 "LC_TIME", "first_weekday", 7);
502
503 if (time->first_workday == '\0')
504 /* The definition does not specify this so the default is used. */
505 time->first_workday = 2;
506 else if (time->first_workday > time->week_ndays)
507 record_error (0, 0, _("\
508 %s: values for field `%s' must not be larger than %d"),
509 "LC_TIME", "first_workday", 7);
510
511 if (time->cal_direction == '\0')
512 /* The definition does not specify this so the default is used. */
513 time->cal_direction = 1;
514 else if (time->cal_direction > 3)
515 record_error (0, 0, _("\
516 %s: values for field `%s' must not be larger than %d"),
517 "LC_TIME", "cal_direction", 3);
518
519 /* XXX We don't perform any tests on the timezone value since this is
520 simply useless, stupid $&$!@... */
521 if (time->timezone == NULL)
522 time->timezone = "";
523
524 if (time->date_fmt == NULL)
525 time->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
526 if (time->wdate_fmt == NULL)
527 time->wdate_fmt = (const uint32_t *) L"%a %b %e %H:%M:%S %Z %Y";
528 }
529
530
531 void
532 time_output (struct localedef_t *locale, const struct charmap_t *charmap,
533 const char *output_path)
534 {
535 struct locale_time_t *time = locale->categories[LC_TIME].time;
536 struct locale_file file;
537 size_t num, n;
538
539 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
540
541 /* The ab'days. */
542 for (n = 0; n < 7; ++n)
543 add_locale_string (&file, time->abday[n] ?: "");
544
545 /* The days. */
546 for (n = 0; n < 7; ++n)
547 add_locale_string (&file, time->day[n] ?: "");
548
549 /* The ab'mons. */
550 for (n = 0; n < 12; ++n)
551 add_locale_string (&file, time->abmon[n] ?: "");
552
553 /* The mons. */
554 for (n = 0; n < 12; ++n)
555 add_locale_string (&file, time->mon[n] ?: "");
556
557 /* AM/PM. */
558 for (n = 0; n < 2; ++n)
559 add_locale_string (&file, time->am_pm[n]);
560
561 add_locale_string (&file, time->d_t_fmt ?: "");
562 add_locale_string (&file, time->d_fmt ?: "");
563 add_locale_string (&file, time->t_fmt ?: "");
564 add_locale_string (&file, time->t_fmt_ampm ?: "");
565
566 start_locale_structure (&file);
567 for (num = 0; num < time->num_era; ++num)
568 add_locale_string (&file, time->era[num]);
569 end_locale_structure (&file);
570
571 add_locale_string (&file, time->era_year ?: "");
572 add_locale_string (&file, time->era_d_fmt ?: "");
573
574 start_locale_structure (&file);
575 for (num = 0; num < 100; ++num)
576 add_locale_string (&file, time->alt_digits[num] ?: "");
577 end_locale_structure (&file);
578
579 add_locale_string (&file, time->era_d_t_fmt ?: "");
580 add_locale_string (&file, time->era_t_fmt ?: "");
581 add_locale_uint32 (&file, time->num_era);
582
583 start_locale_structure (&file);
584 for (num = 0; num < time->num_era; ++num)
585 {
586 add_locale_uint32 (&file, time->era_entries[num].direction);
587 add_locale_uint32 (&file, time->era_entries[num].offset);
588 add_locale_uint32 (&file, time->era_entries[num].start_date[0]);
589 add_locale_uint32 (&file, time->era_entries[num].start_date[1]);
590 add_locale_uint32 (&file, time->era_entries[num].start_date[2]);
591 add_locale_uint32 (&file, time->era_entries[num].stop_date[0]);
592 add_locale_uint32 (&file, time->era_entries[num].stop_date[1]);
593 add_locale_uint32 (&file, time->era_entries[num].stop_date[2]);
594 add_locale_string (&file, time->era_entries[num].name);
595 add_locale_string (&file, time->era_entries[num].format);
596 add_locale_wstring (&file, time->era_entries[num].wname);
597 add_locale_wstring (&file, time->era_entries[num].wformat);
598 }
599 end_locale_structure (&file);
600
601 /* The wide character ab'days. */
602 for (n = 0; n < 7; ++n)
603 add_locale_wstring (&file, time->wabday[n] ?: empty_wstr);
604
605 /* The wide character days. */
606 for (n = 0; n < 7; ++n)
607 add_locale_wstring (&file, time->wday[n] ?: empty_wstr);
608
609 /* The wide character ab'mons. */
610 for (n = 0; n < 12; ++n)
611 add_locale_wstring (&file, time->wabmon[n] ?: empty_wstr);
612
613 /* The wide character mons. */
614 for (n = 0; n < 12; ++n)
615 add_locale_wstring (&file, time->wmon[n] ?: empty_wstr);
616
617 /* Wide character AM/PM. */
618 for (n = 0; n < 2; ++n)
619 add_locale_wstring (&file, time->wam_pm[n] ?: empty_wstr);
620
621 add_locale_wstring (&file, time->wd_t_fmt ?: empty_wstr);
622 add_locale_wstring (&file, time->wd_fmt ?: empty_wstr);
623 add_locale_wstring (&file, time->wt_fmt ?: empty_wstr);
624 add_locale_wstring (&file, time->wt_fmt_ampm ?: empty_wstr);
625 add_locale_wstring (&file, time->wera_year ?: empty_wstr);
626 add_locale_wstring (&file, time->wera_d_fmt ?: empty_wstr);
627
628 start_locale_structure (&file);
629 for (num = 0; num < 100; ++num)
630 add_locale_wstring (&file, time->walt_digits[num] ?: empty_wstr);
631 end_locale_structure (&file);
632
633 add_locale_wstring (&file, time->wera_d_t_fmt ?: empty_wstr);
634 add_locale_wstring (&file, time->wera_t_fmt ?: empty_wstr);
635 add_locale_char (&file, time->week_ndays);
636 add_locale_uint32 (&file, time->week_1stday);
637 add_locale_char (&file, time->week_1stweek);
638 add_locale_char (&file, time->first_weekday);
639 add_locale_char (&file, time->first_workday);
640 add_locale_char (&file, time->cal_direction);
641 add_locale_string (&file, time->timezone);
642 add_locale_string (&file, time->date_fmt);
643 add_locale_wstring (&file, time->wdate_fmt);
644 add_locale_string (&file, charmap->code_set_name);
645
646 /* The alt'mons. */
647 for (n = 0; n < 12; ++n)
648 add_locale_string (&file, time->alt_mon[n] ?: "");
649
650 /* The wide character alt'mons. */
651 for (n = 0; n < 12; ++n)
652 add_locale_wstring (&file, time->walt_mon[n] ?: empty_wstr);
653
654 write_locale_data (output_path, LC_TIME, "LC_TIME", &file);
655 }
656
657
658 /* The parser for the LC_TIME section of the locale definition. */
659 void
660 time_read (struct linereader *ldfile, struct localedef_t *result,
661 const struct charmap_t *charmap, const char *repertoire_name,
662 int ignore_content)
663 {
664 struct repertoire_t *repertoire = NULL;
665 struct locale_time_t *time;
666 struct token *now;
667 enum token_t nowtok;
668 size_t cnt;
669
670 /* Get the repertoire we have to use. */
671 if (repertoire_name != NULL)
672 repertoire = repertoire_read (repertoire_name);
673
674 /* The rest of the line containing `LC_TIME' must be free. */
675 lr_ignore_rest (ldfile, 1);
676
677
678 do
679 {
680 now = lr_token (ldfile, charmap, result, repertoire, verbose);
681 nowtok = now->tok;
682 }
683 while (nowtok == tok_eol);
684
685 /* If we see `copy' now we are almost done. */
686 if (nowtok == tok_copy)
687 {
688 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
689 LC_TIME, "LC_TIME", ignore_content);
690 return;
691 }
692
693 /* Prepare the data structures. */
694 time_startup (ldfile, result, ignore_content);
695 time = result->categories[LC_TIME].time;
696
697 while (1)
698 {
699 /* Of course we don't proceed beyond the end of file. */
700 if (nowtok == tok_eof)
701 break;
702
703 /* Ingore empty lines. */
704 if (nowtok == tok_eol)
705 {
706 now = lr_token (ldfile, charmap, result, repertoire, verbose);
707 nowtok = now->tok;
708 continue;
709 }
710
711 switch (nowtok)
712 {
713 #define STRARR_ELEM(cat, min, max) \
714 case tok_##cat: \
715 /* Ignore the rest of the line if we don't need the input of \
716 this line. */ \
717 if (ignore_content) \
718 { \
719 lr_ignore_rest (ldfile, 0); \
720 break; \
721 } \
722 \
723 for (cnt = 0; cnt < max; ++cnt) \
724 { \
725 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
726 if (now->tok == tok_eol) \
727 { \
728 if (cnt < min) \
729 lr_error (ldfile, _("%s: too few values for field `%s'"), \
730 "LC_TIME", #cat); \
731 if (!ignore_content) \
732 do \
733 { \
734 time->cat[cnt] = ""; \
735 time->w##cat[cnt] = empty_wstr; \
736 } \
737 while (++cnt < max); \
738 break; \
739 } \
740 else if (now->tok != tok_string) \
741 goto err_label; \
742 else if (!ignore_content && (now->val.str.startmb == NULL \
743 || now->val.str.startwc == NULL)) \
744 { \
745 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
746 "LC_TIME", #cat); \
747 time->cat[cnt] = ""; \
748 time->w##cat[cnt] = empty_wstr; \
749 } \
750 else if (!ignore_content) \
751 { \
752 time->cat[cnt] = now->val.str.startmb; \
753 time->w##cat[cnt] = now->val.str.startwc; \
754 } \
755 \
756 /* Match the semicolon. */ \
757 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
758 if (now->tok != tok_semicolon && now->tok != tok_eol) \
759 break; \
760 } \
761 if (now->tok != tok_eol) \
762 { \
763 while (!ignore_content && cnt < min) \
764 { \
765 time->cat[cnt] = ""; \
766 time->w##cat[cnt++] = empty_wstr; \
767 } \
768 \
769 if (now->tok == tok_semicolon) \
770 { \
771 now = lr_token (ldfile, charmap, result, repertoire, \
772 verbose); \
773 if (now->tok == tok_eol) \
774 lr_error (ldfile, _("extra trailing semicolon")); \
775 else if (now->tok == tok_string) \
776 { \
777 lr_error (ldfile, _("\
778 %s: too many values for field `%s'"), \
779 "LC_TIME", #cat); \
780 lr_ignore_rest (ldfile, 0); \
781 } \
782 else \
783 goto err_label; \
784 } \
785 else \
786 goto err_label; \
787 } \
788 time->cat##_defined = 1; \
789 break
790
791 STRARR_ELEM (abday, 7, 7);
792 STRARR_ELEM (day, 7, 7);
793 STRARR_ELEM (abmon, 12, 12);
794 STRARR_ELEM (mon, 12, 12);
795 STRARR_ELEM (am_pm, 2, 2);
796 STRARR_ELEM (alt_digits, 0, 100);
797 STRARR_ELEM (alt_mon, 12, 12);
798
799 case tok_era:
800 /* Ignore the rest of the line if we don't need the input of
801 this line. */
802 if (ignore_content)
803 {
804 lr_ignore_rest (ldfile, 0);
805 break;
806 }
807 do
808 {
809 now = lr_token (ldfile, charmap, result, repertoire, verbose);
810 if (now->tok != tok_string)
811 goto err_label;
812 if (!ignore_content && (now->val.str.startmb == NULL
813 || now->val.str.startwc == NULL))
814 {
815 lr_error (ldfile, _("%s: unknown character in field `%s'"),
816 "LC_TIME", "era");
817 lr_ignore_rest (ldfile, 0);
818 break;
819 }
820 if (!ignore_content)
821 {
822 time->era = xrealloc (time->era,
823 (time->num_era + 1) * sizeof (char *));
824 time->era[time->num_era] = now->val.str.startmb;
825
826 time->wera = xrealloc (time->wera,
827 (time->num_era + 1)
828 * sizeof (char *));
829 time->wera[time->num_era++] = now->val.str.startwc;
830 }
831 now = lr_token (ldfile, charmap, result, repertoire, verbose);
832 if (now->tok != tok_eol && now->tok != tok_semicolon)
833 goto err_label;
834 }
835 while (now->tok == tok_semicolon);
836 break;
837
838 #define STR_ELEM(cat) \
839 case tok_##cat: \
840 /* Ignore the rest of the line if we don't need the input of \
841 this line. */ \
842 if (ignore_content) \
843 { \
844 lr_ignore_rest (ldfile, 0); \
845 break; \
846 } \
847 \
848 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
849 if (now->tok != tok_string) \
850 goto err_label; \
851 else if (time->cat != NULL) \
852 lr_error (ldfile, _("\
853 %s: field `%s' declared more than once"), "LC_TIME", #cat); \
854 else if (!ignore_content && (now->val.str.startmb == NULL \
855 || now->val.str.startwc == NULL)) \
856 { \
857 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
858 "LC_TIME", #cat); \
859 time->cat = ""; \
860 time->w##cat = empty_wstr; \
861 } \
862 else if (!ignore_content) \
863 { \
864 time->cat = now->val.str.startmb; \
865 time->w##cat = now->val.str.startwc; \
866 } \
867 break
868
869 STR_ELEM (d_t_fmt);
870 STR_ELEM (d_fmt);
871 STR_ELEM (t_fmt);
872 STR_ELEM (t_fmt_ampm);
873 STR_ELEM (era_year);
874 STR_ELEM (era_d_t_fmt);
875 STR_ELEM (era_d_fmt);
876 STR_ELEM (era_t_fmt);
877 STR_ELEM (timezone);
878 STR_ELEM (date_fmt);
879
880 #define INT_ELEM(cat) \
881 case tok_##cat: \
882 /* Ignore the rest of the line if we don't need the input of \
883 this line. */ \
884 if (ignore_content) \
885 { \
886 lr_ignore_rest (ldfile, 0); \
887 break; \
888 } \
889 \
890 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
891 if (now->tok != tok_number) \
892 goto err_label; \
893 else if (time->cat != 0) \
894 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
895 "LC_TIME", #cat); \
896 else if (!ignore_content) \
897 time->cat = now->val.num; \
898 break
899
900 INT_ELEM (first_weekday);
901 INT_ELEM (first_workday);
902 INT_ELEM (cal_direction);
903
904 case tok_week:
905 /* Ignore the rest of the line if we don't need the input of
906 this line. */
907 if (ignore_content)
908 {
909 lr_ignore_rest (ldfile, 0);
910 break;
911 }
912
913 now = lr_token (ldfile, charmap, result, repertoire, verbose);
914 if (now->tok != tok_number)
915 goto err_label;
916 time->week_ndays = now->val.num;
917
918 now = lr_token (ldfile, charmap, result, repertoire, verbose);
919 if (now->tok != tok_semicolon)
920 goto err_label;
921
922 now = lr_token (ldfile, charmap, result, repertoire, verbose);
923 if (now->tok != tok_number)
924 goto err_label;
925 time->week_1stday = now->val.num;
926
927 now = lr_token (ldfile, charmap, result, repertoire, verbose);
928 if (now->tok != tok_semicolon)
929 goto err_label;
930
931 now = lr_token (ldfile, charmap, result, repertoire, verbose);
932 if (now->tok != tok_number)
933 goto err_label;
934 time->week_1stweek = now->val.num;
935
936 lr_ignore_rest (ldfile, 1);
937 break;
938
939 case tok_end:
940 /* Next we assume `LC_TIME'. */
941 now = lr_token (ldfile, charmap, result, repertoire, verbose);
942 if (now->tok == tok_eof)
943 break;
944 if (now->tok == tok_eol)
945 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TIME");
946 else if (now->tok != tok_lc_time)
947 lr_error (ldfile, _("\
948 %1$s: definition does not end with `END %1$s'"), "LC_TIME");
949 lr_ignore_rest (ldfile, now->tok == tok_lc_time);
950
951 /* If alt_mon was not specified, make it a copy of mon. */
952 if (!ignore_content && !time->alt_mon_defined)
953 {
954 memcpy (time->alt_mon, time->mon, sizeof (time->mon));
955 memcpy (time->walt_mon, time->wmon, sizeof (time->wmon));
956 time->alt_mon_defined = 1;
957 }
958 return;
959
960 default:
961 err_label:
962 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
963 }
964
965 /* Prepare for the next round. */
966 now = lr_token (ldfile, charmap, result, repertoire, verbose);
967 nowtok = now->tok;
968 }
969
970 /* When we come here we reached the end of the file. */
971 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");
972 }