2003-12-30 22:30:32 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-08-23 16:00:29 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-10-18 13:44:47 +02:00
|
|
|
#define _GNU_SOURCE /* need sighandler_t */
|
|
|
|
|
2004-01-25 23:34:35 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
2004-08-23 16:00:29 +02:00
|
|
|
#ifdef HAVE_KQUEUE
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
2004-01-25 23:34:35 +01:00
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <string.h>
|
2004-01-19 17:56:03 +01:00
|
|
|
#include <curses.h>
|
2009-10-18 13:44:47 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
2004-01-30 03:49:29 +01:00
|
|
|
|
|
|
|
#include "dgamelaunch.h"
|
|
|
|
#include "ttyplay.h"
|
2003-12-30 22:30:32 +01:00
|
|
|
#include "ttyrec.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "stripgfx.h"
|
|
|
|
|
2016-01-23 04:10:36 +01:00
|
|
|
#if defined(__MACH__) || defined(__NetBSD__)
|
2010-03-02 18:19:43 +01:00
|
|
|
typedef void (*sighandler_t)(int);
|
|
|
|
#endif
|
|
|
|
|
2004-02-03 03:55:24 +01:00
|
|
|
int stripped = NO_GRAPHICS;
|
2009-10-18 13:44:47 +02:00
|
|
|
static int got_sigwinch = 0;
|
|
|
|
|
2009-10-18 20:24:22 +02:00
|
|
|
static int term_resizex = -1;
|
|
|
|
static int term_resizey = -1;
|
|
|
|
|
2009-10-18 13:44:47 +02:00
|
|
|
void
|
|
|
|
ttyplay_sigwinch_func(int sig)
|
|
|
|
{
|
|
|
|
signal(SIGWINCH, ttyplay_sigwinch_func);
|
|
|
|
got_sigwinch = 1;
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
|
|
|
struct timeval
|
|
|
|
timeval_diff (struct timeval tv1, struct timeval tv2)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
struct timeval diff;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
diff.tv_sec = tv2.tv_sec - tv1.tv_sec;
|
|
|
|
diff.tv_usec = tv2.tv_usec - tv1.tv_usec;
|
|
|
|
if (diff.tv_usec < 0)
|
|
|
|
{
|
|
|
|
diff.tv_sec--;
|
|
|
|
diff.tv_usec += 1000000;
|
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return diff;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval
|
|
|
|
timeval_div (struct timeval tv1, double n)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
double x = ((double) tv1.tv_sec + (double) tv1.tv_usec / 1000000.0) / n;
|
|
|
|
struct timeval div;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
div.tv_sec = (int) x;
|
|
|
|
div.tv_usec = (x - (int) x) * 1000000;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return div;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
ttywait (struct timeval prev, struct timeval cur, double speed)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
struct timeval diff = timeval_diff (prev, cur);
|
|
|
|
|
|
|
|
assert (speed != 0);
|
|
|
|
diff = timeval_div (diff, speed);
|
|
|
|
|
2004-02-10 22:19:49 +01:00
|
|
|
select (1, NULL, NULL, NULL, &diff); /* skip if a user hits any key */
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return speed;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
ttynowait (struct timeval prev, struct timeval cur, double speed)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
return 0; /* Speed isn't important. */
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
2010-03-02 18:42:24 +01:00
|
|
|
int
|
|
|
|
kbhit(void)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
nodelay(stdscr, TRUE);
|
|
|
|
timeout(0);
|
|
|
|
i = wgetch(stdscr);
|
|
|
|
nodelay(stdscr, FALSE);
|
|
|
|
|
|
|
|
if (i == -1)
|
|
|
|
i = 0;
|
|
|
|
else
|
|
|
|
ungetch(i);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ttyplay_keyboard_action(int c)
|
|
|
|
{
|
|
|
|
struct termios t;
|
|
|
|
switch (c)
|
|
|
|
{
|
2010-05-05 13:32:37 +02:00
|
|
|
case ERR:
|
2010-03-02 18:42:24 +01:00
|
|
|
case 'q':
|
|
|
|
return READ_QUIT;
|
|
|
|
case 'r':
|
|
|
|
if (term_resizex > 0 && term_resizey > 0) {
|
|
|
|
printf ("\033[8;%d;%dt", term_resizey, term_resizex);
|
|
|
|
return READ_RESTART;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
switch (stripped)
|
|
|
|
{
|
|
|
|
case NO_GRAPHICS: populate_gfx_array ((stripped = DEC_GRAPHICS)); break;
|
|
|
|
case DEC_GRAPHICS: populate_gfx_array ((stripped = IBM_GRAPHICS)); break;
|
|
|
|
case IBM_GRAPHICS: populate_gfx_array ((stripped = NO_GRAPHICS)); break;
|
|
|
|
}
|
|
|
|
return READ_RESTART;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
tcgetattr (0, &t);
|
|
|
|
if (!loggedin)
|
|
|
|
{
|
|
|
|
initcurses();
|
|
|
|
loginprompt(1);
|
|
|
|
}
|
|
|
|
if (loggedin)
|
|
|
|
{
|
|
|
|
initcurses ();
|
|
|
|
domailuser (chosen_name);
|
|
|
|
}
|
|
|
|
endwin ();
|
|
|
|
tcsetattr (0, TCSANOW, &t);
|
|
|
|
return READ_RESTART;
|
|
|
|
case '?':
|
|
|
|
tcgetattr (0, &t);
|
|
|
|
initcurses();
|
|
|
|
(void) runmenuloop(dgl_find_menu("watchmenu_help"));
|
|
|
|
endwin ();
|
|
|
|
tcsetattr (0, TCSANOW, &t);
|
|
|
|
return READ_RESTART;
|
|
|
|
}
|
|
|
|
return (READ_DATA);
|
|
|
|
}
|
|
|
|
|
2003-12-30 22:30:32 +01:00
|
|
|
int
|
|
|
|
ttyread (FILE * fp, Header * h, char **buf, int pread)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
long offset;
|
2010-05-05 19:13:18 +02:00
|
|
|
int kb = kbhit();
|
|
|
|
|
|
|
|
if (kb == ERR) return READ_QUIT;
|
|
|
|
else if (kb) {
|
|
|
|
const int c = dgl_getch();
|
|
|
|
const int action = ttyplay_keyboard_action(c);
|
|
|
|
if (action != READ_DATA)
|
|
|
|
return (action);
|
|
|
|
}
|
2010-03-02 18:42:24 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
/* do this BEFORE header read, hlen bug */
|
|
|
|
offset = ftell (fp);
|
|
|
|
|
|
|
|
if (read_header (fp, h) == 0)
|
|
|
|
{
|
2004-01-30 22:28:38 +01:00
|
|
|
return READ_EOF;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* length should never be longer than one BUFSIZ */
|
|
|
|
if (h->len > BUFSIZ)
|
|
|
|
{
|
2004-01-30 22:28:38 +01:00
|
|
|
fprintf (stderr, "h->len too big (%ld) limit %ld\n",
|
|
|
|
(long)h->len, (long)BUFSIZ);
|
2010-05-05 13:32:37 +02:00
|
|
|
return READ_QUIT;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
2009-10-18 14:05:16 +02:00
|
|
|
*buf = malloc (h->len + 1);
|
2003-12-31 20:47:49 +01:00
|
|
|
if (*buf == NULL)
|
|
|
|
{
|
|
|
|
perror ("malloc");
|
2010-05-05 13:32:37 +02:00
|
|
|
return READ_QUIT;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fread (*buf, 1, h->len, fp) != h->len)
|
|
|
|
{
|
|
|
|
fseek (fp, offset, SEEK_SET);
|
2004-01-30 22:28:38 +01:00
|
|
|
return READ_EOF;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2009-10-18 14:05:16 +02:00
|
|
|
(*buf)[h->len] = 0;
|
2004-01-30 22:28:38 +01:00
|
|
|
return READ_DATA;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ttypread (FILE * fp, Header * h, char **buf, int pread)
|
|
|
|
{
|
2004-08-23 16:00:29 +02:00
|
|
|
int n;
|
|
|
|
#ifdef HAVE_KQUEUE
|
|
|
|
struct kevent evt[2];
|
|
|
|
static int kq = -1;
|
2004-08-28 23:30:54 +02:00
|
|
|
#endif
|
2004-08-23 16:00:29 +02:00
|
|
|
struct timeval w = { 0, 100000 };
|
2007-03-31 18:21:58 +02:00
|
|
|
struct timeval origw = { 0, 100000 };
|
2003-12-31 20:47:49 +01:00
|
|
|
int counter = 0;
|
|
|
|
fd_set readfs;
|
2004-08-23 16:00:29 +02:00
|
|
|
int doread = 0;
|
2010-03-02 18:42:24 +01:00
|
|
|
int action = READ_DATA;
|
2004-08-23 16:00:29 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_KQUEUE
|
|
|
|
if (kq == -1)
|
|
|
|
kq = kqueue ();
|
|
|
|
if (kq == -1)
|
|
|
|
{
|
|
|
|
printf ("kqueue() failed.\n");
|
2010-05-05 13:32:37 +02:00
|
|
|
return READ_QUIT;
|
2004-08-23 16:00:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
2003-12-31 20:47:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read persistently just like tail -f.
|
|
|
|
*/
|
2010-03-02 18:42:24 +01:00
|
|
|
while ((action = ttyread (fp, h, buf, 1)) == READ_EOF)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2010-03-02 19:32:57 +01:00
|
|
|
idle_alarm_reset();
|
2004-08-23 00:32:10 +02:00
|
|
|
fflush(stdout);
|
2003-12-31 20:47:49 +01:00
|
|
|
clearerr (fp);
|
2004-08-23 16:00:29 +02:00
|
|
|
#ifdef HAVE_KQUEUE
|
2004-08-28 23:30:54 +02:00
|
|
|
n = -1;
|
|
|
|
if (kq != -2)
|
|
|
|
{
|
|
|
|
EV_SET (&evt[0], STDIN_FILENO, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, NULL);
|
|
|
|
EV_SET (&evt[1], fileno (fp), EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, NULL);
|
|
|
|
n = kevent (kq, evt, 2, evt, 1, NULL);
|
|
|
|
doread = (n >= 1 && evt[0].ident == STDIN_FILENO &&
|
|
|
|
evt[0].filter == EVFILT_READ) ||
|
|
|
|
(n >= 2 && evt[1].ident == STDIN_FILENO &&
|
|
|
|
evt[1].filter == EVFILT_READ);
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Perhaps kevent(2) doesn't work on this fstype,
|
|
|
|
* use select(2) instead. Never use kevent again, assuming all
|
|
|
|
* active ttyrecs are on the same fstype.
|
|
|
|
*/
|
|
|
|
close(kq);
|
|
|
|
kq = -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n == -1)
|
2004-08-23 16:00:29 +02:00
|
|
|
#endif
|
2004-08-28 23:30:54 +02:00
|
|
|
{
|
|
|
|
if (counter++ > (20 * 60 * 10))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The reason for this timeout is that the select() method uses
|
|
|
|
* some CPU in waiting. The kqueue() method does not do that, so it
|
|
|
|
* does not need the timeout.
|
|
|
|
*/
|
|
|
|
endwin ();
|
|
|
|
printf ("Exiting due to 20 minutes of inactivity.\n");
|
2010-05-05 13:32:37 +02:00
|
|
|
return READ_QUIT;
|
2004-08-28 23:30:54 +02:00
|
|
|
}
|
|
|
|
FD_ZERO (&readfs);
|
|
|
|
FD_SET (STDIN_FILENO, &readfs);
|
|
|
|
n = select (1, &readfs, NULL, NULL, &w);
|
2007-03-31 18:21:58 +02:00
|
|
|
w = origw;
|
2004-08-28 23:30:54 +02:00
|
|
|
doread = n >= 1 && FD_ISSET (0, &readfs);
|
|
|
|
}
|
2004-08-23 16:00:29 +02:00
|
|
|
if (n == -1)
|
|
|
|
{
|
2009-10-18 13:44:47 +02:00
|
|
|
if ((errno == EINTR) && got_sigwinch) {
|
|
|
|
got_sigwinch = 0;
|
|
|
|
return READ_RESTART;
|
|
|
|
} else {
|
|
|
|
printf("select()/kevent() failed.\n");
|
2010-05-05 13:32:37 +02:00
|
|
|
return READ_QUIT;
|
2009-10-18 13:44:47 +02:00
|
|
|
}
|
2004-08-23 16:00:29 +02:00
|
|
|
}
|
|
|
|
if (doread)
|
2004-08-23 14:43:59 +02:00
|
|
|
{ /* user hits a character? */
|
2010-03-02 19:32:57 +01:00
|
|
|
const int c = dgl_getch();
|
2010-03-02 18:42:24 +01:00
|
|
|
action = ttyplay_keyboard_action(c);
|
|
|
|
if (action != READ_DATA)
|
|
|
|
return action;
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-02 18:42:24 +01:00
|
|
|
return (action);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ttywrite (char *buf, int len)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
int i;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-02-02 21:43:12 +01:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2004-02-03 03:55:24 +01:00
|
|
|
if (stripped != NO_GRAPHICS)
|
|
|
|
buf[i] = strip_gfx (buf[i]);
|
2004-02-02 21:43:12 +01:00
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
fwrite (buf, 1, len, stdout);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ttynowrite (char *buf, int len)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
/* do nothing */
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
2004-01-30 22:28:38 +01:00
|
|
|
int
|
2003-12-30 22:30:32 +01:00
|
|
|
ttyplay (FILE * fp, double speed, ReadFunc read_func,
|
2003-12-31 20:47:49 +01:00
|
|
|
WriteFunc write_func, WaitFunc wait_func, off_t offset)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
int first_time = 1;
|
2004-01-30 22:28:38 +01:00
|
|
|
int r = READ_EOF;
|
2003-12-31 20:47:49 +01:00
|
|
|
struct timeval prev;
|
|
|
|
|
|
|
|
/* for dtype's attempt to get the last clrscr and playback from there */
|
2004-08-23 00:32:10 +02:00
|
|
|
if (offset != -1)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2004-01-23 19:21:07 +01:00
|
|
|
fseek (fp, offset, SEEK_SET);
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
Header h;
|
|
|
|
|
2004-01-30 22:28:38 +01:00
|
|
|
r = read_func (fp, &h, &buf, 0);
|
|
|
|
if (r != READ_DATA)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!first_time)
|
|
|
|
{
|
|
|
|
speed = wait_func (prev, h.tv, speed);
|
|
|
|
}
|
|
|
|
first_time = 0;
|
|
|
|
|
|
|
|
write_func (buf, h.len);
|
|
|
|
prev = h.tv;
|
|
|
|
free (buf);
|
|
|
|
}
|
2004-01-30 22:28:38 +01:00
|
|
|
return r;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
2004-08-23 14:30:24 +02:00
|
|
|
static off_t
|
2010-02-28 12:35:30 +01:00
|
|
|
find_last_string_in_file(FILE * fp, const char *seq)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2010-02-28 12:35:30 +01:00
|
|
|
char buf[512];
|
|
|
|
struct stat mystat;
|
|
|
|
off_t offset = 0L;
|
|
|
|
const long readsz = sizeof(buf);
|
|
|
|
int bytes_read = 0;
|
|
|
|
const int seqlen = strlen(seq);
|
|
|
|
const char *reset_pos = seq + seqlen - 1;
|
|
|
|
const char *match_pos = reset_pos;
|
|
|
|
|
|
|
|
fstat(fileno (fp), &mystat);
|
|
|
|
offset = mystat.st_size - readsz;
|
|
|
|
if (offset < 0)
|
|
|
|
offset = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
const char *search_pos = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2010-02-28 12:35:30 +01:00
|
|
|
fseeko(fp, offset, SEEK_SET);
|
|
|
|
bytes_read = fread(buf, 1, readsz, fp);
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2010-02-28 12:35:30 +01:00
|
|
|
if (bytes_read <= 0)
|
|
|
|
break;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2010-02-28 12:35:30 +01:00
|
|
|
search_pos = buf + bytes_read - 1;
|
|
|
|
while (search_pos >= buf)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
2010-02-28 12:35:30 +01:00
|
|
|
int matched = *search_pos == *match_pos;
|
|
|
|
if (!matched && match_pos != reset_pos)
|
|
|
|
{
|
|
|
|
match_pos = reset_pos;
|
|
|
|
matched = *search_pos == *match_pos;
|
|
|
|
}
|
|
|
|
if (matched)
|
|
|
|
{
|
|
|
|
if (match_pos == seq)
|
|
|
|
return offset + (search_pos - buf);
|
|
|
|
--match_pos;
|
|
|
|
}
|
|
|
|
--search_pos;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2010-02-28 12:35:30 +01:00
|
|
|
|
|
|
|
// If we've reached the start of the file, exit.
|
|
|
|
if (!offset)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset -= readsz;
|
|
|
|
if (offset < 0)
|
|
|
|
offset = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:35:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static off_t
|
|
|
|
find_seek_offset_clrscr (FILE * fp)
|
|
|
|
{
|
|
|
|
off_t raw_seek_offset = 0;
|
2010-09-13 18:18:22 +02:00
|
|
|
off_t raw_seek_offset2 = 0;
|
2010-02-28 12:35:30 +01:00
|
|
|
off_t seek_offset_clrscr;
|
|
|
|
|
|
|
|
raw_seek_offset = find_last_string_in_file(fp, "\033[2J");
|
2010-09-13 18:18:22 +02:00
|
|
|
raw_seek_offset2 = find_last_string_in_file(fp, "\033[H\033[J");
|
|
|
|
if (raw_seek_offset2>raw_seek_offset) raw_seek_offset=raw_seek_offset2;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-08-23 14:30:24 +02:00
|
|
|
seek_offset_clrscr = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
/* now find last filepos that is less than seek offset */
|
2004-01-23 19:21:07 +01:00
|
|
|
fseek (fp, 0, SEEK_SET);
|
2003-12-31 20:47:49 +01:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
Header h;
|
2010-02-28 12:35:30 +01:00
|
|
|
long offset;
|
2003-12-31 20:47:49 +01:00
|
|
|
|
2004-01-30 22:28:38 +01:00
|
|
|
if (ttyread (fp, &h, &buf, 0) != READ_DATA)
|
2003-12-31 20:47:49 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (buf);
|
2010-02-28 12:35:30 +01:00
|
|
|
|
|
|
|
offset = ftell(fp);
|
|
|
|
if (offset < raw_seek_offset)
|
|
|
|
seek_offset_clrscr = ftell (fp);
|
|
|
|
else
|
|
|
|
break;
|
2003-12-31 20:47:49 +01:00
|
|
|
}
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-08-23 14:30:24 +02:00
|
|
|
return seek_offset_clrscr;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
2004-08-23 00:32:10 +02:00
|
|
|
#if 0 /* not used anymore */
|
2003-12-30 22:30:32 +01:00
|
|
|
void
|
|
|
|
ttyskipall (FILE * fp)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
/*
|
|
|
|
* Skip all records.
|
|
|
|
*/
|
|
|
|
ttyplay (fp, 0, ttyread, ttynowrite, ttynowait, 0);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
2004-08-23 00:32:10 +02:00
|
|
|
#endif
|
2003-12-30 22:30:32 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
ttyplayback (FILE * fp, double speed, ReadFunc read_func, WaitFunc wait_func)
|
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
ttyplay (fp, speed, ttyread, ttywrite, wait_func, 0);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-01-30 22:28:38 +01:00
|
|
|
ttypeek (FILE * fp, double speed)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2004-01-30 22:28:38 +01:00
|
|
|
int r;
|
|
|
|
do
|
|
|
|
{
|
2004-08-23 00:32:10 +02:00
|
|
|
setvbuf (fp, NULL, _IOFBF, 0);
|
2010-03-02 18:42:24 +01:00
|
|
|
r = ttyplay(fp, 0, ttyread, ttywrite, ttynowait, find_seek_offset_clrscr(fp));
|
|
|
|
if (r == READ_EOF) {
|
|
|
|
clearerr (fp);
|
|
|
|
setvbuf (fp, NULL, _IONBF, 0);
|
|
|
|
fflush (stdout);
|
|
|
|
r = ttyplay (fp, speed, ttypread, ttywrite, ttynowait, -1);
|
|
|
|
}
|
2004-01-30 22:28:38 +01:00
|
|
|
} while (r == READ_RESTART);
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2009-10-18 20:24:22 +02:00
|
|
|
ttyplay_main (char *ttyfile, int mode, int resizex, int resizey)
|
2003-12-30 22:30:32 +01:00
|
|
|
{
|
2003-12-31 20:47:49 +01:00
|
|
|
double speed = 1.0;
|
|
|
|
ReadFunc read_func = ttyread;
|
|
|
|
WaitFunc wait_func = ttywait;
|
|
|
|
FILE *input = stdin;
|
|
|
|
struct termios old, new;
|
2009-10-18 13:44:47 +02:00
|
|
|
sighandler_t old_sigwinch;
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2004-02-23 04:55:21 +01:00
|
|
|
populate_gfx_array (stripped);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
input = efopen (ttyfile, "r");
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
tcgetattr (0, &old); /* Get current terminal state */
|
|
|
|
new = old; /* Make a copy */
|
|
|
|
new.c_lflag &= ~(ICANON | ECHO | ECHONL); /* unbuffered, no echo */
|
2004-01-16 17:36:33 +01:00
|
|
|
new.c_cc[VMIN] = 1;
|
|
|
|
new.c_cc[VTIME] = 0;
|
2003-12-31 20:47:49 +01:00
|
|
|
tcsetattr (0, TCSANOW, &new); /* Make it current */
|
2010-09-13 18:18:22 +02:00
|
|
|
raw();
|
2009-10-18 20:24:22 +02:00
|
|
|
|
|
|
|
if (resizex > 0 && resizey > 0) {
|
|
|
|
term_resizex = resizex;
|
|
|
|
term_resizey = resizey;
|
|
|
|
}
|
|
|
|
|
2009-10-18 13:44:47 +02:00
|
|
|
got_sigwinch = 0;
|
|
|
|
old_sigwinch = signal(SIGWINCH, ttyplay_sigwinch_func);
|
|
|
|
|
2004-01-30 22:28:38 +01:00
|
|
|
if (mode == 1)
|
|
|
|
ttypeek (input, speed);
|
|
|
|
else
|
|
|
|
ttyplayback (input, speed, read_func, wait_func);
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
tcsetattr (0, TCSANOW, &old); /* Return terminal state */
|
2004-08-23 14:30:24 +02:00
|
|
|
fclose (input);
|
2003-12-30 22:30:32 +01:00
|
|
|
|
2009-10-18 13:44:47 +02:00
|
|
|
if (old_sigwinch != SIG_ERR)
|
|
|
|
signal(SIGWINCH, old_sigwinch);
|
|
|
|
|
2009-10-18 20:24:22 +02:00
|
|
|
term_resizex = term_resizey = -1;
|
|
|
|
|
2010-05-05 13:37:59 +02:00
|
|
|
printf("\033[2J"); /* clear screen afterwards */
|
|
|
|
|
2003-12-31 20:47:49 +01:00
|
|
|
return 0;
|
2003-12-30 22:30:32 +01:00
|
|
|
}
|