Subject: bin/6661: [PATCH] Worms: curses conversion patch
To: None <gnats-bugs@gnats.netbsd.org>
From: Joseph Myers <jsm28@cam.ac.uk>
List: netbsd-bugs
Date: 12/27/1998 22:48:52
>Number:         6661
>Category:       bin
>Synopsis:       [PATCH] Worms: curses conversion patch
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun Dec 27 16:05:01 1998
>Last-Modified:
>Originator:     Joseph S. Myers
>Organization:
Trinity College, University of Cambridge, UK
>Release:        NetBSD-current of 1998-12-07
>Environment:
[
System: Linux decomino 2.0.36 #1 Mon Nov 16 14:25:34 UTC 1998 i686 unknown
Architecture: i686
]
>Description:

The appended patch converts worms(6) to use curses, thereby
simplifying the code and improving its portability.  It also adds a
delay option from OpenBSD, to allow reasonable speed display on fast
terminals, adds use of const, and fixes signal handling and use of
errx() where appropriate.

>How-To-Repeat:

>Fix:

diff -ruN worms/worms.6 worms+/worms.6
--- worms/worms.6	Tue Apr 28 11:06:53 1998
+++ worms+/worms.6	Wed Dec 23 13:54:07 1998
@@ -41,23 +41,28 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl ft
+.Op Fl d Ar delay
 .Op Fl l Ar length
 .Op Fl n Ar number
 .Sh DESCRIPTION
 A
 .Ux
-version of the DEC-2136 program ``worms''.
+version of the DEC-2136 program
+.Dq worms .
 .Pp
 The options are as follows:
 .Bl -tag -width indent
 .It Fl f
-Makes a ``field'' for the worm(s) to eat.
+Makes a 
+.Dq field
+for the worm(s) to eat.
 .It Fl t
 Makes each worm leave a trail behind it.
+.It Fl d
+Specifies a delay, in milliseconds, between each update.  This is
+useful for fast terminals.  Reasonable values are around 20-200.  The
+default is 0.
 .It Fl l
 Specifies a length for each worm; the default is 16.
 .It Fl n
 Specifies the number of worms; the default is 3.
-.Sh BUGS
-The lower-right-hand character position will not be updated properly
-on a terminal that wraps at the right margin.
diff -ruN worms/worms.c worms+/worms.c
--- worms/worms.c	Mon Sep 14 11:05:06 1998
+++ worms+/worms.c	Sun Dec 27 22:39:23 1998
@@ -65,15 +65,15 @@
  *
  */
 #include <sys/types.h>
-#include <sys/ioctl.h>
 
+#include <curses.h>
+#include <err.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <termios.h>
 #include <unistd.h>
 
-static struct options {
+static const struct options {
 	int nopts;
 	int opts[3];
 }
@@ -167,13 +167,11 @@
 	{ 0, { 0, 0, 0 } }
 };
 
-#define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, fputchar)
 
-char *tcp;
-static char	flavor[] = {
+static const char	flavor[] = {
 	'O', '*', '#', '$', '%', '0', '@', '~'
 };
-static short	xinc[] = {
+static const short	xinc[] = {
 	1,  1,  1,  0, -1, -1, -1,  0
 }, yinc[] = {
 	-1,  0,  1,  1,  1,  0, -1, -1
@@ -183,60 +181,51 @@
 	short *xpos, *ypos;
 } *worm;
 
-void	 fputchar __P((int));
+volatile sig_atomic_t sig_caught = 0;
+
 int	 main __P((int, char **));
 void	 nomem __P((void)) __attribute__((__noreturn__));
-void	 onsig __P((int)) __attribute__((__noreturn__));
-int	 tgetent __P((char *, char *));
-int	 tgetflag __P((char *));
-int	 tgetnum __P((char *));
-char	*tgetstr __P((char *, char **));
-char	*tgoto __P((char *, int, int));
-int	 tputs __P((char *, int, void (*)(int)));
+void	 onsig __P((int));
 
 int
 main(argc, argv)
 	int argc;
 	char *argv[];
 {
-	extern char *UP;
 	int x, y, h, n;
 	struct worm *w;
-	struct options *op;
+	const struct options *op;
 	short *ip;
-	char *term;
-	int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap;
+	int CO, LI, last, bottom, ch, length, number, trail;
 	short **ref;
-	char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR;
-	char *field, tcb[100], *mp;
-	struct termios ti;
-#ifdef TIOCGWINSZ
-	struct winsize ws;
-#endif
+	const char *field;
+	char *mp;
+	unsigned int delay = 0;
 
 	mp = NULL;
 	length = 16;
 	number = 3;
 	trail = ' ';
 	field = NULL;
-	while ((ch = getopt(argc, argv, "fl:n:t")) != -1)
+	while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
 		switch(ch) {
+		case 'd':
+			if ((delay = (unsigned int)strtoul(optarg, (char **)NULL, 10)) < 1 || delay > 1000)
+				errx(1, "invalid delay (1-1000)");
+			delay *= 1000;  /* ms -> us */
+			break;
 		case 'f':
 			field = "WORM";
 			break;
 		case 'l':
 			if ((length = atoi(optarg)) < 2 || length > 1024) {
-				(void)fprintf(stderr,
-				    "worms: invalid length (%d - %d).\n",
+				errx(1, "invalid length (%d - %d).",
 				     2, 1024);
-				exit(1);
 			}
 			break;
 		case 'n':
 			if ((number = atoi(optarg)) < 1) {
-				(void)fprintf(stderr,
-				    "worms: invalid number of worms.\n");
-				exit(1);
+				errx(1, "invalid number of worms.");
 			}
 			break;
 		case 't':
@@ -245,55 +234,18 @@
 		case '?':
 		default:
 			(void)fprintf(stderr,
-			    "usage: worms [-ft] [-l length] [-n number]\n");
+			    "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
 			exit(1);
 		}
 
-	if (!(term = getenv("TERM"))) {
-		(void)fprintf(stderr, "worms: no TERM environment variable.\n");
-		exit(1);
-	}
 	if (!(worm = malloc((size_t)number *
 	    sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
 		nomem();
-	if (tgetent(mp, term) <= 0) {
-		(void)fprintf(stderr, "worms: %s: unknown terminal type.\n",
-		    term);
-		exit(1);
-	}
-	tcp = tcb;
-	if (!(CM = tgetstr("cm", &tcp))) {
-		(void)fprintf(stderr,
-		    "worms: terminal incapable of cursor motion.\n");
-		exit(1);
-	}
-	AL = tgetstr("al", &tcp);
-	BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp);
-	EI = tgetstr("ei", &tcp);
-	HO = tgetstr("ho", &tcp);
-	IC = tgetstr("ic", &tcp);
-	IM = tgetstr("im", &tcp);
-	IN = tgetflag("in");
-	IP = tgetstr("ip", &tcp);
-	SR = tgetstr("sr", &tcp);
-	UP = tgetstr("up", &tcp);
-#ifdef TIOCGWINSZ
-	if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
-	    ws.ws_col && ws.ws_row) {
-		CO = ws.ws_col;
-		LI = ws.ws_row;
-	} else
-#endif
-	{
-		if ((CO = tgetnum("co")) <= 0)
-			CO = 80;
-		if ((LI = tgetnum("li")) <= 0)
-			LI = 24;
-	}
+	initscr();
+	CO = COLS;
+	LI = LINES;
 	last = CO - 1;
 	bottom = LI - 1;
-	tcgetattr(fileno(stdout), &ti);
-	Wrap = tgetflag("am");
 	if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
 		nomem();
 	if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
@@ -304,8 +256,6 @@
 	}
 	for (ip = ref[0], n = LI * CO; --n >= 0;)
 		*ip++ = 0;
-	if (Wrap)
-		ref[bottom][last] = 1;
 	for (n = number, w = &worm[0]; --n >= 0; w++) {
 		w->orientation = w->head = 0;
 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
@@ -327,78 +277,30 @@
 	(void)signal(SIGTSTP, onsig);
 	(void)signal(SIGTERM, onsig);
 
-	tputs(tgetstr("ti", &tcp), 1, fputchar);
-	tputs(tgetstr("cl", &tcp), 1, fputchar);
 	if (field) {
-		char *p = field;
+		const char *p = field;
 
-		for (y = bottom; --y >= 0;) {
+		for (y = LI; --y >= 0;) {
 			for (x = CO; --x >= 0;) {
-				fputchar(*p++);
-				if (!*p)
-					p = field;
-			}
-			if (!Wrap)
-				fputchar('\n');
-			(void)fflush(stdout);
-		}
-		if (Wrap) {
-			if (IM && !IN) {
-				for (x = last; --x > 0;) {
-					fputchar(*p++);
-					if (!*p)
-						p = field;
-				}
-				y = *p++;
+				addch(*p++);
 				if (!*p)
 					p = field;
-				fputchar(*p);
-				if (BC)
-					tputs(BC, 1, fputchar);
-				else
-					cursor(last - 1, bottom);
-				tputs(IM, 1, fputchar);
-				if (IC)
-					tputs(IC, 1, fputchar);
-				fputchar(y);
-				if (IP)
-					tputs(IP, 1, fputchar);
-				tputs(EI, 1, fputchar);
 			}
-			else if (SR || AL) {
-				if (HO)
-					tputs(HO, 1, fputchar);
-				else
-					cursor(0, 0);
-				if (SR)
-					tputs(SR, 1, fputchar);
-				else
-					tputs(AL, LI, fputchar);
-				for (x = CO; --x >= 0;) {
-					fputchar(*p++);
-					if (!*p)
-						p = field;
-				}
-			}
-			else for (x = last; --x >= 0;) {
-				fputchar(*p++);
-				if (!*p)
-					p = field;
-			}
-		}
-		else for (x = CO; --x >= 0;) {
-			fputchar(*p++);
-			if (!*p)
-				p = field;
+			refresh();
 		}
 	}
 	for (;;) {
-		(void)fflush(stdout);
+		refresh();
+		if (sig_caught) {
+			endwin();
+			exit(0);
+		}
+		if (delay) usleep(delay);
 		for (n = 0, w = &worm[0]; n < number; n++, w++) {
 			if ((x = w->xpos[h = w->head]) < 0) {
-				cursor(x = w->xpos[h] = 0,
-				     y = w->ypos[h] = bottom);
-				fputchar(flavor[n % sizeof(flavor)]);
+				mvaddch(y = w->ypos[h] = bottom,
+					x = w->xpos[h] = 0,
+					flavor[n % sizeof(flavor)]);
 				ref[y][x]++;
 			}
 			else
@@ -411,15 +313,13 @@
 				x1 = w->xpos[h];
 				y1 = w->ypos[h];
 				if (--ref[y1][x1] == 0) {
-					cursor(x1, y1);
-					if (trail)
-						fputchar(trail);
+					mvaddch(y1, x1, trail);
 				}
 			}
 			op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
 			switch (op->nopts) {
 			case 0:
-				(void)fflush(stdout);
+				refresh();
 				abort();
 				return(1);
 			case 1:
@@ -429,10 +329,9 @@
 				w->orientation =
 				    op->opts[(int)random() % op->nopts];
 			}
-			cursor(x += xinc[w->orientation],
-			    y += yinc[w->orientation]);
-			if (!Wrap || x != last || y != bottom)
-				fputchar(flavor[n % sizeof(flavor)]);
+			mvaddch(y += yinc[w->orientation],
+				x += xinc[w->orientation],
+				flavor[n % sizeof(flavor)]);
 			ref[w->ypos[h] = y][w->xpos[h] = x]++;
 		}
 	}
@@ -440,23 +339,13 @@
 
 void
 onsig(signo)
-	int signo;
-{
-	tputs(tgetstr("cl", &tcp), 1, fputchar);
-	tputs(tgetstr("te", &tcp), 1, fputchar);
-	exit(0);
-}
-
-void
-fputchar(c)
-	int c;
+	int signo __attribute__((__unused__));
 {
-	(void)putchar(c);
+	sig_caught = 1;
 }
 
 void
 nomem()
 {
-	(void)fprintf(stderr, "worms: not enough memory.\n");
-	exit(1);
+	errx(1, "not enough memory.");
 }
>Audit-Trail:
>Unformatted: