ncal.c revision 205071
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: head/usr.bin/ncal/ncal.c 205071 2010-03-12 06:57:53Z edwin $"; 3032310Scharnier#endif /* not lint */ 3132310Scharnier 3231744Shelbig#include <calendar.h> 33200462Sdelphij#include <ctype.h> 3431744Shelbig#include <err.h> 3574573Sache#include <langinfo.h> 3631744Shelbig#include <locale.h> 3731744Shelbig#include <stdio.h> 3831744Shelbig#include <stdlib.h> 3931744Shelbig#include <string.h> 4031744Shelbig#include <sysexits.h> 4131744Shelbig#include <time.h> 4231744Shelbig#include <unistd.h> 43189804Sdas#include <wchar.h> 44189804Sdas#include <wctype.h> 45191330Srdivacky#include <term.h> 46191330Srdivacky#undef lines /* term.h defines this */ 4731744Shelbig 48204697Sedwin/* Width of one month with backward compatibility and in regular mode*/ 4931744Shelbig#define MONTH_WIDTH_B_J 27 5031744Shelbig#define MONTH_WIDTH_B 20 5131744Shelbig 52204697Sedwin#define MONTH_WIDTH_R_J 24 53204697Sedwin#define MONTH_WIDTH_R 18 5431744Shelbig 55191330Srdivacky#define MAX_WIDTH 64 5631744Shelbig 5731744Shelbigtypedef struct date date; 5831744Shelbig 5931744Shelbigstruct monthlines { 60189804Sdas wchar_t name[MAX_WIDTH + 1]; 6131744Shelbig char lines[7][MAX_WIDTH + 1]; 6231744Shelbig char weeks[MAX_WIDTH + 1]; 63204908Sedwin unsigned int extralen[7]; 6431744Shelbig}; 6531744Shelbig 6631744Shelbigstruct weekdays { 67189804Sdas wchar_t names[7][4]; 6831744Shelbig}; 6931744Shelbig 7031744Shelbig/* The switches from Julian to Gregorian in some countries */ 7131744Shelbigstatic struct djswitch { 7295641Smarkm const char *cc; /* Country code according to ISO 3166 */ 7395641Smarkm const char *nm; /* Name of country */ 7431744Shelbig date dt; /* Last day of Julian calendar */ 7531744Shelbig} switches[] = { 7653963Sache {"AL", "Albania", {1912, 11, 30}}, 7753963Sache {"AT", "Austria", {1583, 10, 5}}, 7853963Sache {"AU", "Australia", {1752, 9, 2}}, 7953963Sache {"BE", "Belgium", {1582, 12, 14}}, 8053963Sache {"BG", "Bulgaria", {1916, 3, 18}}, 8153963Sache {"CA", "Canada", {1752, 9, 2}}, 8253963Sache {"CH", "Switzerland", {1655, 2, 28}}, 8353963Sache {"CN", "China", {1911, 12, 18}}, 8453963Sache {"CZ", "Czech Republic",{1584, 1, 6}}, 8553963Sache {"DE", "Germany", {1700, 2, 18}}, 8653963Sache {"DK", "Denmark", {1700, 2, 18}}, 8753963Sache {"ES", "Spain", {1582, 10, 4}}, 8853963Sache {"FI", "Finland", {1753, 2, 17}}, 8953963Sache {"FR", "France", {1582, 12, 9}}, 9053963Sache {"GB", "United Kingdom",{1752, 9, 2}}, 9153963Sache {"GR", "Greece", {1924, 3, 9}}, 9253963Sache {"HU", "Hungary", {1587, 10, 21}}, 9353963Sache {"IS", "Iceland", {1700, 11, 16}}, 9453963Sache {"IT", "Italy", {1582, 10, 4}}, 9553963Sache {"JP", "Japan", {1918, 12, 18}}, 9653963Sache {"LI", "Lithuania", {1918, 2, 1}}, 9753963Sache {"LN", "Latin", {9999, 05, 31}}, 9853963Sache {"LU", "Luxembourg", {1582, 12, 14}}, 9953963Sache {"LV", "Latvia", {1918, 2, 1}}, 10053963Sache {"NL", "Netherlands", {1582, 12, 14}}, 10153963Sache {"NO", "Norway", {1700, 2, 18}}, 10253963Sache {"PL", "Poland", {1582, 10, 4}}, 10353963Sache {"PT", "Portugal", {1582, 10, 4}}, 10453963Sache {"RO", "Romania", {1919, 3, 31}}, 10553963Sache {"RU", "Russia", {1918, 1, 31}}, 10653963Sache {"SI", "Slovenia", {1919, 3, 4}}, 10753963Sache {"SW", "Sweden", {1753, 2, 17}}, 10853963Sache {"TR", "Turkey", {1926, 12, 18}}, 10953963Sache {"US", "United States", {1752, 9, 2}}, 11053963Sache {"YU", "Yugoslavia", {1919, 3, 4}} 11131744Shelbig}; 11231744Shelbig 11332215Shelbigstruct djswitch *dftswitch = 11432215Shelbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 11532215Shelbig /* default switch (should be "US") */ 11632215Shelbig 11731744Shelbig/* Table used to print day of month and week numbers */ 11831744Shelbigchar daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 11931744Shelbig " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 12031744Shelbig " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 12131744Shelbig " 48 49 50 51 52 53"; 12231744Shelbig 12331744Shelbig/* Table used to print day of year and week numbers */ 12431744Shelbigchar jdaystr[] = " 1 2 3 4 5 6 7 8 9" 12531744Shelbig " 10 11 12 13 14 15 16 17 18 19" 12631744Shelbig " 20 21 22 23 24 25 26 27 28 29" 12731744Shelbig " 30 31 32 33 34 35 36 37 38 39" 12831744Shelbig " 40 41 42 43 44 45 46 47 48 49" 12931744Shelbig " 50 51 52 53 54 55 56 57 58 59" 13031744Shelbig " 60 61 62 63 64 65 66 67 68 69" 13131744Shelbig " 70 71 72 73 74 75 76 77 78 79" 13231744Shelbig " 80 81 82 83 84 85 86 87 88 89" 13331744Shelbig " 90 91 92 93 94 95 96 97 98 99" 13431744Shelbig " 100 101 102 103 104 105 106 107 108 109" 13531744Shelbig " 110 111 112 113 114 115 116 117 118 119" 13631744Shelbig " 120 121 122 123 124 125 126 127 128 129" 13731744Shelbig " 130 131 132 133 134 135 136 137 138 139" 13831744Shelbig " 140 141 142 143 144 145 146 147 148 149" 13931744Shelbig " 150 151 152 153 154 155 156 157 158 159" 14031744Shelbig " 160 161 162 163 164 165 166 167 168 169" 14131744Shelbig " 170 171 172 173 174 175 176 177 178 179" 14231744Shelbig " 180 181 182 183 184 185 186 187 188 189" 14331744Shelbig " 190 191 192 193 194 195 196 197 198 199" 14431744Shelbig " 200 201 202 203 204 205 206 207 208 209" 14531744Shelbig " 210 211 212 213 214 215 216 217 218 219" 14631744Shelbig " 220 221 222 223 224 225 226 227 228 229" 14731744Shelbig " 230 231 232 233 234 235 236 237 238 239" 14831744Shelbig " 240 241 242 243 244 245 246 247 248 249" 14931744Shelbig " 250 251 252 253 254 255 256 257 258 259" 15031744Shelbig " 260 261 262 263 264 265 266 267 268 269" 15131744Shelbig " 270 271 272 273 274 275 276 277 278 279" 15231744Shelbig " 280 281 282 283 284 285 286 287 288 289" 15331744Shelbig " 290 291 292 293 294 295 296 297 298 299" 15431744Shelbig " 300 301 302 303 304 305 306 307 308 309" 15531744Shelbig " 310 311 312 313 314 315 316 317 318 319" 15631744Shelbig " 320 321 322 323 324 325 326 327 328 329" 15731744Shelbig " 330 331 332 333 334 335 336 337 338 339" 15831744Shelbig " 340 341 342 343 344 345 346 347 348 349" 15931744Shelbig " 350 351 352 353 354 355 356 357 358 359" 16031744Shelbig " 360 361 362 363 364 365 366"; 16131744Shelbig 162204849Sedwinint flag_nohighlight; /* user doesn't want a highlighted today */ 16332139Shelbigint flag_weeks; /* user wants number of week */ 16431744Shelbigint nswitch; /* user defined switch date */ 16531744Shelbigint nswitchb; /* switch date for backward compatibility */ 166205071Sedwinint highlightdate; 16731744Shelbig 168204697Sedwinchar *center(char *s, char *t, int w); 169189804Sdaswchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 170205071Sedwinint firstday(int y, int m); 171205071Sedwinvoid highlight(char *dst, char *src, int len, int *extraletters); 172204697Sedwinvoid mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl); 173204697Sedwinvoid mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 174204697Sedwinvoid mkweekdays(struct weekdays * wds); 175205071Sedwinvoid monthranger(int year, int m, int jd_flag, int before, int after); 176205071Sedwinvoid monthrangeb(int year, int m, int jd_flag, int before, int after); 177204697Sedwinint parsemonth(const char *s, int *m, int *y); 178204697Sedwinvoid printcc(void); 179204697Sedwinvoid printeaster(int year, int julian, int orthodox); 180204697Sedwindate *sdater(int ndays, struct date * d); 181204697Sedwindate *sdateb(int ndays, struct date * d); 182204697Sedwinint sndaysr(struct date * d); 183204697Sedwinint sndaysb(struct date * d); 184204697Sedwinstatic void usage(void); 18531744Shelbig 18631744Shelbigint 18731744Shelbigmain(int argc, char *argv[]) 18831744Shelbig{ 18931744Shelbig struct djswitch *p, *q; /* to search user defined switch date */ 19031744Shelbig date never = {10000, 1, 1}; /* outside valid range of dates */ 19131744Shelbig date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 192204697Sedwin date dt; 19331744Shelbig int ch; /* holds the option character */ 19431744Shelbig int m = 0; /* month */ 19531744Shelbig int y = 0; /* year */ 19631744Shelbig int flag_backward = 0; /* user called cal--backward compat. */ 197205071Sedwin int flag_wholeyear = 0; /* user wants the whole year */ 19831744Shelbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 199205071Sedwin int flag_julian_day = 0; /* user wants the Julian day numbers */ 200205071Sedwin int flag_orthodox = 0; /* user wants Orthodox easter */ 201205071Sedwin int flag_easter = 0; /* user wants easter date */ 202205071Sedwin int flag_3months = 0; /* user wants 3 month display (-3) */ 203205071Sedwin int flag_after = 0; /* user wants to see months after */ 204205071Sedwin int flag_before = 0; /* user wants to see months before */ 205205071Sedwin int flag_specifiedmonth = 0;/* user wants to see this month (-m) */ 206205071Sedwin int flag_givenmonth = 0; /* user has specified month [n] */ 207205071Sedwin int flag_givenyear = 0; /* user has specified year [n] */ 20831744Shelbig char *cp; /* character pointer */ 209205071Sedwin char *flag_today = NULL; /* debug: use date as being today */ 210138036Swollman char *flag_month = NULL; /* requested month as string */ 211205071Sedwin char *flag_highlightdate = NULL; /* debug: date to highlight */ 212204697Sedwin int before, after; 21395641Smarkm const char *locale; /* locale to get country code */ 21431744Shelbig 215204849Sedwin flag_nohighlight = 0; 216204849Sedwin flag_weeks = 0; 217191330Srdivacky 21832215Shelbig /* 21932215Shelbig * Use locale to determine the country code, 22032215Shelbig * and use the country code to determine the default 22132215Shelbig * switchdate and date format from the switches table. 22232215Shelbig */ 22374612Sache if (setlocale(LC_ALL, "") == NULL) 22432215Shelbig warn("setlocale"); 22574612Sache locale = setlocale(LC_TIME, NULL); 22678016Sache if (locale == NULL || 22778016Sache strcmp(locale, "C") == 0 || 22878027Sache strcmp(locale, "POSIX") == 0 || 22978027Sache strcmp(locale, "ASCII") == 0 || 23078027Sache strcmp(locale, "US-ASCII") == 0) 23132215Shelbig locale = "_US"; 23232215Shelbig q = switches + sizeof(switches) / sizeof(struct djswitch); 23332215Shelbig for (p = switches; p != q; p++) 23432215Shelbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 23532215Shelbig break; 23632215Shelbig if (p == q) { 23732215Shelbig nswitch = ndaysj(&dftswitch->dt); 23832215Shelbig } else { 23932215Shelbig nswitch = ndaysj(&p->dt); 24032215Shelbig dftswitch = p; 24132215Shelbig } 24231744Shelbig 24332215Shelbig 24431744Shelbig /* 24531744Shelbig * Get the filename portion of argv[0] and set flag_backward if 24631744Shelbig * this program is called "cal". 24731744Shelbig */ 248138036Swollman cp = strrchr(argv[0], '/'); 249138036Swollman cp = (cp == NULL) ? argv[0] : cp + 1; 250138036Swollman if (strcmp("cal", cp) == 0) 25131744Shelbig flag_backward = 1; 25231744Shelbig 25331744Shelbig /* Set the switch date to United Kingdom if backwards compatible */ 25431744Shelbig if (flag_backward) 25531744Shelbig nswitchb = ndaysj(&ukswitch); 25631744Shelbig 257204697Sedwin before = after = -1; 258204697Sedwin 259205071Sedwin while ((ch = getopt(argc, argv, "A:B:3Jbd:eH:hjm:ops:wy")) != -1) 26031744Shelbig switch (ch) { 261204697Sedwin case '3': 262205071Sedwin flag_3months = 1; 263204697Sedwin break; 264204697Sedwin case 'A': 265205071Sedwin if (flag_after > 0) 266205071Sedwin errx(EX_USAGE, "Double -A specified"); 267205071Sedwin flag_after = strtol(optarg, NULL, 10); 268205071Sedwin if (flag_after <= 0) 269205071Sedwin errx(EX_USAGE, 270205071Sedwin "Argument to -A must be positive"); 271204697Sedwin break; 272204697Sedwin case 'B': 273205071Sedwin if (flag_before > 0) 274205071Sedwin errx(EX_USAGE, "Double -A specified"); 275205071Sedwin flag_before = strtol(optarg, NULL, 10); 276205071Sedwin if (flag_before <= 0) 277205071Sedwin errx(EX_USAGE, 278205071Sedwin "Argument to -B must be positive"); 279204697Sedwin break; 28031744Shelbig case 'J': 28131744Shelbig if (flag_backward) 28231744Shelbig usage(); 28331744Shelbig nswitch = ndaysj(&never); 28431744Shelbig flag_julian_cal = 1; 28531744Shelbig break; 286204697Sedwin case 'b': 287204697Sedwin flag_backward = 1; 288204697Sedwin break; 289204697Sedwin case 'd': 290205071Sedwin flag_today = optarg; 291205071Sedwin break; 292205071Sedwin case 'H': 293204697Sedwin flag_highlightdate = optarg; 294204697Sedwin break; 295191364Srdivacky case 'h': 296204849Sedwin flag_nohighlight = 1; 297191364Srdivacky break; 29831744Shelbig case 'e': 29931744Shelbig if (flag_backward) 30031744Shelbig usage(); 30131744Shelbig flag_easter = 1; 30231744Shelbig break; 30331744Shelbig case 'j': 30431744Shelbig flag_julian_day = 1; 30531744Shelbig break; 306138036Swollman case 'm': 307205071Sedwin if (flag_specifiedmonth) 308205071Sedwin errx(EX_USAGE, "Double -m specified"); 309138036Swollman flag_month = optarg; 310205071Sedwin flag_specifiedmonth = 1; 311138036Swollman break; 31231744Shelbig case 'o': 31331744Shelbig if (flag_backward) 31431744Shelbig usage(); 31531744Shelbig flag_orthodox = 1; 31631744Shelbig flag_easter = 1; 31731744Shelbig break; 31831744Shelbig case 'p': 31931744Shelbig if (flag_backward) 32031744Shelbig usage(); 32131744Shelbig printcc(); 32231744Shelbig return (0); 32331744Shelbig break; 32431744Shelbig case 's': 32531744Shelbig if (flag_backward) 32631744Shelbig usage(); 32731744Shelbig q = switches + 32831744Shelbig sizeof(switches) / sizeof(struct djswitch); 32931744Shelbig for (p = switches; 33031744Shelbig p != q && strcmp(p->cc, optarg) != 0; p++) 33131744Shelbig ; 33231744Shelbig if (p == q) 33331744Shelbig errx(EX_USAGE, 33431744Shelbig "%s: invalid country code", optarg); 33531744Shelbig nswitch = ndaysj(&(p->dt)); 33631744Shelbig break; 33731744Shelbig case 'w': 33831744Shelbig if (flag_backward) 33931744Shelbig usage(); 34031744Shelbig flag_weeks = 1; 34131744Shelbig break; 34231744Shelbig case 'y': 343205071Sedwin flag_wholeyear = 1; 34431744Shelbig break; 34531744Shelbig default: 34631744Shelbig usage(); 34731744Shelbig } 34831744Shelbig 34931744Shelbig argc -= optind; 35031744Shelbig argv += optind; 35131744Shelbig 35231744Shelbig switch (argc) { 35331744Shelbig case 2: 35431744Shelbig if (flag_easter) 35531744Shelbig usage(); 356138036Swollman flag_month = *argv++; 357205071Sedwin flag_givenmonth = 1; 358204908Sedwin m = strtol(flag_month, NULL, 10); 35931744Shelbig /* FALLTHROUGH */ 36031744Shelbig case 1: 361205071Sedwin y = atoi(*argv); 36231744Shelbig if (y < 1 || y > 9999) 363205071Sedwin errx(EX_USAGE, "year `%s' not in range 1..9999", *argv); 364205071Sedwin argv++; 365205071Sedwin flag_givenyear = 1; 36631744Shelbig break; 36731744Shelbig case 0: 368205071Sedwin if (flag_today != NULL) { 369205071Sedwin y = strtol(flag_today, NULL, 10); 370205071Sedwin m = strtol(flag_today + 5, NULL, 10); 371205071Sedwin } else { 372138036Swollman time_t t; 373138036Swollman struct tm *tm; 374138036Swollman 375138036Swollman t = time(NULL); 376138036Swollman tm = localtime(&t); 377138036Swollman y = tm->tm_year + 1900; 378138036Swollman m = tm->tm_mon + 1; 379138036Swollman } 38031744Shelbig break; 38131744Shelbig default: 38231744Shelbig usage(); 38331744Shelbig } 38431744Shelbig 385138036Swollman if (flag_month != NULL) { 386186401Swollman if (parsemonth(flag_month, &m, &y)) { 387138036Swollman errx(EX_USAGE, 388138036Swollman "%s is neither a month number (1..12) nor a name", 389138036Swollman flag_month); 390186401Swollman } 391138036Swollman } 392138036Swollman 393205071Sedwin /* 394205071Sedwin * What is not supported: 395205071Sedwin * -3 with -A or -B 396205071Sedwin * -3 displays 3 months, -A and -B change that behaviour. 397205071Sedwin * -3 with -y 398205071Sedwin * -3 displays 3 months, -y says display a whole year. 399205071Sedwin * -3 with a given year but no given month or without -m 400205071Sedwin * -3 displays 3 months, no month specified doesn't make clear 401205071Sedwin * which three months. 402205071Sedwin * -m with a given month 403205071Sedwin * conflicting arguments, both specify the same field. 404205071Sedwin * -y with -m 405205071Sedwin * -y displays the whole year, -m displays a single month. 406205071Sedwin * -y with a given month 407205071Sedwin * -y displays the whole year, the given month displays a single 408205071Sedwin * month. 409205071Sedwin * -y with -A or -B 410205071Sedwin * -y displays the whole year, -A and -B display extra months. 411205071Sedwin */ 412205071Sedwin 413205071Sedwin /* -3 together with -A or -B. */ 414205071Sedwin if (flag_3months && (flag_after || flag_before)) 415205071Sedwin errx(EX_USAGE, "-3 together with -A and -B is not supported."); 416205071Sedwin /* -3 together with -y. */ 417205071Sedwin if (flag_3months && flag_wholeyear) 418205071Sedwin errx(EX_USAGE, "-3 together with -y is not supported."); 419205071Sedwin /* -3 together with givenyear but no givenmonth. */ 420205071Sedwin if (flag_3months && flag_givenyear && 421205071Sedwin !(flag_givenmonth || flag_specifiedmonth)) 422205071Sedwin errx(EX_USAGE, 423205071Sedwin "-3 together with a given year but no given month is " 424205071Sedwin "not supported."); 425205071Sedwin /* -m together with xx xxxx. */ 426205071Sedwin if (flag_specifiedmonth && flag_givenmonth) 427205071Sedwin errx(EX_USAGE, 428205071Sedwin "-m together with a given month is not supported."); 429205071Sedwin /* -y together with -m. */ 430205071Sedwin if (flag_wholeyear && flag_specifiedmonth) 431205071Sedwin errx(EX_USAGE, "-y together with -m is not supported."); 432205071Sedwin /* -y together with xx xxxx. */ 433205071Sedwin if (flag_wholeyear && flag_givenmonth) 434205071Sedwin errx(EX_USAGE, "-y together a given month is not supported."); 435205071Sedwin /* -y together with -A or -B. */ 436205071Sedwin if (flag_wholeyear && (flag_before > 0 || flag_after > 0)) 437205071Sedwin errx(EX_USAGE, "-y together a -A or -B is not supported."); 438205071Sedwin /* The rest should be fine. */ 439205071Sedwin 440205071Sedwin /* Select the period to display, in order of increasing priority .*/ 441205071Sedwin if (flag_wholeyear || 442205071Sedwin (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) { 443205071Sedwin m = 1; 444205071Sedwin before = 0; 445205071Sedwin after = 11; 446205071Sedwin } 447205071Sedwin if (flag_givenyear && flag_givenmonth) { 448205071Sedwin before = 0; 449205071Sedwin after = 0; 450205071Sedwin } 451205071Sedwin if (flag_specifiedmonth) { 452205071Sedwin before = 0; 453205071Sedwin after = 0; 454205071Sedwin } 455205071Sedwin if (flag_before) { 456205071Sedwin before = flag_before; 457205071Sedwin } 458205071Sedwin if (flag_after) { 459205071Sedwin after = flag_after; 460205071Sedwin } 461205071Sedwin if (flag_3months) { 462205071Sedwin before = 1; 463205071Sedwin after = 1; 464205071Sedwin } 465205071Sedwin if (after == -1) 466205071Sedwin after = 0; 467205071Sedwin if (before == -1) 468205071Sedwin before = 0; 469205071Sedwin 470205071Sedwin /* Highlight a specified day or today .*/ 471204697Sedwin if (flag_highlightdate != NULL) { 472204697Sedwin dt.y = strtol(flag_highlightdate, NULL, 10); 473204697Sedwin dt.m = strtol(flag_highlightdate + 5, NULL, 10); 474204697Sedwin dt.d = strtol(flag_highlightdate + 8, NULL, 10); 475204697Sedwin } else { 476204697Sedwin time_t t; 477204697Sedwin struct tm *tm1; 478204697Sedwin 479204697Sedwin t = time(NULL); 480204697Sedwin tm1 = localtime(&t); 481204697Sedwin dt.y = tm1->tm_year + 1900; 482204697Sedwin dt.m = tm1->tm_mon + 1; 483204697Sedwin dt.d = tm1->tm_mday; 484204697Sedwin } 485205071Sedwin highlightdate = sndaysb(&dt); 486204697Sedwin 487205071Sedwin /* And now we finally start to calculate and output calendars. */ 48831744Shelbig if (flag_easter) 48931744Shelbig printeaster(y, flag_julian_cal, flag_orthodox); 490204697Sedwin else 49131744Shelbig if (flag_backward) 492205071Sedwin monthrangeb(y, m, flag_julian_day, before, after); 49331744Shelbig else 494205071Sedwin monthranger(y, m, flag_julian_day, before, after); 49531744Shelbig return (0); 49631744Shelbig} 49731744Shelbig 49832310Scharnierstatic void 49931744Shelbigusage(void) 50031744Shelbig{ 50131744Shelbig 502138036Swollman fputs( 503191737Srdivacky "usage: cal [-hjy] [[month] year]\n" 504191737Srdivacky " cal [-hj] [-m month] [year]\n" 505191737Srdivacky " ncal [-hJjpwy] [-s country_code] [[month] year]\n" 506204697Sedwin " ncal [-hJeo] [year]\n" 507205071Sedwin "for debug the highlighting: [-b] [-H yyyy-mm-dd] [-d yyyy-mm]\n", 508204697Sedwin stderr); 50931744Shelbig exit(EX_USAGE); 51031744Shelbig} 51131744Shelbig 512205071Sedwin/* Print the assumed switches for all countries. */ 51331744Shelbigvoid 51431744Shelbigprintcc(void) 51531744Shelbig{ 51631744Shelbig struct djswitch *p; 51731744Shelbig int n; /* number of lines to print */ 51831744Shelbig int m; /* offset from left to right table entry on the same line */ 51931744Shelbig 52032215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d" 52132215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 52232215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 52331744Shelbig 52431744Shelbig n = sizeof(switches) / sizeof(struct djswitch); 52531744Shelbig m = (n + 1) / 2; 52631744Shelbig n /= 2; 52732215Shelbig for (p = switches; p != switches + n; p++) 52832215Shelbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 52931744Shelbig if (m != n) 53032215Shelbig printf(FSTR"\n", FSTRARG(p)); 53131744Shelbig} 53231744Shelbig 533205071Sedwin/* Print the date of easter sunday. */ 53431744Shelbigvoid 53531744Shelbigprinteaster(int y, int julian, int orthodox) 53631744Shelbig{ 53732215Shelbig date dt; 53832215Shelbig struct tm tm; 539204697Sedwin char buf[MAX_WIDTH]; 54074573Sache static int d_first = -1; 54131744Shelbig 54274573Sache if (d_first < 0) 54374573Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 54431744Shelbig /* force orthodox easter for years before 1583 */ 54531744Shelbig if (y < 1583) 54631744Shelbig orthodox = 1; 54731744Shelbig 54831744Shelbig if (orthodox) 54931744Shelbig if (julian) 55031744Shelbig easteroj(y, &dt); 55131744Shelbig else 55231744Shelbig easterog(y, &dt); 55331744Shelbig else 55431744Shelbig easterg(y, &dt); 55532215Shelbig 55632215Shelbig memset(&tm, 0, sizeof(tm)); 55732215Shelbig tm.tm_year = dt.y - 1900; 55832215Shelbig tm.tm_mon = dt.m - 1; 55932215Shelbig tm.tm_mday = dt.d; 56074573Sache strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 56132215Shelbig printf("%s\n", buf); 56231744Shelbig} 56331744Shelbig 564204908Sedwin#define MW(mw, me) ((mw) + me) 565204697Sedwin#define DECREASEMONTH(m, y) \ 566204697Sedwin if (--m == 0) { \ 567204697Sedwin m = 12; \ 568204697Sedwin y--; \ 569204697Sedwin } 570204697Sedwin#define INCREASEMONTH(m, y) \ 571204697Sedwin if (++(m) == 13) { \ 572204697Sedwin (m) = 1; \ 573204697Sedwin (y)++; \ 574204697Sedwin } 575204697Sedwin#define M2Y(m) ((m) / 12) 576204697Sedwin#define M2M(m) (1 + (m) % 12) 577204697Sedwin 578205071Sedwin/* Print all months for the period in the range [ before .. y-m .. after ]. */ 57931744Shelbigvoid 580205071Sedwinmonthrangeb(int y, int m, int jd_flag, int before, int after) 58131744Shelbig{ 582204697Sedwin struct monthlines year[12]; 58331744Shelbig struct weekdays wds; 584204697Sedwin char s[MAX_WIDTH], t[MAX_WIDTH]; 585204697Sedwin wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH]; 586204697Sedwin const char *wdss; 587204697Sedwin int i, j; 588204697Sedwin int mpl; 589204697Sedwin int mw; 590204697Sedwin int m1, m2; 591204697Sedwin int printyearheader; 592204697Sedwin int prevyear = -1; 59331744Shelbig 594204697Sedwin mpl = jd_flag ? 2 : 3; 595204697Sedwin mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 596204697Sedwin wdss = (mpl == 2) ? " " : ""; 597204697Sedwin 598204697Sedwin while (before != 0) { 599204697Sedwin DECREASEMONTH(m, y); 600204697Sedwin before--; 601204697Sedwin after++; 602194366Sume } 603204697Sedwin m1 = y * 12 + m - 1; 604204697Sedwin m2 = m1 + after; 60531744Shelbig 60631744Shelbig mkweekdays(&wds); 60731744Shelbig 608204697Sedwin /* 609204697Sedwin * The year header is printed when there are more than 'mpl' months 610204697Sedwin * and if the first month is a multitude of 'mpl'. 611204697Sedwin * If not, it will print the year behind every month. 612204697Sedwin */ 613204697Sedwin printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 61431744Shelbig 615204697Sedwin m = m1; 616204697Sedwin while (m <= m2) { 617204697Sedwin int count = 0; 618204697Sedwin for (i = 0; i != mpl && m + i <= m2; i++) { 619204697Sedwin mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 620204697Sedwin count++; 621204697Sedwin } 62231744Shelbig 623204697Sedwin /* Empty line between two rows of months */ 624204697Sedwin if (m != m1) 625204697Sedwin printf("\n"); 62631744Shelbig 627205071Sedwin /* Year at the top. */ 628204697Sedwin if (printyearheader && M2Y(m) != prevyear) { 629204697Sedwin sprintf(s, "%d", M2Y(m)); 630204697Sedwin printf("%s\n", center(t, s, mpl * mw)); 631204697Sedwin prevyear = M2Y(m); 632204697Sedwin } 63331744Shelbig 634205071Sedwin /* Month names. */ 635204697Sedwin for (i = 0; i < count; i++) 636204697Sedwin if (printyearheader) 637204697Sedwin wprintf(L"%-*ls ", 638204697Sedwin mw, wcenter(ws, year[i].name, mw)); 639204697Sedwin else { 640204697Sedwin swprintf(ws, sizeof(ws), L"%-ls %d", 641204697Sedwin year[i].name, M2Y(m + i)); 642204697Sedwin wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw)); 643204697Sedwin } 644204697Sedwin printf("\n"); 64531744Shelbig 646205071Sedwin /* Day of the week names. */ 647204697Sedwin for (i = 0; i < count; i++) { 648204697Sedwin wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ", 649204697Sedwin wdss, wds.names[6], wdss, wds.names[0], 650204697Sedwin wdss, wds.names[1], wdss, wds.names[2], 651204697Sedwin wdss, wds.names[3], wdss, wds.names[4], 652204697Sedwin wdss, wds.names[5]); 653204697Sedwin } 654204697Sedwin printf("\n"); 65531744Shelbig 656205071Sedwin /* And the days of the month. */ 657204697Sedwin for (i = 0; i != 6; i++) { 658204697Sedwin for (j = 0; j < count; j++) 659204908Sedwin printf("%-*s ", 660204908Sedwin MW(mw, year[j].extralen[i]), 661204908Sedwin year[j].lines[i]+1); 662204697Sedwin printf("\n"); 663204697Sedwin } 66431744Shelbig 665204697Sedwin m += mpl; 66631744Shelbig } 66731744Shelbig} 66831744Shelbig 66931744Shelbigvoid 670205071Sedwinmonthranger(int y, int m, int jd_flag, int before, int after) 67131744Shelbig{ 67231744Shelbig struct monthlines year[12]; 67331744Shelbig struct weekdays wds; 674204697Sedwin char s[MAX_WIDTH], t[MAX_WIDTH]; 67531744Shelbig int i, j; 67631744Shelbig int mpl; 67731744Shelbig int mw; 678204697Sedwin int m1, m2; 679204697Sedwin int prevyear = -1; 680204697Sedwin int printyearheader; 68131744Shelbig 682204697Sedwin mpl = jd_flag ? 3 : 4; 683204697Sedwin mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R; 684204697Sedwin 685204697Sedwin while (before != 0) { 686204697Sedwin DECREASEMONTH(m, y); 687204697Sedwin before--; 688204697Sedwin after++; 689204697Sedwin } 690204697Sedwin m1 = y * 12 + m - 1; 691204697Sedwin m2 = m1 + after; 692204697Sedwin 69331744Shelbig mkweekdays(&wds); 69431744Shelbig 695204697Sedwin /* 696204697Sedwin * The year header is printed when there are more than 'mpl' months 697204697Sedwin * and if the first month is a multitude of 'mpl'. 698204697Sedwin * If not, it will print the year behind every month. 699204697Sedwin */ 700204697Sedwin printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0; 70131744Shelbig 702204697Sedwin m = m1; 703204697Sedwin while (m <= m2) { 704204697Sedwin int count = 0; 705204697Sedwin for (i = 0; i != mpl && m + i <= m2; i++) { 706204697Sedwin mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i); 707204697Sedwin count++; 708204697Sedwin } 70931744Shelbig 710205071Sedwin /* Empty line between two rows of months. */ 711204697Sedwin if (m != m1) 712204697Sedwin printf("\n"); 713204697Sedwin 714205071Sedwin /* Year at the top. */ 715204697Sedwin if (printyearheader && M2Y(m) != prevyear) { 716204697Sedwin sprintf(s, "%d", M2Y(m)); 717204697Sedwin printf("%s\n", center(t, s, mpl * mw)); 718204697Sedwin prevyear = M2Y(m); 719204697Sedwin } 720204697Sedwin 721205071Sedwin /* Month names. */ 722204697Sedwin wprintf(L" "); 723204697Sedwin for (i = 0; i < count; i++) 724204697Sedwin if (printyearheader) 725204697Sedwin wprintf(L"%-*ls", mw, year[i].name); 72631744Shelbig else 727204697Sedwin wprintf(L"%-ls %-*d", year[i].name, 728204697Sedwin mw - wcslen(year[i].name) - 1, M2Y(m + i)); 729204697Sedwin printf("\n"); 73031744Shelbig 731205071Sedwin /* And the days of the month. */ 732204697Sedwin for (i = 0; i != 7; i++) { 733204697Sedwin /* Week day */ 734204697Sedwin wprintf(L"%.2ls", wds.names[i]); 735204697Sedwin 736204697Sedwin /* Full months */ 737204697Sedwin for (j = 0; j < count; j++) 738204697Sedwin printf("%-*s", 739204908Sedwin MW(mw, year[j].extralen[i]), 740204908Sedwin year[j].lines[i]); 741204697Sedwin printf("\n"); 74231744Shelbig } 743204697Sedwin 744205071Sedwin /* Week numbers. */ 745204697Sedwin if (flag_weeks) { 746204697Sedwin printf(" "); 747204697Sedwin for (i = 0; i < count; i++) 748204697Sedwin printf("%-*s", mw, year[i].weeks); 749204697Sedwin printf("\n"); 750204697Sedwin } 751204697Sedwin 752204697Sedwin m += mpl; 75331744Shelbig } 754204697Sedwin return; 75531744Shelbig} 75631744Shelbig 75731744Shelbigvoid 758204697Sedwinmkmonthr(int y, int m, int jd_flag, struct monthlines *mlines) 75931744Shelbig{ 76031744Shelbig 76131744Shelbig struct tm tm; /* for strftime printing local names of 76231744Shelbig * months */ 76331744Shelbig date dt; /* handy date */ 76431744Shelbig int dw; /* width of numbers */ 76531744Shelbig int first; /* first day of month */ 76631744Shelbig int firstm; /* first day of first week of month */ 767191330Srdivacky int i, j, k, l; /* just indices */ 76831744Shelbig int last; /* the first day of next month */ 76931744Shelbig int jan1 = 0; /* the first day of this year */ 77031744Shelbig char *ds; /* pointer to day strings (daystr or 77131744Shelbig * jdaystr) */ 77231744Shelbig 77331744Shelbig /* Set name of month. */ 77431744Shelbig memset(&tm, 0, sizeof(tm)); 77531744Shelbig tm.tm_mon = m; 776189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 777189804Sdas L"%OB", &tm); 778189804Sdas mlines->name[0] = towupper(mlines->name[0]); 77931744Shelbig 78031744Shelbig /* 78131744Shelbig * Set first and last to the day number of the first day of this 78231744Shelbig * month and the first day of next month respectively. Set jan1 to 78332282Shelbig * the day number of the first day of this year. 78431744Shelbig */ 78532282Shelbig first = firstday(y, m + 1); 78632282Shelbig if (m == 11) 78732282Shelbig last = firstday(y + 1, 1); 78832282Shelbig else 78932282Shelbig last = firstday(y, m + 2); 79031744Shelbig 79132282Shelbig if (jd_flag) 79232525Shelbig jan1 = firstday(y, 1); 79331744Shelbig 79431744Shelbig /* 79531744Shelbig * Set firstm to the day number of monday of the first week of 79631744Shelbig * this month. (This might be in the last month) 79731744Shelbig */ 79831744Shelbig firstm = first - weekday(first); 79931744Shelbig 800205071Sedwin /* Set ds (daystring) and dw (daywidth) according to the jd_flag. */ 80131744Shelbig if (jd_flag) { 80231744Shelbig ds = jdaystr; 80331744Shelbig dw = 4; 80431744Shelbig } else { 80531744Shelbig ds = daystr; 80631744Shelbig dw = 3; 80731744Shelbig } 80831744Shelbig 80931744Shelbig /* 81031744Shelbig * Fill the lines with day of month or day of year (julian day) 81131744Shelbig * line index: i, each line is one weekday. column index: j, each 81231744Shelbig * column is one day number. print column index: k. 81331744Shelbig */ 81431744Shelbig for (i = 0; i != 7; i++) { 815191330Srdivacky l = 0; 816191330Srdivacky for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 81731744Shelbig if (j >= first) { 81831744Shelbig if (jd_flag) 81931744Shelbig dt.d = j - jan1 + 1; 82031744Shelbig else 821204697Sedwin sdater(j, &dt); 822205071Sedwin if (j == highlightdate && !flag_nohighlight) 823204849Sedwin highlight(mlines->lines[i] + k, 824204849Sedwin ds + dt.d * dw, dw, &l); 825204849Sedwin else 826204849Sedwin memcpy(mlines->lines[i] + k + l, 827204849Sedwin ds + dt.d * dw, dw); 82831744Shelbig } else 829191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 830191330Srdivacky } 831191330Srdivacky mlines->lines[i][k + l] = '\0'; 832204908Sedwin mlines->extralen[i] = l; 83331744Shelbig } 83431744Shelbig 835205071Sedwin /* fill the weeknumbers. */ 83631744Shelbig if (flag_weeks) { 83731744Shelbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 83831744Shelbig if (j <= nswitch) 83931744Shelbig memset(mlines->weeks + k, ' ', dw); 84031744Shelbig else 84131744Shelbig memcpy(mlines->weeks + k, 84231744Shelbig ds + week(j, &i)*dw, dw); 84331744Shelbig mlines->weeks[k] = '\0'; 84431744Shelbig } 84531744Shelbig} 84631744Shelbig 84731744Shelbigvoid 84831744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 84931744Shelbig{ 85031744Shelbig 85131744Shelbig struct tm tm; /* for strftime printing local names of 85231744Shelbig * months */ 85331744Shelbig date dt; /* handy date */ 85431744Shelbig int dw; /* width of numbers */ 85531744Shelbig int first; /* first day of month */ 85631744Shelbig int firsts; /* sunday of first week of month */ 857191330Srdivacky int i, j, k, l; /* just indices */ 85831744Shelbig int jan1 = 0; /* the first day of this year */ 85931744Shelbig int last; /* the first day of next month */ 86031744Shelbig char *ds; /* pointer to day strings (daystr or 86131744Shelbig * jdaystr) */ 86231744Shelbig 86331744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 86431744Shelbig if (jd_flag) { 86531744Shelbig ds = jdaystr; 86631744Shelbig dw = 4; 86731744Shelbig } else { 86831744Shelbig ds = daystr; 86931744Shelbig dw = 3; 87031744Shelbig } 87131744Shelbig 872205071Sedwin /* Set name of month centered. */ 87331744Shelbig memset(&tm, 0, sizeof(tm)); 87431744Shelbig tm.tm_mon = m; 875189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 876189804Sdas L"%OB", &tm); 877189804Sdas mlines->name[0] = towupper(mlines->name[0]); 87831744Shelbig 87931744Shelbig /* 88031744Shelbig * Set first and last to the day number of the first day of this 88131744Shelbig * month and the first day of next month respectively. Set jan1 to 88231744Shelbig * the day number of Jan 1st of this year. 88331744Shelbig */ 88431744Shelbig dt.y = y; 88531744Shelbig dt.m = m + 1; 88631744Shelbig dt.d = 1; 88731744Shelbig first = sndaysb(&dt); 88831744Shelbig if (m == 11) { 88931744Shelbig dt.y = y + 1; 89031744Shelbig dt.m = 1; 89131744Shelbig dt.d = 1; 89231744Shelbig } else { 89331744Shelbig dt.y = y; 89431744Shelbig dt.m = m + 2; 89531744Shelbig dt.d = 1; 89631744Shelbig } 89731744Shelbig last = sndaysb(&dt); 89831744Shelbig 89931744Shelbig if (jd_flag) { 90031744Shelbig dt.y = y; 90131744Shelbig dt.m = 1; 90231744Shelbig dt.d = 1; 90331744Shelbig jan1 = sndaysb(&dt); 90431744Shelbig } 90531744Shelbig 90631744Shelbig /* 90731744Shelbig * Set firsts to the day number of sunday of the first week of 90831744Shelbig * this month. (This might be in the last month) 90931744Shelbig */ 91031744Shelbig firsts = first - (weekday(first)+1) % 7; 91131744Shelbig 91231744Shelbig /* 91331744Shelbig * Fill the lines with day of month or day of year (Julian day) 91431744Shelbig * line index: i, each line is one week. column index: j, each 91531744Shelbig * column is one day number. print column index: k. 91631744Shelbig */ 91731744Shelbig for (i = 0; i != 6; i++) { 918191330Srdivacky l = 0; 91931744Shelbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 920191330Srdivacky j++, k += dw) { 921204849Sedwin if (j >= first) { 922191330Srdivacky if (jd_flag) 923191330Srdivacky dt.d = j - jan1 + 1; 924191330Srdivacky else 925191330Srdivacky sdateb(j, &dt); 926205071Sedwin if (j == highlightdate && !flag_nohighlight) 927204849Sedwin highlight(mlines->lines[i] + k, 928204849Sedwin ds + dt.d * dw, dw, &l); 92931744Shelbig else 930204849Sedwin memcpy(mlines->lines[i] + k + l, 931204849Sedwin ds + dt.d * dw, dw); 93231744Shelbig } else 933191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 934191330Srdivacky } 93531744Shelbig if (k == 0) 93631744Shelbig mlines->lines[i][1] = '\0'; 93731744Shelbig else 938191330Srdivacky mlines->lines[i][k + l] = '\0'; 939204908Sedwin mlines->extralen[i] = l; 94031744Shelbig } 94131744Shelbig} 94231744Shelbig 943205071Sedwin/* Put the local names of weekdays into the wds. */ 94432139Shelbigvoid 94532139Shelbigmkweekdays(struct weekdays *wds) 94631744Shelbig{ 947194366Sume int i, len, width = 0; 94831744Shelbig struct tm tm; 949189804Sdas wchar_t buf[20]; 95031744Shelbig 95131744Shelbig memset(&tm, 0, sizeof(tm)); 95231744Shelbig 95331744Shelbig for (i = 0; i != 7; i++) { 95431744Shelbig tm.tm_wday = (i+1) % 7; 955189804Sdas wcsftime(buf, sizeof(buf), L"%a", &tm); 956194447Sume for (len = 2; len > 0; --len) { 957194366Sume if ((width = wcswidth(buf, len)) <= 2) 958194366Sume break; 959194366Sume } 960194366Sume wmemset(wds->names[i], L'\0', 4); 961194366Sume if (width == 1) 962194366Sume wds->names[i][0] = L' '; 963194366Sume wcsncat(wds->names[i], buf, len); 964194447Sume wcsncat(wds->names[i], L" ", 1); 96531744Shelbig } 96631744Shelbig} 96731744Shelbig 96831744Shelbig/* 969205071Sedwin * Compute the day number of the first existing date after the first day in 970205071Sedwin * month. (the first day in month and even the month might not exist!) 97132282Shelbig */ 97232282Shelbigint 97332282Shelbigfirstday(int y, int m) 97432282Shelbig{ 97532282Shelbig date dt; 97632282Shelbig int nd; 97732282Shelbig 97832282Shelbig dt.y = y; 97932282Shelbig dt.m = m; 98032282Shelbig dt.d = 1; 981204697Sedwin nd = sndaysr(&dt); 98245064Shelbig for (;;) { 983204697Sedwin sdater(nd, &dt); 98445064Shelbig if ((dt.m >= m && dt.y == y) || dt.y > y) 98545064Shelbig return (nd); 98645064Shelbig else 98745064Shelbig nd++; 98845064Shelbig } 98945064Shelbig /* NEVER REACHED */ 99032282Shelbig} 99132282Shelbig 99232282Shelbig/* 99331744Shelbig * Compute the number of days from date, obey the local switch from 99431744Shelbig * Julian to Gregorian if specified by the user. 99531744Shelbig */ 99631744Shelbigint 997204697Sedwinsndaysr(struct date *d) 99831744Shelbig{ 99931744Shelbig 100031744Shelbig if (nswitch != 0) 100131744Shelbig if (nswitch < ndaysj(d)) 100231744Shelbig return (ndaysg(d)); 100331744Shelbig else 100431744Shelbig return (ndaysj(d)); 100531744Shelbig else 100631744Shelbig return ndaysg(d); 100731744Shelbig} 100831744Shelbig 100931744Shelbig/* 101031744Shelbig * Compute the number of days from date, obey the switch from 101131744Shelbig * Julian to Gregorian as used by UK and her colonies. 101231744Shelbig */ 101331744Shelbigint 101431744Shelbigsndaysb(struct date *d) 101531744Shelbig{ 101631744Shelbig 101731744Shelbig if (nswitchb < ndaysj(d)) 101831744Shelbig return (ndaysg(d)); 101931744Shelbig else 102031744Shelbig return (ndaysj(d)); 102131744Shelbig} 102231744Shelbig 1023205071Sedwin/* Inverse of sndays. */ 102431744Shelbigstruct date * 1025204697Sedwinsdater(int nd, struct date *d) 102631744Shelbig{ 102731744Shelbig 102831744Shelbig if (nswitch < nd) 102931744Shelbig return (gdate(nd, d)); 103031744Shelbig else 103131744Shelbig return (jdate(nd, d)); 103231744Shelbig} 103331744Shelbig 1034205071Sedwin/* Inverse of sndaysb. */ 103531744Shelbigstruct date * 103631744Shelbigsdateb(int nd, struct date *d) 103731744Shelbig{ 103831744Shelbig 103931744Shelbig if (nswitchb < nd) 104031744Shelbig return (gdate(nd, d)); 104131744Shelbig else 104231744Shelbig return (jdate(nd, d)); 104331744Shelbig} 104431744Shelbig 1045205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */ 104631744Shelbigchar * 104731744Shelbigcenter(char *s, char *t, int w) 104831744Shelbig{ 1049204697Sedwin char blanks[MAX_WIDTH]; 105031744Shelbig 105131744Shelbig memset(blanks, ' ', sizeof(blanks)); 105231744Shelbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 105331744Shelbig return (s); 105431744Shelbig} 1055104369Sroam 1056205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */ 1057189804Sdaswchar_t * 1058189804Sdaswcenter(wchar_t *s, wchar_t *t, int w) 1059189804Sdas{ 1060204697Sedwin char blanks[MAX_WIDTH]; 1061189804Sdas 1062189804Sdas memset(blanks, ' ', sizeof(blanks)); 1063189804Sdas swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 1064189804Sdas return (s); 1065189804Sdas} 1066189804Sdas 1067104369Sroamint 1068186401Swollmanparsemonth(const char *s, int *m, int *y) 1069104369Sroam{ 1070186401Swollman int nm, ny; 1071104369Sroam char *cp; 1072104369Sroam struct tm tm; 1073104369Sroam 1074186401Swollman nm = (int)strtol(s, &cp, 10); 1075186401Swollman if (cp != s) { 1076186401Swollman ny = *y; 1077186401Swollman if (*cp == '\0') { 1078186401Swollman ; /* no special action */ 1079186401Swollman } else if (*cp == 'f' || *cp == 'F') { 1080186401Swollman if (nm <= *m) 1081186401Swollman ny++; 1082186401Swollman } else if (*cp == 'p' || *cp == 'P') { 1083186401Swollman if (nm >= *m) 1084186401Swollman ny--; 1085186401Swollman } else 1086186401Swollman return (1); 1087186401Swollman if (nm < 1 || nm > 12) 1088186401Swollman return 1; 1089186401Swollman *m = nm; 1090186401Swollman *y = ny; 1091186401Swollman return (0); 1092186401Swollman } 1093186401Swollman if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 1094186401Swollman *m = tm.tm_mon + 1; 1095186401Swollman return (0); 1096186401Swollman } 1097186401Swollman return (1); 1098104369Sroam} 1099204849Sedwin 1100204849Sedwinvoid 1101204849Sedwinhighlight(char *dst, char *src, int len, int *extralen) 1102204849Sedwin{ 1103204849Sedwin static int first = 1; 1104204849Sedwin static const char *term_so, *term_se; 1105204849Sedwin 1106204849Sedwin if (first) { 1107204849Sedwin char tbuf[1024], cbuf[512], *b; 1108204849Sedwin 1109204849Sedwin term_se = term_so = NULL; 1110204849Sedwin 1111205071Sedwin /* On how to highlight on this type of terminal (if any). */ 1112204849Sedwin if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) { 1113204849Sedwin b = cbuf; 1114204849Sedwin term_so = tgetstr("so", &b); 1115204849Sedwin term_se = tgetstr("se", &b); 1116204849Sedwin } 1117204849Sedwin 1118204849Sedwin first = 0; 1119204849Sedwin } 1120204849Sedwin 1121204849Sedwin /* 1122204849Sedwin * This check is not necessary, should have been handled before calling 1123204849Sedwin * this function. 1124204849Sedwin */ 1125204849Sedwin if (flag_nohighlight) { 1126204849Sedwin memcpy(dst, src, len); 1127204849Sedwin return; 1128204849Sedwin } 1129204849Sedwin 1130205071Sedwin /* 1131205071Sedwin * If it is a real terminal, use the data from the termcap database. 1132205071Sedwin */ 1133204849Sedwin if (term_so != NULL && term_se != NULL) { 1134205071Sedwin /* separator. */ 1135204849Sedwin dst[0] = ' '; 1136204849Sedwin dst++; 1137205071Sedwin /* highlight on. */ 1138204849Sedwin memcpy(dst, term_so, strlen(term_so)); 1139204849Sedwin dst += strlen(term_so); 1140205071Sedwin /* the actual text. (minus leading space) */ 1141204849Sedwin len--; 1142204849Sedwin src++; 1143204849Sedwin memcpy(dst, src, len); 1144204849Sedwin dst += len; 1145205071Sedwin /* highlight off. */ 1146204849Sedwin memcpy(dst, term_se, strlen(term_se)); 1147204849Sedwin *extralen = strlen(term_so) + strlen(term_se); 1148204849Sedwin return; 1149204849Sedwin } 1150204849Sedwin 1151204849Sedwin /* 1152205071Sedwin * Otherwise, print a _, backspace and the letter. 1153204849Sedwin */ 1154204849Sedwin *extralen = 0; 1155205071Sedwin /* skip leading space. */ 1156204849Sedwin src++; 1157204849Sedwin len--; 1158205071Sedwin /* separator. */ 1159204849Sedwin dst[0] = ' '; 1160204849Sedwin dst++; 1161204849Sedwin while (len > 0) { 1162205071Sedwin /* _ and backspace. */ 1163204849Sedwin memcpy(dst, "_\010", 2); 1164204849Sedwin dst += 2; 1165204849Sedwin *extralen += 2; 1166205071Sedwin /* the character. */ 1167204849Sedwin *dst++ = *src++; 1168204849Sedwin len--; 1169204849Sedwin } 1170205071Sedwin return; 1171204849Sedwin} 1172