mirror of
https://github.com/opnsense/src.git
synced 2026-06-09 00:32:25 -04:00
Round two of MetBSD games merges.
Submitted by: Stephen J. Roznowski <sjr@home.net>
This commit is contained in:
parent
9045b882d5
commit
5b85aead34
10 changed files with 190 additions and 342 deletions
|
|
@ -5,8 +5,8 @@ CFLAGS+=-DBSD -I${.CURDIR} -I.
|
|||
SRCS= extern.c grammar.y graphics.c input.c lex.l list.c log.c \
|
||||
main.c tunable.c update.c y.tab.h
|
||||
MAN6= atc.6
|
||||
DPADD= ${LIBL} ${LIBM} ${LIBCURSES} ${LIBTERMCAP} ${LIBCOMPAT}
|
||||
LDADD= -ll -lm -lcurses -ltermcap -lcompat
|
||||
DPADD= ${LIBL} ${LIBM} ${LIBCURSES} ${LIBTERMCAP}
|
||||
LDADD= -ll -lm -lcurses -ltermcap
|
||||
GAMES= Game_List Killer crossover default easy game_2
|
||||
HIDEGAME=hidegame
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,12 @@ static char sccsid[] = "@(#)log.c 8.1 (Berkeley) 5/31/93";
|
|||
#include "include.h"
|
||||
#include "pathnames.h"
|
||||
|
||||
#ifdef SYSV
|
||||
#include <sys/utsname.h>
|
||||
#endif
|
||||
|
||||
static FILE *score_fp;
|
||||
|
||||
int
|
||||
compar(va, vb)
|
||||
const void *va, *vb;
|
||||
|
|
@ -95,44 +101,68 @@ timestr(t)
|
|||
return (s);
|
||||
}
|
||||
|
||||
log_score(list_em)
|
||||
void
|
||||
open_score_file()
|
||||
{
|
||||
register int i, fd, num_scores = 0, good, changed = 0, found = 0;
|
||||
mode_t old_mask;
|
||||
int score_fd;
|
||||
int flags;
|
||||
|
||||
old_mask = umask(0);
|
||||
score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
|
||||
umask(old_mask);
|
||||
if (score_fd < 0) {
|
||||
warn("open %s", _PATH_SCORE);
|
||||
return;
|
||||
}
|
||||
/* Set the close-on-exec flag. If this fails for any reason, quit
|
||||
* rather than leave the score file open to tampering. */
|
||||
flags = fcntl(score_fd, F_GETFD);
|
||||
if (flags < 0)
|
||||
err(1, "fcntl F_GETFD");
|
||||
flags |= FD_CLOEXEC;
|
||||
if (fcntl(score_fd, F_SETFD, flags) == -1)
|
||||
err(1, "fcntl F_SETFD");
|
||||
/*
|
||||
* This is done to take advantage of stdio, while still
|
||||
* allowing a O_CREAT during the open(2) of the log file.
|
||||
*/
|
||||
score_fp = fdopen(score_fd, "r+");
|
||||
if (score_fp == NULL) {
|
||||
warn("fdopen %s", _PATH_SCORE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
log_score(list_em)
|
||||
int list_em;
|
||||
{
|
||||
int i, num_scores = 0, good, changed = 0, found = 0;
|
||||
struct passwd *pw;
|
||||
FILE *fp;
|
||||
char *cp, *index(), *rindex();
|
||||
char *cp;
|
||||
SCORE score[100], thisscore;
|
||||
#ifdef SYSV
|
||||
struct utsname name;
|
||||
#endif
|
||||
|
||||
umask(0);
|
||||
fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
|
||||
if (fd < 0) {
|
||||
perror(_PATH_SCORE);
|
||||
return (-1);
|
||||
}
|
||||
/*
|
||||
* This is done to take advantage of stdio, while still
|
||||
* allowing a O_CREAT during the open(2) of the log file.
|
||||
*/
|
||||
fp = fdopen(fd, "r+");
|
||||
if (fp == NULL) {
|
||||
perror(_PATH_SCORE);
|
||||
if (score_fp == NULL) {
|
||||
warnx("no score file available");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
#ifdef BSD
|
||||
if (flock(fileno(fp), LOCK_EX) < 0)
|
||||
if (flock(fileno(score_fp), LOCK_EX) < 0)
|
||||
#endif
|
||||
#ifdef SYSV
|
||||
while (lockf(fileno(fp), F_LOCK, 1) < 0)
|
||||
while (lockf(fileno(score_fp), F_LOCK, 1) < 0)
|
||||
#endif
|
||||
{
|
||||
perror("flock");
|
||||
warn("flock %s", _PATH_SCORE);
|
||||
return (-1);
|
||||
}
|
||||
for (;;) {
|
||||
good = fscanf(fp, "%s %s %s %d %d %d",
|
||||
good = fscanf(score_fp, SCORE_SCANF_FMT,
|
||||
score[num_scores].name,
|
||||
score[num_scores].host,
|
||||
score[num_scores].game,
|
||||
|
|
@ -146,7 +176,7 @@ log_score(list_em)
|
|||
if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
|
||||
fprintf(stderr,
|
||||
"getpwuid failed for uid %d. Who are you?\n",
|
||||
getuid());
|
||||
(int)getuid());
|
||||
return (-1);
|
||||
}
|
||||
strcpy(thisscore.name, pw->pw_name);
|
||||
|
|
@ -216,12 +246,21 @@ log_score(list_em)
|
|||
else
|
||||
puts("You made the top players list!");
|
||||
qsort(score, num_scores, sizeof (*score), compar);
|
||||
rewind(fp);
|
||||
rewind(score_fp);
|
||||
for (i = 0; i < num_scores; i++)
|
||||
fprintf(fp, "%s %s %s %d %d %d\n",
|
||||
fprintf(score_fp, "%s %s %s %d %d %d\n",
|
||||
score[i].name, score[i].host,
|
||||
score[i].game, score[i].planes,
|
||||
score[i].time, score[i].real_time);
|
||||
fflush(score_fp);
|
||||
if (ferror(score_fp))
|
||||
warn("error writing %s", _PATH_SCORE);
|
||||
/* It is just possible that updating an entry could
|
||||
* have reduced the length of the file, so we
|
||||
* truncate it. The lseek is required for stream/fd
|
||||
* synchronisation by POSIX.1. */
|
||||
lseek(fileno(score_fp), 0, SEEK_END);
|
||||
ftruncate(fileno(score_fp), ftell(score_fp));
|
||||
} else {
|
||||
if (found)
|
||||
puts("You didn't beat your previous score.");
|
||||
|
|
@ -231,12 +270,12 @@ log_score(list_em)
|
|||
putchar('\n');
|
||||
}
|
||||
#ifdef BSD
|
||||
flock(fileno(fp), LOCK_UN);
|
||||
flock(fileno(score_fp), LOCK_UN);
|
||||
#endif
|
||||
#ifdef SYSV
|
||||
/* lock will evaporate upon close */
|
||||
#endif
|
||||
fclose(fp);
|
||||
fclose(score_fp);
|
||||
printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
|
||||
"game", "time", "real time", "planes safe");
|
||||
puts("-------------------------------------------------------------------------------");
|
||||
|
|
|
|||
|
|
@ -72,6 +72,10 @@ main(ac, av)
|
|||
extern const char *default_game(), *okay_game();
|
||||
extern void log_score(), quit(), update();
|
||||
|
||||
/* Open the score file then revoke setgid privileges */
|
||||
open_score_file();
|
||||
setregid(getgid(), getgid());
|
||||
|
||||
start_time = time(0);
|
||||
|
||||
name = *av++;
|
||||
|
|
|
|||
|
|
@ -105,6 +105,8 @@ typedef struct {
|
|||
int real_time;
|
||||
} SCORE;
|
||||
|
||||
#define SCORE_SCANF_FMT "%9s %255s %255s %d %d %d"
|
||||
|
||||
typedef struct displacement {
|
||||
int dx;
|
||||
int dy;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
PROG= rain
|
||||
MAN6= rain.6
|
||||
DPADD= ${LIBTERMCAP} ${LIBCOMPAT}
|
||||
LDADD= -ltermcap -lcompat
|
||||
DPADD= ${LIBCURSES} ${LIBTERMCAP}
|
||||
LDADD= -lcurses -ltermcap
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
|
|
|||
|
|
@ -37,18 +37,16 @@
|
|||
rain \- animated raindrops display
|
||||
.SH SYNOPSIS
|
||||
rain
|
||||
[ \-d delay ]
|
||||
.SH DESCRIPTION
|
||||
.PP
|
||||
.ad b
|
||||
.IR Rain 's
|
||||
display is modeled after the VAX/VMS program of the same name.
|
||||
The terminal has to be set for 9600 baud to obtain the proper effect.
|
||||
.PP
|
||||
As with all programs that use
|
||||
.IR termcap ,
|
||||
the TERM environment
|
||||
variable must be set (and exported) to the type of the terminal being used.
|
||||
.SH FILES
|
||||
/etc/termcap
|
||||
To obtain the proper effect, either the terminal must be set for 9600
|
||||
baud or the
|
||||
.Fl d
|
||||
option must be used to specify a delay, in milliseconds, between each
|
||||
update. A reasonable delay is 120; the default is 0.
|
||||
.SH AUTHOR
|
||||
Eric P. Scott
|
||||
|
|
|
|||
|
|
@ -47,202 +47,97 @@ static char sccsid[] = "@(#)rain.c 8.1 (Berkeley) 5/31/93";
|
|||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <curses.h>
|
||||
#include <err.h>
|
||||
#include <stdio.h>
|
||||
#ifdef USG
|
||||
#include <termio.h>
|
||||
#else
|
||||
#include <sgtty.h>
|
||||
#endif
|
||||
#include <signal.h>
|
||||
#include <termcap.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
|
||||
|
||||
#ifdef USG
|
||||
static struct termio sg, old_tty;
|
||||
#else
|
||||
static struct sgttyb sg, old_tty;
|
||||
#endif
|
||||
|
||||
int fputchar();
|
||||
char *LL, *TE;
|
||||
volatile sig_atomic_t sig_caught = 0;
|
||||
|
||||
int
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
extern short ospeed;
|
||||
extern char *UP;
|
||||
register int x, y, j;
|
||||
register char *CM, *BC, *DN, *ND, *term;
|
||||
char *TI, *tcp, *mp, tcb[100];
|
||||
long cols, lines;
|
||||
int xpos[5], ypos[5];
|
||||
unsigned int delay = 0;
|
||||
int ch;
|
||||
static void onsig();
|
||||
|
||||
if (!(term = getenv("TERM"))) {
|
||||
fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
|
||||
exit(1);
|
||||
}
|
||||
if (!(mp = malloc((u_int)1024))) {
|
||||
fprintf(stderr, "%s: out of space.\n", *argv);
|
||||
exit(1);
|
||||
}
|
||||
if (tgetent(mp, term) <= 0) {
|
||||
fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
|
||||
exit(1);
|
||||
}
|
||||
tcp = tcb;
|
||||
if (!(CM = tgetstr("cm", &tcp))) {
|
||||
fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
|
||||
exit(1);
|
||||
}
|
||||
if (!(BC = tgetstr("bc", &tcp)))
|
||||
BC = "\b";
|
||||
if (!(DN = tgetstr("dn", &tcp)))
|
||||
DN = "\n";
|
||||
if (!(ND = tgetstr("nd", &tcp)))
|
||||
ND = " ";
|
||||
if ((cols = tgetnum("co")) == -1)
|
||||
cols = 80;
|
||||
if ((lines = tgetnum("li")) == -1)
|
||||
lines = 24;
|
||||
cols -= 4;
|
||||
lines -= 4;
|
||||
TE = tgetstr("te", &tcp);
|
||||
TI = tgetstr("ti", &tcp);
|
||||
UP = tgetstr("up", &tcp);
|
||||
if (!(LL = tgetstr("ll", &tcp))) {
|
||||
if (!(LL = malloc((u_int)10))) {
|
||||
fprintf(stderr, "%s: out of space.\n", *argv);
|
||||
while ((ch = getopt(argc, argv, "d:h")) != -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 'h':
|
||||
default:
|
||||
(void)fprintf(stderr, "usage: rain [-d delay]\n");
|
||||
exit(1);
|
||||
}
|
||||
(void)strcpy(LL, tgoto(CM, 0, 23));
|
||||
}
|
||||
srandomdev();
|
||||
#ifdef USG
|
||||
ioctl(1, TCGETA, &sg);
|
||||
ospeed = sg.c_cflag&CBAUD;
|
||||
#else
|
||||
gtty(1, &sg);
|
||||
ospeed = sg.sg_ospeed;
|
||||
#endif
|
||||
|
||||
initscr();
|
||||
cols = COLS - 4;
|
||||
lines = 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);
|
||||
#ifdef USG
|
||||
ioctl(1, TCGETA, &old_tty); /* save tty bits for exit */
|
||||
ioctl(1, TCGETA, &sg);
|
||||
sg.c_iflag &= ~ICRNL;
|
||||
sg.c_oflag &= ~ONLCR;
|
||||
sg.c_lflag &= ~ECHO;
|
||||
ioctl(1, TCSETAW, &sg);
|
||||
#else
|
||||
gtty(1, &old_tty); /* save tty bits for exit */
|
||||
gtty(1, &sg);
|
||||
sg.sg_flags &= ~(CRMOD|ECHO);
|
||||
stty(1, &sg);
|
||||
#endif
|
||||
if (TI)
|
||||
tputs(TI, 1, fputchar);
|
||||
tputs(tgetstr("cl", &tcp), 1, fputchar);
|
||||
(void)fflush(stdout);
|
||||
|
||||
for (j = 4; j >= 0; --j) {
|
||||
xpos[j] = random() % cols + 2;
|
||||
ypos[j] = random() % lines + 2;
|
||||
}
|
||||
for (j = 0;;) {
|
||||
if (sig_caught) {
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
||||
x = random() % cols + 2;
|
||||
y = random() % lines + 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);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
onsig()
|
||||
{
|
||||
tputs(LL, 1, fputchar);
|
||||
if (TE)
|
||||
tputs(TE, 1, fputchar);
|
||||
(void)fflush(stdout);
|
||||
#ifdef USG
|
||||
ioctl(1, TCSETAW, &old_tty);
|
||||
#else
|
||||
stty(1, &old_tty);
|
||||
#endif
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int
|
||||
fputchar(c)
|
||||
int c;
|
||||
{
|
||||
putchar(c);
|
||||
sig_caught = 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
PROG= worms
|
||||
MAN6= worms.6
|
||||
DPADD= ${LIBCURSES} ${LIBTERMCAP} ${LIBCOMPAT}
|
||||
LDADD= -lcurses -ltermcap -lcompat
|
||||
DPADD= ${LIBCURSES} ${LIBTERMCAP}
|
||||
LDADD= -lcurses -ltermcap
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
|
|
|||
|
|
@ -39,21 +39,26 @@
|
|||
.Sh SYNOPSIS
|
||||
.Nm worms
|
||||
.Op Fl ft
|
||||
.Op Fl d Ar delay
|
||||
.Op Fl l Ar length
|
||||
.Op Fl n Ar number
|
||||
.Sh DESCRIPTION
|
||||
A UNIX version of the DEC-2136 program ``worms''.
|
||||
A UNIX 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.
|
||||
|
|
|
|||
|
|
@ -59,13 +59,14 @@ static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
|
|||
*
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sgtty.h>
|
||||
#include <curses.h>
|
||||
#include <err.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static struct options {
|
||||
static const struct options {
|
||||
int nopts;
|
||||
int opts[3];
|
||||
}
|
||||
|
|
@ -159,13 +160,11 @@ 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
|
||||
|
|
@ -175,11 +174,9 @@ static struct worm {
|
|||
short *xpos, *ypos;
|
||||
} *worm;
|
||||
|
||||
void fputchar __P((int));
|
||||
volatile sig_atomic_t sig_caught = 0;
|
||||
void onsig __P((int));
|
||||
char *tgetstr __P((char *, char **));
|
||||
char *tgoto __P((char *, int, int));
|
||||
int tputs __P((char *, int, void (*)(int)));
|
||||
void nomem(void);
|
||||
|
||||
int
|
||||
main(argc, argv)
|
||||
|
|
@ -187,42 +184,42 @@ main(argc, argv)
|
|||
char *argv[];
|
||||
{
|
||||
extern int optind;
|
||||
extern char *optarg, *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;
|
||||
extern char *optarg;
|
||||
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;
|
||||
long random();
|
||||
struct sgttyb tt;
|
||||
extern short ospeed;
|
||||
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':
|
||||
|
|
@ -231,47 +228,18 @@ main(argc, argv)
|
|||
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 (gtty(1, &tt) == 0)
|
||||
ospeed = tt.sg_ospeed;
|
||||
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);
|
||||
if ((CO = tgetnum("co")) <= 0)
|
||||
CO = 80;
|
||||
initscr();
|
||||
CO = COLS;
|
||||
LI = LINES;
|
||||
last = CO - 1;
|
||||
EI = tgetstr("ei", &tcp);
|
||||
HO = tgetstr("ho", &tcp);
|
||||
IC = tgetstr("ic", &tcp);
|
||||
IM = tgetstr("im", &tcp);
|
||||
IN = tgetflag("in");
|
||||
IP = tgetstr("ip", &tcp);
|
||||
if ((LI = tgetnum("li")) <= 0)
|
||||
LI = 24;
|
||||
bottom = LI - 1;
|
||||
SR = tgetstr("sr", &tcp);
|
||||
UP = tgetstr("up", &tcp);
|
||||
Wrap = tgetflag("am");
|
||||
if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
|
||||
nomem();
|
||||
if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
|
||||
|
|
@ -282,8 +250,6 @@ main(argc, argv)
|
|||
}
|
||||
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)))))
|
||||
|
|
@ -305,78 +271,29 @@ main(argc, argv)
|
|||
(void)signal(SIGTSTP, onsig);
|
||||
(void)signal(SIGTERM, onsig);
|
||||
|
||||
tputs(tgetstr("ti", &tcp), 1, fputchar);
|
||||
tputs(tgetstr("cl", &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);
|
||||
}
|
||||
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
|
||||
|
|
@ -384,22 +301,20 @@ main(argc, argv)
|
|||
if (++h == length)
|
||||
h = 0;
|
||||
if (w->xpos[w->head = h] >= 0) {
|
||||
register int x1, y1;
|
||||
int x1, y1;
|
||||
|
||||
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;
|
||||
return(1);
|
||||
case 1:
|
||||
w->orientation = op->opts[0];
|
||||
break;
|
||||
|
|
@ -407,10 +322,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]++;
|
||||
}
|
||||
}
|
||||
|
|
@ -420,20 +334,11 @@ void
|
|||
onsig(signo)
|
||||
int signo;
|
||||
{
|
||||
tputs(tgetstr("cl", &tcp), 1, fputchar);
|
||||
tputs(tgetstr("te", &tcp), 1, fputchar);
|
||||
exit(0);
|
||||
sig_caught = 1;
|
||||
}
|
||||
|
||||
void
|
||||
fputchar(c)
|
||||
int c;
|
||||
{
|
||||
(void)putchar(c);
|
||||
}
|
||||
|
||||
nomem()
|
||||
{
|
||||
(void)fprintf(stderr, "worms: not enough memory.\n");
|
||||
exit(1);
|
||||
errx(1, "not enough memory.");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue