10cb2e609SWolfgang Helbig /*- 20cb2e609SWolfgang Helbig * Copyright (c) 1997 Wolfgang Helbig 30cb2e609SWolfgang Helbig * All rights reserved. 40cb2e609SWolfgang Helbig * 50cb2e609SWolfgang Helbig * Redistribution and use in source and binary forms, with or without 60cb2e609SWolfgang Helbig * modification, are permitted provided that the following conditions 70cb2e609SWolfgang Helbig * are met: 80cb2e609SWolfgang Helbig * 1. Redistributions of source code must retain the above copyright 90cb2e609SWolfgang Helbig * notice, this list of conditions and the following disclaimer. 100cb2e609SWolfgang Helbig * 2. Redistributions in binary form must reproduce the above copyright 110cb2e609SWolfgang Helbig * notice, this list of conditions and the following disclaimer in the 120cb2e609SWolfgang Helbig * documentation and/or other materials provided with the distribution. 130cb2e609SWolfgang Helbig * 140cb2e609SWolfgang Helbig * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 150cb2e609SWolfgang Helbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 160cb2e609SWolfgang Helbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 170cb2e609SWolfgang Helbig * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 180cb2e609SWolfgang Helbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190cb2e609SWolfgang Helbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 200cb2e609SWolfgang Helbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 210cb2e609SWolfgang Helbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 220cb2e609SWolfgang Helbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 230cb2e609SWolfgang Helbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 240cb2e609SWolfgang Helbig * SUCH DAMAGE. 250cb2e609SWolfgang Helbig */ 26a99e4564SPhilippe Charnier 27bf70beceSEd Schouten #include <sys/cdefs.h> 28bf70beceSEd Schouten __FBSDID("$FreeBSD$"); 29a99e4564SPhilippe Charnier 300cb2e609SWolfgang Helbig #include <calendar.h> 31821df508SXin LI #include <ctype.h> 320cb2e609SWolfgang Helbig #include <err.h> 33f5e40af2SAndrey A. Chernov #include <langinfo.h> 34746b67cdSEdwin Groothuis #include <libgen.h> 350cb2e609SWolfgang Helbig #include <locale.h> 360cb2e609SWolfgang Helbig #include <stdio.h> 370cb2e609SWolfgang Helbig #include <stdlib.h> 380cb2e609SWolfgang Helbig #include <string.h> 390cb2e609SWolfgang Helbig #include <sysexits.h> 400cb2e609SWolfgang Helbig #include <time.h> 410cb2e609SWolfgang Helbig #include <unistd.h> 424646cea7SDavid Schultz #include <wchar.h> 434646cea7SDavid Schultz #include <wctype.h> 44e454a171SRoman Divacky #include <term.h> 45e454a171SRoman Divacky #undef lines /* term.h defines this */ 460cb2e609SWolfgang Helbig 470851fbdfSEdwin Groothuis /* Width of one month with backward compatibility and in regular mode*/ 480cb2e609SWolfgang Helbig #define MONTH_WIDTH_B_J 27 490cb2e609SWolfgang Helbig #define MONTH_WIDTH_B 20 500cb2e609SWolfgang Helbig 510851fbdfSEdwin Groothuis #define MONTH_WIDTH_R_J 24 520851fbdfSEdwin Groothuis #define MONTH_WIDTH_R 18 530cb2e609SWolfgang Helbig 54e454a171SRoman Divacky #define MAX_WIDTH 64 550cb2e609SWolfgang Helbig 560cb2e609SWolfgang Helbig typedef struct date date; 570cb2e609SWolfgang Helbig 580cb2e609SWolfgang Helbig struct monthlines { 594646cea7SDavid Schultz wchar_t name[MAX_WIDTH + 1]; 600cb2e609SWolfgang Helbig char lines[7][MAX_WIDTH + 1]; 610cb2e609SWolfgang Helbig char weeks[MAX_WIDTH + 1]; 621d0e1dacSEdwin Groothuis unsigned int extralen[7]; 630cb2e609SWolfgang Helbig }; 640cb2e609SWolfgang Helbig 650cb2e609SWolfgang Helbig struct weekdays { 664646cea7SDavid Schultz wchar_t names[7][4]; 670cb2e609SWolfgang Helbig }; 680cb2e609SWolfgang Helbig 690cb2e609SWolfgang Helbig /* The switches from Julian to Gregorian in some countries */ 700cb2e609SWolfgang Helbig static struct djswitch { 71f372d010SMark Murray const char *cc; /* Country code according to ISO 3166 */ 72f372d010SMark Murray const char *nm; /* Name of country */ 730cb2e609SWolfgang Helbig date dt; /* Last day of Julian calendar */ 740cb2e609SWolfgang Helbig } switches[] = { 759d969a8bSAndrey A. Chernov {"AL", "Albania", {1912, 11, 30}}, 769d969a8bSAndrey A. Chernov {"AT", "Austria", {1583, 10, 5}}, 779d969a8bSAndrey A. Chernov {"AU", "Australia", {1752, 9, 2}}, 789d969a8bSAndrey A. Chernov {"BE", "Belgium", {1582, 12, 14}}, 799d969a8bSAndrey A. Chernov {"BG", "Bulgaria", {1916, 3, 18}}, 809d969a8bSAndrey A. Chernov {"CA", "Canada", {1752, 9, 2}}, 819d969a8bSAndrey A. Chernov {"CH", "Switzerland", {1655, 2, 28}}, 829d969a8bSAndrey A. Chernov {"CN", "China", {1911, 12, 18}}, 839d969a8bSAndrey A. Chernov {"CZ", "Czech Republic",{1584, 1, 6}}, 849d969a8bSAndrey A. Chernov {"DE", "Germany", {1700, 2, 18}}, 859d969a8bSAndrey A. Chernov {"DK", "Denmark", {1700, 2, 18}}, 869d969a8bSAndrey A. Chernov {"ES", "Spain", {1582, 10, 4}}, 879d969a8bSAndrey A. Chernov {"FI", "Finland", {1753, 2, 17}}, 889d969a8bSAndrey A. Chernov {"FR", "France", {1582, 12, 9}}, 899d969a8bSAndrey A. Chernov {"GB", "United Kingdom",{1752, 9, 2}}, 909d969a8bSAndrey A. Chernov {"GR", "Greece", {1924, 3, 9}}, 919d969a8bSAndrey A. Chernov {"HU", "Hungary", {1587, 10, 21}}, 929d969a8bSAndrey A. Chernov {"IS", "Iceland", {1700, 11, 16}}, 939d969a8bSAndrey A. Chernov {"IT", "Italy", {1582, 10, 4}}, 949d969a8bSAndrey A. Chernov {"JP", "Japan", {1918, 12, 18}}, 959d969a8bSAndrey A. Chernov {"LI", "Lithuania", {1918, 2, 1}}, 969d969a8bSAndrey A. Chernov {"LN", "Latin", {9999, 05, 31}}, 979d969a8bSAndrey A. Chernov {"LU", "Luxembourg", {1582, 12, 14}}, 989d969a8bSAndrey A. Chernov {"LV", "Latvia", {1918, 2, 1}}, 999d969a8bSAndrey A. Chernov {"NL", "Netherlands", {1582, 12, 14}}, 1009d969a8bSAndrey A. Chernov {"NO", "Norway", {1700, 2, 18}}, 1019d969a8bSAndrey A. Chernov {"PL", "Poland", {1582, 10, 4}}, 1029d969a8bSAndrey A. Chernov {"PT", "Portugal", {1582, 10, 4}}, 1039d969a8bSAndrey A. Chernov {"RO", "Romania", {1919, 3, 31}}, 1049d969a8bSAndrey A. Chernov {"RU", "Russia", {1918, 1, 31}}, 1059d969a8bSAndrey A. Chernov {"SI", "Slovenia", {1919, 3, 4}}, 106fb53214dSMaxim Konovalov {"SE", "Sweden", {1753, 2, 17}}, 1079d969a8bSAndrey A. Chernov {"TR", "Turkey", {1926, 12, 18}}, 1089d969a8bSAndrey A. Chernov {"US", "United States", {1752, 9, 2}}, 1099d969a8bSAndrey A. Chernov {"YU", "Yugoslavia", {1919, 3, 4}} 1100cb2e609SWolfgang Helbig }; 1110cb2e609SWolfgang Helbig 112bf70beceSEd Schouten static struct djswitch *dftswitch = 113cde26ed2SWolfgang Helbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 114cde26ed2SWolfgang Helbig /* default switch (should be "US") */ 115cde26ed2SWolfgang Helbig 1160cb2e609SWolfgang Helbig /* Table used to print day of month and week numbers */ 117bf70beceSEd Schouten static char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 1180cb2e609SWolfgang Helbig " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 1190cb2e609SWolfgang Helbig " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 1200cb2e609SWolfgang Helbig " 48 49 50 51 52 53"; 1210cb2e609SWolfgang Helbig 1220cb2e609SWolfgang Helbig /* Table used to print day of year and week numbers */ 123bf70beceSEd Schouten static char jdaystr[] = " 1 2 3 4 5 6 7 8 9" 1240cb2e609SWolfgang Helbig " 10 11 12 13 14 15 16 17 18 19" 1250cb2e609SWolfgang Helbig " 20 21 22 23 24 25 26 27 28 29" 1260cb2e609SWolfgang Helbig " 30 31 32 33 34 35 36 37 38 39" 1270cb2e609SWolfgang Helbig " 40 41 42 43 44 45 46 47 48 49" 1280cb2e609SWolfgang Helbig " 50 51 52 53 54 55 56 57 58 59" 1290cb2e609SWolfgang Helbig " 60 61 62 63 64 65 66 67 68 69" 1300cb2e609SWolfgang Helbig " 70 71 72 73 74 75 76 77 78 79" 1310cb2e609SWolfgang Helbig " 80 81 82 83 84 85 86 87 88 89" 1320cb2e609SWolfgang Helbig " 90 91 92 93 94 95 96 97 98 99" 1330cb2e609SWolfgang Helbig " 100 101 102 103 104 105 106 107 108 109" 1340cb2e609SWolfgang Helbig " 110 111 112 113 114 115 116 117 118 119" 1350cb2e609SWolfgang Helbig " 120 121 122 123 124 125 126 127 128 129" 1360cb2e609SWolfgang Helbig " 130 131 132 133 134 135 136 137 138 139" 1370cb2e609SWolfgang Helbig " 140 141 142 143 144 145 146 147 148 149" 1380cb2e609SWolfgang Helbig " 150 151 152 153 154 155 156 157 158 159" 1390cb2e609SWolfgang Helbig " 160 161 162 163 164 165 166 167 168 169" 1400cb2e609SWolfgang Helbig " 170 171 172 173 174 175 176 177 178 179" 1410cb2e609SWolfgang Helbig " 180 181 182 183 184 185 186 187 188 189" 1420cb2e609SWolfgang Helbig " 190 191 192 193 194 195 196 197 198 199" 1430cb2e609SWolfgang Helbig " 200 201 202 203 204 205 206 207 208 209" 1440cb2e609SWolfgang Helbig " 210 211 212 213 214 215 216 217 218 219" 1450cb2e609SWolfgang Helbig " 220 221 222 223 224 225 226 227 228 229" 1460cb2e609SWolfgang Helbig " 230 231 232 233 234 235 236 237 238 239" 1470cb2e609SWolfgang Helbig " 240 241 242 243 244 245 246 247 248 249" 1480cb2e609SWolfgang Helbig " 250 251 252 253 254 255 256 257 258 259" 1490cb2e609SWolfgang Helbig " 260 261 262 263 264 265 266 267 268 269" 1500cb2e609SWolfgang Helbig " 270 271 272 273 274 275 276 277 278 279" 1510cb2e609SWolfgang Helbig " 280 281 282 283 284 285 286 287 288 289" 1520cb2e609SWolfgang Helbig " 290 291 292 293 294 295 296 297 298 299" 1530cb2e609SWolfgang Helbig " 300 301 302 303 304 305 306 307 308 309" 1540cb2e609SWolfgang Helbig " 310 311 312 313 314 315 316 317 318 319" 1550cb2e609SWolfgang Helbig " 320 321 322 323 324 325 326 327 328 329" 1560cb2e609SWolfgang Helbig " 330 331 332 333 334 335 336 337 338 339" 1570cb2e609SWolfgang Helbig " 340 341 342 343 344 345 346 347 348 349" 1580cb2e609SWolfgang Helbig " 350 351 352 353 354 355 356 357 358 359" 1590cb2e609SWolfgang Helbig " 360 361 362 363 364 365 366"; 1600cb2e609SWolfgang Helbig 161bf70beceSEd Schouten static int flag_nohighlight; /* user doesn't want a highlighted today */ 162bf70beceSEd Schouten static int flag_weeks; /* user wants number of week */ 163bf70beceSEd Schouten static int nswitch; /* user defined switch date */ 164bf70beceSEd Schouten static int nswitchb; /* switch date for backward compatibility */ 165bf70beceSEd Schouten static int highlightdate; 1660cb2e609SWolfgang Helbig 167bf70beceSEd Schouten static char *center(char *s, char *t, int w); 168bf70beceSEd Schouten static wchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 169bf70beceSEd Schouten static int firstday(int y, int m); 170bf70beceSEd Schouten static void highlight(char *dst, char *src, int len, int *extraletters); 171bf70beceSEd Schouten static void mkmonthr(int year, int month, int jd_flag, 172bf70beceSEd Schouten struct monthlines * monthl); 173bf70beceSEd Schouten static void mkmonthb(int year, int month, int jd_flag, 174bf70beceSEd Schouten struct monthlines * monthl); 175bf70beceSEd Schouten static void mkweekdays(struct weekdays * wds); 176bf70beceSEd Schouten static void monthranger(int year, int m, int jd_flag, 177bf70beceSEd Schouten int before, int after); 178bf70beceSEd Schouten static void monthrangeb(int year, int m, int jd_flag, 179bf70beceSEd Schouten int before, int after); 180bf70beceSEd Schouten static int parsemonth(const char *s, int *m, int *y); 181bf70beceSEd Schouten static void printcc(void); 182bf70beceSEd Schouten static void printeaster(int year, int julian, int orthodox); 183bf70beceSEd Schouten static date *sdater(int ndays, struct date * d); 184bf70beceSEd Schouten static date *sdateb(int ndays, struct date * d); 185bf70beceSEd Schouten static int sndaysr(struct date * d); 186bf70beceSEd Schouten static int sndaysb(struct date * d); 187a99e4564SPhilippe Charnier static void usage(void); 1880cb2e609SWolfgang Helbig 1890cb2e609SWolfgang Helbig int 1900cb2e609SWolfgang Helbig main(int argc, char *argv[]) 1910cb2e609SWolfgang Helbig { 1920cb2e609SWolfgang Helbig struct djswitch *p, *q; /* to search user defined switch date */ 1930cb2e609SWolfgang Helbig date never = {10000, 1, 1}; /* outside valid range of dates */ 1940cb2e609SWolfgang Helbig date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 1950851fbdfSEdwin Groothuis date dt; 1960cb2e609SWolfgang Helbig int ch; /* holds the option character */ 1970cb2e609SWolfgang Helbig int m = 0; /* month */ 1980cb2e609SWolfgang Helbig int y = 0; /* year */ 1990cb2e609SWolfgang Helbig int flag_backward = 0; /* user called cal--backward compat. */ 2004af997a8SEdwin Groothuis int flag_wholeyear = 0; /* user wants the whole year */ 2010cb2e609SWolfgang Helbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 2024af997a8SEdwin Groothuis int flag_julian_day = 0; /* user wants the Julian day numbers */ 2034af997a8SEdwin Groothuis int flag_orthodox = 0; /* user wants Orthodox easter */ 2044af997a8SEdwin Groothuis int flag_easter = 0; /* user wants easter date */ 2054af997a8SEdwin Groothuis int flag_3months = 0; /* user wants 3 month display (-3) */ 2064af997a8SEdwin Groothuis int flag_after = 0; /* user wants to see months after */ 2074af997a8SEdwin Groothuis int flag_before = 0; /* user wants to see months before */ 2084af997a8SEdwin Groothuis int flag_specifiedmonth = 0;/* user wants to see this month (-m) */ 2094af997a8SEdwin Groothuis int flag_givenmonth = 0; /* user has specified month [n] */ 2104af997a8SEdwin Groothuis int flag_givenyear = 0; /* user has specified year [n] */ 2110cb2e609SWolfgang Helbig char *cp; /* character pointer */ 2124af997a8SEdwin Groothuis char *flag_today = NULL; /* debug: use date as being today */ 2130c4cafeaSGarrett Wollman char *flag_month = NULL; /* requested month as string */ 2144af997a8SEdwin Groothuis char *flag_highlightdate = NULL; /* debug: date to highlight */ 2150851fbdfSEdwin Groothuis int before, after; 216f372d010SMark Murray const char *locale; /* locale to get country code */ 217e454a171SRoman Divacky 218cae11c25SEdwin Groothuis flag_nohighlight = 0; 219cae11c25SEdwin Groothuis flag_weeks = 0; 2200cb2e609SWolfgang Helbig 221cde26ed2SWolfgang Helbig /* 222cde26ed2SWolfgang Helbig * Use locale to determine the country code, 223cde26ed2SWolfgang Helbig * and use the country code to determine the default 224cde26ed2SWolfgang Helbig * switchdate and date format from the switches table. 225cde26ed2SWolfgang Helbig */ 226e036a70eSAndrey A. Chernov if (setlocale(LC_ALL, "") == NULL) 227cde26ed2SWolfgang Helbig warn("setlocale"); 228e036a70eSAndrey A. Chernov locale = setlocale(LC_TIME, NULL); 229c052429fSAndrey A. Chernov if (locale == NULL || 230c052429fSAndrey A. Chernov strcmp(locale, "C") == 0 || 2311c593a0dSAndrey A. Chernov strcmp(locale, "POSIX") == 0 || 2321c593a0dSAndrey A. Chernov strcmp(locale, "ASCII") == 0 || 2331c593a0dSAndrey A. Chernov strcmp(locale, "US-ASCII") == 0) 234cde26ed2SWolfgang Helbig locale = "_US"; 235cde26ed2SWolfgang Helbig q = switches + sizeof(switches) / sizeof(struct djswitch); 236cde26ed2SWolfgang Helbig for (p = switches; p != q; p++) 237cde26ed2SWolfgang Helbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 238cde26ed2SWolfgang Helbig break; 239cde26ed2SWolfgang Helbig if (p == q) { 240cde26ed2SWolfgang Helbig nswitch = ndaysj(&dftswitch->dt); 241cde26ed2SWolfgang Helbig } else { 242cde26ed2SWolfgang Helbig nswitch = ndaysj(&p->dt); 243cde26ed2SWolfgang Helbig dftswitch = p; 244cde26ed2SWolfgang Helbig } 245cde26ed2SWolfgang Helbig 2460cb2e609SWolfgang Helbig 2470cb2e609SWolfgang Helbig /* 2480cb2e609SWolfgang Helbig * Get the filename portion of argv[0] and set flag_backward if 2490cb2e609SWolfgang Helbig * this program is called "cal". 2500cb2e609SWolfgang Helbig */ 251746b67cdSEdwin Groothuis if (strncmp(basename(argv[0]), "cal", strlen("cal")) == 0) 2520cb2e609SWolfgang Helbig flag_backward = 1; 2530cb2e609SWolfgang Helbig 2540cb2e609SWolfgang Helbig /* Set the switch date to United Kingdom if backwards compatible */ 2550cb2e609SWolfgang Helbig if (flag_backward) 2560cb2e609SWolfgang Helbig nswitchb = ndaysj(&ukswitch); 2570cb2e609SWolfgang Helbig 2580851fbdfSEdwin Groothuis before = after = -1; 2590851fbdfSEdwin Groothuis 260531a1a4bSEdwin Groothuis while ((ch = getopt(argc, argv, "3A:B:Cd:eH:hjJm:Nops:wy")) != -1) 2610cb2e609SWolfgang Helbig switch (ch) { 2620851fbdfSEdwin Groothuis case '3': 2634af997a8SEdwin Groothuis flag_3months = 1; 2640851fbdfSEdwin Groothuis break; 2650851fbdfSEdwin Groothuis case 'A': 2664af997a8SEdwin Groothuis if (flag_after > 0) 2674af997a8SEdwin Groothuis errx(EX_USAGE, "Double -A specified"); 2684af997a8SEdwin Groothuis flag_after = strtol(optarg, NULL, 10); 2694af997a8SEdwin Groothuis if (flag_after <= 0) 2704af997a8SEdwin Groothuis errx(EX_USAGE, 2714af997a8SEdwin Groothuis "Argument to -A must be positive"); 2720851fbdfSEdwin Groothuis break; 2730851fbdfSEdwin Groothuis case 'B': 2744af997a8SEdwin Groothuis if (flag_before > 0) 2754af997a8SEdwin Groothuis errx(EX_USAGE, "Double -A specified"); 2764af997a8SEdwin Groothuis flag_before = strtol(optarg, NULL, 10); 2774af997a8SEdwin Groothuis if (flag_before <= 0) 2784af997a8SEdwin Groothuis errx(EX_USAGE, 2794af997a8SEdwin Groothuis "Argument to -B must be positive"); 2800851fbdfSEdwin Groothuis break; 2810cb2e609SWolfgang Helbig case 'J': 2820cb2e609SWolfgang Helbig if (flag_backward) 2830cb2e609SWolfgang Helbig usage(); 2840cb2e609SWolfgang Helbig nswitch = ndaysj(&never); 2850cb2e609SWolfgang Helbig flag_julian_cal = 1; 2860cb2e609SWolfgang Helbig break; 287531a1a4bSEdwin Groothuis case 'C': 2880851fbdfSEdwin Groothuis flag_backward = 1; 2890851fbdfSEdwin Groothuis break; 290531a1a4bSEdwin Groothuis case 'N': 291531a1a4bSEdwin Groothuis flag_backward = 0; 292531a1a4bSEdwin Groothuis break; 2930851fbdfSEdwin Groothuis case 'd': 2944af997a8SEdwin Groothuis flag_today = optarg; 2954af997a8SEdwin Groothuis break; 2964af997a8SEdwin Groothuis case 'H': 2970851fbdfSEdwin Groothuis flag_highlightdate = optarg; 2980851fbdfSEdwin Groothuis break; 2990cd51de1SRoman Divacky case 'h': 300cae11c25SEdwin Groothuis flag_nohighlight = 1; 3010cd51de1SRoman Divacky break; 3020cb2e609SWolfgang Helbig case 'e': 3030cb2e609SWolfgang Helbig if (flag_backward) 3040cb2e609SWolfgang Helbig usage(); 3050cb2e609SWolfgang Helbig flag_easter = 1; 3060cb2e609SWolfgang Helbig break; 3070cb2e609SWolfgang Helbig case 'j': 3080cb2e609SWolfgang Helbig flag_julian_day = 1; 3090cb2e609SWolfgang Helbig break; 3100c4cafeaSGarrett Wollman case 'm': 3114af997a8SEdwin Groothuis if (flag_specifiedmonth) 3124af997a8SEdwin Groothuis errx(EX_USAGE, "Double -m specified"); 3130c4cafeaSGarrett Wollman flag_month = optarg; 3144af997a8SEdwin Groothuis flag_specifiedmonth = 1; 3150c4cafeaSGarrett Wollman break; 3160cb2e609SWolfgang Helbig case 'o': 3170cb2e609SWolfgang Helbig if (flag_backward) 3180cb2e609SWolfgang Helbig usage(); 3190cb2e609SWolfgang Helbig flag_orthodox = 1; 3200cb2e609SWolfgang Helbig flag_easter = 1; 3210cb2e609SWolfgang Helbig break; 3220cb2e609SWolfgang Helbig case 'p': 3230cb2e609SWolfgang Helbig if (flag_backward) 3240cb2e609SWolfgang Helbig usage(); 3250cb2e609SWolfgang Helbig printcc(); 3260cb2e609SWolfgang Helbig return (0); 3270cb2e609SWolfgang Helbig break; 3280cb2e609SWolfgang Helbig case 's': 3290cb2e609SWolfgang Helbig if (flag_backward) 3300cb2e609SWolfgang Helbig usage(); 3310cb2e609SWolfgang Helbig q = switches + 3320cb2e609SWolfgang Helbig sizeof(switches) / sizeof(struct djswitch); 3330cb2e609SWolfgang Helbig for (p = switches; 3340cb2e609SWolfgang Helbig p != q && strcmp(p->cc, optarg) != 0; p++) 3350cb2e609SWolfgang Helbig ; 3360cb2e609SWolfgang Helbig if (p == q) 3370cb2e609SWolfgang Helbig errx(EX_USAGE, 3380cb2e609SWolfgang Helbig "%s: invalid country code", optarg); 3390cb2e609SWolfgang Helbig nswitch = ndaysj(&(p->dt)); 3400cb2e609SWolfgang Helbig break; 3410cb2e609SWolfgang Helbig case 'w': 3420cb2e609SWolfgang Helbig if (flag_backward) 3430cb2e609SWolfgang Helbig usage(); 3440cb2e609SWolfgang Helbig flag_weeks = 1; 3450cb2e609SWolfgang Helbig break; 3460cb2e609SWolfgang Helbig case 'y': 3474af997a8SEdwin Groothuis flag_wholeyear = 1; 3480cb2e609SWolfgang Helbig break; 3490cb2e609SWolfgang Helbig default: 3500cb2e609SWolfgang Helbig usage(); 3510cb2e609SWolfgang Helbig } 3520cb2e609SWolfgang Helbig 3530cb2e609SWolfgang Helbig argc -= optind; 3540cb2e609SWolfgang Helbig argv += optind; 3550cb2e609SWolfgang Helbig 3560c4cafeaSGarrett Wollman switch (argc) { 3570c4cafeaSGarrett Wollman case 2: 3580c4cafeaSGarrett Wollman if (flag_easter) 3590c4cafeaSGarrett Wollman usage(); 3600c4cafeaSGarrett Wollman flag_month = *argv++; 3614af997a8SEdwin Groothuis flag_givenmonth = 1; 3621d0e1dacSEdwin Groothuis m = strtol(flag_month, NULL, 10); 3630c4cafeaSGarrett Wollman /* FALLTHROUGH */ 3640c4cafeaSGarrett Wollman case 1: 3654af997a8SEdwin Groothuis y = atoi(*argv); 3660c4cafeaSGarrett Wollman if (y < 1 || y > 9999) 3674af997a8SEdwin Groothuis errx(EX_USAGE, "year `%s' not in range 1..9999", *argv); 3684af997a8SEdwin Groothuis argv++; 3694af997a8SEdwin Groothuis flag_givenyear = 1; 3700c4cafeaSGarrett Wollman break; 3710c4cafeaSGarrett Wollman case 0: 3724af997a8SEdwin Groothuis if (flag_today != NULL) { 3734af997a8SEdwin Groothuis y = strtol(flag_today, NULL, 10); 3744af997a8SEdwin Groothuis m = strtol(flag_today + 5, NULL, 10); 3754af997a8SEdwin Groothuis } else { 3760cb2e609SWolfgang Helbig time_t t; 3770cb2e609SWolfgang Helbig struct tm *tm; 3780cb2e609SWolfgang Helbig 3790cb2e609SWolfgang Helbig t = time(NULL); 3800cb2e609SWolfgang Helbig tm = localtime(&t); 3810cb2e609SWolfgang Helbig y = tm->tm_year + 1900; 3820cb2e609SWolfgang Helbig m = tm->tm_mon + 1; 3830cb2e609SWolfgang Helbig } 3840cb2e609SWolfgang Helbig break; 3850cb2e609SWolfgang Helbig default: 3860cb2e609SWolfgang Helbig usage(); 3870cb2e609SWolfgang Helbig } 3880cb2e609SWolfgang Helbig 3890c4cafeaSGarrett Wollman if (flag_month != NULL) { 390ba29aec0SGarrett Wollman if (parsemonth(flag_month, &m, &y)) { 3910c4cafeaSGarrett Wollman errx(EX_USAGE, 3920c4cafeaSGarrett Wollman "%s is neither a month number (1..12) nor a name", 3930c4cafeaSGarrett Wollman flag_month); 3940c4cafeaSGarrett Wollman } 395ba29aec0SGarrett Wollman } 3960c4cafeaSGarrett Wollman 3974af997a8SEdwin Groothuis /* 3984af997a8SEdwin Groothuis * What is not supported: 3994af997a8SEdwin Groothuis * -3 with -A or -B 4004af997a8SEdwin Groothuis * -3 displays 3 months, -A and -B change that behaviour. 4014af997a8SEdwin Groothuis * -3 with -y 4024af997a8SEdwin Groothuis * -3 displays 3 months, -y says display a whole year. 4034af997a8SEdwin Groothuis * -3 with a given year but no given month or without -m 4044af997a8SEdwin Groothuis * -3 displays 3 months, no month specified doesn't make clear 4054af997a8SEdwin Groothuis * which three months. 4064af997a8SEdwin Groothuis * -m with a given month 4074af997a8SEdwin Groothuis * conflicting arguments, both specify the same field. 4084af997a8SEdwin Groothuis * -y with -m 4094af997a8SEdwin Groothuis * -y displays the whole year, -m displays a single month. 4104af997a8SEdwin Groothuis * -y with a given month 4114af997a8SEdwin Groothuis * -y displays the whole year, the given month displays a single 4124af997a8SEdwin Groothuis * month. 4134af997a8SEdwin Groothuis * -y with -A or -B 4144af997a8SEdwin Groothuis * -y displays the whole year, -A and -B display extra months. 4154af997a8SEdwin Groothuis */ 4164af997a8SEdwin Groothuis 4174af997a8SEdwin Groothuis /* -3 together with -A or -B. */ 4184af997a8SEdwin Groothuis if (flag_3months && (flag_after || flag_before)) 4194af997a8SEdwin Groothuis errx(EX_USAGE, "-3 together with -A and -B is not supported."); 4204af997a8SEdwin Groothuis /* -3 together with -y. */ 4214af997a8SEdwin Groothuis if (flag_3months && flag_wholeyear) 4224af997a8SEdwin Groothuis errx(EX_USAGE, "-3 together with -y is not supported."); 4234af997a8SEdwin Groothuis /* -3 together with givenyear but no givenmonth. */ 4244af997a8SEdwin Groothuis if (flag_3months && flag_givenyear && 4254af997a8SEdwin Groothuis !(flag_givenmonth || flag_specifiedmonth)) 4264af997a8SEdwin Groothuis errx(EX_USAGE, 4274af997a8SEdwin Groothuis "-3 together with a given year but no given month is " 4284af997a8SEdwin Groothuis "not supported."); 4294af997a8SEdwin Groothuis /* -m together with xx xxxx. */ 4304af997a8SEdwin Groothuis if (flag_specifiedmonth && flag_givenmonth) 4314af997a8SEdwin Groothuis errx(EX_USAGE, 4324af997a8SEdwin Groothuis "-m together with a given month is not supported."); 4334af997a8SEdwin Groothuis /* -y together with -m. */ 4344af997a8SEdwin Groothuis if (flag_wholeyear && flag_specifiedmonth) 4354af997a8SEdwin Groothuis errx(EX_USAGE, "-y together with -m is not supported."); 4364af997a8SEdwin Groothuis /* -y together with xx xxxx. */ 4374af997a8SEdwin Groothuis if (flag_wholeyear && flag_givenmonth) 4384af997a8SEdwin Groothuis errx(EX_USAGE, "-y together a given month is not supported."); 4394af997a8SEdwin Groothuis /* -y together with -A or -B. */ 4404af997a8SEdwin Groothuis if (flag_wholeyear && (flag_before > 0 || flag_after > 0)) 4414af997a8SEdwin Groothuis errx(EX_USAGE, "-y together a -A or -B is not supported."); 4424af997a8SEdwin Groothuis /* The rest should be fine. */ 4434af997a8SEdwin Groothuis 4444af997a8SEdwin Groothuis /* Select the period to display, in order of increasing priority .*/ 4454af997a8SEdwin Groothuis if (flag_wholeyear || 4464af997a8SEdwin Groothuis (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) { 4474af997a8SEdwin Groothuis m = 1; 4484af997a8SEdwin Groothuis before = 0; 4494af997a8SEdwin Groothuis after = 11; 4504af997a8SEdwin Groothuis } 4514af997a8SEdwin Groothuis if (flag_givenyear && flag_givenmonth) { 4524af997a8SEdwin Groothuis before = 0; 4534af997a8SEdwin Groothuis after = 0; 4544af997a8SEdwin Groothuis } 4554af997a8SEdwin Groothuis if (flag_specifiedmonth) { 4564af997a8SEdwin Groothuis before = 0; 4574af997a8SEdwin Groothuis after = 0; 4584af997a8SEdwin Groothuis } 4594af997a8SEdwin Groothuis if (flag_before) { 4604af997a8SEdwin Groothuis before = flag_before; 4614af997a8SEdwin Groothuis } 4624af997a8SEdwin Groothuis if (flag_after) { 4634af997a8SEdwin Groothuis after = flag_after; 4644af997a8SEdwin Groothuis } 4654af997a8SEdwin Groothuis if (flag_3months) { 4664af997a8SEdwin Groothuis before = 1; 4674af997a8SEdwin Groothuis after = 1; 4684af997a8SEdwin Groothuis } 4694af997a8SEdwin Groothuis if (after == -1) 4704af997a8SEdwin Groothuis after = 0; 4714af997a8SEdwin Groothuis if (before == -1) 4724af997a8SEdwin Groothuis before = 0; 4734af997a8SEdwin Groothuis 4744af997a8SEdwin Groothuis /* Highlight a specified day or today .*/ 4750851fbdfSEdwin Groothuis if (flag_highlightdate != NULL) { 4760851fbdfSEdwin Groothuis dt.y = strtol(flag_highlightdate, NULL, 10); 4770851fbdfSEdwin Groothuis dt.m = strtol(flag_highlightdate + 5, NULL, 10); 4780851fbdfSEdwin Groothuis dt.d = strtol(flag_highlightdate + 8, NULL, 10); 4790851fbdfSEdwin Groothuis } else { 4800851fbdfSEdwin Groothuis time_t t; 4810851fbdfSEdwin Groothuis struct tm *tm1; 4820851fbdfSEdwin Groothuis 4830851fbdfSEdwin Groothuis t = time(NULL); 4840851fbdfSEdwin Groothuis tm1 = localtime(&t); 4850851fbdfSEdwin Groothuis dt.y = tm1->tm_year + 1900; 4860851fbdfSEdwin Groothuis dt.m = tm1->tm_mon + 1; 4870851fbdfSEdwin Groothuis dt.d = tm1->tm_mday; 4880851fbdfSEdwin Groothuis } 4894af997a8SEdwin Groothuis highlightdate = sndaysb(&dt); 4900851fbdfSEdwin Groothuis 4914af997a8SEdwin Groothuis /* And now we finally start to calculate and output calendars. */ 4920cb2e609SWolfgang Helbig if (flag_easter) 4930cb2e609SWolfgang Helbig printeaster(y, flag_julian_cal, flag_orthodox); 4940cb2e609SWolfgang Helbig else 4950cb2e609SWolfgang Helbig if (flag_backward) 4964af997a8SEdwin Groothuis monthrangeb(y, m, flag_julian_day, before, after); 4970cb2e609SWolfgang Helbig else 4984af997a8SEdwin Groothuis monthranger(y, m, flag_julian_day, before, after); 4990cb2e609SWolfgang Helbig return (0); 5000cb2e609SWolfgang Helbig } 5010cb2e609SWolfgang Helbig 502a99e4564SPhilippe Charnier static void 5030cb2e609SWolfgang Helbig usage(void) 5040cb2e609SWolfgang Helbig { 5050cb2e609SWolfgang Helbig 5060c4cafeaSGarrett Wollman fputs( 507531a1a4bSEdwin Groothuis "Usage: cal [general options] [-hjy] [[month] year]\n" 508531a1a4bSEdwin Groothuis " cal [general options] [-hj] [-m month] [year]\n" 509531a1a4bSEdwin Groothuis " ncal [general options] [-hJjpwy] [-s country_code] [[month] year]\n" 510531a1a4bSEdwin Groothuis " ncal [general options] [-hJeo] [year]\n" 511531a1a4bSEdwin Groothuis "General options: [-NC3] [-A months] [-B months]\n" 512531a1a4bSEdwin Groothuis "For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]\n", 5130851fbdfSEdwin Groothuis stderr); 5140cb2e609SWolfgang Helbig exit(EX_USAGE); 5150cb2e609SWolfgang Helbig } 5160cb2e609SWolfgang Helbig 5174af997a8SEdwin Groothuis /* Print the assumed switches for all countries. */ 518bf70beceSEd Schouten static void 5190cb2e609SWolfgang Helbig printcc(void) 5200cb2e609SWolfgang Helbig { 5210cb2e609SWolfgang Helbig struct djswitch *p; 5220cb2e609SWolfgang Helbig int n; /* number of lines to print */ 5230cb2e609SWolfgang Helbig int m; /* offset from left to right table entry on the same line */ 5240cb2e609SWolfgang Helbig 525cde26ed2SWolfgang Helbig #define FSTR "%c%s %-15s%4d-%02d-%02d" 526cde26ed2SWolfgang Helbig #define DFLT(p) ((p) == dftswitch ? '*' : ' ') 527cde26ed2SWolfgang Helbig #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 5280cb2e609SWolfgang Helbig 5290cb2e609SWolfgang Helbig n = sizeof(switches) / sizeof(struct djswitch); 5300cb2e609SWolfgang Helbig m = (n + 1) / 2; 5310cb2e609SWolfgang Helbig n /= 2; 532cde26ed2SWolfgang Helbig for (p = switches; p != switches + n; p++) 533cde26ed2SWolfgang Helbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 5340cb2e609SWolfgang Helbig if (m != n) 535cde26ed2SWolfgang Helbig printf(FSTR"\n", FSTRARG(p)); 5360cb2e609SWolfgang Helbig } 5370cb2e609SWolfgang Helbig 5384af997a8SEdwin Groothuis /* Print the date of easter sunday. */ 539bf70beceSEd Schouten static void 5400cb2e609SWolfgang Helbig printeaster(int y, int julian, int orthodox) 5410cb2e609SWolfgang Helbig { 5420cb2e609SWolfgang Helbig date dt; 543cde26ed2SWolfgang Helbig struct tm tm; 5440851fbdfSEdwin Groothuis char buf[MAX_WIDTH]; 545f5e40af2SAndrey A. Chernov static int d_first = -1; 5460cb2e609SWolfgang Helbig 547f5e40af2SAndrey A. Chernov if (d_first < 0) 548f5e40af2SAndrey A. Chernov d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 5490cb2e609SWolfgang Helbig /* force orthodox easter for years before 1583 */ 5500cb2e609SWolfgang Helbig if (y < 1583) 5510cb2e609SWolfgang Helbig orthodox = 1; 5520cb2e609SWolfgang Helbig 5530cb2e609SWolfgang Helbig if (orthodox) 5540cb2e609SWolfgang Helbig if (julian) 5550cb2e609SWolfgang Helbig easteroj(y, &dt); 5560cb2e609SWolfgang Helbig else 5570cb2e609SWolfgang Helbig easterog(y, &dt); 5580cb2e609SWolfgang Helbig else 5590cb2e609SWolfgang Helbig easterg(y, &dt); 560cde26ed2SWolfgang Helbig 561cde26ed2SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 562cde26ed2SWolfgang Helbig tm.tm_year = dt.y - 1900; 563cde26ed2SWolfgang Helbig tm.tm_mon = dt.m - 1; 564cde26ed2SWolfgang Helbig tm.tm_mday = dt.d; 565f5e40af2SAndrey A. Chernov strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 566cde26ed2SWolfgang Helbig printf("%s\n", buf); 5670cb2e609SWolfgang Helbig } 5680cb2e609SWolfgang Helbig 5691d0e1dacSEdwin Groothuis #define MW(mw, me) ((mw) + me) 5700851fbdfSEdwin Groothuis #define DECREASEMONTH(m, y) \ 5710851fbdfSEdwin Groothuis if (--m == 0) { \ 5720851fbdfSEdwin Groothuis m = 12; \ 5730851fbdfSEdwin Groothuis y--; \ 574bd97a998SHajimu UMEMOTO } 5750851fbdfSEdwin Groothuis #define INCREASEMONTH(m, y) \ 5760851fbdfSEdwin Groothuis if (++(m) == 13) { \ 5770851fbdfSEdwin Groothuis (m) = 1; \ 5780851fbdfSEdwin Groothuis (y)++; \ 5790cb2e609SWolfgang Helbig } 5800851fbdfSEdwin Groothuis #define M2Y(m) ((m) / 12) 5810851fbdfSEdwin Groothuis #define M2M(m) (1 + (m) % 12) 5820cb2e609SWolfgang Helbig 5834af997a8SEdwin Groothuis /* Print all months for the period in the range [ before .. y-m .. after ]. */ 584bf70beceSEd Schouten static void 5854af997a8SEdwin Groothuis monthrangeb(int y, int m, int jd_flag, int before, int after) 5860cb2e609SWolfgang Helbig { 5870cb2e609SWolfgang Helbig struct monthlines year[12]; 5880cb2e609SWolfgang Helbig struct weekdays wds; 5890851fbdfSEdwin Groothuis char s[MAX_WIDTH], t[MAX_WIDTH]; 5900851fbdfSEdwin Groothuis wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH]; 5910851fbdfSEdwin Groothuis const char *wdss; 5920cb2e609SWolfgang Helbig int i, j; 5930cb2e609SWolfgang Helbig int mpl; 5940cb2e609SWolfgang Helbig int mw; 5950851fbdfSEdwin Groothuis int m1, m2; 5960851fbdfSEdwin Groothuis int printyearheader; 5970851fbdfSEdwin Groothuis int prevyear = -1; 5980cb2e609SWolfgang Helbig 5990cb2e609SWolfgang Helbig mpl = jd_flag ? 2 : 3; 6000cb2e609SWolfgang Helbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 6010851fbdfSEdwin Groothuis wdss = (mpl == 2) ? " " : ""; 6020cb2e609SWolfgang Helbig 6030851fbdfSEdwin Groothuis while (before != 0) { 6040851fbdfSEdwin Groothuis DECREASEMONTH(m, y); 6050851fbdfSEdwin Groothuis before--; 6060851fbdfSEdwin Groothuis after++; 6070cb2e609SWolfgang Helbig } 6080851fbdfSEdwin Groothuis m1 = y * 12 + m - 1; 6090851fbdfSEdwin Groothuis m2 = m1 + after; 6100851fbdfSEdwin Groothuis 6110851fbdfSEdwin Groothuis mkweekdays(&wds); 6120851fbdfSEdwin Groothuis 6130851fbdfSEdwin Groothuis /* 6140851fbdfSEdwin Groothuis * The year header is printed when there are more than 'mpl' months 6150851fbdfSEdwin Groothuis * and if the first month is a multitude of 'mpl'. 6160851fbdfSEdwin Groothuis * If not, it will print the year behind every month. 6170851fbdfSEdwin Groothuis */ 6180851fbdfSEdwin Groothuis printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 6190851fbdfSEdwin Groothuis 6200851fbdfSEdwin Groothuis m = m1; 6210851fbdfSEdwin Groothuis while (m <= m2) { 6220851fbdfSEdwin Groothuis int count = 0; 6230851fbdfSEdwin Groothuis for (i = 0; i != mpl && m + i <= m2; i++) { 6240851fbdfSEdwin Groothuis mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 6250851fbdfSEdwin Groothuis count++; 6260851fbdfSEdwin Groothuis } 6270851fbdfSEdwin Groothuis 6280851fbdfSEdwin Groothuis /* Empty line between two rows of months */ 6290851fbdfSEdwin Groothuis if (m != m1) 6300851fbdfSEdwin Groothuis printf("\n"); 6310851fbdfSEdwin Groothuis 6324af997a8SEdwin Groothuis /* Year at the top. */ 6330851fbdfSEdwin Groothuis if (printyearheader && M2Y(m) != prevyear) { 6340851fbdfSEdwin Groothuis sprintf(s, "%d", M2Y(m)); 6350851fbdfSEdwin Groothuis printf("%s\n", center(t, s, mpl * mw)); 6360851fbdfSEdwin Groothuis prevyear = M2Y(m); 6370851fbdfSEdwin Groothuis } 6380851fbdfSEdwin Groothuis 6394af997a8SEdwin Groothuis /* Month names. */ 6400851fbdfSEdwin Groothuis for (i = 0; i < count; i++) 6410851fbdfSEdwin Groothuis if (printyearheader) 6420851fbdfSEdwin Groothuis wprintf(L"%-*ls ", 6430851fbdfSEdwin Groothuis mw, wcenter(ws, year[i].name, mw)); 6440851fbdfSEdwin Groothuis else { 645*b74e3739SDon Lewis swprintf(ws, sizeof(ws)/sizeof(ws[0]), 646*b74e3739SDon Lewis L"%-ls %d", year[i].name, M2Y(m + i)); 6470851fbdfSEdwin Groothuis wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw)); 6480851fbdfSEdwin Groothuis } 6490851fbdfSEdwin Groothuis printf("\n"); 6500851fbdfSEdwin Groothuis 6514af997a8SEdwin Groothuis /* Day of the week names. */ 6520851fbdfSEdwin Groothuis for (i = 0; i < count; i++) { 6530851fbdfSEdwin Groothuis wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", 6540851fbdfSEdwin Groothuis wdss, wds.names[6], wdss, wds.names[0], 6550851fbdfSEdwin Groothuis wdss, wds.names[1], wdss, wds.names[2], 6560851fbdfSEdwin Groothuis wdss, wds.names[3], wdss, wds.names[4], 6570851fbdfSEdwin Groothuis wdss, wds.names[5]); 6580851fbdfSEdwin Groothuis } 6590851fbdfSEdwin Groothuis printf("\n"); 6600851fbdfSEdwin Groothuis 6614af997a8SEdwin Groothuis /* And the days of the month. */ 6620851fbdfSEdwin Groothuis for (i = 0; i != 6; i++) { 6630851fbdfSEdwin Groothuis for (j = 0; j < count; j++) 6641d0e1dacSEdwin Groothuis printf("%-*s ", 6651d0e1dacSEdwin Groothuis MW(mw, year[j].extralen[i]), 6661d0e1dacSEdwin Groothuis year[j].lines[i]+1); 6670851fbdfSEdwin Groothuis printf("\n"); 6680851fbdfSEdwin Groothuis } 6690851fbdfSEdwin Groothuis 6700851fbdfSEdwin Groothuis m += mpl; 6710cb2e609SWolfgang Helbig } 6720cb2e609SWolfgang Helbig } 6730cb2e609SWolfgang Helbig 674bf70beceSEd Schouten static void 6754af997a8SEdwin Groothuis monthranger(int y, int m, int jd_flag, int before, int after) 6760851fbdfSEdwin Groothuis { 6770851fbdfSEdwin Groothuis struct monthlines year[12]; 6780851fbdfSEdwin Groothuis struct weekdays wds; 6790851fbdfSEdwin Groothuis char s[MAX_WIDTH], t[MAX_WIDTH]; 6800851fbdfSEdwin Groothuis int i, j; 6810851fbdfSEdwin Groothuis int mpl; 6820851fbdfSEdwin Groothuis int mw; 6830851fbdfSEdwin Groothuis int m1, m2; 6840851fbdfSEdwin Groothuis int prevyear = -1; 6850851fbdfSEdwin Groothuis int printyearheader; 6860851fbdfSEdwin Groothuis 6870851fbdfSEdwin Groothuis mpl = jd_flag ? 3 : 4; 6880851fbdfSEdwin Groothuis mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R; 6890851fbdfSEdwin Groothuis 6900851fbdfSEdwin Groothuis while (before != 0) { 6910851fbdfSEdwin Groothuis DECREASEMONTH(m, y); 6920851fbdfSEdwin Groothuis before--; 6930851fbdfSEdwin Groothuis after++; 6940851fbdfSEdwin Groothuis } 6950851fbdfSEdwin Groothuis m1 = y * 12 + m - 1; 6960851fbdfSEdwin Groothuis m2 = m1 + after; 6970851fbdfSEdwin Groothuis 6980851fbdfSEdwin Groothuis mkweekdays(&wds); 6990851fbdfSEdwin Groothuis 7000851fbdfSEdwin Groothuis /* 7010851fbdfSEdwin Groothuis * The year header is printed when there are more than 'mpl' months 7020851fbdfSEdwin Groothuis * and if the first month is a multitude of 'mpl'. 7030851fbdfSEdwin Groothuis * If not, it will print the year behind every month. 7040851fbdfSEdwin Groothuis */ 7050851fbdfSEdwin Groothuis printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 7060851fbdfSEdwin Groothuis 7070851fbdfSEdwin Groothuis m = m1; 7080851fbdfSEdwin Groothuis while (m <= m2) { 7090851fbdfSEdwin Groothuis int count = 0; 7100851fbdfSEdwin Groothuis for (i = 0; i != mpl && m + i <= m2; i++) { 7110851fbdfSEdwin Groothuis mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 7120851fbdfSEdwin Groothuis count++; 7130851fbdfSEdwin Groothuis } 7140851fbdfSEdwin Groothuis 7154af997a8SEdwin Groothuis /* Empty line between two rows of months. */ 7160851fbdfSEdwin Groothuis if (m != m1) 7170851fbdfSEdwin Groothuis printf("\n"); 7180851fbdfSEdwin Groothuis 7194af997a8SEdwin Groothuis /* Year at the top. */ 7200851fbdfSEdwin Groothuis if (printyearheader && M2Y(m) != prevyear) { 7210851fbdfSEdwin Groothuis sprintf(s, "%d", M2Y(m)); 7220851fbdfSEdwin Groothuis printf("%s\n", center(t, s, mpl * mw)); 7230851fbdfSEdwin Groothuis prevyear = M2Y(m); 7240851fbdfSEdwin Groothuis } 7250851fbdfSEdwin Groothuis 7264af997a8SEdwin Groothuis /* Month names. */ 7270851fbdfSEdwin Groothuis wprintf(L" "); 7280851fbdfSEdwin Groothuis for (i = 0; i < count; i++) 7290851fbdfSEdwin Groothuis if (printyearheader) 7300851fbdfSEdwin Groothuis wprintf(L"%-*ls", mw, year[i].name); 7310851fbdfSEdwin Groothuis else 7320851fbdfSEdwin Groothuis wprintf(L"%-ls %-*d", year[i].name, 7330851fbdfSEdwin Groothuis mw - wcslen(year[i].name) - 1, M2Y(m + i)); 7340851fbdfSEdwin Groothuis printf("\n"); 7350851fbdfSEdwin Groothuis 7364af997a8SEdwin Groothuis /* And the days of the month. */ 7370851fbdfSEdwin Groothuis for (i = 0; i != 7; i++) { 7380851fbdfSEdwin Groothuis /* Week day */ 7390851fbdfSEdwin Groothuis wprintf(L"%.2ls", wds.names[i]); 7400851fbdfSEdwin Groothuis 7410851fbdfSEdwin Groothuis /* Full months */ 7420851fbdfSEdwin Groothuis for (j = 0; j < count; j++) 7430851fbdfSEdwin Groothuis printf("%-*s", 7441d0e1dacSEdwin Groothuis MW(mw, year[j].extralen[i]), 7451d0e1dacSEdwin Groothuis year[j].lines[i]); 7460851fbdfSEdwin Groothuis printf("\n"); 7470851fbdfSEdwin Groothuis } 7480851fbdfSEdwin Groothuis 7494af997a8SEdwin Groothuis /* Week numbers. */ 7500851fbdfSEdwin Groothuis if (flag_weeks) { 7510851fbdfSEdwin Groothuis printf(" "); 7520851fbdfSEdwin Groothuis for (i = 0; i < count; i++) 7530851fbdfSEdwin Groothuis printf("%-*s", mw, year[i].weeks); 7540851fbdfSEdwin Groothuis printf("\n"); 7550851fbdfSEdwin Groothuis } 7560851fbdfSEdwin Groothuis 7570851fbdfSEdwin Groothuis m += mpl; 7580851fbdfSEdwin Groothuis } 7590851fbdfSEdwin Groothuis return; 7600851fbdfSEdwin Groothuis } 7610851fbdfSEdwin Groothuis 762bf70beceSEd Schouten static void 7630851fbdfSEdwin Groothuis mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines) 7640cb2e609SWolfgang Helbig { 7650cb2e609SWolfgang Helbig 7660cb2e609SWolfgang Helbig struct tm tm; /* for strftime printing local names of 7670cb2e609SWolfgang Helbig * months */ 7680cb2e609SWolfgang Helbig date dt; /* handy date */ 7690cb2e609SWolfgang Helbig int dw; /* width of numbers */ 7700cb2e609SWolfgang Helbig int first; /* first day of month */ 7710cb2e609SWolfgang Helbig int firstm; /* first day of first week of month */ 772e454a171SRoman Divacky int i, j, k, l; /* just indices */ 7730cb2e609SWolfgang Helbig int last; /* the first day of next month */ 7740cb2e609SWolfgang Helbig int jan1 = 0; /* the first day of this year */ 7750cb2e609SWolfgang Helbig char *ds; /* pointer to day strings (daystr or 7760cb2e609SWolfgang Helbig * jdaystr) */ 7770cb2e609SWolfgang Helbig 7780cb2e609SWolfgang Helbig /* Set name of month. */ 7790cb2e609SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 7800cb2e609SWolfgang Helbig tm.tm_mon = m; 7814646cea7SDavid Schultz wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 7824646cea7SDavid Schultz L"%OB", &tm); 7834646cea7SDavid Schultz mlines->name[0] = towupper(mlines->name[0]); 7840cb2e609SWolfgang Helbig 7850cb2e609SWolfgang Helbig /* 7860cb2e609SWolfgang Helbig * Set first and last to the day number of the first day of this 7870cb2e609SWolfgang Helbig * month and the first day of next month respectively. Set jan1 to 788f8a0edbaSWolfgang Helbig * the day number of the first day of this year. 7890cb2e609SWolfgang Helbig */ 790f8a0edbaSWolfgang Helbig first = firstday(y, m + 1); 791f8a0edbaSWolfgang Helbig if (m == 11) 792f8a0edbaSWolfgang Helbig last = firstday(y + 1, 1); 793f8a0edbaSWolfgang Helbig else 794f8a0edbaSWolfgang Helbig last = firstday(y, m + 2); 7950cb2e609SWolfgang Helbig 796f8a0edbaSWolfgang Helbig if (jd_flag) 797630d15bbSWolfgang Helbig jan1 = firstday(y, 1); 7980cb2e609SWolfgang Helbig 7990cb2e609SWolfgang Helbig /* 8000cb2e609SWolfgang Helbig * Set firstm to the day number of monday of the first week of 8010cb2e609SWolfgang Helbig * this month. (This might be in the last month) 8020cb2e609SWolfgang Helbig */ 8030cb2e609SWolfgang Helbig firstm = first - weekday(first); 8040cb2e609SWolfgang Helbig 8054af997a8SEdwin Groothuis /* Set ds (daystring) and dw (daywidth) according to the jd_flag. */ 8060cb2e609SWolfgang Helbig if (jd_flag) { 8070cb2e609SWolfgang Helbig ds = jdaystr; 8080cb2e609SWolfgang Helbig dw = 4; 8090cb2e609SWolfgang Helbig } else { 8100cb2e609SWolfgang Helbig ds = daystr; 8110cb2e609SWolfgang Helbig dw = 3; 8120cb2e609SWolfgang Helbig } 8130cb2e609SWolfgang Helbig 8140cb2e609SWolfgang Helbig /* 8150cb2e609SWolfgang Helbig * Fill the lines with day of month or day of year (julian day) 8160cb2e609SWolfgang Helbig * line index: i, each line is one weekday. column index: j, each 8170cb2e609SWolfgang Helbig * column is one day number. print column index: k. 8180cb2e609SWolfgang Helbig */ 8190cb2e609SWolfgang Helbig for (i = 0; i != 7; i++) { 820e454a171SRoman Divacky l = 0; 821e454a171SRoman Divacky for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 8220cb2e609SWolfgang Helbig if (j >= first) { 8230cb2e609SWolfgang Helbig if (jd_flag) 8240cb2e609SWolfgang Helbig dt.d = j - jan1 + 1; 8250cb2e609SWolfgang Helbig else 8260851fbdfSEdwin Groothuis sdater(j, &dt); 827f379d691SEdwin Groothuis if (j == highlightdate && !flag_nohighlight 828f379d691SEdwin Groothuis && isatty(STDOUT_FILENO)) 829cae11c25SEdwin Groothuis highlight(mlines->lines[i] + k, 830cae11c25SEdwin Groothuis ds + dt.d * dw, dw, &l); 831cae11c25SEdwin Groothuis else 832e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, 8330cb2e609SWolfgang Helbig ds + dt.d * dw, dw); 8340cb2e609SWolfgang Helbig } else 835e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, " ", dw); 836e454a171SRoman Divacky } 837e454a171SRoman Divacky mlines->lines[i][k + l] = '\0'; 8381d0e1dacSEdwin Groothuis mlines->extralen[i] = l; 8390cb2e609SWolfgang Helbig } 8400cb2e609SWolfgang Helbig 8414af997a8SEdwin Groothuis /* fill the weeknumbers. */ 8420cb2e609SWolfgang Helbig if (flag_weeks) { 8430cb2e609SWolfgang Helbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 8440cb2e609SWolfgang Helbig if (j <= nswitch) 8450cb2e609SWolfgang Helbig memset(mlines->weeks + k, ' ', dw); 8460cb2e609SWolfgang Helbig else 8470cb2e609SWolfgang Helbig memcpy(mlines->weeks + k, 8480cb2e609SWolfgang Helbig ds + week(j, &i)*dw, dw); 8490cb2e609SWolfgang Helbig mlines->weeks[k] = '\0'; 8500cb2e609SWolfgang Helbig } 8510cb2e609SWolfgang Helbig } 8520cb2e609SWolfgang Helbig 853bf70beceSEd Schouten static void 8540cb2e609SWolfgang Helbig mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 8550cb2e609SWolfgang Helbig { 8560cb2e609SWolfgang Helbig 8570cb2e609SWolfgang Helbig struct tm tm; /* for strftime printing local names of 8580cb2e609SWolfgang Helbig * months */ 8590cb2e609SWolfgang Helbig date dt; /* handy date */ 8600cb2e609SWolfgang Helbig int dw; /* width of numbers */ 8610cb2e609SWolfgang Helbig int first; /* first day of month */ 8620cb2e609SWolfgang Helbig int firsts; /* sunday of first week of month */ 863e454a171SRoman Divacky int i, j, k, l; /* just indices */ 8640cb2e609SWolfgang Helbig int jan1 = 0; /* the first day of this year */ 8650cb2e609SWolfgang Helbig int last; /* the first day of next month */ 8660cb2e609SWolfgang Helbig char *ds; /* pointer to day strings (daystr or 8670cb2e609SWolfgang Helbig * jdaystr) */ 8680cb2e609SWolfgang Helbig 8690cb2e609SWolfgang Helbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 8700cb2e609SWolfgang Helbig if (jd_flag) { 8710cb2e609SWolfgang Helbig ds = jdaystr; 8720cb2e609SWolfgang Helbig dw = 4; 8730cb2e609SWolfgang Helbig } else { 8740cb2e609SWolfgang Helbig ds = daystr; 8750cb2e609SWolfgang Helbig dw = 3; 8760cb2e609SWolfgang Helbig } 8770cb2e609SWolfgang Helbig 8784af997a8SEdwin Groothuis /* Set name of month centered. */ 8790cb2e609SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 8800cb2e609SWolfgang Helbig tm.tm_mon = m; 8814646cea7SDavid Schultz wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 8824646cea7SDavid Schultz L"%OB", &tm); 8834646cea7SDavid Schultz mlines->name[0] = towupper(mlines->name[0]); 8840cb2e609SWolfgang Helbig 8850cb2e609SWolfgang Helbig /* 8860cb2e609SWolfgang Helbig * Set first and last to the day number of the first day of this 8870cb2e609SWolfgang Helbig * month and the first day of next month respectively. Set jan1 to 8880cb2e609SWolfgang Helbig * the day number of Jan 1st of this year. 8890cb2e609SWolfgang Helbig */ 8900cb2e609SWolfgang Helbig dt.y = y; 8910cb2e609SWolfgang Helbig dt.m = m + 1; 8920cb2e609SWolfgang Helbig dt.d = 1; 8930cb2e609SWolfgang Helbig first = sndaysb(&dt); 8940cb2e609SWolfgang Helbig if (m == 11) { 8950cb2e609SWolfgang Helbig dt.y = y + 1; 8960cb2e609SWolfgang Helbig dt.m = 1; 8970cb2e609SWolfgang Helbig dt.d = 1; 8980cb2e609SWolfgang Helbig } else { 8990cb2e609SWolfgang Helbig dt.y = y; 9000cb2e609SWolfgang Helbig dt.m = m + 2; 9010cb2e609SWolfgang Helbig dt.d = 1; 9020cb2e609SWolfgang Helbig } 9030cb2e609SWolfgang Helbig last = sndaysb(&dt); 9040cb2e609SWolfgang Helbig 9050cb2e609SWolfgang Helbig if (jd_flag) { 9060cb2e609SWolfgang Helbig dt.y = y; 9070cb2e609SWolfgang Helbig dt.m = 1; 9080cb2e609SWolfgang Helbig dt.d = 1; 9090cb2e609SWolfgang Helbig jan1 = sndaysb(&dt); 9100cb2e609SWolfgang Helbig } 9110cb2e609SWolfgang Helbig 9120cb2e609SWolfgang Helbig /* 9130cb2e609SWolfgang Helbig * Set firsts to the day number of sunday of the first week of 9140cb2e609SWolfgang Helbig * this month. (This might be in the last month) 9150cb2e609SWolfgang Helbig */ 9160cb2e609SWolfgang Helbig firsts = first - (weekday(first)+1) % 7; 9170cb2e609SWolfgang Helbig 9180cb2e609SWolfgang Helbig /* 9190cb2e609SWolfgang Helbig * Fill the lines with day of month or day of year (Julian day) 9200cb2e609SWolfgang Helbig * line index: i, each line is one week. column index: j, each 9210cb2e609SWolfgang Helbig * column is one day number. print column index: k. 9220cb2e609SWolfgang Helbig */ 9230cb2e609SWolfgang Helbig for (i = 0; i != 6; i++) { 924e454a171SRoman Divacky l = 0; 9250cb2e609SWolfgang Helbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 926e454a171SRoman Divacky j++, k += dw) { 9270cb2e609SWolfgang Helbig if (j >= first) { 9280cb2e609SWolfgang Helbig if (jd_flag) 9290cb2e609SWolfgang Helbig dt.d = j - jan1 + 1; 9300cb2e609SWolfgang Helbig else 9310cb2e609SWolfgang Helbig sdateb(j, &dt); 9324af997a8SEdwin Groothuis if (j == highlightdate && !flag_nohighlight) 933cae11c25SEdwin Groothuis highlight(mlines->lines[i] + k, 934cae11c25SEdwin Groothuis ds + dt.d * dw, dw, &l); 935cae11c25SEdwin Groothuis else 936e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, 9370cb2e609SWolfgang Helbig ds + dt.d * dw, dw); 9380cb2e609SWolfgang Helbig } else 939e454a171SRoman Divacky memcpy(mlines->lines[i] + k + l, " ", dw); 940e454a171SRoman Divacky } 9410cb2e609SWolfgang Helbig if (k == 0) 9420cb2e609SWolfgang Helbig mlines->lines[i][1] = '\0'; 9430cb2e609SWolfgang Helbig else 944e454a171SRoman Divacky mlines->lines[i][k + l] = '\0'; 9451d0e1dacSEdwin Groothuis mlines->extralen[i] = l; 9460cb2e609SWolfgang Helbig } 9470cb2e609SWolfgang Helbig } 9480cb2e609SWolfgang Helbig 9494af997a8SEdwin Groothuis /* Put the local names of weekdays into the wds. */ 950bf70beceSEd Schouten static void 951a4264dceSWolfgang Helbig mkweekdays(struct weekdays *wds) 9520cb2e609SWolfgang Helbig { 953bd97a998SHajimu UMEMOTO int i, len, width = 0; 9540cb2e609SWolfgang Helbig struct tm tm; 9554646cea7SDavid Schultz wchar_t buf[20]; 9560cb2e609SWolfgang Helbig 9570cb2e609SWolfgang Helbig memset(&tm, 0, sizeof(tm)); 9580cb2e609SWolfgang Helbig 9590cb2e609SWolfgang Helbig for (i = 0; i != 7; i++) { 9600cb2e609SWolfgang Helbig tm.tm_wday = (i+1) % 7; 961*b74e3739SDon Lewis wcsftime(buf, sizeof(buf)/sizeof(buf[0]), L"%a", &tm); 96246f39347SHajimu UMEMOTO for (len = 2; len > 0; --len) { 963bd97a998SHajimu UMEMOTO if ((width = wcswidth(buf, len)) <= 2) 964bd97a998SHajimu UMEMOTO break; 965bd97a998SHajimu UMEMOTO } 966bd97a998SHajimu UMEMOTO wmemset(wds->names[i], L'\0', 4); 967bd97a998SHajimu UMEMOTO if (width == 1) 968bd97a998SHajimu UMEMOTO wds->names[i][0] = L' '; 969bd97a998SHajimu UMEMOTO wcsncat(wds->names[i], buf, len); 97046f39347SHajimu UMEMOTO wcsncat(wds->names[i], L" ", 1); 9710cb2e609SWolfgang Helbig } 9720cb2e609SWolfgang Helbig } 9730cb2e609SWolfgang Helbig 9740cb2e609SWolfgang Helbig /* 9754af997a8SEdwin Groothuis * Compute the day number of the first existing date after the first day in 9764af997a8SEdwin Groothuis * month. (the first day in month and even the month might not exist!) 977f8a0edbaSWolfgang Helbig */ 978bf70beceSEd Schouten static int 979f8a0edbaSWolfgang Helbig firstday(int y, int m) 980f8a0edbaSWolfgang Helbig { 981f8a0edbaSWolfgang Helbig date dt; 982f8a0edbaSWolfgang Helbig int nd; 983f8a0edbaSWolfgang Helbig 984f8a0edbaSWolfgang Helbig dt.y = y; 985f8a0edbaSWolfgang Helbig dt.m = m; 986f8a0edbaSWolfgang Helbig dt.d = 1; 9870851fbdfSEdwin Groothuis nd = sndaysr(&dt); 988cf5f6adfSWolfgang Helbig for (;;) { 9890851fbdfSEdwin Groothuis sdater(nd, &dt); 990cf5f6adfSWolfgang Helbig if ((dt.m >= m && dt.y == y) || dt.y > y) 991f8a0edbaSWolfgang Helbig return (nd); 992cf5f6adfSWolfgang Helbig else 993cf5f6adfSWolfgang Helbig nd++; 994cf5f6adfSWolfgang Helbig } 995cf5f6adfSWolfgang Helbig /* NEVER REACHED */ 996f8a0edbaSWolfgang Helbig } 997f8a0edbaSWolfgang Helbig 998f8a0edbaSWolfgang Helbig /* 9990cb2e609SWolfgang Helbig * Compute the number of days from date, obey the local switch from 10000cb2e609SWolfgang Helbig * Julian to Gregorian if specified by the user. 10010cb2e609SWolfgang Helbig */ 1002bf70beceSEd Schouten static int 10030851fbdfSEdwin Groothuis sndaysr(struct date *d) 10040cb2e609SWolfgang Helbig { 10050cb2e609SWolfgang Helbig 10060cb2e609SWolfgang Helbig if (nswitch != 0) 10070cb2e609SWolfgang Helbig if (nswitch < ndaysj(d)) 10080cb2e609SWolfgang Helbig return (ndaysg(d)); 10090cb2e609SWolfgang Helbig else 10100cb2e609SWolfgang Helbig return (ndaysj(d)); 10110cb2e609SWolfgang Helbig else 10120cb2e609SWolfgang Helbig return ndaysg(d); 10130cb2e609SWolfgang Helbig } 10140cb2e609SWolfgang Helbig 10150cb2e609SWolfgang Helbig /* 10160cb2e609SWolfgang Helbig * Compute the number of days from date, obey the switch from 10170cb2e609SWolfgang Helbig * Julian to Gregorian as used by UK and her colonies. 10180cb2e609SWolfgang Helbig */ 1019bf70beceSEd Schouten static int 10200cb2e609SWolfgang Helbig sndaysb(struct date *d) 10210cb2e609SWolfgang Helbig { 10220cb2e609SWolfgang Helbig 10230cb2e609SWolfgang Helbig if (nswitchb < ndaysj(d)) 10240cb2e609SWolfgang Helbig return (ndaysg(d)); 10250cb2e609SWolfgang Helbig else 10260cb2e609SWolfgang Helbig return (ndaysj(d)); 10270cb2e609SWolfgang Helbig } 10280cb2e609SWolfgang Helbig 10294af997a8SEdwin Groothuis /* Inverse of sndays. */ 1030bf70beceSEd Schouten static struct date * 10310851fbdfSEdwin Groothuis sdater(int nd, struct date *d) 10320cb2e609SWolfgang Helbig { 10330cb2e609SWolfgang Helbig 10340cb2e609SWolfgang Helbig if (nswitch < nd) 10350cb2e609SWolfgang Helbig return (gdate(nd, d)); 10360cb2e609SWolfgang Helbig else 10370cb2e609SWolfgang Helbig return (jdate(nd, d)); 10380cb2e609SWolfgang Helbig } 10390cb2e609SWolfgang Helbig 10404af997a8SEdwin Groothuis /* Inverse of sndaysb. */ 1041bf70beceSEd Schouten static struct date * 10420cb2e609SWolfgang Helbig sdateb(int nd, struct date *d) 10430cb2e609SWolfgang Helbig { 10440cb2e609SWolfgang Helbig 10450cb2e609SWolfgang Helbig if (nswitchb < nd) 10460cb2e609SWolfgang Helbig return (gdate(nd, d)); 10470cb2e609SWolfgang Helbig else 10480cb2e609SWolfgang Helbig return (jdate(nd, d)); 10490cb2e609SWolfgang Helbig } 10500cb2e609SWolfgang Helbig 10514af997a8SEdwin Groothuis /* Center string t in string s of length w by putting enough leading blanks. */ 1052bf70beceSEd Schouten static char * 10530cb2e609SWolfgang Helbig center(char *s, char *t, int w) 10540cb2e609SWolfgang Helbig { 10550851fbdfSEdwin Groothuis char blanks[MAX_WIDTH]; 10560cb2e609SWolfgang Helbig 10570cb2e609SWolfgang Helbig memset(blanks, ' ', sizeof(blanks)); 10580cb2e609SWolfgang Helbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 10590cb2e609SWolfgang Helbig return (s); 10600cb2e609SWolfgang Helbig } 1061ac6d1c22SPeter Pentchev 10624af997a8SEdwin Groothuis /* Center string t in string s of length w by putting enough leading blanks. */ 1063bf70beceSEd Schouten static wchar_t * 10644646cea7SDavid Schultz wcenter(wchar_t *s, wchar_t *t, int w) 10654646cea7SDavid Schultz { 10660851fbdfSEdwin Groothuis char blanks[MAX_WIDTH]; 10674646cea7SDavid Schultz 10684646cea7SDavid Schultz memset(blanks, ' ', sizeof(blanks)); 10694646cea7SDavid Schultz swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 10704646cea7SDavid Schultz return (s); 10714646cea7SDavid Schultz } 10724646cea7SDavid Schultz 1073bf70beceSEd Schouten static int 1074ba29aec0SGarrett Wollman parsemonth(const char *s, int *m, int *y) 1075ac6d1c22SPeter Pentchev { 1076ba29aec0SGarrett Wollman int nm, ny; 1077ac6d1c22SPeter Pentchev char *cp; 1078ac6d1c22SPeter Pentchev struct tm tm; 1079ac6d1c22SPeter Pentchev 1080ba29aec0SGarrett Wollman nm = (int)strtol(s, &cp, 10); 1081ba29aec0SGarrett Wollman if (cp != s) { 1082ba29aec0SGarrett Wollman ny = *y; 1083ba29aec0SGarrett Wollman if (*cp == '\0') { 1084ba29aec0SGarrett Wollman ; /* no special action */ 1085ba29aec0SGarrett Wollman } else if (*cp == 'f' || *cp == 'F') { 1086ba29aec0SGarrett Wollman if (nm <= *m) 1087ba29aec0SGarrett Wollman ny++; 1088ba29aec0SGarrett Wollman } else if (*cp == 'p' || *cp == 'P') { 1089ba29aec0SGarrett Wollman if (nm >= *m) 1090ba29aec0SGarrett Wollman ny--; 1091ba29aec0SGarrett Wollman } else 1092ba29aec0SGarrett Wollman return (1); 1093ba29aec0SGarrett Wollman if (nm < 1 || nm > 12) 1094ba29aec0SGarrett Wollman return 1; 1095ba29aec0SGarrett Wollman *m = nm; 1096ba29aec0SGarrett Wollman *y = ny; 1097ac6d1c22SPeter Pentchev return (0); 1098ac6d1c22SPeter Pentchev } 1099ba29aec0SGarrett Wollman if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 1100ba29aec0SGarrett Wollman *m = tm.tm_mon + 1; 1101ba29aec0SGarrett Wollman return (0); 1102ba29aec0SGarrett Wollman } 1103ba29aec0SGarrett Wollman return (1); 1104ba29aec0SGarrett Wollman } 1105cae11c25SEdwin Groothuis 1106bf70beceSEd Schouten static void 1107cae11c25SEdwin Groothuis highlight(char *dst, char *src, int len, int *extralen) 1108cae11c25SEdwin Groothuis { 1109cae11c25SEdwin Groothuis static int first = 1; 1110cae11c25SEdwin Groothuis static const char *term_so, *term_se; 1111cae11c25SEdwin Groothuis 1112cae11c25SEdwin Groothuis if (first) { 1113cae11c25SEdwin Groothuis char tbuf[1024], cbuf[512], *b; 1114cae11c25SEdwin Groothuis 1115cae11c25SEdwin Groothuis term_se = term_so = NULL; 1116cae11c25SEdwin Groothuis 11174af997a8SEdwin Groothuis /* On how to highlight on this type of terminal (if any). */ 1118cae11c25SEdwin Groothuis if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) { 1119cae11c25SEdwin Groothuis b = cbuf; 1120cae11c25SEdwin Groothuis term_so = tgetstr("so", &b); 1121cae11c25SEdwin Groothuis term_se = tgetstr("se", &b); 1122cae11c25SEdwin Groothuis } 1123cae11c25SEdwin Groothuis 1124cae11c25SEdwin Groothuis first = 0; 1125cae11c25SEdwin Groothuis } 1126cae11c25SEdwin Groothuis 1127cae11c25SEdwin Groothuis /* 1128cae11c25SEdwin Groothuis * This check is not necessary, should have been handled before calling 1129cae11c25SEdwin Groothuis * this function. 1130cae11c25SEdwin Groothuis */ 1131cae11c25SEdwin Groothuis if (flag_nohighlight) { 1132cae11c25SEdwin Groothuis memcpy(dst, src, len); 1133cae11c25SEdwin Groothuis return; 1134cae11c25SEdwin Groothuis } 1135cae11c25SEdwin Groothuis 11364af997a8SEdwin Groothuis /* 11374af997a8SEdwin Groothuis * If it is a real terminal, use the data from the termcap database. 11384af997a8SEdwin Groothuis */ 1139cae11c25SEdwin Groothuis if (term_so != NULL && term_se != NULL) { 11404af997a8SEdwin Groothuis /* separator. */ 1141cae11c25SEdwin Groothuis dst[0] = ' '; 1142cae11c25SEdwin Groothuis dst++; 11434af997a8SEdwin Groothuis /* highlight on. */ 1144cae11c25SEdwin Groothuis memcpy(dst, term_so, strlen(term_so)); 1145cae11c25SEdwin Groothuis dst += strlen(term_so); 11464af997a8SEdwin Groothuis /* the actual text. (minus leading space) */ 1147cae11c25SEdwin Groothuis len--; 1148cae11c25SEdwin Groothuis src++; 1149cae11c25SEdwin Groothuis memcpy(dst, src, len); 1150cae11c25SEdwin Groothuis dst += len; 11514af997a8SEdwin Groothuis /* highlight off. */ 1152cae11c25SEdwin Groothuis memcpy(dst, term_se, strlen(term_se)); 1153cae11c25SEdwin Groothuis *extralen = strlen(term_so) + strlen(term_se); 1154cae11c25SEdwin Groothuis return; 1155cae11c25SEdwin Groothuis } 1156cae11c25SEdwin Groothuis 1157cae11c25SEdwin Groothuis /* 11584af997a8SEdwin Groothuis * Otherwise, print a _, backspace and the letter. 1159cae11c25SEdwin Groothuis */ 1160cae11c25SEdwin Groothuis *extralen = 0; 11614af997a8SEdwin Groothuis /* skip leading space. */ 1162cae11c25SEdwin Groothuis src++; 1163cae11c25SEdwin Groothuis len--; 11644af997a8SEdwin Groothuis /* separator. */ 1165cae11c25SEdwin Groothuis dst[0] = ' '; 1166cae11c25SEdwin Groothuis dst++; 1167cae11c25SEdwin Groothuis while (len > 0) { 11684af997a8SEdwin Groothuis /* _ and backspace. */ 1169cae11c25SEdwin Groothuis memcpy(dst, "_\010", 2); 1170cae11c25SEdwin Groothuis dst += 2; 1171cae11c25SEdwin Groothuis *extralen += 2; 11724af997a8SEdwin Groothuis /* the character. */ 1173cae11c25SEdwin Groothuis *dst++ = *src++; 1174cae11c25SEdwin Groothuis len--; 1175cae11c25SEdwin Groothuis } 11764af997a8SEdwin Groothuis return; 1177cae11c25SEdwin Groothuis } 1178