ncal.c revision 138036
131744Shelbig/*-
231744Shelbig * Copyright (c) 1997 Wolfgang Helbig
331744Shelbig * All rights reserved.
431744Shelbig *
531744Shelbig * Redistribution and use in source and binary forms, with or without
631744Shelbig * modification, are permitted provided that the following conditions
731744Shelbig * are met:
831744Shelbig * 1. Redistributions of source code must retain the above copyright
931744Shelbig *    notice, this list of conditions and the following disclaimer.
1031744Shelbig * 2. Redistributions in binary form must reproduce the above copyright
1131744Shelbig *    notice, this list of conditions and the following disclaimer in the
1231744Shelbig *    documentation and/or other materials provided with the distribution.
1331744Shelbig *
1431744Shelbig * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1531744Shelbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1631744Shelbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1731744Shelbig * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1831744Shelbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1931744Shelbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2031744Shelbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2131744Shelbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2231744Shelbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2331744Shelbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2431744Shelbig * SUCH DAMAGE.
2531744Shelbig */
2632310Scharnier
2732310Scharnier#ifndef lint
2832310Scharnierstatic const char rcsid[] =
2950477Speter  "$FreeBSD: head/usr.bin/ncal/ncal.c 138036 2004-11-23 22:57:17Z wollman $";
3032310Scharnier#endif /* not lint */
3132310Scharnier
3231744Shelbig#include <calendar.h>
3374612Sache#include <ctype.h>
3431744Shelbig#include <err.h>
3574573Sache#include <langinfo.h>
3631744Shelbig#include <locale.h>
3731744Shelbig#include <stdio.h>
3831744Shelbig#include <stdlib.h>
3931744Shelbig#include <string.h>
4031744Shelbig#include <sysexits.h>
4131744Shelbig#include <time.h>
4231744Shelbig#include <unistd.h>
4331744Shelbig
4431744Shelbig/* Width of one month with backward compatibility */
4531744Shelbig#define MONTH_WIDTH_B_J 27
4631744Shelbig#define MONTH_WIDTH_B 20
4731744Shelbig
4831744Shelbig#define MONTH_WIDTH_J 24
4931744Shelbig#define MONTH_WIDTH 18
5031744Shelbig
5131744Shelbig#define MAX_WIDTH 28
5231744Shelbig
5331744Shelbigtypedef struct date date;
5431744Shelbig
5531744Shelbigstruct monthlines {
5631744Shelbig	char name[MAX_WIDTH + 1];
5731744Shelbig	char lines[7][MAX_WIDTH + 1];
5831744Shelbig	char weeks[MAX_WIDTH + 1];
5931744Shelbig};
6031744Shelbig
6131744Shelbigstruct weekdays {
6231744Shelbig	char names[7][4];
6331744Shelbig};
6431744Shelbig
6531744Shelbig/* The switches from Julian to Gregorian in some countries */
6631744Shelbigstatic struct djswitch {
6795641Smarkm	const char *cc;	/* Country code according to ISO 3166 */
6895641Smarkm	const char *nm;	/* Name of country */
6931744Shelbig	date dt;	/* Last day of Julian calendar */
7031744Shelbig} switches[] = {
7153963Sache	{"AL", "Albania",       {1912, 11, 30}},
7253963Sache	{"AT", "Austria",       {1583, 10,  5}},
7353963Sache	{"AU", "Australia",     {1752,  9,  2}},
7453963Sache	{"BE", "Belgium",       {1582, 12, 14}},
7553963Sache	{"BG", "Bulgaria",      {1916,  3, 18}},
7653963Sache	{"CA", "Canada",        {1752,  9,  2}},
7753963Sache	{"CH", "Switzerland",   {1655,  2, 28}},
7853963Sache	{"CN", "China",         {1911, 12, 18}},
7953963Sache	{"CZ", "Czech Republic",{1584,  1,  6}},
8053963Sache	{"DE", "Germany",       {1700,  2, 18}},
8153963Sache	{"DK", "Denmark",       {1700,  2, 18}},
8253963Sache	{"ES", "Spain",         {1582, 10,  4}},
8353963Sache	{"FI", "Finland",       {1753,  2, 17}},
8453963Sache	{"FR", "France",        {1582, 12,  9}},
8553963Sache	{"GB", "United Kingdom",{1752,  9,  2}},
8653963Sache	{"GR", "Greece",        {1924,  3,  9}},
8753963Sache	{"HU", "Hungary",       {1587, 10, 21}},
8853963Sache	{"IS", "Iceland",       {1700, 11, 16}},
8953963Sache	{"IT", "Italy",         {1582, 10,  4}},
9053963Sache	{"JP", "Japan",         {1918, 12, 18}},
9153963Sache	{"LI", "Lithuania",     {1918,  2,  1}},
9253963Sache	{"LN", "Latin",         {9999, 05, 31}},
9353963Sache	{"LU", "Luxembourg",    {1582, 12, 14}},
9453963Sache	{"LV", "Latvia",        {1918,  2,  1}},
9553963Sache	{"NL", "Netherlands",   {1582, 12, 14}},
9653963Sache	{"NO", "Norway",        {1700,  2, 18}},
9753963Sache	{"PL", "Poland",        {1582, 10,  4}},
9853963Sache	{"PT", "Portugal",      {1582, 10,  4}},
9953963Sache	{"RO", "Romania",       {1919,  3, 31}},
10053963Sache	{"RU", "Russia",        {1918,  1, 31}},
10153963Sache	{"SI", "Slovenia",      {1919,  3,  4}},
10253963Sache	{"SW", "Sweden",        {1753,  2, 17}},
10353963Sache	{"TR", "Turkey",        {1926, 12, 18}},
10453963Sache	{"US", "United States", {1752,  9,  2}},
10553963Sache	{"YU", "Yugoslavia",    {1919,  3,  4}}
10631744Shelbig};
10731744Shelbig
10832215Shelbigstruct djswitch *dftswitch =
10932215Shelbig    switches + sizeof(switches) / sizeof(struct djswitch) - 2;
11032215Shelbig    /* default switch (should be "US") */
11132215Shelbig
11231744Shelbig/* Table used to print day of month and week numbers */
11331744Shelbigchar daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
11431744Shelbig		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
11531744Shelbig		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
11631744Shelbig		" 48 49 50 51 52 53";
11731744Shelbig
11831744Shelbig/* Table used to print day of year and week numbers */
11931744Shelbigchar jdaystr[] = "       1   2   3   4   5   6   7   8   9"
12031744Shelbig		 "  10  11  12  13  14  15  16  17  18  19"
12131744Shelbig		 "  20  21  22  23  24  25  26  27  28  29"
12231744Shelbig		 "  30  31  32  33  34  35  36  37  38  39"
12331744Shelbig		 "  40  41  42  43  44  45  46  47  48  49"
12431744Shelbig		 "  50  51  52  53  54  55  56  57  58  59"
12531744Shelbig		 "  60  61  62  63  64  65  66  67  68  69"
12631744Shelbig		 "  70  71  72  73  74  75  76  77  78  79"
12731744Shelbig		 "  80  81  82  83  84  85  86  87  88  89"
12831744Shelbig		 "  90  91  92  93  94  95  96  97  98  99"
12931744Shelbig		 " 100 101 102 103 104 105 106 107 108 109"
13031744Shelbig		 " 110 111 112 113 114 115 116 117 118 119"
13131744Shelbig		 " 120 121 122 123 124 125 126 127 128 129"
13231744Shelbig		 " 130 131 132 133 134 135 136 137 138 139"
13331744Shelbig		 " 140 141 142 143 144 145 146 147 148 149"
13431744Shelbig		 " 150 151 152 153 154 155 156 157 158 159"
13531744Shelbig		 " 160 161 162 163 164 165 166 167 168 169"
13631744Shelbig		 " 170 171 172 173 174 175 176 177 178 179"
13731744Shelbig		 " 180 181 182 183 184 185 186 187 188 189"
13831744Shelbig		 " 190 191 192 193 194 195 196 197 198 199"
13931744Shelbig		 " 200 201 202 203 204 205 206 207 208 209"
14031744Shelbig		 " 210 211 212 213 214 215 216 217 218 219"
14131744Shelbig		 " 220 221 222 223 224 225 226 227 228 229"
14231744Shelbig		 " 230 231 232 233 234 235 236 237 238 239"
14331744Shelbig		 " 240 241 242 243 244 245 246 247 248 249"
14431744Shelbig		 " 250 251 252 253 254 255 256 257 258 259"
14531744Shelbig		 " 260 261 262 263 264 265 266 267 268 269"
14631744Shelbig		 " 270 271 272 273 274 275 276 277 278 279"
14731744Shelbig		 " 280 281 282 283 284 285 286 287 288 289"
14831744Shelbig		 " 290 291 292 293 294 295 296 297 298 299"
14931744Shelbig		 " 300 301 302 303 304 305 306 307 308 309"
15031744Shelbig		 " 310 311 312 313 314 315 316 317 318 319"
15131744Shelbig		 " 320 321 322 323 324 325 326 327 328 329"
15231744Shelbig		 " 330 331 332 333 334 335 336 337 338 339"
15331744Shelbig		 " 340 341 342 343 344 345 346 347 348 349"
15431744Shelbig		 " 350 351 352 353 354 355 356 357 358 359"
15531744Shelbig		 " 360 361 362 363 364 365 366";
15631744Shelbig
15732139Shelbigint     flag_weeks;		/* user wants number of week */
15831744Shelbigint     nswitch;		/* user defined switch date */
15931744Shelbigint	nswitchb;		/* switch date for backward compatibility */
16031744Shelbig
16131744Shelbigchar   *center(char *s, char *t, int w);
16231744Shelbigvoid	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
16331744Shelbigvoid    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
16431744Shelbigvoid    mkweekdays(struct weekdays * wds);
165104369Sroamint     parsemonth(const char *s);
16631744Shelbigvoid    printcc(void);
16731744Shelbigvoid    printeaster(int year, int julian, int orthodox);
16831744Shelbigvoid    printmonth(int year, int month, int jd_flag);
16931744Shelbigvoid    printmonthb(int year, int month, int jd_flag);
17031744Shelbigvoid    printyear(int year, int jd_flag);
17131744Shelbigvoid    printyearb(int year, int jd_flag);
17232282Shelbigint	firstday(int y, int m);
17331744Shelbigdate   *sdate(int ndays, struct date * d);
17431744Shelbigdate   *sdateb(int ndays, struct date * d);
17531744Shelbigint     sndays(struct date * d);
17631744Shelbigint     sndaysb(struct date * d);
17732310Scharnierstatic void usage(void);
17831744Shelbigint     weekdayb(int nd);
17931744Shelbig
18031744Shelbigint
18131744Shelbigmain(int argc, char *argv[])
18231744Shelbig{
18331744Shelbig	struct  djswitch *p, *q;	/* to search user defined switch date */
18431744Shelbig	date	never = {10000, 1, 1};	/* outside valid range of dates */
18531744Shelbig	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
18631744Shelbig	int     ch;			/* holds the option character */
18731744Shelbig	int     m = 0;			/* month */
18831744Shelbig	int	y = 0;			/* year */
18931744Shelbig	int     flag_backward = 0;	/* user called cal--backward compat. */
19031744Shelbig	int     flag_hole_year = 0;	/* user wants the whole year */
19131744Shelbig	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
19231744Shelbig	int     flag_julian_day = 0;	/* user wants the Julian day
19331744Shelbig					 * numbers */
19431744Shelbig	int	flag_orthodox = 0;	/* use wants Orthodox easter */
19531744Shelbig	int	flag_easter = 0;	/* use wants easter date */
19631744Shelbig	char	*cp;			/* character pointer */
197138036Swollman	char	*flag_month = NULL;	/* requested month as string */
19895641Smarkm	const char    *locale;		/* locale to get country code */
19931744Shelbig
20032215Shelbig	/*
20132215Shelbig	 * Use locale to determine the country code,
20232215Shelbig	 * and use the country code to determine the default
20332215Shelbig	 * switchdate and date format from the switches table.
20432215Shelbig	 */
20574612Sache	if (setlocale(LC_ALL, "") == NULL)
20632215Shelbig		warn("setlocale");
20774612Sache	locale = setlocale(LC_TIME, NULL);
20878016Sache	if (locale == NULL ||
20978016Sache	    strcmp(locale, "C") == 0 ||
21078027Sache	    strcmp(locale, "POSIX") == 0 ||
21178027Sache	    strcmp(locale, "ASCII") == 0 ||
21278027Sache	    strcmp(locale, "US-ASCII") == 0)
21332215Shelbig		locale = "_US";
21432215Shelbig	q = switches + sizeof(switches) / sizeof(struct djswitch);
21532215Shelbig	for (p = switches; p != q; p++)
21632215Shelbig		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
21732215Shelbig			break;
21832215Shelbig	if (p == q) {
21932215Shelbig		nswitch = ndaysj(&dftswitch->dt);
22032215Shelbig	} else {
22132215Shelbig		nswitch = ndaysj(&p->dt);
22232215Shelbig		dftswitch = p;
22332215Shelbig	}
22431744Shelbig
22532215Shelbig
22631744Shelbig	/*
22731744Shelbig	 * Get the filename portion of argv[0] and set flag_backward if
22831744Shelbig	 * this program is called "cal".
22931744Shelbig	 */
230138036Swollman	cp = strrchr(argv[0], '/');
231138036Swollman	cp = (cp == NULL) ? argv[0] : cp + 1;
232138036Swollman	if (strcmp("cal", cp) == 0)
23331744Shelbig		flag_backward = 1;
23431744Shelbig
23531744Shelbig	/* Set the switch date to United Kingdom if backwards compatible */
23631744Shelbig	if (flag_backward)
23731744Shelbig		nswitchb = ndaysj(&ukswitch);
23831744Shelbig
239138036Swollman	while ((ch = getopt(argc, argv, "Jejm:ops:wy")) != -1)
24031744Shelbig		switch (ch) {
24131744Shelbig		case 'J':
24231744Shelbig			if (flag_backward)
24331744Shelbig				usage();
24431744Shelbig			nswitch = ndaysj(&never);
24531744Shelbig			flag_julian_cal = 1;
24631744Shelbig			break;
24731744Shelbig		case 'e':
24831744Shelbig			if (flag_backward)
24931744Shelbig				usage();
25031744Shelbig			flag_easter = 1;
25131744Shelbig			break;
25231744Shelbig		case 'j':
25331744Shelbig			flag_julian_day = 1;
25431744Shelbig			break;
255138036Swollman		case 'm':
256138036Swollman			flag_month = optarg;
257138036Swollman			break;
25831744Shelbig		case 'o':
25931744Shelbig			if (flag_backward)
26031744Shelbig				usage();
26131744Shelbig			flag_orthodox = 1;
26231744Shelbig			flag_easter = 1;
26331744Shelbig			break;
26431744Shelbig		case 'p':
26531744Shelbig			if (flag_backward)
26631744Shelbig				usage();
26731744Shelbig			printcc();
26831744Shelbig			return (0);
26931744Shelbig			break;
27031744Shelbig		case 's':
27131744Shelbig			if (flag_backward)
27231744Shelbig				usage();
27331744Shelbig			q = switches +
27431744Shelbig			    sizeof(switches) / sizeof(struct djswitch);
27531744Shelbig			for (p = switches;
27631744Shelbig			     p != q && strcmp(p->cc, optarg) != 0; p++)
27731744Shelbig				;
27831744Shelbig			if (p == q)
27931744Shelbig				errx(EX_USAGE,
28031744Shelbig				    "%s: invalid country code", optarg);
28131744Shelbig			nswitch = ndaysj(&(p->dt));
28231744Shelbig			break;
28331744Shelbig		case 'w':
28431744Shelbig			if (flag_backward)
28531744Shelbig				usage();
28631744Shelbig			flag_weeks = 1;
28731744Shelbig			break;
28831744Shelbig		case 'y':
28931744Shelbig			flag_hole_year = 1;
29031744Shelbig			break;
29131744Shelbig		default:
29231744Shelbig			usage();
29331744Shelbig		}
29431744Shelbig
29531744Shelbig	argc -= optind;
29631744Shelbig	argv += optind;
29731744Shelbig
29831744Shelbig	switch (argc) {
29931744Shelbig	case 2:
30031744Shelbig		if (flag_easter)
30131744Shelbig			usage();
302138036Swollman		flag_month = *argv++;
30331744Shelbig		/* FALLTHROUGH */
30431744Shelbig	case 1:
30531744Shelbig		y = atoi(*argv++);
30631744Shelbig		if (y < 1 || y > 9999)
30731744Shelbig			errx(EX_USAGE, "year %d not in range 1..9999", y);
30831744Shelbig		break;
30931744Shelbig	case 0:
310138036Swollman		{
311138036Swollman			time_t t;
312138036Swollman			struct tm *tm;
313138036Swollman
314138036Swollman			t = time(NULL);
315138036Swollman			tm = localtime(&t);
316138036Swollman			y = tm->tm_year + 1900;
317138036Swollman			m = tm->tm_mon + 1;
318138036Swollman		}
31931744Shelbig		break;
32031744Shelbig	default:
32131744Shelbig		usage();
32231744Shelbig	}
32331744Shelbig
324138036Swollman	if (flag_month != NULL) {
325138036Swollman		m = parsemonth(flag_month);
326138036Swollman		if (m < 1 || m > 12)
327138036Swollman			errx(EX_USAGE,
328138036Swollman			    "%s is neither a month number (1..12) nor a name",
329138036Swollman			    flag_month);
330138036Swollman	}
331138036Swollman
33231744Shelbig	if (flag_easter)
33331744Shelbig		printeaster(y, flag_julian_cal, flag_orthodox);
33431744Shelbig	else if (argc == 1 || flag_hole_year)
33531744Shelbig		if (flag_backward)
33631744Shelbig			printyearb(y, flag_julian_day);
33731744Shelbig		else
33831744Shelbig			printyear(y, flag_julian_day);
33931744Shelbig	else
34031744Shelbig		if (flag_backward)
34131744Shelbig			printmonthb(y, m, flag_julian_day);
34231744Shelbig		else
34331744Shelbig			printmonth(y, m, flag_julian_day);
34431744Shelbig
34531744Shelbig	return (0);
34631744Shelbig}
34731744Shelbig
34832310Scharnierstatic void
34931744Shelbigusage(void)
35031744Shelbig{
35131744Shelbig
352138036Swollman	fputs(
353138036Swollman	    "usage: cal [-jy] [[month] year]\n"
354138036Swollman	    "       cal [-j] [-m month] [year]\n"
355138036Swollman	    "       ncal [-Jjpwy] [-s country_code] [[month] year]\n"
356138036Swollman	    "       ncal [-Jeo] [year]\n", stderr);
35731744Shelbig	exit(EX_USAGE);
35831744Shelbig}
35931744Shelbig
36031744Shelbig/* print the assumed switches for all countries */
36131744Shelbigvoid
36231744Shelbigprintcc(void)
36331744Shelbig{
36431744Shelbig	struct djswitch *p;
36531744Shelbig	int n;	/* number of lines to print */
36631744Shelbig	int m;	/* offset from left to right table entry on the same line */
36731744Shelbig
36832215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d"
36932215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
37032215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
37131744Shelbig
37231744Shelbig	n = sizeof(switches) / sizeof(struct djswitch);
37331744Shelbig	m = (n + 1) / 2;
37431744Shelbig	n /= 2;
37532215Shelbig	for (p = switches; p != switches + n; p++)
37632215Shelbig		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
37731744Shelbig	if (m != n)
37832215Shelbig		printf(FSTR"\n", FSTRARG(p));
37931744Shelbig}
38031744Shelbig
38131744Shelbig/* print the date of easter sunday */
38231744Shelbigvoid
38331744Shelbigprinteaster(int y, int julian, int orthodox)
38431744Shelbig{
38532215Shelbig	date    dt;
38632215Shelbig	struct tm tm;
38732215Shelbig	char    buf[80];
38874573Sache	static int d_first = -1;
38931744Shelbig
39074573Sache	if (d_first < 0)
39174573Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
39231744Shelbig	/* force orthodox easter for years before 1583 */
39331744Shelbig	if (y < 1583)
39431744Shelbig		orthodox = 1;
39531744Shelbig
39631744Shelbig	if (orthodox)
39731744Shelbig		if (julian)
39831744Shelbig			easteroj(y, &dt);
39931744Shelbig		else
40031744Shelbig			easterog(y, &dt);
40131744Shelbig	else
40231744Shelbig		easterg(y, &dt);
40332215Shelbig
40432215Shelbig	memset(&tm, 0, sizeof(tm));
40532215Shelbig	tm.tm_year = dt.y - 1900;
40632215Shelbig	tm.tm_mon  = dt.m - 1;
40732215Shelbig	tm.tm_mday = dt.d;
40874573Sache	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
40932215Shelbig	printf("%s\n", buf);
41031744Shelbig}
41131744Shelbig
41231744Shelbigvoid
41331744Shelbigprintmonth(int y, int m, int jd_flag)
41431744Shelbig{
41531744Shelbig	struct monthlines month;
41631744Shelbig	struct weekdays wds;
41731744Shelbig	int i;
41831744Shelbig
41931744Shelbig	mkmonth(y, m - 1, jd_flag, &month);
42031744Shelbig	mkweekdays(&wds);
42131744Shelbig	printf("    %s %d\n", month.name, y);
42231744Shelbig	for (i = 0; i != 7; i++)
42331744Shelbig		printf("%.2s%s\n", wds.names[i], month.lines[i]);
42431744Shelbig	if (flag_weeks)
42531744Shelbig		printf("  %s\n", month.weeks);
42631744Shelbig}
42731744Shelbig
42831744Shelbigvoid
42931744Shelbigprintmonthb(int y, int m, int jd_flag)
43031744Shelbig{
43131744Shelbig	struct monthlines month;
43231744Shelbig	struct weekdays wds;
43331744Shelbig	char s[MAX_WIDTH], t[MAX_WIDTH];
43431744Shelbig	int i;
43531744Shelbig	int mw;
43631744Shelbig
43731744Shelbig	mkmonthb(y, m - 1, jd_flag, &month);
43831744Shelbig	mkweekdays(&wds);
43931744Shelbig
44031744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
44131744Shelbig
44231744Shelbig	sprintf(s, "%s %d", month.name, y);
44331744Shelbig	printf("%s\n", center(t, s, mw));
44431744Shelbig
44531744Shelbig	if (jd_flag)
44631744Shelbig		printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
44731744Shelbig			wds.names[1], wds.names[2], wds.names[3],
44831744Shelbig			wds.names[4], wds.names[5]);
44931744Shelbig	else
45031744Shelbig		printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
45131744Shelbig			wds.names[1], wds.names[2], wds.names[3],
45231744Shelbig			wds.names[4], wds.names[5]);
45331744Shelbig
45431744Shelbig	for (i = 0; i != 6; i++)
45531744Shelbig		printf("%s\n", month.lines[i]+1);
45631744Shelbig}
45731744Shelbig
45831744Shelbigvoid
45931744Shelbigprintyear(int y, int jd_flag)
46031744Shelbig{
46131744Shelbig	struct monthlines year[12];
46231744Shelbig	struct weekdays wds;
46331744Shelbig	char    s[80], t[80];
46431744Shelbig	int     i, j;
46531744Shelbig	int     mpl;
46631744Shelbig	int     mw;
46731744Shelbig
46831744Shelbig	for (i = 0; i != 12; i++)
46931744Shelbig		mkmonth(y, i, jd_flag, year + i);
47031744Shelbig	mkweekdays(&wds);
47131744Shelbig	mpl = jd_flag ? 3 : 4;
47231744Shelbig	mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
47331744Shelbig
47431744Shelbig	sprintf(s, "%d", y);
47531744Shelbig	printf("%s\n", center(t, s, mpl * mw));
47631744Shelbig
47731744Shelbig	for (j = 0; j != 12; j += mpl) {
47831744Shelbig		printf("    %-*s%-*s",
47931744Shelbig		    mw, year[j].name,
48031744Shelbig		    mw, year[j + 1].name);
48131744Shelbig		if (mpl == 3)
48231744Shelbig			printf("%s\n", year[j + 2].name);
48331744Shelbig		else
48431744Shelbig			printf("%-*s%s\n",
48531744Shelbig		    	    mw, year[j + 2].name,
48631744Shelbig		    	    year[j + 3].name);
48731744Shelbig		for (i = 0; i != 7; i++) {
48831744Shelbig			printf("%.2s%-*s%-*s",
48931744Shelbig			    wds.names[i],
49031744Shelbig			    mw, year[j].lines[i],
49131744Shelbig			    mw, year[j + 1].lines[i]);
49231744Shelbig			if (mpl == 3)
49331744Shelbig				printf("%s\n", year[j + 2].lines[i]);
49431744Shelbig			else
49531744Shelbig				printf("%-*s%s\n",
49631744Shelbig			    	    mw, year[j + 2].lines[i],
49731744Shelbig			    	    year[j + 3].lines[i]);
49831744Shelbig		}
49948566Sbillf		if (flag_weeks) {
50031744Shelbig			if (mpl == 3)
50131744Shelbig				printf("  %-*s%-*s%-s\n",
50231744Shelbig				    mw, year[j].weeks,
50331744Shelbig				    mw, year[j + 1].weeks,
50431744Shelbig				    year[j + 2].weeks);
50531744Shelbig			else
50631744Shelbig				printf("  %-*s%-*s%-*s%-s\n",
50731744Shelbig				    mw, year[j].weeks,
50831744Shelbig				    mw, year[j + 1].weeks,
50931744Shelbig				    mw, year[j + 2].weeks,
51031744Shelbig				    year[j + 3].weeks);
51148566Sbillf		}
51231744Shelbig	}
51331744Shelbig}
51431744Shelbig
51531744Shelbigvoid
51631744Shelbigprintyearb(int y, int jd_flag)
51731744Shelbig{
51831744Shelbig	struct monthlines year[12];
51931744Shelbig	struct weekdays wds;
52031744Shelbig	char	s[80], t[80];
52131744Shelbig	int     i, j;
52231744Shelbig	int     mpl;
52331744Shelbig	int     mw;
52431744Shelbig
52531744Shelbig	for (i = 0; i != 12; i++)
52631744Shelbig		mkmonthb(y, i, jd_flag, year + i);
52731744Shelbig	mkweekdays(&wds);
52831744Shelbig	mpl = jd_flag ? 2 : 3;
52931744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
53031744Shelbig
53131744Shelbig	sprintf(s, "%d", y);
53231744Shelbig	printf("%s\n\n", center(t, s, mw * mpl + mpl));
53331744Shelbig
53431744Shelbig	for (j = 0; j != 12; j += mpl) {
53531744Shelbig		printf("%-*s  ", mw, center(s, year[j].name, mw));
53631744Shelbig		if (mpl == 2)
53731744Shelbig			printf("%s\n", center(s, year[j + 1].name, mw));
53831744Shelbig		else
53931744Shelbig			printf("%-*s  %s\n", mw,
54031744Shelbig			    center(s, year[j + 1].name, mw),
54131744Shelbig			    center(t, year[j + 2].name, mw));
54231744Shelbig
54331744Shelbig		if (mpl == 2)
54431744Shelbig			printf(" %s %s %s %s %s %s %s "
54531744Shelbig			       " %s %s %s %s %s %s %.2s\n",
54631744Shelbig				wds.names[6], wds.names[0], wds.names[1],
54731744Shelbig				wds.names[2], wds.names[3], wds.names[4],
54831744Shelbig				wds.names[5],
54931744Shelbig				wds.names[6], wds.names[0], wds.names[1],
55031744Shelbig				wds.names[2], wds.names[3], wds.names[4],
55131744Shelbig				wds.names[5]);
55231744Shelbig		else
55331744Shelbig			printf("%s%s%s%s%s%s%s "
55431744Shelbig				"%s%s%s%s%s%s%s "
55531744Shelbig				"%s%s%s%s%s%s%.2s\n",
55631744Shelbig				wds.names[6], wds.names[0], wds.names[1],
55731744Shelbig				wds.names[2], wds.names[3], wds.names[4],
55831744Shelbig				wds.names[5],
55931744Shelbig				wds.names[6], wds.names[0], wds.names[1],
56031744Shelbig				wds.names[2], wds.names[3], wds.names[4],
56131744Shelbig				wds.names[5],
56231744Shelbig				wds.names[6], wds.names[0], wds.names[1],
56331744Shelbig				wds.names[2], wds.names[3], wds.names[4],
56431744Shelbig				wds.names[5]);
56531744Shelbig		for (i = 0; i != 6; i++) {
56631744Shelbig			if (mpl == 2)
56731744Shelbig				printf("%-*s  %s\n",
56831744Shelbig			    mw, year[j].lines[i]+1,
56931744Shelbig			    year[j + 1].lines[i]+1);
57031744Shelbig			else
57131744Shelbig				printf("%-*s  %-*s  %s\n",
57231744Shelbig			    mw, year[j].lines[i]+1,
57331744Shelbig			    mw, year[j + 1].lines[i]+1,
57431744Shelbig			    year[j + 2].lines[i]+1);
57531744Shelbig
57631744Shelbig		}
57731744Shelbig	}
57831744Shelbig}
57931744Shelbig
58031744Shelbigvoid
58131744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
58231744Shelbig{
58331744Shelbig
58431744Shelbig	struct tm tm;		/* for strftime printing local names of
58531744Shelbig				 * months */
58631744Shelbig	date    dt;		/* handy date */
58731744Shelbig	int     dw;		/* width of numbers */
58831744Shelbig	int     first;		/* first day of month */
58931744Shelbig	int     firstm;		/* first day of first week of month */
59031744Shelbig	int     i, j, k;	/* just indices */
59131744Shelbig	int     last;		/* the first day of next month */
59231744Shelbig	int     jan1 = 0;	/* the first day of this year */
59331744Shelbig	char   *ds;		/* pointer to day strings (daystr or
59431744Shelbig				 * jdaystr) */
59531744Shelbig
59631744Shelbig	/* Set name of month. */
59731744Shelbig	memset(&tm, 0, sizeof(tm));
59831744Shelbig	tm.tm_mon = m;
59951201Sache	strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
60074612Sache	mlines->name[0] = toupper((unsigned char)mlines->name[0]);
60131744Shelbig
60231744Shelbig	/*
60331744Shelbig	 * Set first and last to the day number of the first day of this
60431744Shelbig	 * month and the first day of next month respectively. Set jan1 to
60532282Shelbig	 * the day number of the first day of this year.
60631744Shelbig	 */
60732282Shelbig	first = firstday(y, m + 1);
60832282Shelbig	if (m == 11)
60932282Shelbig		last = firstday(y + 1, 1);
61032282Shelbig	else
61132282Shelbig		last = firstday(y, m + 2);
61231744Shelbig
61332282Shelbig	if (jd_flag)
61432525Shelbig		jan1 = firstday(y, 1);
61531744Shelbig
61631744Shelbig	/*
61731744Shelbig	 * Set firstm to the day number of monday of the first week of
61831744Shelbig	 * this month. (This might be in the last month)
61931744Shelbig	 */
62031744Shelbig	firstm = first - weekday(first);
62131744Shelbig
62231744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
62331744Shelbig	if (jd_flag) {
62431744Shelbig		ds = jdaystr;
62531744Shelbig		dw = 4;
62631744Shelbig	} else {
62731744Shelbig		ds = daystr;
62831744Shelbig		dw = 3;
62931744Shelbig	}
63031744Shelbig
63131744Shelbig	/*
63231744Shelbig	 * Fill the lines with day of month or day of year (julian day)
63331744Shelbig	 * line index: i, each line is one weekday. column index: j, each
63431744Shelbig	 * column is one day number. print column index: k.
63531744Shelbig	 */
63631744Shelbig	for (i = 0; i != 7; i++) {
63731744Shelbig		for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
63831744Shelbig			if (j >= first) {
63931744Shelbig				if (jd_flag)
64031744Shelbig					dt.d = j - jan1 + 1;
64131744Shelbig				else
64231744Shelbig					sdate(j, &dt);
64331744Shelbig				memcpy(mlines->lines[i] + k,
64431744Shelbig				       ds + dt.d * dw, dw);
64531744Shelbig			} else
64631744Shelbig				memcpy(mlines->lines[i] + k, "    ", dw);
64731744Shelbig		mlines->lines[i][k] = '\0';
64831744Shelbig
64931744Shelbig	}
65031744Shelbig
65131744Shelbig	/* fill the weeknumbers */
65231744Shelbig	if (flag_weeks) {
65331744Shelbig		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
65431744Shelbig			if (j <= nswitch)
65531744Shelbig				memset(mlines->weeks + k, ' ', dw);
65631744Shelbig			else
65731744Shelbig				memcpy(mlines->weeks + k,
65831744Shelbig				    ds + week(j, &i)*dw, dw);
65931744Shelbig		mlines->weeks[k] = '\0';
66031744Shelbig	}
66131744Shelbig}
66231744Shelbig
66331744Shelbigvoid
66431744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
66531744Shelbig{
66631744Shelbig
66731744Shelbig	struct tm tm;		/* for strftime printing local names of
66831744Shelbig				 * months */
66931744Shelbig	date    dt;		/* handy date */
67031744Shelbig	int     dw;		/* width of numbers */
67131744Shelbig	int     first;		/* first day of month */
67231744Shelbig	int     firsts;		/* sunday of first week of month */
67331744Shelbig	int     i, j, k;	/* just indices */
67431744Shelbig	int     jan1 = 0;	/* the first day of this year */
67531744Shelbig	int     last;		/* the first day of next month */
67631744Shelbig	char   *ds;		/* pointer to day strings (daystr or
67731744Shelbig				 * jdaystr) */
67831744Shelbig
67931744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
68031744Shelbig	if (jd_flag) {
68131744Shelbig		ds = jdaystr;
68231744Shelbig		dw = 4;
68331744Shelbig	} else {
68431744Shelbig		ds = daystr;
68531744Shelbig		dw = 3;
68631744Shelbig	}
68731744Shelbig
68831744Shelbig	/* Set name of month centered */
68931744Shelbig	memset(&tm, 0, sizeof(tm));
69031744Shelbig	tm.tm_mon = m;
69151201Sache	strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
69274612Sache	mlines->name[0] = toupper((unsigned char)mlines->name[0]);
69331744Shelbig
69431744Shelbig	/*
69531744Shelbig	 * Set first and last to the day number of the first day of this
69631744Shelbig	 * month and the first day of next month respectively. Set jan1 to
69731744Shelbig	 * the day number of Jan 1st of this year.
69831744Shelbig	 */
69931744Shelbig	dt.y = y;
70031744Shelbig	dt.m = m + 1;
70131744Shelbig	dt.d = 1;
70231744Shelbig	first = sndaysb(&dt);
70331744Shelbig	if (m == 11) {
70431744Shelbig		dt.y = y + 1;
70531744Shelbig		dt.m = 1;
70631744Shelbig		dt.d = 1;
70731744Shelbig	} else {
70831744Shelbig		dt.y = y;
70931744Shelbig		dt.m = m + 2;
71031744Shelbig		dt.d = 1;
71131744Shelbig	}
71231744Shelbig	last = sndaysb(&dt);
71331744Shelbig
71431744Shelbig	if (jd_flag) {
71531744Shelbig		dt.y = y;
71631744Shelbig		dt.m = 1;
71731744Shelbig		dt.d = 1;
71831744Shelbig		jan1 = sndaysb(&dt);
71931744Shelbig	}
72031744Shelbig
72131744Shelbig	/*
72231744Shelbig	 * Set firsts to the day number of sunday of the first week of
72331744Shelbig	 * this month. (This might be in the last month)
72431744Shelbig	 */
72531744Shelbig	firsts = first - (weekday(first)+1) % 7;
72631744Shelbig
72731744Shelbig	/*
72831744Shelbig	 * Fill the lines with day of month or day of year (Julian day)
72931744Shelbig	 * line index: i, each line is one week. column index: j, each
73031744Shelbig	 * column is one day number. print column index: k.
73131744Shelbig	 */
73231744Shelbig	for (i = 0; i != 6; i++) {
73331744Shelbig		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
73431744Shelbig		     j++, k += dw)
73531744Shelbig			if (j >= first) {
73631744Shelbig				if (jd_flag)
73731744Shelbig					dt.d = j - jan1 + 1;
73831744Shelbig				else
73931744Shelbig					sdateb(j, &dt);
74031744Shelbig				memcpy(mlines->lines[i] + k,
74131744Shelbig				       ds + dt.d * dw, dw);
74231744Shelbig			} else
74331744Shelbig				memcpy(mlines->lines[i] + k, "    ", dw);
74431744Shelbig		if (k == 0)
74531744Shelbig			mlines->lines[i][1] = '\0';
74631744Shelbig		else
74731744Shelbig			mlines->lines[i][k] = '\0';
74831744Shelbig	}
74931744Shelbig}
75031744Shelbig
75131744Shelbig/* Put the local names of weekdays into the wds */
75232139Shelbigvoid
75332139Shelbigmkweekdays(struct weekdays *wds)
75431744Shelbig{
75574618Sache	int i, len;
75631744Shelbig	struct tm tm;
75774618Sache	char buf[20];
75831744Shelbig
75931744Shelbig	memset(&tm, 0, sizeof(tm));
76031744Shelbig
76131744Shelbig	for (i = 0; i != 7; i++) {
76231744Shelbig		tm.tm_wday = (i+1) % 7;
76374618Sache		strftime(buf, sizeof(buf), "%a", &tm);
76474618Sache		len = strlen(buf);
76574618Sache		if (len > 2)
76674618Sache			len = 2;
76774618Sache		strcpy(wds->names[i], "   ");
76874618Sache		strncpy(wds->names[i] + 2 - len, buf, len);
76931744Shelbig	}
77031744Shelbig}
77131744Shelbig
77231744Shelbig/*
77345064Shelbig * Compute the day number of the first
77445064Shelbig * existing date after the first day in month.
77545064Shelbig * (the first day in month and even the month might not exist!)
77632282Shelbig */
77732282Shelbigint
77832282Shelbigfirstday(int y, int m)
77932282Shelbig{
78032282Shelbig	date dt;
78132282Shelbig	int nd;
78232282Shelbig
78332282Shelbig	dt.y = y;
78432282Shelbig	dt.m = m;
78532282Shelbig	dt.d = 1;
78645064Shelbig	nd = sndays(&dt);
78745064Shelbig	for (;;) {
78845064Shelbig		sdate(nd, &dt);
78945064Shelbig		if ((dt.m >= m && dt.y == y) || dt.y > y)
79045064Shelbig			return (nd);
79145064Shelbig		else
79245064Shelbig			nd++;
79345064Shelbig	}
79445064Shelbig	/* NEVER REACHED */
79532282Shelbig}
79632282Shelbig
79732282Shelbig/*
79831744Shelbig * Compute the number of days from date, obey the local switch from
79931744Shelbig * Julian to Gregorian if specified by the user.
80031744Shelbig */
80131744Shelbigint
80231744Shelbigsndays(struct date *d)
80331744Shelbig{
80431744Shelbig
80531744Shelbig	if (nswitch != 0)
80631744Shelbig		if (nswitch < ndaysj(d))
80731744Shelbig			return (ndaysg(d));
80831744Shelbig		else
80931744Shelbig			return (ndaysj(d));
81031744Shelbig	else
81131744Shelbig		return ndaysg(d);
81231744Shelbig}
81331744Shelbig
81431744Shelbig/*
81531744Shelbig * Compute the number of days from date, obey the switch from
81631744Shelbig * Julian to Gregorian as used by UK and her colonies.
81731744Shelbig */
81831744Shelbigint
81931744Shelbigsndaysb(struct date *d)
82031744Shelbig{
82131744Shelbig
82231744Shelbig	if (nswitchb < ndaysj(d))
82331744Shelbig		return (ndaysg(d));
82431744Shelbig	else
82531744Shelbig		return (ndaysj(d));
82631744Shelbig}
82731744Shelbig
82831744Shelbig/* Inverse of sndays */
82931744Shelbigstruct date *
83031744Shelbigsdate(int nd, struct date *d)
83131744Shelbig{
83231744Shelbig
83331744Shelbig	if (nswitch < nd)
83431744Shelbig		return (gdate(nd, d));
83531744Shelbig	else
83631744Shelbig		return (jdate(nd, d));
83731744Shelbig}
83831744Shelbig
83931744Shelbig/* Inverse of sndaysb */
84031744Shelbigstruct date *
84131744Shelbigsdateb(int nd, struct date *d)
84231744Shelbig{
84331744Shelbig
84431744Shelbig	if (nswitchb < nd)
84531744Shelbig		return (gdate(nd, d));
84631744Shelbig	else
84731744Shelbig		return (jdate(nd, d));
84831744Shelbig}
84931744Shelbig
85031744Shelbig/* Center string t in string s of length w by putting enough leading blanks */
85131744Shelbigchar *
85231744Shelbigcenter(char *s, char *t, int w)
85331744Shelbig{
85431744Shelbig	char blanks[80];
85531744Shelbig
85631744Shelbig	memset(blanks, ' ', sizeof(blanks));
85731744Shelbig	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
85831744Shelbig	return (s);
85931744Shelbig}
860104369Sroam
861104369Sroamint
862104369Sroamparsemonth(const char *s)
863104369Sroam{
864104369Sroam	int v;
865104369Sroam	char *cp;
866104369Sroam	struct tm tm;
867104369Sroam
868104369Sroam	v = (int)strtol(s, &cp, 10);
869104369Sroam	if (cp != s)
870104369Sroam		return (v);
871104369Sroam	if (strptime(s, "%B", &tm) != NULL)
872104369Sroam		return (tm.tm_mon + 1);
873104369Sroam	if (strptime(s, "%b", &tm) != NULL)
874104369Sroam		return (tm.tm_mon + 1);
875104369Sroam	return (0);
876104369Sroam}
877