]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-time.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / locale / programs / ld-time.c
1 /* Copyright (C) 1995-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <byteswap.h>
23 #include <langinfo.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wchar.h>
27 #include <stdint.h>
28 #include <sys/uio.h>
29
30 #include <assert.h>
31
32 #include "localedef.h"
33 #include "linereader.h"
34 #include "localeinfo.h"
35 #include "locfile.h"
36
37
38 /* Entry describing an entry of the era specification. */
39 struct era_data
40 {
41 int32_t direction;
42 int32_t offset;
43 int32_t start_date[3];
44 int32_t stop_date[3];
45 const char *name;
46 const char *format;
47 uint32_t *wname;
48 uint32_t *wformat;
49 };
50
51
52 /* The real definition of the struct for the LC_TIME locale. */
53 struct locale_time_t
54 {
55 const char *abday[7];
56 const uint32_t *wabday[7];
57 int abday_defined;
58 const char *day[7];
59 const uint32_t *wday[7];
60 int day_defined;
61 const char *abmon[12];
62 const uint32_t *wabmon[12];
63 int abmon_defined;
64 const char *mon[12];
65 const uint32_t *wmon[12];
66 int mon_defined;
67 const char *am_pm[2];
68 const uint32_t *wam_pm[2];
69 int am_pm_defined;
70 const char *d_t_fmt;
71 const uint32_t *wd_t_fmt;
72 const char *d_fmt;
73 const uint32_t *wd_fmt;
74 const char *t_fmt;
75 const uint32_t *wt_fmt;
76 const char *t_fmt_ampm;
77 const uint32_t *wt_fmt_ampm;
78 const char **era;
79 const uint32_t **wera;
80 uint32_t num_era;
81 const char *era_year;
82 const uint32_t *wera_year;
83 const char *era_d_t_fmt;
84 const uint32_t *wera_d_t_fmt;
85 const char *era_t_fmt;
86 const uint32_t *wera_t_fmt;
87 const char *era_d_fmt;
88 const uint32_t *wera_d_fmt;
89 const char *alt_digits[100];
90 const uint32_t *walt_digits[100];
91 const char *date_fmt;
92 const uint32_t *wdate_fmt;
93 int alt_digits_defined;
94 unsigned char week_ndays;
95 uint32_t week_1stday;
96 unsigned char week_1stweek;
97 unsigned char first_weekday;
98 unsigned char first_workday;
99 unsigned char cal_direction;
100 const char *timezone;
101 const uint32_t *wtimezone;
102
103 struct era_data *era_entries;
104 };
105
106
107 /* This constant is used to represent an empty wide character string. */
108 static const uint32_t empty_wstr[1] = { 0 };
109
110
111 static void
112 time_startup (struct linereader *lr, struct localedef_t *locale,
113 int ignore_content)
114 {
115 if (!ignore_content)
116 locale->categories[LC_TIME].time =
117 (struct locale_time_t *) xcalloc (1, sizeof (struct locale_time_t));
118
119 if (lr != NULL)
120 {
121 lr->translate_strings = 1;
122 lr->return_widestr = 1;
123 }
124 }
125
126
127 void
128 time_finish (struct localedef_t *locale, const struct charmap_t *charmap)
129 {
130 struct locale_time_t *time = locale->categories[LC_TIME].time;
131 int nothing = 0;
132
133 /* Now resolve copying and also handle completely missing definitions. */
134 if (time == NULL)
135 {
136 /* First see whether we were supposed to copy. If yes, find the
137 actual definition. */
138 if (locale->copy_name[LC_TIME] != NULL)
139 {
140 /* Find the copying locale. This has to happen transitively since
141 the locale we are copying from might also copying another one. */
142 struct localedef_t *from = locale;
143
144 do
145 from = find_locale (LC_TIME, from->copy_name[LC_TIME],
146 from->repertoire_name, charmap);
147 while (from->categories[LC_TIME].time == NULL
148 && from->copy_name[LC_TIME] != NULL);
149
150 time = locale->categories[LC_TIME].time
151 = from->categories[LC_TIME].time;
152 }
153
154 /* If there is still no definition issue an warning and create an
155 empty one. */
156 if (time == NULL)
157 {
158 record_warning (_("\
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;
163 }
164 }
165
166 #define noparen(arg1, argn...) arg1, ##argn
167 #define TESTARR_ELEM(cat, val) \
168 if (!time->cat##_defined) \
169 { \
170 const char *initval[] = { noparen val }; \
171 unsigned int i; \
172 \
173 if (! nothing) \
174 record_error (0, 0, _("%s: field `%s' not defined"), \
175 "LC_TIME", #cat); \
176 \
177 for (i = 0; i < sizeof (initval) / sizeof (initval[0]); ++i) \
178 time->cat[i] = initval[i]; \
179 }
180
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" ));
190
191 #define TEST_ELEM(cat, initval) \
192 if (time->cat == NULL) \
193 { \
194 if (! nothing) \
195 record_error (0, 0, _("%s: field `%s' not defined"), \
196 "LC_TIME", #cat); \
197 \
198 time->cat = initval; \
199 }
200
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");
204
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)
208 {
209 if (time->am_pm[0][0] == '\0' && time->am_pm[1][0] == '\0')
210 {
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;
214 }
215 else
216 {
217 time->t_fmt_ampm = "%I:%M:%S %p";
218 time->wt_fmt_ampm = (const uint32_t *) L"%I:%M:%S %p";
219 }
220 }
221
222 /* Now process the era entries. */
223 if (time->num_era != 0)
224 {
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;
229
230 time->era_entries =
231 (struct era_data *) xmalloc (time->num_era
232 * sizeof (struct era_data));
233
234 for (idx = 0; idx < time->num_era; ++idx)
235 {
236 size_t era_len = strlen (time->era[idx]);
237 char *str = xmalloc ((era_len + 1 + 3) & ~3);
238 char *endp;
239
240 memcpy (str, time->era[idx], era_len + 1);
241
242 /* First character must be + or - for the direction. */
243 if (*str != '+' && *str != '-')
244 {
245 record_error (0, 0, _("\
246 %s: direction flag in string %Zd in `era' field is not '+' nor '-'"),
247 "LC_TIME", idx + 1);
248 /* Default arbitrarily to '+'. */
249 time->era_entries[idx].direction = '+';
250 }
251 else
252 time->era_entries[idx].direction = *str;
253 if (*++str != ':')
254 {
255 record_error (0, 0, _("\
256 %s: direction flag in string %Zd in `era' field is not a single character"),
257 "LC_TIME", idx + 1);
258 (void) strsep (&str, ":");
259 }
260 else
261 ++str;
262
263 /* Now the offset year. */
264 time->era_entries[idx].offset = strtol (str, &endp, 10);
265 if (endp == str)
266 {
267 record_error (0, 0, _("\
268 %s: invalid number for offset in string %Zd in `era' field"),
269 "LC_TIME", idx + 1);
270 (void) strsep (&str, ":");
271 }
272 else if (*endp != ':')
273 {
274 record_error (0, 0, _("\
275 %s: garbage at end of offset value in string %Zd in `era' field"),
276 "LC_TIME", idx + 1);
277 (void) strsep (&str, ":");
278 }
279 else
280 str = endp + 1;
281
282 /* Next is the starting date in ISO format. */
283 if (strncmp (str, "-*", 2) == 0)
284 {
285 time->era_entries[idx].start_date[0] =
286 time->era_entries[idx].start_date[1] =
287 time->era_entries[idx].start_date[2] = 0x80000000;
288 if (str[2] != ':')
289 goto garbage_start_date;
290 str += 3;
291 }
292 else if (strncmp (str, "+*", 2) == 0)
293 {
294 time->era_entries[idx].start_date[0] =
295 time->era_entries[idx].start_date[1] =
296 time->era_entries[idx].start_date[2] = 0x7fffffff;
297 if (str[2] != ':')
298 goto garbage_start_date;
299 str += 3;
300 }
301 else
302 {
303 time->era_entries[idx].start_date[0] = strtol (str, &endp, 10);
304 if (endp == str || *endp != '/')
305 goto invalid_start_date;
306 else
307 str = endp + 1;
308 time->era_entries[idx].start_date[0] -= 1900;
309 /* year -1 represent 1 B.C. (not -1 A.D.) */
310 if (time->era_entries[idx].start_date[0] < -1900)
311 ++time->era_entries[idx].start_date[0];
312
313 time->era_entries[idx].start_date[1] = strtol (str, &endp, 10);
314 if (endp == str || *endp != '/')
315 goto invalid_start_date;
316 else
317 str = endp + 1;
318 time->era_entries[idx].start_date[1] -= 1;
319
320 time->era_entries[idx].start_date[2] = strtol (str, &endp, 10);
321 if (endp == str)
322 {
323 invalid_start_date:
324 record_error (0, 0, _("\
325 %s: invalid starting date in string %Zd in `era' field"),
326 "LC_TIME", idx + 1);
327 (void) strsep (&str, ":");
328 }
329 else if (*endp != ':')
330 {
331 garbage_start_date:
332 record_error (0, 0, _("\
333 %s: garbage at end of starting date in string %Zd in `era' field "),
334 "LC_TIME", idx + 1);
335 (void) strsep (&str, ":");
336 }
337 else
338 {
339 str = endp + 1;
340
341 /* Check for valid value. */
342 if ((time->era_entries[idx].start_date[1] < 0
343 || time->era_entries[idx].start_date[1] >= 12
344 || time->era_entries[idx].start_date[2] < 0
345 || (time->era_entries[idx].start_date[2]
346 > days_per_month[time->era_entries[idx].start_date[1]])
347 || (time->era_entries[idx].start_date[1] == 2
348 && time->era_entries[idx].start_date[2] == 29
349 && !__isleap (time->era_entries[idx].start_date[0]))))
350 record_error (0, 0, _("\
351 %s: starting date is invalid in string %Zd in `era' field"),
352 "LC_TIME", idx + 1);
353 }
354 }
355
356 /* Next is the stopping date in ISO format. */
357 if (strncmp (str, "-*", 2) == 0)
358 {
359 time->era_entries[idx].stop_date[0] =
360 time->era_entries[idx].stop_date[1] =
361 time->era_entries[idx].stop_date[2] = 0x80000000;
362 if (str[2] != ':')
363 goto garbage_stop_date;
364 str += 3;
365 }
366 else if (strncmp (str, "+*", 2) == 0)
367 {
368 time->era_entries[idx].stop_date[0] =
369 time->era_entries[idx].stop_date[1] =
370 time->era_entries[idx].stop_date[2] = 0x7fffffff;
371 if (str[2] != ':')
372 goto garbage_stop_date;
373 str += 3;
374 }
375 else
376 {
377 time->era_entries[idx].stop_date[0] = strtol (str, &endp, 10);
378 if (endp == str || *endp != '/')
379 goto invalid_stop_date;
380 else
381 str = endp + 1;
382 time->era_entries[idx].stop_date[0] -= 1900;
383 /* year -1 represent 1 B.C. (not -1 A.D.) */
384 if (time->era_entries[idx].stop_date[0] < -1900)
385 ++time->era_entries[idx].stop_date[0];
386
387 time->era_entries[idx].stop_date[1] = strtol (str, &endp, 10);
388 if (endp == str || *endp != '/')
389 goto invalid_stop_date;
390 else
391 str = endp + 1;
392 time->era_entries[idx].stop_date[1] -= 1;
393
394 time->era_entries[idx].stop_date[2] = strtol (str, &endp, 10);
395 if (endp == str)
396 {
397 invalid_stop_date:
398 record_error (0, 0, _("\
399 %s: invalid stopping date in string %Zd in `era' field"),
400 "LC_TIME", idx + 1);
401 (void) strsep (&str, ":");
402 }
403 else if (*endp != ':')
404 {
405 garbage_stop_date:
406 record_error (0, 0, _("\
407 %s: garbage at end of stopping date in string %Zd in `era' field"),
408 "LC_TIME", idx + 1);
409 (void) strsep (&str, ":");
410 }
411 else
412 {
413 str = endp + 1;
414
415 /* Check for valid value. */
416 if ((time->era_entries[idx].stop_date[1] < 0
417 || time->era_entries[idx].stop_date[1] >= 12
418 || time->era_entries[idx].stop_date[2] < 0
419 || (time->era_entries[idx].stop_date[2]
420 > days_per_month[time->era_entries[idx].stop_date[1]])
421 || (time->era_entries[idx].stop_date[1] == 2
422 && time->era_entries[idx].stop_date[2] == 29
423 && !__isleap (time->era_entries[idx].stop_date[0]))))
424 record_error (0, 0, _("\
425 %s: invalid stopping date in string %Zd in `era' field"),
426 "LC_TIME", idx + 1);
427 }
428 }
429
430 if (str == NULL || *str == '\0')
431 {
432 record_error (0, 0, _("\
433 %s: missing era name in string %Zd in `era' field"), "LC_TIME", idx + 1);
434 time->era_entries[idx].name =
435 time->era_entries[idx].format = "";
436 }
437 else
438 {
439 time->era_entries[idx].name = strsep (&str, ":");
440
441 if (str == NULL || *str == '\0')
442 {
443 record_error (0, 0, _("\
444 %s: missing era format in string %Zd in `era' field"),
445 "LC_TIME", idx + 1);
446 time->era_entries[idx].name =
447 time->era_entries[idx].format = "";
448 }
449 else
450 time->era_entries[idx].format = str;
451 }
452
453 /* Now generate the wide character name and format. */
454 wstr = wcschr ((wchar_t *) time->wera[idx], L':');/* end direction */
455 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end offset */
456 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end start */
457 wstr = wstr ? wcschr (wstr + 1, L':') : NULL; /* end end */
458 if (wstr != NULL)
459 {
460 time->era_entries[idx].wname = (uint32_t *) wstr + 1;
461 wstr = wcschr (wstr + 1, L':'); /* end name */
462 if (wstr != NULL)
463 {
464 *wstr = L'\0';
465 time->era_entries[idx].wformat = (uint32_t *) wstr + 1;
466 }
467 else
468 time->era_entries[idx].wname =
469 time->era_entries[idx].wformat = (uint32_t *) L"";
470 }
471 else
472 time->era_entries[idx].wname =
473 time->era_entries[idx].wformat = (uint32_t *) L"";
474 }
475 }
476
477 /* Set up defaults based on ISO 30112 WD10 [2014]. */
478 if (time->week_ndays == 0)
479 time->week_ndays = 7;
480
481 if (time->week_1stday == 0)
482 time->week_1stday = 19971130;
483
484 if (time->week_1stweek == 0)
485 time->week_1stweek = 7;
486
487 if (time->week_1stweek > time->week_ndays)
488 record_error (0, 0, _("\
489 %s: third operand for value of field `%s' must not be larger than %d"),
490 "LC_TIME", "week", 7);
491
492 if (time->first_weekday == '\0')
493 /* The definition does not specify this so the default is used. */
494 time->first_weekday = 1;
495 else if (time->first_weekday > time->week_ndays)
496 record_error (0, 0, _("\
497 %s: values for field `%s' must not be larger than %d"),
498 "LC_TIME", "first_weekday", 7);
499
500 if (time->first_workday == '\0')
501 /* The definition does not specify this so the default is used. */
502 time->first_workday = 2;
503 else if (time->first_workday > time->week_ndays)
504 record_error (0, 0, _("\
505 %s: values for field `%s' must not be larger than %d"),
506 "LC_TIME", "first_workday", 7);
507
508 if (time->cal_direction == '\0')
509 /* The definition does not specify this so the default is used. */
510 time->cal_direction = 1;
511 else if (time->cal_direction > 3)
512 record_error (0, 0, _("\
513 %s: values for field `%s' must not be larger than %d"),
514 "LC_TIME", "cal_direction", 3);
515
516 /* XXX We don't perform any tests on the timezone value since this is
517 simply useless, stupid $&$!@... */
518 if (time->timezone == NULL)
519 time->timezone = "";
520
521 if (time->date_fmt == NULL)
522 time->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
523 if (time->wdate_fmt == NULL)
524 time->wdate_fmt = (const uint32_t *) L"%a %b %e %H:%M:%S %Z %Y";
525 }
526
527
528 void
529 time_output (struct localedef_t *locale, const struct charmap_t *charmap,
530 const char *output_path)
531 {
532 struct locale_time_t *time = locale->categories[LC_TIME].time;
533 struct locale_file file;
534 size_t num, n;
535
536 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_TIME));
537
538 /* The ab'days. */
539 for (n = 0; n < 7; ++n)
540 add_locale_string (&file, time->abday[n] ?: "");
541
542 /* The days. */
543 for (n = 0; n < 7; ++n)
544 add_locale_string (&file, time->day[n] ?: "");
545
546 /* The ab'mons. */
547 for (n = 0; n < 12; ++n)
548 add_locale_string (&file, time->abmon[n] ?: "");
549
550 /* The mons. */
551 for (n = 0; n < 12; ++n)
552 add_locale_string (&file, time->mon[n] ?: "");
553
554 /* AM/PM. */
555 for (n = 0; n < 2; ++n)
556 add_locale_string (&file, time->am_pm[n]);
557
558 add_locale_string (&file, time->d_t_fmt ?: "");
559 add_locale_string (&file, time->d_fmt ?: "");
560 add_locale_string (&file, time->t_fmt ?: "");
561 add_locale_string (&file, time->t_fmt_ampm ?: "");
562
563 start_locale_structure (&file);
564 for (num = 0; num < time->num_era; ++num)
565 add_locale_string (&file, time->era[num]);
566 end_locale_structure (&file);
567
568 add_locale_string (&file, time->era_year ?: "");
569 add_locale_string (&file, time->era_d_fmt ?: "");
570
571 start_locale_structure (&file);
572 for (num = 0; num < 100; ++num)
573 add_locale_string (&file, time->alt_digits[num] ?: "");
574 end_locale_structure (&file);
575
576 add_locale_string (&file, time->era_d_t_fmt ?: "");
577 add_locale_string (&file, time->era_t_fmt ?: "");
578 add_locale_uint32 (&file, time->num_era);
579
580 start_locale_structure (&file);
581 for (num = 0; num < time->num_era; ++num)
582 {
583 add_locale_uint32 (&file, time->era_entries[num].direction);
584 add_locale_uint32 (&file, time->era_entries[num].offset);
585 add_locale_uint32 (&file, time->era_entries[num].start_date[0]);
586 add_locale_uint32 (&file, time->era_entries[num].start_date[1]);
587 add_locale_uint32 (&file, time->era_entries[num].start_date[2]);
588 add_locale_uint32 (&file, time->era_entries[num].stop_date[0]);
589 add_locale_uint32 (&file, time->era_entries[num].stop_date[1]);
590 add_locale_uint32 (&file, time->era_entries[num].stop_date[2]);
591 add_locale_string (&file, time->era_entries[num].name);
592 add_locale_string (&file, time->era_entries[num].format);
593 add_locale_wstring (&file, time->era_entries[num].wname);
594 add_locale_wstring (&file, time->era_entries[num].wformat);
595 }
596 end_locale_structure (&file);
597
598 /* The wide character ab'days. */
599 for (n = 0; n < 7; ++n)
600 add_locale_wstring (&file, time->wabday[n] ?: empty_wstr);
601
602 /* The wide character days. */
603 for (n = 0; n < 7; ++n)
604 add_locale_wstring (&file, time->wday[n] ?: empty_wstr);
605
606 /* The wide character ab'mons. */
607 for (n = 0; n < 12; ++n)
608 add_locale_wstring (&file, time->wabmon[n] ?: empty_wstr);
609
610 /* The wide character mons. */
611 for (n = 0; n < 12; ++n)
612 add_locale_wstring (&file, time->wmon[n] ?: empty_wstr);
613
614 /* Wide character AM/PM. */
615 for (n = 0; n < 2; ++n)
616 add_locale_wstring (&file, time->wam_pm[n] ?: empty_wstr);
617
618 add_locale_wstring (&file, time->wd_t_fmt ?: empty_wstr);
619 add_locale_wstring (&file, time->wd_fmt ?: empty_wstr);
620 add_locale_wstring (&file, time->wt_fmt ?: empty_wstr);
621 add_locale_wstring (&file, time->wt_fmt_ampm ?: empty_wstr);
622 add_locale_wstring (&file, time->wera_year ?: empty_wstr);
623 add_locale_wstring (&file, time->wera_d_fmt ?: empty_wstr);
624
625 start_locale_structure (&file);
626 for (num = 0; num < 100; ++num)
627 add_locale_wstring (&file, time->walt_digits[num] ?: empty_wstr);
628 end_locale_structure (&file);
629
630 add_locale_wstring (&file, time->wera_d_t_fmt ?: empty_wstr);
631 add_locale_wstring (&file, time->wera_t_fmt ?: empty_wstr);
632 add_locale_char (&file, time->week_ndays);
633 add_locale_uint32 (&file, time->week_1stday);
634 add_locale_char (&file, time->week_1stweek);
635 add_locale_char (&file, time->first_weekday);
636 add_locale_char (&file, time->first_workday);
637 add_locale_char (&file, time->cal_direction);
638 add_locale_string (&file, time->timezone);
639 add_locale_string (&file, time->date_fmt);
640 add_locale_wstring (&file, time->wdate_fmt);
641 add_locale_string (&file, charmap->code_set_name);
642 write_locale_data (output_path, LC_TIME, "LC_TIME", &file);
643 }
644
645
646 /* The parser for the LC_TIME section of the locale definition. */
647 void
648 time_read (struct linereader *ldfile, struct localedef_t *result,
649 const struct charmap_t *charmap, const char *repertoire_name,
650 int ignore_content)
651 {
652 struct repertoire_t *repertoire = NULL;
653 struct locale_time_t *time;
654 struct token *now;
655 enum token_t nowtok;
656 size_t cnt;
657
658 /* Get the repertoire we have to use. */
659 if (repertoire_name != NULL)
660 repertoire = repertoire_read (repertoire_name);
661
662 /* The rest of the line containing `LC_TIME' must be free. */
663 lr_ignore_rest (ldfile, 1);
664
665
666 do
667 {
668 now = lr_token (ldfile, charmap, result, repertoire, verbose);
669 nowtok = now->tok;
670 }
671 while (nowtok == tok_eol);
672
673 /* If we see `copy' now we are almost done. */
674 if (nowtok == tok_copy)
675 {
676 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_time,
677 LC_TIME, "LC_TIME", ignore_content);
678 return;
679 }
680
681 /* Prepare the data structures. */
682 time_startup (ldfile, result, ignore_content);
683 time = result->categories[LC_TIME].time;
684
685 while (1)
686 {
687 /* Of course we don't proceed beyond the end of file. */
688 if (nowtok == tok_eof)
689 break;
690
691 /* Ingore empty lines. */
692 if (nowtok == tok_eol)
693 {
694 now = lr_token (ldfile, charmap, result, repertoire, verbose);
695 nowtok = now->tok;
696 continue;
697 }
698
699 switch (nowtok)
700 {
701 #define STRARR_ELEM(cat, min, max) \
702 case tok_##cat: \
703 /* Ignore the rest of the line if we don't need the input of \
704 this line. */ \
705 if (ignore_content) \
706 { \
707 lr_ignore_rest (ldfile, 0); \
708 break; \
709 } \
710 \
711 for (cnt = 0; cnt < max; ++cnt) \
712 { \
713 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
714 if (now->tok == tok_eol) \
715 { \
716 if (cnt < min) \
717 lr_error (ldfile, _("%s: too few values for field `%s'"), \
718 "LC_TIME", #cat); \
719 if (!ignore_content) \
720 do \
721 { \
722 time->cat[cnt] = ""; \
723 time->w##cat[cnt] = empty_wstr; \
724 } \
725 while (++cnt < max); \
726 break; \
727 } \
728 else if (now->tok != tok_string) \
729 goto err_label; \
730 else if (!ignore_content && (now->val.str.startmb == NULL \
731 || now->val.str.startwc == NULL)) \
732 { \
733 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
734 "LC_TIME", #cat); \
735 time->cat[cnt] = ""; \
736 time->w##cat[cnt] = empty_wstr; \
737 } \
738 else if (!ignore_content) \
739 { \
740 time->cat[cnt] = now->val.str.startmb; \
741 time->w##cat[cnt] = now->val.str.startwc; \
742 } \
743 \
744 /* Match the semicolon. */ \
745 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
746 if (now->tok != tok_semicolon && now->tok != tok_eol) \
747 break; \
748 } \
749 if (now->tok != tok_eol) \
750 { \
751 while (!ignore_content && cnt < min) \
752 { \
753 time->cat[cnt] = ""; \
754 time->w##cat[cnt++] = empty_wstr; \
755 } \
756 \
757 if (now->tok == tok_semicolon) \
758 { \
759 now = lr_token (ldfile, charmap, result, repertoire, \
760 verbose); \
761 if (now->tok == tok_eol) \
762 lr_error (ldfile, _("extra trailing semicolon")); \
763 else if (now->tok == tok_string) \
764 { \
765 lr_error (ldfile, _("\
766 %s: too many values for field `%s'"), \
767 "LC_TIME", #cat); \
768 lr_ignore_rest (ldfile, 0); \
769 } \
770 else \
771 goto err_label; \
772 } \
773 else \
774 goto err_label; \
775 } \
776 time->cat##_defined = 1; \
777 break
778
779 STRARR_ELEM (abday, 7, 7);
780 STRARR_ELEM (day, 7, 7);
781 STRARR_ELEM (abmon, 12, 12);
782 STRARR_ELEM (mon, 12, 12);
783 STRARR_ELEM (am_pm, 2, 2);
784 STRARR_ELEM (alt_digits, 0, 100);
785
786 case tok_era:
787 /* Ignore the rest of the line if we don't need the input of
788 this line. */
789 if (ignore_content)
790 {
791 lr_ignore_rest (ldfile, 0);
792 break;
793 }
794 do
795 {
796 now = lr_token (ldfile, charmap, result, repertoire, verbose);
797 if (now->tok != tok_string)
798 goto err_label;
799 if (!ignore_content && (now->val.str.startmb == NULL
800 || now->val.str.startwc == NULL))
801 {
802 lr_error (ldfile, _("%s: unknown character in field `%s'"),
803 "LC_TIME", "era");
804 lr_ignore_rest (ldfile, 0);
805 break;
806 }
807 if (!ignore_content)
808 {
809 time->era = xrealloc (time->era,
810 (time->num_era + 1) * sizeof (char *));
811 time->era[time->num_era] = now->val.str.startmb;
812
813 time->wera = xrealloc (time->wera,
814 (time->num_era + 1)
815 * sizeof (char *));
816 time->wera[time->num_era++] = now->val.str.startwc;
817 }
818 now = lr_token (ldfile, charmap, result, repertoire, verbose);
819 if (now->tok != tok_eol && now->tok != tok_semicolon)
820 goto err_label;
821 }
822 while (now->tok == tok_semicolon);
823 break;
824
825 #define STR_ELEM(cat) \
826 case tok_##cat: \
827 /* Ignore the rest of the line if we don't need the input of \
828 this line. */ \
829 if (ignore_content) \
830 { \
831 lr_ignore_rest (ldfile, 0); \
832 break; \
833 } \
834 \
835 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
836 if (now->tok != tok_string) \
837 goto err_label; \
838 else if (time->cat != NULL) \
839 lr_error (ldfile, _("\
840 %s: field `%s' declared more than once"), "LC_TIME", #cat); \
841 else if (!ignore_content && (now->val.str.startmb == NULL \
842 || now->val.str.startwc == NULL)) \
843 { \
844 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
845 "LC_TIME", #cat); \
846 time->cat = ""; \
847 time->w##cat = empty_wstr; \
848 } \
849 else if (!ignore_content) \
850 { \
851 time->cat = now->val.str.startmb; \
852 time->w##cat = now->val.str.startwc; \
853 } \
854 break
855
856 STR_ELEM (d_t_fmt);
857 STR_ELEM (d_fmt);
858 STR_ELEM (t_fmt);
859 STR_ELEM (t_fmt_ampm);
860 STR_ELEM (era_year);
861 STR_ELEM (era_d_t_fmt);
862 STR_ELEM (era_d_fmt);
863 STR_ELEM (era_t_fmt);
864 STR_ELEM (timezone);
865 STR_ELEM (date_fmt);
866
867 #define INT_ELEM(cat) \
868 case tok_##cat: \
869 /* Ignore the rest of the line if we don't need the input of \
870 this line. */ \
871 if (ignore_content) \
872 { \
873 lr_ignore_rest (ldfile, 0); \
874 break; \
875 } \
876 \
877 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
878 if (now->tok != tok_number) \
879 goto err_label; \
880 else if (time->cat != 0) \
881 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
882 "LC_TIME", #cat); \
883 else if (!ignore_content) \
884 time->cat = now->val.num; \
885 break
886
887 INT_ELEM (first_weekday);
888 INT_ELEM (first_workday);
889 INT_ELEM (cal_direction);
890
891 case tok_week:
892 /* Ignore the rest of the line if we don't need the input of
893 this line. */
894 if (ignore_content)
895 {
896 lr_ignore_rest (ldfile, 0);
897 break;
898 }
899
900 now = lr_token (ldfile, charmap, result, repertoire, verbose);
901 if (now->tok != tok_number)
902 goto err_label;
903 time->week_ndays = now->val.num;
904
905 now = lr_token (ldfile, charmap, result, repertoire, verbose);
906 if (now->tok != tok_semicolon)
907 goto err_label;
908
909 now = lr_token (ldfile, charmap, result, repertoire, verbose);
910 if (now->tok != tok_number)
911 goto err_label;
912 time->week_1stday = now->val.num;
913
914 now = lr_token (ldfile, charmap, result, repertoire, verbose);
915 if (now->tok != tok_semicolon)
916 goto err_label;
917
918 now = lr_token (ldfile, charmap, result, repertoire, verbose);
919 if (now->tok != tok_number)
920 goto err_label;
921 time->week_1stweek = now->val.num;
922
923 lr_ignore_rest (ldfile, 1);
924 break;
925
926 case tok_end:
927 /* Next we assume `LC_TIME'. */
928 now = lr_token (ldfile, charmap, result, repertoire, verbose);
929 if (now->tok == tok_eof)
930 break;
931 if (now->tok == tok_eol)
932 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TIME");
933 else if (now->tok != tok_lc_time)
934 lr_error (ldfile, _("\
935 %1$s: definition does not end with `END %1$s'"), "LC_TIME");
936 lr_ignore_rest (ldfile, now->tok == tok_lc_time);
937 return;
938
939 default:
940 err_label:
941 SYNTAX_ERROR (_("%s: syntax error"), "LC_TIME");
942 }
943
944 /* Prepare for the next round. */
945 now = lr_token (ldfile, charmap, result, repertoire, verbose);
946 nowtok = now->tok;
947 }
948
949 /* When we come here we reached the end of the file. */
950 lr_error (ldfile, _("%s: premature end of file"), "LC_TIME");
951 }