ncal.c revision 205427
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 205427 2010-03-21 21:33:21Z edwin $";
3032310Scharnier#endif /* not lint */
3132310Scharnier
3231744Shelbig#include <calendar.h>
33200462Sdelphij#include <ctype.h>
3431744Shelbig#include <err.h>
3574573Sache#include <langinfo.h>
3631744Shelbig#include <locale.h>
3731744Shelbig#include <stdio.h>
3831744Shelbig#include <stdlib.h>
3931744Shelbig#include <string.h>
4031744Shelbig#include <sysexits.h>
4131744Shelbig#include <time.h>
4231744Shelbig#include <unistd.h>
43189804Sdas#include <wchar.h>
44189804Sdas#include <wctype.h>
45191330Srdivacky#include <term.h>
46191330Srdivacky#undef lines			/* term.h defines this */
4731744Shelbig
48204697Sedwin/* Width of one month with backward compatibility and in regular mode*/
4931744Shelbig#define MONTH_WIDTH_B_J 27
5031744Shelbig#define MONTH_WIDTH_B 20
5131744Shelbig
52204697Sedwin#define MONTH_WIDTH_R_J 24
53204697Sedwin#define MONTH_WIDTH_R 18
5431744Shelbig
55191330Srdivacky#define MAX_WIDTH 64
5631744Shelbig
5731744Shelbigtypedef struct date date;
5831744Shelbig
5931744Shelbigstruct monthlines {
60189804Sdas	wchar_t name[MAX_WIDTH + 1];
6131744Shelbig	char lines[7][MAX_WIDTH + 1];
6231744Shelbig	char weeks[MAX_WIDTH + 1];
63204908Sedwin	unsigned int extralen[7];
6431744Shelbig};
6531744Shelbig
6631744Shelbigstruct weekdays {
67189804Sdas	wchar_t names[7][4];
6831744Shelbig};
6931744Shelbig
7031744Shelbig/* The switches from Julian to Gregorian in some countries */
7131744Shelbigstatic struct djswitch {
7295641Smarkm	const char *cc;	/* Country code according to ISO 3166 */
7395641Smarkm	const char *nm;	/* Name of country */
7431744Shelbig	date dt;	/* Last day of Julian calendar */
7531744Shelbig} switches[] = {
7653963Sache	{"AL", "Albania",       {1912, 11, 30}},
7753963Sache	{"AT", "Austria",       {1583, 10,  5}},
7853963Sache	{"AU", "Australia",     {1752,  9,  2}},
7953963Sache	{"BE", "Belgium",       {1582, 12, 14}},
8053963Sache	{"BG", "Bulgaria",      {1916,  3, 18}},
8153963Sache	{"CA", "Canada",        {1752,  9,  2}},
8253963Sache	{"CH", "Switzerland",   {1655,  2, 28}},
8353963Sache	{"CN", "China",         {1911, 12, 18}},
8453963Sache	{"CZ", "Czech Republic",{1584,  1,  6}},
8553963Sache	{"DE", "Germany",       {1700,  2, 18}},
8653963Sache	{"DK", "Denmark",       {1700,  2, 18}},
8753963Sache	{"ES", "Spain",         {1582, 10,  4}},
8853963Sache	{"FI", "Finland",       {1753,  2, 17}},
8953963Sache	{"FR", "France",        {1582, 12,  9}},
9053963Sache	{"GB", "United Kingdom",{1752,  9,  2}},
9153963Sache	{"GR", "Greece",        {1924,  3,  9}},
9253963Sache	{"HU", "Hungary",       {1587, 10, 21}},
9353963Sache	{"IS", "Iceland",       {1700, 11, 16}},
9453963Sache	{"IT", "Italy",         {1582, 10,  4}},
9553963Sache	{"JP", "Japan",         {1918, 12, 18}},
9653963Sache	{"LI", "Lithuania",     {1918,  2,  1}},
9753963Sache	{"LN", "Latin",         {9999, 05, 31}},
9853963Sache	{"LU", "Luxembourg",    {1582, 12, 14}},
9953963Sache	{"LV", "Latvia",        {1918,  2,  1}},
10053963Sache	{"NL", "Netherlands",   {1582, 12, 14}},
10153963Sache	{"NO", "Norway",        {1700,  2, 18}},
10253963Sache	{"PL", "Poland",        {1582, 10,  4}},
10353963Sache	{"PT", "Portugal",      {1582, 10,  4}},
10453963Sache	{"RO", "Romania",       {1919,  3, 31}},
10553963Sache	{"RU", "Russia",        {1918,  1, 31}},
10653963Sache	{"SI", "Slovenia",      {1919,  3,  4}},
10753963Sache	{"SW", "Sweden",        {1753,  2, 17}},
10853963Sache	{"TR", "Turkey",        {1926, 12, 18}},
10953963Sache	{"US", "United States", {1752,  9,  2}},
11053963Sache	{"YU", "Yugoslavia",    {1919,  3,  4}}
11131744Shelbig};
11231744Shelbig
11332215Shelbigstruct djswitch *dftswitch =
11432215Shelbig    switches + sizeof(switches) / sizeof(struct djswitch) - 2;
11532215Shelbig    /* default switch (should be "US") */
11632215Shelbig
11731744Shelbig/* Table used to print day of month and week numbers */
11831744Shelbigchar daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
11931744Shelbig		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
12031744Shelbig		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
12131744Shelbig		" 48 49 50 51 52 53";
12231744Shelbig
12331744Shelbig/* Table used to print day of year and week numbers */
12431744Shelbigchar jdaystr[] = "       1   2   3   4   5   6   7   8   9"
12531744Shelbig		 "  10  11  12  13  14  15  16  17  18  19"
12631744Shelbig		 "  20  21  22  23  24  25  26  27  28  29"
12731744Shelbig		 "  30  31  32  33  34  35  36  37  38  39"
12831744Shelbig		 "  40  41  42  43  44  45  46  47  48  49"
12931744Shelbig		 "  50  51  52  53  54  55  56  57  58  59"
13031744Shelbig		 "  60  61  62  63  64  65  66  67  68  69"
13131744Shelbig		 "  70  71  72  73  74  75  76  77  78  79"
13231744Shelbig		 "  80  81  82  83  84  85  86  87  88  89"
13331744Shelbig		 "  90  91  92  93  94  95  96  97  98  99"
13431744Shelbig		 " 100 101 102 103 104 105 106 107 108 109"
13531744Shelbig		 " 110 111 112 113 114 115 116 117 118 119"
13631744Shelbig		 " 120 121 122 123 124 125 126 127 128 129"
13731744Shelbig		 " 130 131 132 133 134 135 136 137 138 139"
13831744Shelbig		 " 140 141 142 143 144 145 146 147 148 149"
13931744Shelbig		 " 150 151 152 153 154 155 156 157 158 159"
14031744Shelbig		 " 160 161 162 163 164 165 166 167 168 169"
14131744Shelbig		 " 170 171 172 173 174 175 176 177 178 179"
14231744Shelbig		 " 180 181 182 183 184 185 186 187 188 189"
14331744Shelbig		 " 190 191 192 193 194 195 196 197 198 199"
14431744Shelbig		 " 200 201 202 203 204 205 206 207 208 209"
14531744Shelbig		 " 210 211 212 213 214 215 216 217 218 219"
14631744Shelbig		 " 220 221 222 223 224 225 226 227 228 229"
14731744Shelbig		 " 230 231 232 233 234 235 236 237 238 239"
14831744Shelbig		 " 240 241 242 243 244 245 246 247 248 249"
14931744Shelbig		 " 250 251 252 253 254 255 256 257 258 259"
15031744Shelbig		 " 260 261 262 263 264 265 266 267 268 269"
15131744Shelbig		 " 270 271 272 273 274 275 276 277 278 279"
15231744Shelbig		 " 280 281 282 283 284 285 286 287 288 289"
15331744Shelbig		 " 290 291 292 293 294 295 296 297 298 299"
15431744Shelbig		 " 300 301 302 303 304 305 306 307 308 309"
15531744Shelbig		 " 310 311 312 313 314 315 316 317 318 319"
15631744Shelbig		 " 320 321 322 323 324 325 326 327 328 329"
15731744Shelbig		 " 330 331 332 333 334 335 336 337 338 339"
15831744Shelbig		 " 340 341 342 343 344 345 346 347 348 349"
15931744Shelbig		 " 350 351 352 353 354 355 356 357 358 359"
16031744Shelbig		 " 360 361 362 363 364 365 366";
16131744Shelbig
162204849Sedwinint	flag_nohighlight;	/* user doesn't want a highlighted today */
16332139Shelbigint     flag_weeks;		/* user wants number of week */
16431744Shelbigint     nswitch;		/* user defined switch date */
16531744Shelbigint	nswitchb;		/* switch date for backward compatibility */
166205071Sedwinint	highlightdate;
16731744Shelbig
168204697Sedwinchar	*center(char *s, char *t, int w);
169189804Sdaswchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170205071Sedwinint	firstday(int y, int m);
171205071Sedwinvoid	highlight(char *dst, char *src, int len, int *extraletters);
172204697Sedwinvoid	mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
173204697Sedwinvoid	mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
174204697Sedwinvoid	mkweekdays(struct weekdays * wds);
175205071Sedwinvoid	monthranger(int year, int m, int jd_flag, int before, int after);
176205071Sedwinvoid	monthrangeb(int year, int m, int jd_flag, int before, int after);
177204697Sedwinint	parsemonth(const char *s, int *m, int *y);
178204697Sedwinvoid	printcc(void);
179204697Sedwinvoid	printeaster(int year, int julian, int orthodox);
180204697Sedwindate	*sdater(int ndays, struct date * d);
181204697Sedwindate	*sdateb(int ndays, struct date * d);
182204697Sedwinint	sndaysr(struct date * d);
183204697Sedwinint	sndaysb(struct date * d);
184204697Sedwinstatic void	usage(void);
18531744Shelbig
18631744Shelbigint
18731744Shelbigmain(int argc, char *argv[])
18831744Shelbig{
18931744Shelbig	struct  djswitch *p, *q;	/* to search user defined switch date */
19031744Shelbig	date	never = {10000, 1, 1};	/* outside valid range of dates */
19131744Shelbig	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
192204697Sedwin	date	dt;
19331744Shelbig	int     ch;			/* holds the option character */
19431744Shelbig	int     m = 0;			/* month */
19531744Shelbig	int	y = 0;			/* year */
19631744Shelbig	int     flag_backward = 0;	/* user called cal--backward compat. */
197205071Sedwin	int     flag_wholeyear = 0;	/* user wants the whole year */
19831744Shelbig	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
199205071Sedwin	int     flag_julian_day = 0;	/* user wants the Julian day numbers */
200205071Sedwin	int	flag_orthodox = 0;	/* user wants Orthodox easter */
201205071Sedwin	int	flag_easter = 0;	/* user wants easter date */
202205071Sedwin	int	flag_3months = 0;	/* user wants 3 month display (-3) */
203205071Sedwin	int	flag_after = 0;		/* user wants to see months after */
204205071Sedwin	int	flag_before = 0;	/* user wants to see months before */
205205071Sedwin	int	flag_specifiedmonth = 0;/* user wants to see this month (-m) */
206205071Sedwin	int	flag_givenmonth = 0;	/* user has specified month [n] */
207205071Sedwin	int	flag_givenyear = 0;	/* user has specified year [n] */
20831744Shelbig	char	*cp;			/* character pointer */
209205071Sedwin	char	*flag_today = NULL;	/* debug: use date as being today */
210138036Swollman	char	*flag_month = NULL;	/* requested month as string */
211205071Sedwin	char	*flag_highlightdate = NULL; /* debug: date to highlight */
212204697Sedwin	int	before, after;
21395641Smarkm	const char    *locale;		/* locale to get country code */
21431744Shelbig
215204849Sedwin	flag_nohighlight = 0;
216204849Sedwin	flag_weeks = 0;
217191330Srdivacky
21832215Shelbig	/*
21932215Shelbig	 * Use locale to determine the country code,
22032215Shelbig	 * and use the country code to determine the default
22132215Shelbig	 * switchdate and date format from the switches table.
22232215Shelbig	 */
22374612Sache	if (setlocale(LC_ALL, "") == NULL)
22432215Shelbig		warn("setlocale");
22574612Sache	locale = setlocale(LC_TIME, NULL);
22678016Sache	if (locale == NULL ||
22778016Sache	    strcmp(locale, "C") == 0 ||
22878027Sache	    strcmp(locale, "POSIX") == 0 ||
22978027Sache	    strcmp(locale, "ASCII") == 0 ||
23078027Sache	    strcmp(locale, "US-ASCII") == 0)
23132215Shelbig		locale = "_US";
23232215Shelbig	q = switches + sizeof(switches) / sizeof(struct djswitch);
23332215Shelbig	for (p = switches; p != q; p++)
23432215Shelbig		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
23532215Shelbig			break;
23632215Shelbig	if (p == q) {
23732215Shelbig		nswitch = ndaysj(&dftswitch->dt);
23832215Shelbig	} else {
23932215Shelbig		nswitch = ndaysj(&p->dt);
24032215Shelbig		dftswitch = p;
24132215Shelbig	}
24231744Shelbig
24332215Shelbig
24431744Shelbig	/*
24531744Shelbig	 * Get the filename portion of argv[0] and set flag_backward if
24631744Shelbig	 * this program is called "cal".
24731744Shelbig	 */
248138036Swollman	cp = strrchr(argv[0], '/');
249138036Swollman	cp = (cp == NULL) ? argv[0] : cp + 1;
250138036Swollman	if (strcmp("cal", cp) == 0)
25131744Shelbig		flag_backward = 1;
25231744Shelbig
25331744Shelbig	/* Set the switch date to United Kingdom if backwards compatible */
25431744Shelbig	if (flag_backward)
25531744Shelbig		nswitchb = ndaysj(&ukswitch);
25631744Shelbig
257204697Sedwin	before = after = -1;
258204697Sedwin
259205427Sedwin	while ((ch = getopt(argc, argv, "3A:B:Cd:eH:hjJm:Nops:wy")) != -1)
26031744Shelbig		switch (ch) {
261204697Sedwin		case '3':
262205071Sedwin			flag_3months = 1;
263204697Sedwin			break;
264204697Sedwin		case 'A':
265205071Sedwin			if (flag_after > 0)
266205071Sedwin				errx(EX_USAGE, "Double -A specified");
267205071Sedwin			flag_after = strtol(optarg, NULL, 10);
268205071Sedwin			if (flag_after <= 0)
269205071Sedwin				errx(EX_USAGE,
270205071Sedwin				    "Argument to -A must be positive");
271204697Sedwin			break;
272204697Sedwin		case 'B':
273205071Sedwin			if (flag_before > 0)
274205071Sedwin				errx(EX_USAGE, "Double -A specified");
275205071Sedwin			flag_before = strtol(optarg, NULL, 10);
276205071Sedwin			if (flag_before <= 0)
277205071Sedwin				errx(EX_USAGE,
278205071Sedwin				    "Argument to -B must be positive");
279204697Sedwin			break;
28031744Shelbig		case 'J':
28131744Shelbig			if (flag_backward)
28231744Shelbig				usage();
28331744Shelbig			nswitch = ndaysj(&never);
28431744Shelbig			flag_julian_cal = 1;
28531744Shelbig			break;
286205427Sedwin		case 'C':
287204697Sedwin			flag_backward = 1;
288204697Sedwin			break;
289205427Sedwin		case 'N':
290205427Sedwin			flag_backward = 0;
291205427Sedwin			break;
292204697Sedwin		case 'd':
293205071Sedwin			flag_today = optarg;
294205071Sedwin			break;
295205071Sedwin		case 'H':
296204697Sedwin			flag_highlightdate = optarg;
297204697Sedwin			break;
298191364Srdivacky		case 'h':
299204849Sedwin			flag_nohighlight = 1;
300191364Srdivacky			break;
30131744Shelbig		case 'e':
30231744Shelbig			if (flag_backward)
30331744Shelbig				usage();
30431744Shelbig			flag_easter = 1;
30531744Shelbig			break;
30631744Shelbig		case 'j':
30731744Shelbig			flag_julian_day = 1;
30831744Shelbig			break;
309138036Swollman		case 'm':
310205071Sedwin			if (flag_specifiedmonth)
311205071Sedwin				errx(EX_USAGE, "Double -m specified");
312138036Swollman			flag_month = optarg;
313205071Sedwin			flag_specifiedmonth = 1;
314138036Swollman			break;
31531744Shelbig		case 'o':
31631744Shelbig			if (flag_backward)
31731744Shelbig				usage();
31831744Shelbig			flag_orthodox = 1;
31931744Shelbig			flag_easter = 1;
32031744Shelbig			break;
32131744Shelbig		case 'p':
32231744Shelbig			if (flag_backward)
32331744Shelbig				usage();
32431744Shelbig			printcc();
32531744Shelbig			return (0);
32631744Shelbig			break;
32731744Shelbig		case 's':
32831744Shelbig			if (flag_backward)
32931744Shelbig				usage();
33031744Shelbig			q = switches +
33131744Shelbig			    sizeof(switches) / sizeof(struct djswitch);
33231744Shelbig			for (p = switches;
33331744Shelbig			     p != q && strcmp(p->cc, optarg) != 0; p++)
33431744Shelbig				;
33531744Shelbig			if (p == q)
33631744Shelbig				errx(EX_USAGE,
33731744Shelbig				    "%s: invalid country code", optarg);
33831744Shelbig			nswitch = ndaysj(&(p->dt));
33931744Shelbig			break;
34031744Shelbig		case 'w':
34131744Shelbig			if (flag_backward)
34231744Shelbig				usage();
34331744Shelbig			flag_weeks = 1;
34431744Shelbig			break;
34531744Shelbig		case 'y':
346205071Sedwin			flag_wholeyear = 1;
34731744Shelbig			break;
34831744Shelbig		default:
34931744Shelbig			usage();
35031744Shelbig		}
35131744Shelbig
35231744Shelbig	argc -= optind;
35331744Shelbig	argv += optind;
35431744Shelbig
35531744Shelbig	switch (argc) {
35631744Shelbig	case 2:
35731744Shelbig		if (flag_easter)
35831744Shelbig			usage();
359138036Swollman		flag_month = *argv++;
360205071Sedwin		flag_givenmonth = 1;
361204908Sedwin		m = strtol(flag_month, NULL, 10);
36231744Shelbig		/* FALLTHROUGH */
36331744Shelbig	case 1:
364205071Sedwin		y = atoi(*argv);
36531744Shelbig		if (y < 1 || y > 9999)
366205071Sedwin			errx(EX_USAGE, "year `%s' not in range 1..9999", *argv);
367205071Sedwin		argv++;
368205071Sedwin		flag_givenyear = 1;
36931744Shelbig		break;
37031744Shelbig	case 0:
371205071Sedwin		if (flag_today != NULL) {
372205071Sedwin			y = strtol(flag_today, NULL, 10);
373205071Sedwin			m = strtol(flag_today + 5, NULL, 10);
374205071Sedwin		} else {
375138036Swollman			time_t t;
376138036Swollman			struct tm *tm;
377138036Swollman
378138036Swollman			t = time(NULL);
379138036Swollman			tm = localtime(&t);
380138036Swollman			y = tm->tm_year + 1900;
381138036Swollman			m = tm->tm_mon + 1;
382138036Swollman		}
38331744Shelbig		break;
38431744Shelbig	default:
38531744Shelbig		usage();
38631744Shelbig	}
38731744Shelbig
388138036Swollman	if (flag_month != NULL) {
389186401Swollman		if (parsemonth(flag_month, &m, &y)) {
390138036Swollman			errx(EX_USAGE,
391138036Swollman			    "%s is neither a month number (1..12) nor a name",
392138036Swollman			    flag_month);
393186401Swollman		}
394138036Swollman	}
395138036Swollman
396205071Sedwin	/*
397205071Sedwin	 * What is not supported:
398205071Sedwin	 * -3 with -A or -B
399205071Sedwin	 *	-3 displays 3 months, -A and -B change that behaviour.
400205071Sedwin	 * -3 with -y
401205071Sedwin	 *	-3 displays 3 months, -y says display a whole year.
402205071Sedwin	 * -3 with a given year but no given month or without -m
403205071Sedwin	 *	-3 displays 3 months, no month specified doesn't make clear
404205071Sedwin	 *      which three months.
405205071Sedwin	 * -m with a given month
406205071Sedwin	 *	conflicting arguments, both specify the same field.
407205071Sedwin	 * -y with -m
408205071Sedwin	 *	-y displays the whole year, -m displays a single month.
409205071Sedwin	 * -y with a given month
410205071Sedwin	 *	-y displays the whole year, the given month displays a single
411205071Sedwin	 *	month.
412205071Sedwin	 * -y with -A or -B
413205071Sedwin	 *	-y displays the whole year, -A and -B display extra months.
414205071Sedwin	 */
415205071Sedwin
416205071Sedwin	/* -3 together with -A or -B. */
417205071Sedwin	if (flag_3months && (flag_after || flag_before))
418205071Sedwin		errx(EX_USAGE, "-3 together with -A and -B is not supported.");
419205071Sedwin	/* -3 together with -y. */
420205071Sedwin	if (flag_3months && flag_wholeyear)
421205071Sedwin		errx(EX_USAGE, "-3 together with -y is not supported.");
422205071Sedwin	/* -3 together with givenyear but no givenmonth. */
423205071Sedwin	if (flag_3months && flag_givenyear &&
424205071Sedwin	    !(flag_givenmonth || flag_specifiedmonth))
425205071Sedwin		errx(EX_USAGE,
426205071Sedwin		    "-3 together with a given year but no given month is "
427205071Sedwin		    "not supported.");
428205071Sedwin	/* -m together with xx xxxx. */
429205071Sedwin	if (flag_specifiedmonth && flag_givenmonth)
430205071Sedwin		errx(EX_USAGE,
431205071Sedwin		    "-m together with a given month is not supported.");
432205071Sedwin	/* -y together with -m. */
433205071Sedwin	if (flag_wholeyear && flag_specifiedmonth)
434205071Sedwin		errx(EX_USAGE, "-y together with -m is not supported.");
435205071Sedwin	/* -y together with xx xxxx. */
436205071Sedwin	if (flag_wholeyear && flag_givenmonth)
437205071Sedwin		errx(EX_USAGE, "-y together a given month is not supported.");
438205071Sedwin	/* -y together with -A or -B. */
439205071Sedwin	if (flag_wholeyear && (flag_before > 0 || flag_after > 0))
440205071Sedwin		errx(EX_USAGE, "-y together a -A or -B is not supported.");
441205071Sedwin	/* The rest should be fine. */
442205071Sedwin
443205071Sedwin	/* Select the period to display, in order of increasing priority .*/
444205071Sedwin	if (flag_wholeyear ||
445205071Sedwin	    (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) {
446205071Sedwin		m = 1;
447205071Sedwin		before = 0;
448205071Sedwin		after = 11;
449205071Sedwin	}
450205071Sedwin	if (flag_givenyear && flag_givenmonth) {
451205071Sedwin		before = 0;
452205071Sedwin		after = 0;
453205071Sedwin	}
454205071Sedwin	if (flag_specifiedmonth) {
455205071Sedwin		before = 0;
456205071Sedwin		after = 0;
457205071Sedwin	}
458205071Sedwin	if (flag_before) {
459205071Sedwin		before = flag_before;
460205071Sedwin	}
461205071Sedwin	if (flag_after) {
462205071Sedwin		after = flag_after;
463205071Sedwin	}
464205071Sedwin	if (flag_3months) {
465205071Sedwin		before = 1;
466205071Sedwin		after = 1;
467205071Sedwin	}
468205071Sedwin	if (after == -1)
469205071Sedwin		after = 0;
470205071Sedwin	if (before == -1)
471205071Sedwin		before = 0;
472205071Sedwin
473205071Sedwin	/* Highlight a specified day or today .*/
474204697Sedwin	if (flag_highlightdate != NULL) {
475204697Sedwin		dt.y = strtol(flag_highlightdate, NULL, 10);
476204697Sedwin		dt.m = strtol(flag_highlightdate + 5, NULL, 10);
477204697Sedwin		dt.d = strtol(flag_highlightdate + 8, NULL, 10);
478204697Sedwin	} else {
479204697Sedwin		time_t t;
480204697Sedwin		struct tm *tm1;
481204697Sedwin
482204697Sedwin		t = time(NULL);
483204697Sedwin		tm1 = localtime(&t);
484204697Sedwin		dt.y = tm1->tm_year + 1900;
485204697Sedwin		dt.m = tm1->tm_mon + 1;
486204697Sedwin		dt.d = tm1->tm_mday;
487204697Sedwin	}
488205071Sedwin	highlightdate = sndaysb(&dt);
489204697Sedwin
490205071Sedwin	/* And now we finally start to calculate and output calendars. */
49131744Shelbig	if (flag_easter)
49231744Shelbig		printeaster(y, flag_julian_cal, flag_orthodox);
493204697Sedwin	else
49431744Shelbig		if (flag_backward)
495205071Sedwin			monthrangeb(y, m, flag_julian_day, before, after);
49631744Shelbig		else
497205071Sedwin			monthranger(y, m, flag_julian_day, before, after);
49831744Shelbig	return (0);
49931744Shelbig}
50031744Shelbig
50132310Scharnierstatic void
50231744Shelbigusage(void)
50331744Shelbig{
50431744Shelbig
505138036Swollman	fputs(
506205427Sedwin"Usage: cal [general options] [-hjy] [[month] year]\n"
507205427Sedwin"       cal [general options] [-hj] [-m month] [year]\n"
508205427Sedwin"       ncal [general options] [-hJjpwy] [-s country_code] [[month] year]\n"
509205427Sedwin"       ncal [general options] [-hJeo] [year]\n"
510205427Sedwin"General options: [-NC3] [-A months] [-B months]\n"
511205427Sedwin"For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]\n",
512204697Sedwin	    stderr);
51331744Shelbig	exit(EX_USAGE);
51431744Shelbig}
51531744Shelbig
516205071Sedwin/* Print the assumed switches for all countries. */
51731744Shelbigvoid
51831744Shelbigprintcc(void)
51931744Shelbig{
52031744Shelbig	struct djswitch *p;
52131744Shelbig	int n;	/* number of lines to print */
52231744Shelbig	int m;	/* offset from left to right table entry on the same line */
52331744Shelbig
52432215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d"
52532215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
52632215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
52731744Shelbig
52831744Shelbig	n = sizeof(switches) / sizeof(struct djswitch);
52931744Shelbig	m = (n + 1) / 2;
53031744Shelbig	n /= 2;
53132215Shelbig	for (p = switches; p != switches + n; p++)
53232215Shelbig		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
53331744Shelbig	if (m != n)
53432215Shelbig		printf(FSTR"\n", FSTRARG(p));
53531744Shelbig}
53631744Shelbig
537205071Sedwin/* Print the date of easter sunday. */
53831744Shelbigvoid
53931744Shelbigprinteaster(int y, int julian, int orthodox)
54031744Shelbig{
54132215Shelbig	date    dt;
54232215Shelbig	struct tm tm;
543204697Sedwin	char    buf[MAX_WIDTH];
54474573Sache	static int d_first = -1;
54531744Shelbig
54674573Sache	if (d_first < 0)
54774573Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
54831744Shelbig	/* force orthodox easter for years before 1583 */
54931744Shelbig	if (y < 1583)
55031744Shelbig		orthodox = 1;
55131744Shelbig
55231744Shelbig	if (orthodox)
55331744Shelbig		if (julian)
55431744Shelbig			easteroj(y, &dt);
55531744Shelbig		else
55631744Shelbig			easterog(y, &dt);
55731744Shelbig	else
55831744Shelbig		easterg(y, &dt);
55932215Shelbig
56032215Shelbig	memset(&tm, 0, sizeof(tm));
56132215Shelbig	tm.tm_year = dt.y - 1900;
56232215Shelbig	tm.tm_mon  = dt.m - 1;
56332215Shelbig	tm.tm_mday = dt.d;
56474573Sache	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
56532215Shelbig	printf("%s\n", buf);
56631744Shelbig}
56731744Shelbig
568204908Sedwin#define MW(mw, me)		((mw) + me)
569204697Sedwin#define	DECREASEMONTH(m, y) 		\
570204697Sedwin		if (--m == 0) {		\
571204697Sedwin			m = 12;		\
572204697Sedwin			y--;		\
573204697Sedwin		}
574204697Sedwin#define	INCREASEMONTH(m, y)		\
575204697Sedwin		if (++(m) == 13) {	\
576204697Sedwin			(m) = 1;	\
577204697Sedwin			(y)++;		\
578204697Sedwin		}
579204697Sedwin#define	M2Y(m)	((m) / 12)
580204697Sedwin#define	M2M(m)	(1 + (m) % 12)
581204697Sedwin
582205071Sedwin/* Print all months for the period in the range [ before .. y-m .. after ]. */
58331744Shelbigvoid
584205071Sedwinmonthrangeb(int y, int m, int jd_flag, int before, int after)
58531744Shelbig{
586204697Sedwin	struct monthlines year[12];
58731744Shelbig	struct weekdays wds;
588204697Sedwin	char	s[MAX_WIDTH], t[MAX_WIDTH];
589204697Sedwin	wchar_t	ws[MAX_WIDTH], ws1[MAX_WIDTH];
590204697Sedwin	const char	*wdss;
591204697Sedwin	int     i, j;
592204697Sedwin	int     mpl;
593204697Sedwin	int     mw;
594204697Sedwin	int	m1, m2;
595204697Sedwin	int	printyearheader;
596204697Sedwin	int	prevyear = -1;
59731744Shelbig
598204697Sedwin	mpl = jd_flag ? 2 : 3;
599204697Sedwin	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
600204697Sedwin	wdss = (mpl == 2) ? " " : "";
601204697Sedwin
602204697Sedwin	while (before != 0) {
603204697Sedwin		DECREASEMONTH(m, y);
604204697Sedwin		before--;
605204697Sedwin		after++;
606194366Sume	}
607204697Sedwin	m1 = y * 12 + m - 1;
608204697Sedwin	m2 = m1 + after;
60931744Shelbig
61031744Shelbig	mkweekdays(&wds);
61131744Shelbig
612204697Sedwin	/*
613204697Sedwin	 * The year header is printed when there are more than 'mpl' months
614204697Sedwin	 * and if the first month is a multitude of 'mpl'.
615204697Sedwin	 * If not, it will print the year behind every month.
616204697Sedwin	 */
617204697Sedwin	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
61831744Shelbig
619204697Sedwin	m = m1;
620204697Sedwin	while (m <= m2) {
621204697Sedwin		int count = 0;
622204697Sedwin		for (i = 0; i != mpl && m + i <= m2; i++) {
623204697Sedwin			mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
624204697Sedwin			count++;
625204697Sedwin		}
62631744Shelbig
627204697Sedwin		/* Empty line between two rows of months */
628204697Sedwin		if (m != m1)
629204697Sedwin			printf("\n");
63031744Shelbig
631205071Sedwin		/* Year at the top. */
632204697Sedwin		if (printyearheader && M2Y(m) != prevyear) {
633204697Sedwin			sprintf(s, "%d", M2Y(m));
634204697Sedwin			printf("%s\n", center(t, s, mpl * mw));
635204697Sedwin			prevyear = M2Y(m);
636204697Sedwin		}
63731744Shelbig
638205071Sedwin		/* Month names. */
639204697Sedwin		for (i = 0; i < count; i++)
640204697Sedwin			if (printyearheader)
641204697Sedwin				wprintf(L"%-*ls  ",
642204697Sedwin				    mw, wcenter(ws, year[i].name, mw));
643204697Sedwin			else {
644204697Sedwin				swprintf(ws, sizeof(ws), L"%-ls %d",
645204697Sedwin				    year[i].name, M2Y(m + i));
646204697Sedwin				wprintf(L"%-*ls  ", mw, wcenter(ws1, ws, mw));
647204697Sedwin			}
648204697Sedwin		printf("\n");
64931744Shelbig
650205071Sedwin		/* Day of the week names. */
651204697Sedwin		for (i = 0; i < count; i++) {
652204697Sedwin			wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
653204697Sedwin				wdss, wds.names[6], wdss, wds.names[0],
654204697Sedwin				wdss, wds.names[1], wdss, wds.names[2],
655204697Sedwin				wdss, wds.names[3], wdss, wds.names[4],
656204697Sedwin				wdss, wds.names[5]);
657204697Sedwin		}
658204697Sedwin		printf("\n");
65931744Shelbig
660205071Sedwin		/* And the days of the month. */
661204697Sedwin		for (i = 0; i != 6; i++) {
662204697Sedwin			for (j = 0; j < count; j++)
663204908Sedwin				printf("%-*s  ",
664204908Sedwin				    MW(mw, year[j].extralen[i]),
665204908Sedwin					year[j].lines[i]+1);
666204697Sedwin			printf("\n");
667204697Sedwin		}
66831744Shelbig
669204697Sedwin		m += mpl;
67031744Shelbig	}
67131744Shelbig}
67231744Shelbig
67331744Shelbigvoid
674205071Sedwinmonthranger(int y, int m, int jd_flag, int before, int after)
67531744Shelbig{
67631744Shelbig	struct monthlines year[12];
67731744Shelbig	struct weekdays wds;
678204697Sedwin	char    s[MAX_WIDTH], t[MAX_WIDTH];
67931744Shelbig	int     i, j;
68031744Shelbig	int     mpl;
68131744Shelbig	int     mw;
682204697Sedwin	int	m1, m2;
683204697Sedwin	int	prevyear = -1;
684204697Sedwin	int	printyearheader;
68531744Shelbig
686204697Sedwin	mpl = jd_flag ? 3 : 4;
687204697Sedwin	mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R;
688204697Sedwin
689204697Sedwin	while (before != 0) {
690204697Sedwin		DECREASEMONTH(m, y);
691204697Sedwin		before--;
692204697Sedwin		after++;
693204697Sedwin	}
694204697Sedwin	m1 = y * 12 + m - 1;
695204697Sedwin	m2 = m1 + after;
696204697Sedwin
69731744Shelbig	mkweekdays(&wds);
69831744Shelbig
699204697Sedwin	/*
700204697Sedwin	 * The year header is printed when there are more than 'mpl' months
701204697Sedwin	 * and if the first month is a multitude of 'mpl'.
702204697Sedwin	 * If not, it will print the year behind every month.
703204697Sedwin	 */
704204697Sedwin	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
70531744Shelbig
706204697Sedwin	m = m1;
707204697Sedwin	while (m <= m2) {
708204697Sedwin		int count = 0;
709204697Sedwin		for (i = 0; i != mpl && m + i <= m2; i++) {
710204697Sedwin			mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
711204697Sedwin			count++;
712204697Sedwin		}
71331744Shelbig
714205071Sedwin		/* Empty line between two rows of months. */
715204697Sedwin		if (m != m1)
716204697Sedwin			printf("\n");
717204697Sedwin
718205071Sedwin		/* Year at the top. */
719204697Sedwin		if (printyearheader && M2Y(m) != prevyear) {
720204697Sedwin			sprintf(s, "%d", M2Y(m));
721204697Sedwin			printf("%s\n", center(t, s, mpl * mw));
722204697Sedwin			prevyear = M2Y(m);
723204697Sedwin		}
724204697Sedwin
725205071Sedwin		/* Month names. */
726204697Sedwin		wprintf(L"    ");
727204697Sedwin		for (i = 0; i < count; i++)
728204697Sedwin			if (printyearheader)
729204697Sedwin				wprintf(L"%-*ls", mw, year[i].name);
73031744Shelbig			else
731204697Sedwin				wprintf(L"%-ls %-*d", year[i].name,
732204697Sedwin				    mw - wcslen(year[i].name) - 1, M2Y(m + i));
733204697Sedwin		printf("\n");
73431744Shelbig
735205071Sedwin		/* And the days of the month. */
736204697Sedwin		for (i = 0; i != 7; i++) {
737204697Sedwin			/* Week day */
738204697Sedwin			wprintf(L"%.2ls", wds.names[i]);
739204697Sedwin
740204697Sedwin			/* Full months */
741204697Sedwin			for (j = 0; j < count; j++)
742204697Sedwin				printf("%-*s",
743204908Sedwin				    MW(mw, year[j].extralen[i]),
744204908Sedwin					year[j].lines[i]);
745204697Sedwin			printf("\n");
74631744Shelbig		}
747204697Sedwin
748205071Sedwin		/* Week numbers. */
749204697Sedwin		if (flag_weeks) {
750204697Sedwin			printf("  ");
751204697Sedwin			for (i = 0; i < count; i++)
752204697Sedwin				printf("%-*s", mw, year[i].weeks);
753204697Sedwin			printf("\n");
754204697Sedwin		}
755204697Sedwin
756204697Sedwin		m += mpl;
75731744Shelbig	}
758204697Sedwin	return;
75931744Shelbig}
76031744Shelbig
76131744Shelbigvoid
762204697Sedwinmkmonthr(int y, int m, int jd_flag, struct monthlines *mlines)
76331744Shelbig{
76431744Shelbig
76531744Shelbig	struct tm tm;		/* for strftime printing local names of
76631744Shelbig				 * months */
76731744Shelbig	date    dt;		/* handy date */
76831744Shelbig	int     dw;		/* width of numbers */
76931744Shelbig	int     first;		/* first day of month */
77031744Shelbig	int     firstm;		/* first day of first week of month */
771191330Srdivacky	int     i, j, k, l;	/* just indices */
77231744Shelbig	int     last;		/* the first day of next month */
77331744Shelbig	int     jan1 = 0;	/* the first day of this year */
77431744Shelbig	char   *ds;		/* pointer to day strings (daystr or
77531744Shelbig				 * jdaystr) */
77631744Shelbig
77731744Shelbig	/* Set name of month. */
77831744Shelbig	memset(&tm, 0, sizeof(tm));
77931744Shelbig	tm.tm_mon = m;
780189804Sdas	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
781189804Sdas		 L"%OB", &tm);
782189804Sdas	mlines->name[0] = towupper(mlines->name[0]);
78331744Shelbig
78431744Shelbig	/*
78531744Shelbig	 * Set first and last to the day number of the first day of this
78631744Shelbig	 * month and the first day of next month respectively. Set jan1 to
78732282Shelbig	 * the day number of the first day of this year.
78831744Shelbig	 */
78932282Shelbig	first = firstday(y, m + 1);
79032282Shelbig	if (m == 11)
79132282Shelbig		last = firstday(y + 1, 1);
79232282Shelbig	else
79332282Shelbig		last = firstday(y, m + 2);
79431744Shelbig
79532282Shelbig	if (jd_flag)
79632525Shelbig		jan1 = firstday(y, 1);
79731744Shelbig
79831744Shelbig	/*
79931744Shelbig	 * Set firstm to the day number of monday of the first week of
80031744Shelbig	 * this month. (This might be in the last month)
80131744Shelbig	 */
80231744Shelbig	firstm = first - weekday(first);
80331744Shelbig
804205071Sedwin	/* Set ds (daystring) and dw (daywidth) according to the jd_flag. */
80531744Shelbig	if (jd_flag) {
80631744Shelbig		ds = jdaystr;
80731744Shelbig		dw = 4;
80831744Shelbig	} else {
80931744Shelbig		ds = daystr;
81031744Shelbig		dw = 3;
81131744Shelbig	}
81231744Shelbig
81331744Shelbig	/*
81431744Shelbig	 * Fill the lines with day of month or day of year (julian day)
81531744Shelbig	 * line index: i, each line is one weekday. column index: j, each
81631744Shelbig	 * column is one day number. print column index: k.
81731744Shelbig	 */
81831744Shelbig	for (i = 0; i != 7; i++) {
819191330Srdivacky		l = 0;
820191330Srdivacky		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
82131744Shelbig			if (j >= first) {
82231744Shelbig				if (jd_flag)
82331744Shelbig					dt.d = j - jan1 + 1;
82431744Shelbig				else
825204697Sedwin					sdater(j, &dt);
826205071Sedwin				if (j == highlightdate && !flag_nohighlight)
827204849Sedwin					highlight(mlines->lines[i] + k,
828204849Sedwin					    ds + dt.d * dw, dw, &l);
829204849Sedwin				else
830204849Sedwin					memcpy(mlines->lines[i] + k + l,
831204849Sedwin					       ds + dt.d * dw, dw);
83231744Shelbig			} else
833191330Srdivacky				memcpy(mlines->lines[i] + k + l, "    ", dw);
834191330Srdivacky		}
835191330Srdivacky		mlines->lines[i][k + l] = '\0';
836204908Sedwin		mlines->extralen[i] = l;
83731744Shelbig	}
83831744Shelbig
839205071Sedwin	/* fill the weeknumbers. */
84031744Shelbig	if (flag_weeks) {
84131744Shelbig		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
84231744Shelbig			if (j <= nswitch)
84331744Shelbig				memset(mlines->weeks + k, ' ', dw);
84431744Shelbig			else
84531744Shelbig				memcpy(mlines->weeks + k,
84631744Shelbig				    ds + week(j, &i)*dw, dw);
84731744Shelbig		mlines->weeks[k] = '\0';
84831744Shelbig	}
84931744Shelbig}
85031744Shelbig
85131744Shelbigvoid
85231744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
85331744Shelbig{
85431744Shelbig
85531744Shelbig	struct tm tm;		/* for strftime printing local names of
85631744Shelbig				 * months */
85731744Shelbig	date    dt;		/* handy date */
85831744Shelbig	int     dw;		/* width of numbers */
85931744Shelbig	int     first;		/* first day of month */
86031744Shelbig	int     firsts;		/* sunday of first week of month */
861191330Srdivacky	int     i, j, k, l;	/* just indices */
86231744Shelbig	int     jan1 = 0;	/* the first day of this year */
86331744Shelbig	int     last;		/* the first day of next month */
86431744Shelbig	char   *ds;		/* pointer to day strings (daystr or
86531744Shelbig				 * jdaystr) */
86631744Shelbig
86731744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
86831744Shelbig	if (jd_flag) {
86931744Shelbig		ds = jdaystr;
87031744Shelbig		dw = 4;
87131744Shelbig	} else {
87231744Shelbig		ds = daystr;
87331744Shelbig		dw = 3;
87431744Shelbig	}
87531744Shelbig
876205071Sedwin	/* Set name of month centered. */
87731744Shelbig	memset(&tm, 0, sizeof(tm));
87831744Shelbig	tm.tm_mon = m;
879189804Sdas	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
880189804Sdas		 L"%OB", &tm);
881189804Sdas	mlines->name[0] = towupper(mlines->name[0]);
88231744Shelbig
88331744Shelbig	/*
88431744Shelbig	 * Set first and last to the day number of the first day of this
88531744Shelbig	 * month and the first day of next month respectively. Set jan1 to
88631744Shelbig	 * the day number of Jan 1st of this year.
88731744Shelbig	 */
88831744Shelbig	dt.y = y;
88931744Shelbig	dt.m = m + 1;
89031744Shelbig	dt.d = 1;
89131744Shelbig	first = sndaysb(&dt);
89231744Shelbig	if (m == 11) {
89331744Shelbig		dt.y = y + 1;
89431744Shelbig		dt.m = 1;
89531744Shelbig		dt.d = 1;
89631744Shelbig	} else {
89731744Shelbig		dt.y = y;
89831744Shelbig		dt.m = m + 2;
89931744Shelbig		dt.d = 1;
90031744Shelbig	}
90131744Shelbig	last = sndaysb(&dt);
90231744Shelbig
90331744Shelbig	if (jd_flag) {
90431744Shelbig		dt.y = y;
90531744Shelbig		dt.m = 1;
90631744Shelbig		dt.d = 1;
90731744Shelbig		jan1 = sndaysb(&dt);
90831744Shelbig	}
90931744Shelbig
91031744Shelbig	/*
91131744Shelbig	 * Set firsts to the day number of sunday of the first week of
91231744Shelbig	 * this month. (This might be in the last month)
91331744Shelbig	 */
91431744Shelbig	firsts = first - (weekday(first)+1) % 7;
91531744Shelbig
91631744Shelbig	/*
91731744Shelbig	 * Fill the lines with day of month or day of year (Julian day)
91831744Shelbig	 * line index: i, each line is one week. column index: j, each
91931744Shelbig	 * column is one day number. print column index: k.
92031744Shelbig	 */
92131744Shelbig	for (i = 0; i != 6; i++) {
922191330Srdivacky		l = 0;
92331744Shelbig		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
924191330Srdivacky		    j++, k += dw) {
925204849Sedwin			if (j >= first) {
926191330Srdivacky				if (jd_flag)
927191330Srdivacky					dt.d = j - jan1 + 1;
928191330Srdivacky				else
929191330Srdivacky					sdateb(j, &dt);
930205071Sedwin				if (j == highlightdate && !flag_nohighlight)
931204849Sedwin					highlight(mlines->lines[i] + k,
932204849Sedwin					    ds + dt.d * dw, dw, &l);
93331744Shelbig				else
934204849Sedwin					memcpy(mlines->lines[i] + k + l,
935204849Sedwin					       ds + dt.d * dw, dw);
93631744Shelbig			} else
937191330Srdivacky				memcpy(mlines->lines[i] + k + l, "    ", dw);
938191330Srdivacky		}
93931744Shelbig		if (k == 0)
94031744Shelbig			mlines->lines[i][1] = '\0';
94131744Shelbig		else
942191330Srdivacky			mlines->lines[i][k + l] = '\0';
943204908Sedwin		mlines->extralen[i] = l;
94431744Shelbig	}
94531744Shelbig}
94631744Shelbig
947205071Sedwin/* Put the local names of weekdays into the wds. */
94832139Shelbigvoid
94932139Shelbigmkweekdays(struct weekdays *wds)
95031744Shelbig{
951194366Sume	int i, len, width = 0;
95231744Shelbig	struct tm tm;
953189804Sdas	wchar_t buf[20];
95431744Shelbig
95531744Shelbig	memset(&tm, 0, sizeof(tm));
95631744Shelbig
95731744Shelbig	for (i = 0; i != 7; i++) {
95831744Shelbig		tm.tm_wday = (i+1) % 7;
959189804Sdas		wcsftime(buf, sizeof(buf), L"%a", &tm);
960194447Sume		for (len = 2; len > 0; --len) {
961194366Sume			if ((width = wcswidth(buf, len)) <= 2)
962194366Sume				break;
963194366Sume		}
964194366Sume		wmemset(wds->names[i], L'\0', 4);
965194366Sume		if (width == 1)
966194366Sume			wds->names[i][0] = L' ';
967194366Sume		wcsncat(wds->names[i], buf, len);
968194447Sume		wcsncat(wds->names[i], L" ", 1);
96931744Shelbig	}
97031744Shelbig}
97131744Shelbig
97231744Shelbig/*
973205071Sedwin * Compute the day number of the first existing date after the first day in
974205071Sedwin * month. (the first day in month and even the month might not exist!)
97532282Shelbig */
97632282Shelbigint
97732282Shelbigfirstday(int y, int m)
97832282Shelbig{
97932282Shelbig	date dt;
98032282Shelbig	int nd;
98132282Shelbig
98232282Shelbig	dt.y = y;
98332282Shelbig	dt.m = m;
98432282Shelbig	dt.d = 1;
985204697Sedwin	nd = sndaysr(&dt);
98645064Shelbig	for (;;) {
987204697Sedwin		sdater(nd, &dt);
98845064Shelbig		if ((dt.m >= m && dt.y == y) || dt.y > y)
98945064Shelbig			return (nd);
99045064Shelbig		else
99145064Shelbig			nd++;
99245064Shelbig	}
99345064Shelbig	/* NEVER REACHED */
99432282Shelbig}
99532282Shelbig
99632282Shelbig/*
99731744Shelbig * Compute the number of days from date, obey the local switch from
99831744Shelbig * Julian to Gregorian if specified by the user.
99931744Shelbig */
100031744Shelbigint
1001204697Sedwinsndaysr(struct date *d)
100231744Shelbig{
100331744Shelbig
100431744Shelbig	if (nswitch != 0)
100531744Shelbig		if (nswitch < ndaysj(d))
100631744Shelbig			return (ndaysg(d));
100731744Shelbig		else
100831744Shelbig			return (ndaysj(d));
100931744Shelbig	else
101031744Shelbig		return ndaysg(d);
101131744Shelbig}
101231744Shelbig
101331744Shelbig/*
101431744Shelbig * Compute the number of days from date, obey the switch from
101531744Shelbig * Julian to Gregorian as used by UK and her colonies.
101631744Shelbig */
101731744Shelbigint
101831744Shelbigsndaysb(struct date *d)
101931744Shelbig{
102031744Shelbig
102131744Shelbig	if (nswitchb < ndaysj(d))
102231744Shelbig		return (ndaysg(d));
102331744Shelbig	else
102431744Shelbig		return (ndaysj(d));
102531744Shelbig}
102631744Shelbig
1027205071Sedwin/* Inverse of sndays. */
102831744Shelbigstruct date *
1029204697Sedwinsdater(int nd, struct date *d)
103031744Shelbig{
103131744Shelbig
103231744Shelbig	if (nswitch < nd)
103331744Shelbig		return (gdate(nd, d));
103431744Shelbig	else
103531744Shelbig		return (jdate(nd, d));
103631744Shelbig}
103731744Shelbig
1038205071Sedwin/* Inverse of sndaysb. */
103931744Shelbigstruct date *
104031744Shelbigsdateb(int nd, struct date *d)
104131744Shelbig{
104231744Shelbig
104331744Shelbig	if (nswitchb < nd)
104431744Shelbig		return (gdate(nd, d));
104531744Shelbig	else
104631744Shelbig		return (jdate(nd, d));
104731744Shelbig}
104831744Shelbig
1049205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */
105031744Shelbigchar *
105131744Shelbigcenter(char *s, char *t, int w)
105231744Shelbig{
1053204697Sedwin	char blanks[MAX_WIDTH];
105431744Shelbig
105531744Shelbig	memset(blanks, ' ', sizeof(blanks));
105631744Shelbig	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
105731744Shelbig	return (s);
105831744Shelbig}
1059104369Sroam
1060205071Sedwin/* Center string t in string s of length w by putting enough leading blanks. */
1061189804Sdaswchar_t *
1062189804Sdaswcenter(wchar_t *s, wchar_t *t, int w)
1063189804Sdas{
1064204697Sedwin	char blanks[MAX_WIDTH];
1065189804Sdas
1066189804Sdas	memset(blanks, ' ', sizeof(blanks));
1067189804Sdas	swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
1068189804Sdas	return (s);
1069189804Sdas}
1070189804Sdas
1071104369Sroamint
1072186401Swollmanparsemonth(const char *s, int *m, int *y)
1073104369Sroam{
1074186401Swollman	int nm, ny;
1075104369Sroam	char *cp;
1076104369Sroam	struct tm tm;
1077104369Sroam
1078186401Swollman	nm = (int)strtol(s, &cp, 10);
1079186401Swollman	if (cp != s) {
1080186401Swollman		ny = *y;
1081186401Swollman		if (*cp == '\0') {
1082186401Swollman			;	/* no special action */
1083186401Swollman		} else if (*cp == 'f' || *cp == 'F') {
1084186401Swollman			if (nm <= *m)
1085186401Swollman				ny++;
1086186401Swollman		} else if (*cp == 'p' || *cp == 'P') {
1087186401Swollman			if (nm >= *m)
1088186401Swollman				ny--;
1089186401Swollman		} else
1090186401Swollman			return (1);
1091186401Swollman		if (nm < 1 || nm > 12)
1092186401Swollman			return 1;
1093186401Swollman		*m = nm;
1094186401Swollman		*y = ny;
1095186401Swollman		return (0);
1096186401Swollman	}
1097186401Swollman	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
1098186401Swollman		*m = tm.tm_mon + 1;
1099186401Swollman		return (0);
1100186401Swollman	}
1101186401Swollman	return (1);
1102104369Sroam}
1103204849Sedwin
1104204849Sedwinvoid
1105204849Sedwinhighlight(char *dst, char *src, int len, int *extralen)
1106204849Sedwin{
1107204849Sedwin	static int first = 1;
1108204849Sedwin	static const char *term_so, *term_se;
1109204849Sedwin
1110204849Sedwin	if (first) {
1111204849Sedwin		char tbuf[1024], cbuf[512], *b;
1112204849Sedwin
1113204849Sedwin		term_se = term_so = NULL;
1114204849Sedwin
1115205071Sedwin		/* On how to highlight on this type of terminal (if any). */
1116204849Sedwin		if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1117204849Sedwin			b = cbuf;
1118204849Sedwin			term_so = tgetstr("so", &b);
1119204849Sedwin			term_se = tgetstr("se", &b);
1120204849Sedwin		}
1121204849Sedwin
1122204849Sedwin		first = 0;
1123204849Sedwin	}
1124204849Sedwin
1125204849Sedwin	/*
1126204849Sedwin	 * This check is not necessary, should have been handled before calling
1127204849Sedwin	 * this function.
1128204849Sedwin	 */
1129204849Sedwin	if (flag_nohighlight) {
1130204849Sedwin		memcpy(dst, src, len);
1131204849Sedwin		return;
1132204849Sedwin	}
1133204849Sedwin
1134205071Sedwin	/*
1135205071Sedwin	 * If it is a real terminal, use the data from the termcap database.
1136205071Sedwin	 */
1137204849Sedwin	if (term_so != NULL && term_se != NULL) {
1138205071Sedwin		/* separator. */
1139204849Sedwin		dst[0] = ' ';
1140204849Sedwin		dst++;
1141205071Sedwin		/* highlight on. */
1142204849Sedwin		memcpy(dst, term_so, strlen(term_so));
1143204849Sedwin		dst += strlen(term_so);
1144205071Sedwin		/* the actual text. (minus leading space) */
1145204849Sedwin		len--;
1146204849Sedwin		src++;
1147204849Sedwin		memcpy(dst, src, len);
1148204849Sedwin		dst += len;
1149205071Sedwin		/* highlight off. */
1150204849Sedwin		memcpy(dst, term_se, strlen(term_se));
1151204849Sedwin		*extralen = strlen(term_so) + strlen(term_se);
1152204849Sedwin		return;
1153204849Sedwin	}
1154204849Sedwin
1155204849Sedwin	/*
1156205071Sedwin	 * Otherwise, print a _, backspace and the letter.
1157204849Sedwin	 */
1158204849Sedwin	*extralen = 0;
1159205071Sedwin	/* skip leading space. */
1160204849Sedwin	src++;
1161204849Sedwin	len--;
1162205071Sedwin	/* separator. */
1163204849Sedwin	dst[0] = ' ';
1164204849Sedwin	dst++;
1165204849Sedwin	while (len > 0) {
1166205071Sedwin		/* _ and backspace. */
1167204849Sedwin		memcpy(dst, "_\010", 2);
1168204849Sedwin		dst += 2;
1169204849Sedwin		*extralen += 2;
1170205071Sedwin		/* the character. */
1171204849Sedwin		*dst++ = *src++;
1172204849Sedwin		len--;
1173204849Sedwin	}
1174205071Sedwin	return;
1175204849Sedwin}
1176