public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/11] pull: cal(1) spring clean up
@ 2013-04-28 17:10 Sami Kerola
  2013-04-28 17:10 ` [PATCH 01/11] cal: fix preprocessor directive indendation Sami Kerola
                   ` (10 more replies)
  0 siblings, 11 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:10 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Hi all,

Occasionally I wonder why cal(1) does not have --week option to include
week numbers to print out.  So I decided to have a closer look of the
cal, and found out that it could made a little cleaner.  These patches
are about that, and hopefully rather uninteresting.  Only patch that has
any implications to users is 0010 which removes of trailing white spaces
from three month print out.

But back to the question.  Would it make sense to try add week number
printing?  It would work much like 'gcal --with-week-number
--iso-week-number=yes', which brings me to next question.  If week
numbers are preferred should both 'standard' and 'iso' week numbers be
implemented?  Hearing that the week numbers are not really needed is also
fine.  It's not often I need such, the feature would be more like 'nice
to have'.


The following changes since commit a48c321dd175d043fb4572bab625631b2055075e:

  textual: fixing typos in a warning and an error message (2013-04-26 13:55:29 +0200)

are available in the git repository at:

  git://github.com/kerolasa/lelux-utiliteetit.git 2013wk16

for you to fetch changes up to 0ec2cbe22ef73a0f3b1814a08510c8a30e76ee07:

  cal: move global variables to local scope (2013-04-28 17:55:40 +0100)

----------------------------------------------------------------
Sami Kerola (11):
      cal: fix preprocessor directive indendation
      cal: convert function like definitions to functions
      cal: clean up use of constants
      tests: add calendar reformation check
      cal: simplify calendar reformat calculations
      cal: remove unnecessary initializations
      cal: de-duplicate julian specific functions
      cal: allow day highlight testing
      tests: add cal day highlight corner cases
      cal: trim three month display trailing white spaces
      cal: move global variables to local scope

 misc-utils/Makemodule.am     |   8 +
 misc-utils/cal.c             | 517 +++++++++++++++++++++----------------------
 tests/commands.sh            |   2 +-
 tests/expected/cal/1m        |   8 +-
 tests/expected/cal/3m        |  24 +-
 tests/expected/cal/highlight |  55 +++++
 tests/expected/cal/sep1752   | 198 +++++++++++++++++
 tests/expected/cal/year      |   8 +-
 tests/ts/cal/highlight       |  70 ++++++
 tests/ts/cal/sep1752         |  95 ++++++++
 10 files changed, 702 insertions(+), 283 deletions(-)
 create mode 100644 tests/expected/cal/highlight
 create mode 100644 tests/expected/cal/sep1752
 create mode 100755 tests/ts/cal/highlight
 create mode 100755 tests/ts/cal/sep1752



^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 01/11] cal: fix preprocessor directive indendation
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
@ 2013-04-28 17:10 ` Sami Kerola
  2013-04-28 17:10 ` [PATCH 02/11] cal: convert function like definitions to functions Sami Kerola
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:10 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

And code within the blocks selected by preprocessor to be live code.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 81 +++++++++++++++++++++++++++-----------------------------
 1 file changed, 39 insertions(+), 42 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 276148d..13830de 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -73,69 +73,66 @@
 #include "strutils.h"
 
 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
-
-#ifdef HAVE_NCURSES_H
-#include <ncurses.h>
-#elif defined(HAVE_NCURSES_NCURSES_H)
-#include <ncurses/ncurses.h>
-#endif
-
-#include <term.h>                       /* include after <curses.h> */
-
-static void
-my_setupterm(const char *term, int fildes, int *errret) {
-    setupterm((char*)term, fildes, errret);
+# ifdef HAVE_NCURSES_H
+#  include <ncurses.h>
+# elif defined(HAVE_NCURSES_NCURSES_H)
+#  include <ncurses/ncurses.h>
+# endif
+# include <term.h>
+
+static void my_setupterm(const char *term, int fildes, int *errret)
+{
+	setupterm((char *)term, fildes, errret);
 }
 
-static void
-my_putstring(char *s) {
-     putp(s);
+static void my_putstring(char *s)
+{
+	putp(s);
 }
 
-static const char *
-my_tgetstr(char *s __attribute__ ((__unused__)), char *ss) {
-    const char* ret = tigetstr(ss);
-    if (!ret || ret==(char*)-1)
-	return "";
-    else
-	return ret;
+static const char *my_tgetstr(char *s __attribute__((__unused__)), char *ss)
+{
+	const char *ret = tigetstr(ss);
+	if (!ret || ret == (char *)-1)
+		return "";
+	else
+		return ret;
 }
 
 #elif defined(HAVE_LIBTERMCAP)
-
-#include <termcap.h>
+# include <termcap.h>
 
 char termbuffer[4096];
 char tcbuffer[4096];
 char *strbuf = termbuffer;
 
-static void
-my_setupterm(const char *term, int fildes, int *errret) {
-    *errret = tgetent(tcbuffer, term);
+static void my_setupterm(const char *term, int fildes, int *errret)
+{
+	*errret = tgetent(tcbuffer, term);
 }
 
-static void
-my_putstring(char *s) {
-     tputs (s, 1, putchar);
+static void my_putstring(char *s)
+{
+	tputs(s, 1, putchar);
 }
 
-static const char *
-my_tgetstr(char *s, char *ss __attribute__ ((__unused__))) {
-    const char* ret = tgetstr(s, &strbuf);
-    if (!ret)
-	return "";
-    else
-	return ret;
+static const char *my_tgetstr(char *s, char *ss __attribute__((__unused__)))
+{
+	const char *ret = tgetstr(s, &strbuf);
+	if (!ret)
+		return "";
+	else
+		return ret;
 }
 
-#else /* ! (HAVE_LIBTERMCAP || HAVE_LIBNCURSES || HAVE_LIBNCURSESW) */
+#else	/* ! (HAVE_LIBTERMCAP || HAVE_LIBNCURSES || HAVE_LIBNCURSESW) */
 
-static void
-my_putstring(char *s) {
-     fputs(s, stdout);
+static void my_putstring(char *s)
+{
+	fputs(s, stdout);
 }
 
-#endif
+#endif	/* end of LIBTERMCAP / NCURSES */
 
 
 const char	*term="";
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 02/11] cal: convert function like definitions to functions
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
  2013-04-28 17:10 ` [PATCH 01/11] cal: fix preprocessor directive indendation Sami Kerola
@ 2013-04-28 17:10 ` Sami Kerola
  2013-04-28 17:28   ` Dave Reisner
  2013-04-29 15:21   ` Thomas Bächler
  2013-04-28 17:10 ` [PATCH 03/11] cal: clean up use of constants Sami Kerola
                   ` (8 subsequent siblings)
  10 siblings, 2 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:10 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 45 ++++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 13830de..eacf3dd 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -209,23 +209,6 @@ char j_day_headings[J_WEEK_LEN*6+1];
 /* weekstart = 1  =>   "  M  Tu   W  Th   F   S   S " */
 const char *full_month[12];
 
-/* leap year -- account for gregorian reformation in 1752 */
-#define	leap_year(yr) \
-	((yr) <= 1752 ? !((yr) % 4) : \
-	(!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
-
-/* number of centuries since 1700, not inclusive */
-#define	centuries_since_1700(yr) \
-	((yr) > 1700 ? (yr) / 100 - 17 : 0)
-
-/* number of centuries since 1700 whose modulo of 400 is 0 */
-#define	quad_centuries_since_1700(yr) \
-	((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
-
-/* number of leap years between year 1 and this year, not inclusive */
-#define	leap_years_since_year_1(yr) \
-	((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
-
 /* 0 => sunday, 1 => monday */
 int weekstart=0;
 int julian;
@@ -239,6 +222,9 @@ struct fmt_st
   char s[FMT_ST_LINES][FMT_ST_CHARS];
 };
 
+int leap_year(int year);
+int centuries_since_1700(int year, int centuries);
+int leap_years_since_year_1(int year);
 char * ascii_day(char *, int);
 int center_str(const char* src, char* dest, size_t dest_size, size_t width);
 void center(const char *, size_t, int);
@@ -413,6 +399,31 @@ main(int argc, char **argv) {
 	return EXIT_SUCCESS;
 }
 
+/* leap year -- account for gregorian reformation in 1752 */
+int leap_year(int year)
+{
+	if (year <= 1752)
+		return !(year % 4);
+	else
+		return !(year % 4 && (year % 100 || year % 400));
+}
+
+/* number of centuries since 1700 */
+int centuries_since_1700(int year, int n)
+{
+	if (year < 1700)
+		return 0;
+	else
+		return ((year / (100 * n)) - (17 / n));
+}
+
+/* number of leap years between year 1 and this year, not inclusive */
+int leap_years_since_year_1(int year)
+{
+	return (year / 4 - centuries_since_1700(year, 1) +
+		centuries_since_1700(year, 4));
+}
+
 void headers_init(void)
 {
 	int i, wd;
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 03/11] cal: clean up use of constants
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
  2013-04-28 17:10 ` [PATCH 01/11] cal: fix preprocessor directive indendation Sami Kerola
  2013-04-28 17:10 ` [PATCH 02/11] cal: convert function like definitions to functions Sami Kerola
@ 2013-04-28 17:10 ` Sami Kerola
  2013-04-29 15:51   ` Thomas Bächler
  2013-04-28 17:11 ` [PATCH 04/11] tests: add calendar reformation check Sami Kerola
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:10 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

The commit also adds few new symbolic names, such as DAYS_IN_WEEK.  While
these definitions may look a little too trivial to have I am sure they
ease understanding what is going on in code where they are used.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 142 ++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 82 insertions(+), 60 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index eacf3dd..f0d97af 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -151,15 +151,51 @@ char		*Hrow;		/* pointer to highlighted row in month */
 #error NUM_MONTHS must be 1 or 3
 #endif
 
-#define	THURSDAY		4		/* for reformation */
-#define	SATURDAY		6		/* 1 Jan 1 was a Saturday */
+enum {
+	SUNDAY = 0,
+	MONDAY,
+	TUESDAY,
+	WEDNESDAY,
+	THURSDAY,
+	FRIDAY,
+	SATURDAY,
+	DAYS_IN_WEEK
+};
+
+#define	FIRST_WEEKDAY		SATURDAY	/* Jan 1st, 1 was a Saturday */
+#define	REFORMATION_WEEKDAY	TUESDAY		/* after reformation it was Thursday */
 
+#define REFORMATION_YEAR	1752		/* Signed-off-by: Lord Chesterfield */
+#define REFORMATION_MONTH	9		/* September */
 #define	FIRST_MISSING_DAY	639799		/* 3 Sep 1752 */
 #define	NUMBER_MISSING_DAYS	11		/* 11 day correction */
 
+#define DAYS_IN_YEAR		365		/* the common case, leap years are calculated */
+#define MONTHS_IN_YEAR		12
+#define DAYS_IN_MONTH		31
 #define	MAXDAYS			42		/* slots in a month array */
 #define	SPACE			-1		/* used in day array */
 
+#define SMALLEST_YEAR		1
+#define GREATEST_YEAR		9999
+
+#define	DAY_LEN			3		/* 3 spaces per day */
+#define	WEEK_LEN		(DAYS_IN_WEEK * DAY_LEN)
+#define	HEAD_SEP		2
+
+#define	J_DAY_LEN		4		/* 4 spaces per day */
+#define	J_WEEK_LEN		(DAYS_IN_WEEK * J_DAY_LEN)
+#define	J_HEAD_SEP		2
+
+#define TODAY_FLAG		0x400		/* flag day for highlighting */
+
+#define FMT_ST_LINES 8
+#define FMT_ST_CHARS 300	/* 90 suffices in most locales */
+struct fmt_st
+{
+  char s[FMT_ST_LINES][FMT_ST_CHARS];
+};
+
 static int days_in_month[2][13] = {
 	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
@@ -195,33 +231,19 @@ int sep1752[MAXDAYS+6] = {
 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE
 };
 
-#define	DAY_LEN		3		/* 3 spaces per day */
-#define	J_DAY_LEN	4		/* 4 spaces per day */
-#define	WEEK_LEN	21		/* 7 days * 3 characters */
-#define	J_WEEK_LEN	28		/* 7 days * 4 characters */
-#define	HEAD_SEP	2		/* spaces between day headings */
-#define	J_HEAD_SEP	2
 
 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
 char day_headings[WEEK_LEN*6+1];
 /* weekstart = 1  =>   " M Tu  W Th  F  S  S " */
 char j_day_headings[J_WEEK_LEN*6+1];
 /* weekstart = 1  =>   "  M  Tu   W  Th   F   S   S " */
-const char *full_month[12];
+const char *full_month[MONTHS_IN_YEAR];
 
 /* 0 => sunday, 1 => monday */
-int weekstart=0;
+int weekstart = SUNDAY;
 int julian;
 
-#define TODAY_FLAG		0x400		/* flag day for highlighting */
-
-#define FMT_ST_LINES 8
-#define FMT_ST_CHARS 300	/* 90 suffices in most locales */
-struct fmt_st
-{
-  char s[FMT_ST_LINES][FMT_ST_CHARS];
-};
-
+/* function prototypes */
 int leap_year(int year);
 int centuries_since_1700(int year, int centuries);
 int leap_years_since_year_1(int year);
@@ -244,7 +266,7 @@ int
 main(int argc, char **argv) {
 	struct tm *local_time;
 	time_t now;
-	int ch, day, month, year, yflag;
+	int ch, day = 0, month = 0, year = 0, yflag = 0;
 	int num_months = NUM_MONTHS;
 
 	static const struct option longopts[] = {
@@ -267,7 +289,7 @@ main(int argc, char **argv) {
 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
 	if ((term = getenv("TERM"))) {
 		int ret;
-		my_setupterm(term, 1, &ret);
+		my_setupterm(term, STDOUT_FILENO, &ret);
 		if (ret > 0) {
 			Senter = my_tgetstr("so","smso");
 			Sexit = my_tgetstr("se","rmso");
@@ -307,11 +329,10 @@ main(int argc, char **argv) {
 
 		wfd = val.word;
 		wfd = day_in_week(wfd % 100, (wfd / 100) % 100, wfd / (100 * 100));
-		weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7;
+		weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
 	}
 #endif
 
-	yflag = 0;
 	while ((ch = getopt_long(argc, argv, "13mjsyVh", longopts, NULL)) != -1)
 		switch(ch) {
 		case '1':
@@ -321,10 +342,10 @@ main(int argc, char **argv) {
 			num_months = 3;
 			break;
 		case 's':
-			weekstart = 0;		/* default */
+			weekstart = SUNDAY;	/* default */
 			break;
 		case 'm':
-			weekstart = 1;
+			weekstart = MONDAY;
 			break;
 		case 'j':
 			julian = 1;
@@ -347,21 +368,20 @@ main(int argc, char **argv) {
 	time(&now);
 	local_time = localtime(&now);
 
-	day = month = year = 0;
 	switch(argc) {
 	case 3:
 		day = strtos32_or_err(*argv++, _("illegal day value"));
-                if (day < 1 || 31 < day)
-			errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), 31);
+		if (day < 1 || DAYS_IN_MONTH < day)
+			errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), DAYS_IN_MONTH);
 		/* FALLTHROUGH */
 	case 2:
 		month = strtos32_or_err(*argv++, _("illegal month value: use 1-12"));
-		if (month < 1 || 12 < month)
+		if (month < 1 || MONTHS_IN_YEAR < month)
 			errx(EXIT_FAILURE, _("illegal month value: use 1-12"));
 		/* FALLTHROUGH */
 	case 1:
 		year = strtos32_or_err(*argv++, _("illegal year value: use 1-9999"));
-		if (year < 1 || 9999 < year)
+		if (year < SMALLEST_YEAR || GREATEST_YEAR < year)
 			errx(EXIT_FAILURE, _("illegal year value: use 1-9999"));
 		if (day) {
 			int dm = days_in_month[leap_year(year)][month];
@@ -384,7 +404,7 @@ main(int argc, char **argv) {
 	}
 	headers_init();
 
-	if (!isatty(1))
+	if (!isatty(STDOUT_FILENO))
 		day = 0; /* don't highlight */
 
 	if (yflag && julian)
@@ -402,7 +422,7 @@ main(int argc, char **argv) {
 /* leap year -- account for gregorian reformation in 1752 */
 int leap_year(int year)
 {
-	if (year <= 1752)
+	if (year <= REFORMATION_YEAR)
 		return !(year % 4);
 	else
 		return !(year % 4 && (year % 100 || year % 400));
@@ -411,10 +431,10 @@ int leap_year(int year)
 /* number of centuries since 1700 */
 int centuries_since_1700(int year, int n)
 {
-	if (year < 1700)
+	if (year < REFORMATION_YEAR)
 		return 0;
 	else
-		return ((year / (100 * n)) - (17 / n));
+		return ((year / (100 * n)) - ((REFORMATION_YEAR / 100) / n));
 }
 
 /* number of leap years between year 1 and this year, not inclusive */
@@ -432,9 +452,9 @@ void headers_init(void)
 	strcpy(day_headings, "");
 	strcpy(j_day_headings, "");
 
-	for (i = 0; i < 7; i++) {
+	for (i = 0; i < DAYS_IN_WEEK; i++) {
 		ssize_t space_left;
-		wd = (i + weekstart) % 7;
+		wd = (i + weekstart) % DAYS_IN_WEEK;
 
 		if (i)
 			strcat(cur_dh++, " ");
@@ -457,7 +477,7 @@ void headers_init(void)
 			       space_left, 3);
 	}
 
-	for (i = 0; i < 12; i++)
+	for (i = 0; i < MONTHS_IN_YEAR; i++)
 		full_month[i] = nl_langinfo(MON_1 + i);
 }
 
@@ -481,10 +501,10 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {
 
 	snprintf(out->s[1], FMT_ST_CHARS, "%s",
 		julian ? j_day_headings : day_headings);
-	for (row = 0; row < 6; row++) {
+	for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
 		int has_hl = 0;
-		for (col = 0, p = lineout; col < 7; col++) {
-			int xd = days[row * 7 + col];
+		for (col = 0, p = lineout; col < DAYS_IN_WEEK; col++) {
+			int xd = days[row * DAYS_IN_WEEK + col];
 			if (xd != SPACE && (xd & TODAY_FLAG))
 				has_hl = 1;
 			p = ascii_day(p, xd);
@@ -521,13 +541,13 @@ monthly3(int day, int month, int year) {
 	int next_month, next_year;
 
 	if (month == 1) {
-		prev_month = 12;
+		prev_month = MONTHS_IN_YEAR;
 		prev_year  = year - 1;
 	} else {
 		prev_month = month - 1;
 		prev_year  = year;
 	}
-	if (month == 12) {
+	if (month == MONTHS_IN_YEAR) {
 		next_month = 1;
 		next_year  = year + 1;
 	} else {
@@ -567,29 +587,29 @@ monthly3(int day, int month, int year) {
 void
 j_yearly(int day, int year) {
 	int col, *dp, i, month, row, which_cal;
-	int days[12][MAXDAYS];
+	int days[MONTHS_IN_YEAR][MAXDAYS];
 	char *p, lineout[80];
 
 	snprintf(lineout, sizeof(lineout), "%d", year);
 	center(lineout, J_WEEK_LEN*2 + J_HEAD_SEP - 1, 0);
 	my_putstring("\n\n");
 
-	for (i = 0; i < 12; i++)
+	for (i = 0; i < MONTHS_IN_YEAR; i++)
 		day_array(day, i + 1, year, days[i]);
 	memset(lineout, ' ', sizeof(lineout) - 1);
 	lineout[sizeof(lineout) - 1] = '\0';
-	for (month = 0; month < 12; month += 2) {
+	for (month = 0; month < MONTHS_IN_YEAR; month += 2) {
 		center(full_month[month], J_WEEK_LEN-1, J_HEAD_SEP+1);
 		center(full_month[month + 1], J_WEEK_LEN-1, 0);
 		snprintf(lineout, sizeof(lineout),
 			    "\n%s%*s %s\n", j_day_headings, J_HEAD_SEP, "",
 			    j_day_headings);
 		my_putstring(lineout);
-		for (row = 0; row < 6; row++) {
+		for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
 			p = lineout;
 			for (which_cal = 0; which_cal < 2; which_cal++) {
-				dp = &days[month + which_cal][row * 7];
-				for (col = 0; col < 7; col++)
+				dp = &days[month + which_cal][row * DAYS_IN_WEEK];
+				for (col = 0; col < DAYS_IN_WEEK; col++)
 					p = ascii_day(p, *dp++);
 				p += sprintf(p, "  ");
 			}
@@ -605,18 +625,18 @@ j_yearly(int day, int year) {
 void
 yearly(int day, int year) {
 	int col, *dp, i, month, row, which_cal;
-	int days[12][MAXDAYS];
+	int days[MONTHS_IN_YEAR][MAXDAYS];
 	char *p, lineout[100];
 
 	snprintf(lineout, sizeof(lineout), "%d", year);
 	center(lineout, WEEK_LEN*3 + HEAD_SEP*2 - 1, 0);
 	my_putstring("\n\n");
 
-	for (i = 0; i < 12; i++)
+	for (i = 0; i < MONTHS_IN_YEAR; i++)
 		day_array(day, i + 1, year, days[i]);
 	memset(lineout, ' ', sizeof(lineout) - 1);
 	lineout[sizeof(lineout) - 1] = '\0';
-	for (month = 0; month < 12; month += 3) {
+	for (month = 0; month < MONTHS_IN_YEAR; month += 3) {
 		center(full_month[month], WEEK_LEN-1, HEAD_SEP+1);
 		center(full_month[month + 1], WEEK_LEN-1, HEAD_SEP+1);
 		center(full_month[month + 2], WEEK_LEN-1, 0);
@@ -624,11 +644,11 @@ yearly(int day, int year) {
 			"\n%s%*s %s%*s %s\n", day_headings, HEAD_SEP,
 			"", day_headings, HEAD_SEP, "", day_headings);
 		my_putstring(lineout);
-		for (row = 0; row < 6; row++) {
+		for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
 			p = lineout;
 			for (which_cal = 0; which_cal < 3; which_cal++) {
-				dp = &days[month + which_cal][row * 7];
-				for (col = 0; col < 7; col++)
+				dp = &days[month + which_cal][row * DAYS_IN_WEEK];
+				for (col = 0; col < DAYS_IN_WEEK; col++)
 					p = ascii_day(p, *dp++);
 				p += sprintf(p, "  ");
 			}
@@ -653,7 +673,7 @@ day_array(int day, int month, int year, int *days) {
 	int julday, daynum, dw, dm;
 	int *d_sep1752;
 
-	if (month == 9 && year == 1752) {
+	if (month == REFORMATION_MONTH && year == REFORMATION_YEAR) {
 		int sep1752_ofs = (weekstart + SEP1752_OFS) % 7;
 		d_sep1752 = julian ? j_sep1752 : sep1752;
 		memcpy(days, d_sep1752 + sep1752_ofs, MAXDAYS * sizeof(int));
@@ -664,7 +684,7 @@ day_array(int day, int month, int year, int *days) {
 	}
 	memcpy(days, empty, MAXDAYS * sizeof(int));
 	dm = days_in_month[leap_year(year)][month];
-	dw = (day_in_week(1, month, year) - weekstart + 7) % 7;
+	dw = (day_in_week(1, month, year) - weekstart + DAYS_IN_WEEK) % DAYS_IN_WEEK;
 	julday = day_in_year(1, month, year);
 	daynum = julian ? julday : 1;
 	while (dm--) {
@@ -700,13 +720,15 @@ int
 day_in_week(int day, int month, int year) {
 	long temp;
 
-	temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
+	temp =
+	    (long)(year - SMALLEST_YEAR) * DAYS_IN_YEAR +
+	    leap_years_since_year_1(year - SMALLEST_YEAR)
 	    + day_in_year(day, month, year);
 	if (temp < FIRST_MISSING_DAY)
-		return ((temp - 1 + SATURDAY) % 7);
+		return ((temp - 1 + FIRST_WEEKDAY) % DAYS_IN_WEEK);
 	if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
-		return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
-	return (THURSDAY);
+		return (((temp - 1 + FIRST_WEEKDAY) - NUMBER_MISSING_DAYS) % DAYS_IN_WEEK);
+	return (REFORMATION_WEEKDAY);
 }
 
 char *
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 04/11] tests: add calendar reformation check
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (2 preceding siblings ...)
  2013-04-28 17:10 ` [PATCH 03/11] cal: clean up use of constants Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-28 17:11 ` [PATCH 05/11] cal: simplify calendar reformat calculations Sami Kerola
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 tests/expected/cal/sep1752 | 198 +++++++++++++++++++++++++++++++++++++++++++++
 tests/ts/cal/sep1752       |  95 ++++++++++++++++++++++
 2 files changed, 293 insertions(+)
 create mode 100644 tests/expected/cal/sep1752
 create mode 100755 tests/ts/cal/sep1752

diff --git a/tests/expected/cal/sep1752 b/tests/expected/cal/sep1752
new file mode 100644
index 0000000..b718fa0
--- /dev/null
+++ b/tests/expected/cal/sep1752
@@ -0,0 +1,198 @@
+
+Gregorian - Monday-based month
+   September 1752   
+Mo Tu We Th Fr Sa Su
+    1  2 14 15 16 17
+18 19 20 21 22 23 24
+25 26 27 28 29 30
+
+
+
+Gregorian - Sunday-based month
+   September 1752   
+Su Mo Tu We Th Fr Sa
+       1  2 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
+Julian - Monday-based month
+       September 1752      
+Mon Tue Wed Thu Fri Sat Sun
+    245 246 258 259 260 261
+262 263 264 265 266 267 268
+269 270 271 272 273 274
+
+
+
+Julian - Sunday-based month
+       September 1752      
+Sun Mon Tue Wed Thu Fri Sat
+        245 246 258 259 260
+261 262 263 264 265 266 267
+268 269 270 271 272 273 274
+
+
+
+Gregorian - Monday-based three months
+     August 1752         September 1752         October 1752    
+Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
+                1  2      1  2 14 15 16 17                     1
+ 3  4  5  6  7  8  9  18 19 20 21 22 23 24   2  3  4  5  6  7  8
+10 11 12 13 14 15 16  25 26 27 28 29 30      9 10 11 12 13 14 15
+17 18 19 20 21 22 23                        16 17 18 19 20 21 22
+24 25 26 27 28 29 30                        23 24 25 26 27 28 29
+31                                          30 31               
+Gregorian - Sunday-based three months
+     August 1752         September 1752         October 1752    
+Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
+                   1         1  2 14 15 16   1  2  3  4  5  6  7
+ 2  3  4  5  6  7  8  17 18 19 20 21 22 23   8  9 10 11 12 13 14
+ 9 10 11 12 13 14 15  24 25 26 27 28 29 30  15 16 17 18 19 20 21
+16 17 18 19 20 21 22                        22 23 24 25 26 27 28
+23 24 25 26 27 28 29                        29 30 31            
+30 31                                                           
+Julian - Monday-based three months
+        August 1752                 September 1752                October 1752       
+Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun
+                    214 215      245 246 258 259 260 261                          275
+216 217 218 219 220 221 222  262 263 264 265 266 267 268  276 277 278 279 280 281 282
+223 224 225 226 227 228 229  269 270 271 272 273 274      283 284 285 286 287 288 289
+230 231 232 233 234 235 236                               290 291 292 293 294 295 296
+237 238 239 240 241 242 243                               297 298 299 300 301 302 303
+244                                                       304 305                    
+Julian - Sunday-based three months
+        August 1752                 September 1752                October 1752       
+Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
+                        214          245 246 258 259 260  275 276 277 278 279 280 281
+215 216 217 218 219 220 221  261 262 263 264 265 266 267  282 283 284 285 286 287 288
+222 223 224 225 226 227 228  268 269 270 271 272 273 274  289 290 291 292 293 294 295
+229 230 231 232 233 234 235                               296 297 298 299 300 301 302
+236 237 238 239 240 241 242                               303 304 305                
+243 244                                                                              
+Gregorian - Monday-based year
+                               1752                               
+
+       January               February                 March       
+Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
+       1  2  3  4  5                   1  2                      1
+ 6  7  8  9 10 11 12    3  4  5  6  7  8  9    2  3  4  5  6  7  8
+13 14 15 16 17 18 19   10 11 12 13 14 15 16    9 10 11 12 13 14 15
+20 21 22 23 24 25 26   17 18 19 20 21 22 23   16 17 18 19 20 21 22
+27 28 29 30 31         24 25 26 27 28 29      23 24 25 26 27 28 29
+                                              30 31
+        April                   May                   June        
+Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
+       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
+ 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
+13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
+20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
+27 28 29 30            25 26 27 28 29 30 31   29 30
+
+        July                  August                September     
+Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
+       1  2  3  4  5                   1  2       1  2 14 15 16 17
+ 6  7  8  9 10 11 12    3  4  5  6  7  8  9   18 19 20 21 22 23 24
+13 14 15 16 17 18 19   10 11 12 13 14 15 16   25 26 27 28 29 30
+20 21 22 23 24 25 26   17 18 19 20 21 22 23
+27 28 29 30 31         24 25 26 27 28 29 30
+                       31
+       October               November               December      
+Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
+                   1          1  2  3  4  5                1  2  3
+ 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
+ 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
+16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
+23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
+30 31
+
+Gregorian - Sunday-based year
+                               1752                               
+
+       January               February                 March       
+Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
+          1  2  3  4                      1    1  2  3  4  5  6  7
+ 5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
+12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
+19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
+26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31
+
+        April                   May                   June        
+Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
+          1  2  3  4                   1  2       1  2  3  4  5  6
+ 5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
+12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
+19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
+26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
+                       31
+        July                  August                September     
+Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
+          1  2  3  4                      1          1  2 14 15 16
+ 5  6  7  8  9 10 11    2  3  4  5  6  7  8   17 18 19 20 21 22 23
+12 13 14 15 16 17 18    9 10 11 12 13 14 15   24 25 26 27 28 29 30
+19 20 21 22 23 24 25   16 17 18 19 20 21 22
+26 27 28 29 30 31      23 24 25 26 27 28 29
+                       30 31
+       October               November               December      
+Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
+ 1  2  3  4  5  6  7             1  2  3  4                   1  2
+ 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
+15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
+22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
+29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
+                                              31
+
+Julian - Monday-based year
+                           1752                          
+
+          January                       February         
+Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
+          1   2   3   4   5                        32  33
+  6   7   8   9  10  11  12    34  35  36  37  38  39  40
+ 13  14  15  16  17  18  19    41  42  43  44  45  46  47
+ 20  21  22  23  24  25  26    48  49  50  51  52  53  54
+ 27  28  29  30  31            55  56  57  58  59  60
+
+           March                         April           
+Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
+                         61            92  93  94  95  96
+ 62  63  64  65  66  67  68    97  98  99 100 101 102 103
+ 69  70  71  72  73  74  75   104 105 106 107 108 109 110
+ 76  77  78  79  80  81  82   111 112 113 114 115 116 117
+ 83  84  85  86  87  88  89   118 119 120 121
+ 90  91
+            May                           June           
+Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
+                122 123 124   153 154 155 156 157 158 159
+125 126 127 128 129 130 131   160 161 162 163 164 165 166
+132 133 134 135 136 137 138   167 168 169 170 171 172 173
+139 140 141 142 143 144 145   174 175 176 177 178 179 180
+146 147 148 149 150 151 152   181 182
+
+            July                         August          
+Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
+        183 184 185 186 187                       214 215
+188 189 190 191 192 193 194   216 217 218 219 220 221 222
+195 196 197 198 199 200 201   223 224 225 226 227 228 229
+202 203 204 205 206 207 208   230 231 232 233 234 235 236
+209 210 211 212 213           237 238 239 240 241 242 243
+                              244
+         September                      October          
+Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
+    245 246 258 259 260 261                           275
+262 263 264 265 266 267 268   276 277 278 279 280 281 282
+269 270 271 272 273 274       283 284 285 286 287 288 289
+                              290 291 292 293 294 295 296
+                              297 298 299 300 301 302 303
+                              304 305
+          November                      December         
+Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
+        306 307 308 309 310                   336 337 338
+311 312 313 314 315 316 317   339 340 341 342 343 344 345
+318 319 320 321 322 323 324   346 347 348 349 350 351 352
+325 326 327 328 329 330 331   353 354 355 356 357 358 359
+332 333 334 335               360 361 362 363 364 365 366
+
+
+Julian - Sunday-based year
diff --git a/tests/ts/cal/sep1752 b/tests/ts/cal/sep1752
new file mode 100755
index 0000000..68ea342
--- /dev/null
+++ b/tests/ts/cal/sep1752
@@ -0,0 +1,95 @@
+#!/bin/bash
+
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+TS_TOPDIR="$(dirname $0)/../.."
+TS_DESC="September 1752"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+set -o pipefail
+
+USETERM=$( ts_has_option "useterm" "$*" )
+MYMONTH="09 1752"
+MYYEAR="1752"
+
+[ "$USETERM" == "yes" ] && TS_VERBOSE="yes"
+ts_log ""
+
+
+ts_log "Gregorian - Monday-based month"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1m
+fi
+$TS_CMD_CAL -1m $MYMONTH >> $TS_OUTPUT
+ts_log "Gregorian - Sunday-based month"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1s $MYMONTH
+fi
+$TS_CMD_CAL -1s $MYMONTH >> $TS_OUTPUT
+ts_log "Julian - Monday-based month"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1mj $MYMONTH
+fi
+$TS_CMD_CAL -1mj $MYMONTH >> $TS_OUTPUT
+ts_log "Julian - Sunday-based month"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1sj $MYMONTH
+fi
+$TS_CMD_CAL -1sj $MYMONTH >> $TS_OUTPUT
+
+
+ts_log "Gregorian - Monday-based three months"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -3m
+fi
+$TS_CMD_CAL -3m $MYMONTH >> $TS_OUTPUT
+ts_log "Gregorian - Sunday-based three months"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -3s $MYMONTH
+fi
+$TS_CMD_CAL -3s $MYMONTH >> $TS_OUTPUT
+ts_log "Julian - Monday-based three months"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -3mj $MYMONTH
+fi
+$TS_CMD_CAL -3mj $MYMONTH >> $TS_OUTPUT
+ts_log "Julian - Sunday-based three months"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -3sj $MYMONTH
+fi
+$TS_CMD_CAL -3sj $MYMONTH >> $TS_OUTPUT
+
+
+ts_log "Gregorian - Monday-based year"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1m $MYYEAR
+fi
+$TS_CMD_CAL -1m $MYYEAR >> $TS_OUTPUT
+ts_log "Gregorian - Sunday-based year"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1s $MYYEAR
+fi
+$TS_CMD_CAL -1s $MYYEAR >> $TS_OUTPUT
+ts_log "Julian - Monday-based year"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1mj $MYYEAR
+fi
+$TS_CMD_CAL -1mj $MYYEAR >> $TS_OUTPUT
+ts_log "Julian - Sunday-based year"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL -1sj $MYYEAR
+fi
+
+ts_finalize
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 05/11] cal: simplify calendar reformat calculations
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (3 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 04/11] tests: add calendar reformation check Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-28 17:11 ` [PATCH 06/11] cal: remove unnecessary initializations Sami Kerola
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

The only September 1752 offset calculation that is necessary is whether
Sun or Mon is the first day of the week.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 41 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 26 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index f0d97af..8afa322 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -201,27 +201,16 @@ static int days_in_month[2][13] = {
 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
 };
 
-#define SEP1752_OFS		4		/* sep1752[4] is a Sunday */
-
-/* 1 Sep 1752 is represented by sep1752[6] and j_sep1752[6] */
-int sep1752[MAXDAYS+6] = {
-				SPACE,	SPACE,	SPACE,	SPACE,
+/* September 1752 is special, and has static assignments for both date
+ * and Julian representations.  */
+ int d_sep1752[MAXDAYS / 2] = {
 	SPACE,	SPACE,	1,	2,	14,	15,	16,
 	17,	18,	19,	20,	21,	22,	23,
-	24,	25,	26,	27,	28,	29,	30,
-	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
-	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
-	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
-	SPACE,	SPACE
-}, j_sep1752[MAXDAYS+6] = {
-				SPACE,	SPACE,	SPACE,	SPACE,
+	24,	25,	26,	27,	28,	29,	30
+}, j_sep1752[MAXDAYS / 2] = {
 	SPACE,	SPACE,	245,	246,	258,	259,	260,
 	261,	262,	263,	264,	265,	266,	267,
-	268,	269,	270,	271,	272,	273,	274,
-	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
-	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
-	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
-	SPACE,	SPACE
+	268,	269,	270,	271,	272,	273,	274
 }, empty[MAXDAYS] = {
 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
@@ -671,18 +660,18 @@ yearly(int day, int year) {
 void
 day_array(int day, int month, int year, int *days) {
 	int julday, daynum, dw, dm;
-	int *d_sep1752;
-
-	if (month == REFORMATION_MONTH && year == REFORMATION_YEAR) {
-		int sep1752_ofs = (weekstart + SEP1752_OFS) % 7;
-		d_sep1752 = julian ? j_sep1752 : sep1752;
-		memcpy(days, d_sep1752 + sep1752_ofs, MAXDAYS * sizeof(int));
-		for (dm=0; dm<MAXDAYS; dm++)
-			if (j_sep1752[dm + sep1752_ofs] == day)
+	int *sep1752;
+
+	memcpy(days, empty, MAXDAYS * sizeof(int));
+	if (year == REFORMATION_YEAR && month == REFORMATION_MONTH) {
+		sep1752 = julian ? j_sep1752 : d_sep1752;
+		memcpy(days, sep1752 + weekstart,
+		       ((MAXDAYS / 2) - weekstart) * sizeof(int));
+		for (dm = 0; dm < MAXDAYS / 2; dm++)
+			if (j_sep1752[dm] == day)
 				days[dm] |= TODAY_FLAG;
 		return;
 	}
-	memcpy(days, empty, MAXDAYS * sizeof(int));
 	dm = days_in_month[leap_year(year)][month];
 	dw = (day_in_week(1, month, year) - weekstart + DAYS_IN_WEEK) % DAYS_IN_WEEK;
 	julday = day_in_year(1, month, year);
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 06/11] cal: remove unnecessary initializations
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (4 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 05/11] cal: simplify calendar reformat calculations Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-28 17:11 ` [PATCH 07/11] cal: de-duplicate julian specific functions Sami Kerola
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 8afa322..3d87f40 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -438,9 +438,6 @@ void headers_init(void)
 	int i, wd;
 	char *cur_dh = day_headings, *cur_j_dh = j_day_headings;
 
-	strcpy(day_headings, "");
-	strcpy(j_day_headings, "");
-
 	for (i = 0; i < DAYS_IN_WEEK; i++) {
 		ssize_t space_left;
 		wd = (i + weekstart) % DAYS_IN_WEEK;
@@ -585,8 +582,6 @@ j_yearly(int day, int year) {
 
 	for (i = 0; i < MONTHS_IN_YEAR; i++)
 		day_array(day, i + 1, year, days[i]);
-	memset(lineout, ' ', sizeof(lineout) - 1);
-	lineout[sizeof(lineout) - 1] = '\0';
 	for (month = 0; month < MONTHS_IN_YEAR; month += 2) {
 		center(full_month[month], J_WEEK_LEN-1, J_HEAD_SEP+1);
 		center(full_month[month + 1], J_WEEK_LEN-1, 0);
@@ -623,8 +618,6 @@ yearly(int day, int year) {
 
 	for (i = 0; i < MONTHS_IN_YEAR; i++)
 		day_array(day, i + 1, year, days[i]);
-	memset(lineout, ' ', sizeof(lineout) - 1);
-	lineout[sizeof(lineout) - 1] = '\0';
 	for (month = 0; month < MONTHS_IN_YEAR; month += 3) {
 		center(full_month[month], WEEK_LEN-1, HEAD_SEP+1);
 		center(full_month[month + 1], WEEK_LEN-1, HEAD_SEP+1);
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 07/11] cal: de-duplicate julian specific functions
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (5 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 06/11] cal: remove unnecessary initializations Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-28 17:11 ` [PATCH 08/11] cal: allow day highlight testing Sami Kerola
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

The cost is a little bit more complex functions, while the benefit is
couple data flows fewer to mind about.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 119 +++++++++++++++++++++----------------------------------
 1 file changed, 45 insertions(+), 74 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 3d87f40..a7fd423 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -182,10 +182,12 @@ enum {
 #define	DAY_LEN			3		/* 3 spaces per day */
 #define	WEEK_LEN		(DAYS_IN_WEEK * DAY_LEN)
 #define	HEAD_SEP		2
+#define MONTH_COLS		3		/* month columns in year view */
 
 #define	J_DAY_LEN		4		/* 4 spaces per day */
 #define	J_WEEK_LEN		(DAYS_IN_WEEK * J_DAY_LEN)
 #define	J_HEAD_SEP		2
+#define J_MONTH_COLS		2
 
 #define TODAY_FLAG		0x400		/* flag day for highlighting */
 
@@ -222,9 +224,7 @@ static int days_in_month[2][13] = {
 
 
 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
-char day_headings[WEEK_LEN*6+1];
-/* weekstart = 1  =>   " M Tu  W Th  F  S  S " */
-char j_day_headings[J_WEEK_LEN*6+1];
+char day_headings[J_WEEK_LEN * 6 + 1];
 /* weekstart = 1  =>   "  M  Tu   W  Th   F   S   S " */
 const char *full_month[MONTHS_IN_YEAR];
 
@@ -242,14 +242,13 @@ void center(const char *, size_t, int);
 void day_array(int, int, int, int *);
 int day_in_week(int, int, int);
 int day_in_year(int, int, int);
-void yearly(int, int);
-void j_yearly(int, int);
+void yearly(int, int, int);
 void do_monthly(int, int, int, struct fmt_st*);
 void monthly(int, int, int);
 void monthly3(int, int, int);
 void trim_trailing_spaces(char *);
 static void __attribute__ ((__noreturn__)) usage(FILE * out);
-void headers_init(void);
+void headers_init(int);
 
 int
 main(int argc, char **argv) {
@@ -391,15 +390,13 @@ main(int argc, char **argv) {
 	default:
 		usage(stderr);
 	}
-	headers_init();
+	headers_init(julian);
 
 	if (!isatty(STDOUT_FILENO))
 		day = 0; /* don't highlight */
 
-	if (yflag && julian)
-		j_yearly(day, year);
-	else if (yflag)
-		yearly(day, year);
+	if (yflag)
+		yearly(day, year, julian);
 	else if (num_months == 1)
 		monthly(day, month, year);
 	else if (num_months == 3)
@@ -433,10 +430,10 @@ int leap_years_since_year_1(int year)
 		centuries_since_1700(year, 4));
 }
 
-void headers_init(void)
+void headers_init(int julian)
 {
-	int i, wd;
-	char *cur_dh = day_headings, *cur_j_dh = j_day_headings;
+	int i, wd, spaces = julian ? J_DAY_LEN - 1 : DAY_LEN - 1;
+	char *cur_dh = day_headings;
 
 	for (i = 0; i < DAYS_IN_WEEK; i++) {
 		ssize_t space_left;
@@ -446,21 +443,11 @@ void headers_init(void)
 			strcat(cur_dh++, " ");
 		space_left =
 		    sizeof(day_headings) - (cur_dh - day_headings);
-		if (space_left <= 2)
+		if (space_left <= spaces)
 			break;
 		cur_dh +=
 		    center_str(nl_langinfo(ABDAY_1 + wd), cur_dh,
-			       space_left, 2);
-
-		if (i)
-			strcat(cur_j_dh++, " ");
-		space_left =
-		    sizeof(j_day_headings) - (cur_j_dh - j_day_headings);
-		if (space_left <= 3)
-			break;
-		cur_j_dh +=
-		    center_str(nl_langinfo(ABDAY_1 + wd), cur_j_dh,
-			       space_left, 3);
+			       space_left, spaces);
 	}
 
 	for (i = 0; i < MONTHS_IN_YEAR; i++)
@@ -485,8 +472,7 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {
 			full_month[month - 1], year);
 	center_str(lineout, out->s[0], ARRAY_SIZE(out->s[0]), width);
 
-	snprintf(out->s[1], FMT_ST_CHARS, "%s",
-		julian ? j_day_headings : day_headings);
+	snprintf(out->s[1], FMT_ST_CHARS, "%s", day_headings);
 	for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
 		int has_hl = 0;
 		for (col = 0, p = lineout; col < DAYS_IN_WEEK; col++) {
@@ -571,64 +557,49 @@ monthly3(int day, int month, int year) {
 }
 
 void
-j_yearly(int day, int year) {
-	int col, *dp, i, month, row, which_cal;
-	int days[MONTHS_IN_YEAR][MAXDAYS];
-	char *p, lineout[80];
-
-	snprintf(lineout, sizeof(lineout), "%d", year);
-	center(lineout, J_WEEK_LEN*2 + J_HEAD_SEP - 1, 0);
-	my_putstring("\n\n");
-
-	for (i = 0; i < MONTHS_IN_YEAR; i++)
-		day_array(day, i + 1, year, days[i]);
-	for (month = 0; month < MONTHS_IN_YEAR; month += 2) {
-		center(full_month[month], J_WEEK_LEN-1, J_HEAD_SEP+1);
-		center(full_month[month + 1], J_WEEK_LEN-1, 0);
-		snprintf(lineout, sizeof(lineout),
-			    "\n%s%*s %s\n", j_day_headings, J_HEAD_SEP, "",
-			    j_day_headings);
-		my_putstring(lineout);
-		for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
-			p = lineout;
-			for (which_cal = 0; which_cal < 2; which_cal++) {
-				dp = &days[month + which_cal][row * DAYS_IN_WEEK];
-				for (col = 0; col < DAYS_IN_WEEK; col++)
-					p = ascii_day(p, *dp++);
-				p += sprintf(p, "  ");
-			}
-			*p = '\0';
-			trim_trailing_spaces(lineout);
-			my_putstring(lineout);
-			my_putstring("\n");
-		}
-	}
-	my_putstring("\n");
-}
-
-void
-yearly(int day, int year) {
+yearly(int day, int year, int julian) {
 	int col, *dp, i, month, row, which_cal;
+	int maxrow, sep_len, week_len;
 	int days[MONTHS_IN_YEAR][MAXDAYS];
 	char *p, lineout[100];
 
+	if (julian) {
+		maxrow = J_MONTH_COLS;
+		sep_len = J_HEAD_SEP;
+		week_len = J_WEEK_LEN;
+	} else {
+		maxrow = MONTH_COLS;
+		sep_len = HEAD_SEP;
+		week_len = WEEK_LEN;
+	}
 	snprintf(lineout, sizeof(lineout), "%d", year);
-	center(lineout, WEEK_LEN*3 + HEAD_SEP*2 - 1, 0);
+	/* 2013-04-28: The -1 near sep_len makes year header to be
+	 * aligned exactly how it has been aligned for long time, but it
+	 * is unexplainable.  */
+	center(lineout, (week_len + sep_len) * maxrow - sep_len - 1, 0);
 	my_putstring("\n\n");
 
 	for (i = 0; i < MONTHS_IN_YEAR; i++)
 		day_array(day, i + 1, year, days[i]);
-	for (month = 0; month < MONTHS_IN_YEAR; month += 3) {
-		center(full_month[month], WEEK_LEN-1, HEAD_SEP+1);
-		center(full_month[month + 1], WEEK_LEN-1, HEAD_SEP+1);
-		center(full_month[month + 2], WEEK_LEN-1, 0);
-		snprintf(lineout, sizeof(lineout),
-			"\n%s%*s %s%*s %s\n", day_headings, HEAD_SEP,
-			"", day_headings, HEAD_SEP, "", day_headings);
+	for (month = 0; month < MONTHS_IN_YEAR; month += maxrow) {
+		center(full_month[month], week_len - 1, sep_len + 1);
+		if (julian) {
+			center(full_month[month + 1], week_len - 1, 0);
+		} else {
+			center(full_month[month + 1], week_len - 1, sep_len + 1);
+			center(full_month[month + 2], week_len - 1, 0);
+		}
+		if (julian)
+			snprintf(lineout, sizeof(lineout),
+				 "\n%s%*s %s\n", day_headings, sep_len, "", day_headings);
+		else
+			snprintf(lineout, sizeof(lineout),
+				 "\n%s%*s %s%*s %s\n", day_headings, sep_len,
+				 "", day_headings, sep_len, "", day_headings);
 		my_putstring(lineout);
 		for (row = 0; row < DAYS_IN_WEEK - 1; row++) {
 			p = lineout;
-			for (which_cal = 0; which_cal < 3; which_cal++) {
+			for (which_cal = 0; which_cal < maxrow; which_cal++) {
 				dp = &days[month + which_cal][row * DAYS_IN_WEEK];
 				for (col = 0; col < DAYS_IN_WEEK; col++)
 					p = ascii_day(p, *dp++);
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 08/11] cal: allow day highlight testing
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (6 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 07/11] cal: de-duplicate julian specific functions Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-29  0:24   ` Pádraig Brady
  2013-04-28 17:11 ` [PATCH 09/11] tests: add cal day highlight corner cases Sami Kerola
                   ` (2 subsequent siblings)
  10 siblings, 1 reply; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Requires similar trick as done with more to by pass check whether a
session is or isn't interactive.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/Makemodule.am | 8 ++++++++
 misc-utils/cal.c         | 6 ++++++
 tests/commands.sh        | 2 +-
 tests/expected/cal/1m    | 8 ++++----
 tests/expected/cal/3m    | 8 ++++----
 tests/expected/cal/year  | 8 ++++----
 6 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am
index a615047..dcd55bf 100644
--- a/misc-utils/Makemodule.am
+++ b/misc-utils/Makemodule.am
@@ -12,14 +12,22 @@ endif
 
 cal_LDADD = $(LDADD)
 
+check_PROGRAMS += test_cal
+test_cal_SOURCES = $(cal_SOURCES)
+test_cal_CFLAGS = -DTEST_CAL_PROGRAM
+test_cal_LDADD = $(LDADD)
+
 if HAVE_TINFO
 cal_LDADD += -ltinfo @NCURSES_LIBS@
+test_cal_LDADD += -ltinfo @NCURSES_LIBS@
 else
 if HAVE_NCURSES
 cal_LDADD += @NCURSES_LIBS@
+test_cal_LDADD += @NCURSES_LIBS@
 else
 if HAVE_TERMCAP
 cal_LDADD += -ltermcap
+test_cal_LDADD += -ltermcap
 endif
 endif # !HAVE_NCURSES
 endif # !HAVE_TINFO
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index a7fd423..949f003 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -72,6 +72,10 @@
 #include "mbsalign.h"
 #include "strutils.h"
 
+#ifdef TEST_CAL_PROGRAM
+# define NON_INTERACTIVE_MORE 1
+#endif
+
 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
 # ifdef HAVE_NCURSES_H
 #  include <ncurses.h>
@@ -392,8 +396,10 @@ main(int argc, char **argv) {
 	}
 	headers_init(julian);
 
+#ifndef NON_INTERACTIVE_MORE
 	if (!isatty(STDOUT_FILENO))
 		day = 0; /* don't highlight */
+#endif
 
 	if (yflag)
 		yearly(day, year, julian);
diff --git a/tests/commands.sh b/tests/commands.sh
index d12c009..7848b10 100644
--- a/tests/commands.sh
+++ b/tests/commands.sh
@@ -25,7 +25,7 @@ TS_HELPER_SYSINFO="$top_builddir/test_sysinfo"
 TS_CMD_ADDPART=${TS_CMD_ADDPART:-"$top_builddir/addpart"}
 TS_CMD_DELPART=${TS_CMD_DELPART:-"$top_builddir/delpart"}
 TS_CMD_BLKID=${TS_CMD_BLKID-"$top_builddir/blkid"}
-TS_CMD_CAL=${TS_CMD_CAL-"$top_builddir/cal"}
+TS_CMD_CAL=${TS_CMD_CAL-"$top_builddir/test_cal"}
 TS_CMD_COLRM=${TS_CMD_COLRM:-"$top_builddir/colrm"}
 TS_CMD_COL=${TS_CMD_COL:-"$top_builddir/col"}
 TS_CMD_COLUMN=${TS_CMD_COLUMN:-"$top_builddir/column"}
diff --git a/tests/expected/cal/1m b/tests/expected/cal/1m
index f657875..1d95643 100644
--- a/tests/expected/cal/1m
+++ b/tests/expected/cal/1m
@@ -6,7 +6,7 @@ Mo Tu We Th Fr Sa Su
  4  5  6  7  8  9 10
 11 12 13 14 15 16 17
 18 19 20 21 22 23 24
-25 26 27 28 29 30
+25 26 ^[[7m27^[[27m 28 29 30
 
 Gregorian - Sunday-based week
    September 2006   
@@ -15,7 +15,7 @@ Su Mo Tu We Th Fr Sa
  3  4  5  6  7  8  9
 10 11 12 13 14 15 16
 17 18 19 20 21 22 23
-24 25 26 27 28 29 30
+24 25 26 ^[[7m27^[[27m 28 29 30
 
 Julian - Monday-based week
        September 2006      
@@ -24,7 +24,7 @@ Mon Tue Wed Thu Fri Sat Sun
 247 248 249 250 251 252 253
 254 255 256 257 258 259 260
 261 262 263 264 265 266 267
-268 269 270 271 272 273
+268 269 ^[[7m270^[[27m 271 272 273
 
 Julian - Sunday-based week
        September 2006      
@@ -33,5 +33,5 @@ Sun Mon Tue Wed Thu Fri Sat
 246 247 248 249 250 251 252
 253 254 255 256 257 258 259
 260 261 262 263 264 265 266
-267 268 269 270 271 272 273
+267 268 269 ^[[7m270^[[27m 271 272 273
 
diff --git a/tests/expected/cal/3m b/tests/expected/cal/3m
index ba9657d..724d521 100644
--- a/tests/expected/cal/3m
+++ b/tests/expected/cal/3m
@@ -6,7 +6,7 @@ Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
  7  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8
 14 15 16 17 18 19 20  11 12 13 14 15 16 17   9 10 11 12 13 14 15
 21 22 23 24 25 26 27  18 19 20 21 22 23 24  16 17 18 19 20 21 22
-28 29 30 31           25 26 27 28 29 30     23 24 25 26 27 28 29
+28 29 30 31           25 26 ^[[7m27^[[27m 28 29 30     23 24 25 26 27 28 29
                                             30 31               
 Gregorian - Sunday-based week
      August 2006         September 2006         October 2006    
@@ -15,7 +15,7 @@ Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
  6  7  8  9 10 11 12   3  4  5  6  7  8  9   8  9 10 11 12 13 14
 13 14 15 16 17 18 19  10 11 12 13 14 15 16  15 16 17 18 19 20 21
 20 21 22 23 24 25 26  17 18 19 20 21 22 23  22 23 24 25 26 27 28
-27 28 29 30 31        24 25 26 27 28 29 30  29 30 31            
+27 28 29 30 31        24 25 26 ^[[7m27^[[27m 28 29 30  29 30 31            
                                                                 
 Julian - Monday-based week
         August 2006                 September 2006                October 2006       
@@ -24,7 +24,7 @@ Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sa
 219 220 221 222 223 224 225  247 248 249 250 251 252 253  275 276 277 278 279 280 281
 226 227 228 229 230 231 232  254 255 256 257 258 259 260  282 283 284 285 286 287 288
 233 234 235 236 237 238 239  261 262 263 264 265 266 267  289 290 291 292 293 294 295
-240 241 242 243              268 269 270 271 272 273      296 297 298 299 300 301 302
+240 241 242 243              268 269 ^[[7m270^[[27m 271 272 273      296 297 298 299 300 301 302
                                                           303 304                    
 Julian - Sunday-based week
         August 2006                 September 2006                October 2006       
@@ -33,5 +33,5 @@ Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fr
 218 219 220 221 222 223 224  246 247 248 249 250 251 252  281 282 283 284 285 286 287
 225 226 227 228 229 230 231  253 254 255 256 257 258 259  288 289 290 291 292 293 294
 232 233 234 235 236 237 238  260 261 262 263 264 265 266  295 296 297 298 299 300 301
-239 240 241 242 243          267 268 269 270 271 272 273  302 303 304                
+239 240 241 242 243          267 268 269 ^[[7m270^[[27m 271 272 273  302 303 304                
                                                                                      
diff --git a/tests/expected/cal/year b/tests/expected/cal/year
index 616d61a..a79589d 100644
--- a/tests/expected/cal/year
+++ b/tests/expected/cal/year
@@ -32,7 +32,7 @@ Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
  2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
  9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
 16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
-23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
+23 24 25 26 27 28 29   27 28 ^[[7m29^[[27m 30            25 26 27 28 29 30 31
 30 31
 
 Gregorian - Sunday-based week
@@ -68,7 +68,7 @@ Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
  8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
 15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
 22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
-29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
+29 30 31               26 27 28 ^[[7m29^[[27m 30         24 25 26 27 28 29 30
                                               31
 
 Julian - Monday-based week
@@ -120,7 +120,7 @@ Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
 310 311 312 313 314 315 316   338 339 340 341 342 343 344
 317 318 319 320 321 322 323   345 346 347 348 349 350 351
 324 325 326 327 328 329 330   352 353 354 355 356 357 358
-331 332 333 334               359 360 361 362 363 364 365
+331 332 ^[[7m333^[[27m 334               359 360 361 362 363 364 365
 
 
 Julian - Sunday-based week
@@ -172,6 +172,6 @@ Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
 309 310 311 312 313 314 315   337 338 339 340 341 342 343
 316 317 318 319 320 321 322   344 345 346 347 348 349 350
 323 324 325 326 327 328 329   351 352 353 354 355 356 357
-330 331 332 333 334           358 359 360 361 362 363 364
+330 331 332 ^[[7m333^[[27m 334           358 359 360 361 362 363 364
                               365
 
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 09/11] tests: add cal day highlight corner cases
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (7 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 08/11] cal: allow day highlight testing Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-28 17:11 ` [PATCH 10/11] cal: trim three month display trailing white spaces Sami Kerola
  2013-04-28 17:11 ` [PATCH 11/11] cal: move global variables to local scope Sami Kerola
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Check the first, last, and strange Sep 1752 dates are highlighted
correctly.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 tests/expected/cal/highlight | 55 ++++++++++++++++++++++++++++++++++
 tests/ts/cal/highlight       | 70 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+)
 create mode 100644 tests/expected/cal/highlight
 create mode 100755 tests/ts/cal/highlight

diff --git a/tests/expected/cal/highlight b/tests/expected/cal/highlight
new file mode 100644
index 0000000..9e70d12
--- /dev/null
+++ b/tests/expected/cal/highlight
@@ -0,0 +1,55 @@
+
+First day
+      January 1     
+Su Mo Tu We Th Fr Sa
+                  ^[[7m 1^[[27m
+ 2  3  4  5  6  7  8
+ 9 10 11 12 13 14 15
+16 17 18 19 20 21 22
+23 24 25 26 27 28 29
+30 31
+Reformation corner cases 1
+   September 1752   
+Su Mo Tu We Th Fr Sa
+       1 ^[[7m 2^[[27m 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
+Reformation corner cases 2
+   September 1752   
+Su Mo Tu We Th Fr Sa
+       1  2 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
+Reformation corner cases 3
+   September 1752   
+Su Mo Tu We Th Fr Sa
+       1  2 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
+Reformation corner cases 4
+   September 1752   
+Su Mo Tu We Th Fr Sa
+       1  2 ^[[7m14^[[27m 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
+Last day
+    December 9999   
+Su Mo Tu We Th Fr Sa
+          1  2  3  4
+ 5  6  7  8  9 10 11
+12 13 14 15 16 17 18
+19 20 21 22 23 24 25
+26 27 28 29 30 ^[[7m31^[[27m
+
diff --git a/tests/ts/cal/highlight b/tests/ts/cal/highlight
new file mode 100755
index 0000000..9532ee7
--- /dev/null
+++ b/tests/ts/cal/highlight
@@ -0,0 +1,70 @@
+#!/bin/bash
+
+#
+# Copyright (C) 2007 Karel Zak <kzak@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+
+TS_TOPDIR="$(dirname $0)/../.."
+TS_DESC="highlights"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+set -o pipefail
+
+USETERM=$( ts_has_option "useterm" "$*" )
+
+[ "$USETERM" == "yes" ] && TS_VERBOSE="yes"
+ts_log ""
+
+ts_log "First day"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL 1 1 1
+fi
+$TS_CMD_CAL 1 1 1 >> $TS_OUTPUT
+
+ts_log "Reformation corner cases 1"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL 2 9 1752
+fi
+$TS_CMD_CAL 2 9 1752 >> $TS_OUTPUT
+
+ts_log "Reformation corner cases 2"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL 3 9 1752
+fi
+$TS_CMD_CAL 3 9 1752 >> $TS_OUTPUT
+
+ts_log "Reformation corner cases 3"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL 13 9 1752
+fi
+$TS_CMD_CAL 13 9 1752 >> $TS_OUTPUT
+
+ts_log "Reformation corner cases 4"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL 14 9 1752
+fi
+$TS_CMD_CAL 14 9 1752 >> $TS_OUTPUT
+
+ts_log "Last day"
+if [ "$USETERM" == "yes" ]; then
+	$TS_CMD_CAL 31 12 9999
+fi
+$TS_CMD_CAL 31 12 9999 >> $TS_OUTPUT
+
+
+ts_finalize
+
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 10/11] cal: trim three month display trailing white spaces
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (8 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 09/11] tests: add cal day highlight corner cases Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  2013-04-29  0:32   ` Pádraig Brady
  2013-04-28 17:11 ` [PATCH 11/11] cal: move global variables to local scope Sami Kerola
  10 siblings, 1 reply; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Includes fix to checks that conform earlier output practise.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c           | 11 +++++++----
 tests/expected/cal/3m      | 20 ++++++++++----------
 tests/expected/cal/sep1752 | 20 ++++++++++----------
 3 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 949f003..a03cd3c 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -540,8 +540,10 @@ monthly3(int day, int month, int year) {
 	width = (julian ? J_WEEK_LEN : WEEK_LEN) -1;
 	for (i = 0; i < 2; i++) {
 		snprintf(lineout, sizeof(lineout),
-			"%s  %s  %s\n", out_prev.s[i], out_curm.s[i], out_next.s[i]);
+			"%s  %s  %s", out_prev.s[i], out_curm.s[i], out_next.s[i]);
+		trim_trailing_spaces(lineout);
 		my_putstring(lineout);
+		my_putstring("\n");
 	}
 	for (i = 2; i < FMT_ST_LINES; i++) {
 		int w1, w2, w3;
@@ -553,12 +555,13 @@ monthly3(int day, int month, int year) {
 		w2 += (out_curm.s[i] == Hrow ? Slen : 0);
 		w3 += (out_next.s[i] == Hrow ? Slen : 0);
 #endif
-		snprintf(lineout, sizeof(lineout), "%-*s  %-*s  %-*s\n",
+		snprintf(lineout, sizeof(lineout), "%-*s  %-*s  %s",
 		       w1, out_prev.s[i],
 		       w2, out_curm.s[i],
-		       w3, out_next.s[i]);
-
+		       out_next.s[i]);
+		trim_trailing_spaces(lineout);
 		my_putstring(lineout);
+		my_putstring("\n");
 	}
 }
 
diff --git a/tests/expected/cal/3m b/tests/expected/cal/3m
index 724d521..a7bdca4 100644
--- a/tests/expected/cal/3m
+++ b/tests/expected/cal/3m
@@ -1,37 +1,37 @@
 
 Gregorian - Monday-based week
-     August 2006         September 2006         October 2006    
+     August 2006         September 2006         October 2006
 Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
     1  2  3  4  5  6               1  2  3                     1
  7  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8
 14 15 16 17 18 19 20  11 12 13 14 15 16 17   9 10 11 12 13 14 15
 21 22 23 24 25 26 27  18 19 20 21 22 23 24  16 17 18 19 20 21 22
 28 29 30 31           25 26 ^[[7m27^[[27m 28 29 30     23 24 25 26 27 28 29
-                                            30 31               
+                                            30 31
 Gregorian - Sunday-based week
-     August 2006         September 2006         October 2006    
+     August 2006         September 2006         October 2006
 Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
        1  2  3  4  5                  1  2   1  2  3  4  5  6  7
  6  7  8  9 10 11 12   3  4  5  6  7  8  9   8  9 10 11 12 13 14
 13 14 15 16 17 18 19  10 11 12 13 14 15 16  15 16 17 18 19 20 21
 20 21 22 23 24 25 26  17 18 19 20 21 22 23  22 23 24 25 26 27 28
-27 28 29 30 31        24 25 26 ^[[7m27^[[27m 28 29 30  29 30 31            
-                                                                
+27 28 29 30 31        24 25 26 ^[[7m27^[[27m 28 29 30  29 30 31
+
 Julian - Monday-based week
-        August 2006                 September 2006                October 2006       
+        August 2006                 September 2006                October 2006
 Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun
     213 214 215 216 217 218                  244 245 246                          274
 219 220 221 222 223 224 225  247 248 249 250 251 252 253  275 276 277 278 279 280 281
 226 227 228 229 230 231 232  254 255 256 257 258 259 260  282 283 284 285 286 287 288
 233 234 235 236 237 238 239  261 262 263 264 265 266 267  289 290 291 292 293 294 295
 240 241 242 243              268 269 ^[[7m270^[[27m 271 272 273      296 297 298 299 300 301 302
-                                                          303 304                    
+                                                          303 304
 Julian - Sunday-based week
-        August 2006                 September 2006                October 2006       
+        August 2006                 September 2006                October 2006
 Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
         213 214 215 216 217                      244 245  274 275 276 277 278 279 280
 218 219 220 221 222 223 224  246 247 248 249 250 251 252  281 282 283 284 285 286 287
 225 226 227 228 229 230 231  253 254 255 256 257 258 259  288 289 290 291 292 293 294
 232 233 234 235 236 237 238  260 261 262 263 264 265 266  295 296 297 298 299 300 301
-239 240 241 242 243          267 268 269 ^[[7m270^[[27m 271 272 273  302 303 304                
-                                                                                     
+239 240 241 242 243          267 268 269 ^[[7m270^[[27m 271 272 273  302 303 304
+
diff --git a/tests/expected/cal/sep1752 b/tests/expected/cal/sep1752
index b718fa0..283983d 100644
--- a/tests/expected/cal/sep1752
+++ b/tests/expected/cal/sep1752
@@ -36,41 +36,41 @@ Sun Mon Tue Wed Thu Fri Sat
 
 
 Gregorian - Monday-based three months
-     August 1752         September 1752         October 1752    
+     August 1752         September 1752         October 1752
 Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
                 1  2      1  2 14 15 16 17                     1
  3  4  5  6  7  8  9  18 19 20 21 22 23 24   2  3  4  5  6  7  8
 10 11 12 13 14 15 16  25 26 27 28 29 30      9 10 11 12 13 14 15
 17 18 19 20 21 22 23                        16 17 18 19 20 21 22
 24 25 26 27 28 29 30                        23 24 25 26 27 28 29
-31                                          30 31               
+31                                          30 31
 Gregorian - Sunday-based three months
-     August 1752         September 1752         October 1752    
+     August 1752         September 1752         October 1752
 Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
                    1         1  2 14 15 16   1  2  3  4  5  6  7
  2  3  4  5  6  7  8  17 18 19 20 21 22 23   8  9 10 11 12 13 14
  9 10 11 12 13 14 15  24 25 26 27 28 29 30  15 16 17 18 19 20 21
 16 17 18 19 20 21 22                        22 23 24 25 26 27 28
-23 24 25 26 27 28 29                        29 30 31            
-30 31                                                           
+23 24 25 26 27 28 29                        29 30 31
+30 31
 Julian - Monday-based three months
-        August 1752                 September 1752                October 1752       
+        August 1752                 September 1752                October 1752
 Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun
                     214 215      245 246 258 259 260 261                          275
 216 217 218 219 220 221 222  262 263 264 265 266 267 268  276 277 278 279 280 281 282
 223 224 225 226 227 228 229  269 270 271 272 273 274      283 284 285 286 287 288 289
 230 231 232 233 234 235 236                               290 291 292 293 294 295 296
 237 238 239 240 241 242 243                               297 298 299 300 301 302 303
-244                                                       304 305                    
+244                                                       304 305
 Julian - Sunday-based three months
-        August 1752                 September 1752                October 1752       
+        August 1752                 September 1752                October 1752
 Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat
                         214          245 246 258 259 260  275 276 277 278 279 280 281
 215 216 217 218 219 220 221  261 262 263 264 265 266 267  282 283 284 285 286 287 288
 222 223 224 225 226 227 228  268 269 270 271 272 273 274  289 290 291 292 293 294 295
 229 230 231 232 233 234 235                               296 297 298 299 300 301 302
-236 237 238 239 240 241 242                               303 304 305                
-243 244                                                                              
+236 237 238 239 240 241 242                               303 304 305
+243 244
 Gregorian - Monday-based year
                                1752                               
 
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 11/11] cal: move global variables to local scope
  2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
                   ` (9 preceding siblings ...)
  2013-04-28 17:11 ` [PATCH 10/11] cal: trim three month display trailing white spaces Sami Kerola
@ 2013-04-28 17:11 ` Sami Kerola
  10 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 17:11 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Also include small change to function parameter coding style.  The braces
are from function line to new line, and return value is in same line with
the function.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 109 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 55 insertions(+), 54 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index a03cd3c..4151ea0 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -227,39 +227,37 @@ static int days_in_month[2][13] = {
 };
 
 
-/* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
-char day_headings[J_WEEK_LEN * 6 + 1];
-/* weekstart = 1  =>   "  M  Tu   W  Th   F   S   S " */
-const char *full_month[MONTHS_IN_YEAR];
-
-/* 0 => sunday, 1 => monday */
-int weekstart = SUNDAY;
-int julian;
 
 /* function prototypes */
 int leap_year(int year);
 int centuries_since_1700(int year, int centuries);
 int leap_years_since_year_1(int year);
-char * ascii_day(char *, int);
+char * ascii_day(const int, char *, int);
 int center_str(const char* src, char* dest, size_t dest_size, size_t width);
 void center(const char *, size_t, int);
-void day_array(int, int, int, int *);
+void day_array(const int, const int, int, int, int, int *);
 int day_in_week(int, int, int);
 int day_in_year(int, int, int);
-void yearly(int, int, int);
-void do_monthly(int, int, int, struct fmt_st*);
-void monthly(int, int, int);
-void monthly3(int, int, int);
+void yearly(int, int, const int, const int, const char *, const char **);
+void do_monthly(const int, const int, const char *, const char **, int, int, int, struct fmt_st*);
+void monthly(const int, const int, const char *, const char **, int, int, int);
+void monthly3(const int, const int, const char *, const char **, int, int, int);
 void trim_trailing_spaces(char *);
 static void __attribute__ ((__noreturn__)) usage(FILE * out);
-void headers_init(int);
+void headers_init(const int, const int, char *, const char **);
 
-int
-main(int argc, char **argv) {
+int main(int argc, char **argv)
+{
 	struct tm *local_time;
 	time_t now;
 	int ch, day = 0, month = 0, year = 0, yflag = 0;
 	int num_months = NUM_MONTHS;
+	/* UTF-8 can have up to 6 bytes per char; and an extra byte for
+	 * ending \0 */
+	char day_headings[J_WEEK_LEN * 6 + 1];
+	const char *full_month[MONTHS_IN_YEAR];
+	int julian = 0;
+	int weekstart = SUNDAY;
 
 	static const struct option longopts[] = {
 		{"one", no_argument, NULL, '1'},
@@ -394,7 +392,7 @@ main(int argc, char **argv) {
 	default:
 		usage(stderr);
 	}
-	headers_init(julian);
+	headers_init(julian, weekstart, day_headings, full_month);
 
 #ifndef NON_INTERACTIVE_MORE
 	if (!isatty(STDOUT_FILENO))
@@ -402,11 +400,11 @@ main(int argc, char **argv) {
 #endif
 
 	if (yflag)
-		yearly(day, year, julian);
+		yearly(day, year, julian, weekstart, day_headings, full_month);
 	else if (num_months == 1)
-		monthly(day, month, year);
+		monthly(julian, weekstart, day_headings, full_month, day, month, year);
 	else if (num_months == 3)
-		monthly3(day, month, year);
+		monthly3(julian, weekstart, day_headings, full_month, day, month, year);
 
 	return EXIT_SUCCESS;
 }
@@ -436,7 +434,8 @@ int leap_years_since_year_1(int year)
 		centuries_since_1700(year, 4));
 }
 
-void headers_init(int julian)
+void headers_init(const int julian, const int weekstart, char *day_headings,
+		  const char **full_month)
 {
 	int i, wd, spaces = julian ? J_DAY_LEN - 1 : DAY_LEN - 1;
 	char *cur_dh = day_headings;
@@ -447,8 +446,7 @@ void headers_init(int julian)
 
 		if (i)
 			strcat(cur_dh++, " ");
-		space_left =
-		    sizeof(day_headings) - (cur_dh - day_headings);
+		space_left = (J_WEEK_LEN * 6 + 1) - (cur_dh - day_headings);
 		if (space_left <= spaces)
 			break;
 		cur_dh +=
@@ -460,13 +458,15 @@ void headers_init(int julian)
 		full_month[i] = nl_langinfo(MON_1 + i);
 }
 
-void
-do_monthly(int day, int month, int year, struct fmt_st *out) {
+void do_monthly(const int julian, const int weekstart, const char *day_headings,
+		const char **full_month, int day, int month, int year,
+		struct fmt_st *out)
+{
 	int col, row, days[MAXDAYS];
 	char *p, lineout[FMT_ST_CHARS];
 	int width = (julian ? J_WEEK_LEN : WEEK_LEN) - 1;
 
-	day_array(day, month, year, days);
+	day_array(julian, weekstart, day, month, year, days);
 
 	/*
 	 * %s is the month name, %d the year number.
@@ -485,7 +485,7 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {
 			int xd = days[row * DAYS_IN_WEEK + col];
 			if (xd != SPACE && (xd & TODAY_FLAG))
 				has_hl = 1;
-			p = ascii_day(p, xd);
+			p = ascii_day(julian, p, xd);
 		}
 		*p = '\0';
 		trim_trailing_spaces(lineout);
@@ -495,20 +495,22 @@ do_monthly(int day, int month, int year, struct fmt_st *out) {
 	}
 }
 
-void
-monthly(int day, int month, int year) {
+void monthly(const int julian, const int weekstart, const char *day_headings,
+	     const char **full_month, int day, int month, int year)
+{
 	int i;
 	struct fmt_st out;
 
-	do_monthly(day, month, year, &out);
+	do_monthly(julian, weekstart, day_headings, full_month, day, month, year, &out);
 	for (i = 0; i < FMT_ST_LINES; i++) {
 		my_putstring(out.s[i]);
 		my_putstring("\n");
 	}
 }
 
-void
-monthly3(int day, int month, int year) {
+void monthly3(const int julian, const int weekstart, const char *day_headings,
+	      const char **full_month, int day, int month, int year)
+{
 	char lineout[FMT_ST_CHARS];
 	int i;
 	int width;
@@ -533,9 +535,9 @@ monthly3(int day, int month, int year) {
 		next_year  = year;
 	}
 
-	do_monthly(day, prev_month, prev_year, &out_prev);
-	do_monthly(day, month,      year,      &out_curm);
-	do_monthly(day, next_month, next_year, &out_next);
+	do_monthly(julian, weekstart, day_headings, full_month, day, prev_month, prev_year, &out_prev);
+	do_monthly(julian, weekstart, day_headings, full_month, day, month,      year,      &out_curm);
+	do_monthly(julian, weekstart, day_headings, full_month, day, next_month, next_year, &out_next);
 
 	width = (julian ? J_WEEK_LEN : WEEK_LEN) -1;
 	for (i = 0; i < 2; i++) {
@@ -565,8 +567,9 @@ monthly3(int day, int month, int year) {
 	}
 }
 
-void
-yearly(int day, int year, int julian) {
+void yearly(int day, int year, const int julian, const int weekstart,
+	    const char *day_headings, const char **full_month)
+{
 	int col, *dp, i, month, row, which_cal;
 	int maxrow, sep_len, week_len;
 	int days[MONTHS_IN_YEAR][MAXDAYS];
@@ -589,7 +592,7 @@ yearly(int day, int year, int julian) {
 	my_putstring("\n\n");
 
 	for (i = 0; i < MONTHS_IN_YEAR; i++)
-		day_array(day, i + 1, year, days[i]);
+		day_array(julian, weekstart,  day, i + 1, year, days[i]);
 	for (month = 0; month < MONTHS_IN_YEAR; month += maxrow) {
 		center(full_month[month], week_len - 1, sep_len + 1);
 		if (julian) {
@@ -611,7 +614,7 @@ yearly(int day, int year, int julian) {
 			for (which_cal = 0; which_cal < maxrow; which_cal++) {
 				dp = &days[month + which_cal][row * DAYS_IN_WEEK];
 				for (col = 0; col < DAYS_IN_WEEK; col++)
-					p = ascii_day(p, *dp++);
+					p = ascii_day(julian, p, *dp++);
 				p += sprintf(p, "  ");
 			}
 			*p = '\0';
@@ -630,8 +633,9 @@ yearly(int day, int year, int julian) {
  *	out end to end.  You would have 42 numbers or spaces.  This routine
  *	builds that array for any month from Jan. 1 through Dec. 9999.
  */
-void
-day_array(int day, int month, int year, int *days) {
+void day_array(const int julian, const int weekstart, int day, int month,
+	       int year, int *days)
+{
 	int julday, daynum, dw, dm;
 	int *sep1752;
 
@@ -661,8 +665,8 @@ day_array(int day, int month, int year, int *days) {
  * day_in_year --
  *	return the 1 based day number within the year
  */
-int
-day_in_year(int day, int month, int year) {
+int day_in_year(int day, int month, int year)
+{
 	int i, leap;
 
 	leap = leap_year(year);
@@ -678,8 +682,8 @@ day_in_year(int day, int month, int year) {
  *	3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
  *	missing days.
  */
-int
-day_in_week(int day, int month, int year) {
+int day_in_week(int day, int month, int year)
+{
 	long temp;
 
 	temp =
@@ -693,8 +697,8 @@ day_in_week(int day, int month, int year) {
 	return (REFORMATION_WEEKDAY);
 }
 
-char *
-ascii_day(char *p, int day) {
+char *ascii_day(const int julian, char *p, int day)
+{
 	int display, val;
 	int highlight = 0;
 	static char *aday[] = {
@@ -741,8 +745,7 @@ ascii_day(char *p, int day) {
 	return p;
 }
 
-void
-trim_trailing_spaces(char *s)
+void trim_trailing_spaces(char *s)
 {
 	char *p;
 
@@ -760,15 +763,13 @@ trim_trailing_spaces(char *s)
  * In addition if the string is too large for the width it's truncated.
  * The number of trailing spaces may be 1 less than the number of leading spaces.
  */
-int
-center_str(const char* src, char* dest, size_t dest_size, size_t width)
+int center_str(const char *src, char *dest, size_t dest_size, size_t width)
 {
 	return mbsalign(src, dest, dest_size, &width,
 			MBS_ALIGN_CENTER, MBA_UNIBYTE_FALLBACK);
 }
 
-void
-center(const char *str, size_t len, int separate)
+void center(const char *str, size_t len, int separate)
 {
 	char lineout[FMT_ST_CHARS];
 
-- 
1.8.2.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH 02/11] cal: convert function like definitions to functions
  2013-04-28 17:10 ` [PATCH 02/11] cal: convert function like definitions to functions Sami Kerola
@ 2013-04-28 17:28   ` Dave Reisner
  2013-04-28 20:20     ` Sami Kerola
  2013-04-29 15:21   ` Thomas Bächler
  1 sibling, 1 reply; 26+ messages in thread
From: Dave Reisner @ 2013-04-28 17:28 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On Sun, Apr 28, 2013 at 06:10:58PM +0100, Sami Kerola wrote:
> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---

I agree with the change, but the new functions (all functions here,
really) should be marked as static.

>  misc-utils/cal.c | 45 ++++++++++++++++++++++++++++-----------------
>  1 file changed, 28 insertions(+), 17 deletions(-)
> 
> diff --git a/misc-utils/cal.c b/misc-utils/cal.c
> index 13830de..eacf3dd 100644
> --- a/misc-utils/cal.c
> +++ b/misc-utils/cal.c
> @@ -209,23 +209,6 @@ char j_day_headings[J_WEEK_LEN*6+1];
>  /* weekstart = 1  =>   "  M  Tu   W  Th   F   S   S " */
>  const char *full_month[12];
>  
> -/* leap year -- account for gregorian reformation in 1752 */
> -#define	leap_year(yr) \
> -	((yr) <= 1752 ? !((yr) % 4) : \
> -	(!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
> -
> -/* number of centuries since 1700, not inclusive */
> -#define	centuries_since_1700(yr) \
> -	((yr) > 1700 ? (yr) / 100 - 17 : 0)
> -
> -/* number of centuries since 1700 whose modulo of 400 is 0 */
> -#define	quad_centuries_since_1700(yr) \
> -	((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
> -
> -/* number of leap years between year 1 and this year, not inclusive */
> -#define	leap_years_since_year_1(yr) \
> -	((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
> -
>  /* 0 => sunday, 1 => monday */
>  int weekstart=0;
>  int julian;
> @@ -239,6 +222,9 @@ struct fmt_st
>    char s[FMT_ST_LINES][FMT_ST_CHARS];
>  };
>  
> +int leap_year(int year);
> +int centuries_since_1700(int year, int centuries);
> +int leap_years_since_year_1(int year);
>  char * ascii_day(char *, int);
>  int center_str(const char* src, char* dest, size_t dest_size, size_t width);
>  void center(const char *, size_t, int);
> @@ -413,6 +399,31 @@ main(int argc, char **argv) {
>  	return EXIT_SUCCESS;
>  }
>  
> +/* leap year -- account for gregorian reformation in 1752 */
> +int leap_year(int year)
> +{
> +	if (year <= 1752)
> +		return !(year % 4);
> +	else
> +		return !(year % 4 && (year % 100 || year % 400));
> +}
> +
> +/* number of centuries since 1700 */
> +int centuries_since_1700(int year, int n)
> +{
> +	if (year < 1700)
> +		return 0;
> +	else
> +		return ((year / (100 * n)) - (17 / n));
> +}
> +
> +/* number of leap years between year 1 and this year, not inclusive */
> +int leap_years_since_year_1(int year)
> +{
> +	return (year / 4 - centuries_since_1700(year, 1) +
> +		centuries_since_1700(year, 4));
> +}
> +
>  void headers_init(void)
>  {
>  	int i, wd;
> -- 
> 1.8.2.2
> 
> --
> To unsubscribe from this list: send the line "unsubscribe util-linux" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 02/11] cal: convert function like definitions to functions
  2013-04-28 17:28   ` Dave Reisner
@ 2013-04-28 20:20     ` Sami Kerola
  0 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-28 20:20 UTC (permalink / raw)
  To: Sami Kerola, util-linux

On 28 April 2013 18:28, Dave Reisner <d@falconindy.com> wrote:
> I agree with the change, but the new functions (all functions here,
> really) should be marked as static.

Thank you for review Dave.

Good point, new version of that patch is below and pushed to my git. I
will shortly submit a change which makes all functions static, and
should be applied on top of the earlier patches.


>From 68f869cc0d258cfe978368fcc084d46901b8ed3d Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 27 Apr 2013 22:51:35 +0100
Subject: [PATCH 02/12] cal: convert function like definitions to functions
Organization: Lastminute.com

Reviewed-by: Dave Reisner <dreisner@archlinux.org>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 45 ++++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 13830de..0c8cffc 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -209,23 +209,6 @@ char j_day_headings[J_WEEK_LEN*6+1];
 /* weekstart = 1  =>   "  M  Tu   W  Th   F   S   S " */
 const char *full_month[12];

-/* leap year -- account for gregorian reformation in 1752 */
-#define leap_year(yr) \
- ((yr) <= 1752 ? !((yr) % 4) : \
- (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
-
-/* number of centuries since 1700, not inclusive */
-#define centuries_since_1700(yr) \
- ((yr) > 1700 ? (yr) / 100 - 17 : 0)
-
-/* number of centuries since 1700 whose modulo of 400 is 0 */
-#define quad_centuries_since_1700(yr) \
- ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
-
-/* number of leap years between year 1 and this year, not inclusive */
-#define leap_years_since_year_1(yr) \
- ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
-
 /* 0 => sunday, 1 => monday */
 int weekstart=0;
 int julian;
@@ -239,6 +222,9 @@ struct fmt_st
   char s[FMT_ST_LINES][FMT_ST_CHARS];
 };

+int leap_year(int year);
+int centuries_since_1700(int year, int centuries);
+int leap_years_since_year_1(int year);
 char * ascii_day(char *, int);
 int center_str(const char* src, char* dest, size_t dest_size, size_t width);
 void center(const char *, size_t, int);
@@ -413,6 +399,31 @@ main(int argc, char **argv) {
  return EXIT_SUCCESS;
 }

+/* leap year -- account for gregorian reformation in 1752 */
+static int leap_year(int year)
+{
+ if (year <= 1752)
+ return !(year % 4);
+ else
+ return !(year % 4 && (year % 100 || year % 400));
+}
+
+/* number of centuries since 1700 */
+static int centuries_since_1700(int year, int n)
+{
+ if (year < 1700)
+ return 0;
+ else
+ return ((year / (100 * n)) - (17 / n));
+}
+
+/* number of leap years between year 1 and this year, not inclusive */
+static int leap_years_since_year_1(int year)
+{
+ return (year / 4 - centuries_since_1700(year, 1) +
+ centuries_since_1700(year, 4));
+}
+
 void headers_init(void)
 {
  int i, wd;
--
1.8.2.2


--
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH 08/11] cal: allow day highlight testing
  2013-04-28 17:11 ` [PATCH 08/11] cal: allow day highlight testing Sami Kerola
@ 2013-04-29  0:24   ` Pádraig Brady
  2013-04-29  8:59     ` Sami Kerola
  0 siblings, 1 reply; 26+ messages in thread
From: Pádraig Brady @ 2013-04-29  0:24 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On 04/28/2013 06:11 PM, Sami Kerola wrote:
> Requires similar trick as done with more to by pass check whether a
> session is or isn't interactive.

I wonder should we have an option or env variable to force highlighting?
I've wanted a couple of times to pipe output like:
  cal --color=always | ansi2html.sh

cheers,
Pádraig.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 10/11] cal: trim three month display trailing white spaces
  2013-04-28 17:11 ` [PATCH 10/11] cal: trim three month display trailing white spaces Sami Kerola
@ 2013-04-29  0:32   ` Pádraig Brady
  2013-04-29  9:11     ` Sami Kerola
  0 siblings, 1 reply; 26+ messages in thread
From: Pádraig Brady @ 2013-04-29  0:32 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On 04/28/2013 06:11 PM, Sami Kerola wrote:
> Includes fix to checks that conform earlier output practise.

I'm not sure about this.
Other output modes have trailing whitespace.
Also consider the case where you're outputting to a <pre> in a web page,
where the pre has a different bg color to the web page.
You'd want the full width output in that case.

thanks,
Pádraig.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 08/11] cal: allow day highlight testing
  2013-04-29  0:24   ` Pádraig Brady
@ 2013-04-29  8:59     ` Sami Kerola
  2013-04-29  9:58       ` Karel Zak
  0 siblings, 1 reply; 26+ messages in thread
From: Sami Kerola @ 2013-04-29  8:59 UTC (permalink / raw)
  To: Pádraig Brady; +Cc: util-linux

On 29 April 2013 01:24, Pádraig Brady <P@draigbrady.com> wrote:
> On 04/28/2013 06:11 PM, Sami Kerola wrote:
>> Requires similar trick as done with more to by pass check whether a
>> session is or isn't interactive.
>
> I wonder should we have an option or env variable to force highlighting?
> I've wanted a couple of times to pipe output like:
>   cal --color=always | ansi2html.sh

Hi Padraig,

Your proposal makes totally sense. Karel, please wait a moment (till
tomorrow) that I have a change to rewrite this change to be more
useful. I'll send new patch to the list, and update my git sometime
off-work-hour.

--
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 10/11] cal: trim three month display trailing white spaces
  2013-04-29  0:32   ` Pádraig Brady
@ 2013-04-29  9:11     ` Sami Kerola
  2013-04-29 13:40       ` Pádraig Brady
  0 siblings, 1 reply; 26+ messages in thread
From: Sami Kerola @ 2013-04-29  9:11 UTC (permalink / raw)
  To: Pádraig Brady; +Cc: util-linux

On 29 April 2013 01:32, Pádraig Brady <P@draigbrady.com> wrote:
> On 04/28/2013 06:11 PM, Sami Kerola wrote:
>> Includes fix to checks that conform earlier output practise.
>
> I'm not sure about this.
> Other output modes have trailing whitespace.
> Also consider the case where you're outputting to a <pre> in a web page,
> where the pre has a different bg color to the web page.
> You'd want the full width output in that case.

I did not realize someone might be doing that.  The reason why I wrote
the change is to be consistent with month and year print out, which
are already trimming spaces. Perhaps there should be
'--trim=[yes|no|auto]', where the 'auto' would be the current
practice, and default.

--
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 08/11] cal: allow day highlight testing
  2013-04-29  8:59     ` Sami Kerola
@ 2013-04-29  9:58       ` Karel Zak
  0 siblings, 0 replies; 26+ messages in thread
From: Karel Zak @ 2013-04-29  9:58 UTC (permalink / raw)
  To: kerolasa; +Cc: Pádraig Brady, util-linux

On Mon, Apr 29, 2013 at 09:59:15AM +0100, Sami Kerola wrote:
> On 29 April 2013 01:24, Pádraig Brady <P@draigbrady.com> wrote:
> > On 04/28/2013 06:11 PM, Sami Kerola wrote:
> >> Requires similar trick as done with more to by pass check whether a
> >> session is or isn't interactive.
> >
> > I wonder should we have an option or env variable to force highlighting?
> > I've wanted a couple of times to pipe output like:
> >   cal --color=always | ansi2html.sh

 yep, coreutils way:

   --color[=WHEN]

 where WHEN is 'auto', 'never' or 'always'. 
 
 Sami, see lib/colors.c. It would be nice to have WHEN evaluation
 (conversion to some enum value) on one place and use the code 
 in dmesg --color[=WHEN] too.

> Your proposal makes totally sense. Karel, please wait a moment (till
> tomorrow) that I have a change to rewrite this change to be more
> useful. I'll send new patch to the list, and update my git sometime
> off-work-hour.

 Maybe we can use more colors in the cal(1) output. What about a
 weekend days, month name, week days line etc. ?


 I personally believe that there is still a lot of places where we can
 improve command line usability and readability. (See for example
 "dmesg -H" in the latest release.)

    Karel
 

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 10/11] cal: trim three month display trailing white spaces
  2013-04-29  9:11     ` Sami Kerola
@ 2013-04-29 13:40       ` Pádraig Brady
  2013-04-29 18:01         ` Sami Kerola
  0 siblings, 1 reply; 26+ messages in thread
From: Pádraig Brady @ 2013-04-29 13:40 UTC (permalink / raw)
  To: kerolasa; +Cc: Sami Kerola, util-linux

On 04/29/2013 10:11 AM, Sami Kerola wrote:
> On 29 April 2013 01:32, Pádraig Brady <P@draigbrady.com> wrote:
>> On 04/28/2013 06:11 PM, Sami Kerola wrote:
>>> Includes fix to checks that conform earlier output practise.
>>
>> I'm not sure about this.
>> Other output modes have trailing whitespace.
>> Also consider the case where you're outputting to a <pre> in a web page,
>> where the pre has a different bg color to the web page.
>> You'd want the full width output in that case.
> 
> I did not realize someone might be doing that.  The reason why I wrote
> the change is to be consistent with month and year print out, which
> are already trimming spaces. 

I don't see month/year here with trimmed spaces?

$ cal -1 | head -n1 | tr ' ' .
.....April.2013.....

$ cal -y | head -n3 | tr ' ' .
...............................2013...............................

.......January...............February.................March.......


I do see that spaces are trimmed from the last day in -1 and -y formats.
So I suppose it's best to be consistent and trail space there.
I suppose one could also not trim spaces for -1 and -y there?

> Perhaps there should be
> '--trim=[yes|no|auto]', where the 'auto' would be the current
> practice, and default.

I don't think it needs an option TBH as removing trailing
spaces is trivial with: sed 's/ *$//'
Padding out with spaces is not trivial though,
which is another reason to err on the side of
leaving the trailing spaces.

cheers,
Pádraig.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 02/11] cal: convert function like definitions to functions
  2013-04-28 17:10 ` [PATCH 02/11] cal: convert function like definitions to functions Sami Kerola
  2013-04-28 17:28   ` Dave Reisner
@ 2013-04-29 15:21   ` Thomas Bächler
  2013-04-29 20:21     ` Sami Kerola
  1 sibling, 1 reply; 26+ messages in thread
From: Thomas Bächler @ 2013-04-29 15:21 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

[-- Attachment #1: Type: text/plain, Size: 634 bytes --]

Am 28.04.2013 19:10, schrieb Sami Kerola:
> -/* leap year -- account for gregorian reformation in 1752 */
> -#define	leap_year(yr) \
> -	((yr) <= 1752 ? !((yr) % 4) : \
> -	(!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
> [...] 
> +/* leap year -- account for gregorian reformation in 1752 */
> +int leap_year(int year)
> +{
> +	if (year <= 1752)
> +		return !(year % 4);
> +	else
> +		return !(year % 4 && (year % 100 || year % 400));
> +}

This formula is incorrect, there's a difference between the old version
and yours. The original (correct) version is:

( !(year % 4) && (year % 100) ) || !(year % 400)



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 901 bytes --]

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 03/11] cal: clean up use of constants
  2013-04-28 17:10 ` [PATCH 03/11] cal: clean up use of constants Sami Kerola
@ 2013-04-29 15:51   ` Thomas Bächler
  2013-04-29 21:05     ` Sami Kerola
  0 siblings, 1 reply; 26+ messages in thread
From: Thomas Bächler @ 2013-04-29 15:51 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux-ng

[-- Attachment #1: Type: text/plain, Size: 316 bytes --]

Am 28.04.2013 19:10, schrieb Sami Kerola:
> +#define	FIRST_WEEKDAY		SATURDAY	/* Jan 1st, 1 was a Saturday */
> +#define	REFORMATION_WEEKDAY	TUESDAY		/* after reformation it was Thursday */

The constant says Tuesday, the comment says Thursday. You should maybe
double-check the rest of the constants, too.



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 901 bytes --]

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 10/11] cal: trim three month display trailing white spaces
  2013-04-29 13:40       ` Pádraig Brady
@ 2013-04-29 18:01         ` Sami Kerola
  2013-05-01  0:54           ` Pádraig Brady
  0 siblings, 1 reply; 26+ messages in thread
From: Sami Kerola @ 2013-04-29 18:01 UTC (permalink / raw)
  To: Pádraig Brady; +Cc: util-linux

On 29 April 2013 14:40, Pádraig Brady <P@draigbrady.com> wrote:
> On 04/29/2013 10:11 AM, Sami Kerola wrote:
>> On 29 April 2013 01:32, Pádraig Brady <P@draigbrady.com> wrote:
>>> On 04/28/2013 06:11 PM, Sami Kerola wrote:
>>>> Includes fix to checks that conform earlier output practise.
>>>
>>> I'm not sure about this.
>>> Other output modes have trailing whitespace.
>>> Also consider the case where you're outputting to a <pre> in a web page,
>>> where the pre has a different bg color to the web page.
>>> You'd want the full width output in that case.
>>
>> I did not realize someone might be doing that.  The reason why I wrote
>> the change is to be consistent with month and year print out, which
>> are already trimming spaces.
>
> I don't see month/year here with trimmed spaces?
>
> $ cal -1 | head -n1 | tr ' ' .
> .....April.2013.....
>
> $ cal -y | head -n3 | tr ' ' .
> ...............................2013...............................
>
> .......January...............February.................March.......
>
> I do see that spaces are trimmed from the last day in -1 and -y formats.
> So I suppose it's best to be consistent and trail space there.
> I suppose one could also not trim spaces for -1 and -y there?

Headers are using mbsalign() function to align center, which also does
printing. That printing is not filtered through trim_trailing_spaces()
like the calendar's numeric output.

>> Perhaps there should be
>> '--trim=[yes|no|auto]', where the 'auto' would be the current
>> practice, and default.
>
> I don't think it needs an option TBH as removing trailing
> spaces is trivial with: sed 's/ *$//'
> Padding out with spaces is not trivial though,
> which is another reason to err on the side of
> leaving the trailing spaces.

True. Perhaps dropping the trimming all together is correct thing to
do. The following demonstrates what not trimming would mean for code
and tests.


commit b5f91fdd9f92512ebebe43a1560635c5fcfbf3c3
Author: Sami Kerola <kerolasa@iki.fi>
Date:   Sun Apr 28 15:35:26 2013 +0100

    cal: stop trimming whitespaces

    Remove trailing spaces from output it is trivial

    cal | sed 's/ *$//'

    but padding them back is difficult.

    CC: Pádraig Brady <P@draigbrady.com>
    Signed-off-by: Sami Kerola <kerolasa@iki.fi>

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 094dfa3..1fcd68c 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -250,7 +250,6 @@ void yearly(int, int, int);
 void do_monthly(int, int, int, struct fmt_st*);
 void monthly(int, int, int);
 void monthly3(int, int, int);
-void trim_trailing_spaces(char *);
 static void __attribute__ ((__noreturn__)) usage(FILE * out);
 void headers_init(int);

@@ -488,7 +487,6 @@ do_monthly(int day, int month, int year, struct
fmt_st *out) {
  p = ascii_day(p, xd);
  }
  *p = '\0';
- trim_trailing_spaces(lineout);
  snprintf(out->s[row+2], FMT_ST_CHARS, "%s", lineout);
  if (has_hl)
  Hrow = out->s[row+2];
@@ -612,7 +610,6 @@ yearly(int day, int year, int julian) {
  p += sprintf(p, "  ");
  }
  *p = '\0';
- trim_trailing_spaces(lineout);
  my_putstring(lineout);
  my_putstring("\n");
  }
@@ -738,20 +735,6 @@ ascii_day(char *p, int day) {
  return p;
 }

-void
-trim_trailing_spaces(char *s)
-{
- char *p;
-
- for (p = s; *p; ++p)
- continue;
- while (p > s && isspace(*--p))
- continue;
- if (p > s)
- ++p;
- *p = '\0';
-}
-
 /*
  * Center string, handling multibyte characters appropriately.
  * In addition if the string is too large for the width it's truncated.
diff --git a/tests/expected/cal/1m b/tests/expected/cal/1m
index 1d95643..7af98e1 100644
--- a/tests/expected/cal/1m
+++ b/tests/expected/cal/1m
@@ -2,36 +2,36 @@
 Gregorian - Monday-based week
    September 2006
 Mo Tu We Th Fr Sa Su
-             1  2  3
- 4  5  6  7  8  9 10
-11 12 13 14 15 16 17
-18 19 20 21 22 23 24
-25 26 ^[[7m27^[[27m 28 29 30
-
+             1  2  3
+ 4  5  6  7  8  9 10
+11 12 13 14 15 16 17
+18 19 20 21 22 23 24
+25 26 ^[[7m27^[[27m 28 29 30
+
 Gregorian - Sunday-based week
    September 2006
 Su Mo Tu We Th Fr Sa
-                1  2
- 3  4  5  6  7  8  9
-10 11 12 13 14 15 16
-17 18 19 20 21 22 23
-24 25 26 ^[[7m27^[[27m 28 29 30
-
+                1  2
+ 3  4  5  6  7  8  9
+10 11 12 13 14 15 16
+17 18 19 20 21 22 23
+24 25 26 ^[[7m27^[[27m 28 29 30
+
 Julian - Monday-based week
        September 2006
 Mon Tue Wed Thu Fri Sat Sun
-                244 245 246
-247 248 249 250 251 252 253
-254 255 256 257 258 259 260
-261 262 263 264 265 266 267
-268 269 ^[[7m270^[[27m 271 272 273
-
+                244 245 246
+247 248 249 250 251 252 253
+254 255 256 257 258 259 260
+261 262 263 264 265 266 267
+268 269 ^[[7m270^[[27m 271 272 273
+
 Julian - Sunday-based week
        September 2006
 Sun Mon Tue Wed Thu Fri Sat
-                    244 245
-246 247 248 249 250 251 252
-253 254 255 256 257 258 259
-260 261 262 263 264 265 266
-267 268 269 ^[[7m270^[[27m 271 272 273
-
+                    244 245
+246 247 248 249 250 251 252
+253 254 255 256 257 258 259
+260 261 262 263 264 265 266
+267 268 269 ^[[7m270^[[27m 271 272 273
+
diff --git a/tests/expected/cal/3m b/tests/expected/cal/3m
index 724d521..f13d3b0 100644
--- a/tests/expected/cal/3m
+++ b/tests/expected/cal/3m
@@ -2,36 +2,36 @@
 Gregorian - Monday-based week
      August 2006         September 2006         October 2006
 Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
-    1  2  3  4  5  6               1  2  3                     1
- 7  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8
-14 15 16 17 18 19 20  11 12 13 14 15 16 17   9 10 11 12 13 14 15
-21 22 23 24 25 26 27  18 19 20 21 22 23 24  16 17 18 19 20 21 22
-28 29 30 31           25 26 ^[[7m27^[[27m 28 29 30     23 24 25 26 27 28 29
-                                            30 31
+    1  2  3  4  5  6                1  2  3                      1
+ 7  8  9 10 11 12 13    4  5  6  7  8  9 10    2  3  4  5  6  7  8
+14 15 16 17 18 19 20   11 12 13 14 15 16 17    9 10 11 12 13 14 15
+21 22 23 24 25 26 27   18 19 20 21 22 23 24   16 17 18 19 20 21 22
+28 29 30 31            25 26 ^[[7m27^[[27m 28 29 30      23 24 25 26 27 28 29
+                                              30 31
 Gregorian - Sunday-based week
      August 2006         September 2006         October 2006
 Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
-       1  2  3  4  5                  1  2   1  2  3  4  5  6  7
- 6  7  8  9 10 11 12   3  4  5  6  7  8  9   8  9 10 11 12 13 14
-13 14 15 16 17 18 19  10 11 12 13 14 15 16  15 16 17 18 19 20 21
-20 21 22 23 24 25 26  17 18 19 20 21 22 23  22 23 24 25 26 27 28
-27 28 29 30 31        24 25 26 ^[[7m27^[[27m 28 29 30  29 30 31
-
+       1  2  3  4  5                   1  2    1  2  3  4  5  6  7
+ 6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
+13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
+20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
+27 28 29 30 31         24 25 26 ^[[7m27^[[27m 28 29 30   29 30 31
+
 Julian - Monday-based week
         August 2006                 September 2006
October 2006
 Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed
Thu Fri Sat Sun
-    213 214 215 216 217 218                  244 245 246
            274
-219 220 221 222 223 224 225  247 248 249 250 251 252 253  275 276 277
278 279 280 281
-226 227 228 229 230 231 232  254 255 256 257 258 259 260  282 283 284
285 286 287 288
-233 234 235 236 237 238 239  261 262 263 264 265 266 267  289 290 291
292 293 294 295
-240 241 242 243              268 269 ^[[7m270^[[27m 271 272 273
296 297 298 299 300 301 302
-                                                          303 304
+    213 214 215 216 217 218                   244 245 246
              274
+219 220 221 222 223 224 225   247 248 249 250 251 252 253   275 276
277 278 279 280 281
+226 227 228 229 230 231 232   254 255 256 257 258 259 260   282 283
284 285 286 287 288
+233 234 235 236 237 238 239   261 262 263 264 265 266 267   289 290
291 292 293 294 295
+240 241 242 243               268 269 ^[[7m270^[[27m 271 272 273
296 297 298 299 300 301 302
+                                                            303 304
 Julian - Sunday-based week
         August 2006                 September 2006
October 2006
 Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue
Wed Thu Fri Sat
-        213 214 215 216 217                      244 245  274 275 276
277 278 279 280
-218 219 220 221 222 223 224  246 247 248 249 250 251 252  281 282 283
284 285 286 287
-225 226 227 228 229 230 231  253 254 255 256 257 258 259  288 289 290
291 292 293 294
-232 233 234 235 236 237 238  260 261 262 263 264 265 266  295 296 297
298 299 300 301
-239 240 241 242 243          267 268 269 ^[[7m270^[[27m 271 272 273
302 303 304
-
+        213 214 215 216 217                       244 245   274 275
276 277 278 279 280
+218 219 220 221 222 223 224   246 247 248 249 250 251 252   281 282
283 284 285 286 287
+225 226 227 228 229 230 231   253 254 255 256 257 258 259   288 289
290 291 292 293 294
+232 233 234 235 236 237 238   260 261 262 263 264 265 266   295 296
297 298 299 300 301
+239 240 241 242 243           267 268 269 ^[[7m270^[[27m 271 272 273
302 303 304
+
diff --git a/tests/expected/cal/highlight b/tests/expected/cal/highlight
index 9e70d12..a207840 100644
--- a/tests/expected/cal/highlight
+++ b/tests/expected/cal/highlight
@@ -2,54 +2,54 @@
 First day
       January 1
 Su Mo Tu We Th Fr Sa
-                  ^[[7m 1^[[27m
- 2  3  4  5  6  7  8
- 9 10 11 12 13 14 15
-16 17 18 19 20 21 22
-23 24 25 26 27 28 29
-30 31
+                  ^[[7m 1^[[27m
+ 2  3  4  5  6  7  8
+ 9 10 11 12 13 14 15
+16 17 18 19 20 21 22
+23 24 25 26 27 28 29
+30 31
 Reformation corner cases 1
    September 1752
 Su Mo Tu We Th Fr Sa
-       1 ^[[7m 2^[[27m 14 15 16
-17 18 19 20 21 22 23
-24 25 26 27 28 29 30
-
-
-
+       1 ^[[7m 2^[[27m 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
 Reformation corner cases 2
    September 1752
 Su Mo Tu We Th Fr Sa
-       1  2 14 15 16
-17 18 19 20 21 22 23
-24 25 26 27 28 29 30
-
-
-
+       1  2 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
 Reformation corner cases 3
    September 1752
 Su Mo Tu We Th Fr Sa
-       1  2 14 15 16
-17 18 19 20 21 22 23
-24 25 26 27 28 29 30
-
-
-
+       1  2 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
 Reformation corner cases 4
    September 1752
 Su Mo Tu We Th Fr Sa
-       1  2 ^[[7m14^[[27m 15 16
-17 18 19 20 21 22 23
-24 25 26 27 28 29 30
-
-
-
+       1  2 ^[[7m14^[[27m 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
 Last day
     December 9999
 Su Mo Tu We Th Fr Sa
-          1  2  3  4
- 5  6  7  8  9 10 11
-12 13 14 15 16 17 18
-19 20 21 22 23 24 25
-26 27 28 29 30 ^[[7m31^[[27m
-
+          1  2  3  4
+ 5  6  7  8  9 10 11
+12 13 14 15 16 17 18
+19 20 21 22 23 24 25
+26 27 28 29 30 ^[[7m31^[[27m
+
diff --git a/tests/expected/cal/sep1752 b/tests/expected/cal/sep1752
index b718fa0..0b12e49 100644
--- a/tests/expected/cal/sep1752
+++ b/tests/expected/cal/sep1752
@@ -2,197 +2,197 @@
 Gregorian - Monday-based month
    September 1752
 Mo Tu We Th Fr Sa Su
-    1  2 14 15 16 17
-18 19 20 21 22 23 24
-25 26 27 28 29 30
-
-
-
+    1  2 14 15 16 17
+18 19 20 21 22 23 24
+25 26 27 28 29 30
+
+
+
 Gregorian - Sunday-based month
    September 1752
 Su Mo Tu We Th Fr Sa
-       1  2 14 15 16
-17 18 19 20 21 22 23
-24 25 26 27 28 29 30
-
-
-
+       1  2 14 15 16
+17 18 19 20 21 22 23
+24 25 26 27 28 29 30
+
+
+
 Julian - Monday-based month
        September 1752
 Mon Tue Wed Thu Fri Sat Sun
-    245 246 258 259 260 261
-262 263 264 265 266 267 268
-269 270 271 272 273 274
-
-
-
+    245 246 258 259 260 261
+262 263 264 265 266 267 268
+269 270 271 272 273 274
+
+
+
 Julian - Sunday-based month
        September 1752
 Sun Mon Tue Wed Thu Fri Sat
-        245 246 258 259 260
-261 262 263 264 265 266 267
-268 269 270 271 272 273 274
-
-
-
+        245 246 258 259 260
+261 262 263 264 265 266 267
+268 269 270 271 272 273 274
+
+
+
 Gregorian - Monday-based three months
      August 1752         September 1752         October 1752
 Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
-                1  2      1  2 14 15 16 17                     1
- 3  4  5  6  7  8  9  18 19 20 21 22 23 24   2  3  4  5  6  7  8
-10 11 12 13 14 15 16  25 26 27 28 29 30      9 10 11 12 13 14 15
-17 18 19 20 21 22 23                        16 17 18 19 20 21 22
-24 25 26 27 28 29 30                        23 24 25 26 27 28 29
-31                                          30 31
+                1  2       1  2 14 15 16 17                      1
+ 3  4  5  6  7  8  9   18 19 20 21 22 23 24    2  3  4  5  6  7  8
+10 11 12 13 14 15 16   25 26 27 28 29 30       9 10 11 12 13 14 15
+17 18 19 20 21 22 23                          16 17 18 19 20 21 22
+24 25 26 27 28 29 30                          23 24 25 26 27 28 29
+31                                            30 31
 Gregorian - Sunday-based three months
      August 1752         September 1752         October 1752
 Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
-                   1         1  2 14 15 16   1  2  3  4  5  6  7
- 2  3  4  5  6  7  8  17 18 19 20 21 22 23   8  9 10 11 12 13 14
- 9 10 11 12 13 14 15  24 25 26 27 28 29 30  15 16 17 18 19 20 21
-16 17 18 19 20 21 22                        22 23 24 25 26 27 28
-23 24 25 26 27 28 29                        29 30 31
-30 31
+                   1          1  2 14 15 16    1  2  3  4  5  6  7
+ 2  3  4  5  6  7  8   17 18 19 20 21 22 23    8  9 10 11 12 13 14
+ 9 10 11 12 13 14 15   24 25 26 27 28 29 30   15 16 17 18 19 20 21
+16 17 18 19 20 21 22                          22 23 24 25 26 27 28
+23 24 25 26 27 28 29                          29 30 31
+30 31
 Julian - Monday-based three months
         August 1752                 September 1752
October 1752
 Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed
Thu Fri Sat Sun
-                    214 215      245 246 258 259 260 261
            275
-216 217 218 219 220 221 222  262 263 264 265 266 267 268  276 277 278
279 280 281 282
-223 224 225 226 227 228 229  269 270 271 272 273 274      283 284 285
286 287 288 289
-230 231 232 233 234 235 236                               290 291 292
293 294 295 296
-237 238 239 240 241 242 243                               297 298 299
300 301 302 303
-244                                                       304 305
+                    214 215       245 246 258 259 260 261
              275
+216 217 218 219 220 221 222   262 263 264 265 266 267 268   276 277
278 279 280 281 282
+223 224 225 226 227 228 229   269 270 271 272 273 274       283 284
285 286 287 288 289
+230 231 232 233 234 235 236                                 290 291
292 293 294 295 296
+237 238 239 240 241 242 243                                 297 298
299 300 301 302 303
+244                                                         304 305
 Julian - Sunday-based three months
         August 1752                 September 1752
October 1752
 Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue Wed Thu Fri Sat  Sun Mon Tue
Wed Thu Fri Sat
-                        214          245 246 258 259 260  275 276 277
278 279 280 281
-215 216 217 218 219 220 221  261 262 263 264 265 266 267  282 283 284
285 286 287 288
-222 223 224 225 226 227 228  268 269 270 271 272 273 274  289 290 291
292 293 294 295
-229 230 231 232 233 234 235                               296 297 298
299 300 301 302
-236 237 238 239 240 241 242                               303 304 305
-243 244
+                        214           245 246 258 259 260   275 276
277 278 279 280 281
+215 216 217 218 219 220 221   261 262 263 264 265 266 267   282 283
284 285 286 287 288
+222 223 224 225 226 227 228   268 269 270 271 272 273 274   289 290
291 292 293 294 295
+229 230 231 232 233 234 235                                 296 297
298 299 300 301 302
+236 237 238 239 240 241 242                                 303 304
305
+243 244
 Gregorian - Monday-based year
                                1752

        January               February                 March
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-       1  2  3  4  5                   1  2                      1
- 6  7  8  9 10 11 12    3  4  5  6  7  8  9    2  3  4  5  6  7  8
-13 14 15 16 17 18 19   10 11 12 13 14 15 16    9 10 11 12 13 14 15
-20 21 22 23 24 25 26   17 18 19 20 21 22 23   16 17 18 19 20 21 22
-27 28 29 30 31         24 25 26 27 28 29      23 24 25 26 27 28 29
-                                              30 31
+       1  2  3  4  5                   1  2                      1
+ 6  7  8  9 10 11 12    3  4  5  6  7  8  9    2  3  4  5  6  7  8
+13 14 15 16 17 18 19   10 11 12 13 14 15 16    9 10 11 12 13 14 15
+20 21 22 23 24 25 26   17 18 19 20 21 22 23   16 17 18 19 20 21 22
+27 28 29 30 31         24 25 26 27 28 29      23 24 25 26 27 28 29
+                                              30 31
         April                   May                   June
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
- 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
-13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
-20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
-27 28 29 30            25 26 27 28 29 30 31   29 30
-
+       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
+ 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
+13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
+20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
+27 28 29 30            25 26 27 28 29 30 31   29 30
+
         July                  August                September
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-       1  2  3  4  5                   1  2       1  2 14 15 16 17
- 6  7  8  9 10 11 12    3  4  5  6  7  8  9   18 19 20 21 22 23 24
-13 14 15 16 17 18 19   10 11 12 13 14 15 16   25 26 27 28 29 30
-20 21 22 23 24 25 26   17 18 19 20 21 22 23
-27 28 29 30 31         24 25 26 27 28 29 30
-                       31
+       1  2  3  4  5                   1  2       1  2 14 15 16 17
+ 6  7  8  9 10 11 12    3  4  5  6  7  8  9   18 19 20 21 22 23 24
+13 14 15 16 17 18 19   10 11 12 13 14 15 16   25 26 27 28 29 30
+20 21 22 23 24 25 26   17 18 19 20 21 22 23
+27 28 29 30 31         24 25 26 27 28 29 30
+                       31
        October               November               December
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-                   1          1  2  3  4  5                1  2  3
- 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
- 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
-16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
-23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
-30 31
+                   1          1  2  3  4  5                1  2  3
+ 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
+ 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
+16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
+23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
+30 31

 Gregorian - Sunday-based year
                                1752

        January               February                 March
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
-          1  2  3  4                      1    1  2  3  4  5  6  7
- 5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
-12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
-19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
-26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31
-
+          1  2  3  4                      1    1  2  3  4  5  6  7
+ 5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
+12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
+19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
+26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31
+
         April                   May                   June
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
-          1  2  3  4                   1  2       1  2  3  4  5  6
- 5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
-12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
-19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
-26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
-                       31
+          1  2  3  4                   1  2       1  2  3  4  5  6
+ 5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
+12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
+19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
+26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
+                       31
         July                  August                September
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
-          1  2  3  4                      1          1  2 14 15 16
- 5  6  7  8  9 10 11    2  3  4  5  6  7  8   17 18 19 20 21 22 23
-12 13 14 15 16 17 18    9 10 11 12 13 14 15   24 25 26 27 28 29 30
-19 20 21 22 23 24 25   16 17 18 19 20 21 22
-26 27 28 29 30 31      23 24 25 26 27 28 29
-                       30 31
+          1  2  3  4                      1          1  2 14 15 16
+ 5  6  7  8  9 10 11    2  3  4  5  6  7  8   17 18 19 20 21 22 23
+12 13 14 15 16 17 18    9 10 11 12 13 14 15   24 25 26 27 28 29 30
+19 20 21 22 23 24 25   16 17 18 19 20 21 22
+26 27 28 29 30 31      23 24 25 26 27 28 29
+                       30 31
        October               November               December
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
- 1  2  3  4  5  6  7             1  2  3  4                   1  2
- 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
-15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
-22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
-29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
-                                              31
+ 1  2  3  4  5  6  7             1  2  3  4                   1  2
+ 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
+15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
+22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
+29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
+                                              31

 Julian - Monday-based year
                            1752

           January                       February
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-          1   2   3   4   5                        32  33
-  6   7   8   9  10  11  12    34  35  36  37  38  39  40
- 13  14  15  16  17  18  19    41  42  43  44  45  46  47
- 20  21  22  23  24  25  26    48  49  50  51  52  53  54
- 27  28  29  30  31            55  56  57  58  59  60
-
+          1   2   3   4   5                        32  33
+  6   7   8   9  10  11  12    34  35  36  37  38  39  40
+ 13  14  15  16  17  18  19    41  42  43  44  45  46  47
+ 20  21  22  23  24  25  26    48  49  50  51  52  53  54
+ 27  28  29  30  31            55  56  57  58  59  60
+
            March                         April
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-                         61            92  93  94  95  96
- 62  63  64  65  66  67  68    97  98  99 100 101 102 103
- 69  70  71  72  73  74  75   104 105 106 107 108 109 110
- 76  77  78  79  80  81  82   111 112 113 114 115 116 117
- 83  84  85  86  87  88  89   118 119 120 121
- 90  91
+                         61            92  93  94  95  96
+ 62  63  64  65  66  67  68    97  98  99 100 101 102 103
+ 69  70  71  72  73  74  75   104 105 106 107 108 109 110
+ 76  77  78  79  80  81  82   111 112 113 114 115 116 117
+ 83  84  85  86  87  88  89   118 119 120 121
+ 90  91
             May                           June
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-                122 123 124   153 154 155 156 157 158 159
-125 126 127 128 129 130 131   160 161 162 163 164 165 166
-132 133 134 135 136 137 138   167 168 169 170 171 172 173
-139 140 141 142 143 144 145   174 175 176 177 178 179 180
-146 147 148 149 150 151 152   181 182
-
+                122 123 124   153 154 155 156 157 158 159
+125 126 127 128 129 130 131   160 161 162 163 164 165 166
+132 133 134 135 136 137 138   167 168 169 170 171 172 173
+139 140 141 142 143 144 145   174 175 176 177 178 179 180
+146 147 148 149 150 151 152   181 182
+
             July                         August
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-        183 184 185 186 187                       214 215
-188 189 190 191 192 193 194   216 217 218 219 220 221 222
-195 196 197 198 199 200 201   223 224 225 226 227 228 229
-202 203 204 205 206 207 208   230 231 232 233 234 235 236
-209 210 211 212 213           237 238 239 240 241 242 243
-                              244
+        183 184 185 186 187                       214 215
+188 189 190 191 192 193 194   216 217 218 219 220 221 222
+195 196 197 198 199 200 201   223 224 225 226 227 228 229
+202 203 204 205 206 207 208   230 231 232 233 234 235 236
+209 210 211 212 213           237 238 239 240 241 242 243
+                              244
          September                      October
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-    245 246 258 259 260 261                           275
-262 263 264 265 266 267 268   276 277 278 279 280 281 282
-269 270 271 272 273 274       283 284 285 286 287 288 289
-                              290 291 292 293 294 295 296
-                              297 298 299 300 301 302 303
-                              304 305
+    245 246 258 259 260 261                           275
+262 263 264 265 266 267 268   276 277 278 279 280 281 282
+269 270 271 272 273 274       283 284 285 286 287 288 289
+                              290 291 292 293 294 295 296
+                              297 298 299 300 301 302 303
+                              304 305
           November                      December
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-        306 307 308 309 310                   336 337 338
-311 312 313 314 315 316 317   339 340 341 342 343 344 345
-318 319 320 321 322 323 324   346 347 348 349 350 351 352
-325 326 327 328 329 330 331   353 354 355 356 357 358 359
-332 333 334 335               360 361 362 363 364 365 366
-
+        306 307 308 309 310                   336 337 338
+311 312 313 314 315 316 317   339 340 341 342 343 344 345
+318 319 320 321 322 323 324   346 347 348 349 350 351 352
+325 326 327 328 329 330 331   353 354 355 356 357 358 359
+332 333 334 335               360 361 362 363 364 365 366
+

 Julian - Sunday-based year
diff --git a/tests/expected/cal/year b/tests/expected/cal/year
index a79589d..6f0ae5c 100644
--- a/tests/expected/cal/year
+++ b/tests/expected/cal/year
@@ -4,174 +4,174 @@ Gregorian - Monday-based week

        January               February                 March
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-                   1          1  2  3  4  5          1  2  3  4  5
- 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
- 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
-16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
-23 24 25 26 27 28 29   27 28                  27 28 29 30 31
-30 31
+                   1          1  2  3  4  5          1  2  3  4  5
+ 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
+ 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
+16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
+23 24 25 26 27 28 29   27 28                  27 28 29 30 31
+30 31
         April                   May                   June
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-                1  2    1  2  3  4  5  6  7             1  2  3  4
- 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
-10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
-17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
-24 25 26 27 28 29 30   29 30 31               26 27 28 29 30
-
+                1  2    1  2  3  4  5  6  7             1  2  3  4
+ 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
+10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
+17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
+24 25 26 27 28 29 30   29 30 31               26 27 28 29 30
+
         July                  August                September
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-                1  2       1  2  3  4  5  6                1  2  3
- 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
-10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
-17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
-24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
-31
+                1  2       1  2  3  4  5  6                1  2  3
+ 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
+10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
+17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
+24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
+31
        October               November               December
 Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
-                   1          1  2  3  4  5                1  2  3
- 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
- 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
-16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
-23 24 25 26 27 28 29   27 28 ^[[7m29^[[27m 30            25 26 27 28 29 30 31
-30 31
+                   1          1  2  3  4  5                1  2  3
+ 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
+ 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
+16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
+23 24 25 26 27 28 29   27 28 ^[[7m29^[[27m 30            25 26 27 28 29 30 31
+30 31

 Gregorian - Sunday-based week
                                2006

        January               February                 March
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
- 1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
- 8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
-15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
-22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
-29 30 31               26 27 28               26 27 28 29 30 31
-
+ 1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
+ 8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
+15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
+22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
+29 30 31               26 27 28               26 27 28 29 30 31
+
         April                   May                   June
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
-                   1       1  2  3  4  5  6                1  2  3
- 2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
- 9 10 11 12 13 14 15   14 15 16 17 18 19 20   11 12 13 14 15 16 17
-16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
-23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
-30
+                   1       1  2  3  4  5  6                1  2  3
+ 2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
+ 9 10 11 12 13 14 15   14 15 16 17 18 19 20   11 12 13 14 15 16 17
+16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
+23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
+30
         July                  August                September
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
-                   1          1  2  3  4  5                   1  2
- 2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
- 9 10 11 12 13 14 15   13 14 15 16 17 18 19   10 11 12 13 14 15 16
-16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
-23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
-30 31
+                   1          1  2  3  4  5                   1  2
+ 2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
+ 9 10 11 12 13 14 15   13 14 15 16 17 18 19   10 11 12 13 14 15 16
+16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
+23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
+30 31
        October               November               December
 Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
- 1  2  3  4  5  6  7             1  2  3  4                   1  2
- 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
-15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
-22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
-29 30 31               26 27 28 ^[[7m29^[[27m 30         24 25 26 27 28 29 30
-                                              31
+ 1  2  3  4  5  6  7             1  2  3  4                   1  2
+ 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
+15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
+22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
+29 30 31               26 27 28 ^[[7m29^[[27m 30         24 25 26 27 28 29 30
+                                              31

 Julian - Monday-based week
                            2006

           January                       February
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-                          1            32  33  34  35  36
-  2   3   4   5   6   7   8    37  38  39  40  41  42  43
-  9  10  11  12  13  14  15    44  45  46  47  48  49  50
- 16  17  18  19  20  21  22    51  52  53  54  55  56  57
- 23  24  25  26  27  28  29    58  59
- 30  31
+                          1            32  33  34  35  36
+  2   3   4   5   6   7   8    37  38  39  40  41  42  43
+  9  10  11  12  13  14  15    44  45  46  47  48  49  50
+ 16  17  18  19  20  21  22    51  52  53  54  55  56  57
+ 23  24  25  26  27  28  29    58  59
+ 30  31
            March                         April
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-         60  61  62  63  64                        91  92
- 65  66  67  68  69  70  71    93  94  95  96  97  98  99
- 72  73  74  75  76  77  78   100 101 102 103 104 105 106
- 79  80  81  82  83  84  85   107 108 109 110 111 112 113
- 86  87  88  89  90           114 115 116 117 118 119 120
-
+         60  61  62  63  64                        91  92
+ 65  66  67  68  69  70  71    93  94  95  96  97  98  99
+ 72  73  74  75  76  77  78   100 101 102 103 104 105 106
+ 79  80  81  82  83  84  85   107 108 109 110 111 112 113
+ 86  87  88  89  90           114 115 116 117 118 119 120
+
             May                           June
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-121 122 123 124 125 126 127               152 153 154 155
-128 129 130 131 132 133 134   156 157 158 159 160 161 162
-135 136 137 138 139 140 141   163 164 165 166 167 168 169
-142 143 144 145 146 147 148   170 171 172 173 174 175 176
-149 150 151                   177 178 179 180 181
-
+121 122 123 124 125 126 127               152 153 154 155
+128 129 130 131 132 133 134   156 157 158 159 160 161 162
+135 136 137 138 139 140 141   163 164 165 166 167 168 169
+142 143 144 145 146 147 148   170 171 172 173 174 175 176
+149 150 151                   177 178 179 180 181
+
             July                         August
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-                    182 183       213 214 215 216 217 218
-184 185 186 187 188 189 190   219 220 221 222 223 224 225
-191 192 193 194 195 196 197   226 227 228 229 230 231 232
-198 199 200 201 202 203 204   233 234 235 236 237 238 239
-205 206 207 208 209 210 211   240 241 242 243
-212
+                    182 183       213 214 215 216 217 218
+184 185 186 187 188 189 190   219 220 221 222 223 224 225
+191 192 193 194 195 196 197   226 227 228 229 230 231 232
+198 199 200 201 202 203 204   233 234 235 236 237 238 239
+205 206 207 208 209 210 211   240 241 242 243
+212
          September                      October
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-                244 245 246                           274
-247 248 249 250 251 252 253   275 276 277 278 279 280 281
-254 255 256 257 258 259 260   282 283 284 285 286 287 288
-261 262 263 264 265 266 267   289 290 291 292 293 294 295
-268 269 270 271 272 273       296 297 298 299 300 301 302
-                              303 304
+                244 245 246                           274
+247 248 249 250 251 252 253   275 276 277 278 279 280 281
+254 255 256 257 258 259 260   282 283 284 285 286 287 288
+261 262 263 264 265 266 267   289 290 291 292 293 294 295
+268 269 270 271 272 273       296 297 298 299 300 301 302
+                              303 304
           November                      December
 Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
-        305 306 307 308 309                   335 336 337
-310 311 312 313 314 315 316   338 339 340 341 342 343 344
-317 318 319 320 321 322 323   345 346 347 348 349 350 351
-324 325 326 327 328 329 330   352 353 354 355 356 357 358
-331 332 ^[[7m333^[[27m 334               359 360 361 362 363 364 365
-
+        305 306 307 308 309                   335 336 337
+310 311 312 313 314 315 316   338 339 340 341 342 343 344
+317 318 319 320 321 322 323   345 346 347 348 349 350 351
+324 325 326 327 328 329 330   352 353 354 355 356 357 358
+331 332 ^[[7m333^[[27m 334               359 360 361 362 363 364 365
+

 Julian - Sunday-based week
                            2006

           January                       February
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
-  1   2   3   4   5   6   7                32  33  34  35
-  8   9  10  11  12  13  14    36  37  38  39  40  41  42
- 15  16  17  18  19  20  21    43  44  45  46  47  48  49
- 22  23  24  25  26  27  28    50  51  52  53  54  55  56
- 29  30  31                    57  58  59
-
+  1   2   3   4   5   6   7                32  33  34  35
+  8   9  10  11  12  13  14    36  37  38  39  40  41  42
+ 15  16  17  18  19  20  21    43  44  45  46  47  48  49
+ 22  23  24  25  26  27  28    50  51  52  53  54  55  56
+ 29  30  31                    57  58  59
+
            March                         April
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
-             60  61  62  63                            91
- 64  65  66  67  68  69  70    92  93  94  95  96  97  98
- 71  72  73  74  75  76  77    99 100 101 102 103 104 105
- 78  79  80  81  82  83  84   106 107 108 109 110 111 112
- 85  86  87  88  89  90       113 114 115 116 117 118 119
-                              120
+             60  61  62  63                            91
+ 64  65  66  67  68  69  70    92  93  94  95  96  97  98
+ 71  72  73  74  75  76  77    99 100 101 102 103 104 105
+ 78  79  80  81  82  83  84   106 107 108 109 110 111 112
+ 85  86  87  88  89  90       113 114 115 116 117 118 119
+                              120
             May                           June
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
-    121 122 123 124 125 126                   152 153 154
-127 128 129 130 131 132 133   155 156 157 158 159 160 161
-134 135 136 137 138 139 140   162 163 164 165 166 167 168
-141 142 143 144 145 146 147   169 170 171 172 173 174 175
-148 149 150 151               176 177 178 179 180 181
-
+    121 122 123 124 125 126                   152 153 154
+127 128 129 130 131 132 133   155 156 157 158 159 160 161
+134 135 136 137 138 139 140   162 163 164 165 166 167 168
+141 142 143 144 145 146 147   169 170 171 172 173 174 175
+148 149 150 151               176 177 178 179 180 181
+
             July                         August
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
-                        182           213 214 215 216 217
-183 184 185 186 187 188 189   218 219 220 221 222 223 224
-190 191 192 193 194 195 196   225 226 227 228 229 230 231
-197 198 199 200 201 202 203   232 233 234 235 236 237 238
-204 205 206 207 208 209 210   239 240 241 242 243
-211 212
+                        182           213 214 215 216 217
+183 184 185 186 187 188 189   218 219 220 221 222 223 224
+190 191 192 193 194 195 196   225 226 227 228 229 230 231
+197 198 199 200 201 202 203   232 233 234 235 236 237 238
+204 205 206 207 208 209 210   239 240 241 242 243
+211 212
          September                      October
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
-                    244 245   274 275 276 277 278 279 280
-246 247 248 249 250 251 252   281 282 283 284 285 286 287
-253 254 255 256 257 258 259   288 289 290 291 292 293 294
-260 261 262 263 264 265 266   295 296 297 298 299 300 301
-267 268 269 270 271 272 273   302 303 304
-
+                    244 245   274 275 276 277 278 279 280
+246 247 248 249 250 251 252   281 282 283 284 285 286 287
+253 254 255 256 257 258 259   288 289 290 291 292 293 294
+260 261 262 263 264 265 266   295 296 297 298 299 300 301
+267 268 269 270 271 272 273   302 303 304
+
           November                      December
 Sun Mon Tue Wed Thu Fri Sat   Sun Mon Tue Wed Thu Fri Sat
-            305 306 307 308                       335 336
-309 310 311 312 313 314 315   337 338 339 340 341 342 343
-316 317 318 319 320 321 322   344 345 346 347 348 349 350
-323 324 325 326 327 328 329   351 352 353 354 355 356 357
-330 331 332 ^[[7m333^[[27m 334           358 359 360 361 362 363 364
-                              365
+            305 306 307 308                       335 336
+309 310 311 312 313 314 315   337 338 339 340 341 342 343
+316 317 318 319 320 321 322   344 345 346 347 348 349 350
+323 324 325 326 327 328 329   351 352 353 354 355 356 357
+330 331 332 ^[[7m333^[[27m 334           358 359 360 361 362 363 364
+                              365




--
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH 02/11] cal: convert function like definitions to functions
  2013-04-29 15:21   ` Thomas Bächler
@ 2013-04-29 20:21     ` Sami Kerola
  0 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-29 20:21 UTC (permalink / raw)
  To: Thomas Bächler; +Cc: Sami Kerola, util-linux

On 29 April 2013 16:21, Thomas Bächler <thomas@archlinux.org> wrote:
> This formula is incorrect, there's a difference between the old version
> and yours. The original (correct) version is:
>
> ( !(year % 4) && (year % 100) ) || !(year % 400)

Indeed. Your version is now part of my git, which I will send later
when all feedback is reflected in code.

--
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 03/11] cal: clean up use of constants
  2013-04-29 15:51   ` Thomas Bächler
@ 2013-04-29 21:05     ` Sami Kerola
  0 siblings, 0 replies; 26+ messages in thread
From: Sami Kerola @ 2013-04-29 21:05 UTC (permalink / raw)
  To: Thomas Bächler; +Cc: Sami Kerola, util-linux-ng

On 29 April 2013 16:51, Thomas Bächler <thomas@archlinux.org> wrote:
> Am 28.04.2013 19:10, schrieb Sami Kerola:
>> +#define      FIRST_WEEKDAY           SATURDAY        /* Jan 1st, 1 was a Saturday */
>> +#define      REFORMATION_WEEKDAY     TUESDAY         /* after reformation it was Thursday */
>
> The constant says Tuesday, the comment says Thursday. You should maybe
> double-check the rest of the constants, too.

Thanks Thomas, good catch... although probably not quite what you expected.

I started to wondering why test suite did not catch this. After gazing
code for moment I decided recompile with coverage turned on. Results
were very clear. Not a once the REFORMATION_WEEKDAY was returned. That
puzzled me for some while, until I realized the September 1752 are
never evaluated with day_in_week(), the day_array() is short cutting
that evaluation by setting the short month with memcpy().

>From the original patch REFORMATION_WEEKDAY is removed, and where that
value was returned there is now abort(). Additionally I think the
day_in_week() can be simplified, which I will do as a separated
change.

--
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 10/11] cal: trim three month display trailing white spaces
  2013-04-29 18:01         ` Sami Kerola
@ 2013-05-01  0:54           ` Pádraig Brady
  0 siblings, 0 replies; 26+ messages in thread
From: Pádraig Brady @ 2013-05-01  0:54 UTC (permalink / raw)
  To: kerolasa; +Cc: Sami Kerola, util-linux

On 04/29/2013 07:01 PM, Sami Kerola wrote:
> On 29 April 2013 14:40, Pádraig Brady <P@draigbrady.com> wrote:
>> On 04/29/2013 10:11 AM, Sami Kerola wrote:
>>> On 29 April 2013 01:32, Pádraig Brady <P@draigbrady.com> wrote:
>>>> On 04/28/2013 06:11 PM, Sami Kerola wrote:
>>>>> Includes fix to checks that conform earlier output practise.
>>>>
>>>> I'm not sure about this.
>>>> Other output modes have trailing whitespace.
>>>> Also consider the case where you're outputting to a <pre> in a web page,
>>>> where the pre has a different bg color to the web page.
>>>> You'd want the full width output in that case.
>>>
>>> I did not realize someone might be doing that.  The reason why I wrote
>>> the change is to be consistent with month and year print out, which
>>> are already trimming spaces.
>>
>> I don't see month/year here with trimmed spaces?
>>
>> $ cal -1 | head -n1 | tr ' ' .
>> .....April.2013.....
>>
>> $ cal -y | head -n3 | tr ' ' .
>> ...............................2013...............................
>>
>> .......January...............February.................March.......
>>
>> I do see that spaces are trimmed from the last day in -1 and -y formats.
>> So I suppose it's best to be consistent and trail space there.
>> I suppose one could also not trim spaces for -1 and -y there?
> 
> Headers are using mbsalign() function to align center, which also does
> printing. That printing is not filtered through trim_trailing_spaces()
> like the calendar's numeric output.

As an aside, the "upstream" for mbsalign() in coreutils
has been updated to support MBA_NO_RIGHT_PAD.  If we were to change
that behaviour, we would probably sync up first.
http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=6cf9c59b

> 
>>> Perhaps there should be
>>> '--trim=[yes|no|auto]', where the 'auto' would be the current
>>> practice, and default.
>>
>> I don't think it needs an option TBH as removing trailing
>> spaces is trivial with: sed 's/ *$//'
>> Padding out with spaces is not trivial though,
>> which is another reason to err on the side of
>> leaving the trailing spaces.
> 
> True. Perhaps dropping the trimming all together is correct thing to
> do. The following demonstrates what not trimming would mean for code
> and tests.

+1

thanks,
Pádraig.


^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2013-05-01  0:54 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-28 17:10 [PATCH 00/11] pull: cal(1) spring clean up Sami Kerola
2013-04-28 17:10 ` [PATCH 01/11] cal: fix preprocessor directive indendation Sami Kerola
2013-04-28 17:10 ` [PATCH 02/11] cal: convert function like definitions to functions Sami Kerola
2013-04-28 17:28   ` Dave Reisner
2013-04-28 20:20     ` Sami Kerola
2013-04-29 15:21   ` Thomas Bächler
2013-04-29 20:21     ` Sami Kerola
2013-04-28 17:10 ` [PATCH 03/11] cal: clean up use of constants Sami Kerola
2013-04-29 15:51   ` Thomas Bächler
2013-04-29 21:05     ` Sami Kerola
2013-04-28 17:11 ` [PATCH 04/11] tests: add calendar reformation check Sami Kerola
2013-04-28 17:11 ` [PATCH 05/11] cal: simplify calendar reformat calculations Sami Kerola
2013-04-28 17:11 ` [PATCH 06/11] cal: remove unnecessary initializations Sami Kerola
2013-04-28 17:11 ` [PATCH 07/11] cal: de-duplicate julian specific functions Sami Kerola
2013-04-28 17:11 ` [PATCH 08/11] cal: allow day highlight testing Sami Kerola
2013-04-29  0:24   ` Pádraig Brady
2013-04-29  8:59     ` Sami Kerola
2013-04-29  9:58       ` Karel Zak
2013-04-28 17:11 ` [PATCH 09/11] tests: add cal day highlight corner cases Sami Kerola
2013-04-28 17:11 ` [PATCH 10/11] cal: trim three month display trailing white spaces Sami Kerola
2013-04-29  0:32   ` Pádraig Brady
2013-04-29  9:11     ` Sami Kerola
2013-04-29 13:40       ` Pádraig Brady
2013-04-29 18:01         ` Sami Kerola
2013-05-01  0:54           ` Pádraig Brady
2013-04-28 17:11 ` [PATCH 11/11] cal: move global variables to local scope Sami Kerola

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox