summaryrefslogtreecommitdiffstats
path: root/src/tsnif-replay.c
blob: 2e4700ad9001a48ca3bea0279269eb99e2ed50d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

#include <stdio.h>
#include <stdio_ext.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/select.h>
#include <setjmp.h>
#include <string.h>
#include <signal.h>

#include "storage.h"
#include "misc.h"
#include "intf.h"

static int killed;
static char *filename;
static struct tsnif_storage_handle storage_handle;
static struct tsnif_storage_opts storage_opts = {
	.flags    = TSNIF_STORAGE_OPT_READ,
};

int tsnif_debug = 0;

static void usage()
{
	printf("tsnif-replay -f <file>\n");
	_exit(-1);
}

static void sig_handler(int sig)
{
	printf("killed\n");
	killed = 1;
}

static int get_args(int argc, char **argv)
{
	while (1) {
		int c;
		int option_index = 0;
		static struct option long_options[] = {
			{"file", required_argument, 0, 'f'},
			{"debug", no_argument, 0, 'd'},
			{"help", no_argument, 0, 'h'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "f:dh",
			long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'f':
			filename = optarg;
			break;

		case 'd':
			tsnif_debug = 1;
			break;

		case 'h':
			usage();
			break;

		default:
			printf("unknown option '%c'", c);
		} /* switch(c) */
	} /* while(1) */

	return 0;
}

static struct timeval get_timeout(struct timespec *ts_new,
				   struct timespec *ts_old)
{
	static struct timeval tv;

	tv.tv_sec = ts_new->tv_sec - ts_old->tv_sec;

	TSNIF_DEBUG("time new %ld:%ld\n", ts_new->tv_sec, ts_new->tv_nsec);
	TSNIF_DEBUG("time old %ld:%ld\n", ts_old->tv_sec, ts_old->tv_nsec);

	if (ts_new->tv_nsec >= ts_old->tv_nsec)
		tv.tv_usec = (ts_new->tv_nsec - ts_old->tv_nsec) / 1000;
	else {
		tv.tv_usec = (ts_new->tv_nsec + (1000000000 - ts_old->tv_nsec)) / 1000;
		if (tv.tv_sec)
			tv.tv_sec--;
	}

	TSNIF_DEBUG("time fin %ld %ld\n", tv.tv_sec, tv.tv_usec);
	return tv;
}

enum {
	ACTION_NEXT,
	ACTION_PREV,
	ACTION_PLAY,
	ACTION_FIRST,
	ACTION_TIMEOUT,
	ACTION_STOP,
	ACTION_RUN,
	ACTION_QUIT,
	ACTION_SLAVE,
	ACTION_MASTER,
};

static int display_slave = 1;
static int display_master= 0;
static int play = 1;

static int display_pty(int flags)
{
	if ((flags == TSNIF_FLAGS_PTY_SLAVE) &&
	    !display_slave)
		return 0;

	if ((flags == TSNIF_FLAGS_PTY_MASTER) &&
	    !display_master)
		return 0;

	return 1;
}

static int output(struct tsnif_storage_rec *rec)
{
	int type = tsnif_storage_term_type(&storage_handle);

	switch(type) {
	case TSNIF_TYPE_PTY:
		/* follow the user pty display settings */
		if (!display_pty(rec->flags))
			return 0;

		/* fallback intentionally */

	case TSNIF_TYPE_TTY:
	case TSNIF_TYPE_TTYS:
		fwrite(rec->ptr, rec->len, 1, stdout);
		fflush(NULL);
		break;

	default:
		printf("failed: got unknown terminal type %d\n", type);
	}

	return 0;
}

static int get_action(struct timeval *tv)
{
	int c, ret;
	jmp_buf env;
	fd_set rfds;

	setjmp(env);

	FD_ZERO(&rfds);
	FD_SET(0, &rfds);

	ret = select(1, &rfds, NULL, NULL, tv);

	/* select failed - finish replay */
	if (-1 == ret)
		return ACTION_QUIT;

	/* timeout - continue with next record */
	if (!ret)
		return ACTION_NEXT;

	/* WTF??? */
	if (!FD_ISSET(0, &rfds))
		return ACTION_QUIT;

	/* we got user input */
	c = getchar();

	if (c == '\033') {
		__fpurge(stdin);
		longjmp(env, 1);
	}

	switch(c) {
	case 'P' : return ACTION_PLAY;
	case 'p' : return ACTION_PREV;
	case 'n' : return ACTION_NEXT;
	case 'f' : return ACTION_FIRST;
	case 'q' : return ACTION_QUIT;
	case 't' : return ACTION_TIMEOUT;
	case 's' : return ACTION_SLAVE;
	case 'm' : return ACTION_MASTER;
	}

	longjmp(env, 1);
	return -1;
}

static int process(void)
{
	struct tsnif_storage_rec rec;
	int err, action;

	err = tsnif_storage_read(&storage_handle,
				TSNIF_STORAGE_READ_NEXT, &rec);
	if (err < 0)
		return err;

	do {
		struct timespec ts_prev;
		struct timeval tv_def, *tv = NULL;

		if (err != TSNIF_STORAGE_READ_EOF) {

			output(&rec);

			if (play) {
				tv = &tv_def;
				memset(tv, 0x0, sizeof(*tv));
				ts_prev = rec.time;

				err = tsnif_storage_read(&storage_handle,
						TSNIF_STORAGE_READ_NEXT, &rec);
				if (err < 0)
					break;

				*tv = get_timeout(&rec.time, &ts_prev);
			}
		}

		action = get_action(tv);

		switch(action) {
		case ACTION_PLAY:
			play = !play;
			break;

		case ACTION_PREV:
			err = tsnif_storage_read(&storage_handle,
				TSNIF_STORAGE_READ_PREV, &rec);
			if (err < 0)
				return err;

			/* we reached the first item, do not report
			 * XXX different code for first item ??? */
			if (err == TSNIF_STORAGE_READ_EOF)
				err = 0;

			/* stop play mode when going back */
			play = 0;
			break;

		case ACTION_SLAVE:
			display_slave = !display_slave;
			break;

		case ACTION_MASTER:
			display_master = !display_master;
			break;
		}

	} while(action != ACTION_QUIT);

	return 0;
}

int main(int argc, char **argv)
{
	int err, ret;
	jmp_buf env;

	if (get_args(argc, argv))
		usage();

	err = tsnif_storage_init(&storage_handle, &storage_opts, filename);
	if (err)
		return -1;

	if ((ret = setjmp(env))) {
		set_term(1);
		tsnif_storage_close(&storage_handle);

		printf("done err = %d\n", err);
		return err;
	}

	set_term(0);
        signal(SIGINT, sig_handler);

	err = process();

	longjmp(env, 1);
}