]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/echochar.c
ncurses 6.2 - patch 20200212
[ncurses.git] / test / echochar.c
index 9f2d11c5bb9476796ac87a67c2f43ba0e0cbd535..ce7f03d385fe83d7253cf2fe31b791e1d144765c 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2006 Free Software Foundation, Inc.                        *
+ * Copyright 2019,2020 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.3 2006/12/03 00:14:29 tom Exp $
+ * $Id: echochar.c,v 1.21 2020/02/02 23:34:34 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,11 +44,11 @@ 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) / (time((time_t *) 0) - started)));
+          ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
 }
 
 static void
@@ -58,56 +57,58 @@ onsig(int n GCC_UNUSED)
     interrupted = TRUE;
 }
 
-static float
+static double
 ranf(void)
 {
     long r = (rand() & 077777);
-    return ((float) r / 32768.);
+    return ((double) r / 32768.);
 }
 
 static void
 set_color(char *my_pairs, int fg, int bg)
 {
     int pair = (fg * COLORS) + bg;
-    if (!my_pairs[pair]) {
-       init_pair(pair, fg, bg);
+    if (pair < COLOR_PAIRS) {
+       if (!my_pairs[pair]) {
+           init_pair((short) pair,
+                     (short) fg,
+                     (short) bg);
+       }
+       attron(COLOR_PAIR(pair));
     }
-    attron(COLOR_PAIR(pair));
 }
 
 int
-main(
-       int argc GCC_UNUSED,
-       char *argv[]GCC_UNUSED)
+main(int argc GCC_UNUSED,
+     char *argv[]GCC_UNUSED)
 {
-    int ch, x, y, z, p;
-    float r;
-    float c;
+    int ch;
+    double r;
+    double c;
     bool use_colors;
     bool opt_r = FALSE;
     char *my_pairs = 0;
     int last_fg = 0;
     int last_bg = 0;
 
-    while ((ch = getopt(argc, argv, "r")) != EOF) {
+    while ((ch = getopt(argc, argv, "r")) != -1) {
        switch (ch) {
        case 'r':
            opt_r = TRUE;
            break;
        default:
-           fprintf(stderr, "usage: echochar [-r]\n");
+           fprintf(stderr, "Usage: echochar [-r]\n");
            ExitProgram(EXIT_FAILURE);
        }
     }
 
-    CATCHALL(onsig);
-    initscr();
+    InitAndCatch(initscr(), onsig);
 
     use_colors = has_colors();
     if (use_colors) {
        start_color();
        if (COLOR_PAIRS > 0) {
-           my_pairs = calloc(COLOR_PAIRS, sizeof(*my_pairs));
+           my_pairs = typeCalloc(char, (size_t) COLOR_PAIRS);
        }
        use_colors = (my_pairs != 0);
     }
@@ -116,18 +117,18 @@ main(
 
     curs_set(0);
 
-    r = (float) (LINES - 4);
-    c = (float) (COLS - 4);
+    r = (double) (LINES - 4);
+    c = (double) (COLS - 4);
     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;
@@ -146,13 +147,14 @@ main(
            }
        }
        if (opt_r) {
-           addch(p);
+           AddCh(UChar(p));
            refresh();
        } else {
-           echochar(p);
+           echochar(UChar(p));
        }
        ++total_chars;
     }
     cleanup();
+    free(my_pairs);
     ExitProgram(EXIT_SUCCESS);
 }