Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 10 Mar 2010 07:07:13 +0000 (UTC)
From:      Edwin Groothuis <edwin@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r204944 - user/edwin/ncal
Message-ID:  <201003100707.o2A77Dvp063320@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: edwin
Date: Wed Mar 10 07:07:12 2010
New Revision: 204944
URL: http://svn.freebsd.org/changeset/base/204944

Log:
  Do sanity checking: for various combinations of the options, do not
  allow them together.

Modified:
  user/edwin/ncal/ncal.1
  user/edwin/ncal/ncal.c

Modified: user/edwin/ncal/ncal.1
==============================================================================
--- user/edwin/ncal/ncal.1	Wed Mar 10 06:10:39 2010	(r204943)
+++ user/edwin/ncal/ncal.1	Wed Mar 10 07:07:12 2010	(r204944)
@@ -148,6 +148,14 @@ year default to those of the current sys
 will display a calendar for the month of August in the current
 year).
 .Pp
+Not all options can be used together. For example
+.Dq Li -3 -A 2 -B 3 -y -m 7
+would mean:
+show me the three months around the seventh month, three before
+that, two after that and the whole year.
+.Nm ncal
+will warn about these combinations.
+.Pp
 A year starts on January 1.
 .Sh SEE ALSO
 .Xr calendar 3 ,

Modified: user/edwin/ncal/ncal.c
==============================================================================
--- user/edwin/ncal/ncal.c	Wed Mar 10 06:10:39 2010	(r204943)
+++ user/edwin/ncal/ncal.c	Wed Mar 10 07:07:12 2010	(r204944)
@@ -194,12 +194,18 @@ main(int argc, char *argv[])
 	int     m = 0;			/* month */
 	int	y = 0;			/* year */
 	int     flag_backward = 0;	/* user called cal--backward compat. */
-	int     flag_hole_year = 0;	/* user wants the whole year */
+	int     flag_wholeyear = 0;	/* user wants the whole year */
 	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
 	int     flag_julian_day = 0;	/* user wants the Julian day
 					 * numbers */
-	int	flag_orthodox = 0;	/* use wants Orthodox easter */
-	int	flag_easter = 0;	/* use wants easter date */
+	int	flag_orthodox = 0;	/* user wants Orthodox easter */
+	int	flag_easter = 0;	/* user wants easter date */
+	int	flag_3months = 0;	/* user wants 3 month display (-3) */
+	int	flag_after = 0;		/* user wants to see months after */
+	int	flag_before = 0;	/* user wants to see months before */
+	int	flag_specifiedmonth = 0;/* user wants to see this month (-m) */
+	int	flag_givenmonth = 0;	/* user has specified month [n] */
+	int	flag_givenyear = 0;	/* user has specified year [n] */
 	char	*cp;			/* character pointer */
 	char	*flag_month = NULL;	/* requested month as string */
 	char	*flag_highlightdate = NULL;
@@ -253,17 +259,23 @@ main(int argc, char *argv[])
 	while ((ch = getopt(argc, argv, "A:B:3Jbd:ehjm:ops:wy")) != -1)
 		switch (ch) {
 		case '3':
-			before = after = 1;
+			flag_3months = 1;
 			break;
 		case 'A':
-			after = strtol(optarg, NULL, 10);
-			if (after < 0)
-				errx(1, "Argument to -A must be positive");
+			if (flag_after > 0)
+				errx(EX_USAGE, "Double -A specified");
+			flag_after = strtol(optarg, NULL, 10);
+			if (flag_after <= 0)
+				errx(EX_USAGE,
+				    "Argument to -A must be positive");
 			break;
 		case 'B':
-			before = strtol(optarg, NULL, 10);
-			if (before < 0)
-				errx(1, "Argument to -B must be positive");
+			if (flag_before > 0)
+				errx(EX_USAGE, "Double -A specified");
+			flag_before = strtol(optarg, NULL, 10);
+			if (flag_before <= 0)
+				errx(EX_USAGE,
+				    "Argument to -B must be positive");
 			break;
 		case 'J':
 			if (flag_backward)
@@ -289,9 +301,10 @@ main(int argc, char *argv[])
 			flag_julian_day = 1;
 			break;
 		case 'm':
+			if (flag_specifiedmonth)
+				errx(EX_USAGE, "Double -m specified");
 			flag_month = optarg;
-			before = 0;
-			after = 0;
+			flag_specifiedmonth = 1;
 			break;
 		case 'o':
 			if (flag_backward)
@@ -324,7 +337,7 @@ main(int argc, char *argv[])
 			flag_weeks = 1;
 			break;
 		case 'y':
-			flag_hole_year = 1;
+			flag_wholeyear = 1;
 			break;
 		default:
 			usage();
@@ -338,21 +351,15 @@ main(int argc, char *argv[])
 		if (flag_easter)
 			usage();
 		flag_month = *argv++;
-		if (before == -1 && after == -1) {
-			before = 0;
-			after = 0;
-		}
+		flag_givenmonth = 1;
 		m = strtol(flag_month, NULL, 10);
 		/* FALLTHROUGH */
 	case 1:
-		y = atoi(*argv++);
+		y = atoi(*argv);
 		if (y < 1 || y > 9999)
-			errx(EX_USAGE, "year %d not in range 1..9999", y);
-		if (before == -1 && after == -1) {
-			before = 0;
-			after = 11;
-			m = 1;
-		}
+			errx(EX_USAGE, "year `%s' not in range 1..9999", *argv);
+		argv++;
+		flag_givenyear = 1;
 		break;
 	case 0:
 		{
@@ -363,22 +370,12 @@ main(int argc, char *argv[])
 			tm = localtime(&t);
 			y = tm->tm_year + 1900;
 			m = tm->tm_mon + 1;
-			if (before == -1)
-				before = 0;
-			if (after == -1)
-				after = 0;
 		}
 		break;
 	default:
 		usage();
 	}
 
-	if (flag_hole_year) {
-		m = 1;
-		before = 0;
-		after = 11;
-	}
-
 	if (flag_month != NULL) {
 		if (parsemonth(flag_month, &m, &y)) {
 			errx(EX_USAGE,
@@ -387,6 +384,67 @@ main(int argc, char *argv[])
 		}
 	}
 
+	/*
+	 * What do we support and what do we not support
+	 * flag_3month, flag_before, flag_after
+	 * flag_givenyear, flag_givenmonth
+	 * 
+	 */
+
+	/* -3 together with -A or -B */
+	if (flag_3months && (flag_after || flag_before))
+		errx(EX_USAGE, "-3 together with -A and -B is not supported.");
+	/* -3 together with -y */
+	if (flag_3months && flag_wholeyear)
+		errx(EX_USAGE, "-3 together with -y is not supported.");
+	/* -3 together with givenyear but no givenmonth */
+	if (flag_3months && flag_givenyear &&
+	    !(flag_givenmonth || flag_specifiedmonth))
+		errx(EX_USAGE,
+		    "-3 together with a given year but no given month is "
+		    "not supported.");
+	/* -m together with xx xxxx */
+	if (flag_specifiedmonth && flag_givenmonth)
+		errx(EX_USAGE,
+		    "-m together with a given month is not supported.");
+	/* -y together with -m */
+	if (flag_wholeyear && flag_specifiedmonth)
+		errx(EX_USAGE, "-y together with -m is not supported.");
+	/* -y together with xx xxxx */
+	if (flag_wholeyear && flag_givenmonth)
+		errx(EX_USAGE, "-y together a given month is not supported.");
+	/* -y together with -A or -B */
+	if (flag_wholeyear && (flag_before > 0 || flag_after > 0))
+		errx(EX_USAGE, "-y together a -A or -B is not supported.");
+	/* The rest should be fine */
+
+	/* Select the period to display, in order of increasing priority */
+	if (flag_wholeyear ||
+	    (flag_givenyear && !(flag_givenmonth || flag_specifiedmonth))) {
+		m = 1;
+		before = 0;
+		after = 11;
+	}
+	if (flag_givenyear && flag_givenmonth) {
+		before = 0;
+		after = 0;
+	}
+	if (flag_specifiedmonth) {
+		before = 0;
+		after = 0;
+	}
+	if (flag_before) {
+		before = flag_before;
+	}
+	if (flag_after) {
+		after = flag_after;
+	}
+	if (flag_3months) {
+		before = 1;
+		after = 1;
+	}
+
+	/* Highlight a specified day or today */
 	if (flag_highlightdate != NULL) {
 		dt.y = strtol(flag_highlightdate, NULL, 10);
 		dt.m = strtol(flag_highlightdate + 5, NULL, 10);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201003100707.o2A77Dvp063320>