+#ifdef USE_PTHREADS
+static bool
+quit_worm(int bitnum)
+{
+ Locked(pending = (pending | (unsigned) (1 << bitnum)));
+
+ napms(10); /* let the other thread(s) have a chance */
+
+ Locked(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))) {
+ for (;;) {
+ bool done = FALSE;
+ Locked(done = (compare >= sequence));
+ if (done)
+ break;
+ ++compare;
+ USING_WINDOW2(stdscr, draw_worm, arg);
+ }
+ }
+ 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 (USING_WINDOW2(stdscr, draw_worm, w))
+ 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(int ok)
+{
+ static const char *msg[] =
+ {
+ "Usage: worm [options]"
+ ,""
+ ,USAGE_COMMON
+ ,"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(ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
+