X-Git-Url: https://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=test%2Fechochar.c;h=0033f8735b4e992b6ce9a0f20c5176451176882c;hb=6641601a9dd7f34deeaa978899b3730b27635afb;hp=8cb0833aa023b1e158dfb41b62ea2d5e143489e5;hpb=b6d0d9ad9e372e856f01a4c283cf784a15993903;p=ncurses.git diff --git a/test/echochar.c b/test/echochar.c index 8cb0833a..0033f873 100644 --- a/test/echochar.c +++ b/test/echochar.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2006-2010,2012 Free Software Foundation, Inc. * + * Copyright 2019-2020,2022 Thomas E. Dickey * + * Copyright 2006-2014,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -26,7 +27,7 @@ * authorization. * ****************************************************************************/ /* - * $Id: echochar.c,v 1.9 2012/06/09 20:30:32 tom Exp $ + * $Id: echochar.c,v 1.24 2022/12/04 00:40:11 tom Exp $ * * Demonstrate the echochar function (compare to dots.c). * Thomas Dickey - 2006/11/4 @@ -36,8 +37,6 @@ #include -#define valid(s) ((s != 0) && s != (char *)-1) - static bool interrupted = FALSE; static long total_chars = 0; static time_t started; @@ -45,9 +44,9 @@ static time_t started; static void cleanup(void) { - endwin(); + stop_curses(); - printf("\n\n%ld total chars, rate %.2f/sec\n", + printf("\n\n%ld total cells, rate %.2f/sec\n", total_chars, ((double) (total_chars) / (double) (time((time_t *) 0) - started))); } @@ -69,19 +68,43 @@ static void set_color(char *my_pairs, int fg, int bg) { int pair = (fg * COLORS) + bg; - if (!my_pairs[pair]) { - init_pair((short) pair, - (short) fg, - (short) bg); + if (pair < COLOR_PAIRS) { + if (!my_pairs[pair]) { + init_pair((short) pair, + (short) fg, + (short) bg); + } + attron(COLOR_PAIR(pair)); } - attron((attr_t) COLOR_PAIR(pair)); } +static void +usage(int ok) +{ + static const char *msg[] = + { + "Usage: echochar" + ,"" + ,USAGE_COMMON + ,"Options:" + ," -r use addch/refresh rather than echochar()" + }; + size_t n; + + for (n = 0; n < SIZEOF(msg); n++) + fprintf(stderr, "%s\n", msg[n]); + + ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); +} +/* *INDENT-OFF* */ +VERSION_COMMON() +/* *INDENT-ON* */ + int main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) { - int ch, x, y, z, p; + int ch; double r; double c; bool use_colors; @@ -90,19 +113,21 @@ main(int argc GCC_UNUSED, int last_fg = 0; int last_bg = 0; - while ((ch = getopt(argc, argv, "r")) != -1) { + while ((ch = getopt(argc, argv, OPTS_COMMON "r")) != -1) { switch (ch) { case 'r': opt_r = TRUE; break; + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - fprintf(stderr, "usage: echochar [-r]\n"); - ExitProgram(EXIT_FAILURE); + usage(ch == OPTS_USAGE); + /* NOTREACHED */ } } - CATCHALL(onsig); - initscr(); + InitAndCatch(initscr(), onsig); use_colors = has_colors(); if (use_colors) { @@ -122,13 +147,13 @@ main(int argc GCC_UNUSED, started = time((time_t *) 0); while (!interrupted) { - x = (int) (c * ranf()) + 2; - y = (int) (r * ranf()) + 2; - p = (ranf() > 0.9) ? '*' : ' '; + int x = (int) (c * ranf()) + 2; + int y = (int) (r * ranf()) + 2; + int p = (ranf() > 0.9) ? '*' : ' '; move(y, x); if (use_colors > 0) { - z = (int) (ranf() * COLORS); + int z = (int) (ranf() * COLORS); if (ranf() > 0.01) { set_color(my_pairs, z, last_bg); last_fg = z; @@ -147,7 +172,7 @@ main(int argc GCC_UNUSED, } } if (opt_r) { - addch(UChar(p)); + AddCh(UChar(p)); refresh(); } else { echochar(UChar(p)); @@ -155,5 +180,6 @@ main(int argc GCC_UNUSED, ++total_chars; } cleanup(); + free(my_pairs); ExitProgram(EXIT_SUCCESS); }