]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_vid_puts.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / test_vid_puts.c
1 /****************************************************************************
2  * Copyright 2020,2021 Thomas E. Dickey                                     *
3  * Copyright 2013-2014,2017 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  * $Id: test_vid_puts.c,v 1.12 2021/04/25 00:10:43 tom Exp $
31  *
32  * Demonstrate the vid_puts and vid_attr functions.
33  * Thomas Dickey - 2013/01/12
34  */
35
36 #define USE_TINFO
37 #include <test.priv.h>
38
39 #if USE_WIDEC_SUPPORT && HAVE_SETUPTERM && HAVE_VID_PUTS
40
41 static FILE *my_fp;
42 static bool p_opt = FALSE;
43
44 static
45 TPUTS_PROTO(outc, c)
46 {
47     int rc;
48
49     rc = putc(c, my_fp);
50     TPUTS_RETURN(rc);
51 }
52
53 static bool
54 outs(const char *s)
55 {
56     if (VALID_STRING(s)) {
57         tputs(s, 1, outc);
58         return TRUE;
59     }
60     return FALSE;
61 }
62
63 static void
64 cleanup(void)
65 {
66     if (cur_term != 0) {
67         outs(exit_attribute_mode);
68         if (!outs(orig_colors))
69             outs(orig_pair);
70         outs(cursor_normal);
71     }
72 }
73
74 static void
75 change_attr(chtype attr)
76 {
77     if (p_opt) {
78         vid_puts(attr, (short) 0, (void *) 0, outc);
79     } else {
80         vid_attr(attr, (short) 0, (void *) 0);
81     }
82 }
83
84 static void
85 test_vid_puts(void)
86 {
87     fprintf(my_fp, "Name: ");
88     change_attr(A_BOLD);
89     fputs("Bold", my_fp);
90     change_attr(A_REVERSE);
91     fputs(" Reverse", my_fp);
92     change_attr(A_NORMAL);
93     fputs("\n", my_fp);
94 }
95
96 static void
97 usage(void)
98 {
99     static const char *tbl[] =
100     {
101         "Usage: test_vid_puts [options]"
102         ,""
103         ,"Options:"
104         ,"  -e      use stderr (default stdout)"
105         ,"  -n      do not initialize terminal"
106         ,"  -p      use vid_puts (default vid_attr)"
107     };
108     unsigned n;
109     for (n = 0; n < SIZEOF(tbl); ++n)
110         fprintf(stderr, "%s\n", tbl[n]);
111     ExitProgram(EXIT_FAILURE);
112 }
113
114 int
115 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
116 {
117     int ch;
118     bool no_init = FALSE;
119
120     my_fp = stdout;
121
122     while ((ch = getopt(argc, argv, "enp")) != -1) {
123         switch (ch) {
124         case 'e':
125             my_fp = stderr;
126             break;
127         case 'n':
128             no_init = TRUE;
129             break;
130         case 'p':
131             p_opt = TRUE;
132             break;
133         default:
134             usage();
135             break;
136         }
137     }
138     if (optind < argc)
139         usage();
140
141     if (no_init) {
142         START_TRACE();
143     } else {
144         setupterm((char *) 0, fileno(my_fp), (int *) 0);
145     }
146     test_vid_puts();
147     cleanup();
148     ExitProgram(EXIT_SUCCESS);
149 }
150
151 #else
152 int
153 main(void)
154 {
155     printf("This program requires the wide-ncurses terminfo library\n");
156     ExitProgram(EXIT_FAILURE);
157 }
158 #endif