worms.c revision 1.26 1 1.26 kre /* $NetBSD: worms.c,v 1.26 2023/04/15 15:21:56 kre Exp $ */
2 1.6 cgd
3 1.1 cgd /*
4 1.6 cgd * Copyright (c) 1980, 1993
5 1.6 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.12 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.9 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.18 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 1.18 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.6 cgd #if 0
40 1.6 cgd static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93";
41 1.6 cgd #else
42 1.26 kre __RCSID("$NetBSD: worms.c,v 1.26 2023/04/15 15:21:56 kre Exp $");
43 1.6 cgd #endif
44 1.1 cgd #endif /* not lint */
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 *
58 1.1 cgd * Eric P. Scott
59 1.1 cgd * Caltech High Energy Physics
60 1.1 cgd * October, 1980
61 1.1 cgd *
62 1.1 cgd */
63 1.1 cgd #include <sys/types.h>
64 1.6 cgd
65 1.26 kre #include <ctype.h>
66 1.11 hubertf #include <curses.h>
67 1.11 hubertf #include <err.h>
68 1.26 kre #include <limits.h>
69 1.6 cgd #include <signal.h>
70 1.1 cgd #include <stdio.h>
71 1.2 mycroft #include <stdlib.h>
72 1.26 kre #include <strings.h>
73 1.6 cgd #include <unistd.h>
74 1.1 cgd
75 1.11 hubertf static const struct options {
76 1.1 cgd int nopts;
77 1.1 cgd int opts[3];
78 1.1 cgd }
79 1.1 cgd normal[8] = {
80 1.1 cgd { 3, { 7, 0, 1 } },
81 1.1 cgd { 3, { 0, 1, 2 } },
82 1.1 cgd { 3, { 1, 2, 3 } },
83 1.1 cgd { 3, { 2, 3, 4 } },
84 1.1 cgd { 3, { 3, 4, 5 } },
85 1.1 cgd { 3, { 4, 5, 6 } },
86 1.1 cgd { 3, { 5, 6, 7 } },
87 1.1 cgd { 3, { 6, 7, 0 } }
88 1.1 cgd }, upper[8] = {
89 1.1 cgd { 1, { 1, 0, 0 } },
90 1.1 cgd { 2, { 1, 2, 0 } },
91 1.1 cgd { 0, { 0, 0, 0 } },
92 1.1 cgd { 0, { 0, 0, 0 } },
93 1.1 cgd { 0, { 0, 0, 0 } },
94 1.1 cgd { 2, { 4, 5, 0 } },
95 1.1 cgd { 1, { 5, 0, 0 } },
96 1.1 cgd { 2, { 1, 5, 0 } }
97 1.1 cgd },
98 1.1 cgd left[8] = {
99 1.1 cgd { 0, { 0, 0, 0 } },
100 1.1 cgd { 0, { 0, 0, 0 } },
101 1.1 cgd { 0, { 0, 0, 0 } },
102 1.1 cgd { 2, { 2, 3, 0 } },
103 1.1 cgd { 1, { 3, 0, 0 } },
104 1.1 cgd { 2, { 3, 7, 0 } },
105 1.1 cgd { 1, { 7, 0, 0 } },
106 1.1 cgd { 2, { 7, 0, 0 } }
107 1.1 cgd },
108 1.1 cgd right[8] = {
109 1.1 cgd { 1, { 7, 0, 0 } },
110 1.1 cgd { 2, { 3, 7, 0 } },
111 1.1 cgd { 1, { 3, 0, 0 } },
112 1.1 cgd { 2, { 3, 4, 0 } },
113 1.1 cgd { 0, { 0, 0, 0 } },
114 1.1 cgd { 0, { 0, 0, 0 } },
115 1.1 cgd { 0, { 0, 0, 0 } },
116 1.1 cgd { 2, { 6, 7, 0 } }
117 1.1 cgd },
118 1.1 cgd lower[8] = {
119 1.1 cgd { 0, { 0, 0, 0 } },
120 1.1 cgd { 2, { 0, 1, 0 } },
121 1.1 cgd { 1, { 1, 0, 0 } },
122 1.1 cgd { 2, { 1, 5, 0 } },
123 1.1 cgd { 1, { 5, 0, 0 } },
124 1.1 cgd { 2, { 5, 6, 0 } },
125 1.1 cgd { 0, { 0, 0, 0 } },
126 1.1 cgd { 0, { 0, 0, 0 } }
127 1.1 cgd },
128 1.1 cgd upleft[8] = {
129 1.1 cgd { 0, { 0, 0, 0 } },
130 1.1 cgd { 0, { 0, 0, 0 } },
131 1.1 cgd { 0, { 0, 0, 0 } },
132 1.1 cgd { 0, { 0, 0, 0 } },
133 1.1 cgd { 0, { 0, 0, 0 } },
134 1.1 cgd { 1, { 3, 0, 0 } },
135 1.1 cgd { 2, { 1, 3, 0 } },
136 1.1 cgd { 1, { 1, 0, 0 } }
137 1.1 cgd },
138 1.1 cgd upright[8] = {
139 1.1 cgd { 2, { 3, 5, 0 } },
140 1.1 cgd { 1, { 3, 0, 0 } },
141 1.1 cgd { 0, { 0, 0, 0 } },
142 1.1 cgd { 0, { 0, 0, 0 } },
143 1.1 cgd { 0, { 0, 0, 0 } },
144 1.1 cgd { 0, { 0, 0, 0 } },
145 1.1 cgd { 0, { 0, 0, 0 } },
146 1.1 cgd { 1, { 5, 0, 0 } }
147 1.1 cgd },
148 1.1 cgd lowleft[8] = {
149 1.1 cgd { 3, { 7, 0, 1 } },
150 1.1 cgd { 0, { 0, 0, 0 } },
151 1.1 cgd { 0, { 0, 0, 0 } },
152 1.1 cgd { 1, { 1, 0, 0 } },
153 1.1 cgd { 2, { 1, 7, 0 } },
154 1.1 cgd { 1, { 7, 0, 0 } },
155 1.1 cgd { 0, { 0, 0, 0 } },
156 1.1 cgd { 0, { 0, 0, 0 } }
157 1.1 cgd },
158 1.1 cgd lowright[8] = {
159 1.1 cgd { 0, { 0, 0, 0 } },
160 1.1 cgd { 1, { 7, 0, 0 } },
161 1.1 cgd { 2, { 5, 7, 0 } },
162 1.1 cgd { 1, { 5, 0, 0 } },
163 1.1 cgd { 0, { 0, 0, 0 } },
164 1.1 cgd { 0, { 0, 0, 0 } },
165 1.1 cgd { 0, { 0, 0, 0 } },
166 1.1 cgd { 0, { 0, 0, 0 } }
167 1.1 cgd };
168 1.1 cgd
169 1.1 cgd
170 1.11 hubertf static const char flavor[] = {
171 1.1 cgd 'O', '*', '#', '$', '%', '0', '@', '~'
172 1.1 cgd };
173 1.11 hubertf static const short xinc[] = {
174 1.1 cgd 1, 1, 1, 0, -1, -1, -1, 0
175 1.1 cgd }, yinc[] = {
176 1.1 cgd -1, 0, 1, 1, 1, 0, -1, -1
177 1.1 cgd };
178 1.1 cgd static struct worm {
179 1.1 cgd int orientation, head;
180 1.1 cgd short *xpos, *ypos;
181 1.1 cgd } *worm;
182 1.1 cgd
183 1.20 dholland static volatile sig_atomic_t sig_caught = 0;
184 1.11 hubertf
185 1.13 jsm int main(int, char **);
186 1.20 dholland static void nomem(void) __dead;
187 1.20 dholland static void onsig(int);
188 1.2 mycroft
189 1.6 cgd int
190 1.22 dholland main(int argc, char *argv[])
191 1.1 cgd {
192 1.9 lukem int x, y, h, n;
193 1.9 lukem struct worm *w;
194 1.11 hubertf const struct options *op;
195 1.9 lukem short *ip;
196 1.11 hubertf int CO, LI, last, bottom, ch, length, number, trail;
197 1.1 cgd short **ref;
198 1.11 hubertf const char *field;
199 1.26 kre char *ep;
200 1.23 nia unsigned int delay = 20000;
201 1.26 kre unsigned long ul;
202 1.1 cgd
203 1.1 cgd length = 16;
204 1.1 cgd number = 3;
205 1.1 cgd trail = ' ';
206 1.1 cgd field = NULL;
207 1.11 hubertf while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1)
208 1.1 cgd switch(ch) {
209 1.11 hubertf case 'd':
210 1.26 kre ul = strtoul(optarg, &ep, 10);
211 1.26 kre if (ep != optarg) {
212 1.26 kre while (isspace(*(unsigned char *)ep))
213 1.26 kre ep++;
214 1.26 kre }
215 1.26 kre if (ep == optarg ||
216 1.26 kre (*ep != '\0' &&
217 1.26 kre ( ep[1] == '\0' ? (*ep != 'm' && *ep != 'u') :
218 1.26 kre ( strcasecmp(ep, "ms") != 0 &&
219 1.26 kre strcasecmp(ep, "us") != 0 )) )) {
220 1.26 kre errx(1, "-d: invalid delay (%s)", optarg);
221 1.26 kre }
222 1.26 kre /*
223 1.26 kre * if ul >= INT_MAX/1000 we don't need the *1000,
224 1.26 kre * as even without that it will exceed the limit
225 1.26 kre * just below and be treated as an error.
226 1.26 kre * (This does assume >=32 bit int, but so does POSIX)
227 1.26 kre */
228 1.26 kre if (*ep != 'u' && ul < INT_MAX / 1000)
229 1.26 kre ul *= 1000; /* ms -> us */
230 1.26 kre if (ul > 1000*1000) {
231 1.26 kre errx(1,
232 1.26 kre "-d: delay (%s) out of rannge [0 - 1000]",
233 1.26 kre optarg);
234 1.26 kre }
235 1.26 kre delay = (unsigned int)ul;
236 1.11 hubertf break;
237 1.1 cgd case 'f':
238 1.1 cgd field = "WORM";
239 1.1 cgd break;
240 1.1 cgd case 'l':
241 1.26 kre ul = strtoul(optarg, &ep, 10);
242 1.26 kre if (ep == optarg || *ep != '\0' ||
243 1.26 kre ul < 2 || ul > 1024) {
244 1.26 kre errx(1, "-l: invalid length (%s) [%d - %d].",
245 1.26 kre optarg, 2, 1024);
246 1.1 cgd }
247 1.26 kre length = (int)ul;
248 1.1 cgd break;
249 1.1 cgd case 'n':
250 1.26 kre ul = strtoul(optarg, &ep, 10);
251 1.26 kre if (ep == optarg || *ep != '\0' ||
252 1.26 kre ul < 1 || ul > INT_MAX / 10 ) {
253 1.26 kre errx(1, "-n: invalid number of worms (%s).",
254 1.26 kre optarg);
255 1.1 cgd }
256 1.26 kre /* upper bound is further limited later */
257 1.26 kre number = (int)ul;
258 1.1 cgd break;
259 1.1 cgd case 't':
260 1.1 cgd trail = '.';
261 1.1 cgd break;
262 1.1 cgd case '?':
263 1.1 cgd default:
264 1.1 cgd (void)fprintf(stderr,
265 1.26 kre "usage: worms [-ft] [-d delay] [-l length]"
266 1.26 kre " [-n number]\n");
267 1.1 cgd exit(1);
268 1.1 cgd }
269 1.1 cgd
270 1.19 drochner if (!initscr())
271 1.26 kre errx(1, "couldn't initialize screen");
272 1.14 jsm curs_set(0);
273 1.11 hubertf CO = COLS;
274 1.11 hubertf LI = LINES;
275 1.26 kre
276 1.26 kre if (CO > 4*length || LI < 4*length) {
277 1.26 kre ul = (unsigned long)LI / 2;
278 1.26 kre ul *= (unsigned long)CO / length;
279 1.26 kre } else {
280 1.26 kre ul = (unsigned long)CO / 2;
281 1.26 kre ul *= (unsigned long)LI / length;
282 1.26 kre }
283 1.26 kre
284 1.26 kre if ((unsigned long)(unsigned)number > ul) {
285 1.26 kre endwin();
286 1.26 kre errx(1, "-n: too many worms (%d) max: %lu", number, ul);
287 1.26 kre }
288 1.26 kre if (!(worm = calloc((size_t)number, sizeof(struct worm))))
289 1.26 kre nomem();
290 1.26 kre
291 1.4 mycroft last = CO - 1;
292 1.4 mycroft bottom = LI - 1;
293 1.6 cgd if (!(ip = malloc((size_t)(LI * CO * sizeof(short)))))
294 1.1 cgd nomem();
295 1.6 cgd if (!(ref = malloc((size_t)(LI * sizeof(short *)))))
296 1.1 cgd nomem();
297 1.1 cgd for (n = 0; n < LI; ++n) {
298 1.1 cgd ref[n] = ip;
299 1.1 cgd ip += CO;
300 1.1 cgd }
301 1.1 cgd for (ip = ref[0], n = LI * CO; --n >= 0;)
302 1.1 cgd *ip++ = 0;
303 1.1 cgd for (n = number, w = &worm[0]; --n >= 0; w++) {
304 1.1 cgd w->orientation = w->head = 0;
305 1.6 cgd if (!(ip = malloc((size_t)(length * sizeof(short)))))
306 1.1 cgd nomem();
307 1.1 cgd w->xpos = ip;
308 1.1 cgd for (x = length; --x >= 0;)
309 1.1 cgd *ip++ = -1;
310 1.6 cgd if (!(ip = malloc((size_t)(length * sizeof(short)))))
311 1.1 cgd nomem();
312 1.1 cgd w->ypos = ip;
313 1.1 cgd for (y = length; --y >= 0;)
314 1.1 cgd *ip++ = -1;
315 1.1 cgd }
316 1.1 cgd
317 1.1 cgd (void)signal(SIGHUP, onsig);
318 1.1 cgd (void)signal(SIGINT, onsig);
319 1.1 cgd (void)signal(SIGQUIT, onsig);
320 1.1 cgd (void)signal(SIGTSTP, onsig);
321 1.1 cgd (void)signal(SIGTERM, onsig);
322 1.1 cgd
323 1.1 cgd if (field) {
324 1.11 hubertf const char *p = field;
325 1.1 cgd
326 1.11 hubertf for (y = LI; --y >= 0;) {
327 1.1 cgd for (x = CO; --x >= 0;) {
328 1.11 hubertf addch(*p++);
329 1.1 cgd if (!*p)
330 1.1 cgd p = field;
331 1.1 cgd }
332 1.11 hubertf refresh();
333 1.1 cgd }
334 1.1 cgd }
335 1.1 cgd for (;;) {
336 1.11 hubertf refresh();
337 1.11 hubertf if (sig_caught) {
338 1.11 hubertf endwin();
339 1.11 hubertf exit(0);
340 1.11 hubertf }
341 1.15 hubertf if (delay) {
342 1.15 hubertf if (delay % 1000000 != 0)
343 1.15 hubertf usleep(delay % 1000000);
344 1.15 hubertf if (delay >= 1000000)
345 1.15 hubertf sleep(delay / 1000000);
346 1.15 hubertf }
347 1.1 cgd for (n = 0, w = &worm[0]; n < number; n++, w++) {
348 1.1 cgd if ((x = w->xpos[h = w->head]) < 0) {
349 1.11 hubertf mvaddch(y = w->ypos[h] = bottom,
350 1.11 hubertf x = w->xpos[h] = 0,
351 1.11 hubertf flavor[n % sizeof(flavor)]);
352 1.1 cgd ref[y][x]++;
353 1.1 cgd }
354 1.1 cgd else
355 1.1 cgd y = w->ypos[h];
356 1.1 cgd if (++h == length)
357 1.1 cgd h = 0;
358 1.1 cgd if (w->xpos[w->head = h] >= 0) {
359 1.9 lukem int x1, y1;
360 1.1 cgd
361 1.1 cgd x1 = w->xpos[h];
362 1.1 cgd y1 = w->ypos[h];
363 1.1 cgd if (--ref[y1][x1] == 0) {
364 1.11 hubertf mvaddch(y1, x1, trail);
365 1.1 cgd }
366 1.1 cgd }
367 1.25 kre
368 1.25 kre op = &(!x
369 1.25 kre ? (!y
370 1.25 kre ? upleft
371 1.25 kre : (y == bottom ? lowleft : left))
372 1.25 kre : (x == last
373 1.25 kre ? (!y ? upright
374 1.25 kre : (y == bottom ? lowright : right))
375 1.25 kre : (!y ? upper
376 1.25 kre : (y == bottom ? lower : normal)))
377 1.25 kre )[w->orientation];
378 1.25 kre
379 1.1 cgd switch (op->nopts) {
380 1.1 cgd case 0:
381 1.11 hubertf refresh();
382 1.1 cgd abort();
383 1.9 lukem return(1);
384 1.1 cgd case 1:
385 1.1 cgd w->orientation = op->opts[0];
386 1.1 cgd break;
387 1.1 cgd default:
388 1.1 cgd w->orientation =
389 1.1 cgd op->opts[(int)random() % op->nopts];
390 1.1 cgd }
391 1.11 hubertf mvaddch(y += yinc[w->orientation],
392 1.11 hubertf x += xinc[w->orientation],
393 1.11 hubertf flavor[n % sizeof(flavor)]);
394 1.1 cgd ref[w->ypos[h] = y][w->xpos[h] = x]++;
395 1.1 cgd }
396 1.1 cgd }
397 1.1 cgd }
398 1.1 cgd
399 1.20 dholland static void
400 1.22 dholland onsig(int signo __unused)
401 1.6 cgd {
402 1.11 hubertf sig_caught = 1;
403 1.1 cgd }
404 1.1 cgd
405 1.26 kre /* This is never called before curses is initialised */
406 1.20 dholland static void
407 1.22 dholland nomem(void)
408 1.1 cgd {
409 1.26 kre endwin();
410 1.11 hubertf errx(1, "not enough memory.");
411 1.1 cgd }
412