1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)worms.c 5.11 (Berkeley) 03/22/93"; 16 #endif /* not lint */ 17 18 /* 19 * 20 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@ 21 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@ 22 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@ 23 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 24 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 25 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ 26 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ 27 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@ 28 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@ 29 * 30 * Eric P. Scott 31 * Caltech High Energy Physics 32 * October, 1980 33 * 34 */ 35 #include <sys/types.h> 36 37 #include <signal.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 42 static struct options { 43 int nopts; 44 int opts[3]; 45 } 46 normal[8] = { 47 { 3, { 7, 0, 1 } }, 48 { 3, { 0, 1, 2 } }, 49 { 3, { 1, 2, 3 } }, 50 { 3, { 2, 3, 4 } }, 51 { 3, { 3, 4, 5 } }, 52 { 3, { 4, 5, 6 } }, 53 { 3, { 5, 6, 7 } }, 54 { 3, { 6, 7, 0 } } 55 }, upper[8] = { 56 { 1, { 1, 0, 0 } }, 57 { 2, { 1, 2, 0 } }, 58 { 0, { 0, 0, 0 } }, 59 { 0, { 0, 0, 0 } }, 60 { 0, { 0, 0, 0 } }, 61 { 2, { 4, 5, 0 } }, 62 { 1, { 5, 0, 0 } }, 63 { 2, { 1, 5, 0 } } 64 }, 65 left[8] = { 66 { 0, { 0, 0, 0 } }, 67 { 0, { 0, 0, 0 } }, 68 { 0, { 0, 0, 0 } }, 69 { 2, { 2, 3, 0 } }, 70 { 1, { 3, 0, 0 } }, 71 { 2, { 3, 7, 0 } }, 72 { 1, { 7, 0, 0 } }, 73 { 2, { 7, 0, 0 } } 74 }, 75 right[8] = { 76 { 1, { 7, 0, 0 } }, 77 { 2, { 3, 7, 0 } }, 78 { 1, { 3, 0, 0 } }, 79 { 2, { 3, 4, 0 } }, 80 { 0, { 0, 0, 0 } }, 81 { 0, { 0, 0, 0 } }, 82 { 0, { 0, 0, 0 } }, 83 { 2, { 6, 7, 0 } } 84 }, 85 lower[8] = { 86 { 0, { 0, 0, 0 } }, 87 { 2, { 0, 1, 0 } }, 88 { 1, { 1, 0, 0 } }, 89 { 2, { 1, 5, 0 } }, 90 { 1, { 5, 0, 0 } }, 91 { 2, { 5, 6, 0 } }, 92 { 0, { 0, 0, 0 } }, 93 { 0, { 0, 0, 0 } } 94 }, 95 upleft[8] = { 96 { 0, { 0, 0, 0 } }, 97 { 0, { 0, 0, 0 } }, 98 { 0, { 0, 0, 0 } }, 99 { 0, { 0, 0, 0 } }, 100 { 0, { 0, 0, 0 } }, 101 { 1, { 3, 0, 0 } }, 102 { 2, { 1, 3, 0 } }, 103 { 1, { 1, 0, 0 } } 104 }, 105 upright[8] = { 106 { 2, { 3, 5, 0 } }, 107 { 1, { 3, 0, 0 } }, 108 { 0, { 0, 0, 0 } }, 109 { 0, { 0, 0, 0 } }, 110 { 0, { 0, 0, 0 } }, 111 { 0, { 0, 0, 0 } }, 112 { 0, { 0, 0, 0 } }, 113 { 1, { 5, 0, 0 } } 114 }, 115 lowleft[8] = { 116 { 3, { 7, 0, 1 } }, 117 { 0, { 0, 0, 0 } }, 118 { 0, { 0, 0, 0 } }, 119 { 1, { 1, 0, 0 } }, 120 { 2, { 1, 7, 0 } }, 121 { 1, { 7, 0, 0 } }, 122 { 0, { 0, 0, 0 } }, 123 { 0, { 0, 0, 0 } } 124 }, 125 lowright[8] = { 126 { 0, { 0, 0, 0 } }, 127 { 1, { 7, 0, 0 } }, 128 { 2, { 5, 7, 0 } }, 129 { 1, { 5, 0, 0 } }, 130 { 0, { 0, 0, 0 } }, 131 { 0, { 0, 0, 0 } }, 132 { 0, { 0, 0, 0 } }, 133 { 0, { 0, 0, 0 } } 134 }; 135 136 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar) 137 138 char *tcp; 139 static char flavor[] = { 140 'O', '*', '#', '$', '%', '0', '@', '~' 141 }; 142 static short xinc[] = { 143 1, 1, 1, 0, -1, -1, -1, 0 144 }, yinc[] = { 145 -1, 0, 1, 1, 1, 0, -1, -1 146 }; 147 static struct worm { 148 int orientation, head; 149 short *xpos, *ypos; 150 } *worm; 151 152 void fputchar __P((int)); 153 void onsig __P((int)); 154 char *tgetstr __P((char *, char **)); 155 char *tgoto __P((char *, int, int)); 156 int tputs __P((char *, int, void (*)(int))); 157 158 int 159 main(argc, argv) 160 int argc; 161 char *argv[]; 162 { 163 extern int optind; 164 extern char *optarg, *UP; 165 register int x, y, h, n; 166 register struct worm *w; 167 register struct options *op; 168 register short *ip; 169 register char *term; 170 int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap; 171 short **ref; 172 char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR; 173 char *field, tcb[100], *mp; 174 long random(); 175 176 length = 16; 177 number = 3; 178 trail = ' '; 179 field = NULL; 180 while ((ch = getopt(argc, argv, "fl:n:t")) != EOF) 181 switch(ch) { 182 case 'f': 183 field = "WORM"; 184 break; 185 case 'l': 186 if ((length = atoi(optarg)) < 2 || length > 1024) { 187 (void)fprintf(stderr, 188 "worms: invalid length (%d - %d).\n", 189 2, 1024); 190 exit(1); 191 } 192 break; 193 case 'n': 194 if ((number = atoi(optarg)) < 1) { 195 (void)fprintf(stderr, 196 "worms: invalid number of worms.\n"); 197 exit(1); 198 } 199 break; 200 case 't': 201 trail = '.'; 202 break; 203 case '?': 204 default: 205 (void)fprintf(stderr, 206 "usage: worms [-ft] [-l length] [-n number]\n"); 207 exit(1); 208 } 209 210 if (!(term = getenv("TERM"))) { 211 (void)fprintf(stderr, "worms: no TERM environment variable.\n"); 212 exit(1); 213 } 214 if (!(worm = malloc((size_t)number * 215 sizeof(struct worm))) || !(mp = malloc((size_t)1024))) 216 nomem(); 217 if (tgetent(mp, term) <= 0) { 218 (void)fprintf(stderr, "worms: %s: unknown terminal type.\n", 219 term); 220 exit(1); 221 } 222 tcp = tcb; 223 if (!(CM = tgetstr("cm", &tcp))) { 224 (void)fprintf(stderr, 225 "worms: terminal incapable of cursor motion.\n"); 226 exit(1); 227 } 228 AL = tgetstr("al", &tcp); 229 BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp); 230 if ((CO = tgetnum("co")) <= 0) 231 CO = 80; 232 last = CO - 1; 233 EI = tgetstr("ei", &tcp); 234 HO = tgetstr("ho", &tcp); 235 IC = tgetstr("ic", &tcp); 236 IM = tgetstr("im", &tcp); 237 IN = tgetflag("in"); 238 IP = tgetstr("ip", &tcp); 239 if ((LI = tgetnum("li")) <= 0) 240 LI = 24; 241 bottom = LI - 1; 242 SR = tgetstr("sr", &tcp); 243 UP = tgetstr("up", &tcp); 244 Wrap = tgetflag("am"); 245 if (!(ip = malloc((size_t)(LI * CO * sizeof(short))))) 246 nomem(); 247 if (!(ref = malloc((size_t)(LI * sizeof(short *))))) 248 nomem(); 249 for (n = 0; n < LI; ++n) { 250 ref[n] = ip; 251 ip += CO; 252 } 253 for (ip = ref[0], n = LI * CO; --n >= 0;) 254 *ip++ = 0; 255 if (Wrap) 256 ref[bottom][last] = 1; 257 for (n = number, w = &worm[0]; --n >= 0; w++) { 258 w->orientation = w->head = 0; 259 if (!(ip = malloc((size_t)(length * sizeof(short))))) 260 nomem(); 261 w->xpos = ip; 262 for (x = length; --x >= 0;) 263 *ip++ = -1; 264 if (!(ip = malloc((size_t)(length * sizeof(short))))) 265 nomem(); 266 w->ypos = ip; 267 for (y = length; --y >= 0;) 268 *ip++ = -1; 269 } 270 271 (void)signal(SIGHUP, onsig); 272 (void)signal(SIGINT, onsig); 273 (void)signal(SIGQUIT, onsig); 274 (void)signal(SIGSTOP, onsig); 275 (void)signal(SIGTSTP, onsig); 276 (void)signal(SIGTERM, onsig); 277 278 tputs(tgetstr("ti", &tcp), 1, fputchar); 279 tputs(tgetstr("cl", &tcp), 1, fputchar); 280 if (field) { 281 register char *p = field; 282 283 for (y = bottom; --y >= 0;) { 284 for (x = CO; --x >= 0;) { 285 fputchar(*p++); 286 if (!*p) 287 p = field; 288 } 289 if (!Wrap) 290 fputchar('\n'); 291 (void)fflush(stdout); 292 } 293 if (Wrap) { 294 if (IM && !IN) { 295 for (x = last; --x > 0;) { 296 fputchar(*p++); 297 if (!*p) 298 p = field; 299 } 300 y = *p++; 301 if (!*p) 302 p = field; 303 fputchar(*p); 304 if (BC) 305 tputs(BC, 1, fputchar); 306 else 307 cursor(last - 1, bottom); 308 tputs(IM, 1, fputchar); 309 if (IC) 310 tputs(IC, 1, fputchar); 311 fputchar(y); 312 if (IP) 313 tputs(IP, 1, fputchar); 314 tputs(EI, 1, fputchar); 315 } 316 else if (SR || AL) { 317 if (HO) 318 tputs(HO, 1, fputchar); 319 else 320 cursor(0, 0); 321 if (SR) 322 tputs(SR, 1, fputchar); 323 else 324 tputs(AL, LI, fputchar); 325 for (x = CO; --x >= 0;) { 326 fputchar(*p++); 327 if (!*p) 328 p = field; 329 } 330 } 331 else for (x = last; --x >= 0;) { 332 fputchar(*p++); 333 if (!*p) 334 p = field; 335 } 336 } 337 else for (x = CO; --x >= 0;) { 338 fputchar(*p++); 339 if (!*p) 340 p = field; 341 } 342 } 343 for (;;) { 344 (void)fflush(stdout); 345 for (n = 0, w = &worm[0]; n < number; n++, w++) { 346 if ((x = w->xpos[h = w->head]) < 0) { 347 cursor(x = w->xpos[h] = 0, 348 y = w->ypos[h] = bottom); 349 fputchar(flavor[n % sizeof(flavor)]); 350 ref[y][x]++; 351 } 352 else 353 y = w->ypos[h]; 354 if (++h == length) 355 h = 0; 356 if (w->xpos[w->head = h] >= 0) { 357 register int x1, y1; 358 359 x1 = w->xpos[h]; 360 y1 = w->ypos[h]; 361 if (--ref[y1][x1] == 0) { 362 cursor(x1, y1); 363 if (trail) 364 fputchar(trail); 365 } 366 } 367 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; 368 switch (op->nopts) { 369 case 0: 370 (void)fflush(stdout); 371 abort(); 372 return; 373 case 1: 374 w->orientation = op->opts[0]; 375 break; 376 default: 377 w->orientation = 378 op->opts[(int)random() % op->nopts]; 379 } 380 cursor(x += xinc[w->orientation], 381 y += yinc[w->orientation]); 382 if (!Wrap || x != last || y != bottom) 383 fputchar(flavor[n % sizeof(flavor)]); 384 ref[w->ypos[h] = y][w->xpos[h] = x]++; 385 } 386 } 387 } 388 389 void 390 onsig(signo) 391 int signo; 392 { 393 tputs(tgetstr("cl", &tcp), 1, fputchar); 394 tputs(tgetstr("te", &tcp), 1, fputchar); 395 exit(0); 396 } 397 398 void 399 fputchar(c) 400 int c; 401 { 402 (void)putchar(c); 403 } 404 405 nomem() 406 { 407 (void)fprintf(stderr, "worms: not enough memory.\n"); 408 exit(1); 409 } 410