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 2018-2022,2023 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 | |
| 30 | /* |
| 31 | * Author: Thomas E. Dickey |
| 32 | * |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 33 | * $Id: dots_termcap.c,v 1.32 2023/02/25 18:11:21 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 34 | * |
| 35 | * A simple demo of the termcap interface. |
| 36 | */ |
| 37 | #define USE_TINFO |
| 38 | #include <test.priv.h> |
| 39 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 40 | #if !defined(_NC_WINDOWS) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 41 | #include <sys/time.h> |
| 42 | #endif |
| 43 | |
| 44 | #if HAVE_TGETENT |
| 45 | |
| 46 | #include <time.h> |
| 47 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 48 | static bool interrupted = FALSE; |
| 49 | static long total_chars = 0; |
| 50 | static time_t started; |
| 51 | |
| 52 | static char *t_AB; |
| 53 | static char *t_AF; |
| 54 | static char *t_cl; |
| 55 | static char *t_cm; |
| 56 | static char *t_me; |
| 57 | static char *t_mr; |
| 58 | static char *t_oc; |
| 59 | static char *t_op; |
| 60 | static char *t_ve; |
| 61 | static char *t_vi; |
| 62 | |
| 63 | static struct { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 64 | NCURSES_CONST char *name; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 65 | char **value; |
| 66 | } my_caps[] = { |
| 67 | |
| 68 | { |
| 69 | "AB", &t_AB |
| 70 | }, |
| 71 | { |
| 72 | "AF", &t_AF |
| 73 | }, |
| 74 | { |
| 75 | "cl", &t_cl |
| 76 | }, |
| 77 | { |
| 78 | "cm", &t_cm |
| 79 | }, |
| 80 | { |
| 81 | "me", &t_me |
| 82 | }, |
| 83 | { |
| 84 | "mr", &t_mr |
| 85 | }, |
| 86 | { |
| 87 | "oc", &t_oc |
| 88 | }, |
| 89 | { |
| 90 | "op", &t_op |
| 91 | }, |
| 92 | { |
| 93 | "ve", &t_ve |
| 94 | }, |
| 95 | { |
| 96 | "vi", &t_vi |
| 97 | }, |
| 98 | }; |
| 99 | |
| 100 | static |
| 101 | TPUTS_PROTO(outc, c) |
| 102 | { |
| 103 | int rc = c; |
| 104 | |
| 105 | if (interrupted) { |
| 106 | char tmp = (char) c; |
| 107 | if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1) |
| 108 | rc = EOF; |
| 109 | } else { |
| 110 | rc = putc(c, stdout); |
| 111 | } |
| 112 | TPUTS_RETURN(rc); |
| 113 | } |
| 114 | |
| 115 | static bool |
| 116 | outs(char *s) |
| 117 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 118 | if (VALID_STRING(s)) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 119 | tputs(s, 1, outc); |
| 120 | return TRUE; |
| 121 | } |
| 122 | return FALSE; |
| 123 | } |
| 124 | |
| 125 | static void |
| 126 | cleanup(void) |
| 127 | { |
| 128 | outs(t_me); |
| 129 | if (!outs(t_oc)) |
| 130 | outs(t_op); |
| 131 | outs(t_cl); |
| 132 | outs(t_ve); |
| 133 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 134 | fflush(stdout); |
| 135 | fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n", |
| 136 | total_chars, |
| 137 | ((double) (total_chars) / (double) (time((time_t *) 0) - started))); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void |
| 141 | onsig(int n GCC_UNUSED) |
| 142 | { |
| 143 | interrupted = TRUE; |
| 144 | } |
| 145 | |
| 146 | static double |
| 147 | ranf(void) |
| 148 | { |
| 149 | long r = (rand() & 077777); |
| 150 | return ((double) r / 32768.); |
| 151 | } |
| 152 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 153 | /* |
| 154 | * napms is a curses function which happens to be usable without initializing |
| 155 | * the screen, but if this program happened to be build with a "real" termcap |
| 156 | * library, there is nothing like napms. |
| 157 | */ |
| 158 | #if HAVE_NAPMS |
| 159 | #define my_napms(ms) napms(ms) |
| 160 | #else |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 161 | static void |
| 162 | my_napms(int ms) |
| 163 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 164 | if (ms > 0) { |
| 165 | #if defined(_NC_WINDOWS) |
| 166 | Sleep((unsigned int) ms); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 167 | #else |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 168 | struct timeval data; |
| 169 | data.tv_sec = 0; |
| 170 | data.tv_usec = ms * 1000; |
| 171 | select(0, NULL, NULL, NULL, &data); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 172 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | static int |
| 178 | get_number(NCURSES_CONST char *cap, const char *env) |
| 179 | { |
| 180 | int result = tgetnum(cap); |
| 181 | char *value = env ? getenv(env) : 0; |
| 182 | if (value != 0 && *value != 0) { |
| 183 | char *next = 0; |
| 184 | long check = strtol(value, &next, 10); |
| 185 | if (check > 0 && *next == '\0') |
| 186 | result = (int) check; |
| 187 | } |
| 188 | return result; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 189 | } |
| 190 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 191 | static void |
| 192 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 193 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 194 | static const char *msg[] = |
| 195 | { |
| 196 | "Usage: dots_termcap [options]" |
| 197 | ,"" |
| 198 | ,USAGE_COMMON |
| 199 | ,"Options:" |
| 200 | ," -T TERM override $TERM" |
| 201 | ," -e allow environment $LINES / $COLUMNS" |
| 202 | ," -m SIZE set margin (default: 2)" |
| 203 | ," -r SECS self-interrupt/exit after specified number of seconds" |
| 204 | ," -s MSECS delay 1% of the time (default: 1 msecs)" |
| 205 | }; |
| 206 | size_t n; |
| 207 | |
| 208 | for (n = 0; n < SIZEOF(msg); n++) |
| 209 | fprintf(stderr, "%s\n", msg[n]); |
| 210 | |
| 211 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 212 | } |
| 213 | /* *INDENT-OFF* */ |
| 214 | VERSION_COMMON() |
| 215 | /* *INDENT-ON* */ |
| 216 | |
| 217 | int |
| 218 | main(int argc, char *argv[]) |
| 219 | { |
| 220 | int ch; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 221 | int num_colors; |
| 222 | int num_lines; |
| 223 | int num_columns; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 224 | int e_option = 0; |
| 225 | int m_option = 2; |
| 226 | int r_option = 0; |
| 227 | int s_option = 1; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 228 | double r; |
| 229 | double c; |
| 230 | char buffer[1024]; |
| 231 | char area[1024]; |
| 232 | char *name; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 233 | size_t need; |
| 234 | char *my_env; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 235 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 236 | while ((ch = getopt(argc, argv, OPTS_COMMON "T:em:r:s:")) != -1) { |
| 237 | switch (ch) { |
| 238 | case 'T': |
| 239 | need = 6 + strlen(optarg); |
| 240 | if ((my_env = malloc(need)) != NULL) { |
| 241 | _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg); |
| 242 | putenv(my_env); |
| 243 | } |
| 244 | break; |
| 245 | case 'e': |
| 246 | e_option = 1; |
| 247 | break; |
| 248 | case 'm': |
| 249 | m_option = atoi(optarg); |
| 250 | break; |
| 251 | case 'r': |
| 252 | r_option = atoi(optarg); |
| 253 | break; |
| 254 | case 's': |
| 255 | s_option = atoi(optarg); |
| 256 | break; |
| 257 | case OPTS_VERSION: |
| 258 | show_version(argv); |
| 259 | ExitProgram(EXIT_SUCCESS); |
| 260 | default: |
| 261 | usage(ch == OPTS_USAGE); |
| 262 | /* NOTREACHED */ |
| 263 | } |
| 264 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 265 | |
| 266 | if ((name = getenv("TERM")) == 0) { |
| 267 | fprintf(stderr, "TERM is not set\n"); |
| 268 | ExitProgram(EXIT_FAILURE); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | srand((unsigned) time(0)); |
| 272 | |
| 273 | SetupAlarm((unsigned) r_option); |
| 274 | InitAndCatch(ch = tgetent(buffer, name), onsig); |
| 275 | if (ch < 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 276 | fprintf(stderr, "terminal description not found\n"); |
| 277 | ExitProgram(EXIT_FAILURE); |
| 278 | } else { |
| 279 | size_t t; |
| 280 | char *ap = area; |
| 281 | for (t = 0; t < SIZEOF(my_caps); ++t) { |
| 282 | *(my_caps[t].value) = tgetstr((NCURSES_CONST char *) |
| 283 | my_caps[t].name, &ap); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | num_colors = tgetnum("Co"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 288 | #define GetNumber(cap,env) get_number(cap, e_option ? env : 0) |
| 289 | num_lines = GetNumber("li", "LINES"); |
| 290 | num_columns = GetNumber("co", "COLUMNS"); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 291 | |
| 292 | outs(t_cl); |
| 293 | outs(t_vi); |
| 294 | if (num_colors > 1) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 295 | if (!VALID_STRING(t_AF) |
| 296 | || !VALID_STRING(t_AB) |
| 297 | || (!VALID_STRING(t_oc) && !VALID_STRING(t_op))) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 298 | num_colors = -1; |
| 299 | } |
| 300 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 301 | r = (double) (num_lines - (2 * m_option)); |
| 302 | c = (double) (num_columns - (2 * m_option)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 303 | started = time((time_t *) 0); |
| 304 | |
| 305 | while (!interrupted) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 306 | int x = (int) (c * ranf()) + m_option; |
| 307 | int y = (int) (r * ranf()) + m_option; |
| 308 | int p = (ranf() > 0.9) ? '*' : ' '; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 309 | |
| 310 | tputs(tgoto(t_cm, x, y), 1, outc); |
| 311 | if (num_colors > 0) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 312 | int z = (int) (ranf() * num_colors); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 313 | if (ranf() > 0.01) { |
| 314 | tputs(tgoto(t_AF, 0, z), 1, outc); |
| 315 | } else { |
| 316 | tputs(tgoto(t_AB, 0, z), 1, outc); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 317 | if (s_option) |
| 318 | my_napms(s_option); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 319 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 320 | } else if (VALID_STRING(t_me) |
| 321 | && VALID_STRING(t_mr)) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 322 | if (ranf() <= 0.01) { |
| 323 | outs((ranf() > 0.6) |
| 324 | ? t_mr |
| 325 | : t_me); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 326 | if (s_option) |
| 327 | my_napms(s_option); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | outc(p); |
| 331 | fflush(stdout); |
| 332 | ++total_chars; |
| 333 | } |
| 334 | cleanup(); |
| 335 | ExitProgram(EXIT_SUCCESS); |
| 336 | } |
| 337 | #else |
| 338 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 339 | main(void) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 340 | { |
| 341 | fprintf(stderr, "This program requires termcap\n"); |
| 342 | exit(EXIT_FAILURE); |
| 343 | } |
| 344 | #endif |