]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_vidputs.c
ncurses 5.9 - patch 20150502
[ncurses.git] / test / test_vidputs.c
1 /****************************************************************************
2  * Copyright (c) 2013,2014 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_vidputs.c,v 1.5 2014/07/19 23:09:58 tom Exp $
30  *
31  * Demonstrate the vidputs and vidattr functions.
32  * Thomas Dickey - 2013/01/12
33  */
34
35 #define USE_TINFO
36 #include <test.priv.h>
37
38 #if HAVE_SETUPTERM && HAVE_VIDPUTS
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     outs(exit_attribute_mode);
68     if (!outs(orig_colors))
69         outs(orig_pair);
70     outs(cursor_normal);
71 }
72
73 static void
74 change_attr(chtype attr)
75 {
76     if (p_opt) {
77         vidputs(attr, outc);
78     } else {
79         vidattr(attr);
80     }
81 }
82
83 static void
84 test_vidputs(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_vidputs [options]"
101         ,""
102         ,"Options:"
103         ,"  -e      use stderr (default stdout)"
104         ,"  -p      use vidputs (default vidattr)"
105     };
106     unsigned n;
107     for (n = 0; n < SIZEOF(tbl); ++n)
108         fprintf(stderr, "%s\n", tbl[n]);
109     ExitProgram(EXIT_FAILURE);
110 }
111
112 int
113 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
114 {
115     int ch;
116
117     my_fp = stdout;
118
119     while ((ch = getopt(argc, argv, "ep")) != -1) {
120         switch (ch) {
121         case 'e':
122             my_fp = stderr;
123             break;
124         case 'p':
125             p_opt = TRUE;
126             break;
127         default:
128             usage();
129             break;
130         }
131     }
132     if (optind < argc)
133         usage();
134
135     setupterm((char *) 0, 1, (int *) 0);
136     test_vidputs();
137     cleanup();
138     ExitProgram(EXIT_SUCCESS);
139 }
140 #else
141 int
142 main(int argc GCC_UNUSED,
143      char *argv[]GCC_UNUSED)
144 {
145     fprintf(stderr, "This program requires terminfo\n");
146     exit(EXIT_FAILURE);
147 }
148 #endif