From owner-svn-src-all@FreeBSD.ORG Wed Apr 22 06:03:23 2009 Return-Path: Delivered-To: svn-src-all@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id EF3271065675; Wed, 22 Apr 2009 06:03:23 +0000 (UTC) (envelope-from das@FreeBSD.ORG) Received: from zim.MIT.EDU (ZIM.MIT.EDU [18.95.3.101]) by mx1.freebsd.org (Postfix) with ESMTP id B9C6B8FC19; Wed, 22 Apr 2009 06:03:23 +0000 (UTC) (envelope-from das@FreeBSD.ORG) Received: from zim.MIT.EDU (localhost [127.0.0.1]) by zim.MIT.EDU (8.14.3/8.14.2) with ESMTP id n3M66h7m022242; Wed, 22 Apr 2009 02:06:43 -0400 (EDT) (envelope-from das@FreeBSD.ORG) Received: (from das@localhost) by zim.MIT.EDU (8.14.3/8.14.2/Submit) id n3M66h6A022241; Wed, 22 Apr 2009 02:06:43 -0400 (EDT) (envelope-from das@FreeBSD.ORG) Date: Wed, 22 Apr 2009 02:06:43 -0400 From: David Schultz To: Roman Divacky Message-ID: <20090422060643.GA22109@zim.MIT.EDU> Mail-Followup-To: Roman Divacky , Robert Watson , src-committers@FreeBSD.ORG, svn-src-all@FreeBSD.ORG, svn-src-head@FreeBSD.ORG References: <200904201819.n3KIJcZo054306@svn.freebsd.org> <20090421185436.GA18628@zim.MIT.EDU> <20090421190651.GA2505@freebsd.org> <20090421194622.GA19215@zim.MIT.EDU> <20090421202754.GA19417@zim.MIT.EDU> <20090421203106.GA13661@freebsd.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20090421203106.GA13661@freebsd.org> Cc: svn-src-head@FreeBSD.ORG, svn-src-all@FreeBSD.ORG, src-committers@FreeBSD.ORG, Robert Watson Subject: Re: svn commit: r191330 - head/usr.bin/ncal X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Apr 2009 06:03:24 -0000 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'; } }