xref: /freebsd/usr.bin/ncal/ncal.c (revision 4af997a87fe5082e60a1a65537a3fd51cb845efa)
10cb2e609SWolfgang Helbig /*-
20cb2e609SWolfgang Helbig  * Copyright (c) 1997 Wolfgang Helbig
30cb2e609SWolfgang Helbig  * All rights reserved.
40cb2e609SWolfgang Helbig  *
50cb2e609SWolfgang Helbig  * Redistribution and use in source and binary forms, with or without
60cb2e609SWolfgang Helbig  * modification, are permitted provided that the following conditions
70cb2e609SWolfgang Helbig  * are met:
80cb2e609SWolfgang Helbig  * 1. Redistributions of source code must retain the above copyright
90cb2e609SWolfgang Helbig  *    notice, this list of conditions and the following disclaimer.
100cb2e609SWolfgang Helbig  * 2. Redistributions in binary form must reproduce the above copyright
110cb2e609SWolfgang Helbig  *    notice, this list of conditions and the following disclaimer in the
120cb2e609SWolfgang Helbig  *    documentation and/or other materials provided with the distribution.
130cb2e609SWolfgang Helbig  *
140cb2e609SWolfgang Helbig  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150cb2e609SWolfgang Helbig  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160cb2e609SWolfgang Helbig  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170cb2e609SWolfgang Helbig  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180cb2e609SWolfgang Helbig  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190cb2e609SWolfgang Helbig  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200cb2e609SWolfgang Helbig  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210cb2e609SWolfgang Helbig  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
220cb2e609SWolfgang Helbig  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
230cb2e609SWolfgang Helbig  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240cb2e609SWolfgang Helbig  * SUCH DAMAGE.
250cb2e609SWolfgang Helbig  */
26a99e4564SPhilippe Charnier 
27a99e4564SPhilippe Charnier #ifndef lint
28a99e4564SPhilippe Charnier static const char rcsid[] =
29c3aac50fSPeter Wemm   "$FreeBSD$";
30a99e4564SPhilippe Charnier #endif /* not lint */
31a99e4564SPhilippe Charnier 
320cb2e609SWolfgang Helbig #include <calendar.h>
33821df508SXin LI #include <ctype.h>
340cb2e609SWolfgang Helbig #include <err.h>
35f5e40af2SAndrey A. Chernov #include <langinfo.h>
360cb2e609SWolfgang Helbig #include <locale.h>
370cb2e609SWolfgang Helbig #include <stdio.h>
380cb2e609SWolfgang Helbig #include <stdlib.h>
390cb2e609SWolfgang Helbig #include <string.h>
400cb2e609SWolfgang Helbig #include <sysexits.h>
410cb2e609SWolfgang Helbig #include <time.h>
420cb2e609SWolfgang Helbig #include <unistd.h>
434646cea7SDavid Schultz #include <wchar.h>
444646cea7SDavid Schultz #include <wctype.h>
45e454a171SRoman Divacky #include <term.h>
46e454a171SRoman Divacky #undef lines			/* term.h defines this */
470cb2e609SWolfgang Helbig 
480851fbdfSEdwin Groothuis /* Width of one month with backward compatibility and in regular mode*/
490cb2e609SWolfgang Helbig #define MONTH_WIDTH_B_J 27
500cb2e609SWolfgang Helbig #define MONTH_WIDTH_B 20
510cb2e609SWolfgang Helbig 
520851fbdfSEdwin Groothuis #define MONTH_WIDTH_R_J 24
530851fbdfSEdwin Groothuis #define MONTH_WIDTH_R 18
540cb2e609SWolfgang Helbig 
55e454a171SRoman Divacky #define MAX_WIDTH 64
560cb2e609SWolfgang Helbig 
570cb2e609SWolfgang Helbig typedef struct date date;
580cb2e609SWolfgang Helbig 
590cb2e609SWolfgang Helbig struct monthlines {
604646cea7SDavid Schultz 	wchar_t name[MAX_WIDTH + 1];
610cb2e609SWolfgang Helbig 	char lines[7][MAX_WIDTH + 1];
620cb2e609SWolfgang Helbig 	char weeks[MAX_WIDTH + 1];
631d0e1dacSEdwin Groothuis 	unsigned int extralen[7];
640cb2e609SWolfgang Helbig };
650cb2e609SWolfgang Helbig 
660cb2e609SWolfgang Helbig struct weekdays {
674646cea7SDavid Schultz 	wchar_t names[7][4];
680cb2e609SWolfgang Helbig };
690cb2e609SWolfgang Helbig 
700cb2e609SWolfgang Helbig /* The switches from Julian to Gregorian in some countries */
710cb2e609SWolfgang Helbig static struct djswitch {
72f372d010SMark Murray 	const char *cc;	/* Country code according to ISO 3166 */
73f372d010SMark Murray 	const char *nm;	/* Name of country */
740cb2e609SWolfgang Helbig 	date dt;	/* Last day of Julian calendar */
750cb2e609SWolfgang Helbig } switches[] = {
769d969a8bSAndrey A. Chernov 	{"AL", "Albania",       {1912, 11, 30}},
779d969a8bSAndrey A. Chernov 	{"AT", "Austria",       {1583, 10,  5}},
789d969a8bSAndrey A. Chernov 	{"AU", "Australia",     {1752,  9,  2}},
799d969a8bSAndrey A. Chernov 	{"BE", "Belgium",       {1582, 12, 14}},
809d969a8bSAndrey A. Chernov 	{"BG", "Bulgaria",      {1916,  3, 18}},
819d969a8bSAndrey A. Chernov 	{"CA", "Canada",        {1752,  9,  2}},
829d969a8bSAndrey A. Chernov 	{"CH", "Switzerland",   {1655,  2, 28}},
839d969a8bSAndrey A. Chernov 	{"CN", "China",         {1911, 12, 18}},
849d969a8bSAndrey A. Chernov 	{"CZ", "Czech Republic",{1584,  1,  6}},
859d969a8bSAndrey A. Chernov 	{"DE", "Germany",       {1700,  2, 18}},
869d969a8bSAndrey A. Chernov 	{"DK", "Denmark",       {1700,  2, 18}},
879d969a8bSAndrey A. Chernov 	{"ES", "Spain",         {1582, 10,  4}},
889d969a8bSAndrey A. Chernov 	{"FI", "Finland",       {1753,  2, 17}},
899d969a8bSAndrey A. Chernov 	{"FR", "France",        {1582, 12,  9}},
909d969a8bSAndrey A. Chernov 	{"GB", "United Kingdom",{1752,  9,  2}},
919d969a8bSAndrey A. Chernov 	{"GR", "Greece",        {1924,  3,  9}},
929d969a8bSAndrey A. Chernov 	{"HU", "Hungary",       {1587, 10, 21}},
939d969a8bSAndrey A. Chernov 	{"IS", "Iceland",       {1700, 11, 16}},
949d969a8bSAndrey A. Chernov 	{"IT", "Italy",         {1582, 10,  4}},
959d969a8bSAndrey A. Chernov 	{"JP", "Japan",         {1918, 12, 18}},
969d969a8bSAndrey A. Chernov 	{"LI", "Lithuania",     {1918,  2,  1}},
979d969a8bSAndrey A. Chernov 	{"LN", "Latin",         {9999, 05, 31}},
989d969a8bSAndrey A. Chernov 	{"LU", "Luxembourg",    {1582, 12, 14}},
999d969a8bSAndrey A. Chernov 	{"LV", "Latvia",        {1918,  2,  1}},
1009d969a8bSAndrey A. Chernov 	{"NL", "Netherlands",   {1582, 12, 14}},
1019d969a8bSAndrey A. Chernov 	{"NO", "Norway",        {1700,  2, 18}},
1029d969a8bSAndrey A. Chernov 	{"PL", "Poland",        {1582, 10,  4}},
1039d969a8bSAndrey A. Chernov 	{"PT", "Portugal",      {1582, 10,  4}},
1049d969a8bSAndrey A. Chernov 	{"RO", "Romania",       {1919,  3, 31}},
1059d969a8bSAndrey A. Chernov 	{"RU", "Russia",        {1918,  1, 31}},
1069d969a8bSAndrey A. Chernov 	{"SI", "Slovenia",      {1919,  3,  4}},
1079d969a8bSAndrey A. Chernov 	{"SW", "Sweden",        {1753,  2, 17}},
1089d969a8bSAndrey A. Chernov 	{"TR", "Turkey",        {1926, 12, 18}},
1099d969a8bSAndrey A. Chernov 	{"US", "United States", {1752,  9,  2}},
1109d969a8bSAndrey A. Chernov 	{"YU", "Yugoslavia",    {1919,  3,  4}}
1110cb2e609SWolfgang Helbig };
1120cb2e609SWolfgang Helbig 
113cde26ed2SWolfgang Helbig struct djswitch *dftswitch =
114cde26ed2SWolfgang Helbig     switches + sizeof(switches) / sizeof(struct djswitch) - 2;
115cde26ed2SWolfgang Helbig     /* default switch (should be "US") */
116cde26ed2SWolfgang Helbig 
1170cb2e609SWolfgang Helbig /* Table used to print day of month and week numbers */
1180cb2e609SWolfgang Helbig char daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
1190cb2e609SWolfgang Helbig 		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
1200cb2e609SWolfgang Helbig 		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
1210cb2e609SWolfgang Helbig 		" 48 49 50 51 52 53";
1220cb2e609SWolfgang Helbig 
1230cb2e609SWolfgang Helbig /* Table used to print day of year and week numbers */
1240cb2e609SWolfgang Helbig char jdaystr[] = "       1   2   3   4   5   6   7   8   9"
1250cb2e609SWolfgang Helbig 		 "  10  11  12  13  14  15  16  17  18  19"
1260cb2e609SWolfgang Helbig 		 "  20  21  22  23  24  25  26  27  28  29"
1270cb2e609SWolfgang Helbig 		 "  30  31  32  33  34  35  36  37  38  39"
1280cb2e609SWolfgang Helbig 		 "  40  41  42  43  44  45  46  47  48  49"
1290cb2e609SWolfgang Helbig 		 "  50  51  52  53  54  55  56  57  58  59"
1300cb2e609SWolfgang Helbig 		 "  60  61  62  63  64  65  66  67  68  69"
1310cb2e609SWolfgang Helbig 		 "  70  71  72  73  74  75  76  77  78  79"
1320cb2e609SWolfgang Helbig 		 "  80  81  82  83  84  85  86  87  88  89"
1330cb2e609SWolfgang Helbig 		 "  90  91  92  93  94  95  96  97  98  99"
1340cb2e609SWolfgang Helbig 		 " 100 101 102 103 104 105 106 107 108 109"
1350cb2e609SWolfgang Helbig 		 " 110 111 112 113 114 115 116 117 118 119"
1360cb2e609SWolfgang Helbig 		 " 120 121 122 123 124 125 126 127 128 129"
1370cb2e609SWolfgang Helbig 		 " 130 131 132 133 134 135 136 137 138 139"
1380cb2e609SWolfgang Helbig 		 " 140 141 142 143 144 145 146 147 148 149"
1390cb2e609SWolfgang Helbig 		 " 150 151 152 153 154 155 156 157 158 159"
1400cb2e609SWolfgang Helbig 		 " 160 161 162 163 164 165 166 167 168 169"
1410cb2e609SWolfgang Helbig 		 " 170 171 172 173 174 175 176 177 178 179"
1420cb2e609SWolfgang Helbig 		 " 180 181 182 183 184 185 186 187 188 189"
1430cb2e609SWolfgang Helbig 		 " 190 191 192 193 194 195 196 197 198 199"
1440cb2e609SWolfgang Helbig 		 " 200 201 202 203 204 205 206 207 208 209"
1450cb2e609SWolfgang Helbig 		 " 210 211 212 213 214 215 216 217 218 219"
1460cb2e609SWolfgang Helbig 		 " 220 221 222 223 224 225 226 227 228 229"
1470cb2e609SWolfgang Helbig 		 " 230 231 232 233 234 235 236 237 238 239"
1480cb2e609SWolfgang Helbig 		 " 240 241 242 243 244 245 246 247 248 249"
1490cb2e609SWolfgang Helbig 		 " 250 251 252 253 254 255 256 257 258 259"
1500cb2e609SWolfgang Helbig 		 " 260 261 262 263 264 265 266 267 268 269"
1510cb2e609SWolfgang Helbig 		 " 270 271 272 273 274 275 276 277 278 279"
1520cb2e609SWolfgang Helbig 		 " 280 281 282 283 284 285 286 287 288 289"
1530cb2e609SWolfgang Helbig 		 " 290 291 292 293 294 295 296 297 298 299"
1540cb2e609SWolfgang Helbig 		 " 300 301 302 303 304 305 306 307 308 309"
1550cb2e609SWolfgang Helbig 		 " 310 311 312 313 314 315 316 317 318 319"
1560cb2e609SWolfgang Helbig 		 " 320 321 322 323 324 325 326 327 328 329"
1570cb2e609SWolfgang Helbig 		 " 330 331 332 333 334 335 336 337 338 339"
1580cb2e609SWolfgang Helbig 		 " 340 341 342 343 344 345 346 347 348 349"
1590cb2e609SWolfgang Helbig 		 " 350 351 352 353 354 355 356 357 358 359"
1600cb2e609SWolfgang Helbig 		 " 360 361 362 363 364 365 366";
1610cb2e609SWolfgang Helbig 
162cae11c25SEdwin Groothuis int	flag_nohighlight;	/* user doesn't want a highlighted today */
163a4264dceSWolfgang Helbig int     flag_weeks;		/* user wants number of week */
1640cb2e609SWolfgang Helbig int     nswitch;		/* user defined switch date */
1650cb2e609SWolfgang Helbig int	nswitchb;		/* switch date for backward compatibility */
1664af997a8SEdwin Groothuis int	highlightdate;
1670cb2e609SWolfgang Helbig 
1680cb2e609SWolfgang Helbig char	*center(char *s, char *t, int w);
1694646cea7SDavid Schultz wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
1704af997a8SEdwin Groothuis int	firstday(int y, int m);
1714af997a8SEdwin Groothuis void	highlight(char *dst, char *src, int len, int *extraletters);
1720851fbdfSEdwin Groothuis void	mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
1730cb2e609SWolfgang Helbig void	mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
1740cb2e609SWolfgang Helbig void	mkweekdays(struct weekdays * wds);
1754af997a8SEdwin Groothuis void	monthranger(int year, int m, int jd_flag, int before, int after);
1764af997a8SEdwin Groothuis void	monthrangeb(int year, int m, int jd_flag, int before, int after);
177ba29aec0SGarrett Wollman int	parsemonth(const char *s, int *m, int *y);
1780cb2e609SWolfgang Helbig void	printcc(void);
1790cb2e609SWolfgang Helbig void	printeaster(int year, int julian, int orthodox);
1800851fbdfSEdwin Groothuis date	*sdater(int ndays, struct date * d);
1810cb2e609SWolfgang Helbig date	*sdateb(int ndays, struct date * d);
1820851fbdfSEdwin Groothuis int	sndaysr(struct date * d);
1830cb2e609SWolfgang Helbig int	sndaysb(struct date * d);
184a99e4564SPhilippe Charnier static void	usage(void);
1850cb2e609SWolfgang Helbig 
1860cb2e609SWolfgang Helbig int
1870cb2e609SWolfgang Helbig main(int argc, char *argv[])
1880cb2e609SWolfgang Helbig {
1890cb2e609SWolfgang Helbig 	struct  djswitch *p, *q;	/* to search user defined switch date */
1900cb2e609SWolfgang Helbig 	date	never = {10000, 1, 1};	/* outside valid range of dates */
1910cb2e609SWolfgang Helbig 	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
1920851fbdfSEdwin Groothuis 	date	dt;
1930cb2e609SWolfgang Helbig 	int     ch;			/* holds the option character */
1940cb2e609SWolfgang Helbig 	int     m = 0;			/* month */
1950cb2e609SWolfgang Helbig 	int	y = 0;			/* year */
1960cb2e609SWolfgang Helbig 	int     flag_backward = 0;	/* user called cal--backward compat. */
1974af997a8SEdwin Groothuis 	int     flag_wholeyear = 0;	/* user wants the whole year */
1980cb2e609SWolfgang Helbig 	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
1994af997a8SEdwin Groothuis 	int     flag_julian_day = 0;	/* user wants the Julian day numbers */
2004af997a8SEdwin Groothuis 	int	flag_orthodox = 0;	/* user wants Orthodox easter */
2014af997a8SEdwin Groothuis 	int	flag_easter = 0;	/* user wants easter date */
2024af997a8SEdwin Groothuis 	int	flag_3months = 0;	/* user wants 3 month display (-3) */
2034af997a8SEdwin Groothuis 	int	flag_after = 0;		/* user wants to see months after */
2044af997a8SEdwin Groothuis 	int	flag_before = 0;	/* user wants to see months before */
2054af997a8SEdwin Groothuis 	int	flag_specifiedmonth = 0;/* user wants to see this month (-m) */
2064af997a8SEdwin Groothuis 	int	flag_givenmonth = 0;	/* user has specified month [n] */
2074af997a8SEdwin Groothuis 	int	flag_givenyear = 0;	/* user has specified year [n] */
2080cb2e609SWolfgang Helbig 	char	*cp;			/* character pointer */
2094af997a8SEdwin Groothuis 	char	*flag_today = NULL;	/* debug: use date as being today */
2100c4cafeaSGarrett Wollman 	char	*flag_month = NULL;	/* requested month as string */
2114af997a8SEdwin Groothuis 	char	*flag_highlightdate = NULL; /* debug: date to highlight */
2120851fbdfSEdwin Groothuis 	int	before, after;
213f372d010SMark Murray 	const char    *locale;		/* locale to get country code */
214e454a171SRoman Divacky 
215cae11c25SEdwin Groothuis 	flag_nohighlight = 0;
216cae11c25SEdwin Groothuis 	flag_weeks = 0;
2170cb2e609SWolfgang Helbig 
218cde26ed2SWolfgang Helbig 	/*
219cde26ed2SWolfgang Helbig 	 * Use locale to determine the country code,
220cde26ed2SWolfgang Helbig 	 * and use the country code to determine the default
221cde26ed2SWolfgang Helbig 	 * switchdate and date format from the switches table.
222cde26ed2SWolfgang Helbig 	 */
223e036a70eSAndrey A. Chernov 	if (setlocale(LC_ALL, "") == NULL)
224cde26ed2SWolfgang Helbig 		warn("setlocale");
225e036a70eSAndrey A. Chernov 	locale = setlocale(LC_TIME, NULL);
226c052429fSAndrey A. Chernov 	if (locale == NULL ||
227c052429fSAndrey A. Chernov 	    strcmp(locale, "C") == 0 ||
2281c593a0dSAndrey A. Chernov 	    strcmp(locale, "POSIX") == 0 ||
2291c593a0dSAndrey A. Chernov 	    strcmp(locale, "ASCII") == 0 ||
2301c593a0dSAndrey A. Chernov 	    strcmp(locale, "US-ASCII") == 0)
231cde26ed2SWolfgang Helbig 		locale = "_US";
232cde26ed2SWolfgang Helbig 	q = switches + sizeof(switches) / sizeof(struct djswitch);
233cde26ed2SWolfgang Helbig 	for (p = switches; p != q; p++)
234cde26ed2SWolfgang Helbig 		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
235cde26ed2SWolfgang Helbig 			break;
236cde26ed2SWolfgang Helbig 	if (p == q) {
237cde26ed2SWolfgang Helbig 		nswitch = ndaysj(&dftswitch->dt);
238cde26ed2SWolfgang Helbig 	} else {
239cde26ed2SWolfgang Helbig 		nswitch = ndaysj(&p->dt);
240cde26ed2SWolfgang Helbig 		dftswitch = p;
241cde26ed2SWolfgang Helbig 	}
242cde26ed2SWolfgang Helbig 
2430cb2e609SWolfgang Helbig 
2440cb2e609SWolfgang Helbig 	/*
2450cb2e609SWolfgang Helbig 	 * Get the filename portion of argv[0] and set flag_backward if
2460cb2e609SWolfgang Helbig 	 * this program is called "cal".
2470cb2e609SWolfgang Helbig 	 */
2480c4cafeaSGarrett Wollman 	cp = strrchr(argv[0], '/');
2490c4cafeaSGarrett Wollman 	cp = (cp == NULL) ? argv[0] : cp + 1;
2500c4cafeaSGarrett Wollman 	if (strcmp("cal", cp) == 0)
2510cb2e609SWolfgang Helbig 		flag_backward = 1;
2520cb2e609SWolfgang Helbig 
2530cb2e609SWolfgang Helbig 	/* Set the switch date to United Kingdom if backwards compatible */
2540cb2e609SWolfgang Helbig 	if (flag_backward)
2550cb2e609SWolfgang Helbig 		nswitchb = ndaysj(&ukswitch);
2560cb2e609SWolfgang Helbig 
2570851fbdfSEdwin Groothuis 	before = after = -1;
2580851fbdfSEdwin Groothuis 
2594af997a8SEdwin Groothuis 	while ((ch = getopt(argc, argv, "A:B:3Jbd:eH:hjm:ops:wy")) != -1)
2600cb2e609SWolfgang Helbig 		switch (ch) {
2610851fbdfSEdwin Groothuis 		case '3':
2624af997a8SEdwin Groothuis 			flag_3months = 1;
2630851fbdfSEdwin Groothuis 			break;
2640851fbdfSEdwin Groothuis 		case 'A':
2654af997a8SEdwin Groothuis 			if (flag_after > 0)
2664af997a8SEdwin Groothuis 				errx(EX_USAGE, "Double -A specified");
2674af997a8SEdwin Groothuis 			flag_after = strtol(optarg, NULL, 10);
2684af997a8SEdwin Groothuis 			if (flag_after <= 0)
2694af997a8SEdwin Groothuis 				errx(EX_USAGE,
2704af997a8SEdwin Groothuis 				    "Argument to -A must be positive");
2710851fbdfSEdwin Groothuis 			break;
2720851fbdfSEdwin Groothuis 		case 'B':
2734af997a8SEdwin Groothuis 			if (flag_before > 0)
2744af997a8SEdwin Groothuis 				errx(EX_USAGE, "Double -A specified");
2754af997a8SEdwin Groothuis 			flag_before = strtol(optarg, NULL, 10);
2764af997a8SEdwin Groothuis 			if (flag_before <= 0)
2774af997a8SEdwin Groothuis 				errx(EX_USAGE,
2784af997a8SEdwin Groothuis 				    "Argument to -B must be positive");
2790851fbdfSEdwin Groothuis 			break;
2800cb2e609SWolfgang Helbig 		case 'J':
2810cb2e609SWolfgang Helbig 			if (flag_backward)
2820cb2e609SWolfgang Helbig 				usage();
2830cb2e609SWolfgang Helbig 			nswitch = ndaysj(&never);
2840cb2e609SWolfgang Helbig 			flag_julian_cal = 1;
2850cb2e609SWolfgang Helbig 			break;
2860851fbdfSEdwin Groothuis 		case 'b':
2870851fbdfSEdwin Groothuis 			flag_backward = 1;
2880851fbdfSEdwin Groothuis 			break;
2890851fbdfSEdwin Groothuis 		case 'd':
2904af997a8SEdwin Groothuis 			flag_today = optarg;
2914af997a8SEdwin Groothuis 			break;
2924af997a8SEdwin Groothuis 		case 'H':
2930851fbdfSEdwin Groothuis 			flag_highlightdate = optarg;
2940851fbdfSEdwin Groothuis 			break;
2950cd51de1SRoman Divacky 		case 'h':
296cae11c25SEdwin Groothuis 			flag_nohighlight = 1;
2970cd51de1SRoman Divacky 			break;
2980cb2e609SWolfgang Helbig 		case 'e':
2990cb2e609SWolfgang Helbig 			if (flag_backward)
3000cb2e609SWolfgang Helbig 				usage();
3010cb2e609SWolfgang Helbig 			flag_easter = 1;
3020cb2e609SWolfgang Helbig 			break;
3030cb2e609SWolfgang Helbig 		case 'j':
3040cb2e609SWolfgang Helbig 			flag_julian_day = 1;
3050cb2e609SWolfgang Helbig 			break;
3060c4cafeaSGarrett Wollman 		case 'm':
3074af997a8SEdwin Groothuis 			if (flag_specifiedmonth)
3084af997a8SEdwin Groothuis 				errx(EX_USAGE, "Double -m specified");
3090c4cafeaSGarrett Wollman 			flag_month = optarg;
3104af997a8SEdwin Groothuis 			flag_specifiedmonth = 1;
3110c4cafeaSGarrett Wollman 			break;
3120cb2e609SWolfgang Helbig 		case 'o':
3130cb2e609SWolfgang Helbig 			if (flag_backward)
3140cb2e609SWolfgang Helbig 				usage();
3150cb2e609SWolfgang Helbig 			flag_orthodox = 1;
3160cb2e609SWolfgang Helbig 			flag_easter = 1;
3170cb2e609SWolfgang Helbig 			break;
3180cb2e609SWolfgang Helbig 		case 'p':
3190cb2e609SWolfgang Helbig 			if (flag_backward)
3200cb2e609SWolfgang Helbig 				usage();
3210cb2e609SWolfgang Helbig 			printcc();
3220cb2e609SWolfgang Helbig 			return (0);
3230cb2e609SWolfgang Helbig 			break;
3240cb2e609SWolfgang Helbig 		case 's':
3250cb2e609SWolfgang Helbig 			if (flag_backward)
3260cb2e609SWolfgang Helbig 				usage();
3270cb2e609SWolfgang Helbig 			q = switches +
3280cb2e609SWolfgang Helbig 			    sizeof(switches) / sizeof(struct djswitch);
3290cb2e609SWolfgang Helbig 			for (p = switches;
3300cb2e609SWolfgang Helbig 			     p != q && strcmp(p->cc, optarg) != 0; p++)
3310cb2e609SWolfgang Helbig 				;
3320cb2e609SWolfgang Helbig 			if (p == q)
3330cb2e609SWolfgang Helbig 				errx(EX_USAGE,
3340cb2e609SWolfgang Helbig 				    "%s: invalid country code", optarg);
3350cb2e609SWolfgang Helbig 			nswitch = ndaysj(&(p->dt));
3360cb2e609SWolfgang Helbig 			break;
3370cb2e609SWolfgang Helbig 		case 'w':
3380cb2e609SWolfgang Helbig 			if (flag_backward)
3390cb2e609SWolfgang Helbig 				usage();
3400cb2e609SWolfgang Helbig 			flag_weeks = 1;
3410cb2e609SWolfgang Helbig 			break;
3420cb2e609SWolfgang Helbig 		case 'y':
3434af997a8SEdwin Groothuis 			flag_wholeyear = 1;
3440cb2e609SWolfgang Helbig 			break;
3450cb2e609SWolfgang Helbig 		default:
3460cb2e609SWolfgang Helbig 			usage();
3470cb2e609SWolfgang Helbig 		}
3480cb2e609SWolfgang Helbig 
3490cb2e609SWolfgang Helbig 	argc -= optind;
3500cb2e609SWolfgang Helbig 	argv += optind;
3510cb2e609SWolfgang Helbig 
3520c4cafeaSGarrett Wollman 	switch (argc) {
3530c4cafeaSGarrett Wollman 	case 2:
3540c4cafeaSGarrett Wollman 		if (flag_easter)
3550c4cafeaSGarrett Wollman 			usage();
3560c4cafeaSGarrett Wollman 		flag_month = *argv++;
3574af997a8SEdwin Groothuis 		flag_givenmonth = 1;
3581d0e1dacSEdwin Groothuis 		m = strtol(flag_month, NULL, 10);
3590c4cafeaSGarrett Wollman 		/* FALLTHROUGH */
3600c4cafeaSGarrett Wollman 	case 1:
3614af997a8SEdwin Groothuis 		y = atoi(*argv);
3620c4cafeaSGarrett Wollman 		if (y < 1 || y > 9999)
3634af997a8SEdwin Groothuis 			errx(EX_USAGE, "year `%s' not in range 1..9999", *argv);
3644af997a8SEdwin Groothuis 		argv++;
3654af997a8SEdwin Groothuis 		flag_givenyear = 1;
3660c4cafeaSGarrett Wollman 		break;
3670c4cafeaSGarrett Wollman 	case 0:
3684af997a8SEdwin Groothuis 		if (flag_today != NULL) {
3694af997a8SEdwin Groothuis 			y = strtol(flag_today, NULL, 10);
3704af997a8SEdwin Groothuis 			m = strtol(flag_today + 5, NULL, 10);
3714af997a8SEdwin Groothuis 		} else {
3720cb2e609SWolfgang Helbig 			time_t t;
3730cb2e609SWolfgang Helbig 			struct tm *tm;
3740cb2e609SWolfgang Helbig 
3750cb2e609SWolfgang Helbig 			t = time(NULL);
3760cb2e609SWolfgang Helbig 			tm = localtime(&t);
3770cb2e609SWolfgang Helbig 			y = tm->tm_year + 1900;
3780cb2e609SWolfgang Helbig 			m = tm->tm_mon + 1;
3790cb2e609SWolfgang Helbig 		}
3800cb2e609SWolfgang Helbig 		break;
3810cb2e609SWolfgang Helbig 	default:
3820cb2e609SWolfgang Helbig 		usage();
3830cb2e609SWolfgang Helbig 	}
3840cb2e609SWolfgang Helbig 
3850c4cafeaSGarrett Wollman 	if (flag_month != NULL) {
386ba29aec0SGarrett Wollman 		if (parsemonth(flag_month, &m, &y)) {
3870c4cafeaSGarrett Wollman 			errx(EX_USAGE,
3880c4cafeaSGarrett Wollman 			    "%s is neither a month number (1..12) nor a name",
3890c4cafeaSGarrett Wollman 			    flag_month);
3900c4cafeaSGarrett Wollman 		}
391ba29aec0SGarrett Wollman 	}
3920c4cafeaSGarrett Wollman 
3934af997a8SEdwin Groothuis 	/*
3944af997a8SEdwin Groothuis 	 * What is not supported:
3954af997a8SEdwin Groothuis 	 * -3 with -A or -B
3964af997a8SEdwin Groothuis 	 *	-3 displays 3 months, -A and -B change that behaviour.
3974af997a8SEdwin Groothuis 	 * -3 with -y
3984af997a8SEdwin Groothuis 	 *	-3 displays 3 months, -y says display a whole year.
3994af997a8SEdwin Groothuis 	 * -3 with a given year but no given month or without -m
4004af997a8SEdwin Groothuis 	 *	-3 displays 3 months, no month specified doesn't make clear
4014af997a8SEdwin Groothuis 	 *      which three months.
4024af997a8SEdwin Groothuis 	 * -m with a given month
4034af997a8SEdwin Groothuis 	 *	conflicting arguments, both specify the same field.
4044af997a8SEdwin Groothuis 	 * -y with -m
4054af997a8SEdwin Groothuis 	 *	-y displays the whole year, -m displays a single month.
4064af997a8SEdwin Groothuis 	 * -y with a given month
4074af997a8SEdwin Groothuis 	 *	-y displays the whole year, the given month displays a single
4084af997a8SEdwin Groothuis 	 *	month.
4094af997a8SEdwin Groothuis 	 * -y with -A or -B
4104af997a8SEdwin Groothuis 	 *	-y displays the whole year, -A and -B display extra months.
4114af997a8SEdwin Groothuis 	 */
4124af997a8SEdwin Groothuis 
4134af997a8SEdwin Groothuis 	/* -3 together with -A or -B. */
4144af997a8SEdwin Groothuis 	if (flag_3months && (flag_after || flag_before))
4154af997a8SEdwin Groothuis 		errx(EX_USAGE, "-3 together with -A and -B is not supported.");
4164af997a8SEdwin Groothuis 	/* -3 together with -y. */
4174af997a8SEdwin Groothuis 	if (flag_3months && flag_wholeyear)
4184af997a8SEdwin Groothuis 		errx(EX_USAGE, "-3 together with -y is not supported.");
4194af997a8SEdwin Groothuis 	/* -3 together with givenyear but no givenmonth. */
4204af997a8SEdwin Groothuis 	if (flag_3months && flag_givenyear &&
4214af997a8SEdwin Groothuis 	    !(flag_givenmonth || flag_specifiedmonth))
4224af997a8SEdwin Groothuis 		errx(EX_USAGE,
4234af997a8SEdwin Groothuis 		    "-3 together with a given year but no given month is "
4244af997a8SEdwin Groothuis 		    "not supported.");
4254af997a8SEdwin Groothuis 	/* -m together with xx xxxx. */
4264af997a8SEdwin Groothuis 	if (flag_specifiedmonth && flag_givenmonth)
4274af997a8SEdwin Groothuis 		errx(EX_USAGE,
4284af997a8SEdwin Groothuis 		    "-m together with a given month is not supported.");
4294af997a8SEdwin Groothuis 	/* -y together with -m. */
4304af997a8SEdwin Groothuis 	if (flag_wholeyear && flag_specifiedmonth)
4314af997a8SEdwin Groothuis 		errx(EX_USAGE, "-y together with -m is not supported.");
4324af997a8SEdwin Groothuis 	/* -y together with xx xxxx. */
4334af997a8SEdwin Groothuis 	if (flag_wholeyear && flag_givenmonth)
4344af997a8SEdwin Groothuis 		errx(EX_USAGE, "-y together a given month is not supported.");
4354af997a8SEdwin Groothuis 	/* -y together with -A or -B. */
4364af997a8SEdwin Groothuis 	if (flag_wholeyear && (flag_before > 0 || flag_after > 0))
4374af997a8SEdwin Groothuis 		errx(EX_USAGE, "-y together a -A or -B is not supported.");
4384af997a8SEdwin Groothuis 	/* The rest should be fine. */
4394af997a8SEdwin Groothuis 
4404af997a8SEdwin Groothuis 	/* Select the period to display, in order of increasing priority .*/
4414af997a8SEdwin Groothuis 	if (flag_wholeyear ||
4424af997a8SEdwin Groothuis 	    (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) {
4434af997a8SEdwin Groothuis 		m = 1;
4444af997a8SEdwin Groothuis 		before = 0;
4454af997a8SEdwin Groothuis 		after = 11;
4464af997a8SEdwin Groothuis 	}
4474af997a8SEdwin Groothuis 	if (flag_givenyear && flag_givenmonth) {
4484af997a8SEdwin Groothuis 		before = 0;
4494af997a8SEdwin Groothuis 		after = 0;
4504af997a8SEdwin Groothuis 	}
4514af997a8SEdwin Groothuis 	if (flag_specifiedmonth) {
4524af997a8SEdwin Groothuis 		before = 0;
4534af997a8SEdwin Groothuis 		after = 0;
4544af997a8SEdwin Groothuis 	}
4554af997a8SEdwin Groothuis 	if (flag_before) {
4564af997a8SEdwin Groothuis 		before = flag_before;
4574af997a8SEdwin Groothuis 	}
4584af997a8SEdwin Groothuis 	if (flag_after) {
4594af997a8SEdwin Groothuis 		after = flag_after;
4604af997a8SEdwin Groothuis 	}
4614af997a8SEdwin Groothuis 	if (flag_3months) {
4624af997a8SEdwin Groothuis 		before = 1;
4634af997a8SEdwin Groothuis 		after = 1;
4644af997a8SEdwin Groothuis 	}
4654af997a8SEdwin Groothuis 	if (after == -1)
4664af997a8SEdwin Groothuis 		after = 0;
4674af997a8SEdwin Groothuis 	if (before == -1)
4684af997a8SEdwin Groothuis 		before = 0;
4694af997a8SEdwin Groothuis 
4704af997a8SEdwin Groothuis 	/* Highlight a specified day or today .*/
4710851fbdfSEdwin Groothuis 	if (flag_highlightdate != NULL) {
4720851fbdfSEdwin Groothuis 		dt.y = strtol(flag_highlightdate, NULL, 10);
4730851fbdfSEdwin Groothuis 		dt.m = strtol(flag_highlightdate + 5, NULL, 10);
4740851fbdfSEdwin Groothuis 		dt.d = strtol(flag_highlightdate + 8, NULL, 10);
4750851fbdfSEdwin Groothuis 	} else {
4760851fbdfSEdwin Groothuis 		time_t t;
4770851fbdfSEdwin Groothuis 		struct tm *tm1;
4780851fbdfSEdwin Groothuis 
4790851fbdfSEdwin Groothuis 		t = time(NULL);
4800851fbdfSEdwin Groothuis 		tm1 = localtime(&t);
4810851fbdfSEdwin Groothuis 		dt.y = tm1->tm_year + 1900;
4820851fbdfSEdwin Groothuis 		dt.m = tm1->tm_mon + 1;
4830851fbdfSEdwin Groothuis 		dt.d = tm1->tm_mday;
4840851fbdfSEdwin Groothuis 	}
4854af997a8SEdwin Groothuis 	highlightdate = sndaysb(&dt);
4860851fbdfSEdwin Groothuis 
4874af997a8SEdwin Groothuis 	/* And now we finally start to calculate and output calendars. */
4880cb2e609SWolfgang Helbig 	if (flag_easter)
4890cb2e609SWolfgang Helbig 		printeaster(y, flag_julian_cal, flag_orthodox);
4900cb2e609SWolfgang Helbig 	else
4910cb2e609SWolfgang Helbig 		if (flag_backward)
4924af997a8SEdwin Groothuis 			monthrangeb(y, m, flag_julian_day, before, after);
4930cb2e609SWolfgang Helbig 		else
4944af997a8SEdwin Groothuis 			monthranger(y, m, flag_julian_day, before, after);
4950cb2e609SWolfgang Helbig 	return (0);
4960cb2e609SWolfgang Helbig }
4970cb2e609SWolfgang Helbig 
498a99e4564SPhilippe Charnier static void
4990cb2e609SWolfgang Helbig usage(void)
5000cb2e609SWolfgang Helbig {
5010cb2e609SWolfgang Helbig 
5020c4cafeaSGarrett Wollman 	fputs(
5037149aa01SRoman Divacky 	    "usage: cal [-hjy] [[month] year]\n"
5047149aa01SRoman Divacky 	    "       cal [-hj] [-m month] [year]\n"
5057149aa01SRoman Divacky 	    "       ncal [-hJjpwy] [-s country_code] [[month] year]\n"
5060851fbdfSEdwin Groothuis 	    "       ncal [-hJeo] [year]\n"
5074af997a8SEdwin Groothuis 	    "for debug the highlighting: [-b] [-H yyyy-mm-dd] [-d yyyy-mm]\n",
5080851fbdfSEdwin Groothuis 	    stderr);
5090cb2e609SWolfgang Helbig 	exit(EX_USAGE);
5100cb2e609SWolfgang Helbig }
5110cb2e609SWolfgang Helbig 
5124af997a8SEdwin Groothuis /* Print the assumed switches for all countries. */
5130cb2e609SWolfgang Helbig void
5140cb2e609SWolfgang Helbig printcc(void)
5150cb2e609SWolfgang Helbig {
5160cb2e609SWolfgang Helbig 	struct djswitch *p;
5170cb2e609SWolfgang Helbig 	int n;	/* number of lines to print */
5180cb2e609SWolfgang Helbig 	int m;	/* offset from left to right table entry on the same line */
5190cb2e609SWolfgang Helbig 
520cde26ed2SWolfgang Helbig #define FSTR "%c%s %-15s%4d-%02d-%02d"
521cde26ed2SWolfgang Helbig #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
522cde26ed2SWolfgang Helbig #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
5230cb2e609SWolfgang Helbig 
5240cb2e609SWolfgang Helbig 	n = sizeof(switches) / sizeof(struct djswitch);
5250cb2e609SWolfgang Helbig 	m = (n + 1) / 2;
5260cb2e609SWolfgang Helbig 	n /= 2;
527cde26ed2SWolfgang Helbig 	for (p = switches; p != switches + n; p++)
528cde26ed2SWolfgang Helbig 		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
5290cb2e609SWolfgang Helbig 	if (m != n)
530cde26ed2SWolfgang Helbig 		printf(FSTR"\n", FSTRARG(p));
5310cb2e609SWolfgang Helbig }
5320cb2e609SWolfgang Helbig 
5334af997a8SEdwin Groothuis /* Print the date of easter sunday. */
5340cb2e609SWolfgang Helbig void
5350cb2e609SWolfgang Helbig printeaster(int y, int julian, int orthodox)
5360cb2e609SWolfgang Helbig {
5370cb2e609SWolfgang Helbig 	date    dt;
538cde26ed2SWolfgang Helbig 	struct tm tm;
5390851fbdfSEdwin Groothuis 	char    buf[MAX_WIDTH];
540f5e40af2SAndrey A. Chernov 	static int d_first = -1;
5410cb2e609SWolfgang Helbig 
542f5e40af2SAndrey A. Chernov 	if (d_first < 0)
543f5e40af2SAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
5440cb2e609SWolfgang Helbig 	/* force orthodox easter for years before 1583 */
5450cb2e609SWolfgang Helbig 	if (y < 1583)
5460cb2e609SWolfgang Helbig 		orthodox = 1;
5470cb2e609SWolfgang Helbig 
5480cb2e609SWolfgang Helbig 	if (orthodox)
5490cb2e609SWolfgang Helbig 		if (julian)
5500cb2e609SWolfgang Helbig 			easteroj(y, &dt);
5510cb2e609SWolfgang Helbig 		else
5520cb2e609SWolfgang Helbig 			easterog(y, &dt);
5530cb2e609SWolfgang Helbig 	else
5540cb2e609SWolfgang Helbig 		easterg(y, &dt);
555cde26ed2SWolfgang Helbig 
556cde26ed2SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
557cde26ed2SWolfgang Helbig 	tm.tm_year = dt.y - 1900;
558cde26ed2SWolfgang Helbig 	tm.tm_mon  = dt.m - 1;
559cde26ed2SWolfgang Helbig 	tm.tm_mday = dt.d;
560f5e40af2SAndrey A. Chernov 	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
561cde26ed2SWolfgang Helbig 	printf("%s\n", buf);
5620cb2e609SWolfgang Helbig }
5630cb2e609SWolfgang Helbig 
5641d0e1dacSEdwin Groothuis #define MW(mw, me)		((mw) + me)
5650851fbdfSEdwin Groothuis #define	DECREASEMONTH(m, y) 		\
5660851fbdfSEdwin Groothuis 		if (--m == 0) {		\
5670851fbdfSEdwin Groothuis 			m = 12;		\
5680851fbdfSEdwin Groothuis 			y--;		\
569bd97a998SHajimu UMEMOTO 		}
5700851fbdfSEdwin Groothuis #define	INCREASEMONTH(m, y)		\
5710851fbdfSEdwin Groothuis 		if (++(m) == 13) {	\
5720851fbdfSEdwin Groothuis 			(m) = 1;	\
5730851fbdfSEdwin Groothuis 			(y)++;		\
5740cb2e609SWolfgang Helbig 		}
5750851fbdfSEdwin Groothuis #define	M2Y(m)	((m) / 12)
5760851fbdfSEdwin Groothuis #define	M2M(m)	(1 + (m) % 12)
5770cb2e609SWolfgang Helbig 
5784af997a8SEdwin Groothuis /* Print all months for the period in the range [ before .. y-m .. after ]. */
5790cb2e609SWolfgang Helbig void
5804af997a8SEdwin Groothuis monthrangeb(int y, int m, int jd_flag, int before, int after)
5810cb2e609SWolfgang Helbig {
5820cb2e609SWolfgang Helbig 	struct monthlines year[12];
5830cb2e609SWolfgang Helbig 	struct weekdays wds;
5840851fbdfSEdwin Groothuis 	char	s[MAX_WIDTH], t[MAX_WIDTH];
5850851fbdfSEdwin Groothuis 	wchar_t	ws[MAX_WIDTH], ws1[MAX_WIDTH];
5860851fbdfSEdwin Groothuis 	const char	*wdss;
5870cb2e609SWolfgang Helbig 	int     i, j;
5880cb2e609SWolfgang Helbig 	int     mpl;
5890cb2e609SWolfgang Helbig 	int     mw;
5900851fbdfSEdwin Groothuis 	int	m1, m2;
5910851fbdfSEdwin Groothuis 	int	printyearheader;
5920851fbdfSEdwin Groothuis 	int	prevyear = -1;
5930cb2e609SWolfgang Helbig 
5940cb2e609SWolfgang Helbig 	mpl = jd_flag ? 2 : 3;
5950cb2e609SWolfgang Helbig 	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
5960851fbdfSEdwin Groothuis 	wdss = (mpl == 2) ? " " : "";
5970cb2e609SWolfgang Helbig 
5980851fbdfSEdwin Groothuis 	while (before != 0) {
5990851fbdfSEdwin Groothuis 		DECREASEMONTH(m, y);
6000851fbdfSEdwin Groothuis 		before--;
6010851fbdfSEdwin Groothuis 		after++;
6020cb2e609SWolfgang Helbig 	}
6030851fbdfSEdwin Groothuis 	m1 = y * 12 + m - 1;
6040851fbdfSEdwin Groothuis 	m2 = m1 + after;
6050851fbdfSEdwin Groothuis 
6060851fbdfSEdwin Groothuis 	mkweekdays(&wds);
6070851fbdfSEdwin Groothuis 
6080851fbdfSEdwin Groothuis 	/*
6090851fbdfSEdwin Groothuis 	 * The year header is printed when there are more than 'mpl' months
6100851fbdfSEdwin Groothuis 	 * and if the first month is a multitude of 'mpl'.
6110851fbdfSEdwin Groothuis 	 * If not, it will print the year behind every month.
6120851fbdfSEdwin Groothuis 	 */
6130851fbdfSEdwin Groothuis 	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
6140851fbdfSEdwin Groothuis 
6150851fbdfSEdwin Groothuis 	m = m1;
6160851fbdfSEdwin Groothuis 	while (m <= m2) {
6170851fbdfSEdwin Groothuis 		int count = 0;
6180851fbdfSEdwin Groothuis 		for (i = 0; i != mpl && m + i <= m2; i++) {
6190851fbdfSEdwin Groothuis 			mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
6200851fbdfSEdwin Groothuis 			count++;
6210851fbdfSEdwin Groothuis 		}
6220851fbdfSEdwin Groothuis 
6230851fbdfSEdwin Groothuis 		/* Empty line between two rows of months */
6240851fbdfSEdwin Groothuis 		if (m != m1)
6250851fbdfSEdwin Groothuis 			printf("\n");
6260851fbdfSEdwin Groothuis 
6274af997a8SEdwin Groothuis 		/* Year at the top. */
6280851fbdfSEdwin Groothuis 		if (printyearheader && M2Y(m) != prevyear) {
6290851fbdfSEdwin Groothuis 			sprintf(s, "%d", M2Y(m));
6300851fbdfSEdwin Groothuis 			printf("%s\n", center(t, s, mpl * mw));
6310851fbdfSEdwin Groothuis 			prevyear = M2Y(m);
6320851fbdfSEdwin Groothuis 		}
6330851fbdfSEdwin Groothuis 
6344af997a8SEdwin Groothuis 		/* Month names. */
6350851fbdfSEdwin Groothuis 		for (i = 0; i < count; i++)
6360851fbdfSEdwin Groothuis 			if (printyearheader)
6370851fbdfSEdwin Groothuis 				wprintf(L"%-*ls  ",
6380851fbdfSEdwin Groothuis 				    mw, wcenter(ws, year[i].name, mw));
6390851fbdfSEdwin Groothuis 			else {
6400851fbdfSEdwin Groothuis 				swprintf(ws, sizeof(ws), L"%-ls %d",
6410851fbdfSEdwin Groothuis 				    year[i].name, M2Y(m + i));
6420851fbdfSEdwin Groothuis 				wprintf(L"%-*ls  ", mw, wcenter(ws1, ws, mw));
6430851fbdfSEdwin Groothuis 			}
6440851fbdfSEdwin Groothuis 		printf("\n");
6450851fbdfSEdwin Groothuis 
6464af997a8SEdwin Groothuis 		/* Day of the week names. */
6470851fbdfSEdwin Groothuis 		for (i = 0; i < count; i++) {
6480851fbdfSEdwin Groothuis 			wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
6490851fbdfSEdwin Groothuis 				wdss, wds.names[6], wdss, wds.names[0],
6500851fbdfSEdwin Groothuis 				wdss, wds.names[1], wdss, wds.names[2],
6510851fbdfSEdwin Groothuis 				wdss, wds.names[3], wdss, wds.names[4],
6520851fbdfSEdwin Groothuis 				wdss, wds.names[5]);
6530851fbdfSEdwin Groothuis 		}
6540851fbdfSEdwin Groothuis 		printf("\n");
6550851fbdfSEdwin Groothuis 
6564af997a8SEdwin Groothuis 		/* And the days of the month. */
6570851fbdfSEdwin Groothuis 		for (i = 0; i != 6; i++) {
6580851fbdfSEdwin Groothuis 			for (j = 0; j < count; j++)
6591d0e1dacSEdwin Groothuis 				printf("%-*s  ",
6601d0e1dacSEdwin Groothuis 				    MW(mw, year[j].extralen[i]),
6611d0e1dacSEdwin Groothuis 					year[j].lines[i]+1);
6620851fbdfSEdwin Groothuis 			printf("\n");
6630851fbdfSEdwin Groothuis 		}
6640851fbdfSEdwin Groothuis 
6650851fbdfSEdwin Groothuis 		m += mpl;
6660cb2e609SWolfgang Helbig 	}
6670cb2e609SWolfgang Helbig }
6680cb2e609SWolfgang Helbig 
6690cb2e609SWolfgang Helbig void
6704af997a8SEdwin Groothuis monthranger(int y, int m, int jd_flag, int before, int after)
6710851fbdfSEdwin Groothuis {
6720851fbdfSEdwin Groothuis 	struct monthlines year[12];
6730851fbdfSEdwin Groothuis 	struct weekdays wds;
6740851fbdfSEdwin Groothuis 	char    s[MAX_WIDTH], t[MAX_WIDTH];
6750851fbdfSEdwin Groothuis 	int     i, j;
6760851fbdfSEdwin Groothuis 	int     mpl;
6770851fbdfSEdwin Groothuis 	int     mw;
6780851fbdfSEdwin Groothuis 	int	m1, m2;
6790851fbdfSEdwin Groothuis 	int	prevyear = -1;
6800851fbdfSEdwin Groothuis 	int	printyearheader;
6810851fbdfSEdwin Groothuis 
6820851fbdfSEdwin Groothuis 	mpl = jd_flag ? 3 : 4;
6830851fbdfSEdwin Groothuis 	mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R;
6840851fbdfSEdwin Groothuis 
6850851fbdfSEdwin Groothuis 	while (before != 0) {
6860851fbdfSEdwin Groothuis 		DECREASEMONTH(m, y);
6870851fbdfSEdwin Groothuis 		before--;
6880851fbdfSEdwin Groothuis 		after++;
6890851fbdfSEdwin Groothuis 	}
6900851fbdfSEdwin Groothuis 	m1 = y * 12 + m - 1;
6910851fbdfSEdwin Groothuis 	m2 = m1 + after;
6920851fbdfSEdwin Groothuis 
6930851fbdfSEdwin Groothuis 	mkweekdays(&wds);
6940851fbdfSEdwin Groothuis 
6950851fbdfSEdwin Groothuis 	/*
6960851fbdfSEdwin Groothuis 	 * The year header is printed when there are more than 'mpl' months
6970851fbdfSEdwin Groothuis 	 * and if the first month is a multitude of 'mpl'.
6980851fbdfSEdwin Groothuis 	 * If not, it will print the year behind every month.
6990851fbdfSEdwin Groothuis 	 */
7000851fbdfSEdwin Groothuis 	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
7010851fbdfSEdwin Groothuis 
7020851fbdfSEdwin Groothuis 	m = m1;
7030851fbdfSEdwin Groothuis 	while (m <= m2) {
7040851fbdfSEdwin Groothuis 		int count = 0;
7050851fbdfSEdwin Groothuis 		for (i = 0; i != mpl && m + i <= m2; i++) {
7060851fbdfSEdwin Groothuis 			mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
7070851fbdfSEdwin Groothuis 			count++;
7080851fbdfSEdwin Groothuis 		}
7090851fbdfSEdwin Groothuis 
7104af997a8SEdwin Groothuis 		/* Empty line between two rows of months. */
7110851fbdfSEdwin Groothuis 		if (m != m1)
7120851fbdfSEdwin Groothuis 			printf("\n");
7130851fbdfSEdwin Groothuis 
7144af997a8SEdwin Groothuis 		/* Year at the top. */
7150851fbdfSEdwin Groothuis 		if (printyearheader && M2Y(m) != prevyear) {
7160851fbdfSEdwin Groothuis 			sprintf(s, "%d", M2Y(m));
7170851fbdfSEdwin Groothuis 			printf("%s\n", center(t, s, mpl * mw));
7180851fbdfSEdwin Groothuis 			prevyear = M2Y(m);
7190851fbdfSEdwin Groothuis 		}
7200851fbdfSEdwin Groothuis 
7214af997a8SEdwin Groothuis 		/* Month names. */
7220851fbdfSEdwin Groothuis 		wprintf(L"    ");
7230851fbdfSEdwin Groothuis 		for (i = 0; i < count; i++)
7240851fbdfSEdwin Groothuis 			if (printyearheader)
7250851fbdfSEdwin Groothuis 				wprintf(L"%-*ls", mw, year[i].name);
7260851fbdfSEdwin Groothuis 			else
7270851fbdfSEdwin Groothuis 				wprintf(L"%-ls %-*d", year[i].name,
7280851fbdfSEdwin Groothuis 				    mw - wcslen(year[i].name) - 1, M2Y(m + i));
7290851fbdfSEdwin Groothuis 		printf("\n");
7300851fbdfSEdwin Groothuis 
7314af997a8SEdwin Groothuis 		/* And the days of the month. */
7320851fbdfSEdwin Groothuis 		for (i = 0; i != 7; i++) {
7330851fbdfSEdwin Groothuis 			/* Week day */
7340851fbdfSEdwin Groothuis 			wprintf(L"%.2ls", wds.names[i]);
7350851fbdfSEdwin Groothuis 
7360851fbdfSEdwin Groothuis 			/* Full months */
7370851fbdfSEdwin Groothuis 			for (j = 0; j < count; j++)
7380851fbdfSEdwin Groothuis 				printf("%-*s",
7391d0e1dacSEdwin Groothuis 				    MW(mw, year[j].extralen[i]),
7401d0e1dacSEdwin Groothuis 					year[j].lines[i]);
7410851fbdfSEdwin Groothuis 			printf("\n");
7420851fbdfSEdwin Groothuis 		}
7430851fbdfSEdwin Groothuis 
7444af997a8SEdwin Groothuis 		/* Week numbers. */
7450851fbdfSEdwin Groothuis 		if (flag_weeks) {
7460851fbdfSEdwin Groothuis 			printf("  ");
7470851fbdfSEdwin Groothuis 			for (i = 0; i < count; i++)
7480851fbdfSEdwin Groothuis 				printf("%-*s", mw, year[i].weeks);
7490851fbdfSEdwin Groothuis 			printf("\n");
7500851fbdfSEdwin Groothuis 		}
7510851fbdfSEdwin Groothuis 
7520851fbdfSEdwin Groothuis 		m += mpl;
7530851fbdfSEdwin Groothuis 	}
7540851fbdfSEdwin Groothuis 	return;
7550851fbdfSEdwin Groothuis }
7560851fbdfSEdwin Groothuis 
7570851fbdfSEdwin Groothuis void
7580851fbdfSEdwin Groothuis mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines)
7590cb2e609SWolfgang Helbig {
7600cb2e609SWolfgang Helbig 
7610cb2e609SWolfgang Helbig 	struct tm tm;		/* for strftime printing local names of
7620cb2e609SWolfgang Helbig 				 * months */
7630cb2e609SWolfgang Helbig 	date    dt;		/* handy date */
7640cb2e609SWolfgang Helbig 	int     dw;		/* width of numbers */
7650cb2e609SWolfgang Helbig 	int     first;		/* first day of month */
7660cb2e609SWolfgang Helbig 	int     firstm;		/* first day of first week of month */
767e454a171SRoman Divacky 	int     i, j, k, l;	/* just indices */
7680cb2e609SWolfgang Helbig 	int     last;		/* the first day of next month */
7690cb2e609SWolfgang Helbig 	int     jan1 = 0;	/* the first day of this year */
7700cb2e609SWolfgang Helbig 	char   *ds;		/* pointer to day strings (daystr or
7710cb2e609SWolfgang Helbig 				 * jdaystr) */
7720cb2e609SWolfgang Helbig 
7730cb2e609SWolfgang Helbig 	/* Set name of month. */
7740cb2e609SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
7750cb2e609SWolfgang Helbig 	tm.tm_mon = m;
7764646cea7SDavid Schultz 	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
7774646cea7SDavid Schultz 		 L"%OB", &tm);
7784646cea7SDavid Schultz 	mlines->name[0] = towupper(mlines->name[0]);
7790cb2e609SWolfgang Helbig 
7800cb2e609SWolfgang Helbig 	/*
7810cb2e609SWolfgang Helbig 	 * Set first and last to the day number of the first day of this
7820cb2e609SWolfgang Helbig 	 * month and the first day of next month respectively. Set jan1 to
783f8a0edbaSWolfgang Helbig 	 * the day number of the first day of this year.
7840cb2e609SWolfgang Helbig 	 */
785f8a0edbaSWolfgang Helbig 	first = firstday(y, m + 1);
786f8a0edbaSWolfgang Helbig 	if (m == 11)
787f8a0edbaSWolfgang Helbig 		last = firstday(y + 1, 1);
788f8a0edbaSWolfgang Helbig 	else
789f8a0edbaSWolfgang Helbig 		last = firstday(y, m + 2);
7900cb2e609SWolfgang Helbig 
791f8a0edbaSWolfgang Helbig 	if (jd_flag)
792630d15bbSWolfgang Helbig 		jan1 = firstday(y, 1);
7930cb2e609SWolfgang Helbig 
7940cb2e609SWolfgang Helbig 	/*
7950cb2e609SWolfgang Helbig 	 * Set firstm to the day number of monday of the first week of
7960cb2e609SWolfgang Helbig 	 * this month. (This might be in the last month)
7970cb2e609SWolfgang Helbig 	 */
7980cb2e609SWolfgang Helbig 	firstm = first - weekday(first);
7990cb2e609SWolfgang Helbig 
8004af997a8SEdwin Groothuis 	/* Set ds (daystring) and dw (daywidth) according to the jd_flag. */
8010cb2e609SWolfgang Helbig 	if (jd_flag) {
8020cb2e609SWolfgang Helbig 		ds = jdaystr;
8030cb2e609SWolfgang Helbig 		dw = 4;
8040cb2e609SWolfgang Helbig 	} else {
8050cb2e609SWolfgang Helbig 		ds = daystr;
8060cb2e609SWolfgang Helbig 		dw = 3;
8070cb2e609SWolfgang Helbig 	}
8080cb2e609SWolfgang Helbig 
8090cb2e609SWolfgang Helbig 	/*
8100cb2e609SWolfgang Helbig 	 * Fill the lines with day of month or day of year (julian day)
8110cb2e609SWolfgang Helbig 	 * line index: i, each line is one weekday. column index: j, each
8120cb2e609SWolfgang Helbig 	 * column is one day number. print column index: k.
8130cb2e609SWolfgang Helbig 	 */
8140cb2e609SWolfgang Helbig 	for (i = 0; i != 7; i++) {
815e454a171SRoman Divacky 		l = 0;
816e454a171SRoman Divacky 		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
8170cb2e609SWolfgang Helbig 			if (j >= first) {
8180cb2e609SWolfgang Helbig 				if (jd_flag)
8190cb2e609SWolfgang Helbig 					dt.d = j - jan1 + 1;
8200cb2e609SWolfgang Helbig 				else
8210851fbdfSEdwin Groothuis 					sdater(j, &dt);
8224af997a8SEdwin Groothuis 				if (j == highlightdate && !flag_nohighlight)
823cae11c25SEdwin Groothuis 					highlight(mlines->lines[i] + k,
824cae11c25SEdwin Groothuis 					    ds + dt.d * dw, dw, &l);
825cae11c25SEdwin Groothuis 				else
826e454a171SRoman Divacky 					memcpy(mlines->lines[i] + k + l,
8270cb2e609SWolfgang Helbig 					       ds + dt.d * dw, dw);
8280cb2e609SWolfgang Helbig 			} else
829e454a171SRoman Divacky 				memcpy(mlines->lines[i] + k + l, "    ", dw);
830e454a171SRoman Divacky 		}
831e454a171SRoman Divacky 		mlines->lines[i][k + l] = '\0';
8321d0e1dacSEdwin Groothuis 		mlines->extralen[i] = l;
8330cb2e609SWolfgang Helbig 	}
8340cb2e609SWolfgang Helbig 
8354af997a8SEdwin Groothuis 	/* fill the weeknumbers. */
8360cb2e609SWolfgang Helbig 	if (flag_weeks) {
8370cb2e609SWolfgang Helbig 		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
8380cb2e609SWolfgang Helbig 			if (j <= nswitch)
8390cb2e609SWolfgang Helbig 				memset(mlines->weeks + k, ' ', dw);
8400cb2e609SWolfgang Helbig 			else
8410cb2e609SWolfgang Helbig 				memcpy(mlines->weeks + k,
8420cb2e609SWolfgang Helbig 				    ds + week(j, &i)*dw, dw);
8430cb2e609SWolfgang Helbig 		mlines->weeks[k] = '\0';
8440cb2e609SWolfgang Helbig 	}
8450cb2e609SWolfgang Helbig }
8460cb2e609SWolfgang Helbig 
8470cb2e609SWolfgang Helbig void
8480cb2e609SWolfgang Helbig mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
8490cb2e609SWolfgang Helbig {
8500cb2e609SWolfgang Helbig 
8510cb2e609SWolfgang Helbig 	struct tm tm;		/* for strftime printing local names of
8520cb2e609SWolfgang Helbig 				 * months */
8530cb2e609SWolfgang Helbig 	date    dt;		/* handy date */
8540cb2e609SWolfgang Helbig 	int     dw;		/* width of numbers */
8550cb2e609SWolfgang Helbig 	int     first;		/* first day of month */
8560cb2e609SWolfgang Helbig 	int     firsts;		/* sunday of first week of month */
857e454a171SRoman Divacky 	int     i, j, k, l;	/* just indices */
8580cb2e609SWolfgang Helbig 	int     jan1 = 0;	/* the first day of this year */
8590cb2e609SWolfgang Helbig 	int     last;		/* the first day of next month */
8600cb2e609SWolfgang Helbig 	char   *ds;		/* pointer to day strings (daystr or
8610cb2e609SWolfgang Helbig 				 * jdaystr) */
8620cb2e609SWolfgang Helbig 
8630cb2e609SWolfgang Helbig 	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
8640cb2e609SWolfgang Helbig 	if (jd_flag) {
8650cb2e609SWolfgang Helbig 		ds = jdaystr;
8660cb2e609SWolfgang Helbig 		dw = 4;
8670cb2e609SWolfgang Helbig 	} else {
8680cb2e609SWolfgang Helbig 		ds = daystr;
8690cb2e609SWolfgang Helbig 		dw = 3;
8700cb2e609SWolfgang Helbig 	}
8710cb2e609SWolfgang Helbig 
8724af997a8SEdwin Groothuis 	/* Set name of month centered. */
8730cb2e609SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
8740cb2e609SWolfgang Helbig 	tm.tm_mon = m;
8754646cea7SDavid Schultz 	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
8764646cea7SDavid Schultz 		 L"%OB", &tm);
8774646cea7SDavid Schultz 	mlines->name[0] = towupper(mlines->name[0]);
8780cb2e609SWolfgang Helbig 
8790cb2e609SWolfgang Helbig 	/*
8800cb2e609SWolfgang Helbig 	 * Set first and last to the day number of the first day of this
8810cb2e609SWolfgang Helbig 	 * month and the first day of next month respectively. Set jan1 to
8820cb2e609SWolfgang Helbig 	 * the day number of Jan 1st of this year.
8830cb2e609SWolfgang Helbig 	 */
8840cb2e609SWolfgang Helbig 	dt.y = y;
8850cb2e609SWolfgang Helbig 	dt.m = m + 1;
8860cb2e609SWolfgang Helbig 	dt.d = 1;
8870cb2e609SWolfgang Helbig 	first = sndaysb(&dt);
8880cb2e609SWolfgang Helbig 	if (m == 11) {
8890cb2e609SWolfgang Helbig 		dt.y = y + 1;
8900cb2e609SWolfgang Helbig 		dt.m = 1;
8910cb2e609SWolfgang Helbig 		dt.d = 1;
8920cb2e609SWolfgang Helbig 	} else {
8930cb2e609SWolfgang Helbig 		dt.y = y;
8940cb2e609SWolfgang Helbig 		dt.m = m + 2;
8950cb2e609SWolfgang Helbig 		dt.d = 1;
8960cb2e609SWolfgang Helbig 	}
8970cb2e609SWolfgang Helbig 	last = sndaysb(&dt);
8980cb2e609SWolfgang Helbig 
8990cb2e609SWolfgang Helbig 	if (jd_flag) {
9000cb2e609SWolfgang Helbig 		dt.y = y;
9010cb2e609SWolfgang Helbig 		dt.m = 1;
9020cb2e609SWolfgang Helbig 		dt.d = 1;
9030cb2e609SWolfgang Helbig 		jan1 = sndaysb(&dt);
9040cb2e609SWolfgang Helbig 	}
9050cb2e609SWolfgang Helbig 
9060cb2e609SWolfgang Helbig 	/*
9070cb2e609SWolfgang Helbig 	 * Set firsts to the day number of sunday of the first week of
9080cb2e609SWolfgang Helbig 	 * this month. (This might be in the last month)
9090cb2e609SWolfgang Helbig 	 */
9100cb2e609SWolfgang Helbig 	firsts = first - (weekday(first)+1) % 7;
9110cb2e609SWolfgang Helbig 
9120cb2e609SWolfgang Helbig 	/*
9130cb2e609SWolfgang Helbig 	 * Fill the lines with day of month or day of year (Julian day)
9140cb2e609SWolfgang Helbig 	 * line index: i, each line is one week. column index: j, each
9150cb2e609SWolfgang Helbig 	 * column is one day number. print column index: k.
9160cb2e609SWolfgang Helbig 	 */
9170cb2e609SWolfgang Helbig 	for (i = 0; i != 6; i++) {
918e454a171SRoman Divacky 		l = 0;
9190cb2e609SWolfgang Helbig 		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
920e454a171SRoman Divacky 		    j++, k += dw) {
9210cb2e609SWolfgang Helbig 			if (j >= first) {
9220cb2e609SWolfgang Helbig 				if (jd_flag)
9230cb2e609SWolfgang Helbig 					dt.d = j - jan1 + 1;
9240cb2e609SWolfgang Helbig 				else
9250cb2e609SWolfgang Helbig 					sdateb(j, &dt);
9264af997a8SEdwin Groothuis 				if (j == highlightdate && !flag_nohighlight)
927cae11c25SEdwin Groothuis 					highlight(mlines->lines[i] + k,
928cae11c25SEdwin Groothuis 					    ds + dt.d * dw, dw, &l);
929cae11c25SEdwin Groothuis 				else
930e454a171SRoman Divacky 					memcpy(mlines->lines[i] + k + l,
9310cb2e609SWolfgang Helbig 					       ds + dt.d * dw, dw);
9320cb2e609SWolfgang Helbig 			} else
933e454a171SRoman Divacky 				memcpy(mlines->lines[i] + k + l, "    ", dw);
934e454a171SRoman Divacky 		}
9350cb2e609SWolfgang Helbig 		if (k == 0)
9360cb2e609SWolfgang Helbig 			mlines->lines[i][1] = '\0';
9370cb2e609SWolfgang Helbig 		else
938e454a171SRoman Divacky 			mlines->lines[i][k + l] = '\0';
9391d0e1dacSEdwin Groothuis 		mlines->extralen[i] = l;
9400cb2e609SWolfgang Helbig 	}
9410cb2e609SWolfgang Helbig }
9420cb2e609SWolfgang Helbig 
9434af997a8SEdwin Groothuis /* Put the local names of weekdays into the wds. */
944a4264dceSWolfgang Helbig void
945a4264dceSWolfgang Helbig mkweekdays(struct weekdays *wds)
9460cb2e609SWolfgang Helbig {
947bd97a998SHajimu UMEMOTO 	int i, len, width = 0;
9480cb2e609SWolfgang Helbig 	struct tm tm;
9494646cea7SDavid Schultz 	wchar_t buf[20];
9500cb2e609SWolfgang Helbig 
9510cb2e609SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
9520cb2e609SWolfgang Helbig 
9530cb2e609SWolfgang Helbig 	for (i = 0; i != 7; i++) {
9540cb2e609SWolfgang Helbig 		tm.tm_wday = (i+1) % 7;
9554646cea7SDavid Schultz 		wcsftime(buf, sizeof(buf), L"%a", &tm);
95646f39347SHajimu UMEMOTO 		for (len = 2; len > 0; --len) {
957bd97a998SHajimu UMEMOTO 			if ((width = wcswidth(buf, len)) <= 2)
958bd97a998SHajimu UMEMOTO 				break;
959bd97a998SHajimu UMEMOTO 		}
960bd97a998SHajimu UMEMOTO 		wmemset(wds->names[i], L'\0', 4);
961bd97a998SHajimu UMEMOTO 		if (width == 1)
962bd97a998SHajimu UMEMOTO 			wds->names[i][0] = L' ';
963bd97a998SHajimu UMEMOTO 		wcsncat(wds->names[i], buf, len);
96446f39347SHajimu UMEMOTO 		wcsncat(wds->names[i], L" ", 1);
9650cb2e609SWolfgang Helbig 	}
9660cb2e609SWolfgang Helbig }
9670cb2e609SWolfgang Helbig 
9680cb2e609SWolfgang Helbig /*
9694af997a8SEdwin Groothuis  * Compute the day number of the first existing date after the first day in
9704af997a8SEdwin Groothuis  * month. (the first day in month and even the month might not exist!)
971f8a0edbaSWolfgang Helbig  */
972f8a0edbaSWolfgang Helbig int
973f8a0edbaSWolfgang Helbig firstday(int y, int m)
974f8a0edbaSWolfgang Helbig {
975f8a0edbaSWolfgang Helbig 	date dt;
976f8a0edbaSWolfgang Helbig 	int nd;
977f8a0edbaSWolfgang Helbig 
978f8a0edbaSWolfgang Helbig 	dt.y = y;
979f8a0edbaSWolfgang Helbig 	dt.m = m;
980f8a0edbaSWolfgang Helbig 	dt.d = 1;
9810851fbdfSEdwin Groothuis 	nd = sndaysr(&dt);
982cf5f6adfSWolfgang Helbig 	for (;;) {
9830851fbdfSEdwin Groothuis 		sdater(nd, &dt);
984cf5f6adfSWolfgang Helbig 		if ((dt.m >= m && dt.y == y) || dt.y > y)
985f8a0edbaSWolfgang Helbig 			return (nd);
986cf5f6adfSWolfgang Helbig 		else
987cf5f6adfSWolfgang Helbig 			nd++;
988cf5f6adfSWolfgang Helbig 	}
989cf5f6adfSWolfgang Helbig 	/* NEVER REACHED */
990f8a0edbaSWolfgang Helbig }
991f8a0edbaSWolfgang Helbig 
992f8a0edbaSWolfgang Helbig /*
9930cb2e609SWolfgang Helbig  * Compute the number of days from date, obey the local switch from
9940cb2e609SWolfgang Helbig  * Julian to Gregorian if specified by the user.
9950cb2e609SWolfgang Helbig  */
9960cb2e609SWolfgang Helbig int
9970851fbdfSEdwin Groothuis sndaysr(struct date *d)
9980cb2e609SWolfgang Helbig {
9990cb2e609SWolfgang Helbig 
10000cb2e609SWolfgang Helbig 	if (nswitch != 0)
10010cb2e609SWolfgang Helbig 		if (nswitch < ndaysj(d))
10020cb2e609SWolfgang Helbig 			return (ndaysg(d));
10030cb2e609SWolfgang Helbig 		else
10040cb2e609SWolfgang Helbig 			return (ndaysj(d));
10050cb2e609SWolfgang Helbig 	else
10060cb2e609SWolfgang Helbig 		return ndaysg(d);
10070cb2e609SWolfgang Helbig }
10080cb2e609SWolfgang Helbig 
10090cb2e609SWolfgang Helbig /*
10100cb2e609SWolfgang Helbig  * Compute the number of days from date, obey the switch from
10110cb2e609SWolfgang Helbig  * Julian to Gregorian as used by UK and her colonies.
10120cb2e609SWolfgang Helbig  */
10130cb2e609SWolfgang Helbig int
10140cb2e609SWolfgang Helbig sndaysb(struct date *d)
10150cb2e609SWolfgang Helbig {
10160cb2e609SWolfgang Helbig 
10170cb2e609SWolfgang Helbig 	if (nswitchb < ndaysj(d))
10180cb2e609SWolfgang Helbig 		return (ndaysg(d));
10190cb2e609SWolfgang Helbig 	else
10200cb2e609SWolfgang Helbig 		return (ndaysj(d));
10210cb2e609SWolfgang Helbig }
10220cb2e609SWolfgang Helbig 
10234af997a8SEdwin Groothuis /* Inverse of sndays. */
10240cb2e609SWolfgang Helbig struct date *
10250851fbdfSEdwin Groothuis sdater(int nd, struct date *d)
10260cb2e609SWolfgang Helbig {
10270cb2e609SWolfgang Helbig 
10280cb2e609SWolfgang Helbig 	if (nswitch < nd)
10290cb2e609SWolfgang Helbig 		return (gdate(nd, d));
10300cb2e609SWolfgang Helbig 	else
10310cb2e609SWolfgang Helbig 		return (jdate(nd, d));
10320cb2e609SWolfgang Helbig }
10330cb2e609SWolfgang Helbig 
10344af997a8SEdwin Groothuis /* Inverse of sndaysb. */
10350cb2e609SWolfgang Helbig struct date *
10360cb2e609SWolfgang Helbig sdateb(int nd, struct date *d)
10370cb2e609SWolfgang Helbig {
10380cb2e609SWolfgang Helbig 
10390cb2e609SWolfgang Helbig 	if (nswitchb < nd)
10400cb2e609SWolfgang Helbig 		return (gdate(nd, d));
10410cb2e609SWolfgang Helbig 	else
10420cb2e609SWolfgang Helbig 		return (jdate(nd, d));
10430cb2e609SWolfgang Helbig }
10440cb2e609SWolfgang Helbig 
10454af997a8SEdwin Groothuis /* Center string t in string s of length w by putting enough leading blanks. */
10460cb2e609SWolfgang Helbig char *
10470cb2e609SWolfgang Helbig center(char *s, char *t, int w)
10480cb2e609SWolfgang Helbig {
10490851fbdfSEdwin Groothuis 	char blanks[MAX_WIDTH];
10500cb2e609SWolfgang Helbig 
10510cb2e609SWolfgang Helbig 	memset(blanks, ' ', sizeof(blanks));
10520cb2e609SWolfgang Helbig 	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
10530cb2e609SWolfgang Helbig 	return (s);
10540cb2e609SWolfgang Helbig }
1055ac6d1c22SPeter Pentchev 
10564af997a8SEdwin Groothuis /* Center string t in string s of length w by putting enough leading blanks. */
10574646cea7SDavid Schultz wchar_t *
10584646cea7SDavid Schultz wcenter(wchar_t *s, wchar_t *t, int w)
10594646cea7SDavid Schultz {
10600851fbdfSEdwin Groothuis 	char blanks[MAX_WIDTH];
10614646cea7SDavid Schultz 
10624646cea7SDavid Schultz 	memset(blanks, ' ', sizeof(blanks));
10634646cea7SDavid Schultz 	swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
10644646cea7SDavid Schultz 	return (s);
10654646cea7SDavid Schultz }
10664646cea7SDavid Schultz 
1067ac6d1c22SPeter Pentchev int
1068ba29aec0SGarrett Wollman parsemonth(const char *s, int *m, int *y)
1069ac6d1c22SPeter Pentchev {
1070ba29aec0SGarrett Wollman 	int nm, ny;
1071ac6d1c22SPeter Pentchev 	char *cp;
1072ac6d1c22SPeter Pentchev 	struct tm tm;
1073ac6d1c22SPeter Pentchev 
1074ba29aec0SGarrett Wollman 	nm = (int)strtol(s, &cp, 10);
1075ba29aec0SGarrett Wollman 	if (cp != s) {
1076ba29aec0SGarrett Wollman 		ny = *y;
1077ba29aec0SGarrett Wollman 		if (*cp == '\0') {
1078ba29aec0SGarrett Wollman 			;	/* no special action */
1079ba29aec0SGarrett Wollman 		} else if (*cp == 'f' || *cp == 'F') {
1080ba29aec0SGarrett Wollman 			if (nm <= *m)
1081ba29aec0SGarrett Wollman 				ny++;
1082ba29aec0SGarrett Wollman 		} else if (*cp == 'p' || *cp == 'P') {
1083ba29aec0SGarrett Wollman 			if (nm >= *m)
1084ba29aec0SGarrett Wollman 				ny--;
1085ba29aec0SGarrett Wollman 		} else
1086ba29aec0SGarrett Wollman 			return (1);
1087ba29aec0SGarrett Wollman 		if (nm < 1 || nm > 12)
1088ba29aec0SGarrett Wollman 			return 1;
1089ba29aec0SGarrett Wollman 		*m = nm;
1090ba29aec0SGarrett Wollman 		*y = ny;
1091ac6d1c22SPeter Pentchev 		return (0);
1092ac6d1c22SPeter Pentchev 	}
1093ba29aec0SGarrett Wollman 	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
1094ba29aec0SGarrett Wollman 		*m = tm.tm_mon + 1;
1095ba29aec0SGarrett Wollman 		return (0);
1096ba29aec0SGarrett Wollman 	}
1097ba29aec0SGarrett Wollman 	return (1);
1098ba29aec0SGarrett Wollman }
1099cae11c25SEdwin Groothuis 
1100cae11c25SEdwin Groothuis void
1101cae11c25SEdwin Groothuis highlight(char *dst, char *src, int len, int *extralen)
1102cae11c25SEdwin Groothuis {
1103cae11c25SEdwin Groothuis 	static int first = 1;
1104cae11c25SEdwin Groothuis 	static const char *term_so, *term_se;
1105cae11c25SEdwin Groothuis 
1106cae11c25SEdwin Groothuis 	if (first) {
1107cae11c25SEdwin Groothuis 		char tbuf[1024], cbuf[512], *b;
1108cae11c25SEdwin Groothuis 
1109cae11c25SEdwin Groothuis 		term_se = term_so = NULL;
1110cae11c25SEdwin Groothuis 
11114af997a8SEdwin Groothuis 		/* On how to highlight on this type of terminal (if any). */
1112cae11c25SEdwin Groothuis 		if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1113cae11c25SEdwin Groothuis 			b = cbuf;
1114cae11c25SEdwin Groothuis 			term_so = tgetstr("so", &b);
1115cae11c25SEdwin Groothuis 			term_se = tgetstr("se", &b);
1116cae11c25SEdwin Groothuis 		}
1117cae11c25SEdwin Groothuis 
1118cae11c25SEdwin Groothuis 		first = 0;
1119cae11c25SEdwin Groothuis 	}
1120cae11c25SEdwin Groothuis 
1121cae11c25SEdwin Groothuis 	/*
1122cae11c25SEdwin Groothuis 	 * This check is not necessary, should have been handled before calling
1123cae11c25SEdwin Groothuis 	 * this function.
1124cae11c25SEdwin Groothuis 	 */
1125cae11c25SEdwin Groothuis 	if (flag_nohighlight) {
1126cae11c25SEdwin Groothuis 		memcpy(dst, src, len);
1127cae11c25SEdwin Groothuis 		return;
1128cae11c25SEdwin Groothuis 	}
1129cae11c25SEdwin Groothuis 
11304af997a8SEdwin Groothuis 	/*
11314af997a8SEdwin Groothuis 	 * If it is a real terminal, use the data from the termcap database.
11324af997a8SEdwin Groothuis 	 */
1133cae11c25SEdwin Groothuis 	if (term_so != NULL && term_se != NULL) {
11344af997a8SEdwin Groothuis 		/* separator. */
1135cae11c25SEdwin Groothuis 		dst[0] = ' ';
1136cae11c25SEdwin Groothuis 		dst++;
11374af997a8SEdwin Groothuis 		/* highlight on. */
1138cae11c25SEdwin Groothuis 		memcpy(dst, term_so, strlen(term_so));
1139cae11c25SEdwin Groothuis 		dst += strlen(term_so);
11404af997a8SEdwin Groothuis 		/* the actual text. (minus leading space) */
1141cae11c25SEdwin Groothuis 		len--;
1142cae11c25SEdwin Groothuis 		src++;
1143cae11c25SEdwin Groothuis 		memcpy(dst, src, len);
1144cae11c25SEdwin Groothuis 		dst += len;
11454af997a8SEdwin Groothuis 		/* highlight off. */
1146cae11c25SEdwin Groothuis 		memcpy(dst, term_se, strlen(term_se));
1147cae11c25SEdwin Groothuis 		*extralen = strlen(term_so) + strlen(term_se);
1148cae11c25SEdwin Groothuis 		return;
1149cae11c25SEdwin Groothuis 	}
1150cae11c25SEdwin Groothuis 
1151cae11c25SEdwin Groothuis 	/*
11524af997a8SEdwin Groothuis 	 * Otherwise, print a _, backspace and the letter.
1153cae11c25SEdwin Groothuis 	 */
1154cae11c25SEdwin Groothuis 	*extralen = 0;
11554af997a8SEdwin Groothuis 	/* skip leading space. */
1156cae11c25SEdwin Groothuis 	src++;
1157cae11c25SEdwin Groothuis 	len--;
11584af997a8SEdwin Groothuis 	/* separator. */
1159cae11c25SEdwin Groothuis 	dst[0] = ' ';
1160cae11c25SEdwin Groothuis 	dst++;
1161cae11c25SEdwin Groothuis 	while (len > 0) {
11624af997a8SEdwin Groothuis 		/* _ and backspace. */
1163cae11c25SEdwin Groothuis 		memcpy(dst, "_\010", 2);
1164cae11c25SEdwin Groothuis 		dst += 2;
1165cae11c25SEdwin Groothuis 		*extralen += 2;
11664af997a8SEdwin Groothuis 		/* the character. */
1167cae11c25SEdwin Groothuis 		*dst++ = *src++;
1168cae11c25SEdwin Groothuis 		len--;
1169cae11c25SEdwin Groothuis 	}
11704af997a8SEdwin Groothuis 	return;
1171cae11c25SEdwin Groothuis }
1172