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