Skip to contentSkip to conversation

cal: bug on FreeBSD

DISCUSSION Rüdiger Meier 3 participants 6 messages Mar 8, 2016 – May 21, 2016 Util-Linux package

lore
Share & tools
lore
#0 lore raw

Hi,

cal skips the first day of the week if it has 2 digits.

# diff ./cal
 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 27 28 29 30
+ 12 13 14 15 16 17
+ 19 20 21 22 23 24
+ 26 27 28 29 30

The bug was introduced by my own commit
07ac4aa9 cal: all output must use my_putstring

This is the related code in cal_output_months()

for (d = DAYS_IN_WEEK * week_line;
     d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
	if (0 < i->days[d]) {
		if (reqday == i->days[d])
			sprintf(out, "%*s%s%*d%s", skip - (ctl->julian ? 3 : 2),
			       "", Senter, (ctl->julian ? 3 : 2),
			       i->days[d], Sexit);
		else
			sprintf(out, "%*d", skip, i->days[d]);
/*                             ^^^^^
 *         if we print this with fputs instead of my_putstring/putp
 *         below than it looks good */
	} else
		sprintf(out, "%*s", skip, "");
	my_putstring(out);
	if (skip < (int)ctl->day_width)
		skip++;
}

Is putp() broken on that machine or could we fix it?

cu,
Rudi

#1 lore raw
Quoted 38 lines · Rüdiger Meier

cal skips the first day of the week if it has 2 digits.

# diff ./cal
 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 27 28 29 30
+ 12 13 14 15 16 17
+ 19 20 21 22 23 24
+ 26 27 28 29 30

The bug was introduced by my own commit
07ac4aa9 cal: all output must use my_putstring

This is the related code in cal_output_months()

for (d = DAYS_IN_WEEK * week_line;
     d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
        if (0 < i->days[d]) {
                if (reqday == i->days[d])
                        sprintf(out, "%*s%s%*d%s", skip - (ctl->julian ? 3 : 2),
                               "", Senter, (ctl->julian ? 3 : 2),
                               i->days[d], Sexit);
                else
                        sprintf(out, "%*d", skip, i->days[d]);
/*                             ^^^^^
 *         if we print this with fputs instead of my_putstring/putp
 *         below than it looks good */
        } else
                sprintf(out, "%*s", skip, "");
        my_putstring(out);
        if (skip < (int)ctl->day_width)
                skip++;
}
Quoted 1 line

Is putp() broken on that machine or could we fix it?

Hi Rudi,

Does this happen with putp() only? The following should enable you to test the fputs() output.

-- snip

misc-utils/cal.c +1 -0
index 2a53b89..3e513ad 100644
@@ -291,6 +291,7 @@ int main(int argc, char **argv)
  Senter = my_tgetstr("smso");
  Sexit = my_tgetstr("rmso");
  }
+ has_term = 0;
  }
  }
 #endif

-- snip

p.s. What is 'that' machine.. just wondering if problem could be reproduced.

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
#2 lore raw
Quoted 38 lines · On Tuesday 08 March 2016 23:35:01 Sami Kerola wrote:
Quoted 37 lines · Rdiger Meier

cal skips the first day of the week if it has 2 digits.

# diff ./cal
 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 27 28 29 30
+ 12 13 14 15 16 17
+ 19 20 21 22 23 24
+ 26 27 28 29 30

The bug was introduced by my own commit
07ac4aa9 cal: all output must use my_putstring

This is the related code in cal_output_months()

for (d =DAYS_IN_WEEK * week_line;
     d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
        if (0 < i->days[d]) {
                if (reqday = i->days[d])
                        sprintf(out, "%*s%s%*d%s", skip - (ctl->julian ?
3 : 2), "", Senter, (ctl->julian ? 3 : 2), i->days[d], Sexit);
                else
                        sprintf(out, "%*d", skip, i->days[d]);
/*                             ^^^^^
 *         if we print this with fputs instead of my_putstring/putp
 *         below than it looks good */
        } else
                sprintf(out, "%*s", skip, "");
        my_putstring(out);
        if (skip < (int)ctl->day_width)
                skip++;
}
Quoted 6 lines
Quoted 1 line

Is putp() broken on that machine or could we fix it?

Hi Rudi,

Does this happen with putp() only? The following should enable you to test the fputs() output.

It's only broken with putp, I tried it like this

misc-utils/cal.c +11 -4
index c687c6c..ea3187e 100644
@@ -680,15 +680,22 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
                        for (d = DAYS_IN_WEEK * week_line;
                             d < DAYS_IN_WEEK * week_line + DAYS_IN_WEEK; d++) {
                                if (0 < i->days[d]) {
-                                       if (reqday == i->days[d])
+                                       if (reqday == i->days[d]) {
                                                sprintf(out, "%*s%s%*d%s", skip - (ctl->julian ? 3 : 2),
                                                       "", Senter, (ctl->julian ? 3 : 2),
                                                       i->days[d], Sexit);
-                                       else
+                                               my_putstring(out);
+                                       } else {
                                                sprintf(out, "%*d", skip, i->days[d]);
-                               } else
+                                               //my_putstring(out);
+                                               //printf("%s",out);
+                                               fputs(out, stdout);
+                                               //putp(out);
+                                       }
+                               } else {
                                        sprintf(out, "%*s", skip, "");
-                               my_putstring(out);
+                                       my_putstring(out);
+                               }
                                if (skip < (int)ctl->day_width)
                                        skip++;
                        }
Quoted 2 lines

p.s. What is 'that' machine.. just wondering if problem could be reproduced.

It's a FreeBSD 10.2. It's easy to run it via qemu like this

$ qemu-kvm  -smp 2 -m 1024 -cdrom FreeBSD-10.2-RELEASE-amd64-bootonly.iso -drive if=virtio,file=freebsd.qcow2

cu,
Rudi

maintainer
#3 lore raw
Quoted 5 lines · Rüdiger Meier
Quoted 2 lines · On Tuesday 08 March 2016 23:35:01 Sami Kerola wrote:

Does this happen with putp() only? The following should enable you to test the fputs() output.

It's only broken with putp, I tried it like this

It's BSD specific problem, putp (aka tputs) interprets the string. That's fine as tputs is sensitive to sequences prefixed by $< and everything else is printed to terminal.

BUT probably because BSD has origins in 70' LSD era there is a special exception (ncurses sources tinfo/lib_tputs.c):

    #if BSD_TPUTS
       /*
        * This ugly kluge deals with the fact that some ancient BSD
        * programs (like nethack) actually do the likes of tputs("50") to
        * get delays.
        */
        if (isdigit(UChar(*string))) {
        ...
        }
    #endif

and the number is interpreted as argument for delay_output() and never printed to the output.

It seems we're little bit fragile with putp() as it's primarily designed for terminfo string variables than for a generic output. Unfortunately mix tputs() and fputs() is also no perfect due to buffering issues (commit 07ac4aa9d4f36a6618fd3ab1423bc33e3903ddbd).

The easy fix is to disable ncurses support for BSD (words like fuck and suck are allowed in the commit message for this case:-).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#4 lore raw
Quoted 28 lines · Karel Zak
Quoted 5 lines · Rüdiger Meier
Quoted 2 lines · On Tuesday 08 March 2016 23:35:01 Sami Kerola wrote:

Does this happen with putp() only? The following should enable you to test the fputs() output.

It's only broken with putp, I tried it like this

It's BSD specific problem, putp (aka tputs) interprets the string. That's fine as tputs is sensitive to sequences prefixed by $< and everything else is printed to terminal.

BUT probably because BSD has origins in 70' LSD era there is a special exception (ncurses sources tinfo/lib_tputs.c):

    #if BSD_TPUTS
       /*
        * This ugly kluge deals with the fact that some ancient BSD
        * programs (like nethack) actually do the likes of tputs("50") to
        * get delays.
        */
        if (isdigit(UChar(*string))) {
        ...
        }
    #endif

and the number is interpreted as argument for delay_output() and never printed to the output.

Quoted 7 lines

It seems we're little bit fragile with putp() as it's primarily designed for terminfo string variables than for a generic output. Unfortunately mix tputs() and fputs() is also no perfect due to buffering issues (commit 07ac4aa9d4f36a6618fd3ab1423bc33e3903ddbd).

The easy fix is to disable ncurses support for BSD (words like fuck and suck are allowed in the commit message for this case:-).

How about disabling just the kludge instead of ncurses altogether?

The change below is also available in the git repository:
git://github.com/kerolasa/lelux-utiliteetit.git cal-bsd-fix

--->8----
From: Sami Kerola <[email protected]>
Date: Sat, 21 May 2016 08:47:38 +0100
Subject: [PATCH] cal: make BSD not to do ugly hack when calling tputs()

On BSD systems tputs("<number>") is interpreted as argument for delay_output() and never printed to the output when BSD_TPUTS is defined. The cal(1) does not need such delays, and is printing various date numbers so disable this unwanted BSDism.

Reference: http://www.spinics.net/lists/util-linux-ng/msg12931.html
Reference: https://github.com/freebsd/freebsd-base-graphics/blob/05dac67f853adbb295eaff5bb65b005d958317a1/contrib/ncurses/ncurses/tinfo/lib_tputs.c#L305-332

---
 misc-utils/cal.c | 9 +++++++++
 1 file changed, 9 insertions(+)
misc-utils/cal.c +0 -0
index 3e20530..4735278 100644
@@ -87,6 +87,15 @@ static const char *Senter = "", *Sexit = ""; /*

enter and exit standout mode */

 # include <term.h>
 #endif

+/*
+ * Disable ugly BSD hack that makes puts("<number>") to not to be printed.
+ * http://www.spinics.net/lists/util-linux-ng/msg12931.html
+ * https://github.com/freebsd/freebsd-base-graphics/blob/master/contrib/ncurses/ncurses/tinfo/lib_tputs.c#L305-332
+ */
+#ifdef BSD_TPUTS
+# unset BSD_TPUTS
+#endif
+
static int setup_terminal(char *term)
{
#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)

Signature · 1 line
2.8.2
#5 lore raw

Review

Quoted 3 lines · Sami Kerola
(quoted diff) +3 -0
+#ifdef BSD_TPUTS
+# unset BSD_TPUTS
+#endif

There are couple problems with this change. First of all the BSD_TPUTS is a complier directive that should be unset to avoid the problems when compiling ncurses. Fidling after the ncurses compilation with this define makes no difference what so ever. Secondly there weren't enough swearing in the commit message.

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/