summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Janzen <pjanzen@cvs.openbsd.org>2000-01-25 06:37:18 +0000
committerPaul Janzen <pjanzen@cvs.openbsd.org>2000-01-25 06:37:18 +0000
commit234f680e72d150393d2d96f08a76816447575028 (patch)
tree9f09f67b4becf86d1afd70b72a1ea5cd774a8fb6
parente40439a3e2496cea86c39162e0063dee4d8e547e (diff)
use only non-deprecated curses (i.e., no termcap) and tidy a little; mostly
from jsm@netbsd.org
-rw-r--r--games/rain/rain.c176
-rw-r--r--games/worms/worms.c238
2 files changed, 95 insertions, 319 deletions
diff --git a/games/rain/rain.c b/games/rain/rain.c
index 04b7583a4bc..f07c3338021 100644
--- a/games/rain/rain.c
+++ b/games/rain/rain.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rain.c,v 1.8 1999/10/29 03:59:01 pjanzen Exp $ */
+/* $OpenBSD: rain.c,v 1.9 2000/01/25 06:37:09 pjanzen Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,7 +43,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)rain.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$OpenBSD: rain.c,v 1.8 1999/10/29 03:59:01 pjanzen Exp $";
+static char rcsid[] = "$OpenBSD: rain.c,v 1.9 2000/01/25 06:37:09 pjanzen Exp $";
#endif
#endif /* not lint */
@@ -53,44 +53,28 @@ static char rcsid[] = "$OpenBSD: rain.c,v 1.8 1999/10/29 03:59:01 pjanzen Exp $"
*/
#include <sys/types.h>
-#include <sys/ioctl.h>
+#include <curses.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <term.h>
-#include <termios.h>
-#include <time.h>
#include <unistd.h>
-#define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
+volatile sig_atomic_t sig_caught = 0;
-static struct termios sg, old_tty;
-
-int fputchar __P((int));
static void onsig __P((int));
-char *LL, *TE, *VE;
-
int
main(argc, argv)
int argc;
char **argv;
{
- extern speed_t ospeed;
- extern char *UP;
- register int x, y, j;
- register char *CM, *BC, *DN, *ND, *term;
- char *TI, *tcp, *mp, tcb[100];
+ int x, y, j;
long tcols, tlines;
u_int delay = 0;
- char ch;
+ int ch;
int xpos[5], ypos[5];
-#ifdef TIOCGWINSZ
- struct winsize ws;
-#endif
/* revoke */
setegid(getgid());
@@ -106,138 +90,61 @@ main(argc, argv)
break;
case 'h':
default:
- (void)fprintf(stderr,"usage: rain [-d delay]\n");
+ (void)fprintf(stderr, "usage: rain [-d delay]\n");
exit(1);
}
srandom(time((time_t *)NULL));
- if (!(term = getenv("TERM")))
- errx(1, "TERM: parameter not set");
- if (!(mp = malloc((u_int)1024)))
- errx(1, "out of space");
- if (tgetent(mp, term) <= 0)
- errx(1, "unknown terminal type `%s'", term);
- tcp = tcb;
- if (!(CM = tgetstr("cm", &tcp)))
- errx(1, "terminal not capable of cursor motion");
- if (!(BC = tgetstr("bc", &tcp)))
- BC = "\b";
- if (!(DN = tgetstr("dn", &tcp)))
- DN = "\n";
- if (!(ND = tgetstr("nd", &tcp)))
- ND = " ";
- VE = tgetstr("ve", &tcp);
-#ifdef TIOCGWINSZ
- if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
- ws.ws_col && ws.ws_row) {
- tcols = ws.ws_col;
- tlines = ws.ws_row;
- } else
-#endif
- {
- if ((tcols = tgetnum("co")) == -1)
- tcols = 80;
- if ((tlines = tgetnum("li")) == -1)
- tlines = 24;
- }
- tcols -= 4;
- tlines -= 4;
- TE = tgetstr("te", &tcp);
- TI = tgetstr("ti", &tcp);
- UP = tgetstr("up", &tcp);
- if (!(LL = tgetstr("ll", &tcp))) {
- if (!(LL = malloc((u_int)10)))
- errx(1, "out of memory");
- (void)strcpy(LL, tgoto(CM, 0, 23));
- }
+ initscr();
+ tcols = COLS - 4;
+ tlines = LINES - 4;
+
(void)signal(SIGHUP, onsig);
(void)signal(SIGINT, onsig);
(void)signal(SIGQUIT, onsig);
(void)signal(SIGSTOP, onsig);
(void)signal(SIGTSTP, onsig);
(void)signal(SIGTERM, onsig);
- tcgetattr(1, &sg);
- old_tty = sg;
- ospeed = cfgetospeed(&sg);
- sg.c_iflag &= ~ICRNL;
- sg.c_oflag &= ~ONLCR;
- sg.c_lflag &= ~ECHO;
- tcsetattr(1, TCSADRAIN, &sg);
- if (TI)
- tputs(TI, 1, fputchar);
- tputs(tgetstr("cl", &tcp), 1, fputchar);
- tputs(tgetstr("vi", &tcp), 1, fputchar);
- (void)fflush(stdout);
+
+ curs_set(0);
for (j = 4; j >= 0; --j) {
xpos[j] = random() % tcols + 2;
ypos[j] = random() % tlines + 2;
}
for (j = 0;;) {
+ if (sig_caught) {
+ endwin();
+ exit(0);
+ }
x = random() % tcols + 2;
y = random() % tlines + 2;
- cursor(x, y);
- fputchar('.');
- cursor(xpos[j], ypos[j]);
- fputchar('o');
+ mvaddch(y, x, '.');
+ mvaddch(ypos[j], xpos[j], 'o');
if (!j--)
j = 4;
- cursor(xpos[j], ypos[j]);
- fputchar('O');
+ mvaddch(ypos[j], xpos[j], 'O');
if (!j--)
j = 4;
- cursor(xpos[j], ypos[j] - 1);
- fputchar('-');
- tputs(DN, 1, fputchar);
- tputs(BC, 1, fputchar);
- tputs(BC, 1, fputchar);
- fputs("|.|", stdout);
- tputs(DN, 1, fputchar);
- tputs(BC, 1, fputchar);
- tputs(BC, 1, fputchar);
- fputchar('-');
+ mvaddch(ypos[j] - 1, xpos[j], '-');
+ mvaddstr(ypos[j], xpos[j] - 1, "|.|");
+ mvaddch(ypos[j] + 1, xpos[j], '-');
if (!j--)
j = 4;
- cursor(xpos[j], ypos[j] - 2);
- fputchar('-');
- tputs(DN, 1, fputchar);
- tputs(BC, 1, fputchar);
- tputs(BC, 1, fputchar);
- fputs("/ \\", stdout);
- cursor(xpos[j] - 2, ypos[j]);
- fputs("| O |", stdout);
- cursor(xpos[j] - 1, ypos[j] + 1);
- fputs("\\ /", stdout);
- tputs(DN, 1, fputchar);
- tputs(BC, 1, fputchar);
- tputs(BC, 1, fputchar);
- fputchar('-');
+ mvaddch(ypos[j] - 2, xpos[j], '-');
+ mvaddstr(ypos[j] - 1, xpos[j] - 1, "/ \\");
+ mvaddstr(ypos[j], xpos[j] - 2, "| O |");
+ mvaddstr(ypos[j] + 1, xpos[j] - 1, "\\ /");
+ mvaddch(ypos[j] + 2, xpos[j], '-');
if (!j--)
j = 4;
- cursor(xpos[j], ypos[j] - 2);
- fputchar(' ');
- tputs(DN, 1, fputchar);
- tputs(BC, 1, fputchar);
- tputs(BC, 1, fputchar);
- fputchar(' ');
- tputs(ND, 1, fputchar);
- fputchar(' ');
- cursor(xpos[j] - 2, ypos[j]);
- fputchar(' ');
- tputs(ND, 1, fputchar);
- fputchar(' ');
- tputs(ND, 1, fputchar);
- fputchar(' ');
- cursor(xpos[j] - 1, ypos[j] + 1);
- fputchar(' ');
- tputs(ND, 1, fputchar);
- fputchar(' ');
- tputs(DN, 1, fputchar);
- tputs(BC, 1, fputchar);
- tputs(BC, 1, fputchar);
- fputchar(' ');
+ mvaddch(ypos[j] - 2, xpos[j], ' ');
+ mvaddstr(ypos[j] - 1, xpos[j] - 1, " ");
+ mvaddstr(ypos[j], xpos[j] - 2, " ");
+ mvaddstr(ypos[j] + 1, xpos[j] - 1, " ");
+ mvaddch(ypos[j] + 2, xpos[j], ' ');
xpos[j] = x;
ypos[j] = y;
- (void)fflush(stdout);
+ refresh();
if (delay) usleep(delay);
}
}
@@ -246,18 +153,5 @@ static void
onsig(dummy)
int dummy;
{
- tputs(VE, 1, fputchar);
- tputs(LL, 1, fputchar);
- if (TE)
- tputs(TE, 1, fputchar);
- (void)fflush(stdout);
- tcsetattr(1, TCSADRAIN, &old_tty);
- exit(0);
-}
-
-int
-fputchar(c)
- int c;
-{
- return(putchar(c));
+ sig_caught = 1;
}
diff --git a/games/worms/worms.c b/games/worms/worms.c
index b5c0296209a..006efe6fc06 100644
--- a/games/worms/worms.c
+++ b/games/worms/worms.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: worms.c,v 1.9 1999/10/29 03:59:06 pjanzen Exp $ */
+/* $OpenBSD: worms.c,v 1.10 2000/01/25 06:37:17 pjanzen Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -43,7 +43,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$OpenBSD: worms.c,v 1.9 1999/10/29 03:59:06 pjanzen Exp $";
+static char rcsid[] = "$OpenBSD: worms.c,v 1.10 2000/01/25 06:37:17 pjanzen Exp $";
#endif
#endif /* not lint */
@@ -65,16 +65,15 @@ static char rcsid[] = "$OpenBSD: worms.c,v 1.9 1999/10/29 03:59:06 pjanzen Exp $
*
*/
#include <sys/types.h>
-#include <sys/ioctl.h>
+#include <curses.h>
+#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
-#include <term.h>
-#include <time.h>
#include <unistd.h>
-static struct options {
+static const struct options {
int nopts;
int opts[3];
}
@@ -168,13 +167,10 @@ static struct options {
{ 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
@@ -184,8 +180,9 @@ static struct worm {
short *xpos, *ypos;
} *worm;
-int fputchar __P((int));
-void nomem __P(());
+volatile sig_atomic_t sig_caught = 0;
+
+void nomem __P((void));
void onsig __P((int));
int
@@ -193,21 +190,14 @@ main(argc, argv)
int argc;
char *argv[];
{
- extern char *UP;
- register int x, y, h, n;
- register struct worm *w;
- register struct options *op;
- register short *ip;
- register char *term;
- int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap;
+ int x, y, h, n;
+ struct worm *w;
+ const struct options *op;
+ short *ip;
+ 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;
+ const char *field;
u_int delay = 0;
- struct termios ti;
-#ifdef TIOCGWINSZ
- struct winsize ws;
-#endif
/* revoke */
setegid(getgid());
@@ -220,30 +210,22 @@ main(argc, argv)
while ((ch = getopt(argc, argv, "d:fhl:n:t")) != -1)
switch(ch) {
case 'd':
- if ((delay = (u_int)strtoul(optarg,(char **)NULL,10)) < 1
- || delay > 1000) {
- (void)fprintf(stderr,"worms: invalid delay (1-1000)\n");
- exit(1);
- }
+ if ((delay = (u_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",
- 2, 1024);
- exit(1);
- }
+ if ((length = atoi(optarg)) < 2 || length > 1024)
+ errx(1, "invalid length (%d - %d).", 2, 1024);
break;
case 'n':
- if ((number = atoi(optarg)) < 1) {
- (void)fprintf(stderr,
- "worms: invalid number of worms.\n");
- exit(1);
- }
+ if ((number = atoi(optarg)) < 1 || number > 100)
+ errx(1, "invalid number of worms (%d - %d).",
+ 1, 100);
break;
case 't':
trail = '.';
@@ -256,72 +238,38 @@ main(argc, argv)
}
srandom(time((time_t *)NULL));
- 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)))
+ if (!(worm = malloc((size_t)number * sizeof(struct worm))))
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();
+ curs_set(0);
+ 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 *)))))
+ if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))) ||
+ !(ref = malloc((size_t)(LI * sizeof(short *))))) {
+ endwin();
nomem();
+ }
for (n = 0; n < LI; ++n) {
ref[n] = ip;
ip += CO;
}
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)))))
+ if (!(ip = malloc((size_t)(length * sizeof(short))))) {
+ endwin();
nomem();
+ }
w->xpos = ip;
for (x = length; --x >= 0;)
*ip++ = -1;
- if (!(ip = malloc((size_t)(length * sizeof(short)))))
+ if (!(ip = malloc((size_t)(length * sizeof(short))))) {
+ endwin();
nomem();
+ }
w->ypos = ip;
for (y = length; --y >= 0;)
*ip++ = -1;
@@ -334,80 +282,30 @@ main(argc, argv)
(void)signal(SIGTSTP, onsig);
(void)signal(SIGTERM, onsig);
- tputs(tgetstr("ti", &tcp), 1, fputchar);
- tputs(tgetstr("cl", &tcp), 1, fputchar);
- tputs(tgetstr("vi", &tcp), 1, fputchar);
if (field) {
- register char *p = field;
+ const char *p = field;
- for (y = bottom; --y >= 0;) {
+ for (y = LI; --y >= 0;) {
for (x = CO; --x >= 0;) {
- fputchar(*p++);
+ addch(*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++;
- 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
@@ -419,18 +317,14 @@ main(argc, argv)
x1 = w->xpos[h];
y1 = w->ypos[h];
- if (--ref[y1][x1] == 0) {
- cursor(x1, y1);
- if (trail)
- fputchar(trail);
- }
+ if (--ref[y1][x1] == 0)
+ 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);
- abort();
- return(0);
+ endwin();
+ return(1);
case 1:
w->orientation = op->opts[0];
break;
@@ -438,10 +332,9 @@ main(argc, argv)
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]++;
}
}
@@ -451,22 +344,11 @@ void
onsig(signo)
int signo;
{
- tputs(tgetstr("ve", &tcp), 1, fputchar);
- tputs(tgetstr("cl", &tcp), 1, fputchar);
- tputs(tgetstr("te", &tcp), 1, fputchar);
- exit(0);
-}
-
-int
-fputchar(c)
- int c;
-{
- return(putchar(c));
+ sig_caught = 1;
}
void
nomem()
{
- (void)fprintf(stderr, "worms: not enough memory.\n");
- exit(1);
+ errx(1, "not enough memory.");
}