Home | History | Annotate | Line # | Download | only in worms
worms.c revision 1.8
      1 /*	$NetBSD: worms.c,v 1.8 1995/04/22 08:09:22 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1980, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)worms.c	8.1 (Berkeley) 5/31/93";
     45 #else
     46 static char rcsid[] = "$NetBSD: worms.c,v 1.8 1995/04/22 08:09:22 cgd Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 /*
     51  *
     52  *	 @@@        @@@    @@@@@@@@@@     @@@@@@@@@@@    @@@@@@@@@@@@
     53  *	 @@@        @@@   @@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@@@@@
     54  *	 @@@        @@@  @@@@      @@@@   @@@@           @@@@ @@@  @@@@
     55  *	 @@@   @@   @@@  @@@        @@@   @@@            @@@  @@@   @@@
     56  *	 @@@  @@@@  @@@  @@@        @@@   @@@            @@@  @@@   @@@
     57  *	 @@@@ @@@@ @@@@  @@@        @@@   @@@            @@@  @@@   @@@
     58  *	  @@@@@@@@@@@@   @@@@      @@@@   @@@            @@@  @@@   @@@
     59  *	   @@@@  @@@@     @@@@@@@@@@@@    @@@            @@@  @@@   @@@
     60  *	    @@    @@       @@@@@@@@@@     @@@            @@@  @@@   @@@
     61  *
     62  *				 Eric P. Scott
     63  *			  Caltech High Energy Physics
     64  *				 October, 1980
     65  *
     66  */
     67 #include <sys/types.h>
     68 #include <sys/ioctl.h>
     69 
     70 #include <signal.h>
     71 #include <stdio.h>
     72 #include <stdlib.h>
     73 #include <termios.h>
     74 #include <unistd.h>
     75 
     76 static struct options {
     77 	int nopts;
     78 	int opts[3];
     79 }
     80 	normal[8] = {
     81 	{ 3, { 7, 0, 1 } },
     82 	{ 3, { 0, 1, 2 } },
     83 	{ 3, { 1, 2, 3 } },
     84 	{ 3, { 2, 3, 4 } },
     85 	{ 3, { 3, 4, 5 } },
     86 	{ 3, { 4, 5, 6 } },
     87 	{ 3, { 5, 6, 7 } },
     88 	{ 3, { 6, 7, 0 } }
     89 },	upper[8] = {
     90 	{ 1, { 1, 0, 0 } },
     91 	{ 2, { 1, 2, 0 } },
     92 	{ 0, { 0, 0, 0 } },
     93 	{ 0, { 0, 0, 0 } },
     94 	{ 0, { 0, 0, 0 } },
     95 	{ 2, { 4, 5, 0 } },
     96 	{ 1, { 5, 0, 0 } },
     97 	{ 2, { 1, 5, 0 } }
     98 },
     99 	left[8] = {
    100 	{ 0, { 0, 0, 0 } },
    101 	{ 0, { 0, 0, 0 } },
    102 	{ 0, { 0, 0, 0 } },
    103 	{ 2, { 2, 3, 0 } },
    104 	{ 1, { 3, 0, 0 } },
    105 	{ 2, { 3, 7, 0 } },
    106 	{ 1, { 7, 0, 0 } },
    107 	{ 2, { 7, 0, 0 } }
    108 },
    109 	right[8] = {
    110 	{ 1, { 7, 0, 0 } },
    111 	{ 2, { 3, 7, 0 } },
    112 	{ 1, { 3, 0, 0 } },
    113 	{ 2, { 3, 4, 0 } },
    114 	{ 0, { 0, 0, 0 } },
    115 	{ 0, { 0, 0, 0 } },
    116 	{ 0, { 0, 0, 0 } },
    117 	{ 2, { 6, 7, 0 } }
    118 },
    119 	lower[8] = {
    120 	{ 0, { 0, 0, 0 } },
    121 	{ 2, { 0, 1, 0 } },
    122 	{ 1, { 1, 0, 0 } },
    123 	{ 2, { 1, 5, 0 } },
    124 	{ 1, { 5, 0, 0 } },
    125 	{ 2, { 5, 6, 0 } },
    126 	{ 0, { 0, 0, 0 } },
    127 	{ 0, { 0, 0, 0 } }
    128 },
    129 	upleft[8] = {
    130 	{ 0, { 0, 0, 0 } },
    131 	{ 0, { 0, 0, 0 } },
    132 	{ 0, { 0, 0, 0 } },
    133 	{ 0, { 0, 0, 0 } },
    134 	{ 0, { 0, 0, 0 } },
    135 	{ 1, { 3, 0, 0 } },
    136 	{ 2, { 1, 3, 0 } },
    137 	{ 1, { 1, 0, 0 } }
    138 },
    139 	upright[8] = {
    140 	{ 2, { 3, 5, 0 } },
    141 	{ 1, { 3, 0, 0 } },
    142 	{ 0, { 0, 0, 0 } },
    143 	{ 0, { 0, 0, 0 } },
    144 	{ 0, { 0, 0, 0 } },
    145 	{ 0, { 0, 0, 0 } },
    146 	{ 0, { 0, 0, 0 } },
    147 	{ 1, { 5, 0, 0 } }
    148 },
    149 	lowleft[8] = {
    150 	{ 3, { 7, 0, 1 } },
    151 	{ 0, { 0, 0, 0 } },
    152 	{ 0, { 0, 0, 0 } },
    153 	{ 1, { 1, 0, 0 } },
    154 	{ 2, { 1, 7, 0 } },
    155 	{ 1, { 7, 0, 0 } },
    156 	{ 0, { 0, 0, 0 } },
    157 	{ 0, { 0, 0, 0 } }
    158 },
    159 	lowright[8] = {
    160 	{ 0, { 0, 0, 0 } },
    161 	{ 1, { 7, 0, 0 } },
    162 	{ 2, { 5, 7, 0 } },
    163 	{ 1, { 5, 0, 0 } },
    164 	{ 0, { 0, 0, 0 } },
    165 	{ 0, { 0, 0, 0 } },
    166 	{ 0, { 0, 0, 0 } },
    167 	{ 0, { 0, 0, 0 } }
    168 };
    169 
    170 #define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, fputchar)
    171 
    172 char *tcp;
    173 static char	flavor[] = {
    174 	'O', '*', '#', '$', '%', '0', '@', '~'
    175 };
    176 static short	xinc[] = {
    177 	1,  1,  1,  0, -1, -1, -1,  0
    178 }, yinc[] = {
    179 	-1,  0,  1,  1,  1,  0, -1, -1
    180 };
    181 static struct	worm {
    182 	int orientation, head;
    183 	short *xpos, *ypos;
    184 } *worm;
    185 
    186 void	 fputchar __P((int));
    187 void	 onsig __P((int));
    188 char	*tgetstr __P((char *, char **));
    189 char	*tgoto __P((char *, int, int));
    190 int	 tputs __P((char *, int, void (*)(int)));
    191 
    192 int
    193 main(argc, argv)
    194 	int argc;
    195 	char *argv[];
    196 {
    197 	extern int optind;
    198 	extern char *optarg, *UP;
    199 	register int x, y, h, n;
    200 	register struct worm *w;
    201 	register struct options *op;
    202 	register short *ip;
    203 	register char *term;
    204 	int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap;
    205 	short **ref;
    206 	char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR;
    207 	char *field, tcb[100], *mp;
    208 	long random();
    209 	struct termios ti;
    210 #ifdef TIOCGWINSZ
    211 	struct winsize ws;
    212 #endif
    213 
    214 	length = 16;
    215 	number = 3;
    216 	trail = ' ';
    217 	field = NULL;
    218 	while ((ch = getopt(argc, argv, "fl:n:t")) != EOF)
    219 		switch(ch) {
    220 		case 'f':
    221 			field = "WORM";
    222 			break;
    223 		case 'l':
    224 			if ((length = atoi(optarg)) < 2 || length > 1024) {
    225 				(void)fprintf(stderr,
    226 				    "worms: invalid length (%d - %d).\n",
    227 				     2, 1024);
    228 				exit(1);
    229 			}
    230 			break;
    231 		case 'n':
    232 			if ((number = atoi(optarg)) < 1) {
    233 				(void)fprintf(stderr,
    234 				    "worms: invalid number of worms.\n");
    235 				exit(1);
    236 			}
    237 			break;
    238 		case 't':
    239 			trail = '.';
    240 			break;
    241 		case '?':
    242 		default:
    243 			(void)fprintf(stderr,
    244 			    "usage: worms [-ft] [-l length] [-n number]\n");
    245 			exit(1);
    246 		}
    247 
    248 	if (!(term = getenv("TERM"))) {
    249 		(void)fprintf(stderr, "worms: no TERM environment variable.\n");
    250 		exit(1);
    251 	}
    252 	if (!(worm = malloc((size_t)number *
    253 	    sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
    254 		nomem();
    255 	if (tgetent(mp, term) <= 0) {
    256 		(void)fprintf(stderr, "worms: %s: unknown terminal type.\n",
    257 		    term);
    258 		exit(1);
    259 	}
    260 	tcp = tcb;
    261 	if (!(CM = tgetstr("cm", &tcp))) {
    262 		(void)fprintf(stderr,
    263 		    "worms: terminal incapable of cursor motion.\n");
    264 		exit(1);
    265 	}
    266 	AL = tgetstr("al", &tcp);
    267 	BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp);
    268 	EI = tgetstr("ei", &tcp);
    269 	HO = tgetstr("ho", &tcp);
    270 	IC = tgetstr("ic", &tcp);
    271 	IM = tgetstr("im", &tcp);
    272 	IN = tgetflag("in");
    273 	IP = tgetstr("ip", &tcp);
    274 	SR = tgetstr("sr", &tcp);
    275 	UP = tgetstr("up", &tcp);
    276 #ifdef TIOCGWINSZ
    277 	if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
    278 	    ws.ws_col && ws.ws_row) {
    279 		CO = ws.ws_col;
    280 		LI = ws.ws_row;
    281 	} else
    282 #endif
    283 	{
    284 		if ((CO = tgetnum("co")) <= 0)
    285 			CO = 80;
    286 		if ((LI = tgetnum("li")) <= 0)
    287 			LI = 24;
    288 	}
    289 	last = CO - 1;
    290 	bottom = LI - 1;
    291 	tcgetattr(fileno(stdout), &ti);
    292 	Wrap = tgetflag("am");
    293 	if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
    294 		nomem();
    295 	if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
    296 		nomem();
    297 	for (n = 0; n < LI; ++n) {
    298 		ref[n] = ip;
    299 		ip += CO;
    300 	}
    301 	for (ip = ref[0], n = LI * CO; --n >= 0;)
    302 		*ip++ = 0;
    303 	if (Wrap)
    304 		ref[bottom][last] = 1;
    305 	for (n = number, w = &worm[0]; --n >= 0; w++) {
    306 		w->orientation = w->head = 0;
    307 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    308 			nomem();
    309 		w->xpos = ip;
    310 		for (x = length; --x >= 0;)
    311 			*ip++ = -1;
    312 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    313 			nomem();
    314 		w->ypos = ip;
    315 		for (y = length; --y >= 0;)
    316 			*ip++ = -1;
    317 	}
    318 
    319 	(void)signal(SIGHUP, onsig);
    320 	(void)signal(SIGINT, onsig);
    321 	(void)signal(SIGQUIT, onsig);
    322 	(void)signal(SIGSTOP, onsig);
    323 	(void)signal(SIGTSTP, onsig);
    324 	(void)signal(SIGTERM, onsig);
    325 
    326 	tputs(tgetstr("ti", &tcp), 1, fputchar);
    327 	tputs(tgetstr("cl", &tcp), 1, fputchar);
    328 	if (field) {
    329 		register char *p = field;
    330 
    331 		for (y = bottom; --y >= 0;) {
    332 			for (x = CO; --x >= 0;) {
    333 				fputchar(*p++);
    334 				if (!*p)
    335 					p = field;
    336 			}
    337 			if (!Wrap)
    338 				fputchar('\n');
    339 			(void)fflush(stdout);
    340 		}
    341 		if (Wrap) {
    342 			if (IM && !IN) {
    343 				for (x = last; --x > 0;) {
    344 					fputchar(*p++);
    345 					if (!*p)
    346 						p = field;
    347 				}
    348 				y = *p++;
    349 				if (!*p)
    350 					p = field;
    351 				fputchar(*p);
    352 				if (BC)
    353 					tputs(BC, 1, fputchar);
    354 				else
    355 					cursor(last - 1, bottom);
    356 				tputs(IM, 1, fputchar);
    357 				if (IC)
    358 					tputs(IC, 1, fputchar);
    359 				fputchar(y);
    360 				if (IP)
    361 					tputs(IP, 1, fputchar);
    362 				tputs(EI, 1, fputchar);
    363 			}
    364 			else if (SR || AL) {
    365 				if (HO)
    366 					tputs(HO, 1, fputchar);
    367 				else
    368 					cursor(0, 0);
    369 				if (SR)
    370 					tputs(SR, 1, fputchar);
    371 				else
    372 					tputs(AL, LI, fputchar);
    373 				for (x = CO; --x >= 0;) {
    374 					fputchar(*p++);
    375 					if (!*p)
    376 						p = field;
    377 				}
    378 			}
    379 			else for (x = last; --x >= 0;) {
    380 				fputchar(*p++);
    381 				if (!*p)
    382 					p = field;
    383 			}
    384 		}
    385 		else for (x = CO; --x >= 0;) {
    386 			fputchar(*p++);
    387 			if (!*p)
    388 				p = field;
    389 		}
    390 	}
    391 	for (;;) {
    392 		(void)fflush(stdout);
    393 		for (n = 0, w = &worm[0]; n < number; n++, w++) {
    394 			if ((x = w->xpos[h = w->head]) < 0) {
    395 				cursor(x = w->xpos[h] = 0,
    396 				     y = w->ypos[h] = bottom);
    397 				fputchar(flavor[n % sizeof(flavor)]);
    398 				ref[y][x]++;
    399 			}
    400 			else
    401 				y = w->ypos[h];
    402 			if (++h == length)
    403 				h = 0;
    404 			if (w->xpos[w->head = h] >= 0) {
    405 				register int x1, y1;
    406 
    407 				x1 = w->xpos[h];
    408 				y1 = w->ypos[h];
    409 				if (--ref[y1][x1] == 0) {
    410 					cursor(x1, y1);
    411 					if (trail)
    412 						fputchar(trail);
    413 				}
    414 			}
    415 			op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
    416 			switch (op->nopts) {
    417 			case 0:
    418 				(void)fflush(stdout);
    419 				abort();
    420 				return;
    421 			case 1:
    422 				w->orientation = op->opts[0];
    423 				break;
    424 			default:
    425 				w->orientation =
    426 				    op->opts[(int)random() % op->nopts];
    427 			}
    428 			cursor(x += xinc[w->orientation],
    429 			    y += yinc[w->orientation]);
    430 			if (!Wrap || x != last || y != bottom)
    431 				fputchar(flavor[n % sizeof(flavor)]);
    432 			ref[w->ypos[h] = y][w->xpos[h] = x]++;
    433 		}
    434 	}
    435 }
    436 
    437 void
    438 onsig(signo)
    439 	int signo;
    440 {
    441 	tputs(tgetstr("cl", &tcp), 1, fputchar);
    442 	tputs(tgetstr("te", &tcp), 1, fputchar);
    443 	exit(0);
    444 }
    445 
    446 void
    447 fputchar(c)
    448 	int c;
    449 {
    450 	(void)putchar(c);
    451 }
    452 
    453 nomem()
    454 {
    455 	(void)fprintf(stderr, "worms: not enough memory.\n");
    456 	exit(1);
    457 }
    458