ncal: hilight current day
[dragonfly.git] / usr.bin / ncal / ncal.c
blob6fee8d9aefdc064eeb28dc1a0ea77b66a1f29d33
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 int ch; /* holds the option character */
189 int m = 0; /* month */
190 int y = 0; /* year */
191 int flag_backward = 0; /* user called cal--backward compat. */
192 int flag_hole_year = 0; /* user wants the whole year */
193 int flag_julian_cal = 0; /* user wants Julian Calendar */
194 int flag_julian_day = 0; /* user wants the Julian day
195 * numbers */
196 int flag_orthodox = 0; /* use wants Orthodox easter */
197 int flag_easter = 0; /* use wants easter date */
198 char *cp; /* character pointer */
199 char *locale; /* locale to get country code */
200 char tbuf[1024], cbuf[512], *b;
201 time_t t;
202 struct tm *tm1;
204 term_se = term_so = NULL;
205 today = 0;
206 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
207 date dt; /* handy date */
209 b = cbuf;
210 term_so = tgetstr("so", &b);
211 term_se = tgetstr("se", &b);
212 t = time(NULL);
213 tm1 = localtime(&t);
214 dt.y = tm1->tm_year + 1900;
215 dt.m = tm1->tm_mon + 1;
216 dt.d = tm1->tm_mday;
218 today = sndaysb(&dt);
222 * Use locale to determine the country code,
223 * and use the country code to determine the default
224 * switchdate and date format from the switches table.
226 if ((locale = setlocale(LC_ALL, "")) == NULL)
227 warn("setlocale");
228 locale = setlocale(LC_TIME, NULL);
229 if (locale == NULL ||
230 strcmp(locale, "C") == 0 ||
231 strcmp(locale, "POSIX") == 0 ||
232 strcmp(locale, "ASCII") == 0 ||
233 strcmp(locale, "US-ASCII") == 0)
234 locale = "_US";
235 q = switches + sizeof(switches) / sizeof(struct djswitch);
236 for (p = switches; p != q; p++)
237 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
238 break;
239 if (p == q) {
240 nswitch = ndaysj(&dftswitch->dt);
241 } else {
242 nswitch = ndaysj(&p->dt);
243 dftswitch = p;
248 * Get the filename portion of argv[0] and set flag_backward if
249 * this program is called "cal".
251 for (cp = argv[0]; *cp; cp++)
253 while (cp >= argv[0] && *cp != '/')
254 cp--;
255 if (strcmp("cal", ++cp) == 0)
256 flag_backward = 1;
258 /* Set the switch date to United Kingdom if backwards compatible */
259 if (flag_backward)
260 nswitchb = ndaysj(&ukswitch);
262 while ((ch = getopt(argc, argv, "Jejops:wy")) != -1)
263 switch (ch) {
264 case 'J':
265 if (flag_backward)
266 usage();
267 nswitch = ndaysj(&never);
268 flag_julian_cal = 1;
269 break;
270 case 'e':
271 if (flag_backward)
272 usage();
273 flag_easter = 1;
274 break;
275 case 'j':
276 flag_julian_day = 1;
277 break;
278 case 'o':
279 if (flag_backward)
280 usage();
281 flag_orthodox = 1;
282 flag_easter = 1;
283 break;
284 case 'p':
285 if (flag_backward)
286 usage();
287 printcc();
288 return (0);
289 break;
290 case 's':
291 if (flag_backward)
292 usage();
293 q = switches +
294 sizeof(switches) / sizeof(struct djswitch);
295 for (p = switches;
296 p != q && strcmp(p->cc, optarg) != 0; p++)
298 if (p == q)
299 errx(EX_USAGE,
300 "%s: invalid country code", optarg);
301 nswitch = ndaysj(&(p->dt));
302 break;
303 case 'w':
304 if (flag_backward)
305 usage();
306 flag_weeks = 1;
307 break;
308 case 'y':
309 flag_hole_year = 1;
310 break;
311 default:
312 usage();
315 argc -= optind;
316 argv += optind;
318 if (argc == 0) {
319 time_t t;
320 struct tm *tm;
322 t = time(NULL);
323 tm = localtime(&t);
324 y = tm->tm_year + 1900;
325 m = tm->tm_mon + 1;
328 switch (argc) {
329 case 2:
330 if (flag_easter)
331 usage();
332 m = parsemonth(*argv++);
333 if (m < 1 || m > 12)
334 errx(EX_USAGE,
335 "%s is neither a month number (1..12) nor a name",
336 argv[-1]);
337 /* FALLTHROUGH */
338 case 1:
339 y = atoi(*argv++);
340 if (y < 1 || y > 9999)
341 errx(EX_USAGE, "year %d not in range 1..9999", y);
342 break;
343 case 0:
344 break;
345 default:
346 usage();
349 if (flag_easter)
350 printeaster(y, flag_julian_cal, flag_orthodox);
351 else if (argc == 1 || flag_hole_year) {
352 /* disable the highlight for now */
353 today = 0;
354 if (flag_backward)
355 printyearb(y, flag_julian_day);
356 else
357 printyear(y, flag_julian_day);
358 } else
359 if (flag_backward)
360 printmonthb(y, m, flag_julian_day);
361 else
362 printmonth(y, m, flag_julian_day);
364 return (0);
367 static void
368 usage(void)
371 fprintf(stderr, "%s\n%s\n%s\n",
372 "usage: cal [-jy] [[month] year]",
373 " ncal [-Jjpwy] [-s country_code] [[month] year]",
374 " ncal [-Jeo] [year]");
375 exit(EX_USAGE);
378 /* print the assumed switches for all countries */
379 void
380 printcc(void)
382 struct djswitch *p;
383 int n; /* number of lines to print */
384 int m; /* offset from left to right table entry on the same line */
386 #define FSTR "%c%s %-15s%4d-%02d-%02d"
387 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
388 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
390 n = sizeof(switches) / sizeof(struct djswitch);
391 m = (n + 1) / 2;
392 n /= 2;
393 for (p = switches; p != switches + n; p++)
394 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
395 if (m != n)
396 printf(FSTR"\n", FSTRARG(p));
399 /* print the date of easter sunday */
400 void
401 printeaster(int y, int julian, int orthodox)
403 date dt;
404 struct tm tm;
405 char buf[80];
406 static int d_first = -1;
408 if (d_first < 0)
409 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
410 /* force orthodox easter for years before 1583 */
411 if (y < 1583)
412 orthodox = 1;
414 if (orthodox)
415 if (julian)
416 easteroj(y, &dt);
417 else
418 easterog(y, &dt);
419 else
420 easterg(y, &dt);
422 memset(&tm, 0, sizeof(tm));
423 tm.tm_year = dt.y - 1900;
424 tm.tm_mon = dt.m - 1;
425 tm.tm_mday = dt.d;
426 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm);
427 printf("%s\n", buf);
430 void
431 printmonth(int y, int m, int jd_flag)
433 struct monthlines month;
434 struct weekdays wds;
435 int i;
437 mkmonth(y, m - 1, jd_flag, &month);
438 mkweekdays(&wds);
439 printf(" %s %d\n", month.name, y);
440 for (i = 0; i != 7; i++)
441 printf("%.2s%s\n", wds.names[i], month.lines[i]);
442 if (flag_weeks)
443 printf(" %s\n", month.weeks);
446 void
447 printmonthb(int y, int m, int jd_flag)
449 struct monthlines month;
450 struct weekdays wds;
451 char s[MAX_WIDTH], t[MAX_WIDTH];
452 int i;
453 int mw;
455 mkmonthb(y, m - 1, jd_flag, &month);
456 mkweekdays(&wds);
458 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
460 sprintf(s, "%s %d", month.name, y);
461 printf("%s\n", center(t, s, mw));
463 if (jd_flag)
464 printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
465 wds.names[1], wds.names[2], wds.names[3],
466 wds.names[4], wds.names[5]);
467 else
468 printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
469 wds.names[1], wds.names[2], wds.names[3],
470 wds.names[4], wds.names[5]);
472 for (i = 0; i != 6; i++)
473 printf("%s\n", month.lines[i]+1);
476 void
477 printyear(int y, int jd_flag)
479 struct monthlines year[12];
480 struct weekdays wds;
481 char s[80], t[80];
482 int i, j;
483 int mpl;
484 int mw;
486 for (i = 0; i != 12; i++)
487 mkmonth(y, i, jd_flag, year + i);
488 mkweekdays(&wds);
489 mpl = jd_flag ? 3 : 4;
490 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
492 sprintf(s, "%d", y);
493 printf("%s\n", center(t, s, mpl * mw));
495 for (j = 0; j != 12; j += mpl) {
496 printf(" %-*s%-*s",
497 mw, year[j].name,
498 mw, year[j + 1].name);
499 if (mpl == 3)
500 printf("%s\n", year[j + 2].name);
501 else
502 printf("%-*s%s\n",
503 mw, year[j + 2].name,
504 year[j + 3].name);
505 for (i = 0; i != 7; i++) {
506 printf("%.2s%-*s%-*s",
507 wds.names[i],
508 mw, year[j].lines[i],
509 mw, year[j + 1].lines[i]);
510 if (mpl == 3)
511 printf("%s\n", year[j + 2].lines[i]);
512 else
513 printf("%-*s%s\n",
514 mw, year[j + 2].lines[i],
515 year[j + 3].lines[i]);
517 if (flag_weeks) {
518 if (mpl == 3)
519 printf(" %-*s%-*s%-s\n",
520 mw, year[j].weeks,
521 mw, year[j + 1].weeks,
522 year[j + 2].weeks);
523 else
524 printf(" %-*s%-*s%-*s%-s\n",
525 mw, year[j].weeks,
526 mw, year[j + 1].weeks,
527 mw, year[j + 2].weeks,
528 year[j + 3].weeks);
533 void
534 printyearb(int y, int jd_flag)
536 struct monthlines year[12];
537 struct weekdays wds;
538 char s[80], t[80];
539 int i, j;
540 int mpl;
541 int mw;
543 for (i = 0; i != 12; i++)
544 mkmonthb(y, i, jd_flag, year + i);
545 mkweekdays(&wds);
546 mpl = jd_flag ? 2 : 3;
547 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
549 sprintf(s, "%d", y);
550 printf("%s\n\n", center(t, s, mw * mpl + mpl));
552 for (j = 0; j != 12; j += mpl) {
553 printf("%-*s ", mw, center(s, year[j].name, mw));
554 if (mpl == 2)
555 printf("%s\n", center(s, year[j + 1].name, mw));
556 else
557 printf("%-*s %s\n", mw,
558 center(s, year[j + 1].name, mw),
559 center(t, year[j + 2].name, mw));
561 if (mpl == 2)
562 printf(" %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 else
571 printf("%s%s%s%s%s%s%s "
572 "%s%s%s%s%s%s%s "
573 "%s%s%s%s%s%s%.2s\n",
574 wds.names[6], wds.names[0], wds.names[1],
575 wds.names[2], wds.names[3], wds.names[4],
576 wds.names[5],
577 wds.names[6], wds.names[0], wds.names[1],
578 wds.names[2], wds.names[3], wds.names[4],
579 wds.names[5],
580 wds.names[6], wds.names[0], wds.names[1],
581 wds.names[2], wds.names[3], wds.names[4],
582 wds.names[5]);
583 for (i = 0; i != 6; i++) {
584 if (mpl == 2)
585 printf("%-*s %s\n",
586 mw, year[j].lines[i]+1,
587 year[j + 1].lines[i]+1);
588 else
589 printf("%-*s %-*s %s\n",
590 mw, year[j].lines[i]+1,
591 mw, year[j + 1].lines[i]+1,
592 year[j + 2].lines[i]+1);
598 void
599 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
602 struct tm tm; /* for strftime printing local names of
603 * months */
604 date dt; /* handy date */
605 int dw; /* width of numbers */
606 int first; /* first day of month */
607 int firstm; /* first day of first week of month */
608 int i, j, k, l; /* just indices */
609 int last; /* the first day of next month */
610 int jan1 = 0; /* the first day of this year */
611 char *ds; /* pointer to day strings (daystr or
612 * jdaystr) */
614 /* Set name of month. */
615 memset(&tm, 0, sizeof(tm));
616 tm.tm_mon = m;
617 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
618 mlines->name[0] = toupper((unsigned char)mlines->name[0]);
621 * Set first and last to the day number of the first day of this
622 * month and the first day of next month respectively. Set jan1 to
623 * the day number of the first day of this year.
625 first = firstday(y, m + 1);
626 if (m == 11)
627 last = firstday(y + 1, 1);
628 else
629 last = firstday(y, m + 2);
631 if (jd_flag)
632 jan1 = firstday(y, 1);
635 * Set firstm to the day number of monday of the first week of
636 * this month. (This might be in the last month)
638 firstm = first - weekday(first);
640 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
641 if (jd_flag) {
642 ds = jdaystr;
643 dw = 4;
644 } else {
645 ds = daystr;
646 dw = 3;
650 * Fill the lines with day of month or day of year (julian day)
651 * line index: i, each line is one weekday. column index: j, each
652 * column is one day number. print column index: k.
654 for (i = 0; i != 7; i++) {
655 l = 0;
656 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
657 if (j == today && (term_so != NULL && term_se != NULL)) {
658 l = strlen(term_so);
659 if (jd_flag)
660 dt.d = j - jan1 + 1;
661 else
662 sdateb(j, &dt);
663 /* separator */
664 mlines->lines[i][k] = ' ';
665 /* the actual text */
666 memcpy(mlines->lines[i] + k + l,
667 ds + dt.d * dw, dw);
668 /* highlight on */
669 memcpy(mlines->lines[i] + k + 1, term_so, l);
670 /* highlight off */
671 memcpy(mlines->lines[i] + k + l + dw, term_se,
672 strlen(term_se));
673 l = strlen(term_se) + strlen(term_so);
674 continue;
676 if (j >= first) {
677 if (jd_flag)
678 dt.d = j - jan1 + 1;
679 else
680 sdate(j, &dt);
681 memcpy(mlines->lines[i] + k + l,
682 ds + dt.d * dw, dw);
683 } else
684 memcpy(mlines->lines[i] + k + l, " ", dw);
686 mlines->lines[i][k + l] = '\0';
690 /* fill the weeknumbers */
691 if (flag_weeks) {
692 for (j = firstm, k = 0; j < last; k += dw, j += 7)
693 if (j <= nswitch)
694 memset(mlines->weeks + k, ' ', dw);
695 else
696 memcpy(mlines->weeks + k,
697 ds + week(j, &i)*dw, dw);
698 mlines->weeks[k] = '\0';
702 void
703 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
706 struct tm tm; /* for strftime printing local names of
707 * months */
708 date dt; /* handy date */
709 int dw; /* width of numbers */
710 int first; /* first day of month */
711 int firsts; /* sunday of first week of month */
712 int i, j, k, l; /* just indices */
713 int jan1 = 0; /* the first day of this year */
714 int last; /* the first day of next month */
715 char *ds; /* pointer to day strings (daystr or
716 * jdaystr) */
718 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
719 if (jd_flag) {
720 ds = jdaystr;
721 dw = 4;
722 } else {
723 ds = daystr;
724 dw = 3;
727 /* Set name of month centered */
728 memset(&tm, 0, sizeof(tm));
729 tm.tm_mon = m;
730 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
731 mlines->name[0] = toupper((unsigned char)mlines->name[0]);
734 * Set first and last to the day number of the first day of this
735 * month and the first day of next month respectively. Set jan1 to
736 * the day number of Jan 1st of this year.
738 dt.y = y;
739 dt.m = m + 1;
740 dt.d = 1;
741 first = sndaysb(&dt);
742 if (m == 11) {
743 dt.y = y + 1;
744 dt.m = 1;
745 dt.d = 1;
746 } else {
747 dt.y = y;
748 dt.m = m + 2;
749 dt.d = 1;
751 last = sndaysb(&dt);
753 if (jd_flag) {
754 dt.y = y;
755 dt.m = 1;
756 dt.d = 1;
757 jan1 = sndaysb(&dt);
761 * Set firsts to the day number of sunday of the first week of
762 * this month. (This might be in the last month)
764 firsts = first - (weekday(first)+1) % 7;
767 * Fill the lines with day of month or day of year (Julian day)
768 * line index: i, each line is one week. column index: j, each
769 * column is one day number. print column index: k.
771 for (i = 0; i != 6; i++) {
772 l = 0;
773 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
774 j++, k += dw) {
775 if (j == today && (term_so != NULL && term_se != NULL)) {
776 l = strlen(term_so);
777 if (jd_flag)
778 dt.d = j - jan1 + 1;
779 else
780 sdateb(j, &dt);
781 /* separator */
782 mlines->lines[i][k] = ' ';
783 /* the actual text */
784 memcpy(mlines->lines[i] + k + l,
785 ds + dt.d * dw, dw);
786 /* highlight on */
787 memcpy(mlines->lines[i] + k + 1, term_so, l);
788 /* highlight off */
789 memcpy(mlines->lines[i] + k + l + dw, term_se,
790 strlen(term_se));
791 l = strlen(term_se) + strlen(term_so);
792 continue;
794 if (j >= first) {
795 if (jd_flag)
796 dt.d = j - jan1 + 1;
797 else
798 sdateb(j, &dt);
799 memcpy(mlines->lines[i] + k + l,
800 ds + dt.d * dw, dw);
801 } else
802 memcpy(mlines->lines[i] + k + l, " ", dw);
804 if (k == 0)
805 mlines->lines[i][1] = '\0';
806 else
807 mlines->lines[i][k + l] = '\0';
811 /* Put the local names of weekdays into the wds */
812 void
813 mkweekdays(struct weekdays *wds)
815 int i, len;
816 struct tm tm;
817 char buf[20];
819 memset(&tm, 0, sizeof(tm));
821 for (i = 0; i != 7; i++) {
822 tm.tm_wday = (i+1) % 7;
823 strftime(buf, sizeof(buf), "%a", &tm);
824 len = strlen(buf);
825 if (len > 2)
826 len = 2;
827 strcpy(wds->names[i], " ");
828 strncpy(wds->names[i] + 2 - len, buf, len);
833 * Compute the day number of the first
834 * existing date after the first day in month.
835 * (the first day in month and even the month might not exist!)
838 firstday(int y, int m)
840 date dt;
841 int nd;
843 dt.y = y;
844 dt.m = m;
845 dt.d = 1;
846 nd = sndays(&dt);
847 for (;;) {
848 sdate(nd, &dt);
849 if ((dt.m >= m && dt.y == y) || dt.y > y)
850 return (nd);
851 else
852 nd++;
854 /* NEVER REACHED */
858 * Compute the number of days from date, obey the local switch from
859 * Julian to Gregorian if specified by the user.
862 sndays(struct date *d)
865 if (nswitch != 0)
866 if (nswitch < ndaysj(d))
867 return (ndaysg(d));
868 else
869 return (ndaysj(d));
870 else
871 return ndaysg(d);
875 * Compute the number of days from date, obey the switch from
876 * Julian to Gregorian as used by UK and her colonies.
879 sndaysb(struct date *d)
882 if (nswitchb < ndaysj(d))
883 return (ndaysg(d));
884 else
885 return (ndaysj(d));
888 /* Inverse of sndays */
889 struct date *
890 sdate(int nd, struct date *d)
893 if (nswitch < nd)
894 return (gdate(nd, d));
895 else
896 return (jdate(nd, d));
899 /* Inverse of sndaysb */
900 struct date *
901 sdateb(int nd, struct date *d)
904 if (nswitchb < nd)
905 return (gdate(nd, d));
906 else
907 return (jdate(nd, d));
910 /* Center string t in string s of length w by putting enough leading blanks */
911 char *
912 center(char *s, char *t, int w)
914 char blanks[80];
916 memset(blanks, ' ', sizeof(blanks));
917 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
918 return (s);
922 parsemonth(const char *s)
924 int v;
925 char *cp;
926 struct tm tm;
928 v = (int)strtol(s, &cp, 10);
929 if (cp != s)
930 return (v);
931 if (strptime(s, "%B", &tm) != NULL)
932 return (tm.tm_mon + 1);
933 if (strptime(s, "%b", &tm) != NULL)
934 return (tm.tm_mon + 1);
935 return (0);