worms.c revision 1.5 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)worms.c 5.9 (Berkeley) 2/28/91";*/
42 static char rcsid[] = "$Id: worms.c,v 1.5 1995/01/31 15:54:02 jtc Exp $";
43 #endif /* not lint */
44
45 /*
46 *
47 * @@@ @@@ @@@@@@@@@@ @@@@@@@@@@@ @@@@@@@@@@@@
48 * @@@ @@@ @@@@@@@@@@@@ @@@@@@@@@@@@ @@@@@@@@@@@@@
49 * @@@ @@@ @@@@ @@@@ @@@@ @@@@ @@@ @@@@
50 * @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
51 * @@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
52 * @@@@ @@@@ @@@@ @@@ @@@ @@@ @@@ @@@ @@@
53 * @@@@@@@@@@@@ @@@@ @@@@ @@@ @@@ @@@ @@@
54 * @@@@ @@@@ @@@@@@@@@@@@ @@@ @@@ @@@ @@@
55 * @@ @@ @@@@@@@@@@ @@@ @@@ @@@ @@@
56 *
57 * Eric P. Scott
58 * Caltech High Energy Physics
59 * October, 1980
60 *
61 */
62 #include <sys/types.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <termios.h>
66 #include <signal.h>
67 #include <sys/ioctl.h>
68
69 static struct options {
70 int nopts;
71 int opts[3];
72 }
73 normal[8] = {
74 { 3, { 7, 0, 1 } },
75 { 3, { 0, 1, 2 } },
76 { 3, { 1, 2, 3 } },
77 { 3, { 2, 3, 4 } },
78 { 3, { 3, 4, 5 } },
79 { 3, { 4, 5, 6 } },
80 { 3, { 5, 6, 7 } },
81 { 3, { 6, 7, 0 } }
82 }, upper[8] = {
83 { 1, { 1, 0, 0 } },
84 { 2, { 1, 2, 0 } },
85 { 0, { 0, 0, 0 } },
86 { 0, { 0, 0, 0 } },
87 { 0, { 0, 0, 0 } },
88 { 2, { 4, 5, 0 } },
89 { 1, { 5, 0, 0 } },
90 { 2, { 1, 5, 0 } }
91 },
92 left[8] = {
93 { 0, { 0, 0, 0 } },
94 { 0, { 0, 0, 0 } },
95 { 0, { 0, 0, 0 } },
96 { 2, { 2, 3, 0 } },
97 { 1, { 3, 0, 0 } },
98 { 2, { 3, 7, 0 } },
99 { 1, { 7, 0, 0 } },
100 { 2, { 7, 0, 0 } }
101 },
102 right[8] = {
103 { 1, { 7, 0, 0 } },
104 { 2, { 3, 7, 0 } },
105 { 1, { 3, 0, 0 } },
106 { 2, { 3, 4, 0 } },
107 { 0, { 0, 0, 0 } },
108 { 0, { 0, 0, 0 } },
109 { 0, { 0, 0, 0 } },
110 { 2, { 6, 7, 0 } }
111 },
112 lower[8] = {
113 { 0, { 0, 0, 0 } },
114 { 2, { 0, 1, 0 } },
115 { 1, { 1, 0, 0 } },
116 { 2, { 1, 5, 0 } },
117 { 1, { 5, 0, 0 } },
118 { 2, { 5, 6, 0 } },
119 { 0, { 0, 0, 0 } },
120 { 0, { 0, 0, 0 } }
121 },
122 upleft[8] = {
123 { 0, { 0, 0, 0 } },
124 { 0, { 0, 0, 0 } },
125 { 0, { 0, 0, 0 } },
126 { 0, { 0, 0, 0 } },
127 { 0, { 0, 0, 0 } },
128 { 1, { 3, 0, 0 } },
129 { 2, { 1, 3, 0 } },
130 { 1, { 1, 0, 0 } }
131 },
132 upright[8] = {
133 { 2, { 3, 5, 0 } },
134 { 1, { 3, 0, 0 } },
135 { 0, { 0, 0, 0 } },
136 { 0, { 0, 0, 0 } },
137 { 0, { 0, 0, 0 } },
138 { 0, { 0, 0, 0 } },
139 { 0, { 0, 0, 0 } },
140 { 1, { 5, 0, 0 } }
141 },
142 lowleft[8] = {
143 { 3, { 7, 0, 1 } },
144 { 0, { 0, 0, 0 } },
145 { 0, { 0, 0, 0 } },
146 { 1, { 1, 0, 0 } },
147 { 2, { 1, 7, 0 } },
148 { 1, { 7, 0, 0 } },
149 { 0, { 0, 0, 0 } },
150 { 0, { 0, 0, 0 } }
151 },
152 lowright[8] = {
153 { 0, { 0, 0, 0 } },
154 { 1, { 7, 0, 0 } },
155 { 2, { 5, 7, 0 } },
156 { 1, { 5, 0, 0 } },
157 { 0, { 0, 0, 0 } },
158 { 0, { 0, 0, 0 } },
159 { 0, { 0, 0, 0 } },
160 { 0, { 0, 0, 0 } }
161 };
162
163 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, putchar)
164
165 char *tcp;
166
167 static char flavor[] = {
168 'O', '*', '#', '$', '%', '0', '@', '~'
169 };
170 static 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 char *tgetstr(), *tgoto();
181
182 main(argc, argv)
183 int argc;
184 char **argv;
185 {
186 extern int optind;
187 extern char *optarg, *UP;
188 register int x, y, h, n;
189 register struct worm *w;
190 register struct options *op;
191 register short *ip;
192 register char *term;
193 int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap;
194 void onsig();
195 short **ref;
196 char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR;
197 char *field, tcb[100], *mp;
198 long random();
199 struct termios ti;
200 #ifdef TIOCGWINSZ
201 struct winsize ws;
202 #endif
203
204 length = 16;
205 number = 3;
206 trail = ' ';
207 field = NULL;
208 while ((ch = getopt(argc, argv, "fl:n:t")) != EOF)
209 switch(ch) {
210 case 'f':
211 field = "WORM";
212 break;
213 case 'l':
214 if ((length = atoi(optarg)) < 2 || length > 1024) {
215 (void)fprintf(stderr,
216 "worms: invalid length (%d - %d).\n",
217 2, 1024);
218 exit(1);
219 }
220 break;
221 case 'n':
222 if ((number = atoi(optarg)) < 1) {
223 (void)fprintf(stderr,
224 "worms: invalid number of worms.\n");
225 exit(1);
226 }
227 break;
228 case 't':
229 trail = '.';
230 break;
231 case '?':
232 default:
233 (void)fprintf(stderr,
234 "usage: worms [-ft] [-l #] [-n #]\n");
235 exit(1);
236 }
237
238 if (!(term = getenv("TERM"))) {
239 (void)fprintf(stderr, "worms: no TERM environment variable.\n");
240 exit(1);
241 }
242 if (!(worm = (struct worm *)malloc((u_int)number *
243 sizeof(struct worm))) || !(mp = malloc((u_int)1024)))
244 nomem();
245 if (tgetent(mp, term) <= 0) {
246 (void)fprintf(stderr, "worms: %s: unknown terminal type.\n",
247 term);
248 exit(1);
249 }
250 tcp = tcb;
251 if (!(CM = tgetstr("cm", &tcp))) {
252 (void)fprintf(stderr,
253 "worms: terminal incapable of cursor motion.\n");
254 exit(1);
255 }
256 AL = tgetstr("al", &tcp);
257 BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp);
258 EI = tgetstr("ei", &tcp);
259 HO = tgetstr("ho", &tcp);
260 IC = tgetstr("ic", &tcp);
261 IM = tgetstr("im", &tcp);
262 IN = tgetflag("in");
263 IP = tgetstr("ip", &tcp);
264 SR = tgetstr("sr", &tcp);
265 UP = tgetstr("up", &tcp);
266 #ifdef TIOCGWINSZ
267 if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 &&
268 ws.ws_col && ws.ws_row) {
269 CO = ws.ws_col;
270 LI = ws.ws_row;
271 } else
272 #endif
273 {
274 if ((CO = tgetnum("co")) <= 0)
275 CO = 80;
276 if ((LI = tgetnum("li")) <= 0)
277 LI = 24;
278 }
279 last = CO - 1;
280 bottom = LI - 1;
281 tcgetattr(fileno(stdout), &ti);
282 Wrap = tgetflag("am");
283 if (!(ip = (short *)malloc((u_int)(LI * CO * sizeof(short)))))
284 nomem();
285 if (!(ref = (short **)malloc((u_int)(LI * sizeof(short *)))))
286 nomem();
287 for (n = 0; n < LI; ++n) {
288 ref[n] = ip;
289 ip += CO;
290 }
291 for (ip = ref[0], n = LI * CO; --n >= 0;)
292 *ip++ = 0;
293 if (Wrap)
294 ref[bottom][last] = 1;
295 for (n = number, w = &worm[0]; --n >= 0; w++) {
296 w->orientation = w->head = 0;
297 if (!(ip = (short *)malloc((u_int)(length * sizeof(short)))))
298 nomem();
299 w->xpos = ip;
300 for (x = length; --x >= 0;)
301 *ip++ = -1;
302 if (!(ip = (short *)malloc((u_int)(length * sizeof(short)))))
303 nomem();
304 w->ypos = ip;
305 for (y = length; --y >= 0;)
306 *ip++ = -1;
307 }
308
309 (void)signal(SIGHUP, onsig);
310 (void)signal(SIGINT, onsig);
311 (void)signal(SIGQUIT, onsig);
312 (void)signal(SIGSTOP, onsig);
313 (void)signal(SIGTSTP, onsig);
314 (void)signal(SIGTERM, onsig);
315
316 tputs(tgetstr("ti", &tcp), 1, putchar);
317 tputs(tgetstr("cl", &tcp), 1, putchar);
318 if (field) {
319 register char *p = field;
320
321 for (y = bottom; --y >= 0;) {
322 for (x = CO; --x >= 0;) {
323 putchar(*p++);
324 if (!*p)
325 p = field;
326 }
327 if (!Wrap)
328 putchar('\n');
329 (void)fflush(stdout);
330 }
331 if (Wrap) {
332 if (IM && !IN) {
333 for (x = last; --x > 0;) {
334 putchar(*p++);
335 if (!*p)
336 p = field;
337 }
338 y = *p++;
339 if (!*p)
340 p = field;
341 putchar(*p);
342 if (BC)
343 tputs(BC, 1, putchar);
344 else
345 cursor(last - 1, bottom);
346 tputs(IM, 1, putchar);
347 if (IC)
348 tputs(IC, 1, putchar);
349 putchar(y);
350 if (IP)
351 tputs(IP, 1, putchar);
352 tputs(EI, 1, putchar);
353 }
354 else if (SR || AL) {
355 if (HO)
356 tputs(HO, 1, putchar);
357 else
358 cursor(0, 0);
359 if (SR)
360 tputs(SR, 1, putchar);
361 else
362 tputs(AL, LI, putchar);
363 for (x = CO; --x >= 0;) {
364 putchar(*p++);
365 if (!*p)
366 p = field;
367 }
368 }
369 else for (x = last; --x >= 0;) {
370 putchar(*p++);
371 if (!*p)
372 p = field;
373 }
374 }
375 else for (x = CO; --x >= 0;) {
376 putchar(*p++);
377 if (!*p)
378 p = field;
379 }
380 }
381 for (;;) {
382 (void)fflush(stdout);
383 for (n = 0, w = &worm[0]; n < number; n++, w++) {
384 if ((x = w->xpos[h = w->head]) < 0) {
385 cursor(x = w->xpos[h] = 0,
386 y = w->ypos[h] = bottom);
387 putchar(flavor[n % sizeof(flavor)]);
388 ref[y][x]++;
389 }
390 else
391 y = w->ypos[h];
392 if (++h == length)
393 h = 0;
394 if (w->xpos[w->head = h] >= 0) {
395 register int x1, y1;
396
397 x1 = w->xpos[h];
398 y1 = w->ypos[h];
399 if (--ref[y1][x1] == 0) {
400 cursor(x1, y1);
401 if (trail)
402 putchar(trail);
403 }
404 }
405 op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation];
406 switch (op->nopts) {
407 case 0:
408 (void)fflush(stdout);
409 abort();
410 return;
411 case 1:
412 w->orientation = op->opts[0];
413 break;
414 default:
415 w->orientation =
416 op->opts[(int)random() % op->nopts];
417 }
418 cursor(x += xinc[w->orientation],
419 y += yinc[w->orientation]);
420 if (!Wrap || x != last || y != bottom)
421 putchar(flavor[n % sizeof(flavor)]);
422 ref[w->ypos[h] = y][w->xpos[h] = x]++;
423 }
424 }
425 }
426
427 void
428 onsig()
429 {
430 tputs(tgetstr("cl", &tcp), 1, putchar);
431 tputs(tgetstr("te", &tcp), 1, putchar);
432 exit(0);
433 }
434
435 nomem()
436 {
437 (void)fprintf(stderr, "worms: not enough memory.\n");
438 exit(1);
439 }
440