ncal.c revision 200462
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 200462 2009-12-13 03:14:06Z delphij $";
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_so, *term_se;
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_se = term_so = NULL;
211	today = 0;
212	if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
213		date	dt;		/* handy date */
214
215		b = cbuf;
216		term_so = tgetstr("so", &b);
217		term_se = tgetstr("se", &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, "Jehjm: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 'h':
275			term_so = term_se = NULL;
276			break;
277		case 'e':
278			if (flag_backward)
279				usage();
280			flag_easter = 1;
281			break;
282		case 'j':
283			flag_julian_day = 1;
284			break;
285		case 'm':
286			flag_month = optarg;
287			break;
288		case 'o':
289			if (flag_backward)
290				usage();
291			flag_orthodox = 1;
292			flag_easter = 1;
293			break;
294		case 'p':
295			if (flag_backward)
296				usage();
297			printcc();
298			return (0);
299			break;
300		case 's':
301			if (flag_backward)
302				usage();
303			q = switches +
304			    sizeof(switches) / sizeof(struct djswitch);
305			for (p = switches;
306			     p != q && strcmp(p->cc, optarg) != 0; p++)
307				;
308			if (p == q)
309				errx(EX_USAGE,
310				    "%s: invalid country code", optarg);
311			nswitch = ndaysj(&(p->dt));
312			break;
313		case 'w':
314			if (flag_backward)
315				usage();
316			flag_weeks = 1;
317			break;
318		case 'y':
319			flag_hole_year = 1;
320			break;
321		default:
322			usage();
323		}
324
325	argc -= optind;
326	argv += optind;
327
328	switch (argc) {
329	case 2:
330		if (flag_easter)
331			usage();
332		flag_month = *argv++;
333		/* FALLTHROUGH */
334	case 1:
335		y = atoi(*argv++);
336		if (y < 1 || y > 9999)
337			errx(EX_USAGE, "year %d not in range 1..9999", y);
338		break;
339	case 0:
340		{
341			time_t t;
342			struct tm *tm;
343
344			t = time(NULL);
345			tm = localtime(&t);
346			y = tm->tm_year + 1900;
347			m = tm->tm_mon + 1;
348		}
349		break;
350	default:
351		usage();
352	}
353
354	if (flag_month != NULL) {
355		if (parsemonth(flag_month, &m, &y)) {
356			errx(EX_USAGE,
357			    "%s is neither a month number (1..12) nor a name",
358			    flag_month);
359		}
360	}
361
362	if (flag_easter)
363		printeaster(y, flag_julian_cal, flag_orthodox);
364	else if (argc == 1 || flag_hole_year) {
365		/* disable the highlight for now */
366		today = 0;
367		if (flag_backward)
368			printyearb(y, flag_julian_day);
369		else
370			printyear(y, flag_julian_day);
371	} else
372		if (flag_backward)
373			printmonthb(y, m, flag_julian_day);
374		else
375			printmonth(y, m, flag_julian_day);
376
377	return (0);
378}
379
380static void
381usage(void)
382{
383
384	fputs(
385	    "usage: cal [-hjy] [[month] year]\n"
386	    "       cal [-hj] [-m month] [year]\n"
387	    "       ncal [-hJjpwy] [-s country_code] [[month] year]\n"
388	    "       ncal [-hJeo] [year]\n", stderr);
389	exit(EX_USAGE);
390}
391
392/* print the assumed switches for all countries */
393void
394printcc(void)
395{
396	struct djswitch *p;
397	int n;	/* number of lines to print */
398	int m;	/* offset from left to right table entry on the same line */
399
400#define FSTR "%c%s %-15s%4d-%02d-%02d"
401#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
402#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, len;
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		len = wcslen(wds.names[i]);
456		if (wcswidth(wds.names[i], len) == len)
457			wprintf(L"%.2ls%s\n", wds.names[i], month.lines[i]);
458		else
459			wprintf(L"%.1ls%s\n", wds.names[i], month.lines[i]);
460	}
461	if (flag_weeks)
462		printf("  %s\n", month.weeks);
463}
464
465void
466printmonthb(int y, int m, int jd_flag)
467{
468	struct monthlines month;
469	struct weekdays wds;
470	wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
471	int i;
472	int mw;
473
474	mkmonthb(y, m - 1, jd_flag, &month);
475	mkweekdays(&wds);
476
477	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
478
479	swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
480	wprintf(L"%ls\n", wcenter(t, s, mw));
481
482	if (jd_flag)
483		wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
484			wds.names[6], wds.names[0],
485			wds.names[1], wds.names[2], wds.names[3],
486			wds.names[4], wds.names[5]);
487	else
488		wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6],
489			wds.names[0], wds.names[1], wds.names[2], wds.names[3],
490			wds.names[4], wds.names[5]);
491
492	for (i = 0; i != 6; i++)
493		printf("%s\n", month.lines[i]+1);
494}
495
496void
497printyear(int y, int jd_flag)
498{
499	struct monthlines year[12];
500	struct weekdays wds;
501	char    s[80], t[80];
502	int     i, j;
503	int     mpl;
504	int     mw;
505
506	for (i = 0; i != 12; i++)
507		mkmonth(y, i, jd_flag, year + i);
508	mkweekdays(&wds);
509	mpl = jd_flag ? 3 : 4;
510	mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
511
512	sprintf(s, "%d", y);
513	printf("%s\n", center(t, s, mpl * mw));
514
515	for (j = 0; j != 12; j += mpl) {
516		wprintf(L"    %-*ls%-*ls",
517		    mw, year[j].name,
518		    mw, year[j + 1].name);
519		if (mpl == 3)
520			printf("%ls\n", year[j + 2].name);
521		else
522			wprintf(L"%-*ls%ls\n",
523		    	    mw, year[j + 2].name,
524		    	    year[j + 3].name);
525		for (i = 0; i != 7; i++) {
526			wprintf(L"%.2ls%-*s%-*s",
527			    wds.names[i],
528			    mw, year[j].lines[i],
529			    mw, year[j + 1].lines[i]);
530			if (mpl == 3)
531				printf("%s\n", year[j + 2].lines[i]);
532			else
533				printf("%-*s%s\n",
534			    	    mw, year[j + 2].lines[i],
535			    	    year[j + 3].lines[i]);
536		}
537		if (flag_weeks) {
538			if (mpl == 3)
539				printf("  %-*s%-*s%-s\n",
540				    mw, year[j].weeks,
541				    mw, year[j + 1].weeks,
542				    year[j + 2].weeks);
543			else
544				printf("  %-*s%-*s%-*s%-s\n",
545				    mw, year[j].weeks,
546				    mw, year[j + 1].weeks,
547				    mw, year[j + 2].weeks,
548				    year[j + 3].weeks);
549		}
550	}
551}
552
553void
554printyearb(int y, int jd_flag)
555{
556	struct monthlines year[12];
557	struct weekdays wds;
558	char	s[80], t[80];
559	wchar_t	ws[80], wt[80];
560	int     i, j;
561	int     mpl;
562	int     mw;
563
564	for (i = 0; i != 12; i++)
565		mkmonthb(y, i, jd_flag, year + i);
566	mkweekdays(&wds);
567	mpl = jd_flag ? 2 : 3;
568	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
569
570	sprintf(s, "%d", y);
571	printf("%s\n\n", center(t, s, mw * mpl + mpl));
572
573	for (j = 0; j != 12; j += mpl) {
574		wprintf(L"%-*ls  ", mw, wcenter(ws, year[j].name, mw));
575		if (mpl == 2)
576			printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
577		else
578			wprintf(L"%-*ls  %ls\n", mw,
579			    wcenter(ws, year[j + 1].name, mw),
580			    wcenter(wt, year[j + 2].name, mw));
581
582		if (mpl == 2)
583			wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
584				" %ls %ls %ls %ls %ls %ls %.2ls\n",
585				wds.names[6], wds.names[0], wds.names[1],
586				wds.names[2], wds.names[3], wds.names[4],
587				wds.names[5],
588				wds.names[6], wds.names[0], wds.names[1],
589				wds.names[2], wds.names[3], wds.names[4],
590				wds.names[5]);
591		else
592			wprintf(L"%ls%ls%ls%ls%ls%ls%ls "
593				"%ls%ls%ls%ls%ls%ls%ls "
594				"%ls%ls%ls%ls%ls%ls%.2ls\n",
595				wds.names[6], wds.names[0], wds.names[1],
596				wds.names[2], wds.names[3], wds.names[4],
597				wds.names[5],
598				wds.names[6], wds.names[0], wds.names[1],
599				wds.names[2], wds.names[3], wds.names[4],
600				wds.names[5],
601				wds.names[6], wds.names[0], wds.names[1],
602				wds.names[2], wds.names[3], wds.names[4],
603				wds.names[5]);
604		for (i = 0; i != 6; i++) {
605			if (mpl == 2)
606				printf("%-*s  %s\n",
607			    mw, year[j].lines[i]+1,
608			    year[j + 1].lines[i]+1);
609			else
610				printf("%-*s  %-*s  %s\n",
611			    mw, year[j].lines[i]+1,
612			    mw, year[j + 1].lines[i]+1,
613			    year[j + 2].lines[i]+1);
614
615		}
616	}
617}
618
619void
620mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
621{
622
623	struct tm tm;		/* for strftime printing local names of
624				 * months */
625	date    dt;		/* handy date */
626	int     dw;		/* width of numbers */
627	int     first;		/* first day of month */
628	int     firstm;		/* first day of first week of month */
629	int     i, j, k, l;	/* just indices */
630	int     last;		/* the first day of next month */
631	int     jan1 = 0;	/* the first day of this year */
632	char   *ds;		/* pointer to day strings (daystr or
633				 * jdaystr) */
634
635	/* Set name of month. */
636	memset(&tm, 0, sizeof(tm));
637	tm.tm_mon = m;
638	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
639		 L"%OB", &tm);
640	mlines->name[0] = towupper(mlines->name[0]);
641
642	/*
643	 * Set first and last to the day number of the first day of this
644	 * month and the first day of next month respectively. Set jan1 to
645	 * the day number of the first day of this year.
646	 */
647	first = firstday(y, m + 1);
648	if (m == 11)
649		last = firstday(y + 1, 1);
650	else
651		last = firstday(y, m + 2);
652
653	if (jd_flag)
654		jan1 = firstday(y, 1);
655
656	/*
657	 * Set firstm to the day number of monday of the first week of
658	 * this month. (This might be in the last month)
659	 */
660	firstm = first - weekday(first);
661
662	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
663	if (jd_flag) {
664		ds = jdaystr;
665		dw = 4;
666	} else {
667		ds = daystr;
668		dw = 3;
669	}
670
671	/*
672	 * Fill the lines with day of month or day of year (julian day)
673	 * line index: i, each line is one weekday. column index: j, each
674	 * column is one day number. print column index: k.
675	 */
676	for (i = 0; i != 7; i++) {
677		l = 0;
678		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
679			if (j == today && (term_so != NULL && term_se != NULL)) {
680				l = strlen(term_so);
681				if (jd_flag)
682					dt.d = j - jan1 + 1;
683				else
684					sdateb(j, &dt);
685				/* separator */
686				mlines->lines[i][k] = ' ';
687				/* the actual text */
688				memcpy(mlines->lines[i] + k + l,
689				    ds + dt.d * dw, dw);
690				/* highlight on */
691				memcpy(mlines->lines[i] + k + 1, term_so, l);
692				/* highlight off */
693				memcpy(mlines->lines[i] + k + l + dw, term_se,
694				    strlen(term_se));
695				l = strlen(term_se) + strlen(term_so);
696				continue;
697			}
698			if (j >= first) {
699				if (jd_flag)
700					dt.d = j - jan1 + 1;
701				else
702					sdate(j, &dt);
703				memcpy(mlines->lines[i] + k + l,
704				       ds + dt.d * dw, dw);
705			} else
706				memcpy(mlines->lines[i] + k + l, "    ", dw);
707		}
708		mlines->lines[i][k + l] = '\0';
709
710	}
711
712	/* fill the weeknumbers */
713	if (flag_weeks) {
714		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
715			if (j <= nswitch)
716				memset(mlines->weeks + k, ' ', dw);
717			else
718				memcpy(mlines->weeks + k,
719				    ds + week(j, &i)*dw, dw);
720		mlines->weeks[k] = '\0';
721	}
722}
723
724void
725mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
726{
727
728	struct tm tm;		/* for strftime printing local names of
729				 * months */
730	date    dt;		/* handy date */
731	int     dw;		/* width of numbers */
732	int     first;		/* first day of month */
733	int     firsts;		/* sunday of first week of month */
734	int     i, j, k, l;	/* just indices */
735	int     jan1 = 0;	/* the first day of this year */
736	int     last;		/* the first day of next month */
737	char   *ds;		/* pointer to day strings (daystr or
738				 * jdaystr) */
739
740	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
741	if (jd_flag) {
742		ds = jdaystr;
743		dw = 4;
744	} else {
745		ds = daystr;
746		dw = 3;
747	}
748
749	/* Set name of month centered */
750	memset(&tm, 0, sizeof(tm));
751	tm.tm_mon = m;
752	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
753		 L"%OB", &tm);
754	mlines->name[0] = towupper(mlines->name[0]);
755
756	/*
757	 * Set first and last to the day number of the first day of this
758	 * month and the first day of next month respectively. Set jan1 to
759	 * the day number of Jan 1st of this year.
760	 */
761	dt.y = y;
762	dt.m = m + 1;
763	dt.d = 1;
764	first = sndaysb(&dt);
765	if (m == 11) {
766		dt.y = y + 1;
767		dt.m = 1;
768		dt.d = 1;
769	} else {
770		dt.y = y;
771		dt.m = m + 2;
772		dt.d = 1;
773	}
774	last = sndaysb(&dt);
775
776	if (jd_flag) {
777		dt.y = y;
778		dt.m = 1;
779		dt.d = 1;
780		jan1 = sndaysb(&dt);
781	}
782
783	/*
784	 * Set firsts to the day number of sunday of the first week of
785	 * this month. (This might be in the last month)
786	 */
787	firsts = first - (weekday(first)+1) % 7;
788
789	/*
790	 * Fill the lines with day of month or day of year (Julian day)
791	 * line index: i, each line is one week. column index: j, each
792	 * column is one day number. print column index: k.
793	 */
794	for (i = 0; i != 6; i++) {
795		l = 0;
796		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
797		    j++, k += dw) {
798			if (j == today && (term_so != NULL && term_se != NULL)) {
799				l = strlen(term_so);
800				if (jd_flag)
801					dt.d = j - jan1 + 1;
802				else
803					sdateb(j, &dt);
804				/* separator */
805				mlines->lines[i][k] = ' ';
806				/* the actual text */
807				memcpy(mlines->lines[i] + k + l,
808				    ds + dt.d * dw, dw);
809				/* highlight on */
810				memcpy(mlines->lines[i] + k + 1, term_so, l);
811				/* highlight off */
812				memcpy(mlines->lines[i] + k + l + dw, term_se,
813				    strlen(term_se));
814				l = strlen(term_se) + strlen(term_so);
815				continue;
816			}
817			if (j >= first) {
818				if (jd_flag)
819					dt.d = j - jan1 + 1;
820				else
821					sdateb(j, &dt);
822				memcpy(mlines->lines[i] + k + l,
823				       ds + dt.d * dw, dw);
824			} else
825				memcpy(mlines->lines[i] + k + l, "    ", dw);
826		}
827		if (k == 0)
828			mlines->lines[i][1] = '\0';
829		else
830			mlines->lines[i][k + l] = '\0';
831	}
832}
833
834/* Put the local names of weekdays into the wds */
835void
836mkweekdays(struct weekdays *wds)
837{
838	int i, len, width = 0;
839	struct tm tm;
840	wchar_t buf[20];
841
842	memset(&tm, 0, sizeof(tm));
843
844	for (i = 0; i != 7; i++) {
845		tm.tm_wday = (i+1) % 7;
846		wcsftime(buf, sizeof(buf), L"%a", &tm);
847		for (len = 2; len > 0; --len) {
848			if ((width = wcswidth(buf, len)) <= 2)
849				break;
850		}
851		wmemset(wds->names[i], L'\0', 4);
852		if (width == 1)
853			wds->names[i][0] = L' ';
854		wcsncat(wds->names[i], buf, len);
855		wcsncat(wds->names[i], L" ", 1);
856	}
857}
858
859/*
860 * Compute the day number of the first
861 * existing date after the first day in month.
862 * (the first day in month and even the month might not exist!)
863 */
864int
865firstday(int y, int m)
866{
867	date dt;
868	int nd;
869
870	dt.y = y;
871	dt.m = m;
872	dt.d = 1;
873	nd = sndays(&dt);
874	for (;;) {
875		sdate(nd, &dt);
876		if ((dt.m >= m && dt.y == y) || dt.y > y)
877			return (nd);
878		else
879			nd++;
880	}
881	/* NEVER REACHED */
882}
883
884/*
885 * Compute the number of days from date, obey the local switch from
886 * Julian to Gregorian if specified by the user.
887 */
888int
889sndays(struct date *d)
890{
891
892	if (nswitch != 0)
893		if (nswitch < ndaysj(d))
894			return (ndaysg(d));
895		else
896			return (ndaysj(d));
897	else
898		return ndaysg(d);
899}
900
901/*
902 * Compute the number of days from date, obey the switch from
903 * Julian to Gregorian as used by UK and her colonies.
904 */
905int
906sndaysb(struct date *d)
907{
908
909	if (nswitchb < ndaysj(d))
910		return (ndaysg(d));
911	else
912		return (ndaysj(d));
913}
914
915/* Inverse of sndays */
916struct date *
917sdate(int nd, struct date *d)
918{
919
920	if (nswitch < nd)
921		return (gdate(nd, d));
922	else
923		return (jdate(nd, d));
924}
925
926/* Inverse of sndaysb */
927struct date *
928sdateb(int nd, struct date *d)
929{
930
931	if (nswitchb < nd)
932		return (gdate(nd, d));
933	else
934		return (jdate(nd, d));
935}
936
937/* Center string t in string s of length w by putting enough leading blanks */
938char *
939center(char *s, char *t, int w)
940{
941	char blanks[80];
942
943	memset(blanks, ' ', sizeof(blanks));
944	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
945	return (s);
946}
947
948/* Center string t in string s of length w by putting enough leading blanks */
949wchar_t *
950wcenter(wchar_t *s, wchar_t *t, int w)
951{
952	char blanks[80];
953
954	memset(blanks, ' ', sizeof(blanks));
955	swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
956	return (s);
957}
958
959int
960parsemonth(const char *s, int *m, int *y)
961{
962	int nm, ny;
963	char *cp;
964	struct tm tm;
965
966	nm = (int)strtol(s, &cp, 10);
967	if (cp != s) {
968		ny = *y;
969		if (*cp == '\0') {
970			;	/* no special action */
971		} else if (*cp == 'f' || *cp == 'F') {
972			if (nm <= *m)
973				ny++;
974		} else if (*cp == 'p' || *cp == 'P') {
975			if (nm >= *m)
976				ny--;
977		} else
978			return (1);
979		if (nm < 1 || nm > 12)
980			return 1;
981		*m = nm;
982		*y = ny;
983		return (0);
984	}
985	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
986		*m = tm.tm_mon + 1;
987		return (0);
988	}
989	return (1);
990}
991