1 /****************************************************************************
2 * Copyright (c) 2007-2010,2011 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.15 2011/01/15 18:15:11 tom Exp $
31 * Demonstrate save/restore functions from the curses library.
32 * Thomas Dickey - 2007/7/14
35 #include <test.priv.h>
39 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
47 # include <sys/time.h>
53 static bool use_init
= FALSE
;
56 fexists(const char *name
)
59 return (stat(name
, &sb
) == 0 && (sb
.st_mode
& S_IFMT
) == S_IFREG
);
70 cleanup(char *files
[])
74 for (n
= 0; files
[n
] != 0; ++n
) {
80 load_screen(char *filename
)
85 if ((result
= scr_init(filename
)) != ERR
)
86 result
= scr_restore(filename
);
88 result
= scr_set(filename
);
94 * scr_restore() or scr_set() operates on curscr. If we read a character using
95 * getch() that will refresh stdscr, wiping out the result. To avoid that,
96 * copy the data back from curscr to stdscr.
101 overwrite(curscr
, stdscr
);
106 show_what(int which
, int last
)
109 time_t now
= time((time_t *) 0);
114 printw("Saved %d of %d - %s", which
, last
+ 1, ctime(&now
));
122 get_command(int which
, int last
)
129 show_what(which
, last
);
139 static const char *msg
[] =
141 "Usage: savescreen [-r] files",
144 " -i use scr_init/scr_restore rather than scr_set",
145 " -r replay the screen-dump files"
148 for (n
= 0; n
< SIZEOF(msg
); ++n
) {
149 fprintf(stderr
, "%s\n", msg
[n
]);
151 ExitProgram(EXIT_FAILURE
);
155 main(int argc
, char *argv
[])
160 bool replaying
= FALSE
;
164 while ((ch
= getopt(argc
, argv
, "ir")) != -1) {
178 files
= argv
+ optind
;
179 last
= argc
- optind
- 1;
182 while (last
>= 0 && !fexists(files
[last
]))
189 keypad(stdscr
, TRUE
);
193 for (ch
= 0; ch
< COLOR_PAIRS
; ++ch
) {
194 short pair
= (short) (ch
% COLOR_PAIRS
);
195 init_pair(pair
, COLOR_WHITE
, (short) (ch
% COLORS
));
202 * Use the last file as the initial/current screen.
206 printf("No screen-dumps given\n");
207 ExitProgram(EXIT_FAILURE
);
211 if (load_screen(files
[which
]) == ERR
) {
213 printf("Cannot load screen-dump %s\n", files
[which
]);
214 ExitProgram(EXIT_FAILURE
);
218 while (!done
&& (ch
= getch()) != ERR
) {
222 * If we got a "next" here, skip to the final screen before
223 * moving to the next process.
250 } else if (scr_restore(files
[which
]) == ERR
) {
252 printf("Cannot load screen-dump %s\n", files
[which
]);
254 ExitProgram(EXIT_FAILURE
);
264 printw("Use h,j,k,l or arrows to move around the screen\n");
265 printw("Press 'q' to quit, ' ' to dump a screen\n");
266 printw("When the last screen has been dumped, press 'n' to run the\n");
267 printw("screen-loader. That allows only 'q', backspace and ' ' for\n");
268 printw("stepping through the dumped/restored screens.\n");
272 switch (get_command(which
, last
)) {
283 if (files
[which
] != 0) {
284 show_what(which
+ 1, last
);
285 if (scr_dump(files
[which
]) == ERR
) {
287 printf("Cannot write screen-dump %s\n", files
[which
]);
294 short pair
= (short) (which
% COLOR_PAIRS
);
295 bkgd((chtype
) COLOR_PAIR(pair
));
323 time_t now
= time((time_t *) 0);
328 addch('#' | A_REVERSE
);
333 ExitProgram(EXIT_SUCCESS
);
337 main(int argc
, char *argv
[])
339 printf("This program requires the screen-dump functions\n");
340 ExitProgram(EXIT_FAILURE
);