+#if HAVE_USE_WINDOW
+static int
+safe_wgetch(WINDOW *w, void *data GCC_UNUSED)
+{
+ return wgetch(w);
+}
+static int
+safe_wrefresh(WINDOW *w, void *data GCC_UNUSED)
+{
+ return wrefresh(w);
+}
+#endif
+
+#ifdef KEY_RESIZE
+static void
+failed(const char *s)
+{
+ perror(s);
+ stop_curses();
+ ExitProgram(EXIT_FAILURE);
+}
+#endif
+
+static void
+cleanup(void)
+{
+ USING_WINDOW1(stdscr, wrefresh, safe_wrefresh);
+ stop_curses();
+}
+
+static void
+onsig(int sig GCC_UNUSED)
+{
+ cleanup();
+ ExitProgram(EXIT_FAILURE);
+}
+
+static double
+ranf(void)
+{
+ long r = (rand() & 077777);
+ return ((double) r / 32768.);
+}
+
+static int
+draw_worm(WINDOW *win, void *data)
+{
+ WORM *w = (WORM *) data;
+ const struct options *op;
+ unsigned mask = (unsigned) (~(1 << (w - worm)));
+ chtype attrs = w->attrs | ((mask & pending) ? A_REVERSE : 0);
+
+ int x;
+ int y;
+ int h;
+
+ bool done = FALSE;
+
+ if ((x = w->xpos[h = w->head]) < 0) {
+ wmove(win, y = w->ypos[h] = last_y, x = w->xpos[h] = 0);
+ waddch(win, attrs);
+ refs[y][x]++;
+ } else {
+ y = w->ypos[h];
+ }
+
+ if (x > last_x)
+ x = last_x;
+ if (y > last_y)
+ y = last_y;
+
+ if (++h == length)
+ h = 0;
+
+ if (w->xpos[w->head = h] >= 0) {
+ int x1, y1;
+ x1 = w->xpos[h];
+ y1 = w->ypos[h];
+ if (y1 < LINES
+ && x1 < COLS
+ && --refs[y1][x1] == 0) {
+ wmove(win, y1, x1);
+ waddch(win, trail);
+ }
+ }
+
+ op = &(x == 0
+ ? (y == 0
+ ? upleft
+ : (y == last_y
+ ? lowleft
+ : left))
+ : (x == last_x
+ ? (y == 0
+ ? upright
+ : (y == last_y
+ ? lowright
+ : right))
+ : (y == 0
+ ? upper
+ : (y == last_y
+ ? lower
+ : normal))))[w->orientation];
+
+ switch (op->nopts) {
+ case 0:
+ done = TRUE;
+ Trace(("done - draw_worm"));
+ break;
+ case 1:
+ w->orientation = op->opts[0];
+ break;
+ default:
+ w->orientation = op->opts[(int) (ranf() * (double) op->nopts)];
+ break;
+ }
+
+ if (!done) {
+ x += xinc[w->orientation];
+ y += yinc[w->orientation];
+ wmove(win, y, x);
+
+ if (y < 0)
+ y = 0;
+ waddch(win, attrs);
+
+ w->ypos[h] = y;
+ w->xpos[h] = x;
+ refs[y][x]++;
+ }
+
+ return done;
+}
+
+#ifdef USE_PTHREADS
+static bool
+quit_worm(int bitnum)
+{
+ pending = (pending | (unsigned) (1 << bitnum));
+ napms(10); /* let the other thread(s) have a chance */
+ pending = (pending & (unsigned) ~(1 << bitnum));
+ return quitting;
+}
+
+static void *
+start_worm(void *arg)
+{
+ unsigned long compare = 0;
+ Trace(("start_worm"));
+ while (!quit_worm((int) (((struct worm *) arg) - worm))) {
+ while (compare < sequence) {
+ ++compare;
+#if HAVE_USE_WINDOW
+ use_window(stdscr, draw_worm, arg);
+#else
+ draw_worm(stdscr, arg);
+#endif
+ }
+ }
+ Trace(("...start_worm (done)"));
+ return NULL;
+}
+#endif
+
+static bool
+draw_all_worms(void)
+{
+ bool done = FALSE;
+ int n;
+ struct worm *w;
+
+#ifdef USE_PTHREADS
+ static bool first = TRUE;
+ if (first) {
+ first = FALSE;
+ for (n = 0, w = &worm[0]; n < number; n++, w++) {
+ (void) pthread_create(&(w->thread), NULL, start_worm, w);
+ }
+ }
+#else
+ for (n = 0, w = &worm[0]; n < number; n++, w++) {
+ if (
+#if HAVE_USE_WINDOW
+ USING_WINDOW2(stdscr, draw_worm, w)
+#else
+ draw_worm(stdscr, w)
+#endif
+ )
+ done = TRUE;
+ }
+#endif
+ return done;
+}
+
+static int
+get_input(void)
+{
+ int ch;
+ ch = USING_WINDOW1(stdscr, wgetch, safe_wgetch);
+ return ch;
+}
+
+#ifdef KEY_RESIZE
+static int
+update_refs(WINDOW *win, void *data)
+{
+ int x, y;
+
+ (void) win;
+ (void) data;
+ if (last_x != COLS - 1) {
+ for (y = 0; y <= last_y; y++) {
+ refs[y] = typeRealloc(int, (size_t) COLS, refs[y]);
+ if (!refs[y])
+ failed("update_refs");
+ for (x = last_x + 1; x < COLS; x++)
+ refs[y][x] = 0;
+ }
+ last_x = COLS - 1;
+ }
+ if (last_y != LINES - 1) {
+ for (y = LINES; y <= last_y; y++)
+ free(refs[y]);
+ max_refs = LINES;
+ refs = typeRealloc(int *, (size_t) LINES, refs);
+ for (y = last_y + 1; y < LINES; y++) {
+ refs[y] = typeMalloc(int, (size_t) COLS);
+ if (!refs[y])
+ failed("update_refs");
+ for (x = 0; x < COLS; x++)
+ refs[y][x] = 0;
+ }
+ last_y = LINES - 1;
+ }
+ return OK;
+}
+#endif
+
+static void
+usage(void)
+{
+ static const char *msg[] =
+ {
+ "Usage: worm [options]"
+ ,""
+ ,"Options:"
+#if HAVE_USE_DEFAULT_COLORS
+ ," -d invoke use_default_colors"
+#endif
+ ," -f fill screen with copies of \"WORM\" at start"
+ ," -l <n> set length of worms"
+ ," -n <n> set number of worms"
+ ," -t leave trail of \".\""
+#ifdef TRACE
+ ," -T <start>,<end> set trace interval"
+ ," -N suppress cursor-movement optimization"
+#endif
+ };
+ size_t n;
+
+ for (n = 0; n < SIZEOF(msg); n++)
+ fprintf(stderr, "%s\n", msg[n]);
+
+ ExitProgram(EXIT_FAILURE);
+}