]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/savescreen.c
ncurses 5.7 - patch 20101113
[ncurses.git] / test / savescreen.c
old mode 100755 (executable)
new mode 100644 (file)
index 24e8a65..3d6f248
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2007 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2007-2009,2010 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.9 2007/07/14 23:16:55 tom Exp $
+ * $Id: savescreen.c,v 1.13 2010/11/13 21:18:00 tom Exp $
  *
  * Demonstrate save/restore functions from the curses library.
  * Thomas Dickey - 2007/7/14
  */
-/*
-scr_set                        -
-scr_init               -
-*/
 
 #include <test.priv.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #if TIME_WITH_SYS_TIME
 # include <sys/time.h>
 # include <time.h>
@@ -49,6 +48,15 @@ scr_init             -
 # endif
 #endif
 
+static bool use_init = FALSE;
+
+static int
+fexists(const char *name)
+{
+    struct stat sb;
+    return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
+}
+
 static void
 setup_next(void)
 {
@@ -66,6 +74,20 @@ cleanup(char *files[])
     }
 }
 
+static int
+load_screen(char *filename)
+{
+    int result;
+
+    if (use_init) {
+       if ((result = scr_init(filename)) != ERR)
+           result = scr_restore(filename);
+    } else {
+       result = scr_set(filename);
+    }
+    return result;
+}
+
 /*
  * scr_restore() or scr_set() operates on curscr.  If we read a character using
  * getch() that will refresh stdscr, wiping out the result.  To avoid that,
@@ -117,6 +139,7 @@ usage(void)
        "Usage: savescreen [-r] files",
        "",
        "Options:",
+       " -i  use scr_init/scr_restore rather than scr_set",
        " -r  replay the screen-dump files"
     };
     unsigned n;
@@ -136,8 +159,11 @@ main(int argc, char *argv[])
     bool done = FALSE;
     char **files;
 
-    while ((ch = getopt(argc, argv, "r")) != -1) {
+    while ((ch = getopt(argc, argv, "ir")) != -1) {
        switch (ch) {
+       case 'i':
+           use_init = TRUE;
+           break;
        case 'r':
            replaying = TRUE;
            break;
@@ -147,6 +173,14 @@ main(int argc, char *argv[])
        }
     }
 
+    files = argv + optind;
+    last = argc - optind - 1;
+
+    if (replaying) {
+       while (last >= 0 && !fexists(files[last]))
+           --last;
+    }
+
     initscr();
     cbreak();
     noecho();
@@ -155,13 +189,11 @@ main(int argc, char *argv[])
     if (has_colors()) {
        start_color();
        for (ch = 0; ch < COLOR_PAIRS; ++ch) {
-           short pair = ch % COLOR_PAIRS;
-           init_pair(pair, COLOR_WHITE, ch % COLORS);
+           short pair = (short) (ch % COLOR_PAIRS);
+           init_pair(pair, COLOR_WHITE, (short) (ch % COLORS));
        }
     }
 
-    files = argv + optind;
-    last = argc - optind - 1;
     if (replaying) {
 
        /*
@@ -174,7 +206,7 @@ main(int argc, char *argv[])
        }
 
        which = last;
-       if (scr_set(files[which]) == ERR) {
+       if (load_screen(files[which]) == ERR) {
            endwin();
            printf("Cannot load screen-dump %s\n", files[which]);
            ExitProgram(EXIT_FAILURE);
@@ -235,7 +267,7 @@ main(int argc, char *argv[])
        getyx(stdscr, y, x);
 
        while (!done) {
-           switch (ch = get_command(which, last)) {
+           switch (get_command(which, last)) {
            case 'n':
                setup_next();
                done = TRUE;
@@ -257,8 +289,8 @@ main(int argc, char *argv[])
                    }
                    ++which;
                    if (has_colors()) {
-                       short pair = which % COLOR_PAIRS;
-                       bkgd(COLOR_PAIR(pair));
+                       short pair = (short) (which % COLOR_PAIRS);
+                       bkgd((chtype) COLOR_PAIR(pair));
                    }
                } else {
                    beep();