]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/ncurses.c
ncurses 5.7 - patch 20081220
[ncurses.git] / test / ncurses.c
index 05e754e973a570a482dcdb82c6886af496027585..c879d2c826ce8d5425009a80ce20c01a708aac91 100644 (file)
@@ -40,7 +40,7 @@ AUTHOR
    Author: Eric S. Raymond <esr@snark.thyrsus.com> 1993
            Thomas E. Dickey (beginning revision 1.27 in 1996).
 
-$Id: ncurses.c,v 1.324 2008/08/04 16:27:54 tom Exp $
+$Id: ncurses.c,v 1.334 2008/12/20 17:13:27 tom Exp $
 
 ***************************************************************************/
 
@@ -118,6 +118,28 @@ extern unsigned _nc_tracing;
 
 #endif
 
+#if HAVE_WCSRTOMBS
+#define count_wchars(src, len, state)      wcsrtombs(0,   &src, len, state)
+#define trans_wchars(dst, src, len, state) wcsrtombs(dst, &src, len, state)
+#define reset_wchars(state) memset(&state, 0, sizeof(state))
+#elif HAVE_WCSTOMBS && HAVE_MBTOWC && HAVE_MBLEN
+#define count_wchars(src, len, state)      wcstombs(0,   src, len)
+#define trans_wchars(dst, src, len, state) wcstombs(dst, src, len)
+#define reset_wchars(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
+#define state_unused
+#endif
+
+#if HAVE_MBSRTOWCS
+#define count_mbytes(src, len, state)      mbsrtowcs(0,   &src, len, state)
+#define trans_mbytes(dst, src, len, state) mbsrtowcs(dst, &src, len, state)
+#define reset_mbytes(state) memset(&state, 0, sizeof(state))
+#elif HAVE_MBSTOWCS && HAVE_MBTOWC && HAVE_MBLEN
+#define count_mbytes(src, len, state)      mbstowcs(0,   src, len)
+#define trans_mbytes(dst, src, len, state) mbstowcs(dst, src, len)
+#define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
+#define state_unused
+#endif
+
 #define ToggleAcs(temp,real) temp = ((temp == real) ? 0 : real)
 
 #define P(string)      printw("%s\n", string)
@@ -547,8 +569,11 @@ mouse_decode(MEVENT const *ep)
  *
  ****************************************************************************/
 
+#define NUM_GETCH_FLAGS 256
+typedef bool GetchFlags[NUM_GETCH_FLAGS];
+
 static void
-setup_getch(WINDOW *win, bool flags[])
+setup_getch(WINDOW *win, GetchFlags flags)
 {
     keypad(win, flags['k']);   /* should be redundant, but for testing */
     meta(win, flags['m']);     /* force this to a known state */
@@ -559,7 +584,17 @@ setup_getch(WINDOW *win, bool flags[])
 }
 
 static void
-wgetch_help(WINDOW *win, bool flags[])
+init_getch(WINDOW *win, GetchFlags flags)
+{
+    memset(flags, FALSE, NUM_GETCH_FLAGS);
+    flags[UChar('k')] = (win == stdscr);
+    flags[UChar('m')] = TRUE;
+
+    setup_getch(win, flags);
+}
+
+static void
+wgetch_help(WINDOW *win, GetchFlags flags)
 {
     static const char *help[] =
     {
@@ -709,13 +744,10 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
     int first_y, first_x;
     int c;
     int incount = 0;
-    bool flags[256];
+    GetchFlags flags;
     bool blocking = (delay < 0);
 
-    memset(flags, FALSE, sizeof(flags));
-    flags[UChar('k')] = (win == stdscr);
-
-    setup_getch(win, flags);
+    init_getch(win, flags);
     wtimeout(win, delay);
     getyx(win, first_y, first_x);
 
@@ -813,12 +845,18 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
                }
 #endif
                (void) waddstr(win, keyname(c));
-           } else if (c > 0x80) {
-               unsigned c2 = (unsigned) (c & 0x7f);
-               if (isprint(c2))
-                   (void) wprintw(win, "M-%c", UChar(c2));
-               else
+           } else if (c >= 0x80) {
+               unsigned c2 = (unsigned) c;
+#if !(defined(NCURSES_VERSION) || defined(_XOPEN_CURSES))
+               /* at least Solaris SVR4 curses breaks unctrl(128), etc. */
+               c2 &= 0x7f;
+#endif
+               if (isprint(c))
+                   (void) wprintw(win, "%c", UChar(c));
+               else if (c2 != UChar(c))
                    (void) wprintw(win, "M-%s", unctrl(c2));
+               else
+                   (void) wprintw(win, "%s", unctrl(c2));
                waddstr(win, " (high-half character)");
            } else {
                if (isprint(c))
@@ -832,6 +870,9 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
     }
 
     wtimeout(win, -1);
+
+    if (!level)
+       init_getch(win, flags);
 }
 
 static int
@@ -928,16 +969,18 @@ static char *
 wcstos(const wchar_t *src)
 {
     int need;
-    mbstate_t state;
     char *result = 0;
     const wchar_t *tmp = src;
+#ifndef state_unused
+    mbstate_t state;
+#endif
 
-    memset(&state, 0, sizeof(state));
-    if ((need = (int) wcsrtombs(0, &tmp, 0, &state)) > 0) {
+    reset_wchars(state);
+    if ((need = (int) count_wchars(tmp, 0, &state)) > 0) {
        unsigned have = (unsigned) need;
        if ((result = typeCalloc(char, have + 1)) != 0) {
            tmp = src;
-           if (wcsrtombs(result, &tmp, have, &state) != have) {
+           if (trans_wchars(result, tmp, have, &state) != have) {
                free(result);
                result = 0;
            }
@@ -954,15 +997,12 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
     int first_y, first_x;
     wint_t c;
     int incount = 0;
-    bool flags[256];
+    GetchFlags flags;
     bool blocking = (delay < 0);
     int y, x, code;
     char *temp;
 
-    memset(flags, FALSE, sizeof(flags));
-    flags[UChar('k')] = (win == stdscr);
-
-    setup_getch(win, flags);
+    init_getch(win, flags);
     wtimeout(win, delay);
     getyx(win, first_y, first_x);
 
@@ -1072,14 +1112,14 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
                    resize_wide_boxes(level, win);
                }
 #endif
-               (void) waddstr(win, key_name((wchar_t) c));
+               (void) waddstr(win, keyname((wchar_t) c));
            } else {
+               (void) waddstr(win, key_name((wchar_t) c));
                if (c < 256 && iscntrl(c)) {
-                   (void) wprintw(win, "%s (control character)", unctrl(c));
+                   (void) wprintw(win, " (control character)");
                } else {
-                   wchar_t c2 = (wchar_t) c;
-                   waddnwstr(win, &c2, 1);
-                   (void) wprintw(win, " = %#x (printable character)", c);
+                   (void) wprintw(win, " = %#x (printable character)",
+                                  (unsigned) c);
                }
            }
            wgetch_wrap(win, first_y);
@@ -1087,6 +1127,9 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
     }
 
     wtimeout(win, -1);
+
+    if (!level)
+       init_getch(win, flags);
 }
 
 static void
@@ -2489,7 +2532,7 @@ slk_help(void)
 #if HAVE_SLK_COLOR
        ,"F/B        -- cycle through foreground/background colors"
 #endif
-       ,"ESC  -- return to main menu"
+       ,"ESC        -- return to main menu"
        ,""
        ,"Note: if activating the soft keys causes your terminal to scroll up"
        ,"one line, your terminal auto-scrolls when anything is written to the"
@@ -2705,20 +2748,22 @@ wide_slk_test(void)
                size_t used = strlen(temp);
                size_t want = SLKLEN;
                size_t test;
+#ifndef state_unused
                mbstate_t state;
+#endif
 
                buf[0] = L'\0';
                while (want > 0 && used != 0) {
                    const char *base = s;
-                   memset(&state, 0, sizeof(state));
-                   test = mbsrtowcs(0, &base, 0, &state);
+                   reset_mbytes(state);
+                   test = count_mbytes(base, 0, &state);
                    if (test == (size_t) -1) {
                        temp[--used] = 0;
                    } else if (test > want) {
                        temp[--used] = 0;
                    } else {
-                       memset(&state, 0, sizeof(state));
-                       mbsrtowcs(buf, &base, want, &state);
+                       reset_mbytes(state);
+                       trans_mbytes(buf, base, want, &state);
                        break;
                    }
                }
@@ -2776,6 +2821,7 @@ static struct {
 } attrs_to_cycle[] = {
     { A_NORMAL,                "normal" },
     { A_BOLD,          "bold" },
+    { A_BLINK,         "blink" },
     { A_REVERSE,       "reverse" },
     { A_UNDERLINE,     "underline" },
 };
@@ -2876,7 +2922,7 @@ show_upper_chars(unsigned first, int repeat, attr_t attr, short pair)
        do {
            if (C1)
                nodelay(stdscr, TRUE);
-           echochar(code | attr | COLOR_PAIR(pair));
+           echochar(colored_chtype(code, attr, pair));
            if (C1) {
                /* (yes, this _is_ crude) */
                while ((reply = Getchar()) != ERR) {
@@ -2925,7 +2971,7 @@ show_pc_chars(int repeat, attr_t attr, short pair)
                 */
                break;
            default:
-               addch(code | A_ALTCHARSET | attr | COLOR_PAIR(pair));
+               addch(colored_chtype(code, A_ALTCHARSET | attr, pair));
                break;
            }
        } while (--count > 0);
@@ -2943,15 +2989,23 @@ show_box_chars(int repeat, attr_t attr, short pair)
     mvaddstr(0, 20, "Display of the ACS Line-Drawing Set");
     attroff(A_BOLD);
     refresh();
-    box(stdscr, 0, 0);
     /* *INDENT-OFF* */
-    mvhline(LINES / 2, 0,        ACS_HLINE | attr, COLS);
-    mvvline(0,         COLS / 2, ACS_VLINE | attr, LINES);
-    mvaddch(0,         COLS / 2, ACS_TTEE | attr);
-    mvaddch(LINES / 2, COLS / 2, ACS_PLUS | attr);
-    mvaddch(LINES - 1, COLS / 2, ACS_BTEE | attr);
-    mvaddch(LINES / 2, 0,        ACS_LTEE | attr);
-    mvaddch(LINES / 2, COLS - 1, ACS_RTEE | attr);
+    wborder(stdscr,
+           colored_chtype(ACS_VLINE,    attr, pair),
+           colored_chtype(ACS_VLINE,    attr, pair),
+            colored_chtype(ACS_HLINE,    attr, pair),
+           colored_chtype(ACS_HLINE,    attr, pair),
+           colored_chtype(ACS_ULCORNER, attr, pair),
+           colored_chtype(ACS_URCORNER, attr, pair),
+            colored_chtype(ACS_LLCORNER, attr, pair),
+           colored_chtype(ACS_LRCORNER, attr, pair));
+    mvhline(LINES / 2, 0,        colored_chtype(ACS_HLINE, attr, pair), COLS);
+    mvvline(0,         COLS / 2, colored_chtype(ACS_VLINE, attr, pair), LINES);
+    mvaddch(0,         COLS / 2, colored_chtype(ACS_TTEE,  attr, pair));
+    mvaddch(LINES / 2, COLS / 2, colored_chtype(ACS_PLUS,  attr, pair));
+    mvaddch(LINES - 1, COLS / 2, colored_chtype(ACS_BTEE,  attr, pair));
+    mvaddch(LINES / 2, 0,        colored_chtype(ACS_LTEE,  attr, pair));
+    mvaddch(LINES / 2, COLS - 1, colored_chtype(ACS_RTEE,  attr, pair));
     /* *INDENT-ON* */
 
 }
@@ -2976,7 +3030,7 @@ show_acs_chars(int repeat, attr_t attr, short pair)
 {
     int n;
 
-#define BOTH(name) #name, (name | attr | COLOR_PAIR(pair))
+#define BOTH(name) #name, colored_chtype(name, attr, pair)
 
     erase();
     attron(A_BOLD);
@@ -3212,7 +3266,7 @@ show_1_wacs(int n, int repeat, const char *name, const cchar_t *code)
     int col = (n / height) * COLS / 2;
 
     mvprintw(row, col, "%*s : ", COLS / 4, name);
-    while (repeat-- >= 0) {
+    while (--repeat >= 0) {
        add_wch(code);
     }
     return n + 1;
@@ -3281,12 +3335,12 @@ show_wacs_chars(int repeat, attr_t attr, short pair)
 
 #undef MERGE_ATTR
 
-#define MERGE_ATTR(wch) merge_wide_attr(&temp, wch, attr, pair)
+#define MERGE_ATTR(n,wch) merge_wide_attr(&temp[n], wch, attr, pair)
 
 static void
 show_wbox_chars(int repeat, attr_t attr, short pair)
 {
-    cchar_t temp;
+    cchar_t temp[8];
 
     (void) repeat;
     erase();
@@ -3295,17 +3349,23 @@ show_wbox_chars(int repeat, attr_t attr, short pair)
     attroff(A_BOLD);
     refresh();
 
-    attr_set(attr, pair, 0);
-    box_set(stdscr, 0, 0);
-    attr_set(A_NORMAL, 0, 0);
+    wborder_set(stdscr,
+               MERGE_ATTR(0, WACS_VLINE),
+               MERGE_ATTR(1, WACS_VLINE),
+               MERGE_ATTR(2, WACS_HLINE),
+               MERGE_ATTR(3, WACS_HLINE),
+               MERGE_ATTR(4, WACS_ULCORNER),
+               MERGE_ATTR(5, WACS_URCORNER),
+               MERGE_ATTR(6, WACS_LLCORNER),
+               MERGE_ATTR(7, WACS_LRCORNER));
     /* *INDENT-OFF* */
-    mvhline_set(LINES / 2, 0,        MERGE_ATTR(WACS_HLINE), COLS);
-    mvvline_set(0,         COLS / 2, MERGE_ATTR(WACS_VLINE), LINES);
-    mvadd_wch(0,           COLS / 2, MERGE_ATTR(WACS_TTEE));
-    mvadd_wch(LINES / 2,   COLS / 2, MERGE_ATTR(WACS_PLUS));
-    mvadd_wch(LINES - 1,   COLS / 2, MERGE_ATTR(WACS_BTEE));
-    mvadd_wch(LINES / 2,   0,        MERGE_ATTR(WACS_LTEE));
-    mvadd_wch(LINES / 2,   COLS - 1, MERGE_ATTR(WACS_RTEE));
+    mvhline_set(LINES / 2, 0,        MERGE_ATTR(0, WACS_HLINE), COLS);
+    mvvline_set(0,         COLS / 2, MERGE_ATTR(0, WACS_VLINE), LINES);
+    mvadd_wch(0,           COLS / 2, MERGE_ATTR(0, WACS_TTEE));
+    mvadd_wch(LINES / 2,   COLS / 2, MERGE_ATTR(0, WACS_PLUS));
+    mvadd_wch(LINES - 1,   COLS / 2, MERGE_ATTR(0, WACS_BTEE));
+    mvadd_wch(LINES / 2,   0,        MERGE_ATTR(0, WACS_LTEE));
+    mvadd_wch(LINES / 2,   COLS - 1, MERGE_ATTR(0, WACS_RTEE));
     /* *INDENT-ON* */
 
 }
@@ -5778,7 +5838,7 @@ overlap_test_2_attr(WINDOW *win, int flavor, int col)
        break;
     case 2:
        init_pair(cpair, COLOR_RED, COLOR_GREEN);
-       wbkgdset(win, ' ' | A_BLINK | COLOR_PAIR(cpair));
+       wbkgdset(win, colored_chtype(' ', A_BLINK, cpair));
        break;
     case 3:
        wbkgdset(win, ' ' | A_NORMAL);