Bug 30641

Summary: Patch for games/grdc
Product: Base System Reporter: simon <simon>
Component: binAssignee: ru <ru>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description simon 2001-09-18 09:40:00 UTC
grdc(8) outputs big digital clock. Color of digits is red, if grdc(8)
is used on b/w display, then it is hard to see something.

This patch adds new flag to grdc(8): -b, which changes color of digits
to b/w. Also arguments checking is improved.

In this patch type of "sigtermed" variable is changed from "int" to
"voilatile sig_atomic_t", because "sigtermed" variable is changed in signal
handler.

In this patch also one fprintf(stderr) function is changed to
appropriate errx(3) function.

Fix: 

+#include <ctype.h>
+#include <err.h>
 #include <time.h>
 #include <signal.h>
 #include <ncurses.h>
 #include <stdlib.h>
+#include <string.h>
 #ifndef NONPOSIX
 #include <unistd.h>
 #endif
@@ -31,9 +36,9 @@
 	074717, 074757, 071111, 075757, 075717, 002020
 };
 long old[6], next[6], new[6], mask;
-char scrol;
+int scrol = 0, bw = 0;

-int sigtermed=0;
+volatile sig_atomic_t sigtermed = 0;

 int hascolor = 0;

@@ -56,6 +61,32 @@
 long t, a;
 int i, j, s, k;
 int n = 0;
+int opt;
+
+	opterr = 0;
+	while ( (opt = getopt(argc, argv, "sb")) != -1)
+		switch (opt) {
+		case 's':
+			scrol = 1;
+			break;
+		case 'b':
+			bw = 1;
+			break;
+		case '?':
+			errx(1, "invalid switch -%c", optopt);
+			break;
+		default:
+			err(1, "getopt");
+		}
+	if (optind < argc) {
+		if (optind == argc - 1) {
+			for (i = 0; i < strlen(argv[optind]); ++i)
+				if (isdigit(argv[optind][i]) == 0)
+					errx(1, "incorrect number of seconds \"%s\"", argv[optind]);
+			n = atoi(argv[optind]);
+		} else
+			errx(1, "too many arguments \"%s\"", argv[optind + 1]);
+	}

 	initscr();

@@ -71,20 +102,19 @@

 	if(hascolor) {
 		start_color();
-		init_pair(1, COLOR_BLACK, COLOR_RED);
-		init_pair(2, COLOR_RED, COLOR_BLACK);
+		if (bw) {
+			init_pair(1, COLOR_BLACK, COLOR_WHITE);
+			init_pair(2, COLOR_WHITE, COLOR_BLACK);
+		} else {
+			init_pair(1, COLOR_BLACK, COLOR_RED);
+			init_pair(2, COLOR_RED, COLOR_BLACK);
+		}
 		init_pair(3, COLOR_WHITE, COLOR_BLACK);
 		attrset(COLOR_PAIR(2));
 	}

 	clear();
 	refresh();
-	while(--argc > 0) {
-		if(**++argv == '-')
-			scrol = 1;
-		else
-			n = atoi(*argv);
-	}

 	if(hascolor) {
 		attrset(COLOR_PAIR(3));
@@ -155,8 +185,7 @@
 			clear();
 			refresh();
 			endwin();
-			fprintf(stderr, "grdc terminated by signal %d\n", sigtermed);
-			exit(1);
+			errx(1, "terminated by signal %d", sigtermed);
 		}
 	} while(--n);
 	standend();--LB382CqQmUam0u1EDFzDP0YzDdW6UZuffz6pMWU7Me6AQYIS
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

diff -ru /usr/src/games/grdc/grdc.6 grdc/grdc.6
--- /usr/src/games/grdc/grdc.6	Mon Dec  5 19:29:59 1994
+++ grdc/grdc.6	Mon Sep 17 02:40:22 2001
@@ -3,7 +3,7 @@
 grdc \- grand digital clock (curses)
 .SH SYNOPSIS
 .B grdc
-[-s] [
+[-sb] [
 .I n
 ]
 .SH DESCRIPTION
@@ -18,5 +18,8 @@
 .B -s
 flag makes digits scroll as they change. In this curses mode implementation,
 the scrolling option has trouble keeping up.
+The optional
+.B -b
+flag changes color of digits to b/w (useful option for b/w displays).
 .SH AUTHOR
 Amos Shapir, modified for curses by John Lupien.
diff -ru /usr/src/games/grdc/grdc.c grdc/grdc.c
--- /usr/src/games/grdc/grdc.c	Sun Dec 12 03:04:17 1999
+++ grdc/grdc.c	Mon Sep 17 20:26:14 2001
@@ -1,18 +1,23 @@
 /*
  * Grand digital clock for curses compatible terminals
- * Usage: grdc [-s] [n]   -- run for n seconds (default infinity)
+ * Usage: grdc [-sb] [n]   -- run for n seconds (default infinity)
  * Flags: -s: scroll
+ *	  -b: b/w output
  *
  * modified 10-18-89 for curses (jrl)
  * 10-18-89 added signal handling
+ * 09-17-2001 added -b flag, improved arguments checking
  *
  * $FreeBSD: src/games/grdc/grdc.c,v 1.8 1999/12/12 03:04:17 billf Exp $
  */
How-To-Repeat: 
Apply following patch.
Comment 1 ru freebsd_committer freebsd_triage 2001-09-19 13:58:08 UTC
On Tue, Sep 18, 2001 at 11:39:12AM +0300, Andrey Simonenko wrote:
> 
> grdc(8) outputs big digital clock. Color of digits is red, if grdc(8)
> is used on b/w display, then it is hard to see something.
> 
> This patch adds new flag to grdc(8): -b, which changes color of digits
> to b/w. Also arguments checking is improved.
> 
Use the right (B/W) terminal type such as cons25-m.

> In this patch type of "sigtermed" variable is changed from "int" to
> "voilatile sig_atomic_t", because "sigtermed" variable is changed in signal
> handler.
> 
This looks OK though a bit superflows in this case.  :-)
Also, is sig_atomic_t guaranteed to be a superset of int?
(I'm mostly concerned about the portability issues here,
I know that in FreeBSD sig_atomic_t is ether int or long.)


Cheers,
-- 
Ruslan Ermilov		Oracle Developer/DBA,
ru@sunbay.com		Sunbay Software AG,
ru@FreeBSD.org		FreeBSD committer,
+380.652.512.251	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age
Comment 2 Andrey Simonenko 2001-09-19 16:23:25 UTC
----- Original Message -----
From: Ruslan Ermilov <ru@FreeBSD.org>
To: Andrey Simonenko <simon@simon.org.ua>
Cc: <bug-followup@FreeBSD.org>
Sent: Wednesday, September 19, 2001 4:58 PM
Subject: Re: bin/30641: Patch for games/grdc


> On Tue, Sep 18, 2001 at 11:39:12AM +0300, Andrey Simonenko wrote:
> >
> > grdc(8) outputs big digital clock. Color of digits is red, if grdc(8)
> > is used on b/w display, then it is hard to see something.
> >
> > This patch adds new flag to grdc(8): -b, which changes color of digits
> > to b/w. Also arguments checking is improved.
> >
> Use the right (B/W) terminal type such as cons25-m.

I don't know about it. Nevertheless given patch makes better arguments
checking.

>
> > In this patch type of "sigtermed" variable is changed from "int" to
> > "voilatile sig_atomic_t", because "sigtermed" variable is changed in
signal
> > handler.
> >
> This looks OK though a bit superflows in this case.  :-)
> Also, is sig_atomic_t guaranteed to be a superset of int?
> (I'm mostly concerned about the portability issues here,
> I know that in FreeBSD sig_atomic_t is ether int or long.)

I always use "volatile sig_atomic_t" type for variable, which can
be changed in signal handlers. May be somebody another can
help with answer about portability, I think that sig_atomic_t is correct
type in this case.
Comment 3 ru freebsd_committer freebsd_triage 2001-09-20 07:10:09 UTC
On Wed, Sep 19, 2001 at 07:23:25PM +0400, Andrey Simonenko wrote:
> 
> ----- Original Message -----
> From: Ruslan Ermilov <ru@FreeBSD.org>
> To: Andrey Simonenko <simon@simon.org.ua>
> Cc: <bug-followup@FreeBSD.org>
> Sent: Wednesday, September 19, 2001 4:58 PM
> Subject: Re: bin/30641: Patch for games/grdc
> 
> 
> > On Tue, Sep 18, 2001 at 11:39:12AM +0300, Andrey Simonenko wrote:
> > >
> > > grdc(8) outputs big digital clock. Color of digits is red, if grdc(8)
> > > is used on b/w display, then it is hard to see something.
> > >
> > > This patch adds new flag to grdc(8): -b, which changes color of digits
> > > to b/w. Also arguments checking is improved.
> > >
> > Use the right (B/W) terminal type such as cons25-m.
> 
> I don't know about it. Nevertheless given patch makes better arguments
> checking.
> 
cons25r-m is much like the cons25r, but with colors support disabled.
You are not going to fix every program that uses colors, do you?  :-)
You need to set the correct black&white terminal type for black&white
hardware, that's all.  You are not expecting the correct behavior if
you set the wrong terminal type, right?  If you set TERM=at386 for
syscons(4) hardware, function keys will not work, and you will develop
the patch to support cons25r keystorkes for grdc(8)?  :-)


Cheers,
-- 
Ruslan Ermilov		Oracle Developer/DBA,
ru@sunbay.com		Sunbay Software AG,
ru@FreeBSD.org		FreeBSD committer,
+380.652.512.251	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age
Comment 4 simon 2001-09-20 15:22:05 UTC
On Wed, 19 Sep 2001, Ruslan Ermilov wrote:

> On Tue, Sep 18, 2001 at 11:39:12AM +0300, Andrey Simonenko wrote:
> >
> > grdc(8) outputs big digital clock. Color of digits is red, if grdc(8)
> > is used on b/w display, then it is hard to see something.
> >
> > This patch adds new flag to grdc(8): -b, which changes color of digits
> > to b/w. Also arguments checking is improved.
> >
> Use the right (B/W) terminal type such as cons25-m.
>
> > In this patch type of "sigtermed" variable is changed from "int" to
> > "voilatile sig_atomic_t", because "sigtermed" variable is changed in signal
> > handler.
> >
> This looks OK though a bit superflows in this case.  :-)
> Also, is sig_atomic_t guaranteed to be a superset of int?
> (I'm mostly concerned about the portability issues here,
> I know that in FreeBSD sig_atomic_t is ether int or long.)
>

Ok, I understand that there isn't any sense in -b option, so I remove -b
option support from my patch, but all other improvements I keep there:

diff -ru /usr/src/games/grdc/grdc.c grdc/grdc.c
--- /usr/src/games/grdc/grdc.c	Sun Dec 12 03:04:17 1999
+++ grdc/grdc.c	Thu Sep 20 15:38:40 2001
@@ -5,14 +5,18 @@
  *
  * modified 10-18-89 for curses (jrl)
  * 10-18-89 added signal handling
+ * 09-17-2001 improved arguments checking
  *
  * $FreeBSD: src/games/grdc/grdc.c,v 1.8 1999/12/12 03:04:17 billf Exp $
  */

+#include <ctype.h>
+#include <err.h>
 #include <time.h>
 #include <signal.h>
 #include <ncurses.h>
 #include <stdlib.h>
+#include <string.h>
 #ifndef NONPOSIX
 #include <unistd.h>
 #endif
@@ -31,9 +35,9 @@
 	074717, 074757, 071111, 075757, 075717, 002020
 };
 long old[6], next[6], new[6], mask;
-char scrol;
+int scrol = 0;

-int sigtermed=0;
+volatile sig_atomic_t sigtermed = 0;

 int hascolor = 0;

@@ -56,6 +60,29 @@
 long t, a;
 int i, j, s, k;
 int n = 0;
+int opt;
+
+	opterr = 0;
+	while ( (opt = getopt(argc, argv, "s")) != -1)
+		switch (opt) {
+		case 's':
+			scrol = 1;
+			break;
+		case '?':
+			errx(1, "invalid switch -%c", optopt);
+			break;
+		default:
+			err(1, "getopt");
+		}
+	if (optind < argc) {
+		if (optind == argc - 1) {
+			for (i = 0; i < strlen(argv[optind]); ++i)
+				if (isdigit(argv[optind][i]) == 0)
+					errx(1, "incorrect number of seconds \"%s\"", argv[optind]);
+			n = atoi(argv[optind]);
+		} else
+			errx(1, "too many arguments \"%s\"", argv[optind + 1]);
+	}

 	initscr();

@@ -79,12 +106,6 @@

 	clear();
 	refresh();
-	while(--argc > 0) {
-		if(**++argv == '-')
-			scrol = 1;
-		else
-			n = atoi(*argv);
-	}

 	if(hascolor) {
 		attrset(COLOR_PAIR(3));
@@ -155,8 +176,7 @@
 			clear();
 			refresh();
 			endwin();
-			fprintf(stderr, "grdc terminated by signal %d\n", sigtermed);
-			exit(1);
+			errx(1, "terminated by signal %d", sigtermed);
 		}
 	} while(--n);
 	standend();
Comment 5 ru freebsd_committer freebsd_triage 2001-09-25 14:45:35 UTC
State Changed
From-To: open->closed

A slightly modified version committed, thanks. 


Comment 6 ru freebsd_committer freebsd_triage 2001-09-25 14:45:35 UTC
Responsible Changed
From-To: freebsd-bugs->ru