Home | History | Annotate | Line # | Download | only in worms
worms.c revision 1.10
      1 /*	$NetBSD: worms.c,v 1.10 1998/09/13 15:27:31 hubertf 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: worms.c,v 1.10 1998/09/13 15:27:31 hubertf 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 int	 main __P((int, char **));
    188 void	 nomem __P((void)) __attribute__((__noreturn__));
    189 void	 onsig __P((int)) __attribute__((__noreturn__));
    190 int	 tgetent __P((char *, char *));
    191 int	 tgetflag __P((char *));
    192 int	 tgetnum __P((char *));
    193 char	*tgetstr __P((char *, char **));
    194 char	*tgoto __P((char *, int, int));
    195 int	 tputs __P((char *, int, void (*)(int)));
    196 
    197 int
    198 main(argc, argv)
    199 	int argc;
    200 	char *argv[];
    201 {
    202 	extern char *UP;
    203 	int x, y, h, n;
    204 	struct worm *w;
    205 	struct options *op;
    206 	short *ip;
    207 	char *term;
    208 	int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap;
    209 	short **ref;
    210 	char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR;
    211 	char *field, tcb[100], *mp;
    212 	struct termios ti;
    213 #ifdef TIOCGWINSZ
    214 	struct winsize ws;
    215 #endif
    216 
    217 	mp = NULL;
    218 	length = 16;
    219 	number = 3;
    220 	trail = ' ';
    221 	field = NULL;
    222 	while ((ch = getopt(argc, argv, "fl:n:t")) != -1)
    223 		switch(ch) {
    224 		case 'f':
    225 			field = "WORM";
    226 			break;
    227 		case 'l':
    228 			if ((length = atoi(optarg)) < 2 || length > 1024) {
    229 				(void)fprintf(stderr,
    230 				    "worms: invalid length (%d - %d).\n",
    231 				     2, 1024);
    232 				exit(1);
    233 			}
    234 			break;
    235 		case 'n':
    236 			if ((number = atoi(optarg)) < 1) {
    237 				(void)fprintf(stderr,
    238 				    "worms: invalid number of worms.\n");
    239 				exit(1);
    240 			}
    241 			break;
    242 		case 't':
    243 			trail = '.';
    244 			break;
    245 		case '?':
    246 		default:
    247 			(void)fprintf(stderr,
    248 			    "usage: worms [-ft] [-l length] [-n number]\n");
    249 			exit(1);
    250 		}
    251 
    252 	if (!(term = getenv("TERM"))) {
    253 		(void)fprintf(stderr, "worms: no TERM environment variable.\n");
    254 		exit(1);
    255 	}
    256 	if (!(worm = malloc((size_t)number *
    257 	    sizeof(struct worm))) || !(mp = malloc((size_t)1024)))
    258 		nomem();
    259 	if (tgetent(mp, term) <= 0) {
    260 		(void)fprintf(stderr, "worms: %s: unknown terminal type.\n",
    261 		    term);
    262 		exit(1);
    263 	}
    264 	tcp = tcb;
    265 	if (!(CM = tgetstr("cm", &tcp))) {
    266 		(void)fprintf(stderr,
    267 		    "worms: terminal incapable of cursor motion.\n");
    268 		exit(1);
    269 	}
    270 	AL = tgetstr("al", &tcp);
    271 	BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp);
    272 	EI = tgetstr("ei", &tcp);
    273 	HO = tgetstr("ho", &tcp);
    274 	IC = tgetstr("ic", &tcp);
    275 	IM = tgetstr("im", &tcp);
    276 	IN = tgetflag("in");
    277 	IP = tgetstr("ip", &tcp);
    278 	SR = tgetstr("sr", &tcp);
    279 	UP = tgetstr("up", &tcp);
    280 #ifdef TIOCGWINSZ
    281 	if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
    282 	    ws.ws_col && ws.ws_row) {
    283 		CO = ws.ws_col;
    284 		LI = ws.ws_row;
    285 	} else
    286 #endif
    287 	{
    288 		if ((CO = tgetnum("co")) <= 0)
    289 			CO = 80;
    290 		if ((LI = tgetnum("li")) <= 0)
    291 			LI = 24;
    292 	}
    293 	last = CO - 1;
    294 	bottom = LI - 1;
    295 	tcgetattr(fileno(stdout), &ti);
    296 	Wrap = tgetflag("am");
    297 	if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
    298 		nomem();
    299 	if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
    300 		nomem();
    301 	for (n = 0; n < LI; ++n) {
    302 		ref[n] = ip;
    303 		ip += CO;
    304 	}
    305 	for (ip = ref[0], n = LI * CO; --n >= 0;)
    306 		*ip++ = 0;
    307 	if (Wrap)
    308 		ref[bottom][last] = 1;
    309 	for (n = number, w = &worm[0]; --n >= 0; w++) {
    310 		w->orientation = w->head = 0;
    311 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    312 			nomem();
    313 		w->xpos = ip;
    314 		for (x = length; --x >= 0;)
    315 			*ip++ = -1;
    316 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    317 			nomem();
    318 		w->ypos = ip;
    319 		for (y = length; --y >= 0;)
    320 			*ip++ = -1;
    321 	}
    322 
    323 	(void)signal(SIGHUP, onsig);
    324 	(void)signal(SIGINT, onsig);
    325 	(void)signal(SIGQUIT, onsig);
    326 	(void)signal(SIGSTOP, onsig);
    327 	(void)signal(SIGTSTP, onsig);
    328 	(void)signal(SIGTERM, onsig);
    329 
    330 	tputs(tgetstr("ti", &tcp), 1, fputchar);
    331 	tputs(tgetstr("cl", &tcp), 1, fputchar);
    332 	if (field) {
    333 		char *p = field;
    334 
    335 		for (y = bottom; --y >= 0;) {
    336 			for (x = CO; --x >= 0;) {
    337 				fputchar(*p++);
    338 				if (!*p)
    339 					p = field;
    340 			}
    341 			if (!Wrap)
    342 				fputchar('\n');
    343 			(void)fflush(stdout);
    344 		}
    345 		if (Wrap) {
    346 			if (IM && !IN) {
    347 				for (x = last; --x > 0;) {
    348 					fputchar(*p++);
    349 					if (!*p)
    350 						p = field;
    351 				}
    352 				y = *p++;
    353 				if (!*p)
    354 					p = field;
    355 				fputchar(*p);
    356 				if (BC)
    357 					tputs(BC, 1, fputchar);
    358 				else
    359 					cursor(last - 1, bottom);
    360 				tputs(IM, 1, fputchar);
    361 				if (IC)
    362 					tputs(IC, 1, fputchar);
    363 				fputchar(y);
    364 				if (IP)
    365 					tputs(IP, 1, fputchar);
    366 				tputs(EI, 1, fputchar);
    367 			}
    368 			else if (SR || AL) {
    369 				if (HO)
    370 					tputs(HO, 1, fputchar);
    371 				else
    372 					cursor(0, 0);
    373 				if (SR)
    374 					tputs(SR, 1, fputchar);
    375 				else
    376 					tputs(AL, LI, fputchar);
    377 				for (x = CO; --x >= 0;) {
    378 					fputchar(*p++);
    379 					if (!*p)
    380 						p = field;
    381 				}
    382 			}
    383 			else for (x = last; --x >= 0;) {
    384 				fputchar(*p++);
    385 				if (!*p)
    386 					p = field;
    387 			}
    388 		}
    389 		else for (x = CO; --x >= 0;) {
    390 			fputchar(*p++);
    391 			if (!*p)
    392 				p = field;
    393 		}
    394 	}
    395 	for (;;) {
    396 		(void)fflush(stdout);
    397 		for (n = 0, w = &worm[0]; n < number; n++, w++) {
    398 			if ((x = w->xpos[h = w->head]) < 0) {
    399 				cursor(x = w->xpos[h] = 0,
    400 				     y = w->ypos[h] = bottom);
    401 				fputchar(flavor[n % sizeof(flavor)]);
    402 				ref[y][x]++;
    403 			}
    404 			else
    405 				y = w->ypos[h];
    406 			if (++h == length)
    407 				h = 0;
    408 			if (w->xpos[w->head = h] >= 0) {
    409 				int x1, y1;
    410 
    411 				x1 = w->xpos[h];
    412 				y1 = w->ypos[h];
    413 				if (--ref[y1][x1] == 0) {
    414 					cursor(x1, y1);
    415 					if (trail)
    416 						fputchar(trail);
    417 				}
    418 			}
    419 			op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
    420 			switch (op->nopts) {
    421 			case 0:
    422 				(void)fflush(stdout);
    423 				abort();
    424 				return(1);
    425 			case 1:
    426 				w->orientation = op->opts[0];
    427 				break;
    428 			default:
    429 				w->orientation =
    430 				    op->opts[(int)random() % op->nopts];
    431 			}
    432 			cursor(x += xinc[w->orientation],
    433 			    y += yinc[w->orientation]);
    434 			if (!Wrap || x != last || y != bottom)
    435 				fputchar(flavor[n % sizeof(flavor)]);
    436 			ref[w->ypos[h] = y][w->xpos[h] = x]++;
    437 		}
    438 	}
    439 }
    440 
    441 void
    442 onsig(signo)
    443 	int signo;
    444 {
    445 	tputs(tgetstr("cl", &tcp), 1, fputchar);
    446 	tputs(tgetstr("te", &tcp), 1, fputchar);
    447 	exit(0);
    448 }
    449 
    450 void
    451 fputchar(c)
    452 	int c;
    453 {
    454 	(void)putchar(c);
    455 }
    456 
    457 void
    458 nomem()
    459 {
    460 	(void)fprintf(stderr, "worms: not enough memory.\n");
    461 	exit(1);
    462 }
    463