]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_add_wchstr.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / test_add_wchstr.c
index c59a3395628b536babb0953f5045af6186dff2dd..f69e1ddc271a43044994f38f5994aaec64a50ea1 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2010-2012,2014 Free Software Foundation, Inc.                   *
+ * Copyright 2020-2021,2022 Thomas E. Dickey                                *
+ * Copyright 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 +27,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: test_add_wchstr.c,v 1.21 2014/08/02 17:24:55 tom Exp $
+ * $Id: test_add_wchstr.c,v 1.34 2022/12/10 22:28:50 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
 
@@ -125,10 +132,11 @@ ChWLen(const wchar_t *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) {
+       for (n = 0; source[n] != 0; ++n) {
+           const char *s;
+
+           if ((source[n] < 256) && (s = unctrl((chtype) source[n])) != 0) {
                adjust += (strlen(s) - 1);
            }
        }
@@ -183,7 +191,7 @@ legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
 }
 
 static int
-ColOf(wchar_t *buffer, int length, int margin)
+ColOf(const wchar_t *buffer, int length, int margin)
 {
     int n;
     int result;
@@ -291,7 +299,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;
 
@@ -306,14 +314,19 @@ 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 */
@@ -351,8 +364,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';
@@ -369,7 +382,7 @@ test_add_wchstr(int level)
        wmove(work, row, margin + 1);
        switch (ch) {
        case key_RECUR:
-           test_add_wchstr(level + 1);
+           recursive_test(level + 1);
 
            if (look)
                touchwin(look);
@@ -529,33 +542,37 @@ test_add_wchstr(int level)
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *tbl[] =
     {
        "Usage: test_add_wchstr [options]"
        ,""
+       ,USAGE_COMMON
        ,"Options:"
-       ,"  -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"
+       ," -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;
     for (n = 0; n < SIZEOF(tbl); ++n)
        fprintf(stderr, "%s\n", tbl[n]);
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(int argc, char *argv[])
 {
     int ch;
 
     setlocale(LC_ALL, "");
 
-    while ((ch = getopt(argc, argv, "f:mn:pw")) != -1) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "f:mn:pw")) != -1) {
        switch (ch) {
        case 'f':
            init_linedata(optarg);
@@ -574,16 +591,22 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        case 'w':
            w_opt = TRUE;
            break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           break;
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
     if (optind < argc)
-       usage();
+       usage(FALSE);
 
-    test_add_wchstr(0);
+    recursive_test(0);
     endwin();
+#if NO_LEAKS
+    free(temp_buffer);
+#endif
     ExitProgram(EXIT_SUCCESS);
 }
 #else