Update.
[glibc.git] / locale / programs / ld-time.c
blob32d9dad971193ace76d0a69ed6ca77de02c9833c
1 /* Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <byteswap.h>
25 #include <langinfo.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wchar.h>
29 #include <sys/uio.h>
31 #include <assert.h>
33 #include "localedef.h"
34 #include "linereader.h"
35 #include "localeinfo.h"
36 #include "locfile.h"
39 /* Entry describing an entry of the era specification. */
40 struct era_data
42 int32_t direction;
43 int32_t offset;
44 int32_t start_date[3];
45 int32_t stop_date[3];
46 const char *name;
47 const char *format;
48 uint32_t *wname;
49 uint32_t *wformat;
53 /* The real definition of the struct for the LC_TIME locale. */
54 struct locale_time_t
56 const char *abday[7];
57 const uint32_t *wabday[7];
58 int abday_defined;
59 const char *day[7];
60 const uint32_t *wday[7];
61 int day_defined;
62 const char *abmon[12];
63 const uint32_t *wabmon[12];
64 int abmon_defined;
65 const char *mon[12];
66 const uint32_t *wmon[12];
67 int mon_defined;
68 const char *am_pm[2];
69 const uint32_t *wam_pm[2];
70 int am_pm_defined;
71 const char *d_t_fmt;
72 const uint32_t *wd_t_fmt;
73 const char *d_fmt;
74 const uint32_t *wd_fmt;
75 const char *t_fmt;
76 const uint32_t *wt_fmt;
77 const char *t_fmt_ampm;
78 const uint32_t *wt_fmt_ampm;
79 const char **era;
80 const uint32_t **wera;
81 uint32_t num_era;
82 const char *era_year;
83 const uint32_t *wera_year;
84 const char *era_d_t_fmt;
85 const uint32_t *wera_d_t_fmt;
86 const char *era_t_fmt;
87 const uint32_t *wera_t_fmt;
88 const char *era_d_fmt;
89 const uint32_t *wera_d_fmt;
90 const char *alt_digits[100];
91 const uint32_t *walt_digits[100];
92 const char *date_fmt;
93 const uint32_t *wdate_fmt;
94 int alt_digits_defined;
95 unsigned char week_ndays;
96 uint32_t week_1stday;
97 unsigned char week_1stweek;
98 unsigned char first_weekday;
99 unsigned char first_workday;
100 unsigned char cal_direction;
101 const char *timezone;
102 const uint32_t *wtimezone;
104 struct era_data *era_entries;
108 /* This constant is used to represent an empty wide character string. */
109 static const uint32_t empty_wstr[1] = { 0 };
112 static void
113 time_startup (struct linereader *lr, struct localedef_t *locale,
114 int ignore_content)
116 if (!ignore_content)
117 locale->categories[LC_TIME].time =
118 (struct locale_time_t *) xcalloc (1, sizeof (struct locale_time_t));
120 if (lr != NULL)
122 lr->translate_strings = 1;
123 lr->return_widestr = 1;
128 void
129 time_finish (struct localedef_t *locale, const struct charmap_t *charmap)
131 struct locale_time_t *time = locale->categories[LC_TIME].time;
132 int nothing = 0;
134 /* Now resolve copying and also handle completely missing definitions. */
135 if (time == NULL)
137 /* First see whether we were supposed to copy. If yes, find the
138 actual definition. */
139 if (locale->copy_name[LC_TIME] != NULL)
141 /* Find the copying locale. This has to happen transitively since
142 the locale we are copying from might also copying another one. */
143 struct localedef_t *from = locale;
146 from = find_locale (LC_TIME, from->copy_name[LC_TIME],
147 from->repertoire_name, charmap);
148 while (from->categories[LC_TIME].time == NULL
149 && from->copy_name[LC_TIME] != NULL);
151 time = locale->categories[LC_TIME].time
152 = from->categories[LC_TIME].time;
155 /* If there is still no definition issue an warning and create an
156 empty one. */
157 if (time == NULL)
159 if (! be_quiet)
160 WITH_CUR_LOCALE (error (0, 0, _("\
161 No definition for %s category found"), "LC_TIME"));
162 time_startup (NULL, locale, 0);
163 time = locale->categories[LC_TIME].time;
164 nothing = 1;
168 #define noparen(arg1, argn...) arg1, ##argn
169 #define TESTARR_ELEM(cat, val) \
170 if (!time->cat##_defined) \
172 const char *initval[] = { noparen val }; \
173 int i; \
175 if (! be_quiet && ! nothing) \
176 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
177 "LC_TIME", #cat)); \
179 for (i = 0; i < sizeof (initval) / sizeof (initval[0]); ++i) \
180 time->cat[i] = initval[i]; \
183 TESTARR_ELEM (abday, ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ));
184 TESTARR_ELEM (day, ( "Sunday", "Monday", "Tuesday", "Wednesday",
185 "Thursday", "Friday", "Saturday" ));
186 TESTARR_ELEM (abmon, ( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
187 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ));
188 TESTARR_ELEM (mon, ( "January", "February", "March", "April",
189 "May", "June", "July", "August",
190 "September", "October", "November", "December" ));
191 TESTARR_ELEM (am_pm, ( "AM", "PM" ));
193 #define TEST_ELEM(cat, initval) \
194 if (time->cat == NULL) \
196 if (! be_quiet && ! nothing) \
197 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
198 "LC_TIME", #cat)); \
200 time->cat = initval; \
203 TEST_ELEM (d_t_fmt, "%a %b %e %H:%M:%S %Y");
204 TEST_ELEM (d_fmt, "%m/%d/%y");
205 TEST_ELEM (t_fmt, "%H:%M:%S");
207 /* According to C.Y.Alexis Cheng <alexis@vnet.ibm.com> the T_FMT_AMPM
208 field is optional. */
209 if (time->t_fmt_ampm == NULL)
211 if (time->am_pm[0][0] == '\0' && time->am_pm[1][0] == '\0')
213 /* No AM/PM strings defined, use the 24h format as default. */
214 time->t_fmt_ampm = time->t_fmt;
215 time->wt_fmt_ampm = time->wt_fmt;
217 else
219 time->t_fmt_ampm = "%I:%M:%S %p";
220 time->wt_fmt_ampm = (const uint32_t *) L"%I:%M:%S %p";
224 /* Now process the era entries. */
225 if (time->num_era != 0)
227 const int days_per_month[12] = { 31, 29, 31, 30, 31, 30,
228 31, 31, 30, 31 ,30, 31 };
229 size_t idx;
230 wchar_t *wstr;
232 time->era_entries =
233 (struct era_data *) xmalloc (time->num_era
234 * sizeof (struct era_data));
236 for (idx = 0; idx < time->num_era; ++idx)
238 size_t era_len = strlen (time->era[idx]);
239 char *str = xmalloc ((era_len + 1 + 3) & ~3);
240 char *endp;
242 memcpy (str, time->era[idx], era_len + 1);
244 /* First character must be + or - for the direction. */
245 if (*str != '+' && *str != '-')
247 if (!be_quiet)
248 WITH_CUR_LOCALE (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 = '+';
254 else
255 time->era_entries[idx].direction = *str;
256 if (*++str != ':')
258 if (!be_quiet)
259 WITH_CUR_LOCALE (error (0, 0, _("\
260 %s: direction flag in string %Zd in `era' field is not a single character"),
261 "LC_TIME", idx + 1));
262 (void) strsep (&str, ":");
264 else
265 ++str;
267 /* Now the offset year. */
268 time->era_entries[idx].offset = strtol (str, &endp, 10);
269 if (endp == str)
271 if (!be_quiet)
272 WITH_CUR_LOCALE (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 if (!be_quiet)
280 WITH_CUR_LOCALE (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, ":");
285 else
286 str = endp + 1;
288 /* Next is the starting date in ISO format. */
289 if (strncmp (str, "-*", 2) == 0)
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;
298 else if (strncmp (str, "+*", 2) == 0)
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;
307 else
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];
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;
326 time->era_entries[idx].start_date[2] = strtol (str, &endp, 10);
327 if (endp == str)
329 invalid_start_date:
330 if (!be_quiet)
331 WITH_CUR_LOCALE (error (0, 0, _("\
332 %s: invalid starting date in string %Zd in `era' field"),
333 "LC_TIME", idx + 1));
334 (void) strsep (&str, ":");
336 else if (*endp != ':')
338 garbage_start_date:
339 if (!be_quiet)
340 WITH_CUR_LOCALE (error (0, 0, _("\
341 %s: garbage at end of starting date in string %Zd in `era' field "),
342 "LC_TIME", idx + 1));
343 (void) strsep (&str, ":");
345 else
347 str = endp + 1;
349 /* Check for valid value. */
350 if ((time->era_entries[idx].start_date[1] < 0
351 || time->era_entries[idx].start_date[1] >= 12
352 || time->era_entries[idx].start_date[2] < 0
353 || (time->era_entries[idx].start_date[2]
354 > days_per_month[time->era_entries[idx].start_date[1]])
355 || (time->era_entries[idx].start_date[1] == 2
356 && time->era_entries[idx].start_date[2] == 29
357 && !__isleap (time->era_entries[idx].start_date[0])))
358 && !be_quiet)
359 WITH_CUR_LOCALE (error (0, 0, _("\
360 %s: starting date is invalid in string %Zd in `era' field"),
361 "LC_TIME", idx + 1));
365 /* Next is the stopping date in ISO format. */
366 if (strncmp (str, "-*", 2) == 0)
368 time->era_entries[idx].stop_date[0] =
369 time->era_entries[idx].stop_date[1] =
370 time->era_entries[idx].stop_date[2] = 0x80000000;
371 if (str[2] != ':')
372 goto garbage_stop_date;
373 str += 3;
375 else if (strncmp (str, "+*", 2) == 0)
377 time->era_entries[idx].stop_date[0] =
378 time->era_entries[idx].stop_date[1] =
379 time->era_entries[idx].stop_date[2] = 0x7fffffff;
380 if (str[2] != ':')
381 goto garbage_stop_date;
382 str += 3;
384 else
386 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
387 if (endp == str || *endp != '/')
388 goto invalid_stop_date;
389 else
390 str = endp + 1;
391 time->era_entries[idx].stop_date[0] -= 1900;
392 /* year -1 represent 1 B.C. (not -1 A.D.) */
393 if (time->era_entries[idx].stop_date[0] < -1900)
394 ++time->era_entries[idx].stop_date[0];
396 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
397 if (endp == str || *endp != '/')
398 goto invalid_stop_date;
399 else
400 str = endp + 1;
401 time->era_entries[idx].stop_date[1] -= 1;
403 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
404 if (endp == str)
406 invalid_stop_date:
407 if (!be_quiet)
408 WITH_CUR_LOCALE (error (0, 0, _("\
409 %s: invalid stopping date in string %Zd in `era' field"),
410 "LC_TIME", idx + 1));
411 (void) strsep (&str, ":");
413 else if (*endp != ':')
415 garbage_stop_date:
416 if (!be_quiet)
417 WITH_CUR_LOCALE (error (0, 0, _("\
418 %s: garbage at end of stopping date in string %Zd in `era' field"),
419 "LC_TIME", idx + 1));
420 (void) strsep (&str, ":");
422 else
424 str = endp + 1;
426 /* Check for valid value. */
427 if ((time->era_entries[idx].stop_date[1] < 0
428 || time->era_entries[idx].stop_date[1] >= 12
429 || time->era_entries[idx].stop_date[2] < 0
430 || (time->era_entries[idx].stop_date[2]
431 > days_per_month[time->era_entries[idx].stop_date[1]])
432 || (time->era_entries[idx].stop_date[1] == 2
433 && time->era_entries[idx].stop_date[2] == 29
434 && !__isleap (time->era_entries[idx].stop_date[0])))
435 && !be_quiet)
436 WITH_CUR_LOCALE (error (0, 0, _("\
437 %s: stopping date is invalid in string %Zd in `era' field"),
438 "LC_TIME", idx + 1));
442 if (str == NULL || *str == '\0')
444 if (!be_quiet)
445 WITH_CUR_LOCALE (error (0, 0, _("\
446 %s: missing era name in string %Zd in `era' field"), "LC_TIME", idx + 1));
447 time->era_entries[idx].name =
448 time->era_entries[idx].format = "";
450 else
452 time->era_entries[idx].name = strsep (&str, ":");
454 if (str == NULL || *str == '\0')
456 if (!be_quiet)
457 WITH_CUR_LOCALE (error (0, 0, _("\
458 %s: missing era format in string %Zd in `era' field"),
459 "LC_TIME", idx + 1));
460 time->era_entries[idx].name =
461 time->era_entries[idx].format = "";
463 else
464 time->era_entries[idx].format = str;
467 /* Now generate the wide character name and format. */
468 wstr = wcschr ((wchar_t *) time->wera[idx], L':');/* end direction */
469 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end offset */
470 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end start */
471 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end end */
472 time->era_entries[idx].wname = (uint32_t *) wstr + 1;
473 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end name */
474 *wstr = L'\0';
475 time->era_entries[idx].wformat = (uint32_t *) wstr + 1;
479 if (time->week_ndays == 0)
480 time->week_ndays = 7;
482 if (time->week_1stday == 0)
483 time->week_1stday = 19971130;
485 if (time->week_1stweek > time->week_ndays)
486 WITH_CUR_LOCALE (error (0, 0, _("\
487 %s: third operand for value of field `%s' must not be larger than %d"),
488 "LC_TIME", "week", 7));
490 if (time->first_weekday == '\0')
491 /* The definition does not specify this so the default is used. */
492 time->first_weekday = 1;
493 else if (time->first_weekday > time->week_ndays)
494 WITH_CUR_LOCALE (error (0, 0, _("\
495 %s: values of field `%s' must not be larger than %d"),
496 "LC_TIME", "first_weekday", 7));
498 if (time->first_workday == '\0')
499 /* The definition does not specify this so the default is used. */
500 time->first_workday = 1;
501 else if (time->first_workday > time->week_ndays)
502 WITH_CUR_LOCALE (error (0, 0, _("\
503 %s: values of field `%s' must not be larger than %d"),
504 "LC_TIME", "first_workday", 7));
506 if (time->cal_direction == '\0')
507 /* The definition does not specify this so the default is used. */
508 time->cal_direction = 1;
509 else if (time->cal_direction > 3)
510 WITH_CUR_LOCALE (error (0, 0, _("\
511 %s: values for field `%s' must not be larger than %d"),
512 "LC_TIME", "cal_direction", 3));
514 /* XXX We don't perform any tests on the timezone value since this is
515 simply useless, stupid $&$!@... */
516 if (time->timezone == NULL)
517 time->timezone = "";
519 if (time->date_fmt == NULL)
520 time->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
521 if (time->wdate_fmt == NULL)
522 time->wdate_fmt = (const uint32_t *) L"%a %b %e %H:%M:%S %Z %Y";
526 void
527 time_output (struct localedef_t *locale, const struct charmap_t *charmap,
528 const char *output_path)
530 struct locale_time_t *time = locale->categories[LC_TIME].time;
531 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_TIME)
532 + time->num_era - 1
533 + 2 * 99
534 + 2 + time->num_era * 10 - 1];
535 struct locale_file data;
536 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TIME)];
537 size_t cnt, last_idx, num, n;
539 data.magic = LIMAGIC (LC_TIME);
540 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TIME);
541 iov[0].iov_base = (void *) &data;
542 iov[0].iov_len = sizeof (data);
544 iov[1].iov_base = (void *) idx;
545 iov[1].iov_len = sizeof (idx);
547 idx[0] = iov[0].iov_len + iov[1].iov_len;
549 /* The ab'days. */
550 for (cnt = 0; cnt <= _NL_ITEM_INDEX (ABDAY_7); ++cnt)
552 iov[2 + cnt].iov_base =
553 (void *) (time->abday[cnt - _NL_ITEM_INDEX (ABDAY_1)] ?: "");
554 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
555 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
558 /* The days. */
559 for (; cnt <= _NL_ITEM_INDEX (DAY_7); ++cnt)
561 iov[2 + cnt].iov_base =
562 (void *) (time->day[cnt - _NL_ITEM_INDEX (DAY_1)] ?: "");
563 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
564 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
567 /* The ab'mons. */
568 for (; cnt <= _NL_ITEM_INDEX (ABMON_12); ++cnt)
570 iov[2 + cnt].iov_base =
571 (void *) (time->abmon[cnt - _NL_ITEM_INDEX (ABMON_1)] ?: "");
572 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
573 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
576 /* The mons. */
577 for (; cnt <= _NL_ITEM_INDEX (MON_12); ++cnt)
579 iov[2 + cnt].iov_base =
580 (void *) (time->mon[cnt - _NL_ITEM_INDEX (MON_1)] ?: "");
581 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
582 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
585 /* AM/PM. */
586 for (; cnt <= _NL_ITEM_INDEX (PM_STR); ++cnt)
588 iov[2 + cnt].iov_base =
589 (void *) (time->am_pm[cnt - _NL_ITEM_INDEX (AM_STR)] ?: "");
590 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
591 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
594 iov[2 + cnt].iov_base = (void *) (time->d_t_fmt ?: "");
595 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
596 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
597 ++cnt;
599 iov[2 + cnt].iov_base = (void *) (time->d_fmt ?: "");
600 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
601 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
602 ++cnt;
604 iov[2 + cnt].iov_base = (void *) (time->t_fmt ?: "");
605 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
606 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
607 ++cnt;
609 iov[2 + cnt].iov_base = (void *) (time->t_fmt_ampm ?: "");
610 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
611 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
612 last_idx = ++cnt;
614 idx[1 + last_idx] = idx[last_idx];
615 for (num = 0; num < time->num_era; ++num, ++cnt)
617 iov[2 + cnt].iov_base = (void *) time->era[num];
618 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
619 idx[1 + last_idx] += iov[2 + cnt].iov_len;
621 ++last_idx;
623 iov[2 + cnt].iov_base = (void *) (time->era_year ?: "");
624 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
625 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
626 ++cnt;
627 ++last_idx;
629 iov[2 + cnt].iov_base = (void *) (time->era_d_fmt ?: "");
630 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
631 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
632 ++cnt;
633 ++last_idx;
635 idx[1 + last_idx] = idx[last_idx];
636 for (num = 0; num < 100; ++num, ++cnt)
638 iov[2 + cnt].iov_base = (void *) (time->alt_digits[num] ?: "");
639 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
640 idx[1 + last_idx] += iov[2 + cnt].iov_len;
642 ++last_idx;
644 iov[2 + cnt].iov_base = (void *) (time->era_d_t_fmt ?: "");
645 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
646 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
647 ++cnt;
648 ++last_idx;
650 iov[2 + cnt].iov_base = (void *) (time->era_t_fmt ?: "");
651 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
652 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
653 ++cnt;
654 ++last_idx;
657 /* We must align the following data. */
658 iov[2 + cnt].iov_base = (void *) "\0\0";
659 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
660 idx[last_idx] = (idx[last_idx] + 3) & ~3;
661 ++cnt;
663 /* The `era' data in usable form. */
664 iov[2 + cnt].iov_base = (void *) &time->num_era;
665 iov[2 + cnt].iov_len = sizeof (uint32_t);
666 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
667 ++cnt;
668 ++last_idx;
670 idx[1 + last_idx] = idx[last_idx];
671 for (num = 0; num < time->num_era; ++num)
673 size_t l;
675 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].direction;
676 iov[2 + cnt].iov_len = sizeof (int32_t);
677 ++cnt;
678 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].offset;
679 iov[2 + cnt].iov_len = sizeof (int32_t);
680 ++cnt;
681 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[0];
682 iov[2 + cnt].iov_len = sizeof (int32_t);
683 ++cnt;
684 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[1];
685 iov[2 + cnt].iov_len = sizeof (int32_t);
686 ++cnt;
687 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[2];
688 iov[2 + cnt].iov_len = sizeof (int32_t);
689 ++cnt;
690 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[0];
691 iov[2 + cnt].iov_len = sizeof (int32_t);
692 ++cnt;
693 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[1];
694 iov[2 + cnt].iov_len = sizeof (int32_t);
695 ++cnt;
696 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[2];
697 iov[2 + cnt].iov_len = sizeof (int32_t);
698 ++cnt;
700 l = (strchr (time->era_entries[num].format, '\0')
701 - time->era_entries[num].name) + 1;
702 l = (l + 3) & ~3;
703 iov[2 + cnt].iov_base = (void *) time->era_entries[num].name;
704 iov[2 + cnt].iov_len = l;
705 ++cnt;
707 idx[1 + last_idx] += 8 * sizeof (int32_t) + l;
709 assert (idx[1 + last_idx] % 4 == 0);
711 iov[2 + cnt].iov_base = (void *) time->era_entries[num].wname;
712 iov[2 + cnt].iov_len = ((wcschr ((wchar_t *) time->era_entries[num].wformat, L'\0')
713 - (wchar_t *) time->era_entries[num].wname + 1)
714 * sizeof (uint32_t));
715 idx[1 + last_idx] += iov[2 + cnt].iov_len;
716 ++cnt;
718 ++last_idx;
720 /* The wide character ab'days. */
721 for (n = 0; n < 7; ++n, ++cnt, ++last_idx)
723 iov[2 + cnt].iov_base =
724 (void *) (time->wabday[n] ?: empty_wstr);
725 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
726 * sizeof (uint32_t));
727 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
730 /* The wide character days. */
731 for (n = 0; n < 7; ++n, ++cnt, ++last_idx)
733 iov[2 + cnt].iov_base =
734 (void *) (time->wday[n] ?: empty_wstr);
735 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
736 * sizeof (uint32_t));
737 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
740 /* The wide character ab'mons. */
741 for (n = 0; n < 12; ++n, ++cnt, ++last_idx)
743 iov[2 + cnt].iov_base =
744 (void *) (time->wabmon[n] ?: empty_wstr);
745 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
746 * sizeof (uint32_t));
747 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
750 /* The wide character mons. */
751 for (n = 0; n < 12; ++n, ++cnt, ++last_idx)
753 iov[2 + cnt].iov_base =
754 (void *) (time->wmon[n] ?: empty_wstr);
755 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
756 * sizeof (uint32_t));
757 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
760 /* Wide character AM/PM. */
761 for (n = 0; n < 2; ++n, ++cnt, ++last_idx)
763 iov[2 + cnt].iov_base =
764 (void *) (time->wam_pm[n] ?: empty_wstr);
765 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
766 * sizeof (uint32_t));
767 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
770 iov[2 + cnt].iov_base = (void *) (time->wd_t_fmt ?: empty_wstr);
771 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
772 * sizeof (uint32_t));
773 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
774 ++cnt;
775 ++last_idx;
777 iov[2 + cnt].iov_base = (void *) (time->wd_fmt ?: empty_wstr);
778 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
779 * sizeof (uint32_t));
780 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
781 ++cnt;
782 ++last_idx;
784 iov[2 + cnt].iov_base = (void *) (time->wt_fmt ?: empty_wstr);
785 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
786 * sizeof (uint32_t));
787 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
788 ++cnt;
789 ++last_idx;
791 iov[2 + cnt].iov_base = (void *) (time->wt_fmt_ampm ?: empty_wstr);
792 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
793 * sizeof (uint32_t));
794 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
795 ++cnt;
796 ++last_idx;
798 iov[2 + cnt].iov_base = (void *) (time->wera_year ?: empty_wstr);
799 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
800 * sizeof (uint32_t));
801 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
802 ++cnt;
803 ++last_idx;
805 iov[2 + cnt].iov_base = (void *) (time->wera_d_fmt ?: empty_wstr);
806 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
807 * sizeof (uint32_t));
808 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
809 ++cnt;
810 ++last_idx;
812 idx[1 + last_idx] = idx[last_idx];
813 for (num = 0; num < 100; ++num, ++cnt)
815 iov[2 + cnt].iov_base = (void *) (time->walt_digits[num]
816 ?: empty_wstr);
817 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
818 * sizeof (uint32_t));
819 idx[1 + last_idx] += iov[2 + cnt].iov_len;
821 ++last_idx;
823 iov[2 + cnt].iov_base = (void *) (time->wera_d_t_fmt ?: empty_wstr);
824 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
825 * sizeof (uint32_t));
826 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
827 ++cnt;
828 ++last_idx;
830 iov[2 + cnt].iov_base = (void *) (time->wera_t_fmt ?: empty_wstr);
831 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
832 * sizeof (uint32_t));
833 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
834 ++cnt;
835 ++last_idx;
837 iov[2 + cnt].iov_base = (void *) &time->week_ndays;
838 iov[2 + cnt].iov_len = 1;
839 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
840 ++cnt;
841 ++last_idx;
843 /* We must align the following data. */
844 iov[2 + cnt].iov_base = (void *) "\0\0";
845 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
846 idx[last_idx] = (idx[last_idx] + 3) & ~3;
847 ++cnt;
849 iov[2 + cnt].iov_base = (void *) &time->week_1stday;
850 iov[2 + cnt].iov_len = sizeof(uint32_t);
851 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
852 ++cnt;
853 ++last_idx;
855 iov[2 + cnt].iov_base = (void *) &time->week_1stweek;
856 iov[2 + cnt].iov_len = 1;
857 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
858 ++cnt;
859 ++last_idx;
861 iov[2 + cnt].iov_base = (void *) &time->first_weekday;
862 iov[2 + cnt].iov_len = 1;
863 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
864 ++cnt;
865 ++last_idx;
867 iov[2 + cnt].iov_base = (void *) &time->first_workday;
868 iov[2 + cnt].iov_len = 1;
869 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
870 ++cnt;
871 ++last_idx;
873 iov[2 + cnt].iov_base = (void *) &time->cal_direction;
874 iov[2 + cnt].iov_len = 1;
875 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
876 ++cnt;
877 ++last_idx;
879 iov[2 + cnt].iov_base = (void *) time->timezone;
880 iov[2 + cnt].iov_len = strlen (time->timezone) + 1;
881 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
882 ++cnt;
883 ++last_idx;
885 iov[2 + cnt].iov_base = (void *) time->date_fmt;
886 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
887 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
888 ++cnt;
889 ++last_idx;
891 iov[2 + cnt].iov_base = (void *) time->wdate_fmt;
892 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
893 * sizeof (uint32_t));
894 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
895 ++cnt;
896 ++last_idx;
898 iov[2 + cnt].iov_base = (void *) charmap->code_set_name;
899 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
900 ++cnt;
901 ++last_idx;
903 assert (cnt == (_NL_ITEM_INDEX (_NL_NUM_LC_TIME)
904 + time->num_era - 1
905 + 2 * 99
906 + 2 + time->num_era * 10 - 1));
907 assert (last_idx == _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
909 write_locale_data (output_path, "LC_TIME", 2 + cnt, iov);
913 /* The parser for the LC_TIME section of the locale definition. */
914 void
915 time_read (struct linereader *ldfile, struct localedef_t *result,
916 const struct charmap_t *charmap, const char *repertoire_name,
917 int ignore_content)
919 struct repertoire_t *repertoire = NULL;
920 struct locale_time_t *time;
921 struct token *now;
922 enum token_t nowtok;
923 size_t cnt;
925 /* Get the repertoire we have to use. */
926 if (repertoire_name != NULL)
927 repertoire = repertoire_read (repertoire_name);
929 /* The rest of the line containing `LC_TIME' must be free. */
930 lr_ignore_rest (ldfile, 1);
935 now = lr_token (ldfile, charmap, result, repertoire, verbose);
936 nowtok = now->tok;
938 while (nowtok == tok_eol);
940 /* If we see `copy' now we are almost done. */
941 if (nowtok == tok_copy)
943 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
944 LC_TIME, "LC_TIME", ignore_content);
945 return;
948 /* Prepare the data structures. */
949 time_startup (ldfile, result, ignore_content);
950 time = result->categories[LC_TIME].time;
952 while (1)
954 /* Of course we don't proceed beyond the end of file. */
955 if (nowtok == tok_eof)
956 break;
958 /* Ingore empty lines. */
959 if (nowtok == tok_eol)
961 now = lr_token (ldfile, charmap, result, repertoire, verbose);
962 nowtok = now->tok;
963 continue;
966 switch (nowtok)
968 #define STRARR_ELEM(cat, min, max) \
969 case tok_##cat: \
970 /* Ignore the rest of the line if we don't need the input of \
971 this line. */ \
972 if (ignore_content) \
974 lr_ignore_rest (ldfile, 0); \
975 break; \
978 for (cnt = 0; cnt < max; ++cnt) \
980 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
981 if (now->tok == tok_eol) \
983 if (cnt < min) \
984 lr_error (ldfile, _("%s: too few values for field `%s'"), \
985 "LC_TIME", #cat); \
986 if (!ignore_content) \
987 do \
989 time->cat[cnt] = ""; \
990 time->w##cat[cnt] = empty_wstr; \
992 while (++cnt < max); \
993 break; \
995 else if (now->tok != tok_string) \
996 goto err_label; \
997 else if (!ignore_content && (now->val.str.startmb == NULL \
998 || now->val.str.startwc == NULL)) \
1000 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
1001 "LC_TIME", #cat); \
1002 time->cat[cnt] = ""; \
1003 time->w##cat[cnt] = empty_wstr; \
1005 else if (!ignore_content) \
1007 time->cat[cnt] = now->val.str.startmb; \
1008 time->w##cat[cnt] = now->val.str.startwc; \
1011 /* Match the semicolon. */ \
1012 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1013 if (now->tok != tok_semicolon && now->tok != tok_eol) \
1014 break; \
1016 if (now->tok != tok_eol) \
1018 while (!ignore_content && cnt < min) \
1020 time->cat[cnt] = ""; \
1021 time->w##cat[cnt++] = empty_wstr; \
1024 if (now->tok == tok_semicolon) \
1026 now = lr_token (ldfile, charmap, result, repertoire, \
1027 verbose); \
1028 if (now->tok == tok_eol) \
1029 lr_error (ldfile, _("extra trailing semicolon")); \
1030 else if (now->tok == tok_string) \
1032 lr_error (ldfile, _("\
1033 %s: too many values for field `%s'"), \
1034 "LC_TIME", #cat); \
1035 lr_ignore_rest (ldfile, 0); \
1037 else \
1038 goto err_label; \
1040 else \
1041 goto err_label; \
1043 time->cat##_defined = 1; \
1044 break
1046 STRARR_ELEM (abday, 7, 7);
1047 STRARR_ELEM (day, 7, 7);
1048 STRARR_ELEM (abmon, 12, 12);
1049 STRARR_ELEM (mon, 12, 12);
1050 STRARR_ELEM (am_pm, 2, 2);
1051 STRARR_ELEM (alt_digits, 0, 100);
1053 case tok_era:
1054 /* Ignore the rest of the line if we don't need the input of
1055 this line. */
1056 if (ignore_content)
1058 lr_ignore_rest (ldfile, 0);
1059 break;
1063 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1064 if (now->tok != tok_string)
1065 goto err_label;
1066 if (!ignore_content && (now->val.str.startmb == NULL
1067 || now->val.str.startwc == NULL))
1069 lr_error (ldfile, _("%s: unknown character in field `%s'"),
1070 "LC_TIME", "era");
1071 lr_ignore_rest (ldfile, 0);
1072 break;
1074 if (!ignore_content)
1076 time->era = xrealloc (time->era,
1077 (time->num_era + 1) * sizeof (char *));
1078 time->era[time->num_era] = now->val.str.startmb;
1080 time->wera = xrealloc (time->wera,
1081 (time->num_era + 1)
1082 * sizeof (char *));
1083 time->wera[time->num_era++] = now->val.str.startwc;
1085 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1086 if (now->tok != tok_eol && now->tok != tok_semicolon)
1087 goto err_label;
1089 while (now->tok == tok_semicolon);
1090 break;
1092 #define STR_ELEM(cat) \
1093 case tok_##cat: \
1094 /* Ignore the rest of the line if we don't need the input of \
1095 this line. */ \
1096 if (ignore_content) \
1098 lr_ignore_rest (ldfile, 0); \
1099 break; \
1102 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1103 if (now->tok != tok_string) \
1104 goto err_label; \
1105 else if (time->cat != NULL) \
1106 lr_error (ldfile, _("\
1107 %s: field `%s' declared more than once"), "LC_TIME", #cat); \
1108 else if (!ignore_content && (now->val.str.startmb == NULL \
1109 || now->val.str.startwc == NULL)) \
1111 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
1112 "LC_TIME", #cat); \
1113 time->cat = ""; \
1114 time->w##cat = empty_wstr; \
1116 else if (!ignore_content) \
1118 time->cat = now->val.str.startmb; \
1119 time->w##cat = now->val.str.startwc; \
1121 break
1123 STR_ELEM (d_t_fmt);
1124 STR_ELEM (d_fmt);
1125 STR_ELEM (t_fmt);
1126 STR_ELEM (t_fmt_ampm);
1127 STR_ELEM (era_year);
1128 STR_ELEM (era_d_t_fmt);
1129 STR_ELEM (era_d_fmt);
1130 STR_ELEM (era_t_fmt);
1131 STR_ELEM (timezone);
1132 STR_ELEM (date_fmt);
1134 #define INT_ELEM(cat) \
1135 case tok_##cat: \
1136 /* Ignore the rest of the line if we don't need the input of \
1137 this line. */ \
1138 if (ignore_content) \
1140 lr_ignore_rest (ldfile, 0); \
1141 break; \
1144 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1145 if (now->tok != tok_number) \
1146 goto err_label; \
1147 else if (time->cat != 0) \
1148 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
1149 "LC_TIME", #cat); \
1150 else if (!ignore_content) \
1151 time->cat = now->val.num; \
1152 break
1154 INT_ELEM (first_weekday);
1155 INT_ELEM (first_workday);
1156 INT_ELEM (cal_direction);
1158 case tok_week:
1159 /* Ignore the rest of the line if we don't need the input of
1160 this line. */
1161 if (ignore_content)
1163 lr_ignore_rest (ldfile, 0);
1164 break;
1167 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1168 if (now->tok != tok_number)
1169 goto err_label;
1170 time->week_ndays = now->val.num;
1172 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1173 if (now->tok != tok_semicolon)
1174 goto err_label;
1176 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1177 if (now->tok != tok_number)
1178 goto err_label;
1179 time->week_1stday = now->val.num;
1181 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1182 if (now->tok != tok_semicolon)
1183 goto err_label;
1185 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1186 if (now->tok != tok_number)
1187 goto err_label;
1188 time->week_1stweek = now->val.num;
1190 lr_ignore_rest (ldfile, 1);
1191 break;
1193 case tok_end:
1194 /* Next we assume `LC_TIME'. */
1195 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1196 if (now->tok == tok_eof)
1197 break;
1198 if (now->tok == tok_eol)
1199 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TIME");
1200 else if (now->tok != tok_lc_time)
1201 lr_error (ldfile, _("\
1202 %1$s: definition does not end with `END %1$s'"), "LC_TIME");
1203 lr_ignore_rest (ldfile, now->tok == tok_lc_time);
1204 return;
1206 default:
1207 err_label:
1208 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
1211 /* Prepare for the next round. */
1212 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1213 nowtok = now->tok;
1216 /* When we come here we reached the end of the file. */
1217 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");