Home | History | Annotate | Line # | Download | only in worms
worms.c revision 1.24
      1 /*	$NetBSD: worms.c,v 1.24 2023/04/15 13:00:30 kre 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)worms.c	8.1 (Berkeley) 5/31/93";
     41 #else
     42 __RCSID("$NetBSD: worms.c,v 1.24 2023/04/15 13:00:30 kre Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 /*
     47  *
     48  *	 @@@        @@@    @@@@@@@@@@     @@@@@@@@@@@    @@@@@@@@@@@@
     49  *	 @@@        @@@   @@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@@@@@
     50  *	 @@@        @@@  @@@@      @@@@   @@@@           @@@@ @@@  @@@@
     51  *	 @@@   @@   @@@  @@@        @@@   @@@            @@@  @@@   @@@
     52  *	 @@@  @@@@  @@@  @@@        @@@   @@@            @@@  @@@   @@@
     53  *	 @@@@ @@@@ @@@@  @@@        @@@   @@@            @@@  @@@   @@@
     54  *	  @@@@@@@@@@@@   @@@@      @@@@   @@@            @@@  @@@   @@@
     55  *	   @@@@  @@@@     @@@@@@@@@@@@    @@@            @@@  @@@   @@@
     56  *	    @@    @@       @@@@@@@@@@     @@@            @@@  @@@   @@@
     57  *
     58  *				 Eric P. Scott
     59  *			  Caltech High Energy Physics
     60  *				 October, 1980
     61  *
     62  */
     63 #include <sys/types.h>
     64 
     65 #include <curses.h>
     66 #include <err.h>
     67 #include <signal.h>
     68 #include <stdio.h>
     69 #include <stdlib.h>
     70 #include <unistd.h>
     71 
     72 static const struct options {
     73 	int nopts;
     74 	int opts[3];
     75 }
     76 	normal[8] = {
     77 	{ 3, { 7, 0, 1 } },
     78 	{ 3, { 0, 1, 2 } },
     79 	{ 3, { 1, 2, 3 } },
     80 	{ 3, { 2, 3, 4 } },
     81 	{ 3, { 3, 4, 5 } },
     82 	{ 3, { 4, 5, 6 } },
     83 	{ 3, { 5, 6, 7 } },
     84 	{ 3, { 6, 7, 0 } }
     85 },	upper[8] = {
     86 	{ 1, { 1, 0, 0 } },
     87 	{ 2, { 1, 2, 0 } },
     88 	{ 0, { 0, 0, 0 } },
     89 	{ 0, { 0, 0, 0 } },
     90 	{ 0, { 0, 0, 0 } },
     91 	{ 2, { 4, 5, 0 } },
     92 	{ 1, { 5, 0, 0 } },
     93 	{ 2, { 1, 5, 0 } }
     94 },
     95 	left[8] = {
     96 	{ 0, { 0, 0, 0 } },
     97 	{ 0, { 0, 0, 0 } },
     98 	{ 0, { 0, 0, 0 } },
     99 	{ 2, { 2, 3, 0 } },
    100 	{ 1, { 3, 0, 0 } },
    101 	{ 2, { 3, 7, 0 } },
    102 	{ 1, { 7, 0, 0 } },
    103 	{ 2, { 7, 0, 0 } }
    104 },
    105 	right[8] = {
    106 	{ 1, { 7, 0, 0 } },
    107 	{ 2, { 3, 7, 0 } },
    108 	{ 1, { 3, 0, 0 } },
    109 	{ 2, { 3, 4, 0 } },
    110 	{ 0, { 0, 0, 0 } },
    111 	{ 0, { 0, 0, 0 } },
    112 	{ 0, { 0, 0, 0 } },
    113 	{ 2, { 6, 7, 0 } }
    114 },
    115 	lower[8] = {
    116 	{ 0, { 0, 0, 0 } },
    117 	{ 2, { 0, 1, 0 } },
    118 	{ 1, { 1, 0, 0 } },
    119 	{ 2, { 1, 5, 0 } },
    120 	{ 1, { 5, 0, 0 } },
    121 	{ 2, { 5, 6, 0 } },
    122 	{ 0, { 0, 0, 0 } },
    123 	{ 0, { 0, 0, 0 } }
    124 },
    125 	upleft[8] = {
    126 	{ 0, { 0, 0, 0 } },
    127 	{ 0, { 0, 0, 0 } },
    128 	{ 0, { 0, 0, 0 } },
    129 	{ 0, { 0, 0, 0 } },
    130 	{ 0, { 0, 0, 0 } },
    131 	{ 1, { 3, 0, 0 } },
    132 	{ 2, { 1, 3, 0 } },
    133 	{ 1, { 1, 0, 0 } }
    134 },
    135 	upright[8] = {
    136 	{ 2, { 3, 5, 0 } },
    137 	{ 1, { 3, 0, 0 } },
    138 	{ 0, { 0, 0, 0 } },
    139 	{ 0, { 0, 0, 0 } },
    140 	{ 0, { 0, 0, 0 } },
    141 	{ 0, { 0, 0, 0 } },
    142 	{ 0, { 0, 0, 0 } },
    143 	{ 1, { 5, 0, 0 } }
    144 },
    145 	lowleft[8] = {
    146 	{ 3, { 7, 0, 1 } },
    147 	{ 0, { 0, 0, 0 } },
    148 	{ 0, { 0, 0, 0 } },
    149 	{ 1, { 1, 0, 0 } },
    150 	{ 2, { 1, 7, 0 } },
    151 	{ 1, { 7, 0, 0 } },
    152 	{ 0, { 0, 0, 0 } },
    153 	{ 0, { 0, 0, 0 } }
    154 },
    155 	lowright[8] = {
    156 	{ 0, { 0, 0, 0 } },
    157 	{ 1, { 7, 0, 0 } },
    158 	{ 2, { 5, 7, 0 } },
    159 	{ 1, { 5, 0, 0 } },
    160 	{ 0, { 0, 0, 0 } },
    161 	{ 0, { 0, 0, 0 } },
    162 	{ 0, { 0, 0, 0 } },
    163 	{ 0, { 0, 0, 0 } }
    164 };
    165 
    166 
    167 static const char	flavor[] = {
    168 	'O', '*', '#', '$', '%', '0', '@', '~'
    169 };
    170 static const short	xinc[] = {
    171 	1,  1,  1,  0, -1, -1, -1,  0
    172 }, yinc[] = {
    173 	-1,  0,  1,  1,  1,  0, -1, -1
    174 };
    175 static struct	worm {
    176 	int orientation, head;
    177 	short *xpos, *ypos;
    178 } *worm;
    179 
    180 static volatile sig_atomic_t sig_caught = 0;
    181 
    182 int	 main(int, char **);
    183 static void nomem(void) __dead;
    184 static void onsig(int);
    185 
    186 int
    187 main(int argc, char *argv[])
    188 {
    189 	int x, y, h, n;
    190 	struct worm *w;
    191 	const struct options *op;
    192 	short *ip;
    193 	int CO, LI, last, bottom, ch, length, number, trail;
    194 	short **ref;
    195 	const char *field;
    196 	unsigned int delay = 20000;
    197 
    198 	length = 16;
    199 	number = 3;
    200 	trail = ' ';
    201 	field = NULL;
    202 	while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
    203 		switch(ch) {
    204 		case 'd':
    205 			if ((delay = (unsigned int)strtoul(optarg, NULL, 10)) < 1 || delay > 1000)
    206 				errx(1, "invalid delay (1-1000)");
    207 			delay *= 1000;  /* ms -> us */
    208 			break;
    209 		case 'f':
    210 			field = "WORM";
    211 			break;
    212 		case 'l':
    213 			if ((length = atoi(optarg)) < 2 || length > 1024) {
    214 				errx(1, "invalid length (%d - %d).",
    215 				     2, 1024);
    216 			}
    217 			break;
    218 		case 'n':
    219 			if ((number = atoi(optarg)) < 1) {
    220 				errx(1, "invalid number of worms.");
    221 			}
    222 			break;
    223 		case 't':
    224 			trail = '.';
    225 			break;
    226 		case '?':
    227 		default:
    228 			(void)fprintf(stderr,
    229 			    "usage: worms [-ft] [-d delay] [-l length] [-n number]\n");
    230 			exit(1);
    231 		}
    232 
    233 	if (!(worm = calloc((size_t)number, sizeof(struct worm))))
    234 		nomem();
    235 	if (!initscr())
    236 		errx(0, "couldn't initialize screen");
    237 	curs_set(0);
    238 	CO = COLS;
    239 	LI = LINES;
    240 	last = CO - 1;
    241 	bottom = LI - 1;
    242 	if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
    243 		nomem();
    244 	if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
    245 		nomem();
    246 	for (n = 0; n < LI; ++n) {
    247 		ref[n] = ip;
    248 		ip += CO;
    249 	}
    250 	for (ip = ref[0], n = LI * CO; --n >= 0;)
    251 		*ip++ = 0;
    252 	for (n = number, w = &worm[0]; --n >= 0; w++) {
    253 		w->orientation = w->head = 0;
    254 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    255 			nomem();
    256 		w->xpos = ip;
    257 		for (x = length; --x >= 0;)
    258 			*ip++ = -1;
    259 		if (!(ip = malloc((size_t)(length * sizeof(short)))))
    260 			nomem();
    261 		w->ypos = ip;
    262 		for (y = length; --y >= 0;)
    263 			*ip++ = -1;
    264 	}
    265 
    266 	(void)signal(SIGHUP, onsig);
    267 	(void)signal(SIGINT, onsig);
    268 	(void)signal(SIGQUIT, onsig);
    269 	(void)signal(SIGTSTP, onsig);
    270 	(void)signal(SIGTERM, onsig);
    271 
    272 	if (field) {
    273 		const char *p = field;
    274 
    275 		for (y = LI; --y >= 0;) {
    276 			for (x = CO; --x >= 0;) {
    277 				addch(*p++);
    278 				if (!*p)
    279 					p = field;
    280 			}
    281 			refresh();
    282 		}
    283 	}
    284 	for (;;) {
    285 		refresh();
    286 		if (sig_caught) {
    287 			endwin();
    288 			exit(0);
    289 		}
    290 		if (delay) {
    291 			if (delay % 1000000 != 0)
    292 				usleep(delay % 1000000);
    293 			if (delay >= 1000000)
    294 				sleep(delay / 1000000);
    295 		}
    296 		for (n = 0, w = &worm[0]; n < number; n++, w++) {
    297 			if ((x = w->xpos[h = w->head]) < 0) {
    298 				mvaddch(y = w->ypos[h] = bottom,
    299 					x = w->xpos[h] = 0,
    300 					flavor[n % sizeof(flavor)]);
    301 				ref[y][x]++;
    302 			}
    303 			else
    304 				y = w->ypos[h];
    305 			if (++h == length)
    306 				h = 0;
    307 			if (w->xpos[w->head = h] >= 0) {
    308 				int x1, y1;
    309 
    310 				x1 = w->xpos[h];
    311 				y1 = w->ypos[h];
    312 				if (--ref[y1][x1] == 0) {
    313 					mvaddch(y1, x1, trail);
    314 				}
    315 			}
    316 			op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
    317 			switch (op->nopts) {
    318 			case 0:
    319 				refresh();
    320 				abort();
    321 				return(1);
    322 			case 1:
    323 				w->orientation = op->opts[0];
    324 				break;
    325 			default:
    326 				w->orientation =
    327 				    op->opts[(int)random() % op->nopts];
    328 			}
    329 			mvaddch(y += yinc[w->orientation],
    330 				x += xinc[w->orientation],
    331 				flavor[n % sizeof(flavor)]);
    332 			ref[w->ypos[h] = y][w->xpos[h] = x]++;
    333 		}
    334 	}
    335 }
    336 
    337 static void
    338 onsig(int signo __unused)
    339 {
    340 	sig_caught = 1;
    341 }
    342 
    343 static void
    344 nomem(void)
    345 {
    346 	errx(1, "not enough memory.");
    347 }
    348