micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright 2020,2022 Thomas E. Dickey * |
| 3 | * Copyright 2017 Free Software Foundation, Inc. * |
| 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 | /* |
| 30 | * $Id: test_termattrs.c,v 1.8 2022/12/10 23:23:27 tom Exp $ |
| 31 | * |
| 32 | * Demonstrate the termattrs and term_attrs functions. |
| 33 | */ |
| 34 | |
| 35 | #define USE_CURSES |
| 36 | #define USE_TINFO |
| 37 | #include <test.priv.h> |
| 38 | |
| 39 | #if HAVE_SETUPTERM |
| 40 | |
| 41 | static FILE *my_fp; |
| 42 | |
| 43 | static void |
| 44 | test_termattrs(unsigned long value) |
| 45 | { |
| 46 | #define DATA(name) { name, #name } |
| 47 | static struct { |
| 48 | unsigned long code; |
| 49 | const char *name; |
| 50 | } table[] = { |
| 51 | #ifdef A_ATTRIBUTES |
| 52 | DATA(A_ATTRIBUTES), |
| 53 | #endif |
| 54 | #ifdef A_CHARTEXT |
| 55 | DATA(A_CHARTEXT), |
| 56 | #endif |
| 57 | #ifdef A_COLOR |
| 58 | DATA(A_COLOR), |
| 59 | #endif |
| 60 | #ifdef A_STANDOUT |
| 61 | DATA(A_STANDOUT), |
| 62 | #endif |
| 63 | #ifdef A_UNDERLINE |
| 64 | DATA(A_UNDERLINE), |
| 65 | #endif |
| 66 | #ifdef A_REVERSE |
| 67 | DATA(A_REVERSE), |
| 68 | #endif |
| 69 | #ifdef A_BLINK |
| 70 | DATA(A_BLINK), |
| 71 | #endif |
| 72 | #ifdef A_DIM |
| 73 | DATA(A_DIM), |
| 74 | #endif |
| 75 | #ifdef A_BOLD |
| 76 | DATA(A_BOLD), |
| 77 | #endif |
| 78 | #ifdef A_ALTCHARSET |
| 79 | DATA(A_ALTCHARSET), |
| 80 | #endif |
| 81 | #ifdef A_INVIS |
| 82 | DATA(A_INVIS), |
| 83 | #endif |
| 84 | #ifdef A_PROTECT |
| 85 | DATA(A_PROTECT), |
| 86 | #endif |
| 87 | #ifdef A_HORIZONTAL |
| 88 | DATA(A_HORIZONTAL), |
| 89 | #endif |
| 90 | #ifdef A_LEFT |
| 91 | DATA(A_LEFT), |
| 92 | #endif |
| 93 | #ifdef A_LOW |
| 94 | DATA(A_LOW), |
| 95 | #endif |
| 96 | #ifdef A_RIGHT |
| 97 | DATA(A_RIGHT), |
| 98 | #endif |
| 99 | #ifdef A_TOP |
| 100 | DATA(A_TOP), |
| 101 | #endif |
| 102 | #ifdef A_VERTICAL |
| 103 | DATA(A_VERTICAL), |
| 104 | #endif |
| 105 | #ifdef A_ITALIC |
| 106 | DATA(A_ITALIC), |
| 107 | #endif |
| 108 | }; |
| 109 | size_t n; |
| 110 | fprintf(my_fp, "Result: %08lX\r\n", value); |
| 111 | for (n = 0; n < SIZEOF(table); ++n) { |
| 112 | if ((value & table[n].code) != 0) { |
| 113 | fprintf(my_fp, "%08lX %08lX %s\r\n", |
| 114 | table[n].code, value & table[n].code, table[n].name); |
| 115 | } |
| 116 | }; |
| 117 | fputs("\r\n", my_fp); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | usage(int ok) |
| 122 | { |
| 123 | static const char *tbl[] = |
| 124 | { |
| 125 | "Usage: test_termattrs [options]" |
| 126 | ,"" |
| 127 | ,USAGE_COMMON |
| 128 | ,"Options:" |
| 129 | ," -e use stderr (default stdout)" |
| 130 | ," -n do not initialize terminal" |
| 131 | ," -s use setupterm rather than newterm" |
| 132 | #if USE_WIDEC_SUPPORT |
| 133 | ," -w use term_attrs rather than termattrs" |
| 134 | #endif |
| 135 | }; |
| 136 | unsigned n; |
| 137 | for (n = 0; n < SIZEOF(tbl); ++n) |
| 138 | fprintf(stderr, "%s\n", tbl[n]); |
| 139 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 140 | } |
| 141 | /* *INDENT-OFF* */ |
| 142 | VERSION_COMMON() |
| 143 | /* *INDENT-ON* */ |
| 144 | |
| 145 | int |
| 146 | main(int argc, char *argv[]) |
| 147 | { |
| 148 | int ch; |
| 149 | bool no_init = FALSE; |
| 150 | bool s_opt = FALSE; |
| 151 | #if USE_WIDEC_SUPPORT |
| 152 | bool w_opt = FALSE; |
| 153 | #endif |
| 154 | |
| 155 | my_fp = stdout; |
| 156 | |
| 157 | while ((ch = getopt(argc, argv, OPTS_COMMON "ensw")) != -1) { |
| 158 | switch (ch) { |
| 159 | case 'e': |
| 160 | my_fp = stderr; |
| 161 | break; |
| 162 | case 'n': |
| 163 | no_init = TRUE; |
| 164 | break; |
| 165 | case 's': |
| 166 | s_opt = TRUE; |
| 167 | break; |
| 168 | #if USE_WIDEC_SUPPORT |
| 169 | case 'w': |
| 170 | w_opt = TRUE; |
| 171 | break; |
| 172 | #endif |
| 173 | case OPTS_VERSION: |
| 174 | show_version(argv); |
| 175 | ExitProgram(EXIT_SUCCESS); |
| 176 | default: |
| 177 | usage(ch == OPTS_USAGE); |
| 178 | /* NOTREACHED */ |
| 179 | } |
| 180 | } |
| 181 | if (optind < argc) |
| 182 | usage(FALSE); |
| 183 | |
| 184 | if (no_init) { |
| 185 | START_TRACE(); |
| 186 | } else if (s_opt) { |
| 187 | setupterm((char *) 0, fileno(my_fp), (int *) 0); |
| 188 | } else { |
| 189 | newterm((char *) 0, my_fp, stdin); |
| 190 | } |
| 191 | #if USE_WIDEC_SUPPORT |
| 192 | if (w_opt) |
| 193 | test_termattrs((unsigned long) term_attrs()); |
| 194 | else |
| 195 | #endif |
| 196 | test_termattrs((unsigned long) termattrs()); |
| 197 | ExitProgram(EXIT_SUCCESS); |
| 198 | } |
| 199 | |
| 200 | #else |
| 201 | int |
| 202 | main(void) |
| 203 | { |
| 204 | fprintf(stderr, "This program requires terminfo\n"); |
| 205 | exit(EXIT_FAILURE); |
| 206 | } |
| 207 | #endif |