2 * Copyright (c) 1997 Wolfgang Helbig
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
28 static const char rcsid[] =
46 #undef lines /* term.h defines this */
48 /* Width of one month with backward compatibility and in regular mode*/
49 #define MONTH_WIDTH_B_J 27
50 #define MONTH_WIDTH_B 20
52 #define MONTH_WIDTH_R_J 24
53 #define MONTH_WIDTH_R 18
57 typedef struct date date;
60 wchar_t name[MAX_WIDTH + 1];
61 char lines[7][MAX_WIDTH + 1];
62 char weeks[MAX_WIDTH + 1];
63 unsigned int linelen[7];
70 /* The switches from Julian to Gregorian in some countries */
71 static struct djswitch {
72 const char *cc; /* Country code according to ISO 3166 */
73 const char *nm; /* Name of country */
74 date dt; /* Last day of Julian calendar */
76 {"AL", "Albania", {1912, 11, 30}},
77 {"AT", "Austria", {1583, 10, 5}},
78 {"AU", "Australia", {1752, 9, 2}},
79 {"BE", "Belgium", {1582, 12, 14}},
80 {"BG", "Bulgaria", {1916, 3, 18}},
81 {"CA", "Canada", {1752, 9, 2}},
82 {"CH", "Switzerland", {1655, 2, 28}},
83 {"CN", "China", {1911, 12, 18}},
84 {"CZ", "Czech Republic",{1584, 1, 6}},
85 {"DE", "Germany", {1700, 2, 18}},
86 {"DK", "Denmark", {1700, 2, 18}},
87 {"ES", "Spain", {1582, 10, 4}},
88 {"FI", "Finland", {1753, 2, 17}},
89 {"FR", "France", {1582, 12, 9}},
90 {"GB", "United Kingdom",{1752, 9, 2}},
91 {"GR", "Greece", {1924, 3, 9}},
92 {"HU", "Hungary", {1587, 10, 21}},
93 {"IS", "Iceland", {1700, 11, 16}},
94 {"IT", "Italy", {1582, 10, 4}},
95 {"JP", "Japan", {1918, 12, 18}},
96 {"LI", "Lithuania", {1918, 2, 1}},
97 {"LN", "Latin", {9999, 05, 31}},
98 {"LU", "Luxembourg", {1582, 12, 14}},
99 {"LV", "Latvia", {1918, 2, 1}},
100 {"NL", "Netherlands", {1582, 12, 14}},
101 {"NO", "Norway", {1700, 2, 18}},
102 {"PL", "Poland", {1582, 10, 4}},
103 {"PT", "Portugal", {1582, 10, 4}},
104 {"RO", "Romania", {1919, 3, 31}},
105 {"RU", "Russia", {1918, 1, 31}},
106 {"SI", "Slovenia", {1919, 3, 4}},
107 {"SW", "Sweden", {1753, 2, 17}},
108 {"TR", "Turkey", {1926, 12, 18}},
109 {"US", "United States", {1752, 9, 2}},
110 {"YU", "Yugoslavia", {1919, 3, 4}}
113 struct djswitch *dftswitch =
114 switches + sizeof(switches) / sizeof(struct djswitch) - 2;
115 /* default switch (should be "US") */
117 /* Table used to print day of month and week numbers */
118 char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
119 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
120 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
121 " 48 49 50 51 52 53";
123 /* Table used to print day of year and week numbers */
124 char jdaystr[] = " 1 2 3 4 5 6 7 8 9"
125 " 10 11 12 13 14 15 16 17 18 19"
126 " 20 21 22 23 24 25 26 27 28 29"
127 " 30 31 32 33 34 35 36 37 38 39"
128 " 40 41 42 43 44 45 46 47 48 49"
129 " 50 51 52 53 54 55 56 57 58 59"
130 " 60 61 62 63 64 65 66 67 68 69"
131 " 70 71 72 73 74 75 76 77 78 79"
132 " 80 81 82 83 84 85 86 87 88 89"
133 " 90 91 92 93 94 95 96 97 98 99"
134 " 100 101 102 103 104 105 106 107 108 109"
135 " 110 111 112 113 114 115 116 117 118 119"
136 " 120 121 122 123 124 125 126 127 128 129"
137 " 130 131 132 133 134 135 136 137 138 139"
138 " 140 141 142 143 144 145 146 147 148 149"
139 " 150 151 152 153 154 155 156 157 158 159"
140 " 160 161 162 163 164 165 166 167 168 169"
141 " 170 171 172 173 174 175 176 177 178 179"
142 " 180 181 182 183 184 185 186 187 188 189"
143 " 190 191 192 193 194 195 196 197 198 199"
144 " 200 201 202 203 204 205 206 207 208 209"
145 " 210 211 212 213 214 215 216 217 218 219"
146 " 220 221 222 223 224 225 226 227 228 229"
147 " 230 231 232 233 234 235 236 237 238 239"
148 " 240 241 242 243 244 245 246 247 248 249"
149 " 250 251 252 253 254 255 256 257 258 259"
150 " 260 261 262 263 264 265 266 267 268 269"
151 " 270 271 272 273 274 275 276 277 278 279"
152 " 280 281 282 283 284 285 286 287 288 289"
153 " 290 291 292 293 294 295 296 297 298 299"
154 " 300 301 302 303 304 305 306 307 308 309"
155 " 310 311 312 313 314 315 316 317 318 319"
156 " 320 321 322 323 324 325 326 327 328 329"
157 " 330 331 332 333 334 335 336 337 338 339"
158 " 340 341 342 343 344 345 346 347 348 349"
159 " 350 351 352 353 354 355 356 357 358 359"
160 " 360 361 362 363 364 365 366";
162 int flag_weeks; /* user wants number of week */
163 int nswitch; /* user defined switch date */
164 int nswitchb; /* switch date for backward compatibility */
165 const char *term_so, *term_se;
168 char *center(char *s, char *t, int w);
169 wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170 void mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
171 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
172 void mkweekdays(struct weekdays * wds);
173 int parsemonth(const char *s, int *m, int *y);
175 void printeaster(int year, int julian, int orthodox);
176 int firstday(int y, int m);
177 date *sdater(int ndays, struct date * d);
178 date *sdateb(int ndays, struct date * d);
179 int sndaysr(struct date * d);
180 int sndaysb(struct date * d);
181 static void usage(void);
182 void monthranger(int year, int jd_flag, int m, int before, int after);
183 void monthrangeb(int year, int jd_flag, int m, int before, int after);
186 main(int argc, char *argv[])
188 struct djswitch *p, *q; /* to search user defined switch date */
189 date never = {10000, 1, 1}; /* outside valid range of dates */
190 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */
192 int ch; /* holds the option character */
193 int m = 0; /* month */
194 int y = 0; /* year */
195 int flag_backward = 0; /* user called cal--backward compat. */
196 int flag_hole_year = 0; /* user wants the whole year */
197 int flag_julian_cal = 0; /* user wants Julian Calendar */
198 int flag_julian_day = 0; /* user wants the Julian day
200 int flag_orthodox = 0; /* use wants Orthodox easter */
201 int flag_easter = 0; /* use wants easter date */
202 char *cp; /* character pointer */
203 char *flag_month = NULL; /* requested month as string */
204 char *flag_highlightdate = NULL;
206 const char *locale; /* locale to get country code */
207 char tbuf[1024], cbuf[512], *b;
209 /* On how to highlight on this terminal */
210 term_se = term_so = NULL;
211 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
213 term_so = tgetstr("so", &b);
214 term_se = tgetstr("se", &b);
218 * Use locale to determine the country code,
219 * and use the country code to determine the default
220 * switchdate and date format from the switches table.
222 if (setlocale(LC_ALL, "") == NULL)
224 locale = setlocale(LC_TIME, NULL);
225 if (locale == NULL ||
226 strcmp(locale, "C") == 0 ||
227 strcmp(locale, "POSIX") == 0 ||
228 strcmp(locale, "ASCII") == 0 ||
229 strcmp(locale, "US-ASCII") == 0)
231 q = switches + sizeof(switches) / sizeof(struct djswitch);
232 for (p = switches; p != q; p++)
233 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
236 nswitch = ndaysj(&dftswitch->dt);
238 nswitch = ndaysj(&p->dt);
244 * Get the filename portion of argv[0] and set flag_backward if
245 * this program is called "cal".
247 cp = strrchr(argv[0], '/');
248 cp = (cp == NULL) ? argv[0] : cp + 1;
249 if (strcmp("cal", cp) == 0)
252 /* Set the switch date to United Kingdom if backwards compatible */
254 nswitchb = ndaysj(&ukswitch);
258 while ((ch = getopt(argc, argv, "A:B:3Jbd:ehjm:ops:wy")) != -1)
264 after = strtol(optarg, NULL, 10);
266 errx(1, "Argument to -A must be positive");
269 before = strtol(optarg, NULL, 10);
271 errx(1, "Argument to -B must be positive");
276 nswitch = ndaysj(&never);
283 flag_highlightdate = optarg;
286 term_so = term_se = NULL;
315 sizeof(switches) / sizeof(struct djswitch);
317 p != q && strcmp(p->cc, optarg) != 0; p++)
321 "%s: invalid country code", optarg);
322 nswitch = ndaysj(&(p->dt));
343 flag_month = *argv++;
347 if (y < 1 || y > 9999)
348 errx(EX_USAGE, "year %d not in range 1..9999", y);
360 y = tm->tm_year + 1900;
372 if (flag_month != NULL) {
373 if (parsemonth(flag_month, &m, &y)) {
375 "%s is neither a month number (1..12) nor a name",
380 if (flag_highlightdate != NULL) {
381 dt.y = strtol(flag_highlightdate, NULL, 10);
382 dt.m = strtol(flag_highlightdate + 5, NULL, 10);
383 dt.d = strtol(flag_highlightdate + 8, NULL, 10);
390 dt.y = tm1->tm_year + 1900;
391 dt.m = tm1->tm_mon + 1;
394 today = sndaysb(&dt);
397 printeaster(y, flag_julian_cal, flag_orthodox);
400 monthrangeb(y, flag_julian_day, m, before, after);
402 monthranger(y, flag_julian_day, m, before, after);
411 "usage: cal [-hjy] [[month] year]\n"
412 " cal [-hj] [-m month] [year]\n"
413 " ncal [-hJjpwy] [-s country_code] [[month] year]\n"
414 " ncal [-hJeo] [year]\n"
415 "for debug the highlighting: [-b] [-d yyyy-mm-dd]\n",
420 /* print the assumed switches for all countries */
425 int n; /* number of lines to print */
426 int m; /* offset from left to right table entry on the same line */
428 #define FSTR "%c%s %-15s%4d-%02d-%02d"
429 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
430 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
432 n = sizeof(switches) / sizeof(struct djswitch);
435 for (p = switches; p != switches + n; p++)
436 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
438 printf(FSTR"\n", FSTRARG(p));
441 /* print the date of easter sunday */
443 printeaster(int y, int julian, int orthodox)
448 static int d_first = -1;
451 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
452 /* force orthodox easter for years before 1583 */
464 memset(&tm, 0, sizeof(tm));
465 tm.tm_year = dt.y - 1900;
466 tm.tm_mon = dt.m - 1;
468 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm);
472 #define MW(mw, ms, ml) \
473 strlen(ms) > (ml) ? (mw) + 9 : (mw)
474 #define DECREASEMONTH(m, y) \
479 #define INCREASEMONTH(m, y) \
484 #define M2Y(m) ((m) / 12)
485 #define M2M(m) (1 + (m) % 12)
488 monthrangeb(int y, int jd_flag, int m, int before, int after)
490 struct monthlines year[12];
492 char s[MAX_WIDTH], t[MAX_WIDTH];
493 wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH];
502 mpl = jd_flag ? 2 : 3;
503 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
504 wdss = (mpl == 2) ? " " : "";
506 while (before != 0) {
517 * The year header is printed when there are more than 'mpl' months
518 * and if the first month is a multitude of 'mpl'.
519 * If not, it will print the year behind every month.
521 printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
526 for (i = 0; i != mpl && m + i <= m2; i++) {
527 mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
531 /* Empty line between two rows of months */
535 /* Year at the top */
536 if (printyearheader && M2Y(m) != prevyear) {
537 sprintf(s, "%d", M2Y(m));
538 printf("%s\n", center(t, s, mpl * mw));
543 for (i = 0; i < count; i++)
546 mw, wcenter(ws, year[i].name, mw));
548 swprintf(ws, sizeof(ws), L"%-ls %d",
549 year[i].name, M2Y(m + i));
550 wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw));
554 /* Day of the week names */
555 for (i = 0; i < count; i++) {
556 wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
557 wdss, wds.names[6], wdss, wds.names[0],
558 wdss, wds.names[1], wdss, wds.names[2],
559 wdss, wds.names[3], wdss, wds.names[4],
564 for (i = 0; i != 6; i++) {
565 for (j = 0; j < count; j++)
566 printf("%-*s ", mw, year[j].lines[i]+1);
575 monthranger(int y, int jd_flag, int m, int before, int after)
577 struct monthlines year[12];
579 char s[MAX_WIDTH], t[MAX_WIDTH];
587 mpl = jd_flag ? 3 : 4;
588 mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R;
590 while (before != 0) {
601 * The year header is printed when there are more than 'mpl' months
602 * and if the first month is a multitude of 'mpl'.
603 * If not, it will print the year behind every month.
605 printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
610 for (i = 0; i != mpl && m + i <= m2; i++) {
611 mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
615 /* Empty line between two rows of months */
619 /* Year at the top */
620 if (printyearheader && M2Y(m) != prevyear) {
621 sprintf(s, "%d", M2Y(m));
622 printf("%s\n", center(t, s, mpl * mw));
628 for (i = 0; i < count; i++)
630 wprintf(L"%-*ls", mw, year[i].name);
632 wprintf(L"%-ls %-*d", year[i].name,
633 mw - wcslen(year[i].name) - 1, M2Y(m + i));
636 for (i = 0; i != 7; i++) {
638 wprintf(L"%.2ls", wds.names[i]);
641 for (j = 0; j < count; j++)
643 MW(mw, year[j].lines[i],
644 year[j].linelen[i]), year[j].lines[i]);
650 for (i = 0; i < count; i++)
651 printf("%-*s", mw, year[i].weeks);
661 mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines)
664 struct tm tm; /* for strftime printing local names of
666 date dt; /* handy date */
667 int dw; /* width of numbers */
668 int first; /* first day of month */
669 int firstm; /* first day of first week of month */
670 int i, j, k, l; /* just indices */
671 int last; /* the first day of next month */
672 int jan1 = 0; /* the first day of this year */
673 char *ds; /* pointer to day strings (daystr or
676 /* Set name of month. */
677 memset(&tm, 0, sizeof(tm));
679 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
681 mlines->name[0] = towupper(mlines->name[0]);
684 * Set first and last to the day number of the first day of this
685 * month and the first day of next month respectively. Set jan1 to
686 * the day number of the first day of this year.
688 first = firstday(y, m + 1);
690 last = firstday(y + 1, 1);
692 last = firstday(y, m + 2);
695 jan1 = firstday(y, 1);
698 * Set firstm to the day number of monday of the first week of
699 * this month. (This might be in the last month)
701 firstm = first - weekday(first);
703 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
713 * Fill the lines with day of month or day of year (julian day)
714 * line index: i, each line is one weekday. column index: j, each
715 * column is one day number. print column index: k.
717 for (i = 0; i != 7; i++) {
719 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
721 (term_so != NULL && term_se != NULL)) {
728 mlines->lines[i][k] = ' ';
729 /* the actual text */
730 memcpy(mlines->lines[i] + k + l,
733 memcpy(mlines->lines[i] + k + 1, term_so, l);
735 memcpy(mlines->lines[i] + k + l + dw, term_se,
737 l = strlen(term_se) + strlen(term_so);
745 memcpy(mlines->lines[i] + k + l,
748 memcpy(mlines->lines[i] + k + l, " ", dw);
750 mlines->lines[i][k + l] = '\0';
751 mlines->linelen[i] = k;
754 /* fill the weeknumbers */
756 for (j = firstm, k = 0; j < last; k += dw, j += 7)
758 memset(mlines->weeks + k, ' ', dw);
760 memcpy(mlines->weeks + k,
761 ds + week(j, &i)*dw, dw);
762 mlines->weeks[k] = '\0';
767 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
770 struct tm tm; /* for strftime printing local names of
772 date dt; /* handy date */
773 int dw; /* width of numbers */
774 int first; /* first day of month */
775 int firsts; /* sunday of first week of month */
776 int i, j, k, l; /* just indices */
777 int jan1 = 0; /* the first day of this year */
778 int last; /* the first day of next month */
779 char *ds; /* pointer to day strings (daystr or
782 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
791 /* Set name of month centered */
792 memset(&tm, 0, sizeof(tm));
794 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
796 mlines->name[0] = towupper(mlines->name[0]);
799 * Set first and last to the day number of the first day of this
800 * month and the first day of next month respectively. Set jan1 to
801 * the day number of Jan 1st of this year.
806 first = sndaysb(&dt);
826 * Set firsts to the day number of sunday of the first week of
827 * this month. (This might be in the last month)
829 firsts = first - (weekday(first)+1) % 7;
832 * Fill the lines with day of month or day of year (Julian day)
833 * line index: i, each line is one week. column index: j, each
834 * column is one day number. print column index: k.
836 for (i = 0; i != 6; i++) {
838 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
841 (term_so != NULL && term_se != NULL)) {
848 mlines->lines[i][k] = ' ';
849 /* the actual text */
850 memcpy(mlines->lines[i] + k + l,
853 memcpy(mlines->lines[i] + k + 1, term_so, l);
855 memcpy(mlines->lines[i] + k + l + dw, term_se,
857 l = strlen(term_se) + strlen(term_so);
865 memcpy(mlines->lines[i] + k + l,
868 memcpy(mlines->lines[i] + k + l, " ", dw);
871 mlines->lines[i][1] = '\0';
873 mlines->lines[i][k + l] = '\0';
877 /* Put the local names of weekdays into the wds */
879 mkweekdays(struct weekdays *wds)
881 int i, len, width = 0;
885 memset(&tm, 0, sizeof(tm));
887 for (i = 0; i != 7; i++) {
888 tm.tm_wday = (i+1) % 7;
889 wcsftime(buf, sizeof(buf), L"%a", &tm);
890 for (len = 2; len > 0; --len) {
891 if ((width = wcswidth(buf, len)) <= 2)
894 wmemset(wds->names[i], L'\0', 4);
896 wds->names[i][0] = L' ';
897 wcsncat(wds->names[i], buf, len);
898 wcsncat(wds->names[i], L" ", 1);
903 * Compute the day number of the first
904 * existing date after the first day in month.
905 * (the first day in month and even the month might not exist!)
908 firstday(int y, int m)
919 if ((dt.m >= m && dt.y == y) || dt.y > y)
928 * Compute the number of days from date, obey the local switch from
929 * Julian to Gregorian if specified by the user.
932 sndaysr(struct date *d)
936 if (nswitch < ndaysj(d))
945 * Compute the number of days from date, obey the switch from
946 * Julian to Gregorian as used by UK and her colonies.
949 sndaysb(struct date *d)
952 if (nswitchb < ndaysj(d))
958 /* Inverse of sndays */
960 sdater(int nd, struct date *d)
964 return (gdate(nd, d));
966 return (jdate(nd, d));
969 /* Inverse of sndaysb */
971 sdateb(int nd, struct date *d)
975 return (gdate(nd, d));
977 return (jdate(nd, d));
980 /* Center string t in string s of length w by putting enough leading blanks */
982 center(char *s, char *t, int w)
984 char blanks[MAX_WIDTH];
986 memset(blanks, ' ', sizeof(blanks));
987 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
991 /* Center string t in string s of length w by putting enough leading blanks */
993 wcenter(wchar_t *s, wchar_t *t, int w)
995 char blanks[MAX_WIDTH];
997 memset(blanks, ' ', sizeof(blanks));
998 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
1003 parsemonth(const char *s, int *m, int *y)
1009 nm = (int)strtol(s, &cp, 10);
1013 ; /* no special action */
1014 } else if (*cp == 'f' || *cp == 'F') {
1017 } else if (*cp == 'p' || *cp == 'P') {
1022 if (nm < 1 || nm > 12)
1028 if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {