Further harden glibc malloc metadata against 1-byte overflows.
[glibc.git] / locale / programs / ld-time.c
blob32e9c41e3504c4f7fd704dcd0d4228f42fea9c19
1 /* Copyright (C) 1995-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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, see <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
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>
30 #include <assert.h>
32 #include "localedef.h"
33 #include "linereader.h"
34 #include "localeinfo.h"
35 #include "locfile.h"
38 /* Entry describing an entry of the era specification. */
39 struct era_data
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;
52 /* The real definition of the struct for the LC_TIME locale. */
53 struct locale_time_t
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 unsigned char week_ndays;
95 uint32_t week_1stday;
96 unsigned char week_1stweek;
97 unsigned char first_weekday;
98 unsigned char first_workday;
99 unsigned char cal_direction;
100 const char *timezone;
101 const uint32_t *wtimezone;
103 struct era_data *era_entries;
107 /* This constant is used to represent an empty wide character string. */
108 static const uint32_t empty_wstr[1] = { 0 };
111 static void
112 time_startup (struct linereader *lr, struct localedef_t *locale,
113 int ignore_content)
115 if (!ignore_content)
116 locale->categories[LC_TIME].time =
117 (struct locale_time_t *) xcalloc (1, sizeof (struct locale_time_t));
119 if (lr != NULL)
121 lr->translate_strings = 1;
122 lr->return_widestr = 1;
127 void
128 time_finish (struct localedef_t *locale, const struct charmap_t *charmap)
130 struct locale_time_t *time = locale->categories[LC_TIME].time;
131 int nothing = 0;
133 /* Now resolve copying and also handle completely missing definitions. */
134 if (time == NULL)
136 /* First see whether we were supposed to copy. If yes, find the
137 actual definition. */
138 if (locale->copy_name[LC_TIME] != NULL)
140 /* Find the copying locale. This has to happen transitively since
141 the locale we are copying from might also copying another one. */
142 struct localedef_t *from = locale;
145 from = find_locale (LC_TIME, from->copy_name[LC_TIME],
146 from->repertoire_name, charmap);
147 while (from->categories[LC_TIME].time == NULL
148 && from->copy_name[LC_TIME] != NULL);
150 time = locale->categories[LC_TIME].time
151 = from->categories[LC_TIME].time;
154 /* If there is still no definition issue an warning and create an
155 empty one. */
156 if (time == NULL)
158 if (! be_quiet)
159 WITH_CUR_LOCALE (error (0, 0, _("\
160 No definition for %s category found"), "LC_TIME"));
161 time_startup (NULL, locale, 0);
162 time = locale->categories[LC_TIME].time;
163 nothing = 1;
167 #define noparen(arg1, argn...) arg1, ##argn
168 #define TESTARR_ELEM(cat, val) \
169 if (!time->cat##_defined) \
171 const char *initval[] = { noparen val }; \
172 unsigned int i; \
174 if (! be_quiet && ! nothing) \
175 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
176 "LC_TIME", #cat)); \
178 for (i = 0; i < sizeof (initval) / sizeof (initval[0]); ++i) \
179 time->cat[i] = initval[i]; \
182 TESTARR_ELEM (abday, ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ));
183 TESTARR_ELEM (day, ( "Sunday", "Monday", "Tuesday", "Wednesday",
184 "Thursday", "Friday", "Saturday" ));
185 TESTARR_ELEM (abmon, ( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
186 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ));
187 TESTARR_ELEM (mon, ( "January", "February", "March", "April",
188 "May", "June", "July", "August",
189 "September", "October", "November", "December" ));
190 TESTARR_ELEM (am_pm, ( "AM", "PM" ));
192 #define TEST_ELEM(cat, initval) \
193 if (time->cat == NULL) \
195 if (! be_quiet && ! nothing) \
196 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
197 "LC_TIME", #cat)); \
199 time->cat = initval; \
202 TEST_ELEM (d_t_fmt, "%a %b %e %H:%M:%S %Y");
203 TEST_ELEM (d_fmt, "%m/%d/%y");
204 TEST_ELEM (t_fmt, "%H:%M:%S");
206 /* According to C.Y.Alexis Cheng <alexis@vnet.ibm.com> the T_FMT_AMPM
207 field is optional. */
208 if (time->t_fmt_ampm == NULL)
210 if (time->am_pm[0][0] == '\0' && time->am_pm[1][0] == '\0')
212 /* No AM/PM strings defined, use the 24h format as default. */
213 time->t_fmt_ampm = time->t_fmt;
214 time->wt_fmt_ampm = time->wt_fmt;
216 else
218 time->t_fmt_ampm = "%I:%M:%S %p";
219 time->wt_fmt_ampm = (const uint32_t *) L"%I:%M:%S %p";
223 /* Now process the era entries. */
224 if (time->num_era != 0)
226 const int days_per_month[12] = { 31, 29, 31, 30, 31, 30,
227 31, 31, 30, 31 ,30, 31 };
228 size_t idx;
229 wchar_t *wstr;
231 time->era_entries =
232 (struct era_data *) xmalloc (time->num_era
233 * sizeof (struct era_data));
235 for (idx = 0; idx < time->num_era; ++idx)
237 size_t era_len = strlen (time->era[idx]);
238 char *str = xmalloc ((era_len + 1 + 3) & ~3);
239 char *endp;
241 memcpy (str, time->era[idx], era_len + 1);
243 /* First character must be + or - for the direction. */
244 if (*str != '+' && *str != '-')
246 if (!be_quiet)
247 WITH_CUR_LOCALE (error (0, 0, _("\
248 %s: direction flag in string %Zd in `era' field is not '+' nor '-'"),
249 "LC_TIME", idx + 1));
250 /* Default arbitrarily to '+'. */
251 time->era_entries[idx].direction = '+';
253 else
254 time->era_entries[idx].direction = *str;
255 if (*++str != ':')
257 if (!be_quiet)
258 WITH_CUR_LOCALE (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, ":");
263 else
264 ++str;
266 /* Now the offset year. */
267 time->era_entries[idx].offset = strtol (str, &endp, 10);
268 if (endp == str)
270 if (!be_quiet)
271 WITH_CUR_LOCALE (error (0, 0, _("\
272 %s: invalid number for offset in string %Zd in `era' field"),
273 "LC_TIME", idx + 1));
274 (void) strsep (&str, ":");
276 else if (*endp != ':')
278 if (!be_quiet)
279 WITH_CUR_LOCALE (error (0, 0, _("\
280 %s: garbage at end of offset value in string %Zd in `era' field"),
281 "LC_TIME", idx + 1));
282 (void) strsep (&str, ":");
284 else
285 str = endp + 1;
287 /* Next is the starting date in ISO format. */
288 if (strncmp (str, "-*", 2) == 0)
290 time->era_entries[idx].start_date[0] =
291 time->era_entries[idx].start_date[1] =
292 time->era_entries[idx].start_date[2] = 0x80000000;
293 if (str[2] != ':')
294 goto garbage_start_date;
295 str += 3;
297 else if (strncmp (str, "+*", 2) == 0)
299 time->era_entries[idx].start_date[0] =
300 time->era_entries[idx].start_date[1] =
301 time->era_entries[idx].start_date[2] = 0x7fffffff;
302 if (str[2] != ':')
303 goto garbage_start_date;
304 str += 3;
306 else
308 time->era_entries[idx].start_date[0] = strtol (str, &endp, 10);
309 if (endp == str || *endp != '/')
310 goto invalid_start_date;
311 else
312 str = endp + 1;
313 time->era_entries[idx].start_date[0] -= 1900;
314 /* year -1 represent 1 B.C. (not -1 A.D.) */
315 if (time->era_entries[idx].start_date[0] < -1900)
316 ++time->era_entries[idx].start_date[0];
318 time->era_entries[idx].start_date[1] = strtol (str, &endp, 10);
319 if (endp == str || *endp != '/')
320 goto invalid_start_date;
321 else
322 str = endp + 1;
323 time->era_entries[idx].start_date[1] -= 1;
325 time->era_entries[idx].start_date[2] = strtol (str, &endp, 10);
326 if (endp == str)
328 invalid_start_date:
329 if (!be_quiet)
330 WITH_CUR_LOCALE (error (0, 0, _("\
331 %s: invalid starting date in string %Zd in `era' field"),
332 "LC_TIME", idx + 1));
333 (void) strsep (&str, ":");
335 else if (*endp != ':')
337 garbage_start_date:
338 if (!be_quiet)
339 WITH_CUR_LOCALE (error (0, 0, _("\
340 %s: garbage at end of starting date in string %Zd in `era' field "),
341 "LC_TIME", idx + 1));
342 (void) strsep (&str, ":");
344 else
346 str = endp + 1;
348 /* Check for valid value. */
349 if ((time->era_entries[idx].start_date[1] < 0
350 || time->era_entries[idx].start_date[1] >= 12
351 || time->era_entries[idx].start_date[2] < 0
352 || (time->era_entries[idx].start_date[2]
353 > days_per_month[time->era_entries[idx].start_date[1]])
354 || (time->era_entries[idx].start_date[1] == 2
355 && time->era_entries[idx].start_date[2] == 29
356 && !__isleap (time->era_entries[idx].start_date[0])))
357 && !be_quiet)
358 WITH_CUR_LOCALE (error (0, 0, _("\
359 %s: starting date is invalid in string %Zd in `era' field"),
360 "LC_TIME", idx + 1));
364 /* Next is the stopping date in ISO format. */
365 if (strncmp (str, "-*", 2) == 0)
367 time->era_entries[idx].stop_date[0] =
368 time->era_entries[idx].stop_date[1] =
369 time->era_entries[idx].stop_date[2] = 0x80000000;
370 if (str[2] != ':')
371 goto garbage_stop_date;
372 str += 3;
374 else if (strncmp (str, "+*", 2) == 0)
376 time->era_entries[idx].stop_date[0] =
377 time->era_entries[idx].stop_date[1] =
378 time->era_entries[idx].stop_date[2] = 0x7fffffff;
379 if (str[2] != ':')
380 goto garbage_stop_date;
381 str += 3;
383 else
385 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
386 if (endp == str || *endp != '/')
387 goto invalid_stop_date;
388 else
389 str = endp + 1;
390 time->era_entries[idx].stop_date[0] -= 1900;
391 /* year -1 represent 1 B.C. (not -1 A.D.) */
392 if (time->era_entries[idx].stop_date[0] < -1900)
393 ++time->era_entries[idx].stop_date[0];
395 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
396 if (endp == str || *endp != '/')
397 goto invalid_stop_date;
398 else
399 str = endp + 1;
400 time->era_entries[idx].stop_date[1] -= 1;
402 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
403 if (endp == str)
405 invalid_stop_date:
406 if (!be_quiet)
407 WITH_CUR_LOCALE (error (0, 0, _("\
408 %s: invalid stopping date in string %Zd in `era' field"),
409 "LC_TIME", idx + 1));
410 (void) strsep (&str, ":");
412 else if (*endp != ':')
414 garbage_stop_date:
415 if (!be_quiet)
416 WITH_CUR_LOCALE (error (0, 0, _("\
417 %s: garbage at end of stopping date in string %Zd in `era' field"),
418 "LC_TIME", idx + 1));
419 (void) strsep (&str, ":");
421 else
423 str = endp + 1;
425 /* Check for valid value. */
426 if ((time->era_entries[idx].stop_date[1] < 0
427 || time->era_entries[idx].stop_date[1] >= 12
428 || time->era_entries[idx].stop_date[2] < 0
429 || (time->era_entries[idx].stop_date[2]
430 > days_per_month[time->era_entries[idx].stop_date[1]])
431 || (time->era_entries[idx].stop_date[1] == 2
432 && time->era_entries[idx].stop_date[2] == 29
433 && !__isleap (time->era_entries[idx].stop_date[0])))
434 && !be_quiet)
435 WITH_CUR_LOCALE (error (0, 0, _("\
436 %s: invalid stopping date in string %Zd in `era' field"),
437 "LC_TIME", idx + 1));
441 if (str == NULL || *str == '\0')
443 if (!be_quiet)
444 WITH_CUR_LOCALE (error (0, 0, _("\
445 %s: missing era name in string %Zd in `era' field"), "LC_TIME", idx + 1));
446 time->era_entries[idx].name =
447 time->era_entries[idx].format = "";
449 else
451 time->era_entries[idx].name = strsep (&str, ":");
453 if (str == NULL || *str == '\0')
455 if (!be_quiet)
456 WITH_CUR_LOCALE (error (0, 0, _("\
457 %s: missing era format in string %Zd in `era' field"),
458 "LC_TIME", idx + 1));
459 time->era_entries[idx].name =
460 time->era_entries[idx].format = "";
462 else
463 time->era_entries[idx].format = str;
466 /* Now generate the wide character name and format. */
467 wstr = wcschr ((wchar_t *) time->wera[idx], L':');/* end direction */
468 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end offset */
469 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end start */
470 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end end */
471 if (wstr != NULL)
473 time->era_entries[idx].wname = (uint32_t *) wstr + 1;
474 wstr = wcschr (wstr + 1, L':'); /* end name */
475 if (wstr != NULL)
477 *wstr = L'\0';
478 time->era_entries[idx].wformat = (uint32_t *) wstr + 1;
480 else
481 time->era_entries[idx].wname =
482 time->era_entries[idx].wformat = (uint32_t *) L"";
484 else
485 time->era_entries[idx].wname =
486 time->era_entries[idx].wformat = (uint32_t *) L"";
490 /* Set up defaults based on ISO 30112 WD10 [2014]. */
491 if (time->week_ndays == 0)
492 time->week_ndays = 7;
494 if (time->week_1stday == 0)
495 time->week_1stday = 19971130;
497 if (time->week_1stweek == 0)
498 time->week_1stweek = 7;
500 if (time->week_1stweek > time->week_ndays)
501 WITH_CUR_LOCALE (error (0, 0, _("\
502 %s: third operand for value of field `%s' must not be larger than %d"),
503 "LC_TIME", "week", 7));
505 if (time->first_weekday == '\0')
506 /* The definition does not specify this so the default is used. */
507 time->first_weekday = 1;
508 else if (time->first_weekday > time->week_ndays)
509 WITH_CUR_LOCALE (error (0, 0, _("\
510 %s: values for field `%s' must not be larger than %d"),
511 "LC_TIME", "first_weekday", 7));
513 if (time->first_workday == '\0')
514 /* The definition does not specify this so the default is used. */
515 time->first_workday = 2;
516 else if (time->first_workday > time->week_ndays)
517 WITH_CUR_LOCALE (error (0, 0, _("\
518 %s: values for field `%s' must not be larger than %d"),
519 "LC_TIME", "first_workday", 7));
521 if (time->cal_direction == '\0')
522 /* The definition does not specify this so the default is used. */
523 time->cal_direction = 1;
524 else if (time->cal_direction > 3)
525 WITH_CUR_LOCALE (error (0, 0, _("\
526 %s: values for field `%s' must not be larger than %d"),
527 "LC_TIME", "cal_direction", 3));
529 /* XXX We don't perform any tests on the timezone value since this is
530 simply useless, stupid $&$!@... */
531 if (time->timezone == NULL)
532 time->timezone = "";
534 if (time->date_fmt == NULL)
535 time->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
536 if (time->wdate_fmt == NULL)
537 time->wdate_fmt = (const uint32_t *) L"%a %b %e %H:%M:%S %Z %Y";
541 void
542 time_output (struct localedef_t *locale, const struct charmap_t *charmap,
543 const char *output_path)
545 struct locale_time_t *time = locale->categories[LC_TIME].time;
546 struct locale_file file;
547 size_t num, n;
549 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
551 /* The ab'days. */
552 for (n = 0; n < 7; ++n)
553 add_locale_string (&file, time->abday[n] ?: "");
555 /* The days. */
556 for (n = 0; n < 7; ++n)
557 add_locale_string (&file, time->day[n] ?: "");
559 /* The ab'mons. */
560 for (n = 0; n < 12; ++n)
561 add_locale_string (&file, time->abmon[n] ?: "");
563 /* The mons. */
564 for (n = 0; n < 12; ++n)
565 add_locale_string (&file, time->mon[n] ?: "");
567 /* AM/PM. */
568 for (n = 0; n < 2; ++n)
569 add_locale_string (&file, time->am_pm[n]);
571 add_locale_string (&file, time->d_t_fmt ?: "");
572 add_locale_string (&file, time->d_fmt ?: "");
573 add_locale_string (&file, time->t_fmt ?: "");
574 add_locale_string (&file, time->t_fmt_ampm ?: "");
576 start_locale_structure (&file);
577 for (num = 0; num < time->num_era; ++num)
578 add_locale_string (&file, time->era[num]);
579 end_locale_structure (&file);
581 add_locale_string (&file, time->era_year ?: "");
582 add_locale_string (&file, time->era_d_fmt ?: "");
584 start_locale_structure (&file);
585 for (num = 0; num < 100; ++num)
586 add_locale_string (&file, time->alt_digits[num] ?: "");
587 end_locale_structure (&file);
589 add_locale_string (&file, time->era_d_t_fmt ?: "");
590 add_locale_string (&file, time->era_t_fmt ?: "");
591 add_locale_uint32 (&file, time->num_era);
593 start_locale_structure (&file);
594 for (num = 0; num < time->num_era; ++num)
596 add_locale_uint32 (&file, time->era_entries[num].direction);
597 add_locale_uint32 (&file, time->era_entries[num].offset);
598 add_locale_uint32 (&file, time->era_entries[num].start_date[0]);
599 add_locale_uint32 (&file, time->era_entries[num].start_date[1]);
600 add_locale_uint32 (&file, time->era_entries[num].start_date[2]);
601 add_locale_uint32 (&file, time->era_entries[num].stop_date[0]);
602 add_locale_uint32 (&file, time->era_entries[num].stop_date[1]);
603 add_locale_uint32 (&file, time->era_entries[num].stop_date[2]);
604 add_locale_string (&file, time->era_entries[num].name);
605 add_locale_string (&file, time->era_entries[num].format);
606 add_locale_wstring (&file, time->era_entries[num].wname);
607 add_locale_wstring (&file, time->era_entries[num].wformat);
609 end_locale_structure (&file);
611 /* The wide character ab'days. */
612 for (n = 0; n < 7; ++n)
613 add_locale_wstring (&file, time->wabday[n] ?: empty_wstr);
615 /* The wide character days. */
616 for (n = 0; n < 7; ++n)
617 add_locale_wstring (&file, time->wday[n] ?: empty_wstr);
619 /* The wide character ab'mons. */
620 for (n = 0; n < 12; ++n)
621 add_locale_wstring (&file, time->wabmon[n] ?: empty_wstr);
623 /* The wide character mons. */
624 for (n = 0; n < 12; ++n)
625 add_locale_wstring (&file, time->wmon[n] ?: empty_wstr);
627 /* Wide character AM/PM. */
628 for (n = 0; n < 2; ++n)
629 add_locale_wstring (&file, time->wam_pm[n] ?: empty_wstr);
631 add_locale_wstring (&file, time->wd_t_fmt ?: empty_wstr);
632 add_locale_wstring (&file, time->wd_fmt ?: empty_wstr);
633 add_locale_wstring (&file, time->wt_fmt ?: empty_wstr);
634 add_locale_wstring (&file, time->wt_fmt_ampm ?: empty_wstr);
635 add_locale_wstring (&file, time->wera_year ?: empty_wstr);
636 add_locale_wstring (&file, time->wera_d_fmt ?: empty_wstr);
638 start_locale_structure (&file);
639 for (num = 0; num < 100; ++num)
640 add_locale_wstring (&file, time->walt_digits[num] ?: empty_wstr);
641 end_locale_structure (&file);
643 add_locale_wstring (&file, time->wera_d_t_fmt ?: empty_wstr);
644 add_locale_wstring (&file, time->wera_t_fmt ?: empty_wstr);
645 add_locale_char (&file, time->week_ndays);
646 add_locale_uint32 (&file, time->week_1stday);
647 add_locale_char (&file, time->week_1stweek);
648 add_locale_char (&file, time->first_weekday);
649 add_locale_char (&file, time->first_workday);
650 add_locale_char (&file, time->cal_direction);
651 add_locale_string (&file, time->timezone);
652 add_locale_string (&file, time->date_fmt);
653 add_locale_wstring (&file, time->wdate_fmt);
654 add_locale_string (&file, charmap->code_set_name);
655 write_locale_data (output_path, LC_TIME, "LC_TIME", &file);
659 /* The parser for the LC_TIME section of the locale definition. */
660 void
661 time_read (struct linereader *ldfile, struct localedef_t *result,
662 const struct charmap_t *charmap, const char *repertoire_name,
663 int ignore_content)
665 struct repertoire_t *repertoire = NULL;
666 struct locale_time_t *time;
667 struct token *now;
668 enum token_t nowtok;
669 size_t cnt;
671 /* Get the repertoire we have to use. */
672 if (repertoire_name != NULL)
673 repertoire = repertoire_read (repertoire_name);
675 /* The rest of the line containing `LC_TIME' must be free. */
676 lr_ignore_rest (ldfile, 1);
681 now = lr_token (ldfile, charmap, result, repertoire, verbose);
682 nowtok = now->tok;
684 while (nowtok == tok_eol);
686 /* If we see `copy' now we are almost done. */
687 if (nowtok == tok_copy)
689 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
690 LC_TIME, "LC_TIME", ignore_content);
691 return;
694 /* Prepare the data structures. */
695 time_startup (ldfile, result, ignore_content);
696 time = result->categories[LC_TIME].time;
698 while (1)
700 /* Of course we don't proceed beyond the end of file. */
701 if (nowtok == tok_eof)
702 break;
704 /* Ingore empty lines. */
705 if (nowtok == tok_eol)
707 now = lr_token (ldfile, charmap, result, repertoire, verbose);
708 nowtok = now->tok;
709 continue;
712 switch (nowtok)
714 #define STRARR_ELEM(cat, min, max) \
715 case tok_##cat: \
716 /* Ignore the rest of the line if we don't need the input of \
717 this line. */ \
718 if (ignore_content) \
720 lr_ignore_rest (ldfile, 0); \
721 break; \
724 for (cnt = 0; cnt < max; ++cnt) \
726 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
727 if (now->tok == tok_eol) \
729 if (cnt < min) \
730 lr_error (ldfile, _("%s: too few values for field `%s'"), \
731 "LC_TIME", #cat); \
732 if (!ignore_content) \
733 do \
735 time->cat[cnt] = ""; \
736 time->w##cat[cnt] = empty_wstr; \
738 while (++cnt < max); \
739 break; \
741 else if (now->tok != tok_string) \
742 goto err_label; \
743 else if (!ignore_content && (now->val.str.startmb == NULL \
744 || now->val.str.startwc == NULL)) \
746 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
747 "LC_TIME", #cat); \
748 time->cat[cnt] = ""; \
749 time->w##cat[cnt] = empty_wstr; \
751 else if (!ignore_content) \
753 time->cat[cnt] = now->val.str.startmb; \
754 time->w##cat[cnt] = now->val.str.startwc; \
757 /* Match the semicolon. */ \
758 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
759 if (now->tok != tok_semicolon && now->tok != tok_eol) \
760 break; \
762 if (now->tok != tok_eol) \
764 while (!ignore_content && cnt < min) \
766 time->cat[cnt] = ""; \
767 time->w##cat[cnt++] = empty_wstr; \
770 if (now->tok == tok_semicolon) \
772 now = lr_token (ldfile, charmap, result, repertoire, \
773 verbose); \
774 if (now->tok == tok_eol) \
775 lr_error (ldfile, _("extra trailing semicolon")); \
776 else if (now->tok == tok_string) \
778 lr_error (ldfile, _("\
779 %s: too many values for field `%s'"), \
780 "LC_TIME", #cat); \
781 lr_ignore_rest (ldfile, 0); \
783 else \
784 goto err_label; \
786 else \
787 goto err_label; \
789 time->cat##_defined = 1; \
790 break
792 STRARR_ELEM (abday, 7, 7);
793 STRARR_ELEM (day, 7, 7);
794 STRARR_ELEM (abmon, 12, 12);
795 STRARR_ELEM (mon, 12, 12);
796 STRARR_ELEM (am_pm, 2, 2);
797 STRARR_ELEM (alt_digits, 0, 100);
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)
804 lr_ignore_rest (ldfile, 0);
805 break;
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))
815 lr_error (ldfile, _("%s: unknown character in field `%s'"),
816 "LC_TIME", "era");
817 lr_ignore_rest (ldfile, 0);
818 break;
820 if (!ignore_content)
822 time->era = xrealloc (time->era,
823 (time->num_era + 1) * sizeof (char *));
824 time->era[time->num_era] = now->val.str.startmb;
826 time->wera = xrealloc (time->wera,
827 (time->num_era + 1)
828 * sizeof (char *));
829 time->wera[time->num_era++] = now->val.str.startwc;
831 now = lr_token (ldfile, charmap, result, repertoire, verbose);
832 if (now->tok != tok_eol && now->tok != tok_semicolon)
833 goto err_label;
835 while (now->tok == tok_semicolon);
836 break;
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) \
844 lr_ignore_rest (ldfile, 0); \
845 break; \
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)) \
857 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
858 "LC_TIME", #cat); \
859 time->cat = ""; \
860 time->w##cat = empty_wstr; \
862 else if (!ignore_content) \
864 time->cat = now->val.str.startmb; \
865 time->w##cat = now->val.str.startwc; \
867 break
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);
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) \
886 lr_ignore_rest (ldfile, 0); \
887 break; \
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
900 INT_ELEM (first_weekday);
901 INT_ELEM (first_workday);
902 INT_ELEM (cal_direction);
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)
909 lr_ignore_rest (ldfile, 0);
910 break;
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;
918 now = lr_token (ldfile, charmap, result, repertoire, verbose);
919 if (now->tok != tok_semicolon)
920 goto err_label;
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;
927 now = lr_token (ldfile, charmap, result, repertoire, verbose);
928 if (now->tok != tok_semicolon)
929 goto err_label;
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;
936 lr_ignore_rest (ldfile, 1);
937 break;
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 return;
952 default:
953 err_label:
954 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
957 /* Prepare for the next round. */
958 now = lr_token (ldfile, charmap, result, repertoire, verbose);
959 nowtok = now->tok;
962 /* When we come here we reached the end of the file. */
963 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");