ncal.c revision 191737
1235783Skib/*-
2235783Skib * Copyright (c) 1997 Wolfgang Helbig
3235783Skib * All rights reserved.
4235783Skib *
5235783Skib * Redistribution and use in source and binary forms, with or without
6235783Skib * modification, are permitted provided that the following conditions
7235783Skib * are met:
8235783Skib * 1. Redistributions of source code must retain the above copyright
9235783Skib *    notice, this list of conditions and the following disclaimer.
10235783Skib * 2. Redistributions in binary form must reproduce the above copyright
11282199Sdumbbell *    notice, this list of conditions and the following disclaimer in the
12235783Skib *    documentation and/or other materials provided with the distribution.
13235783Skib *
14235783Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15282199Sdumbbell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16235783Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17235783Skib * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18235783Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19235783Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20235783Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21235783Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22235783Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23235783Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24235783Skib * SUCH DAMAGE.
25235783Skib */
26235783Skib
27235783Skib#ifndef lint
28235783Skibstatic const char rcsid[] =
29235783Skib  "$FreeBSD: head/usr.bin/ncal/ncal.c 191737 2009-05-02 07:02:31Z rdivacky $";
30235783Skib#endif /* not lint */
31235783Skib
32235783Skib#include <calendar.h>
33235783Skib#include <ctype.h>
34235783Skib#include <err.h>
35235783Skib#include <langinfo.h>
36235783Skib#include <locale.h>
37235783Skib#include <stdio.h>
38235783Skib#include <stdlib.h>
39235783Skib#include <string.h>
40235783Skib#include <sysexits.h>
41235783Skib#include <time.h>
42235783Skib#include <unistd.h>
43235783Skib#include <wchar.h>
44235783Skib#include <wctype.h>
45235783Skib#include <term.h>
46235783Skib#undef lines			/* term.h defines this */
47235783Skib
48235783Skib/* Width of one month with backward compatibility */
49235783Skib#define MONTH_WIDTH_B_J 27
50235783Skib#define MONTH_WIDTH_B 20
51235783Skib
52235783Skib#define MONTH_WIDTH_J 24
53235783Skib#define MONTH_WIDTH 18
54235783Skib
55235783Skib#define MAX_WIDTH 64
56235783Skib
57235783Skibtypedef struct date date;
58235783Skib
59235783Skibstruct monthlines {
60235783Skib	wchar_t name[MAX_WIDTH + 1];
61235783Skib	char lines[7][MAX_WIDTH + 1];
62235783Skib	char weeks[MAX_WIDTH + 1];
63235783Skib};
64235783Skib
65235783Skibstruct weekdays {
66235783Skib	wchar_t names[7][4];
67235783Skib};
68235783Skib
69235783Skib/* The switches from Julian to Gregorian in some countries */
70235783Skibstatic struct djswitch {
71235783Skib	const char *cc;	/* Country code according to ISO 3166 */
72235783Skib	const char *nm;	/* Name of country */
73282199Sdumbbell	date dt;	/* Last day of Julian calendar */
74282199Sdumbbell} switches[] = {
75282199Sdumbbell	{"AL", "Albania",       {1912, 11, 30}},
76282199Sdumbbell	{"AT", "Austria",       {1583, 10,  5}},
77282199Sdumbbell	{"AU", "Australia",     {1752,  9,  2}},
78235783Skib	{"BE", "Belgium",       {1582, 12, 14}},
79235783Skib	{"BG", "Bulgaria",      {1916,  3, 18}},
80235783Skib	{"CA", "Canada",        {1752,  9,  2}},
81235783Skib	{"CH", "Switzerland",   {1655,  2, 28}},
82235783Skib	{"CN", "China",         {1911, 12, 18}},
83235783Skib	{"CZ", "Czech Republic",{1584,  1,  6}},
84235783Skib	{"DE", "Germany",       {1700,  2, 18}},
85282199Sdumbbell	{"DK", "Denmark",       {1700,  2, 18}},
86282199Sdumbbell	{"ES", "Spain",         {1582, 10,  4}},
87282199Sdumbbell	{"FI", "Finland",       {1753,  2, 17}},
88282199Sdumbbell	{"FR", "France",        {1582, 12,  9}},
89282199Sdumbbell	{"GB", "United Kingdom",{1752,  9,  2}},
90235783Skib	{"GR", "Greece",        {1924,  3,  9}},
91235783Skib	{"HU", "Hungary",       {1587, 10, 21}},
92235783Skib	{"IS", "Iceland",       {1700, 11, 16}},
93235783Skib	{"IT", "Italy",         {1582, 10,  4}},
94235783Skib	{"JP", "Japan",         {1918, 12, 18}},
95235783Skib	{"LI", "Lithuania",     {1918,  2,  1}},
96282199Sdumbbell	{"LN", "Latin",         {9999, 05, 31}},
97282199Sdumbbell	{"LU", "Luxembourg",    {1582, 12, 14}},
98282199Sdumbbell	{"LV", "Latvia",        {1918,  2,  1}},
99282199Sdumbbell	{"NL", "Netherlands",   {1582, 12, 14}},
100282199Sdumbbell	{"NO", "Norway",        {1700,  2, 18}},
101235783Skib	{"PL", "Poland",        {1582, 10,  4}},
102235783Skib	{"PT", "Portugal",      {1582, 10,  4}},
103235783Skib	{"RO", "Romania",       {1919,  3, 31}},
104235783Skib	{"RU", "Russia",        {1918,  1, 31}},
105235783Skib	{"SI", "Slovenia",      {1919,  3,  4}},
106235783Skib	{"SW", "Sweden",        {1753,  2, 17}},
107235783Skib	{"TR", "Turkey",        {1926, 12, 18}},
108235783Skib	{"US", "United States", {1752,  9,  2}},
109235783Skib	{"YU", "Yugoslavia",    {1919,  3,  4}}
110235783Skib};
111235783Skib
112282199Sdumbbellstruct djswitch *dftswitch =
113282199Sdumbbell    switches + sizeof(switches) / sizeof(struct djswitch) - 2;
114282199Sdumbbell    /* default switch (should be "US") */
115282199Sdumbbell
116235783Skib/* Table used to print day of month and week numbers */
117235783Skibchar daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
118282199Sdumbbell		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
119235783Skib		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
120235783Skib		" 48 49 50 51 52 53";
121282199Sdumbbell
122235783Skib/* Table used to print day of year and week numbers */
123235783Skibchar jdaystr[] = "       1   2   3   4   5   6   7   8   9"
124235783Skib		 "  10  11  12  13  14  15  16  17  18  19"
125235783Skib		 "  20  21  22  23  24  25  26  27  28  29"
126235783Skib		 "  30  31  32  33  34  35  36  37  38  39"
127235783Skib		 "  40  41  42  43  44  45  46  47  48  49"
128235783Skib		 "  50  51  52  53  54  55  56  57  58  59"
129235783Skib		 "  60  61  62  63  64  65  66  67  68  69"
130235783Skib		 "  70  71  72  73  74  75  76  77  78  79"
131235783Skib		 "  80  81  82  83  84  85  86  87  88  89"
132235783Skib		 "  90  91  92  93  94  95  96  97  98  99"
133235783Skib		 " 100 101 102 103 104 105 106 107 108 109"
134235783Skib		 " 110 111 112 113 114 115 116 117 118 119"
135235783Skib		 " 120 121 122 123 124 125 126 127 128 129"
136235783Skib		 " 130 131 132 133 134 135 136 137 138 139"
137235783Skib		 " 140 141 142 143 144 145 146 147 148 149"
138235783Skib		 " 150 151 152 153 154 155 156 157 158 159"
139235783Skib		 " 160 161 162 163 164 165 166 167 168 169"
140235783Skib		 " 170 171 172 173 174 175 176 177 178 179"
141235783Skib		 " 180 181 182 183 184 185 186 187 188 189"
142235783Skib		 " 190 191 192 193 194 195 196 197 198 199"
143282199Sdumbbell		 " 200 201 202 203 204 205 206 207 208 209"
144282199Sdumbbell		 " 210 211 212 213 214 215 216 217 218 219"
145282199Sdumbbell		 " 220 221 222 223 224 225 226 227 228 229"
146235783Skib		 " 230 231 232 233 234 235 236 237 238 239"
147235783Skib		 " 240 241 242 243 244 245 246 247 248 249"
148235783Skib		 " 250 251 252 253 254 255 256 257 258 259"
149235783Skib		 " 260 261 262 263 264 265 266 267 268 269"
150235783Skib		 " 270 271 272 273 274 275 276 277 278 279"
151235783Skib		 " 280 281 282 283 284 285 286 287 288 289"
152282199Sdumbbell		 " 290 291 292 293 294 295 296 297 298 299"
153235783Skib		 " 300 301 302 303 304 305 306 307 308 309"
154235783Skib		 " 310 311 312 313 314 315 316 317 318 319"
155235783Skib		 " 320 321 322 323 324 325 326 327 328 329"
156235783Skib		 " 330 331 332 333 334 335 336 337 338 339"
157235783Skib		 " 340 341 342 343 344 345 346 347 348 349"
158235783Skib		 " 350 351 352 353 354 355 356 357 358 359"
159235783Skib		 " 360 361 362 363 364 365 366";
160235783Skib
161235783Skibint     flag_weeks;		/* user wants number of week */
162235783Skibint     nswitch;		/* user defined switch date */
163235783Skibint	nswitchb;		/* switch date for backward compatibility */
164235783Skibconst char	*term_so, *term_se;
165235783Skibint	today;
166282199Sdumbbell
167282199Sdumbbellchar   *center(char *s, char *t, int w);
168235783Skibwchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
169235783Skibvoid	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
170235783Skibvoid    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
171235783Skibvoid    mkweekdays(struct weekdays * wds);
172282199Sdumbbellint     parsemonth(const char *s, int *m, int *y);
173282199Sdumbbellvoid    printcc(void);
174282199Sdumbbellvoid    printeaster(int year, int julian, int orthodox);
175235783Skibvoid    printmonth(int year, int month, int jd_flag);
176235783Skibvoid    printmonthb(int year, int month, int jd_flag);
177282199Sdumbbellvoid    printyear(int year, int jd_flag);
178282199Sdumbbellvoid    printyearb(int year, int jd_flag);
179282199Sdumbbellint	firstday(int y, int m);
180235783Skibdate   *sdate(int ndays, struct date * d);
181235783Skibdate   *sdateb(int ndays, struct date * d);
182235783Skibint     sndays(struct date * d);
183282199Sdumbbellint     sndaysb(struct date * d);
184235783Skibstatic void usage(void);
185235783Skibint     weekdayb(int nd);
186235783Skib
187235783Skibint
188235783Skibmain(int argc, char *argv[])
189235783Skib{
190235783Skib	struct  djswitch *p, *q;	/* to search user defined switch date */
191235783Skib	date	never = {10000, 1, 1};	/* outside valid range of dates */
192235783Skib	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
193235783Skib	int     ch;			/* holds the option character */
194235783Skib	int     m = 0;			/* month */
195235783Skib	int	y = 0;			/* year */
196282199Sdumbbell	int     flag_backward = 0;	/* user called cal--backward compat. */
197282199Sdumbbell	int     flag_hole_year = 0;	/* user wants the whole year */
198255012Sjkim	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
199255012Sjkim	int     flag_julian_day = 0;	/* user wants the Julian day
200255012Sjkim					 * numbers */
201255012Sjkim	int	flag_orthodox = 0;	/* use wants Orthodox easter */
202255012Sjkim	int	flag_easter = 0;	/* use wants easter date */
203255012Sjkim	char	*cp;			/* character pointer */
204255012Sjkim	char	*flag_month = NULL;	/* requested month as string */
205255012Sjkim	const char    *locale;		/* locale to get country code */
206255012Sjkim	char tbuf[1024], cbuf[512], *b;
207235783Skib	time_t t;
208235783Skib	struct tm *tm1;
209235783Skib
210235783Skib	term_se = term_so = NULL;
211235783Skib	today = 0;
212235783Skib	if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
213235783Skib		date	dt;		/* handy date */
214235783Skib
215235783Skib		b = cbuf;
216235783Skib		term_so = tgetstr("so", &b);
217235783Skib		term_se = tgetstr("se", &b);
218282199Sdumbbell		t = time(NULL);
219282199Sdumbbell		tm1 = localtime(&t);
220282199Sdumbbell		dt.y = tm1->tm_year + 1900;
221282199Sdumbbell		dt.m = tm1->tm_mon + 1;
222235783Skib		dt.d = tm1->tm_mday;
223282199Sdumbbell
224235783Skib		today = sndaysb(&dt);
225235783Skib	}
226235783Skib
227235783Skib	/*
228235783Skib	 * Use locale to determine the country code,
229235783Skib	 * and use the country code to determine the default
230235783Skib	 * switchdate and date format from the switches table.
231282199Sdumbbell	 */
232282199Sdumbbell	if (setlocale(LC_ALL, "") == NULL)
233282199Sdumbbell		warn("setlocale");
234282199Sdumbbell	locale = setlocale(LC_TIME, NULL);
235235783Skib	if (locale == NULL ||
236235783Skib	    strcmp(locale, "C") == 0 ||
237235783Skib	    strcmp(locale, "POSIX") == 0 ||
238235783Skib	    strcmp(locale, "ASCII") == 0 ||
239282199Sdumbbell	    strcmp(locale, "US-ASCII") == 0)
240235783Skib		locale = "_US";
241235783Skib	q = switches + sizeof(switches) / sizeof(struct djswitch);
242235783Skib	for (p = switches; p != q; p++)
243235783Skib		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
244255012Sjkim			break;
245255012Sjkim	if (p == q) {
246235783Skib		nswitch = ndaysj(&dftswitch->dt);
247255012Sjkim	} else {
248255012Sjkim		nswitch = ndaysj(&p->dt);
249255012Sjkim		dftswitch = p;
250255012Sjkim	}
251255012Sjkim
252255012Sjkim
253255012Sjkim	/*
254255012Sjkim	 * Get the filename portion of argv[0] and set flag_backward if
255235783Skib	 * this program is called "cal".
256255012Sjkim	 */
257255012Sjkim	cp = strrchr(argv[0], '/');
258235783Skib	cp = (cp == NULL) ? argv[0] : cp + 1;
259282199Sdumbbell	if (strcmp("cal", cp) == 0)
260235783Skib		flag_backward = 1;
261235783Skib
262235783Skib	/* Set the switch date to United Kingdom if backwards compatible */
263235783Skib	if (flag_backward)
264235783Skib		nswitchb = ndaysj(&ukswitch);
265282199Sdumbbell
266282199Sdumbbell	while ((ch = getopt(argc, argv, "Jehjm:ops:wy")) != -1)
267282199Sdumbbell		switch (ch) {
268235783Skib		case 'J':
269235783Skib			if (flag_backward)
270235783Skib				usage();
271235783Skib			nswitch = ndaysj(&never);
272235783Skib			flag_julian_cal = 1;
273235783Skib			break;
274235783Skib		case 'h':
275235783Skib			term_so = term_se = NULL;
276235783Skib			break;
277235783Skib		case 'e':
278235783Skib			if (flag_backward)
279282199Sdumbbell				usage();
280282199Sdumbbell			flag_easter = 1;
281235783Skib			break;
282235783Skib		case 'j':
283235783Skib			flag_julian_day = 1;
284235783Skib			break;
285235783Skib		case 'm':
286235783Skib			flag_month = optarg;
287235783Skib			break;
288235783Skib		case 'o':
289235783Skib			if (flag_backward)
290235783Skib				usage();
291235783Skib			flag_orthodox = 1;
292235783Skib			flag_easter = 1;
293235783Skib			break;
294235783Skib		case 'p':
295235783Skib			if (flag_backward)
296235783Skib				usage();
297235783Skib			printcc();
298235783Skib			return (0);
299235783Skib			break;
300235783Skib		case 's':
301235783Skib			if (flag_backward)
302235783Skib				usage();
303235783Skib			q = switches +
304235783Skib			    sizeof(switches) / sizeof(struct djswitch);
305235783Skib			for (p = switches;
306235783Skib			     p != q && strcmp(p->cc, optarg) != 0; p++)
307235783Skib				;
308235783Skib			if (p == q)
309235783Skib				errx(EX_USAGE,
310235783Skib				    "%s: invalid country code", optarg);
311235783Skib			nswitch = ndaysj(&(p->dt));
312235783Skib			break;
313235783Skib		case 'w':
314235783Skib			if (flag_backward)
315235783Skib				usage();
316235783Skib			flag_weeks = 1;
317235783Skib			break;
318235783Skib		case 'y':
319235783Skib			flag_hole_year = 1;
320235783Skib			break;
321235783Skib		default:
322235783Skib			usage();
323235783Skib		}
324235783Skib
325235783Skib	argc -= optind;
326235783Skib	argv += optind;
327235783Skib
328235783Skib	switch (argc) {
329235783Skib	case 2:
330235783Skib		if (flag_easter)
331235783Skib			usage();
332235783Skib		flag_month = *argv++;
333235783Skib		/* FALLTHROUGH */
334235783Skib	case 1:
335235783Skib		y = atoi(*argv++);
336235783Skib		if (y < 1 || y > 9999)
337235783Skib			errx(EX_USAGE, "year %d not in range 1..9999", y);
338235783Skib		break;
339235783Skib	case 0:
340235783Skib		{
341235783Skib			time_t t;
342235783Skib			struct tm *tm;
343235783Skib
344282199Sdumbbell			t = time(NULL);
345235783Skib			tm = localtime(&t);
346235783Skib			y = tm->tm_year + 1900;
347235783Skib			m = tm->tm_mon + 1;
348235783Skib		}
349235783Skib		break;
350235783Skib	default:
351235783Skib		usage();
352235783Skib	}
353235783Skib
354282199Sdumbbell	if (flag_month != NULL) {
355235783Skib		if (parsemonth(flag_month, &m, &y)) {
356235783Skib			errx(EX_USAGE,
357235783Skib			    "%s is neither a month number (1..12) nor a name",
358235783Skib			    flag_month);
359235783Skib		}
360235783Skib	}
361235783Skib
362235783Skib	if (flag_easter)
363235783Skib		printeaster(y, flag_julian_cal, flag_orthodox);
364235783Skib	else if (argc == 1 || flag_hole_year) {
365282199Sdumbbell		/* disable the highlight for now */
366235783Skib		today = 0;
367235783Skib		if (flag_backward)
368235783Skib			printyearb(y, flag_julian_day);
369235783Skib		else
370235783Skib			printyear(y, flag_julian_day);
371235783Skib	} else
372235783Skib		if (flag_backward)
373235783Skib			printmonthb(y, m, flag_julian_day);
374235783Skib		else
375235783Skib			printmonth(y, m, flag_julian_day);
376235783Skib
377235783Skib	return (0);
378235783Skib}
379235783Skib
380235783Skibstatic void
381235783Skibusage(void)
382235783Skib{
383235783Skib
384235783Skib	fputs(
385235783Skib	    "usage: cal [-hjy] [[month] year]\n"
386235783Skib	    "       cal [-hj] [-m month] [year]\n"
387235783Skib	    "       ncal [-hJjpwy] [-s country_code] [[month] year]\n"
388235783Skib	    "       ncal [-hJeo] [year]\n", stderr);
389235783Skib	exit(EX_USAGE);
390235783Skib}
391235783Skib
392235783Skib/* print the assumed switches for all countries */
393235783Skibvoid
394235783Skibprintcc(void)
395235783Skib{
396235783Skib	struct djswitch *p;
397235783Skib	int n;	/* number of lines to print */
398235783Skib	int m;	/* offset from left to right table entry on the same line */
399235783Skib
400235783Skib#define FSTR "%c%s %-15s%4d-%02d-%02d"
401235783Skib#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
402235783Skib#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
403
404	n = sizeof(switches) / sizeof(struct djswitch);
405	m = (n + 1) / 2;
406	n /= 2;
407	for (p = switches; p != switches + n; p++)
408		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
409	if (m != n)
410		printf(FSTR"\n", FSTRARG(p));
411}
412
413/* print the date of easter sunday */
414void
415printeaster(int y, int julian, int orthodox)
416{
417	date    dt;
418	struct tm tm;
419	char    buf[80];
420	static int d_first = -1;
421
422	if (d_first < 0)
423		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
424	/* force orthodox easter for years before 1583 */
425	if (y < 1583)
426		orthodox = 1;
427
428	if (orthodox)
429		if (julian)
430			easteroj(y, &dt);
431		else
432			easterog(y, &dt);
433	else
434		easterg(y, &dt);
435
436	memset(&tm, 0, sizeof(tm));
437	tm.tm_year = dt.y - 1900;
438	tm.tm_mon  = dt.m - 1;
439	tm.tm_mday = dt.d;
440	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
441	printf("%s\n", buf);
442}
443
444void
445printmonth(int y, int m, int jd_flag)
446{
447	struct monthlines month;
448	struct weekdays wds;
449	int i;
450
451	mkmonth(y, m - 1, jd_flag, &month);
452	mkweekdays(&wds);
453	printf("    %ls %d\n", month.name, y);
454	for (i = 0; i != 7; i++)
455		wprintf(L"%.2ls%s\n", wds.names[i], month.lines[i]);
456	if (flag_weeks)
457		printf("  %s\n", month.weeks);
458}
459
460void
461printmonthb(int y, int m, int jd_flag)
462{
463	struct monthlines month;
464	struct weekdays wds;
465	wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
466	int i;
467	int mw;
468
469	mkmonthb(y, m - 1, jd_flag, &month);
470	mkweekdays(&wds);
471
472	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
473
474	swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
475	wprintf(L"%ls\n", wcenter(t, s, mw));
476
477	if (jd_flag)
478		wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
479			wds.names[6], wds.names[0],
480			wds.names[1], wds.names[2], wds.names[3],
481			wds.names[4], wds.names[5]);
482	else
483		wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6],
484			wds.names[0], wds.names[1], wds.names[2], wds.names[3],
485			wds.names[4], wds.names[5]);
486
487	for (i = 0; i != 6; i++)
488		printf("%s\n", month.lines[i]+1);
489}
490
491void
492printyear(int y, int jd_flag)
493{
494	struct monthlines year[12];
495	struct weekdays wds;
496	char    s[80], t[80];
497	int     i, j;
498	int     mpl;
499	int     mw;
500
501	for (i = 0; i != 12; i++)
502		mkmonth(y, i, jd_flag, year + i);
503	mkweekdays(&wds);
504	mpl = jd_flag ? 3 : 4;
505	mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
506
507	sprintf(s, "%d", y);
508	printf("%s\n", center(t, s, mpl * mw));
509
510	for (j = 0; j != 12; j += mpl) {
511		wprintf(L"    %-*ls%-*ls",
512		    mw, year[j].name,
513		    mw, year[j + 1].name);
514		if (mpl == 3)
515			printf("%ls\n", year[j + 2].name);
516		else
517			wprintf(L"%-*ls%ls\n",
518		    	    mw, year[j + 2].name,
519		    	    year[j + 3].name);
520		for (i = 0; i != 7; i++) {
521			wprintf(L"%.2ls%-*s%-*s",
522			    wds.names[i],
523			    mw, year[j].lines[i],
524			    mw, year[j + 1].lines[i]);
525			if (mpl == 3)
526				printf("%s\n", year[j + 2].lines[i]);
527			else
528				printf("%-*s%s\n",
529			    	    mw, year[j + 2].lines[i],
530			    	    year[j + 3].lines[i]);
531		}
532		if (flag_weeks) {
533			if (mpl == 3)
534				printf("  %-*s%-*s%-s\n",
535				    mw, year[j].weeks,
536				    mw, year[j + 1].weeks,
537				    year[j + 2].weeks);
538			else
539				printf("  %-*s%-*s%-*s%-s\n",
540				    mw, year[j].weeks,
541				    mw, year[j + 1].weeks,
542				    mw, year[j + 2].weeks,
543				    year[j + 3].weeks);
544		}
545	}
546}
547
548void
549printyearb(int y, int jd_flag)
550{
551	struct monthlines year[12];
552	struct weekdays wds;
553	char	s[80], t[80];
554	wchar_t	ws[80], wt[80];
555	int     i, j;
556	int     mpl;
557	int     mw;
558
559	for (i = 0; i != 12; i++)
560		mkmonthb(y, i, jd_flag, year + i);
561	mkweekdays(&wds);
562	mpl = jd_flag ? 2 : 3;
563	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
564
565	sprintf(s, "%d", y);
566	printf("%s\n\n", center(t, s, mw * mpl + mpl));
567
568	for (j = 0; j != 12; j += mpl) {
569		wprintf(L"%-*ls  ", mw, wcenter(ws, year[j].name, mw));
570		if (mpl == 2)
571			printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
572		else
573			wprintf(L"%-*ls  %ls\n", mw,
574			    wcenter(ws, year[j + 1].name, mw),
575			    wcenter(wt, year[j + 2].name, mw));
576
577		if (mpl == 2)
578			wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
579				" %ls %ls %ls %ls %ls %ls %.2ls\n",
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				wds.names[6], wds.names[0], wds.names[1],
584				wds.names[2], wds.names[3], wds.names[4],
585				wds.names[5]);
586		else
587			wprintf(L"%ls%ls%ls%ls%ls%ls%ls "
588				"%ls%ls%ls%ls%ls%ls%ls "
589				"%ls%ls%ls%ls%ls%ls%.2ls\n",
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				wds.names[6], wds.names[0], wds.names[1],
597				wds.names[2], wds.names[3], wds.names[4],
598				wds.names[5]);
599		for (i = 0; i != 6; i++) {
600			if (mpl == 2)
601				printf("%-*s  %s\n",
602			    mw, year[j].lines[i]+1,
603			    year[j + 1].lines[i]+1);
604			else
605				printf("%-*s  %-*s  %s\n",
606			    mw, year[j].lines[i]+1,
607			    mw, year[j + 1].lines[i]+1,
608			    year[j + 2].lines[i]+1);
609
610		}
611	}
612}
613
614void
615mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
616{
617
618	struct tm tm;		/* for strftime printing local names of
619				 * months */
620	date    dt;		/* handy date */
621	int     dw;		/* width of numbers */
622	int     first;		/* first day of month */
623	int     firstm;		/* first day of first week of month */
624	int     i, j, k, l;	/* just indices */
625	int     last;		/* the first day of next month */
626	int     jan1 = 0;	/* the first day of this year */
627	char   *ds;		/* pointer to day strings (daystr or
628				 * jdaystr) */
629
630	/* Set name of month. */
631	memset(&tm, 0, sizeof(tm));
632	tm.tm_mon = m;
633	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
634		 L"%OB", &tm);
635	mlines->name[0] = towupper(mlines->name[0]);
636
637	/*
638	 * Set first and last to the day number of the first day of this
639	 * month and the first day of next month respectively. Set jan1 to
640	 * the day number of the first day of this year.
641	 */
642	first = firstday(y, m + 1);
643	if (m == 11)
644		last = firstday(y + 1, 1);
645	else
646		last = firstday(y, m + 2);
647
648	if (jd_flag)
649		jan1 = firstday(y, 1);
650
651	/*
652	 * Set firstm to the day number of monday of the first week of
653	 * this month. (This might be in the last month)
654	 */
655	firstm = first - weekday(first);
656
657	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
658	if (jd_flag) {
659		ds = jdaystr;
660		dw = 4;
661	} else {
662		ds = daystr;
663		dw = 3;
664	}
665
666	/*
667	 * Fill the lines with day of month or day of year (julian day)
668	 * line index: i, each line is one weekday. column index: j, each
669	 * column is one day number. print column index: k.
670	 */
671	for (i = 0; i != 7; i++) {
672		l = 0;
673		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
674			if (j == today && (term_so != NULL && term_se != NULL)) {
675				l = strlen(term_so);
676				if (jd_flag)
677					dt.d = j - jan1 + 1;
678				else
679					sdateb(j, &dt);
680				/* separator */
681				mlines->lines[i][k] = ' ';
682				/* the actual text */
683				memcpy(mlines->lines[i] + k + l,
684				    ds + dt.d * dw, dw);
685				/* highlight on */
686				memcpy(mlines->lines[i] + k + 1, term_so, l);
687				/* highlight off */
688				memcpy(mlines->lines[i] + k + l + dw, term_se,
689				    strlen(term_se));
690				l = strlen(term_se) + strlen(term_so);
691				continue;
692			}
693			if (j >= first) {
694				if (jd_flag)
695					dt.d = j - jan1 + 1;
696				else
697					sdate(j, &dt);
698				memcpy(mlines->lines[i] + k + l,
699				       ds + dt.d * dw, dw);
700			} else
701				memcpy(mlines->lines[i] + k + l, "    ", dw);
702		}
703		mlines->lines[i][k + l] = '\0';
704
705	}
706
707	/* fill the weeknumbers */
708	if (flag_weeks) {
709		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
710			if (j <= nswitch)
711				memset(mlines->weeks + k, ' ', dw);
712			else
713				memcpy(mlines->weeks + k,
714				    ds + week(j, &i)*dw, dw);
715		mlines->weeks[k] = '\0';
716	}
717}
718
719void
720mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
721{
722
723	struct tm tm;		/* for strftime printing local names of
724				 * months */
725	date    dt;		/* handy date */
726	int     dw;		/* width of numbers */
727	int     first;		/* first day of month */
728	int     firsts;		/* sunday of first week of month */
729	int     i, j, k, l;	/* just indices */
730	int     jan1 = 0;	/* the first day of this year */
731	int     last;		/* the first day of next month */
732	char   *ds;		/* pointer to day strings (daystr or
733				 * jdaystr) */
734
735	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
736	if (jd_flag) {
737		ds = jdaystr;
738		dw = 4;
739	} else {
740		ds = daystr;
741		dw = 3;
742	}
743
744	/* Set name of month centered */
745	memset(&tm, 0, sizeof(tm));
746	tm.tm_mon = m;
747	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
748		 L"%OB", &tm);
749	mlines->name[0] = towupper(mlines->name[0]);
750
751	/*
752	 * Set first and last to the day number of the first day of this
753	 * month and the first day of next month respectively. Set jan1 to
754	 * the day number of Jan 1st of this year.
755	 */
756	dt.y = y;
757	dt.m = m + 1;
758	dt.d = 1;
759	first = sndaysb(&dt);
760	if (m == 11) {
761		dt.y = y + 1;
762		dt.m = 1;
763		dt.d = 1;
764	} else {
765		dt.y = y;
766		dt.m = m + 2;
767		dt.d = 1;
768	}
769	last = sndaysb(&dt);
770
771	if (jd_flag) {
772		dt.y = y;
773		dt.m = 1;
774		dt.d = 1;
775		jan1 = sndaysb(&dt);
776	}
777
778	/*
779	 * Set firsts to the day number of sunday of the first week of
780	 * this month. (This might be in the last month)
781	 */
782	firsts = first - (weekday(first)+1) % 7;
783
784	/*
785	 * Fill the lines with day of month or day of year (Julian day)
786	 * line index: i, each line is one week. column index: j, each
787	 * column is one day number. print column index: k.
788	 */
789	for (i = 0; i != 6; i++) {
790		l = 0;
791		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
792		    j++, k += dw) {
793			if (j == today && (term_so != NULL && term_se != NULL)) {
794				l = strlen(term_so);
795				if (jd_flag)
796					dt.d = j - jan1 + 1;
797				else
798					sdateb(j, &dt);
799				/* separator */
800				mlines->lines[i][k] = ' ';
801				/* the actual text */
802				memcpy(mlines->lines[i] + k + l,
803				    ds + dt.d * dw, dw);
804				/* highlight on */
805				memcpy(mlines->lines[i] + k + 1, term_so, l);
806				/* highlight off */
807				memcpy(mlines->lines[i] + k + l + dw, term_se,
808				    strlen(term_se));
809				l = strlen(term_se) + strlen(term_so);
810				continue;
811			}
812			if (j >= first) {
813				if (jd_flag)
814					dt.d = j - jan1 + 1;
815				else
816					sdateb(j, &dt);
817				memcpy(mlines->lines[i] + k + l,
818				       ds + dt.d * dw, dw);
819			} else
820				memcpy(mlines->lines[i] + k + l, "    ", dw);
821		}
822		if (k == 0)
823			mlines->lines[i][1] = '\0';
824		else
825			mlines->lines[i][k + l] = '\0';
826	}
827}
828
829/* Put the local names of weekdays into the wds */
830void
831mkweekdays(struct weekdays *wds)
832{
833	int i, len;
834	struct tm tm;
835	wchar_t buf[20];
836
837	memset(&tm, 0, sizeof(tm));
838
839	for (i = 0; i != 7; i++) {
840		tm.tm_wday = (i+1) % 7;
841		wcsftime(buf, sizeof(buf), L"%a", &tm);
842		len = wcslen(buf);
843		if (len > 2)
844			len = 2;
845		wcscpy(wds->names[i], L"   ");
846		wcsncpy(wds->names[i] + 2 - len, buf, len);
847	}
848}
849
850/*
851 * Compute the day number of the first
852 * existing date after the first day in month.
853 * (the first day in month and even the month might not exist!)
854 */
855int
856firstday(int y, int m)
857{
858	date dt;
859	int nd;
860
861	dt.y = y;
862	dt.m = m;
863	dt.d = 1;
864	nd = sndays(&dt);
865	for (;;) {
866		sdate(nd, &dt);
867		if ((dt.m >= m && dt.y == y) || dt.y > y)
868			return (nd);
869		else
870			nd++;
871	}
872	/* NEVER REACHED */
873}
874
875/*
876 * Compute the number of days from date, obey the local switch from
877 * Julian to Gregorian if specified by the user.
878 */
879int
880sndays(struct date *d)
881{
882
883	if (nswitch != 0)
884		if (nswitch < ndaysj(d))
885			return (ndaysg(d));
886		else
887			return (ndaysj(d));
888	else
889		return ndaysg(d);
890}
891
892/*
893 * Compute the number of days from date, obey the switch from
894 * Julian to Gregorian as used by UK and her colonies.
895 */
896int
897sndaysb(struct date *d)
898{
899
900	if (nswitchb < ndaysj(d))
901		return (ndaysg(d));
902	else
903		return (ndaysj(d));
904}
905
906/* Inverse of sndays */
907struct date *
908sdate(int nd, struct date *d)
909{
910
911	if (nswitch < nd)
912		return (gdate(nd, d));
913	else
914		return (jdate(nd, d));
915}
916
917/* Inverse of sndaysb */
918struct date *
919sdateb(int nd, struct date *d)
920{
921
922	if (nswitchb < nd)
923		return (gdate(nd, d));
924	else
925		return (jdate(nd, d));
926}
927
928/* Center string t in string s of length w by putting enough leading blanks */
929char *
930center(char *s, char *t, int w)
931{
932	char blanks[80];
933
934	memset(blanks, ' ', sizeof(blanks));
935	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
936	return (s);
937}
938
939/* Center string t in string s of length w by putting enough leading blanks */
940wchar_t *
941wcenter(wchar_t *s, wchar_t *t, int w)
942{
943	char blanks[80];
944
945	memset(blanks, ' ', sizeof(blanks));
946	swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
947	return (s);
948}
949
950int
951parsemonth(const char *s, int *m, int *y)
952{
953	int nm, ny;
954	char *cp;
955	struct tm tm;
956
957	nm = (int)strtol(s, &cp, 10);
958	if (cp != s) {
959		ny = *y;
960		if (*cp == '\0') {
961			;	/* no special action */
962		} else if (*cp == 'f' || *cp == 'F') {
963			if (nm <= *m)
964				ny++;
965		} else if (*cp == 'p' || *cp == 'P') {
966			if (nm >= *m)
967				ny--;
968		} else
969			return (1);
970		if (nm < 1 || nm > 12)
971			return 1;
972		*m = nm;
973		*y = ny;
974		return (0);
975	}
976	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
977		*m = tm.tm_mon + 1;
978		return (0);
979	}
980	return (1);
981}
982