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