Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020-2021,2022 Thomas E. Dickey * |
| 3 | * Copyright 2013-2014,2017 Free Software Foundation, Inc. * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | /* |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 30 | * $Id: test_vidputs.c,v 1.15 2022/12/10 23:23:27 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | * |
| 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 Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 41 | static FILE *my_fp; |
| 42 | static bool p_opt = FALSE; |
| 43 | |
| 44 | static |
| 45 | TPUTS_PROTO(outc, c) |
| 46 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 47 | int rc; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 48 | |
| 49 | rc = putc(c, my_fp); |
| 50 | TPUTS_RETURN(rc); |
| 51 | } |
| 52 | |
| 53 | static bool |
| 54 | outs(const char *s) |
| 55 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 56 | if (VALID_STRING(s)) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 57 | tputs(s, 1, outc); |
| 58 | return TRUE; |
| 59 | } |
| 60 | return FALSE; |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | cleanup(void) |
| 65 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 66 | if (cur_term != 0) { |
| 67 | outs(exit_attribute_mode); |
| 68 | if (!outs(orig_colors)) |
| 69 | outs(orig_pair); |
| 70 | outs(cursor_normal); |
| 71 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void |
| 75 | change_attr(chtype attr) |
| 76 | { |
| 77 | if (p_opt) { |
| 78 | vidputs(attr, outc); |
| 79 | } else { |
| 80 | vidattr(attr); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | test_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 | |
| 96 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 97 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 98 | { |
| 99 | static const char *tbl[] = |
| 100 | { |
| 101 | "Usage: test_vidputs [options]" |
| 102 | ,"" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 103 | ,USAGE_COMMON |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 104 | ,"Options:" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 105 | ," -e use stderr (default stdout)" |
| 106 | ," -n do not initialize terminal" |
| 107 | ," -p use vidputs (default vidattr)" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 108 | }; |
| 109 | unsigned n; |
| 110 | for (n = 0; n < SIZEOF(tbl); ++n) |
| 111 | fprintf(stderr, "%s\n", tbl[n]); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 112 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 113 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 114 | /* *INDENT-OFF* */ |
| 115 | VERSION_COMMON() |
| 116 | /* *INDENT-ON* */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 117 | |
| 118 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 119 | main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 120 | { |
| 121 | int ch; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 122 | bool no_init = FALSE; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 123 | |
| 124 | my_fp = stdout; |
| 125 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 126 | while ((ch = getopt(argc, argv, OPTS_COMMON "enp")) != -1) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 127 | switch (ch) { |
| 128 | case 'e': |
| 129 | my_fp = stderr; |
| 130 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 131 | case 'n': |
| 132 | no_init = TRUE; |
| 133 | break; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 134 | case 'p': |
| 135 | p_opt = TRUE; |
| 136 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 137 | case OPTS_VERSION: |
| 138 | show_version(argv); |
| 139 | ExitProgram(EXIT_SUCCESS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 140 | default: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 141 | usage(ch == OPTS_USAGE); |
| 142 | /* NOTREACHED */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | if (optind < argc) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 146 | usage(FALSE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 147 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 148 | if (no_init) { |
| 149 | START_TRACE(); |
| 150 | } else { |
| 151 | setupterm((char *) 0, fileno(my_fp), (int *) 0); |
| 152 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 153 | test_vidputs(); |
| 154 | cleanup(); |
| 155 | ExitProgram(EXIT_SUCCESS); |
| 156 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 157 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 158 | #else |
| 159 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 160 | main(void) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 161 | { |
| 162 | fprintf(stderr, "This program requires terminfo\n"); |
| 163 | exit(EXIT_FAILURE); |
| 164 | } |
| 165 | #endif |