xref: /freebsd/usr.bin/ncal/ncal.c (revision 5903d9c00b75f17f09404d1001ee5aaae7603a61)
10cb2e609SWolfgang Helbig /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
31de7b4b8SPedro F. Giffuni  *
40cb2e609SWolfgang Helbig  * Copyright (c) 1997 Wolfgang Helbig
50cb2e609SWolfgang Helbig  * All rights reserved.
60cb2e609SWolfgang Helbig  *
70cb2e609SWolfgang Helbig  * Redistribution and use in source and binary forms, with or without
80cb2e609SWolfgang Helbig  * modification, are permitted provided that the following conditions
90cb2e609SWolfgang Helbig  * are met:
100cb2e609SWolfgang Helbig  * 1. Redistributions of source code must retain the above copyright
110cb2e609SWolfgang Helbig  *    notice, this list of conditions and the following disclaimer.
120cb2e609SWolfgang Helbig  * 2. Redistributions in binary form must reproduce the above copyright
130cb2e609SWolfgang Helbig  *    notice, this list of conditions and the following disclaimer in the
140cb2e609SWolfgang Helbig  *    documentation and/or other materials provided with the distribution.
150cb2e609SWolfgang Helbig  *
160cb2e609SWolfgang Helbig  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170cb2e609SWolfgang Helbig  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180cb2e609SWolfgang Helbig  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190cb2e609SWolfgang Helbig  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200cb2e609SWolfgang Helbig  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210cb2e609SWolfgang Helbig  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220cb2e609SWolfgang Helbig  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230cb2e609SWolfgang Helbig  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240cb2e609SWolfgang Helbig  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250cb2e609SWolfgang Helbig  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260cb2e609SWolfgang Helbig  * SUCH DAMAGE.
270cb2e609SWolfgang Helbig  */
28a99e4564SPhilippe Charnier 
29bf70beceSEd Schouten #include <sys/cdefs.h>
30bf70beceSEd Schouten __FBSDID("$FreeBSD$");
31a99e4564SPhilippe Charnier 
320cb2e609SWolfgang Helbig #include <calendar.h>
33821df508SXin LI #include <ctype.h>
340cb2e609SWolfgang Helbig #include <err.h>
35f5e40af2SAndrey A. Chernov #include <langinfo.h>
36746b67cdSEdwin Groothuis #include <libgen.h>
370cb2e609SWolfgang Helbig #include <locale.h>
380cb2e609SWolfgang Helbig #include <stdio.h>
390cb2e609SWolfgang Helbig #include <stdlib.h>
400cb2e609SWolfgang Helbig #include <string.h>
410cb2e609SWolfgang Helbig #include <sysexits.h>
420cb2e609SWolfgang Helbig #include <time.h>
430cb2e609SWolfgang Helbig #include <unistd.h>
444646cea7SDavid Schultz #include <wchar.h>
454646cea7SDavid Schultz #include <wctype.h>
46e454a171SRoman Divacky #include <term.h>
47e454a171SRoman Divacky #undef lines			/* term.h defines this */
480cb2e609SWolfgang Helbig 
490851fbdfSEdwin Groothuis /* Width of one month with backward compatibility and in regular mode*/
500cb2e609SWolfgang Helbig #define MONTH_WIDTH_B_J 27
510cb2e609SWolfgang Helbig #define MONTH_WIDTH_B 20
520cb2e609SWolfgang Helbig 
530851fbdfSEdwin Groothuis #define MONTH_WIDTH_R_J 24
540851fbdfSEdwin Groothuis #define MONTH_WIDTH_R 18
550cb2e609SWolfgang Helbig 
56e454a171SRoman Divacky #define MAX_WIDTH 64
570cb2e609SWolfgang Helbig 
580cb2e609SWolfgang Helbig typedef struct date date;
590cb2e609SWolfgang Helbig 
600cb2e609SWolfgang Helbig struct monthlines {
614646cea7SDavid Schultz 	wchar_t name[MAX_WIDTH + 1];
620cb2e609SWolfgang Helbig 	char lines[7][MAX_WIDTH + 1];
630cb2e609SWolfgang Helbig 	char weeks[MAX_WIDTH + 1];
641d0e1dacSEdwin Groothuis 	unsigned int extralen[7];
650cb2e609SWolfgang Helbig };
660cb2e609SWolfgang Helbig 
670cb2e609SWolfgang Helbig struct weekdays {
684646cea7SDavid Schultz 	wchar_t names[7][4];
690cb2e609SWolfgang Helbig };
700cb2e609SWolfgang Helbig 
710cb2e609SWolfgang Helbig /* The switches from Julian to Gregorian in some countries */
720cb2e609SWolfgang Helbig static struct djswitch {
73f372d010SMark Murray 	const char *cc;	/* Country code according to ISO 3166 */
74f372d010SMark Murray 	const char *nm;	/* Name of country */
750cb2e609SWolfgang Helbig 	date dt;	/* Last day of Julian calendar */
760cb2e609SWolfgang Helbig } switches[] = {
779d969a8bSAndrey A. Chernov 	{"AL", "Albania",       {1912, 11, 30}},
789d969a8bSAndrey A. Chernov 	{"AT", "Austria",       {1583, 10,  5}},
799d969a8bSAndrey A. Chernov 	{"AU", "Australia",     {1752,  9,  2}},
809d969a8bSAndrey A. Chernov 	{"BE", "Belgium",       {1582, 12, 14}},
811916d3b4SWarner Losh 	{"BG", "Bulgaria",      {1916,  3, 31}},
829d969a8bSAndrey A. Chernov 	{"CA", "Canada",        {1752,  9,  2}},
839d969a8bSAndrey A. Chernov 	{"CH", "Switzerland",   {1655,  2, 28}},
849d969a8bSAndrey A. Chernov 	{"CN", "China",         {1911, 12, 18}},
859d969a8bSAndrey A. Chernov 	{"CZ", "Czech Republic",{1584,  1,  6}},
869d969a8bSAndrey A. Chernov 	{"DE", "Germany",       {1700,  2, 18}},
879d969a8bSAndrey A. Chernov 	{"DK", "Denmark",       {1700,  2, 18}},
889d969a8bSAndrey A. Chernov 	{"ES", "Spain",         {1582, 10,  4}},
899d969a8bSAndrey A. Chernov 	{"FI", "Finland",       {1753,  2, 17}},
909d969a8bSAndrey A. Chernov 	{"FR", "France",        {1582, 12,  9}},
919d969a8bSAndrey A. Chernov 	{"GB", "United Kingdom",{1752,  9,  2}},
929d969a8bSAndrey A. Chernov 	{"GR", "Greece",        {1924,  3,  9}},
939d969a8bSAndrey A. Chernov 	{"HU", "Hungary",       {1587, 10, 21}},
949d969a8bSAndrey A. Chernov 	{"IS", "Iceland",       {1700, 11, 16}},
959d969a8bSAndrey A. Chernov 	{"IT", "Italy",         {1582, 10,  4}},
969d969a8bSAndrey A. Chernov 	{"JP", "Japan",         {1918, 12, 18}},
976feb6b4fSWarner Losh 	{"LT", "Lithuania",     {1918,  2,  1}},
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}},
107fb53214dSMaxim Konovalov 	{"SE", "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 
113bf70beceSEd Schouten static 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 */
118bf70beceSEd Schouten static 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 */
124bf70beceSEd Schouten static 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 
162bf70beceSEd Schouten static int flag_nohighlight;	/* user doesn't want a highlighted today */
163bf70beceSEd Schouten static int flag_weeks;		/* user wants number of week */
164bf70beceSEd Schouten static int nswitch;		/* user defined switch date */
165bf70beceSEd Schouten static int nswitchb;		/* switch date for backward compatibility */
166bf70beceSEd Schouten static int highlightdate;
1670cb2e609SWolfgang Helbig 
168bf70beceSEd Schouten static char	*center(char *s, char *t, int w);
169bf70beceSEd Schouten static wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170bf70beceSEd Schouten static int	firstday(int y, int m);
171bf70beceSEd Schouten static void	highlight(char *dst, char *src, int len, int *extraletters);
172bf70beceSEd Schouten static void	mkmonthr(int year, int month, int jd_flag,
173bf70beceSEd Schouten     struct monthlines * monthl);
174bf70beceSEd Schouten static void	mkmonthb(int year, int month, int jd_flag,
175bf70beceSEd Schouten     struct monthlines * monthl);
176bf70beceSEd Schouten static void	mkweekdays(struct weekdays * wds);
177bf70beceSEd Schouten static void	monthranger(int year, int m, int jd_flag,
178bf70beceSEd Schouten     int before, int after);
179bf70beceSEd Schouten static void	monthrangeb(int year, int m, int jd_flag,
180bf70beceSEd Schouten     int before, int after);
181bf70beceSEd Schouten static int	parsemonth(const char *s, int *m, int *y);
182bf70beceSEd Schouten static void	printcc(void);
183bf70beceSEd Schouten static void	printeaster(int year, int julian, int orthodox);
184bf70beceSEd Schouten static date	*sdater(int ndays, struct date * d);
185bf70beceSEd Schouten static date	*sdateb(int ndays, struct date * d);
186bf70beceSEd Schouten static int	sndaysr(struct date * d);
187bf70beceSEd Schouten static int	sndaysb(struct date * d);
188a99e4564SPhilippe Charnier static void	usage(void);
1890cb2e609SWolfgang Helbig 
1900cb2e609SWolfgang Helbig int
1910cb2e609SWolfgang Helbig main(int argc, char *argv[])
1920cb2e609SWolfgang Helbig {
1930cb2e609SWolfgang Helbig 	struct  djswitch *p, *q;	/* to search user defined switch date */
1940cb2e609SWolfgang Helbig 	date	never = {10000, 1, 1};	/* outside valid range of dates */
1950cb2e609SWolfgang Helbig 	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
1960851fbdfSEdwin Groothuis 	date	dt;
1970cb2e609SWolfgang Helbig 	int     ch;			/* holds the option character */
1980cb2e609SWolfgang Helbig 	int     m = 0;			/* month */
1990cb2e609SWolfgang Helbig 	int	y = 0;			/* year */
2000cb2e609SWolfgang Helbig 	int     flag_backward = 0;	/* user called cal--backward compat. */
2014af997a8SEdwin Groothuis 	int     flag_wholeyear = 0;	/* user wants the whole year */
2020cb2e609SWolfgang Helbig 	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
2034af997a8SEdwin Groothuis 	int     flag_julian_day = 0;	/* user wants the Julian day numbers */
2044af997a8SEdwin Groothuis 	int	flag_orthodox = 0;	/* user wants Orthodox easter */
2054af997a8SEdwin Groothuis 	int	flag_easter = 0;	/* user wants easter date */
2064af997a8SEdwin Groothuis 	int	flag_3months = 0;	/* user wants 3 month display (-3) */
2074af997a8SEdwin Groothuis 	int	flag_after = 0;		/* user wants to see months after */
2084af997a8SEdwin Groothuis 	int	flag_before = 0;	/* user wants to see months before */
2094af997a8SEdwin Groothuis 	int	flag_specifiedmonth = 0;/* user wants to see this month (-m) */
2104af997a8SEdwin Groothuis 	int	flag_givenmonth = 0;	/* user has specified month [n] */
2114af997a8SEdwin Groothuis 	int	flag_givenyear = 0;	/* user has specified year [n] */
2120cb2e609SWolfgang Helbig 	char	*cp;			/* character pointer */
2134af997a8SEdwin Groothuis 	char	*flag_today = NULL;	/* debug: use date as being today */
2140c4cafeaSGarrett Wollman 	char	*flag_month = NULL;	/* requested month as string */
2154af997a8SEdwin Groothuis 	char	*flag_highlightdate = NULL; /* debug: date to highlight */
2160851fbdfSEdwin Groothuis 	int	before, after;
217f372d010SMark Murray 	const char    *locale;		/* locale to get country code */
218e454a171SRoman Divacky 
219cae11c25SEdwin Groothuis 	flag_nohighlight = 0;
220cae11c25SEdwin Groothuis 	flag_weeks = 0;
2210cb2e609SWolfgang Helbig 
222cde26ed2SWolfgang Helbig 	/*
223cde26ed2SWolfgang Helbig 	 * Use locale to determine the country code,
224cde26ed2SWolfgang Helbig 	 * and use the country code to determine the default
225cde26ed2SWolfgang Helbig 	 * switchdate and date format from the switches table.
226cde26ed2SWolfgang Helbig 	 */
227e036a70eSAndrey A. Chernov 	if (setlocale(LC_ALL, "") == NULL)
228cde26ed2SWolfgang Helbig 		warn("setlocale");
229e036a70eSAndrey A. Chernov 	locale = setlocale(LC_TIME, NULL);
230c052429fSAndrey A. Chernov 	if (locale == NULL ||
231c052429fSAndrey A. Chernov 	    strcmp(locale, "C") == 0 ||
2321c593a0dSAndrey A. Chernov 	    strcmp(locale, "POSIX") == 0 ||
2331c593a0dSAndrey A. Chernov 	    strcmp(locale, "ASCII") == 0 ||
2341c593a0dSAndrey A. Chernov 	    strcmp(locale, "US-ASCII") == 0)
235cde26ed2SWolfgang Helbig 		locale = "_US";
236cde26ed2SWolfgang Helbig 	q = switches + sizeof(switches) / sizeof(struct djswitch);
237cde26ed2SWolfgang Helbig 	for (p = switches; p != q; p++)
238cde26ed2SWolfgang Helbig 		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
239cde26ed2SWolfgang Helbig 			break;
240cde26ed2SWolfgang Helbig 	if (p == q) {
241cde26ed2SWolfgang Helbig 		nswitch = ndaysj(&dftswitch->dt);
242cde26ed2SWolfgang Helbig 	} else {
243cde26ed2SWolfgang Helbig 		nswitch = ndaysj(&p->dt);
244cde26ed2SWolfgang Helbig 		dftswitch = p;
245cde26ed2SWolfgang Helbig 	}
246cde26ed2SWolfgang Helbig 
2470cb2e609SWolfgang Helbig 
2480cb2e609SWolfgang Helbig 	/*
2490cb2e609SWolfgang Helbig 	 * Get the filename portion of argv[0] and set flag_backward if
2500cb2e609SWolfgang Helbig 	 * this program is called "cal".
2510cb2e609SWolfgang Helbig 	 */
252746b67cdSEdwin Groothuis 	if (strncmp(basename(argv[0]), "cal", strlen("cal")) == 0)
2530cb2e609SWolfgang Helbig 		flag_backward = 1;
2540cb2e609SWolfgang Helbig 
2550cb2e609SWolfgang Helbig 	/* Set the switch date to United Kingdom if backwards compatible */
2560cb2e609SWolfgang Helbig 	if (flag_backward)
2570cb2e609SWolfgang Helbig 		nswitchb = ndaysj(&ukswitch);
2580cb2e609SWolfgang Helbig 
2590851fbdfSEdwin Groothuis 	before = after = -1;
2600851fbdfSEdwin Groothuis 
261531a1a4bSEdwin Groothuis 	while ((ch = getopt(argc, argv, "3A:B:Cd:eH:hjJm:Nops:wy")) != -1)
2620cb2e609SWolfgang Helbig 		switch (ch) {
2630851fbdfSEdwin Groothuis 		case '3':
2644af997a8SEdwin Groothuis 			flag_3months = 1;
2650851fbdfSEdwin Groothuis 			break;
2660851fbdfSEdwin Groothuis 		case 'A':
2674af997a8SEdwin Groothuis 			if (flag_after > 0)
2684af997a8SEdwin Groothuis 				errx(EX_USAGE, "Double -A specified");
2694af997a8SEdwin Groothuis 			flag_after = strtol(optarg, NULL, 10);
2704af997a8SEdwin Groothuis 			if (flag_after <= 0)
2714af997a8SEdwin Groothuis 				errx(EX_USAGE,
2724af997a8SEdwin Groothuis 				    "Argument to -A must be positive");
2730851fbdfSEdwin Groothuis 			break;
2740851fbdfSEdwin Groothuis 		case 'B':
2754af997a8SEdwin Groothuis 			if (flag_before > 0)
2764af997a8SEdwin Groothuis 				errx(EX_USAGE, "Double -A specified");
2774af997a8SEdwin Groothuis 			flag_before = strtol(optarg, NULL, 10);
2784af997a8SEdwin Groothuis 			if (flag_before <= 0)
2794af997a8SEdwin Groothuis 				errx(EX_USAGE,
2804af997a8SEdwin Groothuis 				    "Argument to -B must be positive");
2810851fbdfSEdwin Groothuis 			break;
2820cb2e609SWolfgang Helbig 		case 'J':
2830cb2e609SWolfgang Helbig 			if (flag_backward)
2840cb2e609SWolfgang Helbig 				usage();
2850cb2e609SWolfgang Helbig 			nswitch = ndaysj(&never);
2860cb2e609SWolfgang Helbig 			flag_julian_cal = 1;
2870cb2e609SWolfgang Helbig 			break;
288531a1a4bSEdwin Groothuis 		case 'C':
2890851fbdfSEdwin Groothuis 			flag_backward = 1;
2900851fbdfSEdwin Groothuis 			break;
291531a1a4bSEdwin Groothuis 		case 'N':
292531a1a4bSEdwin Groothuis 			flag_backward = 0;
293531a1a4bSEdwin Groothuis 			break;
2940851fbdfSEdwin Groothuis 		case 'd':
2954af997a8SEdwin Groothuis 			flag_today = optarg;
2964af997a8SEdwin Groothuis 			break;
2974af997a8SEdwin Groothuis 		case 'H':
2980851fbdfSEdwin Groothuis 			flag_highlightdate = optarg;
2990851fbdfSEdwin Groothuis 			break;
3000cd51de1SRoman Divacky 		case 'h':
301cae11c25SEdwin Groothuis 			flag_nohighlight = 1;
3020cd51de1SRoman Divacky 			break;
3030cb2e609SWolfgang Helbig 		case 'e':
3040cb2e609SWolfgang Helbig 			if (flag_backward)
3050cb2e609SWolfgang Helbig 				usage();
3060cb2e609SWolfgang Helbig 			flag_easter = 1;
3070cb2e609SWolfgang Helbig 			break;
3080cb2e609SWolfgang Helbig 		case 'j':
3090cb2e609SWolfgang Helbig 			flag_julian_day = 1;
3100cb2e609SWolfgang Helbig 			break;
3110c4cafeaSGarrett Wollman 		case 'm':
3124af997a8SEdwin Groothuis 			if (flag_specifiedmonth)
3134af997a8SEdwin Groothuis 				errx(EX_USAGE, "Double -m specified");
3140c4cafeaSGarrett Wollman 			flag_month = optarg;
3154af997a8SEdwin Groothuis 			flag_specifiedmonth = 1;
3160c4cafeaSGarrett Wollman 			break;
3170cb2e609SWolfgang Helbig 		case 'o':
3180cb2e609SWolfgang Helbig 			if (flag_backward)
3190cb2e609SWolfgang Helbig 				usage();
3200cb2e609SWolfgang Helbig 			flag_orthodox = 1;
3210cb2e609SWolfgang Helbig 			flag_easter = 1;
3220cb2e609SWolfgang Helbig 			break;
3230cb2e609SWolfgang Helbig 		case 'p':
3240cb2e609SWolfgang Helbig 			if (flag_backward)
3250cb2e609SWolfgang Helbig 				usage();
3260cb2e609SWolfgang Helbig 			printcc();
3270cb2e609SWolfgang Helbig 			return (0);
3280cb2e609SWolfgang Helbig 			break;
3290cb2e609SWolfgang Helbig 		case 's':
3300cb2e609SWolfgang Helbig 			if (flag_backward)
3310cb2e609SWolfgang Helbig 				usage();
3320cb2e609SWolfgang Helbig 			q = switches +
3330cb2e609SWolfgang Helbig 			    sizeof(switches) / sizeof(struct djswitch);
3340cb2e609SWolfgang Helbig 			for (p = switches;
3350cb2e609SWolfgang Helbig 			     p != q && strcmp(p->cc, optarg) != 0; p++)
3360cb2e609SWolfgang Helbig 				;
3370cb2e609SWolfgang Helbig 			if (p == q)
3380cb2e609SWolfgang Helbig 				errx(EX_USAGE,
3390cb2e609SWolfgang Helbig 				    "%s: invalid country code", optarg);
3400cb2e609SWolfgang Helbig 			nswitch = ndaysj(&(p->dt));
3410cb2e609SWolfgang Helbig 			break;
3420cb2e609SWolfgang Helbig 		case 'w':
3430cb2e609SWolfgang Helbig 			if (flag_backward)
3440cb2e609SWolfgang Helbig 				usage();
3450cb2e609SWolfgang Helbig 			flag_weeks = 1;
3460cb2e609SWolfgang Helbig 			break;
3470cb2e609SWolfgang Helbig 		case 'y':
3484af997a8SEdwin Groothuis 			flag_wholeyear = 1;
3490cb2e609SWolfgang Helbig 			break;
3500cb2e609SWolfgang Helbig 		default:
3510cb2e609SWolfgang Helbig 			usage();
3520cb2e609SWolfgang Helbig 		}
3530cb2e609SWolfgang Helbig 
3540cb2e609SWolfgang Helbig 	argc -= optind;
3550cb2e609SWolfgang Helbig 	argv += optind;
3560cb2e609SWolfgang Helbig 
3570c4cafeaSGarrett Wollman 	switch (argc) {
3580c4cafeaSGarrett Wollman 	case 2:
3590c4cafeaSGarrett Wollman 		if (flag_easter)
3600c4cafeaSGarrett Wollman 			usage();
3610c4cafeaSGarrett Wollman 		flag_month = *argv++;
3624af997a8SEdwin Groothuis 		flag_givenmonth = 1;
3631d0e1dacSEdwin Groothuis 		m = strtol(flag_month, NULL, 10);
3640c4cafeaSGarrett Wollman 		/* FALLTHROUGH */
3650c4cafeaSGarrett Wollman 	case 1:
3664af997a8SEdwin Groothuis 		y = atoi(*argv);
3670c4cafeaSGarrett Wollman 		if (y < 1 || y > 9999)
3684af997a8SEdwin Groothuis 			errx(EX_USAGE, "year `%s' not in range 1..9999", *argv);
3694af997a8SEdwin Groothuis 		argv++;
3704af997a8SEdwin Groothuis 		flag_givenyear = 1;
3710c4cafeaSGarrett Wollman 		break;
3720c4cafeaSGarrett Wollman 	case 0:
3734af997a8SEdwin Groothuis 		if (flag_today != NULL) {
3744af997a8SEdwin Groothuis 			y = strtol(flag_today, NULL, 10);
3754af997a8SEdwin Groothuis 			m = strtol(flag_today + 5, NULL, 10);
3764af997a8SEdwin Groothuis 		} else {
3770cb2e609SWolfgang Helbig 			time_t t;
3780cb2e609SWolfgang Helbig 			struct tm *tm;
3790cb2e609SWolfgang Helbig 
3800cb2e609SWolfgang Helbig 			t = time(NULL);
3810cb2e609SWolfgang Helbig 			tm = localtime(&t);
3820cb2e609SWolfgang Helbig 			y = tm->tm_year + 1900;
3830cb2e609SWolfgang Helbig 			m = tm->tm_mon + 1;
3840cb2e609SWolfgang Helbig 		}
3850cb2e609SWolfgang Helbig 		break;
3860cb2e609SWolfgang Helbig 	default:
3870cb2e609SWolfgang Helbig 		usage();
3880cb2e609SWolfgang Helbig 	}
3890cb2e609SWolfgang Helbig 
3900c4cafeaSGarrett Wollman 	if (flag_month != NULL) {
391ba29aec0SGarrett Wollman 		if (parsemonth(flag_month, &m, &y)) {
3920c4cafeaSGarrett Wollman 			errx(EX_USAGE,
3930c4cafeaSGarrett Wollman 			    "%s is neither a month number (1..12) nor a name",
3940c4cafeaSGarrett Wollman 			    flag_month);
3950c4cafeaSGarrett Wollman 		}
396ba29aec0SGarrett Wollman 	}
3970c4cafeaSGarrett Wollman 
3984af997a8SEdwin Groothuis 	/*
3994af997a8SEdwin Groothuis 	 * What is not supported:
4004af997a8SEdwin Groothuis 	 * -3 with -A or -B
4014af997a8SEdwin Groothuis 	 *	-3 displays 3 months, -A and -B change that behaviour.
4024af997a8SEdwin Groothuis 	 * -3 with -y
4034af997a8SEdwin Groothuis 	 *	-3 displays 3 months, -y says display a whole year.
4044af997a8SEdwin Groothuis 	 * -3 with a given year but no given month or without -m
4054af997a8SEdwin Groothuis 	 *	-3 displays 3 months, no month specified doesn't make clear
4064af997a8SEdwin Groothuis 	 *      which three months.
4074af997a8SEdwin Groothuis 	 * -m with a given month
4084af997a8SEdwin Groothuis 	 *	conflicting arguments, both specify the same field.
4094af997a8SEdwin Groothuis 	 * -y with -m
4104af997a8SEdwin Groothuis 	 *	-y displays the whole year, -m displays a single month.
4114af997a8SEdwin Groothuis 	 * -y with a given month
4124af997a8SEdwin Groothuis 	 *	-y displays the whole year, the given month displays a single
4134af997a8SEdwin Groothuis 	 *	month.
4144af997a8SEdwin Groothuis 	 * -y with -A or -B
4154af997a8SEdwin Groothuis 	 *	-y displays the whole year, -A and -B display extra months.
4164af997a8SEdwin Groothuis 	 */
4174af997a8SEdwin Groothuis 
4184af997a8SEdwin Groothuis 	/* -3 together with -A or -B. */
4194af997a8SEdwin Groothuis 	if (flag_3months && (flag_after || flag_before))
4204af997a8SEdwin Groothuis 		errx(EX_USAGE, "-3 together with -A and -B is not supported.");
4214af997a8SEdwin Groothuis 	/* -3 together with -y. */
4224af997a8SEdwin Groothuis 	if (flag_3months && flag_wholeyear)
4234af997a8SEdwin Groothuis 		errx(EX_USAGE, "-3 together with -y is not supported.");
4244af997a8SEdwin Groothuis 	/* -3 together with givenyear but no givenmonth. */
4254af997a8SEdwin Groothuis 	if (flag_3months && flag_givenyear &&
4264af997a8SEdwin Groothuis 	    !(flag_givenmonth || flag_specifiedmonth))
4274af997a8SEdwin Groothuis 		errx(EX_USAGE,
4284af997a8SEdwin Groothuis 		    "-3 together with a given year but no given month is "
4294af997a8SEdwin Groothuis 		    "not supported.");
4304af997a8SEdwin Groothuis 	/* -m together with xx xxxx. */
4314af997a8SEdwin Groothuis 	if (flag_specifiedmonth && flag_givenmonth)
4324af997a8SEdwin Groothuis 		errx(EX_USAGE,
4334af997a8SEdwin Groothuis 		    "-m together with a given month is not supported.");
4344af997a8SEdwin Groothuis 	/* -y together with -m. */
4354af997a8SEdwin Groothuis 	if (flag_wholeyear && flag_specifiedmonth)
4364af997a8SEdwin Groothuis 		errx(EX_USAGE, "-y together with -m is not supported.");
4374af997a8SEdwin Groothuis 	/* -y together with xx xxxx. */
4384af997a8SEdwin Groothuis 	if (flag_wholeyear && flag_givenmonth)
4394af997a8SEdwin Groothuis 		errx(EX_USAGE, "-y together a given month is not supported.");
4404af997a8SEdwin Groothuis 	/* -y together with -A or -B. */
4414af997a8SEdwin Groothuis 	if (flag_wholeyear && (flag_before > 0 || flag_after > 0))
4424af997a8SEdwin Groothuis 		errx(EX_USAGE, "-y together a -A or -B is not supported.");
4434af997a8SEdwin Groothuis 	/* The rest should be fine. */
4444af997a8SEdwin Groothuis 
4454af997a8SEdwin Groothuis 	/* Select the period to display, in order of increasing priority .*/
4464af997a8SEdwin Groothuis 	if (flag_wholeyear ||
4474af997a8SEdwin Groothuis 	    (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) {
4484af997a8SEdwin Groothuis 		m = 1;
4494af997a8SEdwin Groothuis 		before = 0;
4504af997a8SEdwin Groothuis 		after = 11;
4514af997a8SEdwin Groothuis 	}
4524af997a8SEdwin Groothuis 	if (flag_givenyear && flag_givenmonth) {
4534af997a8SEdwin Groothuis 		before = 0;
4544af997a8SEdwin Groothuis 		after = 0;
4554af997a8SEdwin Groothuis 	}
4564af997a8SEdwin Groothuis 	if (flag_specifiedmonth) {
4574af997a8SEdwin Groothuis 		before = 0;
4584af997a8SEdwin Groothuis 		after = 0;
4594af997a8SEdwin Groothuis 	}
4604af997a8SEdwin Groothuis 	if (flag_before) {
4614af997a8SEdwin Groothuis 		before = flag_before;
4624af997a8SEdwin Groothuis 	}
4634af997a8SEdwin Groothuis 	if (flag_after) {
4644af997a8SEdwin Groothuis 		after = flag_after;
4654af997a8SEdwin Groothuis 	}
4664af997a8SEdwin Groothuis 	if (flag_3months) {
4674af997a8SEdwin Groothuis 		before = 1;
4684af997a8SEdwin Groothuis 		after = 1;
4694af997a8SEdwin Groothuis 	}
4704af997a8SEdwin Groothuis 	if (after == -1)
4714af997a8SEdwin Groothuis 		after = 0;
4724af997a8SEdwin Groothuis 	if (before == -1)
4734af997a8SEdwin Groothuis 		before = 0;
4744af997a8SEdwin Groothuis 
4754af997a8SEdwin Groothuis 	/* Highlight a specified day or today .*/
4760851fbdfSEdwin Groothuis 	if (flag_highlightdate != NULL) {
4770851fbdfSEdwin Groothuis 		dt.y = strtol(flag_highlightdate, NULL, 10);
4780851fbdfSEdwin Groothuis 		dt.m = strtol(flag_highlightdate + 5, NULL, 10);
4790851fbdfSEdwin Groothuis 		dt.d = strtol(flag_highlightdate + 8, NULL, 10);
4800851fbdfSEdwin Groothuis 	} else {
4810851fbdfSEdwin Groothuis 		time_t t;
4820851fbdfSEdwin Groothuis 		struct tm *tm1;
4830851fbdfSEdwin Groothuis 
4840851fbdfSEdwin Groothuis 		t = time(NULL);
4850851fbdfSEdwin Groothuis 		tm1 = localtime(&t);
4860851fbdfSEdwin Groothuis 		dt.y = tm1->tm_year + 1900;
4870851fbdfSEdwin Groothuis 		dt.m = tm1->tm_mon + 1;
4880851fbdfSEdwin Groothuis 		dt.d = tm1->tm_mday;
4890851fbdfSEdwin Groothuis 	}
4904af997a8SEdwin Groothuis 	highlightdate = sndaysb(&dt);
4910851fbdfSEdwin Groothuis 
4924af997a8SEdwin Groothuis 	/* And now we finally start to calculate and output calendars. */
4930cb2e609SWolfgang Helbig 	if (flag_easter)
4940cb2e609SWolfgang Helbig 		printeaster(y, flag_julian_cal, flag_orthodox);
4950cb2e609SWolfgang Helbig 	else
4960cb2e609SWolfgang Helbig 		if (flag_backward)
4974af997a8SEdwin Groothuis 			monthrangeb(y, m, flag_julian_day, before, after);
4980cb2e609SWolfgang Helbig 		else
4994af997a8SEdwin Groothuis 			monthranger(y, m, flag_julian_day, before, after);
500*5903d9c0SDag-Erling Smørgrav 	if (ferror(stdout) != 0 || fflush(stdout) != 0)
501*5903d9c0SDag-Erling Smørgrav 		err(1, "stdout");
5020cb2e609SWolfgang Helbig 	return (0);
5030cb2e609SWolfgang Helbig }
5040cb2e609SWolfgang Helbig 
505a99e4564SPhilippe Charnier static void
5060cb2e609SWolfgang Helbig usage(void)
5070cb2e609SWolfgang Helbig {
5080cb2e609SWolfgang Helbig 
5090c4cafeaSGarrett Wollman 	fputs(
510531a1a4bSEdwin Groothuis "Usage: cal [general options] [-hjy] [[month] year]\n"
511531a1a4bSEdwin Groothuis "       cal [general options] [-hj] [-m month] [year]\n"
512531a1a4bSEdwin Groothuis "       ncal [general options] [-hJjpwy] [-s country_code] [[month] year]\n"
513531a1a4bSEdwin Groothuis "       ncal [general options] [-hJeo] [year]\n"
514531a1a4bSEdwin Groothuis "General options: [-NC3] [-A months] [-B months]\n"
515531a1a4bSEdwin Groothuis "For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]\n",
5160851fbdfSEdwin Groothuis 	    stderr);
5170cb2e609SWolfgang Helbig 	exit(EX_USAGE);
5180cb2e609SWolfgang Helbig }
5190cb2e609SWolfgang Helbig 
5204af997a8SEdwin Groothuis /* Print the assumed switches for all countries. */
521bf70beceSEd Schouten static void
5220cb2e609SWolfgang Helbig printcc(void)
5230cb2e609SWolfgang Helbig {
5240cb2e609SWolfgang Helbig 	struct djswitch *p;
5250cb2e609SWolfgang Helbig 	int n;	/* number of lines to print */
5260cb2e609SWolfgang Helbig 	int m;	/* offset from left to right table entry on the same line */
5270cb2e609SWolfgang Helbig 
528cde26ed2SWolfgang Helbig #define FSTR "%c%s %-15s%4d-%02d-%02d"
529cde26ed2SWolfgang Helbig #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
530cde26ed2SWolfgang Helbig #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
5310cb2e609SWolfgang Helbig 
5320cb2e609SWolfgang Helbig 	n = sizeof(switches) / sizeof(struct djswitch);
5330cb2e609SWolfgang Helbig 	m = (n + 1) / 2;
5340cb2e609SWolfgang Helbig 	n /= 2;
535cde26ed2SWolfgang Helbig 	for (p = switches; p != switches + n; p++)
536cde26ed2SWolfgang Helbig 		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
5370cb2e609SWolfgang Helbig 	if (m != n)
538cde26ed2SWolfgang Helbig 		printf(FSTR"\n", FSTRARG(p));
5390cb2e609SWolfgang Helbig }
5400cb2e609SWolfgang Helbig 
5414af997a8SEdwin Groothuis /* Print the date of easter sunday. */
542bf70beceSEd Schouten static void
5430cb2e609SWolfgang Helbig printeaster(int y, int julian, int orthodox)
5440cb2e609SWolfgang Helbig {
5450cb2e609SWolfgang Helbig 	date    dt;
546cde26ed2SWolfgang Helbig 	struct tm tm;
5470851fbdfSEdwin Groothuis 	char    buf[MAX_WIDTH];
548f5e40af2SAndrey A. Chernov 	static int d_first = -1;
5490cb2e609SWolfgang Helbig 
550f5e40af2SAndrey A. Chernov 	if (d_first < 0)
551f5e40af2SAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
5520cb2e609SWolfgang Helbig 	/* force orthodox easter for years before 1583 */
5530cb2e609SWolfgang Helbig 	if (y < 1583)
5540cb2e609SWolfgang Helbig 		orthodox = 1;
5550cb2e609SWolfgang Helbig 
5560cb2e609SWolfgang Helbig 	if (orthodox)
5570cb2e609SWolfgang Helbig 		if (julian)
5580cb2e609SWolfgang Helbig 			easteroj(y, &dt);
5590cb2e609SWolfgang Helbig 		else
5600cb2e609SWolfgang Helbig 			easterog(y, &dt);
5610cb2e609SWolfgang Helbig 	else
5620cb2e609SWolfgang Helbig 		easterg(y, &dt);
563cde26ed2SWolfgang Helbig 
564cde26ed2SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
565cde26ed2SWolfgang Helbig 	tm.tm_year = dt.y - 1900;
566cde26ed2SWolfgang Helbig 	tm.tm_mon  = dt.m - 1;
567cde26ed2SWolfgang Helbig 	tm.tm_mday = dt.d;
568f5e40af2SAndrey A. Chernov 	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
569cde26ed2SWolfgang Helbig 	printf("%s\n", buf);
5700cb2e609SWolfgang Helbig }
5710cb2e609SWolfgang Helbig 
5721d0e1dacSEdwin Groothuis #define MW(mw, me)		((mw) + me)
5730851fbdfSEdwin Groothuis #define	DECREASEMONTH(m, y) 		\
5740851fbdfSEdwin Groothuis 		if (--m == 0) {		\
5750851fbdfSEdwin Groothuis 			m = 12;		\
5760851fbdfSEdwin Groothuis 			y--;		\
577bd97a998SHajimu UMEMOTO 		}
5780851fbdfSEdwin Groothuis #define	INCREASEMONTH(m, y)		\
5790851fbdfSEdwin Groothuis 		if (++(m) == 13) {	\
5800851fbdfSEdwin Groothuis 			(m) = 1;	\
5810851fbdfSEdwin Groothuis 			(y)++;		\
5820cb2e609SWolfgang Helbig 		}
5830851fbdfSEdwin Groothuis #define	M2Y(m)	((m) / 12)
5840851fbdfSEdwin Groothuis #define	M2M(m)	(1 + (m) % 12)
5850cb2e609SWolfgang Helbig 
5864af997a8SEdwin Groothuis /* Print all months for the period in the range [ before .. y-m .. after ]. */
587bf70beceSEd Schouten static void
5884af997a8SEdwin Groothuis monthrangeb(int y, int m, int jd_flag, int before, int after)
5890cb2e609SWolfgang Helbig {
5900cb2e609SWolfgang Helbig 	struct monthlines year[12];
5910cb2e609SWolfgang Helbig 	struct weekdays wds;
5920851fbdfSEdwin Groothuis 	char	s[MAX_WIDTH], t[MAX_WIDTH];
5930851fbdfSEdwin Groothuis 	wchar_t	ws[MAX_WIDTH], ws1[MAX_WIDTH];
5940851fbdfSEdwin Groothuis 	const char	*wdss;
5950cb2e609SWolfgang Helbig 	int     i, j;
5960cb2e609SWolfgang Helbig 	int     mpl;
5970cb2e609SWolfgang Helbig 	int     mw;
5980851fbdfSEdwin Groothuis 	int	m1, m2;
5990851fbdfSEdwin Groothuis 	int	printyearheader;
6000851fbdfSEdwin Groothuis 	int	prevyear = -1;
6010cb2e609SWolfgang Helbig 
6020cb2e609SWolfgang Helbig 	mpl = jd_flag ? 2 : 3;
6030cb2e609SWolfgang Helbig 	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
6040851fbdfSEdwin Groothuis 	wdss = (mpl == 2) ? " " : "";
6050cb2e609SWolfgang Helbig 
6060851fbdfSEdwin Groothuis 	while (before != 0) {
6070851fbdfSEdwin Groothuis 		DECREASEMONTH(m, y);
6080851fbdfSEdwin Groothuis 		before--;
6090851fbdfSEdwin Groothuis 		after++;
6100cb2e609SWolfgang Helbig 	}
6110851fbdfSEdwin Groothuis 	m1 = y * 12 + m - 1;
6120851fbdfSEdwin Groothuis 	m2 = m1 + after;
6130851fbdfSEdwin Groothuis 
6140851fbdfSEdwin Groothuis 	mkweekdays(&wds);
6150851fbdfSEdwin Groothuis 
6160851fbdfSEdwin Groothuis 	/*
6170851fbdfSEdwin Groothuis 	 * The year header is printed when there are more than 'mpl' months
6180851fbdfSEdwin Groothuis 	 * and if the first month is a multitude of 'mpl'.
6190851fbdfSEdwin Groothuis 	 * If not, it will print the year behind every month.
6200851fbdfSEdwin Groothuis 	 */
6210851fbdfSEdwin Groothuis 	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
6220851fbdfSEdwin Groothuis 
6230851fbdfSEdwin Groothuis 	m = m1;
6240851fbdfSEdwin Groothuis 	while (m <= m2) {
6250851fbdfSEdwin Groothuis 		int count = 0;
6260851fbdfSEdwin Groothuis 		for (i = 0; i != mpl && m + i <= m2; i++) {
6270851fbdfSEdwin Groothuis 			mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
6280851fbdfSEdwin Groothuis 			count++;
6290851fbdfSEdwin Groothuis 		}
6300851fbdfSEdwin Groothuis 
6310851fbdfSEdwin Groothuis 		/* Empty line between two rows of months */
6320851fbdfSEdwin Groothuis 		if (m != m1)
6330851fbdfSEdwin Groothuis 			printf("\n");
6340851fbdfSEdwin Groothuis 
6354af997a8SEdwin Groothuis 		/* Year at the top. */
6360851fbdfSEdwin Groothuis 		if (printyearheader && M2Y(m) != prevyear) {
6370851fbdfSEdwin Groothuis 			sprintf(s, "%d", M2Y(m));
6380851fbdfSEdwin Groothuis 			printf("%s\n", center(t, s, mpl * mw));
6390851fbdfSEdwin Groothuis 			prevyear = M2Y(m);
6400851fbdfSEdwin Groothuis 		}
6410851fbdfSEdwin Groothuis 
6424af997a8SEdwin Groothuis 		/* Month names. */
6430851fbdfSEdwin Groothuis 		for (i = 0; i < count; i++)
6440851fbdfSEdwin Groothuis 			if (printyearheader)
6450851fbdfSEdwin Groothuis 				wprintf(L"%-*ls  ",
6460851fbdfSEdwin Groothuis 				    mw, wcenter(ws, year[i].name, mw));
6470851fbdfSEdwin Groothuis 			else {
648b74e3739SDon Lewis 				swprintf(ws, sizeof(ws)/sizeof(ws[0]),
649b74e3739SDon Lewis 				    L"%-ls %d", year[i].name, M2Y(m + i));
6500851fbdfSEdwin Groothuis 				wprintf(L"%-*ls  ", mw, wcenter(ws1, ws, mw));
6510851fbdfSEdwin Groothuis 			}
6520851fbdfSEdwin Groothuis 		printf("\n");
6530851fbdfSEdwin Groothuis 
6544af997a8SEdwin Groothuis 		/* Day of the week names. */
6550851fbdfSEdwin Groothuis 		for (i = 0; i < count; i++) {
6560851fbdfSEdwin Groothuis 			wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
6570851fbdfSEdwin Groothuis 				wdss, wds.names[6], wdss, wds.names[0],
6580851fbdfSEdwin Groothuis 				wdss, wds.names[1], wdss, wds.names[2],
6590851fbdfSEdwin Groothuis 				wdss, wds.names[3], wdss, wds.names[4],
6600851fbdfSEdwin Groothuis 				wdss, wds.names[5]);
6610851fbdfSEdwin Groothuis 		}
6620851fbdfSEdwin Groothuis 		printf("\n");
6630851fbdfSEdwin Groothuis 
6644af997a8SEdwin Groothuis 		/* And the days of the month. */
6650851fbdfSEdwin Groothuis 		for (i = 0; i != 6; i++) {
6660851fbdfSEdwin Groothuis 			for (j = 0; j < count; j++)
6671d0e1dacSEdwin Groothuis 				printf("%-*s  ",
6681d0e1dacSEdwin Groothuis 				    MW(mw, year[j].extralen[i]),
6691d0e1dacSEdwin Groothuis 					year[j].lines[i]+1);
6700851fbdfSEdwin Groothuis 			printf("\n");
6710851fbdfSEdwin Groothuis 		}
6720851fbdfSEdwin Groothuis 
6730851fbdfSEdwin Groothuis 		m += mpl;
6740cb2e609SWolfgang Helbig 	}
6750cb2e609SWolfgang Helbig }
6760cb2e609SWolfgang Helbig 
677bf70beceSEd Schouten static void
6784af997a8SEdwin Groothuis monthranger(int y, int m, int jd_flag, int before, int after)
6790851fbdfSEdwin Groothuis {
6800851fbdfSEdwin Groothuis 	struct monthlines year[12];
6810851fbdfSEdwin Groothuis 	struct weekdays wds;
6820851fbdfSEdwin Groothuis 	char    s[MAX_WIDTH], t[MAX_WIDTH];
6830851fbdfSEdwin Groothuis 	int     i, j;
6840851fbdfSEdwin Groothuis 	int     mpl;
6850851fbdfSEdwin Groothuis 	int     mw;
6860851fbdfSEdwin Groothuis 	int	m1, m2;
6870851fbdfSEdwin Groothuis 	int	prevyear = -1;
6880851fbdfSEdwin Groothuis 	int	printyearheader;
6890851fbdfSEdwin Groothuis 
6900851fbdfSEdwin Groothuis 	mpl = jd_flag ? 3 : 4;
6910851fbdfSEdwin Groothuis 	mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R;
6920851fbdfSEdwin Groothuis 
6930851fbdfSEdwin Groothuis 	while (before != 0) {
6940851fbdfSEdwin Groothuis 		DECREASEMONTH(m, y);
6950851fbdfSEdwin Groothuis 		before--;
6960851fbdfSEdwin Groothuis 		after++;
6970851fbdfSEdwin Groothuis 	}
6980851fbdfSEdwin Groothuis 	m1 = y * 12 + m - 1;
6990851fbdfSEdwin Groothuis 	m2 = m1 + after;
7000851fbdfSEdwin Groothuis 
7010851fbdfSEdwin Groothuis 	mkweekdays(&wds);
7020851fbdfSEdwin Groothuis 
7030851fbdfSEdwin Groothuis 	/*
7040851fbdfSEdwin Groothuis 	 * The year header is printed when there are more than 'mpl' months
7050851fbdfSEdwin Groothuis 	 * and if the first month is a multitude of 'mpl'.
7060851fbdfSEdwin Groothuis 	 * If not, it will print the year behind every month.
7070851fbdfSEdwin Groothuis 	 */
7080851fbdfSEdwin Groothuis 	printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
7090851fbdfSEdwin Groothuis 
7100851fbdfSEdwin Groothuis 	m = m1;
7110851fbdfSEdwin Groothuis 	while (m <= m2) {
7120851fbdfSEdwin Groothuis 		int count = 0;
7130851fbdfSEdwin Groothuis 		for (i = 0; i != mpl && m + i <= m2; i++) {
7140851fbdfSEdwin Groothuis 			mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
7150851fbdfSEdwin Groothuis 			count++;
7160851fbdfSEdwin Groothuis 		}
7170851fbdfSEdwin Groothuis 
7184af997a8SEdwin Groothuis 		/* Empty line between two rows of months. */
7190851fbdfSEdwin Groothuis 		if (m != m1)
7200851fbdfSEdwin Groothuis 			printf("\n");
7210851fbdfSEdwin Groothuis 
7224af997a8SEdwin Groothuis 		/* Year at the top. */
7230851fbdfSEdwin Groothuis 		if (printyearheader && M2Y(m) != prevyear) {
7240851fbdfSEdwin Groothuis 			sprintf(s, "%d", M2Y(m));
7250851fbdfSEdwin Groothuis 			printf("%s\n", center(t, s, mpl * mw));
7260851fbdfSEdwin Groothuis 			prevyear = M2Y(m);
7270851fbdfSEdwin Groothuis 		}
7280851fbdfSEdwin Groothuis 
7294af997a8SEdwin Groothuis 		/* Month names. */
7300851fbdfSEdwin Groothuis 		wprintf(L"    ");
7310851fbdfSEdwin Groothuis 		for (i = 0; i < count; i++)
7320851fbdfSEdwin Groothuis 			if (printyearheader)
7330851fbdfSEdwin Groothuis 				wprintf(L"%-*ls", mw, year[i].name);
7340851fbdfSEdwin Groothuis 			else
7350851fbdfSEdwin Groothuis 				wprintf(L"%-ls %-*d", year[i].name,
7360851fbdfSEdwin Groothuis 				    mw - wcslen(year[i].name) - 1, M2Y(m + i));
7370851fbdfSEdwin Groothuis 		printf("\n");
7380851fbdfSEdwin Groothuis 
7394af997a8SEdwin Groothuis 		/* And the days of the month. */
7400851fbdfSEdwin Groothuis 		for (i = 0; i != 7; i++) {
7410851fbdfSEdwin Groothuis 			/* Week day */
7420851fbdfSEdwin Groothuis 			wprintf(L"%.2ls", wds.names[i]);
7430851fbdfSEdwin Groothuis 
7440851fbdfSEdwin Groothuis 			/* Full months */
7450851fbdfSEdwin Groothuis 			for (j = 0; j < count; j++)
7460851fbdfSEdwin Groothuis 				printf("%-*s",
7471d0e1dacSEdwin Groothuis 				    MW(mw, year[j].extralen[i]),
7481d0e1dacSEdwin Groothuis 					year[j].lines[i]);
7490851fbdfSEdwin Groothuis 			printf("\n");
7500851fbdfSEdwin Groothuis 		}
7510851fbdfSEdwin Groothuis 
7524af997a8SEdwin Groothuis 		/* Week numbers. */
7530851fbdfSEdwin Groothuis 		if (flag_weeks) {
7540851fbdfSEdwin Groothuis 			printf("  ");
7550851fbdfSEdwin Groothuis 			for (i = 0; i < count; i++)
7560851fbdfSEdwin Groothuis 				printf("%-*s", mw, year[i].weeks);
7570851fbdfSEdwin Groothuis 			printf("\n");
7580851fbdfSEdwin Groothuis 		}
7590851fbdfSEdwin Groothuis 
7600851fbdfSEdwin Groothuis 		m += mpl;
7610851fbdfSEdwin Groothuis 	}
7620851fbdfSEdwin Groothuis 	return;
7630851fbdfSEdwin Groothuis }
7640851fbdfSEdwin Groothuis 
765bf70beceSEd Schouten static void
7660851fbdfSEdwin Groothuis mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines)
7670cb2e609SWolfgang Helbig {
7680cb2e609SWolfgang Helbig 
7690cb2e609SWolfgang Helbig 	struct tm tm;		/* for strftime printing local names of
7700cb2e609SWolfgang Helbig 				 * months */
7710cb2e609SWolfgang Helbig 	date    dt;		/* handy date */
7720cb2e609SWolfgang Helbig 	int     dw;		/* width of numbers */
7730cb2e609SWolfgang Helbig 	int     first;		/* first day of month */
7740cb2e609SWolfgang Helbig 	int     firstm;		/* first day of first week of month */
775e454a171SRoman Divacky 	int     i, j, k, l;	/* just indices */
7760cb2e609SWolfgang Helbig 	int     last;		/* the first day of next month */
7770cb2e609SWolfgang Helbig 	int     jan1 = 0;	/* the first day of this year */
7780cb2e609SWolfgang Helbig 	char   *ds;		/* pointer to day strings (daystr or
7790cb2e609SWolfgang Helbig 				 * jdaystr) */
7800cb2e609SWolfgang Helbig 
7810cb2e609SWolfgang Helbig 	/* Set name of month. */
7820cb2e609SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
7830cb2e609SWolfgang Helbig 	tm.tm_mon = m;
7844646cea7SDavid Schultz 	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
7854646cea7SDavid Schultz 		 L"%OB", &tm);
7864646cea7SDavid Schultz 	mlines->name[0] = towupper(mlines->name[0]);
7870cb2e609SWolfgang Helbig 
7880cb2e609SWolfgang Helbig 	/*
7890cb2e609SWolfgang Helbig 	 * Set first and last to the day number of the first day of this
7900cb2e609SWolfgang Helbig 	 * month and the first day of next month respectively. Set jan1 to
791f8a0edbaSWolfgang Helbig 	 * the day number of the first day of this year.
7920cb2e609SWolfgang Helbig 	 */
793f8a0edbaSWolfgang Helbig 	first = firstday(y, m + 1);
794f8a0edbaSWolfgang Helbig 	if (m == 11)
795f8a0edbaSWolfgang Helbig 		last = firstday(y + 1, 1);
796f8a0edbaSWolfgang Helbig 	else
797f8a0edbaSWolfgang Helbig 		last = firstday(y, m + 2);
7980cb2e609SWolfgang Helbig 
799f8a0edbaSWolfgang Helbig 	if (jd_flag)
800630d15bbSWolfgang Helbig 		jan1 = firstday(y, 1);
8010cb2e609SWolfgang Helbig 
8020cb2e609SWolfgang Helbig 	/*
8030cb2e609SWolfgang Helbig 	 * Set firstm to the day number of monday of the first week of
8040cb2e609SWolfgang Helbig 	 * this month. (This might be in the last month)
8050cb2e609SWolfgang Helbig 	 */
8060cb2e609SWolfgang Helbig 	firstm = first - weekday(first);
8070cb2e609SWolfgang Helbig 
8084af997a8SEdwin Groothuis 	/* Set ds (daystring) and dw (daywidth) according to the jd_flag. */
8090cb2e609SWolfgang Helbig 	if (jd_flag) {
8100cb2e609SWolfgang Helbig 		ds = jdaystr;
8110cb2e609SWolfgang Helbig 		dw = 4;
8120cb2e609SWolfgang Helbig 	} else {
8130cb2e609SWolfgang Helbig 		ds = daystr;
8140cb2e609SWolfgang Helbig 		dw = 3;
8150cb2e609SWolfgang Helbig 	}
8160cb2e609SWolfgang Helbig 
8170cb2e609SWolfgang Helbig 	/*
8180cb2e609SWolfgang Helbig 	 * Fill the lines with day of month or day of year (julian day)
8190cb2e609SWolfgang Helbig 	 * line index: i, each line is one weekday. column index: j, each
8200cb2e609SWolfgang Helbig 	 * column is one day number. print column index: k.
8210cb2e609SWolfgang Helbig 	 */
8220cb2e609SWolfgang Helbig 	for (i = 0; i != 7; i++) {
823e454a171SRoman Divacky 		l = 0;
824e454a171SRoman Divacky 		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
8250cb2e609SWolfgang Helbig 			if (j >= first) {
8260cb2e609SWolfgang Helbig 				if (jd_flag)
8270cb2e609SWolfgang Helbig 					dt.d = j - jan1 + 1;
8280cb2e609SWolfgang Helbig 				else
8290851fbdfSEdwin Groothuis 					sdater(j, &dt);
830f379d691SEdwin Groothuis 				if (j == highlightdate && !flag_nohighlight
831f379d691SEdwin Groothuis 				 && isatty(STDOUT_FILENO))
832cae11c25SEdwin Groothuis 					highlight(mlines->lines[i] + k,
833cae11c25SEdwin Groothuis 					    ds + dt.d * dw, dw, &l);
834cae11c25SEdwin Groothuis 				else
835e454a171SRoman Divacky 					memcpy(mlines->lines[i] + k + l,
8360cb2e609SWolfgang Helbig 					       ds + dt.d * dw, dw);
8370cb2e609SWolfgang Helbig 			} else
838e454a171SRoman Divacky 				memcpy(mlines->lines[i] + k + l, "    ", dw);
839e454a171SRoman Divacky 		}
840e454a171SRoman Divacky 		mlines->lines[i][k + l] = '\0';
8411d0e1dacSEdwin Groothuis 		mlines->extralen[i] = l;
8420cb2e609SWolfgang Helbig 	}
8430cb2e609SWolfgang Helbig 
8444af997a8SEdwin Groothuis 	/* fill the weeknumbers. */
8450cb2e609SWolfgang Helbig 	if (flag_weeks) {
8460cb2e609SWolfgang Helbig 		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
8470cb2e609SWolfgang Helbig 			if (j <= nswitch)
8480cb2e609SWolfgang Helbig 				memset(mlines->weeks + k, ' ', dw);
8490cb2e609SWolfgang Helbig 			else
8500cb2e609SWolfgang Helbig 				memcpy(mlines->weeks + k,
8510cb2e609SWolfgang Helbig 				    ds + week(j, &i)*dw, dw);
8520cb2e609SWolfgang Helbig 		mlines->weeks[k] = '\0';
8530cb2e609SWolfgang Helbig 	}
8540cb2e609SWolfgang Helbig }
8550cb2e609SWolfgang Helbig 
856bf70beceSEd Schouten static void
8570cb2e609SWolfgang Helbig mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
8580cb2e609SWolfgang Helbig {
8590cb2e609SWolfgang Helbig 
8600cb2e609SWolfgang Helbig 	struct tm tm;		/* for strftime printing local names of
8610cb2e609SWolfgang Helbig 				 * months */
8620cb2e609SWolfgang Helbig 	date    dt;		/* handy date */
8630cb2e609SWolfgang Helbig 	int     dw;		/* width of numbers */
8640cb2e609SWolfgang Helbig 	int     first;		/* first day of month */
8650cb2e609SWolfgang Helbig 	int     firsts;		/* sunday of first week of month */
866e454a171SRoman Divacky 	int     i, j, k, l;	/* just indices */
8670cb2e609SWolfgang Helbig 	int     jan1 = 0;	/* the first day of this year */
8680cb2e609SWolfgang Helbig 	int     last;		/* the first day of next month */
8690cb2e609SWolfgang Helbig 	char   *ds;		/* pointer to day strings (daystr or
8700cb2e609SWolfgang Helbig 				 * jdaystr) */
8710cb2e609SWolfgang Helbig 
8720cb2e609SWolfgang Helbig 	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
8730cb2e609SWolfgang Helbig 	if (jd_flag) {
8740cb2e609SWolfgang Helbig 		ds = jdaystr;
8750cb2e609SWolfgang Helbig 		dw = 4;
8760cb2e609SWolfgang Helbig 	} else {
8770cb2e609SWolfgang Helbig 		ds = daystr;
8780cb2e609SWolfgang Helbig 		dw = 3;
8790cb2e609SWolfgang Helbig 	}
8800cb2e609SWolfgang Helbig 
8814af997a8SEdwin Groothuis 	/* Set name of month centered. */
8820cb2e609SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
8830cb2e609SWolfgang Helbig 	tm.tm_mon = m;
8844646cea7SDavid Schultz 	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
8854646cea7SDavid Schultz 		 L"%OB", &tm);
8864646cea7SDavid Schultz 	mlines->name[0] = towupper(mlines->name[0]);
8870cb2e609SWolfgang Helbig 
8880cb2e609SWolfgang Helbig 	/*
8890cb2e609SWolfgang Helbig 	 * Set first and last to the day number of the first day of this
8900cb2e609SWolfgang Helbig 	 * month and the first day of next month respectively. Set jan1 to
8910cb2e609SWolfgang Helbig 	 * the day number of Jan 1st of this year.
8920cb2e609SWolfgang Helbig 	 */
8930cb2e609SWolfgang Helbig 	dt.y = y;
8940cb2e609SWolfgang Helbig 	dt.m = m + 1;
8950cb2e609SWolfgang Helbig 	dt.d = 1;
8960cb2e609SWolfgang Helbig 	first = sndaysb(&dt);
8970cb2e609SWolfgang Helbig 	if (m == 11) {
8980cb2e609SWolfgang Helbig 		dt.y = y + 1;
8990cb2e609SWolfgang Helbig 		dt.m = 1;
9000cb2e609SWolfgang Helbig 		dt.d = 1;
9010cb2e609SWolfgang Helbig 	} else {
9020cb2e609SWolfgang Helbig 		dt.y = y;
9030cb2e609SWolfgang Helbig 		dt.m = m + 2;
9040cb2e609SWolfgang Helbig 		dt.d = 1;
9050cb2e609SWolfgang Helbig 	}
9060cb2e609SWolfgang Helbig 	last = sndaysb(&dt);
9070cb2e609SWolfgang Helbig 
9080cb2e609SWolfgang Helbig 	if (jd_flag) {
9090cb2e609SWolfgang Helbig 		dt.y = y;
9100cb2e609SWolfgang Helbig 		dt.m = 1;
9110cb2e609SWolfgang Helbig 		dt.d = 1;
9120cb2e609SWolfgang Helbig 		jan1 = sndaysb(&dt);
9130cb2e609SWolfgang Helbig 	}
9140cb2e609SWolfgang Helbig 
9150cb2e609SWolfgang Helbig 	/*
9160cb2e609SWolfgang Helbig 	 * Set firsts to the day number of sunday of the first week of
9170cb2e609SWolfgang Helbig 	 * this month. (This might be in the last month)
9180cb2e609SWolfgang Helbig 	 */
9190cb2e609SWolfgang Helbig 	firsts = first - (weekday(first)+1) % 7;
9200cb2e609SWolfgang Helbig 
9210cb2e609SWolfgang Helbig 	/*
9220cb2e609SWolfgang Helbig 	 * Fill the lines with day of month or day of year (Julian day)
9230cb2e609SWolfgang Helbig 	 * line index: i, each line is one week. column index: j, each
9240cb2e609SWolfgang Helbig 	 * column is one day number. print column index: k.
9250cb2e609SWolfgang Helbig 	 */
9260cb2e609SWolfgang Helbig 	for (i = 0; i != 6; i++) {
927e454a171SRoman Divacky 		l = 0;
9280cb2e609SWolfgang Helbig 		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
929e454a171SRoman Divacky 		    j++, k += dw) {
9300cb2e609SWolfgang Helbig 			if (j >= first) {
9310cb2e609SWolfgang Helbig 				if (jd_flag)
9320cb2e609SWolfgang Helbig 					dt.d = j - jan1 + 1;
9330cb2e609SWolfgang Helbig 				else
9340cb2e609SWolfgang Helbig 					sdateb(j, &dt);
93592e97843SAlan Somers 				if (j == highlightdate && !flag_nohighlight
93692e97843SAlan Somers 				 && isatty(STDOUT_FILENO))
937cae11c25SEdwin Groothuis 					highlight(mlines->lines[i] + k,
938cae11c25SEdwin Groothuis 					    ds + dt.d * dw, dw, &l);
939cae11c25SEdwin Groothuis 				else
940e454a171SRoman Divacky 					memcpy(mlines->lines[i] + k + l,
9410cb2e609SWolfgang Helbig 					       ds + dt.d * dw, dw);
9420cb2e609SWolfgang Helbig 			} else
943e454a171SRoman Divacky 				memcpy(mlines->lines[i] + k + l, "    ", dw);
944e454a171SRoman Divacky 		}
9450cb2e609SWolfgang Helbig 		if (k == 0)
9460cb2e609SWolfgang Helbig 			mlines->lines[i][1] = '\0';
9470cb2e609SWolfgang Helbig 		else
948e454a171SRoman Divacky 			mlines->lines[i][k + l] = '\0';
9491d0e1dacSEdwin Groothuis 		mlines->extralen[i] = l;
9500cb2e609SWolfgang Helbig 	}
9510cb2e609SWolfgang Helbig }
9520cb2e609SWolfgang Helbig 
9534af997a8SEdwin Groothuis /* Put the local names of weekdays into the wds. */
954bf70beceSEd Schouten static void
955a4264dceSWolfgang Helbig mkweekdays(struct weekdays *wds)
9560cb2e609SWolfgang Helbig {
957bd97a998SHajimu UMEMOTO 	int i, len, width = 0;
9580cb2e609SWolfgang Helbig 	struct tm tm;
9594646cea7SDavid Schultz 	wchar_t buf[20];
9600cb2e609SWolfgang Helbig 
9610cb2e609SWolfgang Helbig 	memset(&tm, 0, sizeof(tm));
9620cb2e609SWolfgang Helbig 
9630cb2e609SWolfgang Helbig 	for (i = 0; i != 7; i++) {
9640cb2e609SWolfgang Helbig 		tm.tm_wday = (i+1) % 7;
965b74e3739SDon Lewis 		wcsftime(buf, sizeof(buf)/sizeof(buf[0]), L"%a", &tm);
96646f39347SHajimu UMEMOTO 		for (len = 2; len > 0; --len) {
967bd97a998SHajimu UMEMOTO 			if ((width = wcswidth(buf, len)) <= 2)
968bd97a998SHajimu UMEMOTO 				break;
969bd97a998SHajimu UMEMOTO 		}
970bd97a998SHajimu UMEMOTO 		wmemset(wds->names[i], L'\0', 4);
971bd97a998SHajimu UMEMOTO 		if (width == 1)
972bd97a998SHajimu UMEMOTO 			wds->names[i][0] = L' ';
973bd97a998SHajimu UMEMOTO 		wcsncat(wds->names[i], buf, len);
97446f39347SHajimu UMEMOTO 		wcsncat(wds->names[i], L" ", 1);
9750cb2e609SWolfgang Helbig 	}
9760cb2e609SWolfgang Helbig }
9770cb2e609SWolfgang Helbig 
9780cb2e609SWolfgang Helbig /*
9794af997a8SEdwin Groothuis  * Compute the day number of the first existing date after the first day in
9804af997a8SEdwin Groothuis  * month. (the first day in month and even the month might not exist!)
981f8a0edbaSWolfgang Helbig  */
982bf70beceSEd Schouten static int
983f8a0edbaSWolfgang Helbig firstday(int y, int m)
984f8a0edbaSWolfgang Helbig {
985f8a0edbaSWolfgang Helbig 	date dt;
986f8a0edbaSWolfgang Helbig 	int nd;
987f8a0edbaSWolfgang Helbig 
988f8a0edbaSWolfgang Helbig 	dt.y = y;
989f8a0edbaSWolfgang Helbig 	dt.m = m;
990f8a0edbaSWolfgang Helbig 	dt.d = 1;
9910851fbdfSEdwin Groothuis 	nd = sndaysr(&dt);
992cf5f6adfSWolfgang Helbig 	for (;;) {
9930851fbdfSEdwin Groothuis 		sdater(nd, &dt);
994cf5f6adfSWolfgang Helbig 		if ((dt.m >= m && dt.y == y) || dt.y > y)
995f8a0edbaSWolfgang Helbig 			return (nd);
996cf5f6adfSWolfgang Helbig 		else
997cf5f6adfSWolfgang Helbig 			nd++;
998cf5f6adfSWolfgang Helbig 	}
999cf5f6adfSWolfgang Helbig 	/* NEVER REACHED */
1000f8a0edbaSWolfgang Helbig }
1001f8a0edbaSWolfgang Helbig 
1002f8a0edbaSWolfgang Helbig /*
10030cb2e609SWolfgang Helbig  * Compute the number of days from date, obey the local switch from
10040cb2e609SWolfgang Helbig  * Julian to Gregorian if specified by the user.
10050cb2e609SWolfgang Helbig  */
1006bf70beceSEd Schouten static int
10070851fbdfSEdwin Groothuis sndaysr(struct date *d)
10080cb2e609SWolfgang Helbig {
10090cb2e609SWolfgang Helbig 
10100cb2e609SWolfgang Helbig 	if (nswitch != 0)
10110cb2e609SWolfgang Helbig 		if (nswitch < ndaysj(d))
10120cb2e609SWolfgang Helbig 			return (ndaysg(d));
10130cb2e609SWolfgang Helbig 		else
10140cb2e609SWolfgang Helbig 			return (ndaysj(d));
10150cb2e609SWolfgang Helbig 	else
10160cb2e609SWolfgang Helbig 		return ndaysg(d);
10170cb2e609SWolfgang Helbig }
10180cb2e609SWolfgang Helbig 
10190cb2e609SWolfgang Helbig /*
10200cb2e609SWolfgang Helbig  * Compute the number of days from date, obey the switch from
10210cb2e609SWolfgang Helbig  * Julian to Gregorian as used by UK and her colonies.
10220cb2e609SWolfgang Helbig  */
1023bf70beceSEd Schouten static int
10240cb2e609SWolfgang Helbig sndaysb(struct date *d)
10250cb2e609SWolfgang Helbig {
10260cb2e609SWolfgang Helbig 
10270cb2e609SWolfgang Helbig 	if (nswitchb < ndaysj(d))
10280cb2e609SWolfgang Helbig 		return (ndaysg(d));
10290cb2e609SWolfgang Helbig 	else
10300cb2e609SWolfgang Helbig 		return (ndaysj(d));
10310cb2e609SWolfgang Helbig }
10320cb2e609SWolfgang Helbig 
10334af997a8SEdwin Groothuis /* Inverse of sndays. */
1034bf70beceSEd Schouten static struct date *
10350851fbdfSEdwin Groothuis sdater(int nd, struct date *d)
10360cb2e609SWolfgang Helbig {
10370cb2e609SWolfgang Helbig 
10380cb2e609SWolfgang Helbig 	if (nswitch < nd)
10390cb2e609SWolfgang Helbig 		return (gdate(nd, d));
10400cb2e609SWolfgang Helbig 	else
10410cb2e609SWolfgang Helbig 		return (jdate(nd, d));
10420cb2e609SWolfgang Helbig }
10430cb2e609SWolfgang Helbig 
10444af997a8SEdwin Groothuis /* Inverse of sndaysb. */
1045bf70beceSEd Schouten static struct date *
10460cb2e609SWolfgang Helbig sdateb(int nd, struct date *d)
10470cb2e609SWolfgang Helbig {
10480cb2e609SWolfgang Helbig 
10490cb2e609SWolfgang Helbig 	if (nswitchb < nd)
10500cb2e609SWolfgang Helbig 		return (gdate(nd, d));
10510cb2e609SWolfgang Helbig 	else
10520cb2e609SWolfgang Helbig 		return (jdate(nd, d));
10530cb2e609SWolfgang Helbig }
10540cb2e609SWolfgang Helbig 
10554af997a8SEdwin Groothuis /* Center string t in string s of length w by putting enough leading blanks. */
1056bf70beceSEd Schouten static char *
10570cb2e609SWolfgang Helbig center(char *s, char *t, int w)
10580cb2e609SWolfgang Helbig {
10590851fbdfSEdwin Groothuis 	char blanks[MAX_WIDTH];
10600cb2e609SWolfgang Helbig 
10610cb2e609SWolfgang Helbig 	memset(blanks, ' ', sizeof(blanks));
10620cb2e609SWolfgang Helbig 	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
10630cb2e609SWolfgang Helbig 	return (s);
10640cb2e609SWolfgang Helbig }
1065ac6d1c22SPeter Pentchev 
10664af997a8SEdwin Groothuis /* Center string t in string s of length w by putting enough leading blanks. */
1067bf70beceSEd Schouten static wchar_t *
10684646cea7SDavid Schultz wcenter(wchar_t *s, wchar_t *t, int w)
10694646cea7SDavid Schultz {
10700851fbdfSEdwin Groothuis 	char blanks[MAX_WIDTH];
10714646cea7SDavid Schultz 
10724646cea7SDavid Schultz 	memset(blanks, ' ', sizeof(blanks));
10734646cea7SDavid Schultz 	swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
10744646cea7SDavid Schultz 	return (s);
10754646cea7SDavid Schultz }
10764646cea7SDavid Schultz 
1077bf70beceSEd Schouten static int
1078ba29aec0SGarrett Wollman parsemonth(const char *s, int *m, int *y)
1079ac6d1c22SPeter Pentchev {
1080ba29aec0SGarrett Wollman 	int nm, ny;
1081ac6d1c22SPeter Pentchev 	char *cp;
1082ac6d1c22SPeter Pentchev 	struct tm tm;
1083ac6d1c22SPeter Pentchev 
1084ba29aec0SGarrett Wollman 	nm = (int)strtol(s, &cp, 10);
1085ba29aec0SGarrett Wollman 	if (cp != s) {
1086ba29aec0SGarrett Wollman 		ny = *y;
1087ba29aec0SGarrett Wollman 		if (*cp == '\0') {
1088ba29aec0SGarrett Wollman 			;	/* no special action */
1089ba29aec0SGarrett Wollman 		} else if (*cp == 'f' || *cp == 'F') {
1090ba29aec0SGarrett Wollman 			if (nm <= *m)
1091ba29aec0SGarrett Wollman 				ny++;
1092ba29aec0SGarrett Wollman 		} else if (*cp == 'p' || *cp == 'P') {
1093ba29aec0SGarrett Wollman 			if (nm >= *m)
1094ba29aec0SGarrett Wollman 				ny--;
1095ba29aec0SGarrett Wollman 		} else
1096ba29aec0SGarrett Wollman 			return (1);
1097ba29aec0SGarrett Wollman 		if (nm < 1 || nm > 12)
1098ba29aec0SGarrett Wollman 			return 1;
1099ba29aec0SGarrett Wollman 		*m = nm;
1100ba29aec0SGarrett Wollman 		*y = ny;
1101ac6d1c22SPeter Pentchev 		return (0);
1102ac6d1c22SPeter Pentchev 	}
1103ba29aec0SGarrett Wollman 	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
1104ba29aec0SGarrett Wollman 		*m = tm.tm_mon + 1;
1105ba29aec0SGarrett Wollman 		return (0);
1106ba29aec0SGarrett Wollman 	}
1107ba29aec0SGarrett Wollman 	return (1);
1108ba29aec0SGarrett Wollman }
1109cae11c25SEdwin Groothuis 
1110bf70beceSEd Schouten static void
1111cae11c25SEdwin Groothuis highlight(char *dst, char *src, int len, int *extralen)
1112cae11c25SEdwin Groothuis {
1113cae11c25SEdwin Groothuis 	static int first = 1;
1114cae11c25SEdwin Groothuis 	static const char *term_so, *term_se;
1115cae11c25SEdwin Groothuis 
1116cae11c25SEdwin Groothuis 	if (first) {
1117bcad3423SEric van Gyzen 		static char cbuf[512];
1118bcad3423SEric van Gyzen 		char tbuf[1024], *b;
1119cae11c25SEdwin Groothuis 
1120cae11c25SEdwin Groothuis 		term_se = term_so = NULL;
1121cae11c25SEdwin Groothuis 
11224af997a8SEdwin Groothuis 		/* On how to highlight on this type of terminal (if any). */
1123cae11c25SEdwin Groothuis 		if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1124cae11c25SEdwin Groothuis 			b = cbuf;
1125cae11c25SEdwin Groothuis 			term_so = tgetstr("so", &b);
1126cae11c25SEdwin Groothuis 			term_se = tgetstr("se", &b);
1127cae11c25SEdwin Groothuis 		}
1128cae11c25SEdwin Groothuis 
1129cae11c25SEdwin Groothuis 		first = 0;
1130cae11c25SEdwin Groothuis 	}
1131cae11c25SEdwin Groothuis 
1132cae11c25SEdwin Groothuis 	/*
1133cae11c25SEdwin Groothuis 	 * This check is not necessary, should have been handled before calling
1134cae11c25SEdwin Groothuis 	 * this function.
1135cae11c25SEdwin Groothuis 	 */
1136cae11c25SEdwin Groothuis 	if (flag_nohighlight) {
1137cae11c25SEdwin Groothuis 		memcpy(dst, src, len);
1138cae11c25SEdwin Groothuis 		return;
1139cae11c25SEdwin Groothuis 	}
1140cae11c25SEdwin Groothuis 
11414af997a8SEdwin Groothuis 	/*
11424af997a8SEdwin Groothuis 	 * If it is a real terminal, use the data from the termcap database.
11434af997a8SEdwin Groothuis 	 */
1144cae11c25SEdwin Groothuis 	if (term_so != NULL && term_se != NULL) {
11454af997a8SEdwin Groothuis 		/* separator. */
1146cae11c25SEdwin Groothuis 		dst[0] = ' ';
1147cae11c25SEdwin Groothuis 		dst++;
11484af997a8SEdwin Groothuis 		/* highlight on. */
1149cae11c25SEdwin Groothuis 		memcpy(dst, term_so, strlen(term_so));
1150cae11c25SEdwin Groothuis 		dst += strlen(term_so);
11514af997a8SEdwin Groothuis 		/* the actual text. (minus leading space) */
1152cae11c25SEdwin Groothuis 		len--;
1153cae11c25SEdwin Groothuis 		src++;
1154cae11c25SEdwin Groothuis 		memcpy(dst, src, len);
1155cae11c25SEdwin Groothuis 		dst += len;
11564af997a8SEdwin Groothuis 		/* highlight off. */
1157cae11c25SEdwin Groothuis 		memcpy(dst, term_se, strlen(term_se));
1158cae11c25SEdwin Groothuis 		*extralen = strlen(term_so) + strlen(term_se);
1159cae11c25SEdwin Groothuis 		return;
1160cae11c25SEdwin Groothuis 	}
1161cae11c25SEdwin Groothuis 
1162cae11c25SEdwin Groothuis 	/*
11634af997a8SEdwin Groothuis 	 * Otherwise, print a _, backspace and the letter.
1164cae11c25SEdwin Groothuis 	 */
1165cae11c25SEdwin Groothuis 	*extralen = 0;
11664af997a8SEdwin Groothuis 	/* skip leading space. */
1167cae11c25SEdwin Groothuis 	src++;
1168cae11c25SEdwin Groothuis 	len--;
11694af997a8SEdwin Groothuis 	/* separator. */
1170cae11c25SEdwin Groothuis 	dst[0] = ' ';
1171cae11c25SEdwin Groothuis 	dst++;
1172cae11c25SEdwin Groothuis 	while (len > 0) {
11734af997a8SEdwin Groothuis 		/* _ and backspace. */
1174cae11c25SEdwin Groothuis 		memcpy(dst, "_\010", 2);
1175cae11c25SEdwin Groothuis 		dst += 2;
1176cae11c25SEdwin Groothuis 		*extralen += 2;
11774af997a8SEdwin Groothuis 		/* the character. */
1178cae11c25SEdwin Groothuis 		*dst++ = *src++;
1179cae11c25SEdwin Groothuis 		len--;
1180cae11c25SEdwin Groothuis 	}
11814af997a8SEdwin Groothuis 	return;
1182cae11c25SEdwin Groothuis }
1183