]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/cal.c
cal: use size_t to calculate width [lgtm scan]
[thirdparty/util-linux.git] / misc-utils / cal.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kim Letkeman.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 /* 1999-02-01 Jean-Francois Bignolles: added option '-m' to display
38 * monday as the first day of the week.
39 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
40 * - added Native Language Support
41 *
42 * 2000-09-01 Michael Charles Pruznick <dummy@netwiz.net>
43 * Added "-3" option to print prev/next month with current.
44 * Added overridable default MONTHS_IN_ROW and "-1" option to
45 * get traditional output when -3 is the default. I hope that
46 * enough people will like -3 as the default that one day the
47 * product can be shipped that way.
48 *
49 * 2001-05-07 Pablo Saratxaga <pablo@mandrakesoft.com>
50 * Fixed the bugs with multi-byte charset (zg: cjk, utf-8)
51 * displaying. made the 'month year' ("%s %d") header translatable
52 * so it can be adapted to conventions used by different languages
53 * added support to read "first_weekday" locale information
54 * still to do: support for 'cal_direction' (will require a major
55 * rewrite of the displaying) and proper handling of RTL scripts
56 */
57
58 #include <sys/types.h>
59
60 #include <ctype.h>
61 #include <getopt.h>
62 #include <stdint.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <time.h>
67 #include <unistd.h>
68 #include <errno.h>
69
70 #include "c.h"
71 #include "closestream.h"
72 #include "colors.h"
73 #include "nls.h"
74 #include "mbsalign.h"
75 #include "strutils.h"
76 #include "optutils.h"
77 #include "timeutils.h"
78 #include "ttyutils.h"
79
80 #define DOY_MONTH_WIDTH 27 /* -j month width */
81 #define DOM_MONTH_WIDTH 20 /* month width */
82
83 static int has_term = 0;
84 static const char *Senter = "", *Sexit = ""; /* enter and exit standout mode */
85
86 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
87 # if defined(HAVE_NCURSESW_TERM_H)
88 # include <ncursesw/term.h>
89 # elif defined(HAVE_NCURSES_TERM_H)
90 # include <ncurses/term.h>
91 # elif defined(HAVE_TERM_H)
92 # include <term.h>
93 # endif
94 #endif
95
96 static int setup_terminal(char *term
97 #if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
98 __attribute__((__unused__))
99 #endif
100 )
101 {
102 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
103 int ret;
104
105 if (setupterm(term, STDOUT_FILENO, &ret) != 0 || ret != 1)
106 return -1;
107 #endif
108 return 0;
109 }
110
111 static void my_putstring(const char *s)
112 {
113 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
114 if (has_term)
115 putp(s);
116 else
117 #endif
118 fputs(s, stdout);
119 }
120
121 static const char *my_tgetstr(char *ss
122 #if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
123 __attribute__((__unused__))
124 #endif
125 )
126 {
127 const char *ret = NULL;
128
129 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
130 if (has_term)
131 ret = tigetstr(ss);
132 #endif
133 if (!ret || ret == (char *)-1)
134 return "";
135 return ret;
136 }
137
138 #include "widechar.h"
139
140 enum {
141 GREGORIAN = INT32_MIN,
142 ISO = INT32_MIN,
143 GB1752 = 1752,
144 DEFAULT_REFORM_YEAR = 1752,
145 JULIAN = INT32_MAX
146 };
147
148 enum {
149 SUNDAY = 0,
150 MONDAY,
151 TUESDAY,
152 WEDNESDAY,
153 THURSDAY,
154 FRIDAY,
155 SATURDAY,
156 DAYS_IN_WEEK,
157 NONEDAY
158 };
159
160 enum {
161 JANUARY = 1,
162 FEBRUARY,
163 MARCH,
164 APRIL,
165 MAY,
166 JUNE,
167 JULY,
168 AUGUST,
169 SEPTEMBER,
170 OCTOBER,
171 NOVEMBER,
172 DECEMBER
173 };
174
175 #define REFORMATION_MONTH SEPTEMBER
176 #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
177 #define YDAY_AFTER_MISSING 258 /* 14th in Sep 1752 */
178
179 #define MONTHS_IN_YEAR DECEMBER
180 #define DAYS_IN_MONTH 31
181 #define MAXDAYS 42 /* slots in a month array */
182 #define SPACE -1 /* used in day array */
183
184 #define SMALLEST_YEAR 1
185
186 #define DAY_LEN 3 /* 3 spaces per day */
187 #define WEEK_LEN (DAYS_IN_WEEK * DAY_LEN)
188 #define MONTHS_IN_YEAR_ROW 3 /* month columns in year view */
189 #define WNUM_LEN 3
190
191 #define FMT_ST_CHARS 300 /* 90 suffices in most locales */
192
193 static const int days_in_month[2][13] = {
194 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
195 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
196 };
197
198 enum {
199 WEEK_NUM_DISABLED = 0,
200 WEEK_NUM_MASK=0xff,
201 WEEK_NUM_ISO=0x100,
202 WEEK_NUM_US=0x200,
203 };
204
205 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
206 static char day_headings[(WEEK_LEN + 1) * 6 + 1];
207
208 struct cal_request {
209 int day;
210 int month;
211 int32_t year;
212 int week;
213 int start_month;
214 };
215
216 struct cal_control {
217 const char *full_month[MONTHS_IN_YEAR]; /* month names */
218 const char *abbr_month[MONTHS_IN_YEAR]; /* abbreviated month names */
219 const char *weekdays[DAYS_IN_WEEK]; /* day names */
220
221 int reform_year; /* Gregorian reform year */
222 int colormode; /* day and week number highlight */
223 int num_months; /* number of requested months */
224 int span_months; /* span the date */
225 int months_in_row; /* number of months horizontally in print out */
226 int weekstart; /* day the week starts, often Sun or Mon */
227 int weektype; /* WEEK_TYPE_{NONE,ISO,US} */
228 size_t day_width; /* day width in characters in printout */
229 size_t week_width; /* 7 * day_width + possible week num */
230 size_t month_width; /* width of a month (vertical mode) */
231 int gutter_width; /* spaces in between horizontal month outputs */
232 struct cal_request req; /* the times user is interested */
233 unsigned int julian:1, /* julian output */
234 header_year:1, /* print year number */
235 header_hint:1, /* does month name + year need two lines to fit */
236 vertical:1; /* display the output in vertical */
237 };
238
239 struct cal_month {
240 int days[MAXDAYS]; /* the day numbers, or SPACE */
241 int weeks[MAXDAYS / DAYS_IN_WEEK];
242 int month;
243 int32_t year;
244 struct cal_month *next;
245 };
246 /* function prototypes */
247 static int leap_year(const struct cal_control *ctl, int32_t year);
248 static int monthname_to_number(struct cal_control *ctl, const char *name);
249 static void weekdays_init(struct cal_control *ctl);
250 static void headers_init(struct cal_control *ctl);
251 static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl);
252 static void cal_output_header(struct cal_month *month, const struct cal_control *ctl);
253 static void cal_output_months(struct cal_month *month, const struct cal_control *ctl);
254 static void cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl);
255 static void monthly(const struct cal_control *ctl);
256 static void yearly(const struct cal_control *ctl);
257 static int day_in_year(const struct cal_control *ctl, int day,
258 int month, int32_t year);
259 static int day_in_week(const struct cal_control *ctl, int day,
260 int month, int32_t year);
261 static int week_number(int day, int month, int32_t year, const struct cal_control *ctl);
262 static int week_to_day(const struct cal_control *ctl);
263 static int center_str(const char *src, char *dest, size_t dest_size, size_t width);
264 static void center(const char *str, size_t len, int separate);
265 static int left_str(const char *src, char *dest, size_t dest_size, size_t width);
266 static void left(const char *str, size_t len, int separate);
267 static int parse_reform_year(const char *reform_year);
268 static void __attribute__((__noreturn__)) usage(void);
269
270 #ifdef TEST_CAL
271 static time_t cal_time(time_t *t)
272 {
273 char *str = getenv("CAL_TEST_TIME");
274
275 if (str) {
276 uint64_t x = strtou64_or_err(str, "failed to parse CAL_TEST_TIME");
277
278 *t = x;
279 return *t;
280 }
281
282 return time(t);
283 }
284 #else
285 # define cal_time(t) time(t)
286 #endif
287
288 int main(int argc, char **argv)
289 {
290 struct tm local_time;
291 char *term;
292 time_t now;
293 int ch = 0, yflag = 0, Yflag = 0;
294
295 static struct cal_control ctl = {
296 .reform_year = DEFAULT_REFORM_YEAR,
297 .weekstart = SUNDAY,
298 .span_months = 0,
299 .colormode = UL_COLORMODE_UNDEF,
300 .weektype = WEEK_NUM_DISABLED,
301 .day_width = DAY_LEN,
302 .gutter_width = 2,
303 .req.day = 0,
304 .req.month = 0
305 };
306
307 enum {
308 OPT_COLOR = CHAR_MAX + 1,
309 OPT_ISO,
310 OPT_REFORM
311 };
312
313 static const struct option longopts[] = {
314 {"one", no_argument, NULL, '1'},
315 {"three", no_argument, NULL, '3'},
316 {"sunday", no_argument, NULL, 's'},
317 {"monday", no_argument, NULL, 'm'},
318 {"julian", no_argument, NULL, 'j'},
319 {"months", required_argument, NULL, 'n'},
320 {"span", no_argument, NULL, 'S'},
321 {"year", no_argument, NULL, 'y'},
322 {"week", optional_argument, NULL, 'w'},
323 {"color", optional_argument, NULL, OPT_COLOR},
324 {"reform", required_argument, NULL, OPT_REFORM},
325 {"iso", no_argument, NULL, OPT_ISO},
326 {"version", no_argument, NULL, 'V'},
327 {"twelve", no_argument, NULL, 'Y'},
328 {"help", no_argument, NULL, 'h'},
329 {"vertical", no_argument, NULL,'v'},
330 {NULL, 0, NULL, 0}
331 };
332
333 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
334 { 'Y','n','y' },
335 { 0 }
336 };
337 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
338
339 setlocale(LC_ALL, "");
340 bindtextdomain(PACKAGE, LOCALEDIR);
341 textdomain(PACKAGE);
342 close_stdout_atexit();
343
344 term = getenv("TERM");
345 if (term) {
346 has_term = setup_terminal(term) == 0;
347 if (has_term) {
348 Senter = my_tgetstr("smso");
349 Sexit = my_tgetstr("rmso");
350 }
351 }
352
353 /*
354 * The traditional Unix cal utility starts the week at Sunday,
355 * while ISO 8601 starts at Monday. We read the start day from
356 * the locale database, which can be overridden with the
357 * -s (Sunday) or -m (Monday) options.
358 */
359 #if HAVE_DECL__NL_TIME_WEEK_1STDAY
360 /*
361 * You need to use 2 locale variables to get the first day of the week.
362 * This is needed to support first_weekday=2 and first_workday=1 for
363 * the rare case where working days span across 2 weeks.
364 * This shell script shows the combinations and calculations involved:
365 *
366 * for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
367 * printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
368 * date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
369 * done
370 *
371 * en_US: 19971130 + 1 -1 = 0 #0 = sunday
372 * ru_RU: 19971130 + 2 -1 = 1
373 * fr_FR: 19971201 + 1 -1 = 1
374 * csb_PL: 19971201 + 2 -1 = 2
375 * POSIX: 19971201 + 7 -1 = 0
376 */
377 {
378 int wfd;
379 union { unsigned int word; char *string; } val;
380 val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
381
382 wfd = val.word;
383 wfd = day_in_week(&ctl, wfd % 100, (wfd / 100) % 100,
384 wfd / (100 * 100));
385 ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
386 }
387 #endif
388 while ((ch = getopt_long(argc, argv, "13mjn:sSywYvVh", longopts, NULL)) != -1) {
389
390 err_exclusive_options(ch, longopts, excl, excl_st);
391
392 switch(ch) {
393 case '1':
394 ctl.num_months = 1;
395 break;
396 case '3':
397 ctl.num_months = 3;
398 ctl.span_months = 1;
399 break;
400 case 's':
401 ctl.weekstart = SUNDAY; /* default */
402 break;
403 case 'm':
404 ctl.weekstart = MONDAY;
405 break;
406 case 'j':
407 ctl.julian = 1;
408 ctl.day_width = DAY_LEN + 1;
409 break;
410 case 'y':
411 yflag = 1;
412 break;
413 case 'Y':
414 Yflag = 1;
415 break;
416 case 'n':
417 ctl.num_months = strtou32_or_err(optarg,
418 _("invalid month argument"));
419 break;
420 case 'S':
421 ctl.span_months = 1;
422 break;
423 case 'w':
424 if (optarg) {
425 ctl.req.week = strtos32_or_err(optarg,
426 _("invalid week argument"));
427 if (ctl.req.week < 1 || 54 < ctl.req.week)
428 errx(EXIT_FAILURE,_("illegal week value: use 1-54"));
429 }
430 ctl.weektype = WEEK_NUM_US; /* default per weekstart */
431 break;
432 case OPT_COLOR:
433 ctl.colormode = UL_COLORMODE_AUTO;
434 if (optarg)
435 ctl.colormode = colormode_or_err(optarg,
436 _("unsupported color mode"));
437 break;
438 case OPT_REFORM:
439 ctl.reform_year = parse_reform_year(optarg);
440 break;
441 case OPT_ISO:
442 ctl.reform_year = ISO;
443 break;
444 case 'v':
445 ctl.vertical = 1;
446 break;
447 case 'V':
448 print_version(EXIT_SUCCESS);
449 case 'h':
450 usage();
451 default:
452 errtryhelp(EXIT_FAILURE);
453 }
454 }
455
456 argc -= optind;
457 argv += optind;
458
459 if (ctl.weektype) {
460 ctl.weektype = ctl.req.week & WEEK_NUM_MASK;
461 ctl.weektype |= (ctl.weekstart == MONDAY ? WEEK_NUM_ISO : WEEK_NUM_US);
462 ctl.week_width = (ctl.day_width * DAYS_IN_WEEK) + WNUM_LEN;
463 } else
464 ctl.week_width = ctl.day_width * DAYS_IN_WEEK;
465 /*
466 * The day_width includes the space between days,
467 * as there is no leading space, remove 1
468 * */
469 ctl.week_width -= 1;
470
471 if (argc == 1 && !isdigit_string(*argv)) {
472 usec_t x;
473 /* cal <timestamp> */
474 if (parse_timestamp(*argv, &x) == 0)
475 now = (time_t) (x / 1000000);
476 /* cal <monthname> */
477 else if ((ctl.req.month = monthname_to_number(&ctl, *argv)) > 0)
478 cal_time(&now); /* this year */
479 else
480 errx(EXIT_FAILURE, _("failed to parse timestamp or unknown month name: %s"), *argv);
481 argc = 0;
482 } else
483 cal_time(&now);
484
485 localtime_r(&now, &local_time);
486
487 switch(argc) {
488 case 3:
489 ctl.req.day = strtos32_or_err(*argv++, _("illegal day value"));
490 if (ctl.req.day < 1 || DAYS_IN_MONTH < ctl.req.day)
491 errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), DAYS_IN_MONTH);
492 /* fallthrough */
493 case 2:
494 if (isdigit(**argv))
495 ctl.req.month = strtos32_or_err(*argv++, _("illegal month value: use 1-12"));
496 else {
497 ctl.req.month = monthname_to_number(&ctl, *argv);
498 if (ctl.req.month < 0)
499 errx(EXIT_FAILURE, _("unknown month name: %s"), *argv);
500 argv++;
501 }
502 if (ctl.req.month < 1 || MONTHS_IN_YEAR < ctl.req.month)
503 errx(EXIT_FAILURE, _("illegal month value: use 1-12"));
504 /* fallthrough */
505 case 1:
506 ctl.req.year = strtos32_or_err(*argv++, _("illegal year value"));
507 if (ctl.req.year < SMALLEST_YEAR)
508 errx(EXIT_FAILURE, _("illegal year value: use positive integer"));
509 if (ctl.req.year == JULIAN)
510 errx(EXIT_FAILURE, _("illegal year value"));
511 if (ctl.req.day) {
512 int dm = days_in_month[leap_year(&ctl, ctl.req.year)]
513 [ctl.req.month];
514 if (ctl.req.day > dm)
515 errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), dm);
516 ctl.req.day = day_in_year(&ctl, ctl.req.day,
517 ctl.req.month, ctl.req.year);
518 } else if ((int32_t) (local_time.tm_year + 1900) == ctl.req.year) {
519 ctl.req.day = local_time.tm_yday + 1;
520 }
521 if (!ctl.req.month && !ctl.req.week) {
522 ctl.req.month = local_time.tm_mon + 1;
523 if (!ctl.num_months)
524 yflag = 1;
525 }
526 break;
527 case 0:
528 ctl.req.day = local_time.tm_yday + 1;
529 ctl.req.year = local_time.tm_year + 1900;
530 if (!ctl.req.month)
531 ctl.req.month = local_time.tm_mon + 1;
532 break;
533 default:
534 warnx(_("bad usage"));
535 errtryhelp(EXIT_FAILURE);
536 }
537
538 if (0 < ctl.req.week) {
539 int yday = week_to_day(&ctl);
540 int leap = leap_year(&ctl, ctl.req.year);
541 int m = 1;
542
543 if (yday < 1)
544 errx(EXIT_FAILURE, _("illegal week value: year %d "
545 "doesn't have week %d"),
546 ctl.req.year, ctl.req.week);
547 while (m <= DECEMBER && yday > days_in_month[leap][m])
548 yday -= days_in_month[leap][m++];
549 if (DECEMBER < m && ctl.weektype & WEEK_NUM_ISO) {
550 /* In some years (e.g. 2010 in ISO mode) it's possible
551 * to have a remnant of week 53 starting the year yet
552 * the year in question ends during 52, in this case
553 * we're assuming that early remnant is being referred
554 * to if 53 is given as argument. */
555 if (ctl.req.week != week_number(31, DECEMBER, ctl.req.year - 1, &ctl))
556 errx(EXIT_FAILURE,
557 _("illegal week value: year %d "
558 "doesn't have week %d"),
559 ctl.req.year, ctl.req.week);
560 }
561 if (!ctl.req.month)
562 ctl.req.month = MONTHS_IN_YEAR < m ? 1 : m;
563 }
564
565 weekdays_init(&ctl);
566 headers_init(&ctl);
567
568 if (colors_init(ctl.colormode, "cal") == 0) {
569 /*
570 * If standout mode available (Senter and Sexit are set) and
571 * user or terminal-colors.d do not disable colors than
572 * ignore colors_init().
573 */
574 if (*Senter && *Sexit && colors_mode() != UL_COLORMODE_NEVER) {
575 /* let use standout mode */
576 ;
577 } else {
578 /* disable */
579 Senter = ""; Sexit = "";
580 ctl.req.day = 0;
581 ctl.weektype &= ~WEEK_NUM_MASK;
582 }
583 }
584
585 if (yflag || Yflag) {
586 ctl.gutter_width = 3;
587 if (!ctl.num_months)
588 ctl.num_months = MONTHS_IN_YEAR;
589 if (yflag) {
590 ctl.req.start_month = 1; /* start from Jan */
591 ctl.header_year = 1; /* print year number */
592 }
593 }
594
595 if (ctl.vertical)
596 ctl.gutter_width = 1;
597
598 if (ctl.num_months > 1 && ctl.months_in_row == 0) {
599 ctl.months_in_row = MONTHS_IN_YEAR_ROW; /* default */
600
601 if (isatty(STDOUT_FILENO)) {
602 int w, mw, extra, new_n;
603
604 w = get_terminal_width(80);
605 mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH;
606
607 if (w < mw)
608 w = mw;
609
610 extra = ((w / mw) - 1) * ctl.gutter_width;
611 new_n = (w - extra) / mw;
612
613 if (new_n < MONTHS_IN_YEAR_ROW)
614 ctl.months_in_row = new_n > 0 ? new_n : 1;
615 }
616 } else if (!ctl.months_in_row)
617 ctl.months_in_row = 1;
618
619 if (!ctl.num_months)
620 ctl.num_months = 1; /* display at least one month */
621
622 if (yflag || Yflag)
623 yearly(&ctl);
624 else
625 monthly(&ctl);
626
627 return EXIT_SUCCESS;
628 }
629
630 /* leap year -- account for gregorian reformation in 1752 */
631 static int leap_year(const struct cal_control *ctl, int32_t year)
632 {
633 if (year <= ctl->reform_year)
634 return !(year % 4);
635
636 return ( !(year % 4) && (year % 100) ) || !(year % 400);
637 }
638
639 static void init_monthnames(struct cal_control *ctl)
640 {
641 size_t i;
642
643 if (ctl->full_month[0] != NULL)
644 return; /* already initialized */
645
646 for (i = 0; i < MONTHS_IN_YEAR; i++)
647 ctl->full_month[i] = nl_langinfo(ALTMON_1 + i);
648 }
649
650 static void init_abbr_monthnames(struct cal_control *ctl)
651 {
652 size_t i;
653
654 if (ctl->abbr_month[0] != NULL)
655 return; /* already initialized */
656
657 for (i = 0; i < MONTHS_IN_YEAR; i++)
658 ctl->abbr_month[i] = nl_langinfo(_NL_ABALTMON_1 + i);
659 }
660
661 static int monthname_to_number(struct cal_control *ctl, const char *name)
662 {
663 size_t i;
664
665 init_monthnames(ctl);
666 for (i = 0; i < MONTHS_IN_YEAR; i++)
667 if (strcasecmp(ctl->full_month[i], name) == 0)
668 return i + 1;
669
670 init_abbr_monthnames(ctl);
671 for (i = 0; i < MONTHS_IN_YEAR; i++)
672 if (strcasecmp(ctl->abbr_month[i], name) == 0)
673 return i + 1;
674
675 return -EINVAL;
676 }
677
678 static void weekdays_init(struct cal_control *ctl)
679 {
680 size_t wd;
681 for (int i = 0; i < DAYS_IN_WEEK; i++) {
682 wd = (i + ctl->weekstart) % DAYS_IN_WEEK;
683 ctl->weekdays[i] = nl_langinfo(ABDAY_1 + wd);
684 }
685 }
686 static void headers_init(struct cal_control *ctl)
687 {
688 size_t i;
689 char *cur_dh = day_headings;
690 char tmp[FMT_ST_CHARS];
691 int year_len;
692
693 year_len = snprintf(tmp, sizeof(tmp), "%04d", ctl->req.year);
694
695 if (year_len < 0 || (size_t)year_len >= sizeof(tmp)) {
696 /* XXX impossible error */
697 return;
698 }
699
700 for (i = 0; i < DAYS_IN_WEEK; i++) {
701 size_t space_left;
702
703 if (i)
704 strcat(cur_dh++, " ");
705 space_left = sizeof(day_headings) - (cur_dh - day_headings);
706
707 if (space_left <= (ctl->day_width - 1))
708 break;
709 cur_dh += center_str(ctl->weekdays[i], cur_dh,
710 space_left, ctl->day_width - 1);
711 }
712
713 init_monthnames(ctl);
714
715 for (i = 0; i < MONTHS_IN_YEAR; i++) {
716 /* The +1 after year_len is space in between month and year. */
717 if (ctl->week_width < strlen(ctl->full_month[i]) + year_len)
718 ctl->header_hint = 1;
719 }
720 }
721
722 static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl)
723 {
724 int first_week_day = day_in_week(ctl, 1, month->month, month->year);
725 int month_days;
726 int i, j, weeklines = 0;
727
728 if (ctl->julian)
729 j = day_in_year(ctl, 1, month->month, month->year);
730 else
731 j = 1;
732 month_days = j + days_in_month[leap_year(ctl, month->year)][month->month];
733
734 /* True when Sunday is not first day in the output week. */
735 if (ctl->weekstart) {
736 first_week_day -= ctl->weekstart;
737 if (first_week_day < 0)
738 first_week_day = DAYS_IN_WEEK - ctl->weekstart;
739 month_days += ctl->weekstart - 1;
740 }
741
742 /* Fill day array. */
743 for (i = 0; i < MAXDAYS; i++) {
744 if (0 < first_week_day) {
745 month->days[i] = SPACE;
746 first_week_day--;
747 continue;
748 }
749 if (j < month_days) {
750 if (month->year == ctl->reform_year &&
751 month->month == REFORMATION_MONTH &&
752 (j == 3 || j == 247))
753 j += NUMBER_MISSING_DAYS;
754 month->days[i] = j;
755 j++;
756 continue;
757 }
758 month->days[i] = SPACE;
759 weeklines++;
760 }
761
762 /* Add week numbers */
763 if (ctl->weektype) {
764 int weeknum = week_number(1, month->month, month->year, ctl);
765 weeklines = MAXDAYS / DAYS_IN_WEEK - weeklines / DAYS_IN_WEEK;
766 for (i = 0; i < MAXDAYS / DAYS_IN_WEEK; i++) {
767 if (0 < weeklines) {
768 if (52 < weeknum)
769 weeknum = week_number(month->days[i * DAYS_IN_WEEK], month->month, month->year, ctl);
770 month->weeks[i] = weeknum++;
771 } else
772 month->weeks[i] = SPACE;
773 weeklines--;
774 }
775 }
776 }
777
778 static void cal_output_header(struct cal_month *month, const struct cal_control *ctl)
779 {
780 char out[FMT_ST_CHARS];
781 struct cal_month *i;
782
783 if (ctl->header_hint || ctl->header_year) {
784 for (i = month; i; i = i->next) {
785 snprintf(out, sizeof(out), "%s", ctl->full_month[i->month - 1]);
786 center(out, ctl->week_width, i->next == NULL ? 0 : ctl->gutter_width);
787 }
788 if (!ctl->header_year) {
789 my_putstring("\n");
790 for (i = month; i; i = i->next) {
791 snprintf(out, sizeof(out), "%04d", i->year);
792 center(out, ctl->week_width, i->next == NULL ? 0 : ctl->gutter_width);
793 }
794 }
795 } else {
796 for (i = month; i; i = i->next) {
797 snprintf(out, sizeof(out), "%s %04d", ctl->full_month[i->month - 1], i->year);
798 center(out, ctl->week_width, i->next == NULL ? 0 : ctl->gutter_width);
799 }
800 }
801 my_putstring("\n");
802 for (i = month; i; i = i->next) {
803 if (ctl->weektype) {
804 if (ctl->julian)
805 snprintf(out, sizeof(out), "%*s%s", (int)ctl->day_width - 1, "", day_headings);
806 else
807 snprintf(out, sizeof(out), "%*s%s", (int)ctl->day_width, "", day_headings);
808 my_putstring(out);
809 } else
810 my_putstring(day_headings);
811 if (i->next != NULL) {
812 snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
813 my_putstring(out);
814 }
815 }
816 my_putstring("\n");
817 }
818
819 static void cal_vert_output_header(struct cal_month *month,
820 const struct cal_control *ctl)
821 {
822 char out[FMT_ST_CHARS];
823 struct cal_month *m;
824 int month_width;
825
826 month_width = ctl->day_width * (MAXDAYS / DAYS_IN_WEEK);
827 /* Padding for the weekdays */
828 snprintf(out, sizeof(out), "%*s", (int)ctl->day_width + 1, "");
829 my_putstring(out);
830 if (ctl->header_hint || ctl->header_year) {
831 for (m = month; m; m = m->next) {
832 snprintf(out, sizeof(out), "%s", ctl->full_month[m->month - 1]);
833 left(out, month_width, ctl->gutter_width);
834 }
835 if (!ctl->header_year) {
836 my_putstring("\n");
837 /* Padding for the weekdays */
838 snprintf(out, sizeof(out), "%*s", (int)ctl->day_width + 1, "");
839 my_putstring(out);
840 for (m = month; m; m = m->next) {
841 snprintf(out, sizeof(out), "%04d", m->year);
842 left(out, month_width, ctl->gutter_width);
843 }
844 }
845 } else {
846 for (m = month; m; m = m->next) {
847 snprintf(out, sizeof(out), "%s %04d", ctl->full_month[m->month - 1], m->year);
848 left(out, month_width, ctl->gutter_width);
849 }
850 }
851 my_putstring("\n");
852 }
853
854 static void cal_output_months(struct cal_month *month, const struct cal_control *ctl)
855 {
856 char out[FMT_ST_CHARS];
857 int reqday, week_line, d;
858 int skip;
859 struct cal_month *i;
860
861 for (week_line = 0; week_line < MAXDAYS / DAYS_IN_WEEK; week_line++) {
862 for (i = month; i; i = i->next) {
863 /* Determine the day that should be highlighted. */
864 reqday = 0;
865 if (i->month == ctl->req.month && i->year == ctl->req.year) {
866 if (ctl->julian)
867 reqday = ctl->req.day;
868 else
869 reqday = ctl->req.day + 1 -
870 day_in_year(ctl, 1, i->month,
871 i->year);
872 }
873
874 if (ctl->weektype) {
875 if (0 < i->weeks[week_line]) {
876 if ((ctl->weektype & WEEK_NUM_MASK) ==
877 i->weeks[week_line])
878 snprintf(out, sizeof(out), "%s%2d%s",
879 Senter, i->weeks[week_line],
880 Sexit);
881 else
882 snprintf(out, sizeof(out), "%2d", i->weeks[week_line]);
883 } else
884 snprintf(out, sizeof(out), "%2s", "");
885 my_putstring(out);
886 skip = ctl->day_width;
887 } else
888 /* First day of the week is one char narrower than the other days,
889 * unless week number is printed. */
890 skip = ctl->day_width - 1;
891
892 for (d = DAYS_IN_WEEK * week_line;
893 d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
894 if (0 < i->days[d]) {
895 if (reqday == i->days[d])
896 snprintf(out, sizeof(out), "%*s%s%*d%s",
897 skip - (ctl->julian ? 3 : 2),
898 "", Senter, (ctl->julian ? 3 : 2),
899 i->days[d], Sexit);
900 else
901 snprintf(out, sizeof(out), "%*d", skip, i->days[d]);
902 } else
903 snprintf(out, sizeof(out), "%*s", skip, "");
904 my_putstring(out);
905 if (skip < (int)ctl->day_width)
906 skip++;
907 }
908 if (i->next != NULL) {
909 snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
910 my_putstring(out);
911 }
912 }
913 if (i == NULL)
914 my_putstring("\n");
915 }
916 }
917
918 static void
919 cal_vert_output_months(struct cal_month *month, const struct cal_control *ctl)
920 {
921 char out[FMT_ST_CHARS];
922 int i, reqday, week, d;
923 int skip;
924 struct cal_month *m;
925
926 skip = ctl->day_width;
927 for (i = 0; i < DAYS_IN_WEEK; i++) {
928 left(ctl->weekdays[i], ctl->day_width - 1, 0);
929 for (m = month; m; m = m->next) {
930 reqday = 0;
931 if (m->month == ctl->req.month && m->year == ctl->req.year) {
932 if (ctl->julian) {
933 reqday = ctl->req.day;
934 } else {
935 reqday = ctl->req.day + 1 -
936 day_in_year(ctl, 1, m->month, m->year);
937 }
938 }
939 for (week = 0; week < MAXDAYS / DAYS_IN_WEEK; week++) {
940 d = i + DAYS_IN_WEEK * week;
941 if (0 < m->days[d]) {
942 if (reqday == m->days[d]) {
943 snprintf(out, sizeof(out), "%*s%s%*d%s",
944 skip - (ctl->julian ? 3 : 2),
945 "", Senter, (ctl->julian ? 3 : 2),
946 m->days[d], Sexit);
947 } else {
948 snprintf(out, sizeof(out), "%*d",
949 skip, m->days[d]);
950 }
951 } else {
952 snprintf(out, sizeof(out), "%*s",
953 skip, "");
954 }
955 my_putstring(out);
956 skip = ctl->day_width;
957 }
958 if (m->next != NULL) {
959 snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
960 my_putstring(out);
961 }
962 }
963 my_putstring("\n");
964 }
965 if (!ctl->weektype)
966 return;
967
968 snprintf(out, sizeof(out), "%*s", (int)ctl->day_width - 1, "");
969 my_putstring(out);
970 for (m = month; m; m = m->next) {
971 for (week = 0; week < MAXDAYS / DAYS_IN_WEEK; week++) {
972 if (0 < m->weeks[week]) {
973 if ((ctl->weektype & WEEK_NUM_MASK) ==
974 m->weeks[week])
975 {
976 snprintf(out, sizeof(out), "%s%*d%s",
977 Senter,
978 skip - (ctl->julian ? 3 : 2),
979 m->weeks[week], Sexit);
980 } else {
981 snprintf(out, sizeof(out), "%*d",
982 skip, m->weeks[week]);
983 }
984 } else {
985 snprintf(out, sizeof(out), "%*s", skip, "");
986 }
987 my_putstring(out);
988 }
989 if (m->next != NULL) {
990 snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
991 my_putstring(out);
992 }
993 }
994 my_putstring("\n");
995 }
996
997
998 static void monthly(const struct cal_control *ctl)
999 {
1000 struct cal_month m1,m2,m3, *m;
1001 int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
1002 int32_t year = ctl->req.year;
1003
1004 /* cal -3, cal -Y --span, etc. */
1005 if (ctl->span_months) {
1006 int new_month = month - ctl->num_months / 2;
1007 if (new_month < 1) {
1008 new_month *= -1;
1009 year -= (new_month / MONTHS_IN_YEAR) + 1;
1010
1011 if (new_month > MONTHS_IN_YEAR)
1012 new_month %= MONTHS_IN_YEAR;
1013 month = MONTHS_IN_YEAR - new_month;
1014 } else
1015 month = new_month;
1016 }
1017
1018 m1.next = (ctl->months_in_row > 1) ? &m2 : NULL;
1019 m2.next = (ctl->months_in_row > 2) ? &m3 : NULL;
1020 m3.next = NULL;
1021
1022 rows = (ctl->num_months - 1) / ctl->months_in_row;
1023 for (i = 0; i < rows + 1 ; i++){
1024 if (i == rows){
1025 switch (ctl->num_months % ctl->months_in_row){
1026 case 1:
1027 m1.next = NULL;
1028 /* fallthrough */
1029 case 2:
1030 m2.next = NULL;
1031 /* fallthrough */
1032 }
1033 }
1034 for (m = &m1; m; m = m->next){
1035 m->month = month++;
1036 m->year = year;
1037 if (MONTHS_IN_YEAR < month) {
1038 year++;
1039 month = 1;
1040 }
1041 cal_fill_month(m, ctl);
1042 }
1043 if (ctl->vertical) {
1044 if (i > 0) {
1045 /* Add a line between row */
1046 my_putstring("\n");
1047 }
1048 cal_vert_output_header(&m1, ctl);
1049 cal_vert_output_months(&m1, ctl);
1050 } else {
1051 cal_output_header(&m1, ctl);
1052 cal_output_months(&m1, ctl);
1053 }
1054 }
1055 }
1056
1057 static void yearly(const struct cal_control *ctl)
1058 {
1059 char out[FMT_ST_CHARS];
1060 size_t year_width;
1061
1062 year_width = (size_t) ctl->months_in_row * ctl->week_width
1063 + ((size_t) ctl->months_in_row - 1) * ctl->gutter_width;
1064
1065 if (ctl->header_year) {
1066 snprintf(out, sizeof(out), "%04d", ctl->req.year);
1067 center(out, year_width, 0);
1068 my_putstring("\n\n");
1069 }
1070 monthly(ctl);
1071 }
1072
1073 /*
1074 * day_in_year --
1075 * return the 1 based day number within the year
1076 */
1077 static int day_in_year(const struct cal_control *ctl,
1078 int day, int month, int32_t year)
1079 {
1080 int i, leap;
1081
1082 leap = leap_year(ctl, year);
1083 for (i = 1; i < month; i++)
1084 day += days_in_month[leap][i];
1085 return day;
1086 }
1087
1088 /*
1089 * day_in_week
1090 * return the 0 based day number for any date from 1 Jan. 1 to
1091 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
1092 * 3 Sep. 1752 through 13 Sep. 1752, and returns invalid weekday
1093 * during the period of 11 days.
1094 */
1095 static int day_in_week(const struct cal_control *ctl, int day,
1096 int month, int32_t year)
1097 {
1098 /*
1099 * The magic constants in the reform[] array are, in a simplified
1100 * sense, the remaining days after slicing into one week periods the total
1101 * days from the beginning of the year to the target month. That is,
1102 * weeks + reform[] days gets us to the target month. The exception is,
1103 * that for the months past February 'DOY - 1' must be used.
1104 *
1105 * DoY (Day of Year): total days to the target month
1106 *
1107 * Month 1 2 3 4 5 6 7 8 9 10 11 12
1108 * DoY 0 31 59 90 120 151 181 212 243 273 304 334
1109 * DoY % 7 0 3
1110 * DoY - 1 % 7 - -- 2 5 0 3 5 1 4 6 2 4
1111 * reform[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
1112 *
1113 * Note: these calculations are for non leap years.
1114 */
1115 static const int reform[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
1116 static const int old[] = { 5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2 };
1117
1118 if (year != ctl->reform_year + 1)
1119 year -= month < MARCH;
1120 else
1121 year -= (month < MARCH) + 14;
1122 if (ctl->reform_year < year
1123 || (year == ctl->reform_year && REFORMATION_MONTH < month)
1124 || (year == ctl->reform_year
1125 && month == REFORMATION_MONTH && 13 < day)) {
1126 return ((int64_t) year + (year / 4)
1127 - (year / 100) + (year / 400)
1128 + reform[month - 1] + day) % DAYS_IN_WEEK;
1129 }
1130 if (year < ctl->reform_year
1131 || (year == ctl->reform_year && month < REFORMATION_MONTH)
1132 || (year == ctl->reform_year && month == REFORMATION_MONTH && day < 3))
1133 return ((int64_t) year + year / 4 + old[month - 1] + day)
1134 % DAYS_IN_WEEK;
1135 return NONEDAY;
1136 }
1137
1138 /*
1139 * week_number
1140 * return the week number of a given date, 1..54.
1141 * Supports ISO-8601 and North American modes.
1142 * Day may be given as Julian day of the year mode, in which
1143 * case the month is disregarded entirely.
1144 */
1145 static int week_number(int day, int month, int32_t year, const struct cal_control *ctl)
1146 {
1147 int fday = 0, yday;
1148 const int wday = day_in_week(ctl, 1, JANUARY, year);
1149
1150 if (ctl->weektype & WEEK_NUM_ISO)
1151 fday = wday + (wday >= FRIDAY ? -2 : 5);
1152 else {
1153 /* WEEK_NUM_US: Jan 1 is always First week, that may
1154 * begin previous year. That means there is very seldom
1155 * more than 52 weeks, */
1156 fday = wday + 6;
1157 }
1158 /* For julian dates the month can be set to 1, the global julian
1159 * variable cannot be relied upon here, because we may recurse
1160 * internally for 31.12. which would not work. */
1161 if (day > DAYS_IN_MONTH)
1162 month = JANUARY;
1163
1164 yday = day_in_year(ctl, day, month, year);
1165 if (year == ctl->reform_year && yday >= YDAY_AFTER_MISSING)
1166 fday -= NUMBER_MISSING_DAYS;
1167
1168 /* Last year is last year */
1169 if (yday + fday < DAYS_IN_WEEK)
1170 return week_number(31, DECEMBER, year - 1, ctl);
1171
1172 /* Or it could be part of the next year. The reformation year had less
1173 * days than 365 making this check invalid, but reformation year ended
1174 * on Sunday and in week 51, so it's ok here. */
1175 if (ctl->weektype == WEEK_NUM_ISO && yday >= 363
1176 && day_in_week(ctl, day, month, year) >= MONDAY
1177 && day_in_week(ctl, day, month, year) <= WEDNESDAY
1178 && day_in_week(ctl, 31, DECEMBER, year) >= MONDAY
1179 && day_in_week(ctl, 31, DECEMBER, year) <= WEDNESDAY)
1180 return week_number(1, JANUARY, year + 1, ctl);
1181
1182 return (yday + fday) / DAYS_IN_WEEK;
1183 }
1184
1185 /*
1186 * week_to_day
1187 * return the yday of the first day in a given week inside
1188 * the given year. This may be something other than Monday
1189 * for ISO-8601 modes. For North American numbering this
1190 * always returns a Sunday.
1191 */
1192 static int week_to_day(const struct cal_control *ctl)
1193 {
1194 int yday, wday;
1195
1196 wday = day_in_week(ctl, 1, JANUARY, ctl->req.year);
1197 yday = ctl->req.week * DAYS_IN_WEEK - wday;
1198
1199 if (ctl->req.year == ctl->reform_year && yday >= YDAY_AFTER_MISSING)
1200 yday += NUMBER_MISSING_DAYS;
1201
1202 if (ctl->weektype & WEEK_NUM_ISO)
1203 yday -= (wday >= FRIDAY ? -2 : 5);
1204 else
1205 yday -= 6; /* WEEK_NUM_US */
1206 if (yday <= 0)
1207 return 1;
1208
1209 return yday;
1210 }
1211
1212 /*
1213 * Center string, handling multibyte characters appropriately.
1214 * In addition if the string is too large for the width it's truncated.
1215 * The number of trailing spaces may be 1 less than the number of leading spaces.
1216 */
1217 static int center_str(const char* src, char* dest,
1218 size_t dest_size, size_t width)
1219 {
1220 return mbsalign(src, dest, dest_size, &width,
1221 MBS_ALIGN_CENTER, MBA_UNIBYTE_FALLBACK);
1222 }
1223
1224 static void center(const char *str, size_t len, int separate)
1225 {
1226 char lineout[FMT_ST_CHARS];
1227
1228 center_str(str, lineout, ARRAY_SIZE(lineout), len);
1229 my_putstring(lineout);
1230
1231 if (separate) {
1232 snprintf(lineout, sizeof(lineout), "%*s", separate, "");
1233 my_putstring(lineout);
1234 }
1235 }
1236 static int left_str(const char* src, char* dest,
1237 size_t dest_size, size_t width)
1238 {
1239 return mbsalign(src, dest, dest_size, &width,
1240 MBS_ALIGN_LEFT, MBA_UNIBYTE_FALLBACK);
1241 }
1242
1243 static void left(const char *str, size_t len, int separate)
1244 {
1245 char lineout[FMT_ST_CHARS];
1246
1247 left_str(str, lineout, sizeof(lineout), len);
1248 my_putstring(lineout);
1249
1250 if (separate) {
1251 snprintf(lineout, sizeof(lineout), "%*s", separate, "");
1252 my_putstring(lineout);
1253 }
1254 }
1255 static int parse_reform_year(const char *reform_year)
1256 {
1257 size_t i;
1258
1259 struct reform {
1260 char *name;
1261 int val;
1262 };
1263
1264 struct reform years[] = {
1265 {"gregorian", GREGORIAN},
1266 {"iso", ISO},
1267 {"1752", GB1752},
1268 {"julian", JULIAN},
1269 };
1270
1271 for (i = 0; i < ARRAY_SIZE(years); i++) {
1272 if (strcasecmp(reform_year, years[i].name) == 0) {
1273 return years[i].val;
1274 }
1275 }
1276 errx(EXIT_FAILURE, "invalid --reform value: '%s'", reform_year);
1277 }
1278
1279 static void __attribute__((__noreturn__)) usage(void)
1280 {
1281 FILE *out = stdout;
1282 fputs(USAGE_HEADER, out);
1283 fprintf(out, _(" %s [options] [[[day] month] year]\n"), program_invocation_short_name);
1284 fprintf(out, _(" %s [options] <timestamp|monthname>\n"), program_invocation_short_name);
1285
1286 fputs(USAGE_SEPARATOR, out);
1287 fputs(_("Display a calendar, or some part of it.\n"), out);
1288 fputs(_("Without any arguments, display the current month.\n"), out);
1289
1290 fputs(USAGE_OPTIONS, out);
1291 fputs(_(" -1, --one show only a single month (default)\n"), out);
1292 fputs(_(" -3, --three show three months spanning the date\n"), out);
1293 fputs(_(" -n, --months <num> show num months starting with date's month\n"), out);
1294 fputs(_(" -S, --span span the date when displaying multiple months\n"), out);
1295 fputs(_(" -s, --sunday Sunday as first day of week\n"), out);
1296 fputs(_(" -m, --monday Monday as first day of week\n"), out);
1297 fputs(_(" -j, --julian use day-of-year for all calendars\n"), out);
1298 fputs(_(" --reform <val> Gregorian reform date (1752|gregorian|iso|julian)\n"), out);
1299 fputs(_(" --iso alias for --reform=iso\n"), out);
1300 fputs(_(" -y, --year show the whole year\n"), out);
1301 fputs(_(" -Y, --twelve show the next twelve months\n"), out);
1302 fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out);
1303 fputs(_(" -v, --vertical show day vertically instead of line\n"), out);
1304 fprintf(out,
1305 _(" --color[=<when>] colorize messages (%s, %s or %s)\n"), "auto", "always", "never");
1306 fprintf(out,
1307 " %s\n", USAGE_COLORS_DEFAULT);
1308
1309 fputs(USAGE_SEPARATOR, out);
1310 printf(USAGE_HELP_OPTIONS(23));
1311 printf(USAGE_MAN_TAIL("cal(1)"));
1312
1313 exit(EXIT_SUCCESS);
1314 }