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