]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_vid_puts.c
ncurses 6.1 - patch 20180317
[ncurses.git] / test / test_vid_puts.c
1 /****************************************************************************
2  * Copyright (c) 2013-2014,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: test_vid_puts.c,v 1.10 2017/10/11 08:16:24 tom Exp $
30  *
31  * Demonstrate the vid_puts and vid_attr functions.
32  * Thomas Dickey - 2013/01/12
33  */
34
35 #define USE_TINFO
36 #include <test.priv.h>
37
38 #if USE_WIDEC_SUPPORT && HAVE_SETUPTERM && HAVE_VID_PUTS
39
40 static FILE *my_fp;
41 static bool p_opt = FALSE;
42
43 static
44 TPUTS_PROTO(outc, c)
45 {
46     int rc = c;
47
48     rc = putc(c, my_fp);
49     TPUTS_RETURN(rc);
50 }
51
52 static bool
53 outs(const char *s)
54 {
55     if (VALID_STRING(s)) {
56         tputs(s, 1, outc);
57         return TRUE;
58     }
59     return FALSE;
60 }
61
62 static void
63 cleanup(void)
64 {
65     if (cur_term != 0) {
66         outs(exit_attribute_mode);
67         if (!outs(orig_colors))
68             outs(orig_pair);
69         outs(cursor_normal);
70     }
71 }
72
73 static void
74 change_attr(chtype attr)
75 {
76     if (p_opt) {
77         vid_puts(attr, (short) 0, (void *) 0, outc);
78     } else {
79         vid_attr(attr, (short) 0, (void *) 0);
80     }
81 }
82
83 static void
84 test_vid_puts(void)
85 {
86     fprintf(my_fp, "Name: ");
87     change_attr(A_BOLD);
88     fputs("Bold", my_fp);
89     change_attr(A_REVERSE);
90     fputs(" Reverse", my_fp);
91     change_attr(A_NORMAL);
92     fputs("\n", my_fp);
93 }
94
95 static void
96 usage(void)
97 {
98     static const char *tbl[] =
99     {
100         "Usage: test_vid_puts [options]"
101         ,""
102         ,"Options:"
103         ,"  -e      use stderr (default stdout)"
104         ,"  -n      do not initialize terminal"
105         ,"  -p      use vid_puts (default vid_attr)"
106     };
107     unsigned n;
108     for (n = 0; n < SIZEOF(tbl); ++n)
109         fprintf(stderr, "%s\n", tbl[n]);
110     ExitProgram(EXIT_FAILURE);
111 }
112
113 int
114 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
115 {
116     int ch;
117     bool no_init = FALSE;
118
119     my_fp = stdout;
120
121     while ((ch = getopt(argc, argv, "enp")) != -1) {
122         switch (ch) {
123         case 'e':
124             my_fp = stderr;
125             break;
126         case 'n':
127             no_init = TRUE;
128             break;
129         case 'p':
130             p_opt = TRUE;
131             break;
132         default:
133             usage();
134             break;
135         }
136     }
137     if (optind < argc)
138         usage();
139
140     if (no_init) {
141         START_TRACE();
142     } else {
143         setupterm((char *) 0, fileno(my_fp), (int *) 0);
144     }
145     test_vid_puts();
146     cleanup();
147     ExitProgram(EXIT_SUCCESS);
148 }
149
150 #else
151 int
152 main(void)
153 {
154     printf("This program requires the wide-ncurses terminfo library\n");
155     ExitProgram(EXIT_FAILURE);
156 }
157 #endif