svn commit: r191330 - head/usr.bin/ncal

David Schultz das at FreeBSD.ORG
Wed Apr 22 06:03:24 UTC 2009


On Tue, Apr 21, 2009, Roman Divacky wrote:
> > On the other hand, I don't mean to blame Roman or insist that he
> > fix it.  When I last touched ncal, I didn't have enough time to fix
> > all of these things either.  In fact, I only converted about half
> > of it to support multibyte month names and so forth, and strictly
> > speaking it should use wide character output routines exclusively.
> 
> feel free to work on it, or anyone else. I wanted to see what day is
> today when I am filling in my job reports. I got this and I dont care
> anymore about cal...

Things in our tree get into this sort of state because over the
years, a dozen different people decide to add "one more feature"
and don't care enough to do a good job. I'm not suggesting that
you rewrite it from scratch, but when you've got 4 indices i, j,
k, and l, one of which isn't even an index even though it is
documented as such, and a bunch of inscrutable code that has been
cut and paste in at least two places, it seems like it's time to
step back and ask if there's a simpler way to do things.

For instance, it seems like the following would accomplish what
you intended in 1/3 as many lines of new code. (FWIW, I don't have
time to mess around with this either; I hacked this together in 5
minutes and haven't tested it much.)

Index: ncal.c
===================================================================
--- ncal.c	(revision 191369)
+++ ncal.c	(working copy)
@@ -207,7 +207,7 @@
 	time_t t;
 	struct tm *tm1;
 
-	term_e = term_r = NULL;
+	term_e = term_r = "";
 	today = 0;
 	if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
 		date	dt;		/* handy date */
@@ -272,7 +272,7 @@
 			flag_julian_cal = 1;
 			break;
 		case 'h':
-			term_r = term_e = NULL;
+			term_r = term_e = "";
 			break;
 		case 'e':
 			if (flag_backward)
@@ -621,7 +621,7 @@
 	int     dw;		/* width of numbers */
 	int     first;		/* first day of month */
 	int     firstm;		/* first day of first week of month */
-	int     i, j, k, l;	/* just indices */
+	int     i, j, k;	/* just indices */
 	int     last;		/* the first day of next month */
 	int     jan1 = 0;	/* the first day of this year */
 	char   *ds;		/* pointer to day strings (daystr or
@@ -666,42 +666,24 @@
 	/*
 	 * Fill the lines with day of month or day of year (julian day)
 	 * line index: i, each line is one weekday. column index: j, each
-	 * column is one day number. print column index: k.
+	 * column is one day number.
 	 */
 	for (i = 0; i != 7; i++) {
-		l = 0;
-		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
-			if (j == today && (term_r != NULL && term_e != NULL)) {
-				l = strlen(term_r);
-				if (jd_flag)
-					dt.d = j - jan1 + 1;
-				else
-					sdateb(j, &dt);
-				/* separator */
-				mlines->lines[i][k] = ' ';
-				/* the actual text */
-				memcpy(mlines->lines[i] + k + l,
-				    ds + dt.d * dw, dw);
-				/* highlight on */
-				memcpy(mlines->lines[i] + k + 1, term_r, l);
-				/* highlight off */
-				memcpy(mlines->lines[i] + k + l + dw, term_e,
-				    strlen(term_e));
-				l = strlen(term_e) + strlen(term_r);
-				continue;
-			}
+		mlines->lines[i][0] = '\0';
+		for (j = firstm + i; j < last; j += 7) {
 			if (j >= first) {
 				if (jd_flag)
 					dt.d = j - jan1 + 1;
 				else
-					sdate(j, &dt);
-				memcpy(mlines->lines[i] + k + l,
-				       ds + dt.d * dw, dw);
+					sdateb(j, &dt);
+				if (j == today)
+					strcat(mlines->lines[i], term_r);
+				strncat(mlines->lines[i], ds + dt.d * dw, dw);
+				if (j == today)
+					strcat(mlines->lines[i], term_e);
 			} else
-				memcpy(mlines->lines[i] + k + l, "    ", dw);
+				strncat(mlines->lines[i], "    ", dw);
 		}
-		mlines->lines[i][k + l] = '\0';
-				
 	}
 
 	/* fill the weeknumbers */
@@ -726,7 +708,7 @@
 	int     dw;		/* width of numbers */
 	int     first;		/* first day of month */
 	int     firsts;		/* sunday of first week of month */
-	int     i, j, k, l;	/* just indices */
+	int     i, j, k;	/* just indices */
 	int     jan1 = 0;	/* the first day of this year */
 	int     last;		/* the first day of next month */
 	char   *ds;		/* pointer to day strings (daystr or
@@ -787,42 +769,21 @@
 	 * column is one day number. print column index: k.
 	 */
 	for (i = 0; i != 6; i++) {
-		l = 0;
-		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
-		    j++, k += dw) { 
-			if (j == today && (term_r != NULL && term_e != NULL)) {
-				l = strlen(term_r);
-				if (jd_flag)
-					dt.d = j - jan1 + 1;
-				else
-					sdateb(j, &dt);
-				/* separator */
-				mlines->lines[i][k] = ' ';
-				/* the actual text */
-				memcpy(mlines->lines[i] + k + l,
-				    ds + dt.d * dw, dw);
-				/* highlight on */
-				memcpy(mlines->lines[i] + k + 1, term_r, l);
-				/* highlight off */
-				memcpy(mlines->lines[i] + k + l + dw, term_e,
-				    strlen(term_e));
-				l = strlen(term_e) + strlen(term_r);
-				continue;
-			}
+		mlines->lines[i][0] = '\0';
+		for (j = firsts + 7 * i, k = 0; j < last && k != 7; j++, k++) { 
 			if (j >= first) {
 				if (jd_flag)
 					dt.d = j - jan1 + 1;
 				else
 					sdateb(j, &dt);
-				memcpy(mlines->lines[i] + k + l,
-				       ds + dt.d * dw, dw);
+				if (j == today)
+					strcat(mlines->lines[i], term_r);
+				strncat(mlines->lines[i], ds + dt.d * dw, dw);
+				if (j == today)
+					strcat(mlines->lines[i], term_e);
 			} else
-				memcpy(mlines->lines[i] + k + l, "    ", dw);
+				strncat(mlines->lines[i], "    ", dw);
 		}
-		if (k == 0)
-			mlines->lines[i][1] = '\0';
-		else
-			mlines->lines[i][k + l] = '\0';
 	}
 }
 


More information about the svn-src-all mailing list