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 2732310Scharnier#ifndef lint 2832310Scharnierstatic const char rcsid[] = 2950477Speter "$FreeBSD$"; 3032310Scharnier#endif /* not lint */ 3132310Scharnier 3231744Shelbig#include <calendar.h> 33200462Sdelphij#include <ctype.h> 3431744Shelbig#include <err.h> 3574573Sache#include <langinfo.h> 36212032Sedwin#include <libgen.h> 3731744Shelbig#include <locale.h> 3831744Shelbig#include <stdio.h> 3931744Shelbig#include <stdlib.h> 4031744Shelbig#include <string.h> 4131744Shelbig#include <sysexits.h> 4231744Shelbig#include <time.h> 4331744Shelbig#include <unistd.h> 44189804Sdas#include <wchar.h> 45189804Sdas#include <wctype.h> 46191330Srdivacky#include <term.h> 47191330Srdivacky#undef lines /* term.h defines this */ 4831744Shelbig 49204697Sedwin/* Width of one month with backward compatibility and in regular mode*/ 5031744Shelbig#define MONTH_WIDTH_B_J 27 5131744Shelbig#define MONTH_WIDTH_B 20 5231744Shelbig 53204697Sedwin#define MONTH_WIDTH_R_J 24 54204697Sedwin#define MONTH_WIDTH_R 18 5531744Shelbig 56191330Srdivacky#define MAX_WIDTH 64 5731744Shelbig 5831744Shelbigtypedef struct date date; 5931744Shelbig 6031744Shelbigstruct monthlines { 61189804Sdas wchar_t name[MAX_WIDTH + 1]; 6231744Shelbig char lines[7][MAX_WIDTH + 1]; 6331744Shelbig char weeks[MAX_WIDTH + 1]; 64204908Sedwin unsigned int extralen[7]; 6531744Shelbig}; 6631744Shelbig 6731744Shelbigstruct weekdays { 68189804Sdas wchar_t names[7][4]; 6931744Shelbig}; 7031744Shelbig 7131744Shelbig/* The switches from Julian to Gregorian in some countries */ 7231744Shelbigstatic struct djswitch { 7395641Smarkm const char *cc; /* Country code according to ISO 3166 */ 7495641Smarkm const char *nm; /* Name of country */ 7531744Shelbig date dt; /* Last day of Julian calendar */ 7631744Shelbig} switches[] = { 7753963Sache {"AL", "Albania", {1912, 11, 30}}, 7853963Sache {"AT", "Austria", {1583, 10, 5}}, 7953963Sache {"AU", "Australia", {1752, 9, 2}}, 8053963Sache {"BE", "Belgium", {1582, 12, 14}}, 8153963Sache {"BG", "Bulgaria", {1916, 3, 18}}, 8253963Sache {"CA", "Canada", {1752, 9, 2}}, 8353963Sache {"CH", "Switzerland", {1655, 2, 28}}, 8453963Sache {"CN", "China", {1911, 12, 18}}, 8553963Sache {"CZ", "Czech Republic",{1584, 1, 6}}, 8653963Sache {"DE", "Germany", {1700, 2, 18}}, 8753963Sache {"DK", "Denmark", {1700, 2, 18}}, 8853963Sache {"ES", "Spain", {1582, 10, 4}}, 8953963Sache {"FI", "Finland", {1753, 2, 17}}, 9053963Sache {"FR", "France", {1582, 12, 9}}, 9153963Sache {"GB", "United Kingdom",{1752, 9, 2}}, 9253963Sache {"GR", "Greece", {1924, 3, 9}}, 9353963Sache {"HU", "Hungary", {1587, 10, 21}}, 9453963Sache {"IS", "Iceland", {1700, 11, 16}}, 9553963Sache {"IT", "Italy", {1582, 10, 4}}, 9653963Sache {"JP", "Japan", {1918, 12, 18}}, 9753963Sache {"LI", "Lithuania", {1918, 2, 1}}, 9853963Sache {"LN", "Latin", {9999, 05, 31}}, 9953963Sache {"LU", "Luxembourg", {1582, 12, 14}}, 10053963Sache {"LV", "Latvia", {1918, 2, 1}}, 10153963Sache {"NL", "Netherlands", {1582, 12, 14}}, 10253963Sache {"NO", "Norway", {1700, 2, 18}}, 10353963Sache {"PL", "Poland", {1582, 10, 4}}, 10453963Sache {"PT", "Portugal", {1582, 10, 4}}, 10553963Sache {"RO", "Romania", {1919, 3, 31}}, 10653963Sache {"RU", "Russia", {1918, 1, 31}}, 10753963Sache {"SI", "Slovenia", {1919, 3, 4}}, 108239893Smaxim {"SE", "Sweden", {1753, 2, 17}}, 10953963Sache {"TR", "Turkey", {1926, 12, 18}}, 11053963Sache {"US", "United States", {1752, 9, 2}}, 11153963Sache {"YU", "Yugoslavia", {1919, 3, 4}} 11231744Shelbig}; 11331744Shelbig 11432215Shelbigstruct djswitch *dftswitch = 11532215Shelbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 11632215Shelbig /* default switch (should be "US") */ 11732215Shelbig 11831744Shelbig/* Table used to print day of month and week numbers */ 11931744Shelbigchar daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 12031744Shelbig " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 12131744Shelbig " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 12231744Shelbig " 48 49 50 51 52 53"; 12331744Shelbig 12431744Shelbig/* Table used to print day of year and week numbers */ 12531744Shelbigchar jdaystr[] = " 1 2 3 4 5 6 7 8 9" 12631744Shelbig " 10 11 12 13 14 15 16 17 18 19" 12731744Shelbig " 20 21 22 23 24 25 26 27 28 29" 12831744Shelbig " 30 31 32 33 34 35 36 37 38 39" 12931744Shelbig " 40 41 42 43 44 45 46 47 48 49" 13031744Shelbig " 50 51 52 53 54 55 56 57 58 59" 13131744Shelbig " 60 61 62 63 64 65 66 67 68 69" 13231744Shelbig " 70 71 72 73 74 75 76 77 78 79" 13331744Shelbig " 80 81 82 83 84 85 86 87 88 89" 13431744Shelbig " 90 91 92 93 94 95 96 97 98 99" 13531744Shelbig " 100 101 102 103 104 105 106 107 108 109" 13631744Shelbig " 110 111 112 113 114 115 116 117 118 119" 13731744Shelbig " 120 121 122 123 124 125 126 127 128 129" 13831744Shelbig " 130 131 132 133 134 135 136 137 138 139" 13931744Shelbig " 140 141 142 143 144 145 146 147 148 149" 14031744Shelbig " 150 151 152 153 154 155 156 157 158 159" 14131744Shelbig " 160 161 162 163 164 165 166 167 168 169" 14231744Shelbig " 170 171 172 173 174 175 176 177 178 179" 14331744Shelbig " 180 181 182 183 184 185 186 187 188 189" 14431744Shelbig " 190 191 192 193 194 195 196 197 198 199" 14531744Shelbig " 200 201 202 203 204 205 206 207 208 209" 14631744Shelbig " 210 211 212 213 214 215 216 217 218 219" 14731744Shelbig " 220 221 222 223 224 225 226 227 228 229" 14831744Shelbig " 230 231 232 233 234 235 236 237 238 239" 14931744Shelbig " 240 241 242 243 244 245 246 247 248 249" 15031744Shelbig " 250 251 252 253 254 255 256 257 258 259" 15131744Shelbig " 260 261 262 263 264 265 266 267 268 269" 15231744Shelbig " 270 271 272 273 274 275 276 277 278 279" 15331744Shelbig " 280 281 282 283 284 285 286 287 288 289" 15431744Shelbig " 290 291 292 293 294 295 296 297 298 299" 15531744Shelbig " 300 301 302 303 304 305 306 307 308 309" 15631744Shelbig " 310 311 312 313 314 315 316 317 318 319" 15731744Shelbig " 320 321 322 323 324 325 326 327 328 329" 15831744Shelbig " 330 331 332 333 334 335 336 337 338 339" 15931744Shelbig " 340 341 342 343 344 345 346 347 348 349" 16031744Shelbig " 350 351 352 353 354 355 356 357 358 359" 16131744Shelbig " 360 361 362 363 364 365 366"; 16231744Shelbig 163204849Sedwinint flag_nohighlight; /* user doesn't want a highlighted today */ 16432139Shelbigint flag_weeks; /* user wants number of week */ 16531744Shelbigint nswitch; /* user defined switch date */ 16631744Shelbigint nswitchb; /* switch date for backward compatibility */ 167205071Sedwinint highlightdate; 16831744Shelbig 169204697Sedwinchar *center(char *s, char *t, int w); 170189804Sdaswchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 171205071Sedwinint firstday(int y, int m); 172205071Sedwinvoid highlight(char *dst, char *src, int len, int *extraletters); 173204697Sedwinvoid mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl); 174204697Sedwinvoid mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 175204697Sedwinvoid mkweekdays(struct weekdays * wds); 176205071Sedwinvoid monthranger(int year, int m, int jd_flag, int before, int after); 177205071Sedwinvoid monthrangeb(int year, int m, int jd_flag, int before, int after); 178204697Sedwinint parsemonth(const char *s, int *m, int *y); 179204697Sedwinvoid printcc(void); 180204697Sedwinvoid printeaster(int year, int julian, int orthodox); 181204697Sedwindate *sdater(int ndays, struct date * d); 182204697Sedwindate *sdateb(int ndays, struct date * d); 183204697Sedwinint sndaysr(struct date * d); 184204697Sedwinint sndaysb(struct date * d); 185204697Sedwinstatic void usage(void); 18631744Shelbig 18731744Shelbigint 18831744Shelbigmain(int argc, char *argv[]) 18931744Shelbig{ 19031744Shelbig struct djswitch *p, *q; /* to search user defined switch date */ 19131744Shelbig date never = {10000, 1, 1}; /* outside valid range of dates */ 19231744Shelbig date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 193204697Sedwin date dt; 19431744Shelbig int ch; /* holds the option character */ 19531744Shelbig int m = 0; /* month */ 19631744Shelbig int y = 0; /* year */ 19731744Shelbig int flag_backward = 0; /* user called cal--backward compat. */ 198205071Sedwin int flag_wholeyear = 0; /* user wants the whole year */ 19931744Shelbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 200205071Sedwin int flag_julian_day = 0; /* user wants the Julian day numbers */ 201205071Sedwin int flag_orthodox = 0; /* user wants Orthodox easter */ 202205071Sedwin int flag_easter = 0; /* user wants easter date */ 203205071Sedwin int flag_3months = 0; /* user wants 3 month display (-3) */ 204205071Sedwin int flag_after = 0; /* user wants to see months after */ 205205071Sedwin int flag_before = 0; /* user wants to see months before */ 206205071Sedwin int flag_specifiedmonth = 0;/* user wants to see this month (-m) */ 207205071Sedwin int flag_givenmonth = 0; /* user has specified month [n] */ 208205071Sedwin int flag_givenyear = 0; /* user has specified year [n] */ 20931744Shelbig char *cp; /* character pointer */ 210205071Sedwin char *flag_today = NULL; /* debug: use date as being today */ 211138036Swollman char *flag_month = NULL; /* requested month as string */ 212205071Sedwin char *flag_highlightdate = NULL; /* debug: date to highlight */ 213204697Sedwin int before, after; 21495641Smarkm const char *locale; /* locale to get country code */ 21531744Shelbig 216204849Sedwin flag_nohighlight = 0; 217204849Sedwin flag_weeks = 0; 218191330Srdivacky 21932215Shelbig /* 22032215Shelbig * Use locale to determine the country code, 22132215Shelbig * and use the country code to determine the default 22232215Shelbig * switchdate and date format from the switches table. 22332215Shelbig */ 22474612Sache if (setlocale(LC_ALL, "") == NULL) 22532215Shelbig warn("setlocale"); 22674612Sache locale = setlocale(LC_TIME, NULL); 22778016Sache if (locale == NULL || 22878016Sache strcmp(locale, "C") == 0 || 22978027Sache strcmp(locale, "POSIX") == 0 || 23078027Sache strcmp(locale, "ASCII") == 0 || 23178027Sache strcmp(locale, "US-ASCII") == 0) 23232215Shelbig locale = "_US"; 23332215Shelbig q = switches + sizeof(switches) / sizeof(struct djswitch); 23432215Shelbig for (p = switches; p != q; p++) 23532215Shelbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 23632215Shelbig break; 23732215Shelbig if (p == q) { 23832215Shelbig nswitch = ndaysj(&dftswitch->dt); 23932215Shelbig } else { 24032215Shelbig nswitch = ndaysj(&p->dt); 24132215Shelbig dftswitch = p; 24232215Shelbig } 24331744Shelbig 24432215Shelbig 24531744Shelbig /* 24631744Shelbig * Get the filename portion of argv[0] and set flag_backward if 24731744Shelbig * this program is called "cal". 24831744Shelbig */ 249212032Sedwin if (strncmp(basename(argv[0]), "cal", strlen("cal")) == 0) 25031744Shelbig flag_backward = 1; 25131744Shelbig 25231744Shelbig /* Set the switch date to United Kingdom if backwards compatible */ 25331744Shelbig if (flag_backward) 25431744Shelbig nswitchb = ndaysj(&ukswitch); 25531744Shelbig 256204697Sedwin before = after = -1; 257204697Sedwin 258205427Sedwin while ((ch = getopt(argc, argv, "3A:B:Cd:eH:hjJm:Nops:wy")) != -1) 25931744Shelbig switch (ch) { 260204697Sedwin case '3': 261205071Sedwin flag_3months = 1; 262204697Sedwin break; 263204697Sedwin case 'A': 264205071Sedwin if (flag_after > 0) 265205071Sedwin errx(EX_USAGE, "Double -A specified"); 266205071Sedwin flag_after = strtol(optarg, NULL, 10); 267205071Sedwin if (flag_after <= 0) 268205071Sedwin errx(EX_USAGE, 269205071Sedwin "Argument to -A must be positive"); 270204697Sedwin break; 271204697Sedwin case 'B': 272205071Sedwin if (flag_before > 0) 273205071Sedwin errx(EX_USAGE, "Double -A specified"); 274205071Sedwin flag_before = strtol(optarg, NULL, 10); 275205071Sedwin if (flag_before <= 0) 276205071Sedwin errx(EX_USAGE, 277205071Sedwin "Argument to -B must be positive"); 278204697Sedwin break; 27931744Shelbig case 'J': 28031744Shelbig if (flag_backward) 28131744Shelbig usage(); 28231744Shelbig nswitch = ndaysj(&never); 28331744Shelbig flag_julian_cal = 1; 28431744Shelbig break; 285205427Sedwin case 'C': 286204697Sedwin flag_backward = 1; 287204697Sedwin break; 288205427Sedwin case 'N': 289205427Sedwin flag_backward = 0; 290205427Sedwin break; 291204697Sedwin case 'd': 292205071Sedwin flag_today = optarg; 293205071Sedwin break; 294205071Sedwin case 'H': 295204697Sedwin flag_highlightdate = optarg; 296204697Sedwin break; 297191364Srdivacky case 'h': 298204849Sedwin flag_nohighlight = 1; 299191364Srdivacky break; 30031744Shelbig case 'e': 30131744Shelbig if (flag_backward) 30231744Shelbig usage(); 30331744Shelbig flag_easter = 1; 30431744Shelbig break; 30531744Shelbig case 'j': 30631744Shelbig flag_julian_day = 1; 30731744Shelbig break; 308138036Swollman case 'm': 309205071Sedwin if (flag_specifiedmonth) 310205071Sedwin errx(EX_USAGE, "Double -m specified"); 311138036Swollman flag_month = optarg; 312205071Sedwin flag_specifiedmonth = 1; 313138036Swollman break; 31431744Shelbig case 'o': 31531744Shelbig if (flag_backward) 31631744Shelbig usage(); 31731744Shelbig flag_orthodox = 1; 31831744Shelbig flag_easter = 1; 31931744Shelbig break; 32031744Shelbig case 'p': 32131744Shelbig if (flag_backward) 32231744Shelbig usage(); 32331744Shelbig printcc(); 32431744Shelbig return (0); 32531744Shelbig break; 32631744Shelbig case 's': 32731744Shelbig if (flag_backward) 32831744Shelbig usage(); 32931744Shelbig q = switches + 33031744Shelbig sizeof(switches) / sizeof(struct djswitch); 33131744Shelbig for (p = switches; 33231744Shelbig p != q && strcmp(p->cc, optarg) != 0; p++) 33331744Shelbig ; 33431744Shelbig if (p == q) 33531744Shelbig errx(EX_USAGE, 33631744Shelbig "%s: invalid country code", optarg); 33731744Shelbig nswitch = ndaysj(&(p->dt)); 33831744Shelbig break; 33931744Shelbig case 'w': 34031744Shelbig if (flag_backward) 34131744Shelbig usage(); 34231744Shelbig flag_weeks = 1; 34331744Shelbig break; 34431744Shelbig case 'y': 345205071Sedwin flag_wholeyear = 1; 34631744Shelbig break; 34731744Shelbig default: 34831744Shelbig usage(); 34931744Shelbig } 35031744Shelbig 35131744Shelbig argc -= optind; 35231744Shelbig argv += optind; 35331744Shelbig 35431744Shelbig switch (argc) { 35531744Shelbig case 2: 35631744Shelbig if (flag_easter) 35731744Shelbig usage(); 358138036Swollman flag_month = *argv++; 359205071Sedwin flag_givenmonth = 1; 360204908Sedwin m = strtol(flag_month, NULL, 10); 36131744Shelbig /* FALLTHROUGH */ 36231744Shelbig case 1: 363205071Sedwin y = atoi(*argv); 36431744Shelbig if (y < 1 || y > 9999) 365205071Sedwin errx(EX_USAGE, "year `%s' not in range 1..9999", *argv); 366205071Sedwin argv++; 367205071Sedwin flag_givenyear = 1; 36831744Shelbig break; 36931744Shelbig case 0: 370205071Sedwin if (flag_today != NULL) { 371205071Sedwin y = strtol(flag_today, NULL, 10); 372205071Sedwin m = strtol(flag_today + 5, NULL, 10); 373205071Sedwin } else { 374138036Swollman time_t t; 375138036Swollman struct tm *tm; 376138036Swollman 377138036Swollman t = time(NULL); 378138036Swollman tm = localtime(&t); 379138036Swollman y = tm->tm_year + 1900; 380138036Swollman m = tm->tm_mon + 1; 381138036Swollman } 38231744Shelbig break; 38331744Shelbig default: 38431744Shelbig usage(); 38531744Shelbig } 38631744Shelbig 387138036Swollman if (flag_month != NULL) { 388186401Swollman if (parsemonth(flag_month, &m, &y)) { 389138036Swollman errx(EX_USAGE, 390138036Swollman "%s is neither a month number (1..12) nor a name", 391138036Swollman flag_month); 392186401Swollman } 393138036Swollman } 394138036Swollman 395205071Sedwin /* 396205071Sedwin * What is not supported: 397205071Sedwin * -3 with -A or -B 398205071Sedwin * -3 displays 3 months, -A and -B change that behaviour. 399205071Sedwin * -3 with -y 400205071Sedwin * -3 displays 3 months, -y says display a whole year. 401205071Sedwin * -3 with a given year but no given month or without -m 402205071Sedwin * -3 displays 3 months, no month specified doesn't make clear 403205071Sedwin * which three months. 404205071Sedwin * -m with a given month 405205071Sedwin * conflicting arguments, both specify the same field. 406205071Sedwin * -y with -m 407205071Sedwin * -y displays the whole year, -m displays a single month. 408205071Sedwin * -y with a given month 409205071Sedwin * -y displays the whole year, the given month displays a single 410205071Sedwin * month. 411205071Sedwin * -y with -A or -B 412205071Sedwin * -y displays the whole year, -A and -B display extra months. 413205071Sedwin */ 414205071Sedwin 415205071Sedwin /* -3 together with -A or -B. */ 416205071Sedwin if (flag_3months && (flag_after || flag_before)) 417205071Sedwin errx(EX_USAGE, "-3 together with -A and -B is not supported."); 418205071Sedwin /* -3 together with -y. */ 419205071Sedwin if (flag_3months && flag_wholeyear) 420205071Sedwin errx(EX_USAGE, "-3 together with -y is not supported."); 421205071Sedwin /* -3 together with givenyear but no givenmonth. */ 422205071Sedwin if (flag_3months && flag_givenyear && 423205071Sedwin !(flag_givenmonth || flag_specifiedmonth)) 424205071Sedwin errx(EX_USAGE, 425205071Sedwin "-3 together with a given year but no given month is " 426205071Sedwin "not supported."); 427205071Sedwin /* -m together with xx xxxx. */ 428205071Sedwin if (flag_specifiedmonth && flag_givenmonth) 429205071Sedwin errx(EX_USAGE, 430205071Sedwin "-m together with a given month is not supported."); 431205071Sedwin /* -y together with -m. */ 432205071Sedwin if (flag_wholeyear && flag_specifiedmonth) 433205071Sedwin errx(EX_USAGE, "-y together with -m is not supported."); 434205071Sedwin /* -y together with xx xxxx. */ 435205071Sedwin if (flag_wholeyear && flag_givenmonth) 436205071Sedwin errx(EX_USAGE, "-y together a given month is not supported."); 437205071Sedwin /* -y together with -A or -B. */ 438205071Sedwin if (flag_wholeyear && (flag_before > 0 || flag_after > 0)) 439205071Sedwin errx(EX_USAGE, "-y together a -A or -B is not supported."); 440205071Sedwin /* The rest should be fine. */ 441205071Sedwin 442205071Sedwin /* Select the period to display, in order of increasing priority .*/ 443205071Sedwin if (flag_wholeyear || 444205071Sedwin (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) { 445205071Sedwin m = 1; 446205071Sedwin before = 0; 447205071Sedwin after = 11; 448205071Sedwin } 449205071Sedwin if (flag_givenyear && flag_givenmonth) { 450205071Sedwin before = 0; 451205071Sedwin after = 0; 452205071Sedwin } 453205071Sedwin if (flag_specifiedmonth) { 454205071Sedwin before = 0; 455205071Sedwin after = 0; 456205071Sedwin } 457205071Sedwin if (flag_before) { 458205071Sedwin before = flag_before; 459205071Sedwin } 460205071Sedwin if (flag_after) { 461205071Sedwin after = flag_after; 462205071Sedwin } 463205071Sedwin if (flag_3months) { 464205071Sedwin before = 1; 465205071Sedwin after = 1; 466205071Sedwin } 467205071Sedwin if (after == -1) 468205071Sedwin after = 0; 469205071Sedwin if (before == -1) 470205071Sedwin before = 0; 471205071Sedwin 472205071Sedwin /* Highlight a specified day or today .*/ 473204697Sedwin if (flag_highlightdate != NULL) { 474204697Sedwin dt.y = strtol(flag_highlightdate, NULL, 10); 475204697Sedwin dt.m = strtol(flag_highlightdate + 5, NULL, 10); 476204697Sedwin dt.d = strtol(flag_highlightdate + 8, NULL, 10); 477204697Sedwin } else { 478204697Sedwin time_t t; 479204697Sedwin struct tm *tm1; 480204697Sedwin 481204697Sedwin t = time(NULL); 482204697Sedwin tm1 = localtime(&t); 483204697Sedwin dt.y = tm1->tm_year + 1900; 484204697Sedwin dt.m = tm1->tm_mon + 1; 485204697Sedwin dt.d = tm1->tm_mday; 486204697Sedwin } 487205071Sedwin highlightdate = sndaysb(&dt); 488204697Sedwin 489205071Sedwin /* And now we finally start to calculate and output calendars. */ 49031744Shelbig if (flag_easter) 49131744Shelbig printeaster(y, flag_julian_cal, flag_orthodox); 492204697Sedwin else 49331744Shelbig if (flag_backward) 494205071Sedwin monthrangeb(y, m, flag_julian_day, before, after); 49531744Shelbig else 496205071Sedwin monthranger(y, m, flag_julian_day, before, after); 49731744Shelbig return (0); 49831744Shelbig} 49931744Shelbig 50032310Scharnierstatic void 50131744Shelbigusage(void) 50231744Shelbig{ 50331744Shelbig 504138036Swollman fputs( 505205427Sedwin"Usage: cal [general options] [-hjy] [[month] year]\n" 506205427Sedwin" cal [general options] [-hj] [-m month] [year]\n" 507205427Sedwin" ncal [general options] [-hJjpwy] [-s country_code] [[month] year]\n" 508205427Sedwin" ncal [general options] [-hJeo] [year]\n" 509205427Sedwin"General options: [-NC3] [-A months] [-B months]\n" 510205427Sedwin"For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]\n", 511204697Sedwin stderr); 51231744Shelbig exit(EX_USAGE); 51331744Shelbig} 51431744Shelbig 515205071Sedwin/* Print the assumed switches for all countries. */ 51631744Shelbigvoid 51731744Shelbigprintcc(void) 51831744Shelbig{ 51931744Shelbig struct djswitch *p; 52031744Shelbig int n; /* number of lines to print */ 52131744Shelbig int m; /* offset from left to right table entry on the same line */ 52231744Shelbig 52332215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d" 52432215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 52532215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 52631744Shelbig 52731744Shelbig n = sizeof(switches) / sizeof(struct djswitch); 52831744Shelbig m = (n + 1) / 2; 52931744Shelbig n /= 2; 53032215Shelbig for (p = switches; p != switches + n; p++) 53132215Shelbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 53231744Shelbig if (m != n) 53332215Shelbig printf(FSTR"\n", FSTRARG(p)); 53431744Shelbig} 53531744Shelbig 536205071Sedwin/* Print the date of easter sunday. */ 53731744Shelbigvoid 53831744Shelbigprinteaster(int y, int julian, int orthodox) 53931744Shelbig{ 54032215Shelbig date dt; 54132215Shelbig struct tm tm; 542204697Sedwin char buf[MAX_WIDTH]; 54374573Sache static int d_first = -1; 54431744Shelbig 54574573Sache if (d_first < 0) 54674573Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 54731744Shelbig /* force orthodox easter for years before 1583 */ 54831744Shelbig if (y < 1583) 54931744Shelbig orthodox = 1; 55031744Shelbig 55131744Shelbig if (orthodox) 55231744Shelbig if (julian) 55331744Shelbig easteroj(y, &dt); 55431744Shelbig else 55531744Shelbig easterog(y, &dt); 55631744Shelbig else 55731744Shelbig easterg(y, &dt); 55832215Shelbig 55932215Shelbig memset(&tm, 0, sizeof(tm)); 56032215Shelbig tm.tm_year = dt.y - 1900; 56132215Shelbig tm.tm_mon = dt.m - 1; 56232215Shelbig tm.tm_mday = dt.d; 56374573Sache strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 56432215Shelbig printf("%s\n", buf); 56531744Shelbig} 56631744Shelbig 567204908Sedwin#define MW(mw, me) ((mw) + me) 568204697Sedwin#define DECREASEMONTH(m, y) \ 569204697Sedwin if (--m == 0) { \ 570204697Sedwin m = 12; \ 571204697Sedwin y--; \ 572204697Sedwin } 573204697Sedwin#define INCREASEMONTH(m, y) \ 574204697Sedwin if (++(m) == 13) { \ 575204697Sedwin (m) = 1; \ 576204697Sedwin (y)++; \ 577204697Sedwin } 578204697Sedwin#define M2Y(m) ((m) / 12) 579204697Sedwin#define M2M(m) (1 + (m) % 12) 580204697Sedwin 581205071Sedwin/* Print all months for the period in the range [ before .. y-m .. after ]. */ 58231744Shelbigvoid 583205071Sedwinmonthrangeb(int y, int m, int jd_flag, int before, int after) 58431744Shelbig{ 585204697Sedwin struct monthlines year[12]; 58631744Shelbig struct weekdays wds; 587204697Sedwin char s[MAX_WIDTH], t[MAX_WIDTH]; 588204697Sedwin wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH]; 589204697Sedwin const char *wdss; 590204697Sedwin int i, j; 591204697Sedwin int mpl; 592204697Sedwin int mw; 593204697Sedwin int m1, m2; 594204697Sedwin int printyearheader; 595204697Sedwin int prevyear = -1; 59631744Shelbig 597204697Sedwin mpl = jd_flag ? 2 : 3; 598204697Sedwin mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 599204697Sedwin wdss = (mpl == 2) ? " " : ""; 600204697Sedwin 601204697Sedwin while (before != 0) { 602204697Sedwin DECREASEMONTH(m, y); 603204697Sedwin before--; 604204697Sedwin after++; 605194366Sume } 606204697Sedwin m1 = y * 12 + m - 1; 607204697Sedwin m2 = m1 + after; 60831744Shelbig 60931744Shelbig mkweekdays(&wds); 61031744Shelbig 611204697Sedwin /* 612204697Sedwin * The year header is printed when there are more than 'mpl' months 613204697Sedwin * and if the first month is a multitude of 'mpl'. 614204697Sedwin * If not, it will print the year behind every month. 615204697Sedwin */ 616204697Sedwin printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 61731744Shelbig 618204697Sedwin m = m1; 619204697Sedwin while (m <= m2) { 620204697Sedwin int count = 0; 621204697Sedwin for (i = 0; i != mpl && m + i <= m2; i++) { 622204697Sedwin mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 623204697Sedwin count++; 624204697Sedwin } 62531744Shelbig 626204697Sedwin /* Empty line between two rows of months */ 627204697Sedwin if (m != m1) 628204697Sedwin printf("\n"); 62931744Shelbig 630205071Sedwin /* Year at the top. */ 631204697Sedwin if (printyearheader && M2Y(m) != prevyear) { 632204697Sedwin sprintf(s, "%d", M2Y(m)); 633204697Sedwin printf("%s\n", center(t, s, mpl * mw)); 634204697Sedwin prevyear = M2Y(m); 635204697Sedwin } 63631744Shelbig 637205071Sedwin /* Month names. */ 638204697Sedwin for (i = 0; i < count; i++) 639204697Sedwin if (printyearheader) 640204697Sedwin wprintf(L"%-*ls ", 641204697Sedwin mw, wcenter(ws, year[i].name, mw)); 642204697Sedwin else { 643204697Sedwin swprintf(ws, sizeof(ws), L"%-ls %d", 644204697Sedwin year[i].name, M2Y(m + i)); 645204697Sedwin wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw)); 646204697Sedwin } 647204697Sedwin printf("\n"); 64831744Shelbig 649205071Sedwin /* Day of the week names. */ 650204697Sedwin for (i = 0; i < count; i++) { 651204697Sedwin wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", 652204697Sedwin wdss, wds.names[6], wdss, wds.names[0], 653204697Sedwin wdss, wds.names[1], wdss, wds.names[2], 654204697Sedwin wdss, wds.names[3], wdss, wds.names[4], 655204697Sedwin wdss, wds.names[5]); 656204697Sedwin } 657204697Sedwin printf("\n"); 65831744Shelbig 659205071Sedwin /* And the days of the month. */ 660204697Sedwin for (i = 0; i != 6; i++) { 661204697Sedwin for (j = 0; j < count; j++) 662204908Sedwin printf("%-*s ", 663204908Sedwin MW(mw, year[j].extralen[i]), 664204908Sedwin year[j].lines[i]+1); 665204697Sedwin printf("\n"); 666204697Sedwin } 66731744Shelbig 668204697Sedwin m += mpl; 66931744Shelbig } 67031744Shelbig} 67131744Shelbig 67231744Shelbigvoid 673205071Sedwinmonthranger(int y, int m, int jd_flag, int before, int after) 67431744Shelbig{ 67531744Shelbig struct monthlines year[12]; 67631744Shelbig struct weekdays wds; 677204697Sedwin char s[MAX_WIDTH], t[MAX_WIDTH]; 67831744Shelbig int i, j; 67931744Shelbig int mpl; 68031744Shelbig int mw; 681204697Sedwin int m1, m2; 682204697Sedwin int prevyear = -1; 683204697Sedwin int printyearheader; 68431744Shelbig 685204697Sedwin mpl = jd_flag ? 3 : 4; 686204697Sedwin mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R; 687204697Sedwin 688204697Sedwin while (before != 0) { 689204697Sedwin DECREASEMONTH(m, y); 690204697Sedwin before--; 691204697Sedwin after++; 692204697Sedwin } 693204697Sedwin m1 = y * 12 + m - 1; 694204697Sedwin m2 = m1 + after; 695204697Sedwin 69631744Shelbig mkweekdays(&wds); 69731744Shelbig 698204697Sedwin /* 699204697Sedwin * The year header is printed when there are more than 'mpl' months 700204697Sedwin * and if the first month is a multitude of 'mpl'. 701204697Sedwin * If not, it will print the year behind every month. 702204697Sedwin */ 703204697Sedwin printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 70431744Shelbig 705204697Sedwin m = m1; 706204697Sedwin while (m <= m2) { 707204697Sedwin int count = 0; 708204697Sedwin for (i = 0; i != mpl && m + i <= m2; i++) { 709204697Sedwin mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 710204697Sedwin count++; 711204697Sedwin } 71231744Shelbig 713205071Sedwin /* Empty line between two rows of months. */ 714204697Sedwin if (m != m1) 715204697Sedwin printf("\n"); 716204697Sedwin 717205071Sedwin /* Year at the top. */ 718204697Sedwin if (printyearheader && M2Y(m) != prevyear) { 719204697Sedwin sprintf(s, "%d", M2Y(m)); 720204697Sedwin printf("%s\n", center(t, s, mpl * mw)); 721204697Sedwin prevyear = M2Y(m); 722204697Sedwin } 723204697Sedwin 724205071Sedwin /* Month names. */ 725204697Sedwin wprintf(L" "); 726204697Sedwin for (i = 0; i < count; i++) 727204697Sedwin if (printyearheader) 728204697Sedwin wprintf(L"%-*ls", mw, year[i].name); 72931744Shelbig else 730204697Sedwin wprintf(L"%-ls %-*d", year[i].name, 731204697Sedwin mw - wcslen(year[i].name) - 1, M2Y(m + i)); 732204697Sedwin printf("\n"); 73331744Shelbig 734205071Sedwin /* And the days of the month. */ 735204697Sedwin for (i = 0; i != 7; i++) { 736204697Sedwin /* Week day */ 737204697Sedwin wprintf(L"%.2ls", wds.names[i]); 738204697Sedwin 739204697Sedwin /* Full months */ 740204697Sedwin for (j = 0; j < count; j++) 741204697Sedwin printf("%-*s", 742204908Sedwin MW(mw, year[j].extralen[i]), 743204908Sedwin year[j].lines[i]); 744204697Sedwin printf("\n"); 74531744Shelbig } 746204697Sedwin 747205071Sedwin /* Week numbers. */ 748204697Sedwin if (flag_weeks) { 749204697Sedwin printf(" "); 750204697Sedwin for (i = 0; i < count; i++) 751204697Sedwin printf("%-*s", mw, year[i].weeks); 752204697Sedwin printf("\n"); 753204697Sedwin } 754204697Sedwin 755204697Sedwin m += mpl; 75631744Shelbig } 757204697Sedwin return; 75831744Shelbig} 75931744Shelbig 76031744Shelbigvoid 761204697Sedwinmkmonthr(int y, int m, int jd_flag, struct monthlines *mlines) 76231744Shelbig{ 76331744Shelbig 76431744Shelbig struct tm tm; /* for strftime printing local names of 76531744Shelbig * months */ 76631744Shelbig date dt; /* handy date */ 76731744Shelbig int dw; /* width of numbers */ 76831744Shelbig int first; /* first day of month */ 76931744Shelbig int firstm; /* first day of first week of month */ 770191330Srdivacky int i, j, k, l; /* just indices */ 77131744Shelbig int last; /* the first day of next month */ 77231744Shelbig int jan1 = 0; /* the first day of this year */ 77331744Shelbig char *ds; /* pointer to day strings (daystr or 77431744Shelbig * jdaystr) */ 77531744Shelbig 77631744Shelbig /* Set name of month. */ 77731744Shelbig memset(&tm, 0, sizeof(tm)); 77831744Shelbig tm.tm_mon = m; 779189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 780189804Sdas L"%OB", &tm); 781189804Sdas mlines->name[0] = towupper(mlines->name[0]); 78231744Shelbig 78331744Shelbig /* 78431744Shelbig * Set first and last to the day number of the first day of this 78531744Shelbig * month and the first day of next month respectively. Set jan1 to 78632282Shelbig * the day number of the first day of this year. 78731744Shelbig */ 78832282Shelbig first = firstday(y, m + 1); 78932282Shelbig if (m == 11) 79032282Shelbig last = firstday(y + 1, 1); 79132282Shelbig else 79232282Shelbig last = firstday(y, m + 2); 79331744Shelbig 79432282Shelbig if (jd_flag) 79532525Shelbig jan1 = firstday(y, 1); 79631744Shelbig 79731744Shelbig /* 79831744Shelbig * Set firstm to the day number of monday of the first week of 79931744Shelbig * this month. (This might be in the last month) 80031744Shelbig */ 80131744Shelbig firstm = first - weekday(first); 80231744Shelbig 803205071Sedwin /* Set ds (daystring) and dw (daywidth) according to the jd_flag. */ 80431744Shelbig if (jd_flag) { 80531744Shelbig ds = jdaystr; 80631744Shelbig dw = 4; 80731744Shelbig } else { 80831744Shelbig ds = daystr; 80931744Shelbig dw = 3; 81031744Shelbig } 81131744Shelbig 81231744Shelbig /* 81331744Shelbig * Fill the lines with day of month or day of year (julian day) 81431744Shelbig * line index: i, each line is one weekday. column index: j, each 81531744Shelbig * column is one day number. print column index: k. 81631744Shelbig */ 81731744Shelbig for (i = 0; i != 7; i++) { 818191330Srdivacky l = 0; 819191330Srdivacky for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 82031744Shelbig if (j >= first) { 82131744Shelbig if (jd_flag) 82231744Shelbig dt.d = j - jan1 + 1; 82331744Shelbig else 824204697Sedwin sdater(j, &dt); 825223931Sedwin if (j == highlightdate && !flag_nohighlight 826223931Sedwin && isatty(STDOUT_FILENO)) 827204849Sedwin highlight(mlines->lines[i] + k, 828204849Sedwin ds + dt.d * dw, dw, &l); 829204849Sedwin else 830204849Sedwin memcpy(mlines->lines[i] + k + l, 831204849Sedwin ds + dt.d * dw, dw); 83231744Shelbig } else 833191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 834191330Srdivacky } 835191330Srdivacky mlines->lines[i][k + l] = '\0'; 836204908Sedwin mlines->extralen[i] = l; 83731744Shelbig } 83831744Shelbig 839205071Sedwin /* fill the weeknumbers. */ 84031744Shelbig if (flag_weeks) { 84131744Shelbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 84231744Shelbig if (j <= nswitch) 84331744Shelbig memset(mlines->weeks + k, ' ', dw); 84431744Shelbig else 84531744Shelbig memcpy(mlines->weeks + k, 84631744Shelbig ds + week(j, &i)*dw, dw); 84731744Shelbig mlines->weeks[k] = '\0'; 84831744Shelbig } 84931744Shelbig} 85031744Shelbig 85131744Shelbigvoid 85231744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 85331744Shelbig{ 85431744Shelbig 85531744Shelbig struct tm tm; /* for strftime printing local names of 85631744Shelbig * months */ 85731744Shelbig date dt; /* handy date */ 85831744Shelbig int dw; /* width of numbers */ 85931744Shelbig int first; /* first day of month */ 86031744Shelbig int firsts; /* sunday of first week of month */ 861191330Srdivacky int i, j, k, l; /* just indices */ 86231744Shelbig int jan1 = 0; /* the first day of this year */ 86331744Shelbig int last; /* the first day of next month */ 86431744Shelbig char *ds; /* pointer to day strings (daystr or 86531744Shelbig * jdaystr) */ 86631744Shelbig 86731744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 86831744Shelbig if (jd_flag) { 86931744Shelbig ds = jdaystr; 87031744Shelbig dw = 4; 87131744Shelbig } else { 87231744Shelbig ds = daystr; 87331744Shelbig dw = 3; 87431744Shelbig } 87531744Shelbig 876205071Sedwin /* Set name of month centered. */ 87731744Shelbig memset(&tm, 0, sizeof(tm)); 87831744Shelbig tm.tm_mon = m; 879189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 880189804Sdas L"%OB", &tm); 881189804Sdas mlines->name[0] = towupper(mlines->name[0]); 88231744Shelbig 88331744Shelbig /* 88431744Shelbig * Set first and last to the day number of the first day of this 88531744Shelbig * month and the first day of next month respectively. Set jan1 to 88631744Shelbig * the day number of Jan 1st of this year. 88731744Shelbig */ 88831744Shelbig dt.y = y; 88931744Shelbig dt.m = m + 1; 89031744Shelbig dt.d = 1; 89131744Shelbig first = sndaysb(&dt); 89231744Shelbig if (m == 11) { 89331744Shelbig dt.y = y + 1; 89431744Shelbig dt.m = 1; 89531744Shelbig dt.d = 1; 89631744Shelbig } else { 89731744Shelbig dt.y = y; 89831744Shelbig dt.m = m + 2; 89931744Shelbig dt.d = 1; 90031744Shelbig } 90131744Shelbig last = sndaysb(&dt); 90231744Shelbig 90331744Shelbig if (jd_flag) { 90431744Shelbig dt.y = y; 90531744Shelbig dt.m = 1; 90631744Shelbig dt.d = 1; 90731744Shelbig jan1 = sndaysb(&dt); 90831744Shelbig } 90931744Shelbig 91031744Shelbig /* 91131744Shelbig * Set firsts to the day number of sunday of the first week of 91231744Shelbig * this month. (This might be in the last month) 91331744Shelbig */ 91431744Shelbig firsts = first - (weekday(first)+1) % 7; 91531744Shelbig 91631744Shelbig /* 91731744Shelbig * Fill the lines with day of month or day of year (Julian day) 91831744Shelbig * line index: i, each line is one week. column index: j, each 91931744Shelbig * column is one day number. print column index: k. 92031744Shelbig */ 92131744Shelbig for (i = 0; i != 6; i++) { 922191330Srdivacky l = 0; 92331744Shelbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 924191330Srdivacky j++, k += dw) { 925204849Sedwin if (j >= first) { 926191330Srdivacky if (jd_flag) 927191330Srdivacky dt.d = j - jan1 + 1; 928191330Srdivacky else 929191330Srdivacky sdateb(j, &dt); 930205071Sedwin if (j == highlightdate && !flag_nohighlight) 931204849Sedwin highlight(mlines->lines[i] + k, 932204849Sedwin ds + dt.d * dw, dw, &l); 93331744Shelbig else 934204849Sedwin memcpy(mlines->lines[i] + k + l, 935204849Sedwin ds + dt.d * dw, dw); 93631744Shelbig } else 937191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 938191330Srdivacky } 93931744Shelbig if (k == 0) 94031744Shelbig mlines->lines[i][1] = '\0'; 94131744Shelbig else 942191330Srdivacky mlines->lines[i][k + l] = '\0'; 943204908Sedwin mlines->extralen[i] = l; 94431744Shelbig } 94531744Shelbig} 94631744Shelbig 947205071Sedwin/* Put the local names of weekdays into the wds. */ 94832139Shelbigvoid 94932139Shelbigmkweekdays(struct weekdays *wds) 95031744Shelbig{ 951194366Sume int i, len, width = 0; 95231744Shelbig struct tm tm; 953189804Sdas wchar_t buf[20]; 95431744Shelbig 95531744Shelbig memset(&tm, 0, sizeof(tm)); 95631744Shelbig 95731744Shelbig for (i = 0; i != 7; i++) { 95831744Shelbig tm.tm_wday = (i+1) % 7; 959189804Sdas wcsftime(buf, sizeof(buf), L"%a", &tm); 960194447Sume for (len = 2; len > 0; --len) { 961194366Sume if ((width = wcswidth(buf, len)) <= 2) 962194366Sume break; 963194366Sume } 964194366Sume wmemset(wds->names[i], L'\0', 4); 965194366Sume if (width == 1) 966194366Sume wds->names[i][0] = L' '; 967194366Sume wcsncat(wds->names[i], buf, len); 968194447Sume wcsncat(wds->names[i], L" ", 1); 96931744Shelbig } 97031744Shelbig} 97131744Shelbig 97231744Shelbig/* 973205071Sedwin * Compute the day number of the first existing date after the first day in 974205071Sedwin * month. (the first day in month and even the month might not exist!) 97532282Shelbig */ 97632282Shelbigint 97732282Shelbigfirstday(int y, int m) 97832282Shelbig{ 97932282Shelbig date dt; 98032282Shelbig int nd; 98132282Shelbig 98232282Shelbig dt.y = y; 98332282Shelbig dt.m = m; 98432282Shelbig dt.d = 1; 985204697Sedwin nd = sndaysr(&dt); 98645064Shelbig for (;;) { 987204697Sedwin sdater(nd, &dt); 98845064Shelbig if ((dt.m >= m && dt.y == y) || dt.y > y) 98945064Shelbig return (nd); 99045064Shelbig else 99145064Shelbig nd++; 99245064Shelbig } 99345064Shelbig /* NEVER REACHED */ 99432282Shelbig} 99532282Shelbig 99632282Shelbig/* 99731744Shelbig * Compute the number of days from date, obey the local switch from 99831744Shelbig * Julian to Gregorian if specified by the user. 99931744Shelbig */ 100031744Shelbigint 1001204697Sedwinsndaysr(struct date *d) 100231744Shelbig{ 100331744Shelbig 100431744Shelbig if (nswitch != 0) 100531744Shelbig if (nswitch < ndaysj(d)) 100631744Shelbig return (ndaysg(d)); 100731744Shelbig else 100831744Shelbig return (ndaysj(d)); 100931744Shelbig else 101031744Shelbig return ndaysg(d); 101131744Shelbig} 101231744Shelbig 101331744Shelbig/* 101431744Shelbig * Compute the number of days from date, obey the switch from 101531744Shelbig * Julian to Gregorian as used by UK and her colonies. 101631744Shelbig */ 101731744Shelbigint 101831744Shelbigsndaysb(struct date *d) 101931744Shelbig{ 102031744Shelbig 102131744Shelbig if (nswitchb < ndaysj(d)) 102231744Shelbig return (ndaysg(d)); 102331744Shelbig else 102431744Shelbig return (ndaysj(d)); 102531744Shelbig} 102631744Shelbig 1027205071Sedwin/* Inverse of sndays. */ 102831744Shelbigstruct date * 1029204697Sedwinsdater(int nd, struct date *d) 103031744Shelbig{ 103131744Shelbig 103231744Shelbig if (nswitch < nd) 103331744Shelbig return (gdate(nd, d)); 103431744Shelbig else 103531744Shelbig return (jdate(nd, d)); 103631744Shelbig} 103731744Shelbig 1038205071Sedwin/* Inverse of sndaysb. */ 103931744Shelbigstruct date * 104031744Shelbigsdateb(int nd, struct date *d) 104131744Shelbig{ 104231744Shelbig 104331744Shelbig if (nswitchb < nd) 104431744Shelbig return (gdate(nd, d)); 104531744Shelbig else 104631744Shelbig return (jdate(nd, d)); 104731744Shelbig} 104831744Shelbig 1049205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */ 105031744Shelbigchar * 105131744Shelbigcenter(char *s, char *t, int w) 105231744Shelbig{ 1053204697Sedwin char blanks[MAX_WIDTH]; 105431744Shelbig 105531744Shelbig memset(blanks, ' ', sizeof(blanks)); 105631744Shelbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 105731744Shelbig return (s); 105831744Shelbig} 1059104369Sroam 1060205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */ 1061189804Sdaswchar_t * 1062189804Sdaswcenter(wchar_t *s, wchar_t *t, int w) 1063189804Sdas{ 1064204697Sedwin char blanks[MAX_WIDTH]; 1065189804Sdas 1066189804Sdas memset(blanks, ' ', sizeof(blanks)); 1067189804Sdas swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 1068189804Sdas return (s); 1069189804Sdas} 1070189804Sdas 1071104369Sroamint 1072186401Swollmanparsemonth(const char *s, int *m, int *y) 1073104369Sroam{ 1074186401Swollman int nm, ny; 1075104369Sroam char *cp; 1076104369Sroam struct tm tm; 1077104369Sroam 1078186401Swollman nm = (int)strtol(s, &cp, 10); 1079186401Swollman if (cp != s) { 1080186401Swollman ny = *y; 1081186401Swollman if (*cp == '\0') { 1082186401Swollman ; /* no special action */ 1083186401Swollman } else if (*cp == 'f' || *cp == 'F') { 1084186401Swollman if (nm <= *m) 1085186401Swollman ny++; 1086186401Swollman } else if (*cp == 'p' || *cp == 'P') { 1087186401Swollman if (nm >= *m) 1088186401Swollman ny--; 1089186401Swollman } else 1090186401Swollman return (1); 1091186401Swollman if (nm < 1 || nm > 12) 1092186401Swollman return 1; 1093186401Swollman *m = nm; 1094186401Swollman *y = ny; 1095186401Swollman return (0); 1096186401Swollman } 1097186401Swollman if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 1098186401Swollman *m = tm.tm_mon + 1; 1099186401Swollman return (0); 1100186401Swollman } 1101186401Swollman return (1); 1102104369Sroam} 1103204849Sedwin 1104204849Sedwinvoid 1105204849Sedwinhighlight(char *dst, char *src, int len, int *extralen) 1106204849Sedwin{ 1107204849Sedwin static int first = 1; 1108204849Sedwin static const char *term_so, *term_se; 1109204849Sedwin 1110204849Sedwin if (first) { 1111204849Sedwin char tbuf[1024], cbuf[512], *b; 1112204849Sedwin 1113204849Sedwin term_se = term_so = NULL; 1114204849Sedwin 1115205071Sedwin /* On how to highlight on this type of terminal (if any). */ 1116204849Sedwin if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) { 1117204849Sedwin b = cbuf; 1118204849Sedwin term_so = tgetstr("so", &b); 1119204849Sedwin term_se = tgetstr("se", &b); 1120204849Sedwin } 1121204849Sedwin 1122204849Sedwin first = 0; 1123204849Sedwin } 1124204849Sedwin 1125204849Sedwin /* 1126204849Sedwin * This check is not necessary, should have been handled before calling 1127204849Sedwin * this function. 1128204849Sedwin */ 1129204849Sedwin if (flag_nohighlight) { 1130204849Sedwin memcpy(dst, src, len); 1131204849Sedwin return; 1132204849Sedwin } 1133204849Sedwin 1134205071Sedwin /* 1135205071Sedwin * If it is a real terminal, use the data from the termcap database. 1136205071Sedwin */ 1137204849Sedwin if (term_so != NULL && term_se != NULL) { 1138205071Sedwin /* separator. */ 1139204849Sedwin dst[0] = ' '; 1140204849Sedwin dst++; 1141205071Sedwin /* highlight on. */ 1142204849Sedwin memcpy(dst, term_so, strlen(term_so)); 1143204849Sedwin dst += strlen(term_so); 1144205071Sedwin /* the actual text. (minus leading space) */ 1145204849Sedwin len--; 1146204849Sedwin src++; 1147204849Sedwin memcpy(dst, src, len); 1148204849Sedwin dst += len; 1149205071Sedwin /* highlight off. */ 1150204849Sedwin memcpy(dst, term_se, strlen(term_se)); 1151204849Sedwin *extralen = strlen(term_so) + strlen(term_se); 1152204849Sedwin return; 1153204849Sedwin } 1154204849Sedwin 1155204849Sedwin /* 1156205071Sedwin * Otherwise, print a _, backspace and the letter. 1157204849Sedwin */ 1158204849Sedwin *extralen = 0; 1159205071Sedwin /* skip leading space. */ 1160204849Sedwin src++; 1161204849Sedwin len--; 1162205071Sedwin /* separator. */ 1163204849Sedwin dst[0] = ' '; 1164204849Sedwin dst++; 1165204849Sedwin while (len > 0) { 1166205071Sedwin /* _ and backspace. */ 1167204849Sedwin memcpy(dst, "_\010", 2); 1168204849Sedwin dst += 2; 1169204849Sedwin *extralen += 2; 1170205071Sedwin /* the character. */ 1171204849Sedwin *dst++ = *src++; 1172204849Sedwin len--; 1173204849Sedwin } 1174205071Sedwin return; 1175204849Sedwin} 1176