1 /* $NetBSD: worms.c,v 1.11 1999/07/30 02:23:27 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.11 1999/07/30 02:23:27 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 69 #include <curses.h> 70 #include <err.h> 71 #include <signal.h> 72 #include <stdio.h> 73 #include <stdlib.h> 74 #include <unistd.h> 75 76 static const 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 171 static const char flavor[] = { 172 'O', '*', '#', '$', '%', '0', '@', '~' 173 }; 174 static const 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 volatile sig_atomic_t sig_caught = 0; 185 186 int main __P((int, char **)); 187 void nomem __P((void)) __attribute__((__noreturn__)); 188 void onsig __P((int)); 189 190 int 191 main(argc, argv) 192 int argc; 193 char *argv[]; 194 { 195 int x, y, h, n; 196 struct worm *w; 197 const struct options *op; 198 short *ip; 199 int CO, LI, last, bottom, ch, length, number, trail; 200 short **ref; 201 const char *field; 202 char *mp; 203 unsigned int delay = 0; 204 205 mp = NULL; 206 length = 16; 207 number = 3; 208 trail = ' '; 209 field = NULL; 210 while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1) 211 switch(ch) { 212 case 'd': 213 if ((delay = (unsigned int)strtoul(optarg, (char **)NULL, 10)) < 1 || delay > 1000) 214 errx(1, "invalid delay (1-1000)"); 215 delay *= 1000; /* ms -> us */ 216 break; 217 case 'f': 218 field = "WORM"; 219 break; 220 case 'l': 221 if ((length = atoi(optarg)) < 2 || length > 1024) { 222 errx(1, "invalid length (%d - %d).", 223 2, 1024); 224 } 225 break; 226 case 'n': 227 if ((number = atoi(optarg)) < 1) { 228 errx(1, "invalid number of worms."); 229 } 230 break; 231 case 't': 232 trail = '.'; 233 break; 234 case '?': 235 default: 236 (void)fprintf(stderr, 237 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n"); 238 exit(1); 239 } 240 241 if (!(worm = malloc((size_t)number * 242 sizeof(struct worm))) || !(mp = malloc((size_t)1024))) 243 nomem(); 244 initscr(); 245 CO = COLS; 246 LI = LINES; 247 last = CO - 1; 248 bottom = LI - 1; 249 if (!(ip = malloc((size_t)(LI * CO * sizeof(short))))) 250 nomem(); 251 if (!(ref = malloc((size_t)(LI * sizeof(short *))))) 252 nomem(); 253 for (n = 0; n < LI; ++n) { 254 ref[n] = ip; 255 ip += CO; 256 } 257 for (ip = ref[0], n = LI * CO; --n >= 0;) 258 *ip++ = 0; 259 for (n = number, w = &worm[0]; --n >= 0; w++) { 260 w->orientation = w->head = 0; 261 if (!(ip = malloc((size_t)(length * sizeof(short))))) 262 nomem(); 263 w->xpos = ip; 264 for (x = length; --x >= 0;) 265 *ip++ = -1; 266 if (!(ip = malloc((size_t)(length * sizeof(short))))) 267 nomem(); 268 w->ypos = ip; 269 for (y = length; --y >= 0;) 270 *ip++ = -1; 271 } 272 273 (void)signal(SIGHUP, onsig); 274 (void)signal(SIGINT, onsig); 275 (void)signal(SIGQUIT, onsig); 276 (void)signal(SIGSTOP, onsig); 277 (void)signal(SIGTSTP, onsig); 278 (void)signal(SIGTERM, onsig); 279 280 if (field) { 281 const char *p = field; 282 283 for (y = LI; --y >= 0;) { 284 for (x = CO; --x >= 0;) { 285 addch(*p++); 286 if (!*p) 287 p = field; 288 } 289 refresh(); 290 } 291 } 292 for (;;) { 293 refresh(); 294 if (sig_caught) { 295 endwin(); 296 exit(0); 297 } 298 if (delay) usleep(delay); 299 for (n = 0, w = &worm[0]; n < number; n++, w++) { 300 if ((x = w->xpos[h = w->head]) < 0) { 301 mvaddch(y = w->ypos[h] = bottom, 302 x = w->xpos[h] = 0, 303 flavor[n % sizeof(flavor)]); 304 ref[y][x]++; 305 } 306 else 307 y = w->ypos[h]; 308 if (++h == length) 309 h = 0; 310 if (w->xpos[w->head = h] >= 0) { 311 int x1, y1; 312 313 x1 = w->xpos[h]; 314 y1 = w->ypos[h]; 315 if (--ref[y1][x1] == 0) { 316 mvaddch(y1, x1, trail); 317 } 318 } 319 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; 320 switch (op->nopts) { 321 case 0: 322 refresh(); 323 abort(); 324 return(1); 325 case 1: 326 w->orientation = op->opts[0]; 327 break; 328 default: 329 w->orientation = 330 op->opts[(int)random() % op->nopts]; 331 } 332 mvaddch(y += yinc[w->orientation], 333 x += xinc[w->orientation], 334 flavor[n % sizeof(flavor)]); 335 ref[w->ypos[h] = y][w->xpos[h] = x]++; 336 } 337 } 338 } 339 340 void 341 onsig(signo) 342 int signo __attribute__((__unused__)); 343 { 344 sig_caught = 1; 345 } 346 347 void 348 nomem() 349 { 350 errx(1, "not enough memory."); 351 } 352