sparc: Fix la_symbind for bind-now (BZ 23734)
[glibc.git] / locale / programs / ld-time.c
bloba590b013b3aaae68ad53e67efe034ee80a4c99a7
1 /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <byteswap.h>
22 #include <langinfo.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
26 #include <stdint.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localedef.h"
32 #include "linereader.h"
33 #include "localeinfo.h"
34 #include "locfile.h"
37 /* Entry describing an entry of the era specification. */
38 struct era_data
40 int32_t direction;
41 int32_t offset;
42 int32_t start_date[3];
43 int32_t stop_date[3];
44 const char *name;
45 const char *format;
46 uint32_t *wname;
47 uint32_t *wformat;
51 /* The real definition of the struct for the LC_TIME locale. */
52 struct locale_time_t
54 const char *abday[7];
55 const uint32_t *wabday[7];
56 int abday_defined;
57 const char *day[7];
58 const uint32_t *wday[7];
59 int day_defined;
60 const char *abmon[12];
61 const uint32_t *wabmon[12];
62 int abmon_defined;
63 const char *mon[12];
64 const uint32_t *wmon[12];
65 int mon_defined;
66 const char *am_pm[2];
67 const uint32_t *wam_pm[2];
68 int am_pm_defined;
69 const char *d_t_fmt;
70 const uint32_t *wd_t_fmt;
71 const char *d_fmt;
72 const uint32_t *wd_fmt;
73 const char *t_fmt;
74 const uint32_t *wt_fmt;
75 const char *t_fmt_ampm;
76 const uint32_t *wt_fmt_ampm;
77 const char **era;
78 const uint32_t **wera;
79 uint32_t num_era;
80 const char *era_year;
81 const uint32_t *wera_year;
82 const char *era_d_t_fmt;
83 const uint32_t *wera_d_t_fmt;
84 const char *era_t_fmt;
85 const uint32_t *wera_t_fmt;
86 const char *era_d_fmt;
87 const uint32_t *wera_d_fmt;
88 const char *alt_digits[100];
89 const uint32_t *walt_digits[100];
90 const char *date_fmt;
91 const uint32_t *wdate_fmt;
92 int alt_digits_defined;
93 const char *alt_mon[12];
94 const uint32_t *walt_mon[12];
95 int alt_mon_defined;
96 const char *ab_alt_mon[12];
97 const uint32_t *wab_alt_mon[12];
98 int ab_alt_mon_defined;
99 unsigned char week_ndays;
100 uint32_t week_1stday;
101 unsigned char week_1stweek;
102 unsigned char first_weekday;
103 unsigned char first_workday;
104 unsigned char cal_direction;
105 const char *timezone;
106 const uint32_t *wtimezone;
108 struct era_data *era_entries;
112 /* This constant is used to represent an empty wide character string. */
113 static const uint32_t empty_wstr[1] = { 0 };
116 static void
117 time_startup (struct linereader *lr, struct localedef_t *locale,
118 int ignore_content)
120 if (!ignore_content)
121 locale->categories[LC_TIME].time =
122 (struct locale_time_t *) xcalloc (1, sizeof (struct locale_time_t));
124 if (lr != NULL)
126 lr->translate_strings = 1;
127 lr->return_widestr = 1;
132 void
133 time_finish (struct localedef_t *locale, const struct charmap_t *charmap)
135 struct locale_time_t *time = locale->categories[LC_TIME].time;
136 int nothing = 0;
138 /* Now resolve copying and also handle completely missing definitions. */
139 if (time == NULL)
141 /* First see whether we were supposed to copy. If yes, find the
142 actual definition. */
143 if (locale->copy_name[LC_TIME] != NULL)
145 /* Find the copying locale. This has to happen transitively since
146 the locale we are copying from might also copying another one. */
147 struct localedef_t *from = locale;
150 from = find_locale (LC_TIME, from->copy_name[LC_TIME],
151 from->repertoire_name, charmap);
152 while (from->categories[LC_TIME].time == NULL
153 && from->copy_name[LC_TIME] != NULL);
155 time = locale->categories[LC_TIME].time
156 = from->categories[LC_TIME].time;
159 /* If there is still no definition issue an warning and create an
160 empty one. */
161 if (time == NULL)
163 record_warning (_("\
164 No definition for %s category found"), "LC_TIME");
165 time_startup (NULL, locale, 0);
166 time = locale->categories[LC_TIME].time;
167 nothing = 1;
171 #define noparen(arg1, argn...) arg1, ##argn
172 #define TESTARR_ELEM(cat, val) \
173 if (!time->cat##_defined) \
175 const char *initval[] = { noparen val }; \
176 unsigned int i; \
178 if (! nothing) \
179 record_error (0, 0, _("%s: field `%s' not defined"), \
180 "LC_TIME", #cat); \
182 for (i = 0; i < sizeof (initval) / sizeof (initval[0]); ++i) \
183 time->cat[i] = initval[i]; \
186 TESTARR_ELEM (abday, ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ));
187 TESTARR_ELEM (day, ( "Sunday", "Monday", "Tuesday", "Wednesday",
188 "Thursday", "Friday", "Saturday" ));
189 TESTARR_ELEM (abmon, ( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
190 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ));
191 TESTARR_ELEM (mon, ( "January", "February", "March", "April",
192 "May", "June", "July", "August",
193 "September", "October", "November", "December" ));
194 TESTARR_ELEM (am_pm, ( "AM", "PM" ));
196 #define TEST_ELEM(cat, initval) \
197 if (time->cat == NULL) \
199 if (! nothing) \
200 record_error (0, 0, _("%s: field `%s' not defined"), \
201 "LC_TIME", #cat); \
203 time->cat = initval; \
206 TEST_ELEM (d_t_fmt, "%a %b %e %H:%M:%S %Y");
207 TEST_ELEM (d_fmt, "%m/%d/%y");
208 TEST_ELEM (t_fmt, "%H:%M:%S");
210 /* According to C.Y.Alexis Cheng <alexis@vnet.ibm.com> the T_FMT_AMPM
211 field is optional. */
212 if (time->t_fmt_ampm == NULL)
214 if (time->am_pm[0][0] == '\0' && time->am_pm[1][0] == '\0')
216 /* No AM/PM strings defined, use the 24h format as default. */
217 time->t_fmt_ampm = time->t_fmt;
218 time->wt_fmt_ampm = time->wt_fmt;
220 else
222 time->t_fmt_ampm = "%I:%M:%S %p";
223 time->wt_fmt_ampm = (const uint32_t *) L"%I:%M:%S %p";
227 /* Now process the era entries. */
228 if (time->num_era != 0)
230 const int days_per_month[12] = { 31, 29, 31, 30, 31, 30,
231 31, 31, 30, 31 ,30, 31 };
232 size_t idx;
233 wchar_t *wstr;
235 time->era_entries =
236 (struct era_data *) xmalloc (time->num_era
237 * sizeof (struct era_data));
239 for (idx = 0; idx < time->num_era; ++idx)
241 size_t era_len = strlen (time->era[idx]);
242 char *str = xmalloc ((era_len + 1 + 3) & ~3);
243 char *endp;
245 memcpy (str, time->era[idx], era_len + 1);
247 /* First character must be + or - for the direction. */
248 if (*str != '+' && *str != '-')
250 record_error (0, 0, _("\
251 %s: direction flag in string %Zd in `era' field is not '+' nor '-'"),
252 "LC_TIME", idx + 1);
253 /* Default arbitrarily to '+'. */
254 time->era_entries[idx].direction = '+';
256 else
257 time->era_entries[idx].direction = *str;
258 if (*++str != ':')
260 record_error (0, 0, _("\
261 %s: direction flag in string %Zd in `era' field is not a single character"),
262 "LC_TIME", idx + 1);
263 (void) strsep (&str, ":");
265 else
266 ++str;
268 /* Now the offset year. */
269 time->era_entries[idx].offset = strtol (str, &endp, 10);
270 if (endp == str)
272 record_error (0, 0, _("\
273 %s: invalid number for offset in string %Zd in `era' field"),
274 "LC_TIME", idx + 1);
275 (void) strsep (&str, ":");
277 else if (*endp != ':')
279 record_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 record_error (0, 0, _("\
330 %s: invalid starting date in string %Zd in `era' field"),
331 "LC_TIME", idx + 1);
332 (void) strsep (&str, ":");
334 else if (*endp != ':')
336 garbage_start_date:
337 record_error (0, 0, _("\
338 %s: garbage at end of starting date in string %Zd in `era' field "),
339 "LC_TIME", idx + 1);
340 (void) strsep (&str, ":");
342 else
344 str = endp + 1;
346 /* Check for valid value. */
347 if ((time->era_entries[idx].start_date[1] < 0
348 || time->era_entries[idx].start_date[1] >= 12
349 || time->era_entries[idx].start_date[2] < 0
350 || (time->era_entries[idx].start_date[2]
351 > days_per_month[time->era_entries[idx].start_date[1]])
352 || (time->era_entries[idx].start_date[1] == 2
353 && time->era_entries[idx].start_date[2] == 29
354 && !__isleap (time->era_entries[idx].start_date[0]))))
355 record_error (0, 0, _("\
356 %s: starting date is invalid in string %Zd in `era' field"),
357 "LC_TIME", idx + 1);
361 /* Next is the stopping date in ISO format. */
362 if (strncmp (str, "-*", 2) == 0)
364 time->era_entries[idx].stop_date[0] =
365 time->era_entries[idx].stop_date[1] =
366 time->era_entries[idx].stop_date[2] = 0x80000000;
367 if (str[2] != ':')
368 goto garbage_stop_date;
369 str += 3;
371 else if (strncmp (str, "+*", 2) == 0)
373 time->era_entries[idx].stop_date[0] =
374 time->era_entries[idx].stop_date[1] =
375 time->era_entries[idx].stop_date[2] = 0x7fffffff;
376 if (str[2] != ':')
377 goto garbage_stop_date;
378 str += 3;
380 else
382 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
383 if (endp == str || *endp != '/')
384 goto invalid_stop_date;
385 else
386 str = endp + 1;
387 time->era_entries[idx].stop_date[0] -= 1900;
388 /* year -1 represent 1 B.C. (not -1 A.D.) */
389 if (time->era_entries[idx].stop_date[0] < -1900)
390 ++time->era_entries[idx].stop_date[0];
392 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
393 if (endp == str || *endp != '/')
394 goto invalid_stop_date;
395 else
396 str = endp + 1;
397 time->era_entries[idx].stop_date[1] -= 1;
399 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
400 if (endp == str)
402 invalid_stop_date:
403 record_error (0, 0, _("\
404 %s: invalid stopping date in string %Zd in `era' field"),
405 "LC_TIME", idx + 1);
406 (void) strsep (&str, ":");
408 else if (*endp != ':')
410 garbage_stop_date:
411 record_error (0, 0, _("\
412 %s: garbage at end of stopping date in string %Zd in `era' field"),
413 "LC_TIME", idx + 1);
414 (void) strsep (&str, ":");
416 else
418 str = endp + 1;
420 /* Check for valid value. */
421 if ((time->era_entries[idx].stop_date[1] < 0
422 || time->era_entries[idx].stop_date[1] >= 12
423 || time->era_entries[idx].stop_date[2] < 0
424 || (time->era_entries[idx].stop_date[2]
425 > days_per_month[time->era_entries[idx].stop_date[1]])
426 || (time->era_entries[idx].stop_date[1] == 2
427 && time->era_entries[idx].stop_date[2] == 29
428 && !__isleap (time->era_entries[idx].stop_date[0]))))
429 record_error (0, 0, _("\
430 %s: invalid stopping date in string %Zd in `era' field"),
431 "LC_TIME", idx + 1);
435 if (str == NULL || *str == '\0')
437 record_error (0, 0, _("\
438 %s: missing era name in string %Zd in `era' field"), "LC_TIME", idx + 1);
439 time->era_entries[idx].name =
440 time->era_entries[idx].format = "";
442 else
444 time->era_entries[idx].name = strsep (&str, ":");
446 if (str == NULL || *str == '\0')
448 record_error (0, 0, _("\
449 %s: missing era format in string %Zd in `era' field"),
450 "LC_TIME", idx + 1);
451 time->era_entries[idx].name =
452 time->era_entries[idx].format = "";
454 else
455 time->era_entries[idx].format = str;
458 /* Now generate the wide character name and format. */
459 wstr = wcschr ((wchar_t *) time->wera[idx], L':');/* end direction */
460 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end offset */
461 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end start */
462 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end end */
463 if (wstr != NULL)
465 time->era_entries[idx].wname = (uint32_t *) wstr + 1;
466 wstr = wcschr (wstr + 1, L':'); /* end name */
467 if (wstr != NULL)
469 *wstr = L'\0';
470 time->era_entries[idx].wformat = (uint32_t *) wstr + 1;
472 else
473 time->era_entries[idx].wname =
474 time->era_entries[idx].wformat = (uint32_t *) L"";
476 else
477 time->era_entries[idx].wname =
478 time->era_entries[idx].wformat = (uint32_t *) L"";
482 /* Set up defaults based on ISO 30112 WD10 [2014]. */
483 if (time->week_ndays == 0)
484 time->week_ndays = 7;
486 if (time->week_1stday == 0)
487 time->week_1stday = 19971130;
489 if (time->week_1stweek == 0)
490 time->week_1stweek = 7;
492 if (time->week_1stweek > time->week_ndays)
493 record_error (0, 0, _("\
494 %s: third operand for value of field `%s' must not be larger than %d"),
495 "LC_TIME", "week", 7);
497 if (time->first_weekday == '\0')
498 /* The definition does not specify this so the default is used. */
499 time->first_weekday = 1;
500 else if (time->first_weekday > time->week_ndays)
501 record_error (0, 0, _("\
502 %s: values for field `%s' must not be larger than %d"),
503 "LC_TIME", "first_weekday", 7);
505 if (time->first_workday == '\0')
506 /* The definition does not specify this so the default is used. */
507 time->first_workday = 2;
508 else if (time->first_workday > time->week_ndays)
509 record_error (0, 0, _("\
510 %s: values for field `%s' must not be larger than %d"),
511 "LC_TIME", "first_workday", 7);
513 if (time->cal_direction == '\0')
514 /* The definition does not specify this so the default is used. */
515 time->cal_direction = 1;
516 else if (time->cal_direction > 3)
517 record_error (0, 0, _("\
518 %s: values for field `%s' must not be larger than %d"),
519 "LC_TIME", "cal_direction", 3);
521 /* XXX We don't perform any tests on the timezone value since this is
522 simply useless, stupid $&$!@... */
523 if (time->timezone == NULL)
524 time->timezone = "";
526 if (time->date_fmt == NULL)
527 time->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
528 if (time->wdate_fmt == NULL)
529 time->wdate_fmt = (const uint32_t *) L"%a %b %e %H:%M:%S %Z %Y";
533 void
534 time_output (struct localedef_t *locale, const struct charmap_t *charmap,
535 const char *output_path)
537 struct locale_time_t *time = locale->categories[LC_TIME].time;
538 struct locale_file file;
539 size_t num, n;
541 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
543 /* The ab'days. */
544 for (n = 0; n < 7; ++n)
545 add_locale_string (&file, time->abday[n] ?: "");
547 /* The days. */
548 for (n = 0; n < 7; ++n)
549 add_locale_string (&file, time->day[n] ?: "");
551 /* The ab'mons. */
552 for (n = 0; n < 12; ++n)
553 add_locale_string (&file, time->abmon[n] ?: "");
555 /* The mons. */
556 for (n = 0; n < 12; ++n)
557 add_locale_string (&file, time->mon[n] ?: "");
559 /* AM/PM. */
560 for (n = 0; n < 2; ++n)
561 add_locale_string (&file, time->am_pm[n]);
563 add_locale_string (&file, time->d_t_fmt ?: "");
564 add_locale_string (&file, time->d_fmt ?: "");
565 add_locale_string (&file, time->t_fmt ?: "");
566 add_locale_string (&file, time->t_fmt_ampm ?: "");
568 start_locale_structure (&file);
569 for (num = 0; num < time->num_era; ++num)
570 add_locale_string (&file, time->era[num]);
571 end_locale_structure (&file);
573 add_locale_string (&file, time->era_year ?: "");
574 add_locale_string (&file, time->era_d_fmt ?: "");
576 start_locale_structure (&file);
577 for (num = 0; num < 100; ++num)
578 add_locale_string (&file, time->alt_digits[num] ?: "");
579 end_locale_structure (&file);
581 add_locale_string (&file, time->era_d_t_fmt ?: "");
582 add_locale_string (&file, time->era_t_fmt ?: "");
583 add_locale_uint32 (&file, time->num_era);
585 start_locale_structure (&file);
586 for (num = 0; num < time->num_era; ++num)
588 add_locale_uint32 (&file, time->era_entries[num].direction);
589 add_locale_uint32 (&file, time->era_entries[num].offset);
590 add_locale_uint32 (&file, time->era_entries[num].start_date[0]);
591 add_locale_uint32 (&file, time->era_entries[num].start_date[1]);
592 add_locale_uint32 (&file, time->era_entries[num].start_date[2]);
593 add_locale_uint32 (&file, time->era_entries[num].stop_date[0]);
594 add_locale_uint32 (&file, time->era_entries[num].stop_date[1]);
595 add_locale_uint32 (&file, time->era_entries[num].stop_date[2]);
596 add_locale_string (&file, time->era_entries[num].name);
597 add_locale_string (&file, time->era_entries[num].format);
598 add_locale_wstring (&file, time->era_entries[num].wname);
599 add_locale_wstring (&file, time->era_entries[num].wformat);
601 end_locale_structure (&file);
603 /* The wide character ab'days. */
604 for (n = 0; n < 7; ++n)
605 add_locale_wstring (&file, time->wabday[n] ?: empty_wstr);
607 /* The wide character days. */
608 for (n = 0; n < 7; ++n)
609 add_locale_wstring (&file, time->wday[n] ?: empty_wstr);
611 /* The wide character ab'mons. */
612 for (n = 0; n < 12; ++n)
613 add_locale_wstring (&file, time->wabmon[n] ?: empty_wstr);
615 /* The wide character mons. */
616 for (n = 0; n < 12; ++n)
617 add_locale_wstring (&file, time->wmon[n] ?: empty_wstr);
619 /* Wide character AM/PM. */
620 for (n = 0; n < 2; ++n)
621 add_locale_wstring (&file, time->wam_pm[n] ?: empty_wstr);
623 add_locale_wstring (&file, time->wd_t_fmt ?: empty_wstr);
624 add_locale_wstring (&file, time->wd_fmt ?: empty_wstr);
625 add_locale_wstring (&file, time->wt_fmt ?: empty_wstr);
626 add_locale_wstring (&file, time->wt_fmt_ampm ?: empty_wstr);
627 add_locale_wstring (&file, time->wera_year ?: empty_wstr);
628 add_locale_wstring (&file, time->wera_d_fmt ?: empty_wstr);
630 start_locale_structure (&file);
631 for (num = 0; num < 100; ++num)
632 add_locale_wstring (&file, time->walt_digits[num] ?: empty_wstr);
633 end_locale_structure (&file);
635 add_locale_wstring (&file, time->wera_d_t_fmt ?: empty_wstr);
636 add_locale_wstring (&file, time->wera_t_fmt ?: empty_wstr);
637 add_locale_char (&file, time->week_ndays);
638 add_locale_uint32 (&file, time->week_1stday);
639 add_locale_char (&file, time->week_1stweek);
640 add_locale_char (&file, time->first_weekday);
641 add_locale_char (&file, time->first_workday);
642 add_locale_char (&file, time->cal_direction);
643 add_locale_string (&file, time->timezone);
644 add_locale_string (&file, time->date_fmt);
645 add_locale_wstring (&file, time->wdate_fmt);
646 add_locale_string (&file, charmap->code_set_name);
648 /* The alt'mons. */
649 for (n = 0; n < 12; ++n)
650 add_locale_string (&file, time->alt_mon[n] ?: "");
652 /* The wide character alt'mons. */
653 for (n = 0; n < 12; ++n)
654 add_locale_wstring (&file, time->walt_mon[n] ?: empty_wstr);
656 /* The ab'alt'mons. */
657 for (n = 0; n < 12; ++n)
658 add_locale_string (&file, time->ab_alt_mon[n] ?: "");
660 /* The wide character ab'alt'mons. */
661 for (n = 0; n < 12; ++n)
662 add_locale_wstring (&file, time->wab_alt_mon[n] ?: empty_wstr);
664 write_locale_data (output_path, LC_TIME, "LC_TIME", &file);
668 /* The parser for the LC_TIME section of the locale definition. */
669 void
670 time_read (struct linereader *ldfile, struct localedef_t *result,
671 const struct charmap_t *charmap, const char *repertoire_name,
672 int ignore_content)
674 struct repertoire_t *repertoire = NULL;
675 struct locale_time_t *time;
676 struct token *now;
677 enum token_t nowtok;
678 size_t cnt;
680 /* Get the repertoire we have to use. */
681 if (repertoire_name != NULL)
682 repertoire = repertoire_read (repertoire_name);
684 /* The rest of the line containing `LC_TIME' must be free. */
685 lr_ignore_rest (ldfile, 1);
690 now = lr_token (ldfile, charmap, result, repertoire, verbose);
691 nowtok = now->tok;
693 while (nowtok == tok_eol);
695 /* If we see `copy' now we are almost done. */
696 if (nowtok == tok_copy)
698 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
699 LC_TIME, "LC_TIME", ignore_content);
700 return;
703 /* Prepare the data structures. */
704 time_startup (ldfile, result, ignore_content);
705 time = result->categories[LC_TIME].time;
707 while (1)
709 /* Of course we don't proceed beyond the end of file. */
710 if (nowtok == tok_eof)
711 break;
713 /* Ingore empty lines. */
714 if (nowtok == tok_eol)
716 now = lr_token (ldfile, charmap, result, repertoire, verbose);
717 nowtok = now->tok;
718 continue;
721 switch (nowtok)
723 #define STRARR_ELEM(cat, min, max) \
724 case tok_##cat: \
725 /* Ignore the rest of the line if we don't need the input of \
726 this line. */ \
727 if (ignore_content) \
729 lr_ignore_rest (ldfile, 0); \
730 break; \
733 for (cnt = 0; cnt < max; ++cnt) \
735 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
736 if (now->tok == tok_eol) \
738 if (cnt < min) \
739 lr_error (ldfile, _("%s: too few values for field `%s'"), \
740 "LC_TIME", #cat); \
741 if (!ignore_content) \
742 do \
744 time->cat[cnt] = ""; \
745 time->w##cat[cnt] = empty_wstr; \
747 while (++cnt < max); \
748 break; \
750 else if (now->tok != tok_string) \
751 goto err_label; \
752 else if (!ignore_content && (now->val.str.startmb == NULL \
753 || now->val.str.startwc == NULL)) \
755 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
756 "LC_TIME", #cat); \
757 time->cat[cnt] = ""; \
758 time->w##cat[cnt] = empty_wstr; \
760 else if (!ignore_content) \
762 time->cat[cnt] = now->val.str.startmb; \
763 time->w##cat[cnt] = now->val.str.startwc; \
766 /* Match the semicolon. */ \
767 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
768 if (now->tok != tok_semicolon && now->tok != tok_eol) \
769 break; \
771 if (now->tok != tok_eol) \
773 while (!ignore_content && cnt < min) \
775 time->cat[cnt] = ""; \
776 time->w##cat[cnt++] = empty_wstr; \
779 if (now->tok == tok_semicolon) \
781 now = lr_token (ldfile, charmap, result, repertoire, \
782 verbose); \
783 if (now->tok == tok_eol) \
784 lr_error (ldfile, _("extra trailing semicolon")); \
785 else if (now->tok == tok_string) \
787 lr_error (ldfile, _("\
788 %s: too many values for field `%s'"), \
789 "LC_TIME", #cat); \
790 lr_ignore_rest (ldfile, 0); \
792 else \
793 goto err_label; \
795 else \
796 goto err_label; \
798 time->cat##_defined = 1; \
799 break
801 STRARR_ELEM (abday, 7, 7);
802 STRARR_ELEM (day, 7, 7);
803 STRARR_ELEM (abmon, 12, 12);
804 STRARR_ELEM (mon, 12, 12);
805 STRARR_ELEM (am_pm, 2, 2);
806 STRARR_ELEM (alt_digits, 0, 100);
807 STRARR_ELEM (alt_mon, 12, 12);
808 STRARR_ELEM (ab_alt_mon, 12, 12);
810 case tok_era:
811 /* Ignore the rest of the line if we don't need the input of
812 this line. */
813 if (ignore_content)
815 lr_ignore_rest (ldfile, 0);
816 break;
820 now = lr_token (ldfile, charmap, result, repertoire, verbose);
821 if (now->tok != tok_string)
822 goto err_label;
823 if (!ignore_content && (now->val.str.startmb == NULL
824 || now->val.str.startwc == NULL))
826 lr_error (ldfile, _("%s: unknown character in field `%s'"),
827 "LC_TIME", "era");
828 lr_ignore_rest (ldfile, 0);
829 break;
831 if (!ignore_content)
833 time->era = xrealloc (time->era,
834 (time->num_era + 1) * sizeof (char *));
835 time->era[time->num_era] = now->val.str.startmb;
837 time->wera = xrealloc (time->wera,
838 (time->num_era + 1)
839 * sizeof (char *));
840 time->wera[time->num_era++] = now->val.str.startwc;
842 now = lr_token (ldfile, charmap, result, repertoire, verbose);
843 if (now->tok != tok_eol && now->tok != tok_semicolon)
844 goto err_label;
846 while (now->tok == tok_semicolon);
847 break;
849 #define STR_ELEM(cat) \
850 case tok_##cat: \
851 /* Ignore the rest of the line if we don't need the input of \
852 this line. */ \
853 if (ignore_content) \
855 lr_ignore_rest (ldfile, 0); \
856 break; \
859 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
860 if (now->tok != tok_string) \
861 goto err_label; \
862 else if (time->cat != NULL) \
863 lr_error (ldfile, _("\
864 %s: field `%s' declared more than once"), "LC_TIME", #cat); \
865 else if (!ignore_content && (now->val.str.startmb == NULL \
866 || now->val.str.startwc == NULL)) \
868 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
869 "LC_TIME", #cat); \
870 time->cat = ""; \
871 time->w##cat = empty_wstr; \
873 else if (!ignore_content) \
875 time->cat = now->val.str.startmb; \
876 time->w##cat = now->val.str.startwc; \
878 break
880 STR_ELEM (d_t_fmt);
881 STR_ELEM (d_fmt);
882 STR_ELEM (t_fmt);
883 STR_ELEM (t_fmt_ampm);
884 STR_ELEM (era_year);
885 STR_ELEM (era_d_t_fmt);
886 STR_ELEM (era_d_fmt);
887 STR_ELEM (era_t_fmt);
888 STR_ELEM (timezone);
889 STR_ELEM (date_fmt);
891 #define INT_ELEM(cat) \
892 case tok_##cat: \
893 /* Ignore the rest of the line if we don't need the input of \
894 this line. */ \
895 if (ignore_content) \
897 lr_ignore_rest (ldfile, 0); \
898 break; \
901 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
902 if (now->tok != tok_number) \
903 goto err_label; \
904 else if (time->cat != 0) \
905 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
906 "LC_TIME", #cat); \
907 else if (!ignore_content) \
908 time->cat = now->val.num; \
909 break
911 INT_ELEM (first_weekday);
912 INT_ELEM (first_workday);
913 INT_ELEM (cal_direction);
915 case tok_week:
916 /* Ignore the rest of the line if we don't need the input of
917 this line. */
918 if (ignore_content)
920 lr_ignore_rest (ldfile, 0);
921 break;
924 now = lr_token (ldfile, charmap, result, repertoire, verbose);
925 if (now->tok != tok_number)
926 goto err_label;
927 time->week_ndays = now->val.num;
929 now = lr_token (ldfile, charmap, result, repertoire, verbose);
930 if (now->tok != tok_semicolon)
931 goto err_label;
933 now = lr_token (ldfile, charmap, result, repertoire, verbose);
934 if (now->tok != tok_number)
935 goto err_label;
936 time->week_1stday = now->val.num;
938 now = lr_token (ldfile, charmap, result, repertoire, verbose);
939 if (now->tok != tok_semicolon)
940 goto err_label;
942 now = lr_token (ldfile, charmap, result, repertoire, verbose);
943 if (now->tok != tok_number)
944 goto err_label;
945 time->week_1stweek = now->val.num;
947 lr_ignore_rest (ldfile, 1);
948 break;
950 case tok_end:
951 /* Next we assume `LC_TIME'. */
952 now = lr_token (ldfile, charmap, result, repertoire, verbose);
953 if (now->tok == tok_eof)
954 break;
955 if (now->tok == tok_eol)
956 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TIME");
957 else if (now->tok != tok_lc_time)
958 lr_error (ldfile, _("\
959 %1$s: definition does not end with `END %1$s'"), "LC_TIME");
960 lr_ignore_rest (ldfile, now->tok == tok_lc_time);
962 /* If alt_mon was not specified, make it a copy of mon. */
963 if (!ignore_content && !time->alt_mon_defined)
965 memcpy (time->alt_mon, time->mon, sizeof (time->mon));
966 memcpy (time->walt_mon, time->wmon, sizeof (time->wmon));
967 time->alt_mon_defined = 1;
969 /* The same for abbreviated versions. */
970 if (!ignore_content && !time->ab_alt_mon_defined)
972 memcpy (time->ab_alt_mon, time->abmon, sizeof (time->abmon));
973 memcpy (time->wab_alt_mon, time->wabmon, sizeof (time->wabmon));
974 time->ab_alt_mon_defined = 1;
976 return;
978 default:
979 err_label:
980 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
983 /* Prepare for the next round. */
984 now = lr_token (ldfile, charmap, result, repertoire, verbose);
985 nowtok = now->tok;
988 /* When we come here we reached the end of the file. */
989 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");