ncal.c (1d0e1dac57c1874f21c85793acf1925c9bdca13d) ncal.c (4af997a87fe5082e60a1a65537a3fd51cb845efa)
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

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

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 */
163int flag_weeks; /* user wants number of week */
164int nswitch; /* user defined switch date */
165int nswitchb; /* switch date for backward compatibility */
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

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

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 */
163int flag_weeks; /* user wants number of week */
164int nswitch; /* user defined switch date */
165int nswitchb; /* switch date for backward compatibility */
166int today;
166int highlightdate;
167
168char *center(char *s, char *t, int w);
169wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
167
168char *center(char *s, char *t, int w);
169wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
170int firstday(int y, int m);
171void highlight(char *dst, char *src, int len, int *extraletters);
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);
172void mkmonthr(int year, int month, int jd_flag, struct monthlines * monthl);
173void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
174void mkweekdays(struct weekdays * wds);
175void monthranger(int year, int m, int jd_flag, int before, int after);
176void monthrangeb(int year, int m, int jd_flag, int before, int after);
173int parsemonth(const char *s, int *m, int *y);
174void printcc(void);
175void printeaster(int year, int julian, int orthodox);
177int parsemonth(const char *s, int *m, int *y);
178void printcc(void);
179void 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);
180date *sdater(int ndays, struct date * d);
181date *sdateb(int ndays, struct date * d);
182int sndaysr(struct date * d);
183int sndaysb(struct date * d);
184static 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);
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;
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. */
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;
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_hole_year = 0; /* user wants the whole year */
197 int flag_wholeyear = 0; /* user wants the whole year */
198 int flag_julian_cal = 0; /* user wants Julian Calendar */
198 int flag_julian_cal = 0; /* user wants Julian Calendar */
199 int flag_julian_day = 0; /* user wants the Julian day
200 * numbers */
201 int flag_orthodox = 0; /* use wants Orthodox easter */
202 int flag_easter = 0; /* use wants easter date */
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] */
203 char *cp; /* character pointer */
208 char *cp; /* character pointer */
209 char *flag_today = NULL; /* debug: use date as being today */
204 char *flag_month = NULL; /* requested month as string */
210 char *flag_month = NULL; /* requested month as string */
205 char *flag_highlightdate = NULL;
211 char *flag_highlightdate = NULL; /* debug: date to highlight */
206 int before, after;
207 const char *locale; /* locale to get country code */
208
209 flag_nohighlight = 0;
210 flag_weeks = 0;
211
212 /*
213 * Use locale to determine the country code,

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

245 flag_backward = 1;
246
247 /* Set the switch date to United Kingdom if backwards compatible */
248 if (flag_backward)
249 nswitchb = ndaysj(&ukswitch);
250
251 before = after = -1;
252
212 int before, after;
213 const char *locale; /* locale to get country code */
214
215 flag_nohighlight = 0;
216 flag_weeks = 0;
217
218 /*
219 * Use locale to determine the country code,

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

251 flag_backward = 1;
252
253 /* Set the switch date to United Kingdom if backwards compatible */
254 if (flag_backward)
255 nswitchb = ndaysj(&ukswitch);
256
257 before = after = -1;
258
253 while ((ch = getopt(argc, argv, "A:B:3Jbd:ehjm:ops:wy")) != -1)
259 while ((ch = getopt(argc, argv, "A:B:3Jbd:eH:hjm:ops:wy")) != -1)
254 switch (ch) {
255 case '3':
260 switch (ch) {
261 case '3':
256 before = after = 1;
262 flag_3months = 1;
257 break;
258 case 'A':
263 break;
264 case 'A':
259 after = strtol(optarg, NULL, 10);
260 if (after < 0)
261 errx(1, "Argument to -A must be positive");
265 if (flag_after > 0)
266 errx(EX_USAGE, "Double -A specified");
267 flag_after = strtol(optarg, NULL, 10);
268 if (flag_after <= 0)
269 errx(EX_USAGE,
270 "Argument to -A must be positive");
262 break;
263 case 'B':
271 break;
272 case 'B':
264 before = strtol(optarg, NULL, 10);
265 if (before < 0)
266 errx(1, "Argument to -B must be positive");
273 if (flag_before > 0)
274 errx(EX_USAGE, "Double -A specified");
275 flag_before = strtol(optarg, NULL, 10);
276 if (flag_before <= 0)
277 errx(EX_USAGE,
278 "Argument to -B must be positive");
267 break;
268 case 'J':
269 if (flag_backward)
270 usage();
271 nswitch = ndaysj(&never);
272 flag_julian_cal = 1;
273 break;
274 case 'b':
275 flag_backward = 1;
276 break;
277 case 'd':
279 break;
280 case 'J':
281 if (flag_backward)
282 usage();
283 nswitch = ndaysj(&never);
284 flag_julian_cal = 1;
285 break;
286 case 'b':
287 flag_backward = 1;
288 break;
289 case 'd':
290 flag_today = optarg;
291 break;
292 case 'H':
278 flag_highlightdate = optarg;
279 break;
280 case 'h':
281 flag_nohighlight = 1;
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;
290 break;
291 case 'm':
293 flag_highlightdate = optarg;
294 break;
295 case 'h':
296 flag_nohighlight = 1;
297 break;
298 case 'e':
299 if (flag_backward)
300 usage();
301 flag_easter = 1;
302 break;
303 case 'j':
304 flag_julian_day = 1;
305 break;
306 case 'm':
307 if (flag_specifiedmonth)
308 errx(EX_USAGE, "Double -m specified");
292 flag_month = optarg;
309 flag_month = optarg;
293 before = 0;
294 after = 0;
310 flag_specifiedmonth = 1;
295 break;
296 case 'o':
297 if (flag_backward)
298 usage();
299 flag_orthodox = 1;
300 flag_easter = 1;
301 break;
302 case 'p':

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

319 nswitch = ndaysj(&(p->dt));
320 break;
321 case 'w':
322 if (flag_backward)
323 usage();
324 flag_weeks = 1;
325 break;
326 case 'y':
311 break;
312 case 'o':
313 if (flag_backward)
314 usage();
315 flag_orthodox = 1;
316 flag_easter = 1;
317 break;
318 case 'p':

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

335 nswitch = ndaysj(&(p->dt));
336 break;
337 case 'w':
338 if (flag_backward)
339 usage();
340 flag_weeks = 1;
341 break;
342 case 'y':
327 flag_hole_year = 1;
343 flag_wholeyear = 1;
328 break;
329 default:
330 usage();
331 }
332
333 argc -= optind;
334 argv += optind;
335
336 switch (argc) {
337 case 2:
338 if (flag_easter)
339 usage();
340 flag_month = *argv++;
344 break;
345 default:
346 usage();
347 }
348
349 argc -= optind;
350 argv += optind;
351
352 switch (argc) {
353 case 2:
354 if (flag_easter)
355 usage();
356 flag_month = *argv++;
341 before = 0;
342 after = 0;
357 flag_givenmonth = 1;
343 m = strtol(flag_month, NULL, 10);
344 /* FALLTHROUGH */
345 case 1:
358 m = strtol(flag_month, NULL, 10);
359 /* FALLTHROUGH */
360 case 1:
346 y = atoi(*argv++);
361 y = atoi(*argv);
347 if (y < 1 || y > 9999)
362 if (y < 1 || y > 9999)
348 errx(EX_USAGE, "year %d not in range 1..9999", y);
349 if (before == -1 && after == -1) {
350 before = 0;
351 after = 11;
352 m = 1;
353 }
363 errx(EX_USAGE, "year `%s' not in range 1..9999", *argv);
364 argv++;
365 flag_givenyear = 1;
354 break;
355 case 0:
366 break;
367 case 0:
356 {
368 if (flag_today != NULL) {
369 y = strtol(flag_today, NULL, 10);
370 m = strtol(flag_today + 5, NULL, 10);
371 } else {
357 time_t t;
358 struct tm *tm;
359
360 t = time(NULL);
361 tm = localtime(&t);
362 y = tm->tm_year + 1900;
363 m = tm->tm_mon + 1;
372 time_t t;
373 struct tm *tm;
374
375 t = time(NULL);
376 tm = localtime(&t);
377 y = tm->tm_year + 1900;
378 m = tm->tm_mon + 1;
364 if (before == -1)
365 before = 0;
366 if (after == -1)
367 after = 0;
368 }
369 break;
370 default:
371 usage();
372 }
373
379 }
380 break;
381 default:
382 usage();
383 }
384
374 if (flag_hole_year) {
375 m = 1;
376 before = 0;
377 after = 11;
378 }
379
380 if (flag_month != NULL) {
381 if (parsemonth(flag_month, &m, &y)) {
382 errx(EX_USAGE,
383 "%s is neither a month number (1..12) nor a name",
384 flag_month);
385 }
386 }
387
385 if (flag_month != NULL) {
386 if (parsemonth(flag_month, &m, &y)) {
387 errx(EX_USAGE,
388 "%s is neither a month number (1..12) nor a name",
389 flag_month);
390 }
391 }
392
393 /*
394 * What is not supported:
395 * -3 with -A or -B
396 * -3 displays 3 months, -A and -B change that behaviour.
397 * -3 with -y
398 * -3 displays 3 months, -y says display a whole year.
399 * -3 with a given year but no given month or without -m
400 * -3 displays 3 months, no month specified doesn't make clear
401 * which three months.
402 * -m with a given month
403 * conflicting arguments, both specify the same field.
404 * -y with -m
405 * -y displays the whole year, -m displays a single month.
406 * -y with a given month
407 * -y displays the whole year, the given month displays a single
408 * month.
409 * -y with -A or -B
410 * -y displays the whole year, -A and -B display extra months.
411 */
412
413 /* -3 together with -A or -B. */
414 if (flag_3months && (flag_after || flag_before))
415 errx(EX_USAGE, "-3 together with -A and -B is not supported.");
416 /* -3 together with -y. */
417 if (flag_3months && flag_wholeyear)
418 errx(EX_USAGE, "-3 together with -y is not supported.");
419 /* -3 together with givenyear but no givenmonth. */
420 if (flag_3months && flag_givenyear &&
421 !(flag_givenmonth || flag_specifiedmonth))
422 errx(EX_USAGE,
423 "-3 together with a given year but no given month is "
424 "not supported.");
425 /* -m together with xx xxxx. */
426 if (flag_specifiedmonth && flag_givenmonth)
427 errx(EX_USAGE,
428 "-m together with a given month is not supported.");
429 /* -y together with -m. */
430 if (flag_wholeyear && flag_specifiedmonth)
431 errx(EX_USAGE, "-y together with -m is not supported.");
432 /* -y together with xx xxxx. */
433 if (flag_wholeyear && flag_givenmonth)
434 errx(EX_USAGE, "-y together a given month is not supported.");
435 /* -y together with -A or -B. */
436 if (flag_wholeyear && (flag_before > 0 || flag_after > 0))
437 errx(EX_USAGE, "-y together a -A or -B is not supported.");
438 /* The rest should be fine. */
439
440 /* Select the period to display, in order of increasing priority .*/
441 if (flag_wholeyear ||
442 (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) {
443 m = 1;
444 before = 0;
445 after = 11;
446 }
447 if (flag_givenyear && flag_givenmonth) {
448 before = 0;
449 after = 0;
450 }
451 if (flag_specifiedmonth) {
452 before = 0;
453 after = 0;
454 }
455 if (flag_before) {
456 before = flag_before;
457 }
458 if (flag_after) {
459 after = flag_after;
460 }
461 if (flag_3months) {
462 before = 1;
463 after = 1;
464 }
465 if (after == -1)
466 after = 0;
467 if (before == -1)
468 before = 0;
469
470 /* Highlight a specified day or today .*/
388 if (flag_highlightdate != NULL) {
389 dt.y = strtol(flag_highlightdate, NULL, 10);
390 dt.m = strtol(flag_highlightdate + 5, NULL, 10);
391 dt.d = strtol(flag_highlightdate + 8, NULL, 10);
392 } else {
393 time_t t;
394 struct tm *tm1;
395
396 t = time(NULL);
397 tm1 = localtime(&t);
398 dt.y = tm1->tm_year + 1900;
399 dt.m = tm1->tm_mon + 1;
400 dt.d = tm1->tm_mday;
401 }
471 if (flag_highlightdate != NULL) {
472 dt.y = strtol(flag_highlightdate, NULL, 10);
473 dt.m = strtol(flag_highlightdate + 5, NULL, 10);
474 dt.d = strtol(flag_highlightdate + 8, NULL, 10);
475 } else {
476 time_t t;
477 struct tm *tm1;
478
479 t = time(NULL);
480 tm1 = localtime(&t);
481 dt.y = tm1->tm_year + 1900;
482 dt.m = tm1->tm_mon + 1;
483 dt.d = tm1->tm_mday;
484 }
402 today = sndaysb(&dt);
485 highlightdate = sndaysb(&dt);
403
486
487 /* And now we finally start to calculate and output calendars. */
404 if (flag_easter)
405 printeaster(y, flag_julian_cal, flag_orthodox);
406 else
407 if (flag_backward)
488 if (flag_easter)
489 printeaster(y, flag_julian_cal, flag_orthodox);
490 else
491 if (flag_backward)
408 monthrangeb(y, flag_julian_day, m, before, after);
492 monthrangeb(y, m, flag_julian_day, before, after);
409 else
493 else
410 monthranger(y, flag_julian_day, m, before, after);
494 monthranger(y, m, flag_julian_day, before, after);
411 return (0);
412}
413
414static void
415usage(void)
416{
417
418 fputs(
419 "usage: cal [-hjy] [[month] year]\n"
420 " cal [-hj] [-m month] [year]\n"
421 " ncal [-hJjpwy] [-s country_code] [[month] year]\n"
422 " ncal [-hJeo] [year]\n"
495 return (0);
496}
497
498static void
499usage(void)
500{
501
502 fputs(
503 "usage: cal [-hjy] [[month] year]\n"
504 " cal [-hj] [-m month] [year]\n"
505 " ncal [-hJjpwy] [-s country_code] [[month] year]\n"
506 " ncal [-hJeo] [year]\n"
423 "for debug the highlighting: [-b] [-d yyyy-mm-dd]\n",
507 "for debug the highlighting: [-b] [-H yyyy-mm-dd] [-d yyyy-mm]\n",
424 stderr);
425 exit(EX_USAGE);
426}
427
508 stderr);
509 exit(EX_USAGE);
510}
511
428/* print the assumed switches for all countries */
512/* Print the assumed switches for all countries. */
429void
430printcc(void)
431{
432 struct djswitch *p;
433 int n; /* number of lines to print */
434 int m; /* offset from left to right table entry on the same line */
435
436#define FSTR "%c%s %-15s%4d-%02d-%02d"

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

441 m = (n + 1) / 2;
442 n /= 2;
443 for (p = switches; p != switches + n; p++)
444 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
445 if (m != n)
446 printf(FSTR"\n", FSTRARG(p));
447}
448
513void
514printcc(void)
515{
516 struct djswitch *p;
517 int n; /* number of lines to print */
518 int m; /* offset from left to right table entry on the same line */
519
520#define FSTR "%c%s %-15s%4d-%02d-%02d"

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

525 m = (n + 1) / 2;
526 n /= 2;
527 for (p = switches; p != switches + n; p++)
528 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
529 if (m != n)
530 printf(FSTR"\n", FSTRARG(p));
531}
532
449/* print the date of easter sunday */
533/* Print the date of easter sunday. */
450void
451printeaster(int y, int julian, int orthodox)
452{
453 date dt;
454 struct tm tm;
455 char buf[MAX_WIDTH];
456 static int d_first = -1;
457

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

486#define INCREASEMONTH(m, y) \
487 if (++(m) == 13) { \
488 (m) = 1; \
489 (y)++; \
490 }
491#define M2Y(m) ((m) / 12)
492#define M2M(m) (1 + (m) % 12)
493
534void
535printeaster(int y, int julian, int orthodox)
536{
537 date dt;
538 struct tm tm;
539 char buf[MAX_WIDTH];
540 static int d_first = -1;
541

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

570#define INCREASEMONTH(m, y) \
571 if (++(m) == 13) { \
572 (m) = 1; \
573 (y)++; \
574 }
575#define M2Y(m) ((m) / 12)
576#define M2M(m) (1 + (m) % 12)
577
578/* Print all months for the period in the range [ before .. y-m .. after ]. */
494void
579void
495monthrangeb(int y, int jd_flag, int m, int before, int after)
580monthrangeb(int y, int m, int jd_flag, int before, int after)
496{
497 struct monthlines year[12];
498 struct weekdays wds;
499 char s[MAX_WIDTH], t[MAX_WIDTH];
500 wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH];
501 const char *wdss;
502 int i, j;
503 int mpl;

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

534 mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
535 count++;
536 }
537
538 /* Empty line between two rows of months */
539 if (m != m1)
540 printf("\n");
541
581{
582 struct monthlines year[12];
583 struct weekdays wds;
584 char s[MAX_WIDTH], t[MAX_WIDTH];
585 wchar_t ws[MAX_WIDTH], ws1[MAX_WIDTH];
586 const char *wdss;
587 int i, j;
588 int mpl;

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

619 mkmonthb(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
620 count++;
621 }
622
623 /* Empty line between two rows of months */
624 if (m != m1)
625 printf("\n");
626
542 /* Year at the top */
627 /* Year at the top. */
543 if (printyearheader && M2Y(m) != prevyear) {
544 sprintf(s, "%d", M2Y(m));
545 printf("%s\n", center(t, s, mpl * mw));
546 prevyear = M2Y(m);
547 }
548
628 if (printyearheader && M2Y(m) != prevyear) {
629 sprintf(s, "%d", M2Y(m));
630 printf("%s\n", center(t, s, mpl * mw));
631 prevyear = M2Y(m);
632 }
633
549 /* Month names */
634 /* Month names. */
550 for (i = 0; i < count; i++)
551 if (printyearheader)
552 wprintf(L"%-*ls ",
553 mw, wcenter(ws, year[i].name, mw));
554 else {
555 swprintf(ws, sizeof(ws), L"%-ls %d",
556 year[i].name, M2Y(m + i));
557 wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw));
558 }
559 printf("\n");
560
635 for (i = 0; i < count; i++)
636 if (printyearheader)
637 wprintf(L"%-*ls ",
638 mw, wcenter(ws, year[i].name, mw));
639 else {
640 swprintf(ws, sizeof(ws), L"%-ls %d",
641 year[i].name, M2Y(m + i));
642 wprintf(L"%-*ls ", mw, wcenter(ws1, ws, mw));
643 }
644 printf("\n");
645
561 /* Day of the week names */
646 /* Day of the week names. */
562 for (i = 0; i < count; i++) {
563 wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
564 wdss, wds.names[6], wdss, wds.names[0],
565 wdss, wds.names[1], wdss, wds.names[2],
566 wdss, wds.names[3], wdss, wds.names[4],
567 wdss, wds.names[5]);
568 }
569 printf("\n");
570
647 for (i = 0; i < count; i++) {
648 wprintf(L"%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls%s%ls ",
649 wdss, wds.names[6], wdss, wds.names[0],
650 wdss, wds.names[1], wdss, wds.names[2],
651 wdss, wds.names[3], wdss, wds.names[4],
652 wdss, wds.names[5]);
653 }
654 printf("\n");
655
656 /* And the days of the month. */
571 for (i = 0; i != 6; i++) {
572 for (j = 0; j < count; j++)
573 printf("%-*s ",
574 MW(mw, year[j].extralen[i]),
575 year[j].lines[i]+1);
576 printf("\n");
577 }
578
579 m += mpl;
580 }
581}
582
583void
657 for (i = 0; i != 6; i++) {
658 for (j = 0; j < count; j++)
659 printf("%-*s ",
660 MW(mw, year[j].extralen[i]),
661 year[j].lines[i]+1);
662 printf("\n");
663 }
664
665 m += mpl;
666 }
667}
668
669void
584monthranger(int y, int jd_flag, int m, int before, int after)
670monthranger(int y, int m, int jd_flag, int before, int after)
585{
586 struct monthlines year[12];
587 struct weekdays wds;
588 char s[MAX_WIDTH], t[MAX_WIDTH];
589 int i, j;
590 int mpl;
591 int mw;
592 int m1, m2;

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

616 m = m1;
617 while (m <= m2) {
618 int count = 0;
619 for (i = 0; i != mpl && m + i <= m2; i++) {
620 mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
621 count++;
622 }
623
671{
672 struct monthlines year[12];
673 struct weekdays wds;
674 char s[MAX_WIDTH], t[MAX_WIDTH];
675 int i, j;
676 int mpl;
677 int mw;
678 int m1, m2;

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

702 m = m1;
703 while (m <= m2) {
704 int count = 0;
705 for (i = 0; i != mpl && m + i <= m2; i++) {
706 mkmonthr(M2Y(m + i), M2M(m + i) - 1, jd_flag, year + i);
707 count++;
708 }
709
624 /* Empty line between two rows of months */
710 /* Empty line between two rows of months. */
625 if (m != m1)
626 printf("\n");
627
711 if (m != m1)
712 printf("\n");
713
628 /* Year at the top */
714 /* Year at the top. */
629 if (printyearheader && M2Y(m) != prevyear) {
630 sprintf(s, "%d", M2Y(m));
631 printf("%s\n", center(t, s, mpl * mw));
632 prevyear = M2Y(m);
633 }
634
715 if (printyearheader && M2Y(m) != prevyear) {
716 sprintf(s, "%d", M2Y(m));
717 printf("%s\n", center(t, s, mpl * mw));
718 prevyear = M2Y(m);
719 }
720
635 /* Month names */
721 /* Month names. */
636 wprintf(L" ");
637 for (i = 0; i < count; i++)
638 if (printyearheader)
639 wprintf(L"%-*ls", mw, year[i].name);
640 else
641 wprintf(L"%-ls %-*d", year[i].name,
642 mw - wcslen(year[i].name) - 1, M2Y(m + i));
643 printf("\n");
644
722 wprintf(L" ");
723 for (i = 0; i < count; i++)
724 if (printyearheader)
725 wprintf(L"%-*ls", mw, year[i].name);
726 else
727 wprintf(L"%-ls %-*d", year[i].name,
728 mw - wcslen(year[i].name) - 1, M2Y(m + i));
729 printf("\n");
730
731 /* And the days of the month. */
645 for (i = 0; i != 7; i++) {
646 /* Week day */
647 wprintf(L"%.2ls", wds.names[i]);
648
649 /* Full months */
650 for (j = 0; j < count; j++)
651 printf("%-*s",
652 MW(mw, year[j].extralen[i]),
653 year[j].lines[i]);
654 printf("\n");
655 }
656
732 for (i = 0; i != 7; i++) {
733 /* Week day */
734 wprintf(L"%.2ls", wds.names[i]);
735
736 /* Full months */
737 for (j = 0; j < count; j++)
738 printf("%-*s",
739 MW(mw, year[j].extralen[i]),
740 year[j].lines[i]);
741 printf("\n");
742 }
743
744 /* Week numbers. */
657 if (flag_weeks) {
658 printf(" ");
659 for (i = 0; i < count; i++)
660 printf("%-*s", mw, year[i].weeks);
661 printf("\n");
662 }
663
664 m += mpl;

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

704 jan1 = firstday(y, 1);
705
706 /*
707 * Set firstm to the day number of monday of the first week of
708 * this month. (This might be in the last month)
709 */
710 firstm = first - weekday(first);
711
745 if (flag_weeks) {
746 printf(" ");
747 for (i = 0; i < count; i++)
748 printf("%-*s", mw, year[i].weeks);
749 printf("\n");
750 }
751
752 m += mpl;

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

792 jan1 = firstday(y, 1);
793
794 /*
795 * Set firstm to the day number of monday of the first week of
796 * this month. (This might be in the last month)
797 */
798 firstm = first - weekday(first);
799
712 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
800 /* Set ds (daystring) and dw (daywidth) according to the jd_flag. */
713 if (jd_flag) {
714 ds = jdaystr;
715 dw = 4;
716 } else {
717 ds = daystr;
718 dw = 3;
719 }
720

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

726 for (i = 0; i != 7; i++) {
727 l = 0;
728 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
729 if (j >= first) {
730 if (jd_flag)
731 dt.d = j - jan1 + 1;
732 else
733 sdater(j, &dt);
801 if (jd_flag) {
802 ds = jdaystr;
803 dw = 4;
804 } else {
805 ds = daystr;
806 dw = 3;
807 }
808

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

814 for (i = 0; i != 7; i++) {
815 l = 0;
816 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
817 if (j >= first) {
818 if (jd_flag)
819 dt.d = j - jan1 + 1;
820 else
821 sdater(j, &dt);
734 if (j == today && !flag_nohighlight)
822 if (j == highlightdate && !flag_nohighlight)
735 highlight(mlines->lines[i] + k,
736 ds + dt.d * dw, dw, &l);
737 else
738 memcpy(mlines->lines[i] + k + l,
739 ds + dt.d * dw, dw);
740 } else
741 memcpy(mlines->lines[i] + k + l, " ", dw);
742 }
743 mlines->lines[i][k + l] = '\0';
744 mlines->extralen[i] = l;
745 }
746
823 highlight(mlines->lines[i] + k,
824 ds + dt.d * dw, dw, &l);
825 else
826 memcpy(mlines->lines[i] + k + l,
827 ds + dt.d * dw, dw);
828 } else
829 memcpy(mlines->lines[i] + k + l, " ", dw);
830 }
831 mlines->lines[i][k + l] = '\0';
832 mlines->extralen[i] = l;
833 }
834
747 /* fill the weeknumbers */
835 /* fill the weeknumbers. */
748 if (flag_weeks) {
749 for (j = firstm, k = 0; j < last; k += dw, j += 7)
750 if (j <= nswitch)
751 memset(mlines->weeks + k, ' ', dw);
752 else
753 memcpy(mlines->weeks + k,
754 ds + week(j, &i)*dw, dw);
755 mlines->weeks[k] = '\0';

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

776 if (jd_flag) {
777 ds = jdaystr;
778 dw = 4;
779 } else {
780 ds = daystr;
781 dw = 3;
782 }
783
836 if (flag_weeks) {
837 for (j = firstm, k = 0; j < last; k += dw, j += 7)
838 if (j <= nswitch)
839 memset(mlines->weeks + k, ' ', dw);
840 else
841 memcpy(mlines->weeks + k,
842 ds + week(j, &i)*dw, dw);
843 mlines->weeks[k] = '\0';

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

864 if (jd_flag) {
865 ds = jdaystr;
866 dw = 4;
867 } else {
868 ds = daystr;
869 dw = 3;
870 }
871
784 /* Set name of month centered */
872 /* Set name of month centered. */
785 memset(&tm, 0, sizeof(tm));
786 tm.tm_mon = m;
787 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
788 L"%OB", &tm);
789 mlines->name[0] = towupper(mlines->name[0]);
790
791 /*
792 * Set first and last to the day number of the first day of this

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

830 l = 0;
831 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
832 j++, k += dw) {
833 if (j >= first) {
834 if (jd_flag)
835 dt.d = j - jan1 + 1;
836 else
837 sdateb(j, &dt);
873 memset(&tm, 0, sizeof(tm));
874 tm.tm_mon = m;
875 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
876 L"%OB", &tm);
877 mlines->name[0] = towupper(mlines->name[0]);
878
879 /*
880 * Set first and last to the day number of the first day of this

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

918 l = 0;
919 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
920 j++, k += dw) {
921 if (j >= first) {
922 if (jd_flag)
923 dt.d = j - jan1 + 1;
924 else
925 sdateb(j, &dt);
838 if (j == today && !flag_nohighlight)
926 if (j == highlightdate && !flag_nohighlight)
839 highlight(mlines->lines[i] + k,
840 ds + dt.d * dw, dw, &l);
841 else
842 memcpy(mlines->lines[i] + k + l,
843 ds + dt.d * dw, dw);
844 } else
845 memcpy(mlines->lines[i] + k + l, " ", dw);
846 }
847 if (k == 0)
848 mlines->lines[i][1] = '\0';
849 else
850 mlines->lines[i][k + l] = '\0';
851 mlines->extralen[i] = l;
852 }
853}
854
927 highlight(mlines->lines[i] + k,
928 ds + dt.d * dw, dw, &l);
929 else
930 memcpy(mlines->lines[i] + k + l,
931 ds + dt.d * dw, dw);
932 } else
933 memcpy(mlines->lines[i] + k + l, " ", dw);
934 }
935 if (k == 0)
936 mlines->lines[i][1] = '\0';
937 else
938 mlines->lines[i][k + l] = '\0';
939 mlines->extralen[i] = l;
940 }
941}
942
855/* Put the local names of weekdays into the wds */
943/* Put the local names of weekdays into the wds. */
856void
857mkweekdays(struct weekdays *wds)
858{
859 int i, len, width = 0;
860 struct tm tm;
861 wchar_t buf[20];
862
863 memset(&tm, 0, sizeof(tm));

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

873 if (width == 1)
874 wds->names[i][0] = L' ';
875 wcsncat(wds->names[i], buf, len);
876 wcsncat(wds->names[i], L" ", 1);
877 }
878}
879
880/*
944void
945mkweekdays(struct weekdays *wds)
946{
947 int i, len, width = 0;
948 struct tm tm;
949 wchar_t buf[20];
950
951 memset(&tm, 0, sizeof(tm));

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

961 if (width == 1)
962 wds->names[i][0] = L' ';
963 wcsncat(wds->names[i], buf, len);
964 wcsncat(wds->names[i], L" ", 1);
965 }
966}
967
968/*
881 * Compute the day number of the first
882 * existing date after the first day in month.
883 * (the first day in month and even the month might not exist!)
969 * Compute the day number of the first existing date after the first day in
970 * month. (the first day in month and even the month might not exist!)
884 */
885int
886firstday(int y, int m)
887{
888 date dt;
889 int nd;
890
891 dt.y = y;

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

928{
929
930 if (nswitchb < ndaysj(d))
931 return (ndaysg(d));
932 else
933 return (ndaysj(d));
934}
935
971 */
972int
973firstday(int y, int m)
974{
975 date dt;
976 int nd;
977
978 dt.y = y;

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

1015{
1016
1017 if (nswitchb < ndaysj(d))
1018 return (ndaysg(d));
1019 else
1020 return (ndaysj(d));
1021}
1022
936/* Inverse of sndays */
1023/* Inverse of sndays. */
937struct date *
938sdater(int nd, struct date *d)
939{
940
941 if (nswitch < nd)
942 return (gdate(nd, d));
943 else
944 return (jdate(nd, d));
945}
946
1024struct date *
1025sdater(int nd, struct date *d)
1026{
1027
1028 if (nswitch < nd)
1029 return (gdate(nd, d));
1030 else
1031 return (jdate(nd, d));
1032}
1033
947/* Inverse of sndaysb */
1034/* Inverse of sndaysb. */
948struct date *
949sdateb(int nd, struct date *d)
950{
951
952 if (nswitchb < nd)
953 return (gdate(nd, d));
954 else
955 return (jdate(nd, d));
956}
957
1035struct date *
1036sdateb(int nd, struct date *d)
1037{
1038
1039 if (nswitchb < nd)
1040 return (gdate(nd, d));
1041 else
1042 return (jdate(nd, d));
1043}
1044
958/* Center string t in string s of length w by putting enough leading blanks */
1045/* Center string t in string s of length w by putting enough leading blanks. */
959char *
960center(char *s, char *t, int w)
961{
962 char blanks[MAX_WIDTH];
963
964 memset(blanks, ' ', sizeof(blanks));
965 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
966 return (s);
967}
968
1046char *
1047center(char *s, char *t, int w)
1048{
1049 char blanks[MAX_WIDTH];
1050
1051 memset(blanks, ' ', sizeof(blanks));
1052 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
1053 return (s);
1054}
1055
969/* Center string t in string s of length w by putting enough leading blanks */
1056/* Center string t in string s of length w by putting enough leading blanks. */
970wchar_t *
971wcenter(wchar_t *s, wchar_t *t, int w)
972{
973 char blanks[MAX_WIDTH];
974
975 memset(blanks, ' ', sizeof(blanks));
976 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
977 return (s);

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

1016 static int first = 1;
1017 static const char *term_so, *term_se;
1018
1019 if (first) {
1020 char tbuf[1024], cbuf[512], *b;
1021
1022 term_se = term_so = NULL;
1023
1057wchar_t *
1058wcenter(wchar_t *s, wchar_t *t, int w)
1059{
1060 char blanks[MAX_WIDTH];
1061
1062 memset(blanks, ' ', sizeof(blanks));
1063 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
1064 return (s);

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

1103 static int first = 1;
1104 static const char *term_so, *term_se;
1105
1106 if (first) {
1107 char tbuf[1024], cbuf[512], *b;
1108
1109 term_se = term_so = NULL;
1110
1024 /* On how to highlight on this type of terminal (if any) */
1111 /* On how to highlight on this type of terminal (if any). */
1025 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1026 b = cbuf;
1027 term_so = tgetstr("so", &b);
1028 term_se = tgetstr("se", &b);
1029 }
1030
1031 first = 0;
1032 }
1033
1034 /*
1035 * This check is not necessary, should have been handled before calling
1036 * this function.
1037 */
1038 if (flag_nohighlight) {
1039 memcpy(dst, src, len);
1040 return;
1041 }
1042
1112 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
1113 b = cbuf;
1114 term_so = tgetstr("so", &b);
1115 term_se = tgetstr("se", &b);
1116 }
1117
1118 first = 0;
1119 }
1120
1121 /*
1122 * This check is not necessary, should have been handled before calling
1123 * this function.
1124 */
1125 if (flag_nohighlight) {
1126 memcpy(dst, src, len);
1127 return;
1128 }
1129
1043 /* If it is a real terminal, use the data from the termcap database. */
1130 /*
1131 * If it is a real terminal, use the data from the termcap database.
1132 */
1044 if (term_so != NULL && term_se != NULL) {
1133 if (term_so != NULL && term_se != NULL) {
1045 /* separator */
1134 /* separator. */
1046 dst[0] = ' ';
1047 dst++;
1135 dst[0] = ' ';
1136 dst++;
1048 /* highlight on */
1137 /* highlight on. */
1049 memcpy(dst, term_so, strlen(term_so));
1050 dst += strlen(term_so);
1138 memcpy(dst, term_so, strlen(term_so));
1139 dst += strlen(term_so);
1051 /* the actual text (minus leading space) */
1140 /* the actual text. (minus leading space) */
1052 len--;
1053 src++;
1054 memcpy(dst, src, len);
1055 dst += len;
1141 len--;
1142 src++;
1143 memcpy(dst, src, len);
1144 dst += len;
1056 /* highlight off */
1145 /* highlight off. */
1057 memcpy(dst, term_se, strlen(term_se));
1058 *extralen = strlen(term_so) + strlen(term_se);
1059 return;
1060 }
1061
1062 /*
1146 memcpy(dst, term_se, strlen(term_se));
1147 *extralen = strlen(term_so) + strlen(term_se);
1148 return;
1149 }
1150
1151 /*
1063 * Otherwise, print a _, backspace and the letter
1152 * Otherwise, print a _, backspace and the letter.
1064 */
1065 *extralen = 0;
1153 */
1154 *extralen = 0;
1066 /* skip leading space */
1155 /* skip leading space. */
1067 src++;
1068 len--;
1156 src++;
1157 len--;
1069 /* separator */
1158 /* separator. */
1070 dst[0] = ' ';
1071 dst++;
1072 while (len > 0) {
1159 dst[0] = ' ';
1160 dst++;
1161 while (len > 0) {
1073 /* _ and backspace */
1162 /* _ and backspace. */
1074 memcpy(dst, "_\010", 2);
1075 dst += 2;
1076 *extralen += 2;
1163 memcpy(dst, "_\010", 2);
1164 dst += 2;
1165 *extralen += 2;
1077 /* the character */
1166 /* the character. */
1078 *dst++ = *src++;
1079 len--;
1080 }
1167 *dst++ = *src++;
1168 len--;
1169 }
1170 return;
1081}
1171}