ncal.c revision 191330
1/*- 2 * Copyright (c) 1997 Wolfgang Helbig 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#ifndef lint 28static const char rcsid[] = 29 "$FreeBSD: head/usr.bin/ncal/ncal.c 191330 2009-04-20 18:19:38Z rdivacky $"; 30#endif /* not lint */ 31 32#include <calendar.h> 33#include <ctype.h> 34#include <err.h> 35#include <langinfo.h> 36#include <locale.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <sysexits.h> 41#include <time.h> 42#include <unistd.h> 43#include <wchar.h> 44#include <wctype.h> 45#include <term.h> 46#undef lines /* term.h defines this */ 47 48/* Width of one month with backward compatibility */ 49#define MONTH_WIDTH_B_J 27 50#define MONTH_WIDTH_B 20 51 52#define MONTH_WIDTH_J 24 53#define MONTH_WIDTH 18 54 55#define MAX_WIDTH 64 56 57typedef struct date date; 58 59struct monthlines { 60 wchar_t name[MAX_WIDTH + 1]; 61 char lines[7][MAX_WIDTH + 1]; 62 char weeks[MAX_WIDTH + 1]; 63}; 64 65struct weekdays { 66 wchar_t names[7][4]; 67}; 68 69/* The switches from Julian to Gregorian in some countries */ 70static struct djswitch { 71 const char *cc; /* Country code according to ISO 3166 */ 72 const char *nm; /* Name of country */ 73 date dt; /* Last day of Julian calendar */ 74} switches[] = { 75 {"AL", "Albania", {1912, 11, 30}}, 76 {"AT", "Austria", {1583, 10, 5}}, 77 {"AU", "Australia", {1752, 9, 2}}, 78 {"BE", "Belgium", {1582, 12, 14}}, 79 {"BG", "Bulgaria", {1916, 3, 18}}, 80 {"CA", "Canada", {1752, 9, 2}}, 81 {"CH", "Switzerland", {1655, 2, 28}}, 82 {"CN", "China", {1911, 12, 18}}, 83 {"CZ", "Czech Republic",{1584, 1, 6}}, 84 {"DE", "Germany", {1700, 2, 18}}, 85 {"DK", "Denmark", {1700, 2, 18}}, 86 {"ES", "Spain", {1582, 10, 4}}, 87 {"FI", "Finland", {1753, 2, 17}}, 88 {"FR", "France", {1582, 12, 9}}, 89 {"GB", "United Kingdom",{1752, 9, 2}}, 90 {"GR", "Greece", {1924, 3, 9}}, 91 {"HU", "Hungary", {1587, 10, 21}}, 92 {"IS", "Iceland", {1700, 11, 16}}, 93 {"IT", "Italy", {1582, 10, 4}}, 94 {"JP", "Japan", {1918, 12, 18}}, 95 {"LI", "Lithuania", {1918, 2, 1}}, 96 {"LN", "Latin", {9999, 05, 31}}, 97 {"LU", "Luxembourg", {1582, 12, 14}}, 98 {"LV", "Latvia", {1918, 2, 1}}, 99 {"NL", "Netherlands", {1582, 12, 14}}, 100 {"NO", "Norway", {1700, 2, 18}}, 101 {"PL", "Poland", {1582, 10, 4}}, 102 {"PT", "Portugal", {1582, 10, 4}}, 103 {"RO", "Romania", {1919, 3, 31}}, 104 {"RU", "Russia", {1918, 1, 31}}, 105 {"SI", "Slovenia", {1919, 3, 4}}, 106 {"SW", "Sweden", {1753, 2, 17}}, 107 {"TR", "Turkey", {1926, 12, 18}}, 108 {"US", "United States", {1752, 9, 2}}, 109 {"YU", "Yugoslavia", {1919, 3, 4}} 110}; 111 112struct djswitch *dftswitch = 113 switches + sizeof(switches) / sizeof(struct djswitch) - 2; 114 /* default switch (should be "US") */ 115 116/* Table used to print day of month and week numbers */ 117char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 118 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 119 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 120 " 48 49 50 51 52 53"; 121 122/* Table used to print day of year and week numbers */ 123char jdaystr[] = " 1 2 3 4 5 6 7 8 9" 124 " 10 11 12 13 14 15 16 17 18 19" 125 " 20 21 22 23 24 25 26 27 28 29" 126 " 30 31 32 33 34 35 36 37 38 39" 127 " 40 41 42 43 44 45 46 47 48 49" 128 " 50 51 52 53 54 55 56 57 58 59" 129 " 60 61 62 63 64 65 66 67 68 69" 130 " 70 71 72 73 74 75 76 77 78 79" 131 " 80 81 82 83 84 85 86 87 88 89" 132 " 90 91 92 93 94 95 96 97 98 99" 133 " 100 101 102 103 104 105 106 107 108 109" 134 " 110 111 112 113 114 115 116 117 118 119" 135 " 120 121 122 123 124 125 126 127 128 129" 136 " 130 131 132 133 134 135 136 137 138 139" 137 " 140 141 142 143 144 145 146 147 148 149" 138 " 150 151 152 153 154 155 156 157 158 159" 139 " 160 161 162 163 164 165 166 167 168 169" 140 " 170 171 172 173 174 175 176 177 178 179" 141 " 180 181 182 183 184 185 186 187 188 189" 142 " 190 191 192 193 194 195 196 197 198 199" 143 " 200 201 202 203 204 205 206 207 208 209" 144 " 210 211 212 213 214 215 216 217 218 219" 145 " 220 221 222 223 224 225 226 227 228 229" 146 " 230 231 232 233 234 235 236 237 238 239" 147 " 240 241 242 243 244 245 246 247 248 249" 148 " 250 251 252 253 254 255 256 257 258 259" 149 " 260 261 262 263 264 265 266 267 268 269" 150 " 270 271 272 273 274 275 276 277 278 279" 151 " 280 281 282 283 284 285 286 287 288 289" 152 " 290 291 292 293 294 295 296 297 298 299" 153 " 300 301 302 303 304 305 306 307 308 309" 154 " 310 311 312 313 314 315 316 317 318 319" 155 " 320 321 322 323 324 325 326 327 328 329" 156 " 330 331 332 333 334 335 336 337 338 339" 157 " 340 341 342 343 344 345 346 347 348 349" 158 " 350 351 352 353 354 355 356 357 358 359" 159 " 360 361 362 363 364 365 366"; 160 161int flag_weeks; /* user wants number of week */ 162int nswitch; /* user defined switch date */ 163int nswitchb; /* switch date for backward compatibility */ 164const char *term_r, *term_e; 165int today; 166 167char *center(char *s, char *t, int w); 168wchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 169void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl); 170void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 171void mkweekdays(struct weekdays * wds); 172int parsemonth(const char *s, int *m, int *y); 173void printcc(void); 174void printeaster(int year, int julian, int orthodox); 175void printmonth(int year, int month, int jd_flag); 176void printmonthb(int year, int month, int jd_flag); 177void printyear(int year, int jd_flag); 178void printyearb(int year, int jd_flag); 179int firstday(int y, int m); 180date *sdate(int ndays, struct date * d); 181date *sdateb(int ndays, struct date * d); 182int sndays(struct date * d); 183int sndaysb(struct date * d); 184static void usage(void); 185int weekdayb(int nd); 186 187int 188main(int argc, char *argv[]) 189{ 190 struct djswitch *p, *q; /* to search user defined switch date */ 191 date never = {10000, 1, 1}; /* outside valid range of dates */ 192 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 193 int ch; /* holds the option character */ 194 int m = 0; /* month */ 195 int y = 0; /* year */ 196 int flag_backward = 0; /* user called cal--backward compat. */ 197 int flag_hole_year = 0; /* user wants the whole year */ 198 int flag_julian_cal = 0; /* user wants Julian Calendar */ 199 int flag_julian_day = 0; /* user wants the Julian day 200 * numbers */ 201 int flag_orthodox = 0; /* use wants Orthodox easter */ 202 int flag_easter = 0; /* use wants easter date */ 203 char *cp; /* character pointer */ 204 char *flag_month = NULL; /* requested month as string */ 205 const char *locale; /* locale to get country code */ 206 char tbuf[1024], cbuf[512], *b; 207 time_t t; 208 struct tm *tm1; 209 210 term_e = term_r = NULL; 211 today = 0; 212 if (isatty(1) && tgetent(tbuf, getenv("TERM")) == 1) { 213 date dt; /* handy date */ 214 215 b = cbuf; 216 term_r = tgetstr("mr", &b); 217 term_e = tgetstr("me", &b); 218 t = time(NULL); 219 tm1 = localtime(&t); 220 dt.y = tm1->tm_year + 1900; 221 dt.m = tm1->tm_mon + 1; 222 dt.d = tm1->tm_mday; 223 224 today = sndaysb(&dt); 225 } 226 227 /* 228 * Use locale to determine the country code, 229 * and use the country code to determine the default 230 * switchdate and date format from the switches table. 231 */ 232 if (setlocale(LC_ALL, "") == NULL) 233 warn("setlocale"); 234 locale = setlocale(LC_TIME, NULL); 235 if (locale == NULL || 236 strcmp(locale, "C") == 0 || 237 strcmp(locale, "POSIX") == 0 || 238 strcmp(locale, "ASCII") == 0 || 239 strcmp(locale, "US-ASCII") == 0) 240 locale = "_US"; 241 q = switches + sizeof(switches) / sizeof(struct djswitch); 242 for (p = switches; p != q; p++) 243 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 244 break; 245 if (p == q) { 246 nswitch = ndaysj(&dftswitch->dt); 247 } else { 248 nswitch = ndaysj(&p->dt); 249 dftswitch = p; 250 } 251 252 253 /* 254 * Get the filename portion of argv[0] and set flag_backward if 255 * this program is called "cal". 256 */ 257 cp = strrchr(argv[0], '/'); 258 cp = (cp == NULL) ? argv[0] : cp + 1; 259 if (strcmp("cal", cp) == 0) 260 flag_backward = 1; 261 262 /* Set the switch date to United Kingdom if backwards compatible */ 263 if (flag_backward) 264 nswitchb = ndaysj(&ukswitch); 265 266 while ((ch = getopt(argc, argv, "Jejm:ops:wy")) != -1) 267 switch (ch) { 268 case 'J': 269 if (flag_backward) 270 usage(); 271 nswitch = ndaysj(&never); 272 flag_julian_cal = 1; 273 break; 274 case 'e': 275 if (flag_backward) 276 usage(); 277 flag_easter = 1; 278 break; 279 case 'j': 280 flag_julian_day = 1; 281 break; 282 case 'm': 283 flag_month = optarg; 284 break; 285 case 'o': 286 if (flag_backward) 287 usage(); 288 flag_orthodox = 1; 289 flag_easter = 1; 290 break; 291 case 'p': 292 if (flag_backward) 293 usage(); 294 printcc(); 295 return (0); 296 break; 297 case 's': 298 if (flag_backward) 299 usage(); 300 q = switches + 301 sizeof(switches) / sizeof(struct djswitch); 302 for (p = switches; 303 p != q && strcmp(p->cc, optarg) != 0; p++) 304 ; 305 if (p == q) 306 errx(EX_USAGE, 307 "%s: invalid country code", optarg); 308 nswitch = ndaysj(&(p->dt)); 309 break; 310 case 'w': 311 if (flag_backward) 312 usage(); 313 flag_weeks = 1; 314 break; 315 case 'y': 316 flag_hole_year = 1; 317 break; 318 default: 319 usage(); 320 } 321 322 argc -= optind; 323 argv += optind; 324 325 switch (argc) { 326 case 2: 327 if (flag_easter) 328 usage(); 329 flag_month = *argv++; 330 /* FALLTHROUGH */ 331 case 1: 332 y = atoi(*argv++); 333 if (y < 1 || y > 9999) 334 errx(EX_USAGE, "year %d not in range 1..9999", y); 335 break; 336 case 0: 337 { 338 time_t t; 339 struct tm *tm; 340 341 t = time(NULL); 342 tm = localtime(&t); 343 y = tm->tm_year + 1900; 344 m = tm->tm_mon + 1; 345 } 346 break; 347 default: 348 usage(); 349 } 350 351 if (flag_month != NULL) { 352 if (parsemonth(flag_month, &m, &y)) { 353 errx(EX_USAGE, 354 "%s is neither a month number (1..12) nor a name", 355 flag_month); 356 } 357 } 358 359 if (flag_easter) 360 printeaster(y, flag_julian_cal, flag_orthodox); 361 else if (argc == 1 || flag_hole_year) { 362 /* disable the highlight for now */ 363 today = 0; 364 if (flag_backward) 365 printyearb(y, flag_julian_day); 366 else 367 printyear(y, flag_julian_day); 368 } else 369 if (flag_backward) 370 printmonthb(y, m, flag_julian_day); 371 else 372 printmonth(y, m, flag_julian_day); 373 374 return (0); 375} 376 377static void 378usage(void) 379{ 380 381 fputs( 382 "usage: cal [-jy] [[month] year]\n" 383 " cal [-j] [-m month] [year]\n" 384 " ncal [-Jjpwy] [-s country_code] [[month] year]\n" 385 " ncal [-Jeo] [year]\n", stderr); 386 exit(EX_USAGE); 387} 388 389/* print the assumed switches for all countries */ 390void 391printcc(void) 392{ 393 struct djswitch *p; 394 int n; /* number of lines to print */ 395 int m; /* offset from left to right table entry on the same line */ 396 397#define FSTR "%c%s %-15s%4d-%02d-%02d" 398#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 399#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 400 401 n = sizeof(switches) / sizeof(struct djswitch); 402 m = (n + 1) / 2; 403 n /= 2; 404 for (p = switches; p != switches + n; p++) 405 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 406 if (m != n) 407 printf(FSTR"\n", FSTRARG(p)); 408} 409 410/* print the date of easter sunday */ 411void 412printeaster(int y, int julian, int orthodox) 413{ 414 date dt; 415 struct tm tm; 416 char buf[80]; 417 static int d_first = -1; 418 419 if (d_first < 0) 420 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 421 /* force orthodox easter for years before 1583 */ 422 if (y < 1583) 423 orthodox = 1; 424 425 if (orthodox) 426 if (julian) 427 easteroj(y, &dt); 428 else 429 easterog(y, &dt); 430 else 431 easterg(y, &dt); 432 433 memset(&tm, 0, sizeof(tm)); 434 tm.tm_year = dt.y - 1900; 435 tm.tm_mon = dt.m - 1; 436 tm.tm_mday = dt.d; 437 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 438 printf("%s\n", buf); 439} 440 441void 442printmonth(int y, int m, int jd_flag) 443{ 444 struct monthlines month; 445 struct weekdays wds; 446 int i; 447 448 mkmonth(y, m - 1, jd_flag, &month); 449 mkweekdays(&wds); 450 printf(" %ls %d\n", month.name, y); 451 for (i = 0; i != 7; i++) 452 printf("%.2ls%s\n", wds.names[i], month.lines[i]); 453 if (flag_weeks) 454 printf(" %s\n", month.weeks); 455} 456 457void 458printmonthb(int y, int m, int jd_flag) 459{ 460 struct monthlines month; 461 struct weekdays wds; 462 wchar_t s[MAX_WIDTH], t[MAX_WIDTH]; 463 int i; 464 int mw; 465 466 mkmonthb(y, m - 1, jd_flag, &month); 467 mkweekdays(&wds); 468 469 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 470 471 swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y); 472 wprintf(L"%ls\n", wcenter(t, s, mw)); 473 474 if (jd_flag) 475 wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n", 476 wds.names[6], wds.names[0], 477 wds.names[1], wds.names[2], wds.names[3], 478 wds.names[4], wds.names[5]); 479 else 480 wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6], 481 wds.names[0], wds.names[1], wds.names[2], wds.names[3], 482 wds.names[4], wds.names[5]); 483 484 for (i = 0; i != 6; i++) 485 printf("%s\n", month.lines[i]+1); 486} 487 488void 489printyear(int y, int jd_flag) 490{ 491 struct monthlines year[12]; 492 struct weekdays wds; 493 char s[80], t[80]; 494 int i, j; 495 int mpl; 496 int mw; 497 498 for (i = 0; i != 12; i++) 499 mkmonth(y, i, jd_flag, year + i); 500 mkweekdays(&wds); 501 mpl = jd_flag ? 3 : 4; 502 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH; 503 504 sprintf(s, "%d", y); 505 printf("%s\n", center(t, s, mpl * mw)); 506 507 for (j = 0; j != 12; j += mpl) { 508 printf(" %-*ls%-*ls", 509 mw, year[j].name, 510 mw, year[j + 1].name); 511 if (mpl == 3) 512 printf("%ls\n", year[j + 2].name); 513 else 514 printf("%-*ls%ls\n", 515 mw, year[j + 2].name, 516 year[j + 3].name); 517 for (i = 0; i != 7; i++) { 518 printf("%.2ls%-*s%-*s", 519 wds.names[i], 520 mw, year[j].lines[i], 521 mw, year[j + 1].lines[i]); 522 if (mpl == 3) 523 printf("%s\n", year[j + 2].lines[i]); 524 else 525 printf("%-*s%s\n", 526 mw, year[j + 2].lines[i], 527 year[j + 3].lines[i]); 528 } 529 if (flag_weeks) { 530 if (mpl == 3) 531 printf(" %-*s%-*s%-s\n", 532 mw, year[j].weeks, 533 mw, year[j + 1].weeks, 534 year[j + 2].weeks); 535 else 536 printf(" %-*s%-*s%-*s%-s\n", 537 mw, year[j].weeks, 538 mw, year[j + 1].weeks, 539 mw, year[j + 2].weeks, 540 year[j + 3].weeks); 541 } 542 } 543} 544 545void 546printyearb(int y, int jd_flag) 547{ 548 struct monthlines year[12]; 549 struct weekdays wds; 550 char s[80], t[80]; 551 wchar_t ws[80], wt[80]; 552 int i, j; 553 int mpl; 554 int mw; 555 556 for (i = 0; i != 12; i++) 557 mkmonthb(y, i, jd_flag, year + i); 558 mkweekdays(&wds); 559 mpl = jd_flag ? 2 : 3; 560 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 561 562 sprintf(s, "%d", y); 563 printf("%s\n\n", center(t, s, mw * mpl + mpl)); 564 565 for (j = 0; j != 12; j += mpl) { 566 printf("%-*ls ", mw, wcenter(ws, year[j].name, mw)); 567 if (mpl == 2) 568 printf("%ls\n", wcenter(ws, year[j + 1].name, mw)); 569 else 570 printf("%-*ls %ls\n", mw, 571 wcenter(ws, year[j + 1].name, mw), 572 wcenter(wt, year[j + 2].name, mw)); 573 574 if (mpl == 2) 575 wprintf(L" %ls %ls %ls %ls %ls %ls %ls " 576 " %ls %ls %ls %ls %ls %ls %.2ls\n", 577 wds.names[6], wds.names[0], wds.names[1], 578 wds.names[2], wds.names[3], wds.names[4], 579 wds.names[5], 580 wds.names[6], wds.names[0], wds.names[1], 581 wds.names[2], wds.names[3], wds.names[4], 582 wds.names[5]); 583 else 584 wprintf(L"%ls%ls%ls%ls%ls%ls%ls " 585 "%ls%ls%ls%ls%ls%ls%ls " 586 "%ls%ls%ls%ls%ls%ls%.2ls\n", 587 wds.names[6], wds.names[0], wds.names[1], 588 wds.names[2], wds.names[3], wds.names[4], 589 wds.names[5], 590 wds.names[6], wds.names[0], wds.names[1], 591 wds.names[2], wds.names[3], wds.names[4], 592 wds.names[5], 593 wds.names[6], wds.names[0], wds.names[1], 594 wds.names[2], wds.names[3], wds.names[4], 595 wds.names[5]); 596 for (i = 0; i != 6; i++) { 597 if (mpl == 2) 598 printf("%-*s %s\n", 599 mw, year[j].lines[i]+1, 600 year[j + 1].lines[i]+1); 601 else 602 printf("%-*s %-*s %s\n", 603 mw, year[j].lines[i]+1, 604 mw, year[j + 1].lines[i]+1, 605 year[j + 2].lines[i]+1); 606 607 } 608 } 609} 610 611void 612mkmonth(int y, int m, int jd_flag, struct monthlines *mlines) 613{ 614 615 struct tm tm; /* for strftime printing local names of 616 * months */ 617 date dt; /* handy date */ 618 int dw; /* width of numbers */ 619 int first; /* first day of month */ 620 int firstm; /* first day of first week of month */ 621 int i, j, k, l; /* just indices */ 622 int last; /* the first day of next month */ 623 int jan1 = 0; /* the first day of this year */ 624 char *ds; /* pointer to day strings (daystr or 625 * jdaystr) */ 626 627 /* Set name of month. */ 628 memset(&tm, 0, sizeof(tm)); 629 tm.tm_mon = m; 630 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 631 L"%OB", &tm); 632 mlines->name[0] = towupper(mlines->name[0]); 633 634 /* 635 * Set first and last to the day number of the first day of this 636 * month and the first day of next month respectively. Set jan1 to 637 * the day number of the first day of this year. 638 */ 639 first = firstday(y, m + 1); 640 if (m == 11) 641 last = firstday(y + 1, 1); 642 else 643 last = firstday(y, m + 2); 644 645 if (jd_flag) 646 jan1 = firstday(y, 1); 647 648 /* 649 * Set firstm to the day number of monday of the first week of 650 * this month. (This might be in the last month) 651 */ 652 firstm = first - weekday(first); 653 654 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 655 if (jd_flag) { 656 ds = jdaystr; 657 dw = 4; 658 } else { 659 ds = daystr; 660 dw = 3; 661 } 662 663 /* 664 * Fill the lines with day of month or day of year (julian day) 665 * line index: i, each line is one weekday. column index: j, each 666 * column is one day number. print column index: k. 667 */ 668 for (i = 0; i != 7; i++) { 669 l = 0; 670 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 671 if (j == today && (term_r != NULL && term_e != NULL)) { 672 l = strlen(term_r); 673 if (jd_flag) 674 dt.d = j - jan1 + 1; 675 else 676 sdateb(j, &dt); 677 /* separator */ 678 mlines->lines[i][k] = ' '; 679 /* the actual text */ 680 memcpy(mlines->lines[i] + k + l, 681 ds + dt.d * dw, dw); 682 /* highlight on */ 683 memcpy(mlines->lines[i] + k + 1, term_r, l); 684 /* highlight off */ 685 memcpy(mlines->lines[i] + k + l + dw, term_e, 686 strlen(term_e)); 687 l = strlen(term_e) + strlen(term_r); 688 continue; 689 } 690 if (j >= first) { 691 if (jd_flag) 692 dt.d = j - jan1 + 1; 693 else 694 sdate(j, &dt); 695 memcpy(mlines->lines[i] + k + l, 696 ds + dt.d * dw, dw); 697 } else 698 memcpy(mlines->lines[i] + k + l, " ", dw); 699 } 700 mlines->lines[i][k + l] = '\0'; 701 702 } 703 704 /* fill the weeknumbers */ 705 if (flag_weeks) { 706 for (j = firstm, k = 0; j < last; k += dw, j += 7) 707 if (j <= nswitch) 708 memset(mlines->weeks + k, ' ', dw); 709 else 710 memcpy(mlines->weeks + k, 711 ds + week(j, &i)*dw, dw); 712 mlines->weeks[k] = '\0'; 713 } 714} 715 716void 717mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 718{ 719 720 struct tm tm; /* for strftime printing local names of 721 * months */ 722 date dt; /* handy date */ 723 int dw; /* width of numbers */ 724 int first; /* first day of month */ 725 int firsts; /* sunday of first week of month */ 726 int i, j, k, l; /* just indices */ 727 int jan1 = 0; /* the first day of this year */ 728 int last; /* the first day of next month */ 729 char *ds; /* pointer to day strings (daystr or 730 * jdaystr) */ 731 732 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 733 if (jd_flag) { 734 ds = jdaystr; 735 dw = 4; 736 } else { 737 ds = daystr; 738 dw = 3; 739 } 740 741 /* Set name of month centered */ 742 memset(&tm, 0, sizeof(tm)); 743 tm.tm_mon = m; 744 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 745 L"%OB", &tm); 746 mlines->name[0] = towupper(mlines->name[0]); 747 748 /* 749 * Set first and last to the day number of the first day of this 750 * month and the first day of next month respectively. Set jan1 to 751 * the day number of Jan 1st of this year. 752 */ 753 dt.y = y; 754 dt.m = m + 1; 755 dt.d = 1; 756 first = sndaysb(&dt); 757 if (m == 11) { 758 dt.y = y + 1; 759 dt.m = 1; 760 dt.d = 1; 761 } else { 762 dt.y = y; 763 dt.m = m + 2; 764 dt.d = 1; 765 } 766 last = sndaysb(&dt); 767 768 if (jd_flag) { 769 dt.y = y; 770 dt.m = 1; 771 dt.d = 1; 772 jan1 = sndaysb(&dt); 773 } 774 775 /* 776 * Set firsts to the day number of sunday of the first week of 777 * this month. (This might be in the last month) 778 */ 779 firsts = first - (weekday(first)+1) % 7; 780 781 /* 782 * Fill the lines with day of month or day of year (Julian day) 783 * line index: i, each line is one week. column index: j, each 784 * column is one day number. print column index: k. 785 */ 786 for (i = 0; i != 6; i++) { 787 l = 0; 788 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 789 j++, k += dw) { 790 if (j == today && (term_r != NULL && term_e != NULL)) { 791 l = strlen(term_r); 792 if (jd_flag) 793 dt.d = j - jan1 + 1; 794 else 795 sdateb(j, &dt); 796 /* separator */ 797 mlines->lines[i][k] = ' '; 798 /* the actual text */ 799 memcpy(mlines->lines[i] + k + l, 800 ds + dt.d * dw, dw); 801 /* highlight on */ 802 memcpy(mlines->lines[i] + k + 1, term_r, l); 803 /* highlight off */ 804 memcpy(mlines->lines[i] + k + l + dw, term_e, 805 strlen(term_e)); 806 l = strlen(term_e) + strlen(term_r); 807 continue; 808 } 809 if (j >= first) { 810 if (jd_flag) 811 dt.d = j - jan1 + 1; 812 else 813 sdateb(j, &dt); 814 memcpy(mlines->lines[i] + k + l, 815 ds + dt.d * dw, dw); 816 } else 817 memcpy(mlines->lines[i] + k + l, " ", dw); 818 } 819 if (k == 0) 820 mlines->lines[i][1] = '\0'; 821 else 822 mlines->lines[i][k + l] = '\0'; 823 } 824} 825 826/* Put the local names of weekdays into the wds */ 827void 828mkweekdays(struct weekdays *wds) 829{ 830 int i, len; 831 struct tm tm; 832 wchar_t buf[20]; 833 834 memset(&tm, 0, sizeof(tm)); 835 836 for (i = 0; i != 7; i++) { 837 tm.tm_wday = (i+1) % 7; 838 wcsftime(buf, sizeof(buf), L"%a", &tm); 839 len = wcslen(buf); 840 if (len > 2) 841 len = 2; 842 wcscpy(wds->names[i], L" "); 843 wcsncpy(wds->names[i] + 2 - len, buf, len); 844 } 845} 846 847/* 848 * Compute the day number of the first 849 * existing date after the first day in month. 850 * (the first day in month and even the month might not exist!) 851 */ 852int 853firstday(int y, int m) 854{ 855 date dt; 856 int nd; 857 858 dt.y = y; 859 dt.m = m; 860 dt.d = 1; 861 nd = sndays(&dt); 862 for (;;) { 863 sdate(nd, &dt); 864 if ((dt.m >= m && dt.y == y) || dt.y > y) 865 return (nd); 866 else 867 nd++; 868 } 869 /* NEVER REACHED */ 870} 871 872/* 873 * Compute the number of days from date, obey the local switch from 874 * Julian to Gregorian if specified by the user. 875 */ 876int 877sndays(struct date *d) 878{ 879 880 if (nswitch != 0) 881 if (nswitch < ndaysj(d)) 882 return (ndaysg(d)); 883 else 884 return (ndaysj(d)); 885 else 886 return ndaysg(d); 887} 888 889/* 890 * Compute the number of days from date, obey the switch from 891 * Julian to Gregorian as used by UK and her colonies. 892 */ 893int 894sndaysb(struct date *d) 895{ 896 897 if (nswitchb < ndaysj(d)) 898 return (ndaysg(d)); 899 else 900 return (ndaysj(d)); 901} 902 903/* Inverse of sndays */ 904struct date * 905sdate(int nd, struct date *d) 906{ 907 908 if (nswitch < nd) 909 return (gdate(nd, d)); 910 else 911 return (jdate(nd, d)); 912} 913 914/* Inverse of sndaysb */ 915struct date * 916sdateb(int nd, struct date *d) 917{ 918 919 if (nswitchb < nd) 920 return (gdate(nd, d)); 921 else 922 return (jdate(nd, d)); 923} 924 925/* Center string t in string s of length w by putting enough leading blanks */ 926char * 927center(char *s, char *t, int w) 928{ 929 char blanks[80]; 930 931 memset(blanks, ' ', sizeof(blanks)); 932 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 933 return (s); 934} 935 936/* Center string t in string s of length w by putting enough leading blanks */ 937wchar_t * 938wcenter(wchar_t *s, wchar_t *t, int w) 939{ 940 char blanks[80]; 941 942 memset(blanks, ' ', sizeof(blanks)); 943 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 944 return (s); 945} 946 947int 948parsemonth(const char *s, int *m, int *y) 949{ 950 int nm, ny; 951 char *cp; 952 struct tm tm; 953 954 nm = (int)strtol(s, &cp, 10); 955 if (cp != s) { 956 ny = *y; 957 if (*cp == '\0') { 958 ; /* no special action */ 959 } else if (*cp == 'f' || *cp == 'F') { 960 if (nm <= *m) 961 ny++; 962 } else if (*cp == 'p' || *cp == 'P') { 963 if (nm >= *m) 964 ny--; 965 } else 966 return (1); 967 if (nm < 1 || nm > 12) 968 return 1; 969 *m = nm; 970 *y = ny; 971 return (0); 972 } 973 if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 974 *m = tm.tm_mon + 1; 975 return (0); 976 } 977 return (1); 978} 979