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