ncal.c revision 191330
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 191330 2009-04-20 18:19:38Z rdivacky $"; 3032310Scharnier#endif /* not lint */ 3132310Scharnier 3231744Shelbig#include <calendar.h> 3374612Sache#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 4831744Shelbig/* Width of one month with backward compatibility */ 4931744Shelbig#define MONTH_WIDTH_B_J 27 5031744Shelbig#define MONTH_WIDTH_B 20 5131744Shelbig 5231744Shelbig#define MONTH_WIDTH_J 24 5331744Shelbig#define MONTH_WIDTH 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]; 6331744Shelbig}; 6431744Shelbig 6531744Shelbigstruct weekdays { 66189804Sdas wchar_t names[7][4]; 6731744Shelbig}; 6831744Shelbig 6931744Shelbig/* The switches from Julian to Gregorian in some countries */ 7031744Shelbigstatic struct djswitch { 7195641Smarkm const char *cc; /* Country code according to ISO 3166 */ 7295641Smarkm const char *nm; /* Name of country */ 7331744Shelbig date dt; /* Last day of Julian calendar */ 7431744Shelbig} switches[] = { 7553963Sache {"AL", "Albania", {1912, 11, 30}}, 7653963Sache {"AT", "Austria", {1583, 10, 5}}, 7753963Sache {"AU", "Australia", {1752, 9, 2}}, 7853963Sache {"BE", "Belgium", {1582, 12, 14}}, 7953963Sache {"BG", "Bulgaria", {1916, 3, 18}}, 8053963Sache {"CA", "Canada", {1752, 9, 2}}, 8153963Sache {"CH", "Switzerland", {1655, 2, 28}}, 8253963Sache {"CN", "China", {1911, 12, 18}}, 8353963Sache {"CZ", "Czech Republic",{1584, 1, 6}}, 8453963Sache {"DE", "Germany", {1700, 2, 18}}, 8553963Sache {"DK", "Denmark", {1700, 2, 18}}, 8653963Sache {"ES", "Spain", {1582, 10, 4}}, 8753963Sache {"FI", "Finland", {1753, 2, 17}}, 8853963Sache {"FR", "France", {1582, 12, 9}}, 8953963Sache {"GB", "United Kingdom",{1752, 9, 2}}, 9053963Sache {"GR", "Greece", {1924, 3, 9}}, 9153963Sache {"HU", "Hungary", {1587, 10, 21}}, 9253963Sache {"IS", "Iceland", {1700, 11, 16}}, 9353963Sache {"IT", "Italy", {1582, 10, 4}}, 9453963Sache {"JP", "Japan", {1918, 12, 18}}, 9553963Sache {"LI", "Lithuania", {1918, 2, 1}}, 9653963Sache {"LN", "Latin", {9999, 05, 31}}, 9753963Sache {"LU", "Luxembourg", {1582, 12, 14}}, 9853963Sache {"LV", "Latvia", {1918, 2, 1}}, 9953963Sache {"NL", "Netherlands", {1582, 12, 14}}, 10053963Sache {"NO", "Norway", {1700, 2, 18}}, 10153963Sache {"PL", "Poland", {1582, 10, 4}}, 10253963Sache {"PT", "Portugal", {1582, 10, 4}}, 10353963Sache {"RO", "Romania", {1919, 3, 31}}, 10453963Sache {"RU", "Russia", {1918, 1, 31}}, 10553963Sache {"SI", "Slovenia", {1919, 3, 4}}, 10653963Sache {"SW", "Sweden", {1753, 2, 17}}, 10753963Sache {"TR", "Turkey", {1926, 12, 18}}, 10853963Sache {"US", "United States", {1752, 9, 2}}, 10953963Sache {"YU", "Yugoslavia", {1919, 3, 4}} 11031744Shelbig}; 11131744Shelbig 11232215Shelbigstruct djswitch *dftswitch = 11332215Shelbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 11432215Shelbig /* default switch (should be "US") */ 11532215Shelbig 11631744Shelbig/* Table used to print day of month and week numbers */ 11731744Shelbigchar daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 11831744Shelbig " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 11931744Shelbig " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 12031744Shelbig " 48 49 50 51 52 53"; 12131744Shelbig 12231744Shelbig/* Table used to print day of year and week numbers */ 12331744Shelbigchar jdaystr[] = " 1 2 3 4 5 6 7 8 9" 12431744Shelbig " 10 11 12 13 14 15 16 17 18 19" 12531744Shelbig " 20 21 22 23 24 25 26 27 28 29" 12631744Shelbig " 30 31 32 33 34 35 36 37 38 39" 12731744Shelbig " 40 41 42 43 44 45 46 47 48 49" 12831744Shelbig " 50 51 52 53 54 55 56 57 58 59" 12931744Shelbig " 60 61 62 63 64 65 66 67 68 69" 13031744Shelbig " 70 71 72 73 74 75 76 77 78 79" 13131744Shelbig " 80 81 82 83 84 85 86 87 88 89" 13231744Shelbig " 90 91 92 93 94 95 96 97 98 99" 13331744Shelbig " 100 101 102 103 104 105 106 107 108 109" 13431744Shelbig " 110 111 112 113 114 115 116 117 118 119" 13531744Shelbig " 120 121 122 123 124 125 126 127 128 129" 13631744Shelbig " 130 131 132 133 134 135 136 137 138 139" 13731744Shelbig " 140 141 142 143 144 145 146 147 148 149" 13831744Shelbig " 150 151 152 153 154 155 156 157 158 159" 13931744Shelbig " 160 161 162 163 164 165 166 167 168 169" 14031744Shelbig " 170 171 172 173 174 175 176 177 178 179" 14131744Shelbig " 180 181 182 183 184 185 186 187 188 189" 14231744Shelbig " 190 191 192 193 194 195 196 197 198 199" 14331744Shelbig " 200 201 202 203 204 205 206 207 208 209" 14431744Shelbig " 210 211 212 213 214 215 216 217 218 219" 14531744Shelbig " 220 221 222 223 224 225 226 227 228 229" 14631744Shelbig " 230 231 232 233 234 235 236 237 238 239" 14731744Shelbig " 240 241 242 243 244 245 246 247 248 249" 14831744Shelbig " 250 251 252 253 254 255 256 257 258 259" 14931744Shelbig " 260 261 262 263 264 265 266 267 268 269" 15031744Shelbig " 270 271 272 273 274 275 276 277 278 279" 15131744Shelbig " 280 281 282 283 284 285 286 287 288 289" 15231744Shelbig " 290 291 292 293 294 295 296 297 298 299" 15331744Shelbig " 300 301 302 303 304 305 306 307 308 309" 15431744Shelbig " 310 311 312 313 314 315 316 317 318 319" 15531744Shelbig " 320 321 322 323 324 325 326 327 328 329" 15631744Shelbig " 330 331 332 333 334 335 336 337 338 339" 15731744Shelbig " 340 341 342 343 344 345 346 347 348 349" 15831744Shelbig " 350 351 352 353 354 355 356 357 358 359" 15931744Shelbig " 360 361 362 363 364 365 366"; 16031744Shelbig 16132139Shelbigint flag_weeks; /* user wants number of week */ 16231744Shelbigint nswitch; /* user defined switch date */ 16331744Shelbigint nswitchb; /* switch date for backward compatibility */ 164191330Srdivackyconst char *term_r, *term_e; 165191330Srdivackyint today; 16631744Shelbig 16731744Shelbigchar *center(char *s, char *t, int w); 168189804Sdaswchar_t *wcenter(wchar_t *s, wchar_t *t, int w); 16931744Shelbigvoid mkmonth(int year, int month, int jd_flag, struct monthlines * monthl); 17031744Shelbigvoid mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 17131744Shelbigvoid mkweekdays(struct weekdays * wds); 172186401Swollmanint parsemonth(const char *s, int *m, int *y); 17331744Shelbigvoid printcc(void); 17431744Shelbigvoid printeaster(int year, int julian, int orthodox); 17531744Shelbigvoid printmonth(int year, int month, int jd_flag); 17631744Shelbigvoid printmonthb(int year, int month, int jd_flag); 17731744Shelbigvoid printyear(int year, int jd_flag); 17831744Shelbigvoid printyearb(int year, int jd_flag); 17932282Shelbigint firstday(int y, int m); 18031744Shelbigdate *sdate(int ndays, struct date * d); 18131744Shelbigdate *sdateb(int ndays, struct date * d); 18231744Shelbigint sndays(struct date * d); 18331744Shelbigint sndaysb(struct date * d); 18432310Scharnierstatic void usage(void); 18531744Shelbigint weekdayb(int nd); 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 */ 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. */ 19731744Shelbig int flag_hole_year = 0; /* user wants the whole year */ 19831744Shelbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 19931744Shelbig int flag_julian_day = 0; /* user wants the Julian day 20031744Shelbig * numbers */ 20131744Shelbig int flag_orthodox = 0; /* use wants Orthodox easter */ 20231744Shelbig int flag_easter = 0; /* use wants easter date */ 20331744Shelbig char *cp; /* character pointer */ 204138036Swollman char *flag_month = NULL; /* requested month as string */ 20595641Smarkm const char *locale; /* locale to get country code */ 206191330Srdivacky char tbuf[1024], cbuf[512], *b; 207191330Srdivacky time_t t; 208191330Srdivacky struct tm *tm1; 20931744Shelbig 210191330Srdivacky term_e = term_r = NULL; 211191330Srdivacky today = 0; 212191330Srdivacky if (isatty(1) && tgetent(tbuf, getenv("TERM")) == 1) { 213191330Srdivacky date dt; /* handy date */ 214191330Srdivacky 215191330Srdivacky b = cbuf; 216191330Srdivacky term_r = tgetstr("mr", &b); 217191330Srdivacky term_e = tgetstr("me", &b); 218191330Srdivacky t = time(NULL); 219191330Srdivacky tm1 = localtime(&t); 220191330Srdivacky dt.y = tm1->tm_year + 1900; 221191330Srdivacky dt.m = tm1->tm_mon + 1; 222191330Srdivacky dt.d = tm1->tm_mday; 223191330Srdivacky 224191330Srdivacky today = sndaysb(&dt); 225191330Srdivacky } 226191330Srdivacky 22732215Shelbig /* 22832215Shelbig * Use locale to determine the country code, 22932215Shelbig * and use the country code to determine the default 23032215Shelbig * switchdate and date format from the switches table. 23132215Shelbig */ 23274612Sache if (setlocale(LC_ALL, "") == NULL) 23332215Shelbig warn("setlocale"); 23474612Sache locale = setlocale(LC_TIME, NULL); 23578016Sache if (locale == NULL || 23678016Sache strcmp(locale, "C") == 0 || 23778027Sache strcmp(locale, "POSIX") == 0 || 23878027Sache strcmp(locale, "ASCII") == 0 || 23978027Sache strcmp(locale, "US-ASCII") == 0) 24032215Shelbig locale = "_US"; 24132215Shelbig q = switches + sizeof(switches) / sizeof(struct djswitch); 24232215Shelbig for (p = switches; p != q; p++) 24332215Shelbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 24432215Shelbig break; 24532215Shelbig if (p == q) { 24632215Shelbig nswitch = ndaysj(&dftswitch->dt); 24732215Shelbig } else { 24832215Shelbig nswitch = ndaysj(&p->dt); 24932215Shelbig dftswitch = p; 25032215Shelbig } 25131744Shelbig 25232215Shelbig 25331744Shelbig /* 25431744Shelbig * Get the filename portion of argv[0] and set flag_backward if 25531744Shelbig * this program is called "cal". 25631744Shelbig */ 257138036Swollman cp = strrchr(argv[0], '/'); 258138036Swollman cp = (cp == NULL) ? argv[0] : cp + 1; 259138036Swollman if (strcmp("cal", cp) == 0) 26031744Shelbig flag_backward = 1; 26131744Shelbig 26231744Shelbig /* Set the switch date to United Kingdom if backwards compatible */ 26331744Shelbig if (flag_backward) 26431744Shelbig nswitchb = ndaysj(&ukswitch); 26531744Shelbig 266138036Swollman while ((ch = getopt(argc, argv, "Jejm:ops:wy")) != -1) 26731744Shelbig switch (ch) { 26831744Shelbig case 'J': 26931744Shelbig if (flag_backward) 27031744Shelbig usage(); 27131744Shelbig nswitch = ndaysj(&never); 27231744Shelbig flag_julian_cal = 1; 27331744Shelbig break; 27431744Shelbig case 'e': 27531744Shelbig if (flag_backward) 27631744Shelbig usage(); 27731744Shelbig flag_easter = 1; 27831744Shelbig break; 27931744Shelbig case 'j': 28031744Shelbig flag_julian_day = 1; 28131744Shelbig break; 282138036Swollman case 'm': 283138036Swollman flag_month = optarg; 284138036Swollman break; 28531744Shelbig case 'o': 28631744Shelbig if (flag_backward) 28731744Shelbig usage(); 28831744Shelbig flag_orthodox = 1; 28931744Shelbig flag_easter = 1; 29031744Shelbig break; 29131744Shelbig case 'p': 29231744Shelbig if (flag_backward) 29331744Shelbig usage(); 29431744Shelbig printcc(); 29531744Shelbig return (0); 29631744Shelbig break; 29731744Shelbig case 's': 29831744Shelbig if (flag_backward) 29931744Shelbig usage(); 30031744Shelbig q = switches + 30131744Shelbig sizeof(switches) / sizeof(struct djswitch); 30231744Shelbig for (p = switches; 30331744Shelbig p != q && strcmp(p->cc, optarg) != 0; p++) 30431744Shelbig ; 30531744Shelbig if (p == q) 30631744Shelbig errx(EX_USAGE, 30731744Shelbig "%s: invalid country code", optarg); 30831744Shelbig nswitch = ndaysj(&(p->dt)); 30931744Shelbig break; 31031744Shelbig case 'w': 31131744Shelbig if (flag_backward) 31231744Shelbig usage(); 31331744Shelbig flag_weeks = 1; 31431744Shelbig break; 31531744Shelbig case 'y': 31631744Shelbig flag_hole_year = 1; 31731744Shelbig break; 31831744Shelbig default: 31931744Shelbig usage(); 32031744Shelbig } 32131744Shelbig 32231744Shelbig argc -= optind; 32331744Shelbig argv += optind; 32431744Shelbig 32531744Shelbig switch (argc) { 32631744Shelbig case 2: 32731744Shelbig if (flag_easter) 32831744Shelbig usage(); 329138036Swollman flag_month = *argv++; 33031744Shelbig /* FALLTHROUGH */ 33131744Shelbig case 1: 33231744Shelbig y = atoi(*argv++); 33331744Shelbig if (y < 1 || y > 9999) 33431744Shelbig errx(EX_USAGE, "year %d not in range 1..9999", y); 33531744Shelbig break; 33631744Shelbig case 0: 337138036Swollman { 338138036Swollman time_t t; 339138036Swollman struct tm *tm; 340138036Swollman 341138036Swollman t = time(NULL); 342138036Swollman tm = localtime(&t); 343138036Swollman y = tm->tm_year + 1900; 344138036Swollman m = tm->tm_mon + 1; 345138036Swollman } 34631744Shelbig break; 34731744Shelbig default: 34831744Shelbig usage(); 34931744Shelbig } 35031744Shelbig 351138036Swollman if (flag_month != NULL) { 352186401Swollman if (parsemonth(flag_month, &m, &y)) { 353138036Swollman errx(EX_USAGE, 354138036Swollman "%s is neither a month number (1..12) nor a name", 355138036Swollman flag_month); 356186401Swollman } 357138036Swollman } 358138036Swollman 35931744Shelbig if (flag_easter) 36031744Shelbig printeaster(y, flag_julian_cal, flag_orthodox); 361191330Srdivacky else if (argc == 1 || flag_hole_year) { 362191330Srdivacky /* disable the highlight for now */ 363191330Srdivacky today = 0; 36431744Shelbig if (flag_backward) 36531744Shelbig printyearb(y, flag_julian_day); 36631744Shelbig else 36731744Shelbig printyear(y, flag_julian_day); 368191330Srdivacky } else 36931744Shelbig if (flag_backward) 37031744Shelbig printmonthb(y, m, flag_julian_day); 37131744Shelbig else 37231744Shelbig printmonth(y, m, flag_julian_day); 37331744Shelbig 37431744Shelbig return (0); 37531744Shelbig} 37631744Shelbig 37732310Scharnierstatic void 37831744Shelbigusage(void) 37931744Shelbig{ 38031744Shelbig 381138036Swollman fputs( 382138036Swollman "usage: cal [-jy] [[month] year]\n" 383138036Swollman " cal [-j] [-m month] [year]\n" 384138036Swollman " ncal [-Jjpwy] [-s country_code] [[month] year]\n" 385138036Swollman " ncal [-Jeo] [year]\n", stderr); 38631744Shelbig exit(EX_USAGE); 38731744Shelbig} 38831744Shelbig 38931744Shelbig/* print the assumed switches for all countries */ 39031744Shelbigvoid 39131744Shelbigprintcc(void) 39231744Shelbig{ 39331744Shelbig struct djswitch *p; 39431744Shelbig int n; /* number of lines to print */ 39531744Shelbig int m; /* offset from left to right table entry on the same line */ 39631744Shelbig 39732215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d" 39832215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 39932215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 40031744Shelbig 40131744Shelbig n = sizeof(switches) / sizeof(struct djswitch); 40231744Shelbig m = (n + 1) / 2; 40331744Shelbig n /= 2; 40432215Shelbig for (p = switches; p != switches + n; p++) 40532215Shelbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 40631744Shelbig if (m != n) 40732215Shelbig printf(FSTR"\n", FSTRARG(p)); 40831744Shelbig} 40931744Shelbig 41031744Shelbig/* print the date of easter sunday */ 41131744Shelbigvoid 41231744Shelbigprinteaster(int y, int julian, int orthodox) 41331744Shelbig{ 41432215Shelbig date dt; 41532215Shelbig struct tm tm; 41632215Shelbig char buf[80]; 41774573Sache static int d_first = -1; 41831744Shelbig 41974573Sache if (d_first < 0) 42074573Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 42131744Shelbig /* force orthodox easter for years before 1583 */ 42231744Shelbig if (y < 1583) 42331744Shelbig orthodox = 1; 42431744Shelbig 42531744Shelbig if (orthodox) 42631744Shelbig if (julian) 42731744Shelbig easteroj(y, &dt); 42831744Shelbig else 42931744Shelbig easterog(y, &dt); 43031744Shelbig else 43131744Shelbig easterg(y, &dt); 43232215Shelbig 43332215Shelbig memset(&tm, 0, sizeof(tm)); 43432215Shelbig tm.tm_year = dt.y - 1900; 43532215Shelbig tm.tm_mon = dt.m - 1; 43632215Shelbig tm.tm_mday = dt.d; 43774573Sache strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 43832215Shelbig printf("%s\n", buf); 43931744Shelbig} 44031744Shelbig 44131744Shelbigvoid 44231744Shelbigprintmonth(int y, int m, int jd_flag) 44331744Shelbig{ 44431744Shelbig struct monthlines month; 44531744Shelbig struct weekdays wds; 44631744Shelbig int i; 44731744Shelbig 44831744Shelbig mkmonth(y, m - 1, jd_flag, &month); 44931744Shelbig mkweekdays(&wds); 450189804Sdas printf(" %ls %d\n", month.name, y); 45131744Shelbig for (i = 0; i != 7; i++) 452189804Sdas printf("%.2ls%s\n", wds.names[i], month.lines[i]); 45331744Shelbig if (flag_weeks) 45431744Shelbig printf(" %s\n", month.weeks); 45531744Shelbig} 45631744Shelbig 45731744Shelbigvoid 45831744Shelbigprintmonthb(int y, int m, int jd_flag) 45931744Shelbig{ 46031744Shelbig struct monthlines month; 46131744Shelbig struct weekdays wds; 462189804Sdas wchar_t s[MAX_WIDTH], t[MAX_WIDTH]; 46331744Shelbig int i; 46431744Shelbig int mw; 46531744Shelbig 46631744Shelbig mkmonthb(y, m - 1, jd_flag, &month); 46731744Shelbig mkweekdays(&wds); 46831744Shelbig 46931744Shelbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 47031744Shelbig 471189804Sdas swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y); 472189804Sdas wprintf(L"%ls\n", wcenter(t, s, mw)); 47331744Shelbig 47431744Shelbig if (jd_flag) 475189804Sdas wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n", 476189804Sdas wds.names[6], wds.names[0], 47731744Shelbig wds.names[1], wds.names[2], wds.names[3], 47831744Shelbig wds.names[4], wds.names[5]); 47931744Shelbig else 480189804Sdas wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6], 481189804Sdas wds.names[0], wds.names[1], wds.names[2], wds.names[3], 48231744Shelbig wds.names[4], wds.names[5]); 48331744Shelbig 48431744Shelbig for (i = 0; i != 6; i++) 48531744Shelbig printf("%s\n", month.lines[i]+1); 48631744Shelbig} 48731744Shelbig 48831744Shelbigvoid 48931744Shelbigprintyear(int y, int jd_flag) 49031744Shelbig{ 49131744Shelbig struct monthlines year[12]; 49231744Shelbig struct weekdays wds; 49331744Shelbig char s[80], t[80]; 49431744Shelbig int i, j; 49531744Shelbig int mpl; 49631744Shelbig int mw; 49731744Shelbig 49831744Shelbig for (i = 0; i != 12; i++) 49931744Shelbig mkmonth(y, i, jd_flag, year + i); 50031744Shelbig mkweekdays(&wds); 50131744Shelbig mpl = jd_flag ? 3 : 4; 50231744Shelbig mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH; 50331744Shelbig 50431744Shelbig sprintf(s, "%d", y); 50531744Shelbig printf("%s\n", center(t, s, mpl * mw)); 50631744Shelbig 50731744Shelbig for (j = 0; j != 12; j += mpl) { 508189804Sdas printf(" %-*ls%-*ls", 50931744Shelbig mw, year[j].name, 51031744Shelbig mw, year[j + 1].name); 51131744Shelbig if (mpl == 3) 512189804Sdas printf("%ls\n", year[j + 2].name); 51331744Shelbig else 514189804Sdas printf("%-*ls%ls\n", 51531744Shelbig mw, year[j + 2].name, 51631744Shelbig year[j + 3].name); 51731744Shelbig for (i = 0; i != 7; i++) { 518189804Sdas printf("%.2ls%-*s%-*s", 51931744Shelbig wds.names[i], 52031744Shelbig mw, year[j].lines[i], 52131744Shelbig mw, year[j + 1].lines[i]); 52231744Shelbig if (mpl == 3) 52331744Shelbig printf("%s\n", year[j + 2].lines[i]); 52431744Shelbig else 52531744Shelbig printf("%-*s%s\n", 52631744Shelbig mw, year[j + 2].lines[i], 52731744Shelbig year[j + 3].lines[i]); 52831744Shelbig } 52948566Sbillf if (flag_weeks) { 53031744Shelbig if (mpl == 3) 53131744Shelbig printf(" %-*s%-*s%-s\n", 53231744Shelbig mw, year[j].weeks, 53331744Shelbig mw, year[j + 1].weeks, 53431744Shelbig year[j + 2].weeks); 53531744Shelbig else 53631744Shelbig printf(" %-*s%-*s%-*s%-s\n", 53731744Shelbig mw, year[j].weeks, 53831744Shelbig mw, year[j + 1].weeks, 53931744Shelbig mw, year[j + 2].weeks, 54031744Shelbig year[j + 3].weeks); 54148566Sbillf } 54231744Shelbig } 54331744Shelbig} 54431744Shelbig 54531744Shelbigvoid 54631744Shelbigprintyearb(int y, int jd_flag) 54731744Shelbig{ 54831744Shelbig struct monthlines year[12]; 54931744Shelbig struct weekdays wds; 55031744Shelbig char s[80], t[80]; 551189804Sdas wchar_t ws[80], wt[80]; 55231744Shelbig int i, j; 55331744Shelbig int mpl; 55431744Shelbig int mw; 55531744Shelbig 55631744Shelbig for (i = 0; i != 12; i++) 55731744Shelbig mkmonthb(y, i, jd_flag, year + i); 55831744Shelbig mkweekdays(&wds); 55931744Shelbig mpl = jd_flag ? 2 : 3; 56031744Shelbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 56131744Shelbig 56231744Shelbig sprintf(s, "%d", y); 56331744Shelbig printf("%s\n\n", center(t, s, mw * mpl + mpl)); 56431744Shelbig 56531744Shelbig for (j = 0; j != 12; j += mpl) { 566189804Sdas printf("%-*ls ", mw, wcenter(ws, year[j].name, mw)); 56731744Shelbig if (mpl == 2) 568189804Sdas printf("%ls\n", wcenter(ws, year[j + 1].name, mw)); 56931744Shelbig else 570189804Sdas printf("%-*ls %ls\n", mw, 571189804Sdas wcenter(ws, year[j + 1].name, mw), 572189804Sdas wcenter(wt, year[j + 2].name, mw)); 57331744Shelbig 57431744Shelbig if (mpl == 2) 575189804Sdas wprintf(L" %ls %ls %ls %ls %ls %ls %ls " 576189804Sdas " %ls %ls %ls %ls %ls %ls %.2ls\n", 57731744Shelbig wds.names[6], wds.names[0], wds.names[1], 57831744Shelbig wds.names[2], wds.names[3], wds.names[4], 57931744Shelbig wds.names[5], 58031744Shelbig wds.names[6], wds.names[0], wds.names[1], 58131744Shelbig wds.names[2], wds.names[3], wds.names[4], 58231744Shelbig wds.names[5]); 58331744Shelbig else 584189804Sdas wprintf(L"%ls%ls%ls%ls%ls%ls%ls " 585189804Sdas "%ls%ls%ls%ls%ls%ls%ls " 586189804Sdas "%ls%ls%ls%ls%ls%ls%.2ls\n", 58731744Shelbig wds.names[6], wds.names[0], wds.names[1], 58831744Shelbig wds.names[2], wds.names[3], wds.names[4], 58931744Shelbig wds.names[5], 59031744Shelbig wds.names[6], wds.names[0], wds.names[1], 59131744Shelbig wds.names[2], wds.names[3], wds.names[4], 59231744Shelbig wds.names[5], 59331744Shelbig wds.names[6], wds.names[0], wds.names[1], 59431744Shelbig wds.names[2], wds.names[3], wds.names[4], 59531744Shelbig wds.names[5]); 59631744Shelbig for (i = 0; i != 6; i++) { 59731744Shelbig if (mpl == 2) 59831744Shelbig printf("%-*s %s\n", 59931744Shelbig mw, year[j].lines[i]+1, 60031744Shelbig year[j + 1].lines[i]+1); 60131744Shelbig else 60231744Shelbig printf("%-*s %-*s %s\n", 60331744Shelbig mw, year[j].lines[i]+1, 60431744Shelbig mw, year[j + 1].lines[i]+1, 60531744Shelbig year[j + 2].lines[i]+1); 60631744Shelbig 60731744Shelbig } 60831744Shelbig } 60931744Shelbig} 61031744Shelbig 61131744Shelbigvoid 61231744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines) 61331744Shelbig{ 61431744Shelbig 61531744Shelbig struct tm tm; /* for strftime printing local names of 61631744Shelbig * months */ 61731744Shelbig date dt; /* handy date */ 61831744Shelbig int dw; /* width of numbers */ 61931744Shelbig int first; /* first day of month */ 62031744Shelbig int firstm; /* first day of first week of month */ 621191330Srdivacky int i, j, k, l; /* just indices */ 62231744Shelbig int last; /* the first day of next month */ 62331744Shelbig int jan1 = 0; /* the first day of this year */ 62431744Shelbig char *ds; /* pointer to day strings (daystr or 62531744Shelbig * jdaystr) */ 62631744Shelbig 62731744Shelbig /* Set name of month. */ 62831744Shelbig memset(&tm, 0, sizeof(tm)); 62931744Shelbig tm.tm_mon = m; 630189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 631189804Sdas L"%OB", &tm); 632189804Sdas mlines->name[0] = towupper(mlines->name[0]); 63331744Shelbig 63431744Shelbig /* 63531744Shelbig * Set first and last to the day number of the first day of this 63631744Shelbig * month and the first day of next month respectively. Set jan1 to 63732282Shelbig * the day number of the first day of this year. 63831744Shelbig */ 63932282Shelbig first = firstday(y, m + 1); 64032282Shelbig if (m == 11) 64132282Shelbig last = firstday(y + 1, 1); 64232282Shelbig else 64332282Shelbig last = firstday(y, m + 2); 64431744Shelbig 64532282Shelbig if (jd_flag) 64632525Shelbig jan1 = firstday(y, 1); 64731744Shelbig 64831744Shelbig /* 64931744Shelbig * Set firstm to the day number of monday of the first week of 65031744Shelbig * this month. (This might be in the last month) 65131744Shelbig */ 65231744Shelbig firstm = first - weekday(first); 65331744Shelbig 65431744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 65531744Shelbig if (jd_flag) { 65631744Shelbig ds = jdaystr; 65731744Shelbig dw = 4; 65831744Shelbig } else { 65931744Shelbig ds = daystr; 66031744Shelbig dw = 3; 66131744Shelbig } 66231744Shelbig 66331744Shelbig /* 66431744Shelbig * Fill the lines with day of month or day of year (julian day) 66531744Shelbig * line index: i, each line is one weekday. column index: j, each 66631744Shelbig * column is one day number. print column index: k. 66731744Shelbig */ 66831744Shelbig for (i = 0; i != 7; i++) { 669191330Srdivacky l = 0; 670191330Srdivacky for (j = firstm + i, k = 0; j < last; j += 7, k += dw) { 671191330Srdivacky if (j == today && (term_r != NULL && term_e != NULL)) { 672191330Srdivacky l = strlen(term_r); 673191330Srdivacky if (jd_flag) 674191330Srdivacky dt.d = j - jan1 + 1; 675191330Srdivacky else 676191330Srdivacky sdateb(j, &dt); 677191330Srdivacky /* separator */ 678191330Srdivacky mlines->lines[i][k] = ' '; 679191330Srdivacky /* the actual text */ 680191330Srdivacky memcpy(mlines->lines[i] + k + l, 681191330Srdivacky ds + dt.d * dw, dw); 682191330Srdivacky /* highlight on */ 683191330Srdivacky memcpy(mlines->lines[i] + k + 1, term_r, l); 684191330Srdivacky /* highlight off */ 685191330Srdivacky memcpy(mlines->lines[i] + k + l + dw, term_e, 686191330Srdivacky strlen(term_e)); 687191330Srdivacky l = strlen(term_e) + strlen(term_r); 688191330Srdivacky continue; 689191330Srdivacky } 69031744Shelbig if (j >= first) { 69131744Shelbig if (jd_flag) 69231744Shelbig dt.d = j - jan1 + 1; 69331744Shelbig else 69431744Shelbig sdate(j, &dt); 695191330Srdivacky memcpy(mlines->lines[i] + k + l, 69631744Shelbig ds + dt.d * dw, dw); 69731744Shelbig } else 698191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 699191330Srdivacky } 700191330Srdivacky mlines->lines[i][k + l] = '\0'; 70131744Shelbig 70231744Shelbig } 70331744Shelbig 70431744Shelbig /* fill the weeknumbers */ 70531744Shelbig if (flag_weeks) { 70631744Shelbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 70731744Shelbig if (j <= nswitch) 70831744Shelbig memset(mlines->weeks + k, ' ', dw); 70931744Shelbig else 71031744Shelbig memcpy(mlines->weeks + k, 71131744Shelbig ds + week(j, &i)*dw, dw); 71231744Shelbig mlines->weeks[k] = '\0'; 71331744Shelbig } 71431744Shelbig} 71531744Shelbig 71631744Shelbigvoid 71731744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 71831744Shelbig{ 71931744Shelbig 72031744Shelbig struct tm tm; /* for strftime printing local names of 72131744Shelbig * months */ 72231744Shelbig date dt; /* handy date */ 72331744Shelbig int dw; /* width of numbers */ 72431744Shelbig int first; /* first day of month */ 72531744Shelbig int firsts; /* sunday of first week of month */ 726191330Srdivacky int i, j, k, l; /* just indices */ 72731744Shelbig int jan1 = 0; /* the first day of this year */ 72831744Shelbig int last; /* the first day of next month */ 72931744Shelbig char *ds; /* pointer to day strings (daystr or 73031744Shelbig * jdaystr) */ 73131744Shelbig 73231744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 73331744Shelbig if (jd_flag) { 73431744Shelbig ds = jdaystr; 73531744Shelbig dw = 4; 73631744Shelbig } else { 73731744Shelbig ds = daystr; 73831744Shelbig dw = 3; 73931744Shelbig } 74031744Shelbig 74131744Shelbig /* Set name of month centered */ 74231744Shelbig memset(&tm, 0, sizeof(tm)); 74331744Shelbig tm.tm_mon = m; 744189804Sdas wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]), 745189804Sdas L"%OB", &tm); 746189804Sdas mlines->name[0] = towupper(mlines->name[0]); 74731744Shelbig 74831744Shelbig /* 74931744Shelbig * Set first and last to the day number of the first day of this 75031744Shelbig * month and the first day of next month respectively. Set jan1 to 75131744Shelbig * the day number of Jan 1st of this year. 75231744Shelbig */ 75331744Shelbig dt.y = y; 75431744Shelbig dt.m = m + 1; 75531744Shelbig dt.d = 1; 75631744Shelbig first = sndaysb(&dt); 75731744Shelbig if (m == 11) { 75831744Shelbig dt.y = y + 1; 75931744Shelbig dt.m = 1; 76031744Shelbig dt.d = 1; 76131744Shelbig } else { 76231744Shelbig dt.y = y; 76331744Shelbig dt.m = m + 2; 76431744Shelbig dt.d = 1; 76531744Shelbig } 76631744Shelbig last = sndaysb(&dt); 76731744Shelbig 76831744Shelbig if (jd_flag) { 76931744Shelbig dt.y = y; 77031744Shelbig dt.m = 1; 77131744Shelbig dt.d = 1; 77231744Shelbig jan1 = sndaysb(&dt); 77331744Shelbig } 77431744Shelbig 77531744Shelbig /* 77631744Shelbig * Set firsts to the day number of sunday of the first week of 77731744Shelbig * this month. (This might be in the last month) 77831744Shelbig */ 77931744Shelbig firsts = first - (weekday(first)+1) % 7; 78031744Shelbig 78131744Shelbig /* 78231744Shelbig * Fill the lines with day of month or day of year (Julian day) 78331744Shelbig * line index: i, each line is one week. column index: j, each 78431744Shelbig * column is one day number. print column index: k. 78531744Shelbig */ 78631744Shelbig for (i = 0; i != 6; i++) { 787191330Srdivacky l = 0; 78831744Shelbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 789191330Srdivacky j++, k += dw) { 790191330Srdivacky if (j == today && (term_r != NULL && term_e != NULL)) { 791191330Srdivacky l = strlen(term_r); 792191330Srdivacky if (jd_flag) 793191330Srdivacky dt.d = j - jan1 + 1; 794191330Srdivacky else 795191330Srdivacky sdateb(j, &dt); 796191330Srdivacky /* separator */ 797191330Srdivacky mlines->lines[i][k] = ' '; 798191330Srdivacky /* the actual text */ 799191330Srdivacky memcpy(mlines->lines[i] + k + l, 800191330Srdivacky ds + dt.d * dw, dw); 801191330Srdivacky /* highlight on */ 802191330Srdivacky memcpy(mlines->lines[i] + k + 1, term_r, l); 803191330Srdivacky /* highlight off */ 804191330Srdivacky memcpy(mlines->lines[i] + k + l + dw, term_e, 805191330Srdivacky strlen(term_e)); 806191330Srdivacky l = strlen(term_e) + strlen(term_r); 807191330Srdivacky continue; 808191330Srdivacky } 80931744Shelbig if (j >= first) { 81031744Shelbig if (jd_flag) 81131744Shelbig dt.d = j - jan1 + 1; 81231744Shelbig else 81331744Shelbig sdateb(j, &dt); 814191330Srdivacky memcpy(mlines->lines[i] + k + l, 81531744Shelbig ds + dt.d * dw, dw); 81631744Shelbig } else 817191330Srdivacky memcpy(mlines->lines[i] + k + l, " ", dw); 818191330Srdivacky } 81931744Shelbig if (k == 0) 82031744Shelbig mlines->lines[i][1] = '\0'; 82131744Shelbig else 822191330Srdivacky mlines->lines[i][k + l] = '\0'; 82331744Shelbig } 82431744Shelbig} 82531744Shelbig 82631744Shelbig/* Put the local names of weekdays into the wds */ 82732139Shelbigvoid 82832139Shelbigmkweekdays(struct weekdays *wds) 82931744Shelbig{ 83074618Sache int i, len; 83131744Shelbig struct tm tm; 832189804Sdas wchar_t buf[20]; 83331744Shelbig 83431744Shelbig memset(&tm, 0, sizeof(tm)); 83531744Shelbig 83631744Shelbig for (i = 0; i != 7; i++) { 83731744Shelbig tm.tm_wday = (i+1) % 7; 838189804Sdas wcsftime(buf, sizeof(buf), L"%a", &tm); 839189804Sdas len = wcslen(buf); 84074618Sache if (len > 2) 84174618Sache len = 2; 842189804Sdas wcscpy(wds->names[i], L" "); 843189804Sdas wcsncpy(wds->names[i] + 2 - len, buf, len); 84431744Shelbig } 84531744Shelbig} 84631744Shelbig 84731744Shelbig/* 84845064Shelbig * Compute the day number of the first 84945064Shelbig * existing date after the first day in month. 85045064Shelbig * (the first day in month and even the month might not exist!) 85132282Shelbig */ 85232282Shelbigint 85332282Shelbigfirstday(int y, int m) 85432282Shelbig{ 85532282Shelbig date dt; 85632282Shelbig int nd; 85732282Shelbig 85832282Shelbig dt.y = y; 85932282Shelbig dt.m = m; 86032282Shelbig dt.d = 1; 86145064Shelbig nd = sndays(&dt); 86245064Shelbig for (;;) { 86345064Shelbig sdate(nd, &dt); 86445064Shelbig if ((dt.m >= m && dt.y == y) || dt.y > y) 86545064Shelbig return (nd); 86645064Shelbig else 86745064Shelbig nd++; 86845064Shelbig } 86945064Shelbig /* NEVER REACHED */ 87032282Shelbig} 87132282Shelbig 87232282Shelbig/* 87331744Shelbig * Compute the number of days from date, obey the local switch from 87431744Shelbig * Julian to Gregorian if specified by the user. 87531744Shelbig */ 87631744Shelbigint 87731744Shelbigsndays(struct date *d) 87831744Shelbig{ 87931744Shelbig 88031744Shelbig if (nswitch != 0) 88131744Shelbig if (nswitch < ndaysj(d)) 88231744Shelbig return (ndaysg(d)); 88331744Shelbig else 88431744Shelbig return (ndaysj(d)); 88531744Shelbig else 88631744Shelbig return ndaysg(d); 88731744Shelbig} 88831744Shelbig 88931744Shelbig/* 89031744Shelbig * Compute the number of days from date, obey the switch from 89131744Shelbig * Julian to Gregorian as used by UK and her colonies. 89231744Shelbig */ 89331744Shelbigint 89431744Shelbigsndaysb(struct date *d) 89531744Shelbig{ 89631744Shelbig 89731744Shelbig if (nswitchb < ndaysj(d)) 89831744Shelbig return (ndaysg(d)); 89931744Shelbig else 90031744Shelbig return (ndaysj(d)); 90131744Shelbig} 90231744Shelbig 90331744Shelbig/* Inverse of sndays */ 90431744Shelbigstruct date * 90531744Shelbigsdate(int nd, struct date *d) 90631744Shelbig{ 90731744Shelbig 90831744Shelbig if (nswitch < nd) 90931744Shelbig return (gdate(nd, d)); 91031744Shelbig else 91131744Shelbig return (jdate(nd, d)); 91231744Shelbig} 91331744Shelbig 91431744Shelbig/* Inverse of sndaysb */ 91531744Shelbigstruct date * 91631744Shelbigsdateb(int nd, struct date *d) 91731744Shelbig{ 91831744Shelbig 91931744Shelbig if (nswitchb < nd) 92031744Shelbig return (gdate(nd, d)); 92131744Shelbig else 92231744Shelbig return (jdate(nd, d)); 92331744Shelbig} 92431744Shelbig 92531744Shelbig/* Center string t in string s of length w by putting enough leading blanks */ 92631744Shelbigchar * 92731744Shelbigcenter(char *s, char *t, int w) 92831744Shelbig{ 92931744Shelbig char blanks[80]; 93031744Shelbig 93131744Shelbig memset(blanks, ' ', sizeof(blanks)); 93231744Shelbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 93331744Shelbig return (s); 93431744Shelbig} 935104369Sroam 936189804Sdas/* Center string t in string s of length w by putting enough leading blanks */ 937189804Sdaswchar_t * 938189804Sdaswcenter(wchar_t *s, wchar_t *t, int w) 939189804Sdas{ 940189804Sdas char blanks[80]; 941189804Sdas 942189804Sdas memset(blanks, ' ', sizeof(blanks)); 943189804Sdas swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t); 944189804Sdas return (s); 945189804Sdas} 946189804Sdas 947104369Sroamint 948186401Swollmanparsemonth(const char *s, int *m, int *y) 949104369Sroam{ 950186401Swollman int nm, ny; 951104369Sroam char *cp; 952104369Sroam struct tm tm; 953104369Sroam 954186401Swollman nm = (int)strtol(s, &cp, 10); 955186401Swollman if (cp != s) { 956186401Swollman ny = *y; 957186401Swollman if (*cp == '\0') { 958186401Swollman ; /* no special action */ 959186401Swollman } else if (*cp == 'f' || *cp == 'F') { 960186401Swollman if (nm <= *m) 961186401Swollman ny++; 962186401Swollman } else if (*cp == 'p' || *cp == 'P') { 963186401Swollman if (nm >= *m) 964186401Swollman ny--; 965186401Swollman } else 966186401Swollman return (1); 967186401Swollman if (nm < 1 || nm > 12) 968186401Swollman return 1; 969186401Swollman *m = nm; 970186401Swollman *y = ny; 971186401Swollman return (0); 972186401Swollman } 973186401Swollman if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) { 974186401Swollman *m = tm.tm_mon + 1; 975186401Swollman return (0); 976186401Swollman } 977186401Swollman return (1); 978104369Sroam} 979