+ int ch;
+ int which = 0;
+ int last;
+ bool replaying = FALSE;
+ bool done = FALSE;
+ char **files;
+
+ while ((ch = getopt(argc, argv, "r")) != -1) {
+ switch (ch) {
+ case 'r':
+ replaying = TRUE;
+ break;
+ default:
+ usage();
+ break;
+ }
+ }
+
+ initscr();
+ cbreak();
+ noecho();
+ keypad(stdscr, TRUE);
+ curs_set(0);
+ if (has_colors()) {
+ start_color();
+ for (ch = 0; ch < COLOR_PAIRS; ++ch) {
+ short pair = ch % COLOR_PAIRS;
+ init_pair(pair, COLOR_WHITE, ch % COLORS);
+ }
+ }
+
+ files = argv + optind;
+ last = argc - optind - 1;
+ if (replaying) {
+
+ /*
+ * Use the last file as the initial/current screen.
+ */
+ if (last < 0) {
+ endwin();
+ printf("No screen-dumps given\n");
+ ExitProgram(EXIT_FAILURE);
+ }
+
+ which = last;
+ if (scr_set(files[which]) == ERR) {
+ endwin();
+ printf("Cannot load screen-dump %s\n", files[which]);
+ ExitProgram(EXIT_FAILURE);
+ }
+ after_load();
+
+ while (!done && (ch = getch()) != ERR) {
+ switch (ch) {
+ case 'n':
+ /*
+ * If we got a "next" here, skip to the final screen before
+ * moving to the next process.
+ */
+ setup_next();
+ which = last;
+ done = TRUE;
+ break;
+ case 'q':
+ endwin();
+ cleanup(files);
+ done = TRUE;
+ break;
+ case KEY_BACKSPACE:
+ case '\b':
+ if (--which < 0)
+ which = last;
+ break;
+ case ' ':
+ if (++which > last)
+ which = 0;
+ break;
+ default:
+ beep();
+ continue;
+ }
+
+ if (ch == 'q') {
+ ;
+ } else if (scr_restore(files[which]) == ERR) {
+ endwin();
+ printf("Cannot load screen-dump %s\n", files[which]);
+ cleanup(files);
+ ExitProgram(EXIT_FAILURE);
+ } else {
+ wrefresh(curscr);
+ }
+ }
+ } else {
+ int y;
+ int x;
+
+ move(2, 0);
+ printw("Use h,j,k,l or arrows to move around the screen\n");
+ printw("Press 'q' to quit, ' ' to dump a screen\n");
+ printw("When the last screen has been dumped, press 'n' to run the\n");
+ printw("screen-loader. That allows only 'q', backspace and ' ' for\n");
+ printw("stepping through the dumped/restored screens.\n");
+ getyx(stdscr, y, x);
+
+ while (!done) {
+ switch (ch = get_command(which, last)) {
+ case 'n':
+ setup_next();
+ done = TRUE;
+ break;
+ case 'q':
+ endwin();
+ cleanup(files);
+ done = TRUE;
+ break;
+ case ' ':
+ if (files[which] != 0) {
+ show_what(which + 1, last);
+ if (scr_dump(files[which]) == ERR) {
+ endwin();
+ printf("Cannot write screen-dump %s\n", files[which]);
+ cleanup(files);
+ done = TRUE;
+ break;
+ }
+ ++which;
+ if (has_colors()) {
+ short pair = which % COLOR_PAIRS;
+ bkgd(COLOR_PAIR(pair));
+ }
+ } else {
+ beep();
+ }
+ break;
+ case KEY_LEFT:
+ case 'h':
+ if (--x < 0)
+ x = COLS - 1;
+ break;
+ case KEY_DOWN:
+ case 'j':
+ if (++y >= LINES)
+ y = 1;
+ break;
+ case KEY_UP:
+ case 'k':
+ if (--y < 1)
+ y = LINES - 1;
+ break;
+ case KEY_RIGHT:
+ case 'l':
+ if (++x >= COLS)
+ x = 0;
+ break;
+ }
+ if (!done) {
+ time_t now = time((time_t *) 0);
+
+ move(0, 0);
+ addstr(ctime(&now));
+ move(y, x);
+ addch('#' | A_REVERSE);
+ move(y, x);
+ }
+ }
+ }
+ ExitProgram(EXIT_SUCCESS);