blob: 31dc9da4da0f38a52639ce593253b012b88f2082 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020-2021,2022 Thomas E. Dickey *
3 * Copyright 2013-2014,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: test_vidputs.c,v 1.15 2022/12/10 23:23:27 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
32 * Demonstrate the vidputs and vidattr functions.
33 * Thomas Dickey - 2013/01/12
34 */
35
36#define USE_TINFO
37#include <test.priv.h>
38
39#if HAVE_SETUPTERM && HAVE_VIDPUTS
40
Steve Kondikae271bc2015-11-15 02:50:53 +010041static FILE *my_fp;
42static bool p_opt = FALSE;
43
44static
45TPUTS_PROTO(outc, c)
46{
micky3879b9f5e72025-07-08 18:04:53 -040047 int rc;
Steve Kondikae271bc2015-11-15 02:50:53 +010048
49 rc = putc(c, my_fp);
50 TPUTS_RETURN(rc);
51}
52
53static bool
54outs(const char *s)
55{
micky3879b9f5e72025-07-08 18:04:53 -040056 if (VALID_STRING(s)) {
Steve Kondikae271bc2015-11-15 02:50:53 +010057 tputs(s, 1, outc);
58 return TRUE;
59 }
60 return FALSE;
61}
62
63static void
64cleanup(void)
65{
micky3879b9f5e72025-07-08 18:04:53 -040066 if (cur_term != 0) {
67 outs(exit_attribute_mode);
68 if (!outs(orig_colors))
69 outs(orig_pair);
70 outs(cursor_normal);
71 }
Steve Kondikae271bc2015-11-15 02:50:53 +010072}
73
74static void
75change_attr(chtype attr)
76{
77 if (p_opt) {
78 vidputs(attr, outc);
79 } else {
80 vidattr(attr);
81 }
82}
83
84static void
85test_vidputs(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
96static void
micky3879b9f5e72025-07-08 18:04:53 -040097usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +010098{
99 static const char *tbl[] =
100 {
101 "Usage: test_vidputs [options]"
102 ,""
micky3879b9f5e72025-07-08 18:04:53 -0400103 ,USAGE_COMMON
Steve Kondikae271bc2015-11-15 02:50:53 +0100104 ,"Options:"
micky3879b9f5e72025-07-08 18:04:53 -0400105 ," -e use stderr (default stdout)"
106 ," -n do not initialize terminal"
107 ," -p use vidputs (default vidattr)"
Steve Kondikae271bc2015-11-15 02:50:53 +0100108 };
109 unsigned n;
110 for (n = 0; n < SIZEOF(tbl); ++n)
111 fprintf(stderr, "%s\n", tbl[n]);
micky3879b9f5e72025-07-08 18:04:53 -0400112 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100113}
micky3879b9f5e72025-07-08 18:04:53 -0400114/* *INDENT-OFF* */
115VERSION_COMMON()
116/* *INDENT-ON* */
Steve Kondikae271bc2015-11-15 02:50:53 +0100117
118int
micky3879b9f5e72025-07-08 18:04:53 -0400119main(int argc, char *argv[])
Steve Kondikae271bc2015-11-15 02:50:53 +0100120{
121 int ch;
micky3879b9f5e72025-07-08 18:04:53 -0400122 bool no_init = FALSE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100123
124 my_fp = stdout;
125
micky3879b9f5e72025-07-08 18:04:53 -0400126 while ((ch = getopt(argc, argv, OPTS_COMMON "enp")) != -1) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100127 switch (ch) {
128 case 'e':
129 my_fp = stderr;
130 break;
micky3879b9f5e72025-07-08 18:04:53 -0400131 case 'n':
132 no_init = TRUE;
133 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100134 case 'p':
135 p_opt = TRUE;
136 break;
micky3879b9f5e72025-07-08 18:04:53 -0400137 case OPTS_VERSION:
138 show_version(argv);
139 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100140 default:
micky3879b9f5e72025-07-08 18:04:53 -0400141 usage(ch == OPTS_USAGE);
142 /* NOTREACHED */
Steve Kondikae271bc2015-11-15 02:50:53 +0100143 }
144 }
145 if (optind < argc)
micky3879b9f5e72025-07-08 18:04:53 -0400146 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100147
micky3879b9f5e72025-07-08 18:04:53 -0400148 if (no_init) {
149 START_TRACE();
150 } else {
151 setupterm((char *) 0, fileno(my_fp), (int *) 0);
152 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100153 test_vidputs();
154 cleanup();
155 ExitProgram(EXIT_SUCCESS);
156}
micky3879b9f5e72025-07-08 18:04:53 -0400157
Steve Kondikae271bc2015-11-15 02:50:53 +0100158#else
159int
micky3879b9f5e72025-07-08 18:04:53 -0400160main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100161{
162 fprintf(stderr, "This program requires terminfo\n");
163 exit(EXIT_FAILURE);
164}
165#endif