]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/echochar.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / echochar.c
index 1a3fd7d3a7833d68ef0b8ed9452f71aa7e96d75a..08497069419a50c0b5923f0a87079a8c45794c4a 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2006-2014,2017 Free Software Foundation, Inc.              *
+ * Copyright 2019-2022,2023 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.15 2017/09/30 15:42:10 tom Exp $
+ * $Id: echochar.c,v 1.26 2023/05/27 20:13:10 tom Exp $
  *
  * Demonstrate the echochar function (compare to dots.c).
  * Thomas Dickey - 2006/11/4
@@ -36,8 +37,6 @@
 
 #include <time.h>
 
-#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)
 {
-    exit_curses();
+    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)));
 }
@@ -66,7 +65,7 @@ ranf(void)
 }
 
 static void
-set_color(char *my_pairs, int fg, int bg)
+set_color(const char *const my_pairs, int fg, int bg)
 {
     int pair = (fg * COLORS) + bg;
     if (pair < COLOR_PAIRS) {
@@ -79,11 +78,32 @@ set_color(char *my_pairs, int fg, int bg)
     }
 }
 
+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)
+main(int argc, char *argv[])
 {
-    int ch, x, y, z, p;
+    int ch;
     double r;
     double c;
     bool use_colors;
@@ -92,14 +112,17 @@ 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 */
        }
     }
 
@@ -123,13 +146,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;