]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_add_wchstr.c
ncurses 6.0 - patch 20170930
[ncurses.git] / test / test_add_wchstr.c
index 50af92f22f7035dc00b2c43e4a702aefb222bac7..3e7f1bafcd80ab1b5a56db5a278b7811c778be78 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2009,2010 Free Software Foundation, Inc.                   *
+ * Copyright (c) 2009-2016,2017 Free Software Foundation, Inc.                   *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -26,7 +26,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: test_add_wchstr.c,v 1.8 2010/05/01 19:13:46 tom Exp $
+ * $Id: test_add_wchstr.c,v 1.26 2017/04/15 15:15:25 tom Exp $
  *
  * Demonstrate the waddwchstr() and wadd_wch functions.
  * Thomas Dickey - 2009/9/12
 #define WIDE_LINEDATA
 #include <linedata.h>
 
+#undef AddCh
 #undef MvAddCh
 #undef MvAddStr
 #undef MvWAddCh
+#undef MvWAddChStr
 #undef MvWAddStr
+#undef WAddCh
 
-/* definitions to make it simpler to compare with test_addstr.c */
+/*
+ * redefinitions to simplify comparison between test_*str programs
+ */
 #define AddNStr    add_wchnstr
 #define AddStr     add_wchstr
 #define MvAddNStr  (void) mvadd_wchnstr
 #define MvAddStr   (void) mvadd_wchstr
 #define MvWAddNStr (void) mvwadd_wchnstr
 #define MvWAddStr  (void) mvwadd_wchstr
+#define MvWAddChStr(w,y,x,s)   (void) mvwadd_wchstr((w),(y),(x),(s))
 #define WAddNStr   wadd_wchnstr
 #define WAddStr    wadd_wchstr
 
@@ -73,6 +79,7 @@ typedef enum {
 } Options;
 
 static bool m_opt = FALSE;
+static bool pass_ctls = FALSE;
 static bool w_opt = FALSE;
 static int n_opt = -1;
 
@@ -81,6 +88,7 @@ static size_t temp_length;
 
 #define TempBuffer(source_len, source_cast) \
     if (source != 0) { \
+       const char *temp; \
        size_t need = source_len + 1; \
        wchar_t have[2]; \
        int n = 0; \
@@ -88,12 +96,25 @@ static size_t temp_length;
        if (need > temp_length) { \
            temp_length = need * 2; \
            temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
+           if (!temp_buffer) \
+               failed("TempBuffer"); \
        } \
        have[0] = 0; \
        have[1] = 0; \
        do { \
            have[0] = source_cast; \
-           setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
+           if (!pass_ctls \
+            && have[0] != 0 \
+            && have[0] < 256 \
+            && (temp = unctrl((chtype) have[0])) != 0 \
+            && strlen(temp) > 1) { \
+               while (*temp != '\0') { \
+                   have[0] = (wchar_t) *temp++; \
+                   setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
+               } \
+           } else { \
+               setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
+           } \
        } while (have[0] != 0); \
     } else if (temp_buffer != 0) { \
        free(temp_buffer); \
@@ -102,6 +123,26 @@ static size_t temp_length;
     } \
     return temp_buffer;
 
+static size_t
+ChWLen(const wchar_t *source)
+{
+    size_t result = wcslen(source);
+
+    if (!pass_ctls) {
+       size_t adjust = 0;
+       size_t n;
+       const char *s;
+
+       for (n = 0; n < result; ++n) {
+           if (source[n] < 256 && (s = unctrl((chtype) source[n])) != 0) {
+               adjust += (strlen(s) - 1);
+           }
+       }
+       result += adjust;
+    }
+    return result;
+}
+
 static cchar_t *
 ChStr(const char *source)
 {
@@ -111,13 +152,13 @@ ChStr(const char *source)
 static cchar_t *
 ChWStr(const wchar_t *source)
 {
-    TempBuffer(wcslen(source), *source++);
+    TempBuffer(ChWLen(source), *source++);
 }
 
 static void
 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
 {
-    NCURSES_CONST char *showstate;
+    const char *showstate;
 
     switch (state) {
     default:
@@ -139,7 +180,7 @@ legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
     wprintw(win,
            "The Strings/Chars displays should match.  Enter any characters, except:\n");
     wprintw(win,
-           "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
+           "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
     wclrtoeol(win);
     wprintw(win, "Level %d,%s added %d characters <", level,
            showstate, length);
@@ -175,7 +216,7 @@ ColOf(wchar_t *buffer, int length, int margin)
            result += 2;
            break;
        default:
-           result += wcwidth(ch);
+           result += wcwidth((wchar_t) ch);
            if (ch < 32)
                ++result;
            break;
@@ -189,7 +230,7 @@ ConvertCh(chtype source, cchar_t *target)
 {
     wchar_t tmp_wchar[2];
 
-    tmp_wchar[0] = source;
+    tmp_wchar[0] = (wchar_t) source;
     tmp_wchar[1] = 0;
     if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
        beep();
@@ -256,7 +297,7 @@ AddCh(chtype ch)
 
 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
 static void
-test_add_wchstr(int level)
+recursive_test(int level)
 {
     static bool first = TRUE;
 
@@ -271,19 +312,33 @@ test_add_wchstr(int level)
     WINDOW *work = 0;
     WINDOW *show = 0;
     int margin = (2 * MY_TABSIZE) - 1;
-    Options option = ((m_opt ? oMove : oDefault)
-                     | ((w_opt || (level > 0)) ? oWindow : oDefault));
+    Options option = (Options) ((unsigned) (m_opt
+                                           ? oMove
+                                           : oDefault)
+                               | (unsigned) ((w_opt || (level > 0))
+                                             ? oWindow
+                                             : oDefault));
 
     if (first) {
        static char cmd[80];
        setlocale(LC_ALL, "");
 
-       putenv(strcpy(cmd, "TABSIZE=8"));
+       _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
+       putenv(cmd);
 
        initscr();
        (void) cbreak();        /* take input chars one at a time, no wait for \n */
        (void) noecho();        /* don't echo input */
        keypad(stdscr, TRUE);
+
+       /*
+        * Show the characters added in color, to distinguish from those that
+        * are shifted.
+        */
+       if (has_colors()) {
+           start_color();
+           init_pair(1, COLOR_WHITE, COLOR_BLUE);
+       }
     }
 
     limit = LINES - 5;
@@ -307,8 +362,8 @@ test_add_wchstr(int level)
     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
     limit /= 2;
 
-    (void) mvwadd_wchstr(work, 1, 2, ChStr("String"));
-    (void) mvwadd_wchstr(work, limit + 1, 2, ChStr("Chars"));
+    MvWAddChStr(work, 1, 2, ChStr("String"));
+    MvWAddChStr(work, limit + 1, 2, ChStr("Chars"));
     wnoutrefresh(work);
 
     buffer[length = 0] = '\0';
@@ -317,27 +372,23 @@ test_add_wchstr(int level)
 
     doupdate();
 
-    /*
-     * Show the characters added in color, to distinguish from those that
-     * are shifted.
-     */
     if (has_colors()) {
-       start_color();
-       init_pair(1, COLOR_WHITE, COLOR_BLUE);
-       wbkgdset(work, COLOR_PAIR(1) | ' ');
+       wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
     }
 
     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
        wmove(work, row, margin + 1);
        switch (ch) {
        case key_RECUR:
-           test_add_wchstr(level + 1);
+           recursive_test(level + 1);
 
-           touchwin(look);
+           if (look)
+               touchwin(look);
            touchwin(work);
            touchwin(show);
 
-           wnoutrefresh(look);
+           if (look)
+               wnoutrefresh(look);
            wnoutrefresh(work);
            wnoutrefresh(show);
 
@@ -428,7 +479,7 @@ test_add_wchstr(int level)
            }
            break;
        default:
-           buffer[length++] = ch;
+           buffer[length++] = (wchar_t) ch;
            buffer[length] = '\0';
 
            /* put the string in, one character at a time */
@@ -456,19 +507,19 @@ test_add_wchstr(int level)
            switch (option) {
            case oDefault:
                if (move(limit + row, col) != ERR) {
-                   AddCh(ch);
+                   AddCh((chtype) ch);
                }
                break;
            case oMove:
-               MvAddCh(limit + row, col, ch);
+               MvAddCh(limit + row, col, (chtype) ch);
                break;
            case oWindow:
                if (wmove(work, limit + row, col) != ERR) {
-                   WAddCh(work, ch);
+                   WAddCh(work, (chtype) ch);
                }
                break;
            case oMoveWindow:
-               MvWAddCh(work, limit + row, col, ch);
+               MvWAddCh(work, limit + row, col, (chtype) ch);
                break;
            }
 
@@ -481,8 +532,8 @@ test_add_wchstr(int level)
            break;
        }
     }
+    delwin(show);
     if (level > 0) {
-       delwin(show);
        delwin(work);
        delwin(look);
     }
@@ -499,6 +550,7 @@ usage(void)
        ,"  -f FILE read data from given file"
        ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
        ,"  -m      perform wmove/move separately from add-functions"
+       ,"  -p      pass-thru control characters without using unctrl()"
        ,"  -w      use window-parameter even when stdscr would be implied"
     };
     unsigned n;
@@ -514,7 +566,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 
     setlocale(LC_ALL, "");
 
-    while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
+    while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
        switch (ch) {
        case 'f':
            init_linedata(optarg);
@@ -527,6 +579,9 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
            if (n_opt == 0)
                n_opt = -1;
            break;
+       case 'p':
+           pass_ctls = TRUE;
+           break;
        case 'w':
            w_opt = TRUE;
            break;
@@ -538,8 +593,11 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
     if (optind < argc)
        usage();
 
-    test_add_wchstr(0);
+    recursive_test(0);
     endwin();
+#if NO_LEAKS
+    free(temp_buffer);
+#endif
     ExitProgram(EXIT_SUCCESS);
 }
 #else