]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/ncurses.c
ncurses 5.7 - patch 20101002
[ncurses.git] / test / ncurses.c
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /****************************************************************************
29
30 NAME
31    ncurses.c --- ncurses library exerciser
32
33 SYNOPSIS
34    ncurses
35
36 DESCRIPTION
37    An interactive test module for the ncurses library.
38
39 AUTHOR
40    Author: Eric S. Raymond <esr@snark.thyrsus.com> 1993
41            Thomas E. Dickey (beginning revision 1.27 in 1996).
42
43 $Id: ncurses.c,v 1.360 2010/07/24 18:04:58 tom Exp $
44
45 ***************************************************************************/
46
47 #include <test.priv.h>
48
49 #ifdef __hpux
50 #undef mvwdelch                 /* HPUX 11.23 macro will not compile */
51 #endif
52
53 #if HAVE_GETTIMEOFDAY
54 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
55 #include <sys/time.h>
56 #endif
57 #if HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif
60 #endif
61
62 #if USE_LIBPANEL
63 #include <panel.h>
64 #endif
65
66 #if USE_LIBMENU
67 #include <menu.h>
68 #endif
69
70 #if USE_LIBFORM
71 #include <form.h>
72 #endif
73
74 #ifdef NCURSES_VERSION
75
76 #define NCURSES_CONST_PARAM const void
77
78 #ifdef TRACE
79 static unsigned save_trace = TRACE_ORDINARY | TRACE_ICALLS | TRACE_CALLS;
80 extern unsigned _nc_tracing;
81 #endif
82
83 #else
84
85 #define NCURSES_CONST_PARAM char
86
87 #define mmask_t chtype          /* not specified in XSI */
88
89 #ifndef ACS_S3
90 #ifdef CURSES_ACS_ARRAY
91 #define ACS_S3          (CURSES_ACS_ARRAY['p'])         /* scan line 3 */
92 #define ACS_S7          (CURSES_ACS_ARRAY['r'])         /* scan line 7 */
93 #define ACS_LEQUAL      (CURSES_ACS_ARRAY['y'])         /* less/equal */
94 #define ACS_GEQUAL      (CURSES_ACS_ARRAY['z'])         /* greater/equal */
95 #define ACS_PI          (CURSES_ACS_ARRAY['{'])         /* Pi */
96 #define ACS_NEQUAL      (CURSES_ACS_ARRAY['|'])         /* not equal */
97 #define ACS_STERLING    (CURSES_ACS_ARRAY['}'])         /* UK pound sign */
98 #else
99 #define ACS_S3          (A_ALTCHARSET + 'p')    /* scan line 3 */
100 #define ACS_S7          (A_ALTCHARSET + 'r')    /* scan line 7 */
101 #define ACS_LEQUAL      (A_ALTCHARSET + 'y')    /* less/equal */
102 #define ACS_GEQUAL      (A_ALTCHARSET + 'z')    /* greater/equal */
103 #define ACS_PI          (A_ALTCHARSET + '{')    /* Pi */
104 #define ACS_NEQUAL      (A_ALTCHARSET + '|')    /* not equal */
105 #define ACS_STERLING    (A_ALTCHARSET + '}')    /* UK pound sign */
106 #endif
107 #endif /* ACS_S3 */
108
109 #ifdef CURSES_WACS_ARRAY
110 #define WACS_S3         (&(CURSES_WACS_ARRAY['p']))     /* scan line 3 */
111 #define WACS_S7         (&(CURSES_WACS_ARRAY['r']))     /* scan line 7 */
112 #define WACS_LEQUAL     (&(CURSES_WACS_ARRAY['y']))     /* less/equal */
113 #define WACS_GEQUAL     (&(CURSES_WACS_ARRAY['z']))     /* greater/equal */
114 #define WACS_PI         (&(CURSES_WACS_ARRAY['{']))     /* Pi */
115 #define WACS_NEQUAL     (&(CURSES_WACS_ARRAY['|']))     /* not equal */
116 #define WACS_STERLING   (&(CURSES_WACS_ARRAY['}']))     /* UK pound sign */
117 #endif
118
119 #endif
120
121 #if HAVE_WCSRTOMBS
122 #define count_wchars(src, len, state)      wcsrtombs(0,   &src, len, state)
123 #define trans_wchars(dst, src, len, state) wcsrtombs(dst, &src, len, state)
124 #define reset_wchars(state) init_mb(state)
125 #elif HAVE_WCSTOMBS && HAVE_MBTOWC && HAVE_MBLEN
126 #define count_wchars(src, len, state)      wcstombs(0,   src, len)
127 #define trans_wchars(dst, src, len, state) wcstombs(dst, src, len)
128 #define reset_wchars(state) IGNORE_RC(mblen(NULL, 0)), IGNORE_RC(mbtowc(NULL, NULL, 0))
129 #define state_unused
130 #endif
131
132 #if HAVE_MBSRTOWCS
133 #define count_mbytes(src, len, state)      mbsrtowcs(0,   &src, len, state)
134 #define trans_mbytes(dst, src, len, state) mbsrtowcs(dst, &src, len, state)
135 #define reset_mbytes(state) init_mb(state)
136 #elif HAVE_MBSTOWCS && HAVE_MBTOWC && HAVE_MBLEN
137 #define count_mbytes(src, len, state)      mbstowcs(0,   src, len)
138 #define trans_mbytes(dst, src, len, state) mbstowcs(dst, src, len)
139 #define reset_mbytes(state) IGNORE_RC(mblen(NULL, 0)), IGNORE_RC(mbtowc(NULL, NULL, 0))
140 #define state_unused
141 #endif
142
143 #define ToggleAcs(temp,real) temp = ((temp == real) ? 0 : real)
144
145 #define P(string)       printw("%s\n", string)
146
147 #define BLANK           ' '     /* this is the background character */
148
149 #undef max_colors
150 static int max_colors;          /* the actual number of colors we'll use */
151 static int min_colors;          /* the minimum color code */
152 static bool use_colors;         /* true if we use colors */
153
154 #undef max_pairs
155 static int max_pairs;           /* ...and the number of color pairs */
156
157 typedef struct {
158     short red;
159     short green;
160     short blue;
161 } RGB_DATA;
162
163 static RGB_DATA *all_colors;
164
165 static void main_menu(bool);
166
167 /* The behavior of mvhline, mvvline for negative/zero length is unspecified,
168  * though we can rely on negative x/y values to stop the macro.
169  */
170 static void
171 do_h_line(int y, int x, chtype c, int to)
172 {
173     if ((to) > (x))
174         MvHLine(y, x, c, (to) - (x));
175 }
176
177 static void
178 do_v_line(int y, int x, chtype c, int to)
179 {
180     if ((to) > (y))
181         MvVLine(y, x, c, (to) - (y));
182 }
183
184 static void
185 Repaint(void)
186 {
187     touchwin(stdscr);
188     touchwin(curscr);
189     wrefresh(curscr);
190 }
191
192 static bool
193 isQuit(int c)
194 {
195     return ((c) == QUIT || (c) == ESCAPE);
196 }
197 #define case_QUIT       QUIT: case ESCAPE
198
199 /* Common function to allow ^T to toggle trace-mode in the middle of a test
200  * so that trace-files can be made smaller.
201  */
202 static int
203 wGetchar(WINDOW *win)
204 {
205     int c;
206 #ifdef TRACE
207     while ((c = wgetch(win)) == CTRL('T')) {
208         if (_nc_tracing) {
209             save_trace = _nc_tracing;
210             Trace(("TOGGLE-TRACING OFF"));
211             _nc_tracing = 0;
212         } else {
213             _nc_tracing = save_trace;
214         }
215         trace(_nc_tracing);
216         if (_nc_tracing)
217             Trace(("TOGGLE-TRACING ON"));
218     }
219 #else
220     c = wgetch(win);
221 #endif
222     return c;
223 }
224 #define Getchar() wGetchar(stdscr)
225
226 /* replaces wgetnstr(), since we want to be able to edit values */
227 static void
228 wGetstring(WINDOW *win, char *buffer, int limit)
229 {
230     int y0, x0, x, ch;
231     bool done = FALSE;
232
233     echo();
234     getyx(win, y0, x0);
235     (void) wattrset(win, A_REVERSE);
236
237     x = (int) strlen(buffer);
238     while (!done) {
239         if (x > (int) strlen(buffer))
240             x = (int) strlen(buffer);
241         wmove(win, y0, x0);
242         wprintw(win, "%-*s", limit, buffer);
243         wmove(win, y0, x0 + x);
244         switch (ch = wGetchar(win)) {
245         case '\n':
246         case KEY_ENTER:
247             done = TRUE;
248             break;
249         case CTRL('U'):
250             *buffer = '\0';
251             break;
252         case '\b':
253         case KEY_BACKSPACE:
254         case KEY_DC:
255             if (x > 0) {
256                 int j;
257                 for (j = --x; (buffer[j] = buffer[j + 1]) != '\0'; ++j) {
258                     ;
259                 }
260             } else {
261                 beep();
262             }
263             break;
264         case KEY_LEFT:
265             if (x > 0) {
266                 --x;
267             } else {
268                 flash();
269             }
270             break;
271         case KEY_RIGHT:
272             ++x;
273             break;
274         default:
275             if (!isprint(ch) || ch >= KEY_MIN) {
276                 beep();
277             } else if ((int) strlen(buffer) < limit) {
278                 int j;
279                 for (j = (int) strlen(buffer) + 1; j > x; --j) {
280                     buffer[j] = buffer[j - 1];
281                 }
282                 buffer[x++] = (char) ch;
283             } else {
284                 flash();
285             }
286         }
287     }
288
289     wattroff(win, A_REVERSE);
290     wmove(win, y0, x0);
291     noecho();
292 }
293
294 #if USE_WIDEC_SUPPORT
295 static wchar_t
296 fullwidth_of(int ch)
297 {
298     return (ch + 0xff10 - '0');
299 }
300
301 static void
302 make_fullwidth_text(wchar_t *target, const char *source)
303 {
304     int ch;
305     while ((ch = *source++) != 0) {
306         *target++ = fullwidth_of(ch);
307     }
308     *target = 0;
309 }
310
311 static void
312 make_narrow_text(wchar_t *target, const char *source)
313 {
314     int ch;
315     while ((ch = *source++) != 0) {
316         *target++ = ch;
317     }
318     *target = 0;
319 }
320
321 #if USE_LIBPANEL
322 static void
323 make_fullwidth_digit(cchar_t *target, int digit)
324 {
325     wchar_t source[2];
326
327     source[0] = fullwidth_of(digit + '0');
328     source[1] = 0;
329     setcchar(target, source, A_NORMAL, 0, 0);
330 }
331 #endif
332
333 static int
334 wGet_wchar(WINDOW *win, wint_t *result)
335 {
336     int c;
337 #ifdef TRACE
338     while ((c = wget_wch(win, result)) == CTRL('T')) {
339         if (_nc_tracing) {
340             save_trace = _nc_tracing;
341             Trace(("TOGGLE-TRACING OFF"));
342             _nc_tracing = 0;
343         } else {
344             _nc_tracing = save_trace;
345         }
346         trace(_nc_tracing);
347         if (_nc_tracing)
348             Trace(("TOGGLE-TRACING ON"));
349     }
350 #else
351     c = wget_wch(win, result);
352 #endif
353     return c;
354 }
355 #define Get_wchar(result) wGet_wchar(stdscr, result)
356
357 /* replaces wgetn_wstr(), since we want to be able to edit values */
358 static void
359 wGet_wstring(WINDOW *win, wchar_t *buffer, int limit)
360 {
361     int y0, x0, x;
362     wint_t ch;
363     bool done = FALSE;
364     bool fkey = FALSE;
365
366     echo();
367     getyx(win, y0, x0);
368     (void) wattrset(win, A_REVERSE);
369
370     x = (int) wcslen(buffer);
371     while (!done) {
372         if (x > (int) wcslen(buffer))
373             x = (int) wcslen(buffer);
374
375         /* clear the "window' */
376         wmove(win, y0, x0);
377         wprintw(win, "%*s", limit, " ");
378
379         /* write the existing buffer contents */
380         wmove(win, y0, x0);
381         waddnwstr(win, buffer, limit);
382
383         /* positions the cursor past character 'x' */
384         wmove(win, y0, x0);
385         waddnwstr(win, buffer, x);
386
387         switch (wGet_wchar(win, &ch)) {
388         case KEY_CODE_YES:
389             fkey = TRUE;
390             switch (ch) {
391             case KEY_ENTER:
392                 ch = '\n';
393                 fkey = FALSE;
394                 break;
395             case KEY_BACKSPACE:
396             case KEY_DC:
397                 ch = '\b';
398                 fkey = FALSE;
399                 break;
400             case KEY_LEFT:
401             case KEY_RIGHT:
402                 break;
403             default:
404                 ch = (wint_t) -1;
405                 break;
406             }
407             break;
408         case OK:
409             fkey = FALSE;
410             break;
411         default:
412             ch = (wint_t) -1;
413             fkey = TRUE;
414             break;
415         }
416
417         switch (ch) {
418         case '\n':
419             done = TRUE;
420             break;
421         case CTRL('U'):
422             *buffer = '\0';
423             break;
424         case '\b':
425             if (x > 0) {
426                 int j;
427                 for (j = --x; (buffer[j] = buffer[j + 1]) != '\0'; ++j) {
428                     ;
429                 }
430             } else {
431                 beep();
432             }
433             break;
434         case KEY_LEFT:
435             if (x > 0) {
436                 --x;
437             } else {
438                 beep();
439             }
440             break;
441         case KEY_RIGHT:
442             ++x;
443             break;
444         default:
445             if (fkey) {
446                 beep();
447             } else if ((int) wcslen(buffer) < limit) {
448                 int j;
449                 for (j = (int) wcslen(buffer) + 1; j > x; --j) {
450                     buffer[j] = buffer[j - 1];
451                 }
452                 buffer[x++] = (wchar_t) ch;
453             } else {
454                 beep();
455             }
456         }
457     }
458
459     wattroff(win, A_REVERSE);
460     wmove(win, y0, x0);
461     noecho();
462 }
463
464 #endif
465
466 static void
467 Pause(void)
468 {
469     move(LINES - 1, 0);
470     addstr("Press any key to continue... ");
471     (void) Getchar();
472 }
473
474 static void
475 Cannot(const char *what)
476 {
477     printw("\nThis %s terminal %s\n\n", getenv("TERM"), what);
478     Pause();
479 }
480
481 static void
482 ShellOut(bool message)
483 {
484     if (message)
485         addstr("Shelling out...");
486     def_prog_mode();
487     endwin();
488 #ifdef __MINGW32__
489     system("cmd.exe");
490 #else
491     system("sh");
492 #endif
493     if (message)
494         addstr("returned from shellout.\n");
495     refresh();
496 }
497
498 #ifdef NCURSES_MOUSE_VERSION
499 /*
500  * This function is the same as _tracemouse(), but we cannot count on that
501  * being available in the non-debug library.
502  */
503 static const char *
504 mouse_decode(MEVENT const *ep)
505 {
506     static char buf[80 + (5 * 10) + (32 * 15)];
507
508     (void) sprintf(buf, "id %2d at (%2d, %2d, %d) state %4lx = {",
509                    ep->id, ep->x, ep->y, ep->z, (unsigned long) ep->bstate);
510
511 #define SHOW(m, s) if ((ep->bstate & m)==m) {strcat(buf,s); strcat(buf, ", ");}
512
513     SHOW(BUTTON1_RELEASED, "release-1");
514     SHOW(BUTTON1_PRESSED, "press-1");
515     SHOW(BUTTON1_CLICKED, "click-1");
516     SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1");
517     SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1");
518 #if NCURSES_MOUSE_VERSION == 1
519     SHOW(BUTTON1_RESERVED_EVENT, "reserved-1");
520 #endif
521
522     SHOW(BUTTON2_RELEASED, "release-2");
523     SHOW(BUTTON2_PRESSED, "press-2");
524     SHOW(BUTTON2_CLICKED, "click-2");
525     SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2");
526     SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2");
527 #if NCURSES_MOUSE_VERSION == 1
528     SHOW(BUTTON2_RESERVED_EVENT, "reserved-2");
529 #endif
530
531     SHOW(BUTTON3_RELEASED, "release-3");
532     SHOW(BUTTON3_PRESSED, "press-3");
533     SHOW(BUTTON3_CLICKED, "click-3");
534     SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3");
535     SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3");
536 #if NCURSES_MOUSE_VERSION == 1
537     SHOW(BUTTON3_RESERVED_EVENT, "reserved-3");
538 #endif
539
540     SHOW(BUTTON4_RELEASED, "release-4");
541     SHOW(BUTTON4_PRESSED, "press-4");
542     SHOW(BUTTON4_CLICKED, "click-4");
543     SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4");
544     SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4");
545 #if NCURSES_MOUSE_VERSION == 1
546     SHOW(BUTTON4_RESERVED_EVENT, "reserved-4");
547 #endif
548
549 #if NCURSES_MOUSE_VERSION == 2
550     SHOW(BUTTON5_RELEASED, "release-5");
551     SHOW(BUTTON5_PRESSED, "press-5");
552     SHOW(BUTTON5_CLICKED, "click-5");
553     SHOW(BUTTON5_DOUBLE_CLICKED, "doubleclick-5");
554     SHOW(BUTTON5_TRIPLE_CLICKED, "tripleclick-5");
555 #endif
556
557     SHOW(BUTTON_CTRL, "ctrl");
558     SHOW(BUTTON_SHIFT, "shift");
559     SHOW(BUTTON_ALT, "alt");
560     SHOW(ALL_MOUSE_EVENTS, "all-events");
561     SHOW(REPORT_MOUSE_POSITION, "position");
562
563 #undef SHOW
564
565     if (buf[strlen(buf) - 1] == ' ')
566         buf[strlen(buf) - 2] = '\0';
567     (void) strcat(buf, "}");
568     return (buf);
569 }
570
571 static void
572 show_mouse(WINDOW *win)
573 {
574     int y, x;
575     MEVENT event;
576     bool outside;
577     bool show_loc;
578
579     getmouse(&event);
580     outside = !wenclose(win, event.y, event.x);
581
582     if (outside) {
583         (void) wstandout(win);
584         waddstr(win, "KEY_MOUSE");
585         (void) wstandend(win);
586     } else {
587         waddstr(win, "KEY_MOUSE");
588     }
589     wprintw(win, ", %s", mouse_decode(&event));
590
591     if (outside)
592         win = stdscr;
593
594     show_loc = wmouse_trafo(win, &event.y, &event.x, FALSE);
595
596     if (show_loc) {
597         getyx(win, y, x);
598         wmove(win, event.y, event.x);
599         waddch(win, '*');
600         wmove(win, y, x);
601     }
602
603     if (outside)
604         wnoutrefresh(win);
605 }
606 #endif /* NCURSES_MOUSE_VERSION */
607
608 /****************************************************************************
609  *
610  * Character input test
611  *
612  ****************************************************************************/
613
614 #define NUM_GETCH_FLAGS 256
615 typedef bool GetchFlags[NUM_GETCH_FLAGS];
616
617 static void
618 setup_getch(WINDOW *win, GetchFlags flags)
619 {
620     keypad(win, flags['k']);    /* should be redundant, but for testing */
621     meta(win, flags['m']);      /* force this to a known state */
622     if (flags['e'])
623         echo();
624     else
625         noecho();
626 }
627
628 static void
629 init_getch(WINDOW *win, GetchFlags flags)
630 {
631     memset(flags, FALSE, NUM_GETCH_FLAGS);
632     flags[UChar('k')] = (win == stdscr);
633     flags[UChar('m')] = TRUE;
634
635     setup_getch(win, flags);
636 }
637
638 static void
639 wgetch_help(WINDOW *win, GetchFlags flags)
640 {
641     static const char *help[] =
642     {
643         "e  -- toggle echo mode"
644         ,"g  -- triggers a getstr test"
645         ,"k  -- toggle keypad/literal mode"
646         ,"m  -- toggle meta (7-bit/8-bit) mode"
647         ,"^q -- quit"
648         ,"s  -- shell out\n"
649         ,"w  -- create a new window"
650 #ifdef SIGTSTP
651         ,"z  -- suspend this process"
652 #endif
653     };
654     int y, x;
655     unsigned chk = ((SIZEOF(help) + 1) / 2);
656     unsigned n;
657
658     getyx(win, y, x);
659     move(0, 0);
660     printw("Type any key to see its %s value.  Also:\n",
661            flags['k'] ? "keypad" : "literal");
662     for (n = 0; n < SIZEOF(help); ++n) {
663         int row = 1 + (int) (n % chk);
664         int col = (n >= chk) ? COLS / 2 : 0;
665         int flg = ((strstr(help[n], "toggle") != 0)
666                    && (flags[UChar(*help[n])] != FALSE));
667         if (flg)
668             (void) standout();
669         MvPrintw(row, col, "%s", help[n]);
670         if (col == 0)
671             clrtoeol();
672         if (flg)
673             (void) standend();
674     }
675     wrefresh(stdscr);
676     wmove(win, y, x);
677 }
678
679 static void
680 wgetch_wrap(WINDOW *win, int first_y)
681 {
682     int last_y = getmaxy(win) - 1;
683     int y = getcury(win) + 1;
684
685     if (y >= last_y)
686         y = first_y;
687     wmove(win, y, 0);
688     wclrtoeol(win);
689 }
690
691 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
692 typedef struct {
693     WINDOW *text;
694     WINDOW *frame;
695 } WINSTACK;
696
697 static WINSTACK *winstack = 0;
698 static unsigned len_winstack = 0;
699
700 static void
701 forget_boxes(void)
702 {
703     if (winstack != 0) {
704         free(winstack);
705     }
706     winstack = 0;
707     len_winstack = 0;
708 }
709
710 static void
711 remember_boxes(unsigned level, WINDOW *txt_win, WINDOW *box_win)
712 {
713     unsigned need = (level + 1) * 2;
714
715     assert(level < (unsigned) COLS);
716
717     if (winstack == 0) {
718         len_winstack = 20;
719         winstack = typeMalloc(WINSTACK, len_winstack);
720     } else if (need >= len_winstack) {
721         len_winstack = need;
722         winstack = typeRealloc(WINSTACK, len_winstack, winstack);
723     }
724     winstack[level].text = txt_win;
725     winstack[level].frame = box_win;
726 }
727
728 #if USE_SOFTKEYS && (NCURSES_VERSION_PATCH < 20071229) && NCURSES_EXT_FUNCS
729 static void
730 slk_repaint(void)
731 {
732     /* this chunk is now done in resize_term() */
733     slk_touch();
734     slk_clear();
735     slk_noutrefresh();
736 }
737
738 #else
739 #define slk_repaint()           /* nothing */
740 #endif
741
742 /*
743  * For wgetch_test(), we create pairs of windows - one for a box, one for text.
744  * Resize both and paint the box in the parent.
745  */
746 static void
747 resize_boxes(unsigned level, WINDOW *win)
748 {
749     unsigned n;
750     int base = 5;
751     int high = LINES - base;
752     int wide = COLS;
753
754     touchwin(stdscr);
755     wnoutrefresh(stdscr);
756
757     slk_repaint();
758
759     for (n = 0; n < level; ++n) {
760         wresize(winstack[n].frame, high, wide);
761         wresize(winstack[n].text, high - 2, wide - 2);
762         high -= 2;
763         wide -= 2;
764         werase(winstack[n].text);
765         box(winstack[n].frame, 0, 0);
766         wnoutrefresh(winstack[n].frame);
767         wprintw(winstack[n].text,
768                 "size %dx%d\n",
769                 getmaxy(winstack[n].text),
770                 getmaxx(winstack[n].text));
771         wnoutrefresh(winstack[n].text);
772         if (winstack[n].text == win)
773             break;
774     }
775     doupdate();
776 }
777 #else
778 #define forget_boxes()          /* nothing */
779 #define remember_boxes(level,text,frame)        /* nothing */
780 #endif
781
782 static void
783 wgetch_test(unsigned level, WINDOW *win, int delay)
784 {
785     char buf[BUFSIZ];
786     int first_y, first_x;
787     int c;
788     int incount = 0;
789     GetchFlags flags;
790     bool blocking = (delay < 0);
791
792     init_getch(win, flags);
793     wtimeout(win, delay);
794     getyx(win, first_y, first_x);
795
796     wgetch_help(win, flags);
797     wsetscrreg(win, first_y, getmaxy(win) - 1);
798     scrollok(win, TRUE);
799
800     for (;;) {
801         while ((c = wGetchar(win)) == ERR) {
802             incount++;
803             if (blocking) {
804                 (void) wprintw(win, "%05d: input error", incount);
805                 break;
806             } else {
807                 (void) wprintw(win, "%05d: input timed out", incount);
808             }
809             wgetch_wrap(win, first_y);
810         }
811         if (c == ERR && blocking) {
812             wprintw(win, "ERR");
813             wgetch_wrap(win, first_y);
814         } else if (isQuit(c)) {
815             break;
816         } else if (c == 'e') {
817             flags[UChar('e')] = !flags[UChar('e')];
818             setup_getch(win, flags);
819             wgetch_help(win, flags);
820         } else if (c == 'g') {
821             waddstr(win, "getstr test: ");
822             echo();
823             wgetnstr(win, buf, sizeof(buf) - 1);
824             noecho();
825             wprintw(win, "I saw %d characters:\n\t`%s'.", (int) strlen(buf), buf);
826             wclrtoeol(win);
827             wgetch_wrap(win, first_y);
828         } else if (c == 'k') {
829             flags[UChar('k')] = !flags[UChar('k')];
830             setup_getch(win, flags);
831             wgetch_help(win, flags);
832         } else if (c == 'm') {
833             flags[UChar('m')] = !flags[UChar('m')];
834             setup_getch(win, flags);
835             wgetch_help(win, flags);
836         } else if (c == 's') {
837             ShellOut(TRUE);
838         } else if (c == 'w') {
839             int high = getmaxy(win) - 1 - first_y + 1;
840             int wide = getmaxx(win) - first_x;
841             int old_y, old_x;
842             int new_y = first_y + getbegy(win);
843             int new_x = first_x + getbegx(win);
844
845             getyx(win, old_y, old_x);
846             if (high > 2 && wide > 2) {
847                 WINDOW *wb = newwin(high, wide, new_y, new_x);
848                 WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1);
849
850                 box(wb, 0, 0);
851                 wrefresh(wb);
852                 wmove(wi, 0, 0);
853                 remember_boxes(level, wi, wb);
854                 wgetch_test(level + 1, wi, delay);
855                 delwin(wi);
856                 delwin(wb);
857
858                 wgetch_help(win, flags);
859                 wmove(win, old_y, old_x);
860                 touchwin(win);
861                 wrefresh(win);
862                 doupdate();
863             }
864 #ifdef SIGTSTP
865         } else if (c == 'z') {
866             kill(getpid(), SIGTSTP);
867 #endif
868         } else {
869             wprintw(win, "Key pressed: %04o ", c);
870 #ifdef NCURSES_MOUSE_VERSION
871             if (c == KEY_MOUSE) {
872                 show_mouse(win);
873             } else
874 #endif /* NCURSES_MOUSE_VERSION */
875             if (c >= KEY_MIN) {
876 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
877                 if (c == KEY_RESIZE) {
878                     resize_boxes(level, win);
879                 }
880 #endif
881                 (void) waddstr(win, keyname(c));
882             } else if (c >= 0x80) {
883                 unsigned c2 = (unsigned) c;
884 #if !(defined(NCURSES_VERSION) || defined(_XOPEN_CURSES))
885                 /* at least Solaris SVR4 curses breaks unctrl(128), etc. */
886                 c2 &= 0x7f;
887 #endif
888                 if (isprint(c))
889                     (void) wprintw(win, "%c", UChar(c));
890                 else if (c2 != UChar(c))
891                     (void) wprintw(win, "M-%s", unctrl(c2));
892                 else
893                     (void) wprintw(win, "%s", unctrl(c2));
894                 waddstr(win, " (high-half character)");
895             } else {
896                 if (isprint(c))
897                     (void) wprintw(win, "%c (ASCII printable character)", c);
898                 else
899                     (void) wprintw(win, "%s (ASCII control character)",
900                                    unctrl(UChar(c)));
901             }
902             wgetch_wrap(win, first_y);
903         }
904     }
905
906     wtimeout(win, -1);
907
908     if (!level)
909         init_getch(win, flags);
910 }
911
912 static int
913 begin_getch_test(void)
914 {
915     char buf[BUFSIZ];
916     int delay;
917
918     refresh();
919
920 #ifdef NCURSES_MOUSE_VERSION
921     mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
922 #endif
923
924     (void) printw("Delay in 10ths of a second (<CR> for blocking input)? ");
925     echo();
926     getnstr(buf, sizeof(buf) - 1);
927     noecho();
928     nonl();
929
930     if (isdigit(UChar(buf[0]))) {
931         delay = atoi(buf) * 100;
932     } else {
933         delay = -1;
934     }
935     raw();
936     move(5, 0);
937     return delay;
938 }
939
940 static void
941 finish_getch_test(void)
942 {
943 #ifdef NCURSES_MOUSE_VERSION
944     mousemask(0, (mmask_t *) 0);
945 #endif
946     erase();
947     noraw();
948     nl();
949     endwin();
950 }
951
952 static void
953 getch_test(void)
954 {
955     int delay = begin_getch_test();
956
957     slk_restore();
958     wgetch_test(0, stdscr, delay);
959     forget_boxes();
960     finish_getch_test();
961     slk_clear();
962 }
963
964 #if USE_WIDEC_SUPPORT
965 /*
966  * For wget_wch_test(), we create pairs of windows - one for a box, one for text.
967  * Resize both and paint the box in the parent.
968  */
969 #if defined(KEY_RESIZE) && HAVE_WRESIZE
970 static void
971 resize_wide_boxes(unsigned level, WINDOW *win)
972 {
973     unsigned n;
974     int base = 5;
975     int high = LINES - base;
976     int wide = COLS;
977
978     touchwin(stdscr);
979     wnoutrefresh(stdscr);
980
981     slk_repaint();
982
983     for (n = 0; n < level; ++n) {
984         wresize(winstack[n].frame, high, wide);
985         wresize(winstack[n].text, high - 2, wide - 2);
986         high -= 2;
987         wide -= 2;
988         werase(winstack[n].text);
989         box_set(winstack[n].frame, 0, 0);
990         wnoutrefresh(winstack[n].frame);
991         wprintw(winstack[n].text,
992                 "size %dx%d\n",
993                 getmaxy(winstack[n].text),
994                 getmaxx(winstack[n].text));
995         wnoutrefresh(winstack[n].text);
996         if (winstack[n].text == win)
997             break;
998     }
999     doupdate();
1000 }
1001 #endif /* KEY_RESIZE */
1002
1003 static char *
1004 wcstos(const wchar_t *src)
1005 {
1006     int need;
1007     char *result = 0;
1008     const wchar_t *tmp = src;
1009 #ifndef state_unused
1010     mbstate_t state;
1011 #endif
1012
1013     reset_wchars(state);
1014     if ((need = (int) count_wchars(tmp, 0, &state)) > 0) {
1015         unsigned have = (unsigned) need;
1016         if ((result = typeCalloc(char, have + 1)) != 0) {
1017             tmp = src;
1018             if (trans_wchars(result, tmp, have, &state) != have) {
1019                 free(result);
1020                 result = 0;
1021             }
1022         }
1023     }
1024     return result;
1025 }
1026
1027 static void
1028 wget_wch_test(unsigned level, WINDOW *win, int delay)
1029 {
1030     wchar_t wchar_buf[BUFSIZ];
1031     wint_t wint_buf[BUFSIZ];
1032     int first_y, first_x;
1033     wint_t c;
1034     int incount = 0;
1035     GetchFlags flags;
1036     bool blocking = (delay < 0);
1037     int code;
1038     char *temp;
1039
1040     init_getch(win, flags);
1041     wtimeout(win, delay);
1042     getyx(win, first_y, first_x);
1043
1044     wgetch_help(win, flags);
1045     wsetscrreg(win, first_y, getmaxy(win) - 1);
1046     scrollok(win, TRUE);
1047
1048     for (;;) {
1049         while ((code = wGet_wchar(win, &c)) == ERR) {
1050             incount++;
1051             if (blocking) {
1052                 (void) wprintw(win, "%05d: input error", incount);
1053                 break;
1054             } else {
1055                 (void) wprintw(win, "%05d: input timed out", incount);
1056             }
1057             wgetch_wrap(win, first_y);
1058         }
1059         if (code == ERR && blocking) {
1060             wprintw(win, "ERR");
1061             wgetch_wrap(win, first_y);
1062         } else if (isQuit((int) c)) {
1063             break;
1064         } else if (c == 'e') {
1065             flags[UChar('e')] = !flags[UChar('e')];
1066             setup_getch(win, flags);
1067             wgetch_help(win, flags);
1068         } else if (c == 'g') {
1069             waddstr(win, "getstr test: ");
1070             echo();
1071             code = wgetn_wstr(win, wint_buf, sizeof(wint_buf) - 1);
1072             noecho();
1073             if (code == ERR) {
1074                 wprintw(win, "wgetn_wstr returns an error.");
1075             } else {
1076                 int n;
1077                 for (n = 0; (wchar_buf[n] = (wchar_t) wint_buf[n]) != 0; ++n) {
1078                     ;
1079                 }
1080                 if ((temp = wcstos(wchar_buf)) != 0) {
1081                     wprintw(win, "I saw %d characters:\n\t`%s'.",
1082                             (int) wcslen(wchar_buf), temp);
1083                     free(temp);
1084                 } else {
1085                     wprintw(win, "I saw %d characters (cannot convert).",
1086                             (int) wcslen(wchar_buf));
1087                 }
1088             }
1089             wclrtoeol(win);
1090             wgetch_wrap(win, first_y);
1091         } else if (c == 'k') {
1092             flags[UChar('k')] = !flags[UChar('k')];
1093             setup_getch(win, flags);
1094             wgetch_help(win, flags);
1095         } else if (c == 'm') {
1096             flags[UChar('m')] = !flags[UChar('m')];
1097             setup_getch(win, flags);
1098             wgetch_help(win, flags);
1099         } else if (c == 's') {
1100             ShellOut(TRUE);
1101         } else if (c == 'w') {
1102             int high = getmaxy(win) - 1 - first_y + 1;
1103             int wide = getmaxx(win) - first_x;
1104             int old_y, old_x;
1105             int new_y = first_y + getbegy(win);
1106             int new_x = first_x + getbegx(win);
1107
1108             getyx(win, old_y, old_x);
1109             if (high > 2 && wide > 2) {
1110                 WINDOW *wb = newwin(high, wide, new_y, new_x);
1111                 WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1);
1112
1113                 box_set(wb, 0, 0);
1114                 wrefresh(wb);
1115                 wmove(wi, 0, 0);
1116                 remember_boxes(level, wi, wb);
1117                 wget_wch_test(level + 1, wi, delay);
1118                 delwin(wi);
1119                 delwin(wb);
1120
1121                 wgetch_help(win, flags);
1122                 wmove(win, old_y, old_x);
1123                 touchwin(win);
1124                 wrefresh(win);
1125             }
1126 #ifdef SIGTSTP
1127         } else if (c == 'z') {
1128             kill(getpid(), SIGTSTP);
1129 #endif
1130         } else {
1131             wprintw(win, "Key pressed: %04o ", (int) c);
1132 #ifdef NCURSES_MOUSE_VERSION
1133             if (c == KEY_MOUSE) {
1134                 show_mouse(win);
1135             } else
1136 #endif /* NCURSES_MOUSE_VERSION */
1137             if (code == KEY_CODE_YES) {
1138 #if defined(KEY_RESIZE) && HAVE_WRESIZE
1139                 if (c == KEY_RESIZE) {
1140                     resize_wide_boxes(level, win);
1141                 }
1142 #endif
1143                 (void) waddstr(win, keyname((wchar_t) c));
1144             } else {
1145                 (void) waddstr(win, key_name((wchar_t) c));
1146                 if (c < 256 && iscntrl(c)) {
1147                     (void) wprintw(win, " (control character)");
1148                 } else {
1149                     (void) wprintw(win, " = %#x (printable character)",
1150                                    (unsigned) c);
1151                 }
1152             }
1153             wgetch_wrap(win, first_y);
1154         }
1155     }
1156
1157     wtimeout(win, -1);
1158
1159     if (!level)
1160         init_getch(win, flags);
1161 }
1162
1163 static void
1164 get_wch_test(void)
1165 {
1166     int delay = begin_getch_test();
1167
1168     slk_restore();
1169     wget_wch_test(0, stdscr, delay);
1170     forget_boxes();
1171     finish_getch_test();
1172     slk_clear();
1173 }
1174 #endif
1175
1176 /****************************************************************************
1177  *
1178  * Character attributes test
1179  *
1180  ****************************************************************************/
1181
1182 #if HAVE_SETUPTERM || HAVE_TGETENT
1183 #define get_ncv() TIGETNUM("ncv","NC")
1184 #define get_xmc() TIGETNUM("xmc","sg")
1185 #else
1186 #define get_ncv() -1
1187 #define get_xmc() -1
1188 #endif
1189
1190 #if !HAVE_TERMATTRS
1191 static chtype
1192 my_termattrs(void)
1193 {
1194     static int first = TRUE;
1195     static chtype result = 0;
1196
1197     if (first) {
1198 #if !HAVE_TIGETSTR
1199         char buffer[4096];
1200         char parsed[4096];
1201         char *area_pointer = parsed;
1202
1203         tgetent(buffer, getenv("TERM"));
1204 #endif
1205
1206         if (TIGETSTR("smso", "so"))
1207             result |= A_STANDOUT;
1208         if (TIGETSTR("smul", "us"))
1209             result |= A_UNDERLINE;
1210         if (TIGETSTR("rev", "mr"))
1211             result |= A_REVERSE;
1212         if (TIGETSTR("blink", "mb"))
1213             result |= A_BLINK;
1214         if (TIGETSTR("dim", "mh"))
1215             result |= A_DIM;
1216         if (TIGETSTR("bold", "md"))
1217             result |= A_BOLD;
1218         if (TIGETSTR("smacs", "ac"))
1219             result |= A_ALTCHARSET;
1220
1221         first = FALSE;
1222     }
1223     return result;
1224 }
1225 #define termattrs() my_termattrs()
1226 #endif
1227
1228 #define MAX_ATTRSTRING 31
1229 #define LEN_ATTRSTRING 26
1230
1231 static char attr_test_string[MAX_ATTRSTRING + 1];
1232
1233 static void
1234 attr_legend(WINDOW *helpwin)
1235 {
1236     int row = 1;
1237     int col = 1;
1238
1239     MvWPrintw(helpwin, row++, col,
1240               "ESC to exit.");
1241     MvWPrintw(helpwin, row++, col,
1242               "^L repaints.");
1243     ++row;
1244     MvWPrintw(helpwin, row++, col,
1245               "Modify the test strings:");
1246     MvWPrintw(helpwin, row++, col,
1247               "  A digit sets gaps on each side of displayed attributes");
1248     MvWPrintw(helpwin, row++, col,
1249               "  </> shifts the text left/right. ");
1250     ++row;
1251     MvWPrintw(helpwin, row++, col,
1252               "Toggles:");
1253     if (use_colors) {
1254         MvWPrintw(helpwin, row++, col,
1255                   "  f/F/b/F toggle foreground/background background color");
1256         MvWPrintw(helpwin, row++, col,
1257                   "  t/T     toggle text/background color attribute");
1258     }
1259     MvWPrintw(helpwin, row++, col,
1260               "  a/A     toggle ACS (alternate character set) mapping");
1261     MvWPrintw(helpwin, row, col,
1262               "  v/V     toggle video attribute to combine with each line");
1263 }
1264
1265 static void
1266 show_color_attr(int fg, int bg, int tx)
1267 {
1268     if (use_colors) {
1269         printw("  Colors (fg %d, bg %d", fg, bg);
1270         if (tx >= 0)
1271             printw(", text %d", tx);
1272         printw("),");
1273     }
1274 }
1275
1276 static bool
1277 cycle_color_attr(int ch, short *fg, short *bg, short *tx)
1278 {
1279     bool error = FALSE;
1280
1281     if (use_colors) {
1282         switch (ch) {
1283         case 'f':
1284             *fg = (short) (*fg + 1);
1285             break;
1286         case 'F':
1287             *fg = (short) (*fg - 1);
1288             break;
1289         case 'b':
1290             *bg = (short) (*bg + 1);
1291             break;
1292         case 'B':
1293             *bg = (short) (*bg - 1);
1294             break;
1295         case 't':
1296             *tx = (short) (*tx + 1);
1297             break;
1298         case 'T':
1299             *tx = (short) (*tx - 1);
1300             break;
1301         default:
1302             beep();
1303             error = TRUE;
1304             break;
1305         }
1306         if (*fg >= COLORS)
1307             *fg = (short) min_colors;
1308         if (*fg < min_colors)
1309             *fg = (short) (COLORS - 1);
1310         if (*bg >= COLORS)
1311             *bg = (short) min_colors;
1312         if (*bg < min_colors)
1313             *bg = (short) (COLORS - 1);
1314         if (*tx >= COLORS)
1315             *tx = -1;
1316         if (*tx < -1)
1317             *tx = (short) (COLORS - 1);
1318     } else {
1319         beep();
1320         error = TRUE;
1321     }
1322     return error;
1323 }
1324
1325 static void
1326 adjust_attr_string(int adjust)
1327 {
1328     int first = ((int) UChar(attr_test_string[0])) + adjust;
1329     int last = first + LEN_ATTRSTRING;
1330
1331     if (first >= ' ' && last <= '~') {  /* 32..126 */
1332         int j, k;
1333         for (j = 0, k = first; j < MAX_ATTRSTRING && k <= last; ++j, ++k) {
1334             attr_test_string[j] = (char) k;
1335             if (((k + 1 - first) % 5) == 0) {
1336                 if (++j >= MAX_ATTRSTRING)
1337                     break;
1338                 attr_test_string[j] = ' ';
1339             }
1340         }
1341         while (j < MAX_ATTRSTRING)
1342             attr_test_string[j++] = ' ';
1343         attr_test_string[j] = '\0';
1344     } else {
1345         beep();
1346     }
1347 }
1348
1349 static void
1350 init_attr_string(void)
1351 {
1352     attr_test_string[0] = 'a';
1353     adjust_attr_string(0);
1354 }
1355
1356 static int
1357 show_attr(int row, int skip, bool arrow, chtype attr, const char *name)
1358 {
1359     int ncv = get_ncv();
1360     chtype test = attr & (chtype) (~A_ALTCHARSET);
1361
1362     if (arrow)
1363         MvPrintw(row, 5, "-->");
1364     MvPrintw(row, 8, "%s mode:", name);
1365     MvPrintw(row, 24, "|");
1366     if (skip)
1367         printw("%*s", skip, " ");
1368     /*
1369      * Just for testing, write text using the alternate character set one
1370      * character at a time (to pass its rendition directly), and use the
1371      * string operation for the other attributes.
1372      */
1373     if (attr & A_ALTCHARSET) {
1374         const char *s;
1375         chtype ch;
1376
1377         for (s = attr_test_string; *s != '\0'; ++s) {
1378             ch = UChar(*s);
1379             addch(ch | attr);
1380         }
1381     } else {
1382         (void) attrset(attr);
1383         addstr(attr_test_string);
1384         attroff(attr);
1385     }
1386     if (skip)
1387         printw("%*s", skip, " ");
1388     printw("|");
1389     if (test != A_NORMAL) {
1390         if (!(termattrs() & test)) {
1391             printw(" (N/A)");
1392         } else {
1393             if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) {
1394                 static const chtype table[] =
1395                 {
1396                     A_STANDOUT,
1397                     A_UNDERLINE,
1398                     A_REVERSE,
1399                     A_BLINK,
1400                     A_DIM,
1401                     A_BOLD,
1402 #ifdef A_INVIS
1403                     A_INVIS,
1404 #endif
1405                     A_PROTECT,
1406                     A_ALTCHARSET
1407                 };
1408                 unsigned n;
1409                 bool found = FALSE;
1410                 for (n = 0; n < SIZEOF(table); n++) {
1411                     if ((table[n] & attr) != 0
1412                         && ((1 << n) & ncv) != 0) {
1413                         found = TRUE;
1414                         break;
1415                     }
1416                 }
1417                 if (found)
1418                     printw(" (NCV)");
1419             }
1420             if ((termattrs() & test) != test)
1421                 printw(" (Part)");
1422         }
1423     }
1424     return row + 2;
1425 }
1426 /* *INDENT-OFF* */
1427 static const struct {
1428     chtype                      attr;
1429     NCURSES_CONST char *        name;
1430 } attrs_to_test[] = {
1431     { A_STANDOUT,       "STANDOUT" },
1432     { A_REVERSE,        "REVERSE" },
1433     { A_BOLD,           "BOLD" },
1434     { A_UNDERLINE,      "UNDERLINE" },
1435     { A_DIM,            "DIM" },
1436     { A_BLINK,          "BLINK" },
1437     { A_PROTECT,        "PROTECT" },
1438 #ifdef A_INVIS
1439     { A_INVIS,          "INVISIBLE" },
1440 #endif
1441     { A_NORMAL,         "NORMAL" },
1442 };
1443 /* *INDENT-ON* */
1444
1445 static bool
1446 attr_getc(int *skip, short *fg, short *bg, short *tx, int *ac, unsigned *kc)
1447 {
1448     bool result = TRUE;
1449     bool error = FALSE;
1450     WINDOW *helpwin;
1451
1452     do {
1453         int ch = Getchar();
1454
1455         error = FALSE;
1456         if (ch < 256 && isdigit(ch)) {
1457             *skip = (ch - '0');
1458         } else {
1459             switch (ch) {
1460             case CTRL('L'):
1461                 Repaint();
1462                 break;
1463             case '?':
1464                 if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
1465                     box(helpwin, 0, 0);
1466                     attr_legend(helpwin);
1467                     wGetchar(helpwin);
1468                     delwin(helpwin);
1469                 }
1470                 break;
1471             case 'a':
1472                 *ac = 0;
1473                 break;
1474             case 'A':
1475                 *ac = A_ALTCHARSET;
1476                 break;
1477             case 'v':
1478                 if (*kc == 0)
1479                     *kc = SIZEOF(attrs_to_test) - 1;
1480                 else
1481                     *kc -= 1;
1482                 break;
1483             case 'V':
1484                 *kc += 1;
1485                 if (*kc >= SIZEOF(attrs_to_test))
1486                     *kc = 0;
1487                 break;
1488             case '<':
1489                 adjust_attr_string(-1);
1490                 break;
1491             case '>':
1492                 adjust_attr_string(1);
1493                 break;
1494             case case_QUIT:
1495                 result = FALSE;
1496                 break;
1497             default:
1498                 error = cycle_color_attr(ch, fg, bg, tx);
1499                 break;
1500             }
1501         }
1502     } while (error);
1503     return result;
1504 }
1505
1506 static void
1507 attr_test(void)
1508 /* test text attributes */
1509 {
1510     int n;
1511     int skip = get_xmc();
1512     short fg = COLOR_BLACK;     /* color pair 0 is special */
1513     short bg = COLOR_BLACK;
1514     short tx = -1;
1515     int ac = 0;
1516     unsigned j, k;
1517
1518     if (skip < 0)
1519         skip = 0;
1520
1521     n = skip;                   /* make it easy */
1522     k = SIZEOF(attrs_to_test) - 1;
1523     init_attr_string();
1524
1525     do {
1526         int row = 2;
1527         chtype normal = A_NORMAL | BLANK;
1528         chtype extras = (chtype) ac;
1529
1530         if (use_colors) {
1531             short pair = (short) (fg != COLOR_BLACK || bg != COLOR_BLACK);
1532             if (pair != 0) {
1533                 pair = 1;
1534                 if (init_pair(pair, fg, bg) == ERR) {
1535                     beep();
1536                 } else {
1537                     normal |= COLOR_PAIR(pair);
1538                 }
1539             }
1540             if (tx >= 0) {
1541                 pair = 2;
1542                 if (init_pair(pair, tx, bg) == ERR) {
1543                     beep();
1544                 } else {
1545                     extras |= COLOR_PAIR(pair);
1546                 }
1547             }
1548         }
1549         bkgd(normal);
1550         bkgdset(normal);
1551         erase();
1552
1553         box(stdscr, 0, 0);
1554         MvAddStr(0, 20, "Character attribute test display");
1555
1556         for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
1557             bool arrow = (j == k);
1558             row = show_attr(row, n, arrow,
1559                             extras |
1560                             attrs_to_test[j].attr |
1561                             attrs_to_test[k].attr,
1562                             attrs_to_test[j].name);
1563         }
1564
1565         MvPrintw(row, 8,
1566                  "This terminal does %shave the magic-cookie glitch",
1567                  get_xmc() > -1 ? "" : "not ");
1568         MvPrintw(row + 1, 8, "Enter '?' for help.");
1569         show_color_attr(fg, bg, tx);
1570         printw("  ACS (%d)", ac != 0);
1571
1572         refresh();
1573     } while (attr_getc(&n, &fg, &bg, &tx, &ac, &k));
1574
1575     bkgdset(A_NORMAL | BLANK);
1576     erase();
1577     endwin();
1578 }
1579
1580 #if USE_WIDEC_SUPPORT
1581 static wchar_t wide_attr_test_string[MAX_ATTRSTRING + 1];
1582
1583 static void
1584 wide_adjust_attr_string(int adjust)
1585 {
1586     int first = ((int) UChar(wide_attr_test_string[0])) + adjust;
1587     int last = first + LEN_ATTRSTRING;
1588
1589     if (first >= ' ' && last <= '~') {  /* 32..126 */
1590         int j, k;
1591         for (j = 0, k = first; j < MAX_ATTRSTRING && k <= last; ++j, ++k) {
1592             wide_attr_test_string[j] = k;
1593             if (((k + 1 - first) % 5) == 0) {
1594                 if (++j >= MAX_ATTRSTRING)
1595                     break;
1596                 wide_attr_test_string[j] = ' ';
1597             }
1598         }
1599         while (j < MAX_ATTRSTRING)
1600             wide_attr_test_string[j++] = ' ';
1601         wide_attr_test_string[j] = '\0';
1602     } else {
1603         beep();
1604     }
1605 }
1606
1607 static void
1608 wide_init_attr_string(void)
1609 {
1610     wide_attr_test_string[0] = 'a';
1611     wide_adjust_attr_string(0);
1612 }
1613
1614 static void
1615 set_wide_background(short pair)
1616 {
1617     cchar_t normal;
1618     wchar_t blank[2];
1619
1620     blank[0] = ' ';
1621     blank[1] = 0;
1622     setcchar(&normal, blank, A_NORMAL, pair, 0);
1623     bkgrnd(&normal);
1624     bkgrndset(&normal);
1625 }
1626
1627 static attr_t
1628 get_wide_background(void)
1629 {
1630     attr_t result = A_NORMAL;
1631     attr_t attr;
1632     cchar_t ch;
1633     short pair;
1634     wchar_t wch[10];
1635
1636     if (getbkgrnd(&ch) != ERR) {
1637         if (getcchar(&ch, wch, &attr, &pair, 0) != ERR) {
1638             result = attr;
1639         }
1640     }
1641     return result;
1642 }
1643
1644 static int
1645 wide_show_attr(int row, int skip, bool arrow, chtype attr, short pair, const char *name)
1646 {
1647     int ncv = get_ncv();
1648     chtype test = attr & ~WA_ALTCHARSET;
1649
1650     if (arrow)
1651         MvPrintw(row, 5, "-->");
1652     MvPrintw(row, 8, "%s mode:", name);
1653     MvPrintw(row, 24, "|");
1654     if (skip)
1655         printw("%*s", skip, " ");
1656
1657     /*
1658      * Just for testing, write text using the alternate character set one
1659      * character at a time (to pass its rendition directly), and use the
1660      * string operation for the other attributes.
1661      */
1662     if (attr & WA_ALTCHARSET) {
1663         const wchar_t *s;
1664         cchar_t ch;
1665
1666         for (s = wide_attr_test_string; *s != L'\0'; ++s) {
1667             wchar_t fill[2];
1668             fill[0] = *s;
1669             fill[1] = L'\0';
1670             setcchar(&ch, fill, attr, pair, 0);
1671             add_wch(&ch);
1672         }
1673     } else {
1674         attr_t old_attr;
1675         short old_pair;
1676
1677         (void) attr_get(&old_attr, &old_pair, 0);
1678         (void) attr_set(attr, pair, 0);
1679         addwstr(wide_attr_test_string);
1680         (void) attr_set(old_attr, old_pair, 0);
1681     }
1682     if (skip)
1683         printw("%*s", skip, " ");
1684     printw("|");
1685     if (test != A_NORMAL) {
1686         if (!(term_attrs() & test)) {
1687             printw(" (N/A)");
1688         } else {
1689             if (ncv > 0 && (get_wide_background() & A_COLOR)) {
1690                 static const attr_t table[] =
1691                 {
1692                     WA_STANDOUT,
1693                     WA_UNDERLINE,
1694                     WA_REVERSE,
1695                     WA_BLINK,
1696                     WA_DIM,
1697                     WA_BOLD,
1698                     WA_INVIS,
1699                     WA_PROTECT,
1700                     WA_ALTCHARSET
1701                 };
1702                 unsigned n;
1703                 bool found = FALSE;
1704                 for (n = 0; n < SIZEOF(table); n++) {
1705                     if ((table[n] & attr) != 0
1706                         && ((1 << n) & ncv) != 0) {
1707                         found = TRUE;
1708                         break;
1709                     }
1710                 }
1711                 if (found)
1712                     printw(" (NCV)");
1713             }
1714             if ((term_attrs() & test) != test)
1715                 printw(" (Part)");
1716         }
1717     }
1718     return row + 2;
1719 }
1720
1721 static bool
1722 wide_attr_getc(int *skip, short *fg, short *bg, short *tx, int *ac, unsigned *kc)
1723 {
1724     bool result = TRUE;
1725     bool error = FALSE;
1726     WINDOW *helpwin;
1727
1728     do {
1729         int ch = Getchar();
1730
1731         error = FALSE;
1732         if (ch < 256 && isdigit(ch)) {
1733             *skip = (ch - '0');
1734         } else {
1735             switch (ch) {
1736             case CTRL('L'):
1737                 Repaint();
1738                 break;
1739             case '?':
1740                 if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
1741                     box_set(helpwin, 0, 0);
1742                     attr_legend(helpwin);
1743                     wGetchar(helpwin);
1744                     delwin(helpwin);
1745                 }
1746                 break;
1747             case 'a':
1748                 *ac = 0;
1749                 break;
1750             case 'A':
1751                 *ac = A_ALTCHARSET;
1752                 break;
1753             case 'v':
1754                 if (*kc == 0)
1755                     *kc = SIZEOF(attrs_to_test) - 1;
1756                 else
1757                     *kc -= 1;
1758                 break;
1759             case 'V':
1760                 *kc += 1;
1761                 if (*kc >= SIZEOF(attrs_to_test))
1762                     *kc = 0;
1763                 break;
1764             case '<':
1765                 wide_adjust_attr_string(-1);
1766                 break;
1767             case '>':
1768                 wide_adjust_attr_string(1);
1769                 break;
1770             case case_QUIT:
1771                 result = FALSE;
1772                 break;
1773             default:
1774                 error = cycle_color_attr(ch, fg, bg, tx);
1775                 break;
1776             }
1777         }
1778     } while (error);
1779     return result;
1780 }
1781
1782 static void
1783 wide_attr_test(void)
1784 /* test text attributes using wide-character calls */
1785 {
1786     int n;
1787     int skip = get_xmc();
1788     short fg = COLOR_BLACK;     /* color pair 0 is special */
1789     short bg = COLOR_BLACK;
1790     short tx = -1;
1791     int ac = 0;
1792     unsigned j, k;
1793
1794     if (skip < 0)
1795         skip = 0;
1796
1797     n = skip;                   /* make it easy */
1798     k = SIZEOF(attrs_to_test) - 1;
1799     wide_init_attr_string();
1800
1801     do {
1802         int row = 2;
1803         short pair = 0;
1804         short extras = 0;
1805
1806         if (use_colors) {
1807             pair = (short) (fg != COLOR_BLACK || bg != COLOR_BLACK);
1808             if (pair != 0) {
1809                 pair = 1;
1810                 if (init_pair(pair, fg, bg) == ERR) {
1811                     beep();
1812                 }
1813             }
1814             extras = pair;
1815             if (tx >= 0) {
1816                 extras = 2;
1817                 if (init_pair(extras, tx, bg) == ERR) {
1818                     beep();
1819                 }
1820             }
1821         }
1822         set_wide_background(pair);
1823         erase();
1824
1825         box_set(stdscr, 0, 0);
1826         MvAddStr(0, 20, "Character attribute test display");
1827
1828         for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
1829             row = wide_show_attr(row, n, j == k,
1830                                  ac |
1831                                  attrs_to_test[j].attr |
1832                                  attrs_to_test[k].attr,
1833                                  extras,
1834                                  attrs_to_test[j].name);
1835         }
1836
1837         MvPrintw(row, 8,
1838                  "This terminal does %shave the magic-cookie glitch",
1839                  get_xmc() > -1 ? "" : "not ");
1840         MvPrintw(row + 1, 8, "Enter '?' for help.");
1841         show_color_attr(fg, bg, tx);
1842         printw("  ACS (%d)", ac != 0);
1843
1844         refresh();
1845     } while (wide_attr_getc(&n, &fg, &bg, &tx, &ac, &k));
1846
1847     set_wide_background(0);
1848     erase();
1849     endwin();
1850 }
1851 #endif
1852
1853 /****************************************************************************
1854  *
1855  * Color support tests
1856  *
1857  ****************************************************************************/
1858
1859 static NCURSES_CONST char *the_color_names[] =
1860 {
1861     "black",
1862     "red",
1863     "green",
1864     "yellow",
1865     "blue",
1866     "magenta",
1867     "cyan",
1868     "white",
1869     "BLACK",
1870     "RED",
1871     "GREEN",
1872     "YELLOW",
1873     "BLUE",
1874     "MAGENTA",
1875     "CYAN",
1876     "WHITE"
1877 };
1878
1879 static void
1880 show_color_name(int y, int x, int color, bool wide)
1881 {
1882     if (move(y, x) != ERR) {
1883         char temp[80];
1884         int width = 8;
1885
1886         if (wide) {
1887             sprintf(temp, "%02d", color);
1888             width = 4;
1889         } else if (color >= 8) {
1890             sprintf(temp, "[%02d]", color);
1891         } else if (color < 0) {
1892             strcpy(temp, "default");
1893         } else {
1894             strcpy(temp, the_color_names[color]);
1895         }
1896         printw("%-*.*s", width, width, temp);
1897     }
1898 }
1899
1900 static void
1901 color_legend(WINDOW *helpwin, bool wide)
1902 {
1903     int row = 1;
1904     int col = 1;
1905
1906     MvWPrintw(helpwin, row++, col,
1907               "ESC to exit.");
1908     ++row;
1909     MvWPrintw(helpwin, row++, col,
1910               "Use up/down arrow to scroll through the display if it is");
1911     MvWPrintw(helpwin, row++, col,
1912               "longer than one screen. Control/N and Control/P can be used");
1913     MvWPrintw(helpwin, row++, col,
1914               "in place of up/down arrow.  Use pageup/pagedown to scroll a");
1915     MvWPrintw(helpwin, row++, col,
1916               "full screen; control/B and control/F can be used here.");
1917     ++row;
1918     MvWPrintw(helpwin, row++, col,
1919               "Toggles:");
1920     MvWPrintw(helpwin, row++, col,
1921               "  a/A     toggle altcharset off/on");
1922     MvWPrintw(helpwin, row++, col,
1923               "  b/B     toggle bold off/on");
1924     MvWPrintw(helpwin, row++, col,
1925               "  n/N     toggle text/number on/off");
1926     MvWPrintw(helpwin, row++, col,
1927               "  r/R     toggle reverse on/off");
1928     MvWPrintw(helpwin, row++, col,
1929               "  w/W     toggle width between 8/16 colors");
1930 #if USE_WIDEC_SUPPORT
1931     if (wide) {
1932         MvWPrintw(helpwin, row++, col,
1933                   "Wide characters:");
1934         MvWPrintw(helpwin, row, col,
1935                   "  x/X     toggle text between ASCII and wide-character");
1936     }
1937 #else
1938     (void) wide;
1939 #endif
1940 }
1941
1942 #define set_color_test(name, value) if (name != value) { name = value; base_row = 0; }
1943
1944 /* generate a color test pattern */
1945 static void
1946 color_test(void)
1947 {
1948     short i;
1949     int top = 0, width;
1950     int base_row = 0;
1951     int grid_top = top + 3;
1952     int page_size = (LINES - grid_top);
1953     int pairs_max = PAIR_NUMBER(A_COLOR) + 1;
1954     int row_limit;
1955     int per_row;
1956     char numbered[80];
1957     const char *hello;
1958     bool done = FALSE;
1959     bool opt_acsc = FALSE;
1960     bool opt_bold = FALSE;
1961     bool opt_revs = FALSE;
1962     bool opt_nums = FALSE;
1963     bool opt_wide = FALSE;
1964     WINDOW *helpwin;
1965
1966     if (COLORS * COLORS == COLOR_PAIRS) {
1967         int limit = (COLORS - min_colors) * (COLORS - min_colors);
1968         if (pairs_max > limit)
1969             pairs_max = limit;
1970     } else {
1971         if (pairs_max > COLOR_PAIRS)
1972             pairs_max = COLOR_PAIRS;
1973     }
1974
1975     while (!done) {
1976         int shown = 0;
1977
1978         /* this assumes an 80-column line */
1979         if (opt_wide) {
1980             width = 4;
1981             hello = "Test";
1982             per_row = (COLORS > 8) ? 16 : 8;
1983         } else {
1984             width = 8;
1985             hello = "Hello";
1986             per_row = 8;
1987         }
1988         per_row -= min_colors;
1989
1990         row_limit = (pairs_max + per_row - 1) / per_row;
1991
1992         move(0, 0);
1993         (void) printw("There are %d color pairs and %d colors%s\n",
1994                       pairs_max, COLORS,
1995                       min_colors ? " besides 'default'" : "");
1996
1997         clrtobot();
1998         MvPrintw(top + 1, 0,
1999                  "%dx%d matrix of foreground/background colors, bold *%s*\n",
2000                  row_limit,
2001                  per_row,
2002                  opt_bold ? "on" : "off");
2003
2004         /* show color names/numbers across the top */
2005         for (i = 0; i < per_row; i++)
2006             show_color_name(top + 2, (i + 1) * width, i + min_colors, opt_wide);
2007
2008         /* show a grid of colors, with color names/ numbers on the left */
2009         for (i = (short) (base_row * per_row); i < pairs_max; i++) {
2010             int row = grid_top + (i / per_row) - base_row;
2011             int col = (i % per_row + 1) * width;
2012             short pair = i;
2013
2014 #define InxToFG(i) (short) ((i % (COLORS - min_colors)) + min_colors)
2015 #define InxToBG(i) (short) ((i / (COLORS - min_colors)) + min_colors)
2016             if (row >= 0 && move(row, col) != ERR) {
2017                 short fg = InxToFG(i);
2018                 short bg = InxToBG(i);
2019
2020                 init_pair(pair, fg, bg);
2021                 attron((attr_t) COLOR_PAIR(pair));
2022                 if (opt_acsc)
2023                     attron((attr_t) A_ALTCHARSET);
2024                 if (opt_bold)
2025                     attron((attr_t) A_BOLD);
2026                 if (opt_revs)
2027                     attron((attr_t) A_REVERSE);
2028
2029                 if (opt_nums) {
2030                     sprintf(numbered, "{%02X}", i);
2031                     hello = numbered;
2032                 }
2033                 printw("%-*.*s", width, width, hello);
2034                 (void) attrset(A_NORMAL);
2035
2036                 if ((i % per_row) == 0 && InxToFG(i) == min_colors) {
2037                     show_color_name(row, 0, InxToBG(i), opt_wide);
2038                 }
2039                 ++shown;
2040             } else if (shown) {
2041                 break;
2042             }
2043         }
2044
2045         switch (wGetchar(stdscr)) {
2046         case 'a':
2047             opt_acsc = FALSE;
2048             break;
2049         case 'A':
2050             opt_acsc = TRUE;
2051             break;
2052         case 'b':
2053             opt_bold = FALSE;
2054             break;
2055         case 'B':
2056             opt_bold = TRUE;
2057             break;
2058         case 'n':
2059             opt_nums = FALSE;
2060             break;
2061         case 'N':
2062             opt_nums = TRUE;
2063             break;
2064         case 'r':
2065             opt_revs = FALSE;
2066             break;
2067         case 'R':
2068             opt_revs = TRUE;
2069             break;
2070         case case_QUIT:
2071             done = TRUE;
2072             continue;
2073         case 'w':
2074             set_color_test(opt_wide, FALSE);
2075             break;
2076         case 'W':
2077             set_color_test(opt_wide, TRUE);
2078             break;
2079         case CTRL('p'):
2080         case KEY_UP:
2081             if (base_row <= 0) {
2082                 beep();
2083             } else {
2084                 base_row -= 1;
2085             }
2086             break;
2087         case CTRL('n'):
2088         case KEY_DOWN:
2089             if (base_row + page_size >= row_limit) {
2090                 beep();
2091             } else {
2092                 base_row += 1;
2093             }
2094             break;
2095         case CTRL('b'):
2096         case KEY_PREVIOUS:
2097         case KEY_PPAGE:
2098             if (base_row <= 0) {
2099                 beep();
2100             } else {
2101                 base_row -= (page_size - 1);
2102                 if (base_row < 0)
2103                     base_row = 0;
2104             }
2105             break;
2106         case CTRL('f'):
2107         case KEY_NEXT:
2108         case KEY_NPAGE:
2109             if (base_row + page_size >= row_limit) {
2110                 beep();
2111             } else {
2112                 base_row += page_size - 1;
2113                 if (base_row + page_size >= row_limit) {
2114                     base_row = row_limit - page_size - 1;
2115                 }
2116             }
2117             break;
2118         case '?':
2119             if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
2120                 box(helpwin, 0, 0);
2121                 color_legend(helpwin, FALSE);
2122                 wGetchar(helpwin);
2123                 delwin(helpwin);
2124             }
2125             break;
2126         default:
2127             beep();
2128             continue;
2129         }
2130     }
2131
2132     erase();
2133     endwin();
2134 }
2135
2136 #if USE_WIDEC_SUPPORT
2137 /* generate a color test pattern */
2138 static void
2139 wide_color_test(void)
2140 {
2141     int i;
2142     int top = 0, width;
2143     int base_row = 0;
2144     int grid_top = top + 3;
2145     int page_size = (LINES - grid_top);
2146     int pairs_max = (unsigned short) (-1);
2147     int row_limit;
2148     int per_row;
2149     char numbered[80];
2150     const char *hello;
2151     bool done = FALSE;
2152     bool opt_acsc = FALSE;
2153     bool opt_bold = FALSE;
2154     bool opt_revs = FALSE;
2155     bool opt_wide = FALSE;
2156     bool opt_nums = FALSE;
2157     bool opt_xchr = FALSE;
2158     wchar_t buffer[10];
2159     WINDOW *helpwin;
2160
2161     if (COLORS * COLORS == COLOR_PAIRS) {
2162         int limit = (COLORS - min_colors) * (COLORS - min_colors);
2163         if (pairs_max > limit)
2164             pairs_max = limit;
2165     } else {
2166         if (pairs_max > COLOR_PAIRS)
2167             pairs_max = COLOR_PAIRS;
2168     }
2169
2170     while (!done) {
2171         int shown = 0;
2172
2173         /* this assumes an 80-column line */
2174         if (opt_wide) {
2175             width = 4;
2176             hello = "Test";
2177             per_row = (COLORS > 8) ? 16 : 8;
2178         } else {
2179             width = 8;
2180             hello = "Hello";
2181             per_row = 8;
2182         }
2183         per_row -= min_colors;
2184
2185         if (opt_xchr) {
2186             make_fullwidth_text(buffer, hello);
2187             width *= 2;
2188             per_row /= 2;
2189         } else {
2190             make_narrow_text(buffer, hello);
2191         }
2192
2193         row_limit = (pairs_max + per_row - 1) / per_row;
2194
2195         move(0, 0);
2196         (void) printw("There are %d color pairs and %d colors%s\n",
2197                       pairs_max, COLORS,
2198                       min_colors ? " besides 'default'" : "");
2199
2200         clrtobot();
2201         MvPrintw(top + 1, 0,
2202                  "%dx%d matrix of foreground/background colors, bold *%s*\n",
2203                  row_limit,
2204                  per_row,
2205                  opt_bold ? "on" : "off");
2206
2207         /* show color names/numbers across the top */
2208         for (i = 0; i < per_row; i++)
2209             show_color_name(top + 2, (i + 1) * width, i + min_colors, opt_wide);
2210
2211         /* show a grid of colors, with color names/ numbers on the left */
2212         for (i = (base_row * per_row); i < pairs_max; i++) {
2213             int row = grid_top + (i / per_row) - base_row;
2214             int col = (i % per_row + 1) * width;
2215             short pair = (short) i;
2216
2217             if (row >= 0 && move(row, col) != ERR) {
2218                 init_pair(pair, InxToFG(i), InxToBG(i));
2219                 color_set(pair, NULL);
2220                 if (opt_acsc)
2221                     attr_on((attr_t) A_ALTCHARSET, NULL);
2222                 if (opt_bold)
2223                     attr_on((attr_t) A_BOLD, NULL);
2224                 if (opt_revs)
2225                     attr_on((attr_t) A_REVERSE, NULL);
2226
2227                 if (opt_nums) {
2228                     sprintf(numbered, "{%02X}", i);
2229                     if (opt_xchr) {
2230                         make_fullwidth_text(buffer, numbered);
2231                     } else {
2232                         make_narrow_text(buffer, numbered);
2233                     }
2234                 }
2235                 addnwstr(buffer, width);
2236                 (void) attr_set(A_NORMAL, 0, NULL);
2237
2238                 if ((i % per_row) == 0 && InxToFG(i) == min_colors) {
2239                     show_color_name(row, 0, InxToBG(i), opt_wide);
2240                 }
2241                 ++shown;
2242             } else if (shown) {
2243                 break;
2244             }
2245         }
2246
2247         switch (wGetchar(stdscr)) {
2248         case 'a':
2249             opt_acsc = FALSE;
2250             break;
2251         case 'A':
2252             opt_acsc = TRUE;
2253             break;
2254         case 'b':
2255             opt_bold = FALSE;
2256             break;
2257         case 'B':
2258             opt_bold = TRUE;
2259             break;
2260         case 'n':
2261             opt_nums = FALSE;
2262             break;
2263         case 'N':
2264             opt_nums = TRUE;
2265             break;
2266         case 'r':
2267             opt_revs = FALSE;
2268             break;
2269         case 'R':
2270             opt_revs = TRUE;
2271             break;
2272         case case_QUIT:
2273             done = TRUE;
2274             continue;
2275         case 'w':
2276             set_color_test(opt_wide, FALSE);
2277             break;
2278         case 'W':
2279             set_color_test(opt_wide, TRUE);
2280             break;
2281         case 'x':
2282             opt_xchr = FALSE;
2283             break;
2284         case 'X':
2285             opt_xchr = TRUE;
2286             break;
2287         case CTRL('p'):
2288         case KEY_UP:
2289             if (base_row <= 0) {
2290                 beep();
2291             } else {
2292                 base_row -= 1;
2293             }
2294             break;
2295         case CTRL('n'):
2296         case KEY_DOWN:
2297             if (base_row + page_size >= row_limit) {
2298                 beep();
2299             } else {
2300                 base_row += 1;
2301             }
2302             break;
2303         case CTRL('b'):
2304         case KEY_PREVIOUS:
2305         case KEY_PPAGE:
2306             if (base_row <= 0) {
2307                 beep();
2308             } else {
2309                 base_row -= (page_size - 1);
2310                 if (base_row < 0)
2311                     base_row = 0;
2312             }
2313             break;
2314         case CTRL('f'):
2315         case KEY_NEXT:
2316         case KEY_NPAGE:
2317             if (base_row + page_size >= row_limit) {
2318                 beep();
2319             } else {
2320                 base_row += page_size - 1;
2321                 if (base_row + page_size >= row_limit) {
2322                     base_row = row_limit - page_size - 1;
2323                 }
2324             }
2325             break;
2326         case '?':
2327             if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
2328                 box(helpwin, 0, 0);
2329                 color_legend(helpwin, TRUE);
2330                 wGetchar(helpwin);
2331                 delwin(helpwin);
2332             }
2333             break;
2334         default:
2335             beep();
2336             continue;
2337         }
2338     }
2339
2340     erase();
2341     endwin();
2342 }
2343 #endif /* USE_WIDEC_SUPPORT */
2344
2345 static void
2346 change_color(short current, int field, int value, int usebase)
2347 {
2348     short red, green, blue;
2349
2350     color_content(current, &red, &green, &blue);
2351
2352     switch (field) {
2353     case 0:
2354         red = (short) (usebase ? (red + value) : value);
2355         break;
2356     case 1:
2357         green = (short) (usebase ? (green + value) : value);
2358         break;
2359     case 2:
2360         blue = (short) (usebase ? (blue + value) : value);
2361         break;
2362     }
2363
2364     if (init_color(current, red, green, blue) == ERR)
2365         beep();
2366 }
2367
2368 static void
2369 init_all_colors(void)
2370 {
2371     short c;
2372
2373     for (c = 0; c < COLORS; ++c)
2374         init_color(c,
2375                    all_colors[c].red,
2376                    all_colors[c].green,
2377                    all_colors[c].blue);
2378 }
2379
2380 #define scaled_rgb(n) ((255 * (n)) / 1000)
2381
2382 static void
2383 color_edit(void)
2384 /* display the color test pattern, without trying to edit colors */
2385 {
2386     int i;
2387     int current = 0;
2388     int this_c = 0, value = 0, field = 0;
2389     int last_c;
2390     int top_color = 0;
2391     int page_size = (LINES - 6);
2392
2393     init_all_colors();
2394     refresh();
2395
2396     for (i = 0; i < max_colors; i++)
2397         init_pair((short) i, (short) COLOR_WHITE, (short) i);
2398
2399     MvPrintw(LINES - 2, 0, "Number: %d", value);
2400
2401     do {
2402         short red, green, blue;
2403
2404         attron(A_BOLD);
2405         MvAddStr(0, 20, "Color RGB Value Editing");
2406         attroff(A_BOLD);
2407
2408         for (i = (short) top_color;
2409              (i - top_color < page_size)
2410              && (i < max_colors); i++) {
2411             char numeric[80];
2412
2413             sprintf(numeric, "[%d]", i);
2414             MvPrintw(2 + i - top_color, 0, "%c %-8s:",
2415                      (i == current ? '>' : ' '),
2416                      (i < (int) SIZEOF(the_color_names)
2417                       ? the_color_names[i] : numeric));
2418             (void) attrset(COLOR_PAIR(i));
2419             addstr("        ");
2420             (void) attrset(A_NORMAL);
2421
2422             color_content((short) i, &red, &green, &blue);
2423             addstr("   R = ");
2424             if (current == i && field == 0)
2425                 attron(A_STANDOUT);
2426             printw("%04d", red);
2427             if (current == i && field == 0)
2428                 (void) attrset(A_NORMAL);
2429             addstr(", G = ");
2430             if (current == i && field == 1)
2431                 attron(A_STANDOUT);
2432             printw("%04d", green);
2433             if (current == i && field == 1)
2434                 (void) attrset(A_NORMAL);
2435             addstr(", B = ");
2436             if (current == i && field == 2)
2437                 attron(A_STANDOUT);
2438             printw("%04d", blue);
2439             if (current == i && field == 2)
2440                 (void) attrset(A_NORMAL);
2441             (void) attrset(A_NORMAL);
2442             printw(" ( %3d %3d %3d )",
2443                    scaled_rgb(red),
2444                    scaled_rgb(green),
2445                    scaled_rgb(blue));
2446         }
2447
2448         MvAddStr(LINES - 3, 0,
2449                  "Use up/down to select a color, left/right to change fields.");
2450         MvAddStr(LINES - 2, 0,
2451                  "Modify field by typing nnn=, nnn-, or nnn+.  ? for help.");
2452
2453         move(2 + current - top_color, 0);
2454
2455         last_c = this_c;
2456         this_c = Getchar();
2457         if (this_c < 256 && isdigit(this_c) && !isdigit(last_c))
2458             value = 0;
2459
2460         switch (this_c) {
2461         case CTRL('b'):
2462         case KEY_PPAGE:
2463             if (current > 0)
2464                 current -= (page_size - 1);
2465             else
2466                 beep();
2467             break;
2468
2469         case CTRL('f'):
2470         case KEY_NPAGE:
2471             if (current < (max_colors - 1))
2472                 current += (page_size - 1);
2473             else
2474                 beep();
2475             break;
2476
2477         case CTRL('p'):
2478         case KEY_UP:
2479             current = (current == 0 ? (max_colors - 1) : current - 1);
2480             break;
2481
2482         case CTRL('n'):
2483         case KEY_DOWN:
2484             current = (current == (max_colors - 1) ? 0 : current + 1);
2485             break;
2486
2487         case KEY_RIGHT:
2488             field = (field == 2 ? 0 : field + 1);
2489             break;
2490
2491         case KEY_LEFT:
2492             field = (field == 0 ? 2 : field - 1);
2493             break;
2494
2495         case '0':
2496         case '1':
2497         case '2':
2498         case '3':
2499         case '4':
2500         case '5':
2501         case '6':
2502         case '7':
2503         case '8':
2504         case '9':
2505             value = value * 10 + (this_c - '0');
2506             break;
2507
2508         case '+':
2509             change_color((short) current, field, value, 1);
2510             break;
2511
2512         case '-':
2513             change_color((short) current, field, -value, 1);
2514             break;
2515
2516         case '=':
2517             change_color((short) current, field, value, 0);
2518             break;
2519
2520         case '?':
2521             erase();
2522             P("                      RGB Value Editing Help");
2523             P("");
2524             P("You are in the RGB value editor.  Use the arrow keys to select one of");
2525             P("the fields in one of the RGB triples of the current colors; the one");
2526             P("currently selected will be reverse-video highlighted.");
2527             P("");
2528             P("To change a field, enter the digits of the new value; they are echoed");
2529             P("as entered.  Finish by typing `='.  The change will take effect instantly.");
2530             P("To increment or decrement a value, use the same procedure, but finish");
2531             P("with a `+' or `-'.");
2532             P("");
2533             P("Press 'm' to invoke the top-level menu with the current color settings.");
2534             P("To quit, do ESC");
2535
2536             Pause();
2537             erase();
2538             break;
2539
2540         case 'm':
2541             endwin();
2542             main_menu(FALSE);
2543             for (i = 0; i < max_colors; i++)
2544                 init_pair((short) i, (short) COLOR_WHITE, (short) i);
2545             refresh();
2546             break;
2547
2548         case case_QUIT:
2549             break;
2550
2551         default:
2552             beep();
2553             break;
2554         }
2555
2556         if (current < 0)
2557             current = 0;
2558         if (current >= max_colors)
2559             current = max_colors - 1;
2560         if (current < top_color)
2561             top_color = current;
2562         if (current - top_color >= page_size)
2563             top_color = current - (page_size - 1);
2564
2565         MvPrintw(LINES - 1, 0, "Number: %d", value);
2566         clrtoeol();
2567     } while
2568         (!isQuit(this_c));
2569
2570     erase();
2571
2572     /*
2573      * ncurses does not reset each color individually when calling endwin().
2574      */
2575     init_all_colors();
2576
2577     endwin();
2578 }
2579
2580 /****************************************************************************
2581  *
2582  * Soft-key label test
2583  *
2584  ****************************************************************************/
2585
2586 #if USE_SOFTKEYS
2587
2588 #define SLK_HELP 17
2589 #define SLK_WORK (SLK_HELP + 3)
2590
2591 static void
2592 slk_help(void)
2593 {
2594     static const char *table[] =
2595     {
2596         "Available commands are:"
2597         ,""
2598         ,"^L         -- repaint this message and activate soft keys"
2599         ,"a/d        -- activate/disable soft keys"
2600         ,"c          -- set centered format for labels"
2601         ,"l          -- set left-justified format for labels"
2602         ,"r          -- set right-justified format for labels"
2603         ,"[12345678] -- set label; labels are numbered 1 through 8"
2604         ,"e          -- erase stdscr (should not erase labels)"
2605         ,"s          -- test scrolling of shortened screen"
2606         ,"v/V        -- cycle through video attributes"
2607 #if HAVE_SLK_COLOR
2608         ,"F/f/B/b    -- cycle through foreground/background colors"
2609 #endif
2610         ,"ESC        -- return to main menu"
2611         ,""
2612         ,"Note: if activating the soft keys causes your terminal to scroll up"
2613         ,"one line, your terminal auto-scrolls when anything is written to the"
2614         ,"last screen position.  The ncurses code does not yet handle this"
2615         ,"gracefully."
2616     };
2617     unsigned j;
2618
2619     move(2, 0);
2620     for (j = 0; j < SIZEOF(table); ++j) {
2621         P(table[j]);
2622     }
2623     refresh();
2624 }
2625
2626 /****************************************************************************
2627  *
2628  * Alternate character-set stuff
2629  *
2630  ****************************************************************************/
2631 /* *INDENT-OFF* */
2632 static struct {
2633     chtype attr;
2634     const char *name;
2635 } attrs_to_cycle[] = {
2636     { A_NORMAL,         "normal" },
2637     { A_BOLD,           "bold" },
2638     { A_BLINK,          "blink" },
2639     { A_REVERSE,        "reverse" },
2640     { A_UNDERLINE,      "underline" },
2641 };
2642 /* *INDENT-ON* */
2643
2644 static bool
2645 cycle_attr(int ch, unsigned *at_code, chtype *attr)
2646 {
2647     bool result = TRUE;
2648
2649     switch (ch) {
2650     case 'v':
2651         if ((*at_code += 1) >= SIZEOF(attrs_to_cycle))
2652             *at_code = 0;
2653         break;
2654     case 'V':
2655         if (*at_code == 0)
2656             *at_code = SIZEOF(attrs_to_cycle) - 1;
2657         else
2658             *at_code -= 1;
2659         break;
2660     default:
2661         result = FALSE;
2662         break;
2663     }
2664     if (result)
2665         *attr = attrs_to_cycle[*at_code].attr;
2666     return result;
2667 }
2668
2669 static bool
2670 cycle_colors(int ch, int *fg, int *bg, short *pair)
2671 {
2672     bool result = FALSE;
2673
2674     if (use_colors) {
2675         result = TRUE;
2676         switch (ch) {
2677         case 'F':
2678             if ((*fg -= 1) < 0)
2679                 *fg = COLORS - 1;
2680             break;
2681         case 'f':
2682             if ((*fg += 1) >= COLORS)
2683                 *fg = 0;
2684             break;
2685         case 'B':
2686             if ((*bg -= 1) < 0)
2687                 *bg = COLORS - 1;
2688             break;
2689         case 'b':
2690             if ((*bg += 1) >= COLORS)
2691                 *bg = 0;
2692             break;
2693         default:
2694             result = FALSE;
2695             break;
2696         }
2697         if (result) {
2698             *pair = (short) (*fg != COLOR_BLACK || *bg != COLOR_BLACK);
2699             if (*pair != 0) {
2700                 *pair = 1;
2701                 if (init_pair(*pair, (short) *fg, (short) *bg) == ERR) {
2702                     result = FALSE;
2703                 }
2704             }
2705         }
2706     }
2707     return result;
2708 }
2709
2710 #if HAVE_SLK_COLOR
2711 static void
2712 call_slk_color(short fg, short bg)
2713 {
2714     init_pair(1, bg, fg);
2715     slk_color(1);
2716     MvPrintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg);
2717     clrtoeol();
2718     slk_touch();
2719     slk_noutrefresh();
2720     refresh();
2721 }
2722 #endif
2723
2724 static void
2725 slk_test(void)
2726 /* exercise the soft keys */
2727 {
2728     int c, fmt = 1;
2729     char buf[9];
2730     char *s;
2731     chtype attr = A_NORMAL;
2732     unsigned at_code = 0;
2733 #if HAVE_SLK_COLOR
2734     int fg = COLOR_BLACK;
2735     int bg = COLOR_WHITE;
2736     short pair = 0;
2737 #endif
2738
2739     c = CTRL('l');
2740 #if HAVE_SLK_COLOR
2741     if (use_colors) {
2742         call_slk_color(fg, bg);
2743     }
2744 #endif
2745
2746     do {
2747         move(0, 0);
2748         switch (c) {
2749         case CTRL('l'):
2750             erase();
2751             attron(A_BOLD);
2752             MvAddStr(0, 20, "Soft Key Exerciser");
2753             attroff(A_BOLD);
2754
2755             slk_help();
2756             /* fall through */
2757
2758         case 'a':
2759             slk_restore();
2760             break;
2761
2762         case 'e':
2763             wclear(stdscr);
2764             break;
2765
2766         case 's':
2767             MvPrintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
2768             while ((c = Getchar()) != 'Q' && (c != ERR))
2769                 addch((chtype) c);
2770             break;
2771
2772         case 'd':
2773             slk_clear();
2774             break;
2775
2776         case 'l':
2777             fmt = 0;
2778             break;
2779
2780         case 'c':
2781             fmt = 1;
2782             break;
2783
2784         case 'r':
2785             fmt = 2;
2786             break;
2787
2788         case '1':
2789         case '2':
2790         case '3':
2791         case '4':
2792         case '5':
2793         case '6':
2794         case '7':
2795         case '8':
2796             MvAddStr(SLK_WORK, 0, "Please enter the label value: ");
2797             strcpy(buf, "");
2798             if ((s = slk_label(c - '0')) != 0) {
2799                 strncpy(buf, s, 8);
2800             }
2801             wGetstring(stdscr, buf, 8);
2802             slk_set((c - '0'), buf, fmt);
2803             slk_refresh();
2804             move(SLK_WORK, 0);
2805             clrtobot();
2806             break;
2807
2808         case case_QUIT:
2809             goto done;
2810
2811 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
2812         case KEY_RESIZE:
2813             wnoutrefresh(stdscr);
2814             break;
2815 #endif
2816
2817         default:
2818             if (cycle_attr(c, &at_code, &attr)) {
2819                 slk_attrset(attr);
2820                 slk_touch();
2821                 slk_noutrefresh();
2822                 break;
2823             }
2824 #if HAVE_SLK_COLOR
2825             if (cycle_colors(c, &fg, &bg, &pair)) {
2826                 if (use_colors) {
2827                     call_slk_color(fg, bg);
2828                 } else {
2829                     beep();
2830                 }
2831                 break;
2832             }
2833 #endif
2834             beep();
2835             break;
2836         }
2837     } while (!isQuit(c = Getchar()));
2838
2839   done:
2840     slk_clear();
2841     erase();
2842     endwin();
2843 }
2844
2845 #if USE_WIDEC_SUPPORT
2846 #define SLKLEN 8
2847 static void
2848 wide_slk_test(void)
2849 /* exercise the soft keys */
2850 {
2851     int c, fmt = 1;
2852     wchar_t buf[SLKLEN + 1];
2853     char *s;
2854     chtype attr = A_NORMAL;
2855     unsigned at_code = 0;
2856     int fg = COLOR_BLACK;
2857     int bg = COLOR_WHITE;
2858     short pair = 0;
2859
2860     c = CTRL('l');
2861     if (use_colors) {
2862         call_slk_color(fg, bg);
2863     }
2864     do {
2865         move(0, 0);
2866         switch (c) {
2867         case CTRL('l'):
2868             erase();
2869             attr_on(WA_BOLD, NULL);
2870             MvAddStr(0, 20, "Soft Key Exerciser");
2871             attr_off(WA_BOLD, NULL);
2872
2873             slk_help();
2874             /* fall through */
2875
2876         case 'a':
2877             slk_restore();
2878             break;
2879
2880         case 'e':
2881             wclear(stdscr);
2882             break;
2883
2884         case 's':
2885             MvPrintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
2886             while ((c = Getchar()) != 'Q' && (c != ERR))
2887                 addch((chtype) c);
2888             break;
2889
2890         case 'd':
2891             slk_clear();
2892             break;
2893
2894         case 'l':
2895             fmt = 0;
2896             break;
2897
2898         case 'c':
2899             fmt = 1;
2900             break;
2901
2902         case 'r':
2903             fmt = 2;
2904             break;
2905
2906         case '1':
2907         case '2':
2908         case '3':
2909         case '4':
2910         case '5':
2911         case '6':
2912         case '7':
2913         case '8':
2914             MvAddStr(SLK_WORK, 0, "Please enter the label value: ");
2915             *buf = 0;
2916             if ((s = slk_label(c - '0')) != 0) {
2917                 char *temp = strdup(s);
2918                 size_t used = strlen(temp);
2919                 size_t want = SLKLEN;
2920                 size_t test;
2921 #ifndef state_unused
2922                 mbstate_t state;
2923 #endif
2924
2925                 buf[0] = L'\0';
2926                 while (want > 0 && used != 0) {
2927                     const char *base = s;
2928                     reset_mbytes(state);
2929                     test = count_mbytes(base, 0, &state);
2930                     if (test == (size_t) -1) {
2931                         temp[--used] = 0;
2932                     } else if (test > want) {
2933                         temp[--used] = 0;
2934                     } else {
2935                         reset_mbytes(state);
2936                         trans_mbytes(buf, base, want, &state);
2937                         break;
2938                     }
2939                 }
2940                 free(temp);
2941             }
2942             wGet_wstring(stdscr, buf, SLKLEN);
2943             slk_wset((c - '0'), buf, fmt);
2944             slk_refresh();
2945             move(SLK_WORK, 0);
2946             clrtobot();
2947             break;
2948
2949         case case_QUIT:
2950             goto done;
2951
2952         case 'F':
2953             if (use_colors) {
2954                 fg = (short) ((fg + 1) % COLORS);
2955                 call_slk_color(fg, bg);
2956             }
2957             break;
2958         case 'B':
2959             if (use_colors) {
2960                 bg = (short) ((bg + 1) % COLORS);
2961                 call_slk_color(fg, bg);
2962             }
2963             break;
2964 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
2965         case KEY_RESIZE:
2966             wnoutrefresh(stdscr);
2967             break;
2968 #endif
2969         default:
2970             if (cycle_attr(c, &at_code, &attr)) {
2971                 slk_attr_set(attr, (fg || bg), NULL);
2972                 slk_touch();
2973                 slk_noutrefresh();
2974                 break;
2975             }
2976 #if HAVE_SLK_COLOR
2977             if (cycle_colors(c, &fg, &bg, &pair)) {
2978                 if (use_colors) {
2979                     call_slk_color(fg, bg);
2980                 } else {
2981                     beep();
2982                 }
2983                 break;
2984             }
2985 #endif
2986             beep();
2987             break;
2988         }
2989     } while (!isQuit(c = Getchar()));
2990
2991   done:
2992     slk_clear();
2993     erase();
2994     endwin();
2995 }
2996 #endif
2997 #endif /* SLK_INIT */
2998
2999 /* ISO 6429:  codes 0x80 to 0x9f may be control characters that cause the
3000  * terminal to perform functions.  The remaining codes can be graphic.
3001  */
3002 static void
3003 show_upper_chars(unsigned first, int repeat, attr_t attr, short pair)
3004 {
3005     bool C1 = (first == 128);
3006     unsigned code;
3007     unsigned last = first + 31;
3008     int reply;
3009
3010     erase();
3011     attron(A_BOLD);
3012     MvPrintw(0, 20, "Display of %s Character Codes %d to %d",
3013              C1 ? "C1" : "GR", first, last);
3014     attroff(A_BOLD);
3015     refresh();
3016
3017     for (code = first; code <= last; code++) {
3018         int count = repeat;
3019         int row = 2 + ((int) (code - first) % 16);
3020         int col = ((int) (code - first) / 16) * COLS / 2;
3021         char tmp[80];
3022         sprintf(tmp, "%3u (0x%x)", code, code);
3023         MvPrintw(row, col, "%*s: ", COLS / 4, tmp);
3024
3025         do {
3026             if (C1)
3027                 nodelay(stdscr, TRUE);
3028             echochar(colored_chtype(code, attr, pair));
3029             if (C1) {
3030                 /* (yes, this _is_ crude) */
3031                 while ((reply = Getchar()) != ERR) {
3032                     addch(UChar(reply));
3033                     napms(10);
3034                 }
3035                 nodelay(stdscr, FALSE);
3036             }
3037         } while (--count > 0);
3038     }
3039 }
3040
3041 #define PC_COLS 4
3042
3043 static void
3044 show_pc_chars(int repeat, attr_t attr, short pair)
3045 {
3046     unsigned code;
3047
3048     erase();
3049     attron(A_BOLD);
3050     MvPrintw(0, 20, "Display of PC Character Codes");
3051     attroff(A_BOLD);
3052     refresh();
3053
3054     for (code = 0; code < 16; ++code) {
3055         MvPrintw(2, (int) code * PC_COLS + 8, "%X", code);
3056     }
3057     for (code = 0; code < 256; code++) {
3058         int count = repeat;
3059         int row = 3 + (int) (code / 16) + (code >= 128);
3060         int col = 8 + (int) (code % 16) * PC_COLS;
3061         if ((code % 16) == 0)
3062             MvPrintw(row, 0, "0x%02x:", code);
3063         move(row, col);
3064         do {
3065             switch (code) {
3066             case '\n':
3067             case '\r':
3068             case '\b':
3069             case '\f':
3070             case '\033':
3071             case 0x9b:
3072                 /*
3073                  * Skip the ones that do not work.
3074                  */
3075                 break;
3076             default:
3077                 addch(colored_chtype(code, A_ALTCHARSET | attr, pair));
3078                 break;
3079             }
3080         } while (--count > 0);
3081     }
3082 }
3083
3084 static void
3085 show_box_chars(int repeat, attr_t attr, short pair)
3086 {
3087     (void) repeat;
3088     attr |= COLOR_PAIR(pair);
3089
3090     erase();
3091     attron(A_BOLD);
3092     MvAddStr(0, 20, "Display of the ACS Line-Drawing Set");
3093     attroff(A_BOLD);
3094     refresh();
3095     /* *INDENT-OFF* */
3096     wborder(stdscr,
3097             colored_chtype(ACS_VLINE,    attr, pair),
3098             colored_chtype(ACS_VLINE,    attr, pair),
3099             colored_chtype(ACS_HLINE,    attr, pair),
3100             colored_chtype(ACS_HLINE,    attr, pair),
3101             colored_chtype(ACS_ULCORNER, attr, pair),
3102             colored_chtype(ACS_URCORNER, attr, pair),
3103             colored_chtype(ACS_LLCORNER, attr, pair),
3104             colored_chtype(ACS_LRCORNER, attr, pair));
3105     MvHLine(LINES / 2, 0,        colored_chtype(ACS_HLINE, attr, pair), COLS);
3106     MvVLine(0,         COLS / 2, colored_chtype(ACS_VLINE, attr, pair), LINES);
3107     MvAddCh(0,         COLS / 2, colored_chtype(ACS_TTEE,  attr, pair));
3108     MvAddCh(LINES / 2, COLS / 2, colored_chtype(ACS_PLUS,  attr, pair));
3109     MvAddCh(LINES - 1, COLS / 2, colored_chtype(ACS_BTEE,  attr, pair));
3110     MvAddCh(LINES / 2, 0,        colored_chtype(ACS_LTEE,  attr, pair));
3111     MvAddCh(LINES / 2, COLS - 1, colored_chtype(ACS_RTEE,  attr, pair));
3112     /* *INDENT-ON* */
3113
3114 }
3115
3116 static int
3117 show_1_acs(int n, int repeat, const char *name, chtype code)
3118 {
3119     const int height = 16;
3120     int row = 2 + (n % height);
3121     int col = (n / height) * COLS / 2;
3122
3123     MvPrintw(row, col, "%*s : ", COLS / 4, name);
3124     do {
3125         addch(code);
3126     } while (--repeat > 0);
3127     return n + 1;
3128 }
3129
3130 static void
3131 show_acs_chars(int repeat, attr_t attr, short pair)
3132 /* display the ACS character set */
3133 {
3134     int n;
3135
3136 #define BOTH(name) #name, colored_chtype(name, attr, pair)
3137
3138     erase();
3139     attron(A_BOLD);
3140     MvAddStr(0, 20, "Display of the ACS Character Set");
3141     attroff(A_BOLD);
3142     refresh();
3143
3144     n = show_1_acs(0, repeat, BOTH(ACS_ULCORNER));
3145     n = show_1_acs(n, repeat, BOTH(ACS_URCORNER));
3146     n = show_1_acs(n, repeat, BOTH(ACS_LLCORNER));
3147     n = show_1_acs(n, repeat, BOTH(ACS_LRCORNER));
3148
3149     n = show_1_acs(n, repeat, BOTH(ACS_LTEE));
3150     n = show_1_acs(n, repeat, BOTH(ACS_RTEE));
3151     n = show_1_acs(n, repeat, BOTH(ACS_TTEE));
3152     n = show_1_acs(n, repeat, BOTH(ACS_BTEE));
3153
3154     n = show_1_acs(n, repeat, BOTH(ACS_HLINE));
3155     n = show_1_acs(n, repeat, BOTH(ACS_VLINE));
3156
3157     /*
3158      * HPUX's ACS definitions are broken here.  Just give up.
3159      */
3160 #if !(defined(__hpux) && !defined(NCURSES_VERSION))
3161     n = show_1_acs(n, repeat, BOTH(ACS_LARROW));
3162     n = show_1_acs(n, repeat, BOTH(ACS_RARROW));
3163     n = show_1_acs(n, repeat, BOTH(ACS_UARROW));
3164     n = show_1_acs(n, repeat, BOTH(ACS_DARROW));
3165
3166     n = show_1_acs(n, repeat, BOTH(ACS_BLOCK));
3167     n = show_1_acs(n, repeat, BOTH(ACS_BOARD));
3168     n = show_1_acs(n, repeat, BOTH(ACS_LANTERN));
3169     n = show_1_acs(n, repeat, BOTH(ACS_BULLET));
3170     n = show_1_acs(n, repeat, BOTH(ACS_CKBOARD));
3171     n = show_1_acs(n, repeat, BOTH(ACS_DEGREE));
3172     n = show_1_acs(n, repeat, BOTH(ACS_DIAMOND));
3173     n = show_1_acs(n, repeat, BOTH(ACS_PLMINUS));
3174     n = show_1_acs(n, repeat, BOTH(ACS_PLUS));
3175
3176     n = show_1_acs(n, repeat, BOTH(ACS_GEQUAL));
3177     n = show_1_acs(n, repeat, BOTH(ACS_NEQUAL));
3178     n = show_1_acs(n, repeat, BOTH(ACS_LEQUAL));
3179
3180     n = show_1_acs(n, repeat, BOTH(ACS_STERLING));
3181     n = show_1_acs(n, repeat, BOTH(ACS_PI));
3182     n = show_1_acs(n, repeat, BOTH(ACS_S1));
3183     n = show_1_acs(n, repeat, BOTH(ACS_S3));
3184     n = show_1_acs(n, repeat, BOTH(ACS_S7));
3185     (void) show_1_acs(n, repeat, BOTH(ACS_S9));
3186 #endif
3187 }
3188
3189 static void
3190 acs_display(void)
3191 {
3192     int c = 'a';
3193     char *term = getenv("TERM");
3194     const char *pch_kludge = ((term != 0 && strstr(term, "linux"))
3195                               ? "p=PC, "
3196                               : "");
3197     chtype attr = A_NORMAL;
3198     int digit = 0;
3199     int repeat = 1;
3200     int fg = COLOR_BLACK;
3201     int bg = COLOR_BLACK;
3202     unsigned at_code = 0;
3203     short pair = 0;
3204     void (*last_show_acs) (int, attr_t, short) = 0;
3205
3206     do {
3207         switch (c) {
3208         case CTRL('L'):
3209             Repaint();
3210             break;
3211         case 'a':
3212             ToggleAcs(last_show_acs, show_acs_chars);
3213             break;
3214         case 'p':
3215             if (*pch_kludge)
3216                 ToggleAcs(last_show_acs, show_pc_chars);
3217             else
3218                 beep();
3219             break;
3220         case 'x':
3221             ToggleAcs(last_show_acs, show_box_chars);
3222             break;
3223         case '0':
3224         case '1':
3225         case '2':
3226         case '3':
3227             digit = (c - '0');
3228             last_show_acs = 0;
3229             break;
3230         case '-':
3231             if (digit > 0) {
3232                 --digit;
3233                 last_show_acs = 0;
3234             } else {
3235                 beep();
3236             }
3237             break;
3238         case '+':
3239             if (digit < 3) {
3240                 ++digit;
3241                 last_show_acs = 0;
3242             } else {
3243                 beep();
3244             }
3245             break;
3246         case '>':
3247             if (repeat < (COLS / 4))
3248                 ++repeat;
3249             break;
3250         case '<':
3251             if (repeat > 1)
3252                 --repeat;
3253             break;
3254         default:
3255             if (cycle_attr(c, &at_code, &attr)
3256                 || cycle_colors(c, &fg, &bg, &pair)) {
3257                 break;
3258             } else {
3259                 beep();
3260             }
3261             break;
3262         }
3263         if (last_show_acs != 0)
3264             last_show_acs(repeat, attr, pair);
3265         else
3266             show_upper_chars((unsigned) (digit * 32 + 128), repeat, attr, pair);
3267
3268         MvPrintw(LINES - 3, 0,
3269                  "Note: ANSI terminals may not display C1 characters.");
3270         MvPrintw(LINES - 2, 0,
3271                  "Select: a=ACS, x=box, %s0=C1, 1-3,+/- non-ASCII, </> repeat, ESC=quit",
3272                  pch_kludge);
3273         if (use_colors) {
3274             MvPrintw(LINES - 1, 0,
3275                      "v/V, f/F, b/B cycle through video attributes (%s) and color %d/%d.",
3276                      attrs_to_cycle[at_code].name,
3277                      fg, bg);
3278         } else {
3279             MvPrintw(LINES - 1, 0,
3280                      "v/V cycles through video attributes (%s).",
3281                      attrs_to_cycle[at_code].name);
3282         }
3283         refresh();
3284     } while (!isQuit(c = Getchar()));
3285
3286     Pause();
3287     erase();
3288     endwin();
3289 }
3290
3291 #if USE_WIDEC_SUPPORT
3292 static cchar_t *
3293 merge_wide_attr(cchar_t *dst, const cchar_t *src, attr_t attr, short pair)
3294 {
3295     int count;
3296
3297     *dst = *src;
3298     do {
3299         TEST_CCHAR(src, count, {
3300             attr |= (test_attrs & A_ALTCHARSET);
3301             setcchar(dst, test_wch, attr, pair, NULL);
3302         }
3303         , {
3304             ;
3305         });
3306     } while (0);
3307     return dst;
3308 }
3309
3310 static void
3311 show_upper_widechars(int first, int repeat, int space, attr_t attr, short pair)
3312 {
3313     cchar_t temp;
3314     wchar_t code;
3315     int last = first + 31;
3316
3317     erase();
3318     attron(A_BOLD);
3319     MvPrintw(0, 20, "Display of Character Codes %d to %d", first, last);
3320     attroff(A_BOLD);
3321
3322     for (code = first; (int) code <= last; code++) {
3323         int row = 2 + ((code - first) % 16);
3324         int col = ((code - first) / 16) * COLS / 2;
3325         wchar_t codes[10];
3326         char tmp[80];
3327         int count = repeat;
3328         int y, x;
3329
3330         memset(&codes, 0, sizeof(codes));
3331         codes[0] = code;
3332         sprintf(tmp, "%3ld (0x%lx)", (long) code, (long) code);
3333         MvPrintw(row, col, "%*s: ", COLS / 4, tmp);
3334         setcchar(&temp, codes, attr, pair, 0);
3335         do {
3336             /*
3337              * Give non-spacing characters something to combine with.  If we
3338              * don't, they'll bunch up in a heap on the space after the ":".
3339              * Mark them with reverse-video to make them simpler to find on
3340              * the display.
3341              */
3342             if (wcwidth(code) == 0)
3343                 addch(space | (A_REVERSE ^ attr) | COLOR_PAIR(pair));
3344             /*
3345              * This could use add_wch(), but is done for comparison with the
3346              * normal 'f' test (and to make a test-case for echo_wchar()).
3347              * The screen will flicker because the erase() at the top of the
3348              * function is met by the builtin refresh() in echo_wchar().
3349              */
3350             echo_wchar(&temp);
3351             /*
3352              * The repeat-count may make text wrap - avoid that.
3353              */
3354             getyx(stdscr, y, x);
3355             if (x >= col + (COLS / 2) - 2)
3356                 break;
3357         } while (--count > 0);
3358     }
3359 }
3360
3361 static int
3362 show_1_wacs(int n, int repeat, const char *name, const cchar_t *code)
3363 {
3364     const int height = 16;
3365     int row = 2 + (n % height);
3366     int col = (n / height) * COLS / 2;
3367
3368     MvPrintw(row, col, "%*s : ", COLS / 4, name);
3369     while (--repeat >= 0) {
3370         add_wch(code);
3371     }
3372     return n + 1;
3373 }
3374
3375 #define MERGE_ATTR(wch) merge_wide_attr(&temp, wch, attr, pair)
3376
3377 static void
3378 show_wacs_chars(int repeat, attr_t attr, short pair)
3379 /* display the wide-ACS character set */
3380 {
3381     cchar_t temp;
3382
3383     int n;
3384
3385 /*#define BOTH2(name) #name, &(name) */
3386 #define BOTH2(name) #name, MERGE_ATTR(name)
3387
3388     erase();
3389     attron(A_BOLD);
3390     MvAddStr(0, 20, "Display of the Wide-ACS Character Set");
3391     attroff(A_BOLD);
3392     refresh();
3393
3394     n = show_1_wacs(0, repeat, BOTH2(WACS_ULCORNER));
3395     n = show_1_wacs(n, repeat, BOTH2(WACS_URCORNER));
3396     n = show_1_wacs(n, repeat, BOTH2(WACS_LLCORNER));
3397     n = show_1_wacs(n, repeat, BOTH2(WACS_LRCORNER));
3398
3399     n = show_1_wacs(n, repeat, BOTH2(WACS_LTEE));
3400     n = show_1_wacs(n, repeat, BOTH2(WACS_RTEE));
3401     n = show_1_wacs(n, repeat, BOTH2(WACS_TTEE));
3402     n = show_1_wacs(n, repeat, BOTH2(WACS_BTEE));
3403
3404     n = show_1_wacs(n, repeat, BOTH2(WACS_HLINE));
3405     n = show_1_wacs(n, repeat, BOTH2(WACS_VLINE));
3406
3407     n = show_1_wacs(n, repeat, BOTH2(WACS_LARROW));
3408     n = show_1_wacs(n, repeat, BOTH2(WACS_RARROW));
3409     n = show_1_wacs(n, repeat, BOTH2(WACS_UARROW));
3410     n = show_1_wacs(n, repeat, BOTH2(WACS_DARROW));
3411
3412     n = show_1_wacs(n, repeat, BOTH2(WACS_BLOCK));
3413     n = show_1_wacs(n, repeat, BOTH2(WACS_BOARD));
3414     n = show_1_wacs(n, repeat, BOTH2(WACS_LANTERN));
3415     n = show_1_wacs(n, repeat, BOTH2(WACS_BULLET));
3416     n = show_1_wacs(n, repeat, BOTH2(WACS_CKBOARD));
3417     n = show_1_wacs(n, repeat, BOTH2(WACS_DEGREE));
3418     n = show_1_wacs(n, repeat, BOTH2(WACS_DIAMOND));
3419     n = show_1_wacs(n, repeat, BOTH2(WACS_PLMINUS));
3420     n = show_1_wacs(n, repeat, BOTH2(WACS_PLUS));
3421
3422 #ifdef CURSES_WACS_ARRAY
3423     n = show_1_wacs(n, repeat, BOTH2(WACS_GEQUAL));
3424     n = show_1_wacs(n, repeat, BOTH2(WACS_NEQUAL));
3425     n = show_1_wacs(n, repeat, BOTH2(WACS_LEQUAL));
3426
3427     n = show_1_wacs(n, repeat, BOTH2(WACS_STERLING));
3428     n = show_1_wacs(n, repeat, BOTH2(WACS_PI));
3429     n = show_1_wacs(n, repeat, BOTH2(WACS_S1));
3430     n = show_1_wacs(n, repeat, BOTH2(WACS_S3));
3431     n = show_1_wacs(n, repeat, BOTH2(WACS_S7));
3432     (void) show_1_wacs(n, repeat, BOTH2(WACS_S9));
3433 #endif
3434 }
3435
3436 #ifdef WACS_D_PLUS
3437 static void
3438 show_wacs_chars_double(int repeat, attr_t attr, short pair)
3439 /* display the wide-ACS character set */
3440 {
3441     cchar_t temp;
3442
3443     int n;
3444
3445 /*#define BOTH2(name) #name, &(name) */
3446 #define BOTH2(name) #name, MERGE_ATTR(name)
3447
3448     erase();
3449     attron(A_BOLD);
3450     MvAddStr(0, 20, "Display of the Wide-ACS Character Set");
3451     attroff(A_BOLD);
3452     refresh();
3453
3454     n = show_1_wacs(0, repeat, BOTH2(WACS_D_ULCORNER));
3455     n = show_1_wacs(n, repeat, BOTH2(WACS_D_URCORNER));
3456     n = show_1_wacs(n, repeat, BOTH2(WACS_D_LLCORNER));
3457     n = show_1_wacs(n, repeat, BOTH2(WACS_D_LRCORNER));
3458
3459     n = show_1_wacs(n, repeat, BOTH2(WACS_D_LTEE));
3460     n = show_1_wacs(n, repeat, BOTH2(WACS_D_RTEE));
3461     n = show_1_wacs(n, repeat, BOTH2(WACS_D_TTEE));
3462     n = show_1_wacs(n, repeat, BOTH2(WACS_D_BTEE));
3463
3464     n = show_1_wacs(n, repeat, BOTH2(WACS_D_HLINE));
3465     n = show_1_wacs(n, repeat, BOTH2(WACS_D_VLINE));
3466
3467     n = show_1_wacs(n, repeat, BOTH2(WACS_LARROW));
3468     n = show_1_wacs(n, repeat, BOTH2(WACS_RARROW));
3469     n = show_1_wacs(n, repeat, BOTH2(WACS_UARROW));
3470     n = show_1_wacs(n, repeat, BOTH2(WACS_DARROW));
3471
3472     n = show_1_wacs(n, repeat, BOTH2(WACS_BLOCK));
3473     n = show_1_wacs(n, repeat, BOTH2(WACS_BOARD));
3474     n = show_1_wacs(n, repeat, BOTH2(WACS_LANTERN));
3475     n = show_1_wacs(n, repeat, BOTH2(WACS_BULLET));
3476     n = show_1_wacs(n, repeat, BOTH2(WACS_CKBOARD));
3477     n = show_1_wacs(n, repeat, BOTH2(WACS_DEGREE));
3478     n = show_1_wacs(n, repeat, BOTH2(WACS_DIAMOND));
3479     n = show_1_wacs(n, repeat, BOTH2(WACS_PLMINUS));
3480     n = show_1_wacs(n, repeat, BOTH2(WACS_PLUS));
3481
3482 #ifdef CURSES_WACS_ARRAY
3483     n = show_1_wacs(n, repeat, BOTH2(WACS_GEQUAL));
3484     n = show_1_wacs(n, repeat, BOTH2(WACS_NEQUAL));
3485     n = show_1_wacs(n, repeat, BOTH2(WACS_LEQUAL));
3486
3487     n = show_1_wacs(n, repeat, BOTH2(WACS_STERLING));
3488     n = show_1_wacs(n, repeat, BOTH2(WACS_PI));
3489     n = show_1_wacs(n, repeat, BOTH2(WACS_S1));
3490     n = show_1_wacs(n, repeat, BOTH2(WACS_S3));
3491     n = show_1_wacs(n, repeat, BOTH2(WACS_S7));
3492     (void) show_1_wacs(n, repeat, BOTH2(WACS_S9));
3493 #endif
3494 }
3495 #endif
3496
3497 #ifdef WACS_T_PLUS
3498 static void
3499 show_wacs_chars_thick(int repeat, attr_t attr, short pair)
3500 /* display the wide-ACS character set */
3501 {
3502     cchar_t temp;
3503
3504     int n;
3505
3506 /*#define BOTH2(name) #name, &(name) */
3507 #define BOTH2(name) #name, MERGE_ATTR(name)
3508
3509     erase();
3510     attron(A_BOLD);
3511     MvAddStr(0, 20, "Display of the Wide-ACS Character Set");
3512     attroff(A_BOLD);
3513     refresh();
3514
3515     n = show_1_wacs(0, repeat, BOTH2(WACS_T_ULCORNER));
3516     n = show_1_wacs(n, repeat, BOTH2(WACS_T_URCORNER));
3517     n = show_1_wacs(n, repeat, BOTH2(WACS_T_LLCORNER));
3518     n = show_1_wacs(n, repeat, BOTH2(WACS_T_LRCORNER));
3519
3520     n = show_1_wacs(n, repeat, BOTH2(WACS_T_LTEE));
3521     n = show_1_wacs(n, repeat, BOTH2(WACS_T_RTEE));
3522     n = show_1_wacs(n, repeat, BOTH2(WACS_T_TTEE));
3523     n = show_1_wacs(n, repeat, BOTH2(WACS_T_BTEE));
3524
3525     n = show_1_wacs(n, repeat, BOTH2(WACS_T_HLINE));
3526     n = show_1_wacs(n, repeat, BOTH2(WACS_T_VLINE));
3527
3528     n = show_1_wacs(n, repeat, BOTH2(WACS_LARROW));
3529     n = show_1_wacs(n, repeat, BOTH2(WACS_RARROW));
3530     n = show_1_wacs(n, repeat, BOTH2(WACS_UARROW));
3531     n = show_1_wacs(n, repeat, BOTH2(WACS_DARROW));
3532
3533     n = show_1_wacs(n, repeat, BOTH2(WACS_BLOCK));
3534     n = show_1_wacs(n, repeat, BOTH2(WACS_BOARD));
3535     n = show_1_wacs(n, repeat, BOTH2(WACS_LANTERN));
3536     n = show_1_wacs(n, repeat, BOTH2(WACS_BULLET));
3537     n = show_1_wacs(n, repeat, BOTH2(WACS_CKBOARD));
3538     n = show_1_wacs(n, repeat, BOTH2(WACS_DEGREE));
3539     n = show_1_wacs(n, repeat, BOTH2(WACS_DIAMOND));
3540     n = show_1_wacs(n, repeat, BOTH2(WACS_PLMINUS));
3541     n = show_1_wacs(n, repeat, BOTH2(WACS_PLUS));
3542
3543 #ifdef CURSES_WACS_ARRAY
3544     n = show_1_wacs(n, repeat, BOTH2(WACS_GEQUAL));
3545     n = show_1_wacs(n, repeat, BOTH2(WACS_NEQUAL));
3546     n = show_1_wacs(n, repeat, BOTH2(WACS_LEQUAL));
3547
3548     n = show_1_wacs(n, repeat, BOTH2(WACS_STERLING));
3549     n = show_1_wacs(n, repeat, BOTH2(WACS_PI));
3550     n = show_1_wacs(n, repeat, BOTH2(WACS_S1));
3551     n = show_1_wacs(n, repeat, BOTH2(WACS_S3));
3552     n = show_1_wacs(n, repeat, BOTH2(WACS_S7));
3553     (void) show_1_wacs(n, repeat, BOTH2(WACS_S9));
3554 #endif
3555 }
3556 #endif
3557
3558 #undef MERGE_ATTR
3559
3560 #define MERGE_ATTR(n,wch) merge_wide_attr(&temp[n], wch, attr, pair)
3561
3562 static void
3563 show_wbox_chars(int repeat, attr_t attr, short pair)
3564 {
3565     cchar_t temp[8];
3566
3567     (void) repeat;
3568     erase();
3569     attron(A_BOLD);
3570     MvAddStr(0, 20, "Display of the Wide-ACS Line-Drawing Set");
3571     attroff(A_BOLD);
3572     refresh();
3573
3574     wborder_set(stdscr,
3575                 MERGE_ATTR(0, WACS_VLINE),
3576                 MERGE_ATTR(1, WACS_VLINE),
3577                 MERGE_ATTR(2, WACS_HLINE),
3578                 MERGE_ATTR(3, WACS_HLINE),
3579                 MERGE_ATTR(4, WACS_ULCORNER),
3580                 MERGE_ATTR(5, WACS_URCORNER),
3581                 MERGE_ATTR(6, WACS_LLCORNER),
3582                 MERGE_ATTR(7, WACS_LRCORNER));
3583     /* *INDENT-OFF* */
3584     (void) mvhline_set(LINES / 2, 0,        MERGE_ATTR(0, WACS_HLINE), COLS);
3585     (void) mvvline_set(0,         COLS / 2, MERGE_ATTR(0, WACS_VLINE), LINES);
3586     (void) mvadd_wch(0,           COLS / 2, MERGE_ATTR(0, WACS_TTEE));
3587     (void) mvadd_wch(LINES / 2,   COLS / 2, MERGE_ATTR(0, WACS_PLUS));
3588     (void) mvadd_wch(LINES - 1,   COLS / 2, MERGE_ATTR(0, WACS_BTEE));
3589     (void) mvadd_wch(LINES / 2,   0,        MERGE_ATTR(0, WACS_LTEE));
3590     (void) mvadd_wch(LINES / 2,   COLS - 1, MERGE_ATTR(0, WACS_RTEE));
3591     /* *INDENT-ON* */
3592
3593 }
3594
3595 #undef MERGE_ATTR
3596
3597 static int
3598 show_2_wacs(int n, const char *name, const char *code, attr_t attr, short pair)
3599 {
3600     const int height = 16;
3601     int row = 2 + (n % height);
3602     int col = (n / height) * COLS / 2;
3603     char temp[80];
3604
3605     MvPrintw(row, col, "%*s : ", COLS / 4, name);
3606     (void) attr_set(attr, pair, 0);
3607     addstr(strcpy(temp, code));
3608     (void) attr_set(A_NORMAL, 0, 0);
3609     return n + 1;
3610 }
3611
3612 #define SHOW_UTF8(n, name, code) show_2_wacs(n, name, code, attr, pair)
3613
3614 static void
3615 show_utf8_chars(int repeat, attr_t attr, short pair)
3616 {
3617     int n;
3618
3619     (void) repeat;
3620     erase();
3621     attron(A_BOLD);
3622     MvAddStr(0, 20, "Display of the Wide-ACS Character Set");
3623     attroff(A_BOLD);
3624     refresh();
3625     /* *INDENT-OFF* */
3626     n = SHOW_UTF8(0, "WACS_ULCORNER",   "\342\224\214");
3627     n = SHOW_UTF8(n, "WACS_URCORNER",   "\342\224\220");
3628     n = SHOW_UTF8(n, "WACS_LLCORNER",   "\342\224\224");
3629     n = SHOW_UTF8(n, "WACS_LRCORNER",   "\342\224\230");
3630
3631     n = SHOW_UTF8(n, "WACS_LTEE",       "\342\224\234");
3632     n = SHOW_UTF8(n, "WACS_RTEE",       "\342\224\244");
3633     n = SHOW_UTF8(n, "WACS_TTEE",       "\342\224\254");
3634     n = SHOW_UTF8(n, "WACS_BTEE",       "\342\224\264");
3635
3636     n = SHOW_UTF8(n, "WACS_HLINE",      "\342\224\200");
3637     n = SHOW_UTF8(n, "WACS_VLINE",      "\342\224\202");
3638
3639     n = SHOW_UTF8(n, "WACS_LARROW",     "\342\206\220");
3640     n = SHOW_UTF8(n, "WACS_RARROW",     "\342\206\222");
3641     n = SHOW_UTF8(n, "WACS_UARROW",     "\342\206\221");
3642     n = SHOW_UTF8(n, "WACS_DARROW",     "\342\206\223");
3643
3644     n = SHOW_UTF8(n, "WACS_BLOCK",      "\342\226\256");
3645     n = SHOW_UTF8(n, "WACS_BOARD",      "\342\226\222");
3646     n = SHOW_UTF8(n, "WACS_LANTERN",    "\342\230\203");
3647     n = SHOW_UTF8(n, "WACS_BULLET",     "\302\267");
3648     n = SHOW_UTF8(n, "WACS_CKBOARD",    "\342\226\222");
3649     n = SHOW_UTF8(n, "WACS_DEGREE",     "\302\260");
3650     n = SHOW_UTF8(n, "WACS_DIAMOND",    "\342\227\206");
3651     n = SHOW_UTF8(n, "WACS_PLMINUS",    "\302\261");
3652     n = SHOW_UTF8(n, "WACS_PLUS",       "\342\224\274");
3653     n = SHOW_UTF8(n, "WACS_GEQUAL",     "\342\211\245");
3654     n = SHOW_UTF8(n, "WACS_NEQUAL",     "\342\211\240");
3655     n = SHOW_UTF8(n, "WACS_LEQUAL",     "\342\211\244");
3656
3657     n = SHOW_UTF8(n, "WACS_STERLING",   "\302\243");
3658     n = SHOW_UTF8(n, "WACS_PI",         "\317\200");
3659     n = SHOW_UTF8(n, "WACS_S1",         "\342\216\272");
3660     n = SHOW_UTF8(n, "WACS_S3",         "\342\216\273");
3661     n = SHOW_UTF8(n, "WACS_S7",         "\342\216\274");
3662     (void) SHOW_UTF8(n, "WACS_S9",      "\342\216\275");
3663     /* *INDENT-ON* */
3664
3665 }
3666
3667 /* display the wide-ACS character set */
3668 static void
3669 wide_acs_display(void)
3670 {
3671     int c = 'a';
3672     int digit = 0;
3673     int repeat = 1;
3674     int space = ' ';
3675     chtype attr = A_NORMAL;
3676     int fg = COLOR_BLACK;
3677     int bg = COLOR_BLACK;
3678     unsigned at_code = 0;
3679     short pair = 0;
3680     void (*last_show_wacs) (int, attr_t, short) = 0;
3681
3682     do {
3683         switch (c) {
3684         case CTRL('L'):
3685             Repaint();
3686             break;
3687         case 'a':
3688             ToggleAcs(last_show_wacs, show_wacs_chars);
3689             break;
3690 #ifdef WACS_D_PLUS
3691         case 'd':
3692             ToggleAcs(last_show_wacs, show_wacs_chars_double);
3693             break;
3694 #endif
3695 #ifdef WACS_T_PLUS
3696         case 't':
3697             ToggleAcs(last_show_wacs, show_wacs_chars_thick);
3698             break;
3699 #endif
3700         case 'x':
3701             ToggleAcs(last_show_wacs, show_wbox_chars);
3702             break;
3703         case 'u':
3704             ToggleAcs(last_show_wacs, show_utf8_chars);
3705             break;
3706         default:
3707             if (c < 256 && isdigit(c)) {
3708                 digit = (c - '0');
3709                 last_show_wacs = 0;
3710             } else if (c == '+') {
3711                 ++digit;
3712                 last_show_wacs = 0;
3713             } else if (c == '-' && digit > 0) {
3714                 --digit;
3715                 last_show_wacs = 0;
3716             } else if (c == '>' && repeat < (COLS / 4)) {
3717                 ++repeat;
3718             } else if (c == '<' && repeat > 1) {
3719                 --repeat;
3720             } else if (c == '_') {
3721                 space = (space == ' ') ? '_' : ' ';
3722                 last_show_wacs = 0;
3723             } else if (cycle_attr(c, &at_code, &attr)
3724                        || cycle_colors(c, &fg, &bg, &pair)) {
3725                 if (last_show_wacs != 0)
3726                     break;
3727             } else {
3728                 beep();
3729                 break;
3730             }
3731             break;
3732         }
3733         if (last_show_wacs != 0)
3734             last_show_wacs(repeat, attr, pair);
3735         else
3736             show_upper_widechars(digit * 32 + 128, repeat, space, attr, pair);
3737
3738         MvPrintw(LINES - 3, 0,
3739                  "Select: a/d/t WACS, x box, u UTF-8, 0-9,+/- non-ASCII, </> repeat, ESC=quit");
3740         if (use_colors) {
3741             MvPrintw(LINES - 2, 0,
3742                      "v/V, f/F, b/B cycle through video attributes (%s) and color %d/%d.",
3743                      attrs_to_cycle[at_code].name,
3744                      fg, bg);
3745         } else {
3746             MvPrintw(LINES - 2, 0,
3747                      "v/V cycles through video attributes (%s).",
3748                      attrs_to_cycle[at_code].name);
3749         }
3750         refresh();
3751     } while (!isQuit(c = Getchar()));
3752
3753     Pause();
3754     erase();
3755     endwin();
3756 }
3757
3758 #endif
3759
3760 /*
3761  * Graphic-rendition test (adapted from vttest)
3762  */
3763 static void
3764 test_sgr_attributes(void)
3765 {
3766     int pass;
3767
3768     for (pass = 0; pass < 2; pass++) {
3769         chtype normal = ((pass == 0 ? A_NORMAL : A_REVERSE)) | BLANK;
3770
3771         /* Use non-default colors if possible to exercise bce a little */
3772         if (use_colors) {
3773             init_pair(1, COLOR_WHITE, COLOR_BLUE);
3774             normal |= COLOR_PAIR(1);
3775         }
3776         bkgdset(normal);
3777         erase();
3778         MvPrintw(1, 20, "Graphic rendition test pattern:");
3779
3780         MvPrintw(4, 1, "vanilla");
3781
3782 #define set_sgr(mask) bkgdset((normal^(mask)));
3783         set_sgr(A_BOLD);
3784         MvPrintw(4, 40, "bold");
3785
3786         set_sgr(A_UNDERLINE);
3787         MvPrintw(6, 6, "underline");
3788
3789         set_sgr(A_BOLD | A_UNDERLINE);
3790         MvPrintw(6, 45, "bold underline");
3791
3792         set_sgr(A_BLINK);
3793         MvPrintw(8, 1, "blink");
3794
3795         set_sgr(A_BLINK | A_BOLD);
3796         MvPrintw(8, 40, "bold blink");
3797
3798         set_sgr(A_UNDERLINE | A_BLINK);
3799         MvPrintw(10, 6, "underline blink");
3800
3801         set_sgr(A_BOLD | A_UNDERLINE | A_BLINK);
3802         MvPrintw(10, 45, "bold underline blink");
3803
3804         set_sgr(A_REVERSE);
3805         MvPrintw(12, 1, "negative");
3806
3807         set_sgr(A_BOLD | A_REVERSE);
3808         MvPrintw(12, 40, "bold negative");
3809
3810         set_sgr(A_UNDERLINE | A_REVERSE);
3811         MvPrintw(14, 6, "underline negative");
3812
3813         set_sgr(A_BOLD | A_UNDERLINE | A_REVERSE);
3814         MvPrintw(14, 45, "bold underline negative");
3815
3816         set_sgr(A_BLINK | A_REVERSE);
3817         MvPrintw(16, 1, "blink negative");
3818
3819         set_sgr(A_BOLD | A_BLINK | A_REVERSE);
3820         MvPrintw(16, 40, "bold blink negative");
3821
3822         set_sgr(A_UNDERLINE | A_BLINK | A_REVERSE);
3823         MvPrintw(18, 6, "underline blink negative");
3824
3825         set_sgr(A_BOLD | A_UNDERLINE | A_BLINK | A_REVERSE);
3826         MvPrintw(18, 45, "bold underline blink negative");
3827
3828         bkgdset(normal);
3829         MvPrintw(LINES - 2, 1, "%s background. ", pass == 0 ? "Dark" :
3830                  "Light");
3831         clrtoeol();
3832         Pause();
3833     }
3834
3835     bkgdset(A_NORMAL | BLANK);
3836     erase();
3837     endwin();
3838 }
3839
3840 /****************************************************************************
3841  *
3842  * Windows and scrolling tester.
3843  *
3844  ****************************************************************************/
3845
3846 #define BOTLINES        4       /* number of line stolen from screen bottom */
3847
3848 typedef struct {
3849     int y, x;
3850 } pair;
3851
3852 #define FRAME struct frame
3853 FRAME
3854 {
3855     FRAME *next, *last;
3856     bool do_scroll;
3857     bool do_keypad;
3858     WINDOW *wind;
3859 };
3860
3861 #if defined(NCURSES_VERSION)
3862 #if (NCURSES_VERSION_PATCH < 20070331) && NCURSES_EXT_FUNCS
3863 #define is_keypad(win)   (win)->_use_keypad
3864 #define is_scrollok(win) (win)->_scroll
3865 #elif !defined(is_keypad)
3866 #define is_keypad(win)   FALSE
3867 #define is_scrollok(win) FALSE
3868 #endif
3869 #else
3870 #define is_keypad(win)   FALSE
3871 #define is_scrollok(win) FALSE
3872 #endif
3873
3874 static WINDOW *
3875 frame_win(FRAME * curp)
3876 {
3877     return (curp != 0) ? curp->wind : stdscr;
3878 }
3879
3880 /* We need to know if these flags are actually set, so don't look in FRAME.
3881  * These names are known to work with SVr4 curses as well as ncurses.  The
3882  * _use_keypad name does not work with Solaris 8.
3883  */
3884 static bool
3885 HaveKeypad(FRAME * curp)
3886 {
3887     WINDOW *win = frame_win(curp);
3888     (void) win;
3889     return is_keypad(win);
3890 }
3891
3892 static bool
3893 HaveScroll(FRAME * curp)
3894 {
3895     WINDOW *win = frame_win(curp);
3896     (void) win;
3897     return is_scrollok(win);
3898 }
3899
3900 static void
3901 newwin_legend(FRAME * curp)
3902 {
3903     static const struct {
3904         const char *msg;
3905         int code;
3906     } legend[] = {
3907         {
3908             "^C = create window", 0
3909         },
3910         {
3911             "^N = next window", 0
3912         },
3913         {
3914             "^P = previous window", 0
3915         },
3916         {
3917             "^F = scroll forward", 0
3918         },
3919         {
3920             "^B = scroll backward", 0
3921         },
3922         {
3923             "^K = keypad(%s)", 1
3924         },
3925         {
3926             "^S = scrollok(%s)", 2
3927         },
3928         {
3929             "^W = save window to file", 0
3930         },
3931         {
3932             "^R = restore window", 0
3933         },
3934 #if HAVE_WRESIZE
3935         {
3936             "^X = resize", 0
3937         },
3938 #endif
3939         {
3940             "^Q%s = exit", 3
3941         }
3942     };
3943     size_t n;
3944     int x;
3945     bool do_keypad = HaveKeypad(curp);
3946     bool do_scroll = HaveScroll(curp);
3947     char buf[BUFSIZ];
3948
3949     move(LINES - 4, 0);
3950     for (n = 0; n < SIZEOF(legend); n++) {
3951         switch (legend[n].code) {
3952         default:
3953             strcpy(buf, legend[n].msg);
3954             break;
3955         case 1:
3956             sprintf(buf, legend[n].msg, do_keypad ? "yes" : "no");
3957             break;
3958         case 2:
3959             sprintf(buf, legend[n].msg, do_scroll ? "yes" : "no");
3960             break;
3961         case 3:
3962             sprintf(buf, legend[n].msg, do_keypad ? "/ESC" : "");
3963             break;
3964         }
3965         x = getcurx(stdscr);
3966         addstr((COLS < (x + 3 + (int) strlen(buf))) ? "\n" : (n ? ", " : ""));
3967         addstr(buf);
3968     }
3969     clrtoeol();
3970 }
3971
3972 static void
3973 transient(FRAME * curp, NCURSES_CONST char *msg)
3974 {
3975     newwin_legend(curp);
3976     if (msg) {
3977         MvAddStr(LINES - 1, 0, msg);
3978         refresh();
3979         napms(1000);
3980     }
3981
3982     move(LINES - 1, 0);
3983     printw("%s characters are echoed, window should %sscroll.",
3984            HaveKeypad(curp) ? "Non-arrow" : "All other",
3985            HaveScroll(curp) ? "" : "not ");
3986     clrtoeol();
3987 }
3988
3989 static void
3990 newwin_report(FRAME * curp)
3991 /* report on the cursor's current position, then restore it */
3992 {
3993     WINDOW *win = frame_win(curp);
3994     int y, x;
3995
3996     if (win != stdscr)
3997         transient(curp, (char *) 0);
3998     getyx(win, y, x);
3999     move(LINES - 1, COLS - 17);
4000     printw("Y = %2d X = %2d", y, x);
4001     if (win != stdscr)
4002         refresh();
4003     else
4004         wmove(win, y, x);
4005 }
4006
4007 static pair *
4008 selectcell(int uli, int ulj, int lri, int lrj)
4009 /* arrows keys move cursor, return location at current on non-arrow key */
4010 {
4011     static pair res;            /* result cell */
4012     int si = lri - uli + 1;     /* depth of the select area */
4013     int sj = lrj - ulj + 1;     /* width of the select area */
4014     int i = 0, j = 0;           /* offsets into the select area */
4015
4016     res.y = uli;
4017     res.x = ulj;
4018     for (;;) {
4019         move(uli + i, ulj + j);
4020         newwin_report((FRAME *) 0);
4021
4022         switch (Getchar()) {
4023         case KEY_UP:
4024             i += si - 1;
4025             break;
4026         case KEY_DOWN:
4027             i++;
4028             break;
4029         case KEY_LEFT:
4030             j += sj - 1;
4031             break;
4032         case KEY_RIGHT:
4033             j++;
4034             break;
4035         case case_QUIT:
4036             return ((pair *) 0);
4037 #ifdef NCURSES_MOUSE_VERSION
4038         case KEY_MOUSE:
4039             {
4040                 MEVENT event;
4041
4042                 getmouse(&event);
4043                 if (event.y > uli && event.x > ulj) {
4044                     i = event.y - uli;
4045                     j = event.x - ulj;
4046                 } else {
4047                     beep();
4048                     break;
4049                 }
4050             }
4051             /* FALLTHRU */
4052 #endif
4053         default:
4054             res.y = uli + i;
4055             res.x = ulj + j;
4056             return (&res);
4057         }
4058         i %= si;
4059         j %= sj;
4060     }
4061 }
4062
4063 static void
4064 outerbox(pair ul, pair lr, bool onoff)
4065 /* draw or erase a box *outside* the given pair of corners */
4066 {
4067     MvAddCh(ul.y - 1, lr.x - 1, onoff ? ACS_ULCORNER : ' ');
4068     MvAddCh(ul.y - 1, lr.x + 1, onoff ? ACS_URCORNER : ' ');
4069     MvAddCh(lr.y + 1, lr.x + 1, onoff ? ACS_LRCORNER : ' ');
4070     MvAddCh(lr.y + 1, ul.x - 1, onoff ? ACS_LLCORNER : ' ');
4071     move(ul.y - 1, ul.x);
4072     hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
4073     move(ul.y, ul.x - 1);
4074     vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
4075     move(lr.y + 1, ul.x);
4076     hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
4077     move(ul.y, lr.x + 1);
4078     vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
4079 }
4080
4081 static WINDOW *
4082 getwindow(void)
4083 /* Ask user for a window definition */
4084 {
4085     WINDOW *rwindow;
4086     pair ul, lr, *tmp;
4087
4088     move(0, 0);
4089     clrtoeol();
4090     addstr("Use arrows to move cursor, anything else to mark corner 1");
4091     refresh();
4092     if ((tmp = selectcell(2, 1, LINES - BOTLINES - 2, COLS - 2)) == (pair *) 0)
4093         return ((WINDOW *) 0);
4094     memcpy(&ul, tmp, sizeof(pair));
4095     MvAddCh(ul.y - 1, ul.x - 1, ACS_ULCORNER);
4096     move(0, 0);
4097     clrtoeol();
4098     addstr("Use arrows to move cursor, anything else to mark corner 2");
4099     refresh();
4100     if ((tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2)) ==
4101         (pair *) 0)
4102         return ((WINDOW *) 0);
4103     memcpy(&lr, tmp, sizeof(pair));
4104
4105     rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
4106
4107     outerbox(ul, lr, TRUE);
4108     refresh();
4109
4110     wrefresh(rwindow);
4111
4112     move(0, 0);
4113     clrtoeol();
4114     return (rwindow);
4115 }
4116
4117 static void
4118 newwin_move(FRAME * curp, int dy, int dx)
4119 {
4120     WINDOW *win = frame_win(curp);
4121     int cur_y, cur_x;
4122     int max_y, max_x;
4123
4124     getyx(win, cur_y, cur_x);
4125     getmaxyx(win, max_y, max_x);
4126     if ((cur_x += dx) < 0)
4127         cur_x = 0;
4128     else if (cur_x >= max_x)
4129         cur_x = max_x - 1;
4130     if ((cur_y += dy) < 0)
4131         cur_y = 0;
4132     else if (cur_y >= max_y)
4133         cur_y = max_y - 1;
4134     wmove(win, cur_y, cur_x);
4135 }
4136
4137 static FRAME *
4138 delete_framed(FRAME * fp, bool showit)
4139 {
4140     FRAME *np = 0;
4141
4142     if (fp != 0) {
4143         fp->last->next = fp->next;
4144         fp->next->last = fp->last;
4145
4146         if (showit) {
4147             werase(fp->wind);
4148             wrefresh(fp->wind);
4149         }
4150         delwin(fp->wind);
4151
4152         np = (fp == fp->next) ? 0 : fp->next;
4153         free(fp);
4154     }
4155     return np;
4156 }
4157
4158 static void
4159 acs_and_scroll(void)
4160 /* Demonstrate windows */
4161 {
4162     int c;
4163     FRAME *current = (FRAME *) 0, *neww;
4164     WINDOW *usescr;
4165 #if HAVE_PUTWIN && HAVE_GETWIN
4166     FILE *fp;
4167 #endif
4168
4169 #define DUMPFILE        "screendump"
4170
4171 #ifdef NCURSES_MOUSE_VERSION
4172     mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
4173 #endif
4174     c = CTRL('C');
4175     raw();
4176     do {
4177         transient((FRAME *) 0, (char *) 0);
4178         switch (c) {
4179         case CTRL('C'):
4180             if ((neww = typeCalloc(FRAME, 1)) == 0) {
4181                 goto breakout;
4182             }
4183             if ((neww->wind = getwindow()) == (WINDOW *) 0) {
4184                 free(neww);
4185                 goto breakout;
4186             }
4187
4188             if (current == 0) { /* First element,  */
4189                 neww->next = neww;      /*   so point it at itself */
4190                 neww->last = neww;
4191             } else {
4192                 neww->next = current->next;
4193                 neww->last = current;
4194                 neww->last->next = neww;
4195                 neww->next->last = neww;
4196             }
4197             current = neww;
4198             /* SVr4 curses sets the keypad on all newly-created windows to
4199              * false.  Someone reported that PDCurses makes new windows inherit
4200              * this flag.  Remove the following 'keypad()' call to test this
4201              */
4202             keypad(current->wind, TRUE);
4203             current->do_keypad = HaveKeypad(current);
4204             current->do_scroll = HaveScroll(current);
4205             break;
4206
4207         case CTRL('N'): /* go to next window */
4208             if (current)
4209                 current = current->next;
4210             break;
4211
4212         case CTRL('P'): /* go to previous window */
4213             if (current)
4214                 current = current->last;
4215             break;
4216
4217         case CTRL('F'): /* scroll current window forward */
4218             if (current)
4219                 wscrl(frame_win(current), 1);
4220             break;
4221
4222         case CTRL('B'): /* scroll current window backwards */
4223             if (current)
4224                 wscrl(frame_win(current), -1);
4225             break;
4226
4227         case CTRL('K'): /* toggle keypad mode for current */
4228             if (current) {
4229                 current->do_keypad = !current->do_keypad;
4230                 keypad(current->wind, current->do_keypad);
4231             }
4232             break;
4233
4234         case CTRL('S'):
4235             if (current) {
4236                 current->do_scroll = !current->do_scroll;
4237                 scrollok(current->wind, current->do_scroll);
4238             }
4239             break;
4240
4241 #if HAVE_PUTWIN && HAVE_GETWIN
4242         case CTRL('W'): /* save and delete window */
4243             if ((current != 0) && (current == current->next)) {
4244                 transient(current, "Will not save/delete ONLY window");
4245                 break;
4246             } else if ((fp = fopen(DUMPFILE, "w")) == (FILE *) 0) {
4247                 transient(current, "Can't open screen dump file");
4248             } else {
4249                 (void) putwin(frame_win(current), fp);
4250                 (void) fclose(fp);
4251
4252                 current = delete_framed(current, TRUE);
4253             }
4254             break;
4255
4256         case CTRL('R'): /* restore window */
4257             if ((fp = fopen(DUMPFILE, "r")) == (FILE *) 0) {
4258                 transient(current, "Can't open screen dump file");
4259             } else {
4260                 if ((neww = typeCalloc(FRAME, 1)) != 0) {
4261
4262                     neww->next = current ? current->next : 0;
4263                     neww->last = current;
4264                     neww->last->next = neww;
4265                     neww->next->last = neww;
4266
4267                     neww->wind = getwin(fp);
4268
4269                     wrefresh(neww->wind);
4270                 }
4271                 (void) fclose(fp);
4272             }
4273             break;
4274 #endif
4275
4276 #if HAVE_WRESIZE
4277         case CTRL('X'): /* resize window */
4278             if (current) {
4279                 pair *tmp, ul, lr;
4280                 int i, mx, my;
4281
4282                 move(0, 0);
4283                 clrtoeol();
4284                 addstr("Use arrows to move cursor, anything else to mark new corner");
4285                 refresh();
4286
4287                 getbegyx(current->wind, ul.y, ul.x);
4288
4289                 tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2);
4290                 if (tmp == (pair *) 0) {
4291                     beep();
4292                     break;
4293                 }
4294
4295                 getmaxyx(current->wind, lr.y, lr.x);
4296                 lr.y += (ul.y - 1);
4297                 lr.x += (ul.x - 1);
4298                 outerbox(ul, lr, FALSE);
4299                 wnoutrefresh(stdscr);
4300
4301                 /* strictly cosmetic hack for the test */
4302                 getmaxyx(current->wind, my, mx);
4303                 if (my > tmp->y - ul.y) {
4304                     getyx(current->wind, lr.y, lr.x);
4305                     wmove(current->wind, tmp->y - ul.y + 1, 0);
4306                     wclrtobot(current->wind);
4307                     wmove(current->wind, lr.y, lr.x);
4308                 }
4309                 if (mx > tmp->x - ul.x)
4310                     for (i = 0; i < my; i++) {
4311                         wmove(current->wind, i, tmp->x - ul.x + 1);
4312                         wclrtoeol(current->wind);
4313                     }
4314                 wnoutrefresh(current->wind);
4315
4316                 memcpy(&lr, tmp, sizeof(pair));
4317                 (void) wresize(current->wind, lr.y - ul.y + 0, lr.x - ul.x + 0);
4318
4319                 getbegyx(current->wind, ul.y, ul.x);
4320                 getmaxyx(current->wind, lr.y, lr.x);
4321                 lr.y += (ul.y - 1);
4322                 lr.x += (ul.x - 1);
4323                 outerbox(ul, lr, TRUE);
4324                 wnoutrefresh(stdscr);
4325
4326                 wnoutrefresh(current->wind);
4327                 move(0, 0);
4328                 clrtoeol();
4329                 doupdate();
4330             }
4331             break;
4332 #endif /* HAVE_WRESIZE */
4333
4334         case KEY_F(10): /* undocumented --- use this to test area clears */
4335             selectcell(0, 0, LINES - 1, COLS - 1);
4336             clrtobot();
4337             refresh();
4338             break;
4339
4340         case KEY_UP:
4341             newwin_move(current, -1, 0);
4342             break;
4343         case KEY_DOWN:
4344             newwin_move(current, 1, 0);
4345             break;
4346         case KEY_LEFT:
4347             newwin_move(current, 0, -1);
4348             break;
4349         case KEY_RIGHT:
4350             newwin_move(current, 0, 1);
4351             break;
4352
4353         case KEY_BACKSPACE:
4354             /* FALLTHROUGH */
4355         case KEY_DC:
4356             {
4357                 int y, x;
4358                 getyx(frame_win(current), y, x);
4359                 if (--x < 0) {
4360                     if (--y < 0)
4361                         break;
4362                     x = getmaxx(frame_win(current)) - 1;
4363                 }
4364                 (void) mvwdelch(frame_win(current), y, x);
4365             }
4366             break;
4367
4368         case '\r':
4369             c = '\n';
4370             /* FALLTHROUGH */
4371
4372         default:
4373             if (current)
4374                 waddch(current->wind, (chtype) c);
4375             else
4376                 beep();
4377             break;
4378         }
4379         newwin_report(current);
4380         usescr = frame_win(current);
4381         wrefresh(usescr);
4382     } while
4383         (!isQuit(c = wGetchar(usescr))
4384          && (c != ERR));
4385
4386   breakout:
4387     while (current != 0)
4388         current = delete_framed(current, FALSE);
4389
4390     scrollok(stdscr, TRUE);     /* reset to driver's default */
4391 #ifdef NCURSES_MOUSE_VERSION
4392     mousemask(0, (mmask_t *) 0);
4393 #endif
4394     noraw();
4395     erase();
4396     endwin();
4397 }
4398
4399 /****************************************************************************
4400  *
4401  * Panels tester
4402  *
4403  ****************************************************************************/
4404
4405 #if USE_LIBPANEL
4406 static int nap_msec = 1;
4407
4408 static NCURSES_CONST char *mod[] =
4409 {
4410     "test ",
4411     "TEST ",
4412     "(**) ",
4413     "*()* ",
4414     "<--> ",
4415     "LAST "
4416 };
4417
4418 /*+-------------------------------------------------------------------------
4419         wait_a_while(msec)
4420 --------------------------------------------------------------------------*/
4421 static void
4422 wait_a_while(int msec GCC_UNUSED)
4423 {
4424 #if HAVE_NAPMS
4425     if (nap_msec == 1)
4426         wGetchar(stdscr);
4427     else
4428         napms(nap_msec);
4429 #else
4430     if (nap_msec == 1)
4431         wGetchar(stdscr);
4432     else if (msec > 1000)
4433         sleep((unsigned) msec / 1000);
4434     else
4435         sleep(1);
4436 #endif
4437 }                               /* end of wait_a_while */
4438
4439 /*+-------------------------------------------------------------------------
4440         saywhat(text)
4441 --------------------------------------------------------------------------*/
4442 static void
4443 saywhat(NCURSES_CONST char *text)
4444 {
4445     wmove(stdscr, LINES - 1, 0);
4446     wclrtoeol(stdscr);
4447     if (text != 0 && *text != '\0') {
4448         waddstr(stdscr, text);
4449         waddstr(stdscr, "; ");
4450     }
4451     waddstr(stdscr, "press any key to continue");
4452 }                               /* end of saywhat */
4453
4454 /*+-------------------------------------------------------------------------
4455         mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
4456 --------------------------------------------------------------------------*/
4457 static PANEL *
4458 mkpanel(short color, int rows, int cols, int tly, int tlx)
4459 {
4460     WINDOW *win;
4461     PANEL *pan = 0;
4462
4463     if ((win = newwin(rows, cols, tly, tlx)) != 0) {
4464         if ((pan = new_panel(win)) == 0) {
4465             delwin(win);
4466         } else if (use_colors) {
4467             short fg = (short) ((color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK);
4468             short bg = color;
4469
4470             init_pair(color, fg, bg);
4471             wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
4472         } else {
4473             wbkgdset(win, A_BOLD | ' ');
4474         }
4475     }
4476     return pan;
4477 }                               /* end of mkpanel */
4478
4479 /*+-------------------------------------------------------------------------
4480         rmpanel(pan)
4481 --------------------------------------------------------------------------*/
4482 static void
4483 rmpanel(PANEL * pan)
4484 {
4485     WINDOW *win = panel_window(pan);
4486     del_panel(pan);
4487     delwin(win);
4488 }                               /* end of rmpanel */
4489
4490 /*+-------------------------------------------------------------------------
4491         pflush()
4492 --------------------------------------------------------------------------*/
4493 static void
4494 pflush(void)
4495 {
4496     update_panels();
4497     doupdate();
4498 }                               /* end of pflush */
4499
4500 /*+-------------------------------------------------------------------------
4501         fill_panel(win)
4502 --------------------------------------------------------------------------*/
4503 static void
4504 init_panel(void)
4505 {
4506     register int y, x;
4507
4508     for (y = 0; y < LINES - 1; y++) {
4509         for (x = 0; x < COLS; x++)
4510             wprintw(stdscr, "%d", (y + x) % 10);
4511     }
4512 }
4513
4514 static void
4515 fill_panel(PANEL * pan)
4516 {
4517     WINDOW *win = panel_window(pan);
4518     const char *userptr = (const char *) panel_userptr(pan);
4519     int num = (userptr && *userptr) ? userptr[1] : '?';
4520     int y, x;
4521
4522     wmove(win, 1, 1);
4523     wprintw(win, "-pan%c-", num);
4524     wclrtoeol(win);
4525     box(win, 0, 0);
4526     for (y = 2; y < getmaxy(win) - 1; y++) {
4527         for (x = 1; x < getmaxx(win) - 1; x++) {
4528             wmove(win, y, x);
4529             waddch(win, UChar(num));
4530         }
4531     }
4532 }
4533
4534 #if USE_WIDEC_SUPPORT
4535 static void
4536 init_wide_panel(void)
4537 {
4538     int digit;
4539     cchar_t temp[10];
4540
4541     for (digit = 0; digit < 10; ++digit)
4542         make_fullwidth_digit(&temp[digit], digit);
4543
4544     do {
4545         int y, x;
4546         getyx(stdscr, y, x);
4547         digit = (y + x / 2) % 10;
4548     } while (add_wch(&temp[digit]) != ERR);
4549 }
4550
4551 static void
4552 fill_wide_panel(PANEL * pan)
4553 {
4554     WINDOW *win = panel_window(pan);
4555     const char *userptr = (const char *) panel_userptr(pan);
4556     int num = (userptr && *userptr) ? userptr[1] : '?';
4557     int y, x;
4558
4559     wmove(win, 1, 1);
4560     wprintw(win, "-pan%c-", num);
4561     wclrtoeol(win);
4562     box(win, 0, 0);
4563     for (y = 2; y < getmaxy(win) - 1; y++) {
4564         for (x = 1; x < getmaxx(win) - 1; x++) {
4565             wmove(win, y, x);
4566             waddch(win, UChar(num));
4567         }
4568     }
4569 }
4570 #endif
4571
4572 #define MAX_PANELS 5
4573
4574 static void
4575 canned_panel(PANEL * px[MAX_PANELS + 1], NCURSES_CONST char *cmd)
4576 {
4577     int which = cmd[1] - '0';
4578
4579     saywhat(cmd);
4580     switch (*cmd) {
4581     case 'h':
4582         hide_panel(px[which]);
4583         break;
4584     case 's':
4585         show_panel(px[which]);
4586         break;
4587     case 't':
4588         top_panel(px[which]);
4589         break;
4590     case 'b':
4591         bottom_panel(px[which]);
4592         break;
4593     case 'd':
4594         rmpanel(px[which]);
4595         break;
4596     }
4597     pflush();
4598     wait_a_while(nap_msec);
4599 }
4600
4601 static void
4602 demo_panels(void (*InitPanel) (void), void (*FillPanel) (PANEL *))
4603 {
4604     int count;
4605     int itmp;
4606     PANEL *px[MAX_PANELS + 1];
4607
4608     scrollok(stdscr, FALSE);    /* we don't want stdscr to scroll! */
4609     refresh();
4610
4611     InitPanel();
4612     for (count = 0; count < 5; count++) {
4613         px[1] = mkpanel(COLOR_RED,
4614                         LINES / 2 - 2,
4615                         COLS / 8 + 1,
4616                         0,
4617                         0);
4618         set_panel_userptr(px[1], (NCURSES_CONST void *) "p1");
4619
4620         px[2] = mkpanel(COLOR_GREEN,
4621                         LINES / 2 + 1,
4622                         COLS / 7,
4623                         LINES / 4,
4624                         COLS / 10);
4625         set_panel_userptr(px[2], (NCURSES_CONST void *) "p2");
4626
4627         px[3] = mkpanel(COLOR_YELLOW,
4628                         LINES / 4,
4629                         COLS / 10,
4630                         LINES / 2,
4631                         COLS / 9);
4632         set_panel_userptr(px[3], (NCURSES_CONST void *) "p3");
4633
4634         px[4] = mkpanel(COLOR_BLUE,
4635                         LINES / 2 - 2,
4636                         COLS / 8,
4637                         LINES / 2 - 2,
4638                         COLS / 3);
4639         set_panel_userptr(px[4], (NCURSES_CONST void *) "p4");
4640
4641         px[5] = mkpanel(COLOR_MAGENTA,
4642                         LINES / 2 - 2,
4643                         COLS / 8,
4644                         LINES / 2,
4645                         COLS / 2 - 2);
4646         set_panel_userptr(px[5], (NCURSES_CONST void *) "p5");
4647
4648         FillPanel(px[1]);
4649         FillPanel(px[2]);
4650         FillPanel(px[3]);
4651         FillPanel(px[4]);
4652         FillPanel(px[5]);
4653
4654         hide_panel(px[4]);
4655         hide_panel(px[5]);
4656         pflush();
4657         saywhat("");
4658         wait_a_while(nap_msec);
4659
4660         saywhat("h3 s1 s2 s4 s5");
4661         move_panel(px[1], 0, 0);
4662         hide_panel(px[3]);
4663         show_panel(px[1]);
4664         show_panel(px[2]);
4665         show_panel(px[4]);
4666         show_panel(px[5]);
4667         pflush();
4668         wait_a_while(nap_msec);
4669
4670         canned_panel(px, "s1");
4671         canned_panel(px, "s2");
4672
4673         saywhat("m2");
4674         move_panel(px[2], LINES / 3 + 1, COLS / 8);
4675         pflush();
4676         wait_a_while(nap_msec);
4677
4678         canned_panel(px, "s3");
4679
4680         saywhat("m3");
4681         move_panel(px[3], LINES / 4 + 1, COLS / 15);
4682         pflush();
4683         wait_a_while(nap_msec);
4684
4685         canned_panel(px, "b3");
4686         canned_panel(px, "s4");
4687         canned_panel(px, "s5");
4688         canned_panel(px, "t3");
4689         canned_panel(px, "t1");
4690         canned_panel(px, "t2");
4691         canned_panel(px, "t3");
4692         canned_panel(px, "t4");
4693
4694         for (itmp = 0; itmp < 6; itmp++) {
4695             WINDOW *w4 = panel_window(px[4]);
4696             WINDOW *w5 = panel_window(px[5]);
4697
4698             saywhat("m4");
4699             wmove(w4, LINES / 8, 1);
4700             waddstr(w4, mod[itmp]);
4701             move_panel(px[4], LINES / 6, itmp * (COLS / 8));
4702             wmove(w5, LINES / 6, 1);
4703             waddstr(w5, mod[itmp]);
4704             pflush();
4705             wait_a_while(nap_msec);
4706
4707             saywhat("m5");
4708             wmove(w4, LINES / 6, 1);
4709             waddstr(w4, mod[itmp]);
4710             move_panel(px[5], LINES / 3 - 1, (itmp * 10) + 6);
4711             wmove(w5, LINES / 8, 1);
4712             waddstr(w5, mod[itmp]);
4713             pflush();
4714             wait_a_while(nap_msec);
4715         }
4716
4717         saywhat("m4");
4718         move_panel(px[4], LINES / 6, itmp * (COLS / 8));
4719         pflush();
4720         wait_a_while(nap_msec);
4721
4722         canned_panel(px, "t5");
4723         canned_panel(px, "t2");
4724         canned_panel(px, "t1");
4725         canned_panel(px, "d2");
4726         canned_panel(px, "h3");
4727         canned_panel(px, "d1");
4728         canned_panel(px, "d4");
4729         canned_panel(px, "d5");
4730         canned_panel(px, "d3");
4731
4732         wait_a_while(nap_msec);
4733         if (nap_msec == 1)
4734             break;
4735         nap_msec = 100L;
4736     }
4737
4738     erase();
4739     endwin();
4740 }
4741 #endif /* USE_LIBPANEL */
4742
4743 /****************************************************************************
4744  *
4745  * Pad tester
4746  *
4747  ****************************************************************************/
4748
4749 #define GRIDSIZE        3
4750
4751 static bool pending_pan = FALSE;
4752 static bool show_panner_legend = TRUE;
4753
4754 static int
4755 panner_legend(int line)
4756 {
4757     static const char *const legend[] =
4758     {
4759         "Use arrow keys (or U,D,L,R) to pan, ESC to quit, ! to shell-out.",
4760         "Use +,- (or j,k) to grow/shrink the panner vertically.",
4761         "Use <,> (or h,l) to grow/shrink the panner horizontally.",
4762         "Number repeats.  Toggle legend:? filler:a timer:t scrollmark:s."
4763     };
4764     int n = ((int) SIZEOF(legend) - (LINES - line));
4765     if (n >= 0) {
4766         if (move(line, 0) != ERR) {
4767             if (show_panner_legend)
4768                 printw("%s", legend[n]);
4769             clrtoeol();
4770             return show_panner_legend;
4771         }
4772     }
4773     return FALSE;
4774 }
4775
4776 static void
4777 panner_h_cleanup(int from_y, int from_x, int to_x)
4778 {
4779     if (!panner_legend(from_y))
4780         do_h_line(from_y, from_x, ' ', to_x);
4781 }
4782
4783 static void
4784 panner_v_cleanup(int from_y, int from_x, int to_y)
4785 {
4786     if (!panner_legend(from_y))
4787         do_v_line(from_y, from_x, ' ', to_y);
4788 }
4789
4790 static void
4791 fill_pad(WINDOW *panpad, bool pan_lines)
4792 {
4793     int y, x;
4794     unsigned gridcount = 0;
4795
4796     wmove(panpad, 0, 0);
4797     for (y = 0; y < getmaxy(panpad); y++) {
4798         for (x = 0; x < getmaxx(panpad); x++) {
4799             if (y % GRIDSIZE == 0 && x % GRIDSIZE == 0) {
4800                 if (y == 0 && x == 0)
4801                     waddch(panpad, pan_lines ? ACS_ULCORNER : '+');
4802                 else if (y == 0)
4803                     waddch(panpad, pan_lines ? ACS_TTEE : '+');
4804                 else if (y == 0 || x == 0)
4805                     waddch(panpad, pan_lines ? ACS_LTEE : '+');
4806                 else
4807                     waddch(panpad, (chtype) ((pan_lines ? 'a' : 'A') +
4808                                              (int) (gridcount++ % 26)));
4809             } else if (y % GRIDSIZE == 0)
4810                 waddch(panpad, pan_lines ? ACS_HLINE : '-');
4811             else if (x % GRIDSIZE == 0)
4812                 waddch(panpad, pan_lines ? ACS_VLINE : '|');
4813             else
4814                 waddch(panpad, ' ');
4815         }
4816     }
4817 }
4818
4819 static void
4820 panner(WINDOW *pad,
4821        int top_x, int top_y, int porty, int portx,
4822        int (*pgetc) (WINDOW *))
4823 {
4824 #if HAVE_GETTIMEOFDAY
4825     struct timeval before, after;
4826     bool timing = TRUE;
4827 #endif
4828     bool pan_lines = FALSE;
4829     bool scrollers = TRUE;
4830     int basex = 0;
4831     int basey = 0;
4832     int pxmax, pymax, lowend, highend, c;
4833
4834     getmaxyx(pad, pymax, pxmax);
4835     scrollok(stdscr, FALSE);    /* we don't want stdscr to scroll! */
4836
4837     c = KEY_REFRESH;
4838     do {
4839 #ifdef NCURSES_VERSION
4840         /*
4841          * During shell-out, the user may have resized the window.  Adjust
4842          * the port size of the pad to accommodate this.  Ncurses automatically
4843          * resizes all of the normal windows to fit on the new screen.
4844          */
4845         if (top_x > COLS)
4846             top_x = COLS;
4847         if (portx > COLS)
4848             portx = COLS;
4849         if (top_y > LINES)
4850             top_y = LINES;
4851         if (porty > LINES)
4852             porty = LINES;
4853 #endif
4854         switch (c) {
4855         case KEY_REFRESH:
4856             erase();
4857
4858             /* FALLTHRU */
4859         case '?':
4860             if (c == '?')
4861                 show_panner_legend = !show_panner_legend;
4862             panner_legend(LINES - 4);
4863             panner_legend(LINES - 3);
4864             panner_legend(LINES - 2);
4865             panner_legend(LINES - 1);
4866             break;
4867         case 'a':
4868             pan_lines = !pan_lines;
4869             fill_pad(pad, pan_lines);
4870             pending_pan = FALSE;
4871             break;
4872
4873 #if HAVE_GETTIMEOFDAY
4874         case 't':
4875             timing = !timing;
4876             if (!timing)
4877                 panner_legend(LINES - 1);
4878             break;
4879 #endif
4880         case 's':
4881             scrollers = !scrollers;
4882             break;
4883
4884             /* Move the top-left corner of the pad, keeping the bottom-right
4885              * corner fixed.
4886              */
4887         case 'h':               /* increase-columns: move left edge to left */
4888             if (top_x <= 0)
4889                 beep();
4890             else {
4891                 panner_v_cleanup(top_y, top_x, porty);
4892                 top_x--;
4893             }
4894             break;
4895
4896         case 'j':               /* decrease-lines: move top-edge down */
4897             if (top_y >= porty)
4898                 beep();
4899             else {
4900                 panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
4901                 top_y++;
4902             }
4903             break;
4904
4905         case 'k':               /* increase-lines: move top-edge up */
4906             if (top_y <= 0)
4907                 beep();
4908             else {
4909                 top_y--;
4910                 panner_h_cleanup(top_y, top_x, portx);
4911             }
4912             break;
4913
4914         case 'l':               /* decrease-columns: move left-edge to right */
4915             if (top_x >= portx)
4916                 beep();
4917             else {
4918                 panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
4919                 top_x++;
4920             }
4921             break;
4922
4923             /* Move the bottom-right corner of the pad, keeping the top-left
4924              * corner fixed.
4925              */
4926         case KEY_IC:            /* increase-columns: move right-edge to right */
4927             if (portx >= pxmax || portx >= COLS)
4928                 beep();
4929             else {
4930                 panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
4931                 ++portx;
4932             }
4933             break;
4934
4935         case KEY_IL:            /* increase-lines: move bottom-edge down */
4936             if (porty >= pymax || porty >= LINES)
4937                 beep();
4938             else {
4939                 panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
4940                 ++porty;
4941             }
4942             break;
4943
4944         case KEY_DC:            /* decrease-columns: move bottom edge up */
4945             if (portx <= top_x)
4946                 beep();
4947             else {
4948                 portx--;
4949                 panner_v_cleanup(top_y - (top_y > 0), portx, porty);
4950             }
4951             break;
4952
4953         case KEY_DL:            /* decrease-lines */
4954             if (porty <= top_y)
4955                 beep();
4956             else {
4957                 porty--;
4958                 panner_h_cleanup(porty, top_x - (top_x > 0), portx);
4959             }
4960             break;
4961
4962         case KEY_LEFT:          /* pan leftwards */
4963             if (basex > 0)
4964                 basex--;
4965             else
4966                 beep();
4967             break;
4968
4969         case KEY_RIGHT: /* pan rightwards */
4970             if (basex + portx - (pymax > porty) < pxmax)
4971                 basex++;
4972             else
4973                 beep();
4974             break;
4975
4976         case KEY_UP:            /* pan upwards */
4977             if (basey > 0)
4978                 basey--;
4979             else
4980                 beep();
4981             break;
4982
4983         case KEY_DOWN:          /* pan downwards */
4984             if (basey + porty - (pxmax > portx) < pymax)
4985                 basey++;
4986             else
4987                 beep();
4988             break;
4989
4990         case 'H':
4991         case KEY_HOME:
4992         case KEY_FIND:
4993             basey = 0;
4994             break;
4995
4996         case 'E':
4997         case KEY_END:
4998         case KEY_SELECT:
4999             basey = pymax - porty;
5000             if (basey < 0)
5001                 basey = 0;
5002             break;
5003
5004         default:
5005             beep();
5006             break;
5007         }
5008
5009         MvAddCh(top_y - 1, top_x - 1, ACS_ULCORNER);
5010         do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
5011         do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
5012
5013         if (scrollers && (pxmax > portx - 1)) {
5014             int length = (portx - top_x - 1);
5015             float ratio = ((float) length) / ((float) pxmax);
5016
5017             lowend = (int) ((float) top_x + ((float) basex * ratio));
5018             highend = (int) ((float) top_x + ((float) (basex + length) * ratio));
5019
5020             do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
5021             if (highend < portx) {
5022                 attron(A_REVERSE);
5023                 do_h_line(porty - 1, lowend, ' ', highend + 1);
5024                 attroff(A_REVERSE);
5025                 do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
5026             }
5027         } else
5028             do_h_line(porty - 1, top_x, ACS_HLINE, portx);
5029
5030         if (scrollers && (pymax > porty - 1)) {
5031             int length = (porty - top_y - 1);
5032             float ratio = ((float) length) / ((float) pymax);
5033
5034             lowend = (int) ((float) top_y + ((float) basey * ratio));
5035             highend = (int) ((float) top_y + ((float) (basey + length) * ratio));
5036
5037             do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
5038             if (highend < porty) {
5039                 attron(A_REVERSE);
5040                 do_v_line(lowend, portx - 1, ' ', highend + 1);
5041                 attroff(A_REVERSE);
5042                 do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
5043             }
5044         } else
5045             do_v_line(top_y, portx - 1, ACS_VLINE, porty);
5046
5047         MvAddCh(top_y - 1, portx - 1, ACS_URCORNER);
5048         MvAddCh(porty - 1, top_x - 1, ACS_LLCORNER);
5049         MvAddCh(porty - 1, portx - 1, ACS_LRCORNER);
5050
5051         if (!pending_pan) {
5052 #if HAVE_GETTIMEOFDAY
5053             gettimeofday(&before, 0);
5054 #endif
5055             wnoutrefresh(stdscr);
5056
5057             pnoutrefresh(pad,
5058                          basey, basex,
5059                          top_y, top_x,
5060                          porty - (pxmax > portx) - 1,
5061                          portx - (pymax > porty) - 1);
5062
5063             doupdate();
5064 #if HAVE_GETTIMEOFDAY
5065             if (timing) {
5066                 double elapsed;
5067                 gettimeofday(&after, 0);
5068                 elapsed = (after.tv_sec + after.tv_usec / 1.0e6)
5069                     - (before.tv_sec + before.tv_usec / 1.0e6);
5070                 move(LINES - 1, COLS - 12);
5071                 printw("Secs: %2.03f", elapsed);
5072                 refresh();
5073             }
5074 #endif
5075         }
5076
5077     } while
5078         ((c = pgetc(pad)) != KEY_EXIT);
5079
5080     scrollok(stdscr, TRUE);     /* reset to driver's default */
5081 }
5082
5083 static int
5084 padgetch(WINDOW *win)
5085 {
5086     static int count;
5087     static int last;
5088     int c;
5089
5090     if ((pending_pan = (count > 0)) != FALSE) {
5091         count--;
5092         pending_pan = (count != 0);
5093     } else {
5094         for (;;) {
5095             switch (c = wGetchar(win)) {
5096             case '!':
5097                 ShellOut(FALSE);
5098                 /* FALLTHRU */
5099             case CTRL('r'):
5100                 endwin();
5101                 refresh();
5102                 c = KEY_REFRESH;
5103                 break;
5104             case CTRL('l'):
5105                 c = KEY_REFRESH;
5106                 break;
5107             case 'U':
5108                 c = KEY_UP;
5109                 break;
5110             case 'D':
5111                 c = KEY_DOWN;
5112                 break;
5113             case 'R':
5114                 c = KEY_RIGHT;
5115                 break;
5116             case 'L':
5117                 c = KEY_LEFT;
5118                 break;
5119             case '+':
5120                 c = KEY_IL;
5121                 break;
5122             case '-':
5123                 c = KEY_DL;
5124                 break;
5125             case '>':
5126                 c = KEY_IC;
5127                 break;
5128             case '<':
5129                 c = KEY_DC;
5130                 break;
5131             case ERR:           /* FALLTHRU */
5132             case case_QUIT:
5133                 count = 0;
5134                 c = KEY_EXIT;
5135                 break;
5136             default:
5137                 if (c >= '0' && c <= '9') {
5138                     count = count * 10 + (c - '0');
5139                     continue;
5140                 }
5141                 break;
5142             }
5143             last = c;
5144             break;
5145         }
5146         if (count > 0)
5147             count--;
5148     }
5149     return (last);
5150 }
5151
5152 #define PAD_HIGH 200
5153 #define PAD_WIDE 200
5154
5155 static void
5156 demo_pad(void)
5157 /* Demonstrate pads. */
5158 {
5159     WINDOW *panpad = newpad(PAD_HIGH, PAD_WIDE);
5160
5161     if (panpad == 0) {
5162         Cannot("cannot create requested pad");
5163         return;
5164     }
5165
5166     fill_pad(panpad, FALSE);
5167
5168     panner_legend(LINES - 4);
5169     panner_legend(LINES - 3);
5170     panner_legend(LINES - 2);
5171     panner_legend(LINES - 1);
5172
5173     keypad(panpad, TRUE);
5174
5175     /* Make the pad (initially) narrow enough that a trace file won't wrap.
5176      * We'll still be able to widen it during a test, since that's required
5177      * for testing boundaries.
5178      */
5179     panner(panpad, 2, 2, LINES - 5, COLS - 15, padgetch);
5180
5181     delwin(panpad);
5182     endwin();
5183     erase();
5184 }
5185
5186 /****************************************************************************
5187  *
5188  * Tests from John Burnell's PDCurses tester
5189  *
5190  ****************************************************************************/
5191
5192 static void
5193 Continue(WINDOW *win)
5194 {
5195     noecho();
5196     wmove(win, 10, 1);
5197     MvWAddStr(win, 10, 1, " Press any key to continue");
5198     wrefresh(win);
5199     wGetchar(win);
5200 }
5201
5202 static void
5203 flushinp_test(WINDOW *win)
5204 /* Input test, adapted from John Burnell's PDCurses tester */
5205 {
5206     int w, h, bx, by, sw, sh, i;
5207
5208     WINDOW *subWin;
5209     wclear(win);
5210
5211     getmaxyx(win, h, w);
5212     getbegyx(win, by, bx);
5213     sw = w / 3;
5214     sh = h / 3;
5215     if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == 0)
5216         return;
5217
5218 #ifdef A_COLOR
5219     if (use_colors) {
5220         init_pair(2, COLOR_CYAN, COLOR_BLUE);
5221         wbkgd(subWin, COLOR_PAIR(2) | ' ');
5222     }
5223 #endif
5224     (void) wattrset(subWin, A_BOLD);
5225     box(subWin, ACS_VLINE, ACS_HLINE);
5226     MvWAddStr(subWin, 2, 1, "This is a subwindow");
5227     wrefresh(win);
5228
5229     /*
5230      * This used to set 'nocbreak()'.  However, Alexander Lukyanov says that
5231      * it only happened to "work" on SVr4 because that implementation does not
5232      * emulate nocbreak+noecho mode, whereas ncurses does.  To get the desired
5233      * test behavior, we're using 'cbreak()', which will allow a single
5234      * character to return without needing a newline. - T.Dickey 1997/10/11.
5235      */
5236     cbreak();
5237     MvWAddStr(win, 0, 1, "This is a test of the flushinp() call.");
5238
5239     MvWAddStr(win, 2, 1, "Type random keys for 5 seconds.");
5240     MvWAddStr(win, 3, 1,
5241               "These should be discarded (not echoed) after the subwindow goes away.");
5242     wrefresh(win);
5243
5244     for (i = 0; i < 5; i++) {
5245         MvWPrintw(subWin, 1, 1, "Time = %d", i);
5246         wrefresh(subWin);
5247         napms(1000);
5248         flushinp();
5249     }
5250
5251     delwin(subWin);
5252     werase(win);
5253     flash();
5254     wrefresh(win);
5255     napms(1000);
5256
5257     MvWAddStr(win, 2, 1,
5258               "If you were still typing when the window timer expired,");
5259     MvWAddStr(win, 3, 1,
5260               "or else you typed nothing at all while it was running,");
5261     MvWAddStr(win, 4, 1,
5262               "test was invalid.  You'll see garbage or nothing at all. ");
5263     MvWAddStr(win, 6, 1, "Press a key");
5264     wmove(win, 9, 10);
5265     wrefresh(win);
5266     echo();
5267     wGetchar(win);
5268     flushinp();
5269     MvWAddStr(win, 12, 0,
5270               "If you see any key other than what you typed, flushinp() is broken.");
5271     Continue(win);
5272
5273     wmove(win, 9, 10);
5274     wdelch(win);
5275     wrefresh(win);
5276     wmove(win, 12, 0);
5277     clrtoeol();
5278     waddstr(win,
5279             "What you typed should now have been deleted; if not, wdelch() failed.");
5280     Continue(win);
5281
5282     cbreak();
5283 }
5284
5285 /****************************************************************************
5286  *
5287  * Menu test
5288  *
5289  ****************************************************************************/
5290
5291 #if USE_LIBMENU
5292
5293 #define MENU_Y  8
5294 #define MENU_X  8
5295
5296 static int
5297 menu_virtualize(int c)
5298 {
5299     if (c == '\n' || c == KEY_EXIT)
5300         return (MAX_COMMAND + 1);
5301     else if (c == 'u')
5302         return (REQ_SCR_ULINE);
5303     else if (c == 'd')
5304         return (REQ_SCR_DLINE);
5305     else if (c == 'b' || c == KEY_NPAGE)
5306         return (REQ_SCR_UPAGE);
5307     else if (c == 'f' || c == KEY_PPAGE)
5308         return (REQ_SCR_DPAGE);
5309     else if (c == 'n' || c == KEY_DOWN)
5310         return (REQ_NEXT_ITEM);
5311     else if (c == 'p' || c == KEY_UP)
5312         return (REQ_PREV_ITEM);
5313     else if (c == ' ')
5314         return (REQ_TOGGLE_ITEM);
5315     else {
5316         if (c != KEY_MOUSE)
5317             beep();
5318         return (c);
5319     }
5320 }
5321
5322 static CONST_MENUS char *animals[] =
5323 {
5324     "Lions",
5325     "Tigers",
5326     "Bears",
5327     "(Oh my!)",
5328     "Newts",
5329     "Platypi",
5330     "Lemurs",
5331     "(Oh really?!)",
5332     "Leopards",
5333     "Panthers",
5334     "Pumas",
5335     "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
5336     "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
5337     (char *) 0
5338 };
5339
5340 static void
5341 menu_test(void)
5342 {
5343     MENU *m;
5344     ITEM *items[SIZEOF(animals)];
5345     ITEM **ip = items;
5346     CONST_MENUS char **ap;
5347     int mrows, mcols, c;
5348     WINDOW *menuwin;
5349
5350 #ifdef NCURSES_MOUSE_VERSION
5351     mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
5352 #endif
5353     MvAddStr(0, 0, "This is the menu test:");
5354     MvAddStr(2, 0, "  Use up and down arrow to move the select bar.");
5355     MvAddStr(3, 0, "  'n' and 'p' act like arrows.");
5356     MvAddStr(4, 0,
5357              "  'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
5358     MvAddStr(5, 0, "  Press return to exit.");
5359     refresh();
5360
5361     for (ap = animals; *ap; ap++) {
5362         if ((*ip = new_item(*ap, "")) != 0)
5363             ++ip;
5364     }
5365     *ip = (ITEM *) 0;
5366
5367     m = new_menu(items);
5368
5369     set_menu_format(m, (SIZEOF(animals) + 1) / 2, 1);
5370     scale_menu(m, &mrows, &mcols);
5371
5372     menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
5373     set_menu_win(m, menuwin);
5374     keypad(menuwin, TRUE);
5375     box(menuwin, 0, 0);
5376
5377     set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
5378
5379     post_menu(m);
5380
5381     while ((c = menu_driver(m, menu_virtualize(wGetchar(menuwin)))) != E_UNKNOWN_COMMAND) {
5382         if (c == E_NOT_POSTED)
5383             break;
5384         if (c == E_REQUEST_DENIED)
5385             beep();
5386         continue;
5387     }
5388
5389     MvPrintw(LINES - 2, 0,
5390              "You chose: %s\n", item_name(current_item(m)));
5391     (void) addstr("Press any key to continue...");
5392     wGetchar(stdscr);
5393
5394     unpost_menu(m);
5395     delwin(menuwin);
5396
5397     free_menu(m);
5398     for (ip = items; *ip; ip++)
5399         free_item(*ip);
5400 #ifdef NCURSES_MOUSE_VERSION
5401     mousemask(0, (mmask_t *) 0);
5402 #endif
5403 }
5404
5405 #ifdef TRACE
5406 #define T_TBL(name) { #name, name }
5407 static struct {
5408     const char *name;
5409     unsigned mask;
5410 } t_tbl[] = {
5411
5412     T_TBL(TRACE_DISABLE),
5413         T_TBL(TRACE_TIMES),
5414         T_TBL(TRACE_TPUTS),
5415         T_TBL(TRACE_UPDATE),
5416         T_TBL(TRACE_MOVE),
5417         T_TBL(TRACE_CHARPUT),
5418         T_TBL(TRACE_ORDINARY),
5419         T_TBL(TRACE_CALLS),
5420         T_TBL(TRACE_VIRTPUT),
5421         T_TBL(TRACE_IEVENT),
5422         T_TBL(TRACE_BITS),
5423         T_TBL(TRACE_ICALLS),
5424         T_TBL(TRACE_CCALLS),
5425         T_TBL(TRACE_DATABASE),
5426         T_TBL(TRACE_ATTRS),
5427         T_TBL(TRACE_MAXIMUM),
5428     {
5429         (char *) 0, 0
5430     }
5431 };
5432
5433 static char *
5434 tracetrace(unsigned tlevel)
5435 {
5436     static char *buf;
5437     int n;
5438
5439     if (buf == 0) {
5440         size_t need = 12;
5441         for (n = 0; t_tbl[n].name != 0; n++)
5442             need += strlen(t_tbl[n].name) + 2;
5443         buf = typeMalloc(char, need);
5444     }
5445     sprintf(buf, "0x%02x = {", tlevel);
5446     if (tlevel == 0) {
5447         sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
5448     } else {
5449         for (n = 1; t_tbl[n].name != 0; n++)
5450             if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
5451                 strcat(buf, t_tbl[n].name);
5452                 strcat(buf, ", ");
5453             }
5454     }
5455     if (buf[strlen(buf) - 2] == ',')
5456         buf[strlen(buf) - 2] = '\0';
5457     return (strcat(buf, "}"));
5458 }
5459
5460 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
5461  * the others
5462  */
5463 static int
5464 run_trace_menu(MENU * m)
5465 {
5466     ITEM **items;
5467     ITEM *i, **p;
5468
5469     for (;;) {
5470         bool changed = FALSE;
5471         switch (menu_driver(m, menu_virtualize(wGetchar(menu_win(m))))) {
5472         case E_UNKNOWN_COMMAND:
5473             return FALSE;
5474         default:
5475             items = menu_items(m);
5476             i = current_item(m);
5477             if (i == items[0]) {
5478                 if (item_value(i)) {
5479                     for (p = items + 1; *p != 0; p++)
5480                         if (item_value(*p)) {
5481                             set_item_value(*p, FALSE);
5482                             changed = TRUE;
5483                         }
5484                 }
5485             } else {
5486                 for (p = items + 1; *p != 0; p++)
5487                     if (item_value(*p)) {
5488                         set_item_value(items[0], FALSE);
5489                         changed = TRUE;
5490                         break;
5491                     }
5492             }
5493             if (!changed)
5494                 return TRUE;
5495         }
5496     }
5497 }
5498
5499 static void
5500 trace_set(void)
5501 /* interactively set the trace level */
5502 {
5503     MENU *m;
5504     ITEM *items[SIZEOF(t_tbl)];
5505     ITEM **ip = items;
5506     int mrows, mcols;
5507     unsigned newtrace;
5508     int n;
5509     WINDOW *menuwin;
5510
5511     MvAddStr(0, 0, "Interactively set trace level:");
5512     MvAddStr(2, 0, "  Press space bar to toggle a selection.");
5513     MvAddStr(3, 0, "  Use up and down arrow to move the select bar.");
5514     MvAddStr(4, 0, "  Press return to set the trace level.");
5515     MvPrintw(6, 0, "(Current trace level is %s)", tracetrace(_nc_tracing));
5516
5517     refresh();
5518
5519     for (n = 0; t_tbl[n].name != 0; n++) {
5520         if ((*ip = new_item(t_tbl[n].name, "")) != 0) {
5521             ++ip;
5522         }
5523     }
5524     *ip = (ITEM *) 0;
5525
5526     m = new_menu(items);
5527
5528     set_menu_format(m, 0, 2);
5529     scale_menu(m, &mrows, &mcols);
5530
5531     menu_opts_off(m, O_ONEVALUE);
5532     menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
5533     set_menu_win(m, menuwin);
5534     keypad(menuwin, TRUE);
5535     box(menuwin, 0, 0);
5536
5537     set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
5538
5539     post_menu(m);
5540
5541     for (ip = menu_items(m); *ip; ip++) {
5542         unsigned mask = t_tbl[item_index(*ip)].mask;
5543         if (mask == 0)
5544             set_item_value(*ip, _nc_tracing == 0);
5545         else if ((mask & _nc_tracing) == mask)
5546             set_item_value(*ip, TRUE);
5547     }
5548
5549     while (run_trace_menu(m))
5550         continue;
5551
5552     newtrace = 0;
5553     for (ip = menu_items(m); *ip; ip++)
5554         if (item_value(*ip))
5555             newtrace |= t_tbl[item_index(*ip)].mask;
5556     trace(newtrace);
5557     Trace(("trace level interactively set to %s", tracetrace(_nc_tracing)));
5558
5559     MvPrintw(LINES - 2, 0,
5560              "Trace level is %s\n", tracetrace(_nc_tracing));
5561     (void) addstr("Press any key to continue...");
5562     wGetchar(stdscr);
5563
5564     unpost_menu(m);
5565     delwin(menuwin);
5566
5567     free_menu(m);
5568     for (ip = items; *ip; ip++)
5569         free_item(*ip);
5570 }
5571 #endif /* TRACE */
5572 #endif /* USE_LIBMENU */
5573
5574 /****************************************************************************
5575  *
5576  * Forms test
5577  *
5578  ****************************************************************************/
5579 #if USE_LIBFORM
5580 static FIELD *
5581 make_label(int frow, int fcol, NCURSES_CONST char *label)
5582 {
5583     FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
5584
5585     if (f) {
5586         set_field_buffer(f, 0, label);
5587         set_field_opts(f, (int) (field_opts(f) & ~O_ACTIVE));
5588     }
5589     return (f);
5590 }
5591
5592 static FIELD *
5593 make_field(int frow, int fcol, int rows, int cols, bool secure)
5594 {
5595     FIELD *f = new_field(rows, cols, frow, fcol, 0, secure ? 1 : 0);
5596
5597     if (f) {
5598         set_field_back(f, A_UNDERLINE);
5599         set_field_userptr(f, (void *) 0);
5600     }
5601     return (f);
5602 }
5603
5604 static void
5605 display_form(FORM * f)
5606 {
5607     WINDOW *w;
5608     int rows, cols;
5609
5610     scale_form(f, &rows, &cols);
5611
5612     if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
5613         set_form_win(f, w);
5614         set_form_sub(f, derwin(w, rows, cols, 1, 2));
5615         box(w, 0, 0);
5616         keypad(w, TRUE);
5617     }
5618
5619     if (post_form(f) != E_OK)
5620         wrefresh(w);
5621 }
5622
5623 static void
5624 erase_form(FORM * f)
5625 {
5626     WINDOW *w = form_win(f);
5627     WINDOW *s = form_sub(f);
5628
5629     unpost_form(f);
5630     werase(w);
5631     wrefresh(w);
5632     delwin(s);
5633     delwin(w);
5634 }
5635
5636 static int
5637 edit_secure(FIELD * me, int c)
5638 {
5639     int rows, cols, frow, fcol, nrow, nbuf;
5640
5641     if (field_info(me, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK
5642         && nbuf > 0) {
5643         char *source = field_buffer(me, 1);
5644         char temp[80];
5645         long len;
5646
5647         strcpy(temp, source ? source : "");
5648         len = (long) (char *) field_userptr(me);
5649         if (c <= KEY_MAX) {
5650             if (isgraph(c) && (len + 1) < (int) sizeof(temp)) {
5651                 temp[len++] = (char) c;
5652                 temp[len] = 0;
5653                 set_field_buffer(me, 1, temp);
5654                 c = '*';
5655             } else {
5656                 c = 0;
5657             }
5658         } else {
5659             switch (c) {
5660             case REQ_BEG_FIELD:
5661             case REQ_CLR_EOF:
5662             case REQ_CLR_EOL:
5663             case REQ_DEL_LINE:
5664             case REQ_DEL_WORD:
5665             case REQ_DOWN_CHAR:
5666             case REQ_END_FIELD:
5667             case REQ_INS_CHAR:
5668             case REQ_INS_LINE:
5669             case REQ_LEFT_CHAR:
5670             case REQ_NEW_LINE:
5671             case REQ_NEXT_WORD:
5672             case REQ_PREV_WORD:
5673             case REQ_RIGHT_CHAR:
5674             case REQ_UP_CHAR:
5675                 c = 0;          /* we don't want to do inline editing */
5676                 break;
5677             case REQ_CLR_FIELD:
5678                 if (len) {
5679                     temp[0] = 0;
5680                     set_field_buffer(me, 1, temp);
5681                 }
5682                 break;
5683             case REQ_DEL_CHAR:
5684             case REQ_DEL_PREV:
5685                 if (len) {
5686                     temp[--len] = 0;
5687                     set_field_buffer(me, 1, temp);
5688                 }
5689                 break;
5690             }
5691         }
5692         set_field_userptr(me, (void *) len);
5693     }
5694     return c;
5695 }
5696
5697 static int
5698 form_virtualize(FORM * f, WINDOW *w)
5699 {
5700     /* *INDENT-OFF* */
5701     static const struct {
5702         int code;
5703         int result;
5704     } lookup[] = {
5705         { CTRL('A'),    REQ_NEXT_CHOICE },
5706         { CTRL('B'),    REQ_PREV_WORD },
5707         { CTRL('C'),    REQ_CLR_EOL },
5708         { CTRL('D'),    REQ_DOWN_FIELD },
5709         { CTRL('E'),    REQ_END_FIELD },
5710         { CTRL('F'),    REQ_NEXT_PAGE },
5711         { CTRL('G'),    REQ_DEL_WORD },
5712         { CTRL('H'),    REQ_DEL_PREV },
5713         { CTRL('I'),    REQ_INS_CHAR },
5714         { CTRL('K'),    REQ_CLR_EOF },
5715         { CTRL('L'),    REQ_LEFT_FIELD },
5716         { CTRL('M'),    REQ_NEW_LINE },
5717         { CTRL('N'),    REQ_NEXT_FIELD },
5718         { CTRL('O'),    REQ_INS_LINE },
5719         { CTRL('P'),    REQ_PREV_FIELD },
5720         { CTRL('R'),    REQ_RIGHT_FIELD },
5721         { CTRL('S'),    REQ_BEG_FIELD },
5722         { CTRL('U'),    REQ_UP_FIELD },
5723         { CTRL('V'),    REQ_DEL_CHAR },
5724         { CTRL('W'),    REQ_NEXT_WORD },
5725         { CTRL('X'),    REQ_CLR_FIELD },
5726         { CTRL('Y'),    REQ_DEL_LINE },
5727         { CTRL('Z'),    REQ_PREV_CHOICE },
5728         { ESCAPE,       MAX_FORM_COMMAND + 1 },
5729         { KEY_BACKSPACE, REQ_DEL_PREV },
5730         { KEY_DOWN,     REQ_DOWN_CHAR },
5731         { KEY_END,      REQ_LAST_FIELD },
5732         { KEY_HOME,     REQ_FIRST_FIELD },
5733         { KEY_LEFT,     REQ_LEFT_CHAR },
5734         { KEY_LL,       REQ_LAST_FIELD },
5735         { KEY_NEXT,     REQ_NEXT_FIELD },
5736         { KEY_NPAGE,    REQ_NEXT_PAGE },
5737         { KEY_PPAGE,    REQ_PREV_PAGE },
5738         { KEY_PREVIOUS, REQ_PREV_FIELD },
5739         { KEY_RIGHT,    REQ_RIGHT_CHAR },
5740         { KEY_UP,       REQ_UP_CHAR },
5741         { QUIT,         MAX_FORM_COMMAND + 1 }
5742     };
5743     /* *INDENT-ON* */
5744
5745     static int mode = REQ_INS_MODE;
5746     int c = wGetchar(w);
5747     unsigned n;
5748     FIELD *me = current_field(f);
5749     bool current = TRUE;
5750
5751     if (c == CTRL(']')) {
5752         if (mode == REQ_INS_MODE) {
5753             mode = REQ_OVL_MODE;
5754         } else {
5755             mode = REQ_INS_MODE;
5756         }
5757         c = mode;
5758     } else {
5759         for (n = 0; n < SIZEOF(lookup); n++) {
5760             if (lookup[n].code == c) {
5761                 c = lookup[n].result;
5762                 break;
5763             }
5764         }
5765     }
5766     MvPrintw(0, COLS - 6, "(%s)", mode == REQ_INS_MODE ? "INS" : "OVL");
5767
5768     /*
5769      * Force the field that the user is typing into to be in reverse video,
5770      * while the other fields are shown underlined.
5771      */
5772     switch (c) {
5773     case REQ_BEG_FIELD:
5774     case REQ_CLR_EOF:
5775     case REQ_CLR_EOL:
5776     case REQ_CLR_FIELD:
5777     case REQ_DEL_CHAR:
5778     case REQ_DEL_LINE:
5779     case REQ_DEL_PREV:
5780     case REQ_DEL_WORD:
5781     case REQ_END_FIELD:
5782     case REQ_INS_CHAR:
5783     case REQ_INS_LINE:
5784     case REQ_LEFT_CHAR:
5785     case REQ_LEFT_FIELD:
5786     case REQ_NEXT_WORD:
5787     case REQ_RIGHT_CHAR:
5788         current = TRUE;
5789         break;
5790     default:
5791         current = (c < KEY_MAX);
5792         break;
5793     }
5794     if (current) {
5795         c = edit_secure(me, c);
5796         set_field_back(me, A_REVERSE);
5797     } else {
5798         c = edit_secure(me, c);
5799         set_field_back(me, A_UNDERLINE);
5800     }
5801     return c;
5802 }
5803
5804 static int
5805 my_form_driver(FORM * form, int c)
5806 {
5807     if (c == (MAX_FORM_COMMAND + 1)
5808         && form_driver(form, REQ_VALIDATION) == E_OK)
5809         return (TRUE);
5810     else {
5811         beep();
5812         return (FALSE);
5813     }
5814 }
5815
5816 #ifdef NCURSES_VERSION
5817 #define FIELDCHECK_CB(func) bool func(FIELD * fld, const void * data GCC_UNUSED)
5818 #define CHAR_CHECK_CB(func) bool func(int ch, const void *data GCC_UNUSED)
5819 #else
5820 #define FIELDCHECK_CB(func) int func(FIELD * fld, char * data GCC_UNUSED)
5821 #define CHAR_CHECK_CB(func) int func(int ch, char *data GCC_UNUSED)
5822 #endif
5823
5824 /*
5825  * Allow a middle initial, optionally with a '.' to end it.
5826  */
5827 static
5828 FIELDCHECK_CB(mi_field_check)
5829 {
5830     char *s = field_buffer(fld, 0);
5831     int state = 0;
5832     int n;
5833
5834     for (n = 0; s[n] != '\0'; ++n) {
5835         switch (state) {
5836         case 0:
5837             if (s[n] == '.') {
5838                 if (n != 1)
5839                     return FALSE;
5840                 state = 2;
5841             } else if (isspace(UChar(s[n]))) {
5842                 state = 2;
5843             }
5844             break;
5845         case 2:
5846             if (!isspace(UChar(s[n])))
5847                 return FALSE;
5848             break;
5849         }
5850     }
5851
5852     /* force the form to display a leading capital */
5853     if (islower(UChar(s[0]))) {
5854         s[0] = (char) toupper(UChar(s[0]));
5855         set_field_buffer(fld, 0, s);
5856     }
5857     return TRUE;
5858 }
5859
5860 static
5861 CHAR_CHECK_CB(mi_char_check)
5862 {
5863     return ((isalpha(ch) || ch == '.') ? TRUE : FALSE);
5864 }
5865
5866 /*
5867  * Passwords should be at least 6 characters.
5868  */
5869 static
5870 FIELDCHECK_CB(pw_field_check)
5871 {
5872     char *s = field_buffer(fld, 0);
5873     int n;
5874
5875     for (n = 0; s[n] != '\0'; ++n) {
5876         if (isspace(UChar(s[n]))) {
5877             if (n < 6)
5878                 return FALSE;
5879         }
5880     }
5881     return TRUE;
5882 }
5883
5884 static
5885 CHAR_CHECK_CB(pw_char_check)
5886 {
5887     return (isgraph(ch) ? TRUE : FALSE);
5888 }
5889
5890 static void
5891 demo_forms(void)
5892 {
5893     WINDOW *w;
5894     FORM *form;
5895     FIELD *f[12], *secure;
5896     FIELDTYPE *fty_middle = new_fieldtype(mi_field_check, mi_char_check);
5897     FIELDTYPE *fty_passwd = new_fieldtype(pw_field_check, pw_char_check);
5898     int finished = 0, c;
5899     unsigned n = 0;
5900
5901 #ifdef NCURSES_MOUSE_VERSION
5902     mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
5903 #endif
5904
5905     move(18, 0);
5906     addstr("Defined edit/traversal keys:   ^Q/ESC- exit form\n");
5907     addstr("^N   -- go to next field       ^P  -- go to previous field\n");
5908     addstr("Home -- go to first field      End -- go to last field\n");
5909     addstr("^L   -- go to field to left    ^R  -- go to field to right\n");
5910     addstr("^U   -- move upward to field   ^D  -- move downward to field\n");
5911     addstr("^W   -- go to next word        ^B  -- go to previous word\n");
5912     addstr("^S   -- go to start of field   ^E  -- go to end of field\n");
5913     addstr("^H   -- delete previous char   ^Y  -- delete line\n");
5914     addstr("^G   -- delete current word    ^C  -- clear to end of line\n");
5915     addstr("^K   -- clear to end of field  ^X  -- clear field\n");
5916     addstr("Arrow keys move within a field as you would expect. ^] toggles overlay mode.");
5917
5918     MvAddStr(4, 57, "Forms Entry Test");
5919
5920     refresh();
5921
5922     /* describe the form */
5923     memset(f, 0, sizeof(f));
5924     f[n++] = make_label(0, 15, "Sample Form");
5925
5926     f[n++] = make_label(2, 0, "Last Name");
5927     f[n++] = make_field(3, 0, 1, 18, FALSE);
5928     set_field_type(f[n - 1], TYPE_ALPHA, 1);
5929
5930     f[n++] = make_label(2, 20, "First Name");
5931     f[n++] = make_field(3, 20, 1, 12, FALSE);
5932     set_field_type(f[n - 1], TYPE_ALPHA, 1);
5933
5934     f[n++] = make_label(2, 34, "Middle Name");
5935     f[n++] = make_field(3, 34, 1, 12, FALSE);
5936     set_field_type(f[n - 1], fty_middle);
5937
5938     f[n++] = make_label(5, 0, "Comments");
5939     f[n++] = make_field(6, 0, 4, 46, FALSE);
5940
5941     f[n++] = make_label(5, 20, "Password:");
5942     secure =
5943         f[n++] = make_field(5, 30, 1, 9, TRUE);
5944     set_field_type(f[n - 1], fty_passwd);
5945     f[n] = (FIELD *) 0;
5946
5947     if ((form = new_form(f)) != 0) {
5948
5949         display_form(form);
5950
5951         w = form_win(form);
5952         raw();
5953         nonl();                 /* lets us read ^M's */
5954         while (!finished) {
5955             switch (form_driver(form, c = form_virtualize(form, w))) {
5956             case E_OK:
5957                 MvAddStr(5, 57, field_buffer(secure, 1));
5958                 clrtoeol();
5959                 refresh();
5960                 break;
5961             case E_UNKNOWN_COMMAND:
5962                 finished = my_form_driver(form, c);
5963                 break;
5964             default:
5965                 beep();
5966                 break;
5967             }
5968         }
5969
5970         erase_form(form);
5971
5972         free_form(form);
5973     }
5974     for (c = 0; f[c] != 0; c++)
5975         free_field(f[c]);
5976     free_fieldtype(fty_middle);
5977     free_fieldtype(fty_passwd);
5978     noraw();
5979     nl();
5980
5981 #ifdef NCURSES_MOUSE_VERSION
5982     mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
5983 #endif
5984 }
5985 #endif /* USE_LIBFORM */
5986
5987 /****************************************************************************
5988  *
5989  * Overlap test
5990  *
5991  ****************************************************************************/
5992
5993 static void
5994 fillwin(WINDOW *win, char ch)
5995 {
5996     int y, x;
5997     int y1, x1;
5998
5999     getmaxyx(win, y1, x1);
6000     for (y = 0; y < y1; y++) {
6001         wmove(win, y, 0);
6002         for (x = 0; x < x1; x++)
6003             waddch(win, UChar(ch));
6004     }
6005 }
6006
6007 static void
6008 crosswin(WINDOW *win, char ch)
6009 {
6010     int y, x;
6011     int y1, x1;
6012
6013     getmaxyx(win, y1, x1);
6014     for (y = 0; y < y1; y++) {
6015         for (x = 0; x < x1; x++)
6016             if (((x > (x1 - 1) / 3) && (x <= (2 * (x1 - 1)) / 3))
6017                 || (((y > (y1 - 1) / 3) && (y <= (2 * (y1 - 1)) / 3)))) {
6018                 wmove(win, y, x);
6019                 waddch(win, UChar(ch));
6020             }
6021     }
6022 }
6023
6024 #define OVERLAP_FLAVORS 5
6025
6026 static void
6027 overlap_helpitem(int state, int item, char *message)
6028 {
6029     int row = (item / 2);
6030     int col = ((item % 2) ? COLS / 2 : 0);
6031
6032     move(LINES - 6 + row, col);
6033     printw("%c%c = %s", state == row ? '>' : ' ', 'a' + item, message);
6034     clrtoeol();
6035 }
6036
6037 static void
6038 overlap_test_1_attr(WINDOW *win, int flavor, int col)
6039 {
6040     short cpair = (short) (1 + (flavor * 2) + col);
6041
6042     switch (flavor) {
6043     case 0:
6044         (void) wattrset(win, A_NORMAL);
6045         break;
6046     case 1:
6047         (void) wattrset(win, A_BOLD);
6048         break;
6049     case 2:
6050         init_pair(cpair, COLOR_BLUE, COLOR_WHITE);
6051         (void) wattrset(win, COLOR_PAIR(cpair) | A_NORMAL);
6052         break;
6053     case 3:
6054         init_pair(cpair, COLOR_WHITE, COLOR_BLUE);
6055         (void) wattrset(win, COLOR_PAIR(cpair) | A_BOLD);
6056         break;
6057     }
6058 }
6059
6060 static void
6061 overlap_test_2_attr(WINDOW *win, int flavor, int col)
6062 {
6063     short cpair = (short) (9 + (flavor * 2) + col);
6064
6065     switch (flavor) {
6066     case 0:
6067         /* no effect */
6068         break;
6069     case 1:
6070         /* no effect */
6071         break;
6072     case 2:
6073         init_pair(cpair, COLOR_RED, COLOR_GREEN);
6074         wbkgdset(win, colored_chtype(' ', A_BLINK, cpair));
6075         break;
6076     case 3:
6077         wbkgdset(win, ' ' | A_NORMAL);
6078         break;
6079     }
6080 }
6081
6082 static int
6083 overlap_help(int state, int flavors[OVERLAP_FLAVORS])
6084 {
6085     int row;
6086     int col;
6087     int item;
6088     const char *ths, *tht;
6089     char msg[80];
6090
6091     if (state < 0)
6092         state += OVERLAP_FLAVORS;
6093     state = state % OVERLAP_FLAVORS;
6094     assert(state >= 0 && state < OVERLAP_FLAVORS);
6095
6096     for (item = 0; item < (2 * OVERLAP_FLAVORS); ++item) {
6097         row = item / 2;
6098         col = item % 2;
6099         ths = col ? "B" : "A";
6100         tht = col ? "A" : "B";
6101
6102         switch (row) {
6103         case 0:
6104             flavors[row] = 0;
6105             sprintf(msg, "refresh %s, then %s, then doupdate.", ths, tht);
6106             break;
6107         case 1:
6108             if (use_colors) {
6109                 flavors[row] %= 4;
6110             } else {
6111                 flavors[row] %= 2;
6112             }
6113             overlap_test_1_attr(stdscr, flavors[row], col);
6114             sprintf(msg, "fill window %s with letter %s.", ths, ths);
6115             break;
6116         case 2:
6117             if (use_colors) {
6118                 flavors[row] %= 4;
6119             } else {
6120                 flavors[row] %= 2;
6121             }
6122             switch (flavors[row]) {
6123             case 0:
6124                 sprintf(msg, "cross pattern in window %s.", ths);
6125                 break;
6126             case 1:
6127                 sprintf(msg, "draw box in window %s.", ths);
6128                 break;
6129             case 2:
6130                 sprintf(msg, "set background of window %s.", ths);
6131                 break;
6132             case 3:
6133                 sprintf(msg, "reset background of window %s.", ths);
6134                 break;
6135             }
6136             break;
6137         case 3:
6138             flavors[row] = 0;
6139             sprintf(msg, "clear window %s.", ths);
6140             break;
6141         case 4:
6142             flavors[row] %= 4;
6143             switch (flavors[row]) {
6144             case 0:
6145                 sprintf(msg, "overwrite %s onto %s.", ths, tht);
6146                 break;
6147             case 1:
6148                 sprintf(msg, "copywin(FALSE) %s onto %s.", ths, tht);
6149                 break;
6150             case 2:
6151                 sprintf(msg, "copywin(TRUE) %s onto %s.", ths, tht);
6152                 break;
6153             case 3:
6154                 sprintf(msg, "overlay %s onto %s.", ths, tht);
6155                 break;
6156             }
6157             break;
6158         }
6159         overlap_helpitem(state, item, msg);
6160         (void) wattrset(stdscr, A_NORMAL);
6161         wbkgdset(stdscr, ' ' | A_NORMAL);
6162     }
6163     move(LINES - 1, 0);
6164     printw("^Q/ESC = terminate test.  Up/down/space select test variations (%d %d).",
6165            state, flavors[state]);
6166
6167     return state;
6168 }
6169
6170 static void
6171 overlap_test_0(WINDOW *a, WINDOW *b)
6172 {
6173     touchwin(a);
6174     touchwin(b);
6175     wnoutrefresh(a);
6176     wnoutrefresh(b);
6177     doupdate();
6178 }
6179
6180 static void
6181 overlap_test_1(int flavor, int col, WINDOW *a, char fill)
6182 {
6183     overlap_test_1_attr(a, flavor, col);
6184     fillwin(a, fill);
6185     (void) wattrset(a, A_NORMAL);
6186 }
6187
6188 static void
6189 overlap_test_2(int flavor, int col, WINDOW *a, char fill)
6190 {
6191     overlap_test_2_attr(a, flavor, col);
6192     switch (flavor) {
6193     case 0:
6194         crosswin(a, fill);
6195         break;
6196     case 1:
6197         box(a, 0, 0);
6198         break;
6199     case 2:
6200         /* done in overlap_test_2_attr */
6201         break;
6202     case 3:
6203         /* done in overlap_test_2_attr */
6204         break;
6205     }
6206 }
6207
6208 static void
6209 overlap_test_3(WINDOW *a)
6210 {
6211     wclear(a);
6212     wmove(a, 0, 0);
6213 }
6214
6215 static void
6216 overlap_test_4(int flavor, WINDOW *a, WINDOW *b)
6217 {
6218     switch (flavor) {
6219     case 0:
6220         overwrite(a, b);
6221         break;
6222     case 1:
6223         copywin(a, b, 0, 0, 0, 0, getmaxy(b), getmaxx(b), FALSE);
6224         break;
6225     case 2:
6226         copywin(a, b, 0, 0, 0, 0, getmaxy(b), getmaxx(b), TRUE);
6227         break;
6228     case 3:
6229         overlay(a, b);
6230         break;
6231     }
6232 }
6233
6234 /* test effects of overlapping windows */
6235 static void
6236 overlap_test(void)
6237 {
6238     int ch;
6239     int state, flavor[OVERLAP_FLAVORS];
6240
6241     WINDOW *win1 = newwin(9, 20, 3, 3);
6242     WINDOW *win2 = newwin(9, 20, 9, 16);
6243
6244     curs_set(0);
6245     raw();
6246     refresh();
6247     move(0, 0);
6248     printw("This test shows the behavior of wnoutrefresh() with respect to\n");
6249     printw("the shared region of two overlapping windows A and B.  The cross\n");
6250     printw("pattern in each window does not overlap the other.\n");
6251
6252     memset(flavor, 0, sizeof(flavor));
6253     state = overlap_help(0, flavor);
6254
6255     while (!isQuit(ch = Getchar()))
6256         switch (ch) {
6257         case 'a':               /* refresh window A first, then B */
6258             overlap_test_0(win1, win2);
6259             break;
6260
6261         case 'b':               /* refresh window B first, then A */
6262             overlap_test_0(win2, win1);
6263             break;
6264
6265         case 'c':               /* fill window A so it's visible */
6266             overlap_test_1(flavor[1], 0, win1, 'A');
6267             break;
6268
6269         case 'd':               /* fill window B so it's visible */
6270             overlap_test_1(flavor[1], 1, win2, 'B');
6271             break;
6272
6273         case 'e':               /* cross test pattern in window A */
6274             overlap_test_2(flavor[2], 0, win1, 'A');
6275             break;
6276
6277         case 'f':               /* cross test pattern in window A */
6278             overlap_test_2(flavor[2], 1, win2, 'B');
6279             break;
6280
6281         case 'g':               /* clear window A */
6282             overlap_test_3(win1);
6283             break;
6284
6285         case 'h':               /* clear window B */
6286             overlap_test_3(win2);
6287             break;
6288
6289         case 'i':               /* overwrite A onto B */
6290             overlap_test_4(flavor[4], win1, win2);
6291             break;
6292
6293         case 'j':               /* overwrite B onto A */
6294             overlap_test_4(flavor[4], win2, win1);
6295             break;
6296
6297         case CTRL('n'):
6298         case KEY_DOWN:
6299             state = overlap_help(state + 1, flavor);
6300             break;
6301
6302         case CTRL('p'):
6303         case KEY_UP:
6304             state = overlap_help(state - 1, flavor);
6305             break;
6306
6307         case ' ':
6308             flavor[state] += 1;
6309             state = overlap_help(state, flavor);
6310             break;
6311
6312         case '?':
6313             state = overlap_help(state, flavor);
6314             break;
6315
6316         default:
6317             beep();
6318             break;
6319         }
6320
6321     delwin(win2);
6322     delwin(win1);
6323     erase();
6324     curs_set(1);
6325     endwin();
6326 }
6327
6328 /****************************************************************************
6329  *
6330  * Main sequence
6331  *
6332  ****************************************************************************/
6333
6334 static bool
6335 do_single_test(const char c)
6336 /* perform a single specified test */
6337 {
6338     switch (c) {
6339     case 'a':
6340         getch_test();
6341         break;
6342
6343 #if USE_WIDEC_SUPPORT
6344     case 'A':
6345         get_wch_test();
6346         break;
6347 #endif
6348
6349     case 'b':
6350         attr_test();
6351         break;
6352
6353 #if USE_WIDEC_SUPPORT
6354     case 'B':
6355         wide_attr_test();
6356         break;
6357 #endif
6358
6359     case 'c':
6360         if (!use_colors)
6361             Cannot("does not support color.");
6362         else
6363             color_test();
6364         break;
6365
6366 #if USE_WIDEC_SUPPORT
6367     case 'C':
6368         if (!use_colors)
6369             Cannot("does not support color.");
6370         else
6371             wide_color_test();
6372         break;
6373 #endif
6374
6375     case 'd':
6376         if (!use_colors)
6377             Cannot("does not support color.");
6378         else if (!can_change_color())
6379             Cannot("has hardwired color values.");
6380         else
6381             color_edit();
6382         break;
6383
6384 #if USE_SOFTKEYS
6385     case 'e':
6386         slk_test();
6387         break;
6388 #endif
6389
6390 #if USE_WIDEC_SUPPORT
6391     case 'E':
6392         wide_slk_test();
6393         break;
6394 #endif
6395     case 'f':
6396         acs_display();
6397         break;
6398
6399 #if USE_WIDEC_SUPPORT
6400     case 'F':
6401         wide_acs_display();
6402         break;
6403 #endif
6404
6405 #if USE_LIBPANEL
6406     case 'o':
6407         demo_panels(init_panel, fill_panel);
6408         break;
6409 #endif
6410
6411 #if USE_WIDEC_SUPPORT && USE_LIBPANEL
6412     case 'O':
6413         demo_panels(init_wide_panel, fill_wide_panel);
6414         break;
6415 #endif
6416
6417     case 'g':
6418         acs_and_scroll();
6419         break;
6420
6421     case 'i':
6422         flushinp_test(stdscr);
6423         break;
6424
6425     case 'k':
6426         test_sgr_attributes();
6427         break;
6428
6429 #if USE_LIBMENU
6430     case 'm':
6431         menu_test();
6432         break;
6433 #endif
6434
6435     case 'p':
6436         demo_pad();
6437         break;
6438
6439 #if USE_LIBFORM
6440     case 'r':
6441         demo_forms();
6442         break;
6443 #endif
6444
6445     case 's':
6446         overlap_test();
6447         break;
6448
6449 #if USE_LIBMENU && defined(TRACE)
6450     case 't':
6451         trace_set();
6452         break;
6453 #endif
6454
6455     case '?':
6456         break;
6457
6458     default:
6459         return FALSE;
6460     }
6461
6462     return TRUE;
6463 }
6464
6465 static void
6466 usage(void)
6467 {
6468     static const char *const tbl[] =
6469     {
6470         "Usage: ncurses [options]"
6471         ,""
6472         ,"Options:"
6473 #ifdef NCURSES_VERSION
6474         ,"  -a f,b   set default-colors (assumed white-on-black)"
6475         ,"  -d       use default-colors if terminal supports them"
6476 #endif
6477 #if USE_SOFTKEYS
6478         ,"  -e fmt   specify format for soft-keys test (e)"
6479 #endif
6480 #if HAVE_RIPOFFLINE
6481         ,"  -f       rip-off footer line (can repeat)"
6482         ,"  -h       rip-off header line (can repeat)"
6483 #endif
6484         ,"  -m       do not use colors"
6485         ,"  -p file  rgb values to use in 'd' rather than ncurses's builtin"
6486 #if USE_LIBPANEL
6487         ,"  -s msec  specify nominal time for panel-demo (default: 1, to hold)"
6488 #endif
6489 #ifdef TRACE
6490         ,"  -t mask  specify default trace-level (may toggle with ^T)"
6491 #endif
6492     };
6493     size_t n;
6494     for (n = 0; n < SIZEOF(tbl); n++)
6495         fprintf(stderr, "%s\n", tbl[n]);
6496     ExitProgram(EXIT_FAILURE);
6497 }
6498
6499 static void
6500 set_terminal_modes(void)
6501 {
6502     noraw();
6503     cbreak();
6504     noecho();
6505     scrollok(stdscr, TRUE);
6506     idlok(stdscr, TRUE);
6507     keypad(stdscr, TRUE);
6508 }
6509
6510 #ifdef SIGUSR1
6511 static RETSIGTYPE
6512 announce_sig(int sig)
6513 {
6514     (void) fprintf(stderr, "Handled signal %d\r\n", sig);
6515 }
6516 #endif
6517
6518 #if HAVE_RIPOFFLINE
6519 static int
6520 rip_footer(WINDOW *win, int cols)
6521 {
6522     wbkgd(win, A_REVERSE);
6523     werase(win);
6524     wmove(win, 0, 0);
6525     wprintw(win, "footer: window %p, %d columns", (void *) win, cols);
6526     wnoutrefresh(win);
6527     return OK;
6528 }
6529
6530 static int
6531 rip_header(WINDOW *win, int cols)
6532 {
6533     wbkgd(win, A_REVERSE);
6534     werase(win);
6535     wmove(win, 0, 0);
6536     wprintw(win, "header: window %p, %d columns", (void *) win, cols);
6537     wnoutrefresh(win);
6538     return OK;
6539 }
6540 #endif /* HAVE_RIPOFFLINE */
6541
6542 static void
6543 main_menu(bool top)
6544 {
6545     char command;
6546
6547     do {
6548         (void) puts("This is the ncurses main menu");
6549         (void) puts("a = keyboard and mouse input test");
6550 #if USE_WIDEC_SUPPORT
6551         (void) puts("A = wide-character keyboard and mouse input test");
6552 #endif
6553         (void) puts("b = character attribute test");
6554 #if USE_WIDEC_SUPPORT
6555         (void) puts("B = wide-character attribute test");
6556 #endif
6557         (void) puts("c = color test pattern");
6558 #if USE_WIDEC_SUPPORT
6559         (void) puts("C = color test pattern using wide-character calls");
6560 #endif
6561         if (top)
6562             (void) puts("d = edit RGB color values");
6563 #if USE_SOFTKEYS
6564         (void) puts("e = exercise soft keys");
6565 #if USE_WIDEC_SUPPORT
6566         (void) puts("E = exercise soft keys using wide-characters");
6567 #endif
6568 #endif
6569         (void) puts("f = display ACS characters");
6570 #if USE_WIDEC_SUPPORT
6571         (void) puts("F = display Wide-ACS characters");
6572 #endif
6573         (void) puts("g = display windows and scrolling");
6574         (void) puts("i = test of flushinp()");
6575         (void) puts("k = display character attributes");
6576 #if USE_LIBMENU
6577         (void) puts("m = menu code test");
6578 #endif
6579 #if USE_LIBPANEL
6580         (void) puts("o = exercise panels library");
6581 #if USE_WIDEC_SUPPORT
6582         (void) puts("O = exercise panels with wide-characters");
6583 #endif
6584 #endif
6585         (void) puts("p = exercise pad features");
6586         (void) puts("q = quit");
6587 #if USE_LIBFORM
6588         (void) puts("r = exercise forms code");
6589 #endif
6590         (void) puts("s = overlapping-refresh test");
6591 #if USE_LIBMENU && defined(TRACE)
6592         (void) puts("t = set trace level");
6593 #endif
6594         (void) puts("? = repeat this command summary");
6595
6596         (void) fputs("> ", stdout);
6597         (void) fflush(stdout);  /* necessary under SVr4 curses */
6598
6599         /*
6600          * This used to be an 'fgets()' call (until 1996/10).  However with
6601          * some runtime libraries, mixing stream I/O and 'read()' causes the
6602          * input stream to be flushed when switching between the two.
6603          */
6604         command = 0;
6605         for (;;) {
6606             char ch = '\0';
6607             if (read(fileno(stdin), &ch, 1) <= 0) {
6608                 if (command == 0)
6609                     command = 'q';
6610                 break;
6611             } else if (command == 0 && !isspace(UChar(ch))) {
6612                 command = ch;
6613             } else if (ch == '\n' || ch == '\r') {
6614                 if ((command == 'd') && !top) {
6615                     (void) fputs("Do not nest test-d\n", stdout);
6616                     command = 0;
6617                 }
6618                 if (command != 0)
6619                     break;
6620                 (void) fputs("> ", stdout);
6621                 (void) fflush(stdout);
6622             }
6623         }
6624
6625         if (do_single_test(command)) {
6626             /*
6627              * This may be overkill; it's intended to reset everything back
6628              * to the initial terminal modes so that tests don't get in
6629              * each other's way.
6630              */
6631             flushinp();
6632             set_terminal_modes();
6633             reset_prog_mode();
6634             clear();
6635             refresh();
6636             endwin();
6637             if (command == '?') {
6638                 (void) puts("This is the ncurses capability tester.");
6639                 (void)
6640                     puts("You may select a test from the main menu by typing the");
6641                 (void)
6642                     puts("key letter of the choice (the letter to left of the =)");
6643                 (void)
6644                     puts("at the > prompt.  Type `q' to exit.");
6645             }
6646             continue;
6647         }
6648     } while
6649         (command != 'q');
6650 }
6651
6652 /*+-------------------------------------------------------------------------
6653         main(argc,argv)
6654 --------------------------------------------------------------------------*/
6655
6656 #define okCOLOR(n) ((n) >= 0 && (n) < max_colors)
6657 #define okRGB(n)   ((n) >= 0 && (n) <= 1000)
6658
6659 int
6660 main(int argc, char *argv[])
6661 {
6662     int c;
6663     int my_e_param = 1;
6664 #ifdef NCURSES_VERSION
6665     int default_fg = COLOR_WHITE;
6666     int default_bg = COLOR_BLACK;
6667     bool assumed_colors = FALSE;
6668     bool default_colors = FALSE;
6669 #endif
6670     char *palette_file = 0;
6671     bool monochrome = FALSE;
6672
6673     setlocale(LC_ALL, "");
6674
6675     while ((c = getopt(argc, argv, "a:de:fhmp:s:t:")) != -1) {
6676         switch (c) {
6677 #ifdef NCURSES_VERSION
6678         case 'a':
6679             assumed_colors = TRUE;
6680             sscanf(optarg, "%d,%d", &default_fg, &default_bg);
6681             break;
6682         case 'd':
6683             default_colors = TRUE;
6684             break;
6685 #endif
6686         case 'e':
6687             my_e_param = atoi(optarg);
6688 #ifdef NCURSES_VERSION
6689             if (my_e_param > 3) /* allow extended layouts */
6690                 usage();
6691 #else
6692             if (my_e_param > 1)
6693                 usage();
6694 #endif
6695             break;
6696 #if HAVE_RIPOFFLINE
6697         case 'f':
6698             ripoffline(-1, rip_footer);
6699             break;
6700         case 'h':
6701             ripoffline(1, rip_header);
6702             break;
6703 #endif /* HAVE_RIPOFFLINE */
6704         case 'm':
6705             monochrome = TRUE;
6706             break;
6707         case 'p':
6708             palette_file = optarg;
6709             break;
6710 #if USE_LIBPANEL
6711         case 's':
6712             nap_msec = atol(optarg);
6713             break;
6714 #endif
6715 #ifdef TRACE
6716         case 't':
6717             save_trace = (unsigned) strtol(optarg, 0, 0);
6718             break;
6719 #endif
6720         default:
6721             usage();
6722         }
6723     }
6724
6725     /*
6726      * If there's no menus (unlikely for ncurses!), then we'll have to set
6727      * tracing on initially, just in case the user wants to test something that
6728      * doesn't involve wGetchar.
6729      */
6730 #ifdef TRACE
6731     /* enable debugging */
6732 #if !USE_LIBMENU
6733     trace(save_trace);
6734 #else
6735     if (!isatty(fileno(stdin)))
6736         trace(save_trace);
6737 #endif /* USE_LIBMENU */
6738 #endif /* TRACE */
6739
6740 #if USE_SOFTKEYS
6741     /* tell it we're going to play with soft keys */
6742     slk_init(my_e_param);
6743 #endif
6744
6745 #ifdef SIGUSR1
6746     /* set up null signal catcher so we can see what interrupts to getch do */
6747     signal(SIGUSR1, announce_sig);
6748 #endif
6749
6750     /* we must initialize the curses data structure only once */
6751     initscr();
6752     bkgdset(BLANK);
6753
6754     /* tests, in general, will want these modes */
6755     use_colors = monochrome ? FALSE : has_colors();
6756
6757     if (use_colors) {
6758         start_color();
6759 #ifdef NCURSES_VERSION_PATCH
6760         max_colors = COLORS;    /* was > 16 ? 16 : COLORS */
6761 #if HAVE_USE_DEFAULT_COLORS
6762         if (default_colors) {
6763             use_default_colors();
6764             min_colors = -1;
6765         }
6766 #if NCURSES_VERSION_PATCH >= 20000708
6767         if (assumed_colors)
6768             assume_default_colors(default_fg, default_bg);
6769 #endif
6770 #endif
6771 #else /* normal SVr4 curses */
6772         max_colors = COLORS;    /* was > 8 ? 8 : COLORS */
6773 #endif
6774         max_pairs = COLOR_PAIRS;        /* was > 256 ? 256 : COLOR_PAIRS */
6775
6776         if (can_change_color()) {
6777             short cp;
6778             all_colors = typeMalloc(RGB_DATA, (unsigned) max_colors);
6779             for (cp = 0; cp < max_colors; ++cp) {
6780                 color_content(cp,
6781                               &all_colors[cp].red,
6782                               &all_colors[cp].green,
6783                               &all_colors[cp].blue);
6784             }
6785             if (palette_file != 0) {
6786                 FILE *fp = fopen(palette_file, "r");
6787                 if (fp != 0) {
6788                     char buffer[BUFSIZ];
6789                     int red, green, blue;
6790                     int scale = 1000;
6791                     while (fgets(buffer, sizeof(buffer), fp) != 0) {
6792                         if (sscanf(buffer, "scale:%d", &c) == 1) {
6793                             scale = c;
6794                         } else if (sscanf(buffer, "%d:%d %d %d",
6795                                           &c,
6796                                           &red,
6797                                           &green,
6798                                           &blue) == 4
6799                                    && okCOLOR(c)
6800                                    && okRGB(red)
6801                                    && okRGB(green)
6802                                    && okRGB(blue)) {
6803                             all_colors[c].red = (short) ((red * 1000) / scale);
6804                             all_colors[c].green = (short) ((green * 1000) / scale);
6805                             all_colors[c].blue = (short) ((blue * 1000) / scale);
6806                         }
6807                     }
6808                     fclose(fp);
6809                 }
6810             }
6811         }
6812     }
6813     set_terminal_modes();
6814     def_prog_mode();
6815
6816     /*
6817      * Return to terminal mode, so we're guaranteed of being able to
6818      * select terminal commands even if the capabilities are wrong.
6819      */
6820     endwin();
6821
6822 #if HAVE_CURSES_VERSION
6823     (void) printf("Welcome to %s.  Press ? for help.\n", curses_version());
6824 #elif defined(NCURSES_VERSION_MAJOR) && defined(NCURSES_VERSION_MINOR) && defined(NCURSES_VERSION_PATCH)
6825     (void) printf("Welcome to ncurses %d.%d.%d.  Press ? for help.\n",
6826                   NCURSES_VERSION_MAJOR,
6827                   NCURSES_VERSION_MINOR,
6828                   NCURSES_VERSION_PATCH);
6829 #else
6830     (void) puts("Welcome to ncurses.  Press ? for help.");
6831 #endif
6832
6833     main_menu(TRUE);
6834
6835     ExitProgram(EXIT_SUCCESS);
6836 }
6837
6838 /* ncurses.c ends here */