ncal.c (0851fbdf3bce91257140fc3436db47726af0c810) ncal.c (cae11c25e3479ee50b0a2cb9474d5e334c1e26e5)
1/*-
2 * Copyright (c) 1997 Wolfgang Helbig
3 * All rights reserved.
4 *
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

--- 145 unchanged lines hidden (view full) ---

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";
161
1/*-
2 * Copyright (c) 1997 Wolfgang Helbig
3 * All rights reserved.
4 *
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

--- 145 unchanged lines hidden (view full) ---

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";
161
162int flag_nohighlight; /* user doesn't want a highlighted today */
162int flag_weeks; /* user wants number of week */
163int nswitch; /* user defined switch date */
164int nswitchb; /* switch date for backward compatibility */
163int flag_weeks; /* user wants number of week */
164int nswitch; /* user defined switch date */
165int nswitchb; /* switch date for backward compatibility */
165const char *term_so, *term_se;
166int today;
167
168char *center(char *s, char *t, int w);
169wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170void mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
171void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
172void mkweekdays(struct weekdays * wds);
173int parsemonth(const char *s, int *m, int *y);
174void printcc(void);
175void printeaster(int year, int julian, int orthodox);
176int firstday(int y, int m);
177date *sdater(int ndays, struct date * d);
178date *sdateb(int ndays, struct date * d);
179int sndaysr(struct date * d);
180int sndaysb(struct date * d);
181static void usage(void);
182void monthranger(int year, int jd_flag, int m, int before, int after);
183void monthrangeb(int year, int jd_flag, int m, int before, int after);
166int today;
167
168char *center(char *s, char *t, int w);
169wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170void mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
171void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
172void mkweekdays(struct weekdays * wds);
173int parsemonth(const char *s, int *m, int *y);
174void printcc(void);
175void printeaster(int year, int julian, int orthodox);
176int firstday(int y, int m);
177date *sdater(int ndays, struct date * d);
178date *sdateb(int ndays, struct date * d);
179int sndaysr(struct date * d);
180int sndaysb(struct date * d);
181static void usage(void);
182void monthranger(int year, int jd_flag, int m, int before, int after);
183void monthrangeb(int year, int jd_flag, int m, int before, int after);
184void highlight(char *dst, char *src, int len, int *extraletters);
184
185int
186main(int argc, char *argv[])
187{
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 */
191 date dt;

--- 7 unchanged lines hidden (view full) ---

199 * numbers */
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;
205 int before, after;
206 const char *locale; /* locale to get country code */
185
186int
187main(int argc, char *argv[])
188{
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 */
192 date dt;

--- 7 unchanged lines hidden (view full) ---

200 * numbers */
201 int flag_orthodox = 0; /* use wants Orthodox easter */
202 int flag_easter = 0; /* use wants easter date */
203 char *cp; /* character pointer */
204 char *flag_month = NULL; /* requested month as string */
205 char *flag_highlightdate = NULL;
206 int before, after;
207 const char *locale; /* locale to get country code */
207 char tbuf[1024], cbuf[512], *b;
208
208
209 /* On how to highlight on this terminal */
210 term_se = term_so = NULL;
211 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
212 b = cbuf;
213 term_so = tgetstr("so", &b);
214 term_se = tgetstr("se", &b);
215 }
209 flag_nohighlight = 0;
210 flag_weeks = 0;
216
217 /*
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.
221 */
222 if (setlocale(LC_ALL, "") == NULL)
223 warn("setlocale");

--- 54 unchanged lines hidden (view full) ---

278 break;
279 case 'b':
280 flag_backward = 1;
281 break;
282 case 'd':
283 flag_highlightdate = optarg;
284 break;
285 case 'h':
211
212 /*
213 * Use locale to determine the country code,
214 * and use the country code to determine the default
215 * switchdate and date format from the switches table.
216 */
217 if (setlocale(LC_ALL, "") == NULL)
218 warn("setlocale");

--- 54 unchanged lines hidden (view full) ---

273 break;
274 case 'b':
275 flag_backward = 1;
276 break;
277 case 'd':
278 flag_highlightdate = optarg;
279 break;
280 case 'h':
286 term_so = term_se = NULL;
281 flag_nohighlight = 1;
287 break;
288 case 'e':
289 if (flag_backward)
290 usage();
291 flag_easter = 1;
292 break;
293 case 'j':
294 flag_julian_day = 1;

--- 69 unchanged lines hidden (view full) ---

364 if (after == -1)
365 after = 0;
366 }
367 break;
368 default:
369 usage();
370 }
371
282 break;
283 case 'e':
284 if (flag_backward)
285 usage();
286 flag_easter = 1;
287 break;
288 case 'j':
289 flag_julian_day = 1;

--- 69 unchanged lines hidden (view full) ---

359 if (after == -1)
360 after = 0;
361 }
362 break;
363 default:
364 usage();
365 }
366
367 if (flag_hole_year) {
368 m = 1;
369 before = 0;
370 after = 11;
371 }
372
372 if (flag_month != NULL) {
373 if (parsemonth(flag_month, &m, &y)) {
374 errx(EX_USAGE,
375 "%s is neither a month number (1..12) nor a name",
376 flag_month);
377 }
378 }
379

--- 332 unchanged lines hidden (view full) ---

712 /*
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.
716 */
717 for (i = 0; i != 7; i++) {
718 l = 0;
719 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
373 if (flag_month != NULL) {
374 if (parsemonth(flag_month, &m, &y)) {
375 errx(EX_USAGE,
376 "%s is neither a month number (1..12) nor a name",
377 flag_month);
378 }
379 }
380

--- 332 unchanged lines hidden (view full) ---

713 /*
714 * Fill the lines with day of month or day of year (julian day)
715 * line index: i, each line is one weekday. column index: j, each
716 * column is one day number. print column index: k.
717 */
718 for (i = 0; i != 7; i++) {
719 l = 0;
720 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
720 if (j == today &&
721 (term_so != NULL && term_se != NULL)) {
722 l = strlen(term_so);
723 if (jd_flag)
724 dt.d = j - jan1 + 1;
725 else
726 sdateb(j, &dt);
727 /* separator */
728 mlines->lines[i][k] = ' ';
729 /* the actual text */
730 memcpy(mlines->lines[i] + k + l,
731 ds + dt.d * dw, dw);
732 /* highlight on */
733 memcpy(mlines->lines[i] + k + 1, term_so, l);
734 /* highlight off */
735 memcpy(mlines->lines[i] + k + l + dw, term_se,
736 strlen(term_se));
737 l = strlen(term_se) + strlen(term_so);
738 continue;
739 }
740 if (j >= first) {
741 if (jd_flag)
742 dt.d = j - jan1 + 1;
743 else
744 sdater(j, &dt);
721 if (j >= first) {
722 if (jd_flag)
723 dt.d = j - jan1 + 1;
724 else
725 sdater(j, &dt);
745 memcpy(mlines->lines[i] + k + l,
746 ds + dt.d * dw, dw);
726 if (j == today && !flag_nohighlight)
727 highlight(mlines->lines[i] + k,
728 ds + dt.d * dw, dw, &l);
729 else
730 memcpy(mlines->lines[i] + k + l,
731 ds + dt.d * dw, dw);
747 } else
748 memcpy(mlines->lines[i] + k + l, " ", dw);
749 }
750 mlines->lines[i][k + l] = '\0';
751 mlines->linelen[i] = k;
752 }
753
754 /* fill the weeknumbers */

--- 77 unchanged lines hidden (view full) ---

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.
835 */
836 for (i = 0; i != 6; i++) {
837 l = 0;
838 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
839 j++, k += dw) {
732 } else
733 memcpy(mlines->lines[i] + k + l, " ", dw);
734 }
735 mlines->lines[i][k + l] = '\0';
736 mlines->linelen[i] = k;
737 }
738
739 /* fill the weeknumbers */

--- 77 unchanged lines hidden (view full) ---

817 * Fill the lines with day of month or day of year (Julian day)
818 * line index: i, each line is one week. column index: j, each
819 * column is one day number. print column index: k.
820 */
821 for (i = 0; i != 6; i++) {
822 l = 0;
823 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
824 j++, k += dw) {
840 if (j == today &&
841 (term_so != NULL && term_se != NULL)) {
842 l = strlen(term_so);
843 if (jd_flag)
844 dt.d = j - jan1 + 1;
845 else
846 sdateb(j, &dt);
847 /* separator */
848 mlines->lines[i][k] = ' ';
849 /* the actual text */
850 memcpy(mlines->lines[i] + k + l,
851 ds + dt.d * dw, dw);
852 /* highlight on */
853 memcpy(mlines->lines[i] + k + 1, term_so, l);
854 /* highlight off */
855 memcpy(mlines->lines[i] + k + l + dw, term_se,
856 strlen(term_se));
857 l = strlen(term_se) + strlen(term_so);
858 continue;
859 }
860 if (j >= first) {
861 if (jd_flag)
862 dt.d = j - jan1 + 1;
863 else
864 sdateb(j, &dt);
825 if (j >= first) {
826 if (jd_flag)
827 dt.d = j - jan1 + 1;
828 else
829 sdateb(j, &dt);
865 memcpy(mlines->lines[i] + k + l,
866 ds + dt.d * dw, dw);
830 if (j == today && !flag_nohighlight)
831 highlight(mlines->lines[i] + k,
832 ds + dt.d * dw, dw, &l);
833 else
834 memcpy(mlines->lines[i] + k + l,
835 ds + dt.d * dw, dw);
867 } else
868 memcpy(mlines->lines[i] + k + l, " ", dw);
869 }
870 if (k == 0)
871 mlines->lines[i][1] = '\0';
872 else
873 mlines->lines[i][k + l] = '\0';
874 }

--- 151 unchanged lines hidden (view full) ---

1026 return (0);
1027 }
1028 if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
1029 *m = tm.tm_mon + 1;
1030 return (0);
1031 }
1032 return (1);
1033}
836 } else
837 memcpy(mlines->lines[i] + k + l, " ", dw);
838 }
839 if (k == 0)
840 mlines->lines[i][1] = '\0';
841 else
842 mlines->lines[i][k + l] = '\0';
843 }

--- 151 unchanged lines hidden (view full) ---

995 return (0);
996 }
997 if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
998 *m = tm.tm_mon + 1;
999 return (0);
1000 }
1001 return (1);
1002}
1003
1004void
1005highlight(char *dst, char *src, int len, int *extralen)
1006{
1007 static int first = 1;
1008 static const char *term_so, *term_se;
1009
1010 if (first) {
1011 char tbuf[1024], cbuf[512], *b;
1012
1013 term_se = term_so = NULL;
1014
1015 /* On how to highlight on this type of terminal (if any) */
1016 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1017 b = cbuf;
1018 term_so = tgetstr("so", &b);
1019 term_se = tgetstr("se", &b);
1020 }
1021
1022 first = 0;
1023 }
1024
1025 /*
1026 * This check is not necessary, should have been handled before calling
1027 * this function.
1028 */
1029 if (flag_nohighlight) {
1030 memcpy(dst, src, len);
1031 return;
1032 }
1033
1034 /* If it is a real terminal, use the data from the termcap database. */
1035 if (term_so != NULL && term_se != NULL) {
1036 /* separator */
1037 dst[0] = ' ';
1038 dst++;
1039 /* highlight on */
1040 memcpy(dst, term_so, strlen(term_so));
1041 dst += strlen(term_so);
1042 /* the actual text (minus leading space) */
1043 len--;
1044 src++;
1045 memcpy(dst, src, len);
1046 dst += len;
1047 /* highlight off */
1048 memcpy(dst, term_se, strlen(term_se));
1049 *extralen = strlen(term_so) + strlen(term_se);
1050 return;
1051 }
1052
1053 /*
1054 * Otherwise, print a _, backspace and the letter
1055 */
1056 *extralen = 0;
1057 /* skip leading space */
1058 src++;
1059 len--;
1060 /* separator */
1061 dst[0] = ' ';
1062 dst++;
1063 while (len > 0) {
1064 /* _ and backspace */
1065 memcpy(dst, "_\010", 2);
1066 dst += 2;
1067 *extralen += 2;
1068 /* the character */
1069 *dst++ = *src++;
1070 len--;
1071 }
1072}