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 extralen[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_nohighlight; /* user doesn't want a highlighted today */
163 int flag_weeks; /* user wants number of week */
164 int nswitch; /* user defined switch date */
165 int nswitchb; /* switch date for backward compatibility */
168 char *center(char *s, char *t, int w);
169 wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170 int firstday(int y, int m);
171 void highlight(char *dst, char *src, int len, int *extraletters);
172 void mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
173 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
174 void mkweekdays(struct weekdays * wds);
175 void monthranger(int year, int m, int jd_flag, int before, int after);
176 void monthrangeb(int year, int m, int jd_flag, int before, int after);
177 int parsemonth(const char *s, int *m, int *y);
179 void printeaster(int year, int julian, int orthodox);
180 date *sdater(int ndays, struct date * d);
181 date *sdateb(int ndays, struct date * d);
182 int sndaysr(struct date * d);
183 int sndaysb(struct date * d);
184 static void usage(void);
187 main(int argc, char *argv[])
189 struct djswitch *p, *q; /* to search user defined switch date */
190 date never = {10000, 1, 1}; /* outside valid range of dates */
191 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */
193 int ch; /* holds the option character */
194 int m = 0; /* month */
195 int y = 0; /* year */
196 int flag_backward = 0; /* user called cal--backward compat. */
197 int flag_wholeyear = 0; /* user wants the whole year */
198 int flag_julian_cal = 0; /* user wants Julian Calendar */
199 int flag_julian_day = 0; /* user wants the Julian day numbers */
200 int flag_orthodox = 0; /* user wants Orthodox easter */
201 int flag_easter = 0; /* user wants easter date */
202 int flag_3months = 0; /* user wants 3 month display (-3) */
203 int flag_after = 0; /* user wants to see months after */
204 int flag_before = 0; /* user wants to see months before */
205 int flag_specifiedmonth = 0;/* user wants to see this month (-m) */
206 int flag_givenmonth = 0; /* user has specified month [n] */
207 int flag_givenyear = 0; /* user has specified year [n] */
208 char *cp; /* character pointer */
209 char *flag_today = NULL; /* debug: use date as being today */
210 char *flag_month = NULL; /* requested month as string */
211 char *flag_highlightdate = NULL; /* debug: date to highlight */
213 const char *locale; /* locale to get country code */
215 flag_nohighlight = 0;
219 * Use locale to determine the country code,
220 * and use the country code to determine the default
221 * switchdate and date format from the switches table.
223 if (setlocale(LC_ALL, "") == NULL)
225 locale = setlocale(LC_TIME, NULL);
226 if (locale == NULL ||
227 strcmp(locale, "C") == 0 ||
228 strcmp(locale, "POSIX") == 0 ||
229 strcmp(locale, "ASCII") == 0 ||
230 strcmp(locale, "US-ASCII") == 0)
232 q = switches + sizeof(switches) / sizeof(struct djswitch);
233 for (p = switches; p != q; p++)
234 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
237 nswitch = ndaysj(&dftswitch->dt);
239 nswitch = ndaysj(&p->dt);
245 * Get the filename portion of argv[0] and set flag_backward if
246 * this program is called "cal".
248 cp = strrchr(argv[0], '/');
249 cp = (cp == NULL) ? argv[0] : cp + 1;
250 if (strcmp("cal", cp) == 0)
253 /* Set the switch date to United Kingdom if backwards compatible */
255 nswitchb = ndaysj(&ukswitch);
259 while ((ch = getopt(argc, argv, "3A:B:Cd:eH:hjJm:Nops:wy")) != -1)
266 errx(EX_USAGE, "Double -A specified");
267 flag_after = strtol(optarg, NULL, 10);
270 "Argument to -A must be positive");
274 errx(EX_USAGE, "Double -A specified");
275 flag_before = strtol(optarg, NULL, 10);
276 if (flag_before <= 0)
278 "Argument to -B must be positive");
283 nswitch = ndaysj(&never);
296 flag_highlightdate = optarg;
299 flag_nohighlight = 1;
310 if (flag_specifiedmonth)
311 errx(EX_USAGE, "Double -m specified");
313 flag_specifiedmonth = 1;
331 sizeof(switches) / sizeof(struct djswitch);
333 p != q && strcmp(p->cc, optarg) != 0; p++)
337 "%s: invalid country code", optarg);
338 nswitch = ndaysj(&(p->dt));
359 flag_month = *argv++;
361 m = strtol(flag_month, NULL, 10);
365 if (y < 1 || y > 9999)
366 errx(EX_USAGE, "year `%s' not in range 1..9999", *argv);
371 if (flag_today != NULL) {
372 y = strtol(flag_today, NULL, 10);
373 m = strtol(flag_today + 5, NULL, 10);
380 y = tm->tm_year + 1900;
388 if (flag_month != NULL) {
389 if (parsemonth(flag_month, &m, &y)) {
391 "%s is neither a month number (1..12) nor a name",
397 * What is not supported:
399 * -3 displays 3 months, -A and -B change that behaviour.
401 * -3 displays 3 months, -y says display a whole year.
402 * -3 with a given year but no given month or without -m
403 * -3 displays 3 months, no month specified doesn't make clear
404 * which three months.
405 * -m with a given month
406 * conflicting arguments, both specify the same field.
408 * -y displays the whole year, -m displays a single month.
409 * -y with a given month
410 * -y displays the whole year, the given month displays a single
413 * -y displays the whole year, -A and -B display extra months.
416 /* -3 together with -A or -B. */
417 if (flag_3months && (flag_after || flag_before))
418 errx(EX_USAGE, "-3 together with -A and -B is not supported.");
419 /* -3 together with -y. */
420 if (flag_3months && flag_wholeyear)
421 errx(EX_USAGE, "-3 together with -y is not supported.");
422 /* -3 together with givenyear but no givenmonth. */
423 if (flag_3months && flag_givenyear &&
424 !(flag_givenmonth || flag_specifiedmonth))
426 "-3 together with a given year but no given month is "
428 /* -m together with xx xxxx. */
429 if (flag_specifiedmonth && flag_givenmonth)
431 "-m together with a given month is not supported.");
432 /* -y together with -m. */
433 if (flag_wholeyear && flag_specifiedmonth)
434 errx(EX_USAGE, "-y together with -m is not supported.");
435 /* -y together with xx xxxx. */
436 if (flag_wholeyear && flag_givenmonth)
437 errx(EX_USAGE, "-y together a given month is not supported.");
438 /* -y together with -A or -B. */
439 if (flag_wholeyear && (flag_before > 0 || flag_after > 0))
440 errx(EX_USAGE, "-y together a -A or -B is not supported.");
441 /* The rest should be fine. */
443 /* Select the period to display, in order of increasing priority .*/
444 if (flag_wholeyear ||
445 (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) {
450 if (flag_givenyear && flag_givenmonth) {
454 if (flag_specifiedmonth) {
459 before = flag_before;
473 /* Highlight a specified day or today .*/
474 if (flag_highlightdate != NULL) {
475 dt.y = strtol(flag_highlightdate, NULL, 10);
476 dt.m = strtol(flag_highlightdate + 5, NULL, 10);
477 dt.d = strtol(flag_highlightdate + 8, NULL, 10);
484 dt.y = tm1->tm_year + 1900;
485 dt.m = tm1->tm_mon + 1;
488 highlightdate = sndaysb(&dt);
490 /* And now we finally start to calculate and output calendars. */
492 printeaster(y, flag_julian_cal, flag_orthodox);
495 monthrangeb(y, m, flag_julian_day, before, after);
497 monthranger(y, m, flag_julian_day, before, after);
506 "Usage: cal [general options] [-hjy] [[month] year]\n"
507 " cal [general options] [-hj] [-m month] [year]\n"
508 " ncal [general options] [-hJjpwy] [-s country_code] [[month] year]\n"
509 " ncal [general options] [-hJeo] [year]\n"
510 "General options: [-NC3] [-A months] [-B months]\n"
511 "For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]\n",
516 /* Print the assumed switches for all countries. */
521 int n; /* number of lines to print */
522 int m; /* offset from left to right table entry on the same line */
524 #define FSTR "%c%s %-15s%4d-%02d-%02d"
525 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
526 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
528 n = sizeof(switches) / sizeof(struct djswitch);
531 for (p = switches; p != switches + n; p++)
532 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
534 printf(FSTR"\n", FSTRARG(p));
537 /* Print the date of easter sunday. */
539 printeaster(int y, int julian, int orthodox)
544 static int d_first = -1;
547 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
548 /* force orthodox easter for years before 1583 */
560 memset(&tm, 0, sizeof(tm));
561 tm.tm_year = dt.y - 1900;
562 tm.tm_mon = dt.m - 1;
564 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm);
568 #define MW(mw, me) ((mw) + me)
569 #define DECREASEMONTH(m, y) \
574 #define INCREASEMONTH(m, y) \
579 #define M2Y(m) ((m) / 12)
580 #define M2M(m) (1 + (m) % 12)
582 /* Print all months for the period in the range [ before .. y-m .. after ]. */
584 monthrangeb(int y, int m, int jd_flag, int before, int after)
586 struct monthlines year[12];
588 char s[MAX_WIDTH], t[MAX_WIDTH];
589 wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH];
598 mpl = jd_flag ? 2 : 3;
599 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
600 wdss = (mpl == 2) ? " " : "";
602 while (before != 0) {
613 * The year header is printed when there are more than 'mpl' months
614 * and if the first month is a multitude of 'mpl'.
615 * If not, it will print the year behind every month.
617 printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
622 for (i = 0; i != mpl && m + i <= m2; i++) {
623 mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
627 /* Empty line between two rows of months */
631 /* Year at the top. */
632 if (printyearheader && M2Y(m) != prevyear) {
633 sprintf(s, "%d", M2Y(m));
634 printf("%s\n", center(t, s, mpl * mw));
639 for (i = 0; i < count; i++)
642 mw, wcenter(ws, year[i].name, mw));
644 swprintf(ws, sizeof(ws), L"%-ls %d",
645 year[i].name, M2Y(m + i));
646 wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw));
650 /* Day of the week names. */
651 for (i = 0; i < count; i++) {
652 wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
653 wdss, wds.names[6], wdss, wds.names[0],
654 wdss, wds.names[1], wdss, wds.names[2],
655 wdss, wds.names[3], wdss, wds.names[4],
660 /* And the days of the month. */
661 for (i = 0; i != 6; i++) {
662 for (j = 0; j < count; j++)
664 MW(mw, year[j].extralen[i]),
674 monthranger(int y, int m, int jd_flag, int before, int after)
676 struct monthlines year[12];
678 char s[MAX_WIDTH], t[MAX_WIDTH];
686 mpl = jd_flag ? 3 : 4;
687 mw = jd_flag ? MONTH_WIDTH_R_J : MONTH_WIDTH_R;
689 while (before != 0) {
700 * The year header is printed when there are more than 'mpl' months
701 * and if the first month is a multitude of 'mpl'.
702 * If not, it will print the year behind every month.
704 printyearheader = (after >= mpl - 1) && (M2M(m1) - 1) % mpl == 0;
709 for (i = 0; i != mpl && m + i <= m2; i++) {
710 mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
714 /* Empty line between two rows of months. */
718 /* Year at the top. */
719 if (printyearheader && M2Y(m) != prevyear) {
720 sprintf(s, "%d", M2Y(m));
721 printf("%s\n", center(t, s, mpl * mw));
727 for (i = 0; i < count; i++)
729 wprintf(L"%-*ls", mw, year[i].name);
731 wprintf(L"%-ls %-*d", year[i].name,
732 mw - wcslen(year[i].name) - 1, M2Y(m + i));
735 /* And the days of the month. */
736 for (i = 0; i != 7; i++) {
738 wprintf(L"%.2ls", wds.names[i]);
741 for (j = 0; j < count; j++)
743 MW(mw, year[j].extralen[i]),
751 for (i = 0; i < count; i++)
752 printf("%-*s", mw, year[i].weeks);
762 mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines)
765 struct tm tm; /* for strftime printing local names of
767 date dt; /* handy date */
768 int dw; /* width of numbers */
769 int first; /* first day of month */
770 int firstm; /* first day of first week of month */
771 int i, j, k, l; /* just indices */
772 int last; /* the first day of next month */
773 int jan1 = 0; /* the first day of this year */
774 char *ds; /* pointer to day strings (daystr or
777 /* Set name of month. */
778 memset(&tm, 0, sizeof(tm));
780 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
782 mlines->name[0] = towupper(mlines->name[0]);
785 * Set first and last to the day number of the first day of this
786 * month and the first day of next month respectively. Set jan1 to
787 * the day number of the first day of this year.
789 first = firstday(y, m + 1);
791 last = firstday(y + 1, 1);
793 last = firstday(y, m + 2);
796 jan1 = firstday(y, 1);
799 * Set firstm to the day number of monday of the first week of
800 * this month. (This might be in the last month)
802 firstm = first - weekday(first);
804 /* Set ds (daystring) and dw (daywidth) according to the jd_flag. */
814 * Fill the lines with day of month or day of year (julian day)
815 * line index: i, each line is one weekday. column index: j, each
816 * column is one day number. print column index: k.
818 for (i = 0; i != 7; i++) {
820 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
826 if (j == highlightdate && !flag_nohighlight)
827 highlight(mlines->lines[i] + k,
828 ds + dt.d * dw, dw, &l);
830 memcpy(mlines->lines[i] + k + l,
833 memcpy(mlines->lines[i] + k + l, " ", dw);
835 mlines->lines[i][k + l] = '\0';
836 mlines->extralen[i] = l;
839 /* fill the weeknumbers. */
841 for (j = firstm, k = 0; j < last; k += dw, j += 7)
843 memset(mlines->weeks + k, ' ', dw);
845 memcpy(mlines->weeks + k,
846 ds + week(j, &i)*dw, dw);
847 mlines->weeks[k] = '\0';
852 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
855 struct tm tm; /* for strftime printing local names of
857 date dt; /* handy date */
858 int dw; /* width of numbers */
859 int first; /* first day of month */
860 int firsts; /* sunday of first week of month */
861 int i, j, k, l; /* just indices */
862 int jan1 = 0; /* the first day of this year */
863 int last; /* the first day of next month */
864 char *ds; /* pointer to day strings (daystr or
867 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
876 /* Set name of month centered. */
877 memset(&tm, 0, sizeof(tm));
879 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
881 mlines->name[0] = towupper(mlines->name[0]);
884 * Set first and last to the day number of the first day of this
885 * month and the first day of next month respectively. Set jan1 to
886 * the day number of Jan 1st of this year.
891 first = sndaysb(&dt);
911 * Set firsts to the day number of sunday of the first week of
912 * this month. (This might be in the last month)
914 firsts = first - (weekday(first)+1) % 7;
917 * Fill the lines with day of month or day of year (Julian day)
918 * line index: i, each line is one week. column index: j, each
919 * column is one day number. print column index: k.
921 for (i = 0; i != 6; i++) {
923 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
930 if (j == highlightdate && !flag_nohighlight)
931 highlight(mlines->lines[i] + k,
932 ds + dt.d * dw, dw, &l);
934 memcpy(mlines->lines[i] + k + l,
937 memcpy(mlines->lines[i] + k + l, " ", dw);
940 mlines->lines[i][1] = '\0';
942 mlines->lines[i][k + l] = '\0';
943 mlines->extralen[i] = l;
947 /* Put the local names of weekdays into the wds. */
949 mkweekdays(struct weekdays *wds)
951 int i, len, width = 0;
955 memset(&tm, 0, sizeof(tm));
957 for (i = 0; i != 7; i++) {
958 tm.tm_wday = (i+1) % 7;
959 wcsftime(buf, sizeof(buf), L"%a", &tm);
960 for (len = 2; len > 0; --len) {
961 if ((width = wcswidth(buf, len)) <= 2)
964 wmemset(wds->names[i], L'\0', 4);
966 wds->names[i][0] = L' ';
967 wcsncat(wds->names[i], buf, len);
968 wcsncat(wds->names[i], L" ", 1);
973 * Compute the day number of the first existing date after the first day in
974 * month. (the first day in month and even the month might not exist!)
977 firstday(int y, int m)
988 if ((dt.m >= m && dt.y == y) || dt.y > y)
997 * Compute the number of days from date, obey the local switch from
998 * Julian to Gregorian if specified by the user.
1001 sndaysr(struct date *d)
1005 if (nswitch < ndaysj(d))
1014 * Compute the number of days from date, obey the switch from
1015 * Julian to Gregorian as used by UK and her colonies.
1018 sndaysb(struct date *d)
1021 if (nswitchb < ndaysj(d))
1027 /* Inverse of sndays. */
1029 sdater(int nd, struct date *d)
1033 return (gdate(nd, d));
1035 return (jdate(nd, d));
1038 /* Inverse of sndaysb. */
1040 sdateb(int nd, struct date *d)
1044 return (gdate(nd, d));
1046 return (jdate(nd, d));
1049 /* Center string t in string s of length w by putting enough leading blanks. */
1051 center(char *s, char *t, int w)
1053 char blanks[MAX_WIDTH];
1055 memset(blanks, ' ', sizeof(blanks));
1056 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
1060 /* Center string t in string s of length w by putting enough leading blanks. */
1062 wcenter(wchar_t *s, wchar_t *t, int w)
1064 char blanks[MAX_WIDTH];
1066 memset(blanks, ' ', sizeof(blanks));
1067 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
1072 parsemonth(const char *s, int *m, int *y)
1078 nm = (int)strtol(s, &cp, 10);
1082 ; /* no special action */
1083 } else if (*cp == 'f' || *cp == 'F') {
1086 } else if (*cp == 'p' || *cp == 'P') {
1091 if (nm < 1 || nm > 12)
1097 if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
1105 highlight(char *dst, char *src, int len, int *extralen)
1107 static int first = 1;
1108 static const char *term_so, *term_se;
1111 char tbuf[1024], cbuf[512], *b;
1113 term_se = term_so = NULL;
1115 /* On how to highlight on this type of terminal (if any). */
1116 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1118 term_so = tgetstr("so", &b);
1119 term_se = tgetstr("se", &b);
1126 * This check is not necessary, should have been handled before calling
1129 if (flag_nohighlight) {
1130 memcpy(dst, src, len);
1135 * If it is a real terminal, use the data from the termcap database.
1137 if (term_so != NULL && term_se != NULL) {
1142 memcpy(dst, term_so, strlen(term_so));
1143 dst += strlen(term_so);
1144 /* the actual text. (minus leading space) */
1147 memcpy(dst, src, len);
1149 /* highlight off. */
1150 memcpy(dst, term_se, strlen(term_se));
1151 *extralen = strlen(term_so) + strlen(term_se);
1156 * Otherwise, print a _, backspace and the letter.
1159 /* skip leading space. */
1166 /* _ and backspace. */
1167 memcpy(dst, "_\010", 2);
1170 /* the character. */