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