]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-time.c
Update.
[thirdparty/glibc.git] / locale / programs / ld-time.c
1 /* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <langinfo.h>
25 #include <string.h>
26
27 /* Undefine following line in production version. */
28 /* #define NDEBUG 1 */
29 #include <assert.h>
30 #include <stdlib.h>
31
32 #include "locales.h"
33 #include "localeinfo.h"
34 #include "stringtrans.h"
35
36 #define SWAPU32(w) \
37 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
38
39
40 extern void *xmalloc (size_t __n);
41 extern void *xrealloc (void *__p, size_t __n);
42
43
44 /* Entry describing an entry of the era specification. */
45 struct era_data
46 {
47 int32_t direction;
48 int32_t offset;
49 int32_t start_date[3];
50 int32_t stop_date[3];
51 const char *name;
52 const char *format;
53 };
54
55
56 /* The real definition of the struct for the LC_TIME locale. */
57 struct locale_time_t
58 {
59 const char *abday[7];
60 size_t cur_num_abday;
61 const char *day[7];
62 size_t cur_num_day;
63 const char *abmon[12];
64 size_t cur_num_abmon;
65 const char *mon[12];
66 size_t cur_num_mon;
67 const char *am_pm[2];
68 size_t cur_num_am_pm;
69 const char *d_t_fmt;
70 const char *d_fmt;
71 const char *t_fmt;
72 const char *t_fmt_ampm;
73 const char **era;
74 u_int32_t cur_num_era;
75 const char *era_year;
76 const char *era_d_t_fmt;
77 const char *era_t_fmt;
78 const char *era_d_fmt;
79 const char *alt_digits[100];
80 u_int32_t cur_num_alt_digits;
81
82 struct era_data *era_entries;
83 struct era_data *era_entries_ob;
84 };
85
86
87 void
88 time_startup (struct linereader *lr, struct localedef_t *locale,
89 struct charset_t *charset)
90 {
91 struct locale_time_t *time;
92
93 /* We have a definition for LC_TIME. */
94 copy_posix.mask &= ~(1 << LC_TIME);
95
96 /* It is important that we always use UCS1 encoding for strings now. */
97 encoding_method = ENC_UCS1;
98
99 locale->categories[LC_TIME].time = time =
100 (struct locale_time_t *) xmalloc (sizeof (struct locale_time_t));
101
102 memset (time, '\0', sizeof (struct locale_time_t));
103 }
104
105
106 void
107 time_finish (struct localedef_t *locale)
108 {
109 struct locale_time_t *time = locale->categories[LC_TIME].time;
110
111 #define TESTARR_ELEM(cat, max) \
112 if (time->cur_num_##cat == 0 && !be_quiet) \
113 error (0, 0, _("field `%s' in category `%s' not defined"), \
114 #cat, "LC_TIME"); \
115 else if (time->cur_num_##cat != max && !be_quiet) \
116 error (0, 0, _("field `%s' in category `%s' has not enough values"), \
117 #cat, "LC_TIME")
118
119 TESTARR_ELEM (abday, 7);
120 TESTARR_ELEM (day, 7);
121 TESTARR_ELEM (abmon, 12);
122 TESTARR_ELEM (mon, 12);
123 TESTARR_ELEM (am_pm, 2);
124
125 #define TEST_ELEM(cat) \
126 if (time->cat == NULL && !be_quiet) \
127 error (0, 0, _("field `%s' in category `%s' not defined"), \
128 #cat, "LC_TIME")
129
130 TEST_ELEM (d_t_fmt);
131 TEST_ELEM (d_fmt);
132 TEST_ELEM (t_fmt);
133
134 /* According to C.Y.Alexis Cheng <alexis@vnet.ibm.com> the T_FMT_AMPM
135 field is optional. */
136 if (time->t_fmt_ampm == NULL)
137 /* Use the 24h format as default. */
138 time->t_fmt_ampm = time->t_fmt;
139
140 /* Now process the era entries. */
141 if (time->cur_num_era != 0)
142 {
143 const int days_per_month[12] = { 31, 29, 31, 30, 31, 30,
144 31, 31, 30, 31 ,30, 31 };
145 size_t idx;
146
147 time->era_entries =
148 (struct era_data *) xmalloc (time->cur_num_era
149 * sizeof (struct era_data));
150
151 for (idx = 0; idx < time->cur_num_era; ++idx)
152 {
153 size_t era_len = strlen (time->era[idx]);
154 char *str = xmalloc ((era_len + 1 + 3) & ~3);
155 char *endp;
156
157 memcpy (str, time->era[idx], era_len + 1);
158
159 /* First character must be + or - for the direction. */
160 if (*str != '+' && *str != '-')
161 {
162 if (!be_quiet)
163 error (0, 0, _("direction flag in string %d in `era' field"
164 " in category `%s' is not '+' nor '-'"),
165 idx + 1, "LC_TIME");
166 /* Default arbitrarily to '+'. */
167 time->era_entries[idx].direction = '+';
168 }
169 else
170 time->era_entries[idx].direction = *str;
171 if (*++str != ':')
172 {
173 if (!be_quiet)
174 error (0, 0, _("direction flag in string %d in `era' field"
175 " in category `%s' is not a single character"),
176 idx + 1, "LC_TIME");
177 (void) strsep (&str, ":");
178 }
179 else
180 ++str;
181
182 /* Now the offset year. */
183 time->era_entries[idx].offset = strtol (str, &endp, 10);
184 if (endp == str)
185 {
186 if (!be_quiet)
187 error (0, 0, _("illegal number for offset in string %d in"
188 " `era' field in category `%s'"),
189 idx + 1, "LC_TIME");
190 (void) strsep (&str, ":");
191 }
192 else if (*endp != ':')
193 {
194 if (!be_quiet)
195 error (0, 0, _("garbage at end of offset value in string %d in"
196 " `era' field in category `%s'"),
197 idx + 1, "LC_TIME");
198 (void) strsep (&str, ":");
199 }
200 else
201 str = endp + 1;
202
203 /* Next is the starting date in ISO format. */
204 if (strncmp (str, "-*", 2) == 0)
205 {
206 time->era_entries[idx].start_date[0] =
207 time->era_entries[idx].start_date[1] =
208 time->era_entries[idx].start_date[2] = 0x80000000;
209 if (str[2] != ':')
210 goto garbage_start_date;
211 str += 3;
212 }
213 else if (strncmp (str, "+*", 2) == 0)
214 {
215 time->era_entries[idx].start_date[0] =
216 time->era_entries[idx].start_date[1] =
217 time->era_entries[idx].start_date[2] = 0x7fffffff;
218 if (str[2] != ':')
219 goto garbage_start_date;
220 str += 3;
221 }
222 else
223 {
224 time->era_entries[idx].start_date[0] = strtol (str, &endp, 10);
225 if (endp == str || *endp != '/')
226 goto invalid_start_date;
227 else
228 str = endp + 1;
229 time->era_entries[idx].start_date[0] -= 1900;
230
231 time->era_entries[idx].start_date[1] = strtol (str, &endp, 10);
232 if (endp == str || *endp != '/')
233 goto invalid_start_date;
234 else
235 str = endp + 1;
236 time->era_entries[idx].start_date[1] -= 1;
237
238 time->era_entries[idx].start_date[2] = strtol (str, &endp, 10);
239 if (endp == str)
240 {
241 invalid_start_date:
242 if (!be_quiet)
243 error (0, 0, _("illegal starting date in string %d in"
244 " `era' field in category `%s'"),
245 idx + 1, "LC_TIME");
246 (void) strsep (&str, ":");
247 }
248 else if (*endp != ':')
249 {
250 garbage_start_date:
251 if (!be_quiet)
252 error (0, 0, _("garbage at end of starting date "
253 "in string %d in `era' field "
254 "in category `%s'"),
255 idx + 1, "LC_TIME");
256 (void) strsep (&str, ":");
257 }
258 else
259 {
260 str = endp + 1;
261
262 /* Check for valid value. */
263 if ((time->era_entries[idx].start_date[1] < 0
264 || time->era_entries[idx].start_date[1] >= 12
265 || time->era_entries[idx].start_date[2] < 0
266 || (time->era_entries[idx].start_date[2]
267 > days_per_month[time->era_entries[idx].start_date[1]])
268 || (time->era_entries[idx].start_date[1] == 2
269 && time->era_entries[idx].start_date[2] == 29
270 && !__isleap (time->era_entries[idx].start_date[0])))
271 && !be_quiet)
272 error (0, 0, _("starting date is illegal in"
273 " string %d in `era' field in"
274 " category `%s'"),
275 idx + 1, "LC_TIME");
276 }
277 }
278
279 /* Next is the stopping date in ISO format. */
280 if (strncmp (str, "-*", 2) == 0)
281 {
282 time->era_entries[idx].stop_date[0] =
283 time->era_entries[idx].stop_date[1] =
284 time->era_entries[idx].stop_date[2] = 0x80000000;
285 if (str[2] != ':')
286 goto garbage_stop_date;
287 str += 3;
288 }
289 else if (strncmp (str, "+*", 2) == 0)
290 {
291 time->era_entries[idx].stop_date[0] =
292 time->era_entries[idx].stop_date[1] =
293 time->era_entries[idx].stop_date[2] = 0x7fffffff;
294 if (str[2] != ':')
295 goto garbage_stop_date;
296 str += 3;
297 }
298 else
299 {
300 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
301 if (endp == str || *endp != '/')
302 goto invalid_stop_date;
303 else
304 str = endp + 1;
305 time->era_entries[idx].stop_date[0] -= 1900;
306
307 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
308 if (endp == str || *endp != '/')
309 goto invalid_stop_date;
310 else
311 str = endp + 1;
312 time->era_entries[idx].stop_date[1] -= 1;
313
314 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
315 if (endp == str)
316 {
317 invalid_stop_date:
318 if (!be_quiet)
319 error (0, 0, _("illegal stopping date in string %d in"
320 " `era' field in category `%s'"),
321 idx + 1, "LC_TIME");
322 (void) strsep (&str, ":");
323 }
324 else if (*endp != ':')
325 {
326 garbage_stop_date:
327 if (!be_quiet)
328 error (0, 0, _("garbage at end of stopping date "
329 "in string %d in `era' field "
330 "in category `%s'"),
331 idx + 1, "LC_TIME");
332 (void) strsep (&str, ":");
333 }
334 else
335 {
336 str = endp + 1;
337
338 /* Check for valid value. */
339 if ((time->era_entries[idx].stop_date[1] < 0
340 || time->era_entries[idx].stop_date[1] >= 12
341 || time->era_entries[idx].stop_date[2] < 0
342 || (time->era_entries[idx].stop_date[2]
343 > days_per_month[time->era_entries[idx].stop_date[1]])
344 || (time->era_entries[idx].stop_date[1] == 2
345 && time->era_entries[idx].stop_date[2] == 29
346 && !__isleap (time->era_entries[idx].stop_date[0])))
347 && !be_quiet)
348 error (0, 0, _("stopping date is illegal in"
349 " string %d in `era' field in"
350 " category `%s'"),
351 idx + 1, "LC_TIME");
352 }
353 }
354
355 if (str == NULL || *str == '\0')
356 {
357 if (!be_quiet)
358 error (0, 0, _("missing era name in string %d in `era' field"
359 " in category `%s'"), idx + 1, "LC_TIME");
360 time->era_entries[idx].name =
361 time->era_entries[idx].format = "";
362 }
363 else
364 {
365 time->era_entries[idx].name = strsep (&str, ":");
366
367 if (str == NULL || *str == '\0')
368 {
369 if (!be_quiet)
370 error (0, 0, _("missing era format in string %d in `era'"
371 " field in category `%s'"),
372 idx + 1, "LC_TIME");
373 time->era_entries[idx].name =
374 time->era_entries[idx].format = "";
375 }
376 else
377 time->era_entries[idx].format = str;
378 }
379 }
380
381 /* Construct the array for the other byte order. */
382 time->era_entries_ob =
383 (struct era_data *) xmalloc (time->cur_num_era
384 * sizeof (struct era_data));
385
386 for (idx = 0; idx < time->cur_num_era; ++idx)
387 {
388 time->era_entries_ob[idx].direction =
389 SWAPU32 (time->era_entries[idx].direction);
390 time->era_entries_ob[idx].offset =
391 SWAPU32 (time->era_entries[idx].offset);
392 time->era_entries_ob[idx].start_date[0] =
393 SWAPU32 (time->era_entries[idx].start_date[0]);
394 time->era_entries_ob[idx].start_date[1] =
395 SWAPU32 (time->era_entries[idx].start_date[1]);
396 time->era_entries_ob[idx].start_date[2] =
397 SWAPU32 (time->era_entries[idx].stop_date[2]);
398 time->era_entries_ob[idx].stop_date[0] =
399 SWAPU32 (time->era_entries[idx].stop_date[0]);
400 time->era_entries_ob[idx].stop_date[1] =
401 SWAPU32 (time->era_entries[idx].stop_date[1]);
402 time->era_entries_ob[idx].stop_date[2] =
403 SWAPU32 (time->era_entries[idx].stop_date[2]);
404 time->era_entries_ob[idx].name =
405 time->era_entries[idx].name;
406 time->era_entries_ob[idx].format =
407 time->era_entries[idx].format;
408 }
409 }
410 }
411
412
413 void
414 time_output (struct localedef_t *locale, const char *output_path)
415 {
416 struct locale_time_t *time = locale->categories[LC_TIME].time;
417 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_TIME)
418 + time->cur_num_era - 1
419 + time->cur_num_alt_digits - 1
420 + 1 + (time->cur_num_era * 9 - 1) * 2
421 + (time->cur_num_era == 0)];
422 struct locale_file data;
423 u_int32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TIME)];
424 size_t cnt, last_idx, num;
425
426 if ((locale->binary & (1 << LC_TIME)) != 0)
427 {
428 iov[0].iov_base = time;
429 iov[0].iov_len = locale->len[LC_TIME];
430
431 write_locale_data (output_path, "LC_TIME", 1, iov);
432
433 return;
434 }
435
436 data.magic = LIMAGIC (LC_TIME);
437 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TIME);
438 iov[0].iov_base = (void *) &data;
439 iov[0].iov_len = sizeof (data);
440
441 iov[1].iov_base = (void *) idx;
442 iov[1].iov_len = sizeof (idx);
443
444 idx[0] = iov[0].iov_len + iov[1].iov_len;
445
446 /* The ab'days. */
447 for (cnt = 0; cnt <= _NL_ITEM_INDEX (ABDAY_7); ++cnt)
448 {
449 iov[2 + cnt].iov_base =
450 (void *) (time->abday[cnt - _NL_ITEM_INDEX (ABDAY_1)] ?: "");
451 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
452 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
453 }
454
455 /* The days. */
456 for (; cnt <= _NL_ITEM_INDEX (DAY_7); ++cnt)
457 {
458 iov[2 + cnt].iov_base =
459 (void *) (time->day[cnt - _NL_ITEM_INDEX (DAY_1)] ?: "");
460 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
461 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
462 }
463
464 /* The ab'mons. */
465 for (; cnt <= _NL_ITEM_INDEX (ABMON_12); ++cnt)
466 {
467 iov[2 + cnt].iov_base =
468 (void *) (time->abmon[cnt - _NL_ITEM_INDEX (ABMON_1)] ?: "");
469 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
470 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
471 }
472
473 /* The mons. */
474 for (; cnt <= _NL_ITEM_INDEX (MON_12); ++cnt)
475 {
476 iov[2 + cnt].iov_base =
477 (void *) (time->mon[cnt - _NL_ITEM_INDEX (MON_1)] ?: "");
478 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
479 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
480 }
481
482 /* AM/PM. */
483 for (; cnt <= _NL_ITEM_INDEX (PM_STR); ++cnt)
484 {
485 iov[2 + cnt].iov_base =
486 (void *) (time->am_pm[cnt - _NL_ITEM_INDEX (AM_STR)] ?: "");
487 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
488 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
489 }
490
491 iov[2 + cnt].iov_base = (void *) (time->d_t_fmt ?: "");
492 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
493 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
494 ++cnt;
495
496 iov[2 + cnt].iov_base = (void *) (time->d_fmt ?: "");
497 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
498 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
499 ++cnt;
500
501 iov[2 + cnt].iov_base = (void *) (time->t_fmt ?: "");
502 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
503 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
504 ++cnt;
505
506 iov[2 + cnt].iov_base = (void *) (time->t_fmt_ampm ?: "");
507 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
508 idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
509 last_idx = ++cnt;
510
511 idx[1 + last_idx] = idx[last_idx];
512 for (num = 0; num < time->cur_num_era; ++num, ++cnt)
513 {
514 iov[2 + cnt].iov_base = (void *) time->era[num];
515 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
516 idx[1 + last_idx] += iov[2 + cnt].iov_len;
517 }
518 ++last_idx;
519
520 iov[2 + cnt].iov_base = (void *) (time->era_year ?: "");
521 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
522 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
523 ++cnt;
524 ++last_idx;
525
526 iov[2 + cnt].iov_base = (void *) (time->era_d_fmt ?: "");
527 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
528 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
529 ++cnt;
530 ++last_idx;
531
532 idx[1 + last_idx] = idx[last_idx];
533 for (num = 0; num < time->cur_num_alt_digits; ++num, ++cnt)
534 {
535 iov[2 + cnt].iov_base = (void *) (time->alt_digits[num] ?: "");
536 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
537 idx[1 + last_idx] += iov[2 + cnt].iov_len;
538 }
539 ++last_idx;
540
541 iov[2 + cnt].iov_base = (void *) (time->era_d_t_fmt ?: "");
542 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
543 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
544 ++cnt;
545 ++last_idx;
546
547 iov[2 + cnt].iov_base = (void *) (time->era_t_fmt ?: "");
548 iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
549 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
550 ++cnt;
551 ++last_idx;
552
553
554 /* We must align the following data. */
555 iov[2 + cnt].iov_base = (void *) "\0\0";
556 iov[2 + cnt].iov_len = ((idx[last_idx] + 3) & ~3) - idx[last_idx];
557 idx[last_idx] = (idx[last_idx] + 3) & ~3;
558 ++cnt;
559
560 iov[2 + cnt].iov_base = (void *) &time->cur_num_alt_digits;
561 iov[2 + cnt].iov_len = sizeof (u_int32_t);
562 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
563 ++cnt;
564 ++last_idx;
565
566 /* The `era' data in usable form. */
567 iov[2 + cnt].iov_base = (void *) &time->cur_num_era;
568 iov[2 + cnt].iov_len = sizeof (u_int32_t);
569 idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
570 ++cnt;
571 ++last_idx;
572
573 #if __BYTE_ORDER == __LITTLE_ENDIAN
574 # define ERA_B1 time->era_entries_ob
575 # define ERA_B2 time->era_entries
576 #else
577 # define ERA_B1 time->era_entries
578 # define ERA_B2 time->era_entries_ob
579 #endif
580 idx[1 + last_idx] = idx[last_idx];
581 for (num = 0; num < time->cur_num_era; ++num)
582 {
583 size_t l;
584
585 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].direction;
586 iov[2 + cnt].iov_len = sizeof (int32_t);
587 ++cnt;
588 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].offset;
589 iov[2 + cnt].iov_len = sizeof (int32_t);
590 ++cnt;
591 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].start_date[0];
592 iov[2 + cnt].iov_len = sizeof (int32_t);
593 ++cnt;
594 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].start_date[1];
595 iov[2 + cnt].iov_len = sizeof (int32_t);
596 ++cnt;
597 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].start_date[2];
598 iov[2 + cnt].iov_len = sizeof (int32_t);
599 ++cnt;
600 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].stop_date[0];
601 iov[2 + cnt].iov_len = sizeof (int32_t);
602 ++cnt;
603 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].stop_date[1];
604 iov[2 + cnt].iov_len = sizeof (int32_t);
605 ++cnt;
606 iov[2 + cnt].iov_base = (void *) &ERA_B1[num].stop_date[2];
607 iov[2 + cnt].iov_len = sizeof (int32_t);
608 ++cnt;
609
610 l = (strchr (ERA_B1[num].format, '\0') - ERA_B1[num].name) + 1;
611 l = (l + 3) & ~3;
612 iov[2 + cnt].iov_base = (void *) ERA_B1[num].name;
613 iov[2 + cnt].iov_len = l;
614 ++cnt;
615
616 idx[1 + last_idx] += 8 * sizeof (int32_t) + l;
617
618 assert (idx[1 + last_idx] % 4 == 0);
619 }
620 ++last_idx;
621
622 /* idx[1 + last_idx] = idx[last_idx]; */
623 for (num = 0; num < time->cur_num_era; ++num)
624 {
625 size_t l;
626
627 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].direction;
628 iov[2 + cnt].iov_len = sizeof (int32_t);
629 ++cnt;
630 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].offset;
631 iov[2 + cnt].iov_len = sizeof (int32_t);
632 ++cnt;
633 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].start_date[0];
634 iov[2 + cnt].iov_len = sizeof (int32_t);
635 ++cnt;
636 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].start_date[1];
637 iov[2 + cnt].iov_len = sizeof (int32_t);
638 ++cnt;
639 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].start_date[2];
640 iov[2 + cnt].iov_len = sizeof (int32_t);
641 ++cnt;
642 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].stop_date[0];
643 iov[2 + cnt].iov_len = sizeof (int32_t);
644 ++cnt;
645 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].stop_date[1];
646 iov[2 + cnt].iov_len = sizeof (int32_t);
647 ++cnt;
648 iov[2 + cnt].iov_base = (void *) &ERA_B2[num].stop_date[2];
649 iov[2 + cnt].iov_len = sizeof (int32_t);
650 ++cnt;
651
652 l = (strchr (ERA_B2[num].format, '\0') - ERA_B2[num].name) + 1;
653 l = (l + 3) & ~3;
654 iov[2 + cnt].iov_base = (void *) ERA_B2[num].name;
655 iov[2 + cnt].iov_len = l;
656 ++cnt;
657
658 /* idx[1 + last_idx] += 8 * sizeof (int32_t) + l; */
659 }
660
661 /* We have a problem when no era data is present. In this case the
662 data pointer for _NL_TIME_ERA_ENTRIES_EB and
663 _NL_TIME_ERA_ENTRIES_EL point after the end of the file. So we
664 introduce some dummy data here. */
665 if (time->cur_num_era == 0)
666 {
667 static u_int32_t dummy = 0;
668 iov[2 + cnt].iov_base = (void *) &dummy;
669 iov[2 + cnt].iov_len = 4;
670 ++cnt;
671 }
672
673 assert (cnt == (_NL_ITEM_INDEX (_NL_NUM_LC_TIME)
674 + time->cur_num_era - 1
675 + time->cur_num_alt_digits - 1
676 + 1 + (time->cur_num_era * 9 - 1) * 2
677 + (time->cur_num_era == 0))
678 && last_idx + 1 == _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
679
680 write_locale_data (output_path, "LC_TIME", 2 + cnt, iov);
681 }
682
683
684 void
685 time_add (struct linereader *lr, struct localedef_t *locale,
686 enum token_t tok, struct token *code,
687 struct charset_t *charset)
688 {
689 struct locale_time_t *time = locale->categories[LC_TIME].time;
690
691 switch (tok)
692 {
693 #define STRARR_ELEM(cat, max) \
694 case tok_##cat: \
695 if (time->cur_num_##cat >= max) \
696 lr_error (lr, _("\
697 too many values for field `%s' in category `%s'"), \
698 #cat, "LC_TIME"); \
699 else if (code->val.str.start == NULL) \
700 { \
701 lr_error (lr, _("unknown character in field `%s' of category `%s'"),\
702 #cat, "LC_TIME"); \
703 time->cat[time->cur_num_##cat++] = ""; \
704 } \
705 else \
706 time->cat[time->cur_num_##cat++] = code->val.str.start; \
707 break
708
709 STRARR_ELEM (abday, 7);
710 STRARR_ELEM (day, 7);
711 STRARR_ELEM (abmon, 12);
712 STRARR_ELEM (mon, 12);
713 STRARR_ELEM (am_pm, 2);
714 STRARR_ELEM (alt_digits, 100);
715
716 case tok_era:
717 if (code->val.str.start == NULL)
718 lr_error (lr, _("unknown character in field `%s' of category `%s'"),
719 "era", "LC_TIME");
720 else
721 {
722 ++time->cur_num_era;
723 time->era = xrealloc (time->era,
724 time->cur_num_era * sizeof (char *));
725 time->era[time->cur_num_era - 1] = code->val.str.start;
726 }
727 break;
728
729 #define STR_ELEM(cat) \
730 case tok_##cat: \
731 if (time->cat != NULL) \
732 lr_error (lr, _("\
733 field `%s' in category `%s' declared more than once"), \
734 #cat, "LC_TIME"); \
735 else if (code->val.str.start == NULL) \
736 { \
737 lr_error (lr, _("unknown character in field `%s' of category `%s'"),\
738 #cat, "LC_TIME"); \
739 time->cat = ""; \
740 } \
741 else \
742 time->cat = code->val.str.start; \
743 break
744
745 STR_ELEM (d_t_fmt);
746 STR_ELEM (d_fmt);
747 STR_ELEM (t_fmt);
748 STR_ELEM (t_fmt_ampm);
749 STR_ELEM (era_year);
750 STR_ELEM (era_d_t_fmt);
751 STR_ELEM (era_d_fmt);
752 STR_ELEM (era_t_fmt);
753
754 default:
755 assert (! "unknown token in category `LC_TIME': should not happen");
756 }
757 }