]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/cal.c
docs: fix typos [codespell]
[thirdparty/util-linux.git] / misc-utils / cal.c
CommitLineData
6dbe3af9
KZ
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
5c36a0eb 37/* 1999-02-01 Jean-Francois Bignolles: added option '-m' to display
d162fcb5 38 * monday as the first day of the week.
b50945d4 39 * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
7eda085c
KZ
40 * - added Native Language Support
41 *
d162fcb5 42 * 2000-09-01 Michael Charles Pruznick <dummy@netwiz.net>
66ee8158 43 * Added "-3" option to print prev/next month with current.
9e930041 44 * Added overridable default MONTHS_IN_ROW and "-1" option to
66ee8158
KZ
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.
e8f26419
KZ
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
5c36a0eb
KZ
56 */
57
6dbe3af9
KZ
58#include <sys/types.h>
59
60#include <ctype.h>
022df321 61#include <getopt.h>
a79735f6 62#include <stdint.h>
6dbe3af9
KZ
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <time.h>
67#include <unistd.h>
37d6897f 68#include <errno.h>
17282538
KZ
69
70#include "c.h"
c05a80ca 71#include "closestream.h"
7b353df2 72#include "colors.h"
7eda085c 73#include "nls.h"
104b92f8 74#include "mbsalign.h"
022df321 75#include "strutils.h"
7800509b 76#include "optutils.h"
cd28d6a4 77#include "timeutils.h"
8315a2ff
KZ
78#include "ttyutils.h"
79
80#define DOY_MONTH_WIDTH 27 /* -j month width */
81#define DOM_MONTH_WIDTH 20 /* month width */
726f69e2 82
2a4b073e
KZ
83static int has_term = 0;
84static const char *Senter = "", *Sexit = ""; /* enter and exit standout mode */
85
310170b8 86#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
3947ca4c
KZ
87# if defined(HAVE_NCURSESW_TERM_H)
88# include <ncursesw/term.h>
18451b2c
KZ
89# elif defined(HAVE_NCURSES_TERM_H)
90# include <ncurses/term.h>
3947ca4c
KZ
91# elif defined(HAVE_TERM_H)
92# include <term.h>
4aefe5e8 93# endif
b4566a8a 94#endif
4aefe5e8 95
58149472
KZ
96static int setup_terminal(char *term
97#if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
98 __attribute__((__unused__))
99#endif
100 )
4aefe5e8 101{
b4566a8a 102#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
2a4b073e
KZ
103 int ret;
104
18451b2c 105 if (setupterm(term, STDOUT_FILENO, &ret) != 0 || ret != 1)
2a4b073e 106 return -1;
b4566a8a 107#endif
2a4b073e 108 return 0;
d162fcb5
KZ
109}
110
4aefe5e8
SK
111static void my_putstring(char *s)
112{
b4566a8a 113#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
2a4b073e
KZ
114 if (has_term)
115 putp(s);
116 else
b4566a8a 117#endif
2a4b073e 118 fputs(s, stdout);
d162fcb5
KZ
119}
120
58149472 121static const char *my_tgetstr(char *ss
18451b2c 122#if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
58149472
KZ
123 __attribute__((__unused__))
124#endif
125 )
4aefe5e8 126{
2a4b073e
KZ
127 const char *ret = NULL;
128
b4566a8a 129#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
2a4b073e
KZ
130 if (has_term)
131 ret = tigetstr(ss);
b4566a8a 132#endif
4aefe5e8
SK
133 if (!ret || ret == (char *)-1)
134 return "";
d4be073d 135 return ret;
d162fcb5
KZ
136}
137
e8f26419 138#include "widechar.h"
95f1bdee 139
ae51e7b4
WP
140enum {
141 GREGORIAN = INT32_MIN,
142 ISO = INT32_MIN,
143 GB1752 = 1752,
144 DEFAULT_REFORM_YEAR = 1752,
145 JULIAN = INT32_MAX
146};
147
5f845cb7
SK
148enum {
149 SUNDAY = 0,
150 MONDAY,
151 TUESDAY,
152 WEDNESDAY,
153 THURSDAY,
154 FRIDAY,
155 SATURDAY,
d68f076f
SK
156 DAYS_IN_WEEK,
157 NONEDAY
5f845cb7 158};
6dbe3af9 159
e1abe57e
SK
160enum {
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
e1abe57e 175#define REFORMATION_MONTH SEPTEMBER
d162fcb5 176#define NUMBER_MISSING_DAYS 11 /* 11 day correction */
c36c4a4e 177#define YDAY_AFTER_MISSING 258 /* 14th in Sep 1752 */
6dbe3af9 178
e1abe57e 179#define MONTHS_IN_YEAR DECEMBER
5f845cb7 180#define DAYS_IN_MONTH 31
f06602a4 181#define MAXDAYS 42 /* slots in a month array */
6dbe3af9
KZ
182#define SPACE -1 /* used in day array */
183
5f845cb7 184#define SMALLEST_YEAR 1
5f845cb7
SK
185
186#define DAY_LEN 3 /* 3 spaces per day */
187#define WEEK_LEN (DAYS_IN_WEEK * DAY_LEN)
7800509b 188#define MONTHS_IN_YEAR_ROW 3 /* month columns in year view */
aae4f87e 189#define WNUM_LEN 3
5f845cb7 190
5f845cb7 191#define FMT_ST_CHARS 300 /* 90 suffices in most locales */
5f845cb7 192
1beb933e 193static const int days_in_month[2][13] = {
6dbe3af9
KZ
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
c36c4a4e
TK
198enum {
199 WEEK_NUM_DISABLED = 0,
aae4f87e
TK
200 WEEK_NUM_MASK=0xff,
201 WEEK_NUM_ISO=0x100,
202 WEEK_NUM_US=0x200,
c36c4a4e 203};
e8f26419
KZ
204
205/* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
71ff238e 206static char day_headings[(WEEK_LEN + 1) * 6 + 1];
6dbe3af9 207
ffc56357
SK
208struct cal_request {
209 int day;
210 int month;
4a7424a5 211 int32_t year;
ffc56357 212 int week;
7800509b 213 int start_month;
ffc56357
SK
214};
215
b549058b
SK
216struct cal_control {
217 const char *full_month[MONTHS_IN_YEAR]; /* month names */
c8805632
KZ
218 const char *abbr_month[MONTHS_IN_YEAR]; /* abbreviated month names */
219
db9baa8a 220 int reform_year; /* Gregorian reform year */
b549058b 221 int colormode; /* day and week number highlight */
12f17a23 222 int num_months; /* number of requested months */
d4cb626d 223 int span_months; /* span the date */
7800509b 224 int months_in_row; /* number of months horizontally in print out */
b549058b
SK
225 int weekstart; /* day the week starts, often Sun or Mon */
226 int weektype; /* WEEK_TYPE_{NONE,ISO,US} */
71ff238e
SK
227 size_t day_width; /* day width in characters in printout */
228 size_t week_width; /* 7 * day_width + possible week num */
95f4adde 229 int gutter_width; /* spaces in between horizontal month outputs */
ffc56357 230 struct cal_request req; /* the times user is interested */
b549058b 231 unsigned int julian:1, /* julian output */
7800509b 232 header_year:1, /* print year number */
efce94ec 233 header_hint:1; /* does month name + year need two lines to fit */
b549058b 234};
6dbe3af9 235
852c8d21
SK
236struct cal_month {
237 int days[MAXDAYS]; /* the day numbers, or SPACE */
238 int weeks[MAXDAYS / DAYS_IN_WEEK];
239 int month;
4a7424a5 240 int32_t year;
852c8d21
SK
241 struct cal_month *next;
242};
243
5f845cb7 244/* function prototypes */
db9baa8a 245static int leap_year(const struct cal_control *ctl, int32_t year);
731441ac 246static int monthname_to_number(struct cal_control *ctl, const char *name);
b549058b 247static void headers_init(struct cal_control *ctl);
852c8d21
SK
248static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl);
249static void cal_output_header(struct cal_month *month, const struct cal_control *ctl);
250static void cal_output_months(struct cal_month *month, const struct cal_control *ctl);
ffc56357 251static void monthly(const struct cal_control *ctl);
ffc56357 252static void yearly(const struct cal_control *ctl);
db9baa8a
WP
253static int day_in_year(const struct cal_control *ctl, int day,
254 int month, int32_t year);
255static int day_in_week(const struct cal_control *ctl, int day,
256 int month, int32_t year);
4a7424a5 257static int week_number(int day, int month, int32_t year, const struct cal_control *ctl);
ffc56357 258static int week_to_day(const struct cal_control *ctl);
0106c9e2
SK
259static int center_str(const char *src, char *dest, size_t dest_size, size_t width);
260static void center(const char *str, size_t len, int separate);
ae51e7b4 261static int parse_reform_year(const char *reform_year);
6e1eda6f 262static void __attribute__((__noreturn__)) usage(void);
6dbe3af9 263
b9d9ab7d
KZ
264#ifdef TEST_CAL
265static time_t cal_time(time_t *t)
266{
267 char *str = getenv("CAL_TEST_TIME");
268
269 if (str) {
270 uint64_t x = strtou64_or_err(str, "failed to parse CAL_TEST_TIME");
271
272 *t = x;
273 return *t;
274 }
275
276 return time(t);
277}
278#else
279# define cal_time(t) time(t)
280#endif
281
d4be073d
KZ
282int main(int argc, char **argv)
283{
6dbe3af9 284 struct tm *local_time;
58149472 285 char *term;
6dbe3af9 286 time_t now;
7800509b 287 int ch = 0, yflag = 0, Yflag = 0;
db9baa8a 288
b549058b 289 static struct cal_control ctl = {
ae51e7b4 290 .reform_year = DEFAULT_REFORM_YEAR,
b549058b 291 .weekstart = SUNDAY,
d4cb626d 292 .span_months = 0,
d0c9ddc3 293 .colormode = UL_COLORMODE_UNDEF,
71ff238e 294 .weektype = WEEK_NUM_DISABLED,
ffc56357 295 .day_width = DAY_LEN,
95f4adde 296 .gutter_width = 2,
ffc56357
SK
297 .req.day = 0,
298 .req.month = 0
b549058b 299 };
7b353df2
SK
300
301 enum {
ae51e7b4
WP
302 OPT_COLOR = CHAR_MAX + 1,
303 OPT_ISO,
304 OPT_REFORM
7b353df2 305 };
eb63b9b8 306
022df321
SK
307 static const struct option longopts[] = {
308 {"one", no_argument, NULL, '1'},
309 {"three", no_argument, NULL, '3'},
310 {"sunday", no_argument, NULL, 's'},
311 {"monday", no_argument, NULL, 'm'},
312 {"julian", no_argument, NULL, 'j'},
7800509b 313 {"months", required_argument, NULL, 'n'},
ccf3dd50 314 {"span", no_argument, NULL, 'S'},
022df321 315 {"year", no_argument, NULL, 'y'},
aae4f87e 316 {"week", optional_argument, NULL, 'w'},
7b353df2 317 {"color", optional_argument, NULL, OPT_COLOR},
ae51e7b4
WP
318 {"reform", required_argument, NULL, OPT_REFORM},
319 {"iso", no_argument, NULL, OPT_ISO},
022df321 320 {"version", no_argument, NULL, 'V'},
7800509b 321 {"twelve", no_argument, NULL, 'Y'},
022df321
SK
322 {"help", no_argument, NULL, 'h'},
323 {NULL, 0, NULL, 0}
324 };
325
a7349ee3 326 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
7800509b
MK
327 { 'Y','n','y' },
328 { 0 }
329 };
330 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
331
7eda085c
KZ
332 setlocale(LC_ALL, "");
333 bindtextdomain(PACKAGE, LOCALEDIR);
334 textdomain(PACKAGE);
c05a80ca 335 atexit(close_stdout);
e8f26419 336
58149472
KZ
337 term = getenv("TERM");
338 if (term) {
339 has_term = setup_terminal(term) == 0;
340 if (has_term) {
341 Senter = my_tgetstr("smso");
342 Sexit = my_tgetstr("rmso");
d162fcb5
KZ
343 }
344 }
d162fcb5 345
612721db 346/*
0a9bead0
PB
347 * The traditional Unix cal utility starts the week at Sunday,
348 * while ISO 8601 starts at Monday. We read the start day from
349 * the locale database, which can be overridden with the
350 * -s (Sunday) or -m (Monday) options.
612721db 351 */
69cabd72 352#if HAVE_DECL__NL_TIME_WEEK_1STDAY
0a9bead0
PB
353 /*
354 * You need to use 2 locale variables to get the first day of the week.
355 * This is needed to support first_weekday=2 and first_workday=1 for
356 * the rare case where working days span across 2 weeks.
357 * This shell script shows the combinations and calculations involved:
33c17354
SK
358 *
359 * for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
360 * printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
361 * date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
362 * done
363 *
364 * en_US: 19971130 + 1 -1 = 0 #0 = sunday
365 * ru_RU: 19971130 + 2 -1 = 1
366 * fr_FR: 19971201 + 1 -1 = 1
367 * csb_PL: 19971201 + 2 -1 = 2
368 * POSIX: 19971201 + 7 -1 = 0
0a9bead0
PB
369 */
370 {
aebb9522
KZ
371 int wfd;
372 union { unsigned int word; char *string; } val;
373 val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
374
375 wfd = val.word;
db9baa8a
WP
376 wfd = day_in_week(&ctl, wfd % 100, (wfd / 100) % 100,
377 wfd / (100 * 100));
b549058b 378 ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
0a9bead0 379 }
e8f26419 380#endif
ccf3dd50 381 while ((ch = getopt_long(argc, argv, "13mjn:sSywYVh", longopts, NULL)) != -1) {
7800509b
MK
382
383 err_exclusive_options(ch, longopts, excl, excl_st);
d162fcb5 384
6dbe3af9 385 switch(ch) {
d162fcb5 386 case '1':
2bcf8f79 387 ctl.num_months = 1;
d162fcb5
KZ
388 break;
389 case '3':
b549058b 390 ctl.num_months = 3;
d4cb626d 391 ctl.span_months = 1;
d162fcb5 392 break;
ffc43748 393 case 's':
b549058b 394 ctl.weekstart = SUNDAY; /* default */
ffc43748 395 break;
5c36a0eb 396 case 'm':
b549058b 397 ctl.weekstart = MONDAY;
5c36a0eb 398 break;
6dbe3af9 399 case 'j':
b549058b 400 ctl.julian = 1;
71ff238e 401 ctl.day_width = DAY_LEN + 1;
6dbe3af9
KZ
402 break;
403 case 'y':
7800509b
MK
404 yflag = 1;
405 break;
406 case 'Y':
407 Yflag = 1;
408 break;
409 case 'n':
410 ctl.num_months = strtou32_or_err(optarg,
411 _("invalid month argument"));
6dbe3af9 412 break;
ccf3dd50
D
413 case 'S':
414 ctl.span_months = 1;
415 break;
c36c4a4e 416 case 'w':
aae4f87e 417 if (optarg) {
ffc56357 418 ctl.req.week = strtos32_or_err(optarg,
aae4f87e 419 _("invalid week argument"));
efafeaf6
SK
420 if (ctl.req.week < 1 || 54 < ctl.req.week)
421 errx(EXIT_FAILURE,_("illegal week value: use 1-54"));
aae4f87e 422 }
b549058b 423 ctl.weektype = WEEK_NUM_US; /* default per weekstart */
c36c4a4e 424 break;
7b353df2 425 case OPT_COLOR:
5d51dc2a 426 ctl.colormode = UL_COLORMODE_AUTO;
201b39f0 427 if (optarg)
b549058b 428 ctl.colormode = colormode_or_err(optarg,
201b39f0 429 _("unsupported color mode"));
7b353df2 430 break;
ae51e7b4
WP
431 case OPT_REFORM:
432 ctl.reform_year = parse_reform_year(optarg);
433 break;
434 case OPT_ISO:
435 ctl.reform_year = ISO;
436 break;
eb63b9b8 437 case 'V':
e421313d 438 printf(UTIL_LINUX_VERSION);
37d6897f 439 return EXIT_SUCCESS;
022df321 440 case 'h':
6e1eda6f 441 usage();
6dbe3af9 442 default:
677ec86c 443 errtryhelp(EXIT_FAILURE);
6dbe3af9 444 }
7800509b
MK
445 }
446
6dbe3af9
KZ
447 argc -= optind;
448 argv += optind;
449
b549058b 450 if (ctl.weektype) {
ffc56357 451 ctl.weektype = ctl.req.week & WEEK_NUM_MASK;
b549058b 452 ctl.weektype |= (ctl.weekstart == MONDAY ? WEEK_NUM_ISO : WEEK_NUM_US);
71ff238e
SK
453 ctl.week_width = (ctl.day_width * DAYS_IN_WEEK) + WNUM_LEN;
454 } else
455 ctl.week_width = ctl.day_width * DAYS_IN_WEEK;
c36c4a4e 456
cd28d6a4
KZ
457 if (argc == 1 && !isdigit_string(*argv)) {
458 usec_t x;
535fd6d2 459 /* cal <timestamp> */
cd28d6a4
KZ
460 if (parse_timestamp(*argv, &x) == 0)
461 now = (time_t) (x / 1000000);
535fd6d2
KZ
462 /* cal <monthname> */
463 else if ((ctl.req.month = monthname_to_number(&ctl, *argv)) > 0)
b9d9ab7d 464 cal_time(&now); /* this year */
cd28d6a4 465 else
535fd6d2 466 errx(EXIT_FAILURE, _("failed to parse timestamp or unknown month name: %s"), *argv);
cd28d6a4
KZ
467 argc = 0;
468 } else
b9d9ab7d 469 cal_time(&now);
cd28d6a4 470
a43145e1
RP
471 local_time = localtime(&now);
472
6dbe3af9 473 switch(argc) {
d7a92b89 474 case 3:
ffc56357
SK
475 ctl.req.day = strtos32_or_err(*argv++, _("illegal day value"));
476 if (ctl.req.day < 1 || DAYS_IN_MONTH < ctl.req.day)
5f845cb7 477 errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), DAYS_IN_MONTH);
b1557fe9 478 /* fallthrough */
6dbe3af9 479 case 2:
731441ac
KZ
480 if (isdigit(**argv))
481 ctl.req.month = strtos32_or_err(*argv++, _("illegal month value: use 1-12"));
c49fb9ca
KZ
482 else {
483 ctl.req.month = monthname_to_number(&ctl, *argv);
484 if (ctl.req.month < 0)
485 errx(EXIT_FAILURE, _("unknown month name: %s"), *argv);
486 argv++;
487 }
ffc56357 488 if (ctl.req.month < 1 || MONTHS_IN_YEAR < ctl.req.month)
022df321 489 errx(EXIT_FAILURE, _("illegal month value: use 1-12"));
b1557fe9 490 /* fallthrough */
6dbe3af9 491 case 1:
4a7424a5 492 ctl.req.year = strtos32_or_err(*argv++, _("illegal year value"));
ffc56357 493 if (ctl.req.year < SMALLEST_YEAR)
e44fe471 494 errx(EXIT_FAILURE, _("illegal year value: use positive integer"));
ae51e7b4 495 if (ctl.req.year == JULIAN)
4a7424a5 496 errx(EXIT_FAILURE, _("illegal year value"));
ffc56357 497 if (ctl.req.day) {
db9baa8a
WP
498 int dm = days_in_month[leap_year(&ctl, ctl.req.year)]
499 [ctl.req.month];
ffc56357 500 if (ctl.req.day > dm)
022df321 501 errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), dm);
db9baa8a
WP
502 ctl.req.day = day_in_year(&ctl, ctl.req.day,
503 ctl.req.month, ctl.req.year);
4a7424a5 504 } else if ((int32_t) (local_time->tm_year + 1900) == ctl.req.year) {
ffc56357 505 ctl.req.day = local_time->tm_yday + 1;
d7a92b89 506 }
95f4adde
SK
507 if (!ctl.req.month && !ctl.req.week) {
508 ctl.req.month = local_time->tm_mon + 1;
37bd991f
KZ
509 if (!ctl.num_months)
510 yflag = 1;
95f4adde 511 }
6dbe3af9
KZ
512 break;
513 case 0:
ffc56357
SK
514 ctl.req.day = local_time->tm_yday + 1;
515 ctl.req.year = local_time->tm_year + 1900;
535fd6d2
KZ
516 if (!ctl.req.month)
517 ctl.req.month = local_time->tm_mon + 1;
6dbe3af9
KZ
518 break;
519 default:
6e1eda6f
RM
520 warnx(_("bad usage"));
521 errtryhelp(EXIT_FAILURE);
6dbe3af9 522 }
aae4f87e 523
ffc56357
SK
524 if (0 < ctl.req.week) {
525 int yday = week_to_day(&ctl);
db9baa8a 526 int leap = leap_year(&ctl, ctl.req.year);
ffc56357 527 int m = 1;
c1732e62 528
aae4f87e 529 if (yday < 1)
4a7424a5 530 errx(EXIT_FAILURE, _("illegal week value: year %d "
c1732e62 531 "doesn't have week %d"),
ffc56357 532 ctl.req.year, ctl.req.week);
e1abe57e 533 while (m <= DECEMBER && yday > days_in_month[leap][m])
ffc56357 534 yday -= days_in_month[leap][m++];
03f8bc1f 535 if (DECEMBER < m && ctl.weektype & WEEK_NUM_ISO) {
c1732e62
KZ
536 /* In some years (e.g. 2010 in ISO mode) it's possible
537 * to have a remnant of week 53 starting the year yet
538 * the year in question ends during 52, in this case
539 * we're assuming that early remnant is being referred
540 * to if 53 is given as argument. */
e1abe57e 541 if (ctl.req.week != week_number(31, DECEMBER, ctl.req.year - 1, &ctl))
c1732e62 542 errx(EXIT_FAILURE,
4a7424a5 543 _("illegal week value: year %d "
c1732e62 544 "doesn't have week %d"),
ffc56357 545 ctl.req.year, ctl.req.week);
aae4f87e 546 }
ffc56357 547 if (!ctl.req.month)
7800509b 548 ctl.req.month = MONTHS_IN_YEAR < m ? 1 : m;
aae4f87e
TK
549 }
550
b549058b 551 headers_init(&ctl);
6dbe3af9 552
f90d8b8b
KZ
553 if (colors_init(ctl.colormode, "cal") == 0) {
554 /*
555 * If standout mode available (Senter and Sexit are set) and
556 * user or terminal-colors.d do not disable colors than
557 * ignore colors_init().
558 */
559 if (*Senter && *Sexit && colors_mode() != UL_COLORMODE_NEVER) {
560 /* let use standout mode */
561 ;
562 } else {
563 /* disable */
d135e1a1 564 Senter = ""; Sexit = "";
f90d8b8b
KZ
565 ctl.req.day = 0;
566 ctl.weektype &= ~WEEK_NUM_MASK;
567 }
aae4f87e 568 }
d7a92b89 569
7800509b 570 if (yflag || Yflag) {
95f4adde 571 ctl.gutter_width = 3;
2bcf8f79
WP
572 if (!ctl.num_months)
573 ctl.num_months = MONTHS_IN_YEAR;
7800509b
MK
574 if (yflag) {
575 ctl.req.start_month = 1; /* start from Jan */
576 ctl.header_year = 1; /* print year number */
577 }
578 }
579
8315a2ff
KZ
580 if (ctl.num_months > 1 && ctl.months_in_row == 0) {
581 ctl.months_in_row = MONTHS_IN_YEAR_ROW; /* default */
582
583 if (isatty(STDOUT_FILENO)) {
584 int w = get_terminal_width(STDOUT_FILENO);
585 int mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH;
586 int extra = ((w / mw) - 1) * ctl.gutter_width;
587 int new_n = (w - extra) / mw;
588
589 if (new_n < MONTHS_IN_YEAR_ROW)
590 ctl.months_in_row = new_n;
591 }
592 } else if (!ctl.months_in_row)
7800509b
MK
593 ctl.months_in_row = 1;
594
2bcf8f79
WP
595 if (!ctl.num_months)
596 ctl.num_months = 1; /* display at least one month */
597
7800509b 598 if (yflag || Yflag)
ffc56357 599 yearly(&ctl);
7800509b 600 else
ffc56357 601 monthly(&ctl);
37d6897f
KZ
602
603 return EXIT_SUCCESS;
6dbe3af9
KZ
604}
605
b9c65010 606/* leap year -- account for gregorian reformation in 1752 */
db9baa8a 607static int leap_year(const struct cal_control *ctl, int32_t year)
3b66dfd6 608{
db9baa8a 609 if (year <= ctl->reform_year)
b9c65010
KZ
610 return !(year % 4);
611 else
612 return ( !(year % 4) && (year % 100) ) || !(year % 400);
3b66dfd6
SK
613}
614
731441ac
KZ
615static void init_monthnames(struct cal_control *ctl)
616{
617 size_t i;
618
87918040 619 if (ctl->full_month[0] != NULL)
731441ac
KZ
620 return; /* already initialized */
621
622 for (i = 0; i < MONTHS_IN_YEAR; i++)
4e12a62e 623 ctl->full_month[i] = nl_langinfo(ALTMON_1 + i);
731441ac
KZ
624}
625
c8805632
KZ
626static void init_abbr_monthnames(struct cal_control *ctl)
627{
628 size_t i;
629
87918040 630 if (ctl->abbr_month[0] != NULL)
c8805632
KZ
631 return; /* already initialized */
632
633 for (i = 0; i < MONTHS_IN_YEAR; i++)
4e12a62e 634 ctl->abbr_month[i] = nl_langinfo(_NL_ABALTMON_1 + i);
c8805632
KZ
635}
636
731441ac
KZ
637static int monthname_to_number(struct cal_control *ctl, const char *name)
638{
639 size_t i;
640
641 init_monthnames(ctl);
731441ac
KZ
642 for (i = 0; i < MONTHS_IN_YEAR; i++)
643 if (strcasecmp(ctl->full_month[i], name) == 0)
644 return i + 1;
645
c8805632
KZ
646 init_abbr_monthnames(ctl);
647 for (i = 0; i < MONTHS_IN_YEAR; i++)
648 if (strcasecmp(ctl->abbr_month[i], name) == 0)
649 return i + 1;
650
c49fb9ca 651 return -EINVAL;
731441ac
KZ
652}
653
b549058b 654static void headers_init(struct cal_control *ctl)
6dbe3af9 655{
71ff238e 656 size_t i, wd;
b283141c 657 char *cur_dh = day_headings;
efce94ec 658 char tmp[FMT_ST_CHARS];
06fa5817 659 int year_len;
efce94ec 660
e671a62e 661 year_len = snprintf(tmp, sizeof(tmp), "%04d", ctl->req.year);
33c17354 662
06fa5817
YK
663 if (year_len < 0 || (size_t)year_len >= sizeof(tmp)) {
664 /* XXX impossible error */
665 return;
666 }
667
5f845cb7 668 for (i = 0; i < DAYS_IN_WEEK; i++) {
add1924d 669 size_t space_left;
b549058b 670 wd = (i + ctl->weekstart) % DAYS_IN_WEEK;
33c17354
SK
671
672 if (i)
673 strcat(cur_dh++, " ");
add1924d
KZ
674 space_left = sizeof(day_headings) - (cur_dh - day_headings);
675
71ff238e 676 if (space_left <= (ctl->day_width - 1))
33c17354 677 break;
add1924d 678 cur_dh += center_str(nl_langinfo(ABDAY_1 + wd), cur_dh,
71ff238e 679 space_left, ctl->day_width - 1);
33c17354
SK
680 }
681
731441ac
KZ
682 init_monthnames(ctl);
683
efce94ec 684 for (i = 0; i < MONTHS_IN_YEAR; i++) {
efce94ec
SK
685 /* The +1 after year_len is space in between month and year. */
686 if (ctl->week_width < strlen(ctl->full_month[i]) + year_len + 1)
687 ctl->header_hint = 1;
688 }
6dbe3af9
KZ
689}
690
852c8d21 691static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl)
d4be073d 692{
db9baa8a 693 int first_week_day = day_in_week(ctl, 1, month->month, month->year);
852c8d21
SK
694 int month_days;
695 int i, j, weeklines = 0;
696
697 if (ctl->julian)
db9baa8a 698 j = day_in_year(ctl, 1, month->month, month->year);
852c8d21
SK
699 else
700 j = 1;
db9baa8a 701 month_days = j + days_in_month[leap_year(ctl, month->year)][month->month];
852c8d21
SK
702
703 /* True when Sunday is not first day in the output week. */
704 if (ctl->weekstart) {
705 first_week_day -= ctl->weekstart;
706 if (first_week_day < 0)
707 first_week_day = DAYS_IN_WEEK - ctl->weekstart;
708 month_days += ctl->weekstart - 1;
709 }
710
711 /* Fill day array. */
712 for (i = 0; i < MAXDAYS; i++) {
713 if (0 < first_week_day) {
714 month->days[i] = SPACE;
715 first_week_day--;
716 continue;
717 }
718 if (j < month_days) {
db9baa8a
WP
719 if (month->year == ctl->reform_year &&
720 month->month == REFORMATION_MONTH &&
721 (j == 3 || j == 247))
852c8d21
SK
722 j += NUMBER_MISSING_DAYS;
723 month->days[i] = j;
724 j++;
725 continue;
726 }
727 month->days[i] = SPACE;
728 weeklines++;
729 }
66ee8158 730
852c8d21
SK
731 /* Add week numbers */
732 if (ctl->weektype) {
733 int weeknum = week_number(1, month->month, month->year, ctl);
734 weeklines = MAXDAYS / DAYS_IN_WEEK - weeklines / DAYS_IN_WEEK;
735 for (i = 0; i < MAXDAYS / DAYS_IN_WEEK; i++) {
efafeaf6
SK
736 if (0 < weeklines) {
737 if (52 < weeknum)
738 weeknum = week_number(month->days[i * DAYS_IN_WEEK], month->month, month->year, ctl);
852c8d21 739 month->weeks[i] = weeknum++;
efafeaf6 740 } else
852c8d21
SK
741 month->weeks[i] = SPACE;
742 weeklines--;
852c8d21 743 }
66ee8158
KZ
744 }
745}
746
852c8d21
SK
747static void cal_output_header(struct cal_month *month, const struct cal_control *ctl)
748{
749 char out[FMT_ST_CHARS];
750 struct cal_month *i;
751
7800509b 752 if (ctl->header_hint || ctl->header_year) {
852c8d21 753 for (i = month; i; i = i->next) {
100a1406 754 snprintf(out, sizeof(out), "%s", ctl->full_month[i->month - 1]);
95f4adde 755 center(out, ctl->week_width - 1, i->next == NULL ? 0 : ctl->gutter_width);
852c8d21 756 }
7800509b 757 if (!ctl->header_year) {
07ac4aa9 758 my_putstring("\n");
95f4adde 759 for (i = month; i; i = i->next) {
100a1406 760 snprintf(out, sizeof(out), "%04d", i->year);
95f4adde
SK
761 center(out, ctl->week_width - 1, i->next == NULL ? 0 : ctl->gutter_width);
762 }
852c8d21
SK
763 }
764 } else {
765 for (i = month; i; i = i->next) {
100a1406 766 snprintf(out, sizeof(out), "%s %04d", ctl->full_month[i->month - 1], i->year);
95f4adde 767 center(out, ctl->week_width - 1, i->next == NULL ? 0 : ctl->gutter_width);
852c8d21
SK
768 }
769 }
07ac4aa9 770 my_putstring("\n");
852c8d21
SK
771 for (i = month; i; i = i->next) {
772 if (ctl->weektype) {
773 if (ctl->julian)
100a1406 774 snprintf(out, sizeof(out), "%*s%s", (int)ctl->day_width - 1, "", day_headings);
852c8d21 775 else
100a1406 776 snprintf(out, sizeof(out), "%*s%s", (int)ctl->day_width, "", day_headings);
07ac4aa9 777 my_putstring(out);
852c8d21 778 } else
07ac4aa9
RM
779 my_putstring(day_headings);
780 if (i->next != NULL) {
100a1406 781 snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
07ac4aa9
RM
782 my_putstring(out);
783 }
852c8d21 784 }
07ac4aa9 785 my_putstring("\n");
852c8d21
SK
786}
787
788static void cal_output_months(struct cal_month *month, const struct cal_control *ctl)
789{
07ac4aa9 790 char out[FMT_ST_CHARS];
852c8d21
SK
791 int reqday, week_line, d;
792 int skip;
793 struct cal_month *i;
794
795 for (week_line = 0; week_line < MAXDAYS / DAYS_IN_WEEK; week_line++) {
796 for (i = month; i; i = i->next) {
797 /* Determine the day that should be highlighted. */
798 reqday = 0;
799 if (i->month == ctl->req.month && i->year == ctl->req.year) {
800 if (ctl->julian)
801 reqday = ctl->req.day;
802 else
db9baa8a
WP
803 reqday = ctl->req.day + 1 -
804 day_in_year(ctl, 1, i->month,
805 i->year);
852c8d21
SK
806 }
807
808 if (ctl->weektype) {
809 if (0 < i->weeks[week_line]) {
810 if ((ctl->weektype & WEEK_NUM_MASK) ==
811 i->weeks[week_line])
100a1406
KZ
812 snprintf(out, sizeof(out), "%s%2d%s",
813 Senter, i->weeks[week_line],
814 Sexit);
852c8d21 815 else
100a1406 816 snprintf(out, sizeof(out), "%2d", i->weeks[week_line]);
852c8d21 817 } else
100a1406 818 snprintf(out, sizeof(out), "%2s", "");
07ac4aa9 819 my_putstring(out);
852c8d21
SK
820 skip = ctl->day_width;
821 } else
822 /* First day of the week is one char narrower than the other days,
823 * unless week number is printed. */
824 skip = ctl->day_width - 1;
825
826 for (d = DAYS_IN_WEEK * week_line;
827 d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
828 if (0 < i->days[d]) {
829 if (reqday == i->days[d])
100a1406
KZ
830 snprintf(out, sizeof(out), "%*s%s%*d%s",
831 skip - (ctl->julian ? 3 : 2),
852c8d21
SK
832 "", Senter, (ctl->julian ? 3 : 2),
833 i->days[d], Sexit);
834 else
100a1406 835 snprintf(out, sizeof(out), "%*d", skip, i->days[d]);
852c8d21 836 } else
100a1406 837 snprintf(out, sizeof(out), "%*s", skip, "");
07ac4aa9 838 my_putstring(out);
852c8d21
SK
839 if (skip < (int)ctl->day_width)
840 skip++;
841 }
07ac4aa9 842 if (i->next != NULL) {
100a1406 843 snprintf(out, sizeof(out), "%*s", ctl->gutter_width, "");
07ac4aa9
RM
844 my_putstring(out);
845 }
846 }
8315a2ff
KZ
847 if (i == NULL)
848 my_putstring("\n");
852c8d21
SK
849 }
850}
851
852static void monthly(const struct cal_control *ctl)
853{
7800509b 854 struct cal_month m1,m2,m3, *m;
2395f93c 855 int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
7800509b
MK
856 int32_t year = ctl->req.year;
857
ccf3dd50
D
858 /* cal -3, cal -Y --span, etc. */
859 if (ctl->span_months) {
2395f93c 860 int new_month = month - ctl->num_months / 2;
ccf3dd50 861 if (new_month < 1) {
2395f93c
KZ
862 new_month *= -1;
863 year -= (new_month / MONTHS_IN_YEAR) + 1;
864
865 if (new_month > MONTHS_IN_YEAR)
866 new_month %= MONTHS_IN_YEAR;
867 month = MONTHS_IN_YEAR - new_month;
868 } else
ccf3dd50 869 month = new_month;
7800509b 870 }
f60117b5 871
7800509b
MK
872 m1.next = (ctl->months_in_row > 1) ? &m2 : NULL;
873 m2.next = (ctl->months_in_row > 2) ? &m3 : NULL;
f60117b5
SK
874 m3.next = NULL;
875
7800509b
MK
876 rows = (ctl->num_months - 1) / ctl->months_in_row;
877 for (i = 0; i < rows + 1 ; i++){
878 if (i == rows){
879 switch (ctl->num_months % ctl->months_in_row){
880 case 1:
881 m1.next = NULL;
882 /* fallthrough */
883 case 2:
884 m2.next = NULL;
885 /* fallthrough */
886 }
887 }
888 for (m = &m1; m; m = m->next){
889 m->month = month++;
890 m->year = year;
891 if (MONTHS_IN_YEAR < month) {
892 year++;
893 month = 1;
894 }
895 cal_fill_month(m, ctl);
896 }
897 cal_output_header(&m1, ctl);
898 cal_output_months(&m1, ctl);
e66ba1bf 899 }
6dbe3af9
KZ
900}
901
ffc56357 902static void yearly(const struct cal_control *ctl)
d4be073d 903{
95f4adde
SK
904 char out[FMT_ST_CHARS];
905 int year_width = 0;
add1924d 906
7800509b 907 year_width += (ctl->week_width + 1) * (ctl->julian ? 2 : 3);
95f4adde
SK
908 if (ctl->julian)
909 year_width--;
7800509b
MK
910
911 if (ctl->header_year) {
100a1406 912 snprintf(out, sizeof(out), "%04d", ctl->req.year);
7800509b
MK
913 center(out, year_width, 0);
914 my_putstring("\n\n");
d162fcb5 915 }
7800509b 916 monthly(ctl);
6dbe3af9
KZ
917}
918
919/*
920 * day_in_year --
921 * return the 1 based day number within the year
922 */
db9baa8a
WP
923static int day_in_year(const struct cal_control *ctl,
924 int day, int month, int32_t year)
d4be073d 925{
6dbe3af9
KZ
926 int i, leap;
927
db9baa8a 928 leap = leap_year(ctl, year);
6dbe3af9
KZ
929 for (i = 1; i < month; i++)
930 day += days_in_month[leap][i];
d162fcb5 931 return day;
6dbe3af9
KZ
932}
933
934/*
935 * day_in_week
936 * return the 0 based day number for any date from 1 Jan. 1 to
937 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
d68f076f
SK
938 * 3 Sep. 1752 through 13 Sep. 1752, and returns invalid weekday
939 * during the period of 11 days.
6dbe3af9 940 */
db9baa8a
WP
941static int day_in_week(const struct cal_control *ctl, int day,
942 int month, int32_t year)
91ac9ef5 943{
6ce7eca3
KZ
944 /*
945 * The magic constants in the reform[] array are, in a simplified
946 * sense, the remaining days after slicing into one week periods the total
947 * days from the beginning of the year to the target month. That is,
948 * weeks + reform[] days gets us to the target month. The exception is,
949 * that for the months past February 'DOY - 1' must be used.
950 *
951 * DoY (Day of Year): total days to the target month
952 *
953 * Month 1 2 3 4 5 6 7 8 9 10 11 12
954 * DoY 0 31 59 90 120 151 181 212 243 273 304 334
955 * DoY % 7 0 3
956 * DoY - 1 % 7 - -- 2 5 0 3 5 1 4 6 2 4
957 * reform[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
958 *
959 * Note: these calculations are for non leap years.
960 */
3c49b23a
KZ
961 static const int reform[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
962 static const int old[] = { 5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2 };
963
db9baa8a 964 if (year != ctl->reform_year + 1)
e1abe57e 965 year -= month < MARCH;
91ac9ef5 966 else
e1abe57e 967 year -= (month < MARCH) + 14;
db9baa8a
WP
968 if (ctl->reform_year < year
969 || (year == ctl->reform_year && REFORMATION_MONTH < month)
970 || (year == ctl->reform_year
971 && month == REFORMATION_MONTH && 13 < day)) {
1dca3edc
WP
972 return ((int64_t) year + (year / 4)
973 - (year / 100) + (year / 400)
974 + reform[month - 1] + day) % DAYS_IN_WEEK;
26f3a386 975 }
db9baa8a
WP
976 if (year < ctl->reform_year
977 || (year == ctl->reform_year && month < REFORMATION_MONTH)
978 || (year == ctl->reform_year && month == REFORMATION_MONTH && day < 3))
1dca3edc
WP
979 return ((int64_t) year + year / 4 + old[month - 1] + day)
980 % DAYS_IN_WEEK;
91ac9ef5 981 return NONEDAY;
6dbe3af9
KZ
982}
983
c36c4a4e
TK
984/*
985 * week_number
efafeaf6 986 * return the week number of a given date, 1..54.
c36c4a4e
TK
987 * Supports ISO-8601 and North American modes.
988 * Day may be given as Julian day of the year mode, in which
989 * case the month is disregarded entirely.
990 */
4a7424a5 991static int week_number(int day, int month, int32_t year, const struct cal_control *ctl)
c1732e62
KZ
992{
993 int fday = 0, yday;
db9baa8a 994 const int wday = day_in_week(ctl, 1, JANUARY, year);
c1732e62 995
b549058b 996 if (ctl->weektype & WEEK_NUM_ISO)
c1732e62 997 fday = wday + (wday >= FRIDAY ? -2 : 5);
efafeaf6
SK
998 else {
999 /* WEEK_NUM_US: Jan 1 is always First week, that may
1000 * begin previous year. That means there is very seldom
1001 * more than 52 weeks, */
1002 fday = wday + 6;
1003 }
c1732e62
KZ
1004 /* For julian dates the month can be set to 1, the global julian
1005 * variable cannot be relied upon here, because we may recurse
1006 * internally for 31.12. which would not work. */
e1abe57e
SK
1007 if (day > DAYS_IN_MONTH)
1008 month = JANUARY;
c1732e62 1009
db9baa8a
WP
1010 yday = day_in_year(ctl, day, month, year);
1011 if (year == ctl->reform_year && yday >= YDAY_AFTER_MISSING)
74ce680a 1012 fday -= NUMBER_MISSING_DAYS;
c1732e62
KZ
1013
1014 /* Last year is last year */
e1abe57e
SK
1015 if (yday + fday < DAYS_IN_WEEK)
1016 return week_number(31, DECEMBER, year - 1, ctl);
c1732e62
KZ
1017
1018 /* Or it could be part of the next year. The reformation year had less
1019 * days than 365 making this check invalid, but reformation year ended
1020 * on Sunday and in week 51, so it's ok here. */
b549058b 1021 if (ctl->weektype == WEEK_NUM_ISO && yday >= 363
db9baa8a
WP
1022 && day_in_week(ctl, day, month, year) >= MONDAY
1023 && day_in_week(ctl, day, month, year) <= WEDNESDAY
1024 && day_in_week(ctl, 31, DECEMBER, year) >= MONDAY
1025 && day_in_week(ctl, 31, DECEMBER, year) <= WEDNESDAY)
e1abe57e 1026 return week_number(1, JANUARY, year + 1, ctl);
c1732e62 1027
e1abe57e 1028 return (yday + fday) / DAYS_IN_WEEK;
c36c4a4e
TK
1029}
1030
aae4f87e
TK
1031/*
1032 * week_to_day
1033 * return the yday of the first day in a given week inside
1034 * the given year. This may be something other than Monday
1035 * for ISO-8601 modes. For North American numbering this
1036 * always returns a Sunday.
1037 */
ffc56357 1038static int week_to_day(const struct cal_control *ctl)
c1732e62 1039{
aae4f87e 1040 int yday, wday;
c1732e62 1041
db9baa8a 1042 wday = day_in_week(ctl, 1, JANUARY, ctl->req.year);
e1abe57e 1043 yday = ctl->req.week * DAYS_IN_WEEK - wday;
c1732e62 1044
e572a765
WP
1045 if (ctl->req.year == ctl->reform_year && yday >= YDAY_AFTER_MISSING)
1046 yday += NUMBER_MISSING_DAYS;
1047
b549058b 1048 if (ctl->weektype & WEEK_NUM_ISO)
c1732e62
KZ
1049 yday -= (wday >= FRIDAY ? -2 : 5);
1050 else
6304358c 1051 yday -= 6; /* WEEK_NUM_US */
c1732e62 1052 if (yday <= 0)
aae4f87e
TK
1053 return 1;
1054
1055 return yday;
1056}
1057
d162fcb5
KZ
1058/*
1059 * Center string, handling multibyte characters appropriately.
1060 * In addition if the string is too large for the width it's truncated.
ff87defc 1061 * The number of trailing spaces may be 1 less than the number of leading spaces.
d162fcb5 1062 */
d4be073d
KZ
1063static int center_str(const char* src, char* dest,
1064 size_t dest_size, size_t width)
d162fcb5 1065{
104b92f8
PB
1066 return mbsalign(src, dest, dest_size, &width,
1067 MBS_ALIGN_CENTER, MBA_UNIBYTE_FALLBACK);
d162fcb5
KZ
1068}
1069
d4be073d 1070static void center(const char *str, size_t len, int separate)
6dbe3af9 1071{
d162fcb5 1072 char lineout[FMT_ST_CHARS];
e66ba1bf 1073
17282538 1074 center_str(str, lineout, ARRAY_SIZE(lineout), len);
e66ba1bf
KZ
1075 my_putstring(lineout);
1076
1077 if (separate) {
1078 snprintf(lineout, sizeof(lineout), "%*s", separate, "");
1079 my_putstring(lineout);
1080 }
6dbe3af9
KZ
1081}
1082
ae51e7b4
WP
1083static int parse_reform_year(const char *reform_year)
1084{
1085 size_t i;
1086
1087 struct reform {
1088 char *name;
1089 int val;
1090 };
1091
1092 struct reform years[] = {
1093 {"gregorian", GREGORIAN},
1094 {"iso", ISO},
1095 {"1752", GB1752},
1096 {"julian", JULIAN},
1097 };
1098
1099 for (i = 0; i < ARRAY_SIZE(years); i++) {
1100 if (strcasecmp(reform_year, years[i].name) == 0) {
1101 return years[i].val;
1102 }
1103 }
1104 errx(EXIT_FAILURE, "invalid --reform value: '%s'", reform_year);
1105}
1106
6e1eda6f 1107static void __attribute__((__noreturn__)) usage(void)
6dbe3af9 1108{
6e1eda6f 1109 FILE *out = stdout;
201d43fd
SK
1110 fputs(USAGE_HEADER, out);
1111 fprintf(out, _(" %s [options] [[[day] month] year]\n"), program_invocation_short_name);
535fd6d2 1112 fprintf(out, _(" %s [options] <timestamp|monthname>\n"), program_invocation_short_name);
d4be073d 1113
233ad1fa
PB
1114 fputs(USAGE_SEPARATOR, out);
1115 fputs(_("Display a calendar, or some part of it.\n"), out);
1116 fputs(_("Without any arguments, display the current month.\n"), out);
1117
201d43fd 1118 fputs(USAGE_OPTIONS, out);
233ad1fa
PB
1119 fputs(_(" -1, --one show only a single month (default)\n"), out);
1120 fputs(_(" -3, --three show three months spanning the date\n"), out);
7800509b 1121 fputs(_(" -n, --months <num> show num months starting with date's month\n"), out);
ccf3dd50 1122 fputs(_(" -S, --span span the date when displaying multiple months\n"), out);
201d43fd
SK
1123 fputs(_(" -s, --sunday Sunday as first day of week\n"), out);
1124 fputs(_(" -m, --monday Monday as first day of week\n"), out);
ae51e7b4
WP
1125 fputs(_(" -j, --julian use day-of-year for all calendars\n"), out);
1126 fputs(_(" --reform <val> Gregorian reform date (1752|gregorian|iso|julian)\n"), out);
1127 fputs(_(" --iso alias for --reform=iso\n"), out);
233ad1fa 1128 fputs(_(" -y, --year show the whole year\n"), out);
7800509b 1129 fputs(_(" -Y, --twelve show the next twelve months\n"), out);
af7c483e 1130 fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out);
201d43fd 1131 fputs(_(" --color[=<when>] colorize messages (auto, always or never)\n"), out);
5d51dc2a
KZ
1132 fprintf(out,
1133 " %s\n", USAGE_COLORS_DEFAULT);
d4be073d 1134
201d43fd 1135 fputs(USAGE_SEPARATOR, out);
f45f3ec3
RM
1136 printf(USAGE_HELP_OPTIONS(23));
1137 printf(USAGE_MAN_TAIL("cal(1)"));
d4be073d 1138
6e1eda6f 1139 exit(EXIT_SUCCESS);
6dbe3af9 1140}