]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/ncurses.c
ncurses 5.7 - patch 20081206
[ncurses.git] / test / ncurses.c
index 05e754e973a570a482dcdb82c6886af496027585..6d0d9355b9c06debf88d9011f002821b3e674ff3 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.332 2008/11/29 20:08:42 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;
                    }
                }
@@ -3212,7 +3257,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;