* sysdeps/i386/tls.h (THREAD_GSCOPE_RESET_FLAG): Use explicit
[glibc.git] / locale / programs / ld-time.c
blob0d193503718edcc930ea27da7759badb6fcce0d5
1 /* Copyright (C) 1995-2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <byteswap.h>
23 #include <langinfo.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wchar.h>
27 #include <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 unsigned char week_ndays;
94 uint32_t week_1stday;
95 unsigned char week_1stweek;
96 unsigned char first_weekday;
97 unsigned char first_workday;
98 unsigned char cal_direction;
99 const char *timezone;
100 const uint32_t *wtimezone;
102 struct era_data *era_entries;
106 /* This constant is used to represent an empty wide character string. */
107 static const uint32_t empty_wstr[1] = { 0 };
110 static void
111 time_startup (struct linereader *lr, struct localedef_t *locale,
112 int ignore_content)
114 if (!ignore_content)
115 locale->categories[LC_TIME].time =
116 (struct locale_time_t *) xcalloc (1, sizeof (struct locale_time_t));
118 if (lr != NULL)
120 lr->translate_strings = 1;
121 lr->return_widestr = 1;
126 void
127 time_finish (struct localedef_t *locale, const struct charmap_t *charmap)
129 struct locale_time_t *time = locale->categories[LC_TIME].time;
130 int nothing = 0;
132 /* Now resolve copying and also handle completely missing definitions. */
133 if (time == NULL)
135 /* First see whether we were supposed to copy. If yes, find the
136 actual definition. */
137 if (locale->copy_name[LC_TIME] != NULL)
139 /* Find the copying locale. This has to happen transitively since
140 the locale we are copying from might also copying another one. */
141 struct localedef_t *from = locale;
144 from = find_locale (LC_TIME, from->copy_name[LC_TIME],
145 from->repertoire_name, charmap);
146 while (from->categories[LC_TIME].time == NULL
147 && from->copy_name[LC_TIME] != NULL);
149 time = locale->categories[LC_TIME].time
150 = from->categories[LC_TIME].time;
153 /* If there is still no definition issue an warning and create an
154 empty one. */
155 if (time == NULL)
157 if (! be_quiet)
158 WITH_CUR_LOCALE (error (0, 0, _("\
159 No definition for %s category found"), "LC_TIME"));
160 time_startup (NULL, locale, 0);
161 time = locale->categories[LC_TIME].time;
162 nothing = 1;
166 #define noparen(arg1, argn...) arg1, ##argn
167 #define TESTARR_ELEM(cat, val) \
168 if (!time->cat##_defined) \
170 const char *initval[] = { noparen val }; \
171 unsigned int i; \
173 if (! be_quiet && ! nothing) \
174 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
175 "LC_TIME", #cat)); \
177 for (i = 0; i < sizeof (initval) / sizeof (initval[0]); ++i) \
178 time->cat[i] = initval[i]; \
181 TESTARR_ELEM (abday, ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ));
182 TESTARR_ELEM (day, ( "Sunday", "Monday", "Tuesday", "Wednesday",
183 "Thursday", "Friday", "Saturday" ));
184 TESTARR_ELEM (abmon, ( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
185 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ));
186 TESTARR_ELEM (mon, ( "January", "February", "March", "April",
187 "May", "June", "July", "August",
188 "September", "October", "November", "December" ));
189 TESTARR_ELEM (am_pm, ( "AM", "PM" ));
191 #define TEST_ELEM(cat, initval) \
192 if (time->cat == NULL) \
194 if (! be_quiet && ! nothing) \
195 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
196 "LC_TIME", #cat)); \
198 time->cat = initval; \
201 TEST_ELEM (d_t_fmt, "%a %b %e %H:%M:%S %Y");
202 TEST_ELEM (d_fmt, "%m/%d/%y");
203 TEST_ELEM (t_fmt, "%H:%M:%S");
205 /* According to C.Y.Alexis Cheng <alexis@vnet.ibm.com> the T_FMT_AMPM
206 field is optional. */
207 if (time->t_fmt_ampm == NULL)
209 if (time->am_pm[0][0] == '\0' && time->am_pm[1][0] == '\0')
211 /* No AM/PM strings defined, use the 24h format as default. */
212 time->t_fmt_ampm = time->t_fmt;
213 time->wt_fmt_ampm = time->wt_fmt;
215 else
217 time->t_fmt_ampm = "%I:%M:%S %p";
218 time->wt_fmt_ampm = (const uint32_t *) L"%I:%M:%S %p";
222 /* Now process the era entries. */
223 if (time->num_era != 0)
225 const int days_per_month[12] = { 31, 29, 31, 30, 31, 30,
226 31, 31, 30, 31 ,30, 31 };
227 size_t idx;
228 wchar_t *wstr;
230 time->era_entries =
231 (struct era_data *) xmalloc (time->num_era
232 * sizeof (struct era_data));
234 for (idx = 0; idx < time->num_era; ++idx)
236 size_t era_len = strlen (time->era[idx]);
237 char *str = xmalloc ((era_len + 1 + 3) & ~3);
238 char *endp;
240 memcpy (str, time->era[idx], era_len + 1);
242 /* First character must be + or - for the direction. */
243 if (*str != '+' && *str != '-')
245 if (!be_quiet)
246 WITH_CUR_LOCALE (error (0, 0, _("\
247 %s: direction flag in string %Zd in `era' field is not '+' nor '-'"),
248 "LC_TIME", idx + 1));
249 /* Default arbitrarily to '+'. */
250 time->era_entries[idx].direction = '+';
252 else
253 time->era_entries[idx].direction = *str;
254 if (*++str != ':')
256 if (!be_quiet)
257 WITH_CUR_LOCALE (error (0, 0, _("\
258 %s: direction flag in string %Zd in `era' field is not a single character"),
259 "LC_TIME", idx + 1));
260 (void) strsep (&str, ":");
262 else
263 ++str;
265 /* Now the offset year. */
266 time->era_entries[idx].offset = strtol (str, &endp, 10);
267 if (endp == str)
269 if (!be_quiet)
270 WITH_CUR_LOCALE (error (0, 0, _("\
271 %s: invalid number for offset in string %Zd in `era' field"),
272 "LC_TIME", idx + 1));
273 (void) strsep (&str, ":");
275 else if (*endp != ':')
277 if (!be_quiet)
278 WITH_CUR_LOCALE (error (0, 0, _("\
279 %s: garbage at end of offset value in string %Zd in `era' field"),
280 "LC_TIME", idx + 1));
281 (void) strsep (&str, ":");
283 else
284 str = endp + 1;
286 /* Next is the starting date in ISO format. */
287 if (strncmp (str, "-*", 2) == 0)
289 time->era_entries[idx].start_date[0] =
290 time->era_entries[idx].start_date[1] =
291 time->era_entries[idx].start_date[2] = 0x80000000;
292 if (str[2] != ':')
293 goto garbage_start_date;
294 str += 3;
296 else if (strncmp (str, "+*", 2) == 0)
298 time->era_entries[idx].start_date[0] =
299 time->era_entries[idx].start_date[1] =
300 time->era_entries[idx].start_date[2] = 0x7fffffff;
301 if (str[2] != ':')
302 goto garbage_start_date;
303 str += 3;
305 else
307 time->era_entries[idx].start_date[0] = strtol (str, &endp, 10);
308 if (endp == str || *endp != '/')
309 goto invalid_start_date;
310 else
311 str = endp + 1;
312 time->era_entries[idx].start_date[0] -= 1900;
313 /* year -1 represent 1 B.C. (not -1 A.D.) */
314 if (time->era_entries[idx].start_date[0] < -1900)
315 ++time->era_entries[idx].start_date[0];
317 time->era_entries[idx].start_date[1] = strtol (str, &endp, 10);
318 if (endp == str || *endp != '/')
319 goto invalid_start_date;
320 else
321 str = endp + 1;
322 time->era_entries[idx].start_date[1] -= 1;
324 time->era_entries[idx].start_date[2] = strtol (str, &endp, 10);
325 if (endp == str)
327 invalid_start_date:
328 if (!be_quiet)
329 WITH_CUR_LOCALE (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 if (!be_quiet)
338 WITH_CUR_LOCALE (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, ":");
343 else
345 str = endp + 1;
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 && !be_quiet)
357 WITH_CUR_LOCALE (error (0, 0, _("\
358 %s: starting date is invalid in string %Zd in `era' field"),
359 "LC_TIME", idx + 1));
363 /* Next is the stopping date in ISO format. */
364 if (strncmp (str, "-*", 2) == 0)
366 time->era_entries[idx].stop_date[0] =
367 time->era_entries[idx].stop_date[1] =
368 time->era_entries[idx].stop_date[2] = 0x80000000;
369 if (str[2] != ':')
370 goto garbage_stop_date;
371 str += 3;
373 else if (strncmp (str, "+*", 2) == 0)
375 time->era_entries[idx].stop_date[0] =
376 time->era_entries[idx].stop_date[1] =
377 time->era_entries[idx].stop_date[2] = 0x7fffffff;
378 if (str[2] != ':')
379 goto garbage_stop_date;
380 str += 3;
382 else
384 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
385 if (endp == str || *endp != '/')
386 goto invalid_stop_date;
387 else
388 str = endp + 1;
389 time->era_entries[idx].stop_date[0] -= 1900;
390 /* year -1 represent 1 B.C. (not -1 A.D.) */
391 if (time->era_entries[idx].stop_date[0] < -1900)
392 ++time->era_entries[idx].stop_date[0];
394 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
395 if (endp == str || *endp != '/')
396 goto invalid_stop_date;
397 else
398 str = endp + 1;
399 time->era_entries[idx].stop_date[1] -= 1;
401 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
402 if (endp == str)
404 invalid_stop_date:
405 if (!be_quiet)
406 WITH_CUR_LOCALE (error (0, 0, _("\
407 %s: invalid stopping date in string %Zd in `era' field"),
408 "LC_TIME", idx + 1));
409 (void) strsep (&str, ":");
411 else if (*endp != ':')
413 garbage_stop_date:
414 if (!be_quiet)
415 WITH_CUR_LOCALE (error (0, 0, _("\
416 %s: garbage at end of stopping date in string %Zd in `era' field"),
417 "LC_TIME", idx + 1));
418 (void) strsep (&str, ":");
420 else
422 str = endp + 1;
424 /* Check for valid value. */
425 if ((time->era_entries[idx].stop_date[1] < 0
426 || time->era_entries[idx].stop_date[1] >= 12
427 || time->era_entries[idx].stop_date[2] < 0
428 || (time->era_entries[idx].stop_date[2]
429 > days_per_month[time->era_entries[idx].stop_date[1]])
430 || (time->era_entries[idx].stop_date[1] == 2
431 && time->era_entries[idx].stop_date[2] == 29
432 && !__isleap (time->era_entries[idx].stop_date[0])))
433 && !be_quiet)
434 WITH_CUR_LOCALE (error (0, 0, _("\
435 %s: stopping date is invalid in string %Zd in `era' field"),
436 "LC_TIME", idx + 1));
440 if (str == NULL || *str == '\0')
442 if (!be_quiet)
443 WITH_CUR_LOCALE (error (0, 0, _("\
444 %s: missing era name in string %Zd in `era' field"), "LC_TIME", idx + 1));
445 time->era_entries[idx].name =
446 time->era_entries[idx].format = "";
448 else
450 time->era_entries[idx].name = strsep (&str, ":");
452 if (str == NULL || *str == '\0')
454 if (!be_quiet)
455 WITH_CUR_LOCALE (error (0, 0, _("\
456 %s: missing era format in string %Zd in `era' field"),
457 "LC_TIME", idx + 1));
458 time->era_entries[idx].name =
459 time->era_entries[idx].format = "";
461 else
462 time->era_entries[idx].format = str;
465 /* Now generate the wide character name and format. */
466 wstr = wcschr ((wchar_t *) time->wera[idx], L':');/* end direction */
467 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end offset */
468 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end start */
469 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end end */
470 if (wstr != NULL)
472 time->era_entries[idx].wname = (uint32_t *) wstr + 1;
473 wstr = wcschr (wstr + 1, L':'); /* end name */
474 if (wstr != NULL)
476 *wstr = L'\0';
477 time->era_entries[idx].wformat = (uint32_t *) wstr + 1;
479 else
480 time->era_entries[idx].wname =
481 time->era_entries[idx].wformat = (uint32_t *) L"";
483 else
484 time->era_entries[idx].wname =
485 time->era_entries[idx].wformat = (uint32_t *) L"";
489 if (time->week_ndays == 0)
490 time->week_ndays = 7;
492 if (time->week_1stday == 0)
493 time->week_1stday = 19971130;
495 if (time->week_1stweek > time->week_ndays)
496 WITH_CUR_LOCALE (error (0, 0, _("\
497 %s: third operand for value of field `%s' must not be larger than %d"),
498 "LC_TIME", "week", 7));
500 if (time->first_weekday == '\0')
501 /* The definition does not specify this so the default is used. */
502 time->first_weekday = 1;
503 else if (time->first_weekday > time->week_ndays)
504 WITH_CUR_LOCALE (error (0, 0, _("\
505 %s: values for field `%s' must not be larger than %d"),
506 "LC_TIME", "first_weekday", 7));
508 if (time->first_workday == '\0')
509 /* The definition does not specify this so the default is used. */
510 time->first_workday = 1;
511 else if (time->first_workday > time->week_ndays)
512 WITH_CUR_LOCALE (error (0, 0, _("\
513 %s: values for field `%s' must not be larger than %d"),
514 "LC_TIME", "first_workday", 7));
516 if (time->cal_direction == '\0')
517 /* The definition does not specify this so the default is used. */
518 time->cal_direction = 1;
519 else if (time->cal_direction > 3)
520 WITH_CUR_LOCALE (error (0, 0, _("\
521 %s: values for field `%s' must not be larger than %d"),
522 "LC_TIME", "cal_direction", 3));
524 /* XXX We don't perform any tests on the timezone value since this is
525 simply useless, stupid $&$!@... */
526 if (time->timezone == NULL)
527 time->timezone = "";
529 if (time->date_fmt == NULL)
530 time->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
531 if (time->wdate_fmt == NULL)
532 time->wdate_fmt = (const uint32_t *) L"%a %b %e %H:%M:%S %Z %Y";
536 void
537 time_output (struct localedef_t *locale, const struct charmap_t *charmap,
538 const char *output_path)
540 struct locale_time_t *time = locale->categories[LC_TIME].time;
541 struct iovec *iov = alloca (sizeof *iov
542 * (2 + _NL_ITEM_INDEX (_NL_NUM_LC_TIME)
543 + time->num_era - 1
544 + 2 * 99
545 + 2 + time->num_era * 10));
546 struct locale_file data;
547 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TIME)];
548 size_t cnt, last_idx, num, n;
550 data.magic = LIMAGIC (LC_TIME);
551 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TIME);
552 iov[0].iov_base = (void *) &data;
553 iov[0].iov_len = sizeof (data);
555 iov[1].iov_base = (void *) idx;
556 iov[1].iov_len = sizeof (idx);
558 idx[0] = iov[0].iov_len + iov[1].iov_len;
560 /* The ab'days. */
561 for (cnt = 0; cnt <= _NL_ITEM_INDEX (ABDAY_7); ++cnt)
563 iov[2 + cnt].iov_base =
564 (void *) (time->abday[cnt - _NL_ITEM_INDEX (ABDAY_1)] ?: "");
565 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
566 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
569 /* The days. */
570 for (; cnt <= _NL_ITEM_INDEX (DAY_7); ++cnt)
572 iov[2 + cnt].iov_base =
573 (void *) (time->day[cnt - _NL_ITEM_INDEX (DAY_1)] ?: "");
574 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
575 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
578 /* The ab'mons. */
579 for (; cnt <= _NL_ITEM_INDEX (ABMON_12); ++cnt)
581 iov[2 + cnt].iov_base =
582 (void *) (time->abmon[cnt - _NL_ITEM_INDEX (ABMON_1)] ?: "");
583 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
584 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
587 /* The mons. */
588 for (; cnt <= _NL_ITEM_INDEX (MON_12); ++cnt)
590 iov[2 + cnt].iov_base =
591 (void *) (time->mon[cnt - _NL_ITEM_INDEX (MON_1)] ?: "");
592 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
593 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
596 /* AM/PM. */
597 for (; cnt <= _NL_ITEM_INDEX (PM_STR); ++cnt)
599 iov[2 + cnt].iov_base =
600 (void *) (time->am_pm[cnt - _NL_ITEM_INDEX (AM_STR)] ?: "");
601 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
602 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
605 iov[2 + cnt].iov_base = (void *) (time->d_t_fmt ?: "");
606 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
607 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
608 ++cnt;
610 iov[2 + cnt].iov_base = (void *) (time->d_fmt ?: "");
611 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
612 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
613 ++cnt;
615 iov[2 + cnt].iov_base = (void *) (time->t_fmt ?: "");
616 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
617 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
618 ++cnt;
620 iov[2 + cnt].iov_base = (void *) (time->t_fmt_ampm ?: "");
621 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
622 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
623 last_idx = ++cnt;
625 idx[1 + last_idx] = idx[last_idx];
626 for (num = 0; num < time->num_era; ++num, ++cnt)
628 iov[2 + cnt].iov_base = (void *) time->era[num];
629 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
630 idx[1 + last_idx] += iov[2 + cnt].iov_len;
632 ++last_idx;
634 iov[2 + cnt].iov_base = (void *) (time->era_year ?: "");
635 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
636 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
637 ++cnt;
638 ++last_idx;
640 iov[2 + cnt].iov_base = (void *) (time->era_d_fmt ?: "");
641 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
642 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
643 ++cnt;
644 ++last_idx;
646 idx[1 + last_idx] = idx[last_idx];
647 for (num = 0; num < 100; ++num, ++cnt)
649 iov[2 + cnt].iov_base = (void *) (time->alt_digits[num] ?: "");
650 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
651 idx[1 + last_idx] += iov[2 + cnt].iov_len;
653 ++last_idx;
655 iov[2 + cnt].iov_base = (void *) (time->era_d_t_fmt ?: "");
656 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
657 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
658 ++cnt;
659 ++last_idx;
661 iov[2 + cnt].iov_base = (void *) (time->era_t_fmt ?: "");
662 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
663 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
664 ++cnt;
665 ++last_idx;
668 /* We must align the following data. */
669 iov[2 + cnt].iov_base = (void *) "\0\0";
670 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
671 idx[last_idx] = (idx[last_idx] + 3) & ~3;
672 ++cnt;
674 /* The `era' data in usable form. */
675 iov[2 + cnt].iov_base = (void *) &time->num_era;
676 iov[2 + cnt].iov_len = sizeof (uint32_t);
677 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
678 ++cnt;
679 ++last_idx;
681 idx[1 + last_idx] = idx[last_idx];
682 for (num = 0; num < time->num_era; ++num)
684 size_t l, l2;
686 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].direction;
687 iov[2 + cnt].iov_len = sizeof (int32_t);
688 ++cnt;
689 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].offset;
690 iov[2 + cnt].iov_len = sizeof (int32_t);
691 ++cnt;
692 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[0];
693 iov[2 + cnt].iov_len = sizeof (int32_t);
694 ++cnt;
695 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[1];
696 iov[2 + cnt].iov_len = sizeof (int32_t);
697 ++cnt;
698 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[2];
699 iov[2 + cnt].iov_len = sizeof (int32_t);
700 ++cnt;
701 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[0];
702 iov[2 + cnt].iov_len = sizeof (int32_t);
703 ++cnt;
704 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[1];
705 iov[2 + cnt].iov_len = sizeof (int32_t);
706 ++cnt;
707 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[2];
708 iov[2 + cnt].iov_len = sizeof (int32_t);
709 ++cnt;
711 l = ((char *) rawmemchr (time->era_entries[num].format, '\0')
712 - time->era_entries[num].name) + 1;
713 l2 = (l + 3) & ~3;
714 iov[2 + cnt].iov_base = alloca (l2);
715 memset (mempcpy (iov[2 + cnt].iov_base, time->era_entries[num].name, l),
716 '\0', l2 - l);
717 iov[2 + cnt].iov_len = l2;
718 ++cnt;
720 idx[1 + last_idx] += 8 * sizeof (int32_t) + l2;
722 assert (idx[1 + last_idx] % 4 == 0);
724 iov[2 + cnt].iov_base = (void *) time->era_entries[num].wname;
725 iov[2 + cnt].iov_len = ((wcschr ((wchar_t *) time->era_entries[num].wformat, L'\0')
726 - (wchar_t *) time->era_entries[num].wname + 1)
727 * sizeof (uint32_t));
728 idx[1 + last_idx] += iov[2 + cnt].iov_len;
729 ++cnt;
731 ++last_idx;
733 /* The wide character ab'days. */
734 for (n = 0; n < 7; ++n, ++cnt, ++last_idx)
736 iov[2 + cnt].iov_base =
737 (void *) (time->wabday[n] ?: empty_wstr);
738 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
739 * sizeof (uint32_t));
740 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
743 /* The wide character days. */
744 for (n = 0; n < 7; ++n, ++cnt, ++last_idx)
746 iov[2 + cnt].iov_base =
747 (void *) (time->wday[n] ?: empty_wstr);
748 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
749 * sizeof (uint32_t));
750 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
753 /* The wide character ab'mons. */
754 for (n = 0; n < 12; ++n, ++cnt, ++last_idx)
756 iov[2 + cnt].iov_base =
757 (void *) (time->wabmon[n] ?: empty_wstr);
758 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
759 * sizeof (uint32_t));
760 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
763 /* The wide character mons. */
764 for (n = 0; n < 12; ++n, ++cnt, ++last_idx)
766 iov[2 + cnt].iov_base =
767 (void *) (time->wmon[n] ?: empty_wstr);
768 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
769 * sizeof (uint32_t));
770 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
773 /* Wide character AM/PM. */
774 for (n = 0; n < 2; ++n, ++cnt, ++last_idx)
776 iov[2 + cnt].iov_base =
777 (void *) (time->wam_pm[n] ?: 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;
783 iov[2 + cnt].iov_base = (void *) (time->wd_t_fmt ?: empty_wstr);
784 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
785 * sizeof (uint32_t));
786 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
787 ++cnt;
788 ++last_idx;
790 iov[2 + cnt].iov_base = (void *) (time->wd_fmt ?: empty_wstr);
791 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
792 * sizeof (uint32_t));
793 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
794 ++cnt;
795 ++last_idx;
797 iov[2 + cnt].iov_base = (void *) (time->wt_fmt ?: empty_wstr);
798 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
799 * sizeof (uint32_t));
800 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
801 ++cnt;
802 ++last_idx;
804 iov[2 + cnt].iov_base = (void *) (time->wt_fmt_ampm ?: empty_wstr);
805 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
806 * sizeof (uint32_t));
807 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
808 ++cnt;
809 ++last_idx;
811 iov[2 + cnt].iov_base = (void *) (time->wera_year ?: empty_wstr);
812 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
813 * sizeof (uint32_t));
814 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
815 ++cnt;
816 ++last_idx;
818 iov[2 + cnt].iov_base = (void *) (time->wera_d_fmt ?: empty_wstr);
819 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
820 * sizeof (uint32_t));
821 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
822 ++cnt;
823 ++last_idx;
825 idx[1 + last_idx] = idx[last_idx];
826 for (num = 0; num < 100; ++num, ++cnt)
828 iov[2 + cnt].iov_base = (void *) (time->walt_digits[num]
829 ?: empty_wstr);
830 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
831 * sizeof (uint32_t));
832 idx[1 + last_idx] += iov[2 + cnt].iov_len;
834 ++last_idx;
836 iov[2 + cnt].iov_base = (void *) (time->wera_d_t_fmt ?: empty_wstr);
837 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
838 * sizeof (uint32_t));
839 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
840 ++cnt;
841 ++last_idx;
843 iov[2 + cnt].iov_base = (void *) (time->wera_t_fmt ?: empty_wstr);
844 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
845 * sizeof (uint32_t));
846 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
847 ++cnt;
848 ++last_idx;
850 iov[2 + cnt].iov_base = (void *) &time->week_ndays;
851 iov[2 + cnt].iov_len = 1;
852 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
853 ++cnt;
854 ++last_idx;
856 /* We must align the following data. */
857 iov[2 + cnt].iov_base = (void *) "\0\0";
858 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
859 idx[last_idx] = (idx[last_idx] + 3) & ~3;
860 ++cnt;
862 iov[2 + cnt].iov_base = (void *) &time->week_1stday;
863 iov[2 + cnt].iov_len = sizeof(uint32_t);
864 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
865 ++cnt;
866 ++last_idx;
868 iov[2 + cnt].iov_base = (void *) &time->week_1stweek;
869 iov[2 + cnt].iov_len = 1;
870 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
871 ++cnt;
872 ++last_idx;
874 iov[2 + cnt].iov_base = (void *) &time->first_weekday;
875 iov[2 + cnt].iov_len = 1;
876 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
877 ++cnt;
878 ++last_idx;
880 iov[2 + cnt].iov_base = (void *) &time->first_workday;
881 iov[2 + cnt].iov_len = 1;
882 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
883 ++cnt;
884 ++last_idx;
886 iov[2 + cnt].iov_base = (void *) &time->cal_direction;
887 iov[2 + cnt].iov_len = 1;
888 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
889 ++cnt;
890 ++last_idx;
892 iov[2 + cnt].iov_base = (void *) time->timezone;
893 iov[2 + cnt].iov_len = strlen (time->timezone) + 1;
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 *) time->date_fmt;
899 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
900 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
901 ++cnt;
902 ++last_idx;
904 /* We must align the following data. */
905 iov[2 + cnt].iov_base = (void *) "\0\0";
906 iov[2 + cnt].iov_len = -idx[last_idx] & 3;
907 idx[last_idx] += -idx[last_idx] & 3;
908 ++cnt;
910 iov[2 + cnt].iov_base = (void *) time->wdate_fmt;
911 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
912 * sizeof (uint32_t));
913 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
914 ++cnt;
915 ++last_idx;
917 iov[2 + cnt].iov_base = (void *) charmap->code_set_name;
918 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
919 ++cnt;
920 ++last_idx;
922 assert (cnt == (_NL_ITEM_INDEX (_NL_NUM_LC_TIME)
923 + time->num_era - 1
924 + 2 * 99
925 + 2 + time->num_era * 10));
926 assert (last_idx == _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
928 write_locale_data (output_path, LC_TIME, "LC_TIME", 2 + cnt, iov);
932 /* The parser for the LC_TIME section of the locale definition. */
933 void
934 time_read (struct linereader *ldfile, struct localedef_t *result,
935 const struct charmap_t *charmap, const char *repertoire_name,
936 int ignore_content)
938 struct repertoire_t *repertoire = NULL;
939 struct locale_time_t *time;
940 struct token *now;
941 enum token_t nowtok;
942 size_t cnt;
944 /* Get the repertoire we have to use. */
945 if (repertoire_name != NULL)
946 repertoire = repertoire_read (repertoire_name);
948 /* The rest of the line containing `LC_TIME' must be free. */
949 lr_ignore_rest (ldfile, 1);
954 now = lr_token (ldfile, charmap, result, repertoire, verbose);
955 nowtok = now->tok;
957 while (nowtok == tok_eol);
959 /* If we see `copy' now we are almost done. */
960 if (nowtok == tok_copy)
962 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
963 LC_TIME, "LC_TIME", ignore_content);
964 return;
967 /* Prepare the data structures. */
968 time_startup (ldfile, result, ignore_content);
969 time = result->categories[LC_TIME].time;
971 while (1)
973 /* Of course we don't proceed beyond the end of file. */
974 if (nowtok == tok_eof)
975 break;
977 /* Ingore empty lines. */
978 if (nowtok == tok_eol)
980 now = lr_token (ldfile, charmap, result, repertoire, verbose);
981 nowtok = now->tok;
982 continue;
985 switch (nowtok)
987 #define STRARR_ELEM(cat, min, max) \
988 case tok_##cat: \
989 /* Ignore the rest of the line if we don't need the input of \
990 this line. */ \
991 if (ignore_content) \
993 lr_ignore_rest (ldfile, 0); \
994 break; \
997 for (cnt = 0; cnt < max; ++cnt) \
999 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1000 if (now->tok == tok_eol) \
1002 if (cnt < min) \
1003 lr_error (ldfile, _("%s: too few values for field `%s'"), \
1004 "LC_TIME", #cat); \
1005 if (!ignore_content) \
1006 do \
1008 time->cat[cnt] = ""; \
1009 time->w##cat[cnt] = empty_wstr; \
1011 while (++cnt < max); \
1012 break; \
1014 else if (now->tok != tok_string) \
1015 goto err_label; \
1016 else if (!ignore_content && (now->val.str.startmb == NULL \
1017 || now->val.str.startwc == NULL)) \
1019 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
1020 "LC_TIME", #cat); \
1021 time->cat[cnt] = ""; \
1022 time->w##cat[cnt] = empty_wstr; \
1024 else if (!ignore_content) \
1026 time->cat[cnt] = now->val.str.startmb; \
1027 time->w##cat[cnt] = now->val.str.startwc; \
1030 /* Match the semicolon. */ \
1031 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1032 if (now->tok != tok_semicolon && now->tok != tok_eol) \
1033 break; \
1035 if (now->tok != tok_eol) \
1037 while (!ignore_content && cnt < min) \
1039 time->cat[cnt] = ""; \
1040 time->w##cat[cnt++] = empty_wstr; \
1043 if (now->tok == tok_semicolon) \
1045 now = lr_token (ldfile, charmap, result, repertoire, \
1046 verbose); \
1047 if (now->tok == tok_eol) \
1048 lr_error (ldfile, _("extra trailing semicolon")); \
1049 else if (now->tok == tok_string) \
1051 lr_error (ldfile, _("\
1052 %s: too many values for field `%s'"), \
1053 "LC_TIME", #cat); \
1054 lr_ignore_rest (ldfile, 0); \
1056 else \
1057 goto err_label; \
1059 else \
1060 goto err_label; \
1062 time->cat##_defined = 1; \
1063 break
1065 STRARR_ELEM (abday, 7, 7);
1066 STRARR_ELEM (day, 7, 7);
1067 STRARR_ELEM (abmon, 12, 12);
1068 STRARR_ELEM (mon, 12, 12);
1069 STRARR_ELEM (am_pm, 2, 2);
1070 STRARR_ELEM (alt_digits, 0, 100);
1072 case tok_era:
1073 /* Ignore the rest of the line if we don't need the input of
1074 this line. */
1075 if (ignore_content)
1077 lr_ignore_rest (ldfile, 0);
1078 break;
1082 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1083 if (now->tok != tok_string)
1084 goto err_label;
1085 if (!ignore_content && (now->val.str.startmb == NULL
1086 || now->val.str.startwc == NULL))
1088 lr_error (ldfile, _("%s: unknown character in field `%s'"),
1089 "LC_TIME", "era");
1090 lr_ignore_rest (ldfile, 0);
1091 break;
1093 if (!ignore_content)
1095 time->era = xrealloc (time->era,
1096 (time->num_era + 1) * sizeof (char *));
1097 time->era[time->num_era] = now->val.str.startmb;
1099 time->wera = xrealloc (time->wera,
1100 (time->num_era + 1)
1101 * sizeof (char *));
1102 time->wera[time->num_era++] = now->val.str.startwc;
1104 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1105 if (now->tok != tok_eol && now->tok != tok_semicolon)
1106 goto err_label;
1108 while (now->tok == tok_semicolon);
1109 break;
1111 #define STR_ELEM(cat) \
1112 case tok_##cat: \
1113 /* Ignore the rest of the line if we don't need the input of \
1114 this line. */ \
1115 if (ignore_content) \
1117 lr_ignore_rest (ldfile, 0); \
1118 break; \
1121 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1122 if (now->tok != tok_string) \
1123 goto err_label; \
1124 else if (time->cat != NULL) \
1125 lr_error (ldfile, _("\
1126 %s: field `%s' declared more than once"), "LC_TIME", #cat); \
1127 else if (!ignore_content && (now->val.str.startmb == NULL \
1128 || now->val.str.startwc == NULL)) \
1130 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
1131 "LC_TIME", #cat); \
1132 time->cat = ""; \
1133 time->w##cat = empty_wstr; \
1135 else if (!ignore_content) \
1137 time->cat = now->val.str.startmb; \
1138 time->w##cat = now->val.str.startwc; \
1140 break
1142 STR_ELEM (d_t_fmt);
1143 STR_ELEM (d_fmt);
1144 STR_ELEM (t_fmt);
1145 STR_ELEM (t_fmt_ampm);
1146 STR_ELEM (era_year);
1147 STR_ELEM (era_d_t_fmt);
1148 STR_ELEM (era_d_fmt);
1149 STR_ELEM (era_t_fmt);
1150 STR_ELEM (timezone);
1151 STR_ELEM (date_fmt);
1153 #define INT_ELEM(cat) \
1154 case tok_##cat: \
1155 /* Ignore the rest of the line if we don't need the input of \
1156 this line. */ \
1157 if (ignore_content) \
1159 lr_ignore_rest (ldfile, 0); \
1160 break; \
1163 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1164 if (now->tok != tok_number) \
1165 goto err_label; \
1166 else if (time->cat != 0) \
1167 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
1168 "LC_TIME", #cat); \
1169 else if (!ignore_content) \
1170 time->cat = now->val.num; \
1171 break
1173 INT_ELEM (first_weekday);
1174 INT_ELEM (first_workday);
1175 INT_ELEM (cal_direction);
1177 case tok_week:
1178 /* Ignore the rest of the line if we don't need the input of
1179 this line. */
1180 if (ignore_content)
1182 lr_ignore_rest (ldfile, 0);
1183 break;
1186 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1187 if (now->tok != tok_number)
1188 goto err_label;
1189 time->week_ndays = now->val.num;
1191 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1192 if (now->tok != tok_semicolon)
1193 goto err_label;
1195 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1196 if (now->tok != tok_number)
1197 goto err_label;
1198 time->week_1stday = now->val.num;
1200 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1201 if (now->tok != tok_semicolon)
1202 goto err_label;
1204 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1205 if (now->tok != tok_number)
1206 goto err_label;
1207 time->week_1stweek = now->val.num;
1209 lr_ignore_rest (ldfile, 1);
1210 break;
1212 case tok_end:
1213 /* Next we assume `LC_TIME'. */
1214 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1215 if (now->tok == tok_eof)
1216 break;
1217 if (now->tok == tok_eol)
1218 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TIME");
1219 else if (now->tok != tok_lc_time)
1220 lr_error (ldfile, _("\
1221 %1$s: definition does not end with `END %1$s'"), "LC_TIME");
1222 lr_ignore_rest (ldfile, now->tok == tok_lc_time);
1223 return;
1225 default:
1226 err_label:
1227 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
1230 /* Prepare for the next round. */
1231 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1232 nowtok = now->tok;
1235 /* When we come here we reached the end of the file. */
1236 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");