worms.c revision 1.20
1/* $OpenBSD: worms.c,v 1.20 2011/04/25 13:30:07 millert 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/* 33 * 34 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@ 35 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@ 36 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@ 37 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 38 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 39 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ 40 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ 41 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@ 42 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@ 43 * 44 * Eric P. Scott 45 * Caltech High Energy Physics 46 * October, 1980 47 * 48 */ 49#include <sys/types.h> 50 51#include <curses.h> 52#include <err.h> 53#include <signal.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <termios.h> 57#include <unistd.h> 58 59static const struct options { 60 int nopts; 61 int opts[3]; 62} 63 normal[8] = { 64 { 3, { 7, 0, 1 } }, 65 { 3, { 0, 1, 2 } }, 66 { 3, { 1, 2, 3 } }, 67 { 3, { 2, 3, 4 } }, 68 { 3, { 3, 4, 5 } }, 69 { 3, { 4, 5, 6 } }, 70 { 3, { 5, 6, 7 } }, 71 { 3, { 6, 7, 0 } } 72}, upper[8] = { 73 { 1, { 1, 0, 0 } }, 74 { 2, { 1, 2, 0 } }, 75 { 0, { 0, 0, 0 } }, 76 { 0, { 0, 0, 0 } }, 77 { 0, { 0, 0, 0 } }, 78 { 2, { 4, 5, 0 } }, 79 { 1, { 5, 0, 0 } }, 80 { 2, { 1, 5, 0 } } 81}, 82 left[8] = { 83 { 0, { 0, 0, 0 } }, 84 { 0, { 0, 0, 0 } }, 85 { 0, { 0, 0, 0 } }, 86 { 2, { 2, 3, 0 } }, 87 { 1, { 3, 0, 0 } }, 88 { 2, { 3, 7, 0 } }, 89 { 1, { 7, 0, 0 } }, 90 { 2, { 7, 0, 0 } } 91}, 92 right[8] = { 93 { 1, { 7, 0, 0 } }, 94 { 2, { 3, 7, 0 } }, 95 { 1, { 3, 0, 0 } }, 96 { 2, { 3, 4, 0 } }, 97 { 0, { 0, 0, 0 } }, 98 { 0, { 0, 0, 0 } }, 99 { 0, { 0, 0, 0 } }, 100 { 2, { 6, 7, 0 } } 101}, 102 lower[8] = { 103 { 0, { 0, 0, 0 } }, 104 { 2, { 0, 1, 0 } }, 105 { 1, { 1, 0, 0 } }, 106 { 2, { 1, 5, 0 } }, 107 { 1, { 5, 0, 0 } }, 108 { 2, { 5, 6, 0 } }, 109 { 0, { 0, 0, 0 } }, 110 { 0, { 0, 0, 0 } } 111}, 112 upleft[8] = { 113 { 0, { 0, 0, 0 } }, 114 { 0, { 0, 0, 0 } }, 115 { 0, { 0, 0, 0 } }, 116 { 0, { 0, 0, 0 } }, 117 { 0, { 0, 0, 0 } }, 118 { 1, { 3, 0, 0 } }, 119 { 2, { 1, 3, 0 } }, 120 { 1, { 1, 0, 0 } } 121}, 122 upright[8] = { 123 { 2, { 3, 5, 0 } }, 124 { 1, { 3, 0, 0 } }, 125 { 0, { 0, 0, 0 } }, 126 { 0, { 0, 0, 0 } }, 127 { 0, { 0, 0, 0 } }, 128 { 0, { 0, 0, 0 } }, 129 { 0, { 0, 0, 0 } }, 130 { 1, { 5, 0, 0 } } 131}, 132 lowleft[8] = { 133 { 3, { 7, 0, 1 } }, 134 { 0, { 0, 0, 0 } }, 135 { 0, { 0, 0, 0 } }, 136 { 1, { 1, 0, 0 } }, 137 { 2, { 1, 7, 0 } }, 138 { 1, { 7, 0, 0 } }, 139 { 0, { 0, 0, 0 } }, 140 { 0, { 0, 0, 0 } } 141}, 142 lowright[8] = { 143 { 0, { 0, 0, 0 } }, 144 { 1, { 7, 0, 0 } }, 145 { 2, { 5, 7, 0 } }, 146 { 1, { 5, 0, 0 } }, 147 { 0, { 0, 0, 0 } }, 148 { 0, { 0, 0, 0 } }, 149 { 0, { 0, 0, 0 } }, 150 { 0, { 0, 0, 0 } } 151}; 152 153static const char flavor[] = { 154 'O', '*', '#', '$', '%', '0', '@', '~' 155}; 156static const short xinc[] = { 157 1, 1, 1, 0, -1, -1, -1, 0 158}, yinc[] = { 159 -1, 0, 1, 1, 1, 0, -1, -1 160}; 161static struct worm { 162 int orientation, head; 163 short *xpos, *ypos; 164} *worm; 165 166volatile sig_atomic_t sig_caught = 0; 167 168void nomem(void); 169void onsig(int); 170 171int 172main(int argc, char *argv[]) 173{ 174 int x, y, h, n; 175 struct worm *w; 176 const struct options *op; 177 short *ip; 178 int CO, LI, last, bottom, ch, length, number, trail; 179 short **ref; 180 const char *field, *errstr; 181 struct timespec sleeptime; 182 struct termios term; 183 speed_t speed; 184 time_t delay = 0; 185 186 /* set default delay based on terminal baud rate */ 187 if (tcgetattr(STDOUT_FILENO, &term) == 0 && 188 (speed = cfgetospeed(&term)) > B9600) 189 delay = (speed / B9600) - 1; 190 191 length = 16; 192 number = 3; 193 trail = ' '; 194 field = NULL; 195 while ((ch = getopt(argc, argv, "d:fhl:n:t")) != -1) 196 switch(ch) { 197 case 'd': 198 delay = (time_t)strtonum(optarg, 0, 1000, &errstr); 199 if (errstr) 200 errx(1, "delay (0-1000) is %s: %s", errstr, optarg); 201 break; 202 case 'f': 203 field = "WORM"; 204 break; 205 case 'l': 206 if ((length = atoi(optarg)) < 2 || length > 1024) 207 errx(1, "invalid length (%d - %d).", 2, 1024); 208 break; 209 case 'n': 210 if ((number = atoi(optarg)) < 1 || number > 100) 211 errx(1, "invalid number of worms (%d - %d).", 212 1, 100); 213 break; 214 case 't': 215 trail = '.'; 216 break; 217 case '?': case 'h': 218 default: 219 (void)fprintf(stderr, 220 "usage: worms [-ft] [-d delay] [-l length] [-n number]\n"); 221 exit(1); 222 } 223 224 /* Convert delay from ms -> ns */ 225 sleeptime.tv_sec = 0; 226 sleeptime.tv_nsec = delay * 500000; 227 timespecadd(&sleeptime, &sleeptime, &sleeptime); 228 229 srandomdev(); 230 if (!(worm = calloc((size_t)number, sizeof(struct worm)))) 231 nomem(); 232 initscr(); 233 curs_set(0); 234 CO = COLS; 235 LI = LINES; 236 last = CO - 1; 237 bottom = LI - 1; 238 if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))) || 239 !(ref = calloc((size_t)LI, sizeof(short *)))) { 240 endwin(); 241 nomem(); 242 } 243 for (n = 0; n < LI; ++n) { 244 ref[n] = ip; 245 ip += CO; 246 } 247 for (ip = ref[0], n = LI * CO; --n >= 0;) 248 *ip++ = 0; 249 for (n = number, w = &worm[0]; --n >= 0; w++) { 250 w->orientation = w->head = 0; 251 if (!(ip = calloc((size_t)length, sizeof(short)))) { 252 endwin(); 253 nomem(); 254 } 255 w->xpos = ip; 256 for (x = length; --x >= 0;) 257 *ip++ = -1; 258 if (!(ip = calloc((size_t)length, sizeof(short)))) { 259 endwin(); 260 nomem(); 261 } 262 w->ypos = ip; 263 for (y = length; --y >= 0;) 264 *ip++ = -1; 265 } 266 267 (void)signal(SIGHUP, onsig); 268 (void)signal(SIGINT, onsig); 269 (void)signal(SIGQUIT, onsig); 270 (void)signal(SIGSTOP, onsig); 271 (void)signal(SIGTSTP, onsig); 272 (void)signal(SIGTERM, onsig); 273 274 if (field) { 275 const char *p = field; 276 277 for (y = LI; --y >= 0;) { 278 for (x = CO; --x >= 0;) { 279 addch(*p++); 280 if (!*p) 281 p = field; 282 } 283 refresh(); 284 } 285 } 286 for (;;) { 287 refresh(); 288 if (sig_caught) { 289 endwin(); 290 exit(0); 291 } 292 nanosleep(&sleeptime, NULL); 293 for (n = 0, w = &worm[0]; n < number; n++, w++) { 294 if ((x = w->xpos[h = w->head]) < 0) { 295 mvaddch(y = w->ypos[h] = bottom, 296 x = w->xpos[h] = 0, 297 flavor[n % sizeof(flavor)]); 298 ref[y][x]++; 299 } 300 else 301 y = w->ypos[h]; 302 if (++h == length) 303 h = 0; 304 if (w->xpos[w->head = h] >= 0) { 305 int x1, y1; 306 307 x1 = w->xpos[h]; 308 y1 = w->ypos[h]; 309 if (--ref[y1][x1] == 0) 310 mvaddch(y1, x1, trail); 311 } 312 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; 313 switch (op->nopts) { 314 case 0: 315 endwin(); 316 return(1); 317 case 1: 318 w->orientation = op->opts[0]; 319 break; 320 default: 321 w->orientation = 322 op->opts[(int)random() % op->nopts]; 323 } 324 mvaddch(y += yinc[w->orientation], 325 x += xinc[w->orientation], 326 flavor[n % sizeof(flavor)]); 327 ref[w->ypos[h] = y][w->xpos[h] = x]++; 328 } 329 } 330} 331 332void 333onsig(int signo) 334{ 335 sig_caught = 1; 336} 337 338void 339nomem(void) 340{ 341 errx(1, "not enough memory."); 342} 343