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