Home | History | Annotate | Line # | Download | only in worms
worms.c revision 1.6
      1 /*	$NetBSD: worms.c,v 1.6 1995/04/22 08:00:58 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.6 1995/04/22 08:00:58 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 
     69 #include <signal.h>
     70 #include <stdio.h>
     71 #include <stdlib.h>
     72 #include <unistd.h>
     73 
     74 static struct options {
     75 	int nopts;
     76 	int opts[3];
     77 }
     78 	normal[8] = {
     79 	{ 3, { 7, 0, 1 } },
     80 	{ 3, { 0, 1, 2 } },
     81 	{ 3, { 1, 2, 3 } },
     82 	{ 3, { 2, 3, 4 } },
     83 	{ 3, { 3, 4, 5 } },
     84 	{ 3, { 4, 5, 6 } },
     85 	{ 3, { 5, 6, 7 } },
     86 	{ 3, { 6, 7, 0 } }
     87 },	upper[8] = {
     88 	{ 1, { 1, 0, 0 } },
     89 	{ 2, { 1, 2, 0 } },
     90 	{ 0, { 0, 0, 0 } },
     91 	{ 0, { 0, 0, 0 } },
     92 	{ 0, { 0, 0, 0 } },
     93 	{ 2, { 4, 5, 0 } },
     94 	{ 1, { 5, 0, 0 } },
     95 	{ 2, { 1, 5, 0 } }
     96 },
     97 	left[8] = {
     98 	{ 0, { 0, 0, 0 } },
     99 	{ 0, { 0, 0, 0 } },
    100 	{ 0, { 0, 0, 0 } },
    101 	{ 2, { 2, 3, 0 } },
    102 	{ 1, { 3, 0, 0 } },
    103 	{ 2, { 3, 7, 0 } },
    104 	{ 1, { 7, 0, 0 } },
    105 	{ 2, { 7, 0, 0 } }
    106 },
    107 	right[8] = {
    108 	{ 1, { 7, 0, 0 } },
    109 	{ 2, { 3, 7, 0 } },
    110 	{ 1, { 3, 0, 0 } },
    111 	{ 2, { 3, 4, 0 } },
    112 	{ 0, { 0, 0, 0 } },
    113 	{ 0, { 0, 0, 0 } },
    114 	{ 0, { 0, 0, 0 } },
    115 	{ 2, { 6, 7, 0 } }
    116 },
    117 	lower[8] = {
    118 	{ 0, { 0, 0, 0 } },
    119 	{ 2, { 0, 1, 0 } },
    120 	{ 1, { 1, 0, 0 } },
    121 	{ 2, { 1, 5, 0 } },
    122 	{ 1, { 5, 0, 0 } },
    123 	{ 2, { 5, 6, 0 } },
    124 	{ 0, { 0, 0, 0 } },
    125 	{ 0, { 0, 0, 0 } }
    126 },
    127 	upleft[8] = {
    128 	{ 0, { 0, 0, 0 } },
    129 	{ 0, { 0, 0, 0 } },
    130 	{ 0, { 0, 0, 0 } },
    131 	{ 0, { 0, 0, 0 } },
    132 	{ 0, { 0, 0, 0 } },
    133 	{ 1, { 3, 0, 0 } },
    134 	{ 2, { 1, 3, 0 } },
    135 	{ 1, { 1, 0, 0 } }
    136 },
    137 	upright[8] = {
    138 	{ 2, { 3, 5, 0 } },
    139 	{ 1, { 3, 0, 0 } },
    140 	{ 0, { 0, 0, 0 } },
    141 	{ 0, { 0, 0, 0 } },
    142 	{ 0, { 0, 0, 0 } },
    143 	{ 0, { 0, 0, 0 } },
    144 	{ 0, { 0, 0, 0 } },
    145 	{ 1, { 5, 0, 0 } }
    146 },
    147 	lowleft[8] = {
    148 	{ 3, { 7, 0, 1 } },
    149 	{ 0, { 0, 0, 0 } },
    150 	{ 0, { 0, 0, 0 } },
    151 	{ 1, { 1, 0, 0 } },
    152 	{ 2, { 1, 7, 0 } },
    153 	{ 1, { 7, 0, 0 } },
    154 	{ 0, { 0, 0, 0 } },
    155 	{ 0, { 0, 0, 0 } }
    156 },
    157 	lowright[8] = {
    158 	{ 0, { 0, 0, 0 } },
    159 	{ 1, { 7, 0, 0 } },
    160 	{ 2, { 5, 7, 0 } },
    161 	{ 1, { 5, 0, 0 } },
    162 	{ 0, { 0, 0, 0 } },
    163 	{ 0, { 0, 0, 0 } },
    164 	{ 0, { 0, 0, 0 } },
    165 	{ 0, { 0, 0, 0 } }
    166 };
    167 
    168 #define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, putchar)
    169 
    170 char *tcp;
    171 static char	flavor[] = {
    172 	'O', '*', '#', '$', '%', '0', '@', '~'
    173 };
    174 static short	xinc[] = {
    175 	1,  1,  1,  0, -1, -1, -1,  0
    176 }, yinc[] = {
    177 	-1,  0,  1,  1,  1,  0, -1, -1
    178 };
    179 static struct	worm {
    180 	int orientation, head;
    181 	short *xpos, *ypos;
    182 } *worm;
    183 
    184 void	 fputchar __P((int));
    185 void	 onsig __P((int));
    186 char	*tgetstr __P((char *, char **));
    187 char	*tgoto __P((char *, int, int));
    188 int	 tputs __P((char *, int, void (*)(int)));
    189 
    190 int
    191 main(argc, argv)
    192 	int argc;
    193 	char *argv[];
    194 {
    195 	extern int optind;
    196 	extern char *optarg, *UP;
    197 	register int x, y, h, n;
    198 	register struct worm *w;
    199 	register struct options *op;
    200 	register short *ip;
    201 	register char *term;
    202 	int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap;
    203 	short **ref;
    204 	char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR;
    205 	char *field, tcb[100], *mp;
    206 	long random();
    207 
    208 	length = 16;
    209 	number = 3;
    210 	trail = ' ';
    211 	field = NULL;
    212 	while ((ch = getopt(argc, argv, "fl:n:t")) != EOF)
    213 		switch(ch) {
    214 		case 'f':
    215 			field = "WORM";
    216 			break;
    217 		case 'l':
    218 			if ((length = atoi(optarg)) < 2 || length > 1024) {
    219 				(void)fprintf(stderr,
    220 				    "worms: invalid length (%d - %d).\n",
    221 				     2, 1024);
    222 				exit(1);
    223 			}
    224 			break;
    225 		case 'n':
    226 			if ((number = atoi(optarg)) < 1) {
    227 				(void)fprintf(stderr,
    228 				    "worms: invalid number of worms.\n");
    229 				exit(1);
    230 			}
    231 			break;
    232 		case 't':
    233 			trail = '.';
    234 			break;
    235 		case '?':
    236 		default:
    237 			(void)fprintf(stderr,
    238 			    "usage: worms [-ft] [-l length] [-n number]\n");
    239 			exit(1);
    240 		}
    241 
    242 	if (!(term = getenv("TERM"))) {
    243 		(void)fprintf(stderr, "worms: no TERM environment variable.\n");
    244 		exit(1);
    245 	}
    246 	if (!(worm = malloc((size_t)number *
    247 	    sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
    248 		nomem();
    249 	if (tgetent(mp, term) <= 0) {
    250 		(void)fprintf(stderr, "worms: %s: unknown terminal type.\n",
    251 		    term);
    252 		exit(1);
    253 	}
    254 	tcp = tcb;
    255 	if (!(CM = tgetstr("cm", &tcp))) {
    256 		(void)fprintf(stderr,
    257 		    "worms: terminal incapable of cursor motion.\n");
    258 		exit(1);
    259 	}
    260 	AL = tgetstr("al", &tcp);
    261 	BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp);
    262 	EI = tgetstr("ei", &tcp);
    263 	HO = tgetstr("ho", &tcp);
    264 	IC = tgetstr("ic", &tcp);
    265 	IM = tgetstr("im", &tcp);
    266 	IN = tgetflag("in");
    267 	IP = tgetstr("ip", &tcp);
    268 	SR = tgetstr("sr", &tcp);
    269 	UP = tgetstr("up", &tcp);
    270 #ifdef TIOCGWINSZ
    271 	if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
    272 	    ws.ws_col && ws.ws_row) {
    273 		CO = ws.ws_col;
    274 		LI = ws.ws_row;
    275 	} else
    276 #endif
    277 	{
    278 		if ((CO = tgetnum("co")) <= 0)
    279 			CO = 80;
    280 		if ((LI = tgetnum("li")) <= 0)
    281 			LI = 24;
    282 	}
    283 	last = CO - 1;
    284 	bottom = LI - 1;
    285 	tcgetattr(fileno(stdout), &ti);
    286 	Wrap = tgetflag("am");
    287 	if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
    288 		nomem();
    289 	if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
    290 		nomem();
    291 	for (n = 0; n < LI; ++n) {
    292 		ref[n] = ip;
    293 		ip += CO;
    294 	}
    295 	for (ip = ref[0], n = LI * CO; --n >= 0;)
    296 		*ip++ = 0;
    297 	if (Wrap)
    298 		ref[bottom][last] = 1;
    299 	for (n = number, w = &worm[0]; --n >= 0; w++) {
    300 		w->orientation = w->head = 0;
    301 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    302 			nomem();
    303 		w->xpos = ip;
    304 		for (x = length; --x >= 0;)
    305 			*ip++ = -1;
    306 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    307 			nomem();
    308 		w->ypos = ip;
    309 		for (y = length; --y >= 0;)
    310 			*ip++ = -1;
    311 	}
    312 
    313 	(void)signal(SIGHUP, onsig);
    314 	(void)signal(SIGINT, onsig);
    315 	(void)signal(SIGQUIT, onsig);
    316 	(void)signal(SIGSTOP, onsig);
    317 	(void)signal(SIGTSTP, onsig);
    318 	(void)signal(SIGTERM, onsig);
    319 
    320 	tputs(tgetstr("ti", &tcp), 1, putchar);
    321 	tputs(tgetstr("cl", &tcp), 1, putchar);
    322 	if (field) {
    323 		register char *p = field;
    324 
    325 		for (y = bottom; --y >= 0;) {
    326 			for (x = CO; --x >= 0;) {
    327 				putchar(*p++);
    328 				if (!*p)
    329 					p = field;
    330 			}
    331 			if (!Wrap)
    332 				putchar('\n');
    333 			(void)fflush(stdout);
    334 		}
    335 		if (Wrap) {
    336 			if (IM && !IN) {
    337 				for (x = last; --x > 0;) {
    338 					putchar(*p++);
    339 					if (!*p)
    340 						p = field;
    341 				}
    342 				y = *p++;
    343 				if (!*p)
    344 					p = field;
    345 				putchar(*p);
    346 				if (BC)
    347 					tputs(BC, 1, putchar);
    348 				else
    349 					cursor(last - 1, bottom);
    350 				tputs(IM, 1, putchar);
    351 				if (IC)
    352 					tputs(IC, 1, putchar);
    353 				putchar(y);
    354 				if (IP)
    355 					tputs(IP, 1, putchar);
    356 				tputs(EI, 1, putchar);
    357 			}
    358 			else if (SR || AL) {
    359 				if (HO)
    360 					tputs(HO, 1, putchar);
    361 				else
    362 					cursor(0, 0);
    363 				if (SR)
    364 					tputs(SR, 1, putchar);
    365 				else
    366 					tputs(AL, LI, putchar);
    367 				for (x = CO; --x >= 0;) {
    368 					putchar(*p++);
    369 					if (!*p)
    370 						p = field;
    371 				}
    372 			}
    373 			else for (x = last; --x >= 0;) {
    374 				putchar(*p++);
    375 				if (!*p)
    376 					p = field;
    377 			}
    378 		}
    379 		else for (x = CO; --x >= 0;) {
    380 			putchar(*p++);
    381 			if (!*p)
    382 				p = field;
    383 		}
    384 	}
    385 	for (;;) {
    386 		(void)fflush(stdout);
    387 		for (n = 0, w = &worm[0]; n < number; n++, w++) {
    388 			if ((x = w->xpos[h = w->head]) < 0) {
    389 				cursor(x = w->xpos[h] = 0,
    390 				     y = w->ypos[h] = bottom);
    391 				putchar(flavor[n % sizeof(flavor)]);
    392 				ref[y][x]++;
    393 			}
    394 			else
    395 				y = w->ypos[h];
    396 			if (++h == length)
    397 				h = 0;
    398 			if (w->xpos[w->head = h] >= 0) {
    399 				register int x1, y1;
    400 
    401 				x1 = w->xpos[h];
    402 				y1 = w->ypos[h];
    403 				if (--ref[y1][x1] == 0) {
    404 					cursor(x1, y1);
    405 					if (trail)
    406 						putchar(trail);
    407 				}
    408 			}
    409 			op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
    410 			switch (op->nopts) {
    411 			case 0:
    412 				(void)fflush(stdout);
    413 				abort();
    414 				return;
    415 			case 1:
    416 				w->orientation = op->opts[0];
    417 				break;
    418 			default:
    419 				w->orientation =
    420 				    op->opts[(int)random() % op->nopts];
    421 			}
    422 			cursor(x += xinc[w->orientation],
    423 			    y += yinc[w->orientation]);
    424 			if (!Wrap || x != last || y != bottom)
    425 				putchar(flavor[n % sizeof(flavor)]);
    426 			ref[w->ypos[h] = y][w->xpos[h] = x]++;
    427 		}
    428 	}
    429 }
    430 
    431 void
    432 onsig(signo)
    433 	int signo;
    434 {
    435 	tputs(tgetstr("cl", &tcp), 1, putchar);
    436 	tputs(tgetstr("te", &tcp), 1, putchar);
    437 	exit(0);
    438 }
    439 
    440 void
    441 fputchar(c)
    442 	int c;
    443 {
    444 	(void)putchar(c);
    445 }
    446 
    447 nomem()
    448 {
    449 	(void)fprintf(stderr, "worms: not enough memory.\n");
    450 	exit(1);
    451 }
    452