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