]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/savescreen.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / savescreen.c
index 911c62fc0d1c76102ec73dfd91cd26abaa741991..3ea619306a84b9c91df254a93e644c4ee4c64a66 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2007-2017,2018 Free Software Foundation, Inc.              *
+ * Copyright 2018-2021,2022 Thomas E. Dickey                                *
+ * Copyright 2006-2017,2018 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            *
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: savescreen.c,v 1.50 2018/01/07 02:24:03 tom Exp $
+ * $Id: savescreen.c,v 1.62 2022/12/10 23:23:27 tom Exp $
  *
  * Demonstrate save/restore functions from the curses library.
  * Thomas Dickey - 2007/7/14
  */
 
+#define NEED_TIME_H
 #include <test.priv.h>
 #include <popup_msg.h>
+#include <parse_rgb.h>
 
 #if HAVE_SCR_DUMP
 
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
 #if defined(__hpux)
 #define MyMarker 'X'
 #else
@@ -68,10 +60,12 @@ static wchar_t
 BaseChar(cchar_t data)
 {
     wchar_t my_wchar[CCHARW_MAX];
+    wchar_t result = 0;
     attr_t my_attr;
     short my_pair;
-    getcchar(&data, my_wchar, &my_attr, &my_pair, NULL);
-    return my_wchar[0];
+    if (getcchar(&data, my_wchar, &my_attr, &my_pair, NULL) == OK)
+       result = my_wchar[0];
+    return result;
 }
 #endif
 
@@ -92,9 +86,9 @@ setup_next(void)
 static void
 cleanup(char *files[])
 {
-    int n;
-
     if (!keep_dumps) {
+       int n;
+
        for (n = 0; files[n] != 0; ++n) {
            unlink(files[n]);
        }
@@ -175,7 +169,6 @@ dump_screen(char **files, int color, int which, int last, bool use_colors)
 {
 #if USE_WIDEC_SUPPORT
     cchar_t mycc;
-    int myxx;
 #endif
     char *filename = files[which];
     bool dumped = FALSE;
@@ -204,7 +197,7 @@ dump_screen(char **files, int color, int which, int last, bool use_colors)
                    wmove(stdscr, cy, cx);
 #if USE_WIDEC_SUPPORT
                    if (win_wch(curscr, &mycc) != ERR) {
-                       myxx = wcwidth(BaseChar(mycc));
+                       int myxx = wcwidth(BaseChar(mycc));
                        if (myxx > 0) {
                            wadd_wchnstr(stdscr, &mycc, 1);
                            cx += (myxx - 1);
@@ -260,24 +253,28 @@ replay_help(void)
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
-       "Usage: savescreen [-r] files",
-       "",
-       "Options:",
-       " -f file  fill/initialize screen using text from this file",
-       " -i       use scr_init/scr_restore rather than scr_set",
-       " -k       keep the restored dump-files rather than removing them",
-       " -r       replay the screen-dump files"
+       "Usage: savescreen [-r] files"
+       ,""
+       ,USAGE_COMMON
+       ,"Options:"
+       ," -f file  fill/initialize screen using text from this file"
+       ," -i       use scr_init/scr_restore rather than scr_set"
+       ," -k       keep the restored dump-files rather than removing them"
+       ," -r       replay the screen-dump files"
     };
     unsigned n;
     for (n = 0; n < SIZEOF(msg); ++n) {
        fprintf(stderr, "%s\n", msg[n]);
     }
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
@@ -291,19 +288,14 @@ main(int argc, char *argv[])
     char **files;
     char *fill_by = 0;
 #if USE_WIDEC_SUPPORT
-    int r, g, b;
-    int cube = 0;
-    int cube0 = 16;
-    int cube1;
     cchar_t mycc;
     static const wchar_t mywc[2] =
     {L'#', 0};
-    bool using_rgb = FALSE;
 #endif
 
     setlocale(LC_ALL, "");
 
-    while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "f:ikr")) != -1) {
        switch (ch) {
        case 'f':
            fill_by = optarg;
@@ -317,9 +309,12 @@ main(int argc, char *argv[])
        case 'r':
            replaying = TRUE;
            break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           break;
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
 
@@ -338,6 +333,9 @@ main(int argc, char *argv[])
     curs_set(0);
 
     if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
+#if USE_WIDEC_SUPPORT
+       bool using_rgb = FALSE;
+#endif
        static const struct {
            int fg, bg;
        } table[MAX_ANSI] = {
@@ -374,42 +372,45 @@ main(int argc, char *argv[])
                    init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
            }
 #if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
-           else if (tigetflag("RGB") > 0) {
-               int rows = LINES - 1;
-               int cols = COLS - 1;
-               /* FIXME: test all 3 types of capability */
-               int b_max = 255;
-               int r_max = 255;
-               int g_max = 255;
-               int b_delta = (b_max / rows);
-               int r_delta = (r_max / cols);
-               int g_delta = (g_max / cols);
-               int row = 0;
-
-               b = 0;
-               using_rgb = TRUE;
-               while (row++ < rows) {
-                   int col = 0;
-                   r = 0;
-                   g = g_max;
-                   while (col++ < cols) {
-                       int color = (((r * (g_max + 1)) + g) * (b_max + 1)
-                                    + b + MAX_ANSI);
-#if HAVE_INIT_EXTENDED_COLOR
-                       init_extended_pair(pair, color, color);
+           else {
+               int r_max, g_max, b_max;
+
+               if (parse_rgb(&r_max, &g_max, &b_max) > 0) {
+                   int rows = LINES - 1;
+                   int cols = COLS - 1;
+                   int b_delta = (b_max / rows);
+                   int r_delta = (r_max / cols);
+                   int g_delta = (g_max / cols);
+                   int row = 0;
+                   int b = 0;
+
+                   using_rgb = TRUE;
+                   while (row++ < rows) {
+                       int col = 0;
+                       int r = 0;
+                       int g = g_max;
+                       while (col++ < cols) {
+                           int color = (((r * (g_max + 1)) + g) * (b_max + 1)
+                                        + b + MAX_ANSI);
+#if USE_EXTENDED_COLOR
+                           init_extended_pair(pair, color, color);
 #else
-                       init_pair(pair, color, color);
+                           init_pair(pair, color, color);
 #endif
-                       pair++;
-                       r += r_delta;
-                       g -= g_delta;
+                           pair++;
+                           r += r_delta;
+                           g -= g_delta;
+                       }
+                       b += b_delta;
                    }
-                   b += b_delta;
                }
            }
 #endif
        }
        if ((fill_by == 0) && !replaying) {
+#if USE_WIDEC_SUPPORT
+           int cube = 0;
+#endif
            /*
             * Originally (before wide-characters) ncurses supported 16 colors.
             */
@@ -417,8 +418,8 @@ main(int argc, char *argv[])
                mvprintw(2, 0, "System colors:\n");
                for (n = 0; n < 16; ++n) {
                    pair = n + MAX_ANSI;
-                   addch(' ' | COLOR_PAIR(pair));
-                   addch(' ' | COLOR_PAIR(pair));
+                   addch((chtype) (' ' | COLOR_PAIR(pair)));
+                   addch((chtype) (' ' | COLOR_PAIR(pair)));
                    if (((n + 1) % 8) == 0)
                        addch('\n');
                }
@@ -434,8 +435,9 @@ main(int argc, char *argv[])
                cube = 6;
            }
            if (cube != 0) {
-               cube0 = 16;
-               cube1 = cube0 + (cube * cube * cube);
+               int r, g, b;
+               int cube0 = 16;
+               int cube1 = cube0 + (cube * cube * cube);
 
                addch('\n');
                printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
@@ -466,7 +468,6 @@ main(int argc, char *argv[])
                int cols = COLS - 1;
                int row = 0;
 
-               b = 0;
                pair = MAX_ANSI;
                while (row++ < rows) {
                    int col = 0;
@@ -502,7 +503,7 @@ main(int argc, char *argv[])
            }
            move(0, 0);
        } else {
-           exit_curses();
+           stop_curses();
            fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
            ExitProgram(EXIT_FAILURE);
        }
@@ -514,14 +515,14 @@ main(int argc, char *argv[])
         * Use the last file as the initial/current screen.
         */
        if (last < 0) {
-           exit_curses();
+           stop_curses();
            printf("No screen-dumps given\n");
            ExitProgram(EXIT_FAILURE);
        }
 
        which = last;
        if (load_screen(files[which]) == ERR) {
-           exit_curses();
+           stop_curses();
            printf("Cannot load screen-dump %s\n", files[which]);
            ExitProgram(EXIT_FAILURE);
        }
@@ -639,7 +640,7 @@ main(int argc, char *argv[])
                chtype attr = A_REVERSE;
                chtype ch2 = (altchars ? MyMarker : '#');
                if (use_colors) {
-                   attr |= COLOR_PAIR(color);
+                   attr |= (chtype) COLOR_PAIR(color);
                }
                move(y, x);
                AddCh(ch2 | attr);
@@ -654,7 +655,7 @@ main(int argc, char *argv[])
 
 #else
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(void)
 {
     printf("This program requires the screen-dump functions\n");
     ExitProgram(EXIT_FAILURE);