ncal: interpret a `.' as current year
[dragonfly.git] / usr.bin / ncal / ncal.c
blob5bc0f48b2d87339d204d0d0375342dc8896b7d11
1 /*-
2 * Copyright (c) 1997 Wolfgang Helbig
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.bin/ncal/ncal.c,v 1.11.2.3 2002/10/23 08:10:00 roam Exp $
27 * $DragonFly: src/usr.bin/ncal/ncal.c,v 1.2 2003/06/17 04:29:29 dillon Exp $
30 #include <calendar.h>
31 #include <ctype.h>
32 #include <err.h>
33 #include <langinfo.h>
34 #include <locale.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sysexits.h>
39 #include <time.h>
40 #include <unistd.h>
41 #include <term.h>
42 #undef lines /* term.h defines this */
44 /* Width of one month with backward compatibility */
45 #define MONTH_WIDTH_B_J 27
46 #define MONTH_WIDTH_B 20
48 #define MONTH_WIDTH_J 24
49 #define MONTH_WIDTH 18
51 #define MAX_WIDTH 64
53 typedef struct date date;
55 struct monthlines {
56 char name[MAX_WIDTH + 1];
57 char lines[7][MAX_WIDTH + 1];
58 char weeks[MAX_WIDTH + 1];
61 struct weekdays {
62 char names[7][4];
65 /* The switches from Julian to Gregorian in some countries */
66 static struct djswitch {
67 char *cc; /* Country code according to ISO 3166 */
68 char *nm; /* Name of country */
69 date dt; /* Last day of Julian calendar */
70 } switches[] = {
71 {"AL", "Albania", {1912, 11, 30}},
72 {"AT", "Austria", {1583, 10, 5}},
73 {"AU", "Australia", {1752, 9, 2}},
74 {"BE", "Belgium", {1582, 12, 14}},
75 {"BG", "Bulgaria", {1916, 3, 18}},
76 {"CA", "Canada", {1752, 9, 2}},
77 {"CH", "Switzerland", {1655, 2, 28}},
78 {"CN", "China", {1911, 12, 18}},
79 {"CZ", "Czech Republic",{1584, 1, 6}},
80 {"DE", "Germany", {1700, 2, 18}},
81 {"DK", "Denmark", {1700, 2, 18}},
82 {"ES", "Spain", {1582, 10, 4}},
83 {"FI", "Finland", {1753, 2, 17}},
84 {"FR", "France", {1582, 12, 9}},
85 {"GB", "United Kingdom",{1752, 9, 2}},
86 {"GR", "Greece", {1924, 3, 9}},
87 {"HU", "Hungary", {1587, 10, 21}},
88 {"IS", "Iceland", {1700, 11, 16}},
89 {"IT", "Italy", {1582, 10, 4}},
90 {"JP", "Japan", {1918, 12, 18}},
91 {"LI", "Lithuania", {1918, 2, 1}},
92 {"LN", "Latin", {9999, 05, 31}},
93 {"LU", "Luxembourg", {1582, 12, 14}},
94 {"LV", "Latvia", {1918, 2, 1}},
95 {"NL", "Netherlands", {1582, 12, 14}},
96 {"NO", "Norway", {1700, 2, 18}},
97 {"PL", "Poland", {1582, 10, 4}},
98 {"PT", "Portugal", {1582, 10, 4}},
99 {"RO", "Romania", {1919, 3, 31}},
100 {"RU", "Russia", {1918, 1, 31}},
101 {"SI", "Slovenia", {1919, 3, 4}},
102 {"SW", "Sweden", {1753, 2, 17}},
103 {"TR", "Turkey", {1926, 12, 18}},
104 {"US", "United States", {1752, 9, 2}},
105 {"YU", "Yugoslavia", {1919, 3, 4}}
108 struct djswitch *dftswitch =
109 switches + sizeof(switches) / sizeof(struct djswitch) - 2;
110 /* default switch (should be "US") */
112 /* Table used to print day of month and week numbers */
113 char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
114 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
115 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
116 " 48 49 50 51 52 53";
118 /* Table used to print day of year and week numbers */
119 char jdaystr[] = " 1 2 3 4 5 6 7 8 9"
120 " 10 11 12 13 14 15 16 17 18 19"
121 " 20 21 22 23 24 25 26 27 28 29"
122 " 30 31 32 33 34 35 36 37 38 39"
123 " 40 41 42 43 44 45 46 47 48 49"
124 " 50 51 52 53 54 55 56 57 58 59"
125 " 60 61 62 63 64 65 66 67 68 69"
126 " 70 71 72 73 74 75 76 77 78 79"
127 " 80 81 82 83 84 85 86 87 88 89"
128 " 90 91 92 93 94 95 96 97 98 99"
129 " 100 101 102 103 104 105 106 107 108 109"
130 " 110 111 112 113 114 115 116 117 118 119"
131 " 120 121 122 123 124 125 126 127 128 129"
132 " 130 131 132 133 134 135 136 137 138 139"
133 " 140 141 142 143 144 145 146 147 148 149"
134 " 150 151 152 153 154 155 156 157 158 159"
135 " 160 161 162 163 164 165 166 167 168 169"
136 " 170 171 172 173 174 175 176 177 178 179"
137 " 180 181 182 183 184 185 186 187 188 189"
138 " 190 191 192 193 194 195 196 197 198 199"
139 " 200 201 202 203 204 205 206 207 208 209"
140 " 210 211 212 213 214 215 216 217 218 219"
141 " 220 221 222 223 224 225 226 227 228 229"
142 " 230 231 232 233 234 235 236 237 238 239"
143 " 240 241 242 243 244 245 246 247 248 249"
144 " 250 251 252 253 254 255 256 257 258 259"
145 " 260 261 262 263 264 265 266 267 268 269"
146 " 270 271 272 273 274 275 276 277 278 279"
147 " 280 281 282 283 284 285 286 287 288 289"
148 " 290 291 292 293 294 295 296 297 298 299"
149 " 300 301 302 303 304 305 306 307 308 309"
150 " 310 311 312 313 314 315 316 317 318 319"
151 " 320 321 322 323 324 325 326 327 328 329"
152 " 330 331 332 333 334 335 336 337 338 339"
153 " 340 341 342 343 344 345 346 347 348 349"
154 " 350 351 352 353 354 355 356 357 358 359"
155 " 360 361 362 363 364 365 366";
157 int flag_weeks; /* user wants number of week */
158 int nswitch; /* user defined switch date */
159 int nswitchb; /* switch date for backward compatibility */
160 const char *term_so, *term_se;
161 int today;
163 char *center(char *s, char *t, int w);
164 void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
165 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
166 void mkweekdays(struct weekdays * wds);
167 int parsemonth(const char *s);
168 void printcc(void);
169 void printeaster(int year, int julian, int orthodox);
170 void printmonth(int year, int month, int jd_flag);
171 void printmonthb(int year, int month, int jd_flag);
172 void printyear(int year, int jd_flag);
173 void printyearb(int year, int jd_flag);
174 int firstday(int y, int m);
175 date *sdate(int ndays, struct date * d);
176 date *sdateb(int ndays, struct date * d);
177 int sndays(struct date * d);
178 int sndaysb(struct date * d);
179 static void usage(void);
180 int weekdayb(int nd);
183 main(int argc, char *argv[])
185 struct djswitch *p, *q; /* to search user defined switch date */
186 date never = {10000, 1, 1}; /* outside valid range of dates */
187 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */
188 date dt; /* today as date */
189 int ch; /* holds the option character */
190 int m = 0; /* month */
191 int y = 0; /* year */
192 int flag_backward = 0; /* user called cal--backward compat. */
193 int flag_hole_year = 0; /* user wants the whole year */
194 int flag_julian_cal = 0; /* user wants Julian Calendar */
195 int flag_julian_day = 0; /* user wants the Julian day
196 * numbers */
197 int flag_orthodox = 0; /* use wants Orthodox easter */
198 int flag_easter = 0; /* use wants easter date */
199 char *cp; /* character pointer */
200 char *locale; /* locale to get country code */
201 char tbuf[1024], cbuf[512], *b;
202 time_t t;
203 struct tm *tm1;
205 term_se = term_so = NULL;
206 today = 0;
207 t = time(NULL);
208 tm1 = localtime(&t);
209 y = dt.y = tm1->tm_year + 1900;
210 m = dt.m = tm1->tm_mon + 1;
211 dt.d = tm1->tm_mday;
212 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
213 b = cbuf;
214 term_so = tgetstr("so", &b);
215 term_se = tgetstr("se", &b);
216 today = sndaysb(&dt);
220 * Use locale to determine the country code,
221 * and use the country code to determine the default
222 * switchdate and date format from the switches table.
224 if ((locale = setlocale(LC_ALL, "")) == NULL)
225 warn("setlocale");
226 locale = setlocale(LC_TIME, NULL);
227 if (locale == NULL ||
228 strcmp(locale, "C") == 0 ||
229 strcmp(locale, "POSIX") == 0 ||
230 strcmp(locale, "ASCII") == 0 ||
231 strcmp(locale, "US-ASCII") == 0)
232 locale = "_US";
233 q = switches + sizeof(switches) / sizeof(struct djswitch);
234 for (p = switches; p != q; p++)
235 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
236 break;
237 if (p == q) {
238 nswitch = ndaysj(&dftswitch->dt);
239 } else {
240 nswitch = ndaysj(&p->dt);
241 dftswitch = p;
246 * Get the filename portion of argv[0] and set flag_backward if
247 * this program is called "cal".
249 for (cp = argv[0]; *cp; cp++)
251 while (cp >= argv[0] && *cp != '/')
252 cp--;
253 if (strcmp("cal", ++cp) == 0)
254 flag_backward = 1;
256 /* Set the switch date to United Kingdom if backwards compatible */
257 if (flag_backward)
258 nswitchb = ndaysj(&ukswitch);
260 while ((ch = getopt(argc, argv, "Jejops:wy")) != -1)
261 switch (ch) {
262 case 'J':
263 if (flag_backward)
264 usage();
265 nswitch = ndaysj(&never);
266 flag_julian_cal = 1;
267 break;
268 case 'e':
269 if (flag_backward)
270 usage();
271 flag_easter = 1;
272 break;
273 case 'j':
274 flag_julian_day = 1;
275 break;
276 case 'o':
277 if (flag_backward)
278 usage();
279 flag_orthodox = 1;
280 flag_easter = 1;
281 break;
282 case 'p':
283 if (flag_backward)
284 usage();
285 printcc();
286 return (0);
287 break;
288 case 's':
289 if (flag_backward)
290 usage();
291 q = switches +
292 sizeof(switches) / sizeof(struct djswitch);
293 for (p = switches;
294 p != q && strcmp(p->cc, optarg) != 0; p++)
296 if (p == q)
297 errx(EX_USAGE,
298 "%s: invalid country code", optarg);
299 nswitch = ndaysj(&(p->dt));
300 break;
301 case 'w':
302 if (flag_backward)
303 usage();
304 flag_weeks = 1;
305 break;
306 case 'y':
307 flag_hole_year = 1;
308 break;
309 default:
310 usage();
313 argc -= optind;
314 argv += optind;
316 switch (argc) {
317 case 2:
318 if (flag_easter)
319 usage();
320 m = parsemonth(*argv++);
321 if (m < 1 || m > 12)
322 errx(EX_USAGE,
323 "%s is neither a month number (1..12) nor a name",
324 argv[-1]);
325 /* FALLTHROUGH */
326 case 1:
327 if (strcmp(*argv, ".") == 0)
328 /* year was already set above */
329 argv++;
330 else
331 y = atoi(*argv++);
332 if (y < 1 || y > 9999)
333 errx(EX_USAGE, "year %d not in range 1..9999", y);
334 break;
335 case 0:
336 break;
337 default:
338 usage();
341 if (flag_easter)
342 printeaster(y, flag_julian_cal, flag_orthodox);
343 else if (argc == 1 || flag_hole_year) {
344 if (flag_backward)
345 printyearb(y, flag_julian_day);
346 else
347 printyear(y, flag_julian_day);
348 } else
349 if (flag_backward)
350 printmonthb(y, m, flag_julian_day);
351 else
352 printmonth(y, m, flag_julian_day);
354 return (0);
357 static void
358 usage(void)
361 fprintf(stderr, "%s\n%s\n%s\n",
362 "usage: cal [-jy] [[month] year]",
363 " ncal [-Jjpwy] [-s country_code] [[month] year]",
364 " ncal [-Jeo] [year]");
365 exit(EX_USAGE);
368 /* print the assumed switches for all countries */
369 void
370 printcc(void)
372 struct djswitch *p;
373 int n; /* number of lines to print */
374 int m; /* offset from left to right table entry on the same line */
376 #define FSTR "%c%s %-15s%4d-%02d-%02d"
377 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
378 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
380 n = sizeof(switches) / sizeof(struct djswitch);
381 m = (n + 1) / 2;
382 n /= 2;
383 for (p = switches; p != switches + n; p++)
384 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
385 if (m != n)
386 printf(FSTR"\n", FSTRARG(p));
389 /* print the date of easter sunday */
390 void
391 printeaster(int y, int julian, int orthodox)
393 date dt;
394 struct tm tm;
395 char buf[80];
396 static int d_first = -1;
398 if (d_first < 0)
399 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
400 /* force orthodox easter for years before 1583 */
401 if (y < 1583)
402 orthodox = 1;
404 if (orthodox)
405 if (julian)
406 easteroj(y, &dt);
407 else
408 easterog(y, &dt);
409 else
410 easterg(y, &dt);
412 memset(&tm, 0, sizeof(tm));
413 tm.tm_year = dt.y - 1900;
414 tm.tm_mon = dt.m - 1;
415 tm.tm_mday = dt.d;
416 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm);
417 printf("%s\n", buf);
420 void
421 printmonth(int y, int m, int jd_flag)
423 struct monthlines month;
424 struct weekdays wds;
425 int i;
427 mkmonth(y, m - 1, jd_flag, &month);
428 mkweekdays(&wds);
429 printf(" %s %d\n", month.name, y);
430 for (i = 0; i != 7; i++)
431 printf("%.2s%s\n", wds.names[i], month.lines[i]);
432 if (flag_weeks)
433 printf(" %s\n", month.weeks);
436 void
437 printmonthb(int y, int m, int jd_flag)
439 struct monthlines month;
440 struct weekdays wds;
441 char s[MAX_WIDTH], t[MAX_WIDTH];
442 int i;
443 int mw;
445 mkmonthb(y, m - 1, jd_flag, &month);
446 mkweekdays(&wds);
448 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
450 sprintf(s, "%s %d", month.name, y);
451 printf("%s\n", center(t, s, mw));
453 if (jd_flag)
454 printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
455 wds.names[1], wds.names[2], wds.names[3],
456 wds.names[4], wds.names[5]);
457 else
458 printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
459 wds.names[1], wds.names[2], wds.names[3],
460 wds.names[4], wds.names[5]);
462 for (i = 0; i != 6; i++)
463 printf("%s\n", month.lines[i]+1);
466 void
467 printyear(int y, int jd_flag)
469 struct monthlines year[12];
470 struct weekdays wds;
471 char s[80], t[80];
472 int i, j;
473 int mpl;
474 int mw;
476 for (i = 0; i != 12; i++)
477 mkmonth(y, i, jd_flag, year + i);
478 mkweekdays(&wds);
479 mpl = jd_flag ? 3 : 4;
480 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
482 sprintf(s, "%d", y);
483 printf("%s\n", center(t, s, mpl * mw));
485 for (j = 0; j != 12; j += mpl) {
486 printf(" %-*s%-*s",
487 mw, year[j].name,
488 mw, year[j + 1].name);
489 if (mpl == 3)
490 printf("%s\n", year[j + 2].name);
491 else
492 printf("%-*s%s\n",
493 mw, year[j + 2].name,
494 year[j + 3].name);
495 for (i = 0; i != 7; i++) {
496 printf("%.2s%s%s",
497 wds.names[i],
498 year[j].lines[i],
499 year[j + 1].lines[i]);
500 if (mpl == 3)
501 printf("%s\n", year[j + 2].lines[i]);
502 else
503 printf("%s%s\n",
504 year[j + 2].lines[i],
505 year[j + 3].lines[i]);
507 if (flag_weeks) {
508 if (mpl == 3)
509 printf(" %s%s%-s\n",
510 year[j].weeks,
511 year[j + 1].weeks,
512 year[j + 2].weeks);
513 else
514 printf(" %s%s%s%-s\n",
515 year[j].weeks,
516 year[j + 1].weeks,
517 year[j + 2].weeks,
518 year[j + 3].weeks);
523 void
524 printyearb(int y, int jd_flag)
526 struct monthlines year[12];
527 struct weekdays wds;
528 char s[80], t[80];
529 int i, j;
530 int mpl;
531 int mw;
533 for (i = 0; i != 12; i++)
534 mkmonthb(y, i, jd_flag, year + i);
535 mkweekdays(&wds);
536 mpl = jd_flag ? 2 : 3;
537 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
539 sprintf(s, "%d", y);
540 printf("%s\n\n", center(t, s, mw * mpl + mpl));
542 for (j = 0; j != 12; j += mpl) {
543 printf("%-*s ", mw, center(s, year[j].name, mw));
544 if (mpl == 2)
545 printf("%s\n", center(s, year[j + 1].name, mw));
546 else
547 printf("%-*s %s\n", mw,
548 center(s, year[j + 1].name, mw),
549 center(t, year[j + 2].name, mw));
551 if (mpl == 2)
552 printf(" %s %s %s %s %s %s %s "
553 " %s %s %s %s %s %s %.2s\n",
554 wds.names[6], wds.names[0], wds.names[1],
555 wds.names[2], wds.names[3], wds.names[4],
556 wds.names[5],
557 wds.names[6], wds.names[0], wds.names[1],
558 wds.names[2], wds.names[3], wds.names[4],
559 wds.names[5]);
560 else
561 printf("%s%s%s%s%s%s%s "
562 "%s%s%s%s%s%s%s "
563 "%s%s%s%s%s%s%.2s\n",
564 wds.names[6], wds.names[0], wds.names[1],
565 wds.names[2], wds.names[3], wds.names[4],
566 wds.names[5],
567 wds.names[6], wds.names[0], wds.names[1],
568 wds.names[2], wds.names[3], wds.names[4],
569 wds.names[5],
570 wds.names[6], wds.names[0], wds.names[1],
571 wds.names[2], wds.names[3], wds.names[4],
572 wds.names[5]);
573 for (i = 0; i != 6; i++) {
574 if (mpl == 2)
575 printf("%s %s\n",
576 year[j].lines[i]+1,
577 year[j + 1].lines[i]+1);
578 else
579 printf("%s %s %s\n",
580 year[j].lines[i]+1,
581 year[j + 1].lines[i]+1,
582 year[j + 2].lines[i]+1);
588 void
589 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
592 struct tm tm; /* for strftime printing local names of
593 * months */
594 date dt; /* handy date */
595 int dw; /* width of numbers */
596 int first; /* first day of month */
597 int firstm; /* first day of first week of month */
598 int i, j, k, l; /* just indices */
599 int last; /* the first day of next month */
600 int lastm; /* first day of first week of next month */
601 int jan1 = 0; /* the first day of this year */
602 char *ds; /* pointer to day strings (daystr or
603 * jdaystr) */
605 /* Set name of month. */
606 memset(&tm, 0, sizeof(tm));
607 tm.tm_mon = m;
608 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
609 mlines->name[0] = toupper((unsigned char)mlines->name[0]);
612 * Set first and last to the day number of the first day of this
613 * month and the first day of next month respectively. Set jan1 to
614 * the day number of the first day of this year.
616 first = firstday(y, m + 1);
617 if (m == 11)
618 last = firstday(y + 1, 1);
619 else
620 last = firstday(y, m + 2);
622 if (jd_flag)
623 jan1 = firstday(y, 1);
626 * Set firstm to the day number of monday of the first week of
627 * this month. (This might be in the last month)
628 * Set lastm to the day number of monday of the first week of
629 * the next month. Our canvas spans [firstm, lastm).
631 firstm = first - weekday(first);
632 lastm = last + 7 - weekday(last);
634 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
635 if (jd_flag) {
636 ds = jdaystr;
637 dw = 4;
638 } else {
639 ds = daystr;
640 dw = 3;
644 * Fill the lines with day of month or day of year (julian day)
645 * line index: i, each line is one weekday. column index: j, each
646 * column is one day number. print column index: k.
648 for (i = 0; i != 7; i++) {
649 l = 0;
650 for (j = firstm + i, k = 0; j < lastm; j += 7, k += dw) {
651 if (j >= first && j < last) {
652 if (jd_flag)
653 dt.d = j - jan1 + 1;
654 else
655 sdate(j, &dt);
656 if (j == today && (term_so != NULL && term_se != NULL)) {
657 l = strlen(term_so);
658 /* separator */
659 mlines->lines[i][k] = ' ';
661 /* the actual text */
662 memcpy(mlines->lines[i] + k + l,
663 ds + dt.d * dw, dw);
664 if (j == today && (term_so != NULL && term_se != NULL)) {
665 /* highlight on */
666 memcpy(mlines->lines[i] + k + 1, term_so, l);
667 /* highlight off */
668 memcpy(mlines->lines[i] + k + l + dw, term_se,
669 strlen(term_se));
670 l = strlen(term_se) + strlen(term_so);
672 } else
673 memcpy(mlines->lines[i] + k + l, " ", dw);
675 mlines->lines[i][k + l] = '\0';
679 /* fill the weeknumbers */
680 if (flag_weeks) {
681 for (j = firstm, k = 0; j < lastm; k += dw, j += 7)
682 if (j <= nswitch || j >= last)
683 memset(mlines->weeks + k, ' ', dw);
684 else
685 memcpy(mlines->weeks + k,
686 ds + week(j, &i)*dw, dw);
687 mlines->weeks[k] = '\0';
691 void
692 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
695 struct tm tm; /* for strftime printing local names of
696 * months */
697 date dt; /* handy date */
698 int dw; /* width of numbers */
699 int first; /* first day of month */
700 int firsts; /* sunday of first week of month */
701 int i, j, k, l; /* just indices */
702 int jan1 = 0; /* the first day of this year */
703 int last; /* the first day of next month */
704 int lasts; /* sunday of first week of next month */
705 char *ds; /* pointer to day strings (daystr or
706 * jdaystr) */
708 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
709 if (jd_flag) {
710 ds = jdaystr;
711 dw = 4;
712 } else {
713 ds = daystr;
714 dw = 3;
717 /* Set name of month centered */
718 memset(&tm, 0, sizeof(tm));
719 tm.tm_mon = m;
720 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
721 mlines->name[0] = toupper((unsigned char)mlines->name[0]);
724 * Set first and last to the day number of the first day of this
725 * month and the first day of next month respectively. Set jan1 to
726 * the day number of Jan 1st of this year.
728 dt.y = y;
729 dt.m = m + 1;
730 dt.d = 1;
731 first = sndaysb(&dt);
732 if (m == 11) {
733 dt.y = y + 1;
734 dt.m = 1;
735 dt.d = 1;
736 } else {
737 dt.y = y;
738 dt.m = m + 2;
739 dt.d = 1;
741 last = sndaysb(&dt);
743 if (jd_flag) {
744 dt.y = y;
745 dt.m = 1;
746 dt.d = 1;
747 jan1 = sndaysb(&dt);
751 * Set firsts to the day number of sunday of the first week of
752 * this month. (This might be in the last month)
753 * Set lasts to the day number of sunday of the first week of
754 * the next month. Our canvas spans [firsts, lasts).
756 firsts = first - (weekday(first)+1) % 7;
757 lasts = last + 7 - (weekday(last)+1) % 7;
760 * Fill the lines with day of month or day of year (Julian day)
761 * line index: i, each line is one week. column index: j, each
762 * column is one day number. print column index: k.
764 for (i = 0; i != 6; i++) {
765 l = 0;
766 for (j = firsts + 7 * i, k = 0; j < lasts && k != dw * 7;
767 j++, k += dw) {
768 if (j >= first && j < last) {
769 if (jd_flag)
770 dt.d = j - jan1 + 1;
771 else
772 sdateb(j, &dt);
773 if (j == today && (term_so != NULL && term_se != NULL)) {
774 l = strlen(term_so);
775 /* separator */
776 mlines->lines[i][k] = ' ';
778 /* the actual text */
779 memcpy(mlines->lines[i] + k + l,
780 ds + dt.d * dw, dw);
781 if (j == today && (term_so != NULL && term_se != NULL)) {
782 /* highlight on */
783 memcpy(mlines->lines[i] + k + 1, term_so, l);
784 /* highlight off */
785 memcpy(mlines->lines[i] + k + l + dw, term_se,
786 strlen(term_se));
787 l = strlen(term_se) + strlen(term_so);
789 } else
790 memcpy(mlines->lines[i] + k + l, " ", dw);
792 if (k == 0)
793 mlines->lines[i][1] = '\0';
794 else
795 mlines->lines[i][k + l] = '\0';
799 /* Put the local names of weekdays into the wds */
800 void
801 mkweekdays(struct weekdays *wds)
803 int i, len;
804 struct tm tm;
805 char buf[20];
807 memset(&tm, 0, sizeof(tm));
809 for (i = 0; i != 7; i++) {
810 tm.tm_wday = (i+1) % 7;
811 strftime(buf, sizeof(buf), "%a", &tm);
812 len = strlen(buf);
813 if (len > 2)
814 len = 2;
815 strcpy(wds->names[i], " ");
816 strncpy(wds->names[i] + 2 - len, buf, len);
821 * Compute the day number of the first
822 * existing date after the first day in month.
823 * (the first day in month and even the month might not exist!)
826 firstday(int y, int m)
828 date dt;
829 int nd;
831 dt.y = y;
832 dt.m = m;
833 dt.d = 1;
834 nd = sndays(&dt);
835 for (;;) {
836 sdate(nd, &dt);
837 if ((dt.m >= m && dt.y == y) || dt.y > y)
838 return (nd);
839 else
840 nd++;
842 /* NEVER REACHED */
846 * Compute the number of days from date, obey the local switch from
847 * Julian to Gregorian if specified by the user.
850 sndays(struct date *d)
853 if (nswitch != 0)
854 if (nswitch < ndaysj(d))
855 return (ndaysg(d));
856 else
857 return (ndaysj(d));
858 else
859 return ndaysg(d);
863 * Compute the number of days from date, obey the switch from
864 * Julian to Gregorian as used by UK and her colonies.
867 sndaysb(struct date *d)
870 if (nswitchb < ndaysj(d))
871 return (ndaysg(d));
872 else
873 return (ndaysj(d));
876 /* Inverse of sndays */
877 struct date *
878 sdate(int nd, struct date *d)
881 if (nswitch < nd)
882 return (gdate(nd, d));
883 else
884 return (jdate(nd, d));
887 /* Inverse of sndaysb */
888 struct date *
889 sdateb(int nd, struct date *d)
892 if (nswitchb < nd)
893 return (gdate(nd, d));
894 else
895 return (jdate(nd, d));
898 /* Center string t in string s of length w by putting enough leading blanks */
899 char *
900 center(char *s, char *t, int w)
902 char blanks[80];
904 memset(blanks, ' ', sizeof(blanks));
905 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
906 return (s);
910 parsemonth(const char *s)
912 int v;
913 char *cp;
914 struct tm tm;
916 v = (int)strtol(s, &cp, 10);
917 if (cp != s)
918 return (v);
919 if (strptime(s, "%B", &tm) != NULL)
920 return (tm.tm_mon + 1);
921 if (strptime(s, "%b", &tm) != NULL)
922 return (tm.tm_mon + 1);
923 return (0);