(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / locale / programs / ld-time.c
bloba7dd25c80db358e92d751b281d9f613debbaac8b
1 /* Copyright (C) 1995-2002, 2003, 2004 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 unsigned 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 = alloca (sizeof *iov
532 * (2 + _NL_ITEM_INDEX (_NL_NUM_LC_TIME)
533 + time->num_era - 1
534 + 2 * 99
535 + 2 + time->num_era * 10 - 1));
536 struct locale_file data;
537 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TIME)];
538 size_t cnt, last_idx, num, n;
540 data.magic = LIMAGIC (LC_TIME);
541 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TIME);
542 iov[0].iov_base = (void *) &data;
543 iov[0].iov_len = sizeof (data);
545 iov[1].iov_base = (void *) idx;
546 iov[1].iov_len = sizeof (idx);
548 idx[0] = iov[0].iov_len + iov[1].iov_len;
550 /* The ab'days. */
551 for (cnt = 0; cnt <= _NL_ITEM_INDEX (ABDAY_7); ++cnt)
553 iov[2 + cnt].iov_base =
554 (void *) (time->abday[cnt - _NL_ITEM_INDEX (ABDAY_1)] ?: "");
555 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
556 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
559 /* The days. */
560 for (; cnt <= _NL_ITEM_INDEX (DAY_7); ++cnt)
562 iov[2 + cnt].iov_base =
563 (void *) (time->day[cnt - _NL_ITEM_INDEX (DAY_1)] ?: "");
564 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
565 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
568 /* The ab'mons. */
569 for (; cnt <= _NL_ITEM_INDEX (ABMON_12); ++cnt)
571 iov[2 + cnt].iov_base =
572 (void *) (time->abmon[cnt - _NL_ITEM_INDEX (ABMON_1)] ?: "");
573 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
574 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
577 /* The mons. */
578 for (; cnt <= _NL_ITEM_INDEX (MON_12); ++cnt)
580 iov[2 + cnt].iov_base =
581 (void *) (time->mon[cnt - _NL_ITEM_INDEX (MON_1)] ?: "");
582 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
583 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
586 /* AM/PM. */
587 for (; cnt <= _NL_ITEM_INDEX (PM_STR); ++cnt)
589 iov[2 + cnt].iov_base =
590 (void *) (time->am_pm[cnt - _NL_ITEM_INDEX (AM_STR)] ?: "");
591 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
592 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
595 iov[2 + cnt].iov_base = (void *) (time->d_t_fmt ?: "");
596 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
597 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
598 ++cnt;
600 iov[2 + cnt].iov_base = (void *) (time->d_fmt ?: "");
601 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
602 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
603 ++cnt;
605 iov[2 + cnt].iov_base = (void *) (time->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->t_fmt_ampm ?: "");
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 last_idx = ++cnt;
615 idx[1 + last_idx] = idx[last_idx];
616 for (num = 0; num < time->num_era; ++num, ++cnt)
618 iov[2 + cnt].iov_base = (void *) time->era[num];
619 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
620 idx[1 + last_idx] += iov[2 + cnt].iov_len;
622 ++last_idx;
624 iov[2 + cnt].iov_base = (void *) (time->era_year ?: "");
625 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
626 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
627 ++cnt;
628 ++last_idx;
630 iov[2 + cnt].iov_base = (void *) (time->era_d_fmt ?: "");
631 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
632 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
633 ++cnt;
634 ++last_idx;
636 idx[1 + last_idx] = idx[last_idx];
637 for (num = 0; num < 100; ++num, ++cnt)
639 iov[2 + cnt].iov_base = (void *) (time->alt_digits[num] ?: "");
640 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
641 idx[1 + last_idx] += iov[2 + cnt].iov_len;
643 ++last_idx;
645 iov[2 + cnt].iov_base = (void *) (time->era_d_t_fmt ?: "");
646 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
647 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
648 ++cnt;
649 ++last_idx;
651 iov[2 + cnt].iov_base = (void *) (time->era_t_fmt ?: "");
652 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
653 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
654 ++cnt;
655 ++last_idx;
658 /* We must align the following data. */
659 iov[2 + cnt].iov_base = (void *) "\0\0";
660 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
661 idx[last_idx] = (idx[last_idx] + 3) & ~3;
662 ++cnt;
664 /* The `era' data in usable form. */
665 iov[2 + cnt].iov_base = (void *) &time->num_era;
666 iov[2 + cnt].iov_len = sizeof (uint32_t);
667 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
668 ++cnt;
669 ++last_idx;
671 idx[1 + last_idx] = idx[last_idx];
672 for (num = 0; num < time->num_era; ++num)
674 size_t l, l2;
676 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].direction;
677 iov[2 + cnt].iov_len = sizeof (int32_t);
678 ++cnt;
679 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].offset;
680 iov[2 + cnt].iov_len = sizeof (int32_t);
681 ++cnt;
682 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[0];
683 iov[2 + cnt].iov_len = sizeof (int32_t);
684 ++cnt;
685 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[1];
686 iov[2 + cnt].iov_len = sizeof (int32_t);
687 ++cnt;
688 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].start_date[2];
689 iov[2 + cnt].iov_len = sizeof (int32_t);
690 ++cnt;
691 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[0];
692 iov[2 + cnt].iov_len = sizeof (int32_t);
693 ++cnt;
694 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[1];
695 iov[2 + cnt].iov_len = sizeof (int32_t);
696 ++cnt;
697 iov[2 + cnt].iov_base = (void *) &time->era_entries[num].stop_date[2];
698 iov[2 + cnt].iov_len = sizeof (int32_t);
699 ++cnt;
701 l = ((char *) rawmemchr (time->era_entries[num].format, '\0')
702 - time->era_entries[num].name) + 1;
703 l2 = (l + 3) & ~3;
704 iov[2 + cnt].iov_base = alloca (l2);
705 memset (mempcpy (iov[2 + cnt].iov_base, time->era_entries[num].name, l),
706 '\0', l2 - l);
707 iov[2 + cnt].iov_len = l2;
708 ++cnt;
710 idx[1 + last_idx] += 8 * sizeof (int32_t) + l2;
712 assert (idx[1 + last_idx] % 4 == 0);
714 iov[2 + cnt].iov_base = (void *) time->era_entries[num].wname;
715 iov[2 + cnt].iov_len = ((wcschr ((wchar_t *) time->era_entries[num].wformat, L'\0')
716 - (wchar_t *) time->era_entries[num].wname + 1)
717 * sizeof (uint32_t));
718 idx[1 + last_idx] += iov[2 + cnt].iov_len;
719 ++cnt;
721 ++last_idx;
723 /* The wide character ab'days. */
724 for (n = 0; n < 7; ++n, ++cnt, ++last_idx)
726 iov[2 + cnt].iov_base =
727 (void *) (time->wabday[n] ?: empty_wstr);
728 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
729 * sizeof (uint32_t));
730 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
733 /* The wide character days. */
734 for (n = 0; n < 7; ++n, ++cnt, ++last_idx)
736 iov[2 + cnt].iov_base =
737 (void *) (time->wday[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 ab'mons. */
744 for (n = 0; n < 12; ++n, ++cnt, ++last_idx)
746 iov[2 + cnt].iov_base =
747 (void *) (time->wabmon[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 mons. */
754 for (n = 0; n < 12; ++n, ++cnt, ++last_idx)
756 iov[2 + cnt].iov_base =
757 (void *) (time->wmon[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 /* Wide character AM/PM. */
764 for (n = 0; n < 2; ++n, ++cnt, ++last_idx)
766 iov[2 + cnt].iov_base =
767 (void *) (time->wam_pm[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 iov[2 + cnt].iov_base = (void *) (time->wd_t_fmt ?: empty_wstr);
774 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
775 * sizeof (uint32_t));
776 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
777 ++cnt;
778 ++last_idx;
780 iov[2 + cnt].iov_base = (void *) (time->wd_fmt ?: empty_wstr);
781 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
782 * sizeof (uint32_t));
783 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
784 ++cnt;
785 ++last_idx;
787 iov[2 + cnt].iov_base = (void *) (time->wt_fmt ?: empty_wstr);
788 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
789 * sizeof (uint32_t));
790 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
791 ++cnt;
792 ++last_idx;
794 iov[2 + cnt].iov_base = (void *) (time->wt_fmt_ampm ?: empty_wstr);
795 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
796 * sizeof (uint32_t));
797 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
798 ++cnt;
799 ++last_idx;
801 iov[2 + cnt].iov_base = (void *) (time->wera_year ?: empty_wstr);
802 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
803 * sizeof (uint32_t));
804 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
805 ++cnt;
806 ++last_idx;
808 iov[2 + cnt].iov_base = (void *) (time->wera_d_fmt ?: empty_wstr);
809 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
810 * sizeof (uint32_t));
811 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
812 ++cnt;
813 ++last_idx;
815 idx[1 + last_idx] = idx[last_idx];
816 for (num = 0; num < 100; ++num, ++cnt)
818 iov[2 + cnt].iov_base = (void *) (time->walt_digits[num]
819 ?: empty_wstr);
820 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
821 * sizeof (uint32_t));
822 idx[1 + last_idx] += iov[2 + cnt].iov_len;
824 ++last_idx;
826 iov[2 + cnt].iov_base = (void *) (time->wera_d_t_fmt ?: empty_wstr);
827 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
828 * sizeof (uint32_t));
829 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
830 ++cnt;
831 ++last_idx;
833 iov[2 + cnt].iov_base = (void *) (time->wera_t_fmt ?: empty_wstr);
834 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
835 * sizeof (uint32_t));
836 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
837 ++cnt;
838 ++last_idx;
840 iov[2 + cnt].iov_base = (void *) &time->week_ndays;
841 iov[2 + cnt].iov_len = 1;
842 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
843 ++cnt;
844 ++last_idx;
846 /* We must align the following data. */
847 iov[2 + cnt].iov_base = (void *) "\0\0";
848 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
849 idx[last_idx] = (idx[last_idx] + 3) & ~3;
850 ++cnt;
852 iov[2 + cnt].iov_base = (void *) &time->week_1stday;
853 iov[2 + cnt].iov_len = sizeof(uint32_t);
854 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
855 ++cnt;
856 ++last_idx;
858 iov[2 + cnt].iov_base = (void *) &time->week_1stweek;
859 iov[2 + cnt].iov_len = 1;
860 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
861 ++cnt;
862 ++last_idx;
864 iov[2 + cnt].iov_base = (void *) &time->first_weekday;
865 iov[2 + cnt].iov_len = 1;
866 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
867 ++cnt;
868 ++last_idx;
870 iov[2 + cnt].iov_base = (void *) &time->first_workday;
871 iov[2 + cnt].iov_len = 1;
872 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
873 ++cnt;
874 ++last_idx;
876 iov[2 + cnt].iov_base = (void *) &time->cal_direction;
877 iov[2 + cnt].iov_len = 1;
878 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
879 ++cnt;
880 ++last_idx;
882 iov[2 + cnt].iov_base = (void *) time->timezone;
883 iov[2 + cnt].iov_len = strlen (time->timezone) + 1;
884 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
885 ++cnt;
886 ++last_idx;
888 iov[2 + cnt].iov_base = (void *) time->date_fmt;
889 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
890 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
891 ++cnt;
892 ++last_idx;
894 iov[2 + cnt].iov_base = (void *) time->wdate_fmt;
895 iov[2 + cnt].iov_len = ((wcslen (iov[2 + cnt].iov_base) + 1)
896 * sizeof (uint32_t));
897 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
898 ++cnt;
899 ++last_idx;
901 iov[2 + cnt].iov_base = (void *) charmap->code_set_name;
902 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
903 ++cnt;
904 ++last_idx;
906 assert (cnt == (_NL_ITEM_INDEX (_NL_NUM_LC_TIME)
907 + time->num_era - 1
908 + 2 * 99
909 + 2 + time->num_era * 10 - 1));
910 assert (last_idx == _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
912 write_locale_data (output_path, LC_TIME, "LC_TIME", 2 + cnt, iov);
916 /* The parser for the LC_TIME section of the locale definition. */
917 void
918 time_read (struct linereader *ldfile, struct localedef_t *result,
919 const struct charmap_t *charmap, const char *repertoire_name,
920 int ignore_content)
922 struct repertoire_t *repertoire = NULL;
923 struct locale_time_t *time;
924 struct token *now;
925 enum token_t nowtok;
926 size_t cnt;
928 /* Get the repertoire we have to use. */
929 if (repertoire_name != NULL)
930 repertoire = repertoire_read (repertoire_name);
932 /* The rest of the line containing `LC_TIME' must be free. */
933 lr_ignore_rest (ldfile, 1);
938 now = lr_token (ldfile, charmap, result, repertoire, verbose);
939 nowtok = now->tok;
941 while (nowtok == tok_eol);
943 /* If we see `copy' now we are almost done. */
944 if (nowtok == tok_copy)
946 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
947 LC_TIME, "LC_TIME", ignore_content);
948 return;
951 /* Prepare the data structures. */
952 time_startup (ldfile, result, ignore_content);
953 time = result->categories[LC_TIME].time;
955 while (1)
957 /* Of course we don't proceed beyond the end of file. */
958 if (nowtok == tok_eof)
959 break;
961 /* Ingore empty lines. */
962 if (nowtok == tok_eol)
964 now = lr_token (ldfile, charmap, result, repertoire, verbose);
965 nowtok = now->tok;
966 continue;
969 switch (nowtok)
971 #define STRARR_ELEM(cat, min, max) \
972 case tok_##cat: \
973 /* Ignore the rest of the line if we don't need the input of \
974 this line. */ \
975 if (ignore_content) \
977 lr_ignore_rest (ldfile, 0); \
978 break; \
981 for (cnt = 0; cnt < max; ++cnt) \
983 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
984 if (now->tok == tok_eol) \
986 if (cnt < min) \
987 lr_error (ldfile, _("%s: too few values for field `%s'"), \
988 "LC_TIME", #cat); \
989 if (!ignore_content) \
990 do \
992 time->cat[cnt] = ""; \
993 time->w##cat[cnt] = empty_wstr; \
995 while (++cnt < max); \
996 break; \
998 else if (now->tok != tok_string) \
999 goto err_label; \
1000 else if (!ignore_content && (now->val.str.startmb == NULL \
1001 || now->val.str.startwc == NULL)) \
1003 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
1004 "LC_TIME", #cat); \
1005 time->cat[cnt] = ""; \
1006 time->w##cat[cnt] = empty_wstr; \
1008 else if (!ignore_content) \
1010 time->cat[cnt] = now->val.str.startmb; \
1011 time->w##cat[cnt] = now->val.str.startwc; \
1014 /* Match the semicolon. */ \
1015 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1016 if (now->tok != tok_semicolon && now->tok != tok_eol) \
1017 break; \
1019 if (now->tok != tok_eol) \
1021 while (!ignore_content && cnt < min) \
1023 time->cat[cnt] = ""; \
1024 time->w##cat[cnt++] = empty_wstr; \
1027 if (now->tok == tok_semicolon) \
1029 now = lr_token (ldfile, charmap, result, repertoire, \
1030 verbose); \
1031 if (now->tok == tok_eol) \
1032 lr_error (ldfile, _("extra trailing semicolon")); \
1033 else if (now->tok == tok_string) \
1035 lr_error (ldfile, _("\
1036 %s: too many values for field `%s'"), \
1037 "LC_TIME", #cat); \
1038 lr_ignore_rest (ldfile, 0); \
1040 else \
1041 goto err_label; \
1043 else \
1044 goto err_label; \
1046 time->cat##_defined = 1; \
1047 break
1049 STRARR_ELEM (abday, 7, 7);
1050 STRARR_ELEM (day, 7, 7);
1051 STRARR_ELEM (abmon, 12, 12);
1052 STRARR_ELEM (mon, 12, 12);
1053 STRARR_ELEM (am_pm, 2, 2);
1054 STRARR_ELEM (alt_digits, 0, 100);
1056 case tok_era:
1057 /* Ignore the rest of the line if we don't need the input of
1058 this line. */
1059 if (ignore_content)
1061 lr_ignore_rest (ldfile, 0);
1062 break;
1066 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1067 if (now->tok != tok_string)
1068 goto err_label;
1069 if (!ignore_content && (now->val.str.startmb == NULL
1070 || now->val.str.startwc == NULL))
1072 lr_error (ldfile, _("%s: unknown character in field `%s'"),
1073 "LC_TIME", "era");
1074 lr_ignore_rest (ldfile, 0);
1075 break;
1077 if (!ignore_content)
1079 time->era = xrealloc (time->era,
1080 (time->num_era + 1) * sizeof (char *));
1081 time->era[time->num_era] = now->val.str.startmb;
1083 time->wera = xrealloc (time->wera,
1084 (time->num_era + 1)
1085 * sizeof (char *));
1086 time->wera[time->num_era++] = now->val.str.startwc;
1088 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1089 if (now->tok != tok_eol && now->tok != tok_semicolon)
1090 goto err_label;
1092 while (now->tok == tok_semicolon);
1093 break;
1095 #define STR_ELEM(cat) \
1096 case tok_##cat: \
1097 /* Ignore the rest of the line if we don't need the input of \
1098 this line. */ \
1099 if (ignore_content) \
1101 lr_ignore_rest (ldfile, 0); \
1102 break; \
1105 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1106 if (now->tok != tok_string) \
1107 goto err_label; \
1108 else if (time->cat != NULL) \
1109 lr_error (ldfile, _("\
1110 %s: field `%s' declared more than once"), "LC_TIME", #cat); \
1111 else if (!ignore_content && (now->val.str.startmb == NULL \
1112 || now->val.str.startwc == NULL)) \
1114 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
1115 "LC_TIME", #cat); \
1116 time->cat = ""; \
1117 time->w##cat = empty_wstr; \
1119 else if (!ignore_content) \
1121 time->cat = now->val.str.startmb; \
1122 time->w##cat = now->val.str.startwc; \
1124 break
1126 STR_ELEM (d_t_fmt);
1127 STR_ELEM (d_fmt);
1128 STR_ELEM (t_fmt);
1129 STR_ELEM (t_fmt_ampm);
1130 STR_ELEM (era_year);
1131 STR_ELEM (era_d_t_fmt);
1132 STR_ELEM (era_d_fmt);
1133 STR_ELEM (era_t_fmt);
1134 STR_ELEM (timezone);
1135 STR_ELEM (date_fmt);
1137 #define INT_ELEM(cat) \
1138 case tok_##cat: \
1139 /* Ignore the rest of the line if we don't need the input of \
1140 this line. */ \
1141 if (ignore_content) \
1143 lr_ignore_rest (ldfile, 0); \
1144 break; \
1147 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
1148 if (now->tok != tok_number) \
1149 goto err_label; \
1150 else if (time->cat != 0) \
1151 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
1152 "LC_TIME", #cat); \
1153 else if (!ignore_content) \
1154 time->cat = now->val.num; \
1155 break
1157 INT_ELEM (first_weekday);
1158 INT_ELEM (first_workday);
1159 INT_ELEM (cal_direction);
1161 case tok_week:
1162 /* Ignore the rest of the line if we don't need the input of
1163 this line. */
1164 if (ignore_content)
1166 lr_ignore_rest (ldfile, 0);
1167 break;
1170 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1171 if (now->tok != tok_number)
1172 goto err_label;
1173 time->week_ndays = now->val.num;
1175 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1176 if (now->tok != tok_semicolon)
1177 goto err_label;
1179 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1180 if (now->tok != tok_number)
1181 goto err_label;
1182 time->week_1stday = now->val.num;
1184 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1185 if (now->tok != tok_semicolon)
1186 goto err_label;
1188 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1189 if (now->tok != tok_number)
1190 goto err_label;
1191 time->week_1stweek = now->val.num;
1193 lr_ignore_rest (ldfile, 1);
1194 break;
1196 case tok_end:
1197 /* Next we assume `LC_TIME'. */
1198 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1199 if (now->tok == tok_eof)
1200 break;
1201 if (now->tok == tok_eol)
1202 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TIME");
1203 else if (now->tok != tok_lc_time)
1204 lr_error (ldfile, _("\
1205 %1$s: definition does not end with `END %1$s'"), "LC_TIME");
1206 lr_ignore_rest (ldfile, now->tok == tok_lc_time);
1207 return;
1209 default:
1210 err_label:
1211 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
1214 /* Prepare for the next round. */
1215 now = lr_token (ldfile, charmap, result, repertoire, verbose);
1216 nowtok = now->tok;
1219 /* When we come here we reached the end of the file. */
1220 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");