--- cal.c.old Mon May 26 15:23:53 2003 +++ cal.c Mon May 26 18:58:24 2003 @@ -53,6 +53,10 @@ * added support to read "first_weekday" locale information * still to do: support for 'cal_direction' (will require a major * rewrite of the displaying) and proper handling of RTL scripts + * + * 2003-05-26 Alan Ford + * Added ability to do a range of months in a year, such as + * "cal 4-7 2003" or "cal 4 7 2003" */ #include @@ -181,6 +185,7 @@ void trim_trailing_spaces __P((char *)); void usage __P((void)); void yearly __P((int)); +void monthly_range __P((int, int, int)); void headers_init(void); extern char *__progname; @@ -191,6 +196,7 @@ int ch, month, year, yflag; char *progname, *p; int num_months = NUM_MONTHS; + int start_month = 0; progname = argv[0]; if ((p = strrchr(progname, '/')) != NULL) @@ -257,8 +263,20 @@ month = year = 0; switch(argc) { + case 3: + if ((start_month = atoi(*argv++)) < 1 || start_month > 12) + errx(1, _("illegal month value: use 1-12")); + /* FALLTHROUGH */ case 2: - if ((month = atoi(*argv++)) < 1 || month > 12) + if ((p = strchr(*argv, '-')) != NULL) { + *p = '\0'; + if ((start_month = atoi(*argv++)) < 1 || start_month > 12) + errx(1, _("illegal month value: use 1-12")); + p++; + } else { + p = *argv++; + } + if ((month = atoi(p)) < 1 || month > 12) errx(1, _("illegal month value: use 1-12")); /* FALLTHROUGH */ case 1: @@ -277,7 +295,11 @@ } headers_init(); - if (month && num_months == 1) + if (month && start_month != 0) { + if (month < start_month) + errx(1, _("end month cannot be before start month")); + monthly_range(start_month, month, year); + } else if (month && num_months == 1) monthly(month, year); else if (month && num_months == 3) monthly3(month, year); @@ -523,6 +545,35 @@ (void)printf("\n"); } +void +monthly_range(start, end, year) + int start, end, year; +{ + int i, j, width, rowend; + int range=end-start+1; + struct fmt_st outs[range]; + + rowend = (range % 3 == 0 ? range : range + 3 - (range % 3)); + + for ( i = 0; i < range; i++ ) + do_monthly(start + i, year, &outs[i]); + + width = (julian ? J_WEEK_LEN : WEEK_LEN); + for ( i = 0; i < rowend; i += 3 ) + { + for ( j = 0; j < FMT_ST_LINES; j++ ) + { + printf("%-*.*s ", width, width, outs[i].s[j]); + if (i+1 < range) + printf("%-*.*s ", width, width, outs[i+1].s[j]); + if (i+2 < range) + printf("%-*.*s ", width, width, outs[i+2].s[j]); + printf("\n"); + } + } + +} + /* * day_array -- * Fill in an array of 42 integers with a calendar. Assume for a moment @@ -673,6 +724,6 @@ usage() { - (void)fprintf(stderr, _("usage: cal [-13smjyV] [[month] year]\n")); + (void)fprintf(stderr, _("usage: cal [-13smjyV] [[[start_month] month] year]\n")); exit(1); }