1999-07-30 06:23:27 +04:00
|
|
|
/* $NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 hubertf Exp $ */
|
1995-04-22 12:00:54 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-04-22 12:00:54 +04:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-12 06:12:45 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-04-22 12:00:54 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
1999-07-30 06:23:27 +04:00
|
|
|
__RCSID("$NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 hubertf Exp $");
|
1995-04-22 12:00:54 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
|
|
|
|
* @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
|
|
|
|
* @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
|
|
|
|
* @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
|
|
|
|
* @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
|
|
|
|
*
|
|
|
|
* Eric P. Scott
|
|
|
|
* Caltech High Energy Physics
|
|
|
|
* October, 1980
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
1995-04-22 12:00:54 +04:00
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
#include <curses.h>
|
|
|
|
#include <err.h>
|
1995-04-22 12:00:54 +04:00
|
|
|
#include <signal.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1993-04-19 01:52:18 +04:00
|
|
|
#include <stdlib.h>
|
1995-04-22 12:00:54 +04:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
static const struct options {
|
1993-03-21 12:45:37 +03:00
|
|
|
int nopts;
|
|
|
|
int opts[3];
|
|
|
|
}
|
|
|
|
normal[8] = {
|
|
|
|
{ 3, { 7, 0, 1 } },
|
|
|
|
{ 3, { 0, 1, 2 } },
|
|
|
|
{ 3, { 1, 2, 3 } },
|
|
|
|
{ 3, { 2, 3, 4 } },
|
|
|
|
{ 3, { 3, 4, 5 } },
|
|
|
|
{ 3, { 4, 5, 6 } },
|
|
|
|
{ 3, { 5, 6, 7 } },
|
|
|
|
{ 3, { 6, 7, 0 } }
|
|
|
|
}, upper[8] = {
|
|
|
|
{ 1, { 1, 0, 0 } },
|
|
|
|
{ 2, { 1, 2, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 2, { 4, 5, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } },
|
|
|
|
{ 2, { 1, 5, 0 } }
|
|
|
|
},
|
|
|
|
left[8] = {
|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0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } }
|
|
|
|
},
|
|
|
|
lowleft[8] = {
|
|
|
|
{ 3, { 7, 0, 1 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 1, 0, 0 } },
|
|
|
|
{ 2, { 1, 7, 0 } },
|
|
|
|
{ 1, { 7, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } }
|
|
|
|
},
|
|
|
|
lowright[8] = {
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 1, { 7, 0, 0 } },
|
|
|
|
{ 2, { 5, 7, 0 } },
|
|
|
|
{ 1, { 5, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } },
|
|
|
|
{ 0, { 0, 0, 0 } }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
static const char flavor[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
'O', '*', '#', '$', '%', '0', '@', '~'
|
|
|
|
};
|
1999-07-30 06:23:27 +04:00
|
|
|
static const short xinc[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
1, 1, 1, 0, -1, -1, -1, 0
|
|
|
|
}, yinc[] = {
|
|
|
|
-1, 0, 1, 1, 1, 0, -1, -1
|
|
|
|
};
|
|
|
|
static struct worm {
|
|
|
|
int orientation, head;
|
|
|
|
short *xpos, *ypos;
|
|
|
|
} *worm;
|
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
volatile sig_atomic_t sig_caught = 0;
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
int main __P((int, char **));
|
1998-09-13 19:27:25 +04:00
|
|
|
void nomem __P((void)) __attribute__((__noreturn__));
|
1999-07-30 06:23:27 +04:00
|
|
|
void onsig __P((int));
|
1993-04-19 01:52:18 +04:00
|
|
|
|
1995-04-22 12:00:54 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1995-04-22 12:00:54 +04:00
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-12 06:12:45 +04:00
|
|
|
int x, y, h, n;
|
|
|
|
struct worm *w;
|
1999-07-30 06:23:27 +04:00
|
|
|
const struct options *op;
|
1997-10-12 06:12:45 +04:00
|
|
|
short *ip;
|
1999-07-30 06:23:27 +04:00
|
|
|
int CO, LI, last, bottom, ch, length, number, trail;
|
1993-03-21 12:45:37 +03:00
|
|
|
short **ref;
|
1999-07-30 06:23:27 +04:00
|
|
|
const char *field;
|
|
|
|
char *mp;
|
|
|
|
unsigned int delay = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
mp = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
length = 16;
|
|
|
|
number = 3;
|
|
|
|
trail = ' ';
|
|
|
|
field = NULL;
|
1999-07-30 06:23:27 +04:00
|
|
|
while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
1999-07-30 06:23:27 +04:00
|
|
|
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;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'f':
|
|
|
|
field = "WORM";
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if ((length = atoi(optarg)) < 2 || length > 1024) {
|
1999-07-30 06:23:27 +04:00
|
|
|
errx(1, "invalid length (%d - %d).",
|
1993-03-21 12:45:37 +03:00
|
|
|
2, 1024);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
if ((number = atoi(optarg)) < 1) {
|
1999-07-30 06:23:27 +04:00
|
|
|
errx(1, "invalid number of worms.");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
trail = '.';
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
(void)fprintf(stderr,
|
1999-07-30 06:23:27 +04:00
|
|
|
"usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1995-04-22 12:00:54 +04:00
|
|
|
if (!(worm = malloc((size_t)number *
|
|
|
|
sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
1999-07-30 06:23:27 +04:00
|
|
|
initscr();
|
|
|
|
CO = COLS;
|
|
|
|
LI = LINES;
|
1994-04-05 04:21:20 +04:00
|
|
|
last = CO - 1;
|
|
|
|
bottom = LI - 1;
|
1995-04-22 12:00:54 +04:00
|
|
|
if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
1995-04-22 12:00:54 +04:00
|
|
|
if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
|
|
|
for (n = 0; n < LI; ++n) {
|
|
|
|
ref[n] = ip;
|
|
|
|
ip += CO;
|
|
|
|
}
|
|
|
|
for (ip = ref[0], n = LI * CO; --n >= 0;)
|
|
|
|
*ip++ = 0;
|
|
|
|
for (n = number, w = &worm[0]; --n >= 0; w++) {
|
|
|
|
w->orientation = w->head = 0;
|
1995-04-22 12:00:54 +04:00
|
|
|
if (!(ip = malloc((size_t)(length * sizeof(short)))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
|
|
|
w->xpos = ip;
|
|
|
|
for (x = length; --x >= 0;)
|
|
|
|
*ip++ = -1;
|
1995-04-22 12:00:54 +04:00
|
|
|
if (!(ip = malloc((size_t)(length * sizeof(short)))))
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem();
|
|
|
|
w->ypos = ip;
|
|
|
|
for (y = length; --y >= 0;)
|
|
|
|
*ip++ = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)signal(SIGHUP, onsig);
|
|
|
|
(void)signal(SIGINT, onsig);
|
|
|
|
(void)signal(SIGQUIT, onsig);
|
|
|
|
(void)signal(SIGSTOP, onsig);
|
|
|
|
(void)signal(SIGTSTP, onsig);
|
|
|
|
(void)signal(SIGTERM, onsig);
|
|
|
|
|
|
|
|
if (field) {
|
1999-07-30 06:23:27 +04:00
|
|
|
const char *p = field;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-07-30 06:23:27 +04:00
|
|
|
for (y = LI; --y >= 0;) {
|
1993-03-21 12:45:37 +03:00
|
|
|
for (x = CO; --x >= 0;) {
|
1999-07-30 06:23:27 +04:00
|
|
|
addch(*p++);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!*p)
|
|
|
|
p = field;
|
|
|
|
}
|
1999-07-30 06:23:27 +04:00
|
|
|
refresh();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
1999-07-30 06:23:27 +04:00
|
|
|
refresh();
|
|
|
|
if (sig_caught) {
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (delay) usleep(delay);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (n = 0, w = &worm[0]; n < number; n++, w++) {
|
|
|
|
if ((x = w->xpos[h = w->head]) < 0) {
|
1999-07-30 06:23:27 +04:00
|
|
|
mvaddch(y = w->ypos[h] = bottom,
|
|
|
|
x = w->xpos[h] = 0,
|
|
|
|
flavor[n % sizeof(flavor)]);
|
1993-03-21 12:45:37 +03:00
|
|
|
ref[y][x]++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
y = w->ypos[h];
|
|
|
|
if (++h == length)
|
|
|
|
h = 0;
|
|
|
|
if (w->xpos[w->head = h] >= 0) {
|
1997-10-12 06:12:45 +04:00
|
|
|
int x1, y1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
x1 = w->xpos[h];
|
|
|
|
y1 = w->ypos[h];
|
|
|
|
if (--ref[y1][x1] == 0) {
|
1999-07-30 06:23:27 +04:00
|
|
|
mvaddch(y1, x1, trail);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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:
|
1999-07-30 06:23:27 +04:00
|
|
|
refresh();
|
1993-03-21 12:45:37 +03:00
|
|
|
abort();
|
1997-10-12 06:12:45 +04:00
|
|
|
return(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
case 1:
|
|
|
|
w->orientation = op->opts[0];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
w->orientation =
|
|
|
|
op->opts[(int)random() % op->nopts];
|
|
|
|
}
|
1999-07-30 06:23:27 +04:00
|
|
|
mvaddch(y += yinc[w->orientation],
|
|
|
|
x += xinc[w->orientation],
|
|
|
|
flavor[n % sizeof(flavor)]);
|
1993-03-21 12:45:37 +03:00
|
|
|
ref[w->ypos[h] = y][w->xpos[h] = x]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1995-04-22 12:00:54 +04:00
|
|
|
onsig(signo)
|
1999-07-30 06:23:27 +04:00
|
|
|
int signo __attribute__((__unused__));
|
1995-04-22 12:00:54 +04:00
|
|
|
{
|
1999-07-30 06:23:27 +04:00
|
|
|
sig_caught = 1;
|
1995-04-22 12:00:54 +04:00
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
nomem()
|
|
|
|
{
|
1999-07-30 06:23:27 +04:00
|
|
|
errx(1, "not enough memory.");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|