131744Shelbig/*- 231744Shelbig * Copyright (c) 1997 Wolfgang Helbig 331744Shelbig * All rights reserved. 431744Shelbig * 531744Shelbig * Redistribution and use in source and binary forms, with or without 631744Shelbig * modification, are permitted provided that the following conditions 731744Shelbig * are met: 831744Shelbig * 1. Redistributions of source code must retain the above copyright 931744Shelbig * notice, this list of conditions and the following disclaimer. 1031744Shelbig * 2. Redistributions in binary form must reproduce the above copyright 1131744Shelbig * notice, this list of conditions and the following disclaimer in the 1231744Shelbig * documentation and/or other materials provided with the distribution. 1331744Shelbig * 1431744Shelbig * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1531744Shelbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1631744Shelbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1731744Shelbig * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1831744Shelbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1931744Shelbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2031744Shelbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2131744Shelbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2231744Shelbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2331744Shelbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2431744Shelbig * SUCH DAMAGE. 2531744Shelbig */ 2632310Scharnier 27241737Sed#include <sys/cdefs.h> 28241737Sed__FBSDID("$FreeBSD$"); 2932310Scharnier 3031744Shelbig#include <calendar.h> 31200462Sdelphij#include <ctype.h> 3231744Shelbig#include <err.h> 3374573Sache#include <langinfo.h> 34212032Sedwin#include <libgen.h> 3531744Shelbig#include <locale.h> 3631744Shelbig#include <stdio.h> 3731744Shelbig#include <stdlib.h> 3831744Shelbig#include <string.h> 3931744Shelbig#include <sysexits.h> 4031744Shelbig#include <time.h> 4131744Shelbig#include <unistd.h> 42189804Sdas#include <wchar.h> 43189804Sdas#include <wctype.h> 44191330Srdivacky#include <term.h> 45191330Srdivacky#undef lines /* term.h defines this */ 4631744Shelbig 47204697Sedwin/* Width of one month with backward compatibility and in regular mode*/ 4831744Shelbig#define MONTH_WIDTH_B_J 27 4931744Shelbig#define MONTH_WIDTH_B 20 5031744Shelbig 51204697Sedwin#define MONTH_WIDTH_R_J 24 52204697Sedwin#define MONTH_WIDTH_R 18 5331744Shelbig 54191330Srdivacky#define MAX_WIDTH 64 5531744Shelbig 5631744Shelbigtypedef struct date date; 5731744Shelbig 5831744Shelbigstruct monthlines { 59189804Sdas wchar_t name[MAX_WIDTH + 1]; 6031744Shelbig char lines[7][MAX_WIDTH + 1]; 6131744Shelbig char weeks[MAX_WIDTH + 1]; 62204908Sedwin unsigned int extralen[7]; 6331744Shelbig}; 6431744Shelbig 6531744Shelbigstruct weekdays { 66189804Sdas wchar_t names[7][4]; 6731744Shelbig}; 6831744Shelbig 6931744Shelbig/* The switches from Julian to Gregorian in some countries */ 7031744Shelbigstatic struct djswitch { 7195641Smarkm const char *cc; /* Country code according to ISO 3166 */ 7295641Smarkm const char *nm; /* Name of country */ 7331744Shelbig date dt; /* Last day of Julian calendar */ 7431744Shelbig} switches[] = { 7553963Sache {"AL", "Albania", {1912, 11, 30}}, 7653963Sache {"AT", "Austria", {1583, 10, 5}}, 7753963Sache {"AU", "Australia", {1752, 9, 2}}, 7853963Sache {"BE", "Belgium", {1582, 12, 14}}, 7953963Sache {"BG", "Bulgaria", {1916, 3, 18}}, 8053963Sache {"CA", "Canada", {1752, 9, 2}}, 8153963Sache {"CH", "Switzerland", {1655, 2, 28}}, 8253963Sache {"CN", "China", {1911, 12, 18}}, 8353963Sache {"CZ", "Czech Republic",{1584, 1, 6}}, 8453963Sache {"DE", "Germany", {1700, 2, 18}}, 8553963Sache {"DK", "Denmark", {1700, 2, 18}}, 8653963Sache {"ES", "Spain", {1582, 10, 4}}, 8753963Sache {"FI", "Finland", {1753, 2, 17}}, 8853963Sache {"FR", "France", {1582, 12, 9}}, 8953963Sache {"GB", "United Kingdom",{1752, 9, 2}}, 9053963Sache {"GR", "Greece", {1924, 3, 9}}, 9153963Sache {"HU", "Hungary", {1587, 10, 21}}, 9253963Sache {"IS", "Iceland", {1700, 11, 16}}, 9353963Sache {"IT", "Italy", {1582, 10, 4}}, 9453963Sache {"JP", "Japan", {1918, 12, 18}}, 9553963Sache {"LI", "Lithuania", {1918, 2, 1}}, 9653963Sache {"LN", "Latin", {9999, 05, 31}}, 9753963Sache {"LU", "Luxembourg", {1582, 12, 14}}, 9853963Sache {"LV", "Latvia", {1918, 2, 1}}, 9953963Sache {"NL", "Netherlands", {1582, 12, 14}}, 10053963Sache {"NO", "Norway", {1700, 2, 18}}, 10153963Sache {"PL", "Poland", {1582, 10, 4}}, 10253963Sache {"PT", "Portugal", {1582, 10, 4}}, 10353963Sache {"RO", "Romania", {1919, 3, 31}}, 10453963Sache {"RU", "Russia", {1918, 1, 31}}, 10553963Sache {"SI", "Slovenia", {1919, 3, 4}}, 106232004Smaxim {"SE", "Sweden", {1753, 2, 17}}, 10753963Sache {"TR", "Turkey", {1926, 12, 18}}, 10853963Sache {"US", "United States", {1752, 9, 2}}, 10953963Sache {"YU", "Yugoslavia", {1919, 3, 4}} 11031744Shelbig}; 11131744Shelbig 112241737Sedstatic struct djswitch *dftswitch = 11332215Shelbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 11432215Shelbig /* default switch (should be "US") */ 11532215Shelbig 11631744Shelbig/* Table used to print day of month and week numbers */ 117241737Sedstatic char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 118241737Sed " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 119241737Sed " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 120241737Sed " 48 49 50 51 52 53"; 12131744Shelbig 12231744Shelbig/* Table used to print day of year and week numbers */ 123241737Sedstatic char jdaystr[] = " 1 2 3 4 5 6 7 8 9" 124241737Sed " 10 11 12 13 14 15 16 17 18 19" 125241737Sed " 20 21 22 23 24 25 26 27 28 29" 126241737Sed " 30 31 32 33 34 35 36 37 38 39" 127241737Sed " 40 41 42 43 44 45 46 47 48 49" 128241737Sed " 50 51 52 53 54 55 56 57 58 59" 129241737Sed " 60 61 62 63 64 65 66 67 68 69" 130241737Sed " 70 71 72 73 74 75 76 77 78 79" 131241737Sed " 80 81 82 83 84 85 86 87 88 89" 132241737Sed " 90 91 92 93 94 95 96 97 98 99" 133241737Sed " 100 101 102 103 104 105 106 107 108 109" 134241737Sed " 110 111 112 113 114 115 116 117 118 119" 135241737Sed " 120 121 122 123 124 125 126 127 128 129" 136241737Sed " 130 131 132 133 134 135 136 137 138 139" 137241737Sed " 140 141 142 143 144 145 146 147 148 149" 138241737Sed " 150 151 152 153 154 155 156 157 158 159" 139241737Sed " 160 161 162 163 164 165 166 167 168 169" 140241737Sed " 170 171 172 173 174 175 176 177 178 179" 141241737Sed " 180 181 182 183 184 185 186 187 188 189" 142241737Sed " 190 191 192 193 194 195 196 197 198 199" 143241737Sed " 200 201 202 203 204 205 206 207 208 209" 144241737Sed " 210 211 212 213 214 215 216 217 218 219" 145241737Sed " 220 221 222 223 224 225 226 227 228 229" 146241737Sed " 230 231 232 233 234 235 236 237 238 239" 147241737Sed " 240 241 242 243 244 245 246 247 248 249" 148241737Sed " 250 251 252 253 254 255 256 257 258 259" 149241737Sed " 260 261 262 263 264 265 266 267 268 269" 150241737Sed " 270 271 272 273 274 275 276 277 278 279" 151241737Sed " 280 281 282 283 284 285 286 287 288 289" 152241737Sed " 290 291 292 293 294 295 296 297 298 299" 153241737Sed " 300 301 302 303 304 305 306 307 308 309" 154241737Sed " 310 311 312 313 314 315 316 317 318 319" 155241737Sed " 320 321 322 323 324 325 326 327 328 329" 156241737Sed " 330 331 332 333 334 335 336 337 338 339" 157241737Sed " 340 341 342 343 344 345 346 347 348 349" 158241737Sed " 350 351 352 353 354 355 356 357 358 359" 159241737Sed " 360 361 362 363 364 365 366"; 16031744Shelbig 161241737Sedstatic int flag_nohighlight; /* user doesn't want a highlighted today */ 162241737Sedstatic int flag_weeks; /* user wants number of week */ 163241737Sedstatic int nswitch; /* user defined switch date */ 164241737Sedstatic int nswitchb; /* switch date for backward compatibility */ 165241737Sedstatic int highlightdate; 16631744Shelbig 167241737Sedstatic char *center(char *s, char *t, int w); 168241737Sedstatic wchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 169241737Sedstatic int firstday(int y, int m); 170241737Sedstatic void highlight(char *dst, char *src, int len, int *extraletters); 171241737Sedstatic void mkmonthr(int year, int month, int jd_flag, 172241737Sed struct monthlines * monthl); 173241737Sedstatic void mkmonthb(int year, int month, int jd_flag, 174241737Sed struct monthlines * monthl); 175241737Sedstatic void mkweekdays(struct weekdays * wds); 176241737Sedstatic void monthranger(int year, int m, int jd_flag, 177241737Sed int before, int after); 178241737Sedstatic void monthrangeb(int year, int m, int jd_flag, 179241737Sed int before, int after); 180241737Sedstatic int parsemonth(const char *s, int *m, int *y); 181241737Sedstatic void printcc(void); 182241737Sedstatic void printeaster(int year, int julian, int orthodox); 183241737Sedstatic date *sdater(int ndays, struct date * d); 184241737Sedstatic date *sdateb(int ndays, struct date * d); 185241737Sedstatic int sndaysr(struct date * d); 186241737Sedstatic int sndaysb(struct date * d); 187204697Sedwinstatic void usage(void); 18831744Shelbig 18931744Shelbigint 19031744Shelbigmain(int argc, char *argv[]) 19131744Shelbig{ 19231744Shelbig struct djswitch *p, *q; /* to search user defined switch date */ 19331744Shelbig date never = {10000, 1, 1}; /* outside valid range of dates */ 19431744Shelbig date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 195204697Sedwin date dt; 19631744Shelbig int ch; /* holds the option character */ 19731744Shelbig int m = 0; /* month */ 19831744Shelbig int y = 0; /* year */ 19931744Shelbig int flag_backward = 0; /* user called cal--backward compat. */ 200205071Sedwin int flag_wholeyear = 0; /* user wants the whole year */ 20131744Shelbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 202205071Sedwin int flag_julian_day = 0; /* user wants the Julian day numbers */ 203205071Sedwin int flag_orthodox = 0; /* user wants Orthodox easter */ 204205071Sedwin int flag_easter = 0; /* user wants easter date */ 205205071Sedwin int flag_3months = 0; /* user wants 3 month display (-3) */ 206205071Sedwin int flag_after = 0; /* user wants to see months after */ 207205071Sedwin int flag_before = 0; /* user wants to see months before */ 208205071Sedwin int flag_specifiedmonth = 0;/* user wants to see this month (-m) */ 209205071Sedwin int flag_givenmonth = 0; /* user has specified month [n] */ 210205071Sedwin int flag_givenyear = 0; /* user has specified year [n] */ 21131744Shelbig char *cp; /* character pointer */ 212205071Sedwin char *flag_today = NULL; /* debug: use date as being today */ 213138036Swollman char *flag_month = NULL; /* requested month as string */ 214205071Sedwin char *flag_highlightdate = NULL; /* debug: date to highlight */ 215204697Sedwin int before, after; 21695641Smarkm const char *locale; /* locale to get country code */ 21731744Shelbig 218204849Sedwin flag_nohighlight = 0; 219204849Sedwin flag_weeks = 0; 220191330Srdivacky 22132215Shelbig /* 22232215Shelbig * Use locale to determine the country code, 22332215Shelbig * and use the country code to determine the default 22432215Shelbig * switchdate and date format from the switches table. 22532215Shelbig */ 22674612Sache if (setlocale(LC_ALL, "") == NULL) 22732215Shelbig warn("setlocale"); 22874612Sache locale = setlocale(LC_TIME, NULL); 22978016Sache if (locale == NULL || 23078016Sache strcmp(locale, "C") == 0 || 23178027Sache strcmp(locale, "POSIX") == 0 || 23278027Sache strcmp(locale, "ASCII") == 0 || 23378027Sache strcmp(locale, "US-ASCII") == 0) 23432215Shelbig locale = "_US"; 23532215Shelbig q = switches + sizeof(switches) / sizeof(struct djswitch); 23632215Shelbig for (p = switches; p != q; p++) 23732215Shelbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 23832215Shelbig break; 23932215Shelbig if (p == q) { 24032215Shelbig nswitch = ndaysj(&dftswitch->dt); 24132215Shelbig } else { 24232215Shelbig nswitch = ndaysj(&p->dt); 24332215Shelbig dftswitch = p; 24432215Shelbig } 24531744Shelbig 24632215Shelbig 24731744Shelbig /* 24831744Shelbig * Get the filename portion of argv[0] and set flag_backward if 24931744Shelbig * this program is called "cal". 25031744Shelbig */ 251212032Sedwin if (strncmp(basename(argv[0]), "cal", strlen("cal")) == 0) 25231744Shelbig flag_backward = 1; 25331744Shelbig 25431744Shelbig /* Set the switch date to United Kingdom if backwards compatible */ 25531744Shelbig if (flag_backward) 25631744Shelbig nswitchb = ndaysj(&ukswitch); 25731744Shelbig 258204697Sedwin before = after = -1; 259204697Sedwin 260205427Sedwin while ((ch = getopt(argc, argv, "3A:B:Cd:eH:hjJm:Nops:wy")) != -1) 26131744Shelbig switch (ch) { 262204697Sedwin case '3': 263205071Sedwin flag_3months = 1; 264204697Sedwin break; 265204697Sedwin case 'A': 266205071Sedwin if (flag_after > 0) 267205071Sedwin errx(EX_USAGE, "Double -A specified"); 268205071Sedwin flag_after = strtol(optarg, NULL, 10); 269205071Sedwin if (flag_after <= 0) 270205071Sedwin errx(EX_USAGE, 271205071Sedwin "Argument to -A must be positive"); 272204697Sedwin break; 273204697Sedwin case 'B': 274205071Sedwin if (flag_before > 0) 275205071Sedwin errx(EX_USAGE, "Double -A specified"); 276205071Sedwin flag_before = strtol(optarg, NULL, 10); 277205071Sedwin if (flag_before <= 0) 278205071Sedwin errx(EX_USAGE, 279205071Sedwin "Argument to -B must be positive"); 280204697Sedwin break; 28131744Shelbig case 'J': 28231744Shelbig if (flag_backward) 28331744Shelbig usage(); 28431744Shelbig nswitch = ndaysj(&never); 28531744Shelbig flag_julian_cal = 1; 28631744Shelbig break; 287205427Sedwin case 'C': 288204697Sedwin flag_backward = 1; 289204697Sedwin break; 290205427Sedwin case 'N': 291205427Sedwin flag_backward = 0; 292205427Sedwin break; 293204697Sedwin case 'd': 294205071Sedwin flag_today = optarg; 295205071Sedwin break; 296205071Sedwin case 'H': 297204697Sedwin flag_highlightdate = optarg; 298204697Sedwin break; 299191364Srdivacky case 'h': 300204849Sedwin flag_nohighlight = 1; 301191364Srdivacky break; 30231744Shelbig case 'e': 30331744Shelbig if (flag_backward) 30431744Shelbig usage(); 30531744Shelbig flag_easter = 1; 30631744Shelbig break; 30731744Shelbig case 'j': 30831744Shelbig flag_julian_day = 1; 30931744Shelbig break; 310138036Swollman case 'm': 311205071Sedwin if (flag_specifiedmonth) 312205071Sedwin errx(EX_USAGE, "Double -m specified"); 313138036Swollman flag_month = optarg; 314205071Sedwin flag_specifiedmonth = 1; 315138036Swollman break; 31631744Shelbig case 'o': 31731744Shelbig if (flag_backward) 31831744Shelbig usage(); 31931744Shelbig flag_orthodox = 1; 32031744Shelbig flag_easter = 1; 32131744Shelbig break; 32231744Shelbig case 'p': 32331744Shelbig if (flag_backward) 32431744Shelbig usage(); 32531744Shelbig printcc(); 32631744Shelbig return (0); 32731744Shelbig break; 32831744Shelbig case 's': 32931744Shelbig if (flag_backward) 33031744Shelbig usage(); 33131744Shelbig q = switches + 33231744Shelbig sizeof(switches) / sizeof(struct djswitch); 33331744Shelbig for (p = switches; 33431744Shelbig p != q && strcmp(p->cc, optarg) != 0; p++) 33531744Shelbig ; 33631744Shelbig if (p == q) 33731744Shelbig errx(EX_USAGE, 33831744Shelbig "%s: invalid country code", optarg); 33931744Shelbig nswitch = ndaysj(&(p->dt)); 34031744Shelbig break; 34131744Shelbig case 'w': 34231744Shelbig if (flag_backward) 34331744Shelbig usage(); 34431744Shelbig flag_weeks = 1; 34531744Shelbig break; 34631744Shelbig case 'y': 347205071Sedwin flag_wholeyear = 1; 34831744Shelbig break; 34931744Shelbig default: 35031744Shelbig usage(); 35131744Shelbig } 35231744Shelbig 35331744Shelbig argc -= optind; 35431744Shelbig argv += optind; 35531744Shelbig 35631744Shelbig switch (argc) { 35731744Shelbig case 2: 35831744Shelbig if (flag_easter) 35931744Shelbig usage(); 360138036Swollman flag_month = *argv++; 361205071Sedwin flag_givenmonth = 1; 362204908Sedwin m = strtol(flag_month, NULL, 10); 36331744Shelbig /* FALLTHROUGH */ 36431744Shelbig case 1: 365205071Sedwin y = atoi(*argv); 36631744Shelbig if (y < 1 || y > 9999) 367205071Sedwin errx(EX_USAGE, "year `%s' not in range 1..9999", *argv); 368205071Sedwin argv++; 369205071Sedwin flag_givenyear = 1; 37031744Shelbig break; 37131744Shelbig case 0: 372205071Sedwin if (flag_today != NULL) { 373205071Sedwin y = strtol(flag_today, NULL, 10); 374205071Sedwin m = strtol(flag_today + 5, NULL, 10); 375205071Sedwin } else { 376138036Swollman time_t t; 377138036Swollman struct tm *tm; 378138036Swollman 379138036Swollman t = time(NULL); 380138036Swollman tm = localtime(&t); 381138036Swollman y = tm->tm_year + 1900; 382138036Swollman m = tm->tm_mon + 1; 383138036Swollman } 38431744Shelbig break; 38531744Shelbig default: 38631744Shelbig usage(); 38731744Shelbig } 38831744Shelbig 389138036Swollman if (flag_month != NULL) { 390186401Swollman if (parsemonth(flag_month, &m, &y)) { 391138036Swollman errx(EX_USAGE, 392138036Swollman "%s is neither a month number (1..12) nor a name", 393138036Swollman flag_month); 394186401Swollman } 395138036Swollman } 396138036Swollman 397205071Sedwin /* 398205071Sedwin * What is not supported: 399205071Sedwin * -3 with -A or -B 400205071Sedwin * -3 displays 3 months, -A and -B change that behaviour. 401205071Sedwin * -3 with -y 402205071Sedwin * -3 displays 3 months, -y says display a whole year. 403205071Sedwin * -3 with a given year but no given month or without -m 404205071Sedwin * -3 displays 3 months, no month specified doesn't make clear 405205071Sedwin * which three months. 406205071Sedwin * -m with a given month 407205071Sedwin * conflicting arguments, both specify the same field. 408205071Sedwin * -y with -m 409205071Sedwin * -y displays the whole year, -m displays a single month. 410205071Sedwin * -y with a given month 411205071Sedwin * -y displays the whole year, the given month displays a single 412205071Sedwin * month. 413205071Sedwin * -y with -A or -B 414205071Sedwin * -y displays the whole year, -A and -B display extra months. 415205071Sedwin */ 416205071Sedwin 417205071Sedwin /* -3 together with -A or -B. */ 418205071Sedwin if (flag_3months && (flag_after || flag_before)) 419205071Sedwin errx(EX_USAGE, "-3 together with -A and -B is not supported."); 420205071Sedwin /* -3 together with -y. */ 421205071Sedwin if (flag_3months && flag_wholeyear) 422205071Sedwin errx(EX_USAGE, "-3 together with -y is not supported."); 423205071Sedwin /* -3 together with givenyear but no givenmonth. */ 424205071Sedwin if (flag_3months && flag_givenyear && 425205071Sedwin !(flag_givenmonth || flag_specifiedmonth)) 426205071Sedwin errx(EX_USAGE, 427205071Sedwin "-3 together with a given year but no given month is " 428205071Sedwin "not supported."); 429205071Sedwin /* -m together with xx xxxx. */ 430205071Sedwin if (flag_specifiedmonth && flag_givenmonth) 431205071Sedwin errx(EX_USAGE, 432205071Sedwin "-m together with a given month is not supported."); 433205071Sedwin /* -y together with -m. */ 434205071Sedwin if (flag_wholeyear && flag_specifiedmonth) 435205071Sedwin errx(EX_USAGE, "-y together with -m is not supported."); 436205071Sedwin /* -y together with xx xxxx. */ 437205071Sedwin if (flag_wholeyear && flag_givenmonth) 438205071Sedwin errx(EX_USAGE, "-y together a given month is not supported."); 439205071Sedwin /* -y together with -A or -B. */ 440205071Sedwin if (flag_wholeyear && (flag_before > 0 || flag_after > 0)) 441205071Sedwin errx(EX_USAGE, "-y together a -A or -B is not supported."); 442205071Sedwin /* The rest should be fine. */ 443205071Sedwin 444205071Sedwin /* Select the period to display, in order of increasing priority .*/ 445205071Sedwin if (flag_wholeyear || 446205071Sedwin (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) { 447205071Sedwin m = 1; 448205071Sedwin before = 0; 449205071Sedwin after = 11; 450205071Sedwin } 451205071Sedwin if (flag_givenyear && flag_givenmonth) { 452205071Sedwin before = 0; 453205071Sedwin after = 0; 454205071Sedwin } 455205071Sedwin if (flag_specifiedmonth) { 456205071Sedwin before = 0; 457205071Sedwin after = 0; 458205071Sedwin } 459205071Sedwin if (flag_before) { 460205071Sedwin before = flag_before; 461205071Sedwin } 462205071Sedwin if (flag_after) { 463205071Sedwin after = flag_after; 464205071Sedwin } 465205071Sedwin if (flag_3months) { 466205071Sedwin before = 1; 467205071Sedwin after = 1; 468205071Sedwin } 469205071Sedwin if (after == -1) 470205071Sedwin after = 0; 471205071Sedwin if (before == -1) 472205071Sedwin before = 0; 473205071Sedwin 474205071Sedwin /* Highlight a specified day or today .*/ 475204697Sedwin if (flag_highlightdate != NULL) { 476204697Sedwin dt.y = strtol(flag_highlightdate, NULL, 10); 477204697Sedwin dt.m = strtol(flag_highlightdate + 5, NULL, 10); 478204697Sedwin dt.d = strtol(flag_highlightdate + 8, NULL, 10); 479204697Sedwin } else { 480204697Sedwin time_t t; 481204697Sedwin struct tm *tm1; 482204697Sedwin 483204697Sedwin t = time(NULL); 484204697Sedwin tm1 = localtime(&t); 485204697Sedwin dt.y = tm1->tm_year + 1900; 486204697Sedwin dt.m = tm1->tm_mon + 1; 487204697Sedwin dt.d = tm1->tm_mday; 488204697Sedwin } 489205071Sedwin highlightdate = sndaysb(&dt); 490204697Sedwin 491205071Sedwin /* And now we finally start to calculate and output calendars. */ 49231744Shelbig if (flag_easter) 49331744Shelbig printeaster(y, flag_julian_cal, flag_orthodox); 494204697Sedwin else 49531744Shelbig if (flag_backward) 496205071Sedwin monthrangeb(y, m, flag_julian_day, before, after); 49731744Shelbig else 498205071Sedwin monthranger(y, m, flag_julian_day, before, after); 49931744Shelbig return (0); 50031744Shelbig} 50131744Shelbig 50232310Scharnierstatic void 50331744Shelbigusage(void) 50431744Shelbig{ 50531744Shelbig 506138036Swollman fputs( 507205427Sedwin"Usage: cal [general options] [-hjy] [[month] year]\n" 508205427Sedwin" cal [general options] [-hj] [-m month] [year]\n" 509205427Sedwin" ncal [general options] [-hJjpwy] [-s country_code] [[month] year]\n" 510205427Sedwin" ncal [general options] [-hJeo] [year]\n" 511205427Sedwin"General options: [-NC3] [-A months] [-B months]\n" 512205427Sedwin"For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]\n", 513204697Sedwin stderr); 51431744Shelbig exit(EX_USAGE); 51531744Shelbig} 51631744Shelbig 517205071Sedwin/* Print the assumed switches for all countries. */ 518241737Sedstatic void 51931744Shelbigprintcc(void) 52031744Shelbig{ 52131744Shelbig struct djswitch *p; 52231744Shelbig int n; /* number of lines to print */ 52331744Shelbig int m; /* offset from left to right table entry on the same line */ 52431744Shelbig 52532215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d" 52632215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 52732215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 52831744Shelbig 52931744Shelbig n = sizeof(switches) / sizeof(struct djswitch); 53031744Shelbig m = (n + 1) / 2; 53131744Shelbig n /= 2; 53232215Shelbig for (p = switches; p != switches + n; p++) 53332215Shelbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 53431744Shelbig if (m != n) 53532215Shelbig printf(FSTR"\n", FSTRARG(p)); 53631744Shelbig} 53731744Shelbig 538205071Sedwin/* Print the date of easter sunday. */ 539241737Sedstatic void 54031744Shelbigprinteaster(int y, int julian, int orthodox) 54131744Shelbig{ 54232215Shelbig date dt; 54332215Shelbig struct tm tm; 544204697Sedwin char buf[MAX_WIDTH]; 54574573Sache static int d_first = -1; 54631744Shelbig 54774573Sache if (d_first < 0) 54874573Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 54931744Shelbig /* force orthodox easter for years before 1583 */ 55031744Shelbig if (y < 1583) 55131744Shelbig orthodox = 1; 55231744Shelbig 55331744Shelbig if (orthodox) 55431744Shelbig if (julian) 55531744Shelbig easteroj(y, &dt); 55631744Shelbig else 55731744Shelbig easterog(y, &dt); 55831744Shelbig else 55931744Shelbig easterg(y, &dt); 56032215Shelbig 56132215Shelbig memset(&tm, 0, sizeof(tm)); 56232215Shelbig tm.tm_year = dt.y - 1900; 56332215Shelbig tm.tm_mon = dt.m - 1; 56432215Shelbig tm.tm_mday = dt.d; 56574573Sache strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 56632215Shelbig printf("%s\n", buf); 56731744Shelbig} 56831744Shelbig 569204908Sedwin#define MW(mw, me) ((mw) + me) 570204697Sedwin#define DECREASEMONTH(m, y) \ 571204697Sedwin if (--m == 0) { \ 572204697Sedwin m = 12; \ 573204697Sedwin y--; \ 574204697Sedwin } 575204697Sedwin#define INCREASEMONTH(m, y) \ 576204697Sedwin if (++(m) == 13) { \ 577204697Sedwin (m) = 1; \ 578204697Sedwin (y)++; \ 579204697Sedwin } 580204697Sedwin#define M2Y(m) ((m) / 12) 581204697Sedwin#define M2M(m) (1 + (m) % 12) 582204697Sedwin 583205071Sedwin/* Print all months for the period in the range [ before .. y-m .. after ]. */ 584241737Sedstatic void 585205071Sedwinmonthrangeb(int y, int m, int jd_flag, int before, int after) 58631744Shelbig{ 587204697Sedwin struct monthlines year[12]; 58831744Shelbig struct weekdays wds; 589204697Sedwin char s[MAX_WIDTH], t[MAX_WIDTH]; 590204697Sedwin wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH]; 591204697Sedwin const char *wdss; 592204697Sedwin int i, j; 593204697Sedwin int mpl; 594204697Sedwin int mw; 595204697Sedwin int m1, m2; 596204697Sedwin int printyearheader; 597204697Sedwin int prevyear = -1; 59831744Shelbig 599204697Sedwin mpl = jd_flag ? 2 : 3; 600204697Sedwin mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 601204697Sedwin wdss = (mpl == 2) ? " " : ""; 602204697Sedwin 603204697Sedwin while (before != 0) { 604204697Sedwin DECREASEMONTH(m, y); 605204697Sedwin before--; 606204697Sedwin after++; 607194366Sume } 608204697Sedwin m1 = y * 12 + m - 1; 609204697Sedwin m2 = m1 + after; 61031744Shelbig 61131744Shelbig mkweekdays(&wds); 61231744Shelbig 613204697Sedwin /* 614204697Sedwin * The year header is printed when there are more than 'mpl' months 615204697Sedwin * and if the first month is a multitude of 'mpl'. 616204697Sedwin * If not, it will print the year behind every month. 617204697Sedwin */ 618204697Sedwin printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 61931744Shelbig 620204697Sedwin m = m1; 621204697Sedwin while (m <= m2) { 622204697Sedwin int count = 0; 623204697Sedwin for (i = 0; i != mpl && m + i <= m2; i++) { 624204697Sedwin mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 625204697Sedwin count++; 626204697Sedwin } 62731744Shelbig 628204697Sedwin /* Empty line between two rows of months */ 629204697Sedwin if (m != m1) 630204697Sedwin printf("\n"); 63131744Shelbig 632205071Sedwin /* Year at the top. */ 633204697Sedwin if (printyearheader && M2Y(m) != prevyear) { 634204697Sedwin sprintf(s, "%d", M2Y(m)); 635204697Sedwin printf("%s\n", center(t, s, mpl * mw)); 636204697Sedwin prevyear = M2Y(m); 637204697Sedwin } 63831744Shelbig 639205071Sedwin /* Month names. */ 640204697Sedwin for (i = 0; i < count; i++) 641204697Sedwin if (printyearheader) 642204697Sedwin wprintf(L"%-*ls ", 643204697Sedwin mw, wcenter(ws, year[i].name, mw)); 644204697Sedwin else { 645204697Sedwin swprintf(ws, sizeof(ws), L"%-ls %d", 646204697Sedwin year[i].name, M2Y(m + i)); 647204697Sedwin wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw)); 648204697Sedwin } 649204697Sedwin printf("\n"); 65031744Shelbig 651205071Sedwin /* Day of the week names. */ 652204697Sedwin for (i = 0; i < count; i++) { 653204697Sedwin wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", 654204697Sedwin wdss, wds.names[6], wdss, wds.names[0], 655204697Sedwin wdss, wds.names[1], wdss, wds.names[2], 656204697Sedwin wdss, wds.names[3], wdss, wds.names[4], 657204697Sedwin wdss, wds.names[5]); 658204697Sedwin } 659204697Sedwin printf("\n"); 66031744Shelbig 661205071Sedwin /* And the days of the month. */ 662204697Sedwin for (i = 0; i != 6; i++) { 663204697Sedwin for (j = 0; j < count; j++) 664204908Sedwin printf("%-*s ", 665204908Sedwin MW(mw, year[j].extralen[i]), 666204908Sedwin year[j].lines[i]+1); 667204697Sedwin printf("\n"); 668204697Sedwin } 66931744Shelbig 670204697Sedwin m += mpl; 67131744Shelbig } 67231744Shelbig} 67331744Shelbig 674241737Sedstatic void 675205071Sedwinmonthranger(int y, int m, int jd_flag, int before, int after) 67631744Shelbig{ 67731744Shelbig struct monthlines year[12]; 67831744Shelbig struct weekdays wds; 679204697Sedwin char s[MAX_WIDTH], t[MAX_WIDTH]; 68031744Shelbig int i, j; 68131744Shelbig int mpl; 68231744Shelbig int mw; 683204697Sedwin int m1, m2; 684204697Sedwin int prevyear = -1; 685204697Sedwin int printyearheader; 68631744Shelbig 687204697Sedwin mpl = jd_flag ? 3 : 4; 688204697Sedwin mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R; 689204697Sedwin 690204697Sedwin while (before != 0) { 691204697Sedwin DECREASEMONTH(m, y); 692204697Sedwin before--; 693204697Sedwin after++; 694204697Sedwin } 695204697Sedwin m1 = y * 12 + m - 1; 696204697Sedwin m2 = m1 + after; 697204697Sedwin 69831744Shelbig mkweekdays(&wds); 69931744Shelbig 700204697Sedwin /* 701204697Sedwin * The year header is printed when there are more than 'mpl' months 702204697Sedwin * and if the first month is a multitude of 'mpl'. 703204697Sedwin * If not, it will print the year behind every month. 704204697Sedwin */ 705204697Sedwin printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 70631744Shelbig 707204697Sedwin m = m1; 708204697Sedwin while (m <= m2) { 709204697Sedwin int count = 0; 710204697Sedwin for (i = 0; i != mpl && m + i <= m2; i++) { 711204697Sedwin mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 712204697Sedwin count++; 713204697Sedwin } 71431744Shelbig 715205071Sedwin /* Empty line between two rows of months. */ 716204697Sedwin if (m != m1) 717204697Sedwin printf("\n"); 718204697Sedwin 719205071Sedwin /* Year at the top. */ 720204697Sedwin if (printyearheader && M2Y(m) != prevyear) { 721204697Sedwin sprintf(s, "%d", M2Y(m)); 722204697Sedwin printf("%s\n", center(t, s, mpl * mw)); 723204697Sedwin prevyear = M2Y(m); 724204697Sedwin } 725204697Sedwin 726205071Sedwin /* Month names. */ 727204697Sedwin wprintf(L" "); 728204697Sedwin for (i = 0; i < count; i++) 729204697Sedwin if (printyearheader) 730204697Sedwin wprintf(L"%-*ls", mw, year[i].name); 73131744Shelbig else 732204697Sedwin wprintf(L"%-ls %-*d", year[i].name, 733204697Sedwin mw - wcslen(year[i].name) - 1, M2Y(m + i)); 734204697Sedwin printf("\n"); 73531744Shelbig 736205071Sedwin /* And the days of the month. */ 737204697Sedwin for (i = 0; i != 7; i++) { 738204697Sedwin /* Week day */ 739204697Sedwin wprintf(L"%.2ls", wds.names[i]); 740204697Sedwin 741204697Sedwin /* Full months */ 742204697Sedwin for (j = 0; j < count; j++) 743204697Sedwin printf("%-*s", 744204908Sedwin MW(mw, year[j].extralen[i]), 745204908Sedwin year[j].lines[i]); 746204697Sedwin printf("\n"); 74731744Shelbig } 748204697Sedwin 749205071Sedwin /* Week numbers. */ 750204697Sedwin if (flag_weeks) { 751204697Sedwin printf(" "); 752204697Sedwin for (i = 0; i < count; i++) 753204697Sedwin printf("%-*s", mw, year[i].weeks); 754204697Sedwin printf("\n"); 755204697Sedwin } 756204697Sedwin 757204697Sedwin m += mpl; 75831744Shelbig } 759204697Sedwin return; 76031744Shelbig} 76131744Shelbig 762241737Sedstatic void 763204697Sedwinmkmonthr(int y, int m, int jd_flag, struct monthlines *mlines) 76431744Shelbig{ 76531744Shelbig 76631744Shelbig struct tm tm; /* for strftime printing local names of 76731744Shelbig * months */ 76831744Shelbig date dt; /* handy date */ 76931744Shelbig int dw; /* width of numbers */ 77031744Shelbig int first; /* first day of month */ 77131744Shelbig int firstm; /* first day of first week of month */ 772191330Srdivacky int i, j, k, l; /* just indices */ 77331744Shelbig int last; /* the first day of next month */ 77431744Shelbig int jan1 = 0; /* the first day of this year */ 77531744Shelbig char *ds; /* pointer to day strings (daystr or 77631744Shelbig * jdaystr) */ 77731744Shelbig 77831744Shelbig /* Set name of month. */ 77931744Shelbig memset(&tm, 0, sizeof(tm)); 78031744Shelbig tm.tm_mon = m; 781189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 782189804Sdas L"%OB", &tm); 783189804Sdas mlines->name[0] = towupper(mlines->name[0]); 78431744Shelbig 78531744Shelbig /* 78631744Shelbig * Set first and last to the day number of the first day of this 78731744Shelbig * month and the first day of next month respectively. Set jan1 to 78832282Shelbig * the day number of the first day of this year. 78931744Shelbig */ 79032282Shelbig first = firstday(y, m + 1); 79132282Shelbig if (m == 11) 79232282Shelbig last = firstday(y + 1, 1); 79332282Shelbig else 79432282Shelbig last = firstday(y, m + 2); 79531744Shelbig 79632282Shelbig if (jd_flag) 79732525Shelbig jan1 = firstday(y, 1); 79831744Shelbig 79931744Shelbig /* 80031744Shelbig * Set firstm to the day number of monday of the first week of 80131744Shelbig * this month. (This might be in the last month) 80231744Shelbig */ 80331744Shelbig firstm = first - weekday(first); 80431744Shelbig 805205071Sedwin /* Set ds (daystring) and dw (daywidth) according to the jd_flag. */ 80631744Shelbig if (jd_flag) { 80731744Shelbig ds = jdaystr; 80831744Shelbig dw = 4; 80931744Shelbig } else { 81031744Shelbig ds = daystr; 81131744Shelbig dw = 3; 81231744Shelbig } 81331744Shelbig 81431744Shelbig /* 81531744Shelbig * Fill the lines with day of month or day of year (julian day) 81631744Shelbig * line index: i, each line is one weekday. column index: j, each 81731744Shelbig * column is one day number. print column index: k. 81831744Shelbig */ 81931744Shelbig for (i = 0; i != 7; i++) { 820191330Srdivacky l = 0; 821191330Srdivacky for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 82231744Shelbig if (j >= first) { 82331744Shelbig if (jd_flag) 82431744Shelbig dt.d = j - jan1 + 1; 82531744Shelbig else 826204697Sedwin sdater(j, &dt); 827223931Sedwin if (j == highlightdate && !flag_nohighlight 828223931Sedwin && isatty(STDOUT_FILENO)) 829204849Sedwin highlight(mlines->lines[i] + k, 830204849Sedwin ds + dt.d * dw, dw, &l); 831204849Sedwin else 832204849Sedwin memcpy(mlines->lines[i] + k + l, 833204849Sedwin ds + dt.d * dw, dw); 83431744Shelbig } else 835191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 836191330Srdivacky } 837191330Srdivacky mlines->lines[i][k + l] = '\0'; 838204908Sedwin mlines->extralen[i] = l; 83931744Shelbig } 84031744Shelbig 841205071Sedwin /* fill the weeknumbers. */ 84231744Shelbig if (flag_weeks) { 84331744Shelbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 84431744Shelbig if (j <= nswitch) 84531744Shelbig memset(mlines->weeks + k, ' ', dw); 84631744Shelbig else 84731744Shelbig memcpy(mlines->weeks + k, 84831744Shelbig ds + week(j, &i)*dw, dw); 84931744Shelbig mlines->weeks[k] = '\0'; 85031744Shelbig } 85131744Shelbig} 85231744Shelbig 853241737Sedstatic void 85431744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 85531744Shelbig{ 85631744Shelbig 85731744Shelbig struct tm tm; /* for strftime printing local names of 85831744Shelbig * months */ 85931744Shelbig date dt; /* handy date */ 86031744Shelbig int dw; /* width of numbers */ 86131744Shelbig int first; /* first day of month */ 86231744Shelbig int firsts; /* sunday of first week of month */ 863191330Srdivacky int i, j, k, l; /* just indices */ 86431744Shelbig int jan1 = 0; /* the first day of this year */ 86531744Shelbig int last; /* the first day of next month */ 86631744Shelbig char *ds; /* pointer to day strings (daystr or 86731744Shelbig * jdaystr) */ 86831744Shelbig 86931744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 87031744Shelbig if (jd_flag) { 87131744Shelbig ds = jdaystr; 87231744Shelbig dw = 4; 87331744Shelbig } else { 87431744Shelbig ds = daystr; 87531744Shelbig dw = 3; 87631744Shelbig } 87731744Shelbig 878205071Sedwin /* Set name of month centered. */ 87931744Shelbig memset(&tm, 0, sizeof(tm)); 88031744Shelbig tm.tm_mon = m; 881189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 882189804Sdas L"%OB", &tm); 883189804Sdas mlines->name[0] = towupper(mlines->name[0]); 88431744Shelbig 88531744Shelbig /* 88631744Shelbig * Set first and last to the day number of the first day of this 88731744Shelbig * month and the first day of next month respectively. Set jan1 to 88831744Shelbig * the day number of Jan 1st of this year. 88931744Shelbig */ 89031744Shelbig dt.y = y; 89131744Shelbig dt.m = m + 1; 89231744Shelbig dt.d = 1; 89331744Shelbig first = sndaysb(&dt); 89431744Shelbig if (m == 11) { 89531744Shelbig dt.y = y + 1; 89631744Shelbig dt.m = 1; 89731744Shelbig dt.d = 1; 89831744Shelbig } else { 89931744Shelbig dt.y = y; 90031744Shelbig dt.m = m + 2; 90131744Shelbig dt.d = 1; 90231744Shelbig } 90331744Shelbig last = sndaysb(&dt); 90431744Shelbig 90531744Shelbig if (jd_flag) { 90631744Shelbig dt.y = y; 90731744Shelbig dt.m = 1; 90831744Shelbig dt.d = 1; 90931744Shelbig jan1 = sndaysb(&dt); 91031744Shelbig } 91131744Shelbig 91231744Shelbig /* 91331744Shelbig * Set firsts to the day number of sunday of the first week of 91431744Shelbig * this month. (This might be in the last month) 91531744Shelbig */ 91631744Shelbig firsts = first - (weekday(first)+1) % 7; 91731744Shelbig 91831744Shelbig /* 91931744Shelbig * Fill the lines with day of month or day of year (Julian day) 92031744Shelbig * line index: i, each line is one week. column index: j, each 92131744Shelbig * column is one day number. print column index: k. 92231744Shelbig */ 92331744Shelbig for (i = 0; i != 6; i++) { 924191330Srdivacky l = 0; 92531744Shelbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 926191330Srdivacky j++, k += dw) { 927204849Sedwin if (j >= first) { 928191330Srdivacky if (jd_flag) 929191330Srdivacky dt.d = j - jan1 + 1; 930191330Srdivacky else 931191330Srdivacky sdateb(j, &dt); 932205071Sedwin if (j == highlightdate && !flag_nohighlight) 933204849Sedwin highlight(mlines->lines[i] + k, 934204849Sedwin ds + dt.d * dw, dw, &l); 93531744Shelbig else 936204849Sedwin memcpy(mlines->lines[i] + k + l, 937204849Sedwin ds + dt.d * dw, dw); 93831744Shelbig } else 939191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 940191330Srdivacky } 94131744Shelbig if (k == 0) 94231744Shelbig mlines->lines[i][1] = '\0'; 94331744Shelbig else 944191330Srdivacky mlines->lines[i][k + l] = '\0'; 945204908Sedwin mlines->extralen[i] = l; 94631744Shelbig } 94731744Shelbig} 94831744Shelbig 949205071Sedwin/* Put the local names of weekdays into the wds. */ 950241737Sedstatic void 95132139Shelbigmkweekdays(struct weekdays *wds) 95231744Shelbig{ 953194366Sume int i, len, width = 0; 95431744Shelbig struct tm tm; 955189804Sdas wchar_t buf[20]; 95631744Shelbig 95731744Shelbig memset(&tm, 0, sizeof(tm)); 95831744Shelbig 95931744Shelbig for (i = 0; i != 7; i++) { 96031744Shelbig tm.tm_wday = (i+1) % 7; 961189804Sdas wcsftime(buf, sizeof(buf), L"%a", &tm); 962194447Sume for (len = 2; len > 0; --len) { 963194366Sume if ((width = wcswidth(buf, len)) <= 2) 964194366Sume break; 965194366Sume } 966194366Sume wmemset(wds->names[i], L'\0', 4); 967194366Sume if (width == 1) 968194366Sume wds->names[i][0] = L' '; 969194366Sume wcsncat(wds->names[i], buf, len); 970194447Sume wcsncat(wds->names[i], L" ", 1); 97131744Shelbig } 97231744Shelbig} 97331744Shelbig 97431744Shelbig/* 975205071Sedwin * Compute the day number of the first existing date after the first day in 976205071Sedwin * month. (the first day in month and even the month might not exist!) 97732282Shelbig */ 978241737Sedstatic int 97932282Shelbigfirstday(int y, int m) 98032282Shelbig{ 98132282Shelbig date dt; 98232282Shelbig int nd; 98332282Shelbig 98432282Shelbig dt.y = y; 98532282Shelbig dt.m = m; 98632282Shelbig dt.d = 1; 987204697Sedwin nd = sndaysr(&dt); 98845064Shelbig for (;;) { 989204697Sedwin sdater(nd, &dt); 99045064Shelbig if ((dt.m >= m && dt.y == y) || dt.y > y) 99145064Shelbig return (nd); 99245064Shelbig else 99345064Shelbig nd++; 99445064Shelbig } 99545064Shelbig /* NEVER REACHED */ 99632282Shelbig} 99732282Shelbig 99832282Shelbig/* 99931744Shelbig * Compute the number of days from date, obey the local switch from 100031744Shelbig * Julian to Gregorian if specified by the user. 100131744Shelbig */ 1002241737Sedstatic int 1003204697Sedwinsndaysr(struct date *d) 100431744Shelbig{ 100531744Shelbig 100631744Shelbig if (nswitch != 0) 100731744Shelbig if (nswitch < ndaysj(d)) 100831744Shelbig return (ndaysg(d)); 100931744Shelbig else 101031744Shelbig return (ndaysj(d)); 101131744Shelbig else 101231744Shelbig return ndaysg(d); 101331744Shelbig} 101431744Shelbig 101531744Shelbig/* 101631744Shelbig * Compute the number of days from date, obey the switch from 101731744Shelbig * Julian to Gregorian as used by UK and her colonies. 101831744Shelbig */ 1019241737Sedstatic int 102031744Shelbigsndaysb(struct date *d) 102131744Shelbig{ 102231744Shelbig 102331744Shelbig if (nswitchb < ndaysj(d)) 102431744Shelbig return (ndaysg(d)); 102531744Shelbig else 102631744Shelbig return (ndaysj(d)); 102731744Shelbig} 102831744Shelbig 1029205071Sedwin/* Inverse of sndays. */ 1030241737Sedstatic struct date * 1031204697Sedwinsdater(int nd, struct date *d) 103231744Shelbig{ 103331744Shelbig 103431744Shelbig if (nswitch < nd) 103531744Shelbig return (gdate(nd, d)); 103631744Shelbig else 103731744Shelbig return (jdate(nd, d)); 103831744Shelbig} 103931744Shelbig 1040205071Sedwin/* Inverse of sndaysb. */ 1041241737Sedstatic struct date * 104231744Shelbigsdateb(int nd, struct date *d) 104331744Shelbig{ 104431744Shelbig 104531744Shelbig if (nswitchb < nd) 104631744Shelbig return (gdate(nd, d)); 104731744Shelbig else 104831744Shelbig return (jdate(nd, d)); 104931744Shelbig} 105031744Shelbig 1051205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */ 1052241737Sedstatic char * 105331744Shelbigcenter(char *s, char *t, int w) 105431744Shelbig{ 1055204697Sedwin char blanks[MAX_WIDTH]; 105631744Shelbig 105731744Shelbig memset(blanks, ' ', sizeof(blanks)); 105831744Shelbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 105931744Shelbig return (s); 106031744Shelbig} 1061104369Sroam 1062205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */ 1063241737Sedstatic wchar_t * 1064189804Sdaswcenter(wchar_t *s, wchar_t *t, int w) 1065189804Sdas{ 1066204697Sedwin char blanks[MAX_WIDTH]; 1067189804Sdas 1068189804Sdas memset(blanks, ' ', sizeof(blanks)); 1069189804Sdas swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 1070189804Sdas return (s); 1071189804Sdas} 1072189804Sdas 1073241737Sedstatic int 1074186401Swollmanparsemonth(const char *s, int *m, int *y) 1075104369Sroam{ 1076186401Swollman int nm, ny; 1077104369Sroam char *cp; 1078104369Sroam struct tm tm; 1079104369Sroam 1080186401Swollman nm = (int)strtol(s, &cp, 10); 1081186401Swollman if (cp != s) { 1082186401Swollman ny = *y; 1083186401Swollman if (*cp == '\0') { 1084186401Swollman ; /* no special action */ 1085186401Swollman } else if (*cp == 'f' || *cp == 'F') { 1086186401Swollman if (nm <= *m) 1087186401Swollman ny++; 1088186401Swollman } else if (*cp == 'p' || *cp == 'P') { 1089186401Swollman if (nm >= *m) 1090186401Swollman ny--; 1091186401Swollman } else 1092186401Swollman return (1); 1093186401Swollman if (nm < 1 || nm > 12) 1094186401Swollman return 1; 1095186401Swollman *m = nm; 1096186401Swollman *y = ny; 1097186401Swollman return (0); 1098186401Swollman } 1099186401Swollman if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 1100186401Swollman *m = tm.tm_mon + 1; 1101186401Swollman return (0); 1102186401Swollman } 1103186401Swollman return (1); 1104104369Sroam} 1105204849Sedwin 1106241737Sedstatic void 1107204849Sedwinhighlight(char *dst, char *src, int len, int *extralen) 1108204849Sedwin{ 1109204849Sedwin static int first = 1; 1110204849Sedwin static const char *term_so, *term_se; 1111204849Sedwin 1112204849Sedwin if (first) { 1113204849Sedwin char tbuf[1024], cbuf[512], *b; 1114204849Sedwin 1115204849Sedwin term_se = term_so = NULL; 1116204849Sedwin 1117205071Sedwin /* On how to highlight on this type of terminal (if any). */ 1118204849Sedwin if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) { 1119204849Sedwin b = cbuf; 1120204849Sedwin term_so = tgetstr("so", &b); 1121204849Sedwin term_se = tgetstr("se", &b); 1122204849Sedwin } 1123204849Sedwin 1124204849Sedwin first = 0; 1125204849Sedwin } 1126204849Sedwin 1127204849Sedwin /* 1128204849Sedwin * This check is not necessary, should have been handled before calling 1129204849Sedwin * this function. 1130204849Sedwin */ 1131204849Sedwin if (flag_nohighlight) { 1132204849Sedwin memcpy(dst, src, len); 1133204849Sedwin return; 1134204849Sedwin } 1135204849Sedwin 1136205071Sedwin /* 1137205071Sedwin * If it is a real terminal, use the data from the termcap database. 1138205071Sedwin */ 1139204849Sedwin if (term_so != NULL && term_se != NULL) { 1140205071Sedwin /* separator. */ 1141204849Sedwin dst[0] = ' '; 1142204849Sedwin dst++; 1143205071Sedwin /* highlight on. */ 1144204849Sedwin memcpy(dst, term_so, strlen(term_so)); 1145204849Sedwin dst += strlen(term_so); 1146205071Sedwin /* the actual text. (minus leading space) */ 1147204849Sedwin len--; 1148204849Sedwin src++; 1149204849Sedwin memcpy(dst, src, len); 1150204849Sedwin dst += len; 1151205071Sedwin /* highlight off. */ 1152204849Sedwin memcpy(dst, term_se, strlen(term_se)); 1153204849Sedwin *extralen = strlen(term_so) + strlen(term_se); 1154204849Sedwin return; 1155204849Sedwin } 1156204849Sedwin 1157204849Sedwin /* 1158205071Sedwin * Otherwise, print a _, backspace and the letter. 1159204849Sedwin */ 1160204849Sedwin *extralen = 0; 1161205071Sedwin /* skip leading space. */ 1162204849Sedwin src++; 1163204849Sedwin len--; 1164205071Sedwin /* separator. */ 1165204849Sedwin dst[0] = ' '; 1166204849Sedwin dst++; 1167204849Sedwin while (len > 0) { 1168205071Sedwin /* _ and backspace. */ 1169204849Sedwin memcpy(dst, "_\010", 2); 1170204849Sedwin dst += 2; 1171204849Sedwin *extralen += 2; 1172205071Sedwin /* the character. */ 1173204849Sedwin *dst++ = *src++; 1174204849Sedwin len--; 1175204849Sedwin } 1176205071Sedwin return; 1177204849Sedwin} 1178