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