1 /****************************************************************************
2 * Copyright (c) 2007,2009 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: savescreen.c,v 1.11 2009/10/10 19:38:21 tom Exp $
31 * Demonstrate save/restore functions from the curses library.
32 * Thomas Dickey - 2007/7/14
35 #include <test.priv.h>
37 #include <sys/types.h>
40 #if TIME_WITH_SYS_TIME
41 # include <sys/time.h>
45 # include <sys/time.h>
51 static bool use_init = FALSE;
54 fexists(const char *name)
57 return (stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG);
68 cleanup(char *files[])
72 for (n = 0; files[n] != 0; ++n) {
78 load_screen(char *filename)
83 if ((result = scr_init(filename)) != ERR)
84 result = scr_restore(filename);
86 result = scr_set(filename);
92 * scr_restore() or scr_set() operates on curscr. If we read a character using
93 * getch() that will refresh stdscr, wiping out the result. To avoid that,
94 * copy the data back from curscr to stdscr.
99 overwrite(curscr, stdscr);
104 show_what(int which, int last)
107 time_t now = time((time_t *) 0);
112 printw("Saved %d of %d - %s", which, last + 1, ctime(&now));
120 get_command(int which, int last)
127 show_what(which, last);
137 static const char *msg[] =
139 "Usage: savescreen [-r] files",
142 " -i use scr_init/scr_restore rather than scr_set",
143 " -r replay the screen-dump files"
146 for (n = 0; n < SIZEOF(msg); ++n) {
147 fprintf(stderr, "%s\n", msg[n]);
149 ExitProgram(EXIT_FAILURE);
153 main(int argc, char *argv[])
158 bool replaying = FALSE;
162 while ((ch = getopt(argc, argv, "ir")) != -1) {
176 files = argv + optind;
177 last = argc - optind - 1;
180 while (last >= 0 && !fexists(files[last]))
187 keypad(stdscr, TRUE);
191 for (ch = 0; ch < COLOR_PAIRS; ++ch) {
192 short pair = ch % COLOR_PAIRS;
193 init_pair(pair, COLOR_WHITE, ch % COLORS);
200 * Use the last file as the initial/current screen.
204 printf("No screen-dumps given\n");
205 ExitProgram(EXIT_FAILURE);
209 if (load_screen(files[which]) == ERR) {
211 printf("Cannot load screen-dump %s\n", files[which]);
212 ExitProgram(EXIT_FAILURE);
216 while (!done && (ch = getch()) != ERR) {
220 * If we got a "next" here, skip to the final screen before
221 * moving to the next process.
248 } else if (scr_restore(files[which]) == ERR) {
250 printf("Cannot load screen-dump %s\n", files[which]);
252 ExitProgram(EXIT_FAILURE);
262 printw("Use h,j,k,l or arrows to move around the screen\n");
263 printw("Press 'q' to quit, ' ' to dump a screen\n");
264 printw("When the last screen has been dumped, press 'n' to run the\n");
265 printw("screen-loader. That allows only 'q', backspace and ' ' for\n");
266 printw("stepping through the dumped/restored screens.\n");
270 switch (ch = get_command(which, last)) {
281 if (files[which] != 0) {
282 show_what(which + 1, last);
283 if (scr_dump(files[which]) == ERR) {
285 printf("Cannot write screen-dump %s\n", files[which]);
292 short pair = which % COLOR_PAIRS;
293 bkgd(COLOR_PAIR(pair));
321 time_t now = time((time_t *) 0);
326 addch('#' | A_REVERSE);
331 ExitProgram(EXIT_SUCCESS);