]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/testaddch.c
ncurses 6.3 - patch 20221210
[ncurses.git] / test / testaddch.c
1 /****************************************************************************
2  * Copyright 2020,2022 Thomas E. Dickey                                     *
3  * Copyright 1998-2013,2014 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * This is an example written by Alexander V. Lukyanov <lav@yars.free.net>,
31  * to demonstrate an inconsistency between ncurses and SVr4 curses.
32  *
33  * $Id: testaddch.c,v 1.15 2022/12/10 23:44:18 tom Exp $
34  */
35 #include <test.priv.h>
36
37 static void
38 attr_addstr(const char *s, chtype a)
39 {
40     while (*s)
41         addch(((unsigned char) (*s++)) | a);
42 }
43
44 static void
45 usage(int ok)
46 {
47     static const char *msg[] =
48     {
49         "Usage: testaddch [options]"
50         ,""
51         ,USAGE_COMMON
52     };
53     size_t n;
54
55     for (n = 0; n < SIZEOF(msg); n++)
56         fprintf(stderr, "%s\n", msg[n]);
57
58     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
59 }
60 /* *INDENT-OFF* */
61 VERSION_COMMON()
62 /* *INDENT-ON* */
63
64 int
65 main(int argc, char *argv[])
66 {
67     unsigned i;
68     chtype back, set, attr;
69     int ch;
70
71     while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
72         switch (ch) {
73         case OPTS_VERSION:
74             show_version(argv);
75             ExitProgram(EXIT_SUCCESS);
76         default:
77             usage(ch == OPTS_USAGE);
78             /* NOTREACHED */
79         }
80     }
81     if (optind < argc)
82         usage(FALSE);
83
84     setlocale(LC_ALL, "");
85
86     initscr();
87     start_color();
88     init_pair(1, COLOR_WHITE, COLOR_BLUE);
89     init_pair(2, COLOR_WHITE, COLOR_RED);
90     init_pair(3, COLOR_BLACK, COLOR_MAGENTA);
91     init_pair(4, COLOR_BLACK, COLOR_GREEN);
92     init_pair(5, COLOR_BLACK, COLOR_CYAN);
93     init_pair(6, COLOR_BLACK, COLOR_YELLOW);
94     init_pair(7, COLOR_BLACK, COLOR_WHITE);
95
96     for (i = 0; i < 8; i++) {
97         back = (i & 1) ? A_BOLD | 'B' : ' ';
98         set = (i & 2) ? A_REVERSE : 0;
99         attr = (chtype) ((i & 4) ? COLOR_PAIR(4) : 0);
100
101         bkgdset(back);
102         (void) attrset(AttrArg(set, 0));
103
104         attr_addstr("Test string with spaces ->   <-\n", attr);
105     }
106     addch('\n');
107     for (i = 0; i < 8; i++) {
108         back = (i & 1) ? (A_BOLD | 'B' | (chtype) COLOR_PAIR(1)) : ' ';
109         set = (i & 2) ? (A_REVERSE | (chtype) COLOR_PAIR(2)) : 0;
110         attr = (chtype) ((i & 4) ? (chtype) COLOR_PAIR(4) : 0);
111
112         bkgdset(back);
113         (void) attrset(AttrArg(set, 0));
114
115         attr_addstr("Test string with spaces ->   <-\n", attr);
116     }
117
118     getch();
119     endwin();
120     ExitProgram(EXIT_SUCCESS);
121 }