X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/4b26ed93ffb8621a7470484dba8916b4fdb550ef..e5c17e55df9ac3ba0cc4e6836b1296ca4e09c863:/worms/worms.c diff --git a/worms/worms.c b/worms/worms.c index b8867eb0..b69ec125 100644 --- a/worms/worms.c +++ b/worms/worms.c @@ -1,6 +1,8 @@ +/* $NetBSD: worms.c,v 1.22 2012/06/19 05:46:09 dholland Exp $ */ + /* - * Copyright (c) 1980 Regents of the University of California. - * All rights reserved. + * Copyright (c) 1980, 1993 + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,11 +12,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,15 +29,18 @@ * SUCH DAMAGE. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1980 Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1980, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)worms.c 5.9 (Berkeley) 2/28/91";*/ -static char rcsid[] = "$Id: worms.c,v 1.4 1994/04/05 00:21:20 mycroft Exp $"; +#if 0 +static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: worms.c,v 1.22 2012/06/19 05:46:09 dholland Exp $"); +#endif #endif /* not lint */ /* @@ -60,13 +61,15 @@ static char rcsid[] = "$Id: worms.c,v 1.4 1994/04/05 00:21:20 mycroft Exp $"; * */ #include + +#include +#include +#include #include #include -#include -#include -#include +#include -static struct options { +static const struct options { int nopts; int opts[3]; } @@ -160,14 +163,11 @@ static struct options { { 0, { 0, 0, 0 } } }; -#define cursor(c, r) tputs(tgoto(CM, c, r), 1, putchar) - -char *tcp; -static char flavor[] = { +static const char flavor[] = { 'O', '*', '#', '$', '%', '0', '@', '~' }; -static short xinc[] = { +static const short xinc[] = { 1, 1, 1, 0, -1, -1, -1, 0 }, yinc[] = { -1, 0, 1, 1, 1, 0, -1, -1 @@ -177,52 +177,49 @@ static struct worm { short *xpos, *ypos; } *worm; -char *tgetstr(), *tgoto(); +static volatile sig_atomic_t sig_caught = 0; + +int main(int, char **); +static void nomem(void) __dead; +static void onsig(int); -main(argc, argv) - int argc; - char **argv; +int +main(int argc, char *argv[]) { - extern int optind; - extern char *optarg, *UP; - register int x, y, h, n; - register struct worm *w; - register struct options *op; - register short *ip; - register char *term; - int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap; - void onsig(); + int x, y, h, n; + struct worm *w; + const struct options *op; + short *ip; + int CO, LI, last, bottom, ch, length, number, trail; short **ref; - char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR; - char *field, tcb[100], *mp; - long random(); - struct termios ti; -#ifdef TIOCGWINSZ - struct winsize ws; -#endif + const char *field; + char *mp; + unsigned int delay = 0; + mp = NULL; length = 16; number = 3; trail = ' '; field = NULL; - while ((ch = getopt(argc, argv, "fl:n:t")) != EOF) + while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1) switch(ch) { + case 'd': + if ((delay = (unsigned int)strtoul(optarg, NULL, 10)) < 1 || delay > 1000) + errx(1, "invalid delay (1-1000)"); + delay *= 1000; /* ms -> us */ + break; case 'f': field = "WORM"; break; case 'l': if ((length = atoi(optarg)) < 2 || length > 1024) { - (void)fprintf(stderr, - "worms: invalid length (%d - %d).\n", + errx(1, "invalid length (%d - %d).", 2, 1024); - exit(1); } break; case 'n': if ((number = atoi(optarg)) < 1) { - (void)fprintf(stderr, - "worms: invalid number of worms.\n"); - exit(1); + errx(1, "invalid number of worms."); } break; case 't': @@ -231,58 +228,23 @@ main(argc, argv) case '?': default: (void)fprintf(stderr, - "usage: worms [-ft] [-length #] [-number #]\n"); + "usage: worms [-ft] [-d delay] [-l length] [-n number]\n"); exit(1); } - if (!(term = getenv("TERM"))) { - (void)fprintf(stderr, "worms: no TERM environment variable.\n"); - exit(1); - } - if (!(worm = (struct worm *)malloc((u_int)number * - sizeof(struct worm))) || !(mp = malloc((u_int)1024))) + if (!(worm = malloc((size_t)number * + sizeof(struct worm))) || !(mp = malloc((size_t)1024))) nomem(); - if (tgetent(mp, term) <= 0) { - (void)fprintf(stderr, "worms: %s: unknown terminal type.\n", - term); - exit(1); - } - tcp = tcb; - if (!(CM = tgetstr("cm", &tcp))) { - (void)fprintf(stderr, - "worms: terminal incapable of cursor motion.\n"); - exit(1); - } - AL = tgetstr("al", &tcp); - BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp); - EI = tgetstr("ei", &tcp); - HO = tgetstr("ho", &tcp); - IC = tgetstr("ic", &tcp); - IM = tgetstr("im", &tcp); - IN = tgetflag("in"); - IP = tgetstr("ip", &tcp); - SR = tgetstr("sr", &tcp); - UP = tgetstr("up", &tcp); -#ifdef TIOCGWINSZ - if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 && - ws.ws_col && ws.ws_row) { - CO = ws.ws_col; - LI = ws.ws_row; - } else -#endif - { - if ((CO = tgetnum("co")) <= 0) - CO = 80; - if ((LI = tgetnum("li")) <= 0) - LI = 24; - } + if (!initscr()) + errx(0, "couldn't initialize screen"); + curs_set(0); + CO = COLS; + LI = LINES; last = CO - 1; bottom = LI - 1; - tcgetattr(fileno(stdout), &ti); - Wrap = tgetflag("am"); - if (!(ip = (short *)malloc((u_int)(LI * CO * sizeof(short))))) + if (!(ip = malloc((size_t)(LI * CO * sizeof(short))))) nomem(); - if (!(ref = (short **)malloc((u_int)(LI * sizeof(short *))))) + if (!(ref = malloc((size_t)(LI * sizeof(short *))))) nomem(); for (n = 0; n < LI; ++n) { ref[n] = ip; @@ -290,16 +252,14 @@ main(argc, argv) } for (ip = ref[0], n = LI * CO; --n >= 0;) *ip++ = 0; - if (Wrap) - ref[bottom][last] = 1; for (n = number, w = &worm[0]; --n >= 0; w++) { w->orientation = w->head = 0; - if (!(ip = (short *)malloc((u_int)(length * sizeof(short))))) + if (!(ip = malloc((size_t)(length * sizeof(short))))) nomem(); w->xpos = ip; for (x = length; --x >= 0;) *ip++ = -1; - if (!(ip = (short *)malloc((u_int)(length * sizeof(short))))) + if (!(ip = malloc((size_t)(length * sizeof(short))))) nomem(); w->ypos = ip; for (y = length; --y >= 0;) @@ -309,82 +269,38 @@ main(argc, argv) (void)signal(SIGHUP, onsig); (void)signal(SIGINT, onsig); (void)signal(SIGQUIT, onsig); - (void)signal(SIGSTOP, onsig); (void)signal(SIGTSTP, onsig); (void)signal(SIGTERM, onsig); - tputs(tgetstr("ti", &tcp), 1, putchar); - tputs(tgetstr("cl", &tcp), 1, putchar); if (field) { - register char *p = field; + const char *p = field; - for (y = bottom; --y >= 0;) { + for (y = LI; --y >= 0;) { for (x = CO; --x >= 0;) { - putchar(*p++); + addch(*p++); if (!*p) p = field; } - if (!Wrap) - putchar('\n'); - (void)fflush(stdout); - } - if (Wrap) { - if (IM && !IN) { - for (x = last; --x > 0;) { - putchar(*p++); - if (!*p) - p = field; - } - y = *p++; - if (!*p) - p = field; - putchar(*p); - if (BC) - tputs(BC, 1, putchar); - else - cursor(last - 1, bottom); - tputs(IM, 1, putchar); - if (IC) - tputs(IC, 1, putchar); - putchar(y); - if (IP) - tputs(IP, 1, putchar); - tputs(EI, 1, putchar); - } - else if (SR || AL) { - if (HO) - tputs(HO, 1, putchar); - else - cursor(0, 0); - if (SR) - tputs(SR, 1, putchar); - else - tputs(AL, LI, putchar); - for (x = CO; --x >= 0;) { - putchar(*p++); - if (!*p) - p = field; - } - } - else for (x = last; --x >= 0;) { - putchar(*p++); - if (!*p) - p = field; - } - } - else for (x = CO; --x >= 0;) { - putchar(*p++); - if (!*p) - p = field; + refresh(); } } for (;;) { - (void)fflush(stdout); + refresh(); + if (sig_caught) { + endwin(); + exit(0); + } + if (delay) { + if (delay % 1000000 != 0) + usleep(delay % 1000000); + if (delay >= 1000000) + sleep(delay / 1000000); + } for (n = 0, w = &worm[0]; n < number; n++, w++) { if ((x = w->xpos[h = w->head]) < 0) { - cursor(x = w->xpos[h] = 0, - y = w->ypos[h] = bottom); - putchar(flavor[n % sizeof(flavor)]); + mvaddch(y = w->ypos[h] = bottom, + x = w->xpos[h] = 0, + flavor[n % sizeof(flavor)]); ref[y][x]++; } else @@ -392,22 +308,20 @@ main(argc, argv) if (++h == length) h = 0; if (w->xpos[w->head = h] >= 0) { - register int x1, y1; + int x1, y1; x1 = w->xpos[h]; y1 = w->ypos[h]; if (--ref[y1][x1] == 0) { - cursor(x1, y1); - if (trail) - putchar(trail); + mvaddch(y1, x1, trail); } } op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; switch (op->nopts) { case 0: - (void)fflush(stdout); + refresh(); abort(); - return; + return(1); case 1: w->orientation = op->opts[0]; break; @@ -415,25 +329,22 @@ main(argc, argv) w->orientation = op->opts[(int)random() % op->nopts]; } - cursor(x += xinc[w->orientation], - y += yinc[w->orientation]); - if (!Wrap || x != last || y != bottom) - putchar(flavor[n % sizeof(flavor)]); + mvaddch(y += yinc[w->orientation], + x += xinc[w->orientation], + flavor[n % sizeof(flavor)]); ref[w->ypos[h] = y][w->xpos[h] = x]++; } } } -void -onsig() +static void +onsig(int signo __unused) { - tputs(tgetstr("cl", &tcp), 1, putchar); - tputs(tgetstr("te", &tcp), 1, putchar); - exit(0); + sig_caught = 1; } -nomem() +static void +nomem(void) { - (void)fprintf(stderr, "worms: not enough memory.\n"); - exit(1); + errx(1, "not enough memory."); }