]> err.no Git - util-linux/commitdiff
cal: add support for highlighting an arbitrary date
authorPádraig Brady <P@draigBrady.com>
Mon, 1 Oct 2007 23:22:45 +0000 (00:22 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 11 Oct 2007 12:19:39 +0000 (14:19 +0200)
This is done by calling cal with the extra day parameter like:

cal 14 9 1752

Note the tests were updated to use the new syntax.

Note also that this patch changes the -y option
to always print a full year, even if a month or
the -[13] options are specified.
This matches the cal operation from bsdmainutils on debian
and also allows one to print a full year while
highlighting a particular date.

Signed-off-by: Pádraig Brady <P@draigBrady.com>
TODO
misc-utils/Makefile.am
misc-utils/cal.1
misc-utils/cal.c
tests/commands.sh.in
tests/ts-cal-1
tests/ts-cal-3
tests/ts-cal-y

diff --git a/TODO b/TODO
index 1171648228377ca7e38a2e9005e6b5722055f2d1..f063f011cff59c3dfa277e0d2803ceb131d9d023 100644 (file)
--- a/TODO
+++ b/TODO
@@ -29,8 +29,6 @@
        From: LaMont Jones <lamont@mmjgroup.com>
        Subject: Re: [PATCH] losetup: support password hashing and specifying the key length
 
- * add an option to the cal command to control highlight date
-
  * convet po/ files to UTF-8
 
  * use rpmatch() for all Y/N questions
index e9f304f409298096411799fc0bcd84617b35b1e5..45ee3cccfe8604b4ab89a7a151efa2b735b189a9 100644 (file)
@@ -75,9 +75,3 @@ install-exec-hook::
 endif
 endif
 endif
-
-noinst_PROGRAMS = cal_test
-cal_test_SOURCES = cal.c
-cal_test_CPPFLAGS = -DTEST_CAL $(AM_CPPFLAGS)
-cal_test_LDADD = $(cal_LDADD)
-
index 8ec44b047caaf3c4851ea2148b18cd1cb6743157..53a6299280ad6141efa01bf5d47f1213be910c45 100644 (file)
@@ -43,7 +43,7 @@
 .Sh SYNOPSIS
 .Nm cal
 .Op Fl smjy13
-.Op [ Ar month ] Ar year
+.Op [ [ Ar day] Ar month ] Ar year
 .Sh DESCRIPTION
 .Nm Cal
 displays a simple calendar.
@@ -76,6 +76,8 @@ will
 .Em not
 display a calendar for 1989.
 Two parameters denote the month (1 - 12) and year.
+Three parameters denote the day (1-31), month and year,
+and the day will be highlighted if the calendar is displayed on a terminal.
 If no parameters are specified, the current month's calendar is
 displayed.
 .Pp
index 10bb8b5cd1d279dfe24a51b065c993bfeb1291db..d5bcee6415508353e28265d099a2d07596e49837 100644 (file)
@@ -341,6 +341,10 @@ main(int argc, char **argv) {
 
        day = month = year = 0;
        switch(argc) {
+       case 3:
+               if ((day = atoi(*argv++)) < 1 || month > 31)
+                       errx(1, _("illegal day value: use 1-%d"), 31);
+               /* FALLTHROUGH */
        case 2:
                if ((month = atoi(*argv++)) < 1 || month > 12)
                        errx(1, _("illegal month value: use 1-12"));
@@ -348,38 +352,38 @@ main(int argc, char **argv) {
        case 1:
                if ((year = atoi(*argv)) < 1 || year > 9999)
                        errx(1, _("illegal year value: use 1-9999"));
+               if (day) {
+                       int dm = days_in_month[leap_year(year)][month];
+                       if (day > dm)
+                               errx(1, _("illegal day value: use 1-%d"), dm);
+                       day = day_in_year(day, month, year);
+               }
+               if (!month)
+                       yflag=1;
                break;
        case 0:
-       {
-#ifdef TEST_CAL
-               char *e = getenv("TEST_TIME");
-
-               if (e && isdigit((unsigned char) *e))
-                       now = atol(e);
-               else
-#endif
-                       time(&now);
-       }
+               time(&now);
                local_time = localtime(&now);
-               if (isatty(1))
-                       day = local_time->tm_yday + 1;
+               day = local_time->tm_yday + 1;
                year = local_time->tm_year + 1900;
-               if (!yflag)
-                       month = local_time->tm_mon + 1;
+               month = local_time->tm_mon + 1;
                break;
        default:
                usage();
        }
        headers_init();
 
-       if (month && num_months == 1)
-               monthly(day, month, year);
-       else if (month && num_months == 3)
-               monthly3(day, month, year);
-       else if (julian)
+       if (!isatty(1))
+               day = 0; /* don't highlight */
+
+       if (yflag && julian)
                j_yearly(day, year);
-       else
+       else if (yflag)
                yearly(day, year);
+       else if (num_months == 1)
+               monthly(day, month, year);
+       else if (num_months == 3)
+               monthly3(day, month, year);
        exit(0);
 }
 
@@ -633,6 +637,9 @@ day_array(int day, int month, int year, int *days) {
        if (month == 9 && year == 1752) {
                d_sep1752 = julian ? j_sep1752 : sep1752;
                memcpy(days, d_sep1752 + week1stday, MAXDAYS * sizeof(int));
+               for (dm=0; dm<MAXDAYS; dm++)
+                       if (j_sep1752[dm] == day)
+                               days[dm] |= TODAY_FLAG;
                return;
        }
        memcpy(days, empty, MAXDAYS * sizeof(int));
@@ -806,6 +813,6 @@ void
 usage()
 {
 
-       (void)fprintf(stderr, _("usage: cal [-13smjyV] [[month] year]\n"));
+       (void)fprintf(stderr, _("usage: cal [-13smjyV] [[[day] month] year]\n"));
        exit(1);
 }
index 5e0bb5041ceedef5aca28214b0d559019e990c07..c96a422c6ef7a72daad1b7ba84355b26c217ba34 100644 (file)
@@ -32,7 +32,6 @@ TS_CMD_COL=${TS_CMD_COL:-"$TOPDIR/text-utils/col"}
 TS_CMD_NAMEI=${TS_CMD_NAMEI-"$TOPDIR/misc-utils/namei"}
 TS_CMD_LOOK=${TS_CMD_LOOK-"$TOPDIR/misc-utils/look"}
 TS_CMD_CAL=${TS_CMD_CAL-"$TOPDIR/misc-utils/cal"}
-TS_CMD_CALTEST=${TS_CMD_CALTEST-"$TOPDIR/misc-utils/cal_test"}
 TS_CMD_SCRIPT=${TS_CMD_SCRIPT-"$TOPDIR/misc-utils/script"}
 
 TS_CMD_CHECKTTY=${TS_CMD_CHECKTTY-"$TOPDIR/login-utils/checktty_test"}
index 414cc831c69f5cc89e3206eb633f6a5ff22ba4ee..53a1395eafcc893a65efc2d646d56252aedf4bf5 100755 (executable)
@@ -26,35 +26,35 @@ ts_init "$*"
 set -o pipefail
 
 USETERM=$( ts_has_option "useterm" "$*" )
-MYTIME=1159350000
+MYTIME="27 09 2006"
 
 [ "$USETERM" == "yes" ] && TS_VERBOSE="yes"
 ts_log ""
 
 ts_log "Gregorian - Monday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -1m
+       $TS_CMD_CAL -1m $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -1m >> $TS_OUTPUT
+$TS_CMD_CAL -1m $MYTIME >> $TS_OUTPUT
 
 ts_log "Gregorian - Sunday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -1s
+       $TS_CMD_CAL -1s $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -1s >> $TS_OUTPUT
+$TS_CMD_CAL -1s $MYTIME >> $TS_OUTPUT
 
 
 ts_log "Julian - Monday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -1mj
+       $TS_CMD_CAL -1mj $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -1mj >> $TS_OUTPUT
+$TS_CMD_CAL -1mj $MYTIME >> $TS_OUTPUT
 
 ts_log "Julian - Sunday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -1sj
+       $TS_CMD_CAL -1sj $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -1sj >> $TS_OUTPUT
+$TS_CMD_CAL -1sj $MYTIME >> $TS_OUTPUT
 
 ts_finalize
 
index 877c194e7247460c9d861c788b81f45def130803..aa815f586dc94c9c6ef670e7edcbd5185944f293 100755 (executable)
@@ -26,35 +26,35 @@ ts_init "$*"
 set -o pipefail
 
 USETERM=$( ts_has_option "useterm" "$*" )
-MYTIME=1159350000
+MYTIME="27 09 2006"
 
 [ "$USETERM" == "yes" ] && TS_VERBOSE="yes"
 ts_log ""
 
 ts_log "Gregorian - Monday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME  $TS_CMD_CALTEST -3m
+        $TS_CMD_CAL -3m $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -3m >> $TS_OUTPUT
+$TS_CMD_CAL -3m $MYTIME >> $TS_OUTPUT
 
 ts_log "Gregorian - Sunday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME  $TS_CMD_CALTEST -3s
+        $TS_CMD_CAL -3s $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -3s >> $TS_OUTPUT
+$TS_CMD_CAL -3s $MYTIME >> $TS_OUTPUT
 
 
 ts_log "Julian - Monday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -3mj
+       $TS_CMD_CAL -3mj $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -3mj >> $TS_OUTPUT
+$TS_CMD_CAL -3mj $MYTIME >> $TS_OUTPUT
 
 ts_log "Julian - Sunday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME  $TS_CMD_CALTEST -3sj
+        $TS_CMD_CAL -3sj $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -3sj >> $TS_OUTPUT
+$TS_CMD_CAL -3sj $MYTIME >> $TS_OUTPUT
 
 ts_finalize
 
index 4a18445d313e7eb087c057213126eb510f3c8269..769373a903d18898fbb8f83edb54e9b9978c2898 100755 (executable)
@@ -26,35 +26,35 @@ ts_init "$*"
 set -o pipefail
 
 USETERM=$( ts_has_option "useterm" "$*" )
-MYTIME=1164800000 # 29-Nov-2006
+MYTIME="29 11 2006"
 
 [ "$USETERM" == "yes" ] && TS_VERBOSE="yes"
 ts_log ""
 
 ts_log "Gregorian - Monday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -ym
+       $TS_CMD_CAL -ym $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -ym >> $TS_OUTPUT
+$TS_CMD_CAL -ym $MYTIME >> $TS_OUTPUT
 
 ts_log "Gregorian - Sunday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -ys
+       $TS_CMD_CAL -ys $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -ys >> $TS_OUTPUT
+$TS_CMD_CAL -ys $MYTIME >> $TS_OUTPUT
 
 
 ts_log "Julian - Monday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -ymj
+       $TS_CMD_CAL -ymj $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -ymj >> $TS_OUTPUT
+$TS_CMD_CAL -ymj $MYTIME >> $TS_OUTPUT
 
 ts_log "Julian - Sunday-based week"
 if [ "$USETERM" == "yes" ]; then
-       TEST_TIME=$MYTIME $TS_CMD_CALTEST -ysj
+       $TS_CMD_CAL -ysj $MYTIME
 fi
-TEST_TIME=$MYTIME $TS_CMD_CALTEST -ysj >> $TS_OUTPUT
+$TS_CMD_CAL -ysj $MYTIME >> $TS_OUTPUT
 
 ts_finalize