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